This repository has been archived by the owner on Jan 12, 2023. It is now read-only.
forked from cortexproject/cortex
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add option to configure approved password authenticators in Cassandra
Signed-off-by: Jacob Lisi <[email protected]>
- Loading branch information
Showing
3 changed files
with
69 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package cassandra | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/gocql/gocql" | ||
) | ||
|
||
// CustomPasswordAuthenticator provides the default behaviour for Username/Password authentication with | ||
// Cassandra while allowing users to specify a non-default Authenticator to accept. | ||
type CustomPasswordAuthenticator struct { | ||
ApprovedAuthenticators []string | ||
Username string | ||
Password string | ||
} | ||
|
||
func (p CustomPasswordAuthenticator) approve(authenticator string) bool { | ||
for _, s := range p.ApprovedAuthenticators { | ||
if authenticator == s { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// Challenge verifies the name of the authenticator and formats the provided username and password | ||
// into a response | ||
func (p CustomPasswordAuthenticator) Challenge(req []byte) ([]byte, gocql.Authenticator, error) { | ||
if !p.approve(string(req)) { | ||
return nil, nil, fmt.Errorf("unexpected authenticator %q", req) | ||
} | ||
resp := make([]byte, 2+len(p.Username)+len(p.Password)) | ||
resp[0] = 0 | ||
copy(resp[1:], p.Username) | ||
resp[len(p.Username)+1] = 0 | ||
copy(resp[2+len(p.Username):], p.Password) | ||
return resp, nil, nil | ||
} | ||
|
||
// Success returns nil by default, identical to the default PasswordAuthenticator | ||
func (p CustomPasswordAuthenticator) Success(data []byte) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters