Skip to content

Commit

Permalink
add validation for config json
Browse files Browse the repository at this point in the history
  • Loading branch information
dsschult committed Oct 15, 2024
1 parent ae8c084 commit c31a111
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
2 changes: 1 addition & 1 deletion iceprod/core/data/dataset.schema.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://iceprod2.icecube.wisc.edu/static/dataset.schema.json",
"$id": "https://iceprod.icecube.aq/schemas/v3/dataset.schema.json",
"title": "IceProd Dataset Config",
"description": "The schema for an IceProd Dataset Config",
"type": "object",
Expand Down
2 changes: 1 addition & 1 deletion iceprod/server/data/config.schema.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://iceprod2.icecube.wisc.edu/static/config.schema.json",
"$id": "https://iceprod.icecube.aq/schemas/v3/config.schema.json",
"title": "IceProd Server Config",
"description": "The schema for an IceProd Server Config",
"type": "object",
Expand Down
43 changes: 39 additions & 4 deletions iceprod/website/data/www_templates/submit.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,34 @@ <h4>$(task)</h4>
{% block body_scripts %}
<script src="/static/fetch.js"></script>
<script type="text/javascript" src="/static/rest.js"></script>
<!--<script type="text/javascript" src="/static/dataclasses.js"></script>
<script type="text/javascript" src="/static/jsviews.min.js"></script>
<script type="text/javascript" src="/static/documentation.js"></script>
<script type="text/javascript" src="/static/jsonlint.js"></script>-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/ajv/8.17.1/ajv2020.bundle.min.js" integrity="sha512-mXlpzWzZB+t4DFFezQkpSiCbT8aW12t688aLsd6KGNbRWDOdCur5C6Fl0rDl75VruBy42GvWsd6F35VQcs3lwQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script type="text/javascript">
var rest_api = '{{ rest_api }}';
var passkey = '{{ passkey }}';
var orig_config = {{ json_encode(config) }};
var edit = {{ json_encode(edit) }};
var dataset_id = '{{ dataset_id }}';

async function validator(config) {
try {
const ret = await fetch(rest_api + '/schemas/dataset.schema.json');
if (!response.ok) {
throw new Error('Response status: '+response.status);
}
const schema = await ret.json();
} catch (error) {
$('#error').text('Error obtaining json schema: '+error.message);
return false;
}
try {
const ajv = new Ajv2020({useDefaults: true});
const validate = ajv.compile(schema);
return validate(config)
} catch (error) {
$('#error').text('Error validating config: '+validate.errors);
return false;
}
}
function parse_json(config){
try {
return JSON.parse(config);
Expand Down Expand Up @@ -119,6 +137,12 @@ <h4>$(task)</h4>
if (ntasks < 0)
return;

// validate config
const ret = await validator(submit_data);
if (!ret) {
return;
}

// create dataset
var response = await fetch_json('POST', '/datasets',
{'description': description,
Expand All @@ -141,6 +165,10 @@ <h4>$(task)</h4>
);
if ('error' in response) {
$('#error').text('error uploading config: '+response['error']);
await fetch_json('PUT', '/datasets/' + dataset_id + '/status',
{'status': 'errors'},
passkey
);
return;
}
window.location = '/dataset/' + dataset_id;
Expand All @@ -150,6 +178,13 @@ <h4>$(task)</h4>
var submit_data = parse_json($("#submit_box").val());
if (submit_data == null)
return;

// validate config
const ret = await validator(submit_data);
if (!ret) {
return;
}

var response = await fetch_json('PUT', '/config/' + dataset_id,
submit_data, passkey
);
Expand Down
14 changes: 14 additions & 0 deletions iceprod/website/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

import iceprod
from iceprod.roles_groups import GROUPS
from iceprod.core.config import CONFIG_SCHEMA as DATASET_SCHEMA
from iceprod.server.config import CONFIG_SCHEMA as SERVER_SCHEMA
import iceprod.core.functions
from iceprod.server import documentation
from iceprod.server.module import FakeStatsClient, StatsClientIgnoreErrors
Expand Down Expand Up @@ -260,6 +262,17 @@ async def get(self):
self.render('main.html')


class Schemas(PublicHandler):
"""Handle /schemas/v3/(.*) urls"""
@catch_error
async def get(self, schema):
if schema == 'dataset.schema.json':
self.write(DATASET_SCHEMA)
elif schema == 'config.schema.json':
self.write(SERVER_SCHEMA)
else:
raise tornado.web.HTTPError(404, reason='unknown schema')

class Submit(PublicHandler):
"""Handle /submit urls"""
@authenticated
Expand Down Expand Up @@ -790,6 +803,7 @@ def __init__(self):
server.add_route(r"/", Default, handler_args)
server.add_route(r"/submit", Submit, handler_args)
server.add_route(r"/config", Config, handler_args)
server.add_route(r"/schemas/v3/([\w\.]+)", Schemas, handler_args)
server.add_route(r"/dataset", DatasetBrowse, handler_args)
server.add_route(r"/dataset/(\w+)", Dataset, handler_args)
server.add_route(r"/dataset/(\w+)/task", TaskBrowse, handler_args)
Expand Down

0 comments on commit c31a111

Please sign in to comment.