Skip to content

Commit 2eb7181

Browse files
authored
Add ability to provide an authentication session to lock (#58)
* Add ability to provide an authentication session to lock * Don't run git tests in parallel
1 parent 3901944 commit 2eb7181

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

git/test/auth_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ var (
2626

2727
// NOTE: All these tests should be run in the provided docker environment to avoid polluting the local git configuration
2828
// settings. The tests will assert that it is running in the docker environment, and will fail if it is not.
29+
// All these tests are also run in serial to avoid race conditions on the git config file.
2930

3031
func TestHTTPSAuth(t *testing.T) {
31-
t.Parallel()
32-
3332
currentDir, err := os.Getwd()
3433
require.NoError(t, err)
3534
require.Equal(t, "/workspace/go-commons/git/test", currentDir)
@@ -45,8 +44,6 @@ func TestHTTPSAuth(t *testing.T) {
4544
}
4645

4746
func TestForceHTTPS(t *testing.T) {
48-
t.Parallel()
49-
5047
currentDir, err := os.Getwd()
5148
require.NoError(t, err)
5249
require.Equal(t, "/workspace/go-commons/git/test", currentDir)

lock/lock.go

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ package lock
22

33
import (
44
"fmt"
5+
"time"
6+
57
"github.com/aws/aws-sdk-go/aws"
68
"github.com/aws/aws-sdk-go/aws/awserr"
79
"github.com/aws/aws-sdk-go/aws/session"
810
"github.com/aws/aws-sdk-go/service/dynamodb"
911
"github.com/gruntwork-io/go-commons/errors"
1012
"github.com/gruntwork-io/go-commons/retry"
1113
"github.com/sirupsen/logrus"
12-
"time"
1314
)
1415

1516
const (
@@ -40,6 +41,10 @@ type Options struct {
4041
SleepBetweenRetries time.Duration
4142
// The logger to use for the lock
4243
Logger *logrus.Logger
44+
45+
// Custom session to use to authenticate to AWS in the SDK. If nil, constructs the session based on the default
46+
// authentication chain in the SDK.
47+
AwsSession *session.Session
4348
}
4449

4550
type TimeoutExceeded struct {
@@ -79,23 +84,13 @@ func NewAuthenticatedSession(awsRegion string) (*session.Session, error) {
7984
return sess, nil
8085
}
8186

82-
// NewDynamoDb returns an authenticated client object for accessing DynamoDb
83-
func NewDynamoDb(awsRegion string) (*dynamodb.DynamoDB, error) {
84-
sess, err := NewAuthenticatedSession(awsRegion)
85-
if err != nil {
86-
return nil, err
87-
}
88-
dynamodbSvc := dynamodb.New(sess)
89-
return dynamodbSvc, nil
90-
}
91-
9287
// AcquireLock will attempt to acquire a lock in DynamoDB table while taking the configuration options into account.
9388
// We are using DynamoDB to create a table to help us track the lock status of different resources.
9489
// The acquiring of a lock attempts to create a table. The intention is that we have 1 table per resource in a single region.
9590
// This would allow the locking mechanism to flexibly decide if a resource is locked or not. For test cases where the AWS resource
9691
// is multi-region, or global, the configuration of which regions to use should reflect that.
9792
func AcquireLock(options *Options) error {
98-
client, err := NewDynamoDb(options.AwsRegion)
93+
client, err := getDynamoDBClient(options)
9994
if err != nil {
10095
options.Logger.Errorf("Error authenticating to AWS: %s\n", err)
10196
return err
@@ -156,7 +151,7 @@ func acquireLock(options *Options, client *dynamodb.DynamoDB) error {
156151
// ReleaseLock will attempt to release the lock defined by the provided lock string in the configured lock table for the
157152
// configured region
158153
func ReleaseLock(options *Options) error {
159-
client, err := NewDynamoDb(options.AwsRegion)
154+
client, err := getDynamoDBClient(options)
160155
if err != nil {
161156
options.Logger.Errorf("Error authenticating to AWS: %s\n", err)
162157
return err
@@ -297,7 +292,7 @@ func lockTableExistsAndIsActive(tableName string, client *dynamodb.DynamoDB) (bo
297292
// GetLockStatus attempts to acquire the lock and check if the expected item is there
298293
// If there's the expected Item with the correct `LockString` value - then the status is `locked`, if the item is not there - then the status is `not locked`
299294
func GetLockStatus(options *Options) (*dynamodb.GetItemOutput, error) {
300-
client, err := NewDynamoDb(options.AwsRegion)
295+
client, err := getDynamoDBClient(options)
301296
if err != nil {
302297
options.Logger.Errorf("Error authenticating to AWS: %s\n", err)
303298
return nil, err
@@ -318,3 +313,20 @@ func GetLockStatus(options *Options) (*dynamodb.GetItemOutput, error) {
318313

319314
return item, nil
320315
}
316+
317+
func getDynamoDBClient(options *Options) (*dynamodb.DynamoDB, error) {
318+
if options.AwsSession == nil {
319+
return NewDynamoDb(options.AwsRegion)
320+
}
321+
return dynamodb.New(options.AwsSession), nil
322+
}
323+
324+
// NewDynamoDb returns an authenticated client object for accessing DynamoDb
325+
func NewDynamoDb(awsRegion string) (*dynamodb.DynamoDB, error) {
326+
sess, err := NewAuthenticatedSession(awsRegion)
327+
if err != nil {
328+
return nil, err
329+
}
330+
dynamodbSvc := dynamodb.New(sess)
331+
return dynamodbSvc, nil
332+
}

0 commit comments

Comments
 (0)