Skip to content

Snap boot unlock under confinement - #31

Merged
gboutry merged 3 commits into
openstack-charmers:masterfrom
Raven-182:snap-boot-unlock-and-confinement
Sep 29, 2026
Merged

gboutry merged 3 commits into
openstack-charmers:masterfrom
Raven-182:snap-boot-unlock-and-confinement

Conversation

@Raven-182

@Raven-182 Raven-182 commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR adds automatic boot-time unlocking for both classic installations and the strictly confined snap.

Vaultlocker previously registered a host systemd service for every managed device. This does not work inside a strictly confined snap because the snap cannot create or manage host systemd units.

This change introduces separate boot-unlock backends:

  • systemd for classic installations;
  • Pebble for the Vaultlocker snap.

The backend is selected automatically depending on whether Vaultlocker is running inside a snap.

Changes

Preserve the Vault configuration for each device

Boot unlocking must use the same Vaultlocker configuration that was used when the device was originally managed.

For classic installations, Vaultlocker now writes a per-device systemd drop-in containing the configuration path.

For the snap, the configuration path is stored in the device's Pebble layer and passed to the boot-time decrypt command.

This allows separate Vaultlocker applications on the same machine to use their own Vault configuration.

Add Pebble boot unlocking

The snap now runs Pebble as a daemon and creates one Pebble service for each managed LUKS UUID.

The generated layers are stored under $SNAP_COMMON/pebble/layers, allowing them to survive both host reboots and snap refreshes.

Each service runs the equivalent of:

vaultlocker --retry <timeout> --config <config-path> decrypt <luks-uuid>

Failed unlock attempts are retried, allowing the device to remain locked while Vault is unavailable and unlock automatically once Vault becomes available again.

Registering the same device UUID again reuses its existing layer instead of creating another service.

Support device opening under strict confinement

After formatting or enrolling a device, Vaultlocker previously depended on udev making the LUKS UUID immediately discoverable before opening it.

A strictly confined snap cannot reliably trigger host udev processing. The udev operations are therefore best-effort, and Vaultlocker opens the newly managed device using the device path it already has.

The mapper name remains based on the LUKS UUID. Boot-time decryption continues using the UUID once the host has completed normal device discovery.

Add real VM functional tests

The new functional tests install the built snap in an Ubuntu 24.04 LXD VM, configure a real file-backed Vault service and operate on a dedicated LUKS block device.

They verify that:

  • a snap refresh preserves the Pebble layer and existing LUKS enrollment;
  • the original operator passphrase continues to work;
  • the device unlocks automatically after a reboot;
  • the device remains locked while Vault is sealed;
  • the device unlocks automatically after Vault is unsealed.

The VM tests are skipped unless the required VM and snap environment variables are set.

Manual validation

Built the snap locally and ran the VM functional suite serially against a vm with a scratch block device with all tests passing.

The tests exercised the strict-confined snap, Vault server, luks device enroll operation, snap refresh and multiple VM reboots.

Full setup details are available at vaultlocker/tests/functional/snap/README.rst to check locally.

Assisted by: GPT 5.6 Sol
Jira: OPEN-4747

@Raven-182
Raven-182 force-pushed the snap-boot-unlock-and-confinement branch 3 times, most recently from f10294b to 0045133 Compare September 18, 2026 16:17
@Raven-182
Raven-182 marked this pull request as ready for review September 18, 2026 16:30
@Raven-182 Raven-182 changed the title Snap boot unlock and confinement Snap boot unlock under confinement Sep 18, 2026

@LucasAPayne LucasAPayne left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the snap functional tests to a CI workflow, as they are currently skipped when running the other functional tests. You could rename build-snap.yml to make its purpose clearer and add a job to that.

Besides that and a few small suggestions, it looks good to me overall. This will be a good addition to the snap, and the per-device config is a great idea!

Comment thread vaultlocker/shell.py Outdated
Comment on lines +321 to +319
# NOTE: store and validate key before trying to encrypt disk
# Store the key before changing the device.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: consider reverting this comment. I think it was clearer before.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updates, thanks!

Comment thread vaultlocker/shell.py Outdated
Comment on lines +340 to +351
try:
# Ask udev to create the UUID link used during boot.
dmcrypt.udevadm_rescan(block_device)
dmcrypt.udevadm_settle(block_uuid)
except subprocess.CalledProcessError as udev_error:
logger.warning(
'udev processing for %s failed with error code: %s\n'
'udev output: %s',
block_device,
udev_error.returncode,
udev_error.output,
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: try/except shouldn't be needed here. We know that the udev commands can't run in the snap, so we can use something like if not running_in_snap() instead of trying the udev functions and letting them fail and log a warning. If there is a chance these functions could fail when running as a normal installation, we can keep the try/except, but the snap could skip it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, we now skip udev using running_in_snap(), kkept the try/except for normal installations, where udev commands could still fail

Comment on lines +35 to +38
def test_1_snap_refresh_preserves_enrollment(self):
"""A snap refresh keeps the managed key and does not add a key."""
keyslots_before = self._keyslot_count()
layers_before = sorted(self._layer_files())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: remove numbers from test names in this file to match other tests

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated, thanks!

Comment thread vaultlocker/shell.py
Comment on lines -324 to -326
# All function calls within try/catch raise a CalledProcessError
# if return code is non-zero
# This way if any of the calls fail, the key can be removed from vault

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: consider adding this comment back. It gives some context for the rest of the function.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This exact comment does not apply anymore but added a shorter comment explaining the key is removed if formatting fails

:returns: str. Pebble layer YAML.
"""
command = (
'vaultlocker --retry {timeout} --config {config} '

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have some sort of checks around spaces in the file name? That's gonna break is config has spaces.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrapped in shlex.quote() and a test for this case, thank you!

:param: label: Pebble layer label.
:returns: str. Layer file path.
"""
existing = _existing_layer_path(layers_dir, label)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see this having race conditions for people using the vaultlocker charm outside a charmed deployment.

This is purely architectural, and for the principle, but maybe a file lock managed by vaultlocker snap?

(since pebble will reject multiple identical prefixes)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a little helper from the caller to _layer_path?

with open(lock_path, "a+b") as lock_file:
    fcntl.flock(lock_file, fcntl.LOCK_EX)
    layer_path = _layer_path(layers_dir, label)

(lock_ex should wait until the lock is released)

This is purely from a correctness consideration.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, added a file lock around choosing the layer, writing the layer, and adding it to Pebble

Comment thread vaultlocker/pebble_layers.py Outdated

try:
subprocess.run(
[PEBBLE_BINARY, 'replan'],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function states that this will schedule immediately a pebble service, but calling https://ubuntu.com/docs/pebble/reference/cli-commands/#replan only acts on the current registered plan.

You'll need to call add first to register the new layer and then replan.
https://ubuntu.com/docs/pebble/reference/cli-commands/#add

(on restart, this is correct, the plans would be reloaded by pebble so replan would actually work)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, fixed

@Raven-182
Raven-182 force-pushed the snap-boot-unlock-and-confinement branch 2 times, most recently from 57aedf8 to 26daf29 Compare September 23, 2026 17:26
@Raven-182
Raven-182 requested a review from gboutry September 23, 2026 17:50

@gboutry gboutry left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either we ensure the config paths are always correctly escaped, or we just make simple validation rejecting invalid combination.

services:
{service}:
override: replace
command: {command}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should serialize this whole command as a yaml string. Just shlex quote
config_path does not make it actually valid.

Maybe I'm being pedantic, but maybe the easiest is just to refuse config path with space names in them? Rather that going on the rabbit hole of quoting everywhere.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking to go simpler and reject configuration paths containing whitespace in get_config. This will validate the path before encryption or enrollment starts and before it reaches the boot-unlock part.

I'll keep the shlex.quote for handling things like quotes and backslashes.

Comment thread vaultlocker/systemd.py
with open(dropin_path, 'w') as dropin:
dropin.write(
'[Service]\n'
'Environment=VAULTLOCKER_CONFIG={}\n'.format(config_path)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is another place where the spacing might break.

@Raven-182
Raven-182 force-pushed the snap-boot-unlock-and-confinement branch from 26daf29 to 3da5cd8 Compare September 24, 2026 17:18
wolsen and others added 2 commits September 24, 2026 16:45
Select systemd for classic deployments and Pebble for the confined snap. Preserve the configuration path that registered each device.

Assisted-by: OpenCode (DeepSeek v4.1 Flash)
Co-authored-by: Raven Kaur <raven.kaur@canonical.com>
Co-authored-by: Lucas Payne <lucas.payne@canonical.com>
Co-authored-by: Heather Lanigan <heather.lanigan@canonical.com>
Signed-off-by: Billy Olsen <billy.olsen@canonical.com>
Signed-off-by: Raven Kaur <raven.kaur@canonical.com>
Run boot unlocking through Pebble in the snap and retain systemd support for classic deployments. Open newly encrypted devices by path so confinement does not depend on an immediate udev link.

Assisted-by: OpenCode (DeepSeek v4.1 Flash)
Co-authored-by: Raven Kaur <raven.kaur@canonical.com>
Co-authored-by: Lucas Payne <lucas.payne@canonical.com>
Co-authored-by: Heather Lanigan <heather.lanigan@canonical.com>
Signed-off-by: Billy Olsen <billy.olsen@canonical.com>
Signed-off-by: Raven Kaur <raven.kaur@canonical.com>
@Raven-182
Raven-182 force-pushed the snap-boot-unlock-and-confinement branch from 3da5cd8 to 2ad07c3 Compare September 24, 2026 20:46
@Raven-182

Copy link
Copy Markdown
Contributor Author

I have also updated the pebble replan call to use --no-wait. The unlock service is a one-shot command, so it can exit successfully during Pebble’s startup window and is being reported as a failed start.

I removed the try/except and with --no-wait, the call returns when Pebble accepts the replan. This way it can raise an error if the replan cannot be submitted which was being swallowed earlier.

@LucasAPayne LucasAPayne left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates, Raven. LGTM

@Raven-182
Raven-182 requested a review from gboutry September 28, 2026 13:40
Comment thread .github/workflows/build-snap.yml Outdated
LXD_STORAGE_VOLUME: vaultlocker-scratch
steps:
- name: Check out Git repository
uses: actions/checkout@v6

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: update to the current v7

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to v7 for both. Thanks for catching this

Comment thread .github/workflows/build-snap.yml Outdated
path: snap-artifact

- name: Set up Python
uses: actions/setup-python@v6

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: update to the current v7

Exercise boot unlocking, Vault recovery, and enrollment persistence with a real snap in an LXD virtual machine. Skip the suite when its VM inputs are unavailable.

Assisted-by: OpenCode (DeepSeek v4.1 Flash)
Co-authored-by: Raven Kaur <raven.kaur@canonical.com>
Co-authored-by: Lucas Payne <lucas.payne@canonical.com>
Co-authored-by: Heather Lanigan <heather.lanigan@canonical.com>
Signed-off-by: Billy Olsen <billy.olsen@canonical.com>
Signed-off-by: Raven Kaur <raven.kaur@canonical.com>
@Raven-182
Raven-182 force-pushed the snap-boot-unlock-and-confinement branch from 2ad07c3 to be2fb71 Compare September 28, 2026 17:38
@gboutry
gboutry merged commit 4d2aecc into openstack-charmers:master Sep 29, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants