Python Forum

Full Version: Cant find S3 Read or Write access from Python code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi
First off I am communicating with AWS with python boto3(Pycharm),So my question is i have created
8 buckets in which 4 buckets Read access and other 4 are Write access problem while executing i can all the buckets which Read and Write my task is to show only Read access and write access in other programme kindly help me out to sort this problem.

------->>>Here is the code what i wrote------------------>:
s3_bucket_public_read_prohibited(self):
    result = True
    failReason = ""
    control = "4.2"
    description = "No Public read access for S3 Buckets"
    scored = False
    offenders = []
    s3_client = boto3.client('s3')
    buckets = s3_client.list_buckets()
    public_access = False

    for bucket in buckets['Buckets']:
        print(bucket)
        acl_bucket = s3_client.get_bucket_acl(Bucket=bucket['Name'])
        print(yaml.dump(acl_bucket))
        for grantee in acl_bucket['Grants']:
            if len(grantee['Grantee']) > 0:
                print(grantee['Grantee'])
                for uri in (grantee['Grantee'].keys()):
                    if uri == 'URI':
                        if grantee['Grantee']['URI'] == 'http://acs.amazonaws.com/groups/global/AllUsers':
                            public_access = True
                            print(public_access)
        if public_access == True:
            offenders.append(bucket['Name'])
        public_access = False
    if len(offenders) > 0:
        result = False
        failReason = "These S3 Buckets available with Public Read Access"
    return {'Result': result, 'failReason': failReason, 'Offenders': offenders, 'ScoredControl': scored,'Description': description, 'ControlId': control}