|
| 1 | +// Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +// license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright |
| 4 | +// ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +// the Apache License, Version 2.0 (the "License"); you may |
| 6 | +// not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package kafka |
| 19 | + |
| 20 | +import ( |
| 21 | + "fmt" |
| 22 | + "strings" |
| 23 | + |
| 24 | + "github.com/Shopify/sarama" |
| 25 | +) |
| 26 | + |
| 27 | +type SaslConfig struct { |
| 28 | + SaslMechanism string `config:"mechanism"` |
| 29 | +} |
| 30 | + |
| 31 | +const ( |
| 32 | + saslTypePlaintext = sarama.SASLTypePlaintext |
| 33 | + saslTypeSCRAMSHA256 = sarama.SASLTypeSCRAMSHA256 |
| 34 | + saslTypeSCRAMSHA512 = sarama.SASLTypeSCRAMSHA512 |
| 35 | +) |
| 36 | + |
| 37 | +func (c *SaslConfig) ConfigureSarama(config *sarama.Config) { |
| 38 | + switch strings.ToUpper(c.SaslMechanism) { // try not to force users to use all upper case |
| 39 | + case "": |
| 40 | + // SASL is not enabled |
| 41 | + return |
| 42 | + case saslTypePlaintext: |
| 43 | + config.Net.SASL.Mechanism = sarama.SASLMechanism(sarama.SASLTypePlaintext) |
| 44 | + case saslTypeSCRAMSHA256: |
| 45 | + config.Net.SASL.Handshake = true |
| 46 | + config.Net.SASL.Mechanism = sarama.SASLMechanism(sarama.SASLTypeSCRAMSHA256) |
| 47 | + config.Net.SASL.SCRAMClientGeneratorFunc = func() sarama.SCRAMClient { |
| 48 | + return &XDGSCRAMClient{HashGeneratorFcn: SHA256} |
| 49 | + } |
| 50 | + case saslTypeSCRAMSHA512: |
| 51 | + config.Net.SASL.Handshake = true |
| 52 | + config.Net.SASL.Mechanism = sarama.SASLMechanism(sarama.SASLTypeSCRAMSHA512) |
| 53 | + config.Net.SASL.SCRAMClientGeneratorFunc = func() sarama.SCRAMClient { |
| 54 | + return &XDGSCRAMClient{HashGeneratorFcn: SHA512} |
| 55 | + } |
| 56 | + default: |
| 57 | + // This should never happen because `SaslMechanism` is checked on `Validate()`, keeping a panic to detect it earlier if it happens. |
| 58 | + panic(fmt.Sprintf("not valid SASL mechanism '%v', only supported with PLAIN|SCRAM-SHA-512|SCRAM-SHA-256", c.SaslMechanism)) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func (c *SaslConfig) Validate() error { |
| 63 | + switch strings.ToUpper(c.SaslMechanism) { // try not to force users to use all upper case |
| 64 | + case "", saslTypePlaintext, saslTypeSCRAMSHA256, saslTypeSCRAMSHA512: |
| 65 | + default: |
| 66 | + return fmt.Errorf("not valid SASL mechanism '%v', only supported with PLAIN|SCRAM-SHA-512|SCRAM-SHA-256", c.SaslMechanism) |
| 67 | + } |
| 68 | + return nil |
| 69 | +} |
0 commit comments