static_provider.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package credentials
  2. import (
  3. "context"
  4. "github.com/aws/aws-sdk-go-v2/aws"
  5. )
  6. const (
  7. // StaticCredentialsName provides a name of Static provider
  8. StaticCredentialsName = "StaticCredentials"
  9. )
  10. // StaticCredentialsEmptyError is emitted when static credentials are empty.
  11. type StaticCredentialsEmptyError struct{}
  12. func (*StaticCredentialsEmptyError) Error() string {
  13. return "static credentials are empty"
  14. }
  15. // A StaticCredentialsProvider is a set of credentials which are set, and will
  16. // never expire.
  17. type StaticCredentialsProvider struct {
  18. Value aws.Credentials
  19. }
  20. // NewStaticCredentialsProvider return a StaticCredentialsProvider initialized with the AWS
  21. // credentials passed in.
  22. func NewStaticCredentialsProvider(key, secret, session string) StaticCredentialsProvider {
  23. return StaticCredentialsProvider{
  24. Value: aws.Credentials{
  25. AccessKeyID: key,
  26. SecretAccessKey: secret,
  27. SessionToken: session,
  28. },
  29. }
  30. }
  31. // Retrieve returns the credentials or error if the credentials are invalid.
  32. func (s StaticCredentialsProvider) Retrieve(_ context.Context) (aws.Credentials, error) {
  33. v := s.Value
  34. if v.AccessKeyID == "" || v.SecretAccessKey == "" {
  35. return aws.Credentials{
  36. Source: StaticCredentialsName,
  37. }, &StaticCredentialsEmptyError{}
  38. }
  39. if len(v.Source) == 0 {
  40. v.Source = StaticCredentialsName
  41. }
  42. return v, nil
  43. }