management.util.startup_strategies¶
Startup strategies for Trustpoint initialization and restoration.
Attributes¶
Classes¶
Protocol for output writing (stdout/stderr). |
|
Enumeration of wizard completion states. |
|
Enumeration of DEK cache states (for HSM storage only). |
|
Enumeration of possible startup scenarios. |
|
Context information for startup decisions. |
|
Abstract base class for startup strategies. |
|
Abstract base class for TLS credential generation strategies. |
|
Standard TLS credential generation using TlsServerCredentialGenerator. |
|
Abstract base class for Trustpoint initialization strategies. |
|
Standard initialization: migrations, static files, messages. |
|
Strategy for handling uninitialized database. |
|
Strategy for handling initialized database without version record. |
|
Strategy for handling version match (normal startup). |
|
Strategy for handling version upgrade. |
|
Strategy: Software storage + Wizard completed - extract TLS only. |
|
Strategy: Software storage + Wizard incomplete - reset to beginning. |
|
Strategy: SoftHSM + Wizard completed + DEK cached - extract TLS and unwrap DEK. |
|
Strategy: SoftHSM + Wizard completed + DEK not cached - needs auto restore. |
|
Strategy: SoftHSM + New KEK (old KEK lost) + Wizard completed - generate temp TLS, wait for backup password. |
|
Strategy: SoftHSM + Wizard incomplete + DEK cached - reset to beginning. |
|
Strategy: SoftHSM + Wizard incomplete + DEK not cached - reset to beginning. |
|
Strategy: Physical HSM + Wizard completed + DEK cached - NOT SUPPORTED. |
|
Strategy: Physical HSM + Wizard completed + DEK not cached - NOT SUPPORTED. |
|
Strategy: Physical HSM + Wizard incomplete + DEK cached - NOT SUPPORTED. |
|
Strategy: Physical HSM + Wizard incomplete + DEK not cached - NOT SUPPORTED. |
|
Selector for determining the appropriate startup strategy. |
Module Contents¶
- class management.util.startup_strategies.OutputWriter[source]¶
Bases:
ProtocolProtocol for output writing (stdout/stderr).
- class management.util.startup_strategies.WizardState(*args, **kwds)[source]¶
Bases:
enum.EnumEnumeration of wizard completion states.
- class management.util.startup_strategies.DekCacheState(*args, **kwds)[source]¶
Bases:
enum.EnumEnumeration of DEK cache states (for HSM storage only).
- class management.util.startup_strategies.StartupScenario(*args, **kwds)[source]¶
Bases:
enum.EnumEnumeration of possible startup scenarios.
- class management.util.startup_strategies.StartupContext[source]¶
Context information for startup decisions.
- wizard_state_enum: WizardState[source]¶
- wizard_state_raw: setup_wizard.SetupWizardState | None[source]¶
- storage_type: management.models.KeyStorageConfig.StorageType | None[source]¶
- dek_cache_state: DekCacheState | None[source]¶
- output: OutputWriter[source]¶
- property is_physical_hsm_storage: bool[source]¶
Check if using physical HSM storage.
- Return type:
bool
- property is_hsm_storage: bool[source]¶
Check if using any HSM storage (SoftHSM or Physical).
- Return type:
bool
- property is_dek_cached: bool[source]¶
Check if DEK is cached (only applicable for HSM storage).
- Return type:
bool
- property is_new_kek_scenario: bool[source]¶
Check if this is a new KEK scenario (old KEK lost, requires backup password).
This scenario occurs when: - Using HSM storage - DEK is not cached - KEK does NOT exist on the HSM (old KEK lost from previous installation) - Backup-encrypted DEK exists in the database
Note: We don’t check wizard_completed here because the wizard state might be inconsistent if restoring a database from a previous installation.
- Returns:
True if this is a new KEK scenario requiring backup password recovery.
- Return type:
bool
- class management.util.startup_strategies.StartupStrategy[source]¶
Bases:
abc.ABCAbstract base class for startup strategies.
- abstractmethod execute(context)[source]¶
Execute the startup strategy.
- Parameters:
context (StartupContext) – The startup context with all relevant information.
- Return type:
None
- class management.util.startup_strategies.TlsCredentialStrategy[source]¶
Bases:
abc.ABCAbstract base class for TLS credential generation strategies.
- abstractmethod generate_and_save_tls_credential(context)[source]¶
Generate and save TLS credentials.
- Parameters:
context (StartupContext) – The startup context with all relevant information.
- Return type:
None
- class management.util.startup_strategies.StandardTlsCredentialStrategy(*, save_to_db=True)[source]¶
Bases:
TlsCredentialStrategyStandard TLS credential generation using TlsServerCredentialGenerator.
- Parameters:
save_to_db (bool)
- generate_and_save_tls_credential(context)[source]¶
Generate TLS credentials and save to database and files.
- Parameters:
context (StartupContext)
- Return type:
None
- _save_to_database(context, tls_server_credential)[source]¶
Save TLS credential to database and return PEM data.
- Returns:
Tuple of (private_key_pem, certificate_pem, trust_store_pem, active_tls)
- Parameters:
context (StartupContext)
tls_server_credential (Any)
- Return type:
tuple[str, str, str, Any]
- _get_pem_without_db_save(context, tls_server_credential)[source]¶
Get PEM data without saving to database.
- Returns:
Tuple of (private_key_pem, certificate_pem, trust_store_pem)
- Parameters:
context (StartupContext)
tls_server_credential (Any)
- Return type:
tuple[str, str, str]
- _write_pem_files(private_key_pem, certificate_pem, trust_store_pem)[source]¶
Write PEM files to Nginx paths.
- Parameters:
private_key_pem (str)
certificate_pem (str)
trust_store_pem (str)
- Return type:
None
- _display_fingerprint(context, active_tls, tls_server_credential)[source]¶
Display SHA256 fingerprint of the certificate.
- Parameters:
context (StartupContext)
active_tls (Any)
tls_server_credential (Any)
- Return type:
None
- class management.util.startup_strategies.InitializationStrategy[source]¶
Bases:
abc.ABCAbstract base class for Trustpoint initialization strategies.
- abstractmethod initialize(context, *, with_tls=False)[source]¶
Initialize Trustpoint.
- Parameters:
context (StartupContext) – The startup context with all relevant information.
with_tls (bool) – Whether to initialize TLS credentials.
- Return type:
None
- class management.util.startup_strategies.StandardInitializationStrategy(tls_strategy=None)[source]¶
Bases:
InitializationStrategyStandard initialization: migrations, static files, messages.
- Parameters:
tls_strategy (TlsCredentialStrategy | None)
- initialize(context, *, with_tls=False)[source]¶
Initialize Trustpoint with standard workflow.
- Parameters:
context (StartupContext)
with_tls (bool)
- Return type:
None
- _raise_runtime_error(error_msg)[source]¶
Raise a RuntimeError.
- Parameters:
error_msg (str) – The error message to log and raise.
- Raises:
RuntimeError – Always raised with the provided error message.
- Return type:
None
- _check_softhsm_connectivity(context)[source]¶
Check if SoftHSM daemon is reachable.
- Parameters:
context (StartupContext) – The startup context.
- Raises:
RuntimeError – If SoftHSM daemon is not reachable.
- Return type:
None
- class management.util.startup_strategies.DatabaseNotInitializedStrategy(init_strategy=None)[source]¶
Bases:
StartupStrategyStrategy for handling uninitialized database.
- Parameters:
init_strategy (InitializationStrategy | None)
- execute(context)[source]¶
Initialize database with TLS setup.
- Parameters:
context (StartupContext)
- Return type:
None
- class management.util.startup_strategies.DatabaseInitializedNoVersionStrategy(init_strategy=None)[source]¶
Bases:
StartupStrategyStrategy for handling initialized database without version record.
- Parameters:
init_strategy (InitializationStrategy | None)
- execute(context)[source]¶
Initialize database with TLS setup.
- Parameters:
context (StartupContext)
- Return type:
None
- class management.util.startup_strategies.VersionMatchStrategy(restore_strategy, init_strategy=None)[source]¶
Bases:
StartupStrategyStrategy for handling version match (normal startup).
- Parameters:
restore_strategy (StartupStrategy)
init_strategy (InitializationStrategy | None)
- execute(context)[source]¶
Initialize Trustpoint and perform restoration.
- Parameters:
context (StartupContext)
- Return type:
None
- class management.util.startup_strategies.VersionUpgradeStrategy(restore_strategy, app_version, init_strategy=None)[source]¶
Bases:
StartupStrategyStrategy for handling version upgrade.
- Parameters:
restore_strategy (StartupStrategy)
app_version (management.models.AppVersion)
init_strategy (InitializationStrategy | None)
- execute(context)[source]¶
Upgrade database version and perform restoration.
- Parameters:
context (StartupContext)
- Return type:
None
- class management.util.startup_strategies.RestoreSoftwareWizardCompletedStrategy[source]¶
Bases:
StartupStrategyStrategy: Software storage + Wizard completed - extract TLS only.
- execute(context)[source]¶
Extract TLS certificates for software storage mode.
- Parameters:
context (StartupContext)
- Return type:
None
- static unwrap_dek_for_token(context, token_label=DEFAULT_TOKEN_LABEL)[source]¶
Test KEK loading and unwrap DEK for the specified token to make it available in cache.
This method follows the same logic as the unwrap_dek management command: 1. Get the token (or return gracefully if not found - software storage case) 2. Test KEK loading 3. Unwrap DEK (only if encrypted_dek exists)
- Parameters:
context (StartupContext) – The startup context.
token_label (str) – The label of the PKCS11 token to unwrap DEK for.
- Return type:
None
- static extract_tls_from_database(context)[source]¶
Extract TLS certificates from database and write to Nginx paths.
- Parameters:
context (StartupContext)
- Return type:
None
- class management.util.startup_strategies.RestoreSoftwareWizardIncompleteStrategy(tls_strategy=None)[source]¶
Bases:
StartupStrategyStrategy: Software storage + Wizard incomplete - reset to beginning.
- Parameters:
tls_strategy (TlsCredentialStrategy | None)
- execute(context)[source]¶
Reset wizard to WIZARD_SETUP_CRYPTO_STORAGE state and generate TLS certificates.
- Parameters:
context (StartupContext)
- Return type:
None
- static _reset_wizard_state(context)[source]¶
Reset wizard state to WIZARD_SETUP_CRYPTO_STORAGE.
- Parameters:
context (StartupContext)
- Return type:
None
- class management.util.startup_strategies.RestoreSoftHsmWizardCompletedDekCachedStrategy[source]¶
Bases:
StartupStrategyStrategy: SoftHSM + Wizard completed + DEK cached - extract TLS and unwrap DEK.
- execute(context)[source]¶
Extract TLS certificates and unwrap DEK for application use.
- Parameters:
context (StartupContext)
- Return type:
None
- class management.util.startup_strategies.RestoreSoftHsmWizardCompletedDekNotCachedStrategy[source]¶
Bases:
StartupStrategyStrategy: SoftHSM + Wizard completed + DEK not cached - needs auto restore.
- execute(context)[source]¶
Trigger auto restore flow for SoftHSM with DEK not cached.
- Parameters:
context (StartupContext)
- Return type:
None
- static _set_auto_restore_state(context)[source]¶
Transition to WIZARD_AUTO_RESTORE_PASSWORD state.
- Parameters:
context (StartupContext)
- Return type:
None
- class management.util.startup_strategies.RestoreSoftHsmNewKekWizardCompletedStrategy(tls_strategy=None)[source]¶
Bases:
StartupStrategyStrategy: SoftHSM + New KEK (old KEK lost) + Wizard completed - generate temp TLS, wait for backup password.
This scenario occurs when: - Trustpoint container and SoftHSM are new (fresh installation) - But there’s an existing database with encrypted data - The database was encrypted with a KEK that no longer exists on the new SoftHSM - The wizard was previously completed
The strategy: 1. Generates a temporary TLS certificate (since DB TLS cert is encrypted and inaccessible) 2. Transitions to WIZARD_AUTO_RESTORE_PASSWORD state 3. Waits for user to input backup password 4. BackupAutoRestorePasswordView will:
Generate a new KEK on the new SoftHSM
Decrypt the DEK using the backup password
Re-wrap the DEK with the new KEK
Extract and activate the actual TLS certificate from the database
Complete the wizard
- Parameters:
tls_strategy (TlsCredentialStrategy | None)
- execute(context)[source]¶
Generate temporary TLS certificate and trigger auto restore flow.
- Parameters:
context (StartupContext)
- Return type:
None
- class management.util.startup_strategies.RestoreSoftHsmWizardIncompleteDekCachedStrategy(tls_strategy=None)[source]¶
Bases:
StartupStrategyStrategy: SoftHSM + Wizard incomplete + DEK cached - reset to beginning.
- Parameters:
tls_strategy (TlsCredentialStrategy | None)
- execute(context)[source]¶
Reset wizard to WIZARD_SETUP_CRYPTO_STORAGE state and generate TLS certificates.
- Parameters:
context (StartupContext)
- Return type:
None
- class management.util.startup_strategies.RestoreSoftHsmWizardIncompleteDekNotCachedStrategy(tls_strategy=None)[source]¶
Bases:
StartupStrategyStrategy: SoftHSM + Wizard incomplete + DEK not cached - reset to beginning.
- Parameters:
tls_strategy (TlsCredentialStrategy | None)
- execute(context)[source]¶
Reset wizard to WIZARD_SETUP_CRYPTO_STORAGE state and generate TLS certificates.
- Parameters:
context (StartupContext)
- Return type:
None
- class management.util.startup_strategies.RestorePhysicalHsmWizardCompletedDekCachedStrategy[source]¶
Bases:
StartupStrategyStrategy: Physical HSM + Wizard completed + DEK cached - NOT SUPPORTED.
- execute(context)[source]¶
Raise error as Physical HSM is not yet supported.
- Parameters:
context (StartupContext)
- Return type:
None
- class management.util.startup_strategies.RestorePhysicalHsmWizardCompletedDekNotCachedStrategy[source]¶
Bases:
StartupStrategyStrategy: Physical HSM + Wizard completed + DEK not cached - NOT SUPPORTED.
- execute(context)[source]¶
Raise error as Physical HSM is not yet supported.
- Parameters:
context (StartupContext)
- Return type:
None
- class management.util.startup_strategies.RestorePhysicalHsmWizardIncompleteDekCachedStrategy[source]¶
Bases:
StartupStrategyStrategy: Physical HSM + Wizard incomplete + DEK cached - NOT SUPPORTED.
- execute(context)[source]¶
Raise error as Physical HSM is not yet supported.
- Parameters:
context (StartupContext)
- Return type:
None
- class management.util.startup_strategies.RestorePhysicalHsmWizardIncompleteDekNotCachedStrategy[source]¶
Bases:
StartupStrategyStrategy: Physical HSM + Wizard incomplete + DEK not cached - NOT SUPPORTED.
- execute(context)[source]¶
Raise error as Physical HSM is not yet supported.
- Parameters:
context (StartupContext)
- Return type:
None
- class management.util.startup_strategies.StartupStrategySelector[source]¶
Selector for determining the appropriate startup strategy.
- static select_restore_strategy(context)[source]¶
Select the appropriate restore strategy based on context.
- Parameters:
context (StartupContext) – The startup context with all relevant information.
- Returns:
The appropriate StartupStrategy instance.
- Return type:
- static _log_strategy_selection(context)[source]¶
Log the current configuration for strategy selection.
- Parameters:
context (StartupContext)
- Return type:
None
- static _select_software_strategy(context)[source]¶
Select strategy for SOFTWARE storage.
- Parameters:
context (StartupContext)
- Return type:
- static _select_softhsm_strategy(context)[source]¶
Select strategy for SOFTHSM storage.
- Parameters:
context (StartupContext)
- Return type:
- static _select_physical_hsm_strategy(context)[source]¶
Select strategy for PHYSICAL_HSM storage.
- Parameters:
context (StartupContext)
- Return type:
- static select_version_strategy(context, app_version)[source]¶
Select the appropriate version management strategy.
- Parameters:
context (StartupContext) – The startup context with version information.
app_version (management.models.AppVersion) – The AppVersion model instance.
- Returns:
The appropriate StartupStrategy instance.
- Raises:
RuntimeError – If version downgrade is detected.
- Return type:
- static select_startup_strategy(*, db_initialized, has_version, context=None, app_version=None)[source]¶
Select the top-level startup strategy.
- Parameters:
db_initialized (bool) – Whether the database is initialized.
has_version (bool) – Whether an app version record exists.
context (StartupContext | None) – The startup context (required if db is initialized).
app_version (management.models.AppVersion | None) – The AppVersion model instance (required if db is initialized).
- Returns:
The appropriate StartupStrategy instance.
- Return type: