|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +import base64 |
| 19 | +import os |
| 20 | +from tempfile import TemporaryDirectory |
| 21 | + |
| 22 | +import pytest |
| 23 | + |
| 24 | +from airflow.providers.google.cloud.hooks.kms import CloudKMSHook |
| 25 | +from tests.providers.google.cloud.utils.gcp_authenticator import GCP_KMS_KEY |
| 26 | +from tests.test_utils.gcp_system_helpers import GoogleSystemTest, provide_gcp_context |
| 27 | + |
| 28 | +# To prevent resource name collisions, key ring and key resources CANNOT be deleted, so |
| 29 | +# to avoid cluttering the project, we only create the key once during project initialization. |
| 30 | +# See: https://cloud.google.com/kms/docs/faq#cannot_delete |
| 31 | +GCP_KMS_KEYRING_NAME = os.environ.get('GCP_KMS_KEYRING_NAME', 'test-airflow-system-tests-keyring') |
| 32 | +GCP_KMS_KEY_NAME = os.environ.get('GCP_KMS_KEY_NAME', 'test-airflow-system-tests-key') |
| 33 | + |
| 34 | + |
| 35 | +@pytest.mark.credential_file(GCP_KMS_KEY) |
| 36 | +class TestKmsHook(GoogleSystemTest): |
| 37 | + @provide_gcp_context(GCP_KMS_KEY) |
| 38 | + def test_encrypt(self): |
| 39 | + with TemporaryDirectory() as tmp_dir: |
| 40 | + kms_hook = CloudKMSHook() |
| 41 | + content = kms_hook.encrypt( |
| 42 | + key_name=( |
| 43 | + f"projects/{kms_hook.project_id}/locations/global/keyRings/" |
| 44 | + f"{GCP_KMS_KEYRING_NAME}/cryptoKeys/{GCP_KMS_KEY_NAME}" |
| 45 | + ), |
| 46 | + plaintext=b"TEST-SECRET", |
| 47 | + ) |
| 48 | + with open(f"{tmp_dir}/mysecret.txt.encrypted", "wb") as encrypted_file: |
| 49 | + encrypted_file.write(base64.b64decode(content)) |
| 50 | + self.execute_cmd( |
| 51 | + [ |
| 52 | + "gcloud", |
| 53 | + "kms", |
| 54 | + "decrypt", |
| 55 | + "--location", |
| 56 | + "global", |
| 57 | + "--keyring", |
| 58 | + GCP_KMS_KEYRING_NAME, |
| 59 | + "--key", |
| 60 | + GCP_KMS_KEY_NAME, |
| 61 | + "--ciphertext-file", |
| 62 | + f"{tmp_dir}/mysecret.txt.encrypted", |
| 63 | + "--plaintext-file", |
| 64 | + f"{tmp_dir}/mysecret.txt", |
| 65 | + ] |
| 66 | + ) |
| 67 | + with open(f"{tmp_dir}/mysecret.txt", "rb") as secret_file: |
| 68 | + secret = secret_file.read() |
| 69 | + self.assertEqual(secret, b"TEST-SECRET") |
| 70 | + |
| 71 | + @provide_gcp_context(GCP_KMS_KEY) |
| 72 | + def test_decrypt(self): |
| 73 | + with TemporaryDirectory() as tmp_dir: |
| 74 | + with open(f"{tmp_dir}/mysecret.txt", "w") as secret_file: |
| 75 | + secret_file.write("TEST-SECRET") |
| 76 | + self.execute_cmd( |
| 77 | + [ |
| 78 | + "gcloud", |
| 79 | + "kms", |
| 80 | + "encrypt", |
| 81 | + "--location", |
| 82 | + "global", |
| 83 | + "--keyring", |
| 84 | + GCP_KMS_KEYRING_NAME, |
| 85 | + "--key", |
| 86 | + GCP_KMS_KEY_NAME, |
| 87 | + "--plaintext-file", |
| 88 | + f"{tmp_dir}/mysecret.txt", |
| 89 | + "--ciphertext-file", |
| 90 | + f"{tmp_dir}/mysecret.txt.encrypted", |
| 91 | + ] |
| 92 | + ) |
| 93 | + with open(f"{tmp_dir}/mysecret.txt.encrypted", "rb") as encrypted_file: |
| 94 | + encrypted_secret = base64.b64encode(encrypted_file.read()).decode() |
| 95 | + |
| 96 | + kms_hook = CloudKMSHook() |
| 97 | + content = kms_hook.decrypt( |
| 98 | + key_name=( |
| 99 | + f"projects/{kms_hook.project_id}/locations/global/keyRings/" |
| 100 | + f"{GCP_KMS_KEYRING_NAME}/cryptoKeys/{GCP_KMS_KEY_NAME}" |
| 101 | + ), |
| 102 | + ciphertext=encrypted_secret, |
| 103 | + ) |
| 104 | + self.assertEqual(content, b"TEST-SECRET") |
0 commit comments