From 4a0bfca91cb8285ca7ef4388af99d0bcd86901e4 Mon Sep 17 00:00:00 2001 From: Cameron Stokes Date: Wed, 8 Mar 2023 10:44:00 -0900 Subject: [PATCH] Add support for OAuth client credentials --- go.mod | 5 ++++ go.sum | 22 ++++++++++++++++ tailscale/client.go | 50 ++++++++++++++++++++++++++++++++++--- tailscale/tailscale_test.go | 2 +- 4 files changed, 75 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 9097c19..d010cd5 100644 --- a/go.mod +++ b/go.mod @@ -5,10 +5,15 @@ go 1.19 require ( github.com/stretchr/testify v1.8.2 github.com/tailscale/hujson v0.0.0-20220506213045-af5ed07155e5 + golang.org/x/oauth2 v0.6.0 ) require ( github.com/davecgh/go-spew v1.1.1 // indirect + github.com/golang/protobuf v1.5.2 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + golang.org/x/net v0.8.0 // indirect + google.golang.org/appengine v1.6.7 // indirect + google.golang.org/protobuf v1.28.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 00c99b9..b1184c7 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,11 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= +github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -13,6 +18,23 @@ github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/tailscale/hujson v0.0.0-20220506213045-af5ed07155e5 h1:erxeiTyq+nw4Cz5+hLDkOwNF5/9IQWCQPv0gpb3+QHU= github.com/tailscale/hujson v0.0.0-20220506213045-af5ed07155e5/go.mod h1:DFSS3NAGHthKo1gTlmEcSBiZrRJXi28rLNd/1udP1c8= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= +golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ= +golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= +golang.org/x/oauth2 v0.6.0 h1:Lh8GPgSKBfWSwFvtuWOfeI3aAAnbXTSutYxJiOJFgIw= +golang.org/x/oauth2 v0.6.0/go.mod h1:ycmewcwgD4Rpr3eZJLSB4Kyyljb3qDh40vJ8STE5HKw= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c= +google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw= +google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/tailscale/client.go b/tailscale/client.go index c9646a0..500b808 100644 --- a/tailscale/client.go +++ b/tailscale/client.go @@ -14,6 +14,8 @@ import ( "time" "github.com/tailscale/hujson" + + "golang.org/x/oauth2/clientcredentials" ) type ( @@ -44,9 +46,18 @@ type ( const baseURL = "https://api.tailscale.com" const contentType = "application/json" +const defaultHttpClientTimeout = time.Minute // NewClient returns a new instance of the Client type that will perform operations against a chosen tailnet and will // provide the apiKey for authorization. Additional options can be provided, see ClientOption for more details. +// +// To use OAuth Client credentials pass an empty string as apiKey and use WithOAuthClientCredentials() as below: +// +// client, err := tailscale.NewClient( +// "", +// tailnet, +// tailscale.WithOAuthClientCredentials(oauthClientID, oauthClientSecret, oauthScopes), +// ) func NewClient(apiKey, tailnet string, options ...ClientOption) (*Client, error) { u, err := url.Parse(baseURL) if err != nil { @@ -54,18 +65,26 @@ func NewClient(apiKey, tailnet string, options ...ClientOption) (*Client, error) } c := &Client{ - apiKey: apiKey, - http: &http.Client{Timeout: time.Minute}, baseURL: u, tailnet: tailnet, } + if apiKey != "" { + c.apiKey = apiKey + c.http = &http.Client{Timeout: defaultHttpClientTimeout} + } + for _, option := range options { if err = option(c); err != nil { return nil, err } } + // apiKey or WithOAuthClientCredentials will initialize the http client. Fail here if both are not set. + if c.apiKey == "" && c.http == nil { + return nil, errors.New("no authentication credentials provided") + } + return c, nil } @@ -82,6 +101,27 @@ func WithBaseURL(baseURL string) ClientOption { } } +// WithOAuthClientCredentials sets the OAuth Client Credentials to use for the Tailscale API. +func WithOAuthClientCredentials(clientID, clientSecret string, scopes []string) ClientOption { + return func(c *Client) error { + relTokenURL, err := url.Parse("/api/v2/oauth/token") + if err != nil { + return err + } + oauthConfig := clientcredentials.Config{ + ClientID: clientID, + ClientSecret: clientSecret, + TokenURL: c.baseURL.ResolveReference(relTokenURL).String(), + Scopes: scopes, + } + + // use context.Background() here, since this is used to refresh the token in the future + c.http = oauthConfig.Client(context.Background()) + c.http.Timeout = defaultHttpClientTimeout + return nil + } +} + // TODO: consider setting `headers` and `body` via opts to decrease the number of arguments. func (c *Client) buildRequest(ctx context.Context, method, uri string, headers map[string]string, body interface{}) (*http.Request, error) { u, err := c.baseURL.Parse(uri) @@ -113,7 +153,11 @@ func (c *Client) buildRequest(ctx context.Context, method, uri string, headers m req.Header.Set("Content-Type", contentType) } - req.SetBasicAuth(c.apiKey, "") + // c.apiKey will not be set on the client was configured with WithOAuthClientCredentials() + if c.apiKey != "" { + req.SetBasicAuth(c.apiKey, "") + } + return req, nil } diff --git a/tailscale/tailscale_test.go b/tailscale/tailscale_test.go index 2168faf..f869511 100644 --- a/tailscale/tailscale_test.go +++ b/tailscale/tailscale_test.go @@ -53,7 +53,7 @@ func NewTestHarness(t *testing.T) (*tailscale.Client, *TestServer) { }) baseURL := fmt.Sprintf("http://localhost:%v", listener.Addr().(*net.TCPAddr).Port) - client, err := tailscale.NewClient("", "example.com", tailscale.WithBaseURL(baseURL)) + client, err := tailscale.NewClient("not a real key", "example.com", tailscale.WithBaseURL(baseURL)) assert.NoError(t, err) return client, testServer