-
Notifications
You must be signed in to change notification settings - Fork 8
/
ecr.go
178 lines (144 loc) · 5.05 KB
/
ecr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package aws
import (
"context"
"encoding/base64"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws/arn"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
"github.com/aws/aws-sdk-go-v2/service/ecr"
dockercfg "github.com/docker/cli/cli/config"
"github.com/docker/cli/cli/config/types"
"github.com/spf13/cobra"
"namespacelabs.dev/foundation/internal/cli/fncobra"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/fnapi"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/workspace/dirs"
)
const dockerCfgName = "config.json"
func newEcrDockerLoginCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "ecr-docker-login",
Short: "Uses Workload Federation to log into ECR for use with Docker.",
Args: cobra.NoArgs,
Hidden: true,
}
roleArn := cmd.Flags().String("role_arn", "", "The ARN of the role to log in as.")
awsProfile := cmd.Flags().String("aws_profile", "", "Use the specified AWS profile.")
region := cmd.Flags().String("region", "", "Use the specified AWS region.")
duration := cmd.Flags().Duration("duration", time.Hour, "For how long the resulting AWS credentials should be valid for.")
temp := cmd.Flags().Bool("temp", false, "Create a temporary Docker config file with the added credentials.")
outputPath := cmd.Flags().String("output_to", "", "If specified, write the path of the Docker config to this path.")
return fncobra.Cmd(cmd).Do(func(ctx context.Context) error {
if *roleArn == "" {
return fnerrors.New("--role_arn is required")
}
arn, err := arn.Parse(*roleArn)
if err != nil {
return fnerrors.New("--role_arn is invalid: %w", err)
}
token, err := fnapi.FetchToken(ctx)
if err != nil {
return err
}
claims, err := token.Claims(ctx)
if err != nil {
return err
}
t := time.Now()
resp, err := fnapi.IssueIdToken(ctx, "sts.amazonaws.com", idTokenVersion)
if err != nil {
return err
}
fmt.Fprintf(console.Stderr(ctx), "Obtained credentials from Namespace (took %v).\n", time.Since(t))
client, err := newSTSClient(ctx, *awsProfile)
if err != nil {
return err
}
sessionName := ""
if claims.InstanceID != "" {
sessionName = fmt.Sprintf("nsc.instance=%s", claims.InstanceID)
} else {
sessionName = fmt.Sprintf("nsc.tenant=%s", claims.TenantID)
}
t = time.Now()
cfg, err := config.LoadDefaultConfig(ctx,
config.WithRegion(*region),
config.WithCredentialsProvider(
stscreds.NewWebIdentityRoleProvider(client, *roleArn, staticToken{resp.IdToken}, func(o *stscreds.WebIdentityRoleOptions) {
o.RoleSessionName = sessionName
o.Duration = *duration
}),
))
if err != nil {
return err
}
ecrToken, err := ecr.NewFromConfig(cfg).GetAuthorizationToken(ctx, &ecr.GetAuthorizationTokenInput{})
if err != nil {
return err
}
if len(ecrToken.AuthorizationData) == 0 {
return fnerrors.New("invalid response from ECR: missing authorization data")
}
authData := ecrToken.AuthorizationData[0].AuthorizationToken
data, err := base64.StdEncoding.DecodeString(*authData)
if err != nil {
return err
}
fmt.Fprintf(console.Stderr(ctx), "Obtained credentials from AWS (took=%v).\n", time.Since(t))
parts := strings.SplitN(string(data), ":", 2)
if len(parts) != 2 {
return fnerrors.New("invalid response from ECR: authorization token has %d parts", len(parts))
}
dockerCfg := dockercfg.LoadDefaultConfigFile(console.Stderr(ctx))
addr := fmt.Sprintf("%s.dkr.ecr.%s.amazonaws.com", arn.AccountID, *region)
dockerCfg.AuthConfigs[addr] = types.AuthConfig{
ServerAddress: addr,
Username: parts[0],
Password: parts[1],
}
filename := dockerCfg.Filename
if *temp {
tmpDir, err := dirs.CreateUserTempDir("dockerconfig", "*")
if err != nil {
return fnerrors.New("failed to create temp dir: %w", err)
}
tmpFile, err := os.OpenFile(filepath.Join(tmpDir, dockerCfgName), os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
if err != nil {
return fnerrors.New("failed to create temp file: %w", err)
}
if err := dockerCfg.SaveToWriter(tmpFile); err != nil {
return fnerrors.New("failed to save config: %w", err)
}
if err := tmpFile.Close(); err != nil {
return fnerrors.New("failed to close docker config: %w", err)
}
filename = tmpFile.Name()
} else {
if err := dockerCfg.Save(); err != nil {
return fnerrors.New("failed to save config: %w", err)
}
}
fmt.Fprintf(console.Stderr(ctx), "Added ECR credentials to %s.\n", filename)
if *outputPath != "" {
if err := os.WriteFile(*outputPath, []byte(filename), 0644); err != nil {
return fnerrors.New("failed to write %q: %w", *outputPath, err)
}
}
return nil
})
}
type staticToken struct {
idToken string
}
func (st staticToken) GetIdentityToken() ([]byte, error) {
return []byte(st.idToken), nil
}