-
Notifications
You must be signed in to change notification settings - Fork 41
Supplement tags #1381
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
Draft
GavMason
wants to merge
14
commits into
main
Choose a base branch
from
supplement_tags
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Supplement tags #1381
Changes from 7 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e69c4f9
first pass on supplement tags - write is just a copy, read has been e…
Amndeep7 b317727
Added filtering on controls (-c) for Read Tags
GavMason 99598a9
put filter in the correct location and added another example
Amndeep7 52d727c
Added tags write functionality
GavMason 22fbc18
Updated tags write command to handle only HDF/InspecProfile inputs an…
GavMason 4baa6ef
Small fix for tags write functionality for HDF type inputs
GavMason 4eeb952
Added write command functionality
GavMason 617d459
Fixed non-type requested changes
GavMason 1c4029f
Added type fixes for requested changes
GavMason a13185c
Cleaned up code
GavMason e015493
Fix flag mismatch
GavMason 24e4a3e
Undo last commit
GavMason fe7155b
Added tags extend functionality
GavMason fabaf53
Added dev notes/TODOs
GavMason File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import {Command, Flags} from '@oclif/core' | ||
import {ExecJSON, ProfileJSON} from 'inspecjs' | ||
import fs from 'fs' | ||
|
||
export default class ReadTags extends Command { | ||
static usage = 'supplement tags read -i <hdf-or-profile-json> [-o <tag-json>] [-c control-id ...]' | ||
|
||
static description = 'Read the `tags` attribute in a given Heimdall Data Format or InSpec Profile JSON file and send it to stdout or write it to a file' | ||
|
||
static examples = ['saf supplement tags read -i hdf.json -o tag.json', 'saf supplement tags read -i hdf.json -o tag.json -c V-00001 V-00002'] | ||
|
||
static flags = { | ||
help: Flags.help({char: 'h'}), | ||
input: Flags.string({char: 'i', required: true, description: 'An input HDF or profile file'}), | ||
output: Flags.string({char: 'o', description: 'An output `tags` JSON file (otherwise the data is sent to stdout)'}), | ||
controls: Flags.string({char: 'c', description: 'The id of the control whose tags will be extracted', multiple: true}), | ||
} | ||
|
||
async run() { | ||
const {flags} = await this.parse(ReadTags) | ||
|
||
const input: ExecJSON.Execution | ProfileJSON.Profile = JSON.parse(fs.readFileSync(flags.input, 'utf8')) | ||
|
||
const extractTags = (profile: ExecJSON.Profile | ProfileJSON.Profile) => (profile.controls as Array<ExecJSON.Control | ProfileJSON.Control>).filter(control => flags.controls ? flags.controls.includes(control.id) : true).map(control => control.tags) | ||
|
||
const tags = Object.hasOwn(input, 'profiles') ? (input as ExecJSON.Execution).profiles.map(profile => extractTags(profile)) : extractTags(input as ProfileJSON.Profile) | ||
|
||
if (flags.output) { | ||
fs.writeFileSync(flags.output, JSON.stringify(tags, null, 2)) | ||
} else { | ||
process.stdout.write(JSON.stringify(tags, null, 2)) | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import {Command, Flags} from '@oclif/core' | ||
import {ExecJSON, ProfileJSON} from 'inspecjs' | ||
import fs from 'fs' | ||
|
||
export default class WriteTags extends Command { | ||
static usage = 'supplement tags write -i <input-hdf-or-profile-json> (-f <input-tags-json> | -d <tags-json>) [-o <output-hdf-json>]' | ||
|
||
static description = 'Overwrite the `tags` attribute in a given Heimdall Data Format or InSpec Profile JSON file and overwrite original file or optionally write it to a new file' | ||
|
||
static summary = 'Tags data can be either a Heimdall Data Format or InSpec Profile JSON file. See sample ideas at https://github.com/mitre/saf/wiki/Supplement-HDF-files-with-additional-information-(ex.-%60tags%60,-%60target%60)' | ||
|
||
static examples = [ | ||
'saf supplement tags write -i hdf.json -d \'[[{"a": 5}]]\'', | ||
'saf supplement tags write -i hdf.json -f tags.json -o new-hdf.json', | ||
'saf supplement tags write -i hdf.json -f tags.json -o new-hdf.json -c "V-000001', | ||
] | ||
|
||
static flags = { | ||
help: Flags.help({char: 'h'}), | ||
input: Flags.string({char: 'i', required: true, description: 'An input HDF or profile file'}), | ||
tagsFile: Flags.string({char: 'f', exclusive: ['tagsData'], description: 'An input tags-data file (can contain JSON that matches structure of tags in input file(HDF or profile)); this flag or `tagsData` must be provided'}), | ||
tagsData: Flags.string({char: 'd', exclusive: ['tagsFile'], description: 'Input tags-data (can contain JSON that matches structure of tags in input file(HDF or profile)); this flag or `tagsFile` must be provided'}), | ||
output: Flags.string({char: 'o', description: 'An output file that matches structure of input file (otherwise the input file is overwritten)'}), | ||
controls: Flags.string({char: 'c', description: 'The id of the control whose tags will be extracted', multiple: true}), | ||
} | ||
|
||
async run() { | ||
const {flags} = await this.parse(WriteTags) | ||
|
||
const input: ExecJSON.Execution | ProfileJSON.Profile = JSON.parse(fs.readFileSync(flags.input, 'utf8')) | ||
|
||
const output: string = flags.output || flags.input | ||
|
||
let tags: any | ||
if (flags.tagsFile) { | ||
try { | ||
tags = JSON.parse(fs.readFileSync(flags.tagsFile, 'utf8')) | ||
} catch (error: unknown) { | ||
throw new Error(`Couldn't parse tags data: ${error}`) | ||
} | ||
} else if (flags.tagsData) { | ||
try { | ||
tags = JSON.parse(flags.tagsData) | ||
} catch { | ||
tags = flags.tagsData | ||
} | ||
} else { | ||
throw new Error('One out of tagsFile or tagsData must be passed') | ||
} | ||
|
||
const overwriteTags = (profile: ExecJSON.Profile | ProfileJSON.Profile, tags: any) => { | ||
// Filter our controls | ||
const filteredControls = (profile.controls as Array<ExecJSON.Control | ProfileJSON.Control>)?.filter(control => flags.controls ? flags.controls.includes(control.id) : true) | ||
|
||
// Check shape | ||
console.log(profile.controls.length) | ||
GavMason marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
console.log(tags.length) | ||
if (!flags.controls && profile.controls.length !== tags.length) { | ||
GavMason marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
throw new TypeError('Structure of tags data is invalid') | ||
} | ||
|
||
// Overwrite tags | ||
const updatedControls = profile.controls.map((control: any, index: number) => { | ||
GavMason marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if (filteredControls.includes(control)) { | ||
return { | ||
...control, | ||
tags: tags[index], | ||
GavMason marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
return control | ||
}) | ||
return updatedControls | ||
} | ||
|
||
if (Object.hasOwn(input, 'profiles')) { | ||
for (const [i, profile] of (input as ExecJSON.Execution).profiles.entries()) { | ||
const updatedControls = overwriteTags(profile, tags[i]) | ||
|
||
profile.controls = updatedControls | ||
} | ||
} else { | ||
const updatedControls = overwriteTags((input as ProfileJSON.Profile), tags); | ||
|
||
(input as ProfileJSON.Profile).controls = updatedControls | ||
} | ||
|
||
fs.writeFileSync(output, JSON.stringify(input, null, 2)) | ||
console.log('Tags successfully overwritten') | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.