-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathstorage.go
More file actions
388 lines (337 loc) · 10.8 KB
/
storage.go
File metadata and controls
388 lines (337 loc) · 10.8 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package auth
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"github.com/stackitcloud/stackit-cli/internal/pkg/config"
pkgErrors "github.com/stackitcloud/stackit-cli/internal/pkg/errors"
"github.com/zalando/go-keyring"
)
// Name of an auth-related field
type authFieldKey string
// Possible values of authentication flows
type AuthFlow string
const (
keyringService = "stackit-cli"
textFileFolderName = "stackit"
textFileName = "cli-auth-storage.txt"
envAccessTokenName = "STACKIT_ACCESS_TOKEN"
)
const (
SESSION_EXPIRES_AT_UNIX authFieldKey = "session_expires_at_unix"
ACCESS_TOKEN authFieldKey = "access_token"
REFRESH_TOKEN authFieldKey = "refresh_token"
SERVICE_ACCOUNT_TOKEN authFieldKey = "service_account_token"
SERVICE_ACCOUNT_EMAIL authFieldKey = "service_account_email"
USER_EMAIL authFieldKey = "user_email"
SERVICE_ACCOUNT_KEY authFieldKey = "service_account_key"
PRIVATE_KEY authFieldKey = "private_key"
TOKEN_CUSTOM_ENDPOINT authFieldKey = "token_custom_endpoint"
IDP_TOKEN_ENDPOINT authFieldKey = "idp_token_endpoint" //nolint:gosec // linter false positive
)
const (
authFlowType authFieldKey = "auth_flow_type"
AUTH_FLOW_USER_TOKEN AuthFlow = "user_token"
AUTH_FLOW_SERVICE_ACCOUNT_TOKEN AuthFlow = "sa_token"
AUTH_FLOW_SERVICE_ACCOUNT_KEY AuthFlow = "sa_key"
)
// Returns all auth field keys managed by the auth storage
var authFieldKeys = []authFieldKey{
SESSION_EXPIRES_AT_UNIX,
ACCESS_TOKEN,
REFRESH_TOKEN,
SERVICE_ACCOUNT_TOKEN,
SERVICE_ACCOUNT_EMAIL,
USER_EMAIL,
SERVICE_ACCOUNT_KEY,
PRIVATE_KEY,
TOKEN_CUSTOM_ENDPOINT,
IDP_TOKEN_ENDPOINT,
authFlowType,
}
// All fields that are set when a user logs in
// These fields should match the ones in LoginUser, which is ensured by the tests
var loginAuthFieldKeys = []authFieldKey{
SESSION_EXPIRES_AT_UNIX,
ACCESS_TOKEN,
REFRESH_TOKEN,
USER_EMAIL,
}
func SetAuthFlow(value AuthFlow) error {
return SetAuthField(authFlowType, string(value))
}
// Sets the values in the auth storage according to the given map
func SetAuthFieldMap(keyMap map[authFieldKey]string) error {
for key, value := range keyMap {
err := SetAuthField(key, value)
if err != nil {
return fmt.Errorf("set auth field \"%s\": %w", key, err)
}
}
return nil
}
func SetAuthField(key authFieldKey, value string) error {
activeProfile, err := config.GetProfile()
if err != nil {
return fmt.Errorf("get profile: %w", err)
}
return setAuthFieldWithProfile(activeProfile, key, value)
}
func setAuthFieldWithProfile(profile string, key authFieldKey, value string) error {
err := setAuthFieldInKeyring(profile, key, value)
if err != nil {
errFallback := setAuthFieldInEncodedTextFile(profile, key, value)
if errFallback != nil {
return fmt.Errorf("write to keyring failed (%w), try writing to encoded text file: %w", err, errFallback)
}
}
return nil
}
func setAuthFieldInKeyring(activeProfile string, key authFieldKey, value string) error {
if activeProfile != config.DefaultProfileName {
activeProfileKeyring := filepath.Join(keyringService, activeProfile)
return keyring.Set(activeProfileKeyring, string(key), value)
}
return keyring.Set(keyringService, string(key), value)
}
func DeleteAuthField(key authFieldKey) error {
activeProfile, err := config.GetProfile()
if err != nil {
return fmt.Errorf("get profile: %w", err)
}
return deleteAuthFieldWithProfile(activeProfile, key)
}
func deleteAuthFieldWithProfile(profile string, key authFieldKey) error {
err := deleteAuthFieldInKeyring(profile, key)
if err != nil {
// if the key is not found, we can ignore the error
if !errors.Is(err, keyring.ErrNotFound) {
errFallback := deleteAuthFieldInEncodedTextFile(profile, key)
if errFallback != nil {
return fmt.Errorf("delete from keyring failed (%w), try deleting from encoded text file: %w", err, errFallback)
}
}
}
return nil
}
func deleteAuthFieldInEncodedTextFile(activeProfile string, key authFieldKey) error {
err := createEncodedTextFile(activeProfile)
if err != nil {
return err
}
textFileDir := config.GetProfileFolderPath(activeProfile)
textFilePath := filepath.Join(textFileDir, textFileName)
contentEncoded, err := os.ReadFile(textFilePath)
if err != nil {
return fmt.Errorf("read file: %w", err)
}
contentBytes, err := base64.StdEncoding.DecodeString(string(contentEncoded))
if err != nil {
return fmt.Errorf("decode file: %w", err)
}
content := map[authFieldKey]string{}
err = json.Unmarshal(contentBytes, &content)
if err != nil {
return fmt.Errorf("unmarshal file: %w", err)
}
delete(content, key)
contentBytes, err = json.Marshal(content)
if err != nil {
return fmt.Errorf("marshal file: %w", err)
}
contentEncoded = []byte(base64.StdEncoding.EncodeToString(contentBytes))
err = os.WriteFile(textFilePath, contentEncoded, 0o600)
if err != nil {
return fmt.Errorf("write file: %w", err)
}
return nil
}
func deleteAuthFieldInKeyring(activeProfile string, key authFieldKey) error {
keyringServiceLocal := keyringService
if activeProfile != config.DefaultProfileName {
keyringServiceLocal = filepath.Join(keyringService, activeProfile)
}
return keyring.Delete(keyringServiceLocal, string(key))
}
func setAuthFieldInEncodedTextFile(activeProfile string, key authFieldKey, value string) error {
err := createEncodedTextFile(activeProfile)
if err != nil {
return err
}
textFileDir := config.GetProfileFolderPath(activeProfile)
textFilePath := filepath.Join(textFileDir, textFileName)
contentEncoded, err := os.ReadFile(textFilePath)
if err != nil {
return fmt.Errorf("read file: %w", err)
}
contentBytes, err := base64.StdEncoding.DecodeString(string(contentEncoded))
if err != nil {
return fmt.Errorf("decode file: %w", err)
}
content := map[authFieldKey]string{}
err = json.Unmarshal(contentBytes, &content)
if err != nil {
return fmt.Errorf("unmarshal file: %w", err)
}
content[key] = value
contentBytes, err = json.Marshal(content)
if err != nil {
return fmt.Errorf("marshal file: %w", err)
}
contentEncoded = []byte(base64.StdEncoding.EncodeToString(contentBytes))
err = os.WriteFile(textFilePath, contentEncoded, 0o600)
if err != nil {
return fmt.Errorf("write file: %w", err)
}
return nil
}
// Populates the values in the given map according to the auth storage
func GetAuthFieldMap(keyMap map[authFieldKey]string) error {
for key := range keyMap {
value, err := GetAuthField(key)
if err != nil {
return fmt.Errorf("get auth field \"%s\": %w", key, err)
}
keyMap[key] = value
}
return nil
}
func GetAuthFlow() (AuthFlow, error) {
value, err := GetAuthField(authFlowType)
return AuthFlow(value), err
}
func GetAuthField(key authFieldKey) (string, error) {
activeProfile, err := config.GetProfile()
if err != nil {
return "", fmt.Errorf("get profile: %w", err)
}
return getAuthFieldWithProfile(activeProfile, key)
}
func getAuthFieldWithProfile(profile string, key authFieldKey) (string, error) {
value, err := getAuthFieldFromKeyring(profile, key)
if err != nil {
var errFallback error
value, errFallback = getAuthFieldFromEncodedTextFile(profile, key)
if errFallback != nil {
return "", fmt.Errorf("read from keyring: %w, read from encoded file as fallback: %w", err, errFallback)
}
}
return value, nil
}
func getAuthFieldFromKeyring(activeProfile string, key authFieldKey) (string, error) {
if activeProfile != config.DefaultProfileName {
activeProfileKeyring := filepath.Join(keyringService, activeProfile)
return keyring.Get(activeProfileKeyring, string(key))
}
return keyring.Get(keyringService, string(key))
}
func getAuthFieldFromEncodedTextFile(activeProfile string, key authFieldKey) (string, error) {
err := createEncodedTextFile(activeProfile)
if err != nil {
return "", err
}
textFileDir := config.GetProfileFolderPath(activeProfile)
textFilePath := filepath.Join(textFileDir, textFileName)
contentEncoded, err := os.ReadFile(textFilePath)
if err != nil {
return "", fmt.Errorf("read file: %w", err)
}
contentBytes, err := base64.StdEncoding.DecodeString(string(contentEncoded))
if err != nil {
return "", fmt.Errorf("decode file: %w", err)
}
var content map[authFieldKey]string
err = json.Unmarshal(contentBytes, &content)
if err != nil {
return "", fmt.Errorf("unmarshal file: %w", err)
}
value, ok := content[key]
if !ok {
return "", fmt.Errorf("value not found")
}
return value, nil
}
// Checks if the encoded text file exist.
// If it doesn't, creates it with the content "{}" encoded.
// If it does, does nothing (and returns nil).
func createEncodedTextFile(activeProfile string) error {
textFileDir := config.GetProfileFolderPath(activeProfile)
textFilePath := filepath.Join(textFileDir, textFileName)
err := os.MkdirAll(textFileDir, 0o750)
if err != nil {
return fmt.Errorf("create file dir: %w", err)
}
_, err = os.Stat(textFilePath)
if !os.IsNotExist(err) {
return nil
}
contentEncoded := base64.StdEncoding.EncodeToString([]byte("{}"))
err = os.WriteFile(textFilePath, []byte(contentEncoded), 0o600)
if err != nil {
return fmt.Errorf("create file: %w", err)
}
return nil
}
// GetProfileEmail returns the email of the user or service account associated with the given profile.
// If the profile is not authenticated or the email can't be obtained, it returns an empty string.
func GetProfileEmail(profile string) string {
value, err := getAuthFieldWithProfile(profile, authFlowType)
if err != nil {
return ""
}
var email string
switch AuthFlow(value) {
case AUTH_FLOW_USER_TOKEN:
email, err = getAuthFieldWithProfile(profile, USER_EMAIL)
if err != nil {
email = ""
}
case AUTH_FLOW_SERVICE_ACCOUNT_TOKEN, AUTH_FLOW_SERVICE_ACCOUNT_KEY:
email, err = getAuthFieldWithProfile(profile, SERVICE_ACCOUNT_EMAIL)
if err != nil {
email = ""
}
}
return email
}
func LoginUser(email, accessToken, refreshToken, sessionExpiresAtUnix string) error {
authFields := map[authFieldKey]string{
SESSION_EXPIRES_AT_UNIX: sessionExpiresAtUnix,
ACCESS_TOKEN: accessToken,
REFRESH_TOKEN: refreshToken,
USER_EMAIL: email,
}
err := SetAuthFieldMap(authFields)
if err != nil {
return fmt.Errorf("set auth fields: %w", err)
}
return nil
}
func LogoutUser() error {
for _, key := range loginAuthFieldKeys {
err := DeleteAuthField(key)
if err != nil {
return fmt.Errorf("delete auth field \"%s\": %w", key, err)
}
}
return nil
}
func DeleteProfileAuth(profile string) error {
err := config.ValidateProfile(profile)
if err != nil {
return fmt.Errorf("validate profile: %w", err)
}
if profile == config.DefaultProfileName {
return &pkgErrors.DeleteDefaultProfile{DefaultProfile: config.DefaultProfileName}
}
for _, key := range authFieldKeys {
err := deleteAuthFieldWithProfile(profile, key)
if err != nil {
return fmt.Errorf("delete auth field \"%s\": %w", key, err)
}
}
return nil
}