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
Fileobject, 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.Pathobject.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.Pathobject indicating where the backup file is stored.- Return type:
- 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
createis called.
- exists()[source]
Checks if the configuration file managed by this object exists on the file system.
- Returns:
Trueif the file exists;Falseotherwise.- Return type:
- 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:
- 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.Pathobject representing the configuration file managed by this object.- Returns:
The path of the configuration file.
- Return type:
- 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=valueformat.
- class OpensslCnf(filepath, conf_type, replace)[source]
This class manages OpenSSL configuration files (
.cnffiles), 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
OpensslCnfobject, 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
- class SSSDConf[source]
This class manages the
/etc/sssd/sssd.conffile, 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
SSSDConfinstance, 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.confalready exist. If any backup file is found, it raises an exception, suggesting that thecreatemethod 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.conffile, then updates it with values from a predefined template. It also backs up the originalsssd.conffile. 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
createwas executed multiple times.
- restore()[source]
Restores the
sssd.conffile 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
- class SoftHSM2Conf(filepath, card_dir)[source]
This class manages the
softhsm2.conffile, 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
SoftHSM2Confobject, 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.conffile should be saved.card_dir (Union[str, pathlib.Path]) – The path to the card’s directory, which will be inserted into the
softhsm2.conftemplate (e.g., fordirectories.tokendir).
- Returns:
None
- Return type:
None
- create()[source]
Populates the internal content attribute by reading the
softhsm2.conftemplate and formatting it with the providedcard_dir.- Returns:
None
- Return type:
None
- save()[source]
Saves the content stored in the internal content attribute to the
softhsm2.conffile on the file system.- Returns:
None
- Return type:
None
- set(*args)[source]
This method is not implemented for
SoftHSM2Confassofthsm2.confdoes not use sections in a way thatFile.setcan handle.- Parameters:
args (tuple) – Positional arguments (not used).
- Returns:
None
- Return type:
None
- Raises:
NotImplementedError – Always raised when this method is called.