Merge pull request #504 from tidwall/nats-tls

Added TLS support for Nats webhook provider
This commit is contained in:
Josh 2021-07-08 07:42:54 -07:00 committed by GitHub
commit d3d4694691
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 9 deletions

View File

@ -114,11 +114,15 @@ type Endpoint struct {
CreateQueue bool CreateQueue bool
} }
NATS struct { NATS struct {
Host string Host string
Port int Port int
User string User string
Pass string Pass string
Topic string Topic string
Token string
TLS bool
TLSCert string
TLSKey string
} }
Local struct { Local struct {
Channel string Channel string
@ -737,6 +741,14 @@ func parseEndpoint(s string) (Endpoint, error) {
endpoint.NATS.User = val[0] endpoint.NATS.User = val[0]
case "pass": case "pass":
endpoint.NATS.Pass = val[0] endpoint.NATS.Pass = val[0]
case "token":
endpoint.NATS.Token = val[0]
case "tls":
endpoint.NATS.TLS = queryBool(val[0])
case "tlscert":
endpoint.NATS.TLSCert = val[0]
case "tlskey":
endpoint.NATS.TLSKey = val[0]
} }
} }
} }

View File

@ -57,13 +57,21 @@ func (conn *NATSConn) Send(msg string) error {
} }
conn.t = time.Now() conn.t = time.Now()
if conn.conn == nil { if conn.conn == nil {
addr := fmt.Sprintf("nats://%s:%d", conn.ep.NATS.Host, conn.ep.NATS.Port) addr := fmt.Sprintf("%s:%d", conn.ep.NATS.Host, conn.ep.NATS.Port)
var err error var err error
var opts []nats.Option
if conn.ep.NATS.User != "" && conn.ep.NATS.Pass != "" { if conn.ep.NATS.User != "" && conn.ep.NATS.Pass != "" {
conn.conn, err = nats.Connect(addr, nats.UserInfo(conn.ep.NATS.User, conn.ep.NATS.Pass)) opts = append(opts, nats.UserInfo(conn.ep.NATS.User, conn.ep.NATS.Pass))
} else {
conn.conn, err = nats.Connect(addr)
} }
if conn.ep.NATS.TLS {
opts = append(opts, nats.ClientCert(
conn.ep.NATS.TLSCert, conn.ep.NATS.TLSKey,
))
}
if conn.ep.NATS.Token != "" {
opts = append(opts, nats.Token(conn.ep.NATS.Token))
}
conn.conn, err = nats.Connect(addr, opts...)
if err != nil { if err != nil {
conn.close() conn.close()
return err return err