forked from NUWCDIVNPT/stigman-watcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·136 lines (124 loc) · 3.38 KB
/
index.js
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
#!/usr/bin/env node
import { logger, getSymbol } from './lib/logger.js'
import { options, configValid } from './lib/args.js'
if (!configValid) {
logger.error({ component: 'main', message: 'invalid configuration... Exiting'})
logger.end()
process.exit(1)
}
import startFsEventWatcher from './lib/events.js'
import { getOpenIDConfiguration, getToken } from './lib/auth.js'
import * as api from './lib/api.js'
import { serializeError } from 'serialize-error'
import { initScanner } from './lib/scan.js'
import semverGte from 'semver/functions/gte.js'
const minApiVersion = '1.2.7'
process.on('SIGINT', () => {
logger.info({
component: 'main',
message: 'received SIGINT, exiting'
})
process.exit(0)
})
run()
async function run() {
try {
logger.info({
component: 'main',
message: 'running',
pid: process.pid,
options: getObfuscatedConfig(options)
})
await preflightServices()
if (options.mode === 'events') {
startFsEventWatcher()
}
else if (options.mode === 'scan') {
initScanner()
}
}
catch (e) {
logError(e)
logger.end()
}
}
function logError(e) {
const errorObj = {
component: e.component || 'main',
message: e.message,
}
if (e.request) {
errorObj.request = {
method: e.request.options?.method,
url: e.request.requestUrl,
body: getSymbol(e.request, 'body')
}
}
if (e.response) {
errorObj.response = {
status: e.response.statusCode,
body: e.response.body
}
}
if (e.name !== 'RequestError' && e.name !== 'HTTPError') {
errorObj.error = serializeError(e)
}
logger.error(errorObj)
}
async function hasMinApiVersion () {
const [remoteApiVersion] = await api.getDefinition('$.info.version')
logger.info({ component: 'main', message: `preflight API version`, minApiVersion, remoteApiVersion})
if (semverGte(remoteApiVersion, minApiVersion)) {
return true
}
else {
throw new Error(`Remote API version ${remoteApiVersion} is not compatible with this release.`)
}
}
async function preflightServices () {
await hasMinApiVersion()
await getOpenIDConfiguration()
await getToken()
logger.info({ component: 'main', message: `preflight token request suceeded`})
const promises = [
api.getCollection(options.collectionId),
api.getCollectionAssets(options.collectionId),
api.getInstalledStigs(),
api.getScapBenchmarkMap()
]
await Promise.all(promises)
setInterval(refreshCollection, 10 * 60000)
// OAuth scope 'stig-manager:user:read' was not required for early versions of Watcher
// For now, fail gracefully if we are blocked from calling /user
try {
await api.getUser()
setInterval(refreshUser, 10 * 60000)
}
catch (e) {
logger.warn({ component: 'main', message: `preflight user request failed; token may be missing scope 'stig-manager:user:read'? Watcher will not set {"status": "accepted"}`})
}
logger.info({ component: 'main', message: `prefilght api requests suceeded`})
}
function getObfuscatedConfig (options) {
const securedConfig = {...options}
if (securedConfig.clientSecret) {
securedConfig.clientSecret = '[hidden]'
}
return securedConfig
}
async function refreshUser() {
try {
await api.getUser()
}
catch (e) {
logError(e)
}
}
async function refreshCollection() {
try {
await api.getCollection(options.collectionId)
}
catch (e) {
logError(e)
}
}