Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

verify action IDs #519

Merged
merged 85 commits into from
Nov 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
85 commits
Select commit Hold shift + click to select a range
e241673
Websockets on Node Client
Aug 13, 2021
4ba7c67
Merge branch 'master' into websockets-node
JohaoRosasRosillo Oct 4, 2021
99947f6
validate same id dont run
JohaoRosasRosillo Oct 8, 2021
b9b84d9
actions run once
JohaoRosasRosillo Oct 12, 2021
aef6950
stopped action with id
JohaoRosasRosillo Oct 13, 2021
21eed02
Add id to some actions
Oct 14, 2021
9dc15b3
restore websocket files
patriciojofre Oct 14, 2021
8cbe997
delete comments and console
JohaoRosasRosillo Oct 14, 2021
bcf2cb2
test verify unique id
JohaoRosasRosillo Oct 14, 2021
1860817
Fixing triggers specs
Oct 15, 2021
42900a3
Keep same actions behavior by name
Oct 15, 2021
f1c976e
Logretrieval with id
Oct 15, 2021
619e5b5
geofencing test
JohaoRosasRosillo Oct 15, 2021
6d4287a
Fix commands storage tests
Oct 18, 2021
a839945
Recover old db fixes
Oct 18, 2021
23a1685
test geofencing
JohaoRosasRosillo Oct 19, 2021
7ecc216
Test for commands recovery
Oct 19, 2021
d214fc6
save files in new database
JohaoRosasRosillo Oct 20, 2021
bc06925
delete row file in db when finish upload
JohaoRosasRosillo Oct 20, 2021
b8d208e
test diskencryption
JohaoRosasRosillo Oct 21, 2021
484e34d
lock test with id
JohaoRosasRosillo Oct 21, 2021
3ef849b
test logretrieval
JohaoRosasRosillo Oct 21, 2021
6657998
wipe test with id
JohaoRosasRosillo Oct 21, 2021
613ba37
test actions
JohaoRosasRosillo Oct 21, 2021
67d2390
report missed fix
JohaoRosasRosillo Oct 21, 2021
a2ed355
fix test check server
JohaoRosasRosillo Oct 22, 2021
308b33f
fix report cancel
JohaoRosasRosillo Oct 22, 2021
5b39bbe
comment storage old in commands
JohaoRosasRosillo Oct 22, 2021
4862ff6
test secure
JohaoRosasRosillo Oct 25, 2021
c40378b
account reverse
JohaoRosasRosillo Oct 25, 2021
ffc741e
Hardware changes specs fix
Oct 26, 2021
ee093a5
Fix security keys and harware specs
Oct 26, 2021
3516917
hostname test
JohaoRosasRosillo Oct 26, 2021
6846092
Merge branch 'unique-id' of https://github.com/prey/prey-node-client …
JohaoRosasRosillo Oct 26, 2021
312a17f
delete console.log command storage
JohaoRosasRosillo Oct 26, 2021
581f587
fix test
JohaoRosasRosillo Oct 26, 2021
4347cb1
npm shrinkwrap
JohaoRosasRosillo Oct 27, 2021
f4e97c3
package update
JohaoRosasRosillo Oct 27, 2021
83b2bdb
Fix specs for geofencing and exceptions
Oct 27, 2021
16de8bc
Geofencing fully tested
Oct 28, 2021
8a623aa
Fix hostname storage logic
Oct 29, 2021
e5bdcd2
Remove old keys storage logic
Oct 29, 2021
91ad0ad
Fix specs for actions
Nov 2, 2021
08fd1fb
delete console.log
JohaoRosasRosillo Nov 2, 2021
21dbcbb
delete comments
JohaoRosasRosillo Nov 2, 2021
e0dd19f
completer console
JohaoRosasRosillo Nov 2, 2021
60a9d48
console log necessary
JohaoRosasRosillo Nov 3, 2021
eb27e16
Fix update storage and some triggers specs
Nov 3, 2021
5beccf0
console restored
JohaoRosasRosillo Nov 3, 2021
a614836
restore console.log
JohaoRosasRosillo Nov 3, 2021
e360c40
Merge master
Nov 3, 2021
fa0b091
Reviewing package specs
Nov 4, 2021
57e00d7
test updating spec fix
JohaoRosasRosillo Nov 4, 2021
42ef45f
Fix package tests and modify logic for commands storage deletion
Nov 8, 2021
0385ef8
fix report recovered device
JohaoRosasRosillo Nov 8, 2021
e1180d4
commands fix
JohaoRosasRosillo Nov 8, 2021
1a046c3
commands fix report target
JohaoRosasRosillo Nov 8, 2021
0a94773
Fix geofencing and storage recover
Nov 8, 2021
e6cdb6b
Fix storage for triggers
Nov 8, 2021
2fb4760
Add id to geofencing and triggers sync function
Nov 9, 2021
8025d4a
Fix action tests
Nov 9, 2021
adec480
Fixes on hardware, storage, etc
Nov 10, 2021
8d81ed4
Fileretrieval fixes and storage rename
Nov 10, 2021
87e3887
delete console test diskencryption
JohaoRosasRosillo Nov 10, 2021
4c56116
hostname fix storage
JohaoRosasRosillo Nov 10, 2021
ae3b734
File retrieval fixes, triggers and long polling tests fix
Nov 11, 2021
78f05ea
Little fixes
Nov 11, 2021
fb9bf68
Update lib/agent/actions.js
JohaoRosasRosillo Nov 11, 2021
8c49c2d
Update lib/agent/actions.js
JohaoRosasRosillo Nov 11, 2021
b645fd8
Update lib/agent/actions.js
JohaoRosasRosillo Nov 11, 2021
9d46d35
Update test/lib/agent/updating_spec.js
JohaoRosasRosillo Nov 11, 2021
b3eda51
Update lib/agent/actions/lock/index.js
JohaoRosasRosillo Nov 11, 2021
478f145
Update lib/agent/actions/lock/index.js
JohaoRosasRosillo Nov 11, 2021
5f58852
Update lib/agent/actions/logretrieval/index.js
JohaoRosasRosillo Nov 11, 2021
bbae64a
Update lib/agent/actions/triggers/index.js
JohaoRosasRosillo Nov 11, 2021
a80b98e
Update lib/agent/commands.js
JohaoRosasRosillo Nov 11, 2021
82bf909
Remove comments
Nov 11, 2021
335aa30
Merge branch 'unique-id' of https://github.com/prey/prey-node-client …
Nov 11, 2021
3dd8211
Update test/lib/agent/actions.js
JohaoRosasRosillo Nov 11, 2021
93bbce7
Update test/lib/agent/actions/triggers.js
JohaoRosasRosillo Nov 11, 2021
18cc28e
Update test/lib/agent/actions/triggers.js
JohaoRosasRosillo Nov 11, 2021
4672afc
test comment for updating_spec and triggers
JohaoRosasRosillo Nov 11, 2021
d9183fb
show error message in commands perform
JohaoRosasRosillo Nov 11, 2021
a5e5fc7
show error in sync triggers
JohaoRosasRosillo Nov 11, 2021
06608aa
Fix exception errors
Nov 11, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 42 additions & 39 deletions lib/agent/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ var load_action = function(type, name) {
}

// type can be 'action' or 'trigger'
var action_running = function(type, action, name, opts, emitter) {
logger.info('Running: ' + name)
running[name] = action;
var action_running = function(type, id, action, name, opts, emitter) {
logger.info(`Running: ${name} ${id}`);
running[id] = {name: name, action: action};

emitter.once('end', function(err, out) {
emitter.once('end', function(id, err, out) {
if (err) hooks.trigger('error', err);

logger.info('Stopped: ' + name);
action_stopped(name);
logger.info(`Stopped: ${name}`);
action_stopped(id);

setTimeout(function() {
hooks.trigger(type, 'stopped', name, opts, err, out);
hooks.trigger(type, 'stopped', id, name, opts, err, out);
}, 1000);

if (action.events)
Expand All @@ -48,25 +48,34 @@ var watch_action_events = function(events, emitter) {
})
}

var action_stopped = function(name) {
delete running[name];
var action_stopped = function(id) {
delete running[id];
}

var start = function(type, name, opts, cb){
if (running[name]) {
var err = new Error('Already running: ' + name);
var start = function(type, id, name, opts, cb){
// Action with same id is not executed
if (running[id]) {
var err = new Error(`Already running: ${running[id].name} with id: ${id}`);
hooks.emit('error', err);

return cb && cb(err);
}

if (Object.values(running).filter(x => x.name == name).length > 0) {
var err = new Error(`Already running: ${name}`);
hooks.emit('error', err);
return cb && cb(err);
}

logger.info('Starting ' + type + ': '+ name);

var action = load_action(type, name);
if (!action) return; // load_action will emit trigger

action.type = type;
action.options = typeof opts == 'function' ? {} : opts;

action.start(opts, function(err, emitter) {
action.start(id, opts, function(err, emitter) {
cb && cb(err);

if (err) {
Expand All @@ -80,11 +89,13 @@ var start = function(type, name, opts, cb){

if (!opts) opts = {};

if (type == 'action')
hooks.trigger(type, 'started', name, opts);

if (type == 'action') {
setTimeout(function() {
hooks.trigger(type, 'started', id, name, opts);
}, 500);
}
if (emitter) {
action_running(type, action, name, opts, emitter);
action_running(type, id, action, name, opts, emitter);
} else {

// no emitter was returned, so we have no way of knowing when
Expand All @@ -93,50 +104,42 @@ var start = function(type, name, opts, cb){
// trigger this event after 10 seconds.

setTimeout(function() {
hooks.trigger(type, 'stopped', name, opts);
hooks.trigger(type, 'stopped', id, name, opts);
}, 10000);

}
})

};

actions.start = function(name, opts) {
start('action', name, opts);
actions.start = function(id, name, opts, cb) {
start('action', id, name, opts, cb);
}

actions.start_trigger = function(name, opts, cb) {
start('trigger', name, opts, cb);
}
// TODO: agregar lo del opts para el response
actions.stop = function(id, name, opts) {
if (!running[id]) {
hooks.trigger('action', 'stopped', id, name, opts);
}

actions.stop = function(name, opts) {
logger.info('Stopping: ' + name);
var action = running[name];
var action = running[id].action;

if (!action) {
logger.warn('Action not running!');
hooks.trigger('action', 'stopped', name, opts);
hooks.trigger('action', 'stopped', id, name, opts);
} else if (!action.stop) {
logger.warn('Action not stoppable!');
} else {
logger.info(`Stopping: ${running[id].name}, with id: ${id}`);
action.stop();
action_stopped(id);
}
}

actions.stop_all = function() {
for (var name in running)
this.stop(name);
}

actions.running = function() {
var list = [];
for (var key in running) {
if (running[key].type == 'action') {
var obj = { name: key, options: running[key].options || {} }
list.push(obj);
}
for (var id in running) {
this.stop(id);
}
return list;
}

module.exports = actions;
10 changes: 5 additions & 5 deletions lib/agent/actions/alarm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var child,
emitter,
raise_interval;

exports.start = function(options, cb) {
exports.start = function(id, options, cb) {

var error,
self = this,
Expand All @@ -30,10 +30,10 @@ exports.start = function(options, cb) {
file = type + '.mp3',
loops = options.loops ? parseInt(options.loops) : 1;

var done = function(err) {
var done = function(id, err) {
clearInterval(raise_interval);
if (returned++) returned;
if (emitter) emitter.emit('end', err);
if (emitter) emitter.emit('end', id, err);
emitter = null;
}

Expand All @@ -45,15 +45,15 @@ exports.start = function(options, cb) {
loops--;

system.spawn_as_logged_user(commands.play, [ file ], function(err, alarm) {
if (err) return done(err);
if (err) return done(id, err);

alarm.on('error', done);

alarm.once('exit', function(code) {
child = null;

if (loops === 0)
return done();
return done(id);

play_queue();
})
Expand Down
4 changes: 2 additions & 2 deletions lib/agent/actions/alert/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var child,
binary,
user_input_str = 'User input: ';

exports.start = function(opts, cb) {
exports.start = function(id, opts, cb) {
var opts = opts || {},
message = opts.message || opts.alert_message,
title = opts.title,
Expand Down Expand Up @@ -68,7 +68,7 @@ exports.start = function(opts, cb) {
if (returned++) return;

if (emitter)
emitter.emit('end', err, reply);
emitter.emit('end', id, err, reply);

emitter = null;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/agent/actions/diskencryption/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var process_options = (options) => {
return opts;
}

exports.start = function(opts, cb) {
exports.start = function(id, opts, cb) {
var os_name = os.platform().replace('darwin', 'mac').replace('win32', 'windows');

if (os_name != 'windows')
Expand Down Expand Up @@ -85,7 +85,7 @@ exports.start = function(opts, cb) {
commands.perform({command: 'get', target: 'encryption_keys'})

output = {}
if (!out) return emitter.emit('end');
if (!out) return emitter.emit('end', id);

out.forEach(disk => {
var data = disk.disk.slice(0, -1);
Expand All @@ -99,7 +99,7 @@ exports.start = function(opts, cb) {
}

if (!emitter) return;
return emitter.emit('end', err, output);
return emitter.emit('end', id, err, output);
}

emitter = new Emitter;
Expand Down
83 changes: 68 additions & 15 deletions lib/agent/actions/fileretrieval/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var fs = require('fs'),
path = require('path'),
needle = require('needle'),
common = require('./../../common'),
files = require('./storage'),
storage = require('./../../utils/storage'),
Emitter = require('events').EventEmitter;

var system = common.system,
Expand All @@ -26,6 +26,7 @@ var config = common.config,
url = protocol + '://' + host;

var UPLOAD_SERVER = url + '/upload/upload';
const { v4: uuidv4 } = require('uuid');

var em,
cp;
Expand All @@ -36,6 +37,33 @@ var path_arg,
// check_pending_files is used to resume any files that might been pending. It's called from
// filesagent/providers/network.

var run_stored = (host, cb) => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seria bueno por temas de estandares pasar todas las variables que estan en snake_case a camelCase. Entiendo que es algo que se puede ir haciendo poco a poco.

if (host != 'solid.preyproject.com') return;

storage.do('all', { type: 'files' }, (err, files) => {
if (err) return cb(new Error('Error retrieving file from local database'))

var count = Object.keys(files).length;
if (count <= 0)
return;
logger.warn('Re-uploading ' + count + ' pending files.');

files.forEach(file => {
var opts = {
path: file.path,
user: file.user,
name: file.name,
size: file.size,
file_id: file.id,
attempts: file.attempts,
resumable: file.resumable
}
let id = uuidv4();
exports.start(id, opts, cb);
});
})
}

var retrieve_file_as_user = function(options, cb) {
if (os_name == 'windows') {
path_arg = path.resolve(options.path);
Expand All @@ -61,11 +89,13 @@ var retrieve_file_as_user = function(options, cb) {
}
logger.info("Ran as user: " + out);
if (out.indexOf("File succesfuly uploaded") != -1) {
files.del(options.file_id);
logger.debug('Removing file_id from DB: ' + options.file_id);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
logger.debug('Removing file_id from DB: ' + options.file_id);
logger.debug(`Removing file_id from DB: ${options.file_id}`);

storage.do('del', { type: 'files', id: options.file_id });
return;
}
if (out.includes("EPIPE") || out.includes("EACCES")) {
files.update(options.file_id, options.path, options.size, options.user, options.name, options.resumable, function(err) {
let resumable_value = options.resumable == 1 ? 0 : 1;
storage.do('update', { type: 'files', id: file_id, columns: 'resumable', values: resumable_value }, (err) => { // resumable
if (err) logger.error("Database update error");
logger.info("Resume file option activated for ID: " + options.file_id);
});
Expand All @@ -74,10 +104,12 @@ var retrieve_file_as_user = function(options, cb) {
}

exports.check_pending_files = function() {
files.run_stored(host);
run_stored(host, (err) => {
if (err) logger.error(err.message)
});
}

exports.start = function(options, cb) {
exports.start = function(id, options, cb) {

var url = UPLOAD_SERVER + '?uploadID=' + options.file_id;
// Make a call to get the last byte processed by the upload server
Expand All @@ -88,25 +120,44 @@ exports.start = function(options, cb) {
return;
}
if (res.statusCode == 404) {
files.del(options.file_id);
logger.debug('Removing file_id from DB: ' + options.file_id);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
logger.debug('Removing file_id from DB: ' + options.file_id);
logger.debug(`Removing file_id from DB: ${options.file_id}`);

storage.do('del', { type: 'files', id: options.file_id });
return;
}
var data = JSON.parse(res.body);
var file_status = JSON.parse(res.body).Status
options.total = data.Total;

if (file_status == 0 || file_status == 4) { // File in progress(0) or Pending(4)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Es preferible usar el === en JS, pero hay que tener cuidado si se hace este cambio.

files.exist(options.file_id, function(err, exist) {
if (!exist) {
options.resumable = false;
storage.do('query', {type: 'files', column: "id", data: options.file_id,}, (err, files) => {
if (files.length == 0) {
options.resumable = 0;
options.total = 0;
files.store(options.file_id, options.path, options.size, options.user, options.name, options.resumable);
retrieve_file_as_user(options);

var data = {
name: options.name,
path: options.path,
size: options.size,
user: options.user,
attempts: 0,
resumable: options.resumable
}

logger.debug('Storing file_id in DB: ' + options.file_id);
storage.do('set', {type: 'files', id: options.file_id, data: data}, () => {
retrieve_file_as_user(options);
});

} else {
setTimeout(function() {
if (options.resumable) {
files.update(options.file_id, options.path, options.size, options.user, options.name, options.resumable, function(err) {
var file_attempts = files[0].attempts;
if (file_attempts >= 3) {
logger.info('File ' + options.file_id + 'has no more attempts, deleting...');
return;
}

if (options.resumable == 1) {
storage.do('update', { type: 'files', id: file_id, columns: ['attempts', 'resumable'], values: [file_attempts + 1, 0] }, (err) => { // resumable
if (err) logger.error("Database update error");
logger.info("Resume file option deactivated for ID: " + options.file_id);
retrieve_file_as_user(options);
Expand All @@ -121,15 +172,17 @@ exports.start = function(options, cb) {
logger.debug("File already uploaded, deleting from db...");
else
logger.debug("File cancelled or with an error, deleting from db...");
files.del(options.file_id);

logger.info('Removing file_id from DB: ' + options.file_id);
storage.do('del', { type: 'files', id: options.file_id });
return;
}
})

em = em || new Emitter();

if (cb) cb(null, em);
em.emit('end');
em.emit('end', id);
}

exports.stop = function() {
Expand Down
Loading