Browse Source

Add Secure Clustering

* Add a connection-string based workflow to MicroStack;
  * microstack add-compute command can be run at the Control node in
    order to generate a connection string (an ASCII blob for the user);
  * the connection string contains:
    * an address of the control node;
    * a sha256 fingerprint of the TLS certificate used by the clustering
      service at the control node (which is used during verification
      similar to the Certificate Pinning approach);
    * an application credential id;
    * an application credential secret (short expiration time, reader
      role on the service project, restricted to listing the service
  * a MicroStack admin is expected to have ssh access to all nodes that
    will participate in a cluster - prior trust establishment is on
    them to figure out which is normal since they provision the nodes;
  * a MicroStack admin is expected to securely copy a connection string
    to a compute node via ssh. Since it is short-lived and does not
    carry service secrets, there is no risk of a replay at a later time;
  * If the compute role is specified during microstack.init, a
    connection string is requested and used to perform a request to the
    clustering service and validate the certificate fingerprint. The
    credential ID and secret are POSTed for verification to the
    clustering service which responds with the necessary config data
    for the compute node upon successful authorization.
* Set up TLS termination for the clustering service;
  * run the flask app as a UWSGI daemon behind nginx;
  * configure nginx to use a TLS certificate;
  * generate a self-signed TLS certificate.

This setup does not require PKI to be present for its own purposes of
joining compute nodes to the cluster. However, this does not mean that
PKI will not be used for TLS termination of the OpenStack endpoints.

Control node init workflow (non-interactive):

sudo microstack init --auto --control
microstack add-compute
<the connection string to be used at the compute node>

Compute node init workflow (non-interactive):

sudo microstack init --auto --compute --join <connection-string>

Change-Id: I9596fe1e6e5c1a325cc71fd3bf0c78b660b9a83e
Dmitrii Shcherbakov 6 months ago
25 changed files with 1077 additions and 185 deletions
  1. +2
  2. +22
  3. +12
  4. +11
  5. +20
  6. +0
  7. +1
  8. +1
  9. +2
  10. +44
  11. +1
  12. +8
  13. +122
  14. +81
  15. +259
  16. +1
  17. +75
  18. +59
  19. +54
  20. +201
  21. +19
  22. +8
  23. +13
  24. +48
  25. +13

+ 2
- 2 View File

@ -77,7 +77,7 @@ each other. Plus, you need a root password and other niceties. Run the
init script to set all of that up:
microstack.init --auto
microstack init --auto --control
(Note that you may leave --auto out at present. The init script will
@ -114,7 +114,7 @@ sudo systemctl restart snap.microstack.*
Create a test instance in your cloud.
`microstack.launch cirros --name test`
`microstack launch cirros --name test`
This will launch a machine using the built-in cirros image. Once the
machine is setup, verify that you can ping it, then tear it down.

+ 22
- 11
snap-overlay/bin/ View File

@ -10,7 +10,18 @@ from init import credentials
def _get_default_config():
snap_common = os.getenv('SNAP_COMMON')
return {
'config.clustered': False,
'': False,
'config.cluster.fingerprint': 'null',
'config.cluster.hostname': 'null',
'config.cluster.credential-id': 'null',
'config.cluster.credential-secret': 'null',
'': True,
'config.keystone.region-name': 'microstack',
'config.credentials.key-pair': '/home/{USER}/snap/{SNAP_NAME}'
@ -70,16 +81,16 @@ def _setup_secrets():
existing_cred_keys = []
k: credentials.generate_password() for k in [
f'config.credentials.{k}': credentials.generate_password() for k in [
] if k not in existing_cred_keys

+ 12
- 0
snap-overlay/snap-openstack.yaml View File

@ -19,6 +19,8 @@ setup:
- "{snap_common}/etc/nova/uwsgi/snap"
- "{snap_common}/etc/horizon/uwsgi/snap"
- "{snap_common}/etc/placement/uwsgi/snap"
- "{snap_common}/etc/cluster/tls"
- "{snap_common}/etc/cluster/uwsgi/snap"
- "{snap_common}/etc/rabbitmq"
- "{snap_common}/fernet-keys"
- "{snap_common}/lib"
@ -31,6 +33,7 @@ setup:
- "{snap_common}/etc/iscsi"
- "{snap_common}/etc/target"
cluster-nginx.conf.j2: "{snap_common}/etc/nginx/snap/sites-enabled/cluster.conf"
keystone-nginx.conf.j2: "{snap_common}/etc/nginx/snap/sites-enabled/keystone.conf"
keystone-snap.conf.j2: "{snap_common}/etc/keystone/keystone.conf.d/keystone-snap.conf"
neutron-snap.conf.j2: "{snap_common}/etc/neutron/neutron.conf.d/neutron-snap.conf"
@ -82,6 +85,9 @@ setup:
"{snap_common}/etc/microstack.rc": 0644
"{snap_common}/etc/microstack.json": 0644
is_clustered: ''
cluster_tls_cert_path: 'config.cluster.tls-cert-path'
cluster_tls_key_path: 'config.cluster.tls-key-path'
region_name: 'config.keystone.region-name'
keystone_password: 'config.credentials.keystone-password'
nova_password: 'config.credentials.nova-password'
@ -132,6 +138,12 @@ entry_points:
- "{snap_common}/etc/keystone/keystone.conf.d"
keystone-api.ini.j2: "{snap_common}/etc/keystone/uwsgi/snap/keystone-api.ini"
type: uwsgi
uwsgi-dir: "{snap_common}/etc/cluster/uwsgi/snap"
uwsgi-dir-override: "{snap_common}/etc/cluster/uwsgi"
cluster-api.ini.j2: "{snap_common}/etc/cluster/uwsgi/snap/cluster-api.ini"
type: nginx
config-file: "{snap_common}/etc/nginx/snap/nginx.conf"

+ 11
- 0
snap-overlay/templates/cluster-api.ini.j2 View File

@ -0,0 +1,11 @@
module = cluster.daemon:app
uwsgi-socket = {{ snap_common }}/run/cluster-api.sock
buffer-size = 65535
master = true
enable-threads = true
processes = 2
thunder-lock = true
lazy-apps = true
home = {{ snap }}/usr
pyargv = {{ pyargv }}

+ 20
- 0
snap-overlay/templates/cluster-nginx.conf.j2 View File

@ -0,0 +1,20 @@
server {
listen 10002 ssl;
error_log syslog:server=unix:/dev/log;
access_log syslog:server=unix:/dev/log;
{% if is_clustered %}
ssl_session_timeout 1d;
ssl_session_tickets off;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_certificate {{ cluster_tls_cert_path }};
ssl_certificate_key {{ cluster_tls_key_path }};
{% endif %}
location / {
include {{ snap }}/usr/conf/uwsgi_params;
uwsgi_param SCRIPT_NAME '';
uwsgi_pass unix://{{ snap_common }}/run/cluster-api.sock;

+ 0
- 10
snap-overlay/templates/clustering-nginx.conf View File

@ -1,10 +0,0 @@
server {
listen 8011;
error_log syslog:server=unix:/dev/log;
access_log syslog:server=unix:/dev/log;
location / {
include {{ snap }}/usr/conf/uwsgi_params;
uwsgi_param SCRIPT_NAME '';
uwsgi_pass unix://{{ snap_common }}/run/keystone-api.sock;

+ 1
- 1
snap-overlay/templates/nrpe.cfg.j2 View File

@ -23,7 +23,7 @@ command[check_swap]={{ snap }}/usr/lib/nagios/plugins/check_swap -n ok -w 5 -c 1
command[check_zombie_procs]={{ snap }}/usr/lib/nagios/plugins/check_procs -w 5 -c 10 -s Z
command[check_total_procs]={{ snap }}/usr/lib/nagios/plugins/check_procs -w 220 -c 300
command[check_rabbitmq_server]=python3 {{ snap }}/usr/lib/nagios/plugins/ snap.microstack.rabbitmq-server
command[check_cluster_server]=python3 {{ snap }}/usr/lib/nagios/plugins/ snap.microstack.cluster-server
command[check_cluster_server]=python3 {{ snap }}/usr/lib/nagios/plugins/ snap.microstack.cluster-uwsgi
#command[check_external_bridge]=python3 {{ snap }}/usr/lib/nagios/plugins/ snap.icrostack.external-bridge
command[check_glance_api]=python3 {{ snap }}/usr/lib/nagios/plugins/ snap.microstack.glance-api
command[check_horizon_uwsgi]=python3 {{ snap }}/usr/lib/nagios/plugins/ snap.microstack.horizon-uwsgi

+ 1
- 1
snap-wrappers/ovn/ovn-wrapper View File

@ -14,7 +14,7 @@ mkdir -p ${OVN_LOGDIR}
mkdir -p ${OVN_RUNDIR}
mkdir -p ${OVN_SYSCONFDIR}/ovn
if [ `basename $1` = 'ovn-ctl' -a `snapctl get config.clustered` == 'true' ]
if [ `basename $1` = 'ovn-ctl' -a `snapctl get` == 'true' ]
# TODO: replace this with a secure alternative once TLS is supported.
# Create an SB TCP socket to be used by remote ovn-controller and neutron-ovn-metadata

+ 2
- 0
snap/hooks/install View File

@ -53,6 +53,8 @@ snapctl stop --disable $SNAP_INSTANCE_NAME.filebeat
snapctl stop --disable $SNAP_INSTANCE_NAME.nrpe
snapctl stop --disable $SNAP_INSTANCE_NAME.telegraf
snapctl stop --disable $SNAP_INSTANCE_NAME.cluster-uwsgi
mkdir -p $SNAP_DATA/lib/libvirt/images
mkdir -p ${SNAP_COMMON}/log/libvirt/qemu
# NOTE(dmitriis): there is currently no way to make sure this directory gets

+ 44
- 8
snapcraft.yaml View File

@ -67,6 +67,21 @@ apps:
- network
# TODO: - microstack-support
# A proxy command to avoid calling <namespace>.<command>.
# TODO: potentially remove the individual commands completely in favor of this.
command: bin/microstack
- network
- mount-observe
- network-bind
- firewall-control
- network-control
- ssh-keys
- system-observe
- hardware-observe
# TODO: - microstack-support
# OpenStack Service Configuration
command: bin/microstack_init
@ -81,6 +96,11 @@ apps:
- hardware-observe # rabbitmq ?
# TODO: - microstack-support
command: bin/microstack_add_compute
- network
# Keystone
command: bin/snap-openstack launch keystone-uwsgi
@ -600,16 +620,13 @@ apps:
# TODO: - microstack-support
# Cluster
command: bin/flask run -p 10002 --host # TODO: run as a uwsgi app
command: bin/snap-openstack launch cluster-uwsgi
daemon: simple
LC_ALL: C.UTF-8 # Makes flask happy
LANG: C.UTF-8 # Makes flask happy
FLASK_APP: $SNAP/lib/python3.8/site-packages/cluster/
- network
- network-bind
# TODO: - microstack-support
command: bin/telegraf
@ -686,11 +703,11 @@ parts:
- uwsgi
- git+
- neutron
- horizon
- python-cinderclient
- python-openstackclient
- python-swiftclient
@ -1453,6 +1470,25 @@ parts:
plugin: python
source: tools/microstack
# note(dmitriis) in order to avoid conflicts about lib64/
# with other parts.
- libc6
build-environment: *python-build-environment
override-build: |
snapcraftctl build
`find $SNAPCRAFT_PART_INSTALL -name '__pycache__' | xargs rm -r`
`find $SNAPCRAFT_PART_INSTALL -name 'RECORD' | xargs rm`
rm $SNAPCRAFT_PART_INSTALL/bin/activate.csh
# Clustering client and server
plugin: python

+ 1
- 1
tests/ View File

@ -118,7 +118,7 @@ class Host():
def init(self, args=['--auto']):
print(f"Initializing the snap with {args}")
check(*self.prefix, 'sudo', 'microstack.init', *args)
check(*self.prefix, 'sudo', 'microstack', 'init', *args)
def multipass(self):
self.machine = petname.generate()

+ 8
- 3
tests/ View File

@ -38,16 +38,21 @@ class TestBasics(Framework):
prefix = host.prefix
endpoints = check_output(
*prefix, '/snap/bin/microstack.openstack', 'endpoint', 'list')
# Endpoints should be listening on
self.assertTrue("" in endpoints)
control_ip = check_output(
*prefix, 'sudo', 'snap', 'get',
'microstack', ''
# Endpoints should contain the control IP.
self.assertTrue(control_ip in endpoints)
# Endpoints should not contain localhost
self.assertFalse("localhost" in endpoints)

+ 122
- 0
tools/cluster/cluster/ View File

@ -0,0 +1,122 @@
#!/usr/bin/env python3
import uuid
import secrets
import argparse
from datetime import datetime
from dateutil.relativedelta import relativedelta
from oslo_serialization import (
from import config_get
from keystoneauth1.identity import v3
from keystoneauth1 import session
from keystoneclient.v3 import client
VALIDITY_PERIOD = relativedelta(minutes=20)
def _create_credential():
project_name = 'service'
domain_name = 'default'
# TODO: add support for TLS-terminated Keystone once this is supported.
auth = v3.password.Password(
sess = session.Session(auth=auth)
keystone_client = client.Client(session=sess)
# Only allow this credential to list the Keystone catalog. After it
# expires, Keystone will return Unauthorized for requests made with tokens
# issued from that credential.
access_rules = [{
'method': 'GET',
'path': '/v3/auth/catalog',
'service': 'identity'
# TODO: make the expiration time customizable since this may be used by
# automation or during live demonstrations where the lag between issuance
# and usage may be more than the expiration time.
expires_at = + VALIDITY_PERIOD
# Role objects themselves are not tied to a specific domain by default
# - this does not affect role assignments themselves which are scoped.
reader_role = keystone_client.roles.find(name='reader', domain_id=None)
return keystone_client.application_credentials.create(
# Do not allow this app credential to create new app credentials.
# Make the secret shorter than the default but secure enough.
def add_compute():
"""Generates connection string for adding a compute node to the cluster.
* Make sure we are running in the clustered mode and this is a control
node which is an initial node in the cluster;
* Generate an application credential via Keystone scoped to the service
project with restricted capabilities (reader role and only able to list
the service catalog) and a short expiration time enough for a user to
copy the connection string to the compute node;
* Get an FQDN that will be used by the client to establish a connection to
the clustering service;
* Serialize the above data into a base64-encoded string.
role = config_get('config.cluster.role')
if role != 'control':
raise Exception('Running add-compute is only supported on a'
' control node.')
app_cred = _create_credential()
data = {
# TODO: we do not use hostname verification, however, using
# an FQDN might be useful here since the host may be behind NAT
# with a split-horizon DNS implemented where a hostname would point
# us to a different IP.
'hostname': config_get(''),
# Store bytes since the representation will be shorter than with hex.
'fingerprint': bytes.fromhex(config_get('config.cluster.fingerprint')),
'secret': app_cred.secret,
connection_string = base64.encode_as_text(msgpackutils.dumps(data))
# Print the connection string and an expiration notice to the user.
print('Use the following connection string to add a new compute node'
f' to the cluster (valid for {VALIDITY_PERIOD.minutes} minutes from'
f' this moment):\n{connection_string}')
def main():
parser = argparse.ArgumentParser(
This command does not have subcommands - just run it to get a connection string
to be used when joining a node to the cluster.
if __name__ == '__main__':

+ 81
- 18
tools/cluster/cluster/ View File

@ -1,37 +1,100 @@
#!/usr/bin/env python3
import urllib3
import json
import requests
from cluster import shell
from import check_output
class UnauthorizedRequestError(Exception):
def join():
"""Join an existing cluster as a compute node."""
config = json.loads(check_output('snapctl', 'get', 'config'))
password = config['cluster']['password']
control_ip = config['network']['control-ip']
my_ip = config['network']['compute-ip']
cluster_config = shell.config_get('config.cluster')
control_hostname = cluster_config['hostname']
fingerprint = cluster_config['fingerprint']
credential_id = cluster_config['credential-id']
credential_secret = cluster_config['credential-secret']
request_body = json.dumps({
'credential-id': credential_id,
'credential-secret': credential_secret
# Create a connection pool and override the TLS certificate
# verification method to use the certificate fingerprint instead
# of hostname validation + validation via CA cert and expiration time.
# This avoids relying on any kind of PKI and DNS assumptions in the
# installation environment.
# If the fingerprint does not match, MaxRetryError will be raised
# with SSLError as a cause even with the rest of the checks disabled.
conn_pool = urllib3.HTTPSConnectionPool(
control_hostname, CLUSTER_SERVICE_PORT,
assert_fingerprint=fingerprint, assert_hostname=False,
resp = conn_pool.urlopen(
'POST', '/join', retries=0, preload_content=True,
'API-VERSION': '1.0.0',
'Content-Type': 'application/json',
}, body=request_body)
except urllib3.exceptions.MaxRetryError as e:
if isinstance(e.reason, urllib3.exceptions.SSLError):
raise Exception(
'The actual clustering service certificate fingerprint'
' did not match the expected one, please make sure that: '
'(1) that a correct token was specified during initialization;'
' (2) a MITM attacks are not performed against HTTPS requests'
' (including transparent proxies).'
) from e.reason
raise Exception('Could not retrieve a response from the clustering'
' service.') from e
if not password:
raise Exception("No cluster password specified!")
if resp.status == 401:
response_data ='utf-8')
# TODO: this should be more bulletproof in case a proxy server
# returns this response - it will not have the expected format.
print('An authorization failure has occurred while joining the'
' the cluster: please make sure the connection string'
' was entered as returned by the "add-compute" command'
' and that it was used before its expiration time.')
if response_data:
message = json.loads(response_data)['message']
raise UnauthorizedRequestError(message)
raise UnauthorizedRequestError()
if resp.status != 200:
raise Exception('Unexpected response status received from the'
f' clustering service: {resp.status}')
resp =
json={'password': password, 'ip_address': my_ip})
if resp.status_code != 200:
# TODO better error and formatting.
raise Exception('Failed to get info from control node: {}'.format(
resp = resp.json()
response_data ='utf-8')
except UnicodeDecodeError:
raise Exception('The response from the clustering service contains'
' bytes invalid for UTF-8')
if not response_data:
raise Exception('The response from the clustering service is empty'
' which is unexpected: please check its status'
' and file an issue if the problem persists')
credentials = resp['config']['credentials']
# Load the response assuming it has the correct format. API versioning
# should rule out inconsistencies, otherwise we will get an error here.
response_dict = json.loads(response_data)
credentials = response_dict['config']['credentials']
control_creds = {f'config.credentials.{k}': v
for k, v in credentials.items()}
# TODO: use the hostname from the connection string instead to
# resolve an IP address (requires a valid DNS setup).
control_ip = response_dict['config']['network']['control-ip']
shell.config_set(**{'': control_ip})
if __name__ == '__main__':

+ 259
- 23
tools/cluster/cluster/ View File

@ -1,51 +1,287 @@
import logging
import json
from flask import Flask, request
import semantic_version
import keystoneclient.exceptions as kc_exceptions
from flask import Flask, request, jsonify
from werkzeug.exceptions import BadRequest
from import check_output
from keystoneauth1.identity import v3
from keystoneauth1 import session
from keystoneclient.v3 import client as v3client
logger = logging.getLogger(__name__)
app = Flask(__name__)
API_VERSION = semantic_version.Version('1.0.0')
class Unauthorized(Exception):
def join_info(password, ip_address):
our_password = check_output('snapctl', 'get', 'config.cluster.password')
class APIException(Exception):
status_code = None
message = ''
def to_dict(self):
return {'message': self.message}
class APIVersionMissing(APIException):
status_code = 400
message = 'An API version was not specified in the request.'
class APIVersionInvalid(APIException):
status_code = 400
message = 'Invalid API version was specified in the request.'
class APIVersionDropped(APIException):
status_code = 410
message = 'The requested join API version is no longer supported.'
class APIVersionNotImplemented(APIException):
status_code = 501
message = 'The requested join API version is not yet implemented.'
class InvalidJSONInRequest(APIException):
status_code = 400
message = 'The request includes invalid JSON.'
class IncorrectContentType(APIException):
status_code = 400
message = ('The request does not have a Content-Type header set to '
class MissingAuthDataInRequest(APIException):
status_code = 400
message = 'The request does not have the required authentication data.'
class InvalidAuthDataFormatInRequest(APIException):
status_code = 400
message = 'The authentication data in the request has invalid format.'
class InvalidAuthDataInRequest(APIException):
status_code = 400
message = 'The authentication data in the request is invalid.'
class AuthorizationFailed(APIException):
status_code = 401
message = ('Failed to pass authorization using the data provided in the'
' request')
class UnexpectedError(APIException):
status_code = 500
message = ('The clustering server has encountered an unexpected'
' error while handling the request.')
def _handle_api_version_exception(error):
response = jsonify(error.to_dict())
response.status_code = error.status_code
return response
def handle_api_version_missing(error):
return _handle_api_version_exception(error)
def handle_api_version_invalid(error):
return _handle_api_version_exception(error)
if password.strip() != our_password.strip():
raise Unauthorized()
# Load config
def handle_api_version_dropped(error):
return _handle_api_version_exception(error)
def handle_api_version_not_implemented(error):
return _handle_api_version_exception(error)
def handle_incorrect_content_type(error):
return _handle_api_version_exception(error)
def handle_invalid_json_in_request(error):
return _handle_api_version_exception(error)
def handle_invalid_auth_data_format_in_request(error):
return _handle_api_version_exception(error)
def handle_invalid_auth_data_in_request(error):
return _handle_api_version_exception(error)
def handle_authorization_failed(error):
return _handle_api_version_exception(error)
def handle_unexpected_error(error):
return _handle_api_version_exception(error)
def join_info():
"""Generate the configuration information to return to a client."""
# TODO: be selective about what we return. For now, we just get everything.
config = json.loads(check_output('snapctl', 'get', 'config'))
info = {'config': config}
return info
@app.route('/join', methods=['POST'])
def join():
"""Authorize a client node and return relevant config."""
# Retrieve an API version from the request - it is a mandatory
# header for this API.
request_version = request.headers.get('API-Version')
if request_version is None:
logger.debug('The client has not specified the API-version header.')
raise APIVersionMissing()
api_version = semantic_version.Version(request_version)
except ValueError:
logger.debug('The client has specified an invalid API version.'
f': {request_version}')
raise APIVersionInvalid()
# Compare the API version used by the clustering service with the
# one specified in the request and return an appropriate response.
if api_version.major > API_VERSION.major:
logger.debug('The client requested a version that is not'
f' supported yet: {api_version}.')
raise APIVersionNotImplemented()
elif api_version.major < API_VERSION.major:
logger.debug('The client request version is no longer supported'
f': {api_version}.')
raise APIVersionDropped()
# Flask raises a BadRequest if the JSON content is invalid and
# returns None if the Content-Type header is missing or not set
# to application/json.
req_json = request.json
except BadRequest:
logger.debug('The client has POSTed an invalid JSON'
' in the request.')
raise InvalidJSONInRequest()
if req_json is None:
logger.debug('The client has not specified the application/json'
' content type in the request.')
raise IncorrectContentType()
# So far we don't have any minor versions with backwards-compatible
# changes so just assume that all data will be present or error out.
credential_id = req_json.get('credential-id')
credential_secret = req_json.get('credential-secret')
if not credential_id or not credential_secret:
logger.debug('The client has not specified the required'
' authentication data in the request.')
return MissingAuthDataInRequest()
# TODO: handle https here when TLS termination support is added.
keystone_base_url = 'http://localhost:5000/v3'
# In an unlikely event of failing to construct an auth object
# treat it as if invalid data got passed in terms of responding
# to the client.
auth = v3.ApplicationCredential(
except Exception:
logger.exception('An exception has occurred while trying to build'
' an auth object for an application credential'
' passed from the clustering client.')
raise InvalidAuthDataInRequest()
# Use the auth object with the app credential to create a session
# which the Keystone client will use.
sess = session.Session(auth=auth)
except Exception:
logger.exception('An exception has occurred while trying to build'
' a Session object with auth data'
' passed from the clustering client.')
raise UnexpectedError()
keystone_client = v3client.Client(session=sess)
except Exception:
logger.exception('An exception has occurred while trying to build'
' a Keystone Client object with auth data'
' passed from the clustering client.')
raise UnexpectedError()
# The add-compute command creates application credentials that
# allow access to /v3/auth/catalog with an expiration time.
# Authorization failures occur after an app credential expires
# in which case an error is returned to the client.
except (kc_exceptions.AuthorizationFailure,
logger.exception('Failed to get a Keystone token'
' with the application credentials'
' passed from the clustering client.')
raise AuthorizationFailed()
except ValueError:
logger.exception('Insufficient amount of parameters were'
' used in the request to Keystone.')
raise UnexpectedError()
except kc_exceptions.ConnectionError:
logger.exception('Failed to connect to Keystone')
raise UnexpectedError()
except kc_exceptions.SSLError:
logger.exception('A TLS-related error has occurred while'
' connecting to Keystone')
raise UnexpectedError()
# We were able to authenticate against Keystone using the
# application credential and verify that it has not expired
# so the information for a compute node to join the cluster can
# now be returned.
return json.dumps(join_info())
def home():
status = {
'status': 'running',
'info': 'Microstack clustering daemon.'
'info': 'MicroStack clustering daemon.'
return json.dumps(status)
@app.route('/join', methods=['POST'])
def join():
req = request.json # TODO: better error messages on failed parse.
password = req.get('password')
ip_address = req.get('ip_address')
if not password:
return 'No password specified', 500
return json.dumps(join_info(password, ip_address))
except Unauthorized:
return (json.dumps({'error': 'Incorrect password.'}), 500)

+ 1
- 0
tools/cluster/ View File

@ -8,6 +8,7 @@ setup(
'console_scripts': [
'microstack_join = cluster.client:join',
'microstack_add_compute = cluster.add_compute:main',

+ 75
- 0
tools/init/init/ View File

@ -0,0 +1,75 @@
#!/usr/bin/env python3
from pathlib import Path
from datetime import datetime
from dateutil.relativedelta import relativedelta
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography import x509
from cryptography.x509.oid import NameOID
from init import shell
def generate_selfsigned():
"""Generate a self-signed certificate with associated keys.
The certificate will have a fake CNAME and subjAltName since
the expectation is that this certificate will only be used by
clients that know its fingerprint and will not use a validation
via a CA certificate and hostname. This approach is similar to
Certificate Pinning, however, here a certificate is not embedded
into the application but is generated on initialization at one
node and its fingerprint is copied in a token to another node
via a secure channel.
cert_path, key_path = (
# Do not generate a new certificate and key if there is already an existing
# pair. TODO: improve this check and allow renewal.
if cert_path.exists() and key_path.exists():
dummy_cn = ''
key = rsa.generate_private_key(
common_name = x509.Name([
x509.NameAttribute(NameOID.COMMON_NAME, dummy_cn)
san = x509.SubjectAlternativeName([x509.DNSName(dummy_cn)])
basic_contraints = x509.BasicConstraints(ca=True, path_length=0)
now = datetime.utcnow()
cert = (
.not_valid_after(now + relativedelta(years=10))
.add_extension(basic_contraints, False)
.add_extension(san, False)
.sign(key, hashes.SHA256(), default_backend())
cert_fprint = cert.fingerprint(hashes.SHA256()).hex()
shell.config_set(**{'config.cluster.fingerprint': cert_fprint})
serialized_cert = cert.public_bytes(encoding=serialization.Encoding.PEM)
serialized_key = key.private_bytes(

+ 59
- 24
tools/init/init/ View File

@ -31,15 +31,18 @@ limitations under the License.
import argparse
import logging
import secrets
import string
import sys
import socket
from functools import wraps
from init.config import log
from import default_network, check, check_output
from import (
from init import questions
@ -69,12 +72,18 @@ def parse_init_args():
parser = argparse.ArgumentParser()
parser.add_argument('--auto', '-a', action='store_true',
help='Run non interactively.')
parser.add_argument('--join', '-j',
help='Pass a connection string generated by the'
' add-compute command at the control node'
' (required for compute nodes, unused for control'
' nodes).')
parser.add_argument('--compute', action='store_true')
parser.add_argument('--control', action='store_true')
parser.add_argument('--debug', action='store_true')
help='(experimental) set up a loop device-backed'
' LVM backend for Cinder.'
@ -94,41 +103,42 @@ def process_init_args(args):
values in our snap config, based on those args.
auto = or args.control or args.compute
if and not (args.control or args.compute):
raise ValueError('A role (--compute or --control) must be specified '
' when using --auto')
if args.compute or args.control:
check('snapctl', 'set', 'config.clustered=true')
config_set(**{'': 'true'})
if args.compute:
check('snapctl', 'set', 'config.cluster.role=compute')
config_set(**{'config.cluster.role': 'compute'})
if args.control:
# If both compute and control are passed for some reason, we
# wind up with the role of 'control', which is best, as a
# control node also serves as a compute node in our hyper
# converged architecture.
check('snapctl', 'set', 'config.cluster.role=control')
config_set(**{'config.cluster.role': 'control'})
if args.cluster_password:
check('snapctl', 'set', 'config.cluster.password={}'.format(
if args.connection_string:
'config.cluster.connection-string.raw': args.connection_string})
if auto and not args.cluster_password:
alphabet = string.ascii_letters + string.digits
password = ''.join(secrets.choice(alphabet) for i in range(10))
check('snapctl', 'set', 'config.cluster.password={}'.format(
if and not args.control and not args.connection_string:
raise ValueError('The connection string parameter must be specified'
' for compute nodes.')
if args.debug:
check('snapctl', 'set',
check('snapctl', 'set',
return auto
@ -136,8 +146,13 @@ def init() -> None:
args = parse_init_args()
auto = process_init_args(args)
# Do not ask about this if a CLI argument asking for it has been
# provided already.
cinder_lvm_question = questions.CinderVolumeLVMSetup()
if args.setup_loop_based_cinder_lvm_backend:
cinder_lvm_question.interactive = False
question_list = [
@ -157,11 +172,31 @@ def init() -> None:
clustering_question = questions.Clustering()
# If the connection string is specified we definitely
# want to set up clustering and we don't need to ask.
if args.connection_string:
clustering_question.interactive = False
if args.control:
raise ValueError('Joining additional control nodes is'
' not supported.')
elif args.compute:
clustering_question.role_interactive = False
config_set(**{'': True})
clustering_question.connection_string_interactive = False
if args.control or args.compute:
clustering_question.role_interactive = False
# The same code-path as for other questions will be executed.
question_list.insert(0, clustering_question)
for question in question_list:
if auto:
# Force all questions to be non-interactive if we passed --auto.

+ 54
- 41
tools/init/init/questions/ View File

@ -31,6 +31,7 @@ from init import shell
from import (check, call, check_output, sql, nc_wait, log_wait,
start, restart, download, disable, enable)
from init.config import Env, log
from init import cluster_tls
from init.questions.question import Question
from init.questions import clustering, network, uninstall # noqa F401
@ -46,66 +47,68 @@ class ConfigError(Exception):
class Clustering(Question):
"""Possibly setup clustering."""
"""Possibly configure clustering."""
_type = 'boolean'
_question = 'Do you want to setup clustering?'
config_key = 'config.clustered'
_question = 'Would you like to configure clustering?'
config_key = ''
interactive = True
# Overrides to be used when options are explicitly specified via
# command-line arguments.
connection_string_interactive = True
role_interactive = True
def yes(self, answer: bool):'Configuring clustering ...')
role_question = clustering.Role()
if not (self.interactive and self.role_interactive):
role_question.interactive = False
questions = [
# Skipped for the compute role and is automatically taken
# from the connection string.
clustering.ComputeIp(), # Automagically skipped role='control'
# Skipped for the control role since it is identical to the
# control node IP.
for question in questions:
if not self.interactive:
question.interactive = False
role = check_output('snapctl', 'get', 'config.cluster.role')
control_ip = check_output('snapctl', 'get',
password = check_output('snapctl', 'get', 'config.cluster.password')
log.debug('Role: {}, IP: {}, Password: {}'.format(
role, control_ip, password))
connection_string_question = clustering.ConnectionString()
if not (self.interactive and self.connection_string_interactive):
connection_string_question.interactive = False
# TODO: raise an exception if any of the above are None (can
# happen if we're automatig and mess up our params.)
role = shell.config_get('config.cluster.role')
if role == 'compute':'I am a compute node.')'Setting up as a compute node.')
# Gets config info and sets local env vals.
# Set default question answers.
check('snapctl', 'set', '')
check('snapctl', 'set', '')
'': 'false',
'': 'true',
if role == 'control':'I am a control node.')
check('snapctl', 'set', '')
# We want to run a hypervisor on our control plane nodes
# -- this is essentially a hyper converged cloud.
check('snapctl', 'set', '')
# TODO: if this is run after init has already been called,
# need to restart services.'Setting up as a control node.')
'': 'true',
'': 'true',
# Generate a self-signed certificate for the clustering service.
# Write templates
check('snap-openstack', 'setup')
def no(self, answer: bool):
# Turn off cluster server
# TODO: it would be more secure to reverse this -- only enable
# to service if we are doing clustering.
class ConfigQuestion(Question):
@ -554,6 +557,10 @@ class NovaControlPlane(Question):
check('openstack', 'role', 'add', '--project',
'service', '--user', 'nova', 'admin')
# Assign the reader role to the nova user so that read-only
# application credentials can be created.
check('openstack', 'role', 'add', '--project',
'service', '--user', 'nova', 'reader')
# Use snapctl to start nova services. We need to call them
# out manually, because systemd doesn't know about them yet.
@ -680,8 +687,8 @@ class CinderVolumeLVMSetup(Question):
_type = 'boolean'
config_key = 'config.cinder.setup-loop-based-cinder-lvm-backend'
_question = ('(experimental) Do you want to setup a loop device-backed LVM'
' volume backend for Cinder?')
_question = ('(experimental) Would you like to setup a loop device-backed'
' LVM volume backend for Cinder?')
interactive = True
def yes(self, answer: bool) -> None:
@ -780,6 +787,7 @@ class NeutronControlPlane(Question):
# Disable the other services.
for service in [
@ -904,7 +912,6 @@ class PostSetup(Question):
config_key = ''
def yes(self, answer: str) -> None:'restarting libvirt and virtlogd ...')
# This fixes an issue w/ logging not getting set.
# TODO: fix issue.
@ -912,7 +919,13 @@ class PostSetup(Question):
role = shell.config_get('config.cluster.role')
if role == 'control':
# TODO: since snap-openstack launch is used, this depends on the
# database readiness and hence the clustering service is enabled
# and started here. There needs to be a better way to do this.
check('snapctl', 'set', 'initialized=true')'Complete. Marked microstack as initialized!')
@ -936,7 +949,7 @@ class SimpleServiceQuestion(Question):
class ExtraServicesQuestion(Question):
_type = 'boolean'
_question = 'Do you want to setup extra services?'
_question = 'Would you like to setup extra services?'
config_key = ''
interactive = True
@ -958,7 +971,7 @@ class ExtraServicesQuestion(Question):
class Filebeat(SimpleServiceQuestion):
_type = 'boolean'
_question = 'Do you want to enable Filebeat?'
_question = 'Would you like to enable Filebeat?'
config_key = ''
interactive = True
@ -971,7 +984,7 @@ class Filebeat(SimpleServiceQuestion):
class Telegraf(SimpleServiceQuestion):
_type = 'boolean'
_question = 'Do you want to enable Telegraf?'
_question = 'Would you like to enable Telegraf?'
config_key = ''
interactive = True
@ -984,7 +997,7 @@ class Telegraf(SimpleServiceQuestion):
class Nrpe(SimpleServiceQuestion):
_type = 'boolean'
_question = 'Do you want to enable NRPE?'
_question = 'Would you like to enable NRPE?'
config_key = ''
interactive = True

+ 201
- 28
tools/init/init/questions/ View File

@ -1,13 +1,32 @@
from getpass import getpass
import logging
import msgpack
import re
import netaddr
from cryptography.hazmat.primitives import hashes
from typing import Tuple
from init.questions.question import Question, InvalidAnswer
from import check, check_output, fetch_ip_address
from import (
from oslo_serialization import (
logger = logging.getLogger(__name__)
class Role(Question):
_type = 'string'
config_key = 'config.cluster.role'
_question = "What is this machines' role? (control/compute)"
_question = ('Which role would you like to use for this node:'
' "control" or "compute"?')
_valid_roles = ('control', 'compute')
interactive = True
@ -20,33 +39,181 @@ class Role(Question):
if role in self._valid_roles:
return role
print('Role must be either "control" or "compute"')
print('The role must be either "control" or "compute".')
raise InvalidAnswer('Too many failed attempts.')
class Password(Question):
_type = 'string' # TODO: type password support
config_key = 'config.cluster.password'
_question = 'Please enter a cluster password > '
class ConnectionString(Question):
_type = 'string'
config_key = 'config.cluster.connection-string.raw'
_question = ('Please enter a connection string returned by the'
' add-compute command > ')
interactive = True
def _input_func(self, prompt):
if not self.interactive:
def _validate(self, answer: str) -> Tuple[str, bool]:
conn_str_bytes = base64.decode_as_bytes(
except TypeError:
print('The connection string contains non-ASCII'
' characters please make sure you entered'
' it as returned by the add-compute command.')
return answer, False
# Get rid of 'default=' string the parent class has added to prompt.
prompt = self._question
conn_info = msgpackutils.loads(conn_str_bytes)
except msgpack.exceptions.ExtraData:
print('The connection string contains extra data'
' characters please make sure you entered'
' it as returned by the add-compute command.')
return answer, False
except ValueError:
print('The connection string contains extra data'
' characters please make sure you entered'
' it as returned by the add-compute command.')
return answer, False
except msgpack.exceptions.FormatError:
print('The connection string format is invalid'
' please make sure you entered'
' it as returned by the add-compute command.')
return answer, False
except Exception:
print('An unexpeted error has occured while trying'
' to decode the connection string. Please'
' make sure you entered it as returned by'
' the add-compute command and raise an'
' issue if the error persists')
return answer, False
for _ in range(0, 3):
password0 = getpass(prompt)
password1 = getpass('Please re-enter password > ')
if password0 == password1:
return password0
# Perform token field validation as well so that the rest of
# the code-base can assume valid input.
# The input can be either an IPv4 or IPv6 address or a hostname.
hostname = conn_info.get('hostname')
is_valid_address = self._validate_address(hostname)
is_valid_address = True
except ValueError:
logger.debug('The hostname specified in the connection string is'
' not an IPv4 or IPv6 address - treating it as'
' a hostname.')
is_valid_address = False
if not is_valid_address:
except ValueError as e:
print(f'The hostname {hostname} provided in the connection'
f' string is invalid: {str(e)}')
return answer, False
print("Passwords don't match!")
fingerprint = conn_info.get('fingerprint')
except ValueError as e:
print('The clustering service TLS certificate fingerprint provided'
f' in the connection string is invalid: {str(e)}')
return answer, False
raise InvalidAnswer('Too many failed attempts.')
credential_id = conn_info.get('id')
except ValueError as e:
print('The credential id provided in the connection string is'
f' invalid: {str(e)}')
return answer, False
credential_secret = conn_info.get('secret')
except ValueError as e:
print('The credential secret provided in the connection string is'
f' invalid: {str(e)}')
return answer, False
self._conn_info = conn_info
return answer, True
def _validate_hostname(self, hostname):
if hostname is None:
raise ValueError('A hostname has not been provided.')
if len(hostname) == 0:
raise ValueError('An empty hostname is invalid.')
# Remove the trailing dot as it does not count to the following
# length limit check.
if hostname.endswith('.'):
name = hostname[:-1]
name = hostname
# See
# 255 - octet limit, 253 - visible hostname limit (without
# a trailing dot. The limit is also documented in hostname(7).
if len(name) > 253:
raise ValueError('The specified hostname is too long.')
allowed = re.compile('(?!-)[A-Z0-9-]{1,63}(?<!-)$', re.IGNORECASE)
if not'[a-zA-Z-]', name.split(".")[-1]):
raise ValueError(f'{hostname} contains no non-numeric characters'
' in the top-level domain part of the hostname.')
if any((not allowed.match(x)) for x in name.split('.')):
raise ValueError('{hostname} is an invalid hostname.')
def _validate_address(self, address):
if address is None:
raise ValueError('An address has not been provided.')
if not (netaddr.valid_ipv4(address, netaddr.core.INET_PTON) or
netaddr.valid_ipv6(address, netaddr.core.INET_PTON)):
raise ValueError(f'{address} is not a valid IPv4 or IPv6 address.')
def _validate_fingerprint(self, fingerprint):
# We expect a byte sequence equal to the SHA256 hash of the cert.
actual_len = len(fingerprint)
expected_len = hashes.SHA256.digest_size
if not actual_len == expected_len:
raise ValueError('The provided fingerprint has an invalid '
f'length: {actual_len}, expected: {expected_len}')
def _validate_credential_id(self, credential_id):
if credential_id is None:
raise ValueError('A credential id has not been provided.')
# We expect a UUID (rfc4122) without dashes.
actual_len = len(credential_id)
if actual_len != UUID_LEN:
raise ValueError('The credential length is not equal to a length'
'of a UUID without dashes:'
f'actual: {actual_len}, expected: {UUID_LEN}')
def _validate_credential_secret(self, credential_secret):
if credential_secret is None:
raise ValueError('A credential secret has not been provided.')
# The artificial secret length controlled by the MicroStack code-base.
actual_len = len(credential_secret)
if actual_len != SECRET_LEN:
raise ValueError('The credential secret has an unexpected length:'
f'actual: {actual_len}, expected: {SECRET_LEN}')
def after(self, answer: str) -> None:
# Store the individual parts of the connection string in the snap
# config for easy access and avoidance of extra parsing.
prefix = 'config.cluster'
f'{prefix}.hostname': self._conn_info['hostname'],
f'{prefix}.fingerprint': self._conn_info['fingerprint'].hex(),
f'{prefix}.credential-id': self._conn_info['id'],
f'{prefix}.credential-secret': self._conn_info['secret'],
def ask(self):
# Skip this question for a control node since we are not connecting
# to ourselves.
role = config_get(Role.config_key)
if role == 'control':
return super().ask()