Sha256: 92ab1ed39bf02be15ef66bf843898d5fd2c72d1d06b3f581167d09a6b6c3f788

Contents?: true

Size: 1.39 KB

Versions: 12

Compression:

Stored size: 1.39 KB

Contents

require 'kafka/sasl/plain'
require 'kafka/sasl/gssapi'
require 'kafka/sasl/scram'

module Kafka
  class SaslAuthenticator
    def initialize(logger:, sasl_gssapi_principal:, sasl_gssapi_keytab:,
                   sasl_plain_authzid:, sasl_plain_username:, sasl_plain_password:,
                   sasl_scram_username:, sasl_scram_password:, sasl_scram_mechanism:)
      @logger = logger

      @plain = Sasl::Plain.new(
        authzid: sasl_plain_authzid,
        username: sasl_plain_username,
        password: sasl_plain_password,
        logger: @logger,
      )

      @gssapi = Sasl::Gssapi.new(
        principal: sasl_gssapi_principal,
        keytab: sasl_gssapi_keytab,
        logger: @logger,
      )

      @scram = Sasl::Scram.new(
        username: sasl_scram_username,
        password: sasl_scram_password,
        mechanism: sasl_scram_mechanism,
        logger: @logger,
      )
    end

    def authenticate!(connection)
      mechanism = [@gssapi, @plain, @scram].find(&:configured?)

      return if mechanism.nil?

      ident = mechanism.ident
      response = connection.send_request(Kafka::Protocol::SaslHandshakeRequest.new(ident))

      unless response.error_code == 0 && response.enabled_mechanisms.include?(ident)
        raise Kafka::Error, "#{ident} is not supported."
      end

      mechanism.authenticate!(connection.to_s, connection.encoder, connection.decoder)
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
ruby-kafka-0.6.0.beta1 lib/kafka/sasl_authenticator.rb
ruby-kafka-0.5.5 lib/kafka/sasl_authenticator.rb
ruby-kafka-0.5.4 lib/kafka/sasl_authenticator.rb
ruby-kafka-0.5.4.beta1 lib/kafka/sasl_authenticator.rb
ruby-kafka-0.5.3 lib/kafka/sasl_authenticator.rb
ruby-kafka-0.5.2 lib/kafka/sasl_authenticator.rb
ruby-kafka-0.5.2.beta3 lib/kafka/sasl_authenticator.rb
ruby-kafka-0.5.2.beta2 lib/kafka/sasl_authenticator.rb
ruby-kafka-0.5.2.beta1 lib/kafka/sasl_authenticator.rb
ruby-kafka-0.5.1 lib/kafka/sasl_authenticator.rb
ruby-kafka-0.5.1.beta2 lib/kafka/sasl_authenticator.rb
ruby-kafka-0.5.1.beta1 lib/kafka/sasl_authenticator.rb