Zum Inhalt

common.blocks

Overview

common.blocks defines a reusable Prefect block for storing webserver credentials and a Prefect flow for registering multiple credential blocks from a CSV file.

The module contains:

  • WebserverSecret, a Prefect Block used to store a URL, username, and password.
  • register_webserver_secrets(), a Prefect flow that reads credential records from a CSV file and saves them as named Prefect blocks.

The password field uses Pydantic's SecretStr.


Dependencies

  • csv
  • pathlib.Path
  • prefect.flow
  • prefect.get_run_logger
  • prefect.blocks.core.Block
  • pydantic.SecretStr

Expected CSV Structure

register_webserver_secrets() requires the following columns:

Column Purpose
Title Prefect block name
Username Webserver username
Password Webserver password
URL Webserver URL

The CSV file is opened with utf-8-sig encoding.


API Reference

common.blocks.WebserverSecret(url: str, username: str, password: SecretStr)

Prefect block containing credentials for a webserver.

Attributes

url: str
URL of the webserver.

username: str
Username used for authentication.

password: SecretStr
Password used for authentication.

Notes

The Prefect block type name is:

Webserver Secret

common.blocks.register_webserver_secrets(csv_file: str, overwrite: bool = True) -> list[str]

Reads webserver credentials from a CSV file and registers them as WebserverSecret Prefect blocks.

Parameters

csv_file: str
Path to the CSV file containing the credential records.

The file must contain the columns Title, Username, Password, and URL.

overwrite: bool = True
Controls handling of existing Prefect blocks.

  • True: save the block using overwrite=True.
  • False: attempt to load an existing block first and skip registration when it already exists.

Returns

list[str]

Names of the blocks registered during the current flow run.

Skipped blocks are not included.

Raises

ValueError
Raised when one or more required CSV columns are missing.

ValueError
Raised when a row contains an empty Title after whitespace is removed.

Other file-access or Prefect-related exceptions are not caught and propagate to the caller.

Processing

  1. Open the CSV file.
  2. Create a csv.DictReader.
  3. Validate the required column names.
  4. Iterate through the data rows, starting with row number 2.
  5. Strip whitespace from Title.
  6. Create a WebserverSecret.
  7. Optionally check whether a block with the same name already exists.
  8. Save the block.
  9. Record and log the registered block name.
  10. Log the total number of registered blocks.

Existing Block Handling

When overwrite=False, the flow calls:

WebserverSecret.load(block_name)

If loading raises ValueError, registration continues. If loading succeeds, the existing block is skipped.

Logging

The flow logs:

  • skipped existing blocks,
  • successfully registered blocks,
  • the total number of registered blocks.

Direct Execution

When executed directly, the module runs:

register_webserver_secrets("export_test.csv", overwrite=False)

Operational Considerations

Credential Source File

The password is stored in the Prefect model as SecretStr, but the source CSV still contains the values used to create the blocks. The module does not delete or transform that source file.

Validation Scope

The module validates the required column names and requires Title to be non-empty. It does not explicitly check that Username, Password, or URL are non-empty.

Duplicate Titles

With overwrite=True, later rows using the same title can overwrite an earlier block. With overwrite=False, an already existing block is skipped.