Add support for Vault Namespaces

Vault Namespaces [0] is a feature available in Vault Enterprise that
can be considered as a more advanced isolation feature on top of current
KV Mountpoint option in Castellan Vault plugin.

Passing a namespace in all request headers (including Auth) allows to organize
Vault-in-Vault style of isolation, with clients using the same simple URI path
but accessing separate sets of entities in Vault.

[0] https://www.vaultproject.io/docs/enterprise/namespaces

Change-Id: I627c20002bb2a0a1b346b57e824f87f856eca4c9
This commit is contained in:
Pavlo Shchelokovskyy
2021-09-06 11:44:48 +03:00
committed by Pavlo Shchelokovskyy
parent aa3a760b61
commit ecf625b65c
5 changed files with 97 additions and 3 deletions

View File

@@ -0,0 +1,66 @@
# Copyright (c) 2021 Mirantis Inc
# All Rights Reserved.
#
# 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.
"""
Test cases for Vault key manager.
"""
import requests_mock
from castellan.key_manager import vault_key_manager
from castellan.tests.unit.key_manager import test_key_manager
class VaultKeyManagerTestCase(test_key_manager.KeyManagerTestCase):
def _create_key_manager(self):
return vault_key_manager.VaultKeyManager(self.conf)
def test_auth_headers_root_token(self):
self.key_mgr._root_token_id = "spam"
expected_headers = {"X-Vault-Token": "spam"}
self.assertEqual(expected_headers,
self.key_mgr._build_auth_headers())
def test_auth_headers_root_token_with_namespace(self):
self.key_mgr._root_token_id = "spam"
self.key_mgr._namespace = "ham"
expected_headers = {"X-Vault-Token": "spam",
"X-Vault-Namespace": "ham"}
self.assertEqual(expected_headers,
self.key_mgr._build_auth_headers())
@requests_mock.Mocker()
def test_auth_headers_app_role(self, m):
self.key_mgr._approle_role_id = "spam"
self.key_mgr._approle_secret_id = "secret"
m.post(
"http://127.0.0.1:8200/v1/auth/approle/login",
json={"auth": {"client_token": "token", "lease_duration": 3600}}
)
expected_headers = {"X-Vault-Token": "token"}
self.assertEqual(expected_headers, self.key_mgr._build_auth_headers())
@requests_mock.Mocker()
def test_auth_headers_app_role_with_namespace(self, m):
self.key_mgr._approle_role_id = "spam"
self.key_mgr._approle_secret_id = "secret"
self.key_mgr._namespace = "ham"
m.post(
"http://127.0.0.1:8200/v1/auth/approle/login",
json={"auth": {"client_token": "token", "lease_duration": 3600}}
)
expected_headers = {"X-Vault-Token": "token",
"X-Vault-Namespace": "ham"}
self.assertEqual(expected_headers, self.key_mgr._build_auth_headers())