Skip to content

Don't allow deleting the default configuration profile #363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 31, 2024
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
4 changes: 4 additions & 0 deletions internal/cmd/config/profile/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ func NewCmd(p *print.Printer) *cobra.Command {
return &errors.DeleteInexistentProfile{Profile: model.Profile}
}

if model.Profile == config.DefaultProfileName {
return &errors.DeleteDefaultProfile{DefaultProfile: config.DefaultProfileName}
}

if !model.AssumeYes {
prompt := fmt.Sprintf("Are you sure you want to delete profile %q? (This cannot be undone)", model.Profile)
err = p.PromptForConfirmation(prompt)
Expand Down
6 changes: 6 additions & 0 deletions internal/pkg/auth/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"encoding/json"
"errors"
"fmt"

"os"
"path/filepath"

"github.com/stackitcloud/stackit-cli/internal/pkg/config"
pkgErrors "github.com/stackitcloud/stackit-cli/internal/pkg/errors"

"github.com/zalando/go-keyring"
)
Expand Down Expand Up @@ -277,6 +279,10 @@ func DeleteProfileFromKeyring(profile string) error {
return fmt.Errorf("validate profile: %w", err)
}

if profile == config.DefaultProfileName {
return &pkgErrors.DeleteDefaultProfile{DefaultProfile: config.DefaultProfileName}
}

for _, key := range authFieldKeys {
err := deleteAuthFieldInKeyring(profile, key)
if err != nil {
Expand Down
27 changes: 9 additions & 18 deletions internal/pkg/auth/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,34 +552,20 @@ func TestDeleteProfileFromKeyring(t *testing.T) {
activeProfile string
isValid bool
}{

{
description: "base, default profile",
keys: authFieldKeys,
activeProfile: config.DefaultProfileName,
isValid: true,
},
{
description: "missing keys, default profile",
keys: []authFieldKey{
ACCESS_TOKEN,
SERVICE_ACCOUNT_EMAIL,
},
activeProfile: config.DefaultProfileName,
isValid: true,
},
{
description: "base, custom profile",
description: "base",
keys: authFieldKeys,
activeProfile: "test-profile",
isValid: true,
},
{
description: "missing keys, custom profile",
description: "missing keys",
keys: []authFieldKey{
ACCESS_TOKEN,
SERVICE_ACCOUNT_EMAIL,
},
activeProfile: config.DefaultProfileName,
activeProfile: "test-profile",
isValid: true,
},
{
Expand All @@ -592,6 +578,11 @@ func TestDeleteProfileFromKeyring(t *testing.T) {
keyringFails: true,
isValid: false,
},
{
description: "default profile",
activeProfile: config.DefaultProfileName,
isValid: false,
},
}

for _, tt := range tests {
Expand Down
7 changes: 6 additions & 1 deletion internal/pkg/config/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,19 @@ func ListProfiles() ([]string, error) {
}

// DeleteProfile deletes a profile.
// If the profile does not exist, it returns an error.
// If the profile does not exist or is the default profile, it returns an error.
// If the profile is the active profile, it sets the active profile to the default profile.
func DeleteProfile(p *print.Printer, profile string) error {
err := ValidateProfile(profile)
if err != nil {
return fmt.Errorf("validate profile: %w", err)
}

// Default profile cannot be deleted
if profile == DefaultProfileName {
return &errors.DeleteDefaultProfile{DefaultProfile: DefaultProfileName}
}

activeProfile, err := GetProfile()
if err != nil {
return fmt.Errorf("get active profile: %w", err)
Expand Down
10 changes: 10 additions & 0 deletions internal/pkg/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ To create it, run:
To list all profiles, run:
$ stackit config profile list`

DELETE_DEFAULT_PROFILE = `the default configuration profile %q cannot be deleted.`

ARGUS_INVALID_INPUT_PLAN = `the instance plan was not correctly provided.

Either provide the plan ID:
Expand Down Expand Up @@ -173,6 +175,14 @@ func (e *DeleteInexistentProfile) Error() string {
return fmt.Sprintf(DELETE_INEXISTENT_PROFILE, e.Profile)
}

type DeleteDefaultProfile struct {
DefaultProfile string
}

func (e *DeleteDefaultProfile) Error() string {
return fmt.Sprintf(DELETE_DEFAULT_PROFILE, e.DefaultProfile)
}

type ArgusInputPlanError struct {
Cmd *cobra.Command
Args []string
Expand Down