Skip to content

Commit

Permalink
edns: name the options --filename and --dir
Browse files Browse the repository at this point in the history
It describes their purpose better than --name and --path.
  • Loading branch information
rvykydal committed Dec 10, 2024
1 parent 0352cf7 commit e564a96
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 60 deletions.
20 changes: 10 additions & 10 deletions dracut/parse-kickstart
Original file line number Diff line number Diff line change
Expand Up @@ -406,20 +406,20 @@ def ksnet_to_dracut(args, lineno, net, bootdev=False):
return " ".join(line)


def _dump_certificate(cert, root="/", path=None):
def _dump_certificate(cert, root="/", dump_dir=None):
"""Dump the certificate into specified file."""
path = path or cert.path
if not path:
log.error("Certificate destination is missing for %s", cert.name)
dump_dir = dump_dir or cert.dir
if not dump_dir:
log.error("Certificate destination is missing for %s", cert.filename)
return

dst_dir = os.path.join(root+path.lstrip('/'))
log.debug("Dumping certificate %s into %s.", cert.name, dst_dir)
dst_dir = os.path.join(root+dump_dir.lstrip('/'))
log.debug("Dumping certificate %s into %s.", cert.filename, dst_dir)
if not os.path.exists(dst_dir):
log.debug("Path %s for certificate does not exist, creating.", dst_dir)
os.makedirs(dst_dir)

dst = os.path.join(dst_dir, cert.name)
dst = os.path.join(dst_dir, cert.filename)

if os.path.exists(dst):
log.warning("Certificate file %s already exists, replacing.", dst)
Expand All @@ -433,10 +433,10 @@ def process_certificates(handler):
"""Import certificates defined in %certificate sections."""
for cert in handler.certificates:

log.info("Processing kickstart certificate %s", cert.name)
log.info("Processing kickstart certificate %s", cert.filename)

if not cert.name:
log.error("Missing certificate name, skipping.")
if not cert.filename:
log.error("Missing certificate file name, skipping.")
continue

_dump_certificate(cert)
Expand Down
28 changes: 14 additions & 14 deletions pyanaconda/modules/common/structures/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ class CertificateData(DBusData):
"""Structure for the certificate data."""

def __init__(self):
self._name = ""
self._filename = ""
self._cert = ""
self._path = ""
self._dir = ""

@property
def name(self) -> Str:
"""The certificate name."""
return self._name
def filename(self) -> Str:
"""The certificate file name."""
return self._filename

@name.setter
def name(self, value: Str) -> None:
self._name = value
@filename.setter
def filename(self, value: Str) -> None:
self._filename = value

@property
def cert(self) -> Str:
Expand All @@ -49,10 +49,10 @@ def cert(self, value: Str) -> None:
self._cert = value

@property
def path(self) -> Str:
"""The certificate path."""
return self._path
def dir(self) -> Str:
"""The certificate directory."""
return self._dir

@path.setter
def path(self, value: Str) -> None:
self._path = value
@dir.setter
def dir(self, value: Str) -> None:
self._dir = value
8 changes: 4 additions & 4 deletions pyanaconda/modules/security/certificates/certificates.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ def process_kickstart(self, data):
certificates = []
for cert in data.certificates:
cert_data = CertificateData()
cert_data.name = cert.name
cert_data.filename = cert.filename
cert_data.cert = cert.cert
if cert.path:
cert_data.path = cert.path
if cert.dir:
cert_data.dir = cert.dir
certificates.append(cert_data)
self.set_certificates(certificates)

def setup_kickstart(self, data):
"""Setup the kickstart data."""
for cert in self._certificates:
cert_ksdata = Certificate(cert=cert.cert, name=cert.name, path=cert.path)
cert_ksdata = Certificate(cert=cert.cert, filename=cert.filename, dir=cert.dir)
data.certificates.append(cert_ksdata)

@property
Expand Down
8 changes: 4 additions & 4 deletions pyanaconda/modules/security/certificates/installation.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ def name(self):
def _dump_certificate(self, cert, root):
"""Dump the certificate into specified file and directory."""

if not cert.path:
if not cert.dir:
raise SecurityInstallationError(
"Certificate destination is missing for {}".format(cert.name)
"Certificate destination is missing for {}".format(cert.filename)
)

dst_dir = join_paths(root, cert.path)
dst_dir = join_paths(root, cert.dir)
if not os.path.exists(dst_dir):
log.debug("Path %s for certificate does not exist, creating.", dst_dir)
make_directories(dst_dir)

dst = join_paths(dst_dir, cert.name)
dst = join_paths(dst_dir, cert.filename)

if os.path.exists(dst):
log.warning("Certificate file %s already exists, replacing.", dst)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ def test_certificates_property(self):
certs_value = [
{
'cert': get_variant(Str, CERT1_CERT),
'name': get_variant(Str, 'rvtest.pem'),
'path': get_variant(Str, '/etc/pki/ca-trust/extracted/pem')
'filename': get_variant(Str, 'rvtest.pem'),
'dir': get_variant(Str, '/etc/pki/ca-trust/extracted/pem'),
},
{
'cert': get_variant(Str, CERT2_CERT),
'name': get_variant(Str, 'rvtest2.pem'),
'path': get_variant(Str, '')
'filename': get_variant(Str, 'rvtest2.pem'),
'dir': get_variant(Str, ''),
}
]
self._check_dbus_property(
Expand All @@ -106,13 +106,13 @@ def test_import_with_task_default(self, publisher):

def _get_2_test_certs(self):
cert1 = CertificateData()
cert1.name = "cert1.pem"
cert1.filename = "cert1.pem"
cert1.cert = CERT1_CERT
cert1.path = "/cert/drop/directory1"
cert1.dir = "/cert/drop/directory1"
cert2 = CertificateData()
cert2.name = "cert2.pem"
cert2.filename = "cert2.pem"
cert2.cert = CERT2_CERT
cert2.path = "/cert/drop/directory2"
cert2.dir = "/cert/drop/directory2"
return(cert1, cert2)

@patch_dbus_publish_object
Expand All @@ -127,9 +127,9 @@ def test_import_with_task_configured(self, publisher):
obj = check_task_creation(task_path, publisher, ImportCertificatesTask)
assert obj.implementation._sysroot == "/"
assert len(obj.implementation._certificates) == 2
t_cert1, t_cert2 = obj.implementation._certificates
assert (t_cert1.name, t_cert1.path, t_cert1.cert) == (cert1.name, cert1.path, cert1.cert)
assert (t_cert2.name, t_cert2.path, t_cert2.cert) == (cert2.name, cert2.path, cert2.cert)
c1, c2 = obj.implementation._certificates
assert (c1.filename, c1.dir, c1.cert) == (cert1.filename, cert1.dir, cert1.cert)
assert (c2.filename, c2.dir, c2.cert) == (cert2.filename, cert2.dir, cert2.cert)

@patch_dbus_publish_object
def test_install_with_task_default(self, publisher):
Expand All @@ -151,9 +151,9 @@ def test_install_with_task_configured(self, publisher):
obj = check_task_creation(task_path, publisher, ImportCertificatesTask)
assert obj.implementation._sysroot == "/mnt/sysroot"
assert len(obj.implementation._certificates) == 2
t_cert1, t_cert2 = obj.implementation._certificates
assert (t_cert1.name, t_cert1.path, t_cert1.cert) == (cert1.name, cert1.path, cert1.cert)
assert (t_cert2.name, t_cert2.path, t_cert2.cert) == (cert2.name, cert2.path, cert2.cert)
c1, c2 = obj.implementation._certificates
assert (c1.filename, c1.dir, c1.cert) == (cert1.filename, cert1.dir, cert1.cert)
assert (c2.filename, c2.dir, c2.cert) == (cert2.filename, cert2.dir, cert2.cert)

@patch_dbus_publish_object
def test_pre_install_with_task_default(self, publisher):
Expand All @@ -175,18 +175,18 @@ def test_pre_install_with_task_configured(self, publisher):
obj = check_task_creation(task_path, publisher, ImportCertificatesTask)
assert obj.implementation._sysroot == "/mnt/sysroot"
assert len(obj.implementation._certificates) == 2
t_cert1, t_cert2 = obj.implementation._certificates
assert (t_cert1.name, t_cert1.path, t_cert1.cert) == (cert1.name, cert1.path, cert1.cert)
assert (t_cert2.name, t_cert2.path, t_cert2.cert) == (cert2.name, cert2.path, cert2.cert)
c1, c2 = obj.implementation._certificates
assert (c1.filename, c1.dir, c1.cert) == (cert1.filename, cert1.dir, cert1.cert)
assert (c2.filename, c2.dir, c2.cert) == (cert2.filename, cert2.dir, cert2.cert)

def test_import_certificates_task_files(self):
"""Test the ImportCertificatesTask task"""
cert1, cert2 = self._get_2_test_certs()

with tempfile.TemporaryDirectory() as sysroot:
# cert1 has existing path
os.makedirs(sysroot+cert1.path)
# cert2 has non-existing path
# cert1 has existing dir
os.makedirs(sysroot+cert1.dir)
# cert2 has non-existing dir

ImportCertificatesTask(
sysroot=sysroot,
Expand All @@ -197,7 +197,7 @@ def test_import_certificates_task_files(self):
self._check_cert_file(cert2, sysroot)

def _check_cert_file(self, cert, sysroot, missing=False):
cert_file = sysroot + cert.path + "/" + cert.name
cert_file = sysroot + cert.dir + "/" + cert.filename
if missing:
assert os.path.exists(cert_file) is False
else:
Expand All @@ -211,8 +211,8 @@ def test_import_certificates_task_existing_file(self):

with tempfile.TemporaryDirectory() as sysroot:
# certificate file to be dumped already exists
os.makedirs(sysroot+cert1.path)
cert1_file = sysroot + cert1.path + "/" + cert1.name
os.makedirs(sysroot+cert1.dir)
cert1_file = sysroot + cert1.dir + "/" + cert1.filename
open(cert1_file, 'w')

ImportCertificatesTask(
Expand All @@ -225,7 +225,7 @@ def test_import_certificates_task_existing_file(self):
def test_import_certificates_missing_destination(self):
"""Test the ImportCertificatesTask task with missing destination"""
cert1, _ = self._get_2_test_certs()
cert1.path = ''
cert1.dir = ''

with tempfile.TemporaryDirectory() as sysroot:
with self.assertRaises(SecurityInstallationError):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def test_realm_kickstart(self):
def test_certificates_kickstart(self):
"""Test the %certificates section."""
ks_in = """
%certificate --name=rvtest.pem --path=/cert_path
%certificate --filename=rvtest.pem --dir=/cert_dir
-----BEGIN CERTIFICATE-----
MIIBjTCCATOgAwIBAgIUWR5HO3v/0I80Ne0jQWVZFODuWLEwCgYIKoZIzj0EAwIw
FDESMBAGA1UEAwwJUlZURVNUIENBMB4XDTI0MTEyMDEzNTk1N1oXDTM0MTExODEz
Expand All @@ -173,7 +173,7 @@ def test_certificates_kickstart(self):
-----END CERTIFICATE-----
%end
%certificate --name=rvtest2.pem --path=/cert_path2
%certificate --filename=rvtest2.pem --dir=/cert_dir2
-----BEGIN CERTIFICATE-----
MIIBkTCCATegAwIBAgIUN6r4TjFJqP/TS6U25iOGL2Wt/6kwCgYIKoZIzj0EAwIw
FjEUMBIGA1UEAwwLUlZURVNUIDIgQ0EwHhcNMjQxMTIwMTQwMzIxWhcNMzQxMTE4
Expand All @@ -188,7 +188,7 @@ def test_certificates_kickstart(self):
%end
"""
ks_out = """
%certificate --name=rvtest.pem --path=/cert_path
%certificate --filename=rvtest.pem --dir=/cert_dir
-----BEGIN CERTIFICATE-----
MIIBjTCCATOgAwIBAgIUWR5HO3v/0I80Ne0jQWVZFODuWLEwCgYIKoZIzj0EAwIw
FDESMBAGA1UEAwwJUlZURVNUIENBMB4XDTI0MTEyMDEzNTk1N1oXDTM0MTExODEz
Expand All @@ -202,7 +202,7 @@ def test_certificates_kickstart(self):
-----END CERTIFICATE-----
%end
%certificate --name=rvtest2.pem --path=/cert_path2
%certificate --filename=rvtest2.pem --dir=/cert_dir2
-----BEGIN CERTIFICATE-----
MIIBkTCCATegAwIBAgIUN6r4TjFJqP/TS6U25iOGL2Wt/6kwCgYIKoZIzj0EAwIw
FjEUMBIGA1UEAwwLUlZURVNUIDIgQ0EwHhcNMjQxMTIwMTQwMzIxWhcNMzQxMTE4
Expand Down

0 comments on commit e564a96

Please sign in to comment.