@@ -18,12 +18,29 @@ import (
18
18
"github.com/openshift/rosa/pkg/rosa"
19
19
)
20
20
21
+ type operatorRolesInput struct {
22
+ prefix string
23
+ permissionsBoundary string
24
+ cluster * cmv1.Cluster
25
+ accountRoleVersion string
26
+ policies map [string ]* cmv1.AWSSTSPolicy
27
+ defaultVersion string
28
+ credRequests map [string ]* cmv1.STSOperator
29
+ managedPolicies bool
30
+ hostedCPPolicies bool
31
+ isHcpSharedVpc bool
32
+ route53RoleArn string
33
+ vpcEndpointRoleArn string
34
+ }
35
+
21
36
func handleOperatorRoleCreationByClusterKey (r * rosa.Runtime , env string ,
22
37
permissionsBoundary string , mode string ,
23
38
policies map [string ]* cmv1.AWSSTSPolicy ,
24
- defaultPolicyVersion string ) error {
39
+ defaultPolicyVersion string , isHcpSharedVpc bool ) error {
25
40
clusterKey := r .GetClusterKey ()
26
41
cluster := r .FetchCluster ()
42
+ route53RoleArn := args .sharedVpcRoleArn
43
+ vpcEndpointRoleArn := args .vpcEndpointRoleArn
27
44
if cluster .AWS ().STS ().RoleARN () == "" {
28
45
r .Reporter .Errorf ("Cluster '%s' is not an STS cluster." , clusterKey )
29
46
os .Exit (1 )
@@ -101,8 +118,20 @@ func handleOperatorRoleCreationByClusterKey(r *rosa.Runtime, env string,
101
118
r .Reporter .Errorf ("Error getting account role version '%v'" , err )
102
119
os .Exit (1 )
103
120
}
104
- err = createRoles (r , operatorRolePolicyPrefix , permissionsBoundary , cluster ,
105
- accountRoleVersion , policies , defaultPolicyVersion , credRequests , managedPolicies , hostedCPPolicies )
121
+ err = createRoles (r , operatorRolesInput {
122
+ prefix : operatorRolePolicyPrefix ,
123
+ permissionsBoundary : permissionsBoundary ,
124
+ cluster : cluster ,
125
+ accountRoleVersion : accountRoleVersion ,
126
+ policies : policies ,
127
+ defaultVersion : defaultPolicyVersion ,
128
+ credRequests : credRequests ,
129
+ managedPolicies : managedPolicies ,
130
+ hostedCPPolicies : hostedCPPolicies ,
131
+ isHcpSharedVpc : isHcpSharedVpc ,
132
+ route53RoleArn : route53RoleArn ,
133
+ vpcEndpointRoleArn : vpcEndpointRoleArn ,
134
+ })
106
135
if err != nil {
107
136
r .Reporter .Errorf ("There was an error creating the operator roles: '%v'" , err )
108
137
isThrottle := "false"
@@ -147,23 +176,12 @@ func handleOperatorRoleCreationByClusterKey(r *rosa.Runtime, env string,
147
176
return nil
148
177
}
149
178
150
- func createRoles (
151
- r * rosa.Runtime ,
152
- prefix string ,
153
- permissionsBoundary string ,
154
- cluster * cmv1.Cluster ,
155
- accountRoleVersion string ,
156
- policies map [string ]* cmv1.AWSSTSPolicy ,
157
- defaultVersion string ,
158
- credRequests map [string ]* cmv1.STSOperator ,
159
- managedPolicies bool ,
160
- hostedCPPolicies bool ,
161
- ) error {
162
- sharedVpcRoleArn := cluster .AWS ().PrivateHostedZoneRoleARN ()
179
+ func createRoles (r * rosa.Runtime , createInput operatorRolesInput ) error {
180
+ sharedVpcRoleArn := createInput .cluster .AWS ().PrivateHostedZoneRoleARN ()
163
181
isSharedVpc := sharedVpcRoleArn != ""
164
182
165
- for credrequest , operator := range credRequests {
166
- ver := cluster .Version ()
183
+ for credrequest , operator := range createInput . credRequests {
184
+ ver := createInput . cluster .Version ()
167
185
if ver != nil && operator .MinVersion () != "" {
168
186
isSupported , err := ocm .CheckSupportedVersion (ocm .GetVersionMinor (ver .ID ()), operator .MinVersion ())
169
187
if err != nil {
@@ -174,30 +192,48 @@ func createRoles(
174
192
continue
175
193
}
176
194
}
177
- roleName , _ := aws .FindOperatorRoleNameBySTSOperator (cluster , operator )
195
+ roleName , _ := aws .FindOperatorRoleNameBySTSOperator (createInput . cluster , operator )
178
196
if roleName == "" {
179
197
return fmt .Errorf ("Failed to find operator IAM role" )
180
198
}
181
199
182
- path , err := aws .GetPathFromAccountRole (cluster , aws .AccountRoles [aws .InstallerAccountRole ].Name )
200
+ path , err := aws .GetPathFromAccountRole (createInput . cluster , aws .AccountRoles [aws .InstallerAccountRole ].Name )
183
201
if err != nil {
184
202
return err
185
203
}
186
204
187
205
var policyArn string
188
- filename := aws .GetOperatorPolicyKey (credrequest , hostedCPPolicies , isSharedVpc )
189
- if managedPolicies {
190
- policyArn , err = aws .GetManagedPolicyARN (policies , filename )
206
+ var policyArns []string
207
+ filename := aws .GetOperatorPolicyKey (credrequest , createInput .hostedCPPolicies , isSharedVpc )
208
+ if createInput .managedPolicies {
209
+ policyArn , err = aws .GetManagedPolicyARN (createInput .policies , filename )
191
210
if err != nil {
192
211
return err
193
212
}
213
+ if createInput .isHcpSharedVpc {
214
+ if credrequest == aws .IngressOperatorCloudCredentialsRoleType {
215
+ sharedVpcPolicyArn , err := getHcpSharedVpcPolicy (r , sharedVpcRoleArn , createInput .defaultVersion )
216
+ if err != nil {
217
+ return err
218
+ }
219
+ policyArns = append (policyArns , sharedVpcPolicyArn )
220
+ } else if credrequest == aws .ControlPlaneCloudCredentialsRoleType {
221
+ for _ , arn := range []string {createInput .vpcEndpointRoleArn , sharedVpcRoleArn } {
222
+ sharedVpcPolicyArn , err := getHcpSharedVpcPolicy (r , arn , createInput .defaultVersion )
223
+ if err != nil {
224
+ return err
225
+ }
226
+ policyArns = append (policyArns , sharedVpcPolicyArn )
227
+ }
228
+ }
229
+ }
194
230
} else {
195
- policyArn = aws .GetOperatorPolicyARN (r .Creator .Partition , r .Creator .AccountID , prefix , operator . Namespace () ,
196
- operator .Name (), path )
197
- policyDetails := aws .GetPolicyDetails (policies , filename )
231
+ policyArn = aws .GetOperatorPolicyARN (r .Creator .Partition , r .Creator .AccountID , createInput . prefix ,
232
+ operator .Namespace (), operator . Name (), path )
233
+ policyDetails := aws .GetPolicyDetails (createInput . policies , filename )
198
234
199
235
if isSharedVpc && credrequest == aws .IngressOperatorCloudCredentialsRoleType {
200
- err = validateIngressOperatorPolicyOverride (r , policyArn , sharedVpcRoleArn , prefix )
236
+ err = validateIngressOperatorPolicyOverride (r , policyArn , sharedVpcRoleArn , createInput . prefix )
201
237
if err != nil {
202
238
return err
203
239
}
@@ -208,30 +244,31 @@ func createRoles(
208
244
}
209
245
210
246
operatorPolicyTags := map [string ]string {
211
- common .OpenShiftVersion : accountRoleVersion ,
212
- tags .RolePrefix : prefix ,
247
+ common .OpenShiftVersion : createInput . accountRoleVersion ,
248
+ tags .RolePrefix : createInput . prefix ,
213
249
tags .RedHatManaged : helper .True ,
214
250
tags .OperatorNamespace : operator .Namespace (),
215
251
tags .OperatorName : operator .Name (),
216
252
}
217
253
218
254
if args .forcePolicyCreation || (isSharedVpc && credrequest == aws .IngressOperatorCloudCredentialsRoleType ) {
219
255
policyArn , err = r .AWSClient .ForceEnsurePolicy (policyArn , policyDetails ,
220
- defaultVersion , operatorPolicyTags , path )
256
+ createInput . defaultVersion , operatorPolicyTags , path )
221
257
if err != nil {
222
258
return err
223
259
}
224
260
} else {
225
261
policyArn , err = r .AWSClient .EnsurePolicy (policyArn , policyDetails ,
226
- defaultVersion , operatorPolicyTags , path )
262
+ createInput . defaultVersion , operatorPolicyTags , path )
227
263
if err != nil {
228
264
return err
229
265
}
230
266
}
231
267
}
268
+ policyArns = append (policyArns , policyArn )
232
269
233
- policyDetails := aws .GetPolicyDetails (policies , "operator_iam_role_policy" )
234
- policy , err := aws .GenerateOperatorRolePolicyDoc (r .Creator .Partition , cluster ,
270
+ policyDetails := aws .GetPolicyDetails (createInput . policies , "operator_iam_role_policy" )
271
+ policy , err := aws .GenerateOperatorRolePolicyDoc (r .Creator .Partition , createInput . cluster ,
235
272
r .Creator .AccountID , operator , policyDetails )
236
273
if err != nil {
237
274
return err
@@ -243,29 +280,31 @@ func createRoles(
243
280
tags .OperatorName : operator .Name (),
244
281
tags .RedHatManaged : helper .True ,
245
282
}
246
- if ! ocm .IsOidcConfigReusable (cluster ) {
247
- tagsList [tags .ClusterID ] = cluster .ID ()
283
+ if ! ocm .IsOidcConfigReusable (createInput . cluster ) {
284
+ tagsList [tags .ClusterID ] = createInput . cluster .ID ()
248
285
}
249
- if managedPolicies {
286
+ if createInput . managedPolicies {
250
287
tagsList [common .ManagedPolicies ] = helper .True
251
288
}
252
- if hostedCPPolicies {
289
+ if createInput . hostedCPPolicies {
253
290
tagsList [tags .HypershiftPolicies ] = helper .True
254
291
}
255
292
256
- roleARN , err := r .AWSClient .EnsureRole (r .Reporter , roleName , policy , permissionsBoundary , accountRoleVersion ,
257
- tagsList , path , managedPolicies )
293
+ roleARN , err := r .AWSClient .EnsureRole (r .Reporter , roleName , policy , createInput . permissionsBoundary ,
294
+ createInput . accountRoleVersion , tagsList , path , createInput . managedPolicies )
258
295
if err != nil {
259
296
return err
260
297
}
261
298
if ! output .HasFlag () || r .Reporter .IsTerminal () {
262
299
r .Reporter .Infof ("Created role '%s' with ARN '%s'" , roleName , roleARN )
263
300
}
264
301
265
- r .Reporter .Debugf ("Attaching permission policy '%s' to role '%s'" , policyArn , roleName )
266
- err = r .AWSClient .AttachRolePolicy (r .Reporter , roleName , policyArn )
267
- if err != nil {
268
- return err
302
+ for _ , arn := range policyArns {
303
+ r .Reporter .Debugf ("Attaching permission policy '%s' to role '%s'" , arn , roleName )
304
+ err = r .AWSClient .AttachRolePolicy (r .Reporter , roleName , arn )
305
+ if err != nil {
306
+ return err
307
+ }
269
308
}
270
309
}
271
310
0 commit comments