Skip to content

Commit 2cf4c40

Browse files
committed
chore: upgrade to aws-sdk-go-v2
1 parent 98d5e8c commit 2cf4c40

7 files changed

Lines changed: 102 additions & 114 deletions

File tree

.github/workflows/build.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ jobs:
1717
uses: actions/checkout@v3
1818

1919
- name: Setup go
20-
uses: actions/setup-go@v2
20+
uses: actions/setup-go@v5
2121
with:
22-
go-version: "1.20"
22+
go-version-file: go.mod
2323

2424
- name: Build binary
2525
run: |-

.github/workflows/golangci-lint.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ jobs:
1919
uses: actions/checkout@v3
2020

2121
- name: Setup go
22-
uses: actions/setup-go@v4
22+
uses: actions/setup-go@v5
2323
with:
24-
go-version: "1.20"
24+
go-version-file: go.mod
2525

2626
- name: Run golangci-lint
2727
uses: golangci/golangci-lint-action@v3

cmd/command.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"time"
1313

1414
"github.com/atotto/clipboard"
15-
"github.com/aws/aws-sdk-go/service/sts"
15+
"github.com/aws/aws-sdk-go-v2/aws"
1616
"github.com/joshdk/aws-console/console"
1717
"github.com/joshdk/aws-console/credentials"
1818
"github.com/joshdk/aws-console/qr"
@@ -83,7 +83,7 @@ func Command() *cobra.Command { //nolint:cyclop
8383

8484
RunE: func(*cobra.Command, []string) error {
8585
// Obtain credentials from either STDIN or a named AWS cli profile.
86-
var creds *sts.Credentials
86+
var creds *aws.Credentials
8787
var err error
8888
var region string
8989
if flags.profile == "-" {
@@ -99,19 +99,22 @@ func Command() *cobra.Command { //nolint:cyclop
9999

100100
// Set the preferred console region:
101101
// - Use the value from --region if given.
102-
// - Use the value from ~/.aws/config if given.
102+
// - Use the value from $AWS_REGION if given.
103103
// - Fall back to us-east-1.
104-
if flags.region != "" {
104+
switch {
105+
case flags.region != "":
105106
region = flags.region
106-
} else if region == "" {
107+
case os.Getenv("AWS_REGION") != "":
108+
region = os.Getenv("AWS_REGION")
109+
case region == "":
107110
region = "us-east-1"
108111
}
109112

110113
// If the named profile was configured with user credentials
111114
// (opposed to a role), then the user must be federated before an
112115
// AWS Console login url can be generated.
113116
federatePolicy := resolvePolicyAlias(flags.federatePolicy)
114-
creds, err = credentials.FederateUser(creds, flags.federateName, federatePolicy, flags.duration, flags.userAgent)
117+
creds, err = credentials.FederateUser(creds, region, flags.federateName, federatePolicy, flags.duration, flags.userAgent)
115118
if err != nil {
116119
return err
117120
}

console/console.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,23 @@ import (
1616
"strconv"
1717
"time"
1818

19-
"github.com/aws/aws-sdk-go/aws"
20-
"github.com/aws/aws-sdk-go/service/sts"
19+
"github.com/aws/aws-sdk-go-v2/aws"
2120
)
2221

2322
// GenerateLoginURL takes the given sts.Credentials and generates a url.URL
2423
// that can be used to login to the AWS Console.
2524
// See https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_enable-console-custom-url.html.
26-
func GenerateLoginURL(creds *sts.Credentials, duration time.Duration, location, userAgent string) (*url.URL, error) {
25+
func GenerateLoginURL(creds *aws.Credentials, duration time.Duration, location, userAgent string) (*url.URL, error) {
2726
// federationURL is the url used for AWS federation actions.
2827
const federationURL = "https://signin.aws.amazon.com/federation"
2928

3029
// timeout is a hardcoded 15 second window for HTTP requests to complete.
3130
const timeout = 15 * time.Second
3231

3332
sessionCreds := map[string]string{
34-
"sessionId": aws.StringValue(creds.AccessKeyId),
35-
"sessionKey": aws.StringValue(creds.SecretAccessKey),
36-
"sessionToken": aws.StringValue(creds.SessionToken),
33+
"sessionId": creds.AccessKeyID,
34+
"sessionKey": creds.SecretAccessKey,
35+
"sessionToken": creds.SessionToken,
3736
}
3837

3938
// Encode our credentials into JSON.

credentials/credentials.go

Lines changed: 41 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,38 @@
88
package credentials
99

1010
import (
11+
"context"
1112
"encoding/json"
1213
"fmt"
1314
"io"
1415
"time"
1516

16-
"github.com/aws/aws-sdk-go/aws"
17-
"github.com/aws/aws-sdk-go/aws/credentials"
18-
"github.com/aws/aws-sdk-go/aws/request"
19-
"github.com/aws/aws-sdk-go/aws/session"
20-
"github.com/aws/aws-sdk-go/service/sts"
17+
"github.com/aws/aws-sdk-go-v2/aws"
18+
"github.com/aws/aws-sdk-go-v2/config"
19+
"github.com/aws/aws-sdk-go-v2/credentials"
20+
"github.com/aws/aws-sdk-go-v2/credentials/processcreds"
21+
"github.com/aws/aws-sdk-go-v2/service/sts"
22+
"github.com/aws/aws-sdk-go-v2/service/sts/types"
2123
)
2224

2325
// FromConfig retrieves credentials from the AWS cli config files, typically
2426
// ~/.aws/credentials and ~/.aws/config. Credentials for the named profile are
2527
// returned, or the default profile if no name is given. Additionally, the
2628
// value of $AWS_PROFILE will be used if it is set.
27-
func FromConfig(profile string) (*sts.Credentials, string, error) {
28-
sess, err := session.NewSessionWithOptions(session.Options{
29-
Profile: profile,
30-
SharedConfigState: session.SharedConfigEnable,
31-
})
29+
func FromConfig(profile string) (*aws.Credentials, string, error) {
30+
ctx := context.Background()
31+
32+
cfg, err := config.LoadDefaultConfig(ctx, config.WithSharedConfigProfile(profile))
3233
if err != nil {
3334
return nil, "", err
3435
}
3536

36-
value, err := sess.Config.Credentials.Get()
37+
creds, err := cfg.Credentials.Retrieve(ctx)
3738
if err != nil {
3839
return nil, "", err
3940
}
4041

41-
return &sts.Credentials{
42-
AccessKeyId: aws.String(value.AccessKeyID),
43-
SecretAccessKey: aws.String(value.SecretAccessKey),
44-
SessionToken: aws.String(value.SessionToken),
45-
}, aws.StringValue(sess.Config.Region), nil
42+
return &creds, cfg.Region, nil
4643
}
4744

4845
// FromReader retrieves credentials from given io.Reader, typically os.Stdin.
@@ -72,36 +69,32 @@ func FromConfig(profile string) (*sts.Credentials, string, error) {
7269
//
7370
// See https://docs.aws.amazon.com/cli/latest/reference/sts/assume-role.html#output.
7471
// See https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-sourcing-external.html.
75-
func FromReader(reader io.Reader) (*sts.Credentials, error) {
72+
func FromReader(reader io.Reader) (*aws.Credentials, error) {
7673
// Read the entire body, as it will be potentially parsed multiple times.
7774
body, err := io.ReadAll(reader)
7875
if err != nil {
7976
return nil, err
8077
}
8178

8279
type creds struct {
83-
Credentials struct {
84-
AccessKeyID string `json:"AccessKeyId"`
85-
SecretAccessKey string `json:"SecretAccessKey"`
86-
SessionToken string `json:"SessionToken"`
87-
} `json:"Credentials"`
80+
Credentials processcreds.CredentialProcessResponse `json:"Credentials"`
8881
}
8982

9083
var result creds
9184

9285
if err := json.Unmarshal(body, &result); err == nil && result.Credentials.AccessKeyID != "" && result.Credentials.SecretAccessKey != "" {
93-
// Credentials were unmarshaled into the entire struct.
94-
return &sts.Credentials{
95-
AccessKeyId: aws.String(result.Credentials.AccessKeyID),
96-
SecretAccessKey: aws.String(result.Credentials.SecretAccessKey),
97-
SessionToken: aws.String(result.Credentials.SessionToken),
86+
// Credentials were unmarshalled into the entire struct.
87+
return &aws.Credentials{
88+
AccessKeyID: result.Credentials.AccessKeyID,
89+
SecretAccessKey: result.Credentials.SecretAccessKey,
90+
SessionToken: result.Credentials.SessionToken,
9891
}, nil
9992
} else if err := json.Unmarshal(body, &result.Credentials); err == nil && result.Credentials.AccessKeyID != "" && result.Credentials.SecretAccessKey != "" {
100-
// Credentials were unmarshaled into part of the struct.
101-
return &sts.Credentials{
102-
AccessKeyId: aws.String(result.Credentials.AccessKeyID),
103-
SecretAccessKey: aws.String(result.Credentials.SecretAccessKey),
104-
SessionToken: aws.String(result.Credentials.SessionToken),
93+
// Credentials were unmarshalled into part of the struct.
94+
return &aws.Credentials{
95+
AccessKeyID: result.Credentials.AccessKeyID,
96+
SecretAccessKey: result.Credentials.SecretAccessKey,
97+
SessionToken: result.Credentials.SessionToken,
10598
}, nil
10699
}
107100

@@ -112,27 +105,24 @@ func FromReader(reader io.Reader) (*sts.Credentials, error) {
112105
// FederateUser will federate the given user credentials by calling STS
113106
// GetFederationToken. If the given credentials are not for a user (like
114107
// credentials for a role) then they are returned unmodified.
115-
func FederateUser(creds *sts.Credentials, name, policy string, duration time.Duration, userAgent string) (*sts.Credentials, error) {
108+
func FederateUser(creds *aws.Credentials, region, name, policy string, duration time.Duration, userAgent string) (*aws.Credentials, error) {
116109
// Only federate if user credentials were given.
117-
if aws.StringValue(creds.SessionToken) != "" {
110+
if creds.SessionToken != "" {
118111
return creds, nil
119112
}
120113

121-
// Create a new session given the static user credentials.
122-
sess, err := session.NewSession(&aws.Config{
123-
Credentials: credentials.NewStaticCredentials(
124-
aws.StringValue(creds.AccessKeyId),
125-
aws.StringValue(creds.SecretAccessKey),
126-
aws.StringValue(creds.SessionToken),
114+
client := sts.NewFromConfig(aws.Config{
115+
Credentials: credentials.NewStaticCredentialsProvider(
116+
creds.AccessKeyID,
117+
creds.SecretAccessKey,
118+
creds.SessionToken,
127119
),
120+
Region: region,
128121
})
129-
if err != nil {
130-
return nil, err
131-
}
132122

133123
input := sts.GetFederationTokenInput{
134124
Name: aws.String(name),
135-
PolicyArns: []*sts.PolicyDescriptorType{{
125+
PolicyArns: []types.PolicyDescriptorType{{
136126
Arn: aws.String(policy),
137127
}},
138128
}
@@ -145,18 +135,18 @@ func FederateUser(creds *sts.Credentials, name, policy string, duration time.Dur
145135
}
146136

147137
if duration != 0 {
148-
input.DurationSeconds = aws.Int64(int64(duration.Seconds()))
138+
input.DurationSeconds = aws.Int32(int32(duration.Seconds()))
149139
}
150140

151-
// Configure client.
152-
client := sts.New(sess)
153-
client.Handlers.Build.PushBack(request.WithSetRequestHeaders(map[string]string{"User-Agent": userAgent}))
154-
155141
// Federate the user.
156-
result, err := client.GetFederationToken(&input)
142+
result, err := client.GetFederationToken(context.Background(), &input)
157143
if err != nil {
158144
return nil, err
159145
}
160146

161-
return result.Credentials, nil
147+
return &aws.Credentials{
148+
AccessKeyID: aws.ToString(result.Credentials.AccessKeyId),
149+
SecretAccessKey: aws.ToString(result.Credentials.SecretAccessKey),
150+
SessionToken: aws.ToString(result.Credentials.SessionToken),
151+
}, nil
162152
}

go.mod

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
module github.com/joshdk/aws-console
22

3-
go 1.20
3+
go 1.24.0
44

55
require (
66
github.com/atotto/clipboard v0.1.4
7-
github.com/aws/aws-sdk-go v1.44.254
8-
github.com/mattn/go-isatty v0.0.18
7+
github.com/aws/aws-sdk-go-v2 v1.39.4
8+
github.com/aws/aws-sdk-go-v2/config v1.31.15
9+
github.com/aws/aws-sdk-go-v2/credentials v1.18.19
10+
github.com/aws/aws-sdk-go-v2/service/sts v1.38.9
11+
github.com/mattn/go-isatty v0.0.20
912
github.com/mattn/go-sixel v0.0.5
1013
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8
1114
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
@@ -14,8 +17,16 @@ require (
1417
)
1518

1619
require (
20+
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.11 // indirect
21+
github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.11 // indirect
22+
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.11 // indirect
23+
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.4 // indirect
24+
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.2 // indirect
25+
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.13.11 // indirect
26+
github.com/aws/aws-sdk-go-v2/service/sso v1.29.8 // indirect
27+
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.35.3 // indirect
28+
github.com/aws/smithy-go v1.23.1 // indirect
1729
github.com/inconshreveable/mousetrap v1.1.0 // indirect
18-
github.com/jmespath/go-jmespath v0.4.0 // indirect
1930
github.com/soniakeys/quant v1.0.0 // indirect
2031
github.com/spf13/pflag v1.0.5 // indirect
2132
golang.org/x/sys v0.7.0 // indirect

0 commit comments

Comments
 (0)