This repository has been archived by the owner on Jun 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
67 lines (63 loc) · 1.75 KB
/
main.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
package main
import (
"context"
"encoding/base64"
"encoding/json"
"io/ioutil"
"log"
"os"
"strings"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ecr"
"github.com/aws/aws-sdk-go-v2/service/sts"
)
type auth struct {
Auth string `json:"auth"`
}
type docker struct {
Auths map[string]auth `json:"auths"`
}
func main() {
dockercfg := docker{
Auths: make(map[string]auth),
}
// Docker Hub authentication
hubUsername := os.Getenv("DOCKER_HUB_USERNAME")
hubPassword := os.Getenv("DOCKER_HUB_PASSWORD")
if hubUsername != "" && hubPassword != "" {
b64auth := base64.URLEncoding.EncodeToString([]byte(hubUsername + ":" + hubPassword))
dockercfg.Auths["https://index.docker.io/v1/"] = auth{Auth: b64auth}
}
// ECR authentication
cfg, err := config.LoadDefaultConfig(context.Background())
if err != nil {
log.Fatalf("unable to load SDK config, %v", err)
}
ids := os.Getenv("AWS_ECR_REGISTRY_IDS")
if ids == "" {
stsSvc := sts.NewFromConfig(cfg)
callerID, err := stsSvc.GetCallerIdentity(context.Background(), &sts.GetCallerIdentityInput{})
if err != nil {
log.Fatalf("unable to get identity, %v", err)
}
ids = *callerID.Account
}
svc := ecr.NewFromConfig(cfg)
resp, err := svc.GetAuthorizationToken(context.Background(), &ecr.GetAuthorizationTokenInput{
RegistryIds: strings.Split(ids, " "),
})
if err != nil {
log.Fatalf("unable to authorization token, %v", err)
}
for _, repo := range resp.AuthorizationData {
dockercfg.Auths[(*repo.ProxyEndpoint)[8:]] = auth{Auth: *repo.AuthorizationToken}
}
out, err := json.Marshal(dockercfg)
if err != nil {
log.Fatalf("unable marshal json, %v", err)
}
err = ioutil.WriteFile(os.Args[1], out, 0644)
if err != nil {
log.Fatalf("unable to write output, %v", err)
}
}