forked from Azure/azure-storage-azcopy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
credentialFactory.go
414 lines (352 loc) · 15.2 KB
/
credentialFactory.go
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// Copyright © 2017 Microsoft <wastore@microsoft.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package common
import (
"context"
"errors"
"fmt"
"math"
"strings"
"sync"
"time"
gcpUtils "cloud.google.com/go/storage"
"github.com/Azure/azure-pipeline-go/pipeline"
"github.com/aymanjarrousms/azure-storage-file-go/azfile"
"github.com/Azure/azure-storage-blob-go/azblob"
"github.com/Azure/go-autorest/autorest/adal"
"github.com/minio/minio-go"
"github.com/minio/minio-go/pkg/credentials"
"github.com/johnmic/azure-storage-azcopy/v10/azbfs"
)
// ==============================================================================================
// credential factories
// ==============================================================================================
// CredentialOpOptions contains credential operations' parameters.
type CredentialOpOptions struct {
LogInfo func(string)
LogError func(string)
Panic func(error)
CallerID string
// Used to cancel operations, if fatal error happened during operation.
Cancel context.CancelFunc
}
// callerMessage formats caller message prefix.
func (o CredentialOpOptions) callerMessage() string {
return IffString(o.CallerID == "", o.CallerID, o.CallerID+" ")
}
// logInfo logs info, if LogInfo is specified in CredentialOpOptions.
func (o CredentialOpOptions) logInfo(str string) {
if o.LogInfo != nil {
o.LogInfo(o.callerMessage() + str)
}
}
// logError logs error, if LogError is specified in CredentialOpOptions.
func (o CredentialOpOptions) logError(str string) {
if o.LogError != nil {
o.LogError(o.callerMessage() + str)
}
}
// panicError uses built-in panic if no Panic is specified in CredentialOpOptions.
func (o CredentialOpOptions) panicError(err error) {
newErr := fmt.Errorf("%s%v", o.callerMessage(), err)
if o.Panic == nil {
panic(newErr)
} else {
o.Panic(newErr)
}
}
func (o CredentialOpOptions) cancel() {
if o.Cancel != nil {
o.Cancel()
} else {
o.panicError(errors.New("cancel the operations"))
}
}
// CreateBlobCredential creates Blob credential according to credential info.
func CreateBlobCredential(ctx context.Context, credInfo CredentialInfo, options CredentialOpOptions) azblob.Credential {
credential := azblob.NewAnonymousCredential()
if credInfo.CredentialType == ECredentialType.OAuthToken() {
if credInfo.OAuthTokenInfo.IsEmpty() {
options.panicError(errors.New("invalid state, cannot get valid OAuth token information"))
}
// Create TokenCredential with refresher.
return azblob.NewTokenCredential(
credInfo.OAuthTokenInfo.AccessToken,
func(credential azblob.TokenCredential) time.Duration {
return refreshBlobToken(ctx, credInfo.OAuthTokenInfo, credential, options)
})
}
return credential
}
// CreateFileCredential creates File credential according to credential info.
func CreateFileCredential(ctx context.Context, credInfo CredentialInfo, options CredentialOpOptions) azfile.Credential {
credential := azfile.NewAnonymousCredential()
if credInfo.CredentialType == ECredentialType.OAuthToken() {
if credInfo.OAuthTokenInfo.IsEmpty() {
options.panicError(errors.New("invalid state, cannot get valid OAuth token information"))
}
// Create TokenCredential with refresher.
return azfile.NewTokenCredential(
credInfo.OAuthTokenInfo.AccessToken,
func(credential azfile.TokenCredential) time.Duration {
return refreshFileToken(ctx, credInfo.OAuthTokenInfo, credential, options)
})
}
return credential
}
// refreshPolicyHalfOfExpiryWithin is used for calculating next refresh time,
// it checkes how long it will be before the token get expired, and use half of the value as
// duration to wait.
func refreshPolicyHalfOfExpiryWithin(token *adal.Token, options CredentialOpOptions) time.Duration {
if token == nil {
// Invalid state, token should not be nil, cancel the operation and stop refresh
options.logError("invalid state, token is nil, cancel will be triggered")
options.cancel()
return time.Duration(math.MaxInt64)
}
waitDuration := token.Expires().Sub(time.Now().UTC()) / 2
// In case of refresh flooding
if waitDuration < time.Second {
waitDuration = time.Second
}
if GlobalTestOAuthInjection.DoTokenRefreshInjection {
waitDuration = GlobalTestOAuthInjection.TokenRefreshDuration
}
options.logInfo(fmt.Sprintf("next token refresh's wait duration: %v", waitDuration))
return waitDuration
}
func refreshBlobToken(ctx context.Context, tokenInfo OAuthTokenInfo, tokenCredential azblob.TokenCredential, options CredentialOpOptions) time.Duration {
newToken, err := tokenInfo.Refresh(ctx)
if err != nil {
// Fail to get new token.
if _, ok := err.(adal.TokenRefreshError); ok && strings.Contains(err.Error(), "refresh token has expired") {
options.logError(fmt.Sprintf("failed to refresh token, OAuth refresh token has expired, please log in with azcopy login command again. (Error details: %v)", err))
} else {
options.logError(fmt.Sprintf("failed to refresh token, please check error details and try to log in with azcopy login command again. (Error details: %v)", err))
}
// Try to refresh again according to original token's info.
return refreshPolicyHalfOfExpiryWithin(&(tokenInfo.Token), options)
}
// Token has been refreshed successfully.
tokenCredential.SetToken(newToken.AccessToken)
options.logInfo(fmt.Sprintf("%v token refreshed successfully", time.Now().UTC()))
// Calculate wait duration, and schedule next refresh.
return refreshPolicyHalfOfExpiryWithin(newToken, options)
}
func refreshFileToken(ctx context.Context, tokenInfo OAuthTokenInfo, tokenCredential azfile.TokenCredential, options CredentialOpOptions) time.Duration {
newToken, err := tokenInfo.Refresh(ctx)
if err != nil {
// Fail to get new token.
if _, ok := err.(adal.TokenRefreshError); ok && strings.Contains(err.Error(), "refresh token has expired") {
options.logError(fmt.Sprintf("failed to refresh token, OAuth refresh token has expired, please log in with azcopy login command again. (Error details: %v)", err))
} else {
options.logError(fmt.Sprintf("failed to refresh token, please check error details and try to log in with azcopy login command again. (Error details: %v)", err))
}
// Try to refresh again according to original token's info.
return refreshPolicyHalfOfExpiryWithin(&(tokenInfo.Token), options)
}
// Token has been refreshed successfully.
tokenCredential.SetToken(newToken.AccessToken)
options.logInfo(fmt.Sprintf("%v token refreshed successfully", time.Now().UTC()))
// Calculate wait duration, and schedule next refresh.
return refreshPolicyHalfOfExpiryWithin(newToken, options)
}
// CreateBlobFSCredential creates BlobFS credential according to credential info.
func CreateBlobFSCredential(ctx context.Context, credInfo CredentialInfo, options CredentialOpOptions) azbfs.Credential {
cred := azbfs.NewAnonymousCredential()
switch credInfo.CredentialType {
case ECredentialType.OAuthToken():
if credInfo.OAuthTokenInfo.IsEmpty() {
options.panicError(errors.New("invalid state, cannot get valid OAuth token information"))
}
// Create TokenCredential with refresher.
cred = azbfs.NewTokenCredential(
credInfo.OAuthTokenInfo.AccessToken,
func(credential azbfs.TokenCredential) time.Duration {
return refreshBlobFSToken(ctx, credInfo.OAuthTokenInfo, credential, options)
})
case ECredentialType.SharedKey():
// Get the Account Name and Key variables from environment
name := lcm.GetEnvironmentVariable(EEnvironmentVariable.AccountName())
key := lcm.GetEnvironmentVariable(EEnvironmentVariable.AccountKey())
// If the ACCOUNT_NAME and ACCOUNT_KEY are not set in environment variables
if name == "" || key == "" {
options.panicError(errors.New("ACCOUNT_NAME and ACCOUNT_KEY environment variables must be set before creating the blobfs SharedKey credential"))
}
// create the shared key credentials
cred = azbfs.NewSharedKeyCredential(name, key)
case ECredentialType.Anonymous():
// do nothing
default:
options.panicError(fmt.Errorf("invalid state, credential type %v is not supported", credInfo.CredentialType))
}
return cred
}
// CreateS3Credential creates AWS S3 credential according to credential info.
func CreateS3Credential(ctx context.Context, credInfo CredentialInfo, options CredentialOpOptions) (*credentials.Credentials, error) {
glcm := GetLifecycleMgr()
switch credInfo.CredentialType {
case ECredentialType.S3PublicBucket():
return credentials.NewStatic("", "", "", credentials.SignatureAnonymous), nil
case ECredentialType.S3AccessKey():
accessKeyID := glcm.GetEnvironmentVariable(EEnvironmentVariable.AWSAccessKeyID())
secretAccessKey := glcm.GetEnvironmentVariable(EEnvironmentVariable.AWSSecretAccessKey())
sessionToken := glcm.GetEnvironmentVariable(EEnvironmentVariable.AwsSessionToken())
// create and return s3 credential
return credentials.NewStaticV4(accessKeyID, secretAccessKey, sessionToken), nil // S3 uses V4 signature
default:
options.panicError(fmt.Errorf("invalid state, credential type %v is not supported", credInfo.CredentialType))
}
panic("work around the compiling, logic wouldn't reach here")
}
func refreshBlobFSToken(ctx context.Context, tokenInfo OAuthTokenInfo, tokenCredential azbfs.TokenCredential, options CredentialOpOptions) time.Duration {
newToken, err := tokenInfo.Refresh(ctx)
if err != nil {
// Fail to get new token.
if _, ok := err.(adal.TokenRefreshError); ok && strings.Contains(err.Error(), "refresh token has expired") {
options.logError(fmt.Sprintf("failed to refresh token, OAuth refresh token has expired, please log in with azcopy login command again. (Error details: %v)", err))
} else {
options.logError(fmt.Sprintf("failed to refresh token, please check error details and try to log in with azcopy login command again. (Error details: %v)", err))
}
// Try to refresh again according to existing token's info.
return refreshPolicyHalfOfExpiryWithin(&(tokenInfo.Token), options)
}
// Token has been refreshed successfully.
tokenCredential.SetToken(newToken.AccessToken)
options.logInfo(fmt.Sprintf("%v token refreshed successfully", time.Now().UTC()))
// Calculate wait duration, and schedule next refresh.
return refreshPolicyHalfOfExpiryWithin(newToken, options)
}
// ==============================================================================================
// S3 credential related factory methods
// ==============================================================================================
func CreateS3Client(ctx context.Context, credInfo CredentialInfo, option CredentialOpOptions, logger ILogger) (*minio.Client, error) {
if credInfo.CredentialType == ECredentialType.S3PublicBucket() {
cred := credentials.NewStatic("", "", "", credentials.SignatureAnonymous)
return minio.NewWithOptions(credInfo.S3CredentialInfo.Endpoint, &minio.Options{Creds: cred, Secure: true, Region: credInfo.S3CredentialInfo.Region})
}
// Support access key
credential, err := CreateS3Credential(ctx, credInfo, option)
if err != nil {
return nil, err
}
s3Client, err := minio.NewWithCredentials(credInfo.S3CredentialInfo.Endpoint, credential, true, credInfo.S3CredentialInfo.Region)
if logger != nil {
s3Client.TraceOn(NewS3HTTPTraceLogger(logger, pipeline.LogDebug))
}
return s3Client, err
}
type S3ClientFactory struct {
s3Clients map[CredentialInfo]*minio.Client
lock sync.RWMutex
}
// NewS3ClientFactory creates new S3 client factory.
func NewS3ClientFactory() S3ClientFactory {
return S3ClientFactory{
s3Clients: make(map[CredentialInfo]*minio.Client),
}
}
// GetS3Client gets S3 client from pool, or create a new S3 client if no client created for specific credInfo.
func (f *S3ClientFactory) GetS3Client(ctx context.Context, credInfo CredentialInfo, option CredentialOpOptions, logger ILogger) (*minio.Client, error) {
f.lock.RLock()
s3Client, ok := f.s3Clients[credInfo]
f.lock.RUnlock()
if ok {
return s3Client, nil
}
f.lock.Lock()
defer f.lock.Unlock()
if s3Client, ok := f.s3Clients[credInfo]; !ok {
newS3Client, err := CreateS3Client(ctx, credInfo, option, logger)
if err != nil {
return nil, err
}
f.s3Clients[credInfo] = newS3Client
return newS3Client, nil
} else {
return s3Client, nil
}
}
// ====================================================================
// GCP credential factory related methods
// ====================================================================
func CreateGCPClient(ctx context.Context) (*gcpUtils.Client, error) {
client, err := gcpUtils.NewClient(ctx)
return client, err
}
type GCPClientFactory struct {
gcpClients map[CredentialInfo]*gcpUtils.Client
lock sync.RWMutex
}
func NewGCPClientFactory() GCPClientFactory {
return GCPClientFactory{
gcpClients: make(map[CredentialInfo]*gcpUtils.Client),
}
}
func (f *GCPClientFactory) GetGCPClient(ctx context.Context, credInfo CredentialInfo, option CredentialOpOptions) (*gcpUtils.Client, error) {
f.lock.RLock()
gcpClient, ok := f.gcpClients[credInfo]
f.lock.RUnlock()
if ok {
return gcpClient, nil
}
f.lock.Lock()
defer f.lock.Unlock()
if gcpClient, ok := f.gcpClients[credInfo]; !ok {
newGCPClient, err := CreateGCPClient(ctx)
if err != nil {
return nil, err
}
f.gcpClients[credInfo] = newGCPClient
return newGCPClient, nil
} else {
return gcpClient, nil
}
}
// Default Encryption Algorithm Supported
const EncryptionAlgorithmAES256 string = "AES256"
func GetCpkInfo(cpkInfo bool) CpkInfo {
if !cpkInfo {
return CpkInfo{}
}
// fetch EncryptionKey and EncryptionKeySHA256 from the environment variables
glcm := GetLifecycleMgr()
encryptionKey := glcm.GetEnvironmentVariable(EEnvironmentVariable.CPKEncryptionKey())
encryptionKeySHA256 := glcm.GetEnvironmentVariable(EEnvironmentVariable.CPKEncryptionKeySHA256())
encryptionAlgorithmAES256 := EncryptionAlgorithmAES256
if encryptionKey == "" || encryptionKeySHA256 == "" {
glcm.Error("fatal: failed to fetch cpk encryption key (" + EEnvironmentVariable.CPKEncryptionKey().Name +
") or hash (" + EEnvironmentVariable.CPKEncryptionKeySHA256().Name + ") from environment variables")
}
return CpkInfo{
EncryptionKey: &encryptionKey,
EncryptionKeySha256: &encryptionKeySHA256,
EncryptionAlgorithm: &encryptionAlgorithmAES256,
}
}
func GetCpkScopeInfo(cpkScopeInfo string) CpkScopeInfo {
if cpkScopeInfo == "" {
return CpkScopeInfo{}
} else {
return CpkScopeInfo{
EncryptionScope: &cpkScopeInfo,
}
}
}