def test_get_existing_key_v1(self, mock_hvac, mock_get_connection): mock_connection = self.get_mock_connection() mock_get_connection.return_value = mock_connection mock_client = mock.MagicMock() mock_hvac.Client.return_value = mock_client connection_dict = {} mock_client.secrets.kv.v1.read_secret.return_value = { 'request_id': '182d0673-618c-9889-4cba-4e1f4cfe4b4b', 'lease_id': '', 'renewable': False, 'lease_duration': 2764800, 'data': { 'value': 'world' }, 'wrap_info': None, 'warnings': None, 'auth': None } mock_connection.extra_dejson.get.side_effect = connection_dict.get kwargs = { "vault_conn_id": "vault_conn_id", "auth_type": "token", "kv_engine_version": 1 } test_hook = VaultHook(**kwargs) secret = test_hook.get_secret(secret_path="missing") self.assertEqual({'value': 'world'}, secret) mock_client.secrets.kv.v1.read_secret.assert_called_once_with( mount_point='secret', path='missing')
def test_get_existing_key_v2_version(self, mock_hvac, mock_get_connection): mock_connection = self.get_mock_connection() mock_get_connection.return_value = mock_connection mock_client = mock.MagicMock() mock_hvac.Client.return_value = mock_client connection_dict = {} mock_client.secrets.kv.v2.read_secret_version.return_value = { 'request_id': '94011e25-f8dc-ec29-221b-1f9c1d9ad2ae', 'lease_id': '', 'renewable': False, 'lease_duration': 0, 'data': { 'data': { 'secret_key': 'secret_value' }, 'metadata': { 'created_time': '2020-03-16T21:01:43.331126Z', 'deletion_time': '', 'destroyed': False, 'version': 1 } }, 'wrap_info': None, 'warnings': None, 'auth': None } mock_connection.extra_dejson.get.side_effect = connection_dict.get kwargs = { "vault_conn_id": "vault_conn_id", "auth_type": "token", "kv_engine_version": 2 } test_hook = VaultHook(**kwargs) secret = test_hook.get_secret(secret_path="missing", secret_version=1) self.assertEqual({'secret_key': 'secret_value'}, secret) mock_client.secrets.kv.v2.read_secret_version.assert_called_once_with( mount_point='secret', path='missing', version=1)