Skip to content

Commit 940d87f

Browse files
authored
fix: Retrieve team for api key (#1372)
Retreive the team associated with an API key to allow usage tracking against a team name fixes: cloudquery/cloudquery-issues#893
1 parent 1492903 commit 940d87f

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

examples/simple_plugin/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ github.com/cloudquery/arrow/go/v14 v14.0.0-20231029080147-50d3871d0804 h1:y4EwAG
8888
github.com/cloudquery/arrow/go/v14 v14.0.0-20231029080147-50d3871d0804/go.mod h1:TqWp9yvMb9yZSxFNiij6cmZefm+1jw3oZU0L0w9lT7E=
8989
github.com/cloudquery/cloudquery-api-go v1.6.0 h1:8yAbNW+njhGmJLEnh7c55WFgs4rnGsIh3koIMkrebxg=
9090
github.com/cloudquery/cloudquery-api-go v1.6.0/go.mod h1:03fojQg0UpdgqXZ9tzZ5gF5CPad/F0sok66bsX6u4RA=
91+
github.com/cloudquery/cloudquery-api-go v1.5.1 h1:7CVbn8/A2bljPMjAUfFALI97ZnWOpRcAW/aCAr1FWYg=
92+
github.com/cloudquery/cloudquery-api-go v1.5.1/go.mod h1:03fojQg0UpdgqXZ9tzZ5gF5CPad/F0sok66bsX6u4RA=
9193
github.com/cloudquery/plugin-pb-go v1.14.0 h1:q2eZzOLVUPBjDj6AG1Smc+xE3Usuc5JDaJxv6s4M60A=
9294
github.com/cloudquery/plugin-pb-go v1.14.0/go.mod h1:Ti4SRHVDau7oF1w0/Oimt1k45yJilD0oFE0Xr7Tf3AI=
9395
github.com/cloudquery/plugin-sdk/v2 v2.7.0 h1:hRXsdEiaOxJtsn/wZMFQC9/jPfU1MeMK3KF+gPGqm7U=

premium/usage.go

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,10 @@ func NewUsageClient(pluginTeam cqapi.PluginTeam, pluginKind cqapi.PluginKind, pl
168168
op(u)
169169
}
170170

171+
tokenClient := auth.NewTokenClient()
172+
171173
// Create a default api client if none was provided
172174
if u.apiClient == nil {
173-
tokenClient := auth.NewTokenClient()
174175
ac, err := cqapi.NewClientWithResponses(u.url, cqapi.WithRequestEditorFn(func(ctx context.Context, req *http.Request) error {
175176
token, err := tokenClient.GetToken()
176177
if err != nil {
@@ -187,14 +188,9 @@ func NewUsageClient(pluginTeam cqapi.PluginTeam, pluginKind cqapi.PluginKind, pl
187188

188189
// Set team name from configuration if not provided
189190
if u.teamName == "" {
190-
teamName, err := config.GetValue("team")
191-
if errors.Is(err, os.ErrNotExist) {
192-
return nil, fmt.Errorf("config file for reading team name not found (%w). Hint: use `cloudquery login` and/or `cloudquery switch <team>`", err)
193-
} else if err != nil {
194-
return nil, fmt.Errorf("failed to get team name from config: %w", err)
195-
}
196-
if teamName == "" {
197-
return nil, fmt.Errorf("team name not set. Hint: use `cloudquery switch <team>`")
191+
teamName, err := u.getTeamNameByTokenType(tokenClient.GetTokenType())
192+
if err != nil {
193+
return nil, fmt.Errorf("failed to get team name: %w", err)
198194
}
199195
u.teamName = teamName
200196
}
@@ -361,3 +357,33 @@ func (u *BatchUpdater) calculateRetryDuration(statusCode int, headers http.Heade
361357
func retryableStatusCode(statusCode int) bool {
362358
return statusCode == http.StatusTooManyRequests || statusCode == http.StatusServiceUnavailable
363359
}
360+
361+
func (u *BatchUpdater) getTeamNameByTokenType(tokenType auth.TokenType) (string, error) {
362+
switch tokenType {
363+
case auth.BearerToken:
364+
teamName, err := config.GetValue("team")
365+
if errors.Is(err, os.ErrNotExist) {
366+
return "", fmt.Errorf("config file for reading team name not found (%w). Hint: use `cloudquery login` and/or `cloudquery switch <team>`", err)
367+
} else if err != nil {
368+
return "", fmt.Errorf("failed to get team name from config: %w", err)
369+
}
370+
if teamName == "" {
371+
return "", fmt.Errorf("team name not set. Hint: use `cloudquery switch <team>`")
372+
}
373+
return teamName, nil
374+
case auth.APIKey:
375+
resp, err := u.apiClient.ListTeamsWithResponse(context.Background(), &cqapi.ListTeamsParams{})
376+
if err != nil {
377+
return "", fmt.Errorf("failed to list teams for API key: %w", err)
378+
}
379+
if resp.StatusCode() != http.StatusOK {
380+
return "", fmt.Errorf("failed to list teams for API key, status code: %s", resp.Status())
381+
}
382+
if len(resp.JSON200.Items) != 1 {
383+
return "", fmt.Errorf("expected to find exactly one team for API key, found %d", len(resp.JSON200.Items))
384+
}
385+
return resp.JSON200.Items[0].Name, nil
386+
default:
387+
return "", fmt.Errorf("unsupported token type: %v", tokenType)
388+
}
389+
}

0 commit comments

Comments
 (0)