Adding a security configuration
Adding a config file for security related items. Only item to start is dns name. Change-Id: Ia55060198c705fdd0c9114aba199857d8196beb5
This commit is contained in:
@@ -17,6 +17,7 @@ class ConfigurationFileLocationsManager:
|
|||||||
self.web_config_file = None
|
self.web_config_file = None
|
||||||
self.database_config_file = None
|
self.database_config_file = None
|
||||||
self.rest_api_config_file = None
|
self.rest_api_config_file = None
|
||||||
|
self.security_config_file = None
|
||||||
|
|
||||||
def set_configs_from_pytest_args(self, session: Session):
|
def set_configs_from_pytest_args(self, session: Session):
|
||||||
"""
|
"""
|
||||||
@@ -60,6 +61,10 @@ class ConfigurationFileLocationsManager:
|
|||||||
if rest_api_config_file:
|
if rest_api_config_file:
|
||||||
self.set_rest_api_config_file(rest_api_config_file)
|
self.set_rest_api_config_file(rest_api_config_file)
|
||||||
|
|
||||||
|
security_config_file = session.config.getoption("--security_config_file")
|
||||||
|
if security_config_file:
|
||||||
|
self.set_security_config_file(security_config_file)
|
||||||
|
|
||||||
def set_configs_from_options_parser(self, parser: OptionParser = None):
|
def set_configs_from_options_parser(self, parser: OptionParser = None):
|
||||||
"""
|
"""
|
||||||
Sets the config files from options parser.
|
Sets the config files from options parser.
|
||||||
@@ -106,6 +111,10 @@ class ConfigurationFileLocationsManager:
|
|||||||
if rest_api_config_file:
|
if rest_api_config_file:
|
||||||
self.set_rest_api_config_file(rest_api_config_file)
|
self.set_rest_api_config_file(rest_api_config_file)
|
||||||
|
|
||||||
|
security_config_file = options.security_config_file
|
||||||
|
if security_config_file:
|
||||||
|
self.set_security_config_file(security_config_file)
|
||||||
|
|
||||||
def _add_options(self, parser: OptionParser):
|
def _add_options(self, parser: OptionParser):
|
||||||
"""
|
"""
|
||||||
Adds the command line options we can expect.
|
Adds the command line options we can expect.
|
||||||
@@ -180,6 +189,14 @@ class ConfigurationFileLocationsManager:
|
|||||||
help="The rest api config file",
|
help="The rest api config file",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
parser.add_option(
|
||||||
|
"--security_config_file",
|
||||||
|
action="store",
|
||||||
|
type="str",
|
||||||
|
dest="security_config_file",
|
||||||
|
help="The security config file",
|
||||||
|
)
|
||||||
|
|
||||||
options, args = parser.parse_args()
|
options, args = parser.parse_args()
|
||||||
|
|
||||||
return options
|
return options
|
||||||
@@ -351,3 +368,23 @@ class ConfigurationFileLocationsManager:
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
return self.rest_api_config_file
|
return self.rest_api_config_file
|
||||||
|
|
||||||
|
def get_security_config_file(self) -> str:
|
||||||
|
"""
|
||||||
|
Getter for security config file
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: the security config file
|
||||||
|
|
||||||
|
"""
|
||||||
|
return self.security_config_file
|
||||||
|
|
||||||
|
def set_security_config_file(self, security_config_file: str):
|
||||||
|
"""
|
||||||
|
Setter for security config file
|
||||||
|
|
||||||
|
Args:
|
||||||
|
security_config_file (str): the security config file
|
||||||
|
|
||||||
|
"""
|
||||||
|
self.security_config_file = security_config_file
|
||||||
|
@@ -6,6 +6,7 @@ from config.lab.objects.lab_config import LabConfig
|
|||||||
from config.logger.objects.logger_config import LoggerConfig
|
from config.logger.objects.logger_config import LoggerConfig
|
||||||
from config.ptp.objects.ptp_config import PTPConfig
|
from config.ptp.objects.ptp_config import PTPConfig
|
||||||
from config.rest_api.objects.rest_api_config import RestAPIConfig
|
from config.rest_api.objects.rest_api_config import RestAPIConfig
|
||||||
|
from config.security.objects.security_config import SecurityConfig
|
||||||
from config.web.objects.web_config import WebConfig
|
from config.web.objects.web_config import WebConfig
|
||||||
from framework.resources.resource_finder import get_stx_resource_path
|
from framework.resources.resource_finder import get_stx_resource_path
|
||||||
|
|
||||||
@@ -25,6 +26,7 @@ class ConfigurationManagerClass:
|
|||||||
self.web_config: WebConfig = None
|
self.web_config: WebConfig = None
|
||||||
self.database_config: DatabaseConfig = None
|
self.database_config: DatabaseConfig = None
|
||||||
self.rest_api_config: RestAPIConfig = None
|
self.rest_api_config: RestAPIConfig = None
|
||||||
|
self.security_config: SecurityConfig = None
|
||||||
self.configuration_locations_manager = None
|
self.configuration_locations_manager = None
|
||||||
|
|
||||||
def is_config_loaded(self) -> bool:
|
def is_config_loaded(self) -> bool:
|
||||||
@@ -46,6 +48,9 @@ class ConfigurationManagerClass:
|
|||||||
Args:
|
Args:
|
||||||
config_file_locations (ConfigurationFileLocationsManager): class with all the config file locations
|
config_file_locations (ConfigurationFileLocationsManager): class with all the config file locations
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
FileNotFoundError: if config file not found
|
||||||
|
|
||||||
"""
|
"""
|
||||||
self.configuration_locations_manager = config_file_locations
|
self.configuration_locations_manager = config_file_locations
|
||||||
|
|
||||||
@@ -81,6 +86,10 @@ class ConfigurationManagerClass:
|
|||||||
if not rest_api_config_file:
|
if not rest_api_config_file:
|
||||||
rest_api_config_file = get_stx_resource_path("config/rest_api/files/default.json5")
|
rest_api_config_file = get_stx_resource_path("config/rest_api/files/default.json5")
|
||||||
|
|
||||||
|
security_config_file = config_file_locations.get_security_config_file()
|
||||||
|
if not security_config_file:
|
||||||
|
security_config_file = get_stx_resource_path("config/security/files/default.json5")
|
||||||
|
|
||||||
if not self.loaded:
|
if not self.loaded:
|
||||||
try:
|
try:
|
||||||
self.lab_config = LabConfig(lab_config_file)
|
self.lab_config = LabConfig(lab_config_file)
|
||||||
@@ -91,6 +100,7 @@ class ConfigurationManagerClass:
|
|||||||
self.web_config = WebConfig(web_config_file)
|
self.web_config = WebConfig(web_config_file)
|
||||||
self.database_config = DatabaseConfig(database_config_file)
|
self.database_config = DatabaseConfig(database_config_file)
|
||||||
self.rest_api_config = RestAPIConfig(rest_api_config_file)
|
self.rest_api_config = RestAPIConfig(rest_api_config_file)
|
||||||
|
self.security_config = SecurityConfig(security_config_file)
|
||||||
self.loaded = True
|
self.loaded = True
|
||||||
except FileNotFoundError as e:
|
except FileNotFoundError as e:
|
||||||
print(f"Unable to load the config using file: {str(e.filename)} ")
|
print(f"Unable to load the config using file: {str(e.filename)} ")
|
||||||
@@ -178,6 +188,15 @@ class ConfigurationManagerClass:
|
|||||||
"""
|
"""
|
||||||
return self.rest_api_config
|
return self.rest_api_config
|
||||||
|
|
||||||
|
def get_security_config(self) -> SecurityConfig:
|
||||||
|
"""
|
||||||
|
Getter for security config
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
SecurityConfig: the security config
|
||||||
|
"""
|
||||||
|
return self.security_config
|
||||||
|
|
||||||
def get_config_pytest_args(self) -> [str]:
|
def get_config_pytest_args(self) -> [str]:
|
||||||
"""
|
"""
|
||||||
Returns the configuration file locations as pytest args.
|
Returns the configuration file locations as pytest args.
|
||||||
@@ -203,6 +222,8 @@ class ConfigurationManagerClass:
|
|||||||
pytest_config_args.append(f"--database_config_file={self.configuration_locations_manager.get_database_config_file()}")
|
pytest_config_args.append(f"--database_config_file={self.configuration_locations_manager.get_database_config_file()}")
|
||||||
if self.configuration_locations_manager.rest_api_config_file:
|
if self.configuration_locations_manager.rest_api_config_file:
|
||||||
pytest_config_args.append(f"--rest_api_config_file={self.configuration_locations_manager.get_rest_api_config_file()}")
|
pytest_config_args.append(f"--rest_api_config_file={self.configuration_locations_manager.get_rest_api_config_file()}")
|
||||||
|
if self.configuration_locations_manager.security_config_file:
|
||||||
|
pytest_config_args.append(f"--security_config_file={self.configuration_locations_manager.get_security_config_file()}")
|
||||||
|
|
||||||
return pytest_config_args
|
return pytest_config_args
|
||||||
|
|
||||||
|
4
config/security/files/default.json5
Normal file
4
config/security/files/default.json5
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
// dns name for the lab
|
||||||
|
"dns_name": "lab_dns_name"
|
||||||
|
}
|
27
config/security/objects/security_config.py
Normal file
27
config/security/objects/security_config.py
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
import json5
|
||||||
|
|
||||||
|
|
||||||
|
class SecurityConfig:
|
||||||
|
"""
|
||||||
|
Class to hold configuration for Security tests
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, config):
|
||||||
|
|
||||||
|
try:
|
||||||
|
json_data = open(config)
|
||||||
|
except FileNotFoundError:
|
||||||
|
print(f"Could not find the security config file: {config}")
|
||||||
|
raise
|
||||||
|
|
||||||
|
security_dict = json5.load(json_data)
|
||||||
|
self.dns_name = security_dict["dns_name"]
|
||||||
|
|
||||||
|
def get_dns_name(self) -> str:
|
||||||
|
"""
|
||||||
|
Getter for the dns name
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: the dns name
|
||||||
|
"""
|
||||||
|
return self.dns_name
|
@@ -1,16 +1,10 @@
|
|||||||
import os
|
import os
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from config.configuration_file_locations_manager import (
|
from config.configuration_file_locations_manager import ConfigurationFileLocationsManager
|
||||||
ConfigurationFileLocationsManager,
|
|
||||||
)
|
|
||||||
from config.configuration_manager import ConfigurationManager
|
from config.configuration_manager import ConfigurationManager
|
||||||
from framework.logging import log_banners
|
from framework.logging import log_banners
|
||||||
from framework.logging.automation_logger import (
|
from framework.logging.automation_logger import configure_testcase_log_handler, get_logger, remove_testcase_handler
|
||||||
configure_testcase_log_handler,
|
|
||||||
get_logger,
|
|
||||||
remove_testcase_handler,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def pytest_addoption(parser: Any):
|
def pytest_addoption(parser: Any):
|
||||||
@@ -29,6 +23,7 @@ def pytest_addoption(parser: Any):
|
|||||||
parser.addoption("--web_config_file", action="store")
|
parser.addoption("--web_config_file", action="store")
|
||||||
parser.addoption("--database_config_file", action="store")
|
parser.addoption("--database_config_file", action="store")
|
||||||
parser.addoption("--rest_api_config_file", action="store")
|
parser.addoption("--rest_api_config_file", action="store")
|
||||||
|
parser.addoption("--security_config_file", action="store")
|
||||||
|
|
||||||
|
|
||||||
def pytest_sessionstart(session: Any):
|
def pytest_sessionstart(session: Any):
|
||||||
|
4
unit_tests/config/security/custom_security_config.json5
Normal file
4
unit_tests/config/security/custom_security_config.json5
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
// dns name for the lab
|
||||||
|
"dns_name": "fake_dns_name"
|
||||||
|
}
|
31
unit_tests/config/security/security_config_test.py
Normal file
31
unit_tests/config/security/security_config_test.py
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
from config.configuration_file_locations_manager import ConfigurationFileLocationsManager
|
||||||
|
from config.configuration_manager import ConfigurationManagerClass
|
||||||
|
from framework.resources.resource_finder import get_stx_resource_path
|
||||||
|
|
||||||
|
|
||||||
|
def test_default_security_config():
|
||||||
|
"""
|
||||||
|
Tests that the default security configuration is as expected.
|
||||||
|
|
||||||
|
"""
|
||||||
|
configuration_manager = ConfigurationManagerClass()
|
||||||
|
config_file_locations = ConfigurationFileLocationsManager()
|
||||||
|
configuration_manager.load_configs(config_file_locations)
|
||||||
|
default_config = configuration_manager.get_security_config()
|
||||||
|
assert default_config is not None, "Default security config wasn't loaded successfully"
|
||||||
|
assert default_config.get_dns_name() == "lab_dns_name", "default dns name was incorrect"
|
||||||
|
|
||||||
|
|
||||||
|
def test_custom_security_config():
|
||||||
|
"""
|
||||||
|
Tests that we can load a custom security configuration.
|
||||||
|
"""
|
||||||
|
custom_file = get_stx_resource_path("unit_tests/config/security/custom_security_config.json5")
|
||||||
|
configuration_manager = ConfigurationManagerClass()
|
||||||
|
config_file_locations = ConfigurationFileLocationsManager()
|
||||||
|
config_file_locations.set_security_config_file(custom_file)
|
||||||
|
configuration_manager.load_configs(config_file_locations)
|
||||||
|
|
||||||
|
custom_config = configuration_manager.get_security_config()
|
||||||
|
assert custom_config is not None, "Custom security config wasn't loaded successfully"
|
||||||
|
assert custom_config.get_dns_name() == "fake_dns_name", "Custom dns name was incorrect"
|
Reference in New Issue
Block a user