Skip to content

Commit 9ba9589

Browse files
committed
OCM-12872 | feat: Manual mode for deleting hcpsharedvpc account role policies
1 parent 72cf14f commit 9ba9589

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

cmd/dlt/accountroles/cmd.go

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@ import (
2222
"os"
2323
"strings"
2424

25+
awssdk "github.com/aws/aws-sdk-go-v2/aws"
26+
"github.com/aws/aws-sdk-go-v2/service/iam"
2527
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
2628
"github.com/spf13/cobra"
2729

2830
"github.com/openshift/rosa/pkg/aws"
2931
awscb "github.com/openshift/rosa/pkg/aws/commandbuilder"
32+
"github.com/openshift/rosa/pkg/aws/tags"
3033
"github.com/openshift/rosa/pkg/interactive"
3134
"github.com/openshift/rosa/pkg/interactive/confirm"
3235
"github.com/openshift/rosa/pkg/ocm"
@@ -207,7 +210,8 @@ func deleteAccountRoles(r *rosa.Runtime, env string, prefix string, clusters []*
207210
r.OCMClient.LogEvent("ROSADeleteAccountRoleModeAuto", nil)
208211
deleteHcpSharedVpcPolicies := false
209212
if roles.CheckIfRolesAreHcpSharedVpc(r, finalRoleList) {
210-
deleteHcpSharedVpcPolicies = confirm.Prompt(true, "Attempt to delete Hosted CP shared VPC policies?")
213+
deleteHcpSharedVpcPolicies = confirm.Prompt(true, "Create commands to delete Hosted CP shared VPC"+
214+
" policies?")
211215
}
212216
for _, role := range finalRoleList {
213217
if !confirm.Prompt(true, "Delete the account role '%s'?", role) {
@@ -227,7 +231,21 @@ func deleteAccountRoles(r *rosa.Runtime, env string, prefix string, clusters []*
227231
if err != nil {
228232
return fmt.Errorf("There was an error getting the policy: %v", err)
229233
}
230-
commands := buildCommand(finalRoleList, policyMap, arbitraryPolicyMap, managedPolicies)
234+
235+
// Get HCP shared vpc policy details if the user is deleting roles related to HCP shared vpc
236+
policiesOutput := make([]*iam.GetPolicyOutput, 0)
237+
if roles.CheckIfRolesAreHcpSharedVpc(r, finalRoleList) &&
238+
confirm.Prompt(true, "Attempt to delete Hosted CP shared VPC policies?") {
239+
for _, role := range finalRoleList {
240+
policiesOutput, err = r.AWSClient.GetPolicyDetailsFromRole(awssdk.String(role))
241+
if err != nil {
242+
r.Reporter.Infof("There was an error getting details of policies attached to role '%s': %v",
243+
role, err)
244+
}
245+
}
246+
}
247+
248+
commands := buildCommand(finalRoleList, policyMap, arbitraryPolicyMap, managedPolicies, policiesOutput)
231249

232250
if r.Reporter.IsTerminal() {
233251
r.Reporter.Infof("Run the following commands to delete the account roles and policies:\n")
@@ -298,7 +316,8 @@ func checkIfRoleAssociated(clusters []*cmv1.Cluster, role aws.Role) string {
298316
}
299317

300318
func buildCommand(roleNames []string, policyMap map[string][]aws.PolicyDetail,
301-
arbitraryPolicyMap map[string][]aws.PolicyDetail, managedPolicies bool) string {
319+
arbitraryPolicyMap map[string][]aws.PolicyDetail, managedPolicies bool,
320+
hcpSharedVpcPoliciesOutput []*iam.GetPolicyOutput) string {
302321
commands := []string{}
303322
for _, roleName := range roleNames {
304323
policyDetails := policyMap[roleName]
@@ -344,6 +363,27 @@ func buildCommand(roleNames []string, policyMap map[string][]aws.PolicyDetail,
344363
AddParam(awscb.RoleName, roleName).
345364
Build()
346365
commands = append(commands, deleteRole)
366+
367+
if len(hcpSharedVpcPoliciesOutput) > 0 { // Delete HCP shared VPC policies
368+
for _, hcpSharedVpcPolicy := range hcpSharedVpcPoliciesOutput {
369+
hasRhManagedTag := false
370+
hasHcpSharedVpcTag := false
371+
for _, tag := range hcpSharedVpcPolicy.Policy.Tags {
372+
if *tag.Key == tags.RedHatManaged {
373+
hasRhManagedTag = true
374+
} else if *tag.Key == tags.HcpSharedVpc {
375+
hasHcpSharedVpcTag = true
376+
}
377+
}
378+
if hasHcpSharedVpcTag && hasRhManagedTag {
379+
deletePolicy := awscb.NewIAMCommandBuilder().
380+
SetCommand(awscb.DeletePolicy).
381+
AddParam(awscb.PolicyName, *hcpSharedVpcPolicy.Policy.PolicyName).
382+
Build()
383+
commands = append(commands, deletePolicy)
384+
}
385+
}
386+
}
347387
}
348388
return awscb.JoinCommands(commands)
349389
}

pkg/aws/policies.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,7 @@ func (c *awsClient) GetPolicyDetailsFromRole(role *string) ([]*iam.GetPolicyOutp
10721072
if err != nil {
10731073
return nil, err
10741074
}
1075-
finalOutput := make([]*iam.GetPolicyOutput, 0)
1075+
var finalOutput []*iam.GetPolicyOutput
10761076
for _, attachedPolicy := range policies.AttachedPolicies {
10771077
policy, err := c.iamClient.GetPolicy(context.Background(), &iam.GetPolicyInput{
10781078
PolicyArn: attachedPolicy.PolicyArn,

0 commit comments

Comments
 (0)