Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve xccdf_results_mapper when converting XCCDF Results to HDF Results #4255

Merged
merged 28 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
403987d
Improve xccdf_results_mapper when converting XCCDF->HDF
candrews Apr 12, 2023
88cd988
Merge branch 'master' into xccdf_results_mapper-improvements
aaronlippold Apr 13, 2023
25b361c
Minor changes requested by code review
candrews May 9, 2023
e8bc220
Use the "RegExp.exec()" method instead
candrews May 9, 2023
263cf60
Set impact to 0 for 'notapplicable' and 'informational' results
candrews May 9, 2023
c2640f5
Don't handle every array item within each array item
candrews May 11, 2023
51f047f
"version" should use "version.text" (not just "version")
candrews May 11, 2023
2f63274
For version, prefer version over id
candrews May 22, 2023
6992f6d
For version, remove unnecessary comment
candrews May 22, 2023
9276f02
Remove unnecessary String conversion
candrews May 22, 2023
71fad37
Add tsdoc to getRulesInGroup
candrews May 22, 2023
4b01555
Merge branch 'master' into xccdf_results_mapper-improvements
Amndeep7 May 23, 2023
f216a6f
removed 'id' as a potential path for 'version'. the complianceascode…
Amndeep7 May 23, 2023
747bdc8
linting
Amndeep7 May 23, 2023
2f618b2
Use triple equals for string comparson
candrews May 23, 2023
5a9cf54
Various fixes
candrews Jun 17, 2023
aad9481
Correct "refs" to comply with schema
candrews Jun 21, 2023
68ab499
Only include description if it has a label
candrews Jun 21, 2023
587282d
make the nist family part of the regexes only match against valid nis…
Amndeep7 Jun 23, 2023
d637e70
Use `as unknown as ControlDescription` instead of `as any`
candrews Jun 23, 2023
8df5b2d
Run lint on src/nist.ts
candrews Jun 23, 2023
cd8b2ee
Regenerate samples
candrews Jun 23, 2023
fc89563
Use concise character class syntax '\d' instead of '[0-9]'.
candrews Jun 23, 2023
ebdaade
the treemap expects a canonized form of the nist controls that are no…
Amndeep7 Jul 13, 2023
d855250
get rid of dupe nist tags - even if there were dupes in the original …
Amndeep7 Jul 13, 2023
e926155
could simplify the default_partial_config implementation and also ran…
Amndeep7 Jul 13, 2023
9e93622
sonarqube
Amndeep7 Jul 13, 2023
cd2aaa6
Merge branch 'master' into xccdf_results_mapper-improvements
Amndeep7 Jul 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

19,803 changes: 8,851 additions & 10,952 deletions libs/hdf-converters/sample_jsons/xccdf_results_mapper/xccdf-openscap-rhel7-hdf.json

Large diffs are not rendered by default.

22,992 changes: 10,174 additions & 12,818 deletions libs/hdf-converters/sample_jsons/xccdf_results_mapper/xccdf-openscap-rhel8-hdf-withraw.json

Large diffs are not rendered by default.

22,976 changes: 10,166 additions & 12,810 deletions libs/hdf-converters/sample_jsons/xccdf_results_mapper/xccdf-openscap-rhel8-hdf.json

Large diffs are not rendered by default.

19,234 changes: 8,642 additions & 10,592 deletions libs/hdf-converters/sample_jsons/xccdf_results_mapper/xccdf-scc-rhel7-hdf-withraw.json

Large diffs are not rendered by default.

19,066 changes: 8,558 additions & 10,508 deletions libs/hdf-converters/sample_jsons/xccdf_results_mapper/xccdf-scc-rhel7-hdf.json

Large diffs are not rendered by default.

22,482 changes: 9,920 additions & 12,562 deletions libs/hdf-converters/sample_jsons/xccdf_results_mapper/xccdf-scc-rhel8-hdf-withraw.json

Large diffs are not rendered by default.

22,484 changes: 9,921 additions & 12,563 deletions libs/hdf-converters/sample_jsons/xccdf_results_mapper/xccdf-scc-rhel8-hdf.json

Large diffs are not rendered by default.

614 changes: 380 additions & 234 deletions libs/hdf-converters/src/xccdf-results-mapper.ts
Amndeep7 marked this conversation as resolved.
Show resolved Hide resolved

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions libs/hdf-converters/test/mappers/forward/xccdf_mapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,30 @@ describe('xccdf_mapper', () => {
});
});
describe('OpenSCAP', () => {
it('ubuntu1804', () => {
const mapper = new XCCDFResultsMapper(
fs.readFileSync(
'sample_jsons/xccdf_results_mapper/sample_input_report/xccdf-results-openscap-ComplianceAsCode-ubuntu1804.xml',
{encoding: 'utf-8'}
)
);

// fs.writeFileSync(
Amndeep7 marked this conversation as resolved.
Show resolved Hide resolved
// 'sample_jsons/xccdf_results_mapper/xccdf-openscap-ComplianceAsCode-ubuntu1804-hdf.json',
// JSON.stringify(mapper.toHdf(), null, 2)
// );

expect(omitVersions(mapper.toHdf())).toEqual(
omitVersions(
JSON.parse(
fs.readFileSync(
'sample_jsons/xccdf_results_mapper/xccdf-openscap-ComplianceAsCode-ubuntu1804-hdf.json',
{encoding: 'utf-8'}
)
)
)
);
});
it('rhel7', () => {
const mapper = new XCCDFResultsMapper(
fs.readFileSync(
Expand Down
37 changes: 21 additions & 16 deletions libs/inspecjs/src/nist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,38 @@ import {ControlStatus} from './compat_wrappers';
import {ALL_NIST_CONTROL_NUMBERS, ALL_NIST_FAMILIES} from './raw_nist';

// Regexes.
const NIST_FAMILY_RE = /^[A-Z]{2}$/;
// Matches against only currently existing NIST Control families instead of allowing any two adjacent uppercase letters
Amndeep7 marked this conversation as resolved.
Show resolved Hide resolved
const NIST_FAMILY_RE =
/^(?:A[CPRTU]|C[AMP]|D[IM]|I[APR]|M[AP]|P[ELMS]|RA|S[ACEI]|TR|U[LM])$/;
// Limit length of children to avoid potential DoS on malicious NIST Control strings
const NIST_CONTROL_RE = /^([A-Z]{2})-([0-9]+)(.{0,60})$/;
const NIST_CONTROL_RE =
/^(A[CPRTU]|C[AMP]|D[IM]|I[APR]|M[AP]|P[ELMS]|RA|S[ACEI]|TR|U[LM])-(\d+)(.{0,60})$/;
const SPEC_SPLITTER = /[\s\(\)\.]+/; // Includes all whitespace, periods, and parenthesis
const REV_RE = /^rev[\s_.]+(\d+)$/i; // Matches Rev_5 etc
type ParseNist = NistControl | NistRevision | null;

export interface CanonizationConfig {
max_specifiers: number;

// All are assumed false
max_specifiers?: number; // default 5: $ rg '<number>' SP_800-53_v5_1_XML.xml | awk -F'[^ ]' '{print length($1)}' | sort -nr | head -1 | xargs -I{} expr \( {} - 6 \) / 3 # this equals 5 as of rev5
pad_zeros?: boolean; // default false
allow_letters?: boolean; // default true
add_spaces?: boolean; // default true
add_parens?: boolean; // default true
add_periods?: boolean; // default true
}

function default_partial_config(c: CanonizationConfig): CanonizationConfig {
return {
pad_zeros: false,
allow_letters: true,
add_spaces: true,
add_parens: true,
add_periods: true,
...c
};
export const DEFAULT_CANONIZATION_CONFIG = {
max_specifiers: 5,
pad_zeros: false,
allow_letters: true,
add_spaces: true,
add_parens: true,
add_periods: true
};

function default_partial_config(
c?: CanonizationConfig
): Required<CanonizationConfig> {
return {...DEFAULT_CANONIZATION_CONFIG, ...c};
}

/** Represents a single nist control, or group of controls if the sub specs are vague enoug. */
Expand Down Expand Up @@ -123,8 +128,8 @@ export class NistControl {
* This is, unfortunately, slightly expensive.
* Avoid repeating this if possible.
*/
canonize(config: CanonizationConfig): string {
config = default_partial_config(config);
canonize(c?: CanonizationConfig): string {
const config = default_partial_config(c);
const ss = this.subSpecifiers;

// Build our string. Start with family
Expand Down