forked from guacsec/trustify-da-javascript-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
182 lines (174 loc) · 4.72 KB
/
Copy pathcli.js
File metadata and controls
182 lines (174 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env node
import * as path from "path";
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import client from './index.js'
// command for component analysis take manifest type and content
const component = {
command: 'component </path/to/manifest>',
desc: 'produce component report for manifest path',
builder: yargs => yargs.positional(
'/path/to/manifest',
{
desc: 'manifest path for analyzing',
type: 'string',
normalize: true,
}
),
handler: async args => {
let manifestName = args['/path/to/manifest']
let res = await client.componentAnalysis(manifestName)
console.log(JSON.stringify(res, null, 2))
}
}
const validateToken = {
command: 'validate-token <token-provider> [--token-value thevalue]',
desc: 'Validates input token if authentic and authorized',
builder: yargs => yargs.positional(
'token-provider',
{
desc: 'the token provider',
type: 'string',
choices: ['snyk','oss-index'],
}
).options({
tokenValue: {
alias: 'value',
desc: 'the actual token value to be checked',
type: 'string',
}
}),
handler: async args => {
let tokenProvider = args['token-provider'].toUpperCase()
let opts={}
if(args['tokenValue'] !== undefined && args['tokenValue'].trim() !=="" ) {
let tokenValue = args['tokenValue'].trim()
opts[`TRUSTIFY_DA_${tokenProvider}_TOKEN`] = tokenValue
}
let res = await client.validateToken(opts)
console.log(res)
}
}
// command for image analysis takes OCI image references
const image = {
command: 'image <image-refs..>',
desc: 'produce image analysis report for OCI image references',
builder: yargs => yargs.positional(
'image-refs',
{
desc: 'OCI image references to analyze (one or more)',
type: 'string',
array: true,
}
).options({
html: {
alias: 'r',
desc: 'Get the report as HTML instead of JSON',
type: 'boolean',
conflicts: 'summary'
},
summary: {
alias: 's',
desc: 'For JSON report, get only the \'summary\'',
type: 'boolean',
conflicts: 'html'
}
}),
handler: async args => {
let imageRefs = args['image-refs']
if (!Array.isArray(imageRefs)) {
imageRefs = [imageRefs]
}
let html = args['html']
let summary = args['summary']
let res = await client.imageAnalysis(imageRefs, html)
if(summary && !html) {
let summaries = {}
for (let [imageRef, report] of Object.entries(res)) {
for (let provider in report.providers) {
if (report.providers[provider].sources !== undefined) {
for (let source in report.providers[provider].sources) {
if (report.providers[provider].sources[source].summary) {
if (!summaries[imageRef]) {
summaries[imageRef] = {};
}
if (!summaries[imageRef][provider]) {
summaries[imageRef][provider] = {};
}
summaries[imageRef][provider][source] = report.providers[provider].sources[source].summary
}
}
}
}
}
res = summaries
}
console.log(html ? res : JSON.stringify(res, null, 2))
}
}
// command for stack analysis takes a manifest path
const stack = {
command: 'stack </path/to/manifest> [--html|--summary]',
desc: 'produce stack report for manifest path',
builder: yargs => yargs.positional(
'/path/to/manifest',
{
desc: 'manifest path for analyzing',
type: 'string',
normalize: true,
}
).options({
html: {
alias: 'r',
desc: 'Get the report as HTML instead of JSON',
type: 'boolean',
conflicts: 'summary'
},
summary: {
alias: 's',
desc: 'For JSON report, get only the \'summary\'',
type: 'boolean',
conflicts: 'html'
}
}),
handler: async args => {
let manifest = args['/path/to/manifest']
let html = args['html']
let summary = args['summary']
let theProvidersSummary = new Map();
let theProvidersObject ={}
let res = await client.stackAnalysis(manifest, html)
if(summary)
{
for (let provider in res.providers ) {
if (res.providers[provider].sources !== undefined) {
for(let source in res.providers[provider].sources ) {
if(res.providers[provider].sources[source].summary) {
theProvidersSummary.set(source,res.providers[provider].sources[source].summary)
}
}
}
}
for (let [provider, providerSummary] of theProvidersSummary) {
theProvidersObject[provider]=providerSummary
}
}
console.log(html ? res : JSON.stringify(
!html && summary ? theProvidersObject : res,
null,
2
))
}
}
// parse and invoke the command
yargs(hideBin(process.argv))
.usage(`Usage: ${process.argv[0].includes("node") ? path.parse(process.argv[1]).base : path.parse(process.argv[0]).base} {component|stack|image|validate-token}`)
.command(stack)
.command(component)
.command(image)
.command(validateToken)
.scriptName('')
.version(false)
.demandCommand(1)
.wrap(null)
.parse()