Chapter 1

Back End

All python documentation are generated with the pydoc-markdown

Subsections of Back End

utils.supported_systems.py

SingletonSupportedSystems Objects

class SingletonSupportedSystems()

This class is a sigleton object for SupportedSystems class

SupportedSystems Objects

class SupportedSystems(SingletonSupportedSystems)

This class saves and checks path of the env selected by user.

reset_params

def reset_params()

Reset varibles of path, used when they are errors

get_entire_path

def get_entire_path()

The method checks and return the complete environment path else raise Exception

Raises:

  • PathDoesNotExist - If the specified path does not exist
  • VariablePathNotDefined - If variables are not filled

Returns:

  • str - The complete environment path selected by the user

set_playbooks_location

def set_playbooks_location(path: str)

Check and set the location of the playbook directory

Arguments:

  • path str - The path of the playbook location

Raises:

  • PathDoesNotExist - If the specified path does not exist
  • VariablePathNotDefined - If variables are not filled

get_os

def get_os() -> list[str]

Search for OS directories contained in the playbook directory

Raises:

  • VariablePathNotDefined - If variable are not filled

Returns:

  • list[str] - The list of OS availables in {playbook}/

get_os_type

def get_os_type() -> list[str]

Search for OS type directories contained in the OS directory selected

Raises:

  • PathDoesNotExist - If the specified path does not exist
  • VariablePathNotDefined - If variables are not filled

Returns:

  • list[str] - The list of OS type availables in {playbook}/{OS}/

get_os_version

def get_os_version() -> list[str]

Search for OS version directories contained in the OS type selected

Raises:

  • PathDoesNotExist - If the specified path does not exist
  • VariablePathNotDefined - If variables are not filled

Returns:

  • list[str] - The list of OS version availables in {playbook}/{OS}/{OS_TYPE}/

set_os

def set_os(os: str) -> None

Set the OS selected by the user

Arguments:

  • os str - OS name selected

Raises:

  • VariablePathNotDefined - If variables are not filled

set_os_type

def set_os_type(os_type: str)

Set the OS type selected by the user

Arguments:

  • os_type str - OS type name selected

Raises:

  • PathDoesNotExist - If the specified path does not exist
  • VariablePathNotDefined - If variables are not filled

set_os_version

def set_os_version(os_version: str)

Set the OS version selected by the user

Arguments:

  • os_version str - OS version name selected

Raises:

  • PathDoesNotExist - If the specified path does not exist
  • VariablePathNotDefined - If variables are not filled

utils.recommendations_selected.py

SingletonRecommendationsSelected Objects

class SingletonRecommendationsSelected()

This class is a sigleton object for RecommendationSelected class

RecommendationsSelected Objects

class RecommendationsSelected(SingletonRecommendationsSelected)

This class keep in memory which recommendations has been selected

utils.questions_parser.py

read_questions_file

def read_questions_file(path: str) -> dict

Take a recommendation path and read the questions.yml file linked to

Arguments:

  • path str - Path of the recommendation

Raises:

  • PathDoesNotExist - If the path {path}/questions.yml does not exist

Returns:

  • dict - A dict that represents the questions.yml file

list_categories

def list_categories(supported_systems: SupportedSystems) -> list[str]

List categories contained in the environment selected by the user

Arguments:

  • supported_systems SupportedSystems - singleton that contains the user env selection

Raises:

  • PathDoesNotExist - If the specified path does not exist
  • VariablePathNotDefined - If variables are not filled

Returns:

  • list[str] - The list of categories in the path

list_reference

def list_reference(category: str,
                   supported_systems: SupportedSystems) -> list[str]

List all reference base (ANSSI, CIS, etc) from a category

Arguments:

  • category str - The category to list
  • supported_systems SupportedSystems - singleton that contains the user env selection

Returns:

  • list[str] - the list of references contained in the category

list_recommendations

def list_recommendations(category: str, reference: str,
                         supported_systems: SupportedSystems) -> list[str]

List recommendation available in the reference directory in a category

Arguments:

  • category str - One of the category available in env selected
  • reference str - The reference to list
  • supported_systems SupportedSystems - singleton that contains the user env selection

Raises:

  • PathDoesNotExist - If the specified path does not exist
  • VariablePathNotDefined - If variables are not filled

Returns:

  • list[str] - The list of recommendations in the reference dir from the category

is_type_ok

def is_type_ok(type_asked: str, answer) -> bool

This method check is the type provided by the user is correct

Arguments:

  • type_asked str - type asked in the questions.yml file
  • answer type - answer provided by the user

Returns:

  • bool - True if the type corresponds, else False

check_answers

def check_answers(r_path: str, answer_list: list[dict]) -> dict[str]

Take the answer provided by the user and check if it is conform in comparaison of the questions.yml. It check, the type, the real format, if value exists in case of required “true”. If everything is correct, return the dict object to inject in the playbook template (playbook.yml.j2).

Arguments:

  • r_path str - path of the recommendation
  • answer_list list[dict] - list of the answers provided by the user

Raises:

  • AnswerIsRequired - If the answers is present but no value
  • WrongAnswerType - If the type provived textually of in object instance is wrong
  • PathDoesNotExist - If the specified path does not exist
  • IndexError - If there are missing answers

Returns:

  • dict[str] - The answers the inject in playbook template

utils.playbook_runner.py

run_ansible_playbook

def run_ansible_playbook()

This function call runner function from ansible to run the playbook.master.yml with the inventory.yml

Raises:

  • PathDoesNotExist - If the path of playbook.master.yml or inventory.yml does not exist

utils.playbook_renderer.py

playbook_render_write

def playbook_render_write(dir_path: str, variables: dict)

This function take the playbook.yml.j2 template to inject into all answers from user input. After this, the function is writing the playbook as ‘playbook.yml’ in the directory

Arguments:

  • dir_path str - The path of the recommendation where template is stored
  • variables dict - A dict containing variable names and variable values to render in the template

Raises:

  • PathDoesNotExist - If the specified path does not exist

utils.path.py

list_dir_in_dir

def list_dir_in_dir(path: str) -> list[str]

This method is a os.listdir wrapper to return only directories without the .git dir

Arguments:

  • path str - Dir to list

Returns:

  • list[str] - List of the directories contained in path

utils.id_management.py

SingletonRecommendationID Objects

class SingletonRecommendationID()

Sigleton of the RecommendationID class

RecommendationID Objects

class RecommendationID(SingletonRecommendationID)

This class manage the ID of each recommendation. To avoid to put ID in recommendation directories and files, the class RecommendationID manage dynamically ID by adding missing pair ID/path in the ID file. Futhermore, all ID are UUID from the uuid.uuid4()

set_playbooks_location

def set_playbooks_location(path: str)

Check and set the location of the playbook directory

Arguments:

  • path str - The path of the playbook location

Raises:

  • PathDoesNotExist - If the specified path does not exist
  • VariablePathNotDefined - If variables are not filled

set_id_file_location

def set_id_file_location(path: str)

Set the location of the ID/PATH pair file

Arguments:

  • path str - Path of the file

Raises:

  • VariablePathNotDefined - If variables are not filled

attribute_new_playbooks

def attribute_new_playbooks(all_recommendation_paths: list[str])

Add missing pair ID/PATH in the file. The pair ID/PATH are not deleted when a playbook is removed.

Arguments:

  • all_recommendation_paths list[str] - list of all recommendation paths

get_available_playbooks

def get_available_playbooks() -> list[str]

browse all folders in the playbook folder to retrieve all recommendation paths

Returns:

  • list[str] - all recommendation paths

get_id_from_path

def get_id_from_path(path: str) -> str

Translate a path to an ID. The ID is used mainly in the front-end

Arguments:

  • path str - path to translate

Raises:

  • IDDoesNotExist - If the path doesn’t have an ID
  • PathDoesNotExist - If the specified path does not exist
  • VariablePathNotDefined - If variables are not filled

Returns:

  • str - The path’s ID

get_path_from_id

def get_path_from_id(id: str) -> str

Translate an ID to a path. The path is used mainly in the back-end

Arguments:

  • id str - The id to translate

Raises:

  • IDDoesNotExist - If the ID does not exist
  • VariablePathNotDefined - If variables are not filled

Returns:

  • str - The ID’s path

utils.hosts_selected.py

SingletonHostsSelected Objects

class SingletonHostsSelected()

This class is a sigleton object for HostsSelected class

Host Objects

class Host()

This class represents one host object to export it to yml for the ansible inventory

__init__

def __init__(hostname: str, host_ip: str, host_port: int)

Create Host instance and fill hostname, host_ip and host_port

Arguments:

  • hostname str - The name of the host
  • host_ip str - The ip or fqdn of the host
  • host_port int - The ssh port of the host

Raises:

  • ValueError - If there are missing value, raise the Exception

set_connection_method

def set_connection_method(connection_method: int, username: str,
                          pass_or_keyfile: str)

Fill connection_method, username and pass_or_keyfile.

Arguments:

  • connection_method HostConnectionMethod - Value from the Enum, define user/password or user/keyfile connection method
  • username str - user to connect on host using ssh
  • pass_or_keyfile str - password or the path of the keyfile to connect on host using ssh

Raises:

  • ValueError - If there are missing value, raise the Exception

set_sudo_access

def set_sudo_access(sudo_username: str, sudo_password: str)

Fill sudo_username and sudo_password to permits privilege escalation

Arguments:

  • sudo_username str - username of a user with sudo privilege
  • sudo_password str - password of a user with sudo privilege

Raises:

  • ValueError - If there are missing value, raise the Exception

get_yml

def get_yml() -> str

Render the Host instance into a string with yml syntax for the Ansible inventory file

Raises:

  • ValueError - If the value of connection_method is not in the Enum

Returns:

  • str - The yml string

HostsSelected Objects

class HostsSelected(SingletonHostsSelected)

This class keep in memory which hosts are selected and their configuration

add_host

def add_host(host: dict)

Create a host and add it to the list of hosts

Arguments:

  • host dict - Dict that contains value to add host

Raises:

HostAlreadyAdded : If the hostname already exists

  • ValueError - If there are missing value, raise the Exception

is_hostname_unique

def is_hostname_unique(new_hostname: str) -> bool

Check if the hostname has already been added

Arguments:

  • new_hostname str - The hostname to check

Raises:

  • ValueError - If there are missing value, raise the Exception

Returns:

  • bool - True if the hostname is unique else False

utils.configuration.py

SingletonConfiguration Objects

class SingletonConfiguration()

Sigleton of the Configuration class

Configuration Objects

class Configuration(SingletonConfiguration)

This class read configuration file and retrieve variables. If a variable is not present the variable is set with a default value.

get

def get(config_key)

return the config value of the key specified in arg

read_configuration

def read_configuration()

Read the configuration file and set required variables