Skip to content

Commit 06cdcaf

Browse files
authored
Merge pull request #28 from ryangardner/use-sts
Use STS to get user information to support using session tokens
2 parents 44819eb + 08a5a1e commit 06cdcaf

2 files changed

Lines changed: 10 additions & 18 deletions

File tree

README.md

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,13 @@ using AWS. We take no responsibility for any charges you may incur.
121121
To use DynamoDB for locking, you must:
122122

123123
1. Set your AWS credentials in the environment using one of the following options:
124-
1. Set your credentials as the environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`.
124+
1. Set your credentials as the environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` (and also `AWS_SESSION_TOKEN` if using [STS temporary credentials](http://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp.html))
125125
1. Run `aws configure` and fill in the details it asks for.
126126
1. Run Terragrunt on an EC2 instance with an IAM Role.
127127
1. Your AWS user must have an [IAM
128128
policy](http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/access-control-identity-based.html)
129129
granting all DynamoDB actions (`dynamodb:*`) on the table `terragrunt_locks` (see the
130-
[DynamoDB locking configuration](#dynamodb-locking-configuration) for how to configure this table name). In
131-
addition, IAM users will need the `iam:GetUser` permission on themselves so that DynamoDB can record which IAM
132-
User wrote the most recent lock.
130+
[DynamoDB locking configuration](#dynamodb-locking-configuration) for how to configure this table name).
133131

134132
Here is an example IAM policy that grants the necessary permissions on the `terragrunt_locks` table in region `us-west-2` for
135133
an account with account id `1234567890`:
@@ -143,12 +141,6 @@ To use DynamoDB for locking, you must:
143141
"Effect": "Allow",
144142
"Action": "dynamodb:*",
145143
"Resource": "arn:aws:dynamodb:us-west-2:1234567890:table/terragrunt_locks"
146-
},
147-
{
148-
"Sid": "GetSelfIamUser",
149-
"Effect": "Allow",
150-
"Action": "iam:GetUser",
151-
"Resource": "*"
152144
}
153145
]
154146
}

dynamodb/dynamo_lock_item.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"github.com/aws/aws-sdk-go/aws"
99
"fmt"
1010
"github.com/gruntwork-io/terragrunt/errors"
11-
"github.com/aws/aws-sdk-go/service/iam"
1211
"github.com/aws/aws-sdk-go/aws/session"
12+
"github.com/aws/aws-sdk-go/service/sts"
1313
)
1414

1515
// Create a DynamoDB key for the given item id
@@ -90,12 +90,12 @@ func getAttribute(item map[string]*dynamodb.AttributeValue, attribute string) (s
9090
// Create a DynamoDB item for the given item id. This item represents a lock and will include metadata about the
9191
// current user, who is trying to acquire the lock.
9292
func createItemAttributes(itemId string, client *dynamodb.DynamoDB) (map[string]*dynamodb.AttributeValue, error) {
93-
iamUsername, err := getIamUsername(client)
93+
callerIdentity, err := getCallerIdentity(client)
9494
if err != nil {
9595
return nil, err
9696
}
9797

98-
lockMetadata, err := locks.CreateLockMetadata(itemId, iamUsername)
98+
lockMetadata, err := locks.CreateLockMetadata(itemId, callerIdentity)
9999
if err != nil {
100100
return nil, err
101101
}
@@ -108,15 +108,15 @@ func createItemAttributes(itemId string, client *dynamodb.DynamoDB) (map[string]
108108
}, nil
109109
}
110110

111-
// Return the IAM username of the currently logged in user
112-
func getIamUsername(client *dynamodb.DynamoDB) (string, error) {
113-
iamClient := iam.New(session.New(), &client.Config)
114-
output, err := iamClient.GetUser(&iam.GetUserInput{})
111+
// Return the UserID
112+
func getCallerIdentity(client *dynamodb.DynamoDB) (string, error) {
113+
stsconn := sts.New(session.New(), &client.Config)
114+
output, err := stsconn.GetCallerIdentity(&sts.GetCallerIdentityInput{})
115115
if err != nil {
116116
return "", errors.WithStackTrace(err)
117117
}
118118

119-
return *output.User.UserName, nil
119+
return *output.UserId, nil
120120
}
121121

122122
type AttributeMissing struct {

0 commit comments

Comments
 (0)