-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (59 loc) · 1.48 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
68
69
70
71
72
73
74
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"os"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ecr"
)
func main() {
address := os.Getenv("address")
parts := strings.Split(address, ".")
accountID := parts[0]
region := parts[3]
sess := session.Must(session.NewSession())
ecrClient := ecr.New(sess, &aws.Config{
Region: aws.String(region),
})
out := &output{
Error: "",
State: "success",
ServerAddress: address,
}
defer finish(out)
authTokenResp, err := ecrClient.GetAuthorizationToken(&ecr.GetAuthorizationTokenInput{
RegistryIds: []*string{aws.String(accountID)},
})
if err != nil {
out.Error = err.Error()
out.State = "error"
return
}
if len(authTokenResp.AuthorizationData) < 1 {
out.Error = "no ecr registry credentials found"
out.State = "error"
return
}
aD := authTokenResp.AuthorizationData[0]
base := aws.StringValue(aD.AuthorizationToken)
creds, _ := base64.StdEncoding.DecodeString(base)
credParts := strings.Split(string(creds), ":")
username := credParts[0]
password := credParts[1]
out.Username = username
out.Password = password
}
func finish(out *output) {
o, _ := json.Marshal(out)
fmt.Printf("%s\n", o)
}
type output struct {
Username string `json:"username"`
Password string `json:"password"`
ServerAddress string `json:"serverAddress"`
Error string `json:"error"`
State string `json:"state"`
}