-
Notifications
You must be signed in to change notification settings - Fork 754
/
sasl.go
55 lines (47 loc) · 1.73 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
package sasl
import (
"fmt"
"github.com/Azure/go-amqp"
"github.com/Jeffail/benthos/v3/internal/docs"
)
// ErrMechanismNotSupported is returned if a SASL mechanism was not recognized.
type ErrMechanismNotSupported string
// Error implements the standard error interface.
func (e ErrMechanismNotSupported) Error() string {
return fmt.Sprintf("SASL mechanism %v was not recognised", string(e))
}
// Config contains configuration for SASL based authentication.
type Config struct {
Mechanism string `json:"mechanism" yaml:"mechanism"`
User string `json:"user" yaml:"user"`
Password string `json:"password" yaml:"password"`
}
// NewConfig returns a new SASL config for AMQP with default values.
func NewConfig() Config {
return Config{
Mechanism: "none",
}
}
// FieldSpec returns specs for SASL fields.
func FieldSpec() docs.FieldSpec {
return docs.FieldAdvanced("sasl", "Enables SASL authentication.").WithChildren(
docs.FieldCommon("mechanism", "The SASL authentication mechanism to use.").HasAnnotatedOptions(
"none", "No SASL based authentication.",
"plain", "Plain text SASL authentication.",
),
docs.FieldCommon("user", "A SASL plain text username. It is recommended that you use environment variables to populate this field.", "${USER}"),
docs.FieldCommon("password", "A SASL plain text password. It is recommended that you use environment variables to populate this field.", "${PASSWORD}"),
)
}
// ToOptFns renders the sasl.Config options into amqp.ConnOption fns.
func (s Config) 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, ErrMechanismNotSupported(s.Mechanism)
}