5
5
6
6
import argparse
7
7
import json
8
- import logging
9
8
import os
10
9
import subprocess
11
10
import sys
12
11
from functools import partial
13
- from pathlib import Path
14
12
from urllib .parse import quote_plus
15
13
16
14
from pymongo import MongoClient
17
15
from pymongo .errors import OperationFailure
18
16
19
- HERE = Path (__file__ ).absolute ().parent
20
- LOGGER = logging .getLogger (__name__ )
21
- logging .basicConfig (level = logging .INFO , format = "%(levelname)-8s %(message)s" )
17
+ HERE = os .path .abspath (os .path .dirname (__file__ ))
22
18
23
19
24
20
def join (* parts ):
25
21
return os .path .join (* parts ).replace (os .sep , "/" )
26
22
27
23
28
- sys .path .insert (0 , str (HERE / "lib" ))
24
+ sys .path .insert (0 , join (HERE , "lib" ))
29
25
from aws_assign_instance_profile import _assign_instance_policy
30
26
from aws_assume_role import _assume_role
31
27
from aws_assume_web_role import _assume_role_with_web_identity
@@ -39,7 +35,7 @@ def join(*parts):
39
35
_USE_AWS_SECRETS = False
40
36
41
37
try :
42
- with ( HERE / "aws_e2e_setup.json" ). open ( ) as fid :
38
+ with open ( join ( HERE , "aws_e2e_setup.json" )) as fid :
43
39
CONFIG = json .load (fid )
44
40
get_key = partial (_get_key , uppercase = False )
45
41
except FileNotFoundError :
@@ -55,7 +51,7 @@ def run(args, env):
55
51
56
52
def create_user (user , kwargs ):
57
53
"""Create a user and verify access."""
58
- LOGGER . info ("Creating user %s " , user )
54
+ print ("Creating user" , user )
59
55
client = MongoClient (username = "bob" , password = "pwd123" )
60
56
db = client ["$external" ]
61
57
try :
@@ -80,7 +76,7 @@ def setup_assume_role():
80
76
81
77
role_name = CONFIG [get_key ("iam_auth_assume_role_name" )]
82
78
creds = _assume_role (role_name , quiet = True )
83
- with ( HERE / "creds.json" ). open ( "w" ) as fid :
79
+ with open ( join ( HERE , "creds.json" ), "w" ) as fid :
84
80
json .dump (creds , fid )
85
81
86
82
# Create the user.
@@ -91,11 +87,6 @@ def setup_assume_role():
91
87
authmechanismproperties = f"AWS_SESSION_TOKEN:{ token } " ,
92
88
)
93
89
create_user (ASSUMED_ROLE , kwargs )
94
- return dict (
95
- USER = kwargs ["username" ],
96
- PASS = kwargs ["password" ],
97
- SESSION_TOKEN = creds ["SessionToken" ],
98
- )
99
90
100
91
101
92
def setup_ec2 ():
@@ -104,7 +95,6 @@ def setup_ec2():
104
95
os .environ .pop ("AWS_ACCESS_KEY_ID" , None )
105
96
os .environ .pop ("AWS_SECRET_ACCESS_KEY" , None )
106
97
create_user (AWS_ACCOUNT_ARN , dict ())
107
- return dict ()
108
98
109
99
110
100
def setup_ecs ():
@@ -148,8 +138,6 @@ def setup_ecs():
148
138
# Run the test in a container
149
139
subprocess .check_call (["/bin/sh" , "-c" , run_test_command ], env = env )
150
140
151
- return dict ()
152
-
153
141
154
142
def setup_regular ():
155
143
# Create the user.
@@ -159,8 +147,6 @@ def setup_regular():
159
147
)
160
148
create_user (CONFIG [get_key ("iam_auth_ecs_account_arn" )], kwargs )
161
149
162
- return dict (USER = kwargs ["username" ], PASS = kwargs ["password" ])
163
-
164
150
165
151
def setup_web_identity ():
166
152
# Unassign the instance profile.
@@ -175,7 +161,7 @@ def setup_web_identity():
175
161
raise RuntimeError ("Request limit exceeded for AWS API" )
176
162
177
163
if ret != 0 :
178
- LOGGER . debug ( "return code was %s " , ret )
164
+ print ( "ret was" , ret )
179
165
raise RuntimeError (
180
166
"Failed to unassign an instance profile from the current machine"
181
167
)
@@ -200,11 +186,10 @@ def setup_web_identity():
200
186
201
187
# Assume the web role to get temp credentials.
202
188
os .environ ["AWS_WEB_IDENTITY_TOKEN_FILE" ] = token_file
203
- role_arn = CONFIG [get_key ("iam_auth_assume_web_role_name" )]
204
- os .environ ["AWS_ROLE_ARN" ] = role_arn
189
+ os .environ ["AWS_ROLE_ARN" ] = CONFIG [get_key ("iam_auth_assume_web_role_name" )]
205
190
206
191
creds = _assume_role_with_web_identity (True )
207
- with ( HERE / "creds.json" ). open ( "w" ) as fid :
192
+ with open ( join ( HERE , "creds.json" ), "w" ) as fid :
208
193
json .dump (creds , fid )
209
194
210
195
# Create the user.
@@ -216,39 +201,12 @@ def setup_web_identity():
216
201
)
217
202
create_user (ASSUMED_WEB_ROLE , kwargs )
218
203
219
- return dict (AWS_WEB_IDENTITY_TOKEN_FILE = token_file , AWS_ROLE_ARN = role_arn )
220
-
221
-
222
- def handle_creds (creds : dict ):
223
- if "USER" in creds :
224
- USER = quote_plus (creds ["USER" ])
225
- PASS = quote_plus (creds ["PASS" ])
226
- MONGODB_URI = f"mongodb://{ USER } :{ PASS } @localhost"
227
- else :
228
- MONGODB_URI = "mongodb://localhost"
229
- MONGODB_URI = f"{ MONGODB_URI } /aws?authMechanism=MONGODB-AWS"
230
- if "SESSION_TOKEN" in creds :
231
- SESSION_TOKEN = quote_plus (creds ["SESSION_TOKEN" ])
232
- MONGODB_URI = (
233
- f"{ MONGODB_URI } &authMechanismProperties=AWS_SESSION_TOKEN:{ SESSION_TOKEN } "
234
- )
235
- with (HERE / "test-env.sh" ).open ("w" , newline = "\n " ) as fid :
236
- fid .write ("#!/usr/bin/env bash\n \n " )
237
- fid .write ("set +x\n " )
238
- for key , value in creds .items ():
239
- if key in ["USER" , "PASS" , "SESSION_TOKEN" ]:
240
- value = quote_plus (value ) # noqa: PLW2901
241
- fid .write (f"export { key } ={ value } \n " )
242
- fid .write (f"export MONGODB_URI={ MONGODB_URI } \n " )
243
-
244
204
245
205
def main ():
246
206
parser = argparse .ArgumentParser (description = "MONGODB-AWS tester." )
247
207
sub = parser .add_subparsers (title = "Tester subcommands" , help = "sub-command help" )
248
208
249
- run_assume_role_cmd = sub .add_parser (
250
- "assume-role" , aliases = ["session-creds" ], help = "Assume role test"
251
- )
209
+ run_assume_role_cmd = sub .add_parser ("assume-role" , help = "Assume role test" )
252
210
run_assume_role_cmd .set_defaults (func = setup_assume_role )
253
211
254
212
run_ec2_cmd = sub .add_parser ("ec2" , help = "EC2 test" )
@@ -257,20 +215,14 @@ def main():
257
215
run_ecs_cmd = sub .add_parser ("ecs" , help = "ECS test" )
258
216
run_ecs_cmd .set_defaults (func = setup_ecs )
259
217
260
- run_regular_cmd = sub .add_parser (
261
- "regular" , aliases = ["env-creds" ], help = "Regular credentials test"
262
- )
218
+ run_regular_cmd = sub .add_parser ("regular" , help = "Regular credentials test" )
263
219
run_regular_cmd .set_defaults (func = setup_regular )
264
220
265
221
run_web_identity_cmd = sub .add_parser ("web-identity" , help = "Web identity test" )
266
222
run_web_identity_cmd .set_defaults (func = setup_web_identity )
267
223
268
224
args = parser .parse_args ()
269
- func_name = args .func .__name__ .replace ("setup_" , "" )
270
- LOGGER .info ("Running aws_tester.py with %s..." , func_name )
271
- creds = args .func ()
272
- handle_creds (creds )
273
- LOGGER .info ("Running aws_tester.py with %s... done." , func_name )
225
+ args .func ()
274
226
275
227
276
228
if __name__ == "__main__" :
0 commit comments