Skip to content

Commit

Permalink
[sc-11671] - add get datasources api key command (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxlevinps authored Nov 15, 2023
1 parent 488c5c1 commit 3363b25
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ We use the following categories for changes:
## [Unreleased]

### Added
- added get datasources API key command [#sc-11671]
- added get client token command [#sc-11670]

### Changed

### Fixed
Expand Down
52 changes: 52 additions & 0 deletions cmd/get_datasources_api_key.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 getDatasourcesAPIKeyCmd = &cobra.Command{
Use: "get-datasources-api-key",
Short: "Get the API key for datasources",
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 = fetchDatasourcesAPIKey(tenant); err != nil {
return err
}

ui.QuietWriter.Println(apiToken.ApiKey)

return nil
},
}

func fetchDatasourcesAPIKey(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 apiToken *auth.ApiKey
if apiToken, err = apiClient.GetDatasourcesAPIKey(tenant); err != nil {
return nil, err
}

return apiToken, nil
}

func init() {
AuthCmd.AddCommand(getDatasourcesAPIKeyCmd)
}
31 changes: 29 additions & 2 deletions pkg/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (client *Client) ApiKey(tenantUUID string) (*auth.ApiKey, error) {
var err error

var url *url.URL
if url, err = client.JoinPath(auth.GENERATE_API_KEY_ENDPOINT); err != nil {
if url, err = client.JoinPath(auth.GenerateAPIKeyEndpoint); err != nil {
return nil, err
}

Expand Down Expand Up @@ -101,12 +101,39 @@ func (client *Client) ServiceAccountToken(tenantUUID string) (*auth.SAToken, err

return saToken, nil
}
func (client *Client) GetDatasourcesAPIKey(tenant *TenantInfo) (*auth.ApiKey, error) {
var err error

var url *url.URL
if url, err = client.JoinPath(auth.GetDatasourcesAPIKeyEndpoint); 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
}

key := &auth.ApiKey{}
if err = key.ParseBody(body); err != nil {
return nil, err
}

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.GENERATE_CLIENT_TOKEN_API_KEY_ENDPOINT); err != nil {
if url, err = client.JoinPath(auth.GenerateClientTokenAPIKeyEndpoint); err != nil {
return nil, err
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/api/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

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

type TenantListResponse struct {
Expand All @@ -23,7 +23,7 @@ func (client *Client) TenantList() ([]*TenantInfo, error) {
var err error

var body []byte
if body, err = client.get(TenantsListEndpoint); err != nil {
if body, err = client.get(TenantListEndpoint); err != nil {
return nil, err
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/auth/api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
)

const (
GENERATE_API_KEY_ENDPOINT = "system/generate-api-key"
GenerateAPIKeyEndpoint = "system/generate-api-key"
GetDatasourcesAPIKeyEndpoint = "system/get-datasources-api-key"
GenerateClientTokenAPIKeyEndpoint = "system/generate-client-token"
)

type ApiKey struct {
Expand Down
5 changes: 0 additions & 5 deletions pkg/auth/client_token.go

This file was deleted.

0 comments on commit 3363b25

Please sign in to comment.