Skip to content

Commit e1a34a5

Browse files
authored
refactor: use credentials module for parsing secrets from keyring (#428)
1 parent ba511ed commit e1a34a5

3 files changed

Lines changed: 15 additions & 23 deletions

File tree

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.25.0
44

55
require (
66
github.com/UpCloudLtd/progress v1.0.3
7+
github.com/UpCloudLtd/upcloud-go-api/credentials v0.1.1
78
github.com/UpCloudLtd/upcloud-go-api/v8 v8.25.0
89
github.com/adrg/xdg v0.5.3
910
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,8 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym
397397
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
398398
github.com/UpCloudLtd/progress v1.0.3 h1:8SfntHkBPyQc5BL3946Bgi9KYnQOxa5RR2EKdadujdg=
399399
github.com/UpCloudLtd/progress v1.0.3/go.mod h1:iGxOnb9HvHW0yrLGUjHr0lxHhn7TehgWwh7a8NqK6iQ=
400+
github.com/UpCloudLtd/upcloud-go-api/credentials v0.1.1 h1:eTfQsv58ufALOk9BZ7WbS/i7pMUD11RnYYpRPsz0LdI=
401+
github.com/UpCloudLtd/upcloud-go-api/credentials v0.1.1/go.mod h1:7OtVs2UqtfvjkC1HfE+Oud0MnbMv7qUWnbEgxnTAqts=
400402
github.com/UpCloudLtd/upcloud-go-api/v8 v8.25.0 h1:OWkIs5Z67jZJb9qNHNaNCl20vJaCIn9U1QnaRZE5grQ=
401403
github.com/UpCloudLtd/upcloud-go-api/v8 v8.25.0/go.mod h1:ImDdnWfVVM6WCRTrskGhAw2W1cRwu5IEkBw+9UCzAv8=
402404
github.com/adrg/xdg v0.5.3 h1:xRnxJXne7+oWDatRhR1JLnvuccuIeCoBu2rtuLqQB78=

internal/config/config.go

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
internal "github.com/UpCloudLtd/upcloud-cli/v3/internal/service"
1414
"github.com/zalando/go-keyring"
1515

16+
"github.com/UpCloudLtd/upcloud-go-api/credentials"
1617
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/client"
1718
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/service"
1819
"github.com/adrg/xdg"
@@ -35,9 +36,6 @@ const (
3536

3637
// env vars custom prefix
3738
envPrefix = "UPCLOUD"
38-
39-
// keyringServiceName is the name of the service to use when using the system keyring
40-
keyringServiceName = "UpCloud"
4139
)
4240

4341
var (
@@ -100,24 +98,15 @@ func (s *Config) Load() error {
10098
}
10199
}
102100

103-
// If no credentials are provided, check if token is stored in keyring
104-
if v.GetString("token") == "" && v.GetString("username") == "" && v.GetString("password") == "" {
105-
token, err := keyring.Get(keyringServiceName, "")
106-
if err == nil {
107-
if err := v.MergeConfigMap(map[string]interface{}{"token": token}); err != nil {
108-
return fmt.Errorf("unable to merge token from keyring: %w", err)
109-
}
110-
}
111-
}
112-
113-
// If only username is provided, check if password is stored in keyring
114-
if v.GetString("username") != "" && v.GetString("token") == "" && v.GetString("password") == "" {
115-
password, err := keyring.Get(keyringServiceName, v.GetString("username"))
116-
if err == nil {
117-
if err := v.MergeConfigMap(map[string]interface{}{"password": password}); err != nil {
118-
return fmt.Errorf("unable to merge password from keyring: %w", err)
119-
}
120-
}
101+
creds, err := credentials.Parse(credentials.Credentials{
102+
Username: v.GetString("username"),
103+
Password: v.GetString("password"),
104+
Token: v.GetString("token"),
105+
})
106+
if err == nil {
107+
v.Set("username", creds.Username)
108+
v.Set("password", creds.Password)
109+
v.Set("token", creds.Token)
121110
}
122111

123112
v.Set("config", v.ConfigFileUsed())
@@ -224,7 +213,7 @@ func (s *Config) CreateService() (internal.AllServices, error) {
224213
if s.GetString("config") != "" {
225214
configDetails = fmt.Sprintf("used %s", s.GetString("config"))
226215
}
227-
return nil, clierrors.MissingCredentialsError{ConfigFile: configDetails, ServiceName: keyringServiceName}
216+
return nil, clierrors.MissingCredentialsError{ConfigFile: configDetails, ServiceName: credentials.KeyringServiceName}
228217
}
229218

230219
configs := []client.ConfigFn{
@@ -253,7 +242,7 @@ func GetVersion() string {
253242
}
254243

255244
func SaveTokenToKeyring(token string) error {
256-
return keyring.Set(keyringServiceName, "", token)
245+
return keyring.Set(credentials.KeyringServiceName, credentials.KeyringTokenUser, token)
257246
}
258247

259248
func getVersion() string {

0 commit comments

Comments
 (0)