Skip to content

Instantly share code, notes, and snippets.

@jonathanwcrane
Last active February 22, 2019 03:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jonathanwcrane/118593560c0d13b17531 to your computer and use it in GitHub Desktop.
Save jonathanwcrane/118593560c0d13b17531 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
#inventory a bucket
import boto3
import re
import os
import logging
import random
from random import choice
from string import ascii_uppercase
import argparse
parser = argparse.ArgumentParser(description='Try to upload an object to a bucket')
parser.add_argument("-b","--bucket",type=str,help="Name of bucket to upload to")
parser.add_argument("-l","--logging",action='store_true',help="Whether to enable detailed logging")
args=parser.parse_args()
bucket = args.bucket
if args.logging:
logging.basicConfig(level=logging.DEBUG)
buckets_of_interest = [bucket]
#This is needed to establish the session, I guess.
region='us-east-1'
#The profile to use in the credentials or boto.config file, if used
profile_nm = 'svc_SysEngScript'
#The env var to look in for the AWS KEY, if used
aws_key_env_var = 'AWS_KEY'
#The env var to look in for the secret key, if used
aws_secret_key_env_var = 'AWS_SECRET_KEY'
#First check for ENV VARs with AWS Credentials
#and make the connection
aws_key = os.environ.get(aws_key_env_var)
aws_secret_key = os.environ.get(aws_secret_key_env_var)
if (aws_key and aws_secret_key):
print("Signing in using ENV VAR credentials")
aws_session = boto3.session.Session(aws_access_key_id=aws_key,aws_secret_access_key=aws_secret_key,region_name=region)
#If env vars don't exist, use the profile in the boto.config file
#If the env vars and profile both exist, the program will never look for the profile, and use the env vars.
else:
print("Signing in using boto config credentials")
aws_session = boto3.session.Session(region_name=region, profile_name=profile_nm)
s3 = aws_session.resource('s3')
obj_nm = ''.join(choice(ascii_uppercase) for i in range(8))
obj_contents = 'Body and mind dropped off.'
repeats = random.randint(1,99)
obj_contents = obj_contents*repeats
for bn in buckets_of_interest:
bucket = s3.Bucket(bn)
bucket.put_object(Body=obj_contents,Key=obj_nm)
#for key in bucket.list(prefix='',delimiter='/'):
# nm = key.name
# print(nm)
# break
{
"Version" : "2008-10-17",
"Id" : "test-vpce1",
"Statement" : [{
"Sid" : "test-vpce-ips-allowed",
"Effect" : "Allow",
"Principal" : {
"AWS" : "*"
},
"Action" : "s3:*",
"Resource" : "arn:aws:s3:::test-vpce/*",
"Condition" : {
"IpAddress" : {
"aws:SourceIp" : [
"65.127.125.240/29",
"107.23.45.183/32",
"107.23.45.169/32",
"107.23.14.153/32",
"107.23.34.206/32",
"107.21.27.124/32",
"107.23.23.218/32",
"107.23.21.52/32",
"107.21.2.244/32",
"63.159.135.136/29",
"107.23.45.225/32",
"107.23.21.1/32",
"107.23.35.45/32"
]
}
}
}, {
"Sid" : "test-vpce-vpc-allowed",
"Effect" : "Allow",
"Principal" : {
"AWS" : "*"
},
"Action" : "s3:*",
"Resource" : "arn:aws:s3:::test-vpce/*",
"Condition" : {
"StringEquals" : {
"aws:SourceVpc" : "vpc-17baf173"
}
}
}
]
}
#!/usr/bin/env python
#inventory a bucket
import boto.s3
from boto.s3.connection import OrdinaryCallingFormat
import re
import os
import logging
logging.basicConfig(level=logging.DEBUG)
buckets_of_interest = ['test-vpce']
#The profile to use in the credentials or boto.config file, if used
profile_nm = 'svc_SysEngScript'
#The env var to look in for the AWS KEY, if used
aws_key_env_var = 'AWS_KEY'
#The env var to look in for the secret key, if used
aws_secret_key_env_var = 'AWS_SECRET_KEY'
#First check for ENV VARs with AWS Credentials
#and make the connection
aws_key = os.environ.get(aws_key_env_var)
aws_secret_key = os.environ.get(aws_secret_key_env_var)
if (aws_key and aws_secret_key):
print("Signing in using ENV VAR credentials")
s3_conn = boto.connect_s3(aws_access_key_id=aws_key,aws_secret_access_key=aws_secret_key,calling_format=OrdinaryCallingFormat())
#If env vars don't exist, use the profile in the boto.config file
#If the env vars and profile both exist, the program will never look for the profile, and use the env vars.
else:
print("Signing in using boto config credentials")
s3_conn = boto.connect_s3(profile_name=profile_nm,calling_format=OrdinaryCallingFormat())
for bn in buckets_of_interest:
bucket = s3_conn.get_bucket(bn)
for key in bucket.list(prefix='',delimiter='/'):
nm = key.name
print(nm)
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment