forked from p34709852/monkey
island: Consolidate duplicate code in test_island_config_options_validator
This commit is contained in:
parent
7fe10af1b2
commit
cf5b1378f2
|
@ -1,5 +1,6 @@
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
|
from collections.abc import Callable
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
@ -11,16 +12,7 @@ LINUX_READ_ONLY_BY_USER = 0o400
|
||||||
LINUX_RWX_BY_ALL = 0o777
|
LINUX_RWX_BY_ALL = 0o777
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
def certificate_test_island_config_options(crt_file, key_file):
|
||||||
def linux_island_config_options(tmpdir, create_empty_file):
|
|
||||||
crt_file = os.path.join(tmpdir, "test.crt")
|
|
||||||
create_empty_file(crt_file)
|
|
||||||
os.chmod(crt_file, LINUX_READ_ONLY_BY_USER)
|
|
||||||
|
|
||||||
key_file = os.path.join(tmpdir, "test.key")
|
|
||||||
create_empty_file(key_file)
|
|
||||||
os.chmod(key_file, LINUX_READ_ONLY_BY_USER)
|
|
||||||
|
|
||||||
return IslandConfigOptions(
|
return IslandConfigOptions(
|
||||||
{
|
{
|
||||||
"ssl_certificate": {
|
"ssl_certificate": {
|
||||||
|
@ -31,6 +23,26 @@ def linux_island_config_options(tmpdir, create_empty_file):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def linux_island_config_options(create_read_only_linux_file: Callable):
|
||||||
|
crt_file = create_read_only_linux_file("test.crt")
|
||||||
|
key_file = create_read_only_linux_file("test.key")
|
||||||
|
|
||||||
|
return certificate_test_island_config_options(crt_file, key_file)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def create_read_only_linux_file(tmpdir: str, create_empty_file: Callable) -> Callable:
|
||||||
|
def inner(file_name: str) -> str:
|
||||||
|
new_file = os.path.join(tmpdir, file_name)
|
||||||
|
create_empty_file(new_file)
|
||||||
|
os.chmod(new_file, LINUX_READ_ONLY_BY_USER)
|
||||||
|
|
||||||
|
return new_file
|
||||||
|
|
||||||
|
return inner
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.skipif(os.name != "posix", reason="Tests Posix (not Windows) permissions.")
|
@pytest.mark.skipif(os.name != "posix", reason="Tests Posix (not Windows) permissions.")
|
||||||
def test_linux_valid_crt_and_key_paths(linux_island_config_options):
|
def test_linux_valid_crt_and_key_paths(linux_island_config_options):
|
||||||
try:
|
try:
|
||||||
|
@ -73,25 +85,24 @@ def test_linux_key_path_insecure_permissions(linux_island_config_options):
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def windows_island_config_options(tmpdir, create_empty_file):
|
def windows_island_config_options(tmpdir: str, create_read_only_windows_file: Callable):
|
||||||
crt_file = os.path.join(tmpdir, "test.crt")
|
crt_file = create_read_only_windows_file("test.crt")
|
||||||
create_empty_file(crt_file)
|
key_file = create_read_only_windows_file("test.key")
|
||||||
cmd_to_change_permissions = get_windows_cmd_to_change_permissions(crt_file, "R")
|
|
||||||
subprocess.run(cmd_to_change_permissions, shell=True)
|
|
||||||
|
|
||||||
key_file = os.path.join(tmpdir, "test.key")
|
return certificate_test_island_config_options(crt_file, key_file)
|
||||||
create_empty_file(key_file)
|
|
||||||
cmd_to_change_permissions = get_windows_cmd_to_change_permissions(key_file, "R")
|
|
||||||
subprocess.run(cmd_to_change_permissions, shell=True)
|
|
||||||
|
|
||||||
return IslandConfigOptions(
|
|
||||||
{
|
@pytest.fixture
|
||||||
"ssl_certificate": {
|
def create_read_only_windows_file(tmpdir: str, create_empty_file: Callable) -> Callable:
|
||||||
"ssl_certificate_file": crt_file,
|
def inner(file_name: str) -> str:
|
||||||
"ssl_certificate_key_file": key_file,
|
new_file = os.path.join(tmpdir, file_name)
|
||||||
}
|
create_empty_file(new_file)
|
||||||
}
|
cmd_to_change_permissions = get_windows_cmd_to_change_permissions(new_file, "R")
|
||||||
)
|
subprocess.run(cmd_to_change_permissions, shell=True) # noqa DUO116
|
||||||
|
|
||||||
|
return new_file
|
||||||
|
|
||||||
|
return inner
|
||||||
|
|
||||||
|
|
||||||
def get_windows_cmd_to_change_permissions(file_name, permissions):
|
def get_windows_cmd_to_change_permissions(file_name, permissions):
|
||||||
|
@ -124,7 +135,7 @@ def test_windows_crt_path_insecure_permissions(windows_island_config_options):
|
||||||
cmd_to_change_permissions = get_windows_cmd_to_change_permissions(
|
cmd_to_change_permissions = get_windows_cmd_to_change_permissions(
|
||||||
windows_island_config_options.crt_path, "W"
|
windows_island_config_options.crt_path, "W"
|
||||||
)
|
)
|
||||||
subprocess.run(cmd_to_change_permissions, shell=True)
|
subprocess.run(cmd_to_change_permissions, shell=True) # noqa DUO116
|
||||||
|
|
||||||
with pytest.raises(InsecurePermissionsError):
|
with pytest.raises(InsecurePermissionsError):
|
||||||
raise_on_invalid_options(windows_island_config_options)
|
raise_on_invalid_options(windows_island_config_options)
|
||||||
|
@ -143,7 +154,7 @@ def test_windows_key_path_insecure_permissions(windows_island_config_options):
|
||||||
cmd_to_change_permissions = get_windows_cmd_to_change_permissions(
|
cmd_to_change_permissions = get_windows_cmd_to_change_permissions(
|
||||||
windows_island_config_options.key_path, "W"
|
windows_island_config_options.key_path, "W"
|
||||||
)
|
)
|
||||||
subprocess.run(cmd_to_change_permissions, shell=True)
|
subprocess.run(cmd_to_change_permissions, shell=True) # noqa DUO116
|
||||||
|
|
||||||
with pytest.raises(InsecurePermissionsError):
|
with pytest.raises(InsecurePermissionsError):
|
||||||
raise_on_invalid_options(windows_island_config_options)
|
raise_on_invalid_options(windows_island_config_options)
|
||||||
|
|
Loading…
Reference in New Issue