예제 #1
0
 def test_with_access_steps_final_with_account_id(self):
     access_steps = [
         AccessStep(role_name="test_role_name1", account_id="1234"),
         AccessStep(role_name="test_role_name2", account_id="5678"),
     ]
     with self.assertRaises(ValueError):
         MultiHopAccessor(role_session_name="test_role_session_name", access_steps=access_steps)
예제 #2
0
 def test_str(self):
     access_steps = [
         AccessStep(role_name="test_role_name1", account_id="1234", external_id="abcd"),
         AccessStep(role_name="test_role_name2", account_id="5678"),
         AccessStep(role_name="test_role_name3"),
     ]
     mha = MultiHopAccessor(
         role_session_name="test_role_session_name", access_steps=access_steps
     )
     expected_str = "accessor:test_role_session_name:test_role_name1@1234,test_role_name2@5678,test_role_name3@target"
     self.assertEqual(str(mha), expected_str)
예제 #3
0
 def test_get_session(self):
     access_steps = [
         AccessStep(role_name="test_role_name1", account_id="1234", external_id="abcd"),
         AccessStep(role_name="test_role_name2", account_id="5678"),
         AccessStep(role_name="test_role_name3"),
     ]
     mha = MultiHopAccessor(
         role_session_name="test_role_session_name", access_steps=access_steps
     )
     session = mha.get_session("4566")
     self.assertIsInstance(session, boto3.Session)
     expected_cache_sorted_keys = [
         "1234:test_role_name1:test_role_session_name:None",
         "4566:test_role_name3:test_role_session_name:None",
         "5678:test_role_name2:test_role_session_name:None",
     ]
     self.assertEqual(sorted(mha.session_cache._cache.keys()), expected_cache_sorted_keys)
예제 #4
0
 def test_to_dict(self):
     access_steps = [
         AccessStep(role_name="test_role_name1", account_id="1234", external_id="abcd"),
         AccessStep(role_name="test_role_name2", account_id="5678"),
         AccessStep(role_name="test_role_name3"),
     ]
     mha = MultiHopAccessor(
         role_session_name="test_role_session_name", access_steps=access_steps
     )
     expected_dict = {
         "role_session_name": "test_role_session_name",
         "access_steps": [
             {"role_name": "test_role_name1", "external_id": "abcd", "account_id": "1234"},
             {"role_name": "test_role_name2", "external_id": None, "account_id": "5678"},
             {"role_name": "test_role_name3", "external_id": None, "account_id": None},
         ],
     }
     self.assertEqual(mha.to_dict(), expected_dict)
예제 #5
0
 def test_cache_usage(self):
     access_steps = [
         AccessStep(role_name="test_role_name1", account_id="1234", external_id="abcd"),
         AccessStep(role_name="test_role_name2", account_id="5678"),
         AccessStep(role_name="test_role_name3"),
     ]
     mha = MultiHopAccessor(
         role_session_name="test_role_session_name", access_steps=access_steps
     )
     mha.get_session("4567")
     mha.get_session("4567")
     mha.get_session("8901")
     mha.get_session("8901")
     expected_cache_sorted_keys = [
         "1234:test_role_name1:test_role_session_name:None",
         "4567:test_role_name3:test_role_session_name:None",
         "5678:test_role_name2:test_role_session_name:None",
         "8901:test_role_name3:test_role_session_name:None",
     ]
     self.assertEqual(sorted(mha.session_cache._cache.keys()), expected_cache_sorted_keys)
예제 #6
0
 def test_from_dict_with_external_id_env_var(self):
     d = {"role_name": "test_role_name", "account_id": "1234", "external_id_env_var": "EXT_ID"}
     os.environ["EXT_ID"] = "abcd"
     try:
         access_step = AccessStep.from_dict(d)
         expected_dict = {
             "role_name": "test_role_name",
             "account_id": "1234",
             "external_id": "abcd",
         }
         self.assertEqual(access_step.to_dict(), expected_dict)
     finally:
         del os.environ["EXT_ID"]
예제 #7
0
 def test_to_dict(self):
     access_step = AccessStep(role_name="test_role_name", account_id="1234", external_id="abcd")
     expected_dict = {"role_name": "test_role_name", "account_id": "1234", "external_id": "abcd"}
     self.assertEqual(expected_dict, access_step.to_dict())
예제 #8
0
 def test_str_without_account(self):
     access_step = AccessStep(role_name="test_role_name")
     self.assertEqual(str(access_step), "test_role_name@target")
예제 #9
0
 def test_str_with_account(self):
     access_step = AccessStep(role_name="test_role_name", account_id="1234")
     self.assertEqual(str(access_step), "test_role_name@1234")
예제 #10
0
 def test_from_dict_with_external_id_env_var_missing_var(self):
     d = {"role_name": "test_role_name", "account_id": "1234", "external_id_env_var": "EXT_ID"}
     with self.assertRaises(ValueError):
         AccessStep.from_dict(d)
예제 #11
0
 def test_from_dict_without_role_name(self):
     d = {"account_id": "1234", "external_id": "abcd"}
     with self.assertRaises(ValueError):
         AccessStep.from_dict(d)
예제 #12
0
 def test_from_dict(self):
     d = {"role_name": "test_role_name", "account_id": "1234", "external_id": "abcd"}
     access_step = AccessStep.from_dict(d)
     self.assertEqual(access_step.to_dict(), d)