Add permission denied command run classification

This commit is contained in:
garrettmills 2020-03-03 17:03:03 -06:00
parent 6f1de65602
commit 068f2bcd66
No known key found for this signature in database
GPG Key ID: 6ACD58D6ADACFC6E
3 changed files with 27 additions and 3 deletions

View File

@ -22,7 +22,7 @@ class ExecutionResult extends Injectable {
}
get clean_err() {
return this.stdout.filter(Boolean)
return this.stderr.filter(Boolean)
}
}

View File

@ -0,0 +1,5 @@
class PermissionDeniedError extends Error {
}
module.exports = exports = PermissionDeniedError

View File

@ -1,5 +1,6 @@
const { Injectable } = require('flitter-di')
const ImplementationError = require('libflitter/errors/ImplementationError')
const PermissionDeniedError = require('../logical/error/PermissionDeniedError')
const uuid = require('uuid/v4')
const UniversalPath = require('../logical/UniversalPath')
const SystemMetrics = require('../logical/SystemMetrics')
@ -178,7 +179,8 @@ class Host extends Injectable {
async run_line_result(command) {
const result = await this.execute(command)
if ( result.exit_code !== 0 || result.clean_out.length < 1 ) {
throw new Error('Unable to get line output from command: '+command)
const E = this._get_result_error_class(result)
throw new E('Unable to get line output from command: '+command)
}
return this.utility.infer(result.clean_out[0].trim())
}
@ -186,7 +188,8 @@ class Host extends Injectable {
async run(command) {
const result = await this.execute(command)
if ( result.exit_code !== 0 ) {
throw new Error('Unable to run command: '+command)
const E = this._get_result_error_class(result)
throw new E('Unable to run command: '+command)
}
return result
}
@ -200,6 +203,22 @@ class Host extends Injectable {
async reboot() {
await this.run_line_result(this._reboot_command)
}
_get_result_error_class(result) {
const both = `${result.clean_out}\n${result.clean_err}`.toLowerCase()
const access_denied_phrases = [
'not permitted',
'access denied',
'insufficient permission',
'permission denied'
]
for ( const phrase of access_denied_phrases ) {
if ( both.includes(phrase) ) return PermissionDeniedError
}
return Error
}
}
module.exports = exports = Host