Skip to content

Commit

Permalink
Deal with multiple instances of encryption information
Browse files Browse the repository at this point in the history
Signed-off-by: mulhern <[email protected]>
  • Loading branch information
mulkieran committed Jan 10, 2025
1 parent d072e32 commit 14b8ad7
Showing 1 changed file with 47 additions and 14 deletions.
61 changes: 47 additions & 14 deletions src/stratis_cli/_actions/_list_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
"""

# isort: STDLIB
import json
import os
from abc import ABC, abstractmethod

# isort: THIRDPARTY
Expand All @@ -33,12 +35,7 @@
print_table,
size_triple,
)
from ._utils import (
EncryptionInfoClevis,
EncryptionInfoKeyDescription,
PoolFeature,
StoppedPool,
)
from ._utils import PoolFeature, StoppedPool


def _fetch_stopped_pools_property(proxy):
Expand Down Expand Up @@ -81,6 +78,35 @@ def _non_existent_or_inconsistent_to_str(
return interp(value)


class TokenSlotInfo:

def __init__(self, token_slot, *, key=None, clevis=None):
"""
Initialize either information about a key or about a Clevis
configuration for purposes of printing later.
:param int token_slot: token slot
:param str key: key
:param clevis: clevis configuration
:type clevis: pair of pin and configuration, str * json
"""
assert (key is None) ^ (clevis is None)

self.token_slot = token_slot
self.key = key
self.clevis = clevis

def __str__(self):
return f"Token Slot: {self.token_slot}{os.linesep}" + (
f" Key Description: {self.key}"
if self.key is not None
else (
f" Clevis Pin: {self.clevis[0]}{os.linesep}"
f" Clevis Configuration: {self.clevis[1]}"
)
)


def list_pools(uuid_formatter, *, stopped=False, selection=None):
"""
List the specified information about pools.
Expand Down Expand Up @@ -270,16 +296,23 @@ def _print_detail_view(self, mopool, size_change_codes):
if encrypted:
print("Encryption Enabled: Yes")

key_description_str = _non_existent_or_inconsistent_to_str(
EncryptionInfoKeyDescription(mopool.KeyDescription())
encryption_infos = sorted(
[
TokenSlotInfo(token_slot, key=str(description))
for token_slot, description in mopool.KeyDescriptions()
]
+ [
TokenSlotInfo(
token_slot, clevis=(str(pin), json.loads(str(config)))
)
for token_slot, (pin, config) in mopool.ClevisInfos()
],
key=lambda x: x.token_slot,
)
print(f" Key Description: {key_description_str}")

clevis_info_str = _non_existent_or_inconsistent_to_str(
EncryptionInfoClevis(mopool.ClevisInfo()),
interp=_clevis_to_str, # pyright: ignore [ reportArgumentType ]
)
print(f" Clevis Configuration: {clevis_info_str}")
for info in encryption_infos:
for line in str(info).split(os.linesep):
print(f" {line}")

else:
print("Encryption Enabled: No")
Expand Down

0 comments on commit 14b8ad7

Please sign in to comment.