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
29 changes: 26 additions & 3 deletions vaultlocker/dmcrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@

KEY_SIZE = 4096

#: Maximum time to wait for a cryptsetup command.
CRYPTSETUP_TIMEOUT_SECONDS = 300

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

Comment on lines +25 to +30

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


def _key_bytes(key):
"""Normalize key material for subprocess input.
Expand Down Expand Up @@ -68,6 +74,7 @@ def luks_format(key, device, uuid):
subprocess.check_output(
command,
input=_key_bytes(key),
timeout=CRYPTSETUP_TIMEOUT_SECONDS,
)


Expand Down Expand Up @@ -99,6 +106,7 @@ def luks_open(key, uuid, device=None):
subprocess.check_output(
command,
input=_key_bytes(key),
timeout=CRYPTSETUP_TIMEOUT_SECONDS,
)
return handle

Expand All @@ -115,7 +123,10 @@ def luks_uuid(device):
'luksUUID',
device,
]
return subprocess.check_output(command).decode('utf-8').strip()
return subprocess.check_output(
command,
timeout=CRYPTSETUP_TIMEOUT_SECONDS,
).decode('utf-8').strip()


def luks_test_key(key, device):
Expand All @@ -141,8 +152,13 @@ def luks_test_key(key, device):
subprocess.check_output(
command,
input=_key_bytes(key),
timeout=CRYPTSETUP_TIMEOUT_SECONDS,
)
except subprocess.CalledProcessError as exc:
# cryptsetup returns exit code 2 for incorrect passphrase
# with --test-passphrase, this confirms that the given key
# does not unlock the device. Other exit codes are inconclusive
# and should be raised.
if exc.returncode == 2:
return False
raise
Expand Down Expand Up @@ -183,6 +199,7 @@ def luks_add_key(existing_key, new_key, device):
command,
input=_key_bytes(new_key),
pass_fds=(existing_key_fd,),
timeout=CRYPTSETUP_TIMEOUT_SECONDS,
)
finally:
os.close(existing_key_fd)
Expand All @@ -203,7 +220,10 @@ def udevadm_rescan(device):
'--name-match={}'.format(device),
'--action=add'
]
subprocess.check_output(command)
subprocess.check_output(
command,
timeout=UDEVADM_TIMEOUT_SECONDS,
)


def udevadm_settle(uuid):
Expand All @@ -220,4 +240,7 @@ def udevadm_settle(uuid):
'settle',
'--exit-if-exists=/dev/disk/by-uuid/{}'.format(uuid),
]
subprocess.check_output(command)
subprocess.check_output(
command,
timeout=UDEVADM_TIMEOUT_SECONDS,
)
67 changes: 60 additions & 7 deletions vaultlocker/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
# License for the specific language governing permissions and limitations
# under the License.

"""Errors returned as controlled command failures."""


class VaultlockerException(Exception):
"""Base class for controlled vaultlocker errors."""

def __init__(self, *args):
if args:
Expand All @@ -25,37 +28,87 @@ def __str__(self):
return self.message


class ConfigurationError(VaultlockerException):
"""The vaultlocker configuration is missing or invalid."""


class VaultWriteError(VaultlockerException):
"""Writing a key to Vault failed."""

def __init__(self, path, error):
super().__init__("Can't write to vault at path {}, error: {}".format(
path, error))


class VaultReadError(VaultlockerException):
"""Reading a key back from Vault failed."""

def __init__(self, path, error):
super().__init__("Can't read vault at path {}, error: {}".format(
path, error))


class VaultDeleteError(VaultlockerException):

def __init__(self, path, error):
super().__init__("Can't delete vault key at path {}, error: {}".format(
path, error))


class VaultKeyMismatch(VaultlockerException):
"""A key read back from Vault does not match what was written."""

def __init__(self, path):
super().__init__(
"Vault key at path {} does not match with generated key".format(
path))


class VaultConnectionError(VaultlockerException):
"""Vault was unavailable or authentication failed after retries."""

def __init__(self, error):
super().__init__("Unable to connect to Vault: {}".format(error))


class ManagedKeyInvalidError(VaultlockerException):
"""The managed key stored in Vault is missing or malformed."""

def __init__(self, path):
super().__init__(
"Vault secret at {} does not contain dmcrypt_key".format(path)
)


class ManagedKeyNotFoundError(VaultlockerException):
"""No Vault key exists at the expected path."""

def __init__(self, path):
super().__init__(
'No vaultlocker-managed key found at {}'.format(path)
)


class ExistingKeyInvalidError(VaultlockerException):
"""Existing LUKS credential is missing, unreadable, or wrong."""


class LUKSFailure(VaultlockerException):
"""Base class for all cryptsetup/LUKS-related errors."""

def __init__(self, block_device, error):
super().__init__("Can't operate on {}. Error: {}".format(
block_device, error))


class LuksValidationError(LUKSFailure):
"""The device's LUKS state could not be verified."""


class LuksFormatError(LUKSFailure):
"""Formatting a LUKS device failed."""


class LuksAddKeyError(LUKSFailure):
"""Adding a LUKS key failed."""


class MapperOpenError(LUKSFailure):
"""Opening the dm-crypt mapper failed."""


class BootConfigError(LUKSFailure):
"""Registering the boot-time unlock mechanism failed."""
27 changes: 27 additions & 0 deletions vaultlocker/exit_codes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- 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.

"""Process exit codes for the vaultlocker CLI."""

from enum import IntEnum


class ExitCode(IntEnum):
"""Exit codes returned by the vaultlocker command."""

SUCCESS = 0
UNEXPECTED_FAILURE = 1

# argparse uses exit code 2 for invalid command-line arguments.
HANDLED_FAILURE = 3
Loading
Loading