Skip to content

Conversation

spuun
Copy link
Contributor

@spuun spuun commented Nov 2, 2017

Added a SASL SCRAM authenticator.

Instead of adding even more parameters to Client constructor it's now possible to inject an authenticator. This makes it possible to create custom authenticators without modifing ruby-kafka. To make this possible the mechanism validation in SaslHandshakeRequest had to be removed. Possible authentication mechanism is validated by the broker so this shouldn't be needed anyway.

It's also possible to inject a SSL context to the Client constructor.

@dasch
Copy link
Contributor

dasch commented Nov 2, 2017

I'd rather keep adding parameters to the initializer – this was a deliberate design choice that may make the code a bit harder to follow, but would be simpler for most users of the library.

raise FailedScramAuthentication, 'Invalid server signature' if response['v'] != server_signature
rescue FailedScramAuthentication
raise
rescue => e
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd like to avoid catch-all rescues.

@spuun
Copy link
Contributor Author

spuun commented Nov 2, 2017

I've changed the code to use new parameters in the initializer. I also put back the validation in SaslHandshakeRequest.

README.md Outdated
```ruby
kafka = Kafka.new(
sasl_scram_username: 'username',
sasl_scram_password: 'password,
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing an end quite there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh! Forgot to validate docs..

README.md Outdated
kafka = Kafka.new(
sasl_scram_username: 'username',
sasl_scram_password: 'password,
sasl_scram_mechanism: Kafka::SCRAM_SHA256,
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd prefer a String name of the mechanism, e.g. "sha256", as that will make it a lot easier to integrate with config files and the like.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok. I can change that. Do you prefer "sha256" over "SHA-256" (the current value of the constant)?

Copy link
Contributor

Choose a reason for hiding this comment

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

I prefer "sha256".

lib/kafka.rb Outdated
@@ -225,6 +225,15 @@ class OffsetCommitError < Error
class FetchError < Error
end

class NoPartitionsAssignedError < Error
end
Copy link
Contributor

Choose a reason for hiding this comment

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

How did this sneak in here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have no idea... Maybe some merge i missed.

@@ -3,7 +3,7 @@ class SaslGssapiAuthenticator
GSSAPI_IDENT = "GSSAPI"
GSSAPI_CONFIDENTIALITY = false

def initialize(connection:, logger:, sasl_gssapi_principal:, sasl_gssapi_keytab:)
def initialize(conncetion:, logger:, sasl_gssapi_principal:, sasl_gssapi_keytab:)
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo here.


begin
msg = first_message
log_debug "[scram] Sending client's first message: #{msg}"
Copy link
Contributor

Choose a reason for hiding this comment

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

There's currently no precedent for "tagging" log lines with e.g. [scram]. Could you instead do Sending first SCRAM client message? The same for the rest of the log lines.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes

@dasch
Copy link
Contributor

dasch commented Nov 3, 2017

Looking good! Let me know when you need another round of review.

@spuun
Copy link
Contributor Author

spuun commented Nov 3, 2017

I've changed the mechanism configuration parameters to be 'sha256' and 'sha512'.

#
# @param sasl_scram_mechanism [String, nil] Scram mechanism ("sha256", "sha512")
#
# @param use_ssl [Booleanm false] Use SSL
Copy link
Contributor

Choose a reason for hiding this comment

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

Why add this parameter? The decision on whether to use SSL is already done here:

def build_ssl_context(ca_cert_file_path, ca_cert, client_cert, client_cert_key)
return nil unless ca_cert_file_path || ca_cert || client_cert || client_cert_key


sasl_authenticator = SaslAuthenticator.new(
sasl_authenticator ||= SaslAuthenticator.new(
Copy link
Contributor

Choose a reason for hiding this comment

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

The || can be removed.

client_id = request_decoder.string
loop do
request_bytes = decoder.bytes
request_data = Kafka::Protocol::Decoder.new(StringIO.new(request_bytes));
Copy link
Contributor

Choose a reason for hiding this comment

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

No reason for a semicolon here.

api_key = request_data.int16
_api_version = request_data.int16
correlation_id = request_data.int32
_client_id = request_data.string
Copy link
Contributor

Choose a reason for hiding this comment

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

No reason for underscores here.

@authenticating = true
@auth_mechanism = message
case api_key
when 17 then
Copy link
Contributor

Choose a reason for hiding this comment

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

No reason to switch to a case statement here.

@dasch
Copy link
Contributor

dasch commented Nov 8, 2017

Thanks for the hard work! ❤️

@dasch dasch merged commit 7d1618e into zendesk:master Nov 8, 2017
@dasch
Copy link
Contributor

dasch commented Nov 9, 2017

@spuun I've released v0.5.1.beta1 with this functionality – would you be able to deploy and test that version?

@spuun
Copy link
Contributor Author

spuun commented Nov 14, 2017

I've tested beta2 and it seems to work just fine! 👍

@dasch
Copy link
Contributor

dasch commented Nov 14, 2017

@spuun great, thanks!

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.

2 participants