github.com/Jeffail/benthos/v3@v3.65.0/lib/util/amqp/sasl/sasl.go (about) 1 package sasl 2 3 import ( 4 "fmt" 5 6 "github.com/Azure/go-amqp" 7 "github.com/Jeffail/benthos/v3/internal/docs" 8 ) 9 10 // ErrMechanismNotSupported is returned if a SASL mechanism was not recognized. 11 type ErrMechanismNotSupported string 12 13 // Error implements the standard error interface. 14 func (e ErrMechanismNotSupported) Error() string { 15 return fmt.Sprintf("SASL mechanism %v was not recognised", string(e)) 16 } 17 18 // Config contains configuration for SASL based authentication. 19 type Config struct { 20 Mechanism string `json:"mechanism" yaml:"mechanism"` 21 User string `json:"user" yaml:"user"` 22 Password string `json:"password" yaml:"password"` 23 } 24 25 // NewConfig returns a new SASL config for AMQP with default values. 26 func NewConfig() Config { 27 return Config{ 28 Mechanism: "none", 29 } 30 } 31 32 // FieldSpec returns specs for SASL fields. 33 func FieldSpec() docs.FieldSpec { 34 return docs.FieldAdvanced("sasl", "Enables SASL authentication.").WithChildren( 35 docs.FieldCommon("mechanism", "The SASL authentication mechanism to use.").HasAnnotatedOptions( 36 "none", "No SASL based authentication.", 37 "plain", "Plain text SASL authentication.", 38 ), 39 docs.FieldCommon("user", "A SASL plain text username. It is recommended that you use environment variables to populate this field.", "${USER}"), 40 docs.FieldCommon("password", "A SASL plain text password. It is recommended that you use environment variables to populate this field.", "${PASSWORD}"), 41 ) 42 } 43 44 // ToOptFns renders the sasl.Config options into amqp.ConnOption fns. 45 func (s Config) ToOptFns() ([]amqp.ConnOption, error) { 46 switch s.Mechanism { 47 case "plain": 48 return []amqp.ConnOption{ 49 amqp.ConnSASLPlain(s.User, s.Password), 50 }, nil 51 case "none": 52 return nil, nil 53 } 54 return nil, ErrMechanismNotSupported(s.Mechanism) 55 }