Skip to content
This repository was archived by the owner on Aug 12, 2025. It is now read-only.

Commit 30448eb

Browse files
thisisaaronlandthisisaaronland
andauthored
add aws-imds-credentials (#6)
Co-authored-by: thisisaaronland <thisisaaronland@localhost>
1 parent 53c7192 commit 30448eb

File tree

5 files changed

+69
-1
lines changed

5 files changed

+69
-1
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ cli:
88
go build -mod $(GOMOD) -ldflags="$(LDFLAGS)" -o bin/aws-set-env cmd/aws-set-env/main.go
99
go build -mod $(GOMOD) -ldflags="$(LDFLAGS)" -o bin/aws-sign-request cmd/aws-sign-request/main.go
1010
go build -mod $(GOMOD) -ldflags="$(LDFLAGS)" -o bin/aws-credentials-json-to-ini cmd/aws-credentials-json-to-ini/main.go
11+
go build -mod $(GOMOD) -ldflags="$(LDFLAGS)" -o bin/aws-imds-credentials cmd/aws-imds-credentials/main.go

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ go build -mod vendor -ldflags="-s -w" -o bin/aws-cognito-credentials cmd/aws-cog
1818
go build -mod vendor -ldflags="-s -w" -o bin/aws-set-env cmd/aws-set-env/main.go
1919
go build -mod vendor -ldflags="-s -w" -o bin/aws-sign-request cmd/aws-sign-request/main.go
2020
go build -mod vendor -ldflags="-s -w" -o bin/aws-credentials-json-to-ini cmd/aws-credentials-json-to-ini/main.go
21+
go build -mod vendor -ldflags="-s -w" -o bin/aws-imds-credentials cmd/aws-imds-credentials/main.go
2122
```
2223

2324
## aws-cognito-credentials
@@ -108,6 +109,22 @@ Usage of ./bin/aws-get-credentials:
108109
A valid AWS credentials profile (default "default")
109110
```
110111

112+
### aws-imds-credentials
113+
114+
`aws-imds-credentials` returns the current `aws.Credentials` derived from the EC2 IMDS API. For example:
115+
116+
```
117+
$> ./bin/aws-imds-credentials | jq
118+
{
119+
"AccessKeyID": "...",
120+
"SecretAccessKey": "...",
121+
"SessionToken": "...",
122+
"Source": "EC2RoleProvider",
123+
"CanExpire": true,
124+
"Expires": "2024-03-28T19:44:42.59621653Z"
125+
}
126+
```
127+
111128
### aws-mfa-session
112129

113130
`aws-mfa-session` is a command line to create session-based authentication keys and secrets for a given profile and multi-factor authentication (MFA) token and then writing that key and secret back to a "credentials" file in a specific profile section.

cmd/aws-imds-credentials/main.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"flag"
7+
"log"
8+
"os"
9+
10+
"github.com/aaronland/go-aws-auth"
11+
)
12+
13+
func main() {
14+
15+
flag.Parse()
16+
ctx := context.Background()
17+
18+
creds, err := auth.EC2RoleCredentials(ctx)
19+
20+
if err != nil {
21+
log.Fatalf("Failed to retrive credentials, %v", err)
22+
}
23+
24+
enc := json.NewEncoder(os.Stdout)
25+
err = enc.Encode(creds)
26+
27+
if err != nil {
28+
log.Fatalf("Failed to encode credentials, %v", err)
29+
}
30+
}

ec2.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package auth
2+
3+
import (
4+
"context"
5+
6+
"github.com/aws/aws-sdk-go-v2/aws"
7+
"github.com/aws/aws-sdk-go-v2/credentials/ec2rolecreds"
8+
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
9+
)
10+
11+
func EC2RoleCredentials(ctx context.Context) (aws.Credentials, error) {
12+
13+
provider := ec2rolecreds.New(func(o *ec2rolecreds.Options) {
14+
o.Client = imds.New(imds.Options{
15+
/* custom options */
16+
})
17+
})
18+
19+
return provider.Retrieve(ctx)
20+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/aws/aws-sdk-go-v2 v1.26.0
77
github.com/aws/aws-sdk-go-v2/config v1.27.9
88
github.com/aws/aws-sdk-go-v2/credentials v1.17.9
9+
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.0
910
github.com/aws/aws-sdk-go-v2/service/cognitoidentity v1.23.4
1011
github.com/aws/aws-sdk-go-v2/service/iam v1.31.3
1112
github.com/aws/aws-sdk-go-v2/service/ssm v1.49.4
@@ -17,7 +18,6 @@ require (
1718
)
1819

1920
require (
20-
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.0 // indirect
2121
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.4 // indirect
2222
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.4 // indirect
2323
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 // indirect

0 commit comments

Comments
 (0)