/
sasl.go
59 lines (50 loc) · 2.09 KB
/
sasl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Package shared contains docs fields that need to be shared across old and new
// component implementations, it needs to be separate from the parent package in
// order to avoid circular dependencies (for now).
package shared
import (
"fmt"
"github.com/Azure/go-amqp"
"github.com/benthosdev/benthos/v4/internal/docs"
)
// ErrSASLMechanismNotSupported is returned if a SASL mechanism was not recognized.
type ErrSASLMechanismNotSupported string
// Error implements the standard error interface.
func (e ErrSASLMechanismNotSupported) Error() string {
return fmt.Sprintf("SASL mechanism %v was not recognised", string(e))
}
// SASLConfig contains configuration for SASL based authentication.
type SASLConfig struct {
Mechanism string `json:"mechanism" yaml:"mechanism"`
User string `json:"user" yaml:"user"`
Password string `json:"password" yaml:"password"`
}
// NewSASLConfig returns a new SASL config for AMQP with default values.
func NewSASLConfig() SASLConfig {
return SASLConfig{
Mechanism: "none",
}
}
// SASLFieldSpec returns specs for SASL fields.
func SASLFieldSpec() docs.FieldSpec {
return docs.FieldObject("sasl", "Enables SASL authentication.").WithChildren(
docs.FieldString("mechanism", "The SASL authentication mechanism to use.").HasAnnotatedOptions(
"none", "No SASL based authentication.",
"plain", "Plain text SASL authentication.",
).HasDefault("none"),
docs.FieldString("user", "A SASL plain text username. It is recommended that you use environment variables to populate this field.", "${USER}").HasDefault(""),
docs.FieldString("password", "A SASL plain text password. It is recommended that you use environment variables to populate this field.", "${PASSWORD}").HasDefault(""),
).Advanced().HasDefault(map[string]interface{}{})
}
// ToOptFns renders the sasl.Config options into amqp.ConnOption fns.
func (s SASLConfig) ToOptFns() ([]amqp.ConnOption, error) {
switch s.Mechanism {
case "plain":
return []amqp.ConnOption{
amqp.ConnSASLPlain(s.User, s.Password),
}, nil
case "none":
return nil, nil
}
return nil, ErrSASLMechanismNotSupported(s.Mechanism)
}