Skip to content

Commit

Permalink
fix: s3 cli support set sign version
Browse files Browse the repository at this point in the history
  • Loading branch information
Qiu Jian committed Dec 31, 2024
1 parent 7ae9257 commit 9470957
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
30 changes: 29 additions & 1 deletion pkg/multicloud/objectstore/objectstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,23 @@ 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

endpoint string
accessKey string
accessSecret string

signVer S3SignVersion

debug bool
}

Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion pkg/multicloud/objectstore/s3cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down Expand Up @@ -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)),
)
}

Expand Down

0 comments on commit 9470957

Please sign in to comment.