Skip to content

Commit c3941e1

Browse files
authored
Merge pull request #1473 from intellitrend-team/fix-nsqauth-tls-root-ca-file
nsqd: use --tls-root-ca-file in nsqauth request
2 parents 2a5fb3e + eb27dd5 commit c3941e1

3 files changed

Lines changed: 38 additions & 9 deletions

File tree

internal/auth/authorizations.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package auth
22

33
import (
4+
"crypto/tls"
45
"errors"
56
"fmt"
67
"math/rand"
@@ -75,13 +76,13 @@ func (a *State) IsExpired() bool {
7576
}
7677

7778
func QueryAnyAuthd(authd []string, remoteIP string, tlsEnabled bool, commonName string, authSecret string,
78-
connectTimeout time.Duration, requestTimeout time.Duration) (*State, error) {
79+
clientTLSConfig *tls.Config, connectTimeout time.Duration, requestTimeout time.Duration) (*State, error) {
7980
var retErr error
8081
start := rand.Int()
8182
n := len(authd)
8283
for i := 0; i < n; i++ {
8384
a := authd[(i+start)%n]
84-
authState, err := QueryAuthd(a, remoteIP, tlsEnabled, commonName, authSecret, connectTimeout, requestTimeout)
85+
authState, err := QueryAuthd(a, remoteIP, tlsEnabled, commonName, authSecret, clientTLSConfig, connectTimeout, requestTimeout)
8586
if err != nil {
8687
es := fmt.Sprintf("failed to auth against %s - %s", a, err)
8788
if retErr != nil {
@@ -96,7 +97,7 @@ func QueryAnyAuthd(authd []string, remoteIP string, tlsEnabled bool, commonName
9697
}
9798

9899
func QueryAuthd(authd string, remoteIP string, tlsEnabled bool, commonName string, authSecret string,
99-
connectTimeout time.Duration, requestTimeout time.Duration) (*State, error) {
100+
clientTLSConfig *tls.Config, connectTimeout time.Duration, requestTimeout time.Duration) (*State, error) {
100101
v := url.Values{}
101102
v.Set("remote_ip", remoteIP)
102103
if tlsEnabled {
@@ -115,7 +116,7 @@ func QueryAuthd(authd string, remoteIP string, tlsEnabled bool, commonName strin
115116
}
116117

117118
var authState State
118-
client := http_api.NewClient(nil, connectTimeout, requestTimeout)
119+
client := http_api.NewClient(clientTLSConfig, connectTimeout, requestTimeout)
119120
if err := client.GETV1(endpoint, &authState); err != nil {
120121
return nil, err
121122
}

nsqd/client_v2.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,7 @@ func (c *clientV2) QueryAuthd() error {
657657

658658
authState, err := auth.QueryAnyAuthd(c.nsqd.getOpts().AuthHTTPAddresses,
659659
remoteIP, tlsEnabled, commonName, c.AuthSecret,
660+
c.nsqd.clientTLSConfig,
660661
c.nsqd.getOpts().HTTPClientConnectTimeout,
661662
c.nsqd.getOpts().HTTPClientRequestTimeout)
662663
if err != nil {

nsqd/nsqd.go

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,12 @@ type NSQD struct {
5757

5858
lookupPeers atomic.Value
5959

60-
tcpServer *tcpServer
61-
tcpListener net.Listener
62-
httpListener net.Listener
63-
httpsListener net.Listener
64-
tlsConfig *tls.Config
60+
tcpServer *tcpServer
61+
tcpListener net.Listener
62+
httpListener net.Listener
63+
httpsListener net.Listener
64+
tlsConfig *tls.Config
65+
clientTLSConfig *tls.Config
6566

6667
poolSize int
6768

@@ -128,6 +129,12 @@ func New(opts *Options) (*NSQD, error) {
128129
}
129130
n.tlsConfig = tlsConfig
130131

132+
clientTLSConfig, err := buildClientTLSConfig(opts)
133+
if err != nil {
134+
return nil, fmt.Errorf("failed to build client TLS config - %s", err)
135+
}
136+
n.clientTLSConfig = clientTLSConfig
137+
131138
for _, v := range opts.E2EProcessingLatencyPercentiles {
132139
if v <= 0 || v > 1 {
133140
return nil, fmt.Errorf("invalid E2E processing latency percentile: %v", v)
@@ -759,6 +766,26 @@ func buildTLSConfig(opts *Options) (*tls.Config, error) {
759766
return tlsConfig, nil
760767
}
761768

769+
func buildClientTLSConfig(opts *Options) (*tls.Config, error) {
770+
tlsConfig := &tls.Config{
771+
MinVersion: opts.TLSMinVersion,
772+
}
773+
774+
if opts.TLSRootCAFile != "" {
775+
tlsCertPool := x509.NewCertPool()
776+
caCertFile, err := os.ReadFile(opts.TLSRootCAFile)
777+
if err != nil {
778+
return nil, err
779+
}
780+
if !tlsCertPool.AppendCertsFromPEM(caCertFile) {
781+
return nil, errors.New("failed to append certificate to pool")
782+
}
783+
tlsConfig.RootCAs = tlsCertPool
784+
}
785+
786+
return tlsConfig, nil
787+
}
788+
762789
func (n *NSQD) IsAuthEnabled() bool {
763790
return len(n.getOpts().AuthHTTPAddresses) != 0
764791
}

0 commit comments

Comments
 (0)