-
Notifications
You must be signed in to change notification settings - Fork 337
SASL SCRAM support #465
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
Merged
Merged
SASL SCRAM support #465
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
caecb0d
Don't validate sasl type
spuun 984d451
Added sasl scram authenticator
spuun a54e9bb
Pass connection to authenticate! instead of constructor
spuun 14749ff
No need for connection to be instance variable
spuun 1a3112c
Added specs for scram
spuun 0be2562
Documented scram
spuun 8b3e751
Code formatting and clean up
spuun e08cfc4
Don't catch all errors
spuun 062c117
Refactorized to use parameters instead of DI
spuun 9fb20e1
Added missing quote to code example
spuun 216e5cf
Removed unused exception
spuun 16138a8
Fixed typo
spuun d954935
Changed log message
spuun 4f00aa6
Changed scram mechanism parameter values
spuun ce5c3bd
Updated scram code example
spuun a6233f4
Removed unwanted and unnecessary characters
spuun 609b840
Changed case to if and removed underscore from unused variables
spuun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,183 @@ | ||
require 'securerandom' | ||
require 'base64' | ||
|
||
module Kafka | ||
|
||
class SaslScramAuthenticator | ||
MECHANISMS = { | ||
sha256: { | ||
mechanism: 'SHA-256' | ||
}, | ||
sha512: { | ||
mechanism: 'SHA-512' | ||
} | ||
}.freeze | ||
|
||
VALID_MECHANISMS = %w{sha256 sha512}.freeze | ||
|
||
def initialize(username, password, mechanism: 'sha256', logger: nil, connection:) | ||
unless VALID_MECHANISMS.include?(mechanism) | ||
raise Kafka::SaslScramError, "SCRAM mechanism #{mechanism} is not supported." | ||
end | ||
@username = username | ||
@password = password | ||
@mechanism = MECHANISMS[mechanism.to_sym][:mechanism] | ||
@logger = logger | ||
@connection = connection | ||
end | ||
|
||
def authenticate! | ||
response = @connection.send_request(Kafka::Protocol::SaslHandshakeRequest.new('SCRAM-' + @mechanism)) | ||
|
||
unless response.error_code == 0 && response.enabled_mechanisms.include?('SCRAM-' + @mechanism) | ||
raise Kafka::SaslScramError, "SCRAM-#{@mechanism} is not supported." | ||
end | ||
|
||
log_debug "Authenticating #{@username} with SASL SCRAM #{@mechanism}" | ||
|
||
@encoder = @connection.encoder | ||
@decoder = @connection.decoder | ||
|
||
begin | ||
msg = first_message | ||
log_debug "Sending first client SASL SCRAM message: #{msg}" | ||
@encoder.write_bytes(msg) | ||
|
||
@server_first_message = @decoder.bytes | ||
log_debug "Received first server SASL SCRAM message: #{@server_first_message}" | ||
|
||
msg = final_message | ||
log_debug "Sending final client SASL SCRAM message: #{msg}" | ||
@encoder.write_bytes(msg) | ||
|
||
response = parse_response(@decoder.bytes) | ||
log_debug "Received last server SASL SCRAM message: #{response}" | ||
|
||
raise FailedScramAuthentication, response['e'] if response['e'] | ||
raise FailedScramAuthentication, 'Invalid server signature' if response['v'] != server_signature | ||
rescue EOFError => e | ||
raise FailedScramAuthentication, e.message | ||
end | ||
log_debug "SASL SCRAM authentication successful" | ||
end | ||
|
||
private | ||
|
||
def log_debug(str) | ||
@logger.debug str if @logger | ||
end | ||
|
||
def first_message | ||
"n,,#{first_message_bare}" | ||
end | ||
|
||
def first_message_bare | ||
"n=#{encoded_username},r=#{nonce}" | ||
end | ||
|
||
def final_message_without_proof | ||
"c=biws,r=#{rnonce}" | ||
end | ||
|
||
def final_message | ||
"#{final_message_without_proof},p=#{client_proof}" | ||
end | ||
|
||
def server_data | ||
parse_response(@server_first_message) | ||
end | ||
|
||
def rnonce | ||
server_data['r'] | ||
end | ||
|
||
def salt | ||
Base64.strict_decode64(server_data['s']) | ||
end | ||
|
||
def iterations | ||
server_data['i'].to_i | ||
end | ||
|
||
def auth_message | ||
msg = [first_message_bare, @server_first_message, final_message_without_proof].join(',') | ||
end | ||
|
||
def salted_password | ||
hi(@password, salt, iterations) | ||
end | ||
|
||
def client_key | ||
hmac(salted_password, 'Client Key') | ||
end | ||
|
||
def stored_key | ||
h(client_key) | ||
end | ||
|
||
def server_key | ||
hmac(salted_password, 'Server Key') | ||
end | ||
|
||
def client_signature | ||
hmac(stored_key, auth_message) | ||
end | ||
|
||
def server_signature | ||
Base64.strict_encode64(hmac(server_key, auth_message)) | ||
end | ||
|
||
def client_proof | ||
Base64.strict_encode64(xor(client_key, client_signature)) | ||
end | ||
|
||
def h(str) | ||
digest.digest(str) | ||
end | ||
|
||
def hi(str, salt, iterations) | ||
OpenSSL::PKCS5.pbkdf2_hmac( | ||
str, | ||
salt, | ||
iterations, | ||
digest.size, | ||
digest | ||
) | ||
end | ||
|
||
def hmac(data, key) | ||
OpenSSL::HMAC.digest(digest, data, key) | ||
end | ||
|
||
def xor(first, second) | ||
first.bytes.zip(second.bytes).map { |(a, b)| (a ^ b).chr }.join('') | ||
end | ||
|
||
def parse_response(data) | ||
data.split(',').map { |s| s.split('=', 2) }.to_h | ||
end | ||
|
||
def encoded_username | ||
safe_str(@username.encode(Encoding::UTF_8)) | ||
end | ||
|
||
def nonce | ||
@nonce ||= SecureRandom.urlsafe_base64(32) | ||
end | ||
|
||
def digest | ||
@digest ||= case @mechanism | ||
when 'SHA-256' | ||
OpenSSL::Digest::SHA256.new.freeze | ||
when 'SHA-512' | ||
OpenSSL::Digest::SHA512.new.freeze | ||
else | ||
raise StandardError, "Unknown mechanism '#{@mechanism}'" | ||
end | ||
end | ||
|
||
def safe_str(val) | ||
val.gsub('=', '=3D').gsub(',', '=2C') | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
ruby-kafka/lib/kafka/client.rb
Lines 528 to 529 in a0e1d14