Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 74 additions & 1 deletion .github/workflows/build-snap.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: vaultlocker snap
name: vaultlocker snap build and VM tests

on:
- pull_request
Expand Down Expand Up @@ -36,3 +36,76 @@ jobs:

- name: Verify snap help
run: vaultlocker -h

test-snap-vm:
name: Test snap in VM
needs: build
runs-on: ubuntu-24.04
timeout-minutes: 60
env:
VAULTLOCKER_TEST_VM: vaultlocker-vm
LXD_STORAGE_POOL: default
LXD_STORAGE_VOLUME: vaultlocker-scratch
steps:
- name: Check out Git repository
uses: actions/checkout@v7

- name: Download snap artifact
uses: actions/download-artifact@v8
with:
name: snap
path: snap-artifact

- name: Set up Python
uses: actions/setup-python@v7
with:
python-version: '3.12'

- name: Install test dependencies
run: |
pip install -r requirements-noble.txt
pip install -r test-requirements.txt
pip install .

- name: Set up LXD
uses: canonical/setup-lxd@v1

- name: Create VM and scratch disk
run: |
lxc storage volume create "$LXD_STORAGE_POOL" "$LXD_STORAGE_VOLUME" \
--type=block size=512MiB
lxc launch ubuntu:24.04 "$VAULTLOCKER_TEST_VM" --vm \
--config limits.cpu=2 --config limits.memory=4GiB
lxc storage volume attach "$LXD_STORAGE_POOL" "$LXD_STORAGE_VOLUME" \
"$VAULTLOCKER_TEST_VM"
timeout 300 bash -c \
'until lxc exec "$VAULTLOCKER_TEST_VM" -- true; do sleep 5; done'
lxc exec "$VAULTLOCKER_TEST_VM" -- cloud-init status --wait
lxc exec "$VAULTLOCKER_TEST_VM" -- apt-get update
lxc exec "$VAULTLOCKER_TEST_VM" -- apt-get install -y \
cryptsetup dmsetup curl snapd

mapfile -t scratch_devices < <(
lxc exec "$VAULTLOCKER_TEST_VM" -- lsblk -bndo PATH,TYPE,SIZE |
awk '$2 == "disk" && $3 == 536870912 {print $1}'
)
test "${#scratch_devices[@]}" -eq 1
lxc exec "$VAULTLOCKER_TEST_VM" -- test -b "${scratch_devices[0]}"
echo "VAULTLOCKER_TEST_DEVICE=${scratch_devices[0]}" >> "$GITHUB_ENV"

- name: Run snap VM functional tests
run: |
snap_path="$(find "$GITHUB_WORKSPACE/snap-artifact" -maxdepth 1 \
-name '*.snap' -print -quit)"
test -n "$snap_path"
VAULTLOCKER_SNAP_PATH="$snap_path" \
python -c 'from vaultlocker.tests.functional.snap.base import snap_tests_enabled; assert snap_tests_enabled()'
VAULTLOCKER_SNAP_PATH="$snap_path" \
stestr run --serial '^vaultlocker.tests.functional.snap.*'

- name: Clean up VM
if: always()
run: |
lxc delete "$VAULTLOCKER_TEST_VM" --force || true
lxc storage volume delete "$LXD_STORAGE_POOL" \
"$LXD_STORAGE_VOLUME" || true
16 changes: 16 additions & 0 deletions snap/snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,27 @@ parts:
craftctl set version="${version}"
craftctl set grade="${grade}"
craftctl default
pebble:
plugin: nil
stage-snaps:
- pebble

apps:
vaultlocker:
command: bin/vaultlocker
environment:
PEBBLE: /var/snap/vaultlocker/common/pebble
plugs:
- network
- dm-crypt
- block-devices
pebble:
command: bin/pebble run --create-dirs
daemon: simple
environment:
PEBBLE: /var/snap/vaultlocker/common/pebble
plugs:
- network
- network-bind
- dm-crypt
- block-devices
3 changes: 2 additions & 1 deletion tools/vaultlocker-decrypt@.service
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ After=nss-lookup.target
[Service]
Type=oneshot
KillMode=none
Environment=VAULTLOCKER_CONFIG=/etc/vaultlocker/vaultlocker.conf
Environment=VAULTLOCKER_TIMEOUT=10000
ExecStart=/bin/sh -c 'vaultlocker --retry $VAULTLOCKER_TIMEOUT decrypt %i'
ExecStart=/bin/sh -c 'vaultlocker --retry $VAULTLOCKER_TIMEOUT --config "$VAULTLOCKER_CONFIG" decrypt %i'
TimeoutSec=0

[Install]
Expand Down
66 changes: 66 additions & 0 deletions vaultlocker/boot_unlock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# -*- coding: utf-8 -*-

# 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.

"""Select the boot-time unlock backend for the running distribution.

The classic (deb/pip) deployment uses a systemd template unit plus a
per-device configuration drop-in. The snap deployment uses Pebble
layers because a strictly confined snap cannot manipulate host systemd
units at runtime.
"""

import logging
import os

from vaultlocker import pebble_layers
from vaultlocker import systemd

logger = logging.getLogger(__name__)

#: Environment variable snapd sets when a process runs inside a snap.
SNAP_ENV_VAR = 'SNAP'

#: Default number of seconds to retry Vault at boot before giving up
#: for this attempt (the unlock service is retried again afterwards).
DEFAULT_RETRY_TIMEOUT_SECONDS = 10000


def running_in_snap():
"""Return True when running inside a snap environment.

:returns: bool.
"""
return bool(os.environ.get(SNAP_ENV_VAR))


def register(block_uuid, config_path,
timeout=DEFAULT_RETRY_TIMEOUT_SECONDS):
"""Register boot-time unlock for a managed device.

Chooses the Pebble backend inside a snap and the systemd backend
otherwise.

:param: block_uuid: UUID of the encrypted block device.
:param: config_path: Path to the vaultlocker configuration file.
:param: timeout: Seconds to retry connecting to Vault at boot.
"""
if running_in_snap():
logger.info('Registering snap (Pebble) boot unlock for %s',
block_uuid)
pebble_layers.register_boot_unlock(
block_uuid, config_path, timeout,
)
else:
logger.info('Registering systemd boot unlock for %s', block_uuid)
systemd.register_decrypt_service(block_uuid, config_path)
10 changes: 6 additions & 4 deletions vaultlocker/dmcrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,27 @@ def luks_format(key, device, uuid):
)


def luks_open(key, uuid):
"""LUKS open a block device by UUID.
def luks_open(key, uuid, device=None):
"""LUKS open a block device.

Open a block device using dm-crypt/LUKS with the
provided key and uuid
provided key and device path or UUID.

:param: key: string containing the encryption key to use.
:param: uuid: uuid to use for encrypted block device.
:param: device: optional block device path. If omitted, use the UUID.
:returns: str. dm-crypt mapping
"""
logger.info('LUKS opening %s', uuid)
handle = 'crypt-{}'.format(uuid)
source = device if device else 'UUID={}'.format(uuid)
command = [
'cryptsetup',
'--batch-mode',
'--key-file',
'-',
'open',
'UUID={}'.format(uuid),
source,
handle,
'--type',
'luks',
Expand Down
Loading
Loading