s3: drop manual credentials loading from environment

credentials.EnvAWS offers a superset of the manually implemented
credentials loading. Rework the error message that is shown if no
credentials were found but either access or secret key are set.
This commit is contained in:
Michael Eischer
2025-09-24 20:47:14 +02:00
parent 93d1e3b211
commit a30a36ca51
2 changed files with 15 additions and 17 deletions

View File

@@ -17,8 +17,6 @@ import (
type Config struct { type Config struct {
Endpoint string Endpoint string
UseHTTP bool UseHTTP bool
KeyID string
Secret options.SecretString
Bucket string Bucket string
Prefix string Prefix string
Layout string `option:"layout" help:"use this backend layout (default: auto-detect) (deprecated)"` Layout string `option:"layout" help:"use this backend layout (default: auto-detect) (deprecated)"`
@@ -35,6 +33,10 @@ type Config struct {
BucketLookup string `option:"bucket-lookup" help:"bucket lookup style: 'auto', 'dns', or 'path'"` BucketLookup string `option:"bucket-lookup" help:"bucket lookup style: 'auto', 'dns', or 'path'"`
ListObjectsV1 bool `option:"list-objects-v1" help:"use deprecated V1 api for ListObjects calls"` ListObjectsV1 bool `option:"list-objects-v1" help:"use deprecated V1 api for ListObjects calls"`
UnsafeAnonymousAuth bool `option:"unsafe-anonymous-auth" help:"use anonymous authentication"` UnsafeAnonymousAuth bool `option:"unsafe-anonymous-auth" help:"use anonymous authentication"`
// For testing only
KeyID string
Secret options.SecretString
} }
// NewConfig returns a new Config with the default values filled in. // NewConfig returns a new Config with the default values filled in.
@@ -109,12 +111,6 @@ var _ backend.ApplyEnvironmenter = &Config{}
// ApplyEnvironment saves values from the environment to the config. // ApplyEnvironment saves values from the environment to the config.
func (cfg *Config) ApplyEnvironment(prefix string) { func (cfg *Config) ApplyEnvironment(prefix string) {
if cfg.KeyID == "" {
cfg.KeyID = os.Getenv(prefix + "AWS_ACCESS_KEY_ID")
}
if cfg.Secret.String() == "" {
cfg.Secret = options.NewSecretString(os.Getenv(prefix + "AWS_SECRET_ACCESS_KEY"))
}
if cfg.Region == "" { if cfg.Region == "" {
cfg.Region = os.Getenv(prefix + "AWS_DEFAULT_REGION") cfg.Region = os.Getenv(prefix + "AWS_DEFAULT_REGION")
} }

View File

@@ -57,12 +57,6 @@ func open(cfg Config, rt http.RoundTripper) (*Backend, error) {
return nil, fmt.Errorf("feature flag `s3-restore` is required to use `-o s3.enable-restore=true`") return nil, fmt.Errorf("feature flag `s3-restore` is required to use `-o s3.enable-restore=true`")
} }
if cfg.KeyID == "" && cfg.Secret.String() != "" {
return nil, errors.Fatalf("unable to open S3 backend: Key ID ($AWS_ACCESS_KEY_ID) is empty")
} else if cfg.KeyID != "" && cfg.Secret.String() == "" {
return nil, errors.Fatalf("unable to open S3 backend: Secret ($AWS_SECRET_ACCESS_KEY) is empty")
}
if cfg.MaxRetries > 0 { if cfg.MaxRetries > 0 {
minio.MaxRetry = int(cfg.MaxRetries) minio.MaxRetry = int(cfg.MaxRetries)
} }
@@ -112,7 +106,7 @@ func getCredentials(cfg Config, tr http.RoundTripper) (*credentials.Credentials,
} }
// Chains all credential types, in the following order: // Chains all credential types, in the following order:
// - Static credentials provided by user // - Static credentials (test only)
// - AWS env vars (i.e. AWS_ACCESS_KEY_ID) // - AWS env vars (i.e. AWS_ACCESS_KEY_ID)
// - Minio env vars (i.e. MINIO_ACCESS_KEY) // - Minio env vars (i.e. MINIO_ACCESS_KEY)
// - AWS creds file (i.e. AWS_SHARED_CREDENTIALS_FILE or ~/.aws/credentials) // - AWS creds file (i.e. AWS_SHARED_CREDENTIALS_FILE or ~/.aws/credentials)
@@ -121,13 +115,13 @@ func getCredentials(cfg Config, tr http.RoundTripper) (*credentials.Credentials,
// call to a pre-defined endpoint, only valid inside // call to a pre-defined endpoint, only valid inside
// configured ec2 instances) // configured ec2 instances)
creds := credentials.NewChainCredentials([]credentials.Provider{ creds := credentials.NewChainCredentials([]credentials.Provider{
&credentials.EnvAWS{}, &credentials.Static{ // test only
&credentials.Static{
Value: credentials.Value{ Value: credentials.Value{
AccessKeyID: cfg.KeyID, AccessKeyID: cfg.KeyID,
SecretAccessKey: cfg.Secret.Unwrap(), SecretAccessKey: cfg.Secret.Unwrap(),
}, },
}, },
&credentials.EnvAWS{},
&credentials.EnvMinio{}, &credentials.EnvMinio{},
&credentials.FileAWSCredentials{}, &credentials.FileAWSCredentials{},
&credentials.FileMinioClient{}, &credentials.FileMinioClient{},
@@ -141,6 +135,14 @@ func getCredentials(cfg Config, tr http.RoundTripper) (*credentials.Credentials,
} }
if c.SignerType == credentials.SignatureAnonymous { if c.SignerType == credentials.SignatureAnonymous {
keyID := os.Getenv("AWS_ACCESS_KEY_ID")
secret := os.Getenv("AWS_SECRET_ACCESS_KEY")
if keyID == "" && secret != "" {
return nil, errors.Fatalf("no credentials found. $AWS_SECRET_ACCESS_KEY is set but $AWS_ACCESS_KEY_ID is empty")
} else if keyID != "" && secret == "" {
return nil, errors.Fatalf("no credentials found. $AWS_ACCESS_KEY_ID is set but $AWS_SECRET_ACCESS_KEY is empty")
}
// Fail if no credentials were found to prevent repeated attempts to (unsuccessfully) retrieve new credentials. // Fail if no credentials were found to prevent repeated attempts to (unsuccessfully) retrieve new credentials.
// The first attempt still has to timeout which slows down restic usage considerably. Thus, migrate towards forcing // The first attempt still has to timeout which slows down restic usage considerably. Thus, migrate towards forcing
// users to explicitly decide between authenticated and anonymous access. // users to explicitly decide between authenticated and anonymous access.