Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added one custom authenticator. #5637

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion pkg/cassandra/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,28 @@ type Configuration struct {

// Authenticator holds the authentication properties needed to connect to a Cassandra cluster
type Authenticator struct {
Basic BasicAuthenticator `yaml:"basic" mapstructure:",squash"`
Basic BasicAuthenticator `yaml:"basic" mapstructure:",squash"`
CustomAuthenticator string `yaml:"custom_authenticator" mapstructure:"custom_authenticator"`
// TODO: add more auth types
}

type DsePlainTextAuthenticator struct {
Username string
Password string
}

func (a DsePlainTextAuthenticator) Challenge(req []byte) ([]byte, gocql.Authenticator, error) {
return []byte(fmt.Sprintf("\x00%s\x00%s", a.Username, a.Password)), nil, nil
}

func (a DsePlainTextAuthenticator) InitialResponse() ([]byte, error) {
return nil, nil
}

func (a DsePlainTextAuthenticator) Success(data []byte) error {
return nil
}

// BasicAuthenticator holds the username and password for a password authenticator for a Cassandra cluster
type BasicAuthenticator struct {
Username string `yaml:"username" mapstructure:"username"`
Expand Down Expand Up @@ -146,7 +164,14 @@ func (c *Configuration) NewCluster(logger *zap.Logger) (*gocql.ClusterConfig, er
Username: c.Authenticator.Basic.Username,
Password: c.Authenticator.Basic.Password,
}
} else if c.Authenticator.CustomAuthenticator != "" {
auth, err := getCustomAuthenticator(c.Authenticator.CustomAuthenticator, c.Authenticator.Basic.Username, c.Authenticator.Basic.Password)
if err != nil {
return nil, fmt.Errorf("could not create custom authenticator: %v", err)
}
cluster.Authenticator = auth
}

tlsCfg, err := c.TLS.Config(logger)
if err != nil {
return nil, err
Expand Down Expand Up @@ -176,3 +201,20 @@ func (c *Configuration) Validate() error {
_, err := govalidator.ValidateStruct(c)
return err
}

func getCustomAuthenticator(authenticatorName, username, password string) (gocql.Authenticator, error) {
switch authenticatorName {
case "Password":
return gocql.PasswordAuthenticator{
Username: username,
Password: password,
}, nil
case "DsePlainText":
return DsePlainTextAuthenticator{
Username: username,
Password: password,
}, nil
default:
return nil, fmt.Errorf("unsupported custom authenticator: %s", authenticatorName)
}
}