forked from openshift/rosa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
468 lines (416 loc) · 13.1 KB
/
helpers.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
Copyright (c) 2020 Red Hat, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package ocm
import (
"crypto/x509"
"fmt"
"io/ioutil"
"math/rand"
"net"
"net/http"
"net/url"
"regexp"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws/arn"
errors "github.com/zgalor/weberr"
amsv1 "github.com/openshift-online/ocm-sdk-go/accountsmgmt/v1"
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
ocmerrors "github.com/openshift-online/ocm-sdk-go/errors"
"github.com/openshift/rosa/pkg/helper"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
const (
ANY = "any"
HibernateCapability = "capability.organization.hibernate_cluster"
//Pendo Events
Success = "Success"
Failure = "Failure"
Response = "Response"
ClusterID = "ClusterID"
Version = "Version"
Username = "Username"
URL = "URL"
OCMRoleLabel = "sts_ocm_role"
USERRoleLabel = "sts_user_role"
)
// Regular expression to used to make sure that the identifier or name given by the user is
// safe and that it there is no risk of SQL injection:
var clusterKeyRE = regexp.MustCompile(`^(\w|-)+$`)
// Cluster names must be valid DNS-1035 labels, so they must consist of lower case alphanumeric
// characters or '-', start with an alphabetic character, and end with an alphanumeric character
var clusterNameRE = regexp.MustCompile(`^[a-z]([-a-z0-9]{0,13}[a-z0-9])?$`)
var badUsernameRE = regexp.MustCompile(`^(~|\.?\.|.*[:\/%].*)$`)
func IsValidClusterKey(clusterKey string) bool {
return clusterKeyRE.MatchString(clusterKey)
}
func IsValidClusterName(clusterName string) bool {
return clusterNameRE.MatchString(clusterName)
}
func ClusterNameValidator(name interface{}) error {
if str, ok := name.(string); ok {
if !IsValidClusterName(str) {
return fmt.Errorf("Cluster name must consist of no more than 15 lowercase " +
"alphanumeric characters or '-', start with a letter, and end with an " +
"alphanumeric character.")
}
return nil
}
return fmt.Errorf("can only validate strings, got %v", name)
}
func ValidateHTTPProxy(val interface{}) error {
if httpProxy, ok := val.(string); ok {
if httpProxy == "" {
return nil
}
url, err := url.ParseRequestURI(httpProxy)
if err != nil {
return fmt.Errorf("Invalid http-proxy value '%s'", httpProxy)
}
if url.Scheme != "http" {
return errors.Errorf("%s", "Expected http-proxy to have an http:// scheme")
}
return nil
}
return fmt.Errorf("can only validate strings, got %v", val)
}
func ValidateAdditionalTrustBundle(val interface{}) error {
if additionalTrustBundleFile, ok := val.(string); ok {
if additionalTrustBundleFile == "" {
return nil
}
cert, err := ioutil.ReadFile(additionalTrustBundleFile)
if err != nil {
return err
}
additionalTrustBundle := string(cert)
if additionalTrustBundle == "" {
return errors.Errorf("%s", "Trust bundle file is empty")
}
additionalTrustBundleBytes := []byte(additionalTrustBundle)
if !x509.NewCertPool().AppendCertsFromPEM(additionalTrustBundleBytes) {
return errors.Errorf("%s", "Failed to parse additional trust bundle")
}
return nil
}
return fmt.Errorf("can only validate strings, got %v", val)
}
func IsValidUsername(username string) bool {
return !badUsernameRE.MatchString(username)
}
func IsEmptyCIDR(cidr net.IPNet) bool {
return cidr.String() == "<nil>"
}
// Determine whether a resources is compatible with ROSA clusters in general
func isCompatible(relatedResource *amsv1.RelatedResource) bool {
product := strings.ToLower(relatedResource.Product())
cloudProvider := strings.ToLower(relatedResource.CloudProvider())
byoc := strings.ToLower(relatedResource.BYOC())
// nolint:goconst
return (product == ANY || product == "rosa" || product == "moa") &&
(cloudProvider == ANY || cloudProvider == "aws") &&
(byoc == ANY || byoc == "byoc")
}
func handleErr(res *ocmerrors.Error, err error) error {
msg := res.Reason()
if msg == "" {
msg = err.Error()
}
// Hack to always display the correct terms and conditions message
if res.Code() == "CLUSTERS-MGMT-451" {
msg = "You must accept the Terms and Conditions in order to continue.\n" +
"Go to https://www.redhat.com/wapps/tnc/ackrequired?site=ocm&event=register\n" +
"Once you accept the terms, you will need to retry the action that was blocked."
}
errType := errors.ErrorType(res.Status())
return errType.Set(errors.Errorf("%s", msg))
}
func (c *Client) GetDefaultClusterFlavors(flavour string) (dMachinecidr *net.IPNet, dPodcidr *net.IPNet,
dServicecidr *net.IPNet, dhostPrefix int) {
flavourGetResponse, err := c.ocm.ClustersMgmt().V1().Flavours().Flavour(flavour).Get().Send()
if err != nil {
flavourGetResponse, _ = c.ocm.ClustersMgmt().V1().Flavours().Flavour("osd-4").Get().Send()
}
network, ok := flavourGetResponse.Body().GetNetwork()
if !ok {
return nil, nil, nil, 0
}
_, dMachinecidr, err = net.ParseCIDR(network.MachineCIDR())
if err != nil {
dMachinecidr = nil
}
_, dPodcidr, err = net.ParseCIDR(network.PodCIDR())
if err != nil {
dPodcidr = nil
}
_, dServicecidr, err = net.ParseCIDR(network.ServiceCIDR())
if err != nil {
dServicecidr = nil
}
dhostPrefix, _ = network.GetHostPrefix()
return dMachinecidr, dPodcidr, dServicecidr, dhostPrefix
}
func (c *Client) LogEvent(key string, body map[string]string) {
event, err := cmv1.NewEvent().Key(key).Body(body).Build()
if err == nil {
_, _ = c.ocm.ClustersMgmt().V1().
Events().
Add().
Body(event).
Send()
}
}
func (c *Client) GetCurrentAccount() (*amsv1.Account, error) {
response, err := c.ocm.AccountsMgmt().V1().
CurrentAccount().
Get().
Send()
if err != nil {
if response.Status() == http.StatusNotFound {
return nil, nil
}
return nil, handleErr(response.Error(), err)
}
return response.Body(), nil
}
func (c *Client) GetCurrentOrganization() (id string, externalID string, err error) {
acctResponse, err := c.GetCurrentAccount()
if err != nil {
return
}
id = acctResponse.Organization().ID()
externalID = acctResponse.Organization().ExternalID()
return
}
func (c *Client) IsHibernateCapabilityEnabled() error {
organizationID, _, err := c.GetCurrentOrganization()
if err != nil {
return err
}
isCapabilityEnable, err := c.IsCapabilityEnabled(HibernateCapability, organizationID)
if err != nil {
return err
}
if !isCapabilityEnable {
return fmt.Errorf("The '%s' capability is not set for org '%s'", HibernateCapability, organizationID)
}
return nil
}
func (c *Client) IsCapabilityEnabled(capabilityName string, orgID string) (bool, error) {
capabilityResponse, err := c.ocm.AccountsMgmt().V1().Organizations().
Organization(orgID).Get().Parameter("fetchCapabilities", true).Send()
if err != nil {
return false, handleErr(capabilityResponse.Error(), err)
}
if len(capabilityResponse.Body().Capabilities()) > 0 {
for _, capability := range capabilityResponse.Body().Capabilities() {
if capability.Name() == capabilityName {
return capability.Value() == "true", nil
}
}
}
return false, nil
}
// ASCII codes of important characters:
const (
aCode = 97
zCode = 122
zeroCode = 48
nineCode = 57
)
// Number of letters and digits:
const (
letterCount = zCode - aCode + 1
digitCount = nineCode - zeroCode + 1
)
func RandomLabel(size int) string {
value := rand.Int() // #nosec G404
chars := make([]byte, size)
for size > 0 {
size--
if size%2 == 0 {
chars[size] = byte(aCode + value%letterCount)
value = value / letterCount
} else {
chars[size] = byte(zeroCode + value%digitCount)
value = value / digitCount
}
}
return string(chars)
}
func (c *Client) LinkAccountRole(accountID string, roleARN string) error {
resp, err := c.ocm.AccountsMgmt().V1().Accounts().Account(accountID).
Labels().Labels("sts_user_role").Get().Send()
if err != nil && resp.Status() != 404 {
if resp.Status() == 403 {
return errors.Forbidden.UserErrorf("%v", err)
}
return handleErr(resp.Error(), err)
}
existingARN := resp.Body().Value()
exists := false
if existingARN != "" {
existingARNArr := strings.Split(existingARN, ",")
if len(existingARNArr) > 0 {
for _, value := range existingARNArr {
if value == roleARN {
exists = true
break
}
}
}
}
if exists {
return nil
}
if existingARN != "" {
roleARN = existingARN + "," + roleARN
}
labelBuilder, err := amsv1.NewLabel().Key("sts_user_role").Value(roleARN).Build()
if err != nil {
return err
}
_, err = c.ocm.AccountsMgmt().V1().Accounts().Account(accountID).
Labels().Add().Body(labelBuilder).Send()
if err != nil {
return handleErr(resp.Error(), err)
}
return err
}
func (c *Client) UnlinkOCMRoleFromOrg(orgID string, roleARN string) error {
linkedRoles, err := c.GetOrganizationLinkedOCMRoles(orgID)
if err != nil {
return err
}
if helper.Contains(linkedRoles, roleARN) {
linkedRoles = helper.RemoveStrFromSlice(linkedRoles, roleARN)
if len(linkedRoles) > 0 {
newRoleARN := strings.Join(linkedRoles, ",")
label, err := amsv1.NewLabel().Key(OCMRoleLabel).Value(newRoleARN).Build()
if err != nil {
return err
}
resp, err := c.ocm.AccountsMgmt().V1().Organizations().Organization(orgID).Labels().
Labels(OCMRoleLabel).Update().Body(label).Send()
if err != nil {
return handleErr(resp.Error(), err)
}
} else {
resp, err := c.ocm.AccountsMgmt().V1().Organizations().Organization(orgID).Labels().
Labels(OCMRoleLabel).Delete().Send()
if err != nil {
return handleErr(resp.Error(), err)
}
}
return nil
}
return errors.UserErrorf("Role-arn '%s' is not linked with the organization account '%s'", roleARN, orgID)
}
func (c *Client) LinkOrgToRole(orgID string, roleARN string) (bool, error) {
parsedARN, err := arn.Parse(roleARN)
if err != nil {
return false, err
}
exists, existingARN, selectedARN, err := c.CheckIfAWSAccountExists(orgID, parsedARN.AccountID)
if err != nil {
return false, err
}
if exists {
if selectedARN != roleARN {
return false, errors.UserErrorf("User organization '%s' has role-arn '%s' associated. "+
"Only one role can be linked per AWS account per organization", orgID, selectedARN)
}
return false, nil
}
if existingARN != "" {
roleARN = existingARN + "," + roleARN
}
labelBuilder, err := amsv1.NewLabel().Key(OCMRoleLabel).Value(roleARN).Build()
if err != nil {
return false, err
}
resp, err := c.ocm.AccountsMgmt().V1().Organizations().Organization(orgID).
Labels().Add().Body(labelBuilder).Send()
if err != nil {
return false, handleErr(resp.Error(), err)
}
return true, nil
}
func (c *Client) GetAccountLinkedUserRoles(accountID string) ([]string, error) {
resp, err := c.ocm.AccountsMgmt().V1().Accounts().Account(accountID).
Labels().Labels(USERRoleLabel).Get().Send()
if err != nil && resp.Status() != http.StatusNotFound {
return nil, handleErr(resp.Error(), err)
}
return strings.Split(resp.Body().Value(), ","), nil
}
func (c *Client) GetOrganizationLinkedOCMRoles(orgID string) ([]string, error) {
resp, err := c.ocm.AccountsMgmt().V1().Organizations().Organization(orgID).
Labels().Labels(OCMRoleLabel).Get().Send()
if err != nil && resp.Status() != http.StatusNotFound {
return nil, err
}
return strings.Split(resp.Body().Value(), ","), nil
}
func (c *Client) CheckIfAWSAccountExists(orgID string, awsAccountID string) (bool, string, string, error) {
resp, err := c.ocm.AccountsMgmt().V1().Organizations().Organization(orgID).
Labels().Labels(OCMRoleLabel).Get().Send()
if err != nil && resp.Status() != 404 {
if resp.Status() == 403 {
return false, "", "", errors.Forbidden.UserErrorf("%v", err)
}
return false, "", "", handleErr(resp.Error(), err)
}
existingARN := resp.Body().Value()
exists := false
selectedARN := ""
if existingARN != "" {
existingARNArr := strings.Split(existingARN, ",")
if len(existingARNArr) > 0 {
for _, value := range existingARNArr {
parsedARN, err := arn.Parse(value)
if err != nil {
return false, "", "", err
}
if parsedARN.AccountID == awsAccountID {
exists = true
selectedARN = value
break
}
}
}
}
return exists, existingARN, selectedARN, nil
}
/**
We should allow only one role per aws account per organization
If the user request same ocm role we should let them proceed to ensure they can add admin role
if not exists or attach policies or link etc
if the user request diff ocm role name we error out
*/
func (c *Client) CheckRoleExists(orgID string, roleName string, awsAccountID string) (bool, string, error) {
exists, _, selectedARN, err := c.CheckIfAWSAccountExists(orgID, awsAccountID)
if err != nil {
return false, "", err
}
if !exists {
return false, "", nil
}
existingRole := strings.SplitN(selectedARN, "/", 2)
if len(existingRole) > 1 && existingRole[1] == roleName {
return false, "", nil
}
return true, existingRole[1], nil
}