code.dwrz.net

Go monorepo.
Log | Files | Refs

static_provider.go (1353B)


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