From d59a570c3d9e91ab5e8e7ddc4282c2087d390f96 Mon Sep 17 00:00:00 2001 From: Milos Gajdos Date: Tue, 19 Dec 2023 14:34:01 +0000 Subject: [PATCH] update: set User-Agent header in GCS storage driver Signed-off-by: Milos Gajdos --- registry/storage/driver/gcs/gcs.go | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/registry/storage/driver/gcs/gcs.go b/registry/storage/driver/gcs/gcs.go index beb9261e..d2cdb596 100644 --- a/registry/storage/driver/gcs/gcs.go +++ b/registry/storage/driver/gcs/gcs.go @@ -155,6 +155,7 @@ func FromParameters(ctx context.Context, parameters map[string]interface{}) (sto jwtConf := new(jwt.Config) var err error var gcs *storage.Client + var options []option.ClientOption if keyfile, ok := parameters["keyfile"]; ok { jsonKey, err := os.ReadFile(fmt.Sprint(keyfile)) if err != nil { @@ -165,10 +166,7 @@ func FromParameters(ctx context.Context, parameters map[string]interface{}) (sto return nil, err } ts = jwtConf.TokenSource(ctx) - gcs, err = storage.NewClient(ctx, option.WithCredentialsFile(fmt.Sprint(keyfile))) - if err != nil { - return nil, err - } + options = append(options, option.WithCredentialsFile(fmt.Sprint(keyfile))) } else if credentials, ok := parameters["credentials"]; ok { credentialMap, ok := credentials.(map[interface{}]interface{}) if !ok { @@ -194,10 +192,7 @@ func FromParameters(ctx context.Context, parameters map[string]interface{}) (sto return nil, err } ts = jwtConf.TokenSource(ctx) - gcs, err = storage.NewClient(ctx, option.WithCredentialsJSON(data)) - if err != nil { - return nil, err - } + options = append(options, option.WithCredentialsJSON(data)) } else { var err error // DefaultTokenSource is a convenience method. It first calls FindDefaultCredentials, @@ -207,12 +202,19 @@ func FromParameters(ctx context.Context, parameters map[string]interface{}) (sto if err != nil { return nil, err } - gcs, err = storage.NewClient(ctx) - if err != nil { - return nil, err + } + + if userAgent, ok := parameters["useragent"]; ok { + if ua, ok := userAgent.(string); ok && ua != "" { + options = append(options, option.WithUserAgent(ua)) } } + gcs, err = storage.NewClient(ctx, options...) + if err != nil { + return nil, err + } + maxConcurrency, err := base.GetLimitFromParameter(parameters["maxconcurrency"], minConcurrency, defaultMaxConcurrency) if err != nil { return nil, fmt.Errorf("maxconcurrency config error: %s", err)