From 685edc8707f921aff5c3ecc1ad4910e7a4b21e73 Mon Sep 17 00:00:00 2001 From: Jakob Beckmann Date: Tue, 4 Jul 2023 18:09:17 +0200 Subject: [PATCH 01/12] feat(vault): add support for HashiCorp Vault container --- vault/README.rst | 1 + vault/setup.py | 18 ++++++ vault/testcontainers/vault/__init__.py | 87 ++++++++++++++++++++++++++ vault/tests/test_vault.py | 39 ++++++++++++ 4 files changed, 145 insertions(+) create mode 100644 vault/README.rst create mode 100644 vault/setup.py create mode 100644 vault/testcontainers/vault/__init__.py create mode 100644 vault/tests/test_vault.py diff --git a/vault/README.rst b/vault/README.rst new file mode 100644 index 00000000..fa4e446a --- /dev/null +++ b/vault/README.rst @@ -0,0 +1 @@ +.. autoclass:: testcontainers.vault.VaultContainer diff --git a/vault/setup.py b/vault/setup.py new file mode 100644 index 00000000..7140cb07 --- /dev/null +++ b/vault/setup.py @@ -0,0 +1,18 @@ +from setuptools import setup, find_namespace_packages + +description = "Vault component of testcontainers-python." + +setup( + name="testcontainers-vault", + version="0.0.1rc1", + packages=find_namespace_packages(), + description=description, + long_description=description, + long_description_content_type="text/x-rst", + url="https://github.com/testcontainers/testcontainers-python", + install_requires=[ + "testcontainers-core", + "hvac", + ], + python_requires=">=3.7", +) diff --git a/vault/testcontainers/vault/__init__.py b/vault/testcontainers/vault/__init__.py new file mode 100644 index 00000000..4859d78e --- /dev/null +++ b/vault/testcontainers/vault/__init__.py @@ -0,0 +1,87 @@ +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import hvac +import requests +from testcontainers.core.container import DockerContainer +from testcontainers.core.utils import raise_for_deprecated_parameter +from testcontainers.core.waiting_utils import wait_container_is_ready + + +class VaultContainer(DockerContainer): + """ + Vault container. + + Example: + + .. doctest:: + + >>> from testcontainers.vault import VaultContainer + + >>> with VaultContainer() as vault_container: + ... vault_client = vault_container.get_client() + """ + def __init__(self, image: str = "hashicorp/vault:latest", port: int = 8200, + root_token: str = "toor", **kwargs) -> None: + raise_for_deprecated_parameter(kwargs, "port_to_expose", "port") + super(VaultContainer, self).__init__(image, **kwargs) + self.port = port + self.root_token = root_token + self.with_exposed_ports(self.port) + self.with_env("VAULT_DEV_ROOT_TOKEN_ID", self.root_token) + + def get_config(self) -> dict: + """ + Get the configuration used to connect to the Vault container, including the address to + connect to, and the root token. + + Returns: + dict: {`address`: str, `root_token`: str} + """ + host_ip = self.get_container_host_ip() + exposed_port = self.get_exposed_port(self.port) + return { + "root_token": self.root_token, + "address": f"http://{host_ip}:{exposed_port}", + } + + def get_client(self) -> hvac.Client: + """ + Get a Vault client. + + Returns: + client: Vault client to connect to the container. + """ + config = self.get_config() + return hvac.Client(url=config["address"]) + + def get_root_client(self) -> hvac.Client: + """ + Get an authenticated Vault client with root token. + + Returns: + client: Vault client to connect to the container. + """ + config = self.get_config() + return hvac.Client(url=config["address"], token=config["root_token"]) + + @wait_container_is_ready(requests.ConnectionError) + def _healthcheck(self) -> None: + url = f"{self.config()['address']}/v1/sys/health" + response = requests.get(url, timeout=3) + response.raise_for_status() + + def start(self) -> "VaultContainer": + super().start() + self._healthcheck() + return self diff --git a/vault/tests/test_vault.py b/vault/tests/test_vault.py new file mode 100644 index 00000000..8272f8cb --- /dev/null +++ b/vault/tests/test_vault.py @@ -0,0 +1,39 @@ +from testcontainers.vault import VaultContainer + + +def test_docker_run_vault(): + config = VaultContainer() + with config as vault: + client = vault.get_client() + assert not client.is_authenticated() + status = client.sys.read_health_status() + assert status.status_code == 200 + + +def test_docker_run_vault_act_as_root(): + config = VaultContainer() + with config as vault: + client = vault.get_root_client + assert client.is_authenticated() + assert client.sys.is_initialized() + assert not client.sys.is_sealed() + + client.sys.enable_secrets_engine( + backend_type="kv", + path="secrets", + config={ + "version": "2", + }, + ) + client.secrets.kv.v2.create_or_update_secret( + path="my-secret", + mount_point="secrets", + secret={ + "pssst": "this is secret", + }, + ) + resp = client.secrets.kv.v1.read_secret( + path="my-secret", + mount_point="secrets", + ) + assert resp["data"]["pssst"] == "this is secret" From f9c9c834761024a17e204fa8b8751cab6507f4cd Mon Sep 17 00:00:00 2001 From: Jakob Beckmann Date: Tue, 4 Jul 2023 18:18:33 +0200 Subject: [PATCH 02/12] fix(vault): remove typo in tests --- vault/tests/test_vault.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vault/tests/test_vault.py b/vault/tests/test_vault.py index 8272f8cb..06f824ef 100644 --- a/vault/tests/test_vault.py +++ b/vault/tests/test_vault.py @@ -13,7 +13,7 @@ def test_docker_run_vault(): def test_docker_run_vault_act_as_root(): config = VaultContainer() with config as vault: - client = vault.get_root_client + client = vault.get_root_client() assert client.is_authenticated() assert client.sys.is_initialized() assert not client.sys.is_sealed() From c2847c53187caccf8ea10d8305f104f053ac9e1d Mon Sep 17 00:00:00 2001 From: Jakob Beckmann Date: Thu, 4 Apr 2024 18:59:43 +0200 Subject: [PATCH 03/12] chore(vault): update files to match new module structure --- index.rst | 1 + {vault => modules/vault}/README.rst | 0 {vault => modules/vault}/setup.py | 0 {vault => modules/vault}/testcontainers/vault/__init__.py | 0 {vault => modules/vault}/tests/test_vault.py | 0 pyproject.toml | 3 +++ 6 files changed, 4 insertions(+) rename {vault => modules/vault}/README.rst (100%) rename {vault => modules/vault}/setup.py (100%) rename {vault => modules/vault}/testcontainers/vault/__init__.py (100%) rename {vault => modules/vault}/tests/test_vault.py (100%) diff --git a/index.rst b/index.rst index 2a2bc659..4f3dad80 100644 --- a/index.rst +++ b/index.rst @@ -42,6 +42,7 @@ testcontainers-python facilitates the use of Docker containers for functional an modules/redis/README modules/registry/README modules/selenium/README + modules/vault/README modules/weaviate/README Getting Started diff --git a/vault/README.rst b/modules/vault/README.rst similarity index 100% rename from vault/README.rst rename to modules/vault/README.rst diff --git a/vault/setup.py b/modules/vault/setup.py similarity index 100% rename from vault/setup.py rename to modules/vault/setup.py diff --git a/vault/testcontainers/vault/__init__.py b/modules/vault/testcontainers/vault/__init__.py similarity index 100% rename from vault/testcontainers/vault/__init__.py rename to modules/vault/testcontainers/vault/__init__.py diff --git a/vault/tests/test_vault.py b/modules/vault/tests/test_vault.py similarity index 100% rename from vault/tests/test_vault.py rename to modules/vault/tests/test_vault.py diff --git a/pyproject.toml b/pyproject.toml index 2d2fbeb5..72775acb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -55,6 +55,7 @@ packages = [ { include = "testcontainers", from = "modules/redis" }, { include = "testcontainers", from = "modules/registry" }, { include = "testcontainers", from = "modules/selenium" }, + { include = "testcontainers", from = "modules/vault" }, { include = "testcontainers", from = "modules/weaviate" } ] @@ -74,6 +75,7 @@ azure-storage-blob = { version = "^12.19", optional = true } clickhouse-driver = { version = "*", optional = true } google-cloud-pubsub = { version = ">=2", optional = true } google-cloud-datastore = { version = ">=2", optional = true } +hvac = { version = ">=2", optional = true } influxdb = { version = "*", optional = true } influxdb-client = { version = "*", optional = true } kubernetes = { version = "*", optional = true } @@ -125,6 +127,7 @@ rabbitmq = ["pika"] redis = ["redis"] registry = ["bcrypt"] selenium = ["selenium"] +vault = ["hvac"] weaviate = ["weaviate-client"] chroma = ["chromadb-client"] From 3e7b7b8e34bbc8bddd7b9bbe26a70fb87181f6f6 Mon Sep 17 00:00:00 2001 From: Jakob Beckmann Date: Thu, 4 Apr 2024 19:03:40 +0200 Subject: [PATCH 04/12] fix(vault): fix vault README and add requests dependency for vault --- modules/vault/README.rst | 1 + pyproject.toml | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/modules/vault/README.rst b/modules/vault/README.rst index fa4e446a..71c079df 100644 --- a/modules/vault/README.rst +++ b/modules/vault/README.rst @@ -1 +1,2 @@ .. autoclass:: testcontainers.vault.VaultContainer +.. title:: testcontainers.vault.VaultContainer diff --git a/pyproject.toml b/pyproject.toml index 72775acb..e2c5c462 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -93,6 +93,7 @@ opensearch-py = { version = "*", optional = true } oracledb = { version = "*", optional = true } pika = { version = "*", optional = true } redis = { version = "*", optional = true } +requests = { version = ">=2", optional = true } selenium = { version = "*", optional = true } weaviate-client = { version = "^4.5.4", optional = true } chromadb-client = { version = "*", optional = true } @@ -127,7 +128,7 @@ rabbitmq = ["pika"] redis = ["redis"] registry = ["bcrypt"] selenium = ["selenium"] -vault = ["hvac"] +vault = ["hvac", "requests"] weaviate = ["weaviate-client"] chroma = ["chromadb-client"] From dd6963d943f2926f8d4173ca5365ec4652a5c4bd Mon Sep 17 00:00:00 2001 From: Jakob Beckmann Date: Thu, 4 Apr 2024 19:21:22 +0200 Subject: [PATCH 05/12] fix(vault): update lock file and fix tests --- .../vault/testcontainers/vault/__init__.py | 2 +- modules/vault/tests/test_vault.py | 8 ++++---- poetry.lock | 20 ++++++++++++++++++- pyproject.toml | 1 + 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/modules/vault/testcontainers/vault/__init__.py b/modules/vault/testcontainers/vault/__init__.py index 4859d78e..3009b87d 100644 --- a/modules/vault/testcontainers/vault/__init__.py +++ b/modules/vault/testcontainers/vault/__init__.py @@ -77,7 +77,7 @@ def get_root_client(self) -> hvac.Client: @wait_container_is_ready(requests.ConnectionError) def _healthcheck(self) -> None: - url = f"{self.config()['address']}/v1/sys/health" + url = f"{self.get_config()['address']}/v1/sys/health" response = requests.get(url, timeout=3) response.raise_for_status() diff --git a/modules/vault/tests/test_vault.py b/modules/vault/tests/test_vault.py index 06f824ef..8913bbe5 100644 --- a/modules/vault/tests/test_vault.py +++ b/modules/vault/tests/test_vault.py @@ -2,7 +2,7 @@ def test_docker_run_vault(): - config = VaultContainer() + config = VaultContainer("hashicorp/vault:1.16.1") with config as vault: client = vault.get_client() assert not client.is_authenticated() @@ -11,7 +11,7 @@ def test_docker_run_vault(): def test_docker_run_vault_act_as_root(): - config = VaultContainer() + config = VaultContainer("hashicorp/vault:1.16.1") with config as vault: client = vault.get_root_client() assert client.is_authenticated() @@ -32,8 +32,8 @@ def test_docker_run_vault_act_as_root(): "pssst": "this is secret", }, ) - resp = client.secrets.kv.v1.read_secret( + resp = client.secrets.kv.v2.read_secret( path="my-secret", mount_point="secrets", ) - assert resp["data"]["pssst"] == "this is secret" + assert resp["data"]["data"]["pssst"] == "this is secret" diff --git a/poetry.lock b/poetry.lock index 614e42e5..824331ef 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1449,6 +1449,23 @@ cli = ["click (==8.*)", "pygments (==2.*)", "rich (>=10,<14)"] http2 = ["h2 (>=3,<5)"] socks = ["socksio (==1.*)"] +[[package]] +name = "hvac" +version = "2.1.0" +description = "HashiCorp Vault API client" +optional = true +python-versions = ">=3.8,<4.0" +files = [ + {file = "hvac-2.1.0-py3-none-any.whl", hash = "sha256:73bc91e58c3fc7c6b8107cdaca9cb71fa0a893dfd80ffbc1c14e20f24c0c29d7"}, + {file = "hvac-2.1.0.tar.gz", hash = "sha256:b48bcda11a4ab0a7b6c47232c7ba7c87fda318ae2d4a7662800c465a78742894"}, +] + +[package.dependencies] +requests = ">=2.27.1,<3.0.0" + +[package.extras] +parser = ["pyhcl (>=0.4.4,<0.5.0)"] + [[package]] name = "hyperframe" version = "6.0.1" @@ -4188,9 +4205,10 @@ rabbitmq = ["pika"] redis = ["redis"] registry = ["bcrypt"] selenium = ["selenium"] +vault = ["hvac", "requests"] weaviate = ["weaviate-client"] [metadata] lock-version = "2.0" python-versions = ">=3.9,<4.0" -content-hash = "af9f21cb52ebd761ba91c852c9839d982124c149e77abdc079fc5657cecb9ff7" +content-hash = "30c865efa5bbb0556ac549c822acd26e33ef7b33bab402e29339a1f0bf5c8c50" diff --git a/pyproject.toml b/pyproject.toml index e2c5c462..110ec944 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -266,6 +266,7 @@ mypy_path = [ # "modules/rabbitmq", # "modules/redis", # "modules/selenium" +# "modules/vault" # "modules/weaviate" ] enable_error_code = [ From 8ff7079046b2166b612f3e01bd66396372508738 Mon Sep 17 00:00:00 2001 From: Jakob Beckmann Date: Thu, 4 Apr 2024 19:28:09 +0200 Subject: [PATCH 06/12] chore(vault): remove obsolete setup.py --- modules/vault/setup.py | 18 ------------------ 1 file changed, 18 deletions(-) delete mode 100644 modules/vault/setup.py diff --git a/modules/vault/setup.py b/modules/vault/setup.py deleted file mode 100644 index 7140cb07..00000000 --- a/modules/vault/setup.py +++ /dev/null @@ -1,18 +0,0 @@ -from setuptools import setup, find_namespace_packages - -description = "Vault component of testcontainers-python." - -setup( - name="testcontainers-vault", - version="0.0.1rc1", - packages=find_namespace_packages(), - description=description, - long_description=description, - long_description_content_type="text/x-rst", - url="https://github.com/testcontainers/testcontainers-python", - install_requires=[ - "testcontainers-core", - "hvac", - ], - python_requires=">=3.7", -) From 1185d00cf23f7946a3bc08f106636b926a4505c5 Mon Sep 17 00:00:00 2001 From: Jakob Beckmann Date: Fri, 5 Apr 2024 09:50:33 +0200 Subject: [PATCH 07/12] chore(vault): remove requests dependency and pin vault version in doctest --- modules/vault/testcontainers/vault/__init__.py | 12 +++++++----- poetry.lock | 4 ++-- pyproject.toml | 3 +-- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/modules/vault/testcontainers/vault/__init__.py b/modules/vault/testcontainers/vault/__init__.py index 3009b87d..e5d533f3 100644 --- a/modules/vault/testcontainers/vault/__init__.py +++ b/modules/vault/testcontainers/vault/__init__.py @@ -12,7 +12,8 @@ # under the License. import hvac -import requests +from urllib.request import urlopen +from http.client import HTTPException from testcontainers.core.container import DockerContainer from testcontainers.core.utils import raise_for_deprecated_parameter from testcontainers.core.waiting_utils import wait_container_is_ready @@ -28,7 +29,7 @@ class VaultContainer(DockerContainer): >>> from testcontainers.vault import VaultContainer - >>> with VaultContainer() as vault_container: + >>> with VaultContainer("hashicorp/vault:1.16.1") as vault_container: ... vault_client = vault_container.get_client() """ def __init__(self, image: str = "hashicorp/vault:latest", port: int = 8200, @@ -75,11 +76,12 @@ def get_root_client(self) -> hvac.Client: config = self.get_config() return hvac.Client(url=config["address"], token=config["root_token"]) - @wait_container_is_ready(requests.ConnectionError) + @wait_container_is_ready(HTTPException) def _healthcheck(self) -> None: url = f"{self.get_config()['address']}/v1/sys/health" - response = requests.get(url, timeout=3) - response.raise_for_status() + with urlopen(url) as res: + if res.status > 299: + raise HTTPException() def start(self) -> "VaultContainer": super().start() diff --git a/poetry.lock b/poetry.lock index 824331ef..3291dd12 100644 --- a/poetry.lock +++ b/poetry.lock @@ -4205,10 +4205,10 @@ rabbitmq = ["pika"] redis = ["redis"] registry = ["bcrypt"] selenium = ["selenium"] -vault = ["hvac", "requests"] +vault = ["hvac"] weaviate = ["weaviate-client"] [metadata] lock-version = "2.0" python-versions = ">=3.9,<4.0" -content-hash = "30c865efa5bbb0556ac549c822acd26e33ef7b33bab402e29339a1f0bf5c8c50" +content-hash = "ac3493c50538b9b40d51671b22fcb679b9624ec6daf47e7ce37e750f2c8fc00d" diff --git a/pyproject.toml b/pyproject.toml index 110ec944..b2c7a639 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -93,7 +93,6 @@ opensearch-py = { version = "*", optional = true } oracledb = { version = "*", optional = true } pika = { version = "*", optional = true } redis = { version = "*", optional = true } -requests = { version = ">=2", optional = true } selenium = { version = "*", optional = true } weaviate-client = { version = "^4.5.4", optional = true } chromadb-client = { version = "*", optional = true } @@ -128,7 +127,7 @@ rabbitmq = ["pika"] redis = ["redis"] registry = ["bcrypt"] selenium = ["selenium"] -vault = ["hvac", "requests"] +vault = ["hvac"] weaviate = ["weaviate-client"] chroma = ["chromadb-client"] From cfa36dc5bd26830b5692388f8288eb2a0caa2821 Mon Sep 17 00:00:00 2001 From: Jakob Beckmann Date: Fri, 5 Apr 2024 10:01:55 +0200 Subject: [PATCH 08/12] impr(vault): remove hvac client from base dependencies, update tests accordingly --- .../vault/testcontainers/vault/__init__.py | 39 ++++--------------- modules/vault/tests/test_vault.py | 8 ++-- poetry.lock | 5 +-- pyproject.toml | 3 +- 4 files changed, 15 insertions(+), 40 deletions(-) diff --git a/modules/vault/testcontainers/vault/__init__.py b/modules/vault/testcontainers/vault/__init__.py index e5d533f3..27deb4ab 100644 --- a/modules/vault/testcontainers/vault/__init__.py +++ b/modules/vault/testcontainers/vault/__init__.py @@ -11,7 +11,6 @@ # License for the specific language governing permissions and limitations # under the License. -import hvac from urllib.request import urlopen from http.client import HTTPException from testcontainers.core.container import DockerContainer @@ -30,55 +29,31 @@ class VaultContainer(DockerContainer): >>> from testcontainers.vault import VaultContainer >>> with VaultContainer("hashicorp/vault:1.16.1") as vault_container: - ... vault_client = vault_container.get_client() + ... connection_url = vault_container.get_connection_url() + ... root_token = vault_container.root_token """ def __init__(self, image: str = "hashicorp/vault:latest", port: int = 8200, root_token: str = "toor", **kwargs) -> None: - raise_for_deprecated_parameter(kwargs, "port_to_expose", "port") super(VaultContainer, self).__init__(image, **kwargs) self.port = port self.root_token = root_token self.with_exposed_ports(self.port) self.with_env("VAULT_DEV_ROOT_TOKEN_ID", self.root_token) - def get_config(self) -> dict: + def get_connection_url(self) -> str: """ - Get the configuration used to connect to the Vault container, including the address to - connect to, and the root token. + Get the connection URL used to connect to the Vault container. Returns: - dict: {`address`: str, `root_token`: str} + str: The address to connect to. """ host_ip = self.get_container_host_ip() exposed_port = self.get_exposed_port(self.port) - return { - "root_token": self.root_token, - "address": f"http://{host_ip}:{exposed_port}", - } - - def get_client(self) -> hvac.Client: - """ - Get a Vault client. - - Returns: - client: Vault client to connect to the container. - """ - config = self.get_config() - return hvac.Client(url=config["address"]) - - def get_root_client(self) -> hvac.Client: - """ - Get an authenticated Vault client with root token. - - Returns: - client: Vault client to connect to the container. - """ - config = self.get_config() - return hvac.Client(url=config["address"], token=config["root_token"]) + return f"http://{host_ip}:{exposed_port}" @wait_container_is_ready(HTTPException) def _healthcheck(self) -> None: - url = f"{self.get_config()['address']}/v1/sys/health" + url = f"{self.get_connection_url()}/v1/sys/health" with urlopen(url) as res: if res.status > 299: raise HTTPException() diff --git a/modules/vault/tests/test_vault.py b/modules/vault/tests/test_vault.py index 8913bbe5..54017d2f 100644 --- a/modules/vault/tests/test_vault.py +++ b/modules/vault/tests/test_vault.py @@ -1,11 +1,12 @@ +import hvac from testcontainers.vault import VaultContainer def test_docker_run_vault(): config = VaultContainer("hashicorp/vault:1.16.1") with config as vault: - client = vault.get_client() - assert not client.is_authenticated() + url = vault.get_connection_url() + client = hvac.Client(url=url) status = client.sys.read_health_status() assert status.status_code == 200 @@ -13,7 +14,8 @@ def test_docker_run_vault(): def test_docker_run_vault_act_as_root(): config = VaultContainer("hashicorp/vault:1.16.1") with config as vault: - client = vault.get_root_client() + url = vault.get_connection_url() + client = hvac.Client(url=url, token=vault.root_token) assert client.is_authenticated() assert client.sys.is_initialized() assert not client.sys.is_sealed() diff --git a/poetry.lock b/poetry.lock index 3291dd12..4a42b1a2 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1453,7 +1453,7 @@ socks = ["socksio (==1.*)"] name = "hvac" version = "2.1.0" description = "HashiCorp Vault API client" -optional = true +optional = false python-versions = ">=3.8,<4.0" files = [ {file = "hvac-2.1.0-py3-none-any.whl", hash = "sha256:73bc91e58c3fc7c6b8107cdaca9cb71fa0a893dfd80ffbc1c14e20f24c0c29d7"}, @@ -4205,10 +4205,9 @@ rabbitmq = ["pika"] redis = ["redis"] registry = ["bcrypt"] selenium = ["selenium"] -vault = ["hvac"] weaviate = ["weaviate-client"] [metadata] lock-version = "2.0" python-versions = ">=3.9,<4.0" -content-hash = "ac3493c50538b9b40d51671b22fcb679b9624ec6daf47e7ce37e750f2c8fc00d" +content-hash = "8a46a48099cce46e1334e60022ceff6922be432a8ba021dc6c1cb93ce7c55866" diff --git a/pyproject.toml b/pyproject.toml index b2c7a639..2ba5471c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -75,7 +75,6 @@ azure-storage-blob = { version = "^12.19", optional = true } clickhouse-driver = { version = "*", optional = true } google-cloud-pubsub = { version = ">=2", optional = true } google-cloud-datastore = { version = ">=2", optional = true } -hvac = { version = ">=2", optional = true } influxdb = { version = "*", optional = true } influxdb-client = { version = "*", optional = true } kubernetes = { version = "*", optional = true } @@ -127,7 +126,6 @@ rabbitmq = ["pika"] redis = ["redis"] registry = ["bcrypt"] selenium = ["selenium"] -vault = ["hvac"] weaviate = ["weaviate-client"] chroma = ["chromadb-client"] @@ -147,6 +145,7 @@ psycopg = "*" cassandra-driver = "*" pytest-asyncio = "0.23.5" kafka-python-ng = "^2.2.0" +hvac = "*" [[tool.poetry.source]] name = "PyPI" From 56057cc16a9782f62c532aab366f46edb22228c2 Mon Sep 17 00:00:00 2001 From: Jakob Beckmann Date: Fri, 5 Apr 2024 10:12:10 +0200 Subject: [PATCH 09/12] docs(vault): improve doctest to better showcase the usage of the module --- modules/vault/testcontainers/vault/__init__.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/modules/vault/testcontainers/vault/__init__.py b/modules/vault/testcontainers/vault/__init__.py index 27deb4ab..2d4b06ac 100644 --- a/modules/vault/testcontainers/vault/__init__.py +++ b/modules/vault/testcontainers/vault/__init__.py @@ -27,10 +27,14 @@ class VaultContainer(DockerContainer): .. doctest:: >>> from testcontainers.vault import VaultContainer + >>> import hvac >>> with VaultContainer("hashicorp/vault:1.16.1") as vault_container: ... connection_url = vault_container.get_connection_url() - ... root_token = vault_container.root_token + ... client = hvac.Client(url=connection_url, token=vault_container.root_token) + ... assert client.is_authenticated() + ... # use root client to perform desired actions, e.g. + ... policies = client.sys.list_acl_policies() """ def __init__(self, image: str = "hashicorp/vault:latest", port: int = 8200, root_token: str = "toor", **kwargs) -> None: From d2a5bcc6b9d0dca945aea9b914c2076e4129559a Mon Sep 17 00:00:00 2001 From: Jakob Beckmann Date: Fri, 5 Apr 2024 10:14:28 +0200 Subject: [PATCH 10/12] fix(vault): update missing entry in extras for poetry --- poetry.lock | 3 ++- pyproject.toml | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/poetry.lock b/poetry.lock index 4a42b1a2..a2f81d3b 100644 --- a/poetry.lock +++ b/poetry.lock @@ -4205,9 +4205,10 @@ rabbitmq = ["pika"] redis = ["redis"] registry = ["bcrypt"] selenium = ["selenium"] +vault = [] weaviate = ["weaviate-client"] [metadata] lock-version = "2.0" python-versions = ">=3.9,<4.0" -content-hash = "8a46a48099cce46e1334e60022ceff6922be432a8ba021dc6c1cb93ce7c55866" +content-hash = "233dfd72d07a555973aafc3fe3b6676574403b9fe4bb2c0230d455cff8aa2933" diff --git a/pyproject.toml b/pyproject.toml index 2ba5471c..08c9c68b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -126,6 +126,7 @@ rabbitmq = ["pika"] redis = ["redis"] registry = ["bcrypt"] selenium = ["selenium"] +vault = [] weaviate = ["weaviate-client"] chroma = ["chromadb-client"] From 92021078761cba6a795a809262af539c459e1595 Mon Sep 17 00:00:00 2001 From: Jakob Beckmann Date: Fri, 5 Apr 2024 12:19:45 +0200 Subject: [PATCH 11/12] ci: re-trigger PR checks From 90fe99f0ebfd2fe01b88d039abdf1b473881a4a0 Mon Sep 17 00:00:00 2001 From: Jakob Beckmann Date: Fri, 5 Apr 2024 13:10:50 +0200 Subject: [PATCH 12/12] style(vault): lint vault container code --- modules/vault/testcontainers/vault/__init__.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/modules/vault/testcontainers/vault/__init__.py b/modules/vault/testcontainers/vault/__init__.py index 2d4b06ac..5f50cdd4 100644 --- a/modules/vault/testcontainers/vault/__init__.py +++ b/modules/vault/testcontainers/vault/__init__.py @@ -11,10 +11,10 @@ # License for the specific language governing permissions and limitations # under the License. -from urllib.request import urlopen from http.client import HTTPException +from urllib.request import urlopen + from testcontainers.core.container import DockerContainer -from testcontainers.core.utils import raise_for_deprecated_parameter from testcontainers.core.waiting_utils import wait_container_is_ready @@ -36,9 +36,15 @@ class VaultContainer(DockerContainer): ... # use root client to perform desired actions, e.g. ... policies = client.sys.list_acl_policies() """ - def __init__(self, image: str = "hashicorp/vault:latest", port: int = 8200, - root_token: str = "toor", **kwargs) -> None: - super(VaultContainer, self).__init__(image, **kwargs) + + def __init__( + self, + image: str = "hashicorp/vault:latest", + port: int = 8200, + root_token: str = "toor", + **kwargs, + ) -> None: + super().__init__(image, **kwargs) self.port = port self.root_token = root_token self.with_exposed_ports(self.port)