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

feat: kafka sasl auth #1186

Merged
merged 21 commits into from
Apr 30, 2021
Merged

Conversation

joshuajorel
Copy link
Contributor

Checklist:

Test Cases:

  • Test Invalid Credentials (EventSource and Trigger)
  • Test Invalid SASL Mechanism (EventSource and Trigger)
  • Test SASL PLAIN Mechanism

Notes:

  • sarama library says that it only supports SASL/PLAIN, but has different mechanisms available in its codebase. I added the other authentication mechanisms just in case
  • This PR was only tested with the SASL PLAIN mechanism

// SASLMechanism is the name of the enabled SASL mechanism.
// Possible values: OAUTHBEARER, PLAIN (defaults to PLAIN).
Mechanism string `json:"mechanism,omitempty" protobuf:"bytes,1,opt,name=mechanism"`
// Version is the SASL Protocol Version to use
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments do not match the fields...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed comments.

User *corev1.SecretKeySelector `json:"user,omitempty" protobuf:"bytes,2,opt,name=user"`
// Password for SASL/PLAIN authentication
Password *corev1.SecretKeySelector `json:"password,omitempty" protobuf:"bytes,3,opt,name=password"`
// authz id used for SASL/SCRAM authentication
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is authz id?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove authz id. It wasn't supposed to be in there. Comments were lifted from the sarama library.


var mechanismSet, userSet, passwordSet bool

if saslConfig.Mechanism != "" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does not need !="" check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed !="" check

}

if !mechanismSet {
return errors.New("invalid sasl config, please configure Mechanism. Possible values are `OAUTHBEARER`, `PLAIN`, `SCRAM-SHA-256`, `SCRAM-SHA-512` and `GSSAPI`")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It says defaults to PLAIN above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed error message to account for the only possible values.

passwordSet = true
}

if !userSet && !passwordSet {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can either of them be missing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have been an OR statement. Will fix.

@@ -63,6 +63,24 @@ func NewKafkaTrigger(sensor *v1alpha1.Sensor, trigger *v1alpha1.Trigger, kafkaPr
config.Version = version
}

if kafkatrigger.SASL != nil {
config.Net.SASL.Enable = true
config.Net.SASL.Mechanism = sarama.SASLMechanism(kafkatrigger.SASL.Mechanism)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Validating?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checks whether the configuration is set. Enables SASL authentication if parameters are set in the YAML configs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added additional validation for empty strings.

@@ -461,28 +461,31 @@ type KafkaEventSource struct {
Topic string `json:"topic" protobuf:"bytes,3,opt,name=topic"`
// Backoff holds parameters applied to connection.
ConnectionBackoff *apicommon.Backoff `json:"connectionBackoff,omitempty" protobuf:"bytes,4,opt,name=connectionBackoff"`
// SASL configuration for the kafka client
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW - we should keep the order of existing fields, otherwise it's a breaking change for proto files, we should reduce that kind of changes.

https://developers.google.com/protocol-buffers/docs/pythontutorial#extending-a-protocol-buffer

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted the byte ordering and added the SASL configs to the end.

return nil
}

var mechanismSet, userSet, passwordSet bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kind of verbose.

switch saslConfig.GetMechanism() {
  case "PLAIN", xxxxxx:
  default:
    return errors.New("invalid sasl config. Possible xxxxxx")
}
if saslConfig.User == nil {
  return errors.New("xxx")
}
if saslConfig.Password == nil {
  return errors.New("xxx")
}
return nil

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed xxxSet flags. merged validating user and password into a single if statemetn.

@@ -499,20 +499,24 @@ type KafkaTrigger struct {
// Defaults to 500 milliseconds.
// +optional
FlushFrequency int32 `json:"flushFrequency,omitempty" protobuf:"varint,7,opt,name=flushFrequency"`
// SASL configuration for the kafka client
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, do not change the order.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted order.

if kafkaEventSource.SASL != nil {
config.Net.SASL.Enable = true

if kafkaEventSource.SASL.Mechanism == "" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A function func (s SASLConfig) GetMechanism() would be helpful to save if..else everywhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will refactor.

Copy link
Contributor Author

@joshuajorel joshuajorel Apr 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored both sensor and event source kafka configs. Function GetMechanism() will by default, will set mechanism to "PLAINTEXT" unless otherwise specified.

@@ -153,3 +167,13 @@ type Metadata struct {
Annotations map[string]string `json:"annotations,omitempty" protobuf:"bytes,1,rep,name=annotations"`
Labels map[string]string `json:"labels,omitempty" protobuf:"bytes,2,rep,name=labels"`
}

func (s SASLConfig) GetMechanism() sarama.SASLMechanism {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall everything looks good to me except this part.

  1. Returning sarama.SASLMechanism makes github.com/Shopify/sarama get introduced to the models, which is not good. Ideally the dependencies in models are either from native GoLang or k8s related. I prefer to return a string here.
  2. Another reason is, since this struct is put in common, it is intended to be used by not only Kafka. Assume it's going to be used by something else, sarma.SASLMechanism might not be recognized by it. Also, I made a little research on SASL auth, it looks like the mechanism is not unified, different product has different support (PLAIN might be the one everyone supports). Correct me if I'm wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Apologies for the oversight. Removed the type reference and refactored again to reflect on the actual sensor and eventsource.
  2. I'm basing the authentication mechanism on the available library which is Sarama. The other authentication mechanisms might be something to look into in the future, but "PLAIN" auth is a good place to start.

Copy link
Member

@whynowy whynowy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@whynowy whynowy merged commit 41027b5 into argoproj:master Apr 30, 2021
juliev0 pushed a commit to juliev0/argo-events that referenced this pull request Mar 29, 2022
* feat: SASL Auth for Kafka

Signed-off-by: Joshua Jorel Lee <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SASL authentication to Kafka
2 participants