-
Notifications
You must be signed in to change notification settings - Fork 26
/
bottlerocket.go
250 lines (213 loc) · 5.61 KB
/
bottlerocket.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package bottlerocket
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io/fs"
"io/ioutil"
"net"
"net/http"
"os"
"strings"
"golang.org/x/mod/semver"
"github.com/aws/eks-anywhere-packages/credentialproviderpackage/pkg/configurator"
"github.com/aws/eks-anywhere-packages/credentialproviderpackage/pkg/constants"
)
type bottleRocket struct {
client http.Client
baseURL string
config constants.CredentialProviderConfigOptions
}
type awsCred struct {
Aws aws `json:"aws"`
}
type aws struct {
Config string `json:"config"`
Profile string `json:"profile"`
Region string `json:"region"`
}
type brKubernetes struct {
Kubernetes kubernetes `json:"kubernetes"`
}
type ecrCredentialProvider struct {
CacheDuration string `json:"cache-duration"`
Enabled bool `json:"enabled"`
ImagePatterns []string `json:"image-patterns"`
}
type credentialProviders struct {
EcrCredentialProvider ecrCredentialProvider `json:"ecr-credential-provider"`
}
type kubernetes struct {
CredentialProviders credentialProviders `json:"credential-providers"`
}
type brVersion struct {
Os struct {
Arch string `json:"arch"`
BuildID string `json:"build_id"`
PrettyName string `json:"pretty_name"`
VariantID string `json:"variant_id"`
VersionID string `json:"version_id"`
} `json:"os"`
}
var _ configurator.Configurator = (*bottleRocket)(nil)
func NewBottleRocketConfigurator(socketPath string) (*bottleRocket, error) {
socket, err := os.Stat(socketPath)
if err != nil {
return nil, err
}
if socket.Mode().Type() != fs.ModeSocket {
return nil, fmt.Errorf("Unexpected type %s expected socket\n", socket.Mode().Type())
}
br := &bottleRocket{
baseURL: "http://localhost/",
client: http.Client{
Transport: &http.Transport{
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
return net.Dial("unix", socketPath)
},
},
},
}
valid, err := br.isSupportedBRVersion()
if err != nil {
return nil, fmt.Errorf("error retrieving BR version %v", err)
}
if !valid {
return nil, fmt.Errorf("unsupported BR version %v", err)
}
return br, nil
}
func (b *bottleRocket) Initialize(config constants.CredentialProviderConfigOptions) {
b.config = config
}
func (b *bottleRocket) UpdateAWSCredentials(path, profile string) error {
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
content := base64.StdEncoding.EncodeToString(data)
payload, err := createCredentialsPayload(content, profile)
if err != nil {
return err
}
err = b.sendSettingsSetRequest(payload)
if err != nil {
return err
}
err = b.CommitChanges()
if err != nil {
return err
}
return err
}
func (b *bottleRocket) UpdateCredentialProvider(_ string) error {
payload, err := createCredentialProviderPayload(b.config)
if err != nil {
return err
}
err = b.sendSettingsSetRequest(payload)
if err != nil {
return err
}
return err
}
func (b *bottleRocket) CommitChanges() error {
// For Bottlerocket this step is committing all changes at once
commitPath := b.baseURL + "tx/commit_and_apply"
resp, err := b.client.Post(commitPath, "application/json", bytes.NewBuffer(make([]byte, 0)))
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("failed to commit changes: %s", resp.Status)
}
return nil
}
func (b *bottleRocket) sendSettingsSetRequest(payload []byte) error {
settingsPath := b.baseURL + "settings"
req, err := http.NewRequest(http.MethodPatch, settingsPath, bytes.NewBuffer(payload))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
respPatch, err := b.client.Do(req)
if err != nil {
return err
}
defer respPatch.Body.Close()
if respPatch.StatusCode != http.StatusNoContent {
return fmt.Errorf("failed patch request: %s", respPatch.Status)
}
return nil
}
func createCredentialsPayload(content, profile string) ([]byte, error) {
aws := aws{
Config: content,
Profile: profile,
}
creds := awsCred{Aws: aws}
payload, err := json.Marshal(creds)
if err != nil {
return nil, err
}
return payload, nil
}
func createCredentialProviderPayload(config constants.CredentialProviderConfigOptions) ([]byte, error) {
providerConfig := brKubernetes{
Kubernetes: kubernetes{
credentialProviders{
ecrCredentialProvider{
Enabled: true,
ImagePatterns: config.ImagePatterns,
CacheDuration: config.DefaultCacheDuration,
},
},
},
}
payload, err := json.Marshal(providerConfig)
if err != nil {
return nil, err
}
return payload, nil
}
func (b *bottleRocket) isSupportedBRVersion() (bool, error) {
allowedVersions := "v1.11.0"
req, err := http.NewRequest(http.MethodGet, b.baseURL, nil)
if err != nil {
return false, err
}
q := req.URL.Query()
q.Add("prefix", "os")
req.URL.RawQuery = q.Encode()
respGet, err := b.client.Do(req)
if err != nil {
return false, err
}
defer respGet.Body.Close()
if respGet.StatusCode != http.StatusOK {
return false, fmt.Errorf("failed GET request: %s", respGet.Status)
}
valueBody, err := ioutil.ReadAll(respGet.Body)
if err != nil {
return false, err
}
osVersion := brVersion{}
err = json.Unmarshal(valueBody, &osVersion)
if err != nil {
return false, err
}
// Check if BR k8s version is 1.25 or greater. If so we will update allowedVersions to be >1.13.0
if osVersion.Os.VariantID != "" {
variants := strings.Split(osVersion.Os.VariantID, "-")
variantSemVar := "v" + variants[len(variants)-1]
if semver.Compare(variantSemVar, "v1.25") >= 0 {
allowedVersions = "v1.13.0"
}
}
ver := "v" + osVersion.Os.VersionID
valid := semver.Compare(ver, allowedVersions)
return valid > 0, nil
}