Skip to content

Commit 1b60476

Browse files
kangastariksa
andauthored
feat(tokens): add bearer token auth support (#364)
Co-authored-by: riku salkia <riku.salkia@upcloud.com>
1 parent 5b69e81 commit 1b60476

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Added support for Valkey properties
1313
- Add termination_protection to upctl database show output
14+
- Experimental support for token authentication by defining token in `UPCLOUD_TOKEN` environment variable.
1415

1516
### Changed
1617

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.22
44

55
require (
66
github.com/UpCloudLtd/progress v1.0.2
7-
github.com/UpCloudLtd/upcloud-go-api/v8 v8.14.0
7+
github.com/UpCloudLtd/upcloud-go-api/v8 v8.16.0
88
github.com/adrg/xdg v0.3.2
99
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d
1010
github.com/gemalto/flume v0.12.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym
1717
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
1818
github.com/UpCloudLtd/progress v1.0.2 h1:CTr1bBuFuXop9TEhR1PakbUMPTlUVL7Bgae9JgqXwPg=
1919
github.com/UpCloudLtd/progress v1.0.2/go.mod h1:iGxOnb9HvHW0yrLGUjHr0lxHhn7TehgWwh7a8NqK6iQ=
20-
github.com/UpCloudLtd/upcloud-go-api/v8 v8.14.0 h1:bJozr/MtrSl4P3ynq4Nkr8kGPQfPAGpGJ7/S/iVI1cc=
21-
github.com/UpCloudLtd/upcloud-go-api/v8 v8.14.0/go.mod h1:bFnrOkfsDDmsb94nnBV5eSQjjsfDnwAzLnCt9+b4t/4=
20+
github.com/UpCloudLtd/upcloud-go-api/v8 v8.16.0 h1:sF4RgPKdz5BgN+KMHeHTcTRxuq06cA/VIxV4Mcs/A6I=
21+
github.com/UpCloudLtd/upcloud-go-api/v8 v8.16.0/go.mod h1:bFnrOkfsDDmsb94nnBV5eSQjjsfDnwAzLnCt9+b4t/4=
2222
github.com/adrg/xdg v0.3.2 h1:GUSGQ5pHdev83AYhDSS1A/CX+0JIsxbiWtow2DSA+RU=
2323
github.com/adrg/xdg v0.3.2/go.mod h1:7I2hH/IT30IsupOpKZ5ue7/qNi3CoKzD6tL3HwpaRMQ=
2424
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=

internal/config/config.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,9 @@ func (s *Config) Context() context.Context {
187187
func (s *Config) CreateService() (internal.AllServices, error) {
188188
username := s.GetString("username")
189189
password := s.GetString("password")
190+
token := s.GetString("token")
190191

191-
if username == "" || password == "" {
192+
if token == "" && (username == "" || password == "") {
192193
// This might give silghtly unexpected results on OS X, as xdg.ConfigHome points to ~/Library/Application Support
193194
// while we really use/prefer/document ~/.config - which does work on osx as well but won't be displayed here.
194195
configDetails := fmt.Sprintf("default location %s", filepath.Join(xdg.ConfigHome, "upctl.yaml"))
@@ -198,7 +199,16 @@ func (s *Config) CreateService() (internal.AllServices, error) {
198199
return nil, clierrors.MissingCredentialsError{ConfigFile: configDetails}
199200
}
200201

201-
client := client.New(username, password, client.WithTimeout(s.ClientTimeout()))
202+
configs := []client.ConfigFn{
203+
client.WithTimeout(s.ClientTimeout()),
204+
}
205+
if token != "" {
206+
configs = append(configs, client.WithBearerAuth(token))
207+
} else {
208+
configs = append(configs, client.WithBasicAuth(username, password))
209+
}
210+
211+
client := client.New("", "", configs...)
202212
client.UserAgent = fmt.Sprintf("upctl/%s", GetVersion())
203213

204214
svc := service.New(client)

0 commit comments

Comments
 (0)