SCAutolib.utils

This module provides a collection of utility and helper functions utilized across the SCAutolib library. These functions are specifically designed to support various internal demands of the library, including system checks, package management, key/certificate handling, and data serialization. They are not intended as general-purpose utilities but as specialized aids tailored to SCAutolib’s operations.

Functions

ca_factory(path=None, cnf=None, card_data=None, ca_name=None, create=False)[source]

A factory function to create or load Certificate Authority (CA) objects based on the provided parameters. It can initialize a new CA instance or load an existing one from a JSON dump file.

Parameters:
  • path (pathlib.Path, optional) – The pathlib.Path object to the CA’s root directory. This is used when creating a new LocalCA instance.

  • cnf (SCAutolib.models.file.OpensslCnf, optional) – An OpensslCnf object representing the OpenSSL configuration file for the CA. Used when creating a new LocalCA.

  • card_data (dict, optional) – A dictionary containing various attributes of the card (e.g., PIN, cardholder, slot). This data is used when creating a new CustomCA for physical cards.

  • ca_name (str, optional) – The name of the CA to load. This parameter is used when create is False to identify the specific CA JSON dump file.

  • create (bool) – If True, a new CA object will be created (either LocalCA or CustomCA). If False, an existing CA object will be loaded from a dump file.

Returns:

An initialized CA object (either LocalCA, CustomCA, or IPAServerCA instance).

Return type:

SCAutolib.models.CA.BaseCA

dump_to_json(obj)[source]

Serializes a given object into a JSON file, using the object’s to_dict() method for serialization and its dump_file attribute to determine the output path. This is used to persist the state of SCAutolib’s internal objects (like users, CAs, and cards) across different runs.

Parameters:

obj (object) – The object to be serialized. It must have a to_dict() method and a dump_file attribute.

Returns:

None

ipa_factory()[source]

Creates and returns an IPAServerCA object. This function loads the IPA server CA configuration from its JSON dump file. It specifically asserts that the loaded CA is an instance of IPAServerCA.

Returns:

An initialized IPAServerCA object.

Return type:

SCAutolib.models.CA.IPAServerCA

Raises:

SCAutolibException – If the IPA server CA dump file is not found or if the loaded object is not a valid IPAServerCA instance.

load_token(card_name=None, update_sssd=False)[source]

Loads a Card object from a JSON dump file based on the provided card name. This function is primarily intended for use in pytest configurations to set up card objects for tests. Optionally, it can update the SSSD configuration file (sssd.conf) with a shadowutils rule for the user of the loaded card.

Parameters:
  • card_name (str, optional) – The name of the card object to load.

  • update_sssd (bool) – If True, the SSSD configuration file will be updated with a shadowutils rule for the user of the loaded card.

Returns:

The loaded Card object

Return type:

SCAutolib.models.card.Card

load_user(username, **kwargs)[source]

Loads a User object from a JSON dump file corresponding to the given username. The file is expected to be located in LIB_DUMP_USERS directory.

Parameters:
  • username (str) – The username of the user to load.

  • kwargs (dict) – Additional keyword arguments that might be required by the User.load static method, particularly for IPAUser objects (e.g., ipa_server object).

Returns:

The loaded User object (either User or IPAUser instance).

Return type:

SCAutolib.models.user.User

Raises:

SCAutolibException – If the user’s JSON dump file does not exist.