From 068f2bcd66cce231d983165bf55dd99608a5cfe6 Mon Sep 17 00:00:00 2001 From: garrettmills Date: Tue, 3 Mar 2020 17:03:03 -0600 Subject: [PATCH] Add permission denied command run classification --- app/classes/logical/ExecutionResult.js | 2 +- .../logical/error/PermissionDeniedError.js | 5 ++++ app/classes/metal/Host.js | 23 +++++++++++++++++-- 3 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 app/classes/logical/error/PermissionDeniedError.js diff --git a/app/classes/logical/ExecutionResult.js b/app/classes/logical/ExecutionResult.js index ac13aa0..5d5bfc0 100644 --- a/app/classes/logical/ExecutionResult.js +++ b/app/classes/logical/ExecutionResult.js @@ -22,7 +22,7 @@ class ExecutionResult extends Injectable { } get clean_err() { - return this.stdout.filter(Boolean) + return this.stderr.filter(Boolean) } } diff --git a/app/classes/logical/error/PermissionDeniedError.js b/app/classes/logical/error/PermissionDeniedError.js new file mode 100644 index 0000000..46e0658 --- /dev/null +++ b/app/classes/logical/error/PermissionDeniedError.js @@ -0,0 +1,5 @@ +class PermissionDeniedError extends Error { + +} + +module.exports = exports = PermissionDeniedError diff --git a/app/classes/metal/Host.js b/app/classes/metal/Host.js index 94a46dd..6a3ad16 100644 --- a/app/classes/metal/Host.js +++ b/app/classes/metal/Host.js @@ -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