diff --git a/CHANGELOG.md b/CHANGELOG.md index 702f57a..32fccdb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ We use the following categories for changes: ### Added - added get datasources API key command [#sc-11671] +- added get client token command [#sc-11670] ### Changed diff --git a/cmd/generate_client_token.go b/cmd/generate_client_token.go new file mode 100644 index 0000000..3cebdcf --- /dev/null +++ b/cmd/generate_client_token.go @@ -0,0 +1,52 @@ +package cmd + +import ( + "github.com/spf13/cobra" + "groundcover.com/pkg/api" + "groundcover.com/pkg/auth" + "groundcover.com/pkg/ui" +) + +var generateClientTokenCmd = &cobra.Command{ + Use: "generate-client-token", + Short: "Get Client Token for Grafana API", + RunE: func(cmd *cobra.Command, args []string) error { + var err error + + var tenant *api.TenantInfo + if tenant, err = fetchTenant(); err != nil { + return err + } + + var apiToken *auth.ApiKey + if apiToken, err = fetchClientToken(tenant); err != nil { + return err + } + + ui.QuietWriter.Println(apiToken.ApiKey) + + return nil + }, +} + +func fetchClientToken(tenant *api.TenantInfo) (*auth.ApiKey, error) { + var err error + + var auth0Token *auth.Auth0Token + if auth0Token, err = auth.LoadAuth0Token(); err != nil { + return nil, err + } + + apiClient := api.NewClient(auth0Token) + + var clientToken *auth.ApiKey + if clientToken, err = apiClient.GetOrCreateClientToken(tenant); err != nil { + return nil, err + } + + return clientToken, nil +} + +func init() { + AuthCmd.AddCommand(generateClientTokenCmd) +} diff --git a/pkg/api/client.go b/pkg/api/client.go index 9ce488b..00fe0bf 100644 --- a/pkg/api/client.go +++ b/pkg/api/client.go @@ -101,7 +101,6 @@ func (client *Client) ServiceAccountToken(tenantUUID string) (*auth.SAToken, err return saToken, nil } - func (client *Client) GetDatasourcesAPIKey(tenant *TenantInfo) (*auth.ApiKey, error) { var err error @@ -130,6 +129,34 @@ func (client *Client) GetDatasourcesAPIKey(tenant *TenantInfo) (*auth.ApiKey, er return key, nil } +func (client *Client) GetOrCreateClientToken(tenant *TenantInfo) (*auth.ApiKey, error) { + var err error + + var url *url.URL + if url, err = client.JoinPath(auth.GenerateClientTokenAPIKeyEndpoint); err != nil { + return nil, err + } + + var request *http.Request + if request, err = http.NewRequest(http.MethodPost, url.String(), nil); err != nil { + return nil, err + } + + request.Header.Add(TenantUUIDHeader, tenant.UUID) + + var body []byte + if body, err = client.do(request); err != nil { + return nil, err + } + + clientToken := &auth.ApiKey{} + if err = clientToken.ParseBody(body); err != nil { + return nil, err + } + + return clientToken, nil +} + func (client *Client) JoinPath(endpoint string) (*url.URL, error) { return client.baseUrl.Parse(endpoint) } diff --git a/pkg/api/tenant.go b/pkg/api/tenant.go index ab2ff87..0f34799 100644 --- a/pkg/api/tenant.go +++ b/pkg/api/tenant.go @@ -5,7 +5,7 @@ import ( ) const ( - TenantUUIDHeader = "X-Tenant-UUID" + TenantUUIDHeader = "X-Tenant-UUID" TenantListEndpoint = "rbac/member/tenants" ) diff --git a/pkg/auth/api_key.go b/pkg/auth/api_key.go index 6795434..a3a6132 100644 --- a/pkg/auth/api_key.go +++ b/pkg/auth/api_key.go @@ -5,8 +5,9 @@ import ( ) const ( - GenerateAPIKeyEndpoint = "system/generate-api-key" - GetDatasourcesAPIKeyEndpoint = "system/get-datasources-api-key" + GenerateAPIKeyEndpoint = "system/generate-api-key" + GetDatasourcesAPIKeyEndpoint = "system/get-datasources-api-key" + GenerateClientTokenAPIKeyEndpoint = "system/generate-client-token" ) type ApiKey struct {