Skip to content

Instantly share code, notes, and snippets.

@kincl
Last active October 3, 2023 12:36
Show Gist options
  • Save kincl/c9fbbaa9aba5be3657f3e9926b5f0b85 to your computer and use it in GitHub Desktop.
Save kincl/c9fbbaa9aba5be3657f3e9926b5f0b85 to your computer and use it in GitHub Desktop.
manual-cloud-credential-operator-iam-policy
#!/usr/bin/env python3
import sys
import yaml
import json
import logging
from pprint import pprint
logger = logging.getLogger(__file__)
logger.setLevel(logging.DEBUG)
console_logging = logging.StreamHandler()
console_logging.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
logger.addHandler(console_logging)
files = sys.argv[1:]
documents = []
for file in files:
with open(file, 'r') as f:
d = yaml.safe_load(f)
try:
annotations = d.get('metadata').get('annotations')
if 'release.openshift.io/feature-set' in annotations.keys():
if annotations['release.openshift.io/feature-set'] == 'TechPreviewNoUpgrade':
logger.warning(f'not including {file} due to presence of TechPreviewNoUpgrade')
except AttributeError as e:
logger.error(f'unable to parse {file} trying to get .metadata.annotations')
continue
documents.append(d)
policy_document = {
"Version": "2012-10-17",
"Statement": []
}
for document in documents:
statements = []
name = document['metadata']['name']
for i, entry in enumerate(document['spec']['providerSpec']['statementEntries']):
statement = {
"Sid": f"{name.replace('-','')}{i}",
"Effect": entry['effect'],
"Resource": entry['resource'],
"Action": entry['action']
}
if entry.get('policyCondition', None) is not None:
statement['Condition'] = entry['policyCondition']
statements.append(statement)
policy_document['Statement'] = policy_document['Statement'] + statements
print(json.dumps(policy_document, indent=2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment