From 176c26e3f793506c648664ac0b143d300b9e9a8a Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Mon, 10 Jul 2017 13:09:33 +0300 Subject: [PATCH] Add config validation for action log archiving --- util/config/validator.py | 2 + .../test/test_validate_actionlog_archiving.py | 51 +++++++++++++++++++ .../validators/validate_actionlog_achiving.py | 22 ++++++++ 3 files changed, 75 insertions(+) create mode 100644 util/config/validators/test/test_validate_actionlog_archiving.py create mode 100644 util/config/validators/validate_actionlog_achiving.py diff --git a/util/config/validator.py b/util/config/validator.py index 4190caa34..1c66ac5fa 100644 --- a/util/config/validator.py +++ b/util/config/validator.py @@ -21,6 +21,7 @@ from util.config.validators.validate_github import GitHubLoginValidator, GitHubT from util.config.validators.validate_oidc import OIDCLoginValidator from util.config.validators.validate_timemachine import TimeMachineValidator from util.config.validators.validate_access import AccessSettingsValidator +from util.config.validators.validate_actionlog_achiving import ActionLogAchivingValidator logger = logging.getLogger(__name__) @@ -57,6 +58,7 @@ VALIDATORS = { OIDCLoginValidator.name: OIDCLoginValidator.validate, TimeMachineValidator.name: TimeMachineValidator.validate, AccessSettingsValidator.name: AccessSettingsValidator.validate, + ActionLogAchivingValidator.name: ActionLogAchivingValidator.validate, } def validate_service_for_config(service, config, password=None): diff --git a/util/config/validators/test/test_validate_actionlog_archiving.py b/util/config/validators/test/test_validate_actionlog_archiving.py new file mode 100644 index 000000000..915de58d1 --- /dev/null +++ b/util/config/validators/test/test_validate_actionlog_archiving.py @@ -0,0 +1,51 @@ +import pytest + +from util.config.validators import ConfigValidationException +from util.config.validators.validate_actionlog_achiving import ActionLogAchivingValidator + +from test.fixtures import * + +@pytest.mark.parametrize('unvalidated_config', [ + ({}), + ({'ACTION_LOG_ARCHIVE_PATH': 'foo'}), + ({'ACTION_LOG_ARCHIVE_LOCATION': ''}), +]) +def test_skip_validate_actionlog(unvalidated_config, app): + validator = ActionLogAchivingValidator() + validator.validate(unvalidated_config, None, None) + + +@pytest.mark.parametrize('config, expected_error', [ + ({'FEATURE_ACTION_LOG_ROTATION': True}, 'Missing action log archive path'), + ({'FEATURE_ACTION_LOG_ROTATION': True, + 'ACTION_LOG_ARCHIVE_PATH': ''}, 'Missing action log archive path'), + ({'FEATURE_ACTION_LOG_ROTATION': True, + 'ACTION_LOG_ARCHIVE_PATH': 'foo'}, 'Missing action log archive storage location'), + ({'FEATURE_ACTION_LOG_ROTATION': True, + 'ACTION_LOG_ARCHIVE_PATH': 'foo', + 'ACTION_LOG_ARCHIVE_LOCATION': ''}, 'Missing action log archive storage location'), + ({'FEATURE_ACTION_LOG_ROTATION': True, + 'ACTION_LOG_ARCHIVE_PATH': 'foo', + 'ACTION_LOG_ARCHIVE_LOCATION': 'invalid'}, + 'Action log archive storage location `invalid` not found in storage config'), +]) +def test_invalid_config(config, expected_error, app): + validator = ActionLogAchivingValidator() + + with pytest.raises(ConfigValidationException) as ipe: + validator.validate(config, None, None) + + assert ipe.value.message == expected_error + +def test_valid_config(app): + config = { + 'FEATURE_ACTION_LOG_ROTATION': True, + 'ACTION_LOG_ARCHIVE_PATH': 'somepath', + 'ACTION_LOG_ARCHIVE_LOCATION': 'somelocation', + 'DISTRIBUTED_STORAGE_CONFIG': { + 'somelocation': {}, + }, + } + + validator = ActionLogAchivingValidator() + validator.validate(config, None, None) diff --git a/util/config/validators/validate_actionlog_achiving.py b/util/config/validators/validate_actionlog_achiving.py new file mode 100644 index 000000000..f6d1d5eba --- /dev/null +++ b/util/config/validators/validate_actionlog_achiving.py @@ -0,0 +1,22 @@ +from util.config.validators import BaseValidator, ConfigValidationException + +class ActionLogAchivingValidator(BaseValidator): + name = "actionlogachiving" + + @classmethod + def validate(cls, config, user, user_password): + """ Validates the action log archiving configuration. """ + if not config.get('FEATURE_ACTION_LOG_ROTATION', False): + return + + if not config.get('ACTION_LOG_ARCHIVE_PATH'): + raise ConfigValidationException('Missing action log archive path') + + if not config.get('ACTION_LOG_ARCHIVE_LOCATION'): + raise ConfigValidationException('Missing action log archive storage location') + + location = config['ACTION_LOG_ARCHIVE_LOCATION'] + storage_config = config.get('DISTRIBUTED_STORAGE_CONFIG') or {} + if location not in storage_config: + msg = 'Action log archive storage location `%s` not found in storage config' % location + raise ConfigValidationException(msg)