diff --git a/module_build_service/views.py b/module_build_service/views.py index 8a1608c7..b4930008 100644 --- a/module_build_service/views.py +++ b/module_build_service/views.py @@ -95,65 +95,19 @@ class ModuleBuildAPI(MethodView): raise NotFound('No such module found.') def post(self): - username, groups = module_build_service.auth.get_user(request) + if "multipart/form-data" in request.headers.get("Content-Type", ""): + handler = YAMLFileHandler(request) + else: + handler = SCMHandler(request) - if conf.allowed_groups and not (conf.allowed_groups & groups): + if conf.allowed_groups and not (conf.allowed_groups & handler.groups): raise Forbidden("%s is not in any of %r, only %r" % ( - username, conf.allowed_groups, groups)) - - kwargs = {"username": username} - module = (self.post_file(**kwargs) if "multipart/form-data" in request.headers.get("Content-Type", "") else - self.post_scm(**kwargs)) + handler.username, conf.allowed_groups, handler.groups)) + handler.validate() + module = handler.post() return jsonify(module.json()), 201 - def post_scm(self, username): - try: - r = json.loads(request.get_data().decode("utf-8")) - except: - log.error('Invalid JSON submitted') - raise ValidationError('Invalid JSON submitted') - - if "scmurl" not in r: - log.error('Missing scmurl') - raise ValidationError('Missing scmurl') - - url = r["scmurl"] - if not any(url.startswith(prefix) for prefix in conf.scmurls): - log.error("The submitted scmurl %r is not allowed" % url) - raise Forbidden("The submitted scmurl %s is not allowed" % url) - - if not get_scm_url_re().match(url): - log.error("The submitted scmurl %r is not valid" % url) - raise Forbidden("The submitted scmurl %s is not valid" % url) - - if "branch" not in r: - log.error('Missing branch') - raise ValidationError('Missing branch') - - branch = r["branch"] - - # python-modulemd expects this to be bytes, not unicode. - if isinstance(branch, unicode): - branch = branch.encode('utf-8') - - validate_optional_params(r) - optional_params = {k: v for k, v in r.items() if k != "scmurl" and k != 'branch'} - return submit_module_build_from_scm(username, url, branch, allow_local_url=False, optional_params=optional_params) - - def post_file(self, username): - if not conf.yaml_submit_allowed: - raise Forbidden("YAML submission is not enabled") - validate_optional_params(request.form) - - try: - r = request.files["yaml"] - except: - log.error('Invalid file submitted') - raise ValidationError('Invalid file submitted') - - return submit_module_build_from_yaml(username, r.read(), optional_params=request.form.to_dict()) - def patch(self, id): username, groups = module_build_service.auth.get_user(request) @@ -193,6 +147,71 @@ class ModuleBuildAPI(MethodView): return jsonify(module.api_json()), 200 +class BaseHandler(object): + def __init__(self, request): + self.username, self.groups = module_build_service.auth.get_user(request) + + +class SCMHandler(BaseHandler): + def __init__(self, request): + super(SCMHandler, self).__init__(request) + try: + self.data = json.loads(request.get_data().decode("utf-8")) + except: + log.error('Invalid JSON submitted') + raise ValidationError('Invalid JSON submitted') + + def validate(self): + if "scmurl" not in self.data: + log.error('Missing scmurl') + raise ValidationError('Missing scmurl') + + url = self.data["scmurl"] + if not any(url.startswith(prefix) for prefix in conf.scmurls): + log.error("The submitted scmurl %r is not allowed" % url) + raise Forbidden("The submitted scmurl %s is not allowed" % url) + + if not get_scm_url_re().match(url): + log.error("The submitted scmurl %r is not valid" % url) + raise Forbidden("The submitted scmurl %s is not valid" % url) + + if "branch" not in self.data: + log.error('Missing branch') + raise ValidationError('Missing branch') + + validate_optional_params(self.data) + + def post(self): + url = self.data["scmurl"] + branch = self.data["branch"] + + # python-modulemd expects this to be bytes, not unicode. + if isinstance(branch, unicode): + branch = branch.encode('utf-8') + + optional_params = {k: v for k, v in self.data.items() if k not in ["scmurl", "branch"]} + return submit_module_build_from_scm(self.username, url, branch, + allow_local_url=False, optional_params=optional_params) + + +class YAMLFileHandler(BaseHandler): + def __init__(self, request): + if not conf.yaml_submit_allowed: + raise Forbidden("YAML submission is not enabled") + super(YAMLFileHandler, self).__init__(request) + self.data = request.form.to_dict() + + def validate(self): + if "yaml" not in request.files: + log.error('Invalid file submitted') + raise ValidationError('Invalid file submitted') + validate_optional_params(self.data) + + def post(self): + r = request.files["yaml"] + return submit_module_build_from_yaml(self.username, r.read(), optional_params=self.data) + + def register_api_v1(): """ Registers version 1 of MBS API. """ module_view = ModuleBuildAPI.as_view('module_builds')