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
44 changes: 44 additions & 0 deletions api/controllers/activity/datatable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* POST /api/v1/activity/datatable
*
* The rows of the Activity page (GET /activity), as DataTables server-side rows
* {draw, recordsTotal, recordsFiltered, data}. Needs activity.view.
*
* Activity is a view over the audit trail (1.6 ADR 0023). A row shows when the
* write succeeded and named its record (an access.api or access.page row with a
* subjectType), or when the event marks itself renderable (the agent events).
* Denials, failures, sign-ins and sign-outs stay on the Audit Log page only.
*
* The paging, the 500-row bound, the search and the order are in
* api/helpers/audit/datatable.js. Each row carries a server-built `summary`
* (api/helpers/audit/summary.js).
*/
const SEARCH_FIELDS = ['createdBy', 'subjectType', 'subjectLabel', 'ip'];
const ROW_FIELDS = ['id', 'createdAt', 'createdBy', 'ip', 'text'];
// The 1.6 "Administrative actions" rule, in Waterline.
const RULE = {
outcome: 'allowed',
or: [
{renderable: true},
{type: {in: ['access.api', 'access.page']}, subjectType: {'!=': ''}}
]
};

module.exports = {
friendlyName: 'Activity datatable',
description: 'The Activity feed, as DataTables server-side rows.',
fn: async function () {
const req = this.req;
const res = this.res;
if (!sails.helpers.audit.can(req, 'activity.view')) {
return res.forbidden();
}
return res.json(await sails.helpers.audit.datatable.with({
req,
where: RULE,
searchFields: SEARCH_FIELDS,
fields: ROW_FIELDS,
extra: (row) => ({summary: sails.helpers.audit.summary.with({row})})
}));
}
};
38 changes: 3 additions & 35 deletions api/controllers/audit/datatable.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,9 @@
* POST /api/v1/audit/datatable
*
* The rows of the Audit Log page (GET /audit), as DataTables server-side rows
* {draw, recordsTotal, recordsFiltered, data}. Needs audit.view.
*
* The page size is bounded in the query, as 1.6 AuditManagement.php does: the
* trail grows for the life of the install, so no request reads it whole. A
* `length` that is missing, -1, not a number, or above MAX_ROWS reads MAX_ROWS
* rows.
*
* Search: a case-insensitive substring match on createdBy, type, permission,
* outcome, subjectType, subjectLabel and ip. It runs in the database query.
* Without makeLikeModifierCaseInsensitive, sails-mongo matches case-sensitively.
*
* Order: createdAt DESC, then id DESC as a tie-break, so paging never skips or
* repeats rows that share a millisecond. The client sets ordering: false.
* {draw, recordsTotal, recordsFiltered, data}. Needs audit.view. The paging, the
* 500-row bound, the search and the order are in api/helpers/audit/datatable.js.
*/
const MAX_ROWS = 500;
const SEARCH_FIELDS = ['createdBy', 'type', 'permission', 'outcome', 'subjectType', 'subjectLabel', 'ip'];
const ROW_FIELDS = [
'id', 'createdAt', 'createdBy', 'ip', 'authSource', 'type', 'subjectType', 'subjectId',
Expand All @@ -32,26 +20,6 @@ module.exports = {
if (!sails.helpers.audit.can(req, 'audit.view')) {
return res.forbidden();
}
// Safe for any JSON value (api/helpers/datatables/parse-request.js).
const parsed = sails.helpers.datatables.parseRequest.with({body: req.body});
const start = parsed.start;
const text = parsed.search;
// A length that is missing, -1, not a number, or above MAX_ROWS reads MAX_ROWS.
const length = parsed.length > 0 && parsed.length <= MAX_ROWS ? parsed.length : MAX_ROWS;

const where = text === '' ? {} : {or: SEARCH_FIELDS.map((field) => ({[field]: {contains: text}}))};
const recordsTotal = await sails.models.auditlog.count();
const recordsFiltered = text === '' ? recordsTotal : await sails.models.auditlog.count(where).meta({makeLikeModifierCaseInsensitive: true});
const rows = await sails.models.auditlog.find(where)
.meta({makeLikeModifierCaseInsensitive: true})
.sort([{createdAt: 'DESC'}, {id: 'DESC'}])
.skip(start)
.limit(length);
return res.json({
draw: parsed.draw,
recordsTotal,
recordsFiltered,
data: rows.map((row) => _.pick(row, ROW_FIELDS))
});
return res.json(await sails.helpers.audit.datatable.with({req, where: {}, searchFields: SEARCH_FIELDS, fields: ROW_FIELDS}));
}
};
51 changes: 51 additions & 0 deletions api/controllers/pages/list/activity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const path = require('path');
const partialPath = path.join(__dirname,'..','..','..','..','views','pages','partials','list');
// GET /activity: what has been done on the server, in plain words, read only
// (1.6 ActivityManagement, issue #206). The rows come from
// POST /api/v1/activity/datatable. Needs activity.view.
//
// It does not call sails.helpers.listLocals, for the reason that
// pages/list/audit.js gives: the activity feed 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: 'Activity',
description: '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, 'activity.view')) {
throw 'forbidden';
}
return {
// The columns of 1.6 ActivityManagement.php:199-204, in order.
theads: [
'Who',
'When',
'What',
'From'
],
section: 'Activity',
page: 'Activity',
singular: 'entry',
plural: 'entries',
model: 'activity',
// No row selection and no Delete selected, as 1.6 ($selectable = false).
selectable: false,
deleteButton: false,
partialname: path.join(partialPath, 'activity.js'),
...await sails.helpers.tableSettings()
};
}
};
72 changes: 72 additions & 0 deletions api/helpers/audit/datatable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* The DataTables server-side rows of an AuditLog view: the Audit Log page
* (audit/datatable.js) and the Activity page (activity/datatable.js). The caller
* checks its own permission first, then calls this helper.
*
* The page size is bounded in the query, as 1.6 AuditManagement.php does: the
* trail grows for the life of the install, so no request reads it whole. A
* `length` that is missing, -1, not a number, or above MAX_ROWS reads MAX_ROWS
* rows.
*
* Search: a case-insensitive substring match on the searchFields. It runs in the
* database query. Without makeLikeModifierCaseInsensitive, sails-mongo matches
* case-sensitively. With a search, the query is {and: [where, {or: search}]}; a
* `where` of {} is left out, so the query is {or: search}.
*
* recordsTotal counts the rows that match `where`, not the whole trail.
*
* Order: createdAt DESC, then id DESC as a tie-break, so paging never skips or
* repeats rows that share a millisecond. The client sets ordering: false.
*
* Inputs: req, where (the base filter; {} for none), searchFields (field names),
* fields (the row fields to pick), extra (optional: called with the stored row,
* returns more keys to add to the picked row, such as Activity's `summary`).
*
* Usage: await sails.helpers.audit.datatable.with({req, where, searchFields, fields})
*/
const MAX_ROWS = 500;

module.exports = {
friendlyName: 'Audit datatable',
description: 'AuditLog rows as DataTables server-side rows.',
inputs: {
req: {type: 'ref', required: true},
// Typed, so a wrong shape fails at the call, not deep in the query.
where: {type: {}, required: true},
searchFields: {type: ['string'], required: true},
fields: {type: ['string'], required: true},
extra: {type: 'ref', defaultsTo: null}
},
exits: {
success: {description: '{draw, recordsTotal, recordsFiltered, data}.'}
},
fn: async function ({req, where, searchFields, fields, extra}) {
// Safe for any JSON value (api/helpers/datatables/parse-request.js).
const parsed = sails.helpers.datatables.parseRequest.with({body: req.body});
const start = parsed.start;
const text = parsed.search;
// A length that is missing, -1, not a number, or above MAX_ROWS reads MAX_ROWS.
const length = parsed.length > 0 && parsed.length <= MAX_ROWS ? parsed.length : MAX_ROWS;

// Waterline normalizes criteria in place, and one request runs up to three
// queries on `where`, so each query gets its own copy.
const queryOf = () => {
if (text === '') { return _.cloneDeep(where); }
const search = {or: searchFields.map((field) => ({[field]: {contains: text}}))};
return Object.keys(where).length === 0 ? search : {and: [_.cloneDeep(where), search]};
};
const recordsTotal = await sails.models.auditlog.count(_.cloneDeep(where));
const recordsFiltered = text === '' ? recordsTotal : await sails.models.auditlog.count(queryOf()).meta({makeLikeModifierCaseInsensitive: true});
const rows = await sails.models.auditlog.find(queryOf())
.meta({makeLikeModifierCaseInsensitive: true})
.sort([{createdAt: 'DESC'}, {id: 'DESC'}])
.skip(start)
.limit(length);
return {
draw: parsed.draw,
recordsTotal,
recordsFiltered,
data: rows.map((row) => Object.assign(_.pick(row, fields), extra ? extra(row) : {}))
};
}
};
61 changes: 61 additions & 0 deletions api/helpers/audit/summary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* The plain-words summary of an AuditLog row, for the Activity page
* (GET /activity, issue #206; 1.6 ActivityManagement.php).
*
* It is built at read time from the stored fields, so a row written before
* this helper existed gets a summary too.
*
* - A renderable row: the event name, `, ` and the subject, then `: ` and the
* stored text. agent.enroll is "Agent enrollment", agent.token is "Agent
* token", any other type is shown as it is.
* - An access.* row: a verb from the HTTP method that starts `text`, then the
* subject, then ` (N records)` when more than one record changed.
* - Any other row: the stored text.
*
* The subject is the type, then the label in double quotes, else #id.
*
* Inputs: row (an AuditLog row, or any object with the same fields).
*
* Usage: sails.helpers.audit.summary.with({row})
*/
const EVENTS = {
'agent.enroll': 'Agent enrollment',
'agent.token': 'Agent token'
};
const VERBS = {DELETE: 'Deleted', PUT: 'Updated', PATCH: 'Updated', POST: 'Saved'};

module.exports = {
friendlyName: 'Summarize an audit row',
description: 'One plain sentence for an AuditLog row.',
sync: true,
inputs: {
row: {type: 'ref', required: true}
},
exits: {
success: {description: 'The summary string.'}
},
fn: function ({row}) {
let subject = '';
if (row.subjectType) {
subject = row.subjectType;
if (row.subjectLabel) {
subject += ` "${row.subjectLabel}"`;
} else if (row.subjectId) {
subject += ` #${row.subjectId}`;
}
}
const text = row.text || '';
if (row.renderable === true) {
const event = EVENTS[row.type] || row.type;
return `${event}${subject ? `, ${subject}` : ''}: ${text}`;
}
if (typeof row.type === 'string' && row.type.startsWith('access.')) {
const verb = VERBS[text.split(' ')[0]];
if (verb) {
const count = row.affectedCount > 1 ? ` (${row.affectedCount} records)` : '';
return `${verb}${subject ? ` ${subject}` : ''}${count}`;
}
}
return text;
}
};
2 changes: 2 additions & 0 deletions api/hooks/audit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ const READ_ACTIONS = [
// The Audit Log DataTable. Without it, every page load and scroll of the
// audit page would write an AuditLog row.
'audit/datatable',
// The Activity DataTable: the same reason as the Audit Log one.
'activity/datatable',
// Signs a JWT for the user and changes no record.
'auth/token'
];
Expand Down
2 changes: 2 additions & 0 deletions config/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ module.exports.globals = {
{
order: 170, text: 'Logging', icon: 'fas fa-scroll',
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'}
]
}
Expand Down
6 changes: 6 additions & 0 deletions config/permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ module.exports.permissions = {
audit: {
view: false
},
// The Activity page (GET /activity, POST /api/v1/activity/datatable). The
// key matches no model, so it opens no generic /api/v1/:model route. An
// admin role gets it through api/helpers/permissions/resolve.js, as every leaf.
activity: {
view: false
},
host: {
create: false,
destroy: false,
Expand Down
3 changes: 3 additions & 0 deletions config/policies.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ module.exports.policies = {
// The Audit Log rows (the action checks audit.view itself). It only reads.
'audit/datatable': ['isLoggedIn','isAuthenticated'],

// The Activity rows (the action checks activity.view itself). It only reads.
'activity/datatable': ['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
7 changes: 7 additions & 0 deletions config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ module.exports.routes = {
// must precede POST /api/v1/:model/datatable, or it is read as :model=audit.
'POST /api/v1/audit/datatable': {action: 'audit/datatable', csrf: false},

// The Activity page rows (GET /activity). A POST read that needs activity.view;
// 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},

// 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 @@ -141,6 +145,9 @@ module.exports.routes = {
// The Audit Log page (1.6 AuditManagement), read-only; it needs audit.view.
'GET /audit': {action: 'pages/list/audit'},

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

// Group Views
'GET /groups': {action: 'pages/list/groups'},
'GET /groups/create': {action: 'pages/create/groups'},
Expand Down
52 changes: 52 additions & 0 deletions test/helpers/audit-summary.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const assert = require('assert');

// The Activity summary (api/helpers/audit/summary.js): one plain sentence for an
// AuditLog row. It is a sync helper, called as the app calls it: .with() with
// no .now(). A helper that lost `sync: true` returns a Deferred, and the
// strictEqual assertions fail on it.
describe('Audit summary helper::', () => {
const summary = (row) => sails.helpers.audit.summary.with({row});

it('names a PUT on a labeled subject', () => {
assert.strictEqual(summary({
type: 'access.api', text: 'PUT /api/v1/host/abc', subjectType: 'host', subjectLabel: 'PC-01'
}), 'Updated host "PC-01"');
});

it('names a DELETE with a count and no label', () => {
assert.strictEqual(summary({
type: 'access.api', text: 'DELETE /api/v1/image', subjectType: 'image', subjectId: '', affectedCount: 3
}), 'Deleted image (3 records)');
});

it('names a POST with a count of 1 without the count', () => {
assert.strictEqual(summary({
type: 'access.api', text: 'POST /api/v1/host/bulk', subjectType: 'host', subjectId: '', affectedCount: 1
}), 'Saved host');
});

it('names a renderable row by its event, subject and text', () => {
assert.strictEqual(summary({
type: 'agent.token', renderable: true, subjectType: 'agentenrolltoken', subjectLabel: 'lab', text: 'minted, 3 use(s)'
}), 'Agent token, agentenrolltoken "lab": minted, 3 use(s)');
});

it('names agent.enroll, and an unknown event by its type', () => {
assert.strictEqual(summary({type: 'agent.enroll', renderable: true, text: 'enrolled'}), 'Agent enrollment: enrolled');
assert.strictEqual(summary({type: 'x.y', renderable: true, subjectType: 'host', subjectId: '7', text: 't'}), 'x.y, host #7: t');
});

it('gives a subject with an id and no label as type #id', () => {
assert.strictEqual(summary({type: 'access.api', text: 'PATCH /api/v1/host/7', subjectType: 'host', subjectId: '7'}), 'Updated host #7');
});

it('leaves no trailing space for a row with no subject', () => {
assert.strictEqual(summary({
type: 'access.page', text: 'POST /account/preferences', subjectType: '', subjectId: ''
}), 'Saved');
});

it('gives the stored text for any other row', () => {
assert.strictEqual(summary({type: 'auth.login', text: 'signed in'}), 'signed in');
});
});
Loading
Loading