Skip to content

Commit

Permalink
first pass adding bounds checks to config options
Browse files Browse the repository at this point in the history
  • Loading branch information
cd-rite committed Jun 10, 2024
1 parent ed5f8c3 commit bdc67e0
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 20 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env node
import { logger, getSymbol } from './lib/logger.js'
import { options, configValid } from './lib/args.js'
const minApiVersion = '1.2.7'
const component = 'index'
if (!configValid) {
logger.error({ component, message: 'invalid configuration... Exiting'})
logger.end()
Expand All @@ -15,8 +17,7 @@ import semverGte from 'semver/functions/gte.js'
import Alarm from './lib/alarm.js'
import * as CONSTANTS from './lib/consts.js'

const minApiVersion = '1.2.7'
const component = 'index'


process.on('SIGINT', () => {
logger.info({
Expand Down
52 changes: 35 additions & 17 deletions lib/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { config } from 'dotenv'
import { dirname, resolve, sep, posix } from 'node:path'
import promptSync from 'prompt-sync'
import { createPrivateKey } from 'crypto'
import * as CONSTANTS from './consts.js'

const prompt = promptSync({ sigint:true })
const component = 'args'
Expand Down Expand Up @@ -118,6 +119,38 @@ program
// Options properties are created as camelCase versions of the long option name
program.parse(process.argv)
const options = program.opts()
// add semver info
options.version = version

if (options.debug) {
options.logLevel = 'debug'
options.logFileLevel = 'debug'
}

// Start logging
logger.addConsoleTransport( options.logLevel, options.logColor, options.silent )
if (options.logFile) {
logger.addFileTransport( options.logFileLevel, options.logFile )
}

logger.logger.log({
level: 'debug',
component: component,
message: 'parsed options',
options: options
})

for (const key in CONSTANTS.configBounds) {
const bounds = CONSTANTS.configBounds[key]
if (options[key] < bounds.min || options[key] > bounds.max) {
logger.logger.log({
level: 'error',
component: component,
message: `Config value out of bounds: ${key}=${options[key]}`
})
configValid = false
}
}

// deprecate ignoreDir
if (options.ignoreDir) {
Expand All @@ -130,8 +163,7 @@ if (options.ignoreDir) {
}
}

// add semver info
options.version = version


// Set path variations
options._originalPath = options.path
Expand All @@ -142,16 +174,8 @@ options.path = options.path.split(sep).join(posix.sep)
if (options.oneShot) {
options.addExisting = true
}
if (options.debug) {
options.logLevel = 'debug'
options.logFileLevel = 'debug'
}

// Start logging
logger.addConsoleTransport( options.logLevel, options.logColor, options.silent )
if (options.logFile) {
logger.addFileTransport( options.logFileLevel, options.logFile )
}


// Validate we can perform the requested client authentication
if (options.clientKey) {
Expand Down Expand Up @@ -187,12 +211,6 @@ else {
}
}

logger.logger.log({
level: 'debug',
component: component,
message: 'parsed options',
options: options
})

function getPrivateKey( pemFile, passphrase, canPrompt) {
let pemKey
Expand Down
51 changes: 50 additions & 1 deletion lib/consts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,53 @@ export const ERR_AUTHOFFLINE = 2
export const ERR_NOTOKEN = 3
export const ERR_NOGRANT = 4
export const ERR_UNKNOWN = 5
export const ERR_FAILINIT = 6
export const ERR_FAILINIT = 6


// Minimum and maximum values for Watcher configuration
const WATCHER_SCAN_INTERVAL_MIN = 60000 // 60 seconds - Should be greater than WATCHER_CARGO_DELAY
const WATCHER_SCAN_INTERVAL_MAX = 24 * 60 * 60000 // 24 hours

const WATCHER_CARGO_SIZE_MIN = 1
const WATCHER_CARGO_SIZE_MAX = 100
const WATCHER_CARGO_DELAY_MIN = 2000 // 2 seconds
const WATCHER_CARGO_DELAY_MAX = 30000 // 30 seconds

const WATCHER_HISTORY_WRITE_INTERVAL_MIN = 10000 // 10 seconds
const WATCHER_HISTORY_WRITE_INTERVAL_MAX = 60000 // 60 seconds

const WATCHER_RESPONSE_TIMEOUT_MIN = 5000 // 5 seconds
const WATCHER_RESPONSE_TIMEOUT_MAX = 60000 // 60 seconds

const WATCHER_STABILITY_THRESHOLD_MAX = 10000 // 10 seconds


export const configBounds = {
scanInterval: {
min: WATCHER_SCAN_INTERVAL_MIN,
max: WATCHER_SCAN_INTERVAL_MAX
},
cargoDelay: {
min: WATCHER_CARGO_DELAY_MIN,
max: WATCHER_CARGO_DELAY_MAX
},
cargoSize: {
min: WATCHER_CARGO_SIZE_MIN,
max: WATCHER_CARGO_SIZE_MAX
},
historyWriteInterval: {
min: WATCHER_HISTORY_WRITE_INTERVAL_MIN,
max: WATCHER_HISTORY_WRITE_INTERVAL_MAX
},
responseTimeout: {
min: WATCHER_RESPONSE_TIMEOUT_MIN,
max: WATCHER_RESPONSE_TIMEOUT_MAX
},
stabilityThreshold: {
min: 0,
max: WATCHER_STABILITY_THRESHOLD_MAX
},
}



0 comments on commit bdc67e0

Please sign in to comment.