-
Notifications
You must be signed in to change notification settings - Fork 185
/
cloudformation.go
230 lines (198 loc) · 7.06 KB
/
cloudformation.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
/*
Copyright (c) 2021 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 aws
import (
"context"
"errors"
"fmt"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudformation"
cloudformationtypes "github.com/aws/aws-sdk-go-v2/service/cloudformation/types"
iamtypes "github.com/aws/aws-sdk-go-v2/service/iam/types"
"github.com/aws/smithy-go"
"github.com/openshift/rosa/assets"
)
func readCloudFormationTemplate(path string) (string, error) {
cfTemplate, err := assets.Asset(path)
if err != nil {
return "", fmt.Errorf("Unable to read cloudformation template: %s", err)
}
return string(cfTemplate), nil
}
// Ensure osdCcsAdmin IAM user is created
func (c *awsClient) EnsureOsdCcsAdminUser(stackName string, adminUserName string, awsRegion string) (bool, error) {
userExists := true
regionForInit, err := c.GetClusterRegionTagForUser(adminUserName)
if err != nil {
//If user doesn't exists proceed normally
//If users exists and tag is not present then add the tag
//if user exists and tag is present then proceed normally
var noEntityErr *iamtypes.NoSuchEntityException
if errors.As(err, &noEntityErr) {
userExists = false
} else {
return false, err
}
}
if userExists {
if regionForInit == "" {
err = c.TagUserRegion(adminUserName, DefaultRegion)
if err != nil {
return false, err
}
}
return false, nil
}
// Check already existing cloudformation stack status
stackReady, stackStatus, err := c.CheckStackReadyOrNotExisting(stackName)
if err != nil {
return false, err
}
// Read cloudformation template
cfTemplatePath := "templates/cloudformation/iam_user_osdCcsAdmin.json"
cfTemplateBody, err := readCloudFormationTemplate(cfTemplatePath)
if err != nil {
return false, err
}
// If stack CREATE_COMPLETE or UPGRADE_COMPLETE the stack is already create
// try to update it in case the cloudformation template has changed
if stackStatus != nil {
if (*stackStatus == string(cloudformationtypes.StackStatusCreateComplete)) ||
(*stackStatus == string(cloudformationtypes.StackStatusUpdateComplete)) {
err = c.UpdateStack(cfTemplateBody, stackName)
if err != nil {
return false, err
}
return false, nil
}
}
// If the Cloudformation stack isn't ready, make sure the IAM user
// doesn't exist or the Cloudformation stack create will fail
if !stackReady {
err = c.CheckAdminUserNotExisting(adminUserName)
if err != nil {
return false, err
}
}
// Create stack
_, err = c.CreateStack(cfTemplateBody, stackName)
if err != nil {
return false, err
}
err = c.TagUserRegion(adminUserName, awsRegion)
if err != nil {
return false, err
}
return true, nil
}
func (c *awsClient) CreateStack(cfTemplateBody, stackName string) (bool, error) {
// Create cloudformation stack
_, err := c.cfClient.CreateStack(context.Background(), buildCreateStackInput(cfTemplateBody, stackName))
if err != nil {
return false, err
}
err = waitForStackCreateComplete(context.Background(), c.cfClient, stackName)
if err != nil {
return false, err
}
return true, nil
}
func (c *awsClient) UpdateStack(cfTemplateBody, stackName string) error {
_, err := c.cfClient.UpdateStack(context.TODO(), buildUpdateStackInput(cfTemplateBody, stackName))
if err != nil {
var apiErr smithy.APIError
if errors.As(err, &apiErr) {
if apiErr.ErrorCode() == "ValidationError" &&
strings.Contains(apiErr.ErrorMessage(), "No updates are to be performed") {
// No updates are to be performed
return nil
}
}
return err
}
// Wait for CloudFormation update to complete
err = waitForStackUpdateComplete(context.TODO(), c.cfClient, stackName)
if err != nil {
return err
}
return nil
}
func (c *awsClient) CheckStackReadyOrNotExisting(stackName string) (stackReady bool, status *string, err error) {
stackList, err := c.cfClient.ListStacks(context.TODO(), &cloudformation.ListStacksInput{})
if err != nil {
return false, nil, err
}
for _, summary := range stackList.StackSummaries {
if *summary.StackName == stackName {
if (summary.StackStatus == cloudformationtypes.StackStatusCreateComplete) ||
(summary.StackStatus == cloudformationtypes.StackStatusUpdateComplete) {
stackStatus := string(summary.StackStatus)
return true, &stackStatus, nil
}
if summary.StackStatus != cloudformationtypes.StackStatusDeleteComplete {
stackStatus := string(summary.StackStatus)
return false, &stackStatus, fmt.Errorf("Error creating user: CloudFormation stack %s exists "+
"with status %s. Expected status is %s.\n"+
"Ensure %s CloudFormation Stack does not exist, then retry with\n"+
"rosa init --delete-stack; rosa init",
*summary.StackName, summary.StackStatus, cloudformationtypes.StackStatusCreateComplete, *summary.StackName)
}
}
}
return false, nil, nil
}
func (c *awsClient) DeleteOsdCcsAdminUser(stackName string) error {
deleteStackInput := &cloudformation.DeleteStackInput{
StackName: aws.String(stackName),
}
// Delete cloudformation stack
_, err := c.cfClient.DeleteStack(context.Background(), deleteStackInput)
if err != nil {
var tokenExistsErr *cloudformationtypes.TokenAlreadyExistsException
if errors.As(err, &tokenExistsErr) {
return nil
}
return err
}
// Wait until cloudformation stack deletes
err = waitForStackDeleteComplete(context.Background(), c.cfClient, stackName)
if err != nil {
return err
}
return nil
}
// Build cloudformation create stack input
func buildCreateStackInput(cfTemplateBody, stackName string) *cloudformation.CreateStackInput {
// Special cloudformation capabilities are required to create IAM resources in AWS
cfCapabilityIAM := cloudformationtypes.CapabilityCapabilityIam
cfCapabilityNamedIAM := cloudformationtypes.CapabilityCapabilityNamedIam
cfTemplateCapabilities := []cloudformationtypes.Capability{cfCapabilityIAM, cfCapabilityNamedIAM}
return &cloudformation.CreateStackInput{
Capabilities: cfTemplateCapabilities,
StackName: aws.String(stackName),
TemplateBody: aws.String(cfTemplateBody),
}
}
// Build cloudformation update stack input
func buildUpdateStackInput(cfTemplateBody, stackName string) *cloudformation.UpdateStackInput {
// Special cloudformation capabilities are required to update IAM resources in AWS
cfCapabilityIAM := cloudformationtypes.CapabilityCapabilityIam
cfCapabilityNamedIAM := cloudformationtypes.CapabilityCapabilityNamedIam
cfTemplateCapabilities := []cloudformationtypes.Capability{cfCapabilityIAM, cfCapabilityNamedIAM}
return &cloudformation.UpdateStackInput{
Capabilities: cfTemplateCapabilities,
StackName: aws.String(stackName),
TemplateBody: aws.String(cfTemplateBody),
}
}