Skip to content
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
59 changes: 56 additions & 3 deletions internal/pkg/auth/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"os"
"path/filepath"

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

"github.com/zalando/go-keyring"
)

Expand Down Expand Up @@ -69,6 +71,15 @@ func SetAuthField(key authFieldKey, value string) error {
}

func setAuthFieldInKeyring(key authFieldKey, value string) error {
activeProfile, err := config.GetProfile()
if err != nil {
return fmt.Errorf("get profile: %w", err)
}
Comment thread
joaopalet marked this conversation as resolved.

if activeProfile != "" {
activeProfileKeyring := filepath.Join(keyringService, activeProfile)
return keyring.Set(activeProfileKeyring, string(key), value)
}
return keyring.Set(keyringService, string(key), value)
}

Expand All @@ -82,7 +93,18 @@ func setAuthFieldInEncodedTextFile(key authFieldKey, value string) error {
if err != nil {
return fmt.Errorf("get config dir: %w", err)
}
textFileDir := filepath.Join(configDir, textFileFolderName)

activeProfile, err := config.GetProfile()
if err != nil {
return fmt.Errorf("get profile: %w", err)
}

profileTextFileFolderName := textFileFolderName
if activeProfile != "" {
profileTextFileFolderName = filepath.Join(activeProfile, textFileFolderName)
}

textFileDir := filepath.Join(configDir, profileTextFileFolderName)
textFilePath := filepath.Join(textFileDir, textFileName)

contentEncoded, err := os.ReadFile(textFilePath)
Expand Down Expand Up @@ -143,6 +165,15 @@ func GetAuthField(key authFieldKey) (string, error) {
}

func getAuthFieldFromKeyring(key authFieldKey) (string, error) {
activeProfile, err := config.GetProfile()
if err != nil {
return "", fmt.Errorf("get profile: %w", err)
}

if activeProfile != "" {
activeProfileKeyring := filepath.Join(keyringService, activeProfile)
return keyring.Get(activeProfileKeyring, string(key))
}
return keyring.Get(keyringService, string(key))
}

Expand All @@ -156,7 +187,18 @@ func getAuthFieldFromEncodedTextFile(key authFieldKey) (string, error) {
if err != nil {
return "", fmt.Errorf("get config dir: %w", err)
}
textFileDir := filepath.Join(configDir, textFileFolderName)

activeProfile, err := config.GetProfile()
if err != nil {
return "", fmt.Errorf("get profile: %w", err)
}

profileTextFileFolderName := textFileFolderName
if activeProfile != "" {
profileTextFileFolderName = filepath.Join(activeProfile, textFileFolderName)
}

textFileDir := filepath.Join(configDir, profileTextFileFolderName)
textFilePath := filepath.Join(textFileDir, textFileName)

contentEncoded, err := os.ReadFile(textFilePath)
Expand Down Expand Up @@ -187,7 +229,18 @@ func createEncodedTextFile() error {
if err != nil {
return fmt.Errorf("get config dir: %w", err)
}
textFileDir := filepath.Join(configDir, textFileFolderName)

activeProfile, err := config.GetProfile()
if err != nil {
return fmt.Errorf("get profile: %w", err)
}

profileTextFileFolderName := textFileFolderName
if activeProfile != "" {
profileTextFileFolderName = filepath.Join(activeProfile, textFileFolderName)
}

textFileDir := filepath.Join(configDir, profileTextFileFolderName)
textFilePath := filepath.Join(textFileDir, textFileName)

err = os.MkdirAll(textFileDir, os.ModePerm)
Expand Down
25 changes: 24 additions & 1 deletion internal/pkg/auth/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"time"

"github.com/zalando/go-keyring"

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

func TestSetGetAuthField(t *testing.T) {
Expand Down Expand Up @@ -336,6 +338,16 @@ func TestSetGetAuthFieldEncodedTextFile(t *testing.T) {
}

func deleteAuthFieldInKeyring(key authFieldKey) error {
activeProfile, err := config.GetProfile()
if err != nil {
return fmt.Errorf("get profile: %w", err)
}

if activeProfile != "" {
activeProfileKeyring := filepath.Join(keyringService, activeProfile)
return keyring.Delete(activeProfileKeyring, string(key))
}

return keyring.Delete(keyringService, string(key))
}

Expand All @@ -349,7 +361,18 @@ func deleteAuthFieldInEncodedTextFile(key authFieldKey) error {
if err != nil {
return fmt.Errorf("get config dir: %w", err)
}
textFileDir := filepath.Join(configDir, textFileFolderName)

activeProfile, err := config.GetProfile()
if err != nil {
return fmt.Errorf("get profile: %w", err)
}

profileTextFileFolderName := textFileFolderName
if activeProfile != "" {
profileTextFileFolderName = filepath.Join(activeProfile, textFileFolderName)
}

textFileDir := filepath.Join(configDir, profileTextFileFolderName)
textFilePath := filepath.Join(textFileDir, textFileName)

contentEncoded, err := os.ReadFile(textFilePath)
Expand Down