Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
41 changes: 41 additions & 0 deletions api/controllers/agent-activity/host.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* POST /api/v1/agent-activity/host
*
* The agent rows of one host, for the expand toggle of the Agent Activity page
* (GET /agent-activity). The body is {hostId}. It returns {rows, total, limit}: the
* host's rows, newest first, at most ROWS_PER_HOST (1.6 bound), each as
* {id, createdAt, type, text, outcome}. `total` counts all of the host's rows,
* and `limit` is ROWS_PER_HOST. A missing, non-string or empty hostId answers
* 400. Needs agentactivity.view.
*
* The rows are the `agent.*` rows of that host id, as in
* agent-activity/summary.js. The id must not be empty: the hostless agent rows
* have the subject id '' and are not shown on this page.
*/
const ROWS_PER_HOST = 500;

module.exports = {
friendlyName: 'Agent activity of a host',
description: 'The agent rows of one host, newest first.',
fn: async function () {
const req = this.req;
const res = this.res;
if (!sails.helpers.audit.can(req, 'agentactivity.view')) {
return res.forbidden();
}
const hostId = req.body && req.body.hostId;
// A string only: an object would be read as a Waterline criteria modifier.
if (typeof hostId !== 'string' || hostId === '') {
return res.status(400).json({message: 'hostId must be a non-empty string'});
}
// Waterline normalizes criteria in place, so each query gets its own copy.
const where = () => ({type: {startsWith: 'agent.'}, subjectType: 'host', subjectId: hostId});
const total = await sails.models.auditlog.count(where());
const rows = await sails.models.auditlog.find(where()).sort([{createdAt: 'DESC'}, {id: 'DESC'}]).limit(ROWS_PER_HOST);
return res.json({
total,
limit: ROWS_PER_HOST,
rows: rows.map((r) => ({id: String(r.id), createdAt: r.createdAt, type: r.type, text: r.text, outcome: r.outcome}))
});
}
};
60 changes: 60 additions & 0 deletions api/controllers/agent-activity/summary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* POST /api/v1/agent-activity/summary
*
* One entry per host that has agent rows, for the Agent Activity page
* (GET /agent-activity): {hosts: [{hostId, hostName, count, latest}]}, the host
* with the newest row first. `latest` is the host's newest row, as
* {id, createdAt, type, text, outcome}. Needs agentactivity.view.
*
* The rows are the AuditLog rows with a type that starts with `agent.`, the
* subject type `host` and a subject id. Today that is the `agent.enroll` rows
* of an enrollment bound to a host and of a renewal. An `agent.token` row has
* the subject type `agentenrolltoken`, so it does not match.
*
* It groups in the database with a native aggregation (the Mongo driver, as
* api/helpers/image/aquire-read-lock.js does), because Waterline has no group.
* At most MAX_HOSTS hosts (1.6 bound). `hostName` is the current Host name; a
* host that is gone shows its stored label followed by " (deleted)".
*/
const MAX_HOSTS = 5000;

module.exports = {
friendlyName: 'Agent activity summary',
description: 'The agent rows of the audit trail, one entry per host.',
fn: async function () {
const req = this.req;
const res = this.res;
if (!sails.helpers.audit.can(req, 'agentactivity.view')) {
return res.forbidden();
}
const db = sails.models.auditlog.getDatastore().manager;
const groups = await db.collection(sails.models.auditlog.tableName).aggregate([
{$match: {type: {$regex: '^agent\\.'}, subjectType: 'host', subjectId: {$ne: ''}}},
// The tie-break of api/controllers/audit/datatable.js: createdAt, then id, newest first.
{$sort: {createdAt: -1, _id: -1}},
{$group: {
_id: '$subjectId',
count: {$sum: 1},
latestId: {$first: '$_id'},
latestAt: {$first: '$createdAt'},
type: {$first: '$type'},
text: {$first: '$text'},
outcome: {$first: '$outcome'},
label: {$first: '$subjectLabel'}
}},
{$sort: {latestAt: -1, latestId: -1}},
{$limit: MAX_HOSTS}
]).toArray();

const hosts = await sails.models.host.find({id: {in: groups.map((g) => g._id)}}).select(['name']);
const names = new Map(hosts.map((h) => [String(h.id), h.name]));
return res.json({
hosts: groups.map((g) => ({
hostId: g._id,
hostName: names.has(g._id) ? names.get(g._id) : `${g.label} (deleted)`,
count: g.count,
latest: {id: String(g.latestId), createdAt: g.latestAt, type: g.type, text: g.text, outcome: g.outcome}
}))
});
}
};
54 changes: 54 additions & 0 deletions api/controllers/pages/list/agent-activity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const path = require('path');
const partialPath = path.join(__dirname,'..','..','..','..','views','pages','partials','list');
// GET /agent-activity: the fog-agent enrollment events of each host, grouped by
// host, read only (1.6 AgentActivityManagement, issue #207). The rows come from
// POST /api/v1/agent-activity/summary and /host. Needs agentactivity.view.
//
// It does not call sails.helpers.listLocals, for the reason that
// pages/list/audit.js gives: the page is a view over the audit trail, not a
// CRUD model. It sets no editBase, so no row links to an edit page.
module.exports = {
friendlyName: 'Agent activity',
description: 'Agent Activity page.',
inputs: {
},
exits: {
error: {
responseType: 'serverError'
},
forbidden: {
responseType: 'forbidden'
},
success: {
viewTemplatePath: 'pages/list',
description: 'Successful'
}
},
fn: async function () {
if (!sails.helpers.audit.can(this.req, 'agentactivity.view')) {
throw 'forbidden';
}
return {
// The columns of 1.6 AgentActivityManagement.php:132-136, plus Host and
// Order. Host and Order are hidden (the partial gives them noVis).
theads: [
'When',
'Event',
'Detail',
'Outcome',
'Host',
'Order'
],
section: 'Agent Activity',
page: 'Agent Activity',
singular: 'event',
plural: 'events',
model: 'agent-activity',
// No row selection and no Delete selected, as 1.6 ($selectable = false).
selectable: false,
deleteButton: false,
partialname: path.join(partialPath, 'agent-activity.js'),
...await sails.helpers.tableSettings()
};
}
};
3 changes: 3 additions & 0 deletions api/hooks/audit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ const READ_ACTIONS = [
'audit/datatable',
// The Activity DataTable: the same reason as the Audit Log one.
'activity/datatable',
// The Agent Activity reads: the same reason as the Audit Log one.
'agent-activity/summary',
'agent-activity/host',
// Signs a JWT for the user and changes no record.
'auth/token'
];
Expand Down
3 changes: 2 additions & 1 deletion config/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ module.exports.globals = {
children: [
// Activity comes before Audit Log (1.6 FOGPage.php:658-661).
{text: 'Activity', link: '/activity', icon: 'fas fa-clock-rotate-left'},
{text: 'Audit Log', link: '/audit', icon: 'fas fa-clipboard-list'}
{text: 'Audit Log', link: '/audit', icon: 'fas fa-clipboard-list'},
{text: 'Agent Activity', link: '/agent-activity', icon: 'fas fa-satellite-dish'}
]
}
],
Expand Down
7 changes: 7 additions & 0 deletions config/permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ module.exports.permissions = {
activity: {
view: false
},
// The Agent Activity page (GET /agent-activity, POST /api/v1/agent-activity/summary
// and /host). Its own key, not audit.view: the agent rows disclose a different
// thing from the audit trail (1.6 Authorization.php:715-724). The key matches no
// model, so it opens no generic /api/v1/:model route.
agentactivity: {
view: false
},
host: {
create: false,
destroy: false,
Expand Down
4 changes: 4 additions & 0 deletions config/policies.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ module.exports.policies = {
// The Activity rows (the action checks activity.view itself). It only reads.
'activity/datatable': ['isLoggedIn','isAuthenticated'],

// The Agent Activity rows (each action checks agentactivity.view itself). They only read.
'agent-activity/summary': ['isLoggedIn','isAuthenticated'],
'agent-activity/host': ['isLoggedIn','isAuthenticated'],

// fog-agent enrollment admin routes (each action checks its host permission
// itself). apiCsrfGuard: a cookie-authed mutation on CSRF-exempt /api/v1.
'agent/enrollments': ['isLoggedIn','isAuthenticated','apiCsrfGuard'],
Expand Down
9 changes: 9 additions & 0 deletions config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ module.exports.routes = {
// it must precede POST /api/v1/:model/datatable, or it is read as :model=activity.
'POST /api/v1/activity/datatable': {action: 'activity/datatable', csrf: false},

// The Agent Activity page (GET /agent-activity): one POST read per host summary
// and one per host's rows. Both need agentactivity.view. They sit with the
// other explicit /api/v1 routes, above the :model routes, by convention.
'POST /api/v1/agent-activity/summary': {action: 'agent-activity/summary', csrf: false},
'POST /api/v1/agent-activity/host': {action: 'agent-activity/host', csrf: false},

// fog-agent enrollment admin API (host permissions; must precede the :model
// routes, or GET /api/v1/agent/tokens is read as :model=agent/:id=tokens).
'GET /api/v1/agent/enrollments': {action: 'agent/enrollments', csrf: false},
Expand Down Expand Up @@ -148,6 +154,9 @@ module.exports.routes = {
// The Activity page (1.6 ActivityManagement), read-only; it needs activity.view.
'GET /activity': {action: 'pages/list/activity'},

// The Agent Activity page (1.6 AgentActivityManagement), read-only; it needs agentactivity.view.
'GET /agent-activity': {action: 'pages/list/agent-activity'},

// Group Views
'GET /groups': {action: 'pages/list/groups'},
'GET /groups/create': {action: 'pages/create/groups'},
Expand Down
Loading
Loading