Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added credential provider example #1438

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions content/en/user-guide/integrations/sdks/go/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,120 @@
})
// ...
}{{< /tab >}}

{{< tab header="aws-go-sdk-v2 Credential Provider" lang="golang" >}}
package main

import (
"context"

Check failure on line 86 in content/en/user-guide/integrations/sdks/go/index.md

View workflow job for this annotation

GitHub Actions / Markdown Lint

Hard tabs

content/en/user-guide/integrations/sdks/go/index.md:86:1 MD010/no-hard-tabs Hard tabs [Column: 1] https://github.com/DavidAnson/markdownlint/blob/v0.34.0/doc/md010.md
"github.com/aws/aws-sdk-go-v2/aws"

Check failure on line 87 in content/en/user-guide/integrations/sdks/go/index.md

View workflow job for this annotation

GitHub Actions / Markdown Lint

Hard tabs

content/en/user-guide/integrations/sdks/go/index.md:87:1 MD010/no-hard-tabs Hard tabs [Column: 1] https://github.com/DavidAnson/markdownlint/blob/v0.34.0/doc/md010.md
"github.com/aws/aws-sdk-go-v2/config"

Check failure on line 88 in content/en/user-guide/integrations/sdks/go/index.md

View workflow job for this annotation

GitHub Actions / Markdown Lint

Hard tabs

content/en/user-guide/integrations/sdks/go/index.md:88:1 MD010/no-hard-tabs Hard tabs [Column: 1] https://github.com/DavidAnson/markdownlint/blob/v0.34.0/doc/md010.md
)

const (
AwsLocalEndpoint = "http://localhost:4566"

Check failure on line 92 in content/en/user-guide/integrations/sdks/go/index.md

View workflow job for this annotation

GitHub Actions / Markdown Lint

Hard tabs

content/en/user-guide/integrations/sdks/go/index.md:92:1 MD010/no-hard-tabs Hard tabs [Column: 1] https://github.com/DavidAnson/markdownlint/blob/v0.34.0/doc/md010.md
AwsLocalCredentialsName = "AwsLocalCredentials"

Check failure on line 93 in content/en/user-guide/integrations/sdks/go/index.md

View workflow job for this annotation

GitHub Actions / Markdown Lint

Hard tabs

content/en/user-guide/integrations/sdks/go/index.md:93:1 MD010/no-hard-tabs Hard tabs [Column: 1] https://github.com/DavidAnson/markdownlint/blob/v0.34.0/doc/md010.md
AwsLocalDefaultRegion = "us-east-1"

Check failure on line 94 in content/en/user-guide/integrations/sdks/go/index.md

View workflow job for this annotation

GitHub Actions / Markdown Lint

Hard tabs

content/en/user-guide/integrations/sdks/go/index.md:94:1 MD010/no-hard-tabs Hard tabs [Column: 1] https://github.com/DavidAnson/markdownlint/blob/v0.34.0/doc/md010.md
AwsLocalAccountId = "000000000000"

Check failure on line 95 in content/en/user-guide/integrations/sdks/go/index.md

View workflow job for this annotation

GitHub Actions / Markdown Lint

Hard tabs

content/en/user-guide/integrations/sdks/go/index.md:95:1 MD010/no-hard-tabs Hard tabs [Column: 1] https://github.com/DavidAnson/markdownlint/blob/v0.34.0/doc/md010.md
AwsLocalAccessKey = "test"

Check failure on line 96 in content/en/user-guide/integrations/sdks/go/index.md

View workflow job for this annotation

GitHub Actions / Markdown Lint

Trailing spaces

content/en/user-guide/integrations/sdks/go/index.md:96:34 MD009/no-trailing-spaces Trailing spaces [Expected: 0 or 2; Actual: 9] https://github.com/DavidAnson/markdownlint/blob/v0.34.0/doc/md009.md

Check failure on line 96 in content/en/user-guide/integrations/sdks/go/index.md

View workflow job for this annotation

GitHub Actions / Markdown Lint

Hard tabs

content/en/user-guide/integrations/sdks/go/index.md:96:1 MD010/no-hard-tabs Hard tabs [Column: 1] https://github.com/DavidAnson/markdownlint/blob/v0.34.0/doc/md010.md
AwsLocalSecret = "test"

Check failure on line 97 in content/en/user-guide/integrations/sdks/go/index.md

View workflow job for this annotation

GitHub Actions / Markdown Lint

Trailing spaces

content/en/user-guide/integrations/sdks/go/index.md:97:34 MD009/no-trailing-spaces Trailing spaces [Expected: 0 or 2; Actual: 9] https://github.com/DavidAnson/markdownlint/blob/v0.34.0/doc/md009.md
)

var (
ErrorAwsLocalCredentialsEmpty = "awslocal credentials are empty"
)

// NewAwsLocalConfig returns a new [aws.Config] object configured to connect to LocalStack.
func NewAwsLocalConfig(ctx context.Context, accountId, endpoint, region, key, secret string, optFns ...func(*config.LoadOptions) error) aws.Config {
opts := []func(*config.LoadOptions) error{
config.WithRegion(region),
config.WithCredentialsProvider(NewAwsLocalCredentialsProvider(key, secret, accountId)),
}
opts = append(opts, optFns...)

cfg, err := config.LoadDefaultConfig(ctx, opts...)
if err != nil {
panic(err)
}

cfg.BaseEndpoint = aws.String(endpoint)
return cfg
}

// NewDefaultAwsLocalConfig returns a new default [aws.Config] object configured to connect to the default LocalStack.
func NewDefaultAwsLocalConfig() aws.Config {
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion(AwsLocalDefaultRegion),
config.WithCredentialsProvider(NewDefaultAwsLocalCredentialsProvider()))
if err != nil {
panic(err)
}
cfg.BaseEndpoint = aws.String(AwsLocalEndpoint)
return cfg
}

// to ensure AwsLocalCredentialsProvider implements the [aws.CredentialsProvider] interface
var _ aws.CredentialsProvider = (*AwsLocalCredentialsProvider)(nil)

// A AwsLocalCredentialsProvider is a static credentials provider that returns the same credentials,
// designed for use with LocalStack
type AwsLocalCredentialsProvider struct {
Value aws.Credentials
}

// NewDefaultAwsLocalCredentialsProvider returns an AwsLocalCredentialsProvider
// initialized with the default AwsLocal credentials.
func NewDefaultAwsLocalCredentialsProvider() AwsLocalCredentialsProvider {
return NewAwsLocalCredentialsProvider(AwsLocalAccessKey, AwsLocalSecret, AwsLocalAccountId)
}

// NewAwsLocalCredentialsProvider return a StaticCredentialsProvider initialized with the AWS credentials passed in.
func NewAwsLocalCredentialsProvider(key, secret, accountId string) AwsLocalCredentialsProvider {
return AwsLocalCredentialsProvider{
Value: aws.Credentials{
AccessKeyID: key,
SecretAccessKey: secret,
AccountID: accountId,
SessionToken: "",
CanExpire: false,
},
}
}

// Retrieve returns the credentials or error if the credentials are invalid.
func (s AwsLocalCredentialsProvider) Retrieve(_ context.Context) (aws.Credentials, error) {
v := s.Value
if v.AccessKeyID == "" || v.SecretAccessKey == "" {
return aws.Credentials{
Source: AwsLocalCredentialsName,
}, &AwsLocalCredentialsEmptyError{}
}

if len(v.Source) == 0 {
v.Source = AwsLocalCredentialsName
}

return v, nil
}

func (s AwsLocalCredentialsProvider) IsExpired() bool {
return false
}

// AwsLocalCredentialsEmptyError is emitted when the AwsLocal credentials are empty.
type AwsLocalCredentialsEmptyError struct{}

func (*AwsLocalCredentialsEmptyError) Error() string {
return ErrorAwsLocalCredentialsEmpty
}

func main() {
// build an aws.Credential-compliant configuration
awsCfg := NewDefaultAwsLocalConfig()

// ...
}
{{< /tab >}}
{{< /tabpane >}}

## Resources
Expand Down