Skip to content

feat: make Vaultlocker device operations safe to repeat - #32

Merged
Raven-182 merged 4 commits into
openstack-charmers:masterfrom
Raven-182:retry-safe-vaultlocker-operations
Sep 30, 2026
Merged

Raven-182 merged 4 commits into
openstack-charmers:masterfrom
Raven-182:retry-safe-vaultlocker-operations

Conversation

@Raven-182

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

Copy link
Copy Markdown
Contributor

Summary

This change makes vaultlocker encrypt and vaultlocker enroll safe for a caller to run again after a failure.

The commands check the existing device and Vault state before continuing. Successful commands return the LUKS UUID and mapper path as JSON. Failures return an error message as JSON.

This builds on the snap and boot-unlock work in #31.

Why

A Vaultlocker operation can fail after part of the work has already completed. For example, a device may have been formatted successfully even if cryptsetup timed out before reporting success.

We should be able to call Vaultlocker again without formatting the device again, overwriting a Vault key, or adding another managed key.

Safe to retry means that it is safe to run the command again. It does not mean that the next attempt will succeed. Configuration, credentials, Vault availability, or device state still need to be fixed first.

Changes

  • Add timeouts to cryptsetup and udevadm commands so a stuck device command cannot block forever.
  • Return luks_uuid and mapper_path as JSON after a successful encrypt or enroll.
  • Use separate exit codes for handled failures and unexpected failures.
  • Inspect the current device and Vault state before making changes.
  • Reuse an existing encrypted device only when its Vault-managed key unlocks it.
  • Stop if a key check times out or fails without confirming whether the key works.
  • Do not overwrite conflicting Vault key data.
  • Preserve the generated UUID and key across internal retries.
  • Check the real device state after luksFormat or luksAddKey reports an error or timeout.
  • Do not delete a Vault key when the device may already have been formatted.
  • Do not add another key when the managed key already unlocks an enrolled device.
  • Read the operator key only when it is needed.
  • Test the workflow with both Vault KV v1 and KV v2.

Command results

Successful encrypt and enroll commands return:

{
  "luks_uuid": "<device UUID>",
  "mapper_path": "/dev/mapper/crypt-<device UUID>"
}

Failures return an error as JSON on stderr:

{
  "error": "<message>"
}

Handled failures exit with code 3. Unexpected failures exit with code 1.

Manual testing

Built the snap and tested in a LXD vm with a scratch device:

  • Running encrypt twice: The second run kept the same UUID, vault key, and LUKS keyslot. It did not format the device again.
  • Running enroll twice: The second run did not add another keyslot or extra keys in vault.
  • Using wrong operator key during enroll: Enrollment stopped without adding any keyslots or adding keys to vault
  • Making vult unavailable: The device stayed unchanged for both encrypt and enroll. Later re-run with correct key succeeded.
  • Making pebble registration fail: Later rerun just submitted the pebble layer successfully without adding another keyslot.

@Raven-182
Raven-182 force-pushed the retry-safe-vaultlocker-operations branch 6 times, most recently from d97ddd3 to df27e3d Compare September 25, 2026 00:02
@Raven-182
Raven-182 marked this pull request as ready for review September 25, 2026 00:02
@Raven-182 Raven-182 mentioned this pull request Sep 27, 2026
1 task done
@Raven-182
Raven-182 force-pushed the retry-safe-vaultlocker-operations branch from df27e3d to 846e006 Compare September 28, 2026 17:38
@Raven-182
Raven-182 requested a review from hmlanigan September 29, 2026 14:52
Raven-182 and others added 2 commits September 29, 2026 11:01
Limit cryptsetup to 300 seconds and udevadm to 60 seconds so stalled
device operations return control to the caller.

Assisted-by: OpenCode (DeepSeek v4.1 Flash)

Co-authored-by: Billy Olsen <billy.olsen@canonical.com>
Co-authored-by: Lucas Payne <lucas.payne@canonical.com>
Co-authored-by: Heather Lanigan <heather.lanigan@canonical.com>
Signed-off-by: Raven Kaur <raven.kaur@canonical.com>
Return JSON for successful device setup and controlled failures. Validate
configuration errors and preserve explicit retryable and unexpected exit
codes.

Assisted-by: OpenCode (DeepSeek v4.1 Flash)

Co-authored-by: Billy Olsen <billy.olsen@canonical.com>
Co-authored-by: Lucas Payne <lucas.payne@canonical.com>
Co-authored-by: Heather Lanigan <heather.lanigan@canonical.com>
Signed-off-by: Raven Kaur <raven.kaur@canonical.com>
@Raven-182
Raven-182 force-pushed the retry-safe-vaultlocker-operations branch from 846e006 to 49d7ef8 Compare September 29, 2026 15:03
"""Test case base class for all functional tests."""

#: KV secrets engine version under test.
kv_version = '1'

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.

question: why default to the old version and not the newest?

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.

For this test fixture, I kept v1 to preserve what it used before this change. The KV v2 test class overrides it with kv_version = '2'

Comment thread vaultlocker/dmcrypt.py
Comment on lines +25 to +30
#: Maximum time to wait for a cryptsetup command.
CRYPTSETUP_TIMEOUT_SECONDS = 300

#: Maximum time to wait for a udevadm command.
UDEVADM_TIMEOUT_SECONDS = 60

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.

question: where did the values of 300 and 60 seconds come from? It'd be handy to have a comment to tell us.

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.

These are initial limits to stop a stuck command from waiting forever, they are initial defaults rather than limits based on benchmarks. We give 5 minutes for cryptsetup for slower device I/O and a 1 minute limit for udevadm

Comment thread vaultlocker/shell.py Outdated
Comment on lines +368 to +556
:param: args: argparser generated cli arguments
:param: args: parsed command-line arguments
:param: client: hvac.Client for Vault access
:param: config: configparser object of vaultlocker config
:param: config: parsed vaultlocker configuration

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.

nit: both ways of describing args and config params are used throughout this file, although the argparser and configparser variants are much more common. We should use a consistent format throughout.

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 Lucas!

Raven-182 and others added 2 commits September 30, 2026 11:59
Reconcile Vault and LUKS state before mutation, preserve operation keys
across retries, and fail closed on conflicts or inconclusive device state.

Assisted-by: OpenCode (DeepSeek v4.1 Flash)

Co-authored-by: Billy Olsen <billy.olsen@canonical.com>
Co-authored-by: Lucas Payne <lucas.payne@canonical.com>
Co-authored-by: Heather Lanigan <heather.lanigan@canonical.com>
Signed-off-by: Raven Kaur <raven.kaur@canonical.com>
Run encryption, decryption, enrollment, and Vault key-conflict scenarios against both supported KV engine versions.

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: Raven Kaur <raven.kaur@canonical.com>
@Raven-182
Raven-182 force-pushed the retry-safe-vaultlocker-operations branch from 49d7ef8 to 922ee7c Compare September 30, 2026 16:00
@Raven-182
Raven-182 merged commit f341b40 into openstack-charmers:master Sep 30, 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.

4 participants