Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

Account delete #237

Merged
merged 2 commits into from
Aug 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion pkg/secrethub/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@ package secrethub

import (
"github.com/secrethub/secrethub-go/internals/api"
"github.com/secrethub/secrethub-go/internals/api/uuid"
"github.com/secrethub/secrethub-go/internals/crypto"
"github.com/secrethub/secrethub-go/internals/errio"
"github.com/secrethub/secrethub-go/pkg/secrethub/credentials"
)

// Errors
var (
ErrNoDecryptionKey = errClient.Code("no_decryption_key").Error("client is not initialized with a method to decrypt the account key")
ErrNoDecryptionKey = errClient.Code("no_decryption_key").Error("client is not initialized with a method to decrypt the account key")
ErrIncorrectAccountID = errClient.Code("incorrect_account_id").Error("the incorrect account ID was provided. To delete the currently authenticated account please provide its ID.")
)

// AccountService handles operations on SecretHub accounts.
type AccountService interface {
// Me retrieves the authenticated account of the client.
Me() (*api.Account, error)
// Delete deletes the authenticated account of the client if it's ID is provided as a parameter.
// Do not use this method. Account deletes should only be performed from the CLI.
Delete(accountID uuid.UUID) error
// Get retrieves an account by name.
Get(name string) (*api.Account, error)
// Keys returns an account key service.
Expand All @@ -37,6 +42,19 @@ func (s accountService) Me() (*api.Account, error) {
return s.client.getMyAccount()
}

// Delete deletes the authenticated account of the client if it's ID is provided as a parameter.
// Do not use this method. Account deletes should only be performed from the CLI.
func (s accountService) Delete(accountID uuid.UUID) error {
account, err := s.client.getMyAccount()
if err != nil {
return err
}
if accountID != account.AccountID {
return ErrIncorrectAccountID
}
return s.client.httpClient.DeleteMyAccount()
}

// Get retrieves an account by name.
func (s accountService) Get(name string) (*api.Account, error) {
accountName, err := api.NewAccountName(name)
Expand Down
7 changes: 7 additions & 0 deletions pkg/secrethub/fakeclient/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ package fakeclient

import (
"github.com/secrethub/secrethub-go/internals/api"
"github.com/secrethub/secrethub-go/internals/api/uuid"
"github.com/secrethub/secrethub-go/pkg/secrethub"
)

// AccountService is a mock of the AccountService interface.
type AccountService struct {
MeFunc func() (*api.Account, error)
DeleteFunc func(accountID uuid.UUID) error
GetFunc func(name string) (*api.Account, error)
AccountKeyService secrethub.AccountKeyService
}
Expand All @@ -23,6 +25,11 @@ func (s *AccountService) Get(name string) (*api.Account, error) {
return s.GetFunc(name)
}

// Delete implements the AccountService interface Delete function.
func (s *AccountService) Delete(accountID uuid.UUID) error {
return s.DeleteFunc(accountID)
}

// Me implements the AccountService interface Me function.
func (s *AccountService) Me() (*api.Account, error) {
return s.MeFunc()
Expand Down
8 changes: 8 additions & 0 deletions pkg/secrethub/internals/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const (

// Current account
pathMeUser = "%s/me/user"
pathMeAccount = "%s/me/account"
pathMeRepos = "%s/me/repos"
pathMeKey = "%s/me/key?key_version=v2"
pathMeEmailVerification = "%s/me/user/verification-email"
Expand Down Expand Up @@ -173,6 +174,13 @@ func (c *Client) GetMyUser() (*api.User, error) {
return out, errio.Error(err)
}

// DeleteMyAccount
func (c *Client) DeleteMyAccount() error {
rawURL := fmt.Sprintf(pathMeAccount, c.base.String())
err := c.delete(rawURL, true, nil)
return errio.Error(err)
}

// CreateCredential creates a new credential for the account.
func (c *Client) CreateCredential(in *api.CreateCredentialRequest) (*api.Credential, error) {
out := &api.Credential{}
Expand Down