From 14b8ad70f3ff07bb953858c034442d856cc88b59 Mon Sep 17 00:00:00 2001 From: mulhern Date: Wed, 8 Jan 2025 16:59:41 -0500 Subject: [PATCH] Deal with multiple instances of encryption information Signed-off-by: mulhern --- src/stratis_cli/_actions/_list_pool.py | 61 ++++++++++++++++++++------ 1 file changed, 47 insertions(+), 14 deletions(-) diff --git a/src/stratis_cli/_actions/_list_pool.py b/src/stratis_cli/_actions/_list_pool.py index fe8b8615d..9ddf93d4f 100644 --- a/src/stratis_cli/_actions/_list_pool.py +++ b/src/stratis_cli/_actions/_list_pool.py @@ -16,6 +16,8 @@ """ # isort: STDLIB +import json +import os from abc import ABC, abstractmethod # isort: THIRDPARTY @@ -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): @@ -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. @@ -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")