-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauthenticate.py
97 lines (70 loc) · 3.06 KB
/
authenticate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import credentials
import boto3
# read AWS credentials
def read_AWS_credentials():
try:
return credentials.aws_region, credentials.aws_access_key, credentials.aws_secret_key
except Exception as e:
print("Error reading AWS credentials: " + str(e))
exit(1)
# read second AWS credentials
def read_AWS_credentials_alt():
try:
return credentials.aws_region_2, credentials.aws_access_key_2, credentials.aws_secret_key_2
except Exception as e:
print("Error reading alternate AWS credentials: " + str(e))
exit(1)
# get regions
def get_region():
aws_region, aws_access_key, aws_secret_key = read_AWS_credentials()
return aws_region
def get_region_alt():
aws_region, aws_access_key, aws_secret_key = read_AWS_credentials_alt()
return aws_region
# connect to Route53 service
def connect_route53():
aws_region, aws_access_key, aws_secret_key = read_AWS_credentials()
return connect_to_service("route53", aws_region, aws_access_key, aws_secret_key)
# connect to second Route53 service
def connect_route53_alt():
aws_region, aws_access_key, aws_secret_key = read_AWS_credentials_alt()
return connect_to_service("route53", aws_region, aws_access_key, aws_secret_key)
# connect to S3 service
def connect_s3():
aws_region, aws_access_key, aws_secret_key = read_AWS_credentials()
return connect_to_service("s3", aws_region, aws_access_key, aws_secret_key)
# connect to second S3 service
def connect_s3_alt():
aws_region, aws_access_key, aws_secret_key = read_AWS_credentials_alt()
return connect_to_service("s3", aws_region, aws_access_key, aws_secret_key)
# connect to EC2 service
def connect_ec2():
aws_region, aws_access_key, aws_secret_key = read_AWS_credentials()
return connect_to_service("ec2", aws_region, aws_access_key, aws_secret_key)
# connect to second EC2 service
def connect_ec2_alt():
aws_region, aws_access_key, aws_secret_key = read_AWS_credentials_alt()
return connect_to_service("ec2", aws_region, aws_access_key, aws_secret_key)
# connect to IAM service
def connect_iam():
aws_region, aws_access_key, aws_secret_key = read_AWS_credentials()
return connect_to_service("iam", aws_region, aws_access_key, aws_secret_key)
# connect to second IAM service
def connect_iam_alt():
aws_region, aws_access_key, aws_secret_key = read_AWS_credentials_alt()
return connect_to_service("iam", aws_region, aws_access_key, aws_secret_key)
# connect to arbitrary service
def connect_to_service(service_name, region, access_key, secret_key):
try:
# authenticate
client = boto3.client(service_name, region_name=region, aws_access_key_id=access_key, aws_secret_access_key=secret_key)
return client
except Exception as e:
print("Error connecting to AWS: " + str(e))
exit(1)
# compare regions
def compare_regions():
if read_AWS_credentials()[0] == read_AWS_credentials_alt()[0]:
return True
else:
return False