2019-09-09 13:12:24 +00:00
|
|
|
"""
|
|
|
|
Deploy and configure Vault for Teuthology
|
|
|
|
"""
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import contextlib
|
|
|
|
import logging
|
|
|
|
import time
|
|
|
|
import json
|
2019-10-14 10:39:45 +00:00
|
|
|
from os import path
|
2020-07-19 09:39:11 +00:00
|
|
|
from http import client as http_client
|
|
|
|
from urllib.parse import urljoin
|
2019-09-09 13:12:24 +00:00
|
|
|
|
|
|
|
from teuthology import misc as teuthology
|
|
|
|
from teuthology import contextutil
|
|
|
|
from teuthology.orchestra import run
|
2021-01-13 05:17:38 +00:00
|
|
|
from teuthology.exceptions import ConfigError, CommandFailedError
|
2019-09-09 13:12:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def assign_ports(ctx, config, initial_port):
|
|
|
|
"""
|
|
|
|
Assign port numbers starting from @initial_port
|
|
|
|
"""
|
|
|
|
port = initial_port
|
|
|
|
role_endpoints = {}
|
2019-10-09 12:36:58 +00:00
|
|
|
for remote, roles_for_host in ctx.cluster.remotes.items():
|
2019-09-09 13:12:24 +00:00
|
|
|
for role in roles_for_host:
|
|
|
|
if role in config:
|
|
|
|
role_endpoints[role] = (remote.name.split('@')[1], port)
|
|
|
|
port += 1
|
|
|
|
|
|
|
|
return role_endpoints
|
|
|
|
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
def download(ctx, config):
|
|
|
|
"""
|
|
|
|
Download Vault Release from Hashicopr website.
|
|
|
|
Remove downloaded file upon exit.
|
|
|
|
"""
|
|
|
|
assert isinstance(config, dict)
|
|
|
|
log.info('Downloading Vault...')
|
|
|
|
testdir = teuthology.get_testdir(ctx)
|
|
|
|
|
|
|
|
for (client, cconf) in config.items():
|
2019-10-14 10:39:45 +00:00
|
|
|
install_url = cconf.get('install_url')
|
|
|
|
install_sha256 = cconf.get('install_sha256')
|
|
|
|
if not install_url or not install_sha256:
|
|
|
|
raise ConfigError("Missing Vault install_url and/or install_sha256")
|
|
|
|
install_zip = path.join(testdir, 'vault.zip')
|
|
|
|
install_dir = path.join(testdir, 'vault')
|
|
|
|
|
|
|
|
log.info('Downloading Vault...')
|
2019-09-09 13:12:24 +00:00
|
|
|
ctx.cluster.only(client).run(
|
2019-10-14 10:39:45 +00:00
|
|
|
args=['curl', '-L', install_url, '-o', install_zip])
|
2019-09-09 13:12:24 +00:00
|
|
|
|
2019-10-14 10:39:45 +00:00
|
|
|
log.info('Verifying SHA256 signature...')
|
|
|
|
ctx.cluster.only(client).run(
|
|
|
|
args=['echo', ' '.join([install_sha256, install_zip]), run.Raw('|'),
|
|
|
|
'sha256sum', '--check', '--status'])
|
2019-09-09 13:12:24 +00:00
|
|
|
|
|
|
|
log.info('Extracting vault...')
|
2019-10-14 10:39:45 +00:00
|
|
|
ctx.cluster.only(client).run(args=['mkdir', '-p', install_dir])
|
2019-09-09 13:12:24 +00:00
|
|
|
# Using python in case unzip is not installed on hosts
|
2021-01-13 05:17:38 +00:00
|
|
|
# Using python3 in case python is not installed on hosts
|
|
|
|
failed=True
|
|
|
|
for f in [
|
|
|
|
lambda z,d: ['unzip', z, '-d', d],
|
|
|
|
lambda z,d: ['python3', '-m', 'zipfile', '-e', z, d],
|
|
|
|
lambda z,d: ['python', '-m', 'zipfile', '-e', z, d]]:
|
|
|
|
try:
|
|
|
|
ctx.cluster.only(client).run(args=f(install_zip, install_dir))
|
|
|
|
failed = False
|
|
|
|
break
|
|
|
|
except CommandFailedError as e:
|
|
|
|
failed = e
|
|
|
|
if failed:
|
|
|
|
raise failed
|
2019-09-09 13:12:24 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
log.info('Removing Vault...')
|
|
|
|
testdir = teuthology.get_testdir(ctx)
|
|
|
|
for client in config:
|
|
|
|
ctx.cluster.only(client).run(
|
2019-10-14 10:39:45 +00:00
|
|
|
args=['rm', '-rf', install_dir, install_zip])
|
2019-09-09 13:12:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_vault_dir(ctx):
|
|
|
|
return '{tdir}/vault'.format(tdir=teuthology.get_testdir(ctx))
|
|
|
|
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
def run_vault(ctx, config):
|
|
|
|
assert isinstance(config, dict)
|
|
|
|
|
|
|
|
for (client, cconf) in config.items():
|
2019-10-11 15:57:47 +00:00
|
|
|
(remote,) = ctx.cluster.only(client).remotes.keys()
|
2019-09-09 13:12:24 +00:00
|
|
|
cluster_name, _, client_id = teuthology.split_role(client)
|
|
|
|
|
|
|
|
_, port = ctx.vault.endpoints[client]
|
|
|
|
listen_addr = "0.0.0.0:{}".format(port)
|
|
|
|
|
|
|
|
root_token = ctx.vault.root_token = cconf.get('root_token', 'root')
|
|
|
|
|
|
|
|
log.info("Starting Vault listening on %s ...", listen_addr)
|
|
|
|
v_params = [
|
|
|
|
'-dev',
|
|
|
|
'-dev-listen-address={}'.format(listen_addr),
|
|
|
|
'-dev-no-store-token',
|
|
|
|
'-dev-root-token-id={}'.format(root_token)
|
|
|
|
]
|
|
|
|
|
|
|
|
cmd = "chmod +x {vdir}/vault && {vdir}/vault server {vargs}".format(vdir=get_vault_dir(ctx), vargs=" ".join(v_params))
|
|
|
|
|
|
|
|
ctx.daemons.add_daemon(
|
|
|
|
remote, 'vault', client_id,
|
|
|
|
cluster=cluster_name,
|
|
|
|
args=['bash', '-c', cmd, run.Raw('& { read; kill %1; }')],
|
|
|
|
logger=log.getChild(client),
|
|
|
|
stdin=run.PIPE,
|
|
|
|
cwd=get_vault_dir(ctx),
|
|
|
|
wait=False,
|
|
|
|
check_status=False,
|
|
|
|
)
|
|
|
|
time.sleep(10)
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
log.info('Stopping Vault instance')
|
|
|
|
ctx.daemons.get_daemon('vault', client_id, cluster_name).stop()
|
|
|
|
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
def setup_vault(ctx, config):
|
|
|
|
"""
|
2019-11-07 15:33:14 +00:00
|
|
|
Mount Transit or KV version 2 secrets engine
|
2019-09-09 13:12:24 +00:00
|
|
|
"""
|
qa/tasks: use next(iter(..)) for accessing first element in a view
in python2, dict.values() and dict.keys() return lists. but in python3,
they return views, which cannot be indexed directly using an integer index.
there are three use cases when we access these views in python3:
1. get the first element
2. get all the elements and then *might* want to access them by index
3. get the first element assuming there is only a single element in
the view
4. iterate thru the view
in the 1st case, we cannot assume the number of elements, so to be
python3 compatible, we should use `next(iter(a_dict))` instead.
in the 2nd case, in this change, the view is materialized using
`list(a_dict)`.
in the 3rd case, we can just continue using the short hand of
```py
(first_element,) = a_dict.keys()
```
to unpack the view. this works in both python2 and python3.
in the 4th case, the existing code works in both python2 and python3, as
both list and view can be iterated using `iter`, and `len` works as
well.
Signed-off-by: Kefu Chai <kchai@redhat.com>
2020-03-31 02:16:40 +00:00
|
|
|
(cclient, cconfig) = next(iter(config.items()))
|
2019-11-07 15:33:14 +00:00
|
|
|
engine = cconfig.get('engine')
|
|
|
|
|
|
|
|
if engine == 'kv':
|
|
|
|
log.info('Mounting kv version 2 secrets engine')
|
|
|
|
mount_path = '/v1/sys/mounts/kv'
|
|
|
|
data = {
|
|
|
|
"type": "kv",
|
|
|
|
"options": {
|
|
|
|
"version": "2"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elif engine == 'transit':
|
|
|
|
log.info('Mounting transit secrets engine')
|
|
|
|
mount_path = '/v1/sys/mounts/transit'
|
|
|
|
data = {
|
|
|
|
"type": "transit"
|
2019-09-09 13:12:24 +00:00
|
|
|
}
|
2019-11-07 15:33:14 +00:00
|
|
|
else:
|
|
|
|
raise Exception("Unknown or missing secrets engine")
|
2019-09-09 13:12:24 +00:00
|
|
|
|
2019-11-07 15:33:14 +00:00
|
|
|
send_req(ctx, cconfig, cclient, mount_path, json.dumps(data))
|
2019-09-09 13:12:24 +00:00
|
|
|
yield
|
|
|
|
|
|
|
|
|
|
|
|
def send_req(ctx, cconfig, client, path, body, method='POST'):
|
|
|
|
host, port = ctx.vault.endpoints[client]
|
2020-03-24 08:33:57 +00:00
|
|
|
req = http_client.HTTPConnection(host, port, timeout=30)
|
2019-09-09 13:12:24 +00:00
|
|
|
token = cconfig.get('root_token', 'atoken')
|
2019-10-14 10:39:45 +00:00
|
|
|
log.info("Send request to Vault: %s:%s at %s with token: %s", host, port, path, token)
|
2019-09-09 13:12:24 +00:00
|
|
|
headers = {'X-Vault-Token': token}
|
|
|
|
req.request(method, path, headers=headers, body=body)
|
|
|
|
resp = req.getresponse()
|
|
|
|
log.info(resp.read())
|
|
|
|
if not (resp.status >= 200 and resp.status < 300):
|
2019-11-07 15:33:14 +00:00
|
|
|
raise Exception("Request to Vault server failed with status %d" % resp.status)
|
2019-09-09 13:12:24 +00:00
|
|
|
return resp
|
|
|
|
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
def create_secrets(ctx, config):
|
qa/tasks: use next(iter(..)) for accessing first element in a view
in python2, dict.values() and dict.keys() return lists. but in python3,
they return views, which cannot be indexed directly using an integer index.
there are three use cases when we access these views in python3:
1. get the first element
2. get all the elements and then *might* want to access them by index
3. get the first element assuming there is only a single element in
the view
4. iterate thru the view
in the 1st case, we cannot assume the number of elements, so to be
python3 compatible, we should use `next(iter(a_dict))` instead.
in the 2nd case, in this change, the view is materialized using
`list(a_dict)`.
in the 3rd case, we can just continue using the short hand of
```py
(first_element,) = a_dict.keys()
```
to unpack the view. this works in both python2 and python3.
in the 4th case, the existing code works in both python2 and python3, as
both list and view can be iterated using `iter`, and `len` works as
well.
Signed-off-by: Kefu Chai <kchai@redhat.com>
2020-03-31 02:16:40 +00:00
|
|
|
(cclient, cconfig) = next(iter(config.items()))
|
2019-11-07 15:33:14 +00:00
|
|
|
engine = cconfig.get('engine')
|
2019-10-14 10:39:45 +00:00
|
|
|
prefix = cconfig.get('prefix')
|
2019-09-09 13:12:24 +00:00
|
|
|
secrets = cconfig.get('secrets')
|
|
|
|
if secrets is None:
|
|
|
|
raise ConfigError("No secrets specified, please specify some.")
|
|
|
|
|
|
|
|
for secret in secrets:
|
|
|
|
try:
|
2019-11-07 15:33:14 +00:00
|
|
|
path = secret['path']
|
2019-09-09 13:12:24 +00:00
|
|
|
except KeyError:
|
2019-11-07 15:33:14 +00:00
|
|
|
raise ConfigError('Missing "path" field in secret')
|
|
|
|
|
|
|
|
if engine == 'kv':
|
|
|
|
try:
|
|
|
|
data = {
|
|
|
|
"data": {
|
|
|
|
"key": secret['secret']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
except KeyError:
|
|
|
|
raise ConfigError('Missing "secret" field in secret')
|
|
|
|
elif engine == 'transit':
|
|
|
|
data = {"exportable": "true"}
|
|
|
|
else:
|
|
|
|
raise Exception("Unknown or missing secrets engine")
|
|
|
|
|
|
|
|
send_req(ctx, cconfig, cclient, urljoin(prefix, path), json.dumps(data))
|
2019-09-09 13:12:24 +00:00
|
|
|
|
|
|
|
log.info("secrets created")
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
def task(ctx, config):
|
|
|
|
"""
|
|
|
|
Deploy and configure Vault
|
|
|
|
|
|
|
|
Example of configuration:
|
|
|
|
|
|
|
|
tasks:
|
|
|
|
- vault:
|
|
|
|
client.0:
|
|
|
|
version: 1.2.2
|
|
|
|
root_token: test_root_token
|
2019-11-07 15:33:14 +00:00
|
|
|
engine: kv
|
|
|
|
prefix: /v1/kv/data/
|
2019-09-09 13:12:24 +00:00
|
|
|
secrets:
|
|
|
|
- path: kv/teuthology/key_a
|
|
|
|
secret: YmluCmJvb3N0CmJvb3N0LWJ1aWxkCmNlcGguY29uZgo=
|
|
|
|
- path: kv/teuthology/key_b
|
|
|
|
secret: aWIKTWFrZWZpbGUKbWFuCm91dApzcmMKVGVzdGluZwo=
|
|
|
|
"""
|
|
|
|
all_clients = ['client.{id}'.format(id=id_)
|
|
|
|
for id_ in teuthology.all_roles_of_type(ctx.cluster, 'client')]
|
|
|
|
if config is None:
|
|
|
|
config = all_clients
|
|
|
|
if isinstance(config, list):
|
|
|
|
config = dict.fromkeys(config)
|
|
|
|
|
|
|
|
overrides = ctx.config.get('overrides', {})
|
|
|
|
# merge each client section, not the top level.
|
2019-10-11 15:57:47 +00:00
|
|
|
for client in config.keys():
|
2019-09-09 13:12:24 +00:00
|
|
|
if not config[client]:
|
|
|
|
config[client] = {}
|
|
|
|
teuthology.deep_merge(config[client], overrides.get('vault', {}))
|
|
|
|
|
|
|
|
log.debug('Vault config is %s', config)
|
|
|
|
|
|
|
|
ctx.vault = argparse.Namespace()
|
|
|
|
ctx.vault.endpoints = assign_ports(ctx, config, 8200)
|
|
|
|
ctx.vault.root_token = None
|
2019-10-14 10:39:45 +00:00
|
|
|
ctx.vault.prefix = config[client].get('prefix')
|
2019-11-07 15:33:14 +00:00
|
|
|
ctx.vault.engine = config[client].get('engine')
|
2019-09-09 13:12:24 +00:00
|
|
|
|
|
|
|
with contextutil.nested(
|
|
|
|
lambda: download(ctx=ctx, config=config),
|
|
|
|
lambda: run_vault(ctx=ctx, config=config),
|
|
|
|
lambda: setup_vault(ctx=ctx, config=config),
|
|
|
|
lambda: create_secrets(ctx=ctx, config=config)
|
|
|
|
):
|
|
|
|
yield
|
|
|
|
|