def test_decrypt(self): with TemporaryDirectory() as tmp_dir: with open(f"{tmp_dir}/mysecret.txt", "w") as secret_file: secret_file.write("TEST-SECRET") self.execute_cmd([ "gcloud", "kms", "encrypt", "--location", "global", "--keyring", GCP_KMS_KEYRING_NAME, "--key", GCP_KMS_KEY_NAME, "--plaintext-file", f"{tmp_dir}/mysecret.txt", "--ciphertext-file", f"{tmp_dir}/mysecret.txt.encrypted", ]) with open(f"{tmp_dir}/mysecret.txt.encrypted", "rb") as encrypted_file: encrypted_secret = base64.b64encode( encrypted_file.read()).decode() kms_hook = CloudKMSHook() content = kms_hook.decrypt( key_name=( f"projects/{kms_hook.project_id}/locations/global/keyRings/" f"{GCP_KMS_KEYRING_NAME}/cryptoKeys/{GCP_KMS_KEY_NAME}"), ciphertext=encrypted_secret, ) assert content == b"TEST-SECRET"
def setUp(self): with mock.patch( "airflow.providers.google.cloud.hooks.base.CloudBaseHook.__init__", new=mock_init, ): self.kms_hook = CloudKMSHook(gcp_conn_id="test")
class TestCloudKMSHook(unittest.TestCase): def setUp(self): with mock.patch( "airflow.providers.google.cloud.hooks.base.CloudBaseHook.__init__", new=mock_init, ): self.kms_hook = CloudKMSHook(gcp_conn_id="test") @mock.patch( "airflow.providers.google.cloud.hooks.kms.CloudKMSHook.client_info", new_callable=mock.PropertyMock, ) @mock.patch( "airflow.providers.google.cloud.hooks.kms.CloudKMSHook._get_credentials" ) @mock.patch( "airflow.providers.google.cloud.hooks.kms.KeyManagementServiceClient") def test_kms_client_creation(self, mock_client, mock_get_creds, mock_client_info): result = self.kms_hook.get_conn() mock_client.assert_called_once_with( credentials=mock_get_creds.return_value, client_info=mock_client_info.return_value, ) self.assertEqual(mock_client.return_value, result) self.assertEqual(self.kms_hook._conn, result) @mock.patch( "airflow.providers.google.cloud.hooks.kms.CloudKMSHook.get_conn") def test_encrypt(self, mock_get_conn): mock_get_conn.return_value.encrypt.return_value = RESPONSE result = self.kms_hook.encrypt(TEST_KEY_ID, PLAINTEXT) mock_get_conn.assert_called_once_with() mock_get_conn.return_value.encrypt.assert_called_once_with( name=TEST_KEY_ID, plaintext=PLAINTEXT, additional_authenticated_data=None, retry=None, timeout=None, metadata=None, ) self.assertEqual(PLAINTEXT_b64, result) @mock.patch( "airflow.providers.google.cloud.hooks.kms.CloudKMSHook.get_conn") def test_encrypt_with_auth_data(self, mock_get_conn): mock_get_conn.return_value.encrypt.return_value = RESPONSE result = self.kms_hook.encrypt(TEST_KEY_ID, PLAINTEXT, AUTH_DATA) mock_get_conn.assert_called_once_with() mock_get_conn.return_value.encrypt.assert_called_once_with( name=TEST_KEY_ID, plaintext=PLAINTEXT, additional_authenticated_data=AUTH_DATA, retry=None, timeout=None, metadata=None, ) self.assertEqual(PLAINTEXT_b64, result) @mock.patch( "airflow.providers.google.cloud.hooks.kms.CloudKMSHook.get_conn") def test_decrypt(self, mock_get_conn): mock_get_conn.return_value.decrypt.return_value = RESPONSE result = self.kms_hook.decrypt(TEST_KEY_ID, CIPHERTEXT_b64) mock_get_conn.assert_called_once_with() mock_get_conn.return_value.decrypt.assert_called_once_with( name=TEST_KEY_ID, ciphertext=CIPHERTEXT, additional_authenticated_data=None, retry=None, timeout=None, metadata=None, ) self.assertEqual(PLAINTEXT, result) @mock.patch( "airflow.providers.google.cloud.hooks.kms.CloudKMSHook.get_conn") def test_decrypt_with_auth_data(self, mock_get_conn): mock_get_conn.return_value.decrypt.return_value = RESPONSE result = self.kms_hook.decrypt(TEST_KEY_ID, CIPHERTEXT_b64, AUTH_DATA) mock_get_conn.assert_called_once_with() mock_get_conn.return_value.decrypt.assert_called_once_with( name=TEST_KEY_ID, ciphertext=CIPHERTEXT, additional_authenticated_data=AUTH_DATA, retry=None, timeout=None, metadata=None, ) self.assertEqual(PLAINTEXT, result)