mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-28 20:46:28 -08:00
feature/conn25: create a secret for TransitIPRequest tokens
Create a secret that can be used for token minting and verification. Updates tailscale/corp#46169 Signed-off-by: Fran Bull <[email protected]>
This commit is contained in:
1 parent
b6fc02d501
commit
eb298524ee
4 files changed
+63
No files matched your search
@@ -547,6 +547,7 @@ func newConn25(logf logger.Logf) *Conn25 {
|
||||
getIPSets: getIPSets,
|
||||
clock: tstime.StdClock{},
|
||||
expiryQueue: list.New(),
|
||||
tokenSecret: newTokenSecret(),
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
@@ -99,6 +99,9 @@ type connector struct {
|
||||
|
||||
// Remember to add new fields to [connector.reset] if needed.
|
||||
mu sync.Mutex // protects the fields below
|
||||
// tokenSecret is the random secret used to mint and verify tokens in TransitIPRequests.
|
||||
// It is generated in memory at creation, rotated on [connector.reset] (ie on profile change), and not persisted.
|
||||
tokenSecret [tokenSecretLen]byte
|
||||
// transitIPs is a map of connector client peer IP -> client transitIPs that we update as connector client peers instruct us to, and then use to route traffic to its destination on behalf of connector clients.
|
||||
// Note that each peer could potentially have two maps: one for its IPv4 address, and one for its IPv6 address. The transit IPs map for a given peer IP will contain transit IPs of the same family as the peer's IP.
|
||||
transitIPs map[netip.Addr]map[netip.Addr]appAddr
|
||||
@@ -234,4 +237,6 @@ func (c *connector) reset() {
|
||||
|
||||
c.transitIPs = make(map[netip.Addr]map[netip.Addr]appAddr)
|
||||
c.expiryQueue = list.New()
|
||||
// old tokens should no longer verify
|
||||
c.tokenSecret = newTokenSecret()
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// Copyright (c) Tailscale Inc & contributors
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
package conn25
|
||||
|
||||
import "crypto/rand"
|
||||
|
||||
// tokenSecretLen is the length in bytes of the connector's token secret. It is
|
||||
// the recommended key size for HMAC-SHA-256, which is used to mint and verify
|
||||
// destination IP tokens.
|
||||
const tokenSecretLen = 32
|
||||
|
||||
// newTokenSecret returns a fresh random token secret.
|
||||
func newTokenSecret() [tokenSecretLen]byte {
|
||||
var secret [tokenSecretLen]byte
|
||||
rand.Read(secret[:])
|
||||
return secret
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// Copyright (c) Tailscale Inc & contributors
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
package conn25
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"tailscale.com/types/logger"
|
||||
)
|
||||
|
||||
func TestConnectorTokenSecretInit(t *testing.T) {
|
||||
c := newConn25(logger.Discard)
|
||||
|
||||
if c.connector.tokenSecret == ([tokenSecretLen]byte{}) {
|
||||
t.Fatal("token secret is all zero bytes, expected random bytes")
|
||||
}
|
||||
}
|
||||
|
||||
func TestConnectorTokenSecretRandomAcrossInstances(t *testing.T) {
|
||||
a := newConn25(logger.Discard)
|
||||
b := newConn25(logger.Discard)
|
||||
|
||||
if a.connector.tokenSecret == b.connector.tokenSecret {
|
||||
t.Fatal("two connectors minted identical token secrets, expected distinct random secrets")
|
||||
}
|
||||
}
|
||||
|
||||
func TestConnectorResetRotatesTokenSecret(t *testing.T) {
|
||||
c := newConn25(logger.Discard)
|
||||
|
||||
before := c.connector.tokenSecret
|
||||
c.connector.reset()
|
||||
after := c.connector.tokenSecret
|
||||
|
||||
if before == after {
|
||||
t.Fatal("token secret unchanged after reset, expected rotation")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user