mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-12-29 23:30:04 +01:00
87c77727e4
* lib/backup/s3remote: update AWS SDK to v2 * Update lib/backup/s3remote/s3.go Co-authored-by: Aliaksandr Valialkin <valyala@victoriametrics.com> * lib/backup/s3remote: refactor error handling Co-authored-by: Aliaksandr Valialkin <valyala@victoriametrics.com>
54 lines
1.3 KiB
Go
54 lines
1.3 KiB
Go
package credentials
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/aws"
|
|
)
|
|
|
|
const (
|
|
// StaticCredentialsName provides a name of Static provider
|
|
StaticCredentialsName = "StaticCredentials"
|
|
)
|
|
|
|
// StaticCredentialsEmptyError is emitted when static credentials are empty.
|
|
type StaticCredentialsEmptyError struct{}
|
|
|
|
func (*StaticCredentialsEmptyError) Error() string {
|
|
return "static credentials are empty"
|
|
}
|
|
|
|
// A StaticCredentialsProvider is a set of credentials which are set, and will
|
|
// never expire.
|
|
type StaticCredentialsProvider struct {
|
|
Value aws.Credentials
|
|
}
|
|
|
|
// NewStaticCredentialsProvider return a StaticCredentialsProvider initialized with the AWS
|
|
// credentials passed in.
|
|
func NewStaticCredentialsProvider(key, secret, session string) StaticCredentialsProvider {
|
|
return StaticCredentialsProvider{
|
|
Value: aws.Credentials{
|
|
AccessKeyID: key,
|
|
SecretAccessKey: secret,
|
|
SessionToken: session,
|
|
},
|
|
}
|
|
}
|
|
|
|
// Retrieve returns the credentials or error if the credentials are invalid.
|
|
func (s StaticCredentialsProvider) Retrieve(_ context.Context) (aws.Credentials, error) {
|
|
v := s.Value
|
|
if v.AccessKeyID == "" || v.SecretAccessKey == "" {
|
|
return aws.Credentials{
|
|
Source: StaticCredentialsName,
|
|
}, &StaticCredentialsEmptyError{}
|
|
}
|
|
|
|
if len(v.Source) == 0 {
|
|
v.Source = StaticCredentialsName
|
|
}
|
|
|
|
return v, nil
|
|
}
|