Created
February 23, 2019 22:33
-
-
Save whalesalad/34f921c3dd3d74f0a176038a25cdb144 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import sys | |
import json | |
import copy | |
import itertools | |
base_policy = json.loads(""" | |
{ | |
"Version": "2012-10-17", | |
"Statement": [ | |
{ | |
"Sid": "test", | |
"Effect": "Allow", | |
"Action": [ | |
"s3:*" | |
], | |
"Resource": [] | |
} | |
] | |
} | |
""") | |
fresh_policy = lambda: copy.copy(base_policy) | |
def resources_for_bucket(bucket): | |
resources = ["arn:aws:s3:::%(bucket)s", "arn:aws:s3:::%(bucket)s/*"] | |
return [ r % { 'bucket': bucket } for r in resources ] | |
def generate_iam_policy(buckets): | |
resources = list(itertools.chain(*[ resources_for_bucket(b) for b in buckets ])) | |
policy = fresh_policy() | |
policy['Statement'][0]['Resource'] = resources | |
return policy | |
if __name__ == '__main__': | |
buckets = sys.argv[1:] | |
if not buckets: | |
print "Please specify buckets for this policy as arguments." | |
print "Ex: %s abc.farmlogs.com www.farmlogs.com" % __file__ | |
sys.exit() | |
policy = generate_iam_policy(buckets) | |
print json.dumps(policy, indent=2) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment