Documentation ¶
Overview ¶
Package credscache provides credentials caching utilities for the AWS SDK for Go v1.
Inject the file cache provider ¶
By default, the file cache provider outputs cache files to the current directory.
sess, err := session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, AssumeRoleTokenProvider: stscreds.StdinTokenProvider, }) if err != nil { log.Fatal(err) } injected, err := credscache.InjectFileCacheProvider(sess.Config) if err != nil { log.Fatal(err) } if !injected { log.Print("unable to inject file cache provider") }
You can share cache with the AWS CLI by specifying `$HOME/.aws/cli/cache` (experimental feature).
sess, err := session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, AssumeRoleTokenProvider: stscreds.StdinTokenProvider, }) if err != nil { log.Fatal(err) } injected, err := credscache.InjectFileCacheProvider(sess.Config, func(o *credscache.FileCacheOptions) { home, _ := os.UserHomeDir() o.FileCacheDir = filepath.Join(home, ".aws/cli/cache") }) if err != nil { log.Fatal(err) } if !injected { log.Print("unable to inject file cache provider") }
Index ¶
- Constants
- Variables
- func AssumeRoleCacheKey(provider *stscreds.AssumeRoleProvider) (string, error)
- func InjectFileCacheProvider(cfg *aws.Config, optFns ...func(o *FileCacheOptions)) (bool, error)
- func LoadCredentials(path string) (*credentials.Value, time.Time, error)
- func StoreCredentials(path string, creds *credentials.Value, expires time.Time) error
- type CredentialsUnsafeAccessor
- type FileCacheOptions
- type FileCacheProvider
- type FileCacheProviderError
- type InjectionError
Examples ¶
Constants ¶
View Source
const (
FileCacheProviderName = "FileCacheProvider"
)
Variables ¶
View Source
var (
ErrNilPointer = credscache.ErrNilPointer
)
Functions ¶
func AssumeRoleCacheKey ¶
func AssumeRoleCacheKey(provider *stscreds.AssumeRoleProvider) (string, error)
Example ¶
package main import ( "fmt" "log" credscache "github.com/Aton-Kish/aws-credscache-go/sdkv1" "github.com/aws/aws-sdk-go/aws/credentials/stscreds" ) func main() { key, err := credscache.AssumeRoleCacheKey(&stscreds.AssumeRoleProvider{ RoleARN: "role_arn", }) if err != nil { log.Fatal(err) } fmt.Println(key) }
Output: de1969e7a880d858c9bef3ba110acf78869d4527
Example (WithRoleSessionNameAndMFASerial) ¶
package main import ( "fmt" "log" credscache "github.com/Aton-Kish/aws-credscache-go/sdkv1" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/credentials/stscreds" ) func main() { key, err := credscache.AssumeRoleCacheKey(&stscreds.AssumeRoleProvider{ RoleARN: "role_arn", RoleSessionName: "role_session_name", SerialNumber: aws.String("mfa_serial"), }) if err != nil { log.Fatal(err) } fmt.Println(key) }
Output: cda918cacd9e1d1c71d510d187e90c5817e04b97
func InjectFileCacheProvider ¶
func InjectFileCacheProvider(cfg *aws.Config, optFns ...func(o *FileCacheOptions)) (bool, error)
Example ¶
package main import ( "log" credscache "github.com/Aton-Kish/aws-credscache-go/sdkv1" "github.com/aws/aws-sdk-go/aws/credentials/stscreds" "github.com/aws/aws-sdk-go/aws/session" ) func main() { sess, err := session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, AssumeRoleTokenProvider: stscreds.StdinTokenProvider, }) if err != nil { log.Fatal(err) } injected, err := credscache.InjectFileCacheProvider(sess.Config) if err != nil { log.Fatal(err) } if !injected { log.Print("unable to inject file cache provider") } }
Output:
Example (SpecifiedFileCacheDir) ¶
package main import ( "log" "os" "path/filepath" credscache "github.com/Aton-Kish/aws-credscache-go/sdkv1" "github.com/aws/aws-sdk-go/aws/credentials/stscreds" "github.com/aws/aws-sdk-go/aws/session" ) func main() { sess, err := session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, AssumeRoleTokenProvider: stscreds.StdinTokenProvider, }) if err != nil { log.Fatal(err) } injected, err := credscache.InjectFileCacheProvider(sess.Config, func(o *credscache.FileCacheOptions) { home, _ := os.UserHomeDir() o.FileCacheDir = filepath.Join(home, ".aws/cli/cache") }) if err != nil { log.Fatal(err) } if !injected { log.Print("unable to inject file cache provider") } }
Output:
func LoadCredentials ¶
Example ¶
package main import ( "fmt" "log" credscache "github.com/Aton-Kish/aws-credscache-go/sdkv1" ) func main() { path := "/home/gopher/.aws/cli/cache/de1969e7a880d858c9bef3ba110acf78869d4527.json" creds, expires, err := credscache.LoadCredentials(path) if err != nil { log.Fatal(err) } fmt.Println(creds) // &credentials.Value{ // AccessKeyID: "AccessKeyID", // SecretAccessKey: "SecretAccessKey", // SessionToken: "SessionToken", // ProviderName: "", // } fmt.Println(expires) // time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC) }
Output:
func StoreCredentials ¶
Example ¶
package main import ( "log" "time" credscache "github.com/Aton-Kish/aws-credscache-go/sdkv1" "github.com/aws/aws-sdk-go/aws/credentials" ) func main() { path := "/home/gopher/.aws/cli/cache/de1969e7a880d858c9bef3ba110acf78869d4527.json" creds := &credentials.Value{ AccessKeyID: "AccessKeyID", SecretAccessKey: "SecretAccessKey", SessionToken: "SessionToken", ProviderName: "TestProvider", } expires := time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC) if err := credscache.StoreCredentials(path, creds, expires); err != nil { log.Fatal(err) } }
Output:
Types ¶
type CredentialsUnsafeAccessor ¶
type CredentialsUnsafeAccessor struct {
// contains filtered or unexported fields
}
func NewCredentialsUnsafeAccessor ¶
func NewCredentialsUnsafeAccessor(ptr *credentials.Credentials) (*CredentialsUnsafeAccessor, error)
func (*CredentialsUnsafeAccessor) Provider ¶
func (a *CredentialsUnsafeAccessor) Provider() credentials.Provider
func (*CredentialsUnsafeAccessor) SetProvider ¶
func (a *CredentialsUnsafeAccessor) SetProvider(provider credentials.Provider)
type FileCacheOptions ¶
type FileCacheProvider ¶
type FileCacheProvider struct { credentials.Expiry // contains filtered or unexported fields }
func NewFileCacheProvider ¶
func NewFileCacheProvider(provider credentials.ProviderWithContext, cacheKey string, optFns ...func(o *FileCacheOptions)) *FileCacheProvider
func (*FileCacheProvider) IsExpired ¶
func (p *FileCacheProvider) IsExpired() bool
func (*FileCacheProvider) Retrieve ¶
func (p *FileCacheProvider) Retrieve() (credentials.Value, error)
func (*FileCacheProvider) RetrieveWithContext ¶
func (p *FileCacheProvider) RetrieveWithContext(ctx context.Context) (credentials.Value, error)
type FileCacheProviderError ¶
type FileCacheProviderError = credscache.FileCacheProviderError
type InjectionError ¶
type InjectionError = credscache.InjectionError
Click to show internal directories.
Click to hide internal directories.