github.com/aavshr/aws-sdk-go@v1.41.3/aws/credentials/env_provider.go (about)

     1  package credentials
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/aavshr/aws-sdk-go/aws/awserr"
     7  )
     8  
     9  // EnvProviderName provides a name of Env provider
    10  const EnvProviderName = "EnvProvider"
    11  
    12  var (
    13  	// ErrAccessKeyIDNotFound is returned when the AWS Access Key ID can't be
    14  	// found in the process's environment.
    15  	ErrAccessKeyIDNotFound = awserr.New("EnvAccessKeyNotFound", "AWS_ACCESS_KEY_ID or AWS_ACCESS_KEY not found in environment", nil)
    16  
    17  	// ErrSecretAccessKeyNotFound is returned when the AWS Secret Access Key
    18  	// can't be found in the process's environment.
    19  	ErrSecretAccessKeyNotFound = awserr.New("EnvSecretNotFound", "AWS_SECRET_ACCESS_KEY or AWS_SECRET_KEY not found in environment", nil)
    20  )
    21  
    22  // A EnvProvider retrieves credentials from the environment variables of the
    23  // running process. Environment credentials never expire.
    24  //
    25  // Environment variables used:
    26  //
    27  // * Access Key ID:     AWS_ACCESS_KEY_ID or AWS_ACCESS_KEY
    28  //
    29  // * Secret Access Key: AWS_SECRET_ACCESS_KEY or AWS_SECRET_KEY
    30  type EnvProvider struct {
    31  	retrieved bool
    32  }
    33  
    34  // NewEnvCredentials returns a pointer to a new Credentials object
    35  // wrapping the environment variable provider.
    36  func NewEnvCredentials() *Credentials {
    37  	return NewCredentials(&EnvProvider{})
    38  }
    39  
    40  // Retrieve retrieves the keys from the environment.
    41  func (e *EnvProvider) Retrieve() (Value, error) {
    42  	e.retrieved = false
    43  
    44  	id := os.Getenv("AWS_ACCESS_KEY_ID")
    45  	if id == "" {
    46  		id = os.Getenv("AWS_ACCESS_KEY")
    47  	}
    48  
    49  	secret := os.Getenv("AWS_SECRET_ACCESS_KEY")
    50  	if secret == "" {
    51  		secret = os.Getenv("AWS_SECRET_KEY")
    52  	}
    53  
    54  	if id == "" {
    55  		return Value{ProviderName: EnvProviderName}, ErrAccessKeyIDNotFound
    56  	}
    57  
    58  	if secret == "" {
    59  		return Value{ProviderName: EnvProviderName}, ErrSecretAccessKeyNotFound
    60  	}
    61  
    62  	e.retrieved = true
    63  	return Value{
    64  		AccessKeyID:     id,
    65  		SecretAccessKey: secret,
    66  		SessionToken:    os.Getenv("AWS_SESSION_TOKEN"),
    67  		ProviderName:    EnvProviderName,
    68  	}, nil
    69  }
    70  
    71  // IsExpired returns if the credentials have been retrieved.
    72  func (e *EnvProvider) IsExpired() bool {
    73  	return !e.retrieved
    74  }