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

awsutil: add GetCallerIdentity to Create/Rotate #10

Merged
merged 3 commits into from
Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 17 additions & 3 deletions awsutil/rotate.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
// if the old one could not be deleted.
//
// Supported options: WithEnvironmentCredentials, WithSharedCredentials,
// WithAwsSession, WithUsername
// WithAwsSession, WithUsername, WithTimeout
func (c *CredentialsConfig) RotateKeys(opt ...Option) error {
if c.AccessKey == "" || c.SecretKey == "" {
return errors.New("cannot rotate credentials when either access_key or secret_key is empty")
Expand Down Expand Up @@ -62,11 +62,11 @@ func (c *CredentialsConfig) RotateKeys(opt ...Option) error {
// CreateAccessKey creates a new access/secret key pair.
//
// Supported options: WithEnvironmentCredentials, WithSharedCredentials,
// WithAwsSession, WithUsername
// WithAwsSession, WithUsername, WithTimeout
vancluever marked this conversation as resolved.
Show resolved Hide resolved
func (c *CredentialsConfig) CreateAccessKey(opt ...Option) (*iam.CreateAccessKeyOutput, error) {
opts, err := getOpts(opt...)
if err != nil {
return nil, fmt.Errorf("error reading options in RotateKeys: %w", err)
return nil, fmt.Errorf("error reading options in CreateAccessKey: %w", err)
}

sess := opts.withAwsSession
Expand Down Expand Up @@ -114,6 +114,20 @@ func (c *CredentialsConfig) CreateAccessKey(opt ...Option) (*iam.CreateAccessKey
return nil, fmt.Errorf("nil AccessKeyId or SecretAccessKey returned from aws.CreateAccessKey")
}

// Check the credentials to make sure they are usable. We only do
// this if withTimeout is non-zero to ensue that we don't
// immediately fail due to eventual consistency.
if opts.withTimeout != 0 {
newC := &CredentialsConfig{
AccessKey: *createAccessKeyRes.AccessKey.AccessKeyId,
SecretKey: *createAccessKeyRes.AccessKey.SecretAccessKey,
}

if _, err := newC.GetCallerIdentity(WithTimeout(opts.withTimeout)); err != nil {
vancluever marked this conversation as resolved.
Show resolved Hide resolved
return nil, fmt.Errorf("error verifying new credentials: %w", err)
}
}

return createAccessKeyRes, nil
}

Expand Down
7 changes: 4 additions & 3 deletions awsutil/rotate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/stretchr/testify/require"
)

const testRotationWaitTimeout = time.Second * 30

func TestRotation(t *testing.T) {
require, assert := require.New(t), assert.New(t)

Expand All @@ -32,7 +34,7 @@ func TestRotation(t *testing.T) {
}

// Create an initial key
out, err := credsConfig.CreateAccessKey(WithUsername(username))
out, err := credsConfig.CreateAccessKey(WithUsername(username), WithTimeout(testRotationWaitTimeout))
require.NoError(err)
require.NotNil(out)

Expand All @@ -49,8 +51,7 @@ func TestRotation(t *testing.T) {
WithSecretKey(secretKey),
)
require.NoError(err)
time.Sleep(10 * time.Second)
require.NoError(c.RotateKeys())
require.NoError(c.RotateKeys(WithTimeout(testRotationWaitTimeout)))
assert.NotEqual(accessKey, c.AccessKey)
assert.NotEqual(secretKey, c.SecretKey)
cleanupKey = &c.AccessKey
Expand Down