@mitre/inspec-objects 0.0.4 → 0.0.7

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.
@@ -1,10 +1,15 @@
1
1
  import { ExecJSON } from "inspecjs";
2
+ export declare function objectifyDescriptions(descs: ExecJSON.ControlDescription[] | {
3
+ [key: string]: string | undefined;
4
+ } | null | undefined): {
5
+ [key: string]: string | undefined;
6
+ } | null | undefined;
2
7
  export default class Control {
3
8
  id?: string | null;
4
9
  title?: string | null;
5
10
  code?: string | null;
6
11
  desc?: string | null;
7
- descs?: ExecJSON.ControlDescription[] | {
12
+ descs?: {
8
13
  [key: string]: string | undefined;
9
14
  } | null;
10
15
  impact?: number;
@@ -1,9 +1,21 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.objectifyDescriptions = void 0;
3
4
  const tslib_1 = require("tslib");
4
5
  const lodash_1 = tslib_1.__importDefault(require("lodash"));
5
6
  const flat_1 = require("flat");
6
7
  const global_1 = require("../utilities/global");
8
+ function objectifyDescriptions(descs) {
9
+ if (Array.isArray(descs)) {
10
+ const descriptions = {};
11
+ descs.forEach((description) => {
12
+ descriptions[description.label] = description.data;
13
+ });
14
+ return descriptions;
15
+ }
16
+ return descs;
17
+ }
18
+ exports.objectifyDescriptions = objectifyDescriptions;
7
19
  class Control {
8
20
  constructor(data) {
9
21
  this.tags = {};
@@ -28,13 +40,13 @@ class Control {
28
40
  let result = "# encoding: UTF-8\n\n";
29
41
  result += `control "${this.id}" do\n`;
30
42
  if (this.title) {
31
- result += ` title "${(0, global_1.wrapAndEscapeQuotes)(this.title, lineLength)}"\n`;
43
+ result += ` title "${(0, global_1.wrapAndEscapeQuotes)((0, global_1.removeNewlinePlaceholders)(this.title), lineLength)}"\n`;
32
44
  }
33
45
  else {
34
46
  console.error(`${this.id} does not have a title`);
35
47
  }
36
48
  if (this.desc) {
37
- result += ` desc "${(0, global_1.wrapAndEscapeQuotes)(this.desc, lineLength)}"\n`;
49
+ result += ` desc "${(0, global_1.wrapAndEscapeQuotes)((0, global_1.removeNewlinePlaceholders)(this.desc), lineLength)}"\n`;
38
50
  }
39
51
  else {
40
52
  console.error(`${this.id} does not have a desc`);
@@ -42,7 +54,7 @@ class Control {
42
54
  if (this.descs) {
43
55
  Object.entries(this.descs).forEach(([key, desc]) => {
44
56
  if (desc) {
45
- result += ` desc "${key}", "${(0, global_1.wrapAndEscapeQuotes)(desc, lineLength)}"\n`;
57
+ result += ` desc "${key}", "${(0, global_1.wrapAndEscapeQuotes)((0, global_1.removeNewlinePlaceholders)(desc), lineLength)}"\n`;
46
58
  }
47
59
  else {
48
60
  console.error(`${this.id} does not have a desc for the value ${key}`);
@@ -58,10 +70,10 @@ class Control {
58
70
  if (this.refs) {
59
71
  this.refs.forEach((ref) => {
60
72
  if (typeof ref === 'string') {
61
- result += ` ref '${(0, global_1.escapeQuotes)(ref)}'\n`;
73
+ result += ` ref '${(0, global_1.escapeQuotes)((0, global_1.removeNewlinePlaceholders)(ref))}'\n`;
62
74
  }
63
75
  else {
64
- result += ` ref '${(0, global_1.escapeQuotes)(ref.ref || '')}', url: '${(0, global_1.escapeQuotes)(ref.url || '')}'`;
76
+ result += ` ref '${(0, global_1.escapeQuotes)((0, global_1.removeNewlinePlaceholders)(ref.ref || ''))}', url: '${(0, global_1.escapeQuotes)((0, global_1.removeNewlinePlaceholders)(ref.url || ''))}'`;
65
77
  }
66
78
  });
67
79
  }
@@ -84,7 +96,7 @@ class Control {
84
96
  }
85
97
  }
86
98
  else if (typeof value === "string") {
87
- result += ` tag ${tag}: "${(0, global_1.wrapAndEscapeQuotes)(value, lineLength)}"\n`;
99
+ result += ` tag ${tag}: "${(0, global_1.wrapAndEscapeQuotes)((0, global_1.removeNewlinePlaceholders)(value), lineLength)}"\n`;
88
100
  }
89
101
  }
90
102
  });
@@ -4,7 +4,7 @@ exports.processJSON = exports.processExecJSON = exports.processProfileJSON = exp
4
4
  const tslib_1 = require("tslib");
5
5
  const inspecjs_1 = require("inspecjs");
6
6
  const lodash_1 = tslib_1.__importDefault(require("lodash"));
7
- const control_1 = tslib_1.__importDefault(require("../objects/control"));
7
+ const control_1 = tslib_1.__importStar(require("../objects/control"));
8
8
  const profile_1 = tslib_1.__importDefault(require("../objects/profile"));
9
9
  function processEvaluation(evaluationInput) {
10
10
  const topLevelProfile = evaluationInput.contains[0];
@@ -25,7 +25,7 @@ function processEvaluation(evaluationInput) {
25
25
  title: control.data.title,
26
26
  impact: control.data.impact,
27
27
  desc: control.data.desc,
28
- descs: control.hdf.wraps.descriptions,
28
+ descs: (0, control_1.objectifyDescriptions)(control.hdf.wraps.descriptions),
29
29
  tags: control.hdf.wraps.tags,
30
30
  }));
31
31
  });
@@ -52,7 +52,7 @@ function processProfileJSON(profileInput) {
52
52
  impact: control.impact,
53
53
  code: control.code,
54
54
  tags: control.tags,
55
- descs: control.descriptions,
55
+ descs: (0, control_1.objectifyDescriptions)(control.descriptions),
56
56
  }));
57
57
  });
58
58
  return profile;
@@ -5,4 +5,4 @@ export declare type GroupContextualizedRule = BenchmarkRule & {
5
5
  group: Omit<BenchmarkGroup, 'Rule' | 'Group'>;
6
6
  };
7
7
  export declare function extractAllRules(groups: BenchmarkGroup[]): GroupContextualizedRule[];
8
- export declare function processXCCDF(xml: string, removeNewlines?: boolean, ovalDefinitions?: Record<string, OvalDefinitionValue>): Profile;
8
+ export declare function processXCCDF(xml: string, removeNewlines: boolean | undefined, useRuleId: 'group' | 'rule' | 'version', ovalDefinitions?: Record<string, OvalDefinitionValue>): Profile;
@@ -25,7 +25,7 @@ function extractAllRules(groups) {
25
25
  return rules;
26
26
  }
27
27
  exports.extractAllRules = extractAllRules;
28
- function processXCCDF(xml, removeNewlines = false, ovalDefinitions) {
28
+ function processXCCDF(xml, removeNewlines = false, useRuleId, ovalDefinitions) {
29
29
  const parsedXML = (0, xccdf_1.convertEncodedXmlIntoJson)(xml);
30
30
  const rules = extractAllRules(parsedXML.Benchmark[0].Group);
31
31
  const profile = new profile_1.default({
@@ -43,7 +43,19 @@ function processXCCDF(xml, removeNewlines = false, ovalDefinitions) {
43
43
  extractedDescription = (0, xccdf_1.convertEncodedHTMLIntoJson)(rule.description);
44
44
  }
45
45
  const control = new control_1.default();
46
- control.id = rule['@_id'];
46
+ switch (useRuleId) {
47
+ case 'group':
48
+ control.id = rule.group['@_id'];
49
+ break;
50
+ case 'rule':
51
+ control.id = rule['@_id'].split('r')[0];
52
+ break;
53
+ case 'version':
54
+ control.id = rule.version;
55
+ break;
56
+ default:
57
+ throw new Error('useRuleId must be one of "group", "rule", or "version"');
58
+ }
47
59
  if (removeNewlines) {
48
60
  const title = (0, xccdf_1.removeXMLSpecialCharacters)(rule['@_severity'] ? rule.title : `[[[MISSING SEVERITY FROM STIG]]] ${rule.title}`);
49
61
  control.title = title.replace(/\n/g, '{{{{newlineHERE}}}}');
@@ -49,10 +49,8 @@ function diffProfile(fromProfile, toProfile) {
49
49
  const toControl = toProfile.controls.find((control) => control.id === fromControl.id);
50
50
  if (toControl) {
51
51
  const controlDiff = (0, json_diff_1.diff)(fromControl, toControl);
52
- console.log(controlDiff);
53
52
  if (controlDiff) {
54
53
  Object.entries(controlDiff).forEach(([key, value]) => {
55
- console.log(value);
56
54
  if (lodash_1.default.has(value, '__new')) {
57
55
  lodash_1.default.set(profileDiff, 'changedControls.' + fromControl.id + '.' + key.replace('.', '\\.'), lodash_1.default.get(controlDiff, key + '.__new'));
58
56
  }
@@ -4,5 +4,6 @@ declare const escapeQuotes: (s: string) => string;
4
4
  declare const escapeDoubleQuotes: (s: string) => string;
5
5
  declare const wrapAndEscapeQuotes: (s: string, lineLength?: number) => string;
6
6
  export { escapeQuotes, escapeDoubleQuotes, wrapAndEscapeQuotes };
7
+ export declare function removeNewlinePlaceholders(s: string): string;
7
8
  export declare function getFirstPath(object: Record<string, unknown>, paths: string[]): string;
8
9
  export declare function hasPath(file: Record<string, unknown>, path: string | string[]): boolean;
@@ -1,11 +1,39 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.hasPath = exports.getFirstPath = exports.wrapAndEscapeQuotes = exports.escapeDoubleQuotes = exports.escapeQuotes = exports.unformatText = exports.wrap = void 0;
3
+ exports.hasPath = exports.getFirstPath = exports.removeNewlinePlaceholders = exports.wrapAndEscapeQuotes = exports.escapeDoubleQuotes = exports.escapeQuotes = exports.unformatText = exports.wrap = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const lodash_1 = tslib_1.__importDefault(require("lodash"));
6
6
  // Breaks lines down to lineLength number of characters
7
7
  function wrap(s, lineLength = 80) {
8
- return s.replace(new RegExp(`(?![^\n]{1,${lineLength}}$)([^\n]{1,${lineLength}})`, "g"), "$1\n");
8
+ let newString = "";
9
+ let currentLine = "";
10
+ let currentLength = 0;
11
+ let shouldBreakLine = false;
12
+ for (var i = 0; i < s.length; i++) {
13
+ if (shouldBreakLine) {
14
+ newString += `\n`;
15
+ currentLength = 0;
16
+ shouldBreakLine = false;
17
+ }
18
+ let currentChar = s.charAt(i);
19
+ let nextChar = s.charAt(i + 1);
20
+ if (nextChar === " ") {
21
+ if (currentLength >= lineLength) {
22
+ shouldBreakLine = true;
23
+ newString += currentChar;
24
+ currentLength++;
25
+ }
26
+ else {
27
+ newString += currentChar;
28
+ currentLength++;
29
+ }
30
+ }
31
+ else {
32
+ newString += currentChar;
33
+ currentLength++;
34
+ }
35
+ }
36
+ return newString;
9
37
  }
10
38
  exports.wrap = wrap;
11
39
  function unformatText(s) {
@@ -18,6 +46,10 @@ const escapeDoubleQuotes = (s) => s.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
18
46
  exports.escapeDoubleQuotes = escapeDoubleQuotes;
19
47
  const wrapAndEscapeQuotes = (s, lineLength) => escapeDoubleQuotes(wrap(s, lineLength)); // Escape backslashes and quotes, and wrap long lines
20
48
  exports.wrapAndEscapeQuotes = wrapAndEscapeQuotes;
49
+ function removeNewlinePlaceholders(s) {
50
+ return s.replace(/\{\{\{\{newlineHERE\}\}\}\}/g, '\n');
51
+ }
52
+ exports.removeNewlinePlaceholders = removeNewlinePlaceholders;
21
53
  function getFirstPath(object, paths) {
22
54
  const index = lodash_1.default.findIndex(paths, (p) => hasPath(object, p));
23
55
  if (index === -1) {
Binary file
Binary file
package/package-lock.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@mitre/inspec-objects",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "lockfileVersion": 2,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "@mitre/inspec-objects",
9
- "version": "0.0.1",
9
+ "version": "0.0.3",
10
10
  "license": "Apache-2.0",
11
11
  "dependencies": {
12
12
  "@types/flat": "^5.0.2",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mitre/inspec-objects",
3
- "version": "0.0.4",
3
+ "version": "0.0.7",
4
4
  "description": "Typescript objects for normalizing between InSpec profiles and XCCDF benchmarks",
5
5
  "main": "lib/index.js",
6
6
  "publishConfig": {