From 37bffb9ceaaf2eb0ccc97fb43bd9632c30351e4e Mon Sep 17 00:00:00 2001 From: Dan Jones <dan.jones@noc.ac.uk> Date: Thu, 23 Feb 2023 09:01:31 +0000 Subject: [PATCH] feat: add soar protocol super class Take tag/branch/commitref as constructor arg and retrieve schema from gitlab --- dist/protocol.esm.js | 79 +++++++++++++++++++++++++++++++++++++++++-- dist/protocol.js | 78 +++++++++++++++++++++++++++++++++++++++++- src/protocol/index.js | 76 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 229 insertions(+), 4 deletions(-) diff --git a/dist/protocol.esm.js b/dist/protocol.esm.js index 3971ac0..0a78d88 100644 --- a/dist/protocol.esm.js +++ b/dist/protocol.esm.js @@ -1,3 +1,4 @@ +import axios from 'axios'; import Validator from 'swagger-model-validator'; /** @@ -25,8 +26,17 @@ import Validator from 'swagger-model-validator'; class GenericProtocol { constructor(schema, services) { this.schema = schema; - this.validator = new Validator(schema); this.services = services; + this.validator = this.createValidator(schema); + } + + /** + * Create a Validator from the provided JSONSchema + * @param {object} schema + * @returns {Validator} + */ + createValidator(schema) { + return new Validator(schema); } /** @@ -84,4 +94,69 @@ class GenericProtocol { } } -export { GenericProtocol }; + +/** + * GenericSoarProtocol defines a simple passthru handler for messages + * + * This provides the same as above but loads a version of the + * SoAR message protocol + */ +class GenericSoarProtocol extends GenericProtocol { + constructor(schema, services) { + super(schema, services); + } + + /** + * Create a Validator from the provided JSONSchema + * + * If schema is a string build a URL and retrieve the + * schema from gitlab + * @param {string|object} schema + * @returns {object} + */ + async createValidator(schema) { + if (typeof schema === 'string' && schema.match(/^[\w\.]+$/)) { + return await this.loadSchema(schema) + .then((schema) => { + return new Validator(schema); + }); + } else { + return new Validator(schema); + } + } + + + /** + * Load schema from gitlab by tag/branch/commitref + * @param {string} version + * @returns {object} + */ + loadSchema(version) { + this.axios = axios; + let repository = "https://git.noc.ac.uk/communications-backbone-system/backbone-message-format"; + // TODO check this path resolves for tags + let url = `${repository}/-/raw/${version}/project/soar/swagger.json`; + this.schema = this.axios.get(url) + .then((response) => { + return response.data; + }); + return this.schema; + } + + /** + * Validate that a message meets the reqiured schema + * @param {object} message + * @returns {object} + */ + validate(message) { + return this.validator.validate( + message, + this.schema.components.schemas.MESSAGE, + this.schema.components.schemas, + false, + true + ); + } +} + +export { GenericProtocol, GenericSoarProtocol }; diff --git a/dist/protocol.js b/dist/protocol.js index 6d8c468..3146ab0 100644 --- a/dist/protocol.js +++ b/dist/protocol.js @@ -1,5 +1,6 @@ 'use strict'; +var axios = require('axios'); var Validator = require('swagger-model-validator'); /** @@ -27,8 +28,17 @@ var Validator = require('swagger-model-validator'); class GenericProtocol { constructor(schema, services) { this.schema = schema; - this.validator = new Validator(schema); this.services = services; + this.validator = this.createValidator(schema); + } + + /** + * Create a Validator from the provided JSONSchema + * @param {object} schema + * @returns {Validator} + */ + createValidator(schema) { + return new Validator(schema); } /** @@ -86,4 +96,70 @@ class GenericProtocol { } } + +/** + * GenericSoarProtocol defines a simple passthru handler for messages + * + * This provides the same as above but loads a version of the + * SoAR message protocol + */ +class GenericSoarProtocol extends GenericProtocol { + constructor(schema, services) { + super(schema, services); + } + + /** + * Create a Validator from the provided JSONSchema + * + * If schema is a string build a URL and retrieve the + * schema from gitlab + * @param {string|object} schema + * @returns {object} + */ + async createValidator(schema) { + if (typeof schema === 'string' && schema.match(/^[\w\.]+$/)) { + return await this.loadSchema(schema) + .then((schema) => { + return new Validator(schema); + }); + } else { + return new Validator(schema); + } + } + + + /** + * Load schema from gitlab by tag/branch/commitref + * @param {string} version + * @returns {object} + */ + loadSchema(version) { + this.axios = axios; + let repository = "https://git.noc.ac.uk/communications-backbone-system/backbone-message-format"; + // TODO check this path resolves for tags + let url = `${repository}/-/raw/${version}/project/soar/swagger.json`; + this.schema = this.axios.get(url) + .then((response) => { + return response.data; + }); + return this.schema; + } + + /** + * Validate that a message meets the reqiured schema + * @param {object} message + * @returns {object} + */ + validate(message) { + return this.validator.validate( + message, + this.schema.components.schemas.MESSAGE, + this.schema.components.schemas, + false, + true + ); + } +} + exports.GenericProtocol = GenericProtocol; +exports.GenericSoarProtocol = GenericSoarProtocol; diff --git a/src/protocol/index.js b/src/protocol/index.js index 266542c..1c55f2a 100644 --- a/src/protocol/index.js +++ b/src/protocol/index.js @@ -1,3 +1,4 @@ +import axios from 'axios'; import Validator from 'swagger-model-validator'; /** @@ -25,8 +26,17 @@ import Validator from 'swagger-model-validator'; export class GenericProtocol { constructor(schema, services) { this.schema = schema; - this.validator = new Validator(schema); this.services = services; + this.validator = this.createValidator(schema); + } + + /** + * Create a Validator from the provided JSONSchema + * @param {object} schema + * @returns {Validator} + */ + createValidator(schema) { + return new Validator(schema); } /** @@ -83,3 +93,67 @@ export class GenericProtocol { return message; } } + + +/** + * GenericSoarProtocol defines a simple passthru handler for messages + * + * This provides the same as above but loads a version of the + * SoAR message protocol + */ +export class GenericSoarProtocol extends GenericProtocol { + constructor(schema, services) { + super(schema, services); + } + + /** + * Create a Validator from the provided JSONSchema + * + * If schema is a string build a URL and retrieve the + * schema from gitlab + * @param {string|object} schema + * @returns {object} + */ + async createValidator(schema) { + if (typeof schema === 'string' && schema.match(/^[\w\.]+$/)) { + return await this.loadSchema(schema) + .then((schema) => { + return new Validator(schema); + }); + } else { + return new Validator(schema); + } + } + + /** + * Load schema from gitlab by tag/branch/commitref + * @param {string} version + * @returns {object} + */ + loadSchema(version) { + this.axios = axios; + let repository = "https://git.noc.ac.uk/communications-backbone-system/backbone-message-format"; + // TODO check this path resolves for tags + let url = `${repository}/-/raw/${version}/project/soar/swagger.json`; + this.schema = this.axios.get(url) + .then((response) => { + return response.data; + }); + return this.schema; + } + + /** + * Validate that a message meets the reqiured schema + * @param {object} message + * @returns {object} + */ + validate(message) { + return this.validator.validate( + message, + this.schema.components.schemas.MESSAGE, + this.schema.components.schemas, + false, + true + ); + } +} \ No newline at end of file -- GitLab