Skip to content

Commit

Permalink
feat(kafka): RHICOMPL-1893 Kafka SASL authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
vkrizan committed Jun 1, 2021
1 parent 17a828e commit 8f5a475
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
14 changes: 13 additions & 1 deletion app/producers/application_producer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,27 @@ def logger
end

def kafka_ca_cert
return unless Settings.kafka.security_protocol == 'ssl'
return unless %w[ssl sasl_ssl].include?(Settings.kafka.security_protocol)

File.read(Settings.kafka.ssl_ca_location)
end

def sasl_config
return unless Settings.kafka.security_protocol == 'sasl_ssl'

{
sasl_scram_username: Settings.kafka.sasl_username,
sasl_scram_password: Settings.kafka.sasl_password,
sasl_scram_mechanism: 'sha512'
}
end

def kafka_config
{}.tap do |config|
config[:client_id] = self::CLIENT_ID
config[:ssl_ca_cert] = kafka_ca_cert if kafka_ca_cert

config.merge!(sasl_config) if sasl_config
end
end

Expand Down
6 changes: 6 additions & 0 deletions config/racecar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@

config.security_protocol = Settings.kafka.security_protocol
config.ssl_ca_location = Settings.kafka.ssl_ca_location

if Settings.kafka.security_protocol == 'sasl_ssl'
config.sasl_username = Settings.kafka.sasl_username
config.sasl_password = Settings.kafka.sasl_password
config.sasl_mechanism = 'SCRAM-SHA-512'
end
end
2 changes: 2 additions & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ kafka:
brokers: ''
security_protocol: plaintext
ssl_ca_location:
sasl_username:
sasl_password:
kafka_consumer_topics:
inventory_events: 'platform.inventory.events'
kafka_producer_topics:
Expand Down
19 changes: 19 additions & 0 deletions test/producers/application_producer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@ class MockProducer < ApplicationProducer; end
assert_equal config, MockProducer.send(:kafka_config)
end

test 'handles SASL SSL settings' do
Settings.kafka.security_protocol = 'sasl_ssl'
Settings.kafka.ssl_ca_location = 'test/fixtures/files/test_ca.crt'
Settings.kafka.sasl_username = 'user'
Settings.kafka.sasl_password = 'youwish'

class MockProducer < ApplicationProducer; end

config = {
client_id: ApplicationProducer::CLIENT_ID,
ssl_ca_cert: "very secure\n",
sasl_scram_username: 'user',
sasl_scram_password: 'youwish',
sasl_scram_mechanism: 'sha512'
}

assert_equal config, MockProducer.send(:kafka_config)
end

test 'handles plaintext settings' do
Settings.kafka.security_protocol = 'plaintext'

Expand Down

0 comments on commit 8f5a475

Please sign in to comment.