Skip to content

Commit

Permalink
don't delete managed policy if in use
Browse files Browse the repository at this point in the history
  • Loading branch information
dgraeber committed Oct 27, 2023
1 parent d815ff4 commit fdd6011
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This project adheres to [Semantic Versioning](http://semver.org/) and [Keep a Ch
- adding support for module-type spec on init of new module `seedfarmer init module -mt cdkv2`

### Fixes
- skip destroy of managed-project-policy if it has roles attached


## v2.10.4 (2023-10-23)
Expand Down
16 changes: 15 additions & 1 deletion seedfarmer/commands/_stack_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,17 @@ def destroy_managed_policy_stack(account_id: str, region: str) -> None:
"""
# Determine if managed policy stack already deployed
session = SessionManager().get_or_create().get_deployment_session(account_id=account_id, region_name=region)
project_managed_policy_stack_exists, _ = services.cfn.does_stack_exist(
project_managed_policy_stack_exists, stack_outputs = services.cfn.does_stack_exist(
stack_name=info.PROJECT_MANAGED_POLICY_CFN_NAME, session=session
)
_logger.debug("project_managed_policy_output is : %s", stack_outputs)
has_roles_attached = False
if project_managed_policy_stack_exists:
project_managed_policy_arn = stack_outputs.get("ProjectPolicyARN")
policy = iam.get_policy_info(policy_arn=project_managed_policy_arn, session=session)
has_roles_attached = True if policy and policy["Policy"]["AttachmentCount"] > 0 else False

if project_managed_policy_stack_exists and not has_roles_attached:
_logger.info(
"Destroying Stack %s in Account/Region: %s/%s", info.PROJECT_MANAGED_POLICY_CFN_NAME, account_id, region
)
Expand All @@ -117,6 +124,13 @@ def destroy_managed_policy_stack(account_id: str, region: str) -> None:
_logger.info(
f"Failed to delete project stack {info.PROJECT_MANAGED_POLICY_CFN_NAME}, ignoring and moving on"
)
else:
_logger.info(
"Stack %s in Account/Region: %s/%s is either not deployed or has roles attached",
info.PROJECT_MANAGED_POLICY_CFN_NAME,
account_id,
region,
)


def destroy_module_stack(
Expand Down
11 changes: 11 additions & 0 deletions seedfarmer/services/_iam.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,14 @@ def detach_inline_policy_from_role(role_name: str, policy_name: str, session: Op
iam_resource.RolePolicy(role_name, policy_name).delete()
except Exception as e:
raise e


def get_policy_info(policy_arn: str, session: Optional[Session] = None) -> Dict[str, Any]:
iam_client = boto3_client("iam", session=session)
try:
return iam_client.get_policy(PolicyArn=policy_arn)
except iam_client.exceptions.NoSuchEntityException as ne:
_logger.info("Policy does not exist: %s ", policy_arn)
return {}
except Exception as e:
raise e

0 comments on commit fdd6011

Please sign in to comment.