diff --git a/lib/galaxy/app_unittest_utils/galaxy_mock.py b/lib/galaxy/app_unittest_utils/galaxy_mock.py index 3e82de3f4c3..426496abc2f 100644 --- a/lib/galaxy/app_unittest_utils/galaxy_mock.py +++ b/lib/galaxy/app_unittest_utils/galaxy_mock.py @@ -182,7 +182,7 @@ class MockAppConfig(GalaxyDataTestConfig, CommonConfigurationMixin): self.enable_tool_shed_check = False self.monitor_thread_join_timeout = 1 self.integrated_tool_panel_config = None - self.vault_config_file = None + self.vault_config_file = kwargs.get('vault_config_file') @property def config_dict(self): diff --git a/lib/galaxy/files/__init__.py b/lib/galaxy/files/__init__.py index 867ebb5147e..b45e0721a07 100644 --- a/lib/galaxy/files/__init__.py +++ b/lib/galaxy/files/__init__.py @@ -265,8 +265,8 @@ class ProvidesUserFileSourcesUserContext: @property def vault(self): - user = self.trans.user - return user and user.personal_vault or defaultdict(lambda: None) + user_vault = self.trans.user_vault + return user_vault or defaultdict(lambda: None) class DictFileSourcesUserContext: diff --git a/lib/galaxy_test/api/test_vault.py b/lib/galaxy_test/api/test_vault.py index 459bffff12a..40f87074931 100644 --- a/lib/galaxy_test/api/test_vault.py +++ b/lib/galaxy_test/api/test_vault.py @@ -1,6 +1,6 @@ import json import os -from typing import cast, Any +from typing import Any, cast from requests import ( get, diff --git a/test/unit/security/fixtures/vault_conf_hashicorp.yaml b/test/unit/security/fixtures/vault_conf_hashicorp.yaml index 43ef3c9ffbc..738d0b896f6 100644 --- a/test/unit/security/fixtures/vault_conf_hashicorp.yaml +++ b/test/unit/security/fixtures/vault_conf_hashicorp.yaml @@ -1,3 +1,5 @@ +# to run tests, start vault with: +# $ vault server -dev -dev-root-token-id=galaxy_test_token type: hashicorp -vault_address: http://localhost:8200 -vault_token: galaxy_test_token +vault_address: ${vault_address} +vault_token: ${vault_token} diff --git a/test/unit/security/test_vault.py b/test/unit/security/test_vault.py index 4585b577302..482ba15aa7a 100644 --- a/test/unit/security/test_vault.py +++ b/test/unit/security/test_vault.py @@ -7,34 +7,44 @@ from abc import ABC from cryptography.fernet import InvalidToken from galaxy.app_unittest_utils.galaxy_mock import MockApp, MockAppConfig -from galaxy.security.vault import NullVault, Vault, VaultFactory +from galaxy.security.vault import Vault, VaultFactory -class VaultTestBase(ABC, unittest.TestCase): - - def __init__(self): - self.vault = NullVault() # type: Vault +class VaultTestBase(ABC): + vault: Vault def test_read_write_secret(self): self.vault.write_secret("my/test/secret", "hello world") - self.assertEqual(self.vault.read_secret("my/test/secret"), "hello world") + self.assertEqual(self.vault.read_secret("my/test/secret"), "hello world") # type: ignore def test_overwrite_secret(self): self.vault.write_secret("my/new/secret", "hello world") self.vault.write_secret("my/new/secret", "hello overwritten") - self.assertEqual(self.vault.read_secret("my/new/secret"), "hello overwritten") + self.assertEqual(self.vault.read_secret("my/new/secret"), "hello overwritten") # type: ignore VAULT_CONF_HASHICORP = os.path.join(os.path.dirname(__file__), "fixtures/vault_conf_hashicorp.yaml") +@unittest.skipIf(not os.environ.get('VAULT_ADDRESS') or not os.environ.get('VAULT_TOKEN'), + "VAULT_ADDRESS and VAULT_TOKEN env vars not set") class TestHashicorpVault(VaultTestBase, unittest.TestCase): def setUp(self) -> None: - config = MockAppConfig(vault_config_file=VAULT_CONF_HASHICORP) + with tempfile.NamedTemporaryFile( + mode="w", prefix="vault_hashicorp", delete=False) as tempconf, open(VAULT_CONF_HASHICORP) as f: + content = string.Template(f.read()).safe_substitute( + vault_address=os.environ.get('VAULT_ADDRESS'), + vault_token=os.environ.get('VAULT_TOKEN')) + tempconf.write(content) + self.vault_temp_conf = tempconf.name + config = MockAppConfig(vault_config_file=self.vault_temp_conf) app = MockApp(config=config) self.vault = VaultFactory.from_app(app) + def tearDown(self) -> None: + os.remove(self.vault_temp_conf) + VAULT_CONF_DATABASE = os.path.join(os.path.dirname(__file__), "fixtures/vault_conf_database.yaml") VAULT_CONF_DATABASE_ROTATED = os.path.join(os.path.dirname(__file__), "fixtures/vault_conf_database_rotated.yaml") @@ -75,12 +85,16 @@ class TestDatabaseVault(VaultTestBase, unittest.TestCase): VAULT_CONF_CUSTOS = os.path.join(os.path.dirname(__file__), "fixtures/vault_conf_custos.yaml") +@unittest.skipIf(not os.environ.get('CUSTOS_CLIENT_ID') or not os.environ.get('CUSTOS_CLIENT_SECRET'), + "CUSTOS_CLIENT_ID and CUSTOS_CLIENT_SECRET env vars not set") class TestCustosVault(VaultTestBase, unittest.TestCase): def setUp(self) -> None: - with tempfile.NamedTemporaryFile(mode="w", prefix="vault_custos", delete=False) as tempconf, open(VAULT_CONF_CUSTOS) as f: - content = string.Template(f.read()).safe_substitute(custos_client_id=os.environ.get('CUSTOS_CLIENT_ID'), - custos_client_secret=os.environ.get('CUSTOS_CLIENT_SECRET')) + with tempfile.NamedTemporaryFile( + mode="w", prefix="vault_custos", delete=False) as tempconf, open(VAULT_CONF_CUSTOS) as f: + content = string.Template(f.read()).safe_substitute( + custos_client_id=os.environ.get('CUSTOS_CLIENT_ID'), + custos_client_secret=os.environ.get('CUSTOS_CLIENT_SECRET')) tempconf.write(content) self.vault_temp_conf = tempconf.name config = MockAppConfig(vault_config_file=self.vault_temp_conf)