Skip to content

Commit

Permalink
Make empty config an error in EBS client constructor (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hakan Memisoglu committed Sep 11, 2019
1 parent 54205b7 commit 6c1cadc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
15 changes: 7 additions & 8 deletions pkg/blockstorage/awsebs/awsebs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
Expand Down Expand Up @@ -76,14 +75,14 @@ func NewProvider(config map[string]string) (blockstorage.Provider, error) {

// newEC2Client returns ec2 client struct.
func newEC2Client(awsRegion string, config *aws.Config, role string) (*EC2, error) {
if config == nil {
return nil, errors.New("Invalid empty AWS config")
}
s, err := session.NewSession(config)
if err != nil {
return nil, errors.Wrap(err, "Failed to create session for EFS")
}
var creds *credentials.Credentials
if config != nil {
creds = config.Credentials
}
creds := config.Credentials
if role != "" {
creds = stscreds.NewCredentials(s, role)
}
Expand Down Expand Up @@ -230,15 +229,15 @@ func (s *ebsStorage) SnapshotCopy(ctx context.Context, from, to blockstorage.Sna
return nil, errors.Errorf("Snapshot %v destination ID must be empty", to)
}
// Copy operation must be initiated from the destination region.
ec2Cli, err := newEC2Client(to.Region, nil, s.role)
ec2Cli, err := newEC2Client(to.Region, s.ec2Cli.Config.Copy(), s.role)
if err != nil {
return nil, errors.Wrapf(err, "Could not get EC2 client")
}
// Include a presigned URL when the regions are different. Include it
// independent of whether or not the snapshot is encrypted.
var presignedURL *string
if to.Region != from.Region {
fromCli, err2 := newEC2Client(from.Region, nil, s.role)
fromCli, err2 := newEC2Client(from.Region, s.ec2Cli.Config.Copy(), s.role)
if err2 != nil {
return nil, errors.Wrap(err2, "Could not create client to presign URL for snapshot copy request")
}
Expand Down Expand Up @@ -600,7 +599,7 @@ func (s *ebsStorage) FromRegion(ctx context.Context, region string) ([]string, e
}

func (s *ebsStorage) queryRegionToZones(ctx context.Context, region string) ([]string, error) {
ec2Cli, err := newEC2Client(region, nil, s.role)
ec2Cli, err := newEC2Client(region, s.ec2Cli.Config.Copy(), s.role)
if err != nil {
return nil, errors.Wrapf(err, "Could not get EC2 client")
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/blockstorage/awsebs/awsebs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"context"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
. "gopkg.in/check.v1"
)

Expand All @@ -32,7 +34,7 @@ func (s AWSEBSSuite) TestQueryRegionToZones(c *C) {
c.Skip("Only works on AWS")
ctx := context.Background()
region := "us-east-1"
ec2Cli, err := newEC2Client(region, nil, "")
ec2Cli, err := newEC2Client(region, aws.NewConfig().WithCredentials(credentials.NewEnvCredentials()), "")
c.Assert(err, IsNil)
provider := &ebsStorage{ec2Cli: ec2Cli}
zs, err := provider.queryRegionToZones(ctx, region)
Expand Down

0 comments on commit 6c1cadc

Please sign in to comment.