Skip to content

Commit a13b691

Browse files
authored
fix: create client with only access key (#1635)
1 parent b799321 commit a13b691

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

internal/auth/access_key.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package auth
2+
3+
import "net/http"
4+
5+
type AccessKeyOnly struct {
6+
// auth config may contain an access key without being authenticated
7+
AccessKey string
8+
}
9+
10+
// NewNoAuth return an auth with no authentication method
11+
func NewAccessKeyOnly(accessKey string) *AccessKeyOnly {
12+
return &AccessKeyOnly{accessKey}
13+
}
14+
15+
func (t *AccessKeyOnly) Headers() http.Header {
16+
return http.Header{}
17+
}
18+
19+
func (t *AccessKeyOnly) AnonymizedHeaders() http.Header {
20+
return http.Header{}
21+
}

scw/client.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,10 @@ func (c *Client) GetSecretKey() (secretKey string, exists bool) {
141141
func (c *Client) GetAccessKey() (accessKey string, exists bool) {
142142
if token, isToken := c.auth.(*auth.Token); isToken {
143143
return token.AccessKey, isToken
144+
} else if token, isAccessKey := c.auth.(*auth.AccessKeyOnly); isAccessKey {
145+
return token.AccessKey, isAccessKey
144146
}
147+
145148
return "", false
146149
}
147150

scw/client_option.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ func WithProfile(p *Profile) ClientOption {
7575
accessKey := ""
7676
if p.AccessKey != nil {
7777
accessKey = *p.AccessKey
78+
s.token = auth.NewAccessKeyOnly(accessKey)
7879
}
7980

8081
if p.SecretKey != nil {

scw/client_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ func TestNewClientWithNoAuth(t *testing.T) {
3838
testhelpers.Equals(t, "", accessKey)
3939
testhelpers.Assert(t, !exist, "accessKey must not exist")
4040
})
41+
t.Run("Only access key", func(t *testing.T) {
42+
client, err := NewClient(WithProfile(&Profile{
43+
AccessKey: StringPtr(testAccessKey),
44+
}))
45+
testhelpers.AssertNoError(t, err)
46+
47+
secretKey, exist := client.GetSecretKey()
48+
testhelpers.Equals(t, "", secretKey)
49+
testhelpers.Assert(t, !exist, "secretKey must not exist")
50+
51+
accessKey, exist := client.GetAccessKey()
52+
testhelpers.Assert(t, exist, "accessKey must exist")
53+
testhelpers.Equals(t, accessKey, testAccessKey)
54+
})
4155
}
4256

4357
func TestNewClientMultipleClients(t *testing.T) {

0 commit comments

Comments
 (0)