Commit c9dc6a14 authored by Rémi Duraffort's avatar Rémi Duraffort Committed by Neil Williams
Browse files

LAVA-1032 reject v1 submission at api level

Change-Id: I4a74bf5bc11c4b626de4d2d43bd490f8781117fd
parent aef7d7b8
......@@ -11,6 +11,7 @@ from lava_scheduler_app.models import (
Device,
DeviceType,
DeviceStateTransition,
is_deprecated_json,
JSONDataError,
DevicesUnavailableException,
TestJob,
......@@ -74,6 +75,10 @@ class SchedulerAPI(ExposedAPI):
If the job is a multinode job, this function returns the list of created
job IDs.
"""
# Reject v1 jobs
if is_deprecated_json(job_data):
raise xmlrpclib.Fault(400, "v1 jobs cannot be submitted to this instance")
self._authenticate()
if not self.user.has_perm('lava_scheduler_app.add_testjob'):
raise xmlrpclib.Fault(
......@@ -126,6 +131,11 @@ class SchedulerAPI(ExposedAPI):
job = get_restricted_job(self.user, job_id)
except TestJob.DoesNotExist:
raise xmlrpclib.Fault(404, "Specified job not found.")
# Reject v1 jobs
if not job.is_pipeline:
raise xmlrpclib.Fault(400, "v1 jobs cannot be submitted to this instance")
if job.is_multinode:
return self.submit_job(job.multinode_definition)
elif job.is_vmgroup:
......
......@@ -19,8 +19,6 @@
import os
import xmlrpclib
from django.contrib.auth.models import AnonymousUser
from linaro_django_xmlrpc.models import ExposedV2API
from lava_scheduler_app.api import SchedulerAPI
from lava_scheduler_app.models import TestJob
......
......@@ -121,18 +121,20 @@ def is_deprecated_json(data):
def validate_job(data):
if not is_deprecated_json(data):
try:
# only try YAML if this is not JSON
# YAML can parse JSON as YAML, JSON cannot parse YAML at all
yaml_data = yaml.load(data)
except yaml.YAMLError as exc:
# neither yaml nor json loaders were able to process the submission.
raise SubmissionException("Loading job submission failed: %s." % exc)
if is_deprecated_json(data):
raise SubmissionException("v1 jobs cannot be submitted to this instance")
try:
# only try YAML if this is not JSON
# YAML can parse JSON as YAML, JSON cannot parse YAML at all
yaml_data = yaml.load(data)
except yaml.YAMLError as exc:
# neither yaml nor json loaders were able to process the submission.
raise SubmissionException("Loading job submission failed: %s." % exc)
# validate against the submission schema.
validate_submission(yaml_data) # raises SubmissionException if invalid.
validate_yaml(yaml_data) # raises SubmissionException if invalid.
# validate against the submission schema.
validate_submission(yaml_data) # raises SubmissionException if invalid.
validate_yaml(yaml_data) # raises SubmissionException if invalid.
def validate_yaml(yaml_data):
......@@ -2369,8 +2371,9 @@ class TestJob(RestrictedResource):
return self._can_admin(user) and self.status <= TestJob.RUNNING
def can_resubmit(self, user):
return (user.is_superuser or
user.has_perm('lava_scheduler_app.cancel_resubmit_testjob'))
return self.is_pipeline and \
(user.is_superuser or
user.has_perm('lava_scheduler_app.cancel_resubmit_testjob'))
def job_device_type(self):
device_type = None
......
......@@ -93,9 +93,12 @@ class TestSchedulerAPI(TestCaseWithFactory): # pylint: disable=too-many-ancesto
user.save()
server = self.server_proxy('test', 'test')
definition = self.factory.make_job_json()
job_id = server.scheduler.submit_job(definition)
job = TestJob.objects.get(id=job_id)
self.assertEqual(definition, job.definition)
try:
job_id = server.scheduler.submit_job(definition)
except xmlrpclib.Fault as f:
self.assertEqual(400, f.faultCode)
else:
self.fail("v1 job was accepted")
def test_cancel_job_rejects_anonymous(self):
job = self.factory.make_testjob()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment