Skip to content

Commit e3b6a75

Browse files
(2.12) [FIXED] Leaf node token auth (#7452)
2 parents 7885ebd + c1317fd commit e3b6a75

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

server/auth.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,8 @@ func (s *Server) processClientOrLeafAuthentication(c *client, opts *Options) (au
11231123
return ok
11241124
}
11251125

1126-
if c.kind == CLIENT {
1126+
// Check for the use of simple auth.
1127+
if c.kind == CLIENT || c.kind == LEAF {
11271128
if proxyRequired = opts.ProxyRequired; proxyRequired && !trustedProxy {
11281129
return setProxyAuthError(ErrAuthProxyRequired)
11291130
}

server/leafnode.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,17 +1049,22 @@ func (c *client) sendLeafConnect(clusterName string, headers bool) error {
10491049
// In addition, and this is to allow auth callout, set user/password or
10501050
// token if applicable.
10511051
if userInfo := c.leaf.remote.curURL.User; userInfo != nil {
1052-
// For backward compatibility, if only username is provided, set both
1053-
// Token and User, not just Token.
10541052
cinfo.User = userInfo.Username()
10551053
var ok bool
10561054
cinfo.Pass, ok = userInfo.Password()
1055+
// For backward compatibility, if only username is provided, set both
1056+
// Token and User, not just Token.
10571057
if !ok {
10581058
cinfo.Token = cinfo.User
10591059
}
10601060
} else if c.leaf.remote.username != _EMPTY_ {
10611061
cinfo.User = c.leaf.remote.username
10621062
cinfo.Pass = c.leaf.remote.password
1063+
// For backward compatibility, if only username is provided, set both
1064+
// Token and User, not just Token.
1065+
if cinfo.Pass == _EMPTY_ {
1066+
cinfo.Token = cinfo.User
1067+
}
10631068
}
10641069
b, err := json.Marshal(cinfo)
10651070
if err != nil {

server/leafnode_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10830,3 +10830,37 @@ func TestLeafNodeConfigureWriteDeadline(t *testing.T) {
1083010830
require_Equal(t, r.out.wdl, 6*time.Second)
1083110831
})
1083210832
}
10833+
10834+
// https://github.com/nats-io/nats-server/issues/7441
10835+
func TestLeafNodesBasicTokenAuth(t *testing.T) {
10836+
hubConf := createConfFile(t, []byte(`
10837+
server_name: "HUB"
10838+
listen: "127.0.0.1:-1"
10839+
authorization {
10840+
token: secret
10841+
}
10842+
leafnodes {
10843+
listen: "127.0.0.1:-1"
10844+
}
10845+
`))
10846+
hub, ohub := RunServerWithConfig(hubConf)
10847+
defer hub.Shutdown()
10848+
10849+
port := ohub.LeafNode.Port
10850+
leafTmpl := `
10851+
server_name: "LEAF"
10852+
listen: "127.0.0.1:-1"
10853+
leafnodes {
10854+
remotes: [
10855+
{ url: "nats://secret@localhost:%d" }
10856+
]
10857+
}
10858+
`
10859+
leafConf := createConfFile(t, fmt.Appendf(nil, leafTmpl, port))
10860+
leaf, _ := RunServerWithConfig(leafConf)
10861+
defer leaf.Shutdown()
10862+
10863+
// Verify that we have only 1 leaf
10864+
checkLeafNodeConnected(t, hub)
10865+
checkLeafNodeConnected(t, leaf)
10866+
}

0 commit comments

Comments
 (0)