From 947095761a7fd920b81e1b2ec328a522d82c3b99 Mon Sep 17 00:00:00 2001 From: Qiu Jian Date: Tue, 31 Dec 2024 10:34:45 +0800 Subject: [PATCH] fix: s3 cli support set sign version --- pkg/multicloud/objectstore/objectstore.go | 30 ++++++++++++++++++++++- pkg/multicloud/objectstore/s3cli/main.go | 3 ++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/pkg/multicloud/objectstore/objectstore.go b/pkg/multicloud/objectstore/objectstore.go index dc14741ec..f93475cc5 100644 --- a/pkg/multicloud/objectstore/objectstore.go +++ b/pkg/multicloud/objectstore/objectstore.go @@ -34,6 +34,14 @@ import ( "yunion.io/x/cloudmux/pkg/multicloud" ) +type S3SignVersion string + +const ( + S3SignAlgDefault = S3SignVersion("") + S3SignAlgV4 = S3SignVersion("v4") + S3SignAlgV2 = S3SignVersion("v2") +) + type ObjectStoreClientConfig struct { cpcfg cloudprovider.ProviderConfig @@ -41,6 +49,8 @@ type ObjectStoreClientConfig struct { accessKey string accessSecret string + signVer S3SignVersion + debug bool } @@ -58,6 +68,11 @@ func (cfg *ObjectStoreClientConfig) CloudproviderConfig(cpcfg cloudprovider.Prov return cfg } +func (cfg *ObjectStoreClientConfig) SignVersion(signVer S3SignVersion) *ObjectStoreClientConfig { + cfg.signVer = signVer + return cfg +} + func (cfg *ObjectStoreClientConfig) Debug(debug bool) *ObjectStoreClientConfig { cfg.debug = debug return cfg @@ -115,7 +130,20 @@ func NewObjectStoreClientAndFetch(cfg *ObjectStoreClientConfig, doFetch bool) (* if parts.Scheme == "https" { useSsl = true } - cli, err := s3cli.New( + s3cliNewFunc := s3cli.New + + switch cfg.signVer { + case S3SignAlgV4: + log.Debugf("Use v4 signing algorithm") + s3cliNewFunc = s3cli.NewV4 + case S3SignAlgV2: + log.Debugf("Use v2 signing algorithm") + s3cliNewFunc = s3cli.NewV2 + default: + log.Debugf("s3 sign algirithm version not set, use default") + } + + cli, err := s3cliNewFunc( parts.Host, client.accessKey, client.accessSecret, diff --git a/pkg/multicloud/objectstore/s3cli/main.go b/pkg/multicloud/objectstore/s3cli/main.go index ff5f41076..6a58b9553 100644 --- a/pkg/multicloud/objectstore/s3cli/main.go +++ b/pkg/multicloud/objectstore/s3cli/main.go @@ -35,6 +35,7 @@ type BaseOptions struct { AccessKey string `help:"Access key" default:"$S3_ACCESS_KEY" metavar:"S3_ACCESS_KEY"` Secret string `help:"Secret" default:"$S3_SECRET" metavar:"S3_SECRET"` Backend string `help:"Backend driver" default:"$S3_BACKEND" metavar:"S3_BACKEND"` + SignVer string `help:"Signing Algorithm Version" default:"$S3_SIGN_VER" metavar:"S3_SIGN_VER"` SUBCOMMAND string `help:"s3cli subcommand" subcommand:"true"` } @@ -96,7 +97,7 @@ func newClient(options *BaseOptions) (cloudprovider.ICloudRegion, error) { return objectstore.NewObjectStoreClient( objectstore.NewObjectStoreClientConfig( options.AccessUrl, options.AccessKey, options.Secret, - ).Debug(options.Debug), + ).Debug(options.Debug).SignVersion(objectstore.S3SignVersion(options.SignVer)), ) }