-
Notifications
You must be signed in to change notification settings - Fork 312
fix(vault): add support for HashiCorp Vault container #366
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
685edc8
feat(vault): add support for HashiCorp Vault container
f4z3r f9c9c83
fix(vault): remove typo in tests
f4z3r c2847c5
chore(vault): update files to match new module structure
f4z3r 3e7b7b8
fix(vault): fix vault README and add requests dependency for vault
f4z3r dd6963d
fix(vault): update lock file and fix tests
f4z3r 8ff7079
chore(vault): remove obsolete setup.py
f4z3r 1185d00
chore(vault): remove requests dependency and pin vault version in doc…
f4z3r cfa36dc
impr(vault): remove hvac client from base dependencies, update tests …
f4z3r 56057cc
docs(vault): improve doctest to better showcase the usage of the module
f4z3r d2a5bcc
fix(vault): update missing entry in extras for poetry
f4z3r 9202107
ci: re-trigger PR checks
f4z3r 90fe99f
style(vault): lint vault container code
f4z3r File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.. autoclass:: testcontainers.vault.VaultContainer | ||
.. title:: testcontainers.vault.VaultContainer |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# | ||
# 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. | ||
|
||
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 | ||
|
||
|
||
class VaultContainer(DockerContainer): | ||
""" | ||
Vault container. | ||
|
||
Example: | ||
|
||
.. doctest:: | ||
|
||
>>> from testcontainers.vault import VaultContainer | ||
|
||
>>> with VaultContainer("hashicorp/vault:1.16.1") as vault_container: | ||
... 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: | ||
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_connection_url(self) -> str: | ||
""" | ||
Get the connection URL used to connect to the Vault container. | ||
|
||
Returns: | ||
str: The address to connect to. | ||
""" | ||
host_ip = self.get_container_host_ip() | ||
exposed_port = self.get_exposed_port(self.port) | ||
return f"http://{host_ip}:{exposed_port}" | ||
|
||
@wait_container_is_ready(HTTPException) | ||
def _healthcheck(self) -> None: | ||
url = f"{self.get_connection_url()}/v1/sys/health" | ||
with urlopen(url) as res: | ||
if res.status > 299: | ||
raise HTTPException() | ||
|
||
def start(self) -> "VaultContainer": | ||
super().start() | ||
self._healthcheck() | ||
return self |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import hvac | ||
from testcontainers.vault import VaultContainer | ||
|
||
|
||
def test_docker_run_vault(): | ||
config = VaultContainer("hashicorp/vault:1.16.1") | ||
with config as vault: | ||
url = vault.get_connection_url() | ||
client = hvac.Client(url=url) | ||
status = client.sys.read_health_status() | ||
assert status.status_code == 200 | ||
|
||
|
||
def test_docker_run_vault_act_as_root(): | ||
config = VaultContainer("hashicorp/vault:1.16.1") | ||
with config as vault: | ||
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() | ||
|
||
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.v2.read_secret( | ||
path="my-secret", | ||
mount_point="secrets", | ||
) | ||
assert resp["data"]["data"]["pssst"] == "this is secret" |
f4z3r marked this conversation as resolved.
Show resolved
Hide resolved
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.