Skip to content

Commit

Permalink
Initial test for testing secure-boot grain
Browse files Browse the repository at this point in the history
  • Loading branch information
dmurphy18 committed Oct 15, 2024
1 parent f6f8251 commit f933a37
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
14 changes: 11 additions & 3 deletions salt/grains/extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,20 @@ def __secure_boot(efivars_dir):
return enabled


def uefi():
"""Populate UEFI grains."""
efivars_dir = next(
def get_secure_boot_path():
"""
Provide paths for secure boot directories and files
"""
efivars_path = next(
filter(os.path.exists, ["/sys/firmware/efi/efivars", "/sys/firmware/efi/vars"]),
None,
)
return efivars_path


def uefi():
"""Populate UEFI grains."""
efivars_dir = get_secure_boot_path()
grains = {
"efi": bool(efivars_dir),
"efi-secure-boot": __secure_boot(efivars_dir) if efivars_dir else False,
Expand Down
51 changes: 51 additions & 0 deletions tests/pytests/functional/grains/test_secure_boot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""
:codeauthor: :email:`David Murphy <[email protected]`
"""

## import logging
import os
import tempfile

import pytest

import salt.utils.files
import salt.utils.path
from tests.support.mock import patch

pytestmark = [
pytest.mark.skip_unless_on_linux(reason="Only supported on Linux family"),
]

## log = logging.getLogger(__name__)


def test_secure_boot_efivars():
_salt_utils_files_fopen = salt.utils.files.fopen

with tempfile.TemporaryDirectory() as tempdir:
secure_boot_path = os.path.join(tempdir, "secure-boot/efivars")

print(
f"DGM test_secure_boot_efivars, secure_boot_path '{secure_boot_path}'",
flush=True,
)

with _salt_utils_files_fopen(
os.path.join(secure_boot_path, "/SecureBoot-dog", "wb+")
) as fd:
binary_data = b"\x06\x00\x00\x00\x01"
fd.write(binary_data)

secure_boot_path_file = os.path.join(secure_boot_path, "/SecureBoot-dog")
print(
f"DGM test_secure_boot_efivars secure_boot_path file '{secure_boot_path_file}'",
flush=True,
)

with patch("salt.grains.extra.get_secure_boot_path", return_value=secure_boot_path):
grains = salt.grains.extra.uefi()

print(f"DGM test_secure_boot_efivars grains '{grains}'", flush=True)

expected = {"efi": True, "efi-secure-boot": True}
assert grains == expected

0 comments on commit f933a37

Please sign in to comment.