Skip to content

Commit d22e281

Browse files
committed
v2: add support for listing all keys
Also adds UserID to Key type. Updates tailscale/corp#22783 Signed-off-by: Percy Wegmann <[email protected]>
1 parent e89a1ab commit d22e281

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

v2/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func main() {
5252
oauthScopes := []string{"all:write"}
5353
tailnet := os.Getenv("TAILSCALE_TAILNET")
5454

55-
&tsclient.Client{
55+
client := &tsclient.Client{
5656
Tailnet: os.Getenv("TAILSCALE_TAILNET"),
5757
HTTP: tsclient.OAuthConfig{
5858
ClientID: os.Getenv("TAILSCALE_OAUTH_CLIENT_ID"),

v2/keys.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type (
4444
Revoked time.Time `json:"revoked"`
4545
Invalid bool `json:"invalid"`
4646
Capabilities KeyCapabilities `json:"capabilities"`
47+
UserID string `json:"userId"`
4748
}
4849
)
4950

@@ -70,8 +71,14 @@ func (kr *KeysResource) Get(ctx context.Context, id string) (*Key, error) {
7071

7172
// List returns every [Key] within the tailnet. The only fields set for each [Key] will be its identifier.
7273
// The keys returned are relative to the user that owns the API key used to authenticate the client.
73-
func (kr *KeysResource) List(ctx context.Context) ([]Key, error) {
74-
req, err := kr.buildRequest(ctx, http.MethodGet, kr.buildTailnetURL("keys"))
74+
//
75+
// Specify all to list both user and tailnet level keys.
76+
func (kr *KeysResource) List(ctx context.Context, all bool) ([]Key, error) {
77+
url := kr.buildTailnetURL("keys")
78+
if all {
79+
url.RawQuery = "all=true"
80+
}
81+
req, err := kr.buildRequest(ctx, http.MethodGet, url)
7582
if err != nil {
7683
return nil, err
7784
}

v2/keys_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func TestClient_Keys(t *testing.T) {
174174
"keys": expected,
175175
}
176176

177-
actual, err := client.Keys().List(context.Background())
177+
actual, err := client.Keys().List(context.Background(), false)
178178
assert.NoError(t, err)
179179
assert.EqualValues(t, expected, actual)
180180
assert.Equal(t, http.MethodGet, server.Method)

0 commit comments

Comments
 (0)