Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/audit'
Browse files Browse the repository at this point in the history
  • Loading branch information
mathcoll committed Aug 15, 2023
2 parents e673338 + 472a09b commit 2522f8e
Show file tree
Hide file tree
Showing 16 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ global.annotate = function(user_id, from_ts, to_ts, flow_id, category_id, rule_i
};
let eName = rule_id!==null?"Annotation added from Rule":"Annotation added from custom";
t6events.addStat("t6Api", eName, newAnnotation.id, user_id, {"user_id": user_id, annotation_id: newAnnotation.id, flow_id: newAnnotation.flow_id, category_id: newAnnotation.category_id, rule_id: rule_id});
t6events.addAudit("t6Api", eName, user_id, newAnnotation.id, {error_id: null, status: 200});
annotations.insert(newAnnotation);
return newAnnotation;
};
Expand Down
8 changes: 7 additions & 1 deletion routes/audits.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ router.get("/:audit_id([0-9a-z\-]+)?", expressJwt({secret: jwtsettings.secret, a
let status = req.query.status;
let what = req.query.what;
let result = req.query.result;
let user_id;
let start;
let end;
if ( req.user.role === "admin" ) {
user_id = typeof req.query.user_id!=="undefined"?req.query.user_id:req.user.id;
} else {
user_id = req.user.id;
}

let where = "";
if ( audit_id ) {
Expand Down Expand Up @@ -97,7 +103,7 @@ router.get("/:audit_id([0-9a-z\-]+)?", expressJwt({secret: jwtsettings.secret, a
} else if (limit < 1) {
limit = 1;
}
let query = `SELECT * from events WHERE who='${req.user.id}' ${where} ORDER BY time ${sorting} LIMIT ${limit} OFFSET ${(page-1)*limit}`;
let query = `SELECT * from ${t6events.getRP()}.events WHERE who='${user_id}' ${where} ORDER BY time ${sorting} LIMIT ${limit} OFFSET ${(page-1)*limit}`;
t6console.debug("Query:", query);
dbInfluxDB.query(query).then((data) => {
if ( data.length > 0 ) {
Expand Down
1 change: 1 addition & 0 deletions routes/classifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ router.post("/categories/?", expressJwt({secret: jwtsettings.secret, algorithms:
description:typeof req.body.description!=="undefined"?(req.body.description).substring(0, 1024):"",
};
t6events.addStat("t6Api", "category add", newCategory.id, req.user.id);
t6events.addAudit("t6Api", "category add", req.user.id, newCategory.id, {error_id: null, status: 201});
categories.insert(newCategory);

res.header("Location", "/v"+version+"/categories/"+newCategory.id);
Expand Down
1 change: 1 addition & 0 deletions routes/dashboards.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ router.post("/", expressJwt({secret: jwtsettings.secret, algorithms: jwtsettings
snippets: typeof req.body.snippets!=="undefined"?req.body.snippets:new Array(),
};
t6events.addStat("t6Api", "dashboard add", new_dashboard.id, req.user.id);
t6events.addAudit("t6Api", "dashboard add", req.user.id, new_dashboard.id, {error_id: null, status: 201});
dashboards.insert(new_dashboard);
//t6console.log(dashboards);

Expand Down
2 changes: 2 additions & 0 deletions routes/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -1313,6 +1313,8 @@ router.post("/(:flow_id([0-9a-z\-]+))?", expressJwt({secret: jwtsettings.secret,
payload.limit = 20;
payload.sort = "asc";
res.status(200).send(new DataSerializer(payload).serialize());
t6events.addStat("t6Api", "datapoint add", payload.flow_id, req.user.id);
t6events.addAudit("t6Api", "datapoint add", req.user.id, payload.flow_id, {error_id: null, status: 201});
t6console.debug(`processAllMeasures Completed with ${payload.length} measurement(s)`);
}).catch((err) => {
t6console.warning("Error on processAllMeasures: ", err);
Expand Down
1 change: 1 addition & 0 deletions routes/flows.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ router.post("/", expressJwt({secret: jwtsettings.secret, algorithms: jwtsettings
influx_db_cloud: typeof req.body.influx_db_cloud!=="undefined"?req.body.influx_db_cloud:"",
};
t6events.addStat("t6Api", "flow add", newFlow.id, req.user.id);
t6events.addAudit("t6Api", "flow add", req.user.id, newFlow.id, {error_id: null, status: 201});
flows.insert(newFlow);

res.header("Location", "/v"+version+"/flows/"+newFlow.id);
Expand Down
1 change: 1 addition & 0 deletions routes/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ router.post("/?", expressJwt({secret: jwtsettings.secret, algorithms: jwtsetting
}
};
t6events.addStat("t6Api", "model add", newModel.id, req.user.id);
t6events.addAudit("t6Api", "model add", req.user.id, newModel.id, {error_id: null, status: 201});
models.insert(newModel);

res.header("Location", "/v"+version+"/models/"+newModel.id);
Expand Down
4 changes: 4 additions & 0 deletions routes/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ router.post("/(:object_id([0-9a-z\-]+))/unlink/(:source_id([0-9a-z\-]+))", expre
if ( typeof result!=="undefined" ) {
db_objects.save();
res.header("Location", "/v"+version+"/objects/"+object_id);
t6events.addAudit("t6App", "object unlink", req.user.id, object_id, {error_id: null, status: 200});
res.status(200).send({ "code": 200, message: "Successfully updated", object: new ObjectSerializer(result).serialize() });
} else {
res.status(412).send(new ErrorSerializer({"id": 185, "code": 412, "message": "Not Found"}).serialize());
Expand Down Expand Up @@ -435,6 +436,7 @@ router.post("/:object_id/build/?:version([0-9]+)?", expressJwt({secret: jwtsetti
}
}
t6otahistory.addEvent(req.user.id, object.id, {fqbn: object.fqbn, ip: object.ipv4}, object.source_id, object.source_version, "build", "success", new Date()-start);
t6events.addAudit("t6App", "object build", req.user.id, object.id, {error_id: null, status: 201});
} else {
if (user && user.pushSubscription!==null && typeof user.pushSubscription!=="undefined" && user.pushSubscription.endpoint!==null && user.pushSubscription.endpoint!=="") {
t6console.debug(user.pushSubscription);
Expand All @@ -451,6 +453,7 @@ router.post("/:object_id/build/?:version([0-9]+)?", expressJwt({secret: jwtsetti
}
}
t6otahistory.addEvent(req.user.id, object.id, {fqbn: object.fqbn, ip: object.ipv4}, object.source_id, object.source_version, "build", "failure", new Date()-start);
t6events.addAudit("t6App", "object build error", req.user.id, object.id, {error_id: null, status: 201});
}
});
});
Expand Down Expand Up @@ -532,6 +535,7 @@ router.post("/", expressJwt({secret: jwtsettings.secret, algorithms: jwtsettings
});
}
t6events.addStat("t6Api", "object add", newObject.id, req.user.id);
t6events.addAudit("t6Api", "object add", req.user.id, newObject.id, {error_id: null, status: 201});
objects.insert(newObject);
//t6console.log(newObject);

Expand Down
1 change: 1 addition & 0 deletions routes/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ router.post("/", expressJwt({secret: jwtsettings.secret, algorithms: jwtsettings
rule: typeof req.body.rule!=="undefined"?req.body.rule:{},
};
t6events.addStat("t6Api", "rule add", newRule.id, req.user.id);
t6events.addAudit("t6Api", "rule add", req.user.id, newRule.id, {error_id: null, status: 201});
rules.insert(newRule);
//t6console.log(rules);

Expand Down
1 change: 1 addition & 0 deletions routes/snippets.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ router.post("/", expressJwt({secret: jwtsettings.secret, algorithms: jwtsettings
options: typeof req.body.options!=="undefined"?req.body.options:undefined,
};
t6events.addStat("t6Api", "snippet add", newSnippet.id, req.user.id);
t6events.addAudit("t6Api", "snippet add", req.user.id, newSnippet.id, {error_id: null, status: 201});
snippets.insert(newSnippet);
//t6console.log(snippets);

Expand Down
1 change: 1 addition & 0 deletions routes/sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ router.post("/", expressJwt({secret: jwtsettings.secret, algorithms: jwtsettings
password: typeof req.body.password!=="undefined"?req.body.password:"",
};
t6events.addStat("t6Api", "source add", newSource.id, req.user.id);
t6events.addAudit("t6Api", "source add", req.user.id, newSource.id, {error_id: null, status: 201});
sources.insert(newSource);
//t6console.log(sources);

Expand Down
1 change: 1 addition & 0 deletions routes/stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ router.post("/", expressJwt({secret: jwtsettings.secret, algorithms: jwtsettings
retention : req.body.retention
};
t6events.addStat("t6Api", "story add", newStory.id, req.user.id);
t6events.addAudit("t6Api", "story add", req.user.id, newStory.id, {error_id: null, status: 201});
stories.insert(newStory);

res.header("Location", "/v"+version+"/stories/"+newStory.id);
Expand Down
1 change: 1 addition & 0 deletions routes/uis.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ router.post("/", expressJwt({secret: jwtsettings.secret, algorithms: jwtsettings
newUi.id = uuid.v4();
newUi.user_id = req.user.id;
t6events.addStat("t6Api", "ui add", newUi.id, req.user.id);
t6events.addAudit("t6Api", "ui add", req.user.id, newUi.id, {error_id: null, status: 201});
uis.insert(newUi);
res.header("Location", "/v"+version+"/uis/"+newUi.id);
res.status(201).send({ "code": 201, message: "Created", ui: new UISerializer(newUi).serialize() });
Expand Down
1 change: 1 addition & 0 deletions routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ router.post("/", function (req, res) {
geoip : {ip: [req.ip]},
};
t6events.addStat("t6Api", "user add", new_user.id, new_user.id);
t6events.addAudit("t6Api", "user add", new_user.id, new_user.id, {error_id: null, status: 201});
users.insert(new_user);

tokens.insert(new_token);
Expand Down
2 changes: 1 addition & 1 deletion serializers/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function FlowSerializer(flow) {
this.serialize = function() {
return new JSONAPISerializer("flow", {
keyForAttribute: "underscore_case",
attributes : [ "name", "unit", "objects", "require_signed", "require_encrypted", "permission", "data_type", "mqtt_topic", "track_id", "fusion_algorithm", "ttl", "preprocessor", "influx_db_cloud", "retention", "meta" ],
attributes : [ "name", "unit", "objects", "require_signed", "require_encrypted", "permission", "data_type", "mqtt_topic", "track_id", "fusion_algorithm", "ttl", "preprocessor", "influx_db_cloud", "retention", "user_id", "meta" ],
topLevelLinks : {
parent : sprintf("%s/v%s/flows", baseUrl_https, version),
first : flow.pageFirst!==undefined?sprintf("%s/v%s/flows/?page=%s&size=%s", baseUrl_https, version, flow.pageFirst, flow.size):undefined,
Expand Down
7 changes: 6 additions & 1 deletion t6events.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@ t6events.setRP = function(rp) {
retention = rp;
};

t6events.getRP = function() {
return retention;
};

t6events.addAudit = function(where, what, who, client_id=null, params=null) {
where = where + ":" + process.env.NODE_ENV;
params.error_id = (typeof params.error_id!=="undefined" && params.error_id!==null)?params.error_id.toString():"";
//TODO : make sure 'what' does not contains multiple lines
if ( db_type.influxdb ) {
var tags = {rp: retention, what: what.replace(/(\r\n|\n|\r)/gm, ""), where: where};
var fields = {who: typeof who!=="undefined"?who:"", status: parseFloat((params!==null && typeof params.status!=="undefined")?params.status:""), error_id: ((params!==null && typeof params.error_id!=="undefined")?params.error_id:"").toString()};
var fields = {who: typeof who!=="undefined"?who:"", status: parseFloat((params!==null && typeof params.status!=="undefined")?params.status:""), error_id: params.error_id};
let dbWrite = typeof dbTelegraf!=="undefined"?dbTelegraf:dbInfluxDB;
dbWrite.writePoints([{
measurement: measurement,
Expand Down

0 comments on commit 2522f8e

Please sign in to comment.