|
| 1 | +# SASL Authentication |
| 2 | + |
| 3 | +KafkaEx supports SASL authentication for secure Kafka clusters. Multiple mechanisms are available with flexible configuration options. |
| 4 | + |
| 5 | +## Supported Mechanisms |
| 6 | + |
| 7 | +- **PLAIN** - Simple username/password (requires SSL/TLS) |
| 8 | +- **SCRAM-SHA-256** - Secure challenge-response authentication (Kafka 0.10.2+) |
| 9 | +- **SCRAM-SHA-512** - Secure challenge-response with stronger hash (Kafka 0.10.2+) |
| 10 | + |
| 11 | +## Configuration |
| 12 | + |
| 13 | +### Via Application Config |
| 14 | + |
| 15 | +```elixir |
| 16 | +# config/config.exs |
| 17 | +config :kafka_ex, |
| 18 | + brokers: [{"localhost", 9292}], |
| 19 | + use_ssl: true, |
| 20 | + ssl_options: [ |
| 21 | + verify: :verify_peer, |
| 22 | + cacertfile: "/path/to/ca-cert" |
| 23 | + ], |
| 24 | + sasl: %{ |
| 25 | + mechanism: :scram, |
| 26 | + username: System.get_env("KAFKA_USERNAME"), |
| 27 | + password: System.get_env("KAFKA_PASSWORD"), |
| 28 | + mechanism_opts: %{algo: :sha256} # :sha256 or :sha512 |
| 29 | + } |
| 30 | +``` |
| 31 | + |
| 32 | +### Via Worker Options |
| 33 | + |
| 34 | +```elixir |
| 35 | +opts = [ |
| 36 | + uris: [{"broker1", 9092}, {"broker2", 9092}], |
| 37 | + use_ssl: true, |
| 38 | + ssl_options: [verify: :verify_none], |
| 39 | + auth: KafkaEx.Auth.Config.new(%{ |
| 40 | + mechanism: :plain, |
| 41 | + username: "alice", |
| 42 | + password: "secret123" |
| 43 | + }) |
| 44 | +] |
| 45 | + |
| 46 | +{:ok, pid} = KafkaEx.create_worker(:my_worker, opts) |
| 47 | +``` |
| 48 | + |
| 49 | +### Docker Compose Setup |
| 50 | + |
| 51 | +The project includes Docker configurations for testing SASL authentication: |
| 52 | + |
| 53 | +```bash |
| 54 | +# Start Kafka with SASL enabled |
| 55 | +docker-compose up -d |
| 56 | + |
| 57 | +# Ports: |
| 58 | +# 9092 - No authentication (SSL) |
| 59 | +# 9192 - SASL/PLAIN (SSL) |
| 60 | +# 9292 - SASL/SCRAM (SSL) |
| 61 | +``` |
| 62 | + |
| 63 | +## Security Considerations |
| 64 | + |
| 65 | +- Always use SSL/TLS with PLAIN mechanism - plain text passwords must be encrypted in transit |
| 66 | +- Use environment variables for credentials - never hardcode passwords |
| 67 | +- SCRAM is preferred over PLAIN when both are available |
| 68 | + |
| 69 | +### Minimum Kafka Versions |
| 70 | + |
| 71 | +- PLAIN: Kafka 0.9.0+ |
| 72 | +- SCRAM: Kafka 0.10.2+ |
| 73 | + |
| 74 | +## Testing with Different Mechanisms |
| 75 | + |
| 76 | +```elixir |
| 77 | +# Test PLAIN authentication |
| 78 | +config :kafka_ex, |
| 79 | + brokers: [{"localhost", 9192}], |
| 80 | + use_ssl: true, |
| 81 | + ssl_options: [verify: :verify_none], |
| 82 | + sasl: %{mechanism: :plain, username: "test", password: "secret"} |
| 83 | + |
| 84 | +# Test SCRAM-SHA-256 |
| 85 | +config :kafka_ex, |
| 86 | + brokers: [{"localhost", 9292}], |
| 87 | + use_ssl: true, |
| 88 | + ssl_options: [verify: :verify_none], |
| 89 | + sasl: %{ |
| 90 | + mechanism: :scram, |
| 91 | + username: "test", |
| 92 | + password: "secret", |
| 93 | + mechanism_opts: %{algo: :sha256} |
| 94 | + } |
| 95 | +``` |
| 96 | + |
| 97 | +## Integration with Existing Code |
| 98 | + |
| 99 | +SASL authentication is transparent to the rest of your KafkaEx usage: |
| 100 | + |
| 101 | +```elixir |
| 102 | +# Once configured, use KafkaEx normally |
| 103 | +KafkaEx.metadata() |
| 104 | +KafkaEx.produce("my-topic", 0, "message") |
| 105 | +messages = KafkaEx.fetch("my-topic", 0, offset: 0) |
| 106 | +``` |
| 107 | + |
| 108 | +## Troubleshooting |
| 109 | + |
| 110 | +- **Connection refused**: Ensure you're using the correct port for SASL (9192 for PLAIN, 9292 for SCRAM in test setup) |
| 111 | +- **Authentication failed**: Check credentials and ensure the user exists in Kafka with the correct SASL mechanism configured |
| 112 | +- **SSL handshake error**: Verify SSL certificates or use verify: :verify_none for testing (not production!) |
| 113 | +- **Unsupported mechanism**: Ensure your Kafka version supports the mechanism (SCRAM requires 0.10.2+) |
| 114 | + |
| 115 | +## Advanced: Custom Authentication |
| 116 | + |
| 117 | +For OAuth or custom mechanisms, implement the `KafkaEx.Auth.Mechanism` behaviour: |
| 118 | + |
| 119 | +```elixir |
| 120 | +defmodule MyAuth do |
| 121 | + @behaviour KafkaEx.Auth.Mechanism |
| 122 | + |
| 123 | + def mechanism_name(_), do: "OAUTHBEARER" |
| 124 | + |
| 125 | + def authenticate(config, send_fun) do |
| 126 | + # Custom authentication logic |
| 127 | + :ok |
| 128 | + end |
| 129 | +end |
| 130 | +``` |
| 131 | + |
| 132 | +## Implementation Notes |
| 133 | + |
| 134 | +### Version Compatibility |
| 135 | + |
| 136 | +The SASL implementation handles different Kafka versions appropriately: |
| 137 | + |
| 138 | +- Kafka 0.9.x: Skips API versions call (not supported) |
| 139 | +- Kafka 0.10.0-0.10.1: Queries API versions, supports PLAIN only |
| 140 | +- Kafka 0.10.2+: Full support including SCRAM mechanisms |
| 141 | + |
| 142 | +### Technical Details |
| 143 | + |
| 144 | +- Authentication occurs immediately after socket creation |
| 145 | +- The implementation handles packet mode switching between raw and length-prefixed formats |
| 146 | +- Correlation IDs are used to match requests with responses |
| 147 | +- Server signatures are validated in SCRAM authentication |
| 148 | +- Passwords are never logged and are redacted in inspect output |
0 commit comments