-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathservice_account_test.go
More file actions
172 lines (154 loc) · 4.03 KB
/
service_account_test.go
File metadata and controls
172 lines (154 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package auth
import (
"fmt"
"net/http"
"testing"
"github.com/stackitcloud/stackit-cli/internal/pkg/config"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/golang-jwt/jwt/v5"
"github.com/stackitcloud/stackit-sdk-go/core/clients"
"github.com/zalando/go-keyring"
)
const (
tokenFlow = "token"
keyFlow = "key"
)
var accessTokenSigningKey = []byte("Test")
type keyFlowMocked struct {
accessToken jwt.Token
config clients.KeyFlowConfig
tokenResponse clients.TokenResponseBody
getAccessTokenFail bool
tokenInvalid bool
}
func (f *keyFlowMocked) GetAccessToken() (string, error) {
if f.getAccessTokenFail {
return "", fmt.Errorf("error")
}
if f.tokenInvalid {
return "", nil
}
raw, err := f.accessToken.SignedString(accessTokenSigningKey)
if err != nil {
return "", fmt.Errorf("sign string from token: %w", err)
}
return raw, nil
}
func (f *keyFlowMocked) GetConfig() clients.KeyFlowConfig {
return f.config
}
func (f *keyFlowMocked) GetToken() clients.TokenResponseBody {
return f.tokenResponse
}
func (f *keyFlowMocked) RoundTrip(*http.Request) (*http.Response, error) {
return nil, nil
}
type tokenFlowMocked struct {
config clients.TokenFlowConfig
}
func (f *tokenFlowMocked) GetConfig() clients.TokenFlowConfig {
return f.config
}
func (f *tokenFlowMocked) RoundTrip(*http.Request) (*http.Response, error) {
return nil, nil
}
func TestAuthenticateServiceAccount(t *testing.T) {
tests := []struct {
description string
flowType string
getAccessTokenFail bool
tokenInvalid bool
accessToken jwt.Token
accessTokenRaw string
refreshToken string
expectedEmail string
isValid bool
}{
{
description: "base_key_flow",
flowType: keyFlow,
accessToken: *jwt.NewWithClaims(jwt.SigningMethodHS256, &tokenClaims{
Email: "test_email",
RegisteredClaims: jwt.RegisteredClaims{},
}),
refreshToken: "refresh_token",
expectedEmail: "test_email",
isValid: true,
},
{
description: "base_token_flow",
flowType: tokenFlow,
accessToken: *jwt.NewWithClaims(jwt.SigningMethodHS256, &tokenClaims{
Email: "test_email",
}),
refreshToken: "refresh_token",
expectedEmail: "test_email",
isValid: true,
},
{
description: "unsupported_flow",
flowType: "unsupported",
isValid: false,
},
{
description: "key_flow_failed_get_access_token",
flowType: keyFlow,
getAccessTokenFail: true,
isValid: false,
},
{
description: "invalid_token",
flowType: keyFlow,
tokenInvalid: true,
isValid: false,
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
keyring.MockInit()
config.InitConfig() // AuthenticateServiceAccount accesses the config
var flow http.RoundTripper
switch tt.flowType {
case keyFlow:
flow = &keyFlowMocked{
accessToken: tt.accessToken,
getAccessTokenFail: tt.getAccessTokenFail,
tokenInvalid: tt.tokenInvalid,
config: clients.KeyFlowConfig{
ServiceAccountKey: &clients.ServiceAccountKeyResponse{},
PrivateKey: "private_key",
},
tokenResponse: clients.TokenResponseBody{
RefreshToken: tt.refreshToken,
},
}
case tokenFlow:
raw, err := tt.accessToken.SignedString(accessTokenSigningKey)
if err != nil {
t.Fatalf("signing string from token: %s", err)
}
flow = &tokenFlowMocked{
config: clients.TokenFlowConfig{
ServiceAccountToken: raw,
},
}
default:
flow = &http.Transport{}
}
p := print.NewPrinter()
email, _, err := AuthenticateServiceAccount(p, flow, false)
if !tt.isValid {
if err == nil {
t.Fatalf("Expected error but no error was returned")
}
} else {
if err != nil {
t.Fatalf("Expected no error but error was returned: %v", err)
}
if tt.expectedEmail != email {
t.Fatalf("The returned email is wrong. Expected %s, got %s", tt.expectedEmail, email)
}
}
})
}
}