forked from bobjana/rancher-ecr-credentials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
145 lines (131 loc) · 3.83 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
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
package main
import (
"encoding/base64"
"errors"
"fmt"
"net/url"
"os"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/rancher/go-rancher/client"
)
// Rancher holds the configuration parameters
type Rancher struct {
URL string
AccessKey string
SecretKey string
AWSAccessKey string
AWSSecretKey string
AWSRegion string
}
func main() {
vargs := Rancher{
URL: os.Getenv("CATTLE_URL"),
AccessKey: os.Getenv("CATTLE_ACCESS_KEY"),
SecretKey: os.Getenv("CATTLE_SECRET_KEY"),
AWSAccessKey: os.Getenv("AWS_ACCESS_KEY_ID"),
AWSSecretKey: os.Getenv("AWS_SECRET_ACCESS_KEY"),
AWSRegion: os.Getenv("AWS_REGION"),
}
err := updateEcr(vargs)
if err != nil {
fmt.Printf("Error updating ECR, %s\n", err)
}
ticker := time.NewTicker(6 * time.Hour)
for {
<-ticker.C
err := updateEcr(vargs)
if err != nil {
fmt.Printf("Error updating ECR, %s\n", err)
}
}
}
func updateEcr(vargs Rancher) error {
fmt.Printf("Updating ECR Credentials\n")
ecrClient := ecr.New(session.New(&aws.Config{
Region: aws.String(vargs.AWSRegion),
Credentials: credentials.NewStaticCredentials(vargs.AWSAccessKey, vargs.AWSSecretKey, ""),
}))
resp, err := ecrClient.GetAuthorizationToken(&ecr.GetAuthorizationTokenInput{})
if err != nil {
return err
}
if len(resp.AuthorizationData) < 1 {
return errors.New("Request did not return authorization data")
}
bytes, err := base64.StdEncoding.DecodeString(*resp.AuthorizationData[0].AuthorizationToken)
if err != nil {
fmt.Printf("Error decoding authorization token: %s\n", err)
return err
}
token := string(bytes[:len(bytes)])
authTokens := strings.Split(token, ":")
if len(authTokens) != 2 {
return fmt.Errorf("Authorization token does not contain data in <user>:<password> format: %s", token)
}
registryURL, err := url.Parse(*resp.AuthorizationData[0].ProxyEndpoint)
if err != nil {
fmt.Printf("Error parsing registry URL: %s\n", err)
return err
}
ecrUsername := authTokens[0]
ecrPassword := authTokens[1]
ecrURL := registryURL.Host
rancher, err := client.NewRancherClient(&client.ClientOpts{
Url: vargs.URL,
AccessKey: vargs.AccessKey,
SecretKey: vargs.SecretKey,
})
if err != nil {
fmt.Printf("Failed to create rancher client: %s\n", err)
return err
}
registries, err := rancher.Registry.List(&client.ListOpts{})
if err != nil {
fmt.Printf("Failed to retrieve registries: %s\n", err)
return err
}
for _, registry := range registries.Data {
serverAddress, err := url.Parse(registry.ServerAddress)
if err != nil {
fmt.Printf("Failed to parse configured registry URL %s\n", registry.ServerAddress)
break
}
registryHost := serverAddress.Host
if registryHost == "" {
registryHost = serverAddress.Path
}
if registryHost == ecrURL {
credentials, err := rancher.RegistryCredential.List(&client.ListOpts{
Filters: map[string]interface{}{
"registryId": registry.Id,
},
})
if err != nil {
fmt.Printf("Failed to retrieved registry credentials for id: %s, %s\n", registry.Id, err)
break
}
if len(credentials.Data) != 1 {
fmt.Printf("No credentials retrieved for registry: %s\n", registry.Id)
break
}
credential := credentials.Data[0]
_, err = rancher.RegistryCredential.Update(&credential, &client.RegistryCredential{
PublicValue: ecrUsername,
SecretValue: ecrPassword,
})
if err != nil {
fmt.Printf("Failed to update registry credential %s, %s\n", credential.Id, err)
} else {
fmt.Printf("Successfully updated credentials %s for registry %s; registry address: %s\n", credential.Id, registry.Id, registryHost)
}
break
}
fmt.Printf("Failed to find configured registry to update for URL %s\n", ecrURL)
}
return nil
}