aws-samples / bulk-policy-migrator-scripts-for-account-cost-billing-consoles

Scripts to bulk migrate IAM policies to use fine-grained IAM actions for account, cost and billing consoles
MIT No Attribution
13 stars 4 forks source link

Bulk Policy Migrator Scripts for Account, Cost and Billing Consoles

These scripts can be used to help migrate your IAM policies to use fine-grained actions (new actions).

These scripts should be run from the payer account of your organization to identify the following affected policies in your organization that use the old IAM actions:

The scripts generate suggestions for new actions that correspond to the old actions used in the policy. You then review the suggestions and use the scripts to add the new actions across all affected polices in your organization.

These scripts provide the following benefits:

Prerequisites

  1. Download and install Python 3.

  2. Ensure that you have an IAM principal in your payer account that has the following IAM permissions:

    "iam:GetAccountAuthorizationDetails",
    "iam:GetPolicy",
    "iam:GetPolicyVersion",
    "iam:GetUserPolicy",
    "iam:GetGroupPolicy",
    "iam:GetRolePolicy",
    "iam:CreatePolicyVersion",
    "iam:DeletePolicyVersion",
    "iam:ListPolicyVersions",
    "iam:PutUserPolicy",
    "iam:PutGroupPolicy",
    "iam:PutRolePolicy",
    "iam:SetDefaultPolicyVersion",
    "organizations:ListAccounts",
    "organizations:ListPolicies",
    "organizations:DescribePolicy",
    "organizations:UpdatePolicy",
    "organizations:DescribeOrganization",
    "sts:AssumeRole",
    "sso:ListInstances",
    "sso:ListPermissionSets",
    "sso:GetInlinePolicyForPermissionSet",
    "sso:DescribePermissionSet",
    "sso:PutInlinePolicyToPermissionSet",
    "sso:ProvisionPermissionSet",
    "sso:DescribePermissionSetProvisioningStatus",
    "iam:GetRole",
    "iam:ListAttachedRolePolicies"

    These are the permissions needed to execute the script. You will be using this IAM principal to configure aws credentials before running the scripts.

Step 1: Set up the environment

  1. Clone the project to your local directory

    git clone https://github.com/aws-samples/bulk-policy-migrator-scripts-for-account-cost-billing-consoles.git
  2. Navigate into the project

    cd bulk-policy-migrator-scripts-for-account-cost-billing-consoles
  3. [ONLY applicable to Debian/Ubuntu systems] Install python3-venv

    This command should only be run if you are using Debian/Ubuntu systems. For all other systems, skip this and move to 4.

    sudo apt install -y python3-venv
  4. Setup virtualenv

    python3 -m venv venv
  5. Activate virtualenv

    source venv/bin/activate
  6. Install dependencies

    pip install -r requirements.txt
  7. Navigate to directory containing the scripts

    cd policy_migration_scripts/scripts
  8. Configure the credentials using AWS CLI. You can read more about how to do this here. Credentials can be configured in multiple ways. Regardless of the method that you choose, you must have both AWS credentials and an AWS Region set before running the scripts. The simplest way is to do this in an interactive manner using AWS CLI and running aws configure command to set up your credentials and default region. Follow the prompts, and it will generate configuration files in the correct locations for you.

Note: Specifying incorrect region can cause errors during script execution. For e.g. when running the script in China regions, if the region is set to us-east-1 you will see errors like - The security token included in the request is invalid. For China regions, the region value should be either cn-north-1 or cn-northwest-1.

Step 2: Create the CloudFormation stack set

Follow this procedure to create a CloudFormation stack set. The stack set creates an IAM role named BillingConsolePolicyMigratorRole across all member accounts of your organization. This IAM role will be assumed by the payer account during the script execution to access affected policies in the member accounts.

Note: You only need to complete this step once from the management account (payer account).

To create the CloudFormation stack set

  1. In a text editor, open the billing_console_policy_migrator_role.json template file and replace each instance of with the account ID of the payer account (for example, 123456789012).
  2. Save the file.
  3. Sign in to the AWS Management Console as the payer account.
  4. In the CloudFormation console, create a stack set with the template file that you updated.

For more information, see Creating a stack set on the AWS CloudFormation console in the AWS CloudFormation User Guide.

After CloudFormation creates the stack set, each member account in your organization has BillingConsolePolicyMigratorRole IAM role. The IAM role contains the following permissions:

"iam:GetAccountAuthorizationDetails",
"iam:GetPolicy",
"iam:GetPolicyVersion",
"iam:GetUserPolicy",
"iam:GetGroupPolicy",
"iam:GetRolePolicy",
"iam:CreatePolicyVersion",
"iam:DeletePolicyVersion",
"iam:ListPolicyVersions"
"iam:PutUserPolicy",
"iam:PutGroupPolicy",
"iam:PutRolePolicy",
"iam:SetDefaultPolicyVersion"

Step 3: Identify the affected policies

To identify affected policies run the identify_affected_policies.py script

Following input parameters are supported:

python identify_affected_policies.py --all
python identify_affected_policies.py --accounts 111122223333,444455556666,777788889999
python identify_affected_policies.py --accounts-file /path/to/accounts_file.csv
python identify_affected_policies.py --all --exclude-accounts 111111111111,222222222222,333333333333
python identify_affected_policies.py –-action-mapping-config-file /path/to/action_mapping_config.json –-all
python identify_affected_policies.py

After you run the script, it creates two JSON files in an AffectedPolicies<Timestamp> folder:

Step 4: Review the suggested changes

  1. In a text editor, open the affected_policies_and_suggestions.json file.
  2. In the AccountsScanned section, verify that the number of similar groups identified across the scanned accounts is expected.
  3. Review the suggested fine-grained actions that will be added to the affected policies.
  4. Update your file as needed and then save it.

Step 5: Update the affected policies

After you review and refine the suggested replacements, run the update_affected_policies.py script. The script takes as argument the absolute path of the directory created by running identify_affected_policies.py script

python update_affected_policies.py --affected-policies-directory /path/to/Affected_Policies_<Timestamp>

Step 6: Revert your changes (Optional)

The rollback_affected_polices.py script reverts the changes applied to each affected policy for the specified accounts. The script removes all Sid blocks that the update_affected_policies.py script appended. These Sid blocks have the BillingConsolePolicyMigrator# format.

Following input parameters are supported:

python rollback_affected_policies.py --all
python rollback_affected_policies.py --accounts 111122223333,444455556666,777788889999
python identify_affected_policies.py --accounts-file /path/to/accounts_file.csv
python rollback_affected_policies.py --all --exclude-accounts 111111111111,222222222222,333333333333
python rollback_affected_policies.py

Security

See CONTRIBUTING for more information.

License

This library is licensed under the MIT-0 License. See the LICENSE file.

Troubleshooting

See TROUBLESHOOTING for more information.