Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into maxlevin/sc-11671/cli…
Browse files Browse the repository at this point in the history
…-add-call-to-getdatasourcesapikey
  • Loading branch information
maxlevinps committed Nov 15, 2023
2 parents c808a66 + 488c5c1 commit 68dc296
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
52 changes: 52 additions & 0 deletions cmd/generate_client_token.go
Original file line number Diff line number Diff line change
@@ -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)
}
29 changes: 28 additions & 1 deletion pkg/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

const (
TenantUUIDHeader = "X-Tenant-UUID"
TenantUUIDHeader = "X-Tenant-UUID"
TenantListEndpoint = "rbac/member/tenants"
)

Expand Down
5 changes: 3 additions & 2 deletions pkg/auth/api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 68dc296

Please sign in to comment.