-
Notifications
You must be signed in to change notification settings - Fork 7
/
options.go
56 lines (51 loc) · 1.33 KB
/
options.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
package amqp
import (
"crypto/tls"
"crypto/x509"
"fmt"
"github.com/Azure/go-amqp"
"strings"
"github.com/kubemq-io/kubemq-targets/config"
)
type options struct {
url string
username string
password string
caCert string
insecure bool
}
func parseOptions(cfg config.Spec) (options, error) {
o := options{}
var err error
o.url, err = cfg.Properties.MustParseString("url")
if err != nil {
return options{}, fmt.Errorf("error parsing url, %w", err)
}
o.username = cfg.Properties.ParseString("username", "")
o.password = cfg.Properties.ParseString("password", "")
o.caCert = cfg.Properties.ParseString("ca_cert", "")
o.insecure = cfg.Properties.ParseBool("skip_insecure", false)
return o, nil
}
func (o options) getConnOptions() (*amqp.ConnOptions, error) {
connOptions := &amqp.ConnOptions{}
if strings.HasPrefix(o.url, "amqps://") {
tlsCfg := &tls.Config{
InsecureSkipVerify: o.insecure,
}
if o.caCert != "" {
caCertPool := x509.NewCertPool()
if !caCertPool.AppendCertsFromPEM([]byte(o.caCert)) {
return nil, fmt.Errorf("error loading Root CA Cert")
}
tlsCfg.RootCAs = caCertPool
}
connOptions.TLSConfig = tlsCfg
}
if o.username != "" {
connOptions.SASLType = amqp.SASLTypePlain(o.username, o.password)
} else {
connOptions.SASLType = amqp.SASLTypeAnonymous()
}
return connOptions, nil
}