forked from claesjac/nginx-aws-auth-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_temp_token.py
41 lines (33 loc) · 1.22 KB
/
generate_temp_token.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
import boto3
import sys
import os
if __name__ == '__main__':
if len(sys.argv) != 6:
print 'Usage:\n\t%s <role_arn> <role_session_name> <service> <region> <session_duration>' % os.path.basename(__file__)
sys.exit(1)
_, role_arn, role_session_name, service, region, session_duration = sys.argv
session_duration = int(session_duration)
sts = boto3.client('sts')
assumed_role = sts.assume_role(
RoleArn=role_arn,
RoleSessionName=role_session_name,
DurationSeconds=session_duration
)
credentials = assumed_role['Credentials']
print '''
aws_auth $aws_token {
access_key %s;
secret_key %s;
service %s;
region %s;
}
''' % (credentials['AccessKeyId'], credentials['SecretAccessKey'], service, region)
print '''
location /proxy/ {
proxy_pass http://mybucket.s3.%s.amazonaws.com/;
proxy_set_header X-Amz-Date $aws_auth_date;
proxy_set_header X-Amz-Content-SHA256 e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855;
proxy_set_header X-Amz-Security-Token %s;
proxy_set_header Authorization $aws_token;
}
''' % (region, credentials['SessionToken'])