mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-21 17:21:18 -08:00
wip
This commit is contained in:
1 parent
1e69418c29
commit
8974fc7e75
2 files changed
+133
No files matched your search
@@ -12,6 +12,7 @@ import (
|
||||
"cmp"
|
||||
"container/list"
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -135,6 +136,14 @@ func (e *extension) Init(host ipnext.Host) error {
|
||||
}
|
||||
e.host = host
|
||||
|
||||
store, _ := e.backend.Sys().StateStore.GetOK()
|
||||
// if we can't get a store, we'll create an ephemeral signing key
|
||||
key, err := loadOrCreateSigningKey(store)
|
||||
if err != nil {
|
||||
return fmt.Errorf("loading conn25 signing key: %w", err)
|
||||
}
|
||||
e.conn25.connector.signingKey = key
|
||||
|
||||
dph := newDatapathHandler(e.conn25, e.conn25.logf)
|
||||
if err := e.installHooks(dph); err != nil {
|
||||
return err
|
||||
@@ -1472,6 +1481,9 @@ type connector struct {
|
||||
logf logger.Logf
|
||||
getIPSets func() ipSets
|
||||
clock tstime.Clock
|
||||
// signingKey is used to mint/authenticate tokens passed with [ConnectorTransitIPRequest]
|
||||
// it is written once during extension Init and so does not need to be protected by mu.
|
||||
signingKey signingKey
|
||||
|
||||
// Remember to add new fields to [connector.reset] if needed.
|
||||
mu sync.Mutex // protects the fields below
|
||||
@@ -1711,3 +1723,42 @@ func pickConnector(nb ipnext.NodeBackend, app appctype.Conn25Attr) []tailcfg.Nod
|
||||
sortByPreference(matches)
|
||||
return matches
|
||||
}
|
||||
|
||||
type signingKey []byte
|
||||
|
||||
const signingKeyLen = 32
|
||||
const connectorSigningKeyStateKey = ipn.StateKey("_conn25-transit-signing-key")
|
||||
|
||||
func generateSigningKey() ([]byte, error) {
|
||||
k := make(signingKey, signingKeyLen)
|
||||
if _, err := rand.Read(k); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return k, nil
|
||||
}
|
||||
|
||||
// loadOrCreateSigningKey returns [signingKeyLen] random bytes. If a store is
|
||||
// provided it will try to read the signingKey from the store, and if it
|
||||
// doesn't find one will write the one it creates to the store. It will return
|
||||
// an error if the store returns an error other then [ipn.ErrStateNotExist].
|
||||
func loadOrCreateSigningKey(store ipn.StateStore) (signingKey, error) {
|
||||
if store == nil {
|
||||
return generateSigningKey()
|
||||
}
|
||||
bs, err := store.ReadState(connectorSigningKeyStateKey)
|
||||
if err != nil && !errors.Is(err, ipn.ErrStateNotExist) {
|
||||
return nil, err
|
||||
}
|
||||
if len(bs) != signingKeyLen {
|
||||
// we need to make and store a new key
|
||||
k, err := generateSigningKey()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ipn.WriteState(store, connectorSigningKeyStateKey, k); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bs = k
|
||||
}
|
||||
return bs, nil
|
||||
}
|
||||
@@ -21,6 +21,7 @@ import (
|
||||
"golang.org/x/net/dns/dnsmessage"
|
||||
"tailscale.com/ipn"
|
||||
"tailscale.com/ipn/ipnext"
|
||||
memstore "tailscale.com/ipn/store/mem"
|
||||
"tailscale.com/net/dns"
|
||||
"tailscale.com/net/netmon"
|
||||
"tailscale.com/net/packet"
|
||||
@@ -3398,3 +3399,84 @@ func TestPickConnector(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadOrCreateSigningKey(t *testing.T) {
|
||||
// test it creates and returns a key
|
||||
store := new(memstore.Store)
|
||||
key, err := loadOrCreateSigningKey(store)
|
||||
if err != nil {
|
||||
t.Fatalf("error using memstore err: %v", err)
|
||||
}
|
||||
if len(key) != signingKeyLen {
|
||||
t.Fatalf("expected key of length %d, but got: %v", signingKeyLen, key)
|
||||
}
|
||||
|
||||
// test it returns the same key
|
||||
keyAgain, err := loadOrCreateSigningKey(store)
|
||||
if err != nil {
|
||||
t.Fatalf("error using memstore: %v", err)
|
||||
}
|
||||
if diff := cmp.Diff(key, keyAgain); diff != "" {
|
||||
t.Fatalf("want key to be the same, diff: %s", diff)
|
||||
}
|
||||
|
||||
// test it treats a bad value like a missing value
|
||||
if err := ipn.WriteState(store, connectorSigningKeyStateKey, []byte{0, 1, 2}); err != nil {
|
||||
t.Fatalf("error using memstore: %v", err)
|
||||
}
|
||||
key, err = loadOrCreateSigningKey(store)
|
||||
if err != nil {
|
||||
t.Fatalf("error using memstore: %v", err)
|
||||
}
|
||||
if len(key) != signingKeyLen {
|
||||
t.Fatalf("expected key of length %d, but got: %v", signingKeyLen, key)
|
||||
}
|
||||
|
||||
// test it returns an error if the store returns an error
|
||||
errorToReturn := errors.New("test")
|
||||
key, err = loadOrCreateSigningKey(storeThatErrors{err: errorToReturn, errorOnWrite: true})
|
||||
if key != nil {
|
||||
t.Fatalf("store errored, wanted nil key, got: %v", key)
|
||||
}
|
||||
if err != errorToReturn {
|
||||
t.Fatalf("store errored, wanted errorToReturn, got: %v", err)
|
||||
}
|
||||
key, err = loadOrCreateSigningKey(storeThatErrors{err: errorToReturn, errorOnRead: true})
|
||||
if key != nil {
|
||||
t.Fatalf("store errored, wanted nil key, got: %v", key)
|
||||
}
|
||||
if err != errorToReturn {
|
||||
t.Fatalf("store errored, wanted errorToReturn, got: %v", err)
|
||||
}
|
||||
|
||||
// test we generate a key anyway if there's no store
|
||||
key, err = loadOrCreateSigningKey(nil)
|
||||
if err != nil {
|
||||
t.Fatalf("wanted no err, got: %v", err)
|
||||
}
|
||||
if len(key) != signingKeyLen {
|
||||
t.Fatalf("expected key of length %d, but got: %v", signingKeyLen, key)
|
||||
}
|
||||
}
|
||||
|
||||
type storeThatErrors struct {
|
||||
err error
|
||||
val []byte
|
||||
errorOnRead bool
|
||||
errorOnWrite bool
|
||||
}
|
||||
|
||||
func (s storeThatErrors) ReadState(ipn.StateKey) ([]byte, error) {
|
||||
if s.errorOnRead {
|
||||
return nil, s.err
|
||||
}
|
||||
return s.val, nil
|
||||
}
|
||||
|
||||
func (s storeThatErrors) WriteState(_ ipn.StateKey, val []byte) error {
|
||||
if s.errorOnWrite {
|
||||
return s.err
|
||||
}
|
||||
s.val = val
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user