Skip to content

Commit

Permalink
Use new endpoint to delete grafana service accounts directly using a …
Browse files Browse the repository at this point in the history
…cloud token
  • Loading branch information
F21 committed Apr 3, 2024
1 parent 824c5c0 commit 8322f22
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 60 deletions.
10 changes: 1 addition & 9 deletions backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,16 +253,8 @@ func (e *testCloudEnv) CleanupCreds(t *testing.T) {
}

if len(e.ServiceAccountIDs) > 0 {
tmpClient, cleanup, err := client.CreateTemporaryStackGrafanaClient(e.CloudStackSlug, "vault-temp-service-account-", 5*time.Minute)

if err != nil {
t.Fatalf("unexpected error creating temporary stack client: %s", err)
}

defer cleanup()

for _, id := range e.ServiceAccountIDs {
err = tmpClient.DeleteServiceAccount(id)
err = client.DeleteGrafanaServiceAccountFromCloud(e.CloudStackSlug, id)
if err != nil {
t.Fatalf("unexpected error deleting service account: %s", err)
}
Expand Down
53 changes: 2 additions & 51 deletions client/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,61 +136,12 @@ func (g *Grafana) CreateGrafanaServiceAccountTokenFromCloud(stack string, input
}

func (g *Grafana) DeleteGrafanaServiceAccountFromCloud(stack string, serviceAccountID int64) error {
tempClient, cleanup, err := g.CreateTemporaryStackGrafanaClient(stack, "vault-temp-service-account-", 5*time.Minute)

if err != nil {
return fmt.Errorf("error creating temporary stack client: %w", err)
}

defer cleanup()

err = tempClient.DeleteServiceAccount(serviceAccountID)
err := g.do(http.MethodDelete, fmt.Sprintf("/api/instances/%s/api/serviceaccounts/%d", stack, serviceAccountID), nil, nil, nil)

if err != nil {
return fmt.Errorf("error deleting service account from cloud: %w", err)
return fmt.Errorf("error deleting service account from cloud token: %w", err)
}

return nil
}

func (g *Grafana) CreateTemporaryStackGrafanaClient(stackSlug string, tempSaPrefix string, tempKeyDuration time.Duration) (tempClient *Grafana, cleanup func() error, err error) {
stack, err := g.StackBySlug(stackSlug)
if err != nil {
return nil, nil, err
}

name := fmt.Sprintf("%s%d", tempSaPrefix, time.Now().UnixNano())

req := CreateServiceAccountInput{
Name: name,
Role: "Admin",
}

sa, err := g.CreateGrafanaServiceAccountFromCloud(stackSlug, req)
if err != nil {
return nil, nil, fmt.Errorf("error creating temporary service account: %w", err)
}

tokenRequest := CreateServiceAccountTokenInput{
Name: name,
ServiceAccountID: sa.ID,
SecondsToLive: int64(tempKeyDuration.Seconds()),
}

token, err := g.CreateGrafanaServiceAccountTokenFromCloud(stackSlug, tokenRequest)
if err != nil {
return nil, nil, fmt.Errorf("error creating temporary service account token: %w", err)
}

client, err := New(stack.URL, token.Key)
if err != nil {
return nil, nil, fmt.Errorf("error creating temporary client: %w", err)
}

cleanup = func() error {
err = client.DeleteServiceAccount(sa.ID)
return err
}

return client, cleanup, nil
}

0 comments on commit 8322f22

Please sign in to comment.