-
Notifications
You must be signed in to change notification settings - Fork 111
/
aws.go
376 lines (324 loc) · 11.4 KB
/
aws.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
package osdCloud
import (
"fmt"
"strings"
awsSdk "github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/arn"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/sts"
stsTypes "github.com/aws/aws-sdk-go-v2/service/sts/types"
sdk "github.com/openshift-online/ocm-sdk-go"
bpcloud "github.com/openshift/backplane-cli/cmd/ocm-backplane/cloud"
bpconfig "github.com/openshift/backplane-cli/pkg/cli/config"
"github.com/openshift/osdctl/pkg/provider/aws"
"github.com/openshift/osdctl/pkg/utils"
"github.com/spf13/viper"
)
const (
RhSreCcsAccessRolename = "RH-SRE-CCS-Access"
RhTechnicalSupportAccess = "RH-Technical-Support-Access"
OrganizationAccountAccessRole = "OrganizationAccountAccessRole"
ProdJumproleConfigKey = "prod_jumprole_account_id"
StageJumproleConfigKey = "stage_jumprole_account_id"
)
// GenerateOrganizationAccountAccessCredentials Uses the provided IAM Client to try and assume OrganizationAccountAccessRole for the given AWS Account
// This only works when the provided client is a user from the root account of an organization and the AWS account provided is a linked accounts within that organization
func GenerateOrganizationAccountAccessCredentials(client aws.Client, accountId, sessionName, partition string) (*stsTypes.Credentials, error) {
roleArnString := aws.GenerateRoleARN(accountId, "OrganizationAccountAccessRole")
targetRoleArn, err := arn.Parse(roleArnString)
if err != nil {
return nil, err
}
targetRoleArn.Partition = partition
assumeRoleOutput, err := client.AssumeRole(
&sts.AssumeRoleInput{
RoleArn: awsSdk.String(targetRoleArn.String()),
RoleSessionName: &sessionName,
},
)
if err != nil {
return nil, err
}
return assumeRoleOutput.Credentials, nil
}
// GenerateSupportRoleCredentials Uses the provided IAM Client to perform the Assume Role chain needed to get to a cluster's Support Role
func GenerateSupportRoleCredentials(client aws.Client, region, sessionName, targetRole string) (*stsTypes.Credentials, error) {
// Perform the Assume Role chain to get the jump
jumpRoleCreds, err := GenerateJumpRoleCredentials(client, region, sessionName)
if err != nil {
return nil, err
}
// Build client for jump role needed for the last step
jumpRoleClient, err := aws.NewAwsClientWithInput(
&aws.ClientInput{
AccessKeyID: *jumpRoleCreds.AccessKeyId,
SecretAccessKey: *jumpRoleCreds.SecretAccessKey,
SessionToken: *jumpRoleCreds.SessionToken,
Region: region,
},
)
if err != nil {
return nil, err
}
// Assume target ManagedOpenShift-Support role in the cluster's AWS Account
targetAssumeRoleOutput, err := jumpRoleClient.AssumeRole(
&sts.AssumeRoleInput{
RoleArn: &targetRole,
RoleSessionName: &sessionName,
},
)
if err != nil {
return nil, err
}
return targetAssumeRoleOutput.Credentials, nil
}
// GenerateJumpRoleCredentials performs the Assume Role chain from IAM User to the Jump role
// This sequence stays within the Red Hat account boundary, so a failure here indicates an internal misconfiguration
func GenerateJumpRoleCredentials(client aws.Client, region, sessionName string) (*stsTypes.Credentials, error) {
callerIdentityOutput, err := client.GetCallerIdentity(&sts.GetCallerIdentityInput{})
if err != nil {
return nil, err
}
sreUserArn, err := arn.Parse(*callerIdentityOutput.Arn)
if err != nil {
return nil, err
}
// Assume RH-SRE-CCS-Access role
sreCcsAccessRoleArn := aws.GenerateRoleARN(sreUserArn.AccountID, RhSreCcsAccessRolename)
sreCcsAccessAssumeRoleOutput, err := client.AssumeRole(
&sts.AssumeRoleInput{
RoleArn: &sreCcsAccessRoleArn,
RoleSessionName: &sessionName,
},
)
if err != nil {
return nil, err
}
// Build client for RH-SRE-CCS-Access role
sreCcsAccessRoleClient, err := aws.NewAwsClientWithInput(
&aws.ClientInput{
AccessKeyID: *sreCcsAccessAssumeRoleOutput.Credentials.AccessKeyId,
SecretAccessKey: *sreCcsAccessAssumeRoleOutput.Credentials.SecretAccessKey,
SessionToken: *sreCcsAccessAssumeRoleOutput.Credentials.SessionToken,
Region: region,
},
)
if err != nil {
return nil, err
}
jumpRoleKey := ProdJumproleConfigKey
conn, err := utils.CreateConnection()
if err != nil {
return nil, err
}
defer conn.Close()
currentEnv := utils.GetCurrentOCMEnv(conn)
if currentEnv == "stage" || currentEnv == "integration" {
jumpRoleKey = StageJumproleConfigKey
}
if !viper.IsSet(jumpRoleKey) {
return nil, fmt.Errorf("key %s is not set in config file", jumpRoleKey)
}
// Assume jump role
// This will be different between stage and prod. There's probably a better way to do this that isn't hardcoding
jumproleAccountID := viper.GetString(jumpRoleKey)
jumpRoleArn := aws.GenerateRoleARN(jumproleAccountID, RhTechnicalSupportAccess)
jumpAssumeRoleOutput, err := sreCcsAccessRoleClient.AssumeRole(
&sts.AssumeRoleInput{
RoleArn: &jumpRoleArn,
RoleSessionName: &sessionName,
},
)
if err != nil {
return nil, err
}
return jumpAssumeRoleOutput.Credentials, nil
}
// GenerateRoleSessionName Uses the current IAM ARN to generate a role name. This should end up being RH-SRE-$kerberosID
func GenerateRoleSessionName(client aws.Client) (string, error) {
callerIdentityOutput, err := client.GetCallerIdentity(&sts.GetCallerIdentityInput{})
if err != nil {
return "", err
}
roleArn, err := arn.Parse(*callerIdentityOutput.Arn)
if err != nil {
return "", err
}
splitArn := strings.Split(roleArn.Resource, "/")
username := splitArn[1]
return fmt.Sprintf("RH-SRE-%s", username), nil
}
// CreateAWSV2Config creates an aws-sdk-go-v2 config via Backplane given an internal cluster id
func CreateAWSV2Config(clusterID string) (awsSdk.Config, error) {
bp, err := bpconfig.GetBackplaneConfiguration()
if err != nil {
return awsSdk.Config{}, fmt.Errorf("failed to load backplane-cli config: %v", err)
}
return bpcloud.GetAWSV2Config(bp.URL, clusterID)
}
func GenerateCCSClusterAWSClient(ocmClient *sdk.Connection, awsClient aws.Client, clusterID string, clusterRegion string, partition string, sessionName string) (aws.Client, error) {
// Determine the right jump role
targetRoleArnString, err := utils.GetSupportRoleArnForCluster(ocmClient, clusterID)
if err != nil {
return nil, err
}
targetRoleArn, err := arn.Parse(targetRoleArnString)
if err != nil {
return nil, err
}
targetRoleArn.Partition = partition
// Start the jump role chain. Result should be credentials for the ManagedOpenShift Support role for the target cluster
assumedRoleCreds, err := GenerateSupportRoleCredentials(awsClient, clusterRegion, sessionName, targetRoleArn.String())
if err != nil {
return nil, err
}
awsClientCCS, err := aws.NewAwsClientWithInput(&aws.ClientInput{
AccessKeyID: *assumedRoleCreds.AccessKeyId,
SecretAccessKey: *assumedRoleCreds.SecretAccessKey,
SessionToken: *assumedRoleCreds.SessionToken,
Region: clusterRegion,
})
if err != nil {
return nil, err
}
return awsClientCCS, nil
}
func GenerateNonCCSClusterAWSClient(ocmClient *sdk.Connection, awsClient aws.Client, clusterID string, clusterRegion string, partition string, sessionName string) (aws.Client, error) {
accountID, err := utils.GetAWSAccountIdForCluster(ocmClient, clusterID)
if err != nil {
return nil, err
}
// If the cluster is non-CCS, or an AWS Account ID was provided with -i, try and use OrganizationAccountAccessRole
assumedRoleCreds, err := GenerateOrganizationAccountAccessCredentials(awsClient, accountID, sessionName, partition)
if err != nil {
fmt.Printf("Could not build AWS Client for OrganizationAccountAccessRole: %s\n", err)
return nil, err
}
awsClientNonCCS, err := aws.NewAwsClientWithInput(&aws.ClientInput{
AccessKeyID: *assumedRoleCreds.AccessKeyId,
SecretAccessKey: *assumedRoleCreds.SecretAccessKey,
SessionToken: *assumedRoleCreds.SessionToken,
Region: clusterRegion,
})
if err != nil {
return nil, err
}
return awsClientNonCCS, nil
}
// GenerateAWSClientForCluster generates an AWS client given an OCM cluster id and AWS profile name.
// If an AWS profile name is not specified, this function will also read the AWS_PROFILE environment
// variable or use the default AWS profile.
func GenerateAWSClientForCluster(awsProfile string, clusterID string) (aws.Client, error) {
ocmClient, err := utils.CreateConnection()
if err != nil {
return nil, err
}
defer ocmClient.Close()
cluster, err := utils.GetClusterAnyStatus(ocmClient, clusterID)
if err != nil {
fmt.Println(err)
return nil, err
}
clusterRegion := cluster.Region().ID()
internalClusterId := cluster.ID()
// Builds the base client using the provided creds (via profile or env vars)
awsClient, err := aws.NewAwsClient(awsProfile, clusterRegion, "")
if err != nil {
fmt.Printf("Could not build AWS Client: %s\n", err)
return nil, err
}
// Get the right partition for the final ARN
partition, err := aws.GetAwsPartition(awsClient)
if err != nil {
return nil, err
}
// Generate a session name using the SRE's kerberos ID
sessionName, err := GenerateRoleSessionName(awsClient)
if err != nil {
fmt.Printf("Could not generate Session Name: %s\n", err)
return nil, err
}
if cluster.CCS().Enabled() {
awsClient, err = GenerateCCSClusterAWSClient(ocmClient, awsClient, internalClusterId, clusterRegion, partition, sessionName)
} else {
awsClient, err = GenerateNonCCSClusterAWSClient(ocmClient, awsClient, internalClusterId, clusterRegion, partition, sessionName)
}
return awsClient, err
}
// AwsCluster Concrete struct with fields required only for interacting with the AWS cloud.
type AwsCluster struct {
*BaseClient
AZs []string
AwsProfile string
AwsClient aws.Client
}
func NewAwsCluster(ocmClient *sdk.Connection, clusterId string, awsProfile string) (ClusterHealthClient, error) {
clusterResp, err := ocmClient.ClustersMgmt().V1().Clusters().Cluster(clusterId).Get().Send()
if err != nil {
fmt.Println(err)
return nil, err
}
cluster := clusterResp.Body()
return &AwsCluster{
BaseClient: &BaseClient{
ClusterId: clusterId,
OcmClient: ocmClient,
Cluster: cluster,
},
AwsProfile: awsProfile,
}, nil
}
func (a *AwsCluster) Login() error {
awsClient, err := GenerateAWSClientForCluster(a.AwsProfile, a.ClusterId)
a.AwsClient = awsClient
a.AZs = a.Cluster.Nodes().AvailabilityZones()
if err != nil {
return err
}
return nil
}
func (a *AwsCluster) Close() {
}
func (a *AwsCluster) GetAZs() []string {
return a.AZs
}
func (a *AwsCluster) GetAllVirtualMachines(string) ([]VirtualMachine, error) {
vms := make([]VirtualMachine, 5)
var nextToken *string
for {
instances, err := a.AwsClient.DescribeInstances(&ec2.DescribeInstancesInput{
MaxResults: awsSdk.Int32(5),
NextToken: nextToken,
})
if err != nil {
return nil, err
}
for idx := range instances.Reservations {
for _, instance := range instances.Reservations[idx].Instances {
stringTags := make(map[string]string, 0)
var name string
size := instance.InstanceType
state := instance.State.Name
for _, t := range instance.Tags {
stringTags[*t.Key] = *t.Value
if *t.Key == "Name" {
name = *t.Value
}
}
vm := VirtualMachine{
Original: instance,
Name: name,
Size: string(size),
State: string(state),
Labels: stringTags,
}
vms = append(vms, vm)
}
}
if instances.NextToken == nil {
break
}
nextToken = instances.NextToken
}
return vms, nil
}