tsnet: enable node registration via federated identity
Updates: tailscale.com/corp#34148 Signed-off-by: Gesa Stupperich <gesa@tailscale.com>
This commit is contained in:
committed by
Gesa Stupperich
parent
957a443b23
commit
536188c1b5
+3
-1
@@ -142,9 +142,11 @@ tailscale.com/tsnet dependencies: (generated by github.com/tailscale/depaware)
|
||||
tailscale.com/feature/buildfeatures from tailscale.com/wgengine/magicsock+
|
||||
tailscale.com/feature/c2n from tailscale.com/tsnet
|
||||
tailscale.com/feature/condlite/expvar from tailscale.com/wgengine/magicsock
|
||||
tailscale.com/feature/condregister/identityfederation from tailscale.com/tsnet
|
||||
tailscale.com/feature/condregister/oauthkey from tailscale.com/tsnet
|
||||
tailscale.com/feature/condregister/portmapper from tailscale.com/tsnet
|
||||
tailscale.com/feature/condregister/useproxy from tailscale.com/tsnet
|
||||
tailscale.com/feature/identityfederation from tailscale.com/feature/condregister/identityfederation
|
||||
tailscale.com/feature/oauthkey from tailscale.com/feature/condregister/oauthkey
|
||||
tailscale.com/feature/portmapper from tailscale.com/feature/condregister/portmapper
|
||||
tailscale.com/feature/syspolicy from tailscale.com/logpolicy
|
||||
@@ -343,7 +345,7 @@ tailscale.com/tsnet dependencies: (generated by github.com/tailscale/depaware)
|
||||
golang.org/x/net/ipv6 from github.com/prometheus-community/pro-bing+
|
||||
LDW golang.org/x/net/proxy from tailscale.com/net/netns
|
||||
DI golang.org/x/net/route from tailscale.com/net/netmon+
|
||||
golang.org/x/oauth2 from golang.org/x/oauth2/clientcredentials
|
||||
golang.org/x/oauth2 from golang.org/x/oauth2/clientcredentials+
|
||||
golang.org/x/oauth2/clientcredentials from tailscale.com/feature/oauthkey
|
||||
golang.org/x/oauth2/internal from golang.org/x/oauth2+
|
||||
golang.org/x/sync/errgroup from github.com/mdlayher/socket+
|
||||
|
||||
+84
-8
@@ -30,6 +30,7 @@ import (
|
||||
"tailscale.com/control/controlclient"
|
||||
"tailscale.com/envknob"
|
||||
_ "tailscale.com/feature/c2n"
|
||||
_ "tailscale.com/feature/condregister/identityfederation"
|
||||
_ "tailscale.com/feature/condregister/oauthkey"
|
||||
_ "tailscale.com/feature/condregister/portmapper"
|
||||
_ "tailscale.com/feature/condregister/useproxy"
|
||||
@@ -115,6 +116,29 @@ type Server struct {
|
||||
// used.
|
||||
AuthKey string
|
||||
|
||||
// ClientSecret, if non-empty, is the OAuth client secret
|
||||
// that will be used to generate authkeys via OAuth. It
|
||||
// will be preferred over the TS_CLIENT_SECRET environment
|
||||
// variable. If the node is already created (from state
|
||||
// previously stored in Store), then this field is not
|
||||
// used.
|
||||
ClientSecret string
|
||||
|
||||
// ClientID, if non-empty, is the client ID used to generate
|
||||
// authkeys via workload identity federation. It will be
|
||||
// preferred over the TS_CLIENT_ID environment variable.
|
||||
// If the node is already created (from state previously
|
||||
// stored in Store), then this field is not used.
|
||||
ClientID string
|
||||
|
||||
// IDToken, if non-empty, is the ID token from the identity
|
||||
// provider to exchange with the control server for workload
|
||||
// identity federation. It will be preferred over the
|
||||
// TS_ID_TOKEN environment variable. If the node is already
|
||||
// created (from state previously stored in Store), then this
|
||||
// field is not used.
|
||||
IDToken string
|
||||
|
||||
// ControlURL optionally specifies the coordination server URL.
|
||||
// If empty, the Tailscale default is used.
|
||||
ControlURL string
|
||||
@@ -517,6 +541,27 @@ func (s *Server) getAuthKey() string {
|
||||
return os.Getenv("TS_AUTH_KEY")
|
||||
}
|
||||
|
||||
func (s *Server) getClientSecret() string {
|
||||
if v := s.ClientSecret; v != "" {
|
||||
return v
|
||||
}
|
||||
return os.Getenv("TS_CLIENT_SECRET")
|
||||
}
|
||||
|
||||
func (s *Server) getClientID() string {
|
||||
if v := s.ClientID; v != "" {
|
||||
return v
|
||||
}
|
||||
return os.Getenv("TS_CLIENT_ID")
|
||||
}
|
||||
|
||||
func (s *Server) getIDToken() string {
|
||||
if v := s.IDToken; v != "" {
|
||||
return v
|
||||
}
|
||||
return os.Getenv("TS_ID_TOKEN")
|
||||
}
|
||||
|
||||
func (s *Server) start() (reterr error) {
|
||||
var closePool closeOnErrorPool
|
||||
defer closePool.closeAllIfError(&reterr)
|
||||
@@ -684,14 +729,9 @@ func (s *Server) start() (reterr error) {
|
||||
prefs.ControlURL = s.ControlURL
|
||||
prefs.RunWebClient = s.RunWebClient
|
||||
prefs.AdvertiseTags = s.AdvertiseTags
|
||||
authKey := s.getAuthKey()
|
||||
// Try to use an OAuth secret to generate an auth key if that functionality
|
||||
// is available.
|
||||
if f, ok := tailscale.HookResolveAuthKey.GetOk(); ok {
|
||||
authKey, err = f(s.shutdownCtx, s.getAuthKey(), prefs.AdvertiseTags)
|
||||
if err != nil {
|
||||
return fmt.Errorf("resolving auth key: %w", err)
|
||||
}
|
||||
authKey, err := s.resolveAuthKey()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error resolving auth key: %w", err)
|
||||
}
|
||||
err = lb.Start(ipn.Options{
|
||||
UpdatePrefs: prefs,
|
||||
@@ -738,6 +778,42 @@ func (s *Server) start() (reterr error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) resolveAuthKey() (string, error) {
|
||||
authKey := s.getAuthKey()
|
||||
var err error
|
||||
// Try to use an OAuth secret to generate an auth key if that functionality
|
||||
// is available.
|
||||
resolveViaOAuth, oauthOk := tailscale.HookResolveAuthKey.GetOk()
|
||||
if oauthOk {
|
||||
clientSecret := authKey
|
||||
if authKey == "" {
|
||||
clientSecret = s.getClientSecret()
|
||||
}
|
||||
authKey, err = resolveViaOAuth(s.shutdownCtx, clientSecret, s.AdvertiseTags)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
// Try to resolve the auth key via workload identity federation if that functionality
|
||||
// is available and no auth key is yet determined.
|
||||
resolveViaWIF, wifOk := tailscale.HookResolveAuthKeyViaWIF.GetOk()
|
||||
if wifOk && authKey == "" {
|
||||
clientID := s.getClientID()
|
||||
idToken := s.getIDToken()
|
||||
if clientID != "" && idToken == "" {
|
||||
return "", fmt.Errorf("client ID for workload identity federation found, but ID token is empty")
|
||||
}
|
||||
if clientID == "" && idToken != "" {
|
||||
return "", fmt.Errorf("ID token for workload identity federation found, but client ID is empty")
|
||||
}
|
||||
authKey, err = resolveViaWIF(s.shutdownCtx, s.ControlURL, clientID, idToken, s.AdvertiseTags)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
return authKey, nil
|
||||
}
|
||||
|
||||
func (s *Server) startLogger(closePool *closeOnErrorPool, health *health.Tracker, tsLogf logger.Logf) error {
|
||||
if testenv.InTest() {
|
||||
return nil
|
||||
|
||||
@@ -38,6 +38,7 @@ import (
|
||||
"golang.org/x/net/proxy"
|
||||
"tailscale.com/client/local"
|
||||
"tailscale.com/cmd/testwrapper/flakytest"
|
||||
"tailscale.com/internal/client/tailscale"
|
||||
"tailscale.com/ipn"
|
||||
"tailscale.com/ipn/store/mem"
|
||||
"tailscale.com/net/netns"
|
||||
@@ -1393,3 +1394,201 @@ func TestDeps(t *testing.T) {
|
||||
},
|
||||
}.Check(t)
|
||||
}
|
||||
|
||||
func TestResolveAuthKey(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
authKey string
|
||||
clientSecret string
|
||||
clientID string
|
||||
idToken string
|
||||
oauthAvailable bool
|
||||
wifAvailable bool
|
||||
resolveViaOAuth func(ctx context.Context, clientSecret string, tags []string) (string, error)
|
||||
resolveViaWIF func(ctx context.Context, baseURL, clientID, idToken string, tags []string) (string, error)
|
||||
wantAuthKey string
|
||||
wantErr bool
|
||||
wantErrContains string
|
||||
}{
|
||||
{
|
||||
name: "successful resolution via OAuth client secret",
|
||||
clientSecret: "tskey-client-secret-123",
|
||||
oauthAvailable: true,
|
||||
resolveViaOAuth: func(ctx context.Context, clientSecret string, tags []string) (string, error) {
|
||||
if clientSecret != "tskey-client-secret-123" {
|
||||
return "", fmt.Errorf("unexpected client secret: %s", clientSecret)
|
||||
}
|
||||
return "tskey-auth-via-oauth", nil
|
||||
},
|
||||
wantAuthKey: "tskey-auth-via-oauth",
|
||||
wantErrContains: "",
|
||||
},
|
||||
{
|
||||
name: "failing resolution via OAuth client secret",
|
||||
clientSecret: "tskey-client-secret-123",
|
||||
oauthAvailable: true,
|
||||
resolveViaOAuth: func(ctx context.Context, clientSecret string, tags []string) (string, error) {
|
||||
return "", fmt.Errorf("resolution failed")
|
||||
},
|
||||
wantErrContains: "resolution failed",
|
||||
},
|
||||
{
|
||||
name: "successful resolution via federated ID token",
|
||||
clientID: "client-id-123",
|
||||
idToken: "id-token-456",
|
||||
wifAvailable: true,
|
||||
resolveViaWIF: func(ctx context.Context, baseURL, clientID, idToken string, tags []string) (string, error) {
|
||||
if clientID != "client-id-123" {
|
||||
return "", fmt.Errorf("unexpected client ID: %s", clientID)
|
||||
}
|
||||
if idToken != "id-token-456" {
|
||||
return "", fmt.Errorf("unexpected ID token: %s", idToken)
|
||||
}
|
||||
return "tskey-auth-via-wif", nil
|
||||
},
|
||||
wantAuthKey: "tskey-auth-via-wif",
|
||||
wantErrContains: "",
|
||||
},
|
||||
{
|
||||
name: "failing resolution via federated ID token",
|
||||
clientID: "client-id-123",
|
||||
idToken: "id-token-456",
|
||||
wifAvailable: true,
|
||||
resolveViaWIF: func(ctx context.Context, baseURL, clientID, idToken string, tags []string) (string, error) {
|
||||
return "", fmt.Errorf("resolution failed")
|
||||
},
|
||||
wantErrContains: "resolution failed",
|
||||
},
|
||||
{
|
||||
name: "empty client ID",
|
||||
clientID: "",
|
||||
idToken: "id-token-456",
|
||||
wifAvailable: true,
|
||||
resolveViaWIF: func(ctx context.Context, baseURL, clientID, idToken string, tags []string) (string, error) {
|
||||
return "", fmt.Errorf("should not be called")
|
||||
},
|
||||
wantErrContains: "empty",
|
||||
},
|
||||
{
|
||||
name: "empty ID token",
|
||||
clientID: "client-id-123",
|
||||
idToken: "",
|
||||
wifAvailable: true,
|
||||
resolveViaWIF: func(ctx context.Context, baseURL, clientID, idToken string, tags []string) (string, error) {
|
||||
return "", fmt.Errorf("should not be called")
|
||||
},
|
||||
wantErrContains: "empty",
|
||||
},
|
||||
{
|
||||
name: "workload identity resolution skipped if resolution via OAuth token succeeds",
|
||||
clientSecret: "tskey-client-secret-123",
|
||||
oauthAvailable: true,
|
||||
resolveViaOAuth: func(ctx context.Context, clientSecret string, tags []string) (string, error) {
|
||||
if clientSecret != "tskey-client-secret-123" {
|
||||
return "", fmt.Errorf("unexpected client secret: %s", clientSecret)
|
||||
}
|
||||
return "tskey-auth-via-oauth", nil
|
||||
},
|
||||
wifAvailable: true,
|
||||
resolveViaWIF: func(ctx context.Context, baseURL, clientID, idToken string, tags []string) (string, error) {
|
||||
return "", fmt.Errorf("should not be called")
|
||||
},
|
||||
wantAuthKey: "tskey-auth-via-oauth",
|
||||
wantErrContains: "",
|
||||
},
|
||||
{
|
||||
name: "workload identity resolution skipped if resolution via OAuth token fails",
|
||||
clientID: "tskey-client-id-123",
|
||||
idToken: "",
|
||||
oauthAvailable: true,
|
||||
resolveViaOAuth: func(ctx context.Context, clientSecret string, tags []string) (string, error) {
|
||||
return "", fmt.Errorf("resolution failed")
|
||||
},
|
||||
wifAvailable: true,
|
||||
resolveViaWIF: func(ctx context.Context, baseURL, clientID, idToken string, tags []string) (string, error) {
|
||||
return "", fmt.Errorf("should not be called")
|
||||
},
|
||||
wantErrContains: "failed",
|
||||
},
|
||||
{
|
||||
name: "authkey set and no resolution available",
|
||||
authKey: "tskey-auth-123",
|
||||
oauthAvailable: false,
|
||||
wifAvailable: false,
|
||||
wantAuthKey: "tskey-auth-123",
|
||||
wantErrContains: "",
|
||||
},
|
||||
{
|
||||
name: "no authkey set and no resolution available",
|
||||
oauthAvailable: false,
|
||||
wifAvailable: false,
|
||||
wantAuthKey: "",
|
||||
wantErrContains: "",
|
||||
},
|
||||
{
|
||||
name: "authkey is client secret and resolution via OAuth client secret succeeds",
|
||||
authKey: "tskey-client-secret-123",
|
||||
oauthAvailable: true,
|
||||
resolveViaOAuth: func(ctx context.Context, clientSecret string, tags []string) (string, error) {
|
||||
if clientSecret != "tskey-client-secret-123" {
|
||||
return "", fmt.Errorf("unexpected client secret: %s", clientSecret)
|
||||
}
|
||||
return "tskey-auth-via-oauth", nil
|
||||
},
|
||||
wantAuthKey: "tskey-auth-via-oauth",
|
||||
wantErrContains: "",
|
||||
},
|
||||
{
|
||||
name: "authkey is client secret but resolution via OAuth client secret fails",
|
||||
authKey: "tskey-client-secret-123",
|
||||
oauthAvailable: true,
|
||||
resolveViaOAuth: func(ctx context.Context, clientSecret string, tags []string) (string, error) {
|
||||
return "", fmt.Errorf("resolution failed")
|
||||
},
|
||||
wantErrContains: "resolution failed",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.oauthAvailable {
|
||||
t.Cleanup(tailscale.HookResolveAuthKey.SetForTest(tt.resolveViaOAuth))
|
||||
}
|
||||
|
||||
if tt.wifAvailable {
|
||||
t.Cleanup(tailscale.HookResolveAuthKeyViaWIF.SetForTest(tt.resolveViaWIF))
|
||||
}
|
||||
|
||||
s := &Server{
|
||||
AuthKey: tt.authKey,
|
||||
ClientSecret: tt.clientSecret,
|
||||
ClientID: tt.clientID,
|
||||
IDToken: tt.idToken,
|
||||
ControlURL: "https://control.example.com",
|
||||
}
|
||||
s.shutdownCtx = context.Background()
|
||||
|
||||
gotAuthKey, err := s.resolveAuthKey()
|
||||
|
||||
if tt.wantErrContains != "" {
|
||||
if err == nil {
|
||||
t.Errorf("expected error but got none")
|
||||
return
|
||||
}
|
||||
if !strings.Contains(err.Error(), tt.wantErrContains) {
|
||||
t.Errorf("expected error containing %q but got error: %v", tt.wantErrContains, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("resolveAuthKey expected no error but got error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if gotAuthKey != tt.wantAuthKey {
|
||||
t.Errorf("resolveAuthKey() = %q, want %q", gotAuthKey, tt.wantAuthKey)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user