Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dump and load methods #87

Merged
merged 11 commits into from
Aug 9, 2022
16 changes: 13 additions & 3 deletions SCAutolib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@


def run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True,
print_=True, **kwargs) -> subprocess.CompletedProcess:
print_=True, return_code: list = None, **kwargs) \
-> subprocess.CompletedProcess:
"""
Wrapper for subrpocess.run function. This function explicitly set several
parameter of original function and also provides similar thing as
Expand All @@ -36,6 +37,10 @@ def run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True,
subprocess.run function needed to be passed to this wrapper, you can do
it by adding same parameters names in key=value format.

:param return_code: acceptable return codes from given commands.
If check=True, and the return code of the cmd is not in the return_code
list an subprocess.CalledProcessError exception would be raised.
:type return_code: list
:param cmd: Command to be executed
:type cmd: list or str
:param stdout: Redirection of stdout. Default is subprocess.PIPE
Expand All @@ -58,6 +63,8 @@ def run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True,
:return: Completed process from subprocess.run
:rtype: subprocess.CompletedProcess
"""
if return_code is None:
return_code = [0]
if type(cmd) == str:
cmd = cmd.split(" ")
logger.debug(f"run: {' '.join([str(i) for i in cmd])}")
Expand All @@ -69,6 +76,9 @@ def run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True,
if out.stderr != "":
logger.warning(out.stderr)

if check and out.returncode != 0:
raise subprocess.CalledProcessError(out.returncode, cmd)
if check:
if out.returncode not in return_code:
logger.error(f"Unexpected return code {out.returncode}. "
f"Expected: {return_code}")
raise subprocess.CalledProcessError(out.returncode, cmd)
return out
29 changes: 29 additions & 0 deletions SCAutolib/cli_commands.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@
import click
from SCAutolib.controller import Controller


@click.group()
def cli():
pass


@click.command()
@click.option("--ca-type", "-t", required=False, default='all',
show_default=True,
help="Type of the CA to be configured. If not set, all CA's "
"from the config file would be configured")
ep69 marked this conversation as resolved.
Show resolved Hide resolved
@click.option("--conf-file", "-c", required=False, default="./conf.json",
type=click.Path(exists=True, resolve_path=True),
show_default=True)
def setup_ca(conf_file, ca_type):
cnt = Controller(conf_file, {"ip_addr": "10.10.10.10"})
ep69 marked this conversation as resolved.
Show resolved Hide resolved
cnt.setup_ipa_client()


@click.command()
ep69 marked this conversation as resolved.
Show resolved Hide resolved
@click.option("--conf", "-c", required=True)
@click.option("--force", "-f", required=False, default=False, is_flag=True)
@click.option("--gdm", "-g", required=False, default=False, is_flag=True)
@click.option("--install-missing", "-i", required=False, default=True,
is_flag=True)
def prepare(conf, force, gdm, install_missing):
cnt = Controller(conf)
cnt.prepare(force, gdm, install_missing)


cli.add_command(setup_ca)
cli.add_command(prepare)
6 changes: 3 additions & 3 deletions SCAutolib/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def setup_local_ca(self, force: bool = False):

ca_dir: Path = self.lib_conf["ca"]["local_ca"]["dir"]
cnf = file.OpensslCnf(ca_dir.joinpath("ca.cnf"), "CA", str(ca_dir))
self.local_ca = CA.LocalCA(root_dir=ca_dir)
self.local_ca = CA.LocalCA(root_dir=ca_dir, cnf=cnf)

if force:
logger.warning(f"Removing previous local CA in a directory "
Expand Down Expand Up @@ -256,7 +256,7 @@ def setup_user(self, user_dict: dict, force: bool = False):
hsm_conf.save()

new_card = card.VirtualCard(new_user)
new_card.softhsm2_conf = hsm_conf
new_card.softhsm2_conf = hsm_conf.path
else:
raise NotImplementedError("Other card type than 'virtual' does not "
"supported yet")
Expand Down Expand Up @@ -429,7 +429,7 @@ def _general_steps_for_ipa():
:return: name of the IPA client package for current Linux
"""
os_version = _get_os_version()
if os_version != OSVersion.RHEL_9:
if os_version not in (OSVersion.RHEL_9, OSVersion.CentOS_9):
run("dnf module enable -y idm:DL1")
run("dnf install @idm:DL1 -y")
logger.debug("idm:DL1 module is installed")
Expand Down
9 changes: 9 additions & 0 deletions SCAutolib/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@ class SCAutolibException(Exception):
"""
def __init__(self, *args):
super().__init__(*args)


class SCAutolibWrongConfig(SCAutolibException):
default = "Key/section for current operation is not present in the " \
"configuration file"

def __init__(self, msg=None):
msg = self.default if msg is None else msg
super().__init__(msg)
Loading