Change validators to use the validator_context

Change InstanceKeys to take a namedtuple for context
This commit is contained in:
Sam Chow 2018-05-25 15:42:27 -04:00
parent e967fde3ae
commit 554d4f47a8
31 changed files with 172 additions and 69 deletions

View file

@ -2,6 +2,7 @@ import logging
from auth.auth_context import get_authenticated_user
from data.users import LDAP_CERT_FILENAME
from util.config import URLSchemeAndHostname
from util.config.validators.validate_database import DatabaseValidator
from util.config.validators.validate_redis import RedisValidator
@ -64,7 +65,7 @@ VALIDATORS = {
AppTokenAuthValidator.name: AppTokenAuthValidator.validate,
}
def validate_service_for_config(service, config, password=None, app=None):
def validate_service_for_config(service, validator_context):
""" Attempts to validate the configuration for the given service. """
if not service in VALIDATORS:
return {
@ -72,7 +73,7 @@ def validate_service_for_config(service, config, password=None, app=None):
}
try:
VALIDATORS[service](config, get_authenticated_user(), password, app)
VALIDATORS[service](validator_context)
return {
'status': True
}
@ -92,3 +93,28 @@ def is_valid_config_upload_filename(filename):
return True
return any([filename.endswith(suffix) for suffix in CONFIG_FILE_SUFFIXES])
class ValidatorContext(object):
""" Context to run validators in, with any additional runtime configuration they need
"""
def __init__(self, config, user_password=None, http_client=None, context=None,
url_scheme_and_hostname=None, jwt_auth_max=None, registry_title=None, ip_resolver=None):
self.config = config
self.user = get_authenticated_user()
self.user_password = user_password
self.http_client = http_client
self.context = context
self.scheme_and_hostname = url_scheme_and_hostname
self.jwt_auth_max = jwt_auth_max
self.registry_title = registry_title
self.ip_resolver = ip_resolver
@classmethod
def from_app(cls, config, user_password, app, ip_resolver):
cls(config, user_password, app.config['HTTP_CLIENT'], app.app_context,
URLSchemeAndHostname(app.config['PREFERRED_URL_SCHEME'], app.config['SERVER_HOSTNAME']),
app.config.get('JWT_AUTH_MAX_FRESH_S', 300), app.config['REGISTRY_TITLE'], ip_resolver)