-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix: load gcs credentials and client inside DriverConstructor #4218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like this code should be inside gcsDriverConstructor
:
distribution/registry/storage/driver/gcs/gcs_test.go
Lines 37 to 69 in 012adca
jsonKey, err := os.ReadFile(credentials) | |
if err != nil { | |
panic(fmt.Sprintf("Error reading JSON key : %v", err)) | |
} | |
var ts oauth2.TokenSource | |
var email string | |
var privateKey []byte | |
ts, err = google.DefaultTokenSource(dcontext.Background(), storage.ScopeFullControl) | |
if err != nil { | |
// Assume that the file contents are within the environment variable since it exists | |
// but does not contain a valid file path | |
jwtConfig, err := google.JWTConfigFromJSON(jsonKey, storage.ScopeFullControl) | |
if err != nil { | |
panic(fmt.Sprintf("Error reading JWT config : %s", err)) | |
} | |
email = jwtConfig.Email | |
privateKey = jwtConfig.PrivateKey | |
if len(privateKey) == 0 { | |
panic("Error reading JWT config : missing private_key property") | |
} | |
if email == "" { | |
panic("Error reading JWT config : missing client_email property") | |
} | |
ts = jwtConfig.TokenSource(dcontext.Background()) | |
} | |
gcs, err := storage.NewClient(dcontext.Background(), option.WithCredentialsJSON(jsonKey)) | |
if err != nil { | |
panic(fmt.Sprintf("Error initializing gcs client : %v", err)) | |
} | |
We do similar things in S3 driver, where anything that might return error whilst constructing the driver is inside the constructor func:
distribution/registry/storage/driver/s3-aws/s3_test.go
Lines 51 to 131 in 012adca
s3DriverConstructor = func(rootDirectory, storageClass string) (*Driver, error) { | |
encryptBool := false | |
if encrypt != "" { | |
encryptBool, err = strconv.ParseBool(encrypt) | |
if err != nil { | |
return nil, err | |
} | |
} | |
secureBool := true | |
if secure != "" { | |
secureBool, err = strconv.ParseBool(secure) | |
if err != nil { | |
return nil, err | |
} | |
} | |
skipVerifyBool := false | |
if skipVerify != "" { | |
skipVerifyBool, err = strconv.ParseBool(skipVerify) | |
if err != nil { | |
return nil, err | |
} | |
} | |
v4Bool := true | |
if v4Auth != "" { | |
v4Bool, err = strconv.ParseBool(v4Auth) | |
if err != nil { | |
return nil, err | |
} | |
} | |
forcePathStyleBool := true | |
if forcePathStyle != "" { | |
forcePathStyleBool, err = strconv.ParseBool(forcePathStyle) | |
if err != nil { | |
return nil, err | |
} | |
} | |
useDualStackBool := false | |
if useDualStack != "" { | |
useDualStackBool, err = strconv.ParseBool(useDualStack) | |
} | |
accelerateBool := true | |
if accelerate != "" { | |
accelerateBool, err = strconv.ParseBool(accelerate) | |
if err != nil { | |
return nil, err | |
} | |
} | |
parameters := DriverParameters{ | |
accessKey, | |
secretKey, | |
bucket, | |
region, | |
regionEndpoint, | |
forcePathStyleBool, | |
encryptBool, | |
keyID, | |
secureBool, | |
skipVerifyBool, | |
v4Bool, | |
minChunkSize, | |
defaultMultipartCopyChunkSize, | |
defaultMultipartCopyMaxConcurrency, | |
defaultMultipartCopyThresholdSize, | |
rootDirectory, | |
storageClass, | |
driverName + "-test", | |
objectACL, | |
sessionToken, | |
useDualStackBool, | |
accelerateBool, | |
getS3LogLevelFromParam(logLevel), | |
} | |
return New(context.Background(), parameters) | |
} |
f3ea6b8
to
09bbf18
Compare
Signed-off-by: Paul Meyer <49727155+katexochen@users.noreply.github.com>
09bbf18
to
5bd7f25
Compare
PTAL @thaJeztah |
ping @thaJeztah |
@corhere mind having a look at this PR? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
Previously the init function would panic with:
Error reading JSON key : open : no such file or directory