UNPKG

@mdf.js/openc2-core

Version:

MMS - API Core - OpenC2

223 lines 8.47 kB
"use strict"; /** * Copyright 2024 Mytra Control S.L. All rights reserved. * * Use of this source code is governed by an MIT-style license that can be found in the LICENSE file * or at https://opensource.org/licenses/MIT. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.Helpers = void 0; const lodash_1 = require("lodash"); const uuid_1 = require("uuid"); const types_1 = require("../types"); const Checkers_1 = require("./Checkers"); const Constants_1 = require("./Constants"); class Helpers { /** * Return a "Processing" response message for concrete command or undefined if the response * its not needed * @param command - Command to be response * @param id - Consumer identification * @param result - Command result * @param statusText - Status text */ static processing(command, id, result, statusText) { return this.baseResponse(command, id, 102, result, statusText); } /** * Return a "OK" response message for concrete command or undefined if the response * its not needed * @param command - Command to be response * @param id - Consumer identification * @param result - Command result * @param statusText - Status text */ static ok(command, id, result, statusText) { return this.baseResponse(command, id, 200, result, statusText); } /** * Return a "Bad Request" response message for concrete command or undefined if the response * its not needed * @param command - Command to be response * @param id - Consumer identification * @param statusText - Status text */ static badRequest(command, id, statusText) { return this.baseResponse(command, id, 400, undefined, statusText); } /** * Return a "Unauthorized " response message for concrete command or undefined if the response * its not needed * @param command - Command to be response * @param id - Consumer identification * @param statusText - Status text */ static unauthorized(command, id, statusText) { return this.baseResponse(command, id, 401, undefined, statusText); } /** * Return a "Forbidden" response message for concrete command or undefined if the response * its not needed * @param command - Command to be response * @param id - Consumer identification * @param statusText - Status text */ static forbidden(command, id, statusText) { return this.baseResponse(command, id, 403, undefined, statusText); } /** * Return a "Not Found" response message for concrete command or undefined if the response * its not needed * @param command - Command to be response * @param id - Consumer identification * @param statusText - Status text */ static notFound(command, id, statusText) { return this.baseResponse(command, id, 404, undefined, statusText); } /** * Return a "Internal Error" response message for concrete command or undefined if the response * its not needed * @param command - Command to be response * @param id - Consumer identification * @param statusText - Status text */ static internalError(command, id, statusText) { return this.baseResponse(command, id, 500, undefined, statusText); } /** * Return a "Not implemented" response message for concrete command or undefined if the response * its not needed * @param command - Command to be response * @param id - Consumer identification * @param statusText - Status text */ static notImplemented(command, id, statusText) { return this.baseResponse(command, id, 501, undefined, statusText); } /** * Return a "Service Unavailable" response message for concrete command or undefined if the * response its not needed * @param command - Command to be response * @param id - Consumer identification * @param statusText - Status text */ static serviceUnavailable(command, id, statusText) { return this.baseResponse(command, id, 503, undefined, statusText); } /** * Return response with the indicated code message for concrete command or undefined if the * response its not needed * @param command - Command to be response * @param id - Consumer identification * @param code - Status code * @param results - response results to be included * @param statusText - Status text */ static baseResponse(command, id, status, results, statusText) { const responseRequested = (0, lodash_1.get)(command, 'content.args.response_requested', types_1.Control.ResponseType.Complete); if (responseRequested === types_1.Control.ResponseType.None) { return undefined; } else { return { content_type: Constants_1.Constants.DEFAULT_CONTENT_TYPE, msg_type: types_1.Control.MessageType.Response, request_id: command.request_id, status, created: Date.now(), from: id, to: [command.from], content: { status, status_text: statusText, results, }, }; } } /** * Create a command message * @param to - expected receiver of this command * @param content - command content * @param id - producer identification * @returns */ static createCommand(to, content, id) { const args = Checkers_1.Checkers.isDelayDefinedOnCommand(content) ? content.args : { start_time: Date.now(), duration: Constants_1.Constants.DEFAULT_MAX_RESPONSE_COMMAND_DELAY, }; return { content_type: Constants_1.Constants.DEFAULT_CONTENT_TYPE, msg_type: types_1.Control.MessageType.Command, request_id: (0, uuid_1.v4)(), created: Date.now(), from: id, to, content: { action: content.action, target: content.target, actuator: content.actuator, args, command_id: content.command_id, }, }; } /** * Create a command message * @param to - expected receiver of this command * @param action - command action * @param target - command target * @param id - producer identification * @returns */ static createCommandByAction(to, action, target, id) { return this.createCommand(to, { action, target }, id); } /** * Return the "query features" request with the requested duration * @returns */ static queryFeatures(duration) { return { action: types_1.Control.Action.Query, target: { features: [ types_1.Control.Features.Pairs, types_1.Control.Features.Profiles, types_1.Control.Features.RateLimit, types_1.Control.Features.Versions, ], }, args: { start_time: Date.now(), duration, response_requested: types_1.Control.ResponseType.Complete, }, }; } /** * Return a "Query Features" response based in the request received * @param command - Command to be response * @param id - Consumer identification * @param pairs - Action-Target pairs supported by the consumer * @param profiles - Profiles supported by the consumer * @returns */ static respondFeatures(command, id, pairs, profiles) { const features = (0, lodash_1.get)(command, 'content.target.features', []); const results = {}; results['pairs'] = features.includes('pairs') ? pairs : undefined; results['rate_limit'] = features.includes('rate_limit') ? Constants_1.Constants.DEFAULT_RATE_LIMIT : undefined; results['profiles'] = features.includes('profiles') ? profiles : undefined; results['versions'] = features.includes('versions') ? Constants_1.Constants.SUPPORTED_VERSIONS : undefined; return this.ok(command, id, results); } } exports.Helpers = Helpers; //# sourceMappingURL=Helpers.js.map