Skip to content

Commit

Permalink
Merge pull request #485 from prey/keys-schedule
Browse files Browse the repository at this point in the history
Encryption keys schedule and big sur error capture
  • Loading branch information
javo authored Dec 20, 2020
2 parents c4f1637 + b96f2c7 commit fb6120e
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Bitlocker integration[\#481](https://github.com/prey/prey-node-client/pull/481) ([javo](https://github.com/javo))
- Location schedule and dependency update [\#482](https://github.com/prey/prey-node-client/pull/482) ([javo](https://github.com/javo))
- Special characters on lock fix [\#483](https://github.com/prey/prey-node-client/pull/483) ([javo](https://github.com/javo))
- Encryption keys schedule and big sur error capture [\#485](https://github.com/prey/prey-node-client/pull/485) ([javo](https://github.com/javo))

## [v1.9.6](https://github.com/prey/prey-node-client/tree/v1.9.6) (2020-10-07)
[Full Changelog](https://github.com/prey/prey-node-client/compare/v1.9.5...v1.9.6)
Expand Down
3 changes: 3 additions & 0 deletions lib/agent/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ function trap_signals() {
logger.critical('UNCAUGHT EXCEPTION: ' + (err.message || err));
logger.debug(err.stack);

// Capture MacOS Big Sur ENETDOWN error for now...
if (err.message == "read ENETDOWN" || err == "read ENETDOWN") return;

if (!common.config.get('send_crash_reports'))
return shutdown(1, 5000);

Expand Down
31 changes: 28 additions & 3 deletions lib/agent/providers/encryption-keys/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,40 @@
// GPLv3 Licensed
////////////////////////////////////////////

var common = require('./../../common'),
logger = common.logger.prefix('encryption'),
system = common.system;
var common = require('./../../common'),
commands = require('./../../commands'),
logger = common.logger.prefix('encryption'),
system = common.system,
processing = false;

exports.scheduled = false;
exports.timeout = 2 * 60 * 60 * 1000; // Every 2 hours

module.exports.get_encryption_keys = function(cb) {
logger.info("Getting encryption keys");
system.get_as_admin_user('recoveryKeys', (err, info) => {
if (err) return cb(err);

// Schedule another keys fetch if there's at least one disk encrypted.
processing = false;
info.forEach((disk) => {
if (disk.diskStatus == "encrypted" || disk.diskStatus == "locked") {
processing = true;
}
})

if (processing) {
if (!exports.scheduled) {
exports.scheduled = true;
setTimeout(() => {
exports.scheduled = false;
commands.perform({command: 'get', target: 'encryption_keys'})
}, exports.timeout)
}
} else {
processing = false;
}

return cb(null, JSON.stringify(info));
})
}
104 changes: 104 additions & 0 deletions test/lib/agent/providers/encryption-keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
var helpers = require('./../../../helpers'),
should = require('should'),
sinon = require('sinon'),
join = require('path').join,
needle = require('needle'),
commands = helpers.load('commands')
sys_index_path = helpers.lib_path('system'),
sys_index = require(sys_index_path),
sys_win = require(join(sys_index_path, 'windows')),
provider_keys = helpers.load('providers/encryption-keys');

describe('Encryption keys', () => {

before(() => {
sys_index.os_name = "windows"
sys_index.check_service = sys_win.check_service;
sys_index.run_as_admin = sys_win.run_as_admin;
sys_index.get_as_admin = sys_win.get_as_admin;
})

describe('when service not available', () => {
before(() => {
sys_win.monitoring_service_go = false;
check_service_stub = sinon.stub(needle, 'get').callsFake((url, cb) => {
return cb(new Error("BUH!"));
});
});

after(() => {
check_service_stub.restore();
})

it('returns error', (done) => {
console.log("PROVIDER2!", provider_keys)
provider_keys.get_encryption_keys((err, obj) => {
should.exist(err);
err.message.should.containEql('Admin service not available');
done();
});
});
})

describe('get keys as admin', () => {
var keys_stub;

before(() => {
sys_win.monitoring_service_go = true;
provider_keys.timeout = 0;
commands_stub = sinon.stub(commands, 'perform').callsFake(() => {
return;
});
})

after(() => {
commands_stub.restore();
})

describe('when one or more disks are being encrypted', () => {
var keys = '{"err": null, "output": [{"mountPoint":"C:","encryptionKey":"","securityType":"","encryptionPassword":"","diskStatus":""}, {"mountPoint":"D:","encryptionKey":"D40F686D-D402-41D9-BB02-9CCB0AB6AD33","securityType":"RecoveryPassword","encryptionPassword":"130031-238238-080982-333795-366278-161326-517352-139458","diskStatus":"encrypted"}]}'

before(() => {
keys_stub = sinon.stub(needle, 'post').callsFake((url, data, opts, cb) => {
cb(null, null, keys)
});
})

after(() => {
keys_stub.restore();
})

it('schedules another keys check', (done) => {
provider_keys.get_encryption_keys((err, obj) => {
should.not.exist(err);
provider_keys.scheduled.should.be.equal(true);
done();
});
})
})

describe('when all disks are all decrypted', () => {
var keys2 = '{"err": null, "output": [{"mountPoint":"C:","encryptionKey":"","securityType":"","encryptionPassword":"","diskStatus":""}, {"mountPoint":"D:","encryptionKey":"","securityType":"","encryptionPassword":"","diskStatus":""}]}'

before(() => {
keys_stub = sinon.stub(needle, 'post').callsFake((url, data, opts, cb) => {
cb(null, null, keys2)
});
})

after(() => {
keys_stub.restore();
})

it('does not schedules another keys fetch', (done) => {
provider_keys.get_encryption_keys(function(err, obj) {
should.not.exist(err);
provider_keys.scheduled.should.be.equal(false);
done();
});
})
})
})
})


0 comments on commit fb6120e

Please sign in to comment.