IAM - reduce filesize default policies (#5263)

This commit is contained in:
Bert Blommers 2022-06-26 12:01:25 +00:00 committed by GitHub
parent 886959b1a2
commit a92dcd149d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 62366 additions and 65214 deletions

File diff suppressed because it is too large Load Diff

View File

@ -32,7 +32,12 @@ try:
response_iterator = paginator.paginate(Scope="AWS") response_iterator = paginator.paginate(Scope="AWS")
for response in response_iterator: for response in response_iterator:
for policy in response["Policies"]: for policy in response["Policies"]:
policies[policy["PolicyName"]] = policy policy.pop("AttachmentCount", None)
policy.pop("IsAttachable", None)
policy.pop("IsDefaultVersion", None)
policy.pop("PolicyId", None)
policy_name = policy.pop("PolicyName")
policies[policy_name] = policy
except NoCredentialsError: except NoCredentialsError:
print("USAGE:") print("USAGE:")
print("Put your AWS credentials into ~/.aws/credentials and run:") print("Put your AWS credentials into ~/.aws/credentials and run:")
@ -48,14 +53,14 @@ except NoCredentialsError:
sys.exit(1) sys.exit(1)
for policy_name in policies: for policy_name in policies:
# We don't need the ARN afterwards
policy_arn = policies[policy_name].pop("Arn")
response = client.get_policy_version( response = client.get_policy_version(
PolicyArn=policies[policy_name]["Arn"], PolicyArn=policy_arn,
VersionId=policies[policy_name]["DefaultVersionId"], VersionId=policies[policy_name]["DefaultVersionId"],
) )
for key in response["PolicyVersion"]: for key in response["PolicyVersion"]:
if ( if key in ["DefaultVersionId", "Path", "Document", "UpdateDate"]:
key != "CreateDate"
): # the policy's CreateDate should not be overwritten by its version's CreateDate
policies[policy_name][key] = response["PolicyVersion"][key] policies[policy_name][key] = response["PolicyVersion"][key]
with open(output_file, "w") as f: with open(output_file, "w") as f:
@ -67,8 +72,8 @@ with open(output_file, "w") as f:
json.dumps( json.dumps(
policies, policies,
sort_keys=True, sort_keys=True,
indent=4, indent=1,
separators=(",", ": "), separators=(",", ":"),
default=json_serial, default=json_serial,
) )
) )