SCAutolib.models.file

This module contains classes that represent and facilitate the manipulation of various configuration files used within SCAutolib’s operations. It defines a generic File class for common file operations and specialized subclasses like SSSDConf, SoftHSM2Conf, and OpensslCnf for managing specific configuration file types. These classes provide methods for creating, modifying (setting values), saving, and cleaning (removing) configuration files, with some supporting backup and restore functionalities.

Classes

class File(filepath, template=None)[source]

This class serves as an interface and base implementation for generic operations on various configuration files.

  • create: create content of config file based on template file

  • set: modify content of config files, add keys or sections if necessary

  • save: save config file

  • clean: remove config file

Note

Set method operates only on:

  • files compatible with ConfigParser (i.e. files containing sections)

  • simple config files without sections.

Other formats of config files are not supported.

Initializes a File object, setting the path to the configuration file and an optional template file for content creation.

Parameters:
  • filepath (Union[str, pathlib.Path]) – The path to the configuration file that this object will manage. Can be a string or a pathlib.Path object.

  • template (pathlib.Path, optional) – The path to a template file from which the configuration file’s content can be generated.

Returns:

None

Return type:

None

backup(name=None)[source]

Saves a copy of the original configuration file to a designated backup directory. The backup file’s name can be customized or defaults to <filename>.<extension>.backup.

Parameters:

name (str, optional) – An optional custom file name to be used for the backup file.

Returns:

The pathlib.Path object indicating where the backup file is stored.

Return type:

pathlib.Path

clean()[source]

Removes the configuration file from the file system if it exists.

Returns:

None

Return type:

None

create()[source]

Populates the internal parser object with content read from the template file. This method is typically called when the configuration file does not yet exist on the system.

Returns:

None

Return type:

None

Raises:
  • FileExistsError – If the configuration file already exists on the file system.

  • ValueError – If no template file was provided during object initialization when create is called.

exists()[source]

Checks if the configuration file managed by this object exists on the file system.

Returns:

True if the file exists; False otherwise.

Return type:

bool

get(key, section=None, separator='=')[source]

Method processes and returns the value of the key in section. If the section is not provided (section=None), then file would be parsed line by line splitting the line on separator. First match wins and is returned.

If section is provided and the file can be parsed by the ConfigParser, then this object would be used to look for the key.

Parameters:
  • key (str) – The key whose value is to be retrieved.

  • section (str, optional) – The name of the section where the key is expected to be found. If None, the file is parsed line by line.

  • separator (str) – The character used to split lines into key-value pairs for simple (non-ConfigParser) files. Defaults to =.

Returns:

The string value associated with the key.

Return type:

str

Raises:
  • SCAutolibException – If the key is not found in a simple (non-ConfigParser) file.

  • configparser.NoSectionError – If the specified section is not found in a ConfigParser-supported file.

  • KeyError – If the key is not present within the specified section in a ConfigParser-supported file.

property path

Returns the pathlib.Path object representing the configuration file managed by this object.

Returns:

The path of the configuration file.

Return type:

pathlib.Path

remove()[source]

Removes the configuration file from the file system if it exists.

Returns:

None

Return type:

None

restore(name=None)[source]

Restores the configuration file by copying a previously created backup file back to the original file’s location. After restoration, the backup file is removed.

Parameters:

name (str, optional) – The custom name of the backup file to restore from. If None, it defaults to <filename>.<extension>.backup.

Returns:

None

Return type:

None

save()[source]

Saves the current content of the configuration file, as stored in the internal parser objects to the file system.

Returns:

None

Return type:

None

set(key, value, section=None, separator='=')[source]

Modifies a specific key-value pair within the configuration file. Modification is made through the ConfigParser object if it is defined. If not, then key value pair would be written to the file through normal write() method with composed string in the following form <key><separator><value>

Parameters:
  • key (str) – The key whose value will be updated.

  • value (Union[int, str, bool]) – The new value to be stored for the specified key.

  • section (str, optional) – The name of the section within the config file where the key is located. If None, the file is treated as a simple key-value file without sections.

  • separator (str) – The character used to separate the key and value in simple (non-ConfigParser) config files. Defaults to =. Spaces around the key should be included as part of the separator if needed.

Returns:

None

Return type:

None

Raises:

ValueError – If a line in a simple config file is not in an expected key=value format.

class OpensslCnf(filepath, conf_type, replace)[source]

This class manages OpenSSL configuration files (.cnf files), providing methods to create and modify their content. It supports different types of configuration files (e.g., for CAs, for users) by utilizing specific templates and performing string replacements.

Initializes an OpensslCnf object, setting up the paths for the configuration file and its corresponding template. It also prepares the strings that will be used for replacement within the template based on the conf_type.

Parameters:
  • filepath (Union[str, pathlib.Path]) – The path where the OpenSSL configuration file will be saved.

  • conf_type (str) – An identifier string indicating the type of configuration file (e.g., “CA” or “user”), which determines the template and replacement patterns.

  • replace (Union[str, list]) – A string or list of strings that will replace specific placeholder strings (e.g., “{ROOT_DIR}”, “{user}”) from the template.

Returns:

None

Return type:

None

create()[source]

Populates the internal content attribute by reading the template file and performing string replacements based on the initialized old and new strings. This prepares the content to be written to the actual configuration file.

Returns:

None

Return type:

None

save()[source]

Saves the content stored in the internal content attribute (or parser if set was used) to the OpenSSL configuration file on the file system.

Returns:

None

Return type:

None

class SSSDConf[source]

This class manages the /etc/sssd/sssd.conf file, providing methods to create, modify, save, and restore its content.

It is implemented as a singleton, ensuring a single representation of the SSSD configuration during runtime.

It also acts as a context manager to temporarily apply and then revert SSSD configuration changes.

Intended use is to create/update and save config file in first runtime and load content of config file to internal parser object in following runtimes.

Initializes the SSSDConf instance, setting up its configuration file paths and internal parsers. It loads default content and checks for existing backup files to maintain state across runs.

check_backups()[source]

Checks if internal backup files for sssd.conf already exist. If any backup file is found, it raises an exception, suggesting that the create method might have been executed multiple times unintentionally.

Returns:

None

Return type:

None

Raises:

FileExistsError – If an internal backup file already exists.

create()[source]

Populates the internal parser object with content from the existing sssd.conf file, then updates it with values from a predefined template. It also backs up the original sssd.conf file. This method handles cases where the file might not initially exist.

Returns:

None

Return type:

None

Raises:

FileExistsError – If internal backup files already exist, suggesting create was executed multiple times.

restore()[source]

Restores the sssd.conf file to its original version before any modifications by SCAutolib. If a backup exists, it is copied back; otherwise, the file is simply removed if it was created by SCAutolib. It also removes internal backup files.

Returns:

None

Return type:

None

save()[source]

Saves the current content of the SSSD configuration file, which is managed by the internal parser objects. The file permissions are set to 0o600.

Returns:

None

Return type:

None

set(key, value, section=None)[source]

Modifies or adds a key-value pair within the SSSD configuration file represented by the internal ConfigParser object.

Parameters:
  • key (str) – The key from a section of the config file to be updated.

  • value (Union[int, str, bool]) – The new value to be stored in the specified section and key.

  • section (str) – The section of the config file to be created or updated. This parameter is required for SSSDConf operations.

Returns:

None

Return type:

None

update_default_content()[source]

Populates the internal parser with the content from the current sssd.conf file on the system. It then backs up this current state.

Returns:

None

Return type:

None

class SoftHSM2Conf(filepath, card_dir)[source]

This class manages the softhsm2.conf file, providing methods to create its content based on a template and save it. It’s specifically designed for SoftHSM2 configuration, which does not use traditional sections.

Initializes a SoftHSM2Conf object, setting the path for the configuration file and the card directory, which is used to format the template content.

Parameters:
  • filepath (Union[str, pathlib.Path]) – The path where the softhsm2.conf file should be saved.

  • card_dir (Union[str, pathlib.Path]) – The path to the card’s directory, which will be inserted into the softhsm2.conf template (e.g., for directories.tokendir).

Returns:

None

Return type:

None

create()[source]

Populates the internal content attribute by reading the softhsm2.conf template and formatting it with the provided card_dir.

Returns:

None

Return type:

None

save()[source]

Saves the content stored in the internal content attribute to the softhsm2.conf file on the file system.

Returns:

None

Return type:

None

set(*args)[source]

This method is not implemented for SoftHSM2Conf as softhsm2.conf does not use sections in a way that File.set can handle.

Parameters:

args (tuple) – Positional arguments (not used).

Returns:

None

Return type:

None

Raises:

NotImplementedError – Always raised when this method is called.