@mitre/inspec-objects 0.0.10 → 0.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,7 +22,15 @@ exports.removeNewlines = removeNewlines;
22
22
  function simplifyDiff(diffData) {
23
23
  return lodash_1.default.transform(diffData, (result, diffValue, key) => {
24
24
  if (lodash_1.default.has(diffValue, '__new')) {
25
- result[key] = lodash_1.default.get(diffValue, '__new');
25
+ // Remove any trailing space
26
+ if (typeof lodash_1.default.get(diffValue, '__new') === 'string' && typeof lodash_1.default.get(diffValue, '__old') === 'string') {
27
+ if (lodash_1.default.get(diffValue, '__new').trim() !== lodash_1.default.get(diffValue, '__old').trim()) {
28
+ lodash_1.default.set(result, key, lodash_1.default.get(diffValue, '__new'));
29
+ }
30
+ }
31
+ else {
32
+ result[key] = lodash_1.default.get(diffValue, '__new');
33
+ }
26
34
  }
27
35
  else if (Array.isArray(diffValue)) {
28
36
  result[key] = diffValue.map((value) => value[0] === '+' && value[1]).filter(value => value);
@@ -43,11 +51,13 @@ function diffProfile(fromProfile, toProfile) {
43
51
  const profileDiff = {
44
52
  addedControlIDs: [],
45
53
  removedControlIDs: [],
54
+ addedControls: {},
46
55
  changedControls: {}
47
56
  };
48
57
  const originalDiff = {
49
58
  addedControlIDs: [],
50
59
  removedControlIDs: [],
60
+ addedControls: {},
51
61
  changedControls: {}
52
62
  };
53
63
  const fromControlIDs = fromProfile.controls.map((control) => control.id).sort();
@@ -68,8 +78,8 @@ function diffProfile(fromProfile, toProfile) {
68
78
  profileDiff.addedControlIDs.forEach((addedControl) => {
69
79
  const newControl = toProfile.controls.find((control) => addedControl === control.id);
70
80
  if (newControl) {
71
- profileDiff.changedControls[addedControl] = newControl;
72
- originalDiff.changedControls[addedControl] = newControl;
81
+ profileDiff.addedControls[addedControl] = newControl;
82
+ originalDiff.addedControls[addedControl] = newControl;
73
83
  }
74
84
  });
75
85
  // Find changed controls
@@ -0,0 +1,6 @@
1
+ import { ProfileDiff } from "../types/diff";
2
+ import Profile from '../objects/profile';
3
+ export declare function createDiffMarkdown(diff: {
4
+ simplified: ProfileDiff;
5
+ originalDiff: any;
6
+ }, updatedProfile: Profile): string;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createDiffMarkdown = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const mustache_1 = tslib_1.__importDefault(require("mustache"));
6
+ const lodash_1 = tslib_1.__importDefault(require("lodash"));
7
+ const global_1 = require("./global");
8
+ const xccdf_1 = require("./xccdf");
9
+ const automatticUpdateTemplate_json_1 = tslib_1.__importDefault(require("../resources/automatticUpdateTemplate.json"));
10
+ function getUpdatedCheckForId(id, profile) {
11
+ const foundControl = profile.controls.find((control) => control.id === id);
12
+ return lodash_1.default.get(foundControl === null || foundControl === void 0 ? void 0 : foundControl.descs, 'check') || 'Missing check';
13
+ }
14
+ function createDiffMarkdown(diff, updatedProfile) {
15
+ const renderableDiffData = {
16
+ addedControls: Object.values(diff.simplified.addedControls),
17
+ checks: [],
18
+ fixes: [],
19
+ };
20
+ Object.entries(diff.simplified.changedControls).forEach(([id, updatedControl]) => {
21
+ if (lodash_1.default.get(updatedControl, "descs.check")) {
22
+ // console.log(removeXMLSpecialCharacters(removeNewlinePlaceholders(getUpdatedCheckForId(id, updatedProfile))))
23
+ renderableDiffData.checks.push({
24
+ id,
25
+ check: (0, xccdf_1.removeXMLSpecialCharacters)((0, global_1.removeNewlinePlaceholders)(getUpdatedCheckForId(id, updatedProfile))),
26
+ });
27
+ }
28
+ });
29
+ // Render output
30
+ return mustache_1.default.render(automatticUpdateTemplate_json_1.default.data, renderableDiffData);
31
+ }
32
+ exports.createDiffMarkdown = createDiffMarkdown;
@@ -0,0 +1,2 @@
1
+ import winston from "winston";
2
+ export declare function createWinstonLogger(mapperName: string, level?: string): winston.Logger;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createWinstonLogger = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const winston_1 = tslib_1.__importDefault(require("winston"));
6
+ function createWinstonLogger(mapperName, level = 'debug') {
7
+ return winston_1.default.createLogger({
8
+ transports: [new winston_1.default.transports.Console()],
9
+ level: level,
10
+ format: winston_1.default.format.combine(winston_1.default.format.timestamp({
11
+ format: 'MMM-DD-YYYY HH:mm:ss Z',
12
+ }), winston_1.default.format.printf(info => `[${[info.timestamp]}] ${mapperName} ${info.message}`)),
13
+ });
14
+ }
15
+ exports.createWinstonLogger = createWinstonLogger;
@@ -0,0 +1,16 @@
1
+ import winston from "winston";
2
+ import Control from '../objects/control';
3
+ import Profile from '../objects/profile';
4
+ import { ProfileDiff } from '../types/diff';
5
+ import { OvalDefinitionValue } from '../types/oval';
6
+ export declare type UpdatedProfileReturn = {
7
+ profile: Profile;
8
+ diff: {
9
+ simplified: ProfileDiff;
10
+ originalDiff: Record<string, unknown>;
11
+ };
12
+ markdown: string;
13
+ };
14
+ export declare function updateControl(from: Control, update: Partial<Control>): Control;
15
+ export declare function updateProfile(from: Profile, using: Profile): Omit<UpdatedProfileReturn, 'markdown'>;
16
+ export declare function updateProfileUsingXCCDF(from: Profile, using: string, id: 'group' | 'rule' | 'version', logger: winston.Logger, ovalDefinitions?: Record<string, OvalDefinitionValue>): UpdatedProfileReturn;
@@ -0,0 +1,97 @@
1
+ "use strict";
2
+ // Utilities to update a profile or control with new metadata
3
+ // The ultimate goal is to preserve all the metadata that is already there and only add what is new
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.updateProfileUsingXCCDF = exports.updateProfile = exports.updateControl = void 0;
6
+ const tslib_1 = require("tslib");
7
+ const lodash_1 = tslib_1.__importDefault(require("lodash"));
8
+ const profile_1 = tslib_1.__importDefault(require("../objects/profile"));
9
+ const xccdf_1 = require("../parsers/xccdf");
10
+ const diff_1 = require("./diff");
11
+ const diffMarkdown_1 = require("./diffMarkdown");
12
+ function projectValuesOntoExistingObj(dst, src, currentPath = '') {
13
+ for (const updatedValue in src) {
14
+ const existingValue = lodash_1.default.get(dst, updatedValue);
15
+ // We have a new value for something that already exists in dst
16
+ if (existingValue !== undefined) {
17
+ if (typeof existingValue === 'object' && existingValue !== null && !Array.isArray(existingValue)) {
18
+ dst[updatedValue] = projectValuesOntoExistingObj(existingValue, src[updatedValue], currentPath + updatedValue + '.');
19
+ }
20
+ else if (typeof src[updatedValue] === 'string') {
21
+ lodash_1.default.set(dst, updatedValue, src[updatedValue].trim());
22
+ }
23
+ else if (typeof src[updatedValue] === 'number') {
24
+ lodash_1.default.set(dst, updatedValue, src[updatedValue]);
25
+ }
26
+ else if (Array.isArray(src[updatedValue])) {
27
+ const uniqueArrayValues = [...new Set(lodash_1.default.get(dst, updatedValue, []).concat(src[updatedValue]))];
28
+ lodash_1.default.set(dst, updatedValue, uniqueArrayValues);
29
+ }
30
+ }
31
+ }
32
+ return dst;
33
+ }
34
+ function updateControl(from, update) {
35
+ return projectValuesOntoExistingObj(from, update);
36
+ }
37
+ exports.updateControl = updateControl;
38
+ function updateProfile(from, using) {
39
+ // Update the profile with the new metadata
40
+ const to = new profile_1.default(lodash_1.default.omit(from, 'controls'));
41
+ // Find the diff
42
+ const diff = (0, diff_1.diffProfile)(from, using);
43
+ // Add the new controls
44
+ diff.simplified.addedControlIDs.forEach(id => {
45
+ const addedControl = diff.simplified.addedControls[id];
46
+ if (addedControl) {
47
+ to.controls.push(addedControl);
48
+ }
49
+ else {
50
+ throw new Error("New control added but don't have the control data");
51
+ }
52
+ });
53
+ // Update the existing controls
54
+ for (const existingControl of from.controls) {
55
+ const updatedControl = using.controls.find(control => control.id === existingControl.id);
56
+ if (updatedControl) {
57
+ const controlDiff = diff.simplified.changedControls[existingControl.id];
58
+ if (controlDiff) {
59
+ to.controls.push(updateControl(existingControl, controlDiff));
60
+ }
61
+ else {
62
+ to.controls.push(existingControl);
63
+ }
64
+ }
65
+ else {
66
+ console.log("Control not updated: " + existingControl.id);
67
+ }
68
+ }
69
+ return {
70
+ profile: to,
71
+ diff,
72
+ };
73
+ }
74
+ exports.updateProfile = updateProfile;
75
+ function updateProfileUsingXCCDF(from, using, id, logger, ovalDefinitions) {
76
+ console.log(from.controls[0].desc);
77
+ // Parse the XCCDF benchmark and convert it into a Profile
78
+ logger.debug('Loading XCCDF File');
79
+ const xccdfProfile = (0, xccdf_1.processXCCDF)(using, false, id);
80
+ logger.debug('Loaded XCCDF File');
81
+ logger.debug('Loading XCCDF File with newline replacements');
82
+ const xccdfProfileWithNLReplacement = (0, xccdf_1.processXCCDF)(using, true, id);
83
+ logger.debug('Loaded XCCDF File with newline replacements');
84
+ // Update the profile and return
85
+ logger.debug('Creating updated profile');
86
+ const updatedProfile = updateProfile(from, xccdfProfile);
87
+ logger.debug('Creating diff markdown');
88
+ // Create the markdown
89
+ const markdown = (0, diffMarkdown_1.createDiffMarkdown)(updatedProfile.diff, xccdfProfileWithNLReplacement);
90
+ logger.debug('Profile update complete');
91
+ return {
92
+ profile: updatedProfile.profile,
93
+ diff: updatedProfile.diff,
94
+ markdown: markdown
95
+ };
96
+ }
97
+ exports.updateProfileUsingXCCDF = updateProfileUsingXCCDF;
@@ -1,6 +1,6 @@
1
1
  import { DecodedDescription } from '../types/xccdf';
2
2
  export declare function convertEncodedXmlIntoJson(encodedXml: string): any;
3
- export declare function removeXMLSpecialCharacters(input: string): string;
3
+ export declare function removeXMLSpecialCharacters(str: string): string;
4
4
  export declare function severityStringToImpact(string: string, id: string): number;
5
5
  export declare function impactNumberToSeverityString(impact: number): string;
6
6
  export declare function convertEncodedHTMLIntoJson(encodedHTML?: string): DecodedDescription;
@@ -5,6 +5,21 @@ const tslib_1 = require("tslib");
5
5
  const fast_xml_parser_1 = tslib_1.__importDefault(require("fast-xml-parser"));
6
6
  const htmlparser = tslib_1.__importStar(require("htmlparser2"));
7
7
  const lodash_1 = tslib_1.__importDefault(require("lodash"));
8
+ const he_1 = tslib_1.__importDefault(require("he"));
9
+ var htmlEntities = {
10
+ nbsp: ' ',
11
+ cent: '¢',
12
+ pound: '£',
13
+ yen: '¥',
14
+ euro: '€',
15
+ copy: '©',
16
+ reg: '®',
17
+ lt: '<',
18
+ gt: '>',
19
+ quot: '"',
20
+ amp: '&',
21
+ apos: '\''
22
+ };
8
23
  function convertEncodedXmlIntoJson(encodedXml) {
9
24
  return fast_xml_parser_1.default.parse(encodedXml, {
10
25
  ignoreAttributes: false,
@@ -14,10 +29,11 @@ function convertEncodedXmlIntoJson(encodedXml) {
14
29
  });
15
30
  }
16
31
  exports.convertEncodedXmlIntoJson = convertEncodedXmlIntoJson;
17
- function removeXMLSpecialCharacters(input) {
18
- return input.replace(/&amp;/gm, '&').replace(/&lt;/gm, '<').replace(/&gt;/gm, '>');
32
+ function removeXMLSpecialCharacters(str) {
33
+ return he_1.default.decode(str);
19
34
  }
20
35
  exports.removeXMLSpecialCharacters = removeXMLSpecialCharacters;
36
+ ;
21
37
  function severityStringToImpact(string, id) {
22
38
  var _a, _b, _c, _d, _e;
23
39
  if ((_a = string.match(/none|na|n\/a|not[\s()*_|]?applicable/i)) === null || _a === void 0 ? void 0 : _a.length) {