diff --git a/api/controllers/agent-activity/host.js b/api/controllers/agent-activity/host.js
new file mode 100644
index 0000000..1dce6a2
--- /dev/null
+++ b/api/controllers/agent-activity/host.js
@@ -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}))
+ });
+ }
+};
diff --git a/api/controllers/agent-activity/summary.js b/api/controllers/agent-activity/summary.js
new file mode 100644
index 0000000..4fb055b
--- /dev/null
+++ b/api/controllers/agent-activity/summary.js
@@ -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}
+ }))
+ });
+ }
+};
diff --git a/api/controllers/pages/list/agent-activity.js b/api/controllers/pages/list/agent-activity.js
new file mode 100644
index 0000000..779a192
--- /dev/null
+++ b/api/controllers/pages/list/agent-activity.js
@@ -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()
+ };
+ }
+};
diff --git a/api/hooks/audit/index.js b/api/hooks/audit/index.js
index 9f9a6b2..0327de0 100644
--- a/api/hooks/audit/index.js
+++ b/api/hooks/audit/index.js
@@ -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'
];
diff --git a/config/globals.js b/config/globals.js
index f1dbaae..e1bfa50 100644
--- a/config/globals.js
+++ b/config/globals.js
@@ -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'}
]
}
],
diff --git a/config/permissions.js b/config/permissions.js
index d03e5af..3fa4cb6 100644
--- a/config/permissions.js
+++ b/config/permissions.js
@@ -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,
diff --git a/config/policies.js b/config/policies.js
index 6a9d321..7435237 100644
--- a/config/policies.js
+++ b/config/policies.js
@@ -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'],
diff --git a/config/routes.js b/config/routes.js
index 8de94cd..56592c6 100644
--- a/config/routes.js
+++ b/config/routes.js
@@ -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},
@@ -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'},
diff --git a/test/integration/agent-activity-page.test.js b/test/integration/agent-activity-page.test.js
new file mode 100644
index 0000000..c313ca8
--- /dev/null
+++ b/test/integration/agent-activity-page.test.js
@@ -0,0 +1,225 @@
+const assert = require('assert');
+const crypto = require('crypto');
+const supertest = require('supertest');
+const support = require('../support/pki');
+const auditWait = require('../support/audit-wait');
+const {waitFor} = auditWait;
+
+// The Agent Activity page (issue #207): GET /agent-activity,
+// POST /api/v1/agent-activity/summary and POST /api/v1/agent-activity/host,
+// behind the agentactivity.view permission. It is a view over the agent rows of
+// the audit trail, grouped by host. The seam writes after the response, so a
+// test that expects a row polls for it. A test that expects NO row makes its
+// request, then a sentinel request that does write a row, waits for the
+// sentinel row, and only then asserts the absence (test/integration/audit.test.js).
+describe('Agent Activity page::', function () {
+ this.timeout(120000);
+
+ support.useFixture();
+ const app = () => sails.hooks.http.app;
+ const HTML = ['Accept', 'text/html'];
+ const AJAX = ['X-Requested-With', 'XMLHttpRequest'];
+ const RUN = crypto.randomBytes(3).toString('hex');
+ const PREFIX = 'mocha-aa' + RUN;
+ const PASSWORD = 'mocha-aa-pw';
+ const SUMMARY = '/api/v1/agent-activity/summary';
+ const HOST = '/api/v1/agent-activity/host';
+ let seq = 0;
+
+ async function mkUser(label, {isAdmin = false, permissions = {}} = {}) {
+ const name = `${PREFIX}-${label}`;
+ const role = await Role.create({name, isAdmin, permissions}).fetch();
+ await User.create({username: name, password: PASSWORD, email: `${name}@example.com`, roles: [role.id]});
+ return name;
+ }
+
+ // Signs in as a browser does: a CSRF token for the login form, then the form POST.
+ async function signIn(username) {
+ const agent = supertest.agent(app());
+ const {body} = await agent.get('/csrfToken').expect(200);
+ await agent.post('/login').type('form').set(...HTML)
+ .send({username, password: PASSWORD, _csrf: body._csrf})
+ .expect(302).expect('Location', '/');
+ return agent;
+ }
+
+ // A denied GET on a model that no one can read (test/support/audit-wait.js).
+ const sentinel = (agent, start) => auditWait.sentinel((url) => agent.get(url).set(...AJAX), `${PREFIX}-sentinel-${seq++}`, start);
+
+ const post = (agent, url, body) => agent.post(url).set(...AJAX).send(body);
+
+ // One agent row for a host, written the way the agent code writes it.
+ const agentRow = (hostId, label, text, extra = {}) => sails.helpers.audit.record.with({req: null, row: {
+ type: 'agent.enroll', subjectType: 'host', subjectId: hostId, subjectLabel: label,
+ renderable: true, outcome: 'allowed', text, createdBy: `${PREFIX}-row`, ...extra
+ }});
+
+ const mkHost = async (name) => (await Host.create({name: `${PREFIX}-${name}`, macs: []}).fetch()).id;
+
+ let admin;
+ let limited;
+ let auditOnly;
+ let granted;
+
+ async function cleanup() {
+ await AuditLog.destroy({createdBy: {startsWith: PREFIX}});
+ await Host.destroy({name: {startsWith: PREFIX}});
+ await User.destroy({username: {startsWith: PREFIX}});
+ await Role.destroy({name: {startsWith: PREFIX}});
+ }
+
+ before(async () => {
+ await cleanup();
+ const adminName = await mkUser('admin', {isAdmin: true});
+ const limitedName = await mkUser('limited', {permissions: {stock: {host: {read: true}}}});
+ const auditName = await mkUser('auditonly', {permissions: {stock: {audit: {view: true}}}});
+ const grantedName = await mkUser('granted', {permissions: {stock: {agentactivity: {view: true}}}});
+ admin = await signIn(adminName);
+ limited = await signIn(limitedName);
+ auditOnly = await signIn(auditName);
+ granted = await signIn(grantedName);
+ admin.username = adminName;
+ limited.username = limitedName;
+ });
+ after(cleanup);
+
+ describe('the page', () => {
+ it('answers 200 with the six headings for an admin', async () => {
+ const res = await admin.get('/agent-activity').set(...HTML).expect(200);
+ const heads = [...res.text.matchAll(/
]*>([\s\S]*?)<\/th>/g)].map((m) => m[1].replace(/<[^>]*>/g, '').trim());
+ assert.deepStrictEqual(heads, ['When', 'Event', 'Detail', 'Outcome', 'Host', 'Order']);
+ });
+
+ it('answers 403 for a limited user, and writes a denied row for agentactivity.view', async () => {
+ const start = Date.now();
+ await limited.get('/agent-activity').set(...HTML).expect(403);
+ const [row] = await waitFor({createdBy: limited.username, permission: 'agentactivity.view'}, start);
+ assert.strictEqual(row.outcome, 'denied');
+ });
+
+ it('answers 403 for a role that holds only audit.view', async () => {
+ await auditOnly.get('/agent-activity').set(...HTML).expect(403);
+ });
+
+ it('answers 200 for a limited role that holds stock.agentactivity.view', async () => {
+ await granted.get('/agent-activity').set(...HTML).expect(200);
+ });
+
+ // A regression guard: it passes before and after the change.
+ it('does not open a generic route on the key', async () => {
+ await admin.get('/api/v1/agentactivity').set(...AJAX).expect(403);
+ });
+ });
+
+ describe('the gate on both endpoints', () => {
+ for (const url of [SUMMARY, HOST]) {
+ it(`answers 403 on ${url} without agentactivity.view, and for audit.view only`, async () => {
+ const start = Date.now();
+ await post(limited, url, {hostId: 'x'}).expect(403);
+ const [row] = await waitFor({createdBy: limited.username, permission: 'agentactivity.view'}, start);
+ assert.strictEqual(row.outcome, 'denied');
+ await post(auditOnly, url, {hostId: 'x'}).expect(403);
+ });
+
+ it(`answers 200 on ${url} for a role that holds agentactivity.view`, async () => {
+ await post(granted, url, {hostId: 'x'}).expect(200);
+ });
+ }
+ });
+
+ describe('the summary', () => {
+ let hostA;
+ let hostB;
+ let hostGone;
+ const goneId = crypto.randomBytes(12).toString('hex');
+
+ before(async () => {
+ hostA = await mkHost('A');
+ hostB = await mkHost('B');
+ // B is written first, so A is the host with the newest row.
+ await agentRow(hostB, 'old-name-B', 'b-1');
+ await agentRow(hostA, 'old-name-A', 'a-1');
+ await agentRow(hostA, 'old-name-A', 'a-2');
+ await agentRow(goneId, 'Gone-PC', 'g-1');
+ await agentRow(hostA, 'old-name-A', 'a-3');
+ hostGone = goneId;
+ // None of these may count: a token row, a non-agent row, a hostless agent row, a near-miss type.
+ await agentRow(hostA, '', 'token', {type: 'agent.token', subjectType: 'agentenrolltoken'});
+ await agentRow(hostA, '', 'access', {type: 'access.api'});
+ await agentRow('', 'nohost', 'hostless');
+ await agentRow(hostA, '', 'near-miss', {type: 'agentx.enroll'});
+ });
+
+ it('has one entry per host, the newest host first, with a correct count', async () => {
+ const {body} = await post(admin, SUMMARY, {}).expect(200);
+ const mine = body.hosts.filter((h) => [hostA, hostB, hostGone].includes(h.hostId));
+ assert.deepStrictEqual(mine.map((h) => h.hostId), [hostA, hostGone, hostB]);
+ assert.deepStrictEqual(mine.map((h) => h.count), [3, 1, 1]);
+ assert.deepStrictEqual(Object.keys(body.hosts[0]).sort(), ['count', 'hostId', 'hostName', 'latest']);
+ assert.deepStrictEqual(Object.keys(mine[0].latest).sort(), ['createdAt', 'id', 'outcome', 'text', 'type']);
+ assert.strictEqual(typeof mine[0].latest.id, 'string');
+ assert.strictEqual(mine[0].latest.text, 'a-3');
+ assert.strictEqual(mine[0].latest.type, 'agent.enroll');
+ assert.strictEqual(mine[0].latest.outcome, 'allowed');
+ });
+
+ it('names a host by its current name, and leaves out the rows that are not agent rows with a host', async () => {
+ const {body} = await post(admin, SUMMARY, {}).expect(200);
+ const a = body.hosts.find((h) => h.hostId === hostA);
+ assert.strictEqual(a.hostName, `${PREFIX}-A`);
+ assert.strictEqual(body.hosts.find((h) => h.hostId === ''), undefined);
+ });
+
+ it('shows a deleted host as its stored label followed by (deleted)', async () => {
+ const {body} = await post(admin, SUMMARY, {}).expect(200);
+ assert.strictEqual(body.hosts.find((h) => h.hostId === hostGone).hostName, 'Gone-PC (deleted)');
+ });
+
+ it('writes no row for an allowed call to either endpoint', async () => {
+ const start = Date.now();
+ await post(admin, SUMMARY, {}).expect(200);
+ await post(admin, HOST, {hostId: hostA}).expect(200);
+ await sentinel(admin, start);
+ const rows = await AuditLog.find({createdBy: admin.username, createdAt: {'>=': start}});
+ assert.deepStrictEqual(rows.filter((r) => r.permission === 'agentactivity.view' || /agent-activity/.test(r.text)), []);
+ });
+
+ describe('the host rows', () => {
+ it('returns the host rows, newest first, with a total', async () => {
+ const {body} = await post(admin, HOST, {hostId: hostA}).expect(200);
+ assert.deepStrictEqual(body.rows.map((r) => r.text), ['a-3', 'a-2', 'a-1']);
+ assert.strictEqual(body.total, 3);
+ assert.strictEqual(body.limit, 500);
+ assert.deepStrictEqual(Object.keys(body.rows[0]).sort(), ['createdAt', 'id', 'outcome', 'text', 'type']);
+ assert.strictEqual(typeof body.rows[0].id, 'string');
+ });
+
+ it('returns no row for a host with none', async () => {
+ const {body} = await post(admin, HOST, {hostId: crypto.randomBytes(12).toString('hex')}).expect(200);
+ assert.deepStrictEqual(body, {rows: [], total: 0, limit: 500});
+ });
+
+ it('answers 400, and does not crash, for a missing or non-string hostId', async () => {
+ await post(admin, HOST, {}).expect(400);
+ await post(admin, HOST, {hostId: {}}).expect(400);
+ await post(admin, HOST, {hostId: {$ne: ''}}).expect(400);
+ await post(admin, HOST, {hostId: 5}).expect(400);
+ // An empty id would match every hostless agent row (seeded in before()).
+ await post(admin, HOST, {hostId: ''}).expect(400);
+ // The server still answers.
+ await post(admin, HOST, {hostId: hostA}).expect(200);
+ });
+
+ it('returns at most 500 rows and counts all of them in total', async () => {
+ const hostBig = await mkHost('Big');
+ for (let i = 0; i < 501; i++) { await agentRow(hostBig, '', `big-${i}`); }
+ const {body} = await post(admin, HOST, {hostId: hostBig}).expect(200);
+ assert.strictEqual(body.rows.length, 500);
+ assert.strictEqual(body.total, 501);
+ assert.strictEqual(body.rows[0].text, 'big-500');
+ const sum = await post(admin, SUMMARY, {}).expect(200);
+ assert.strictEqual(sum.body.hosts.find((h) => h.hostId === hostBig).count, 501);
+ });
+ });
+ });
+});
diff --git a/tools/smoke/checks/agent-activity.js b/tools/smoke/checks/agent-activity.js
new file mode 100644
index 0000000..7a9d7de
--- /dev/null
+++ b/tools/smoke/checks/agent-activity.js
@@ -0,0 +1,59 @@
+/**
+ * The Agent Activity page (issue #207).
+ *
+ * It proves, in a browser, that GET /agent-activity loads with a group header
+ * that shows the host name and its event count, and that the expand toggle
+ * loads the host's events. The smoke run empties its database, so the check
+ * makes its own data: a host with a MAC (POST /api/v1/host), then an agent
+ * enrollment for that MAC (POST /agent/v1/enroll), which writes an agent.enroll
+ * row for the host. The MAC is in colon form: the enroll route drops a bare
+ * 12-digit MAC. The check deletes the host at the end. tools/smoke/ui.js runs
+ * this file (see the comment at its top).
+ */
+
+const crypto = require('crypto');
+const enroll = require('../lib/agent-enroll');
+
+const expect = (what, actual, expected) => {
+ const a = JSON.stringify(actual);
+ const e = JSON.stringify(expected);
+ if (a !== e) throw new Error(`${what} is ${a}, expected ${e}`);
+};
+
+// Same-origin AJAX passes api/policies/apiCsrfGuard.js on /api/v1 mutations.
+const AJAX = {'X-Requested-With': 'XMLHttpRequest', Accept: 'application/json'};
+
+module.exports = {
+ name: 'agent-activity: the Agent Activity page',
+ run: async ({page, base, checkPage}) => {
+ const name = `aa-smoke-${crypto.randomBytes(4).toString('hex')}`;
+ // A locally administered MAC (02:...), in colon form.
+ const mac = ['02', ...crypto.randomBytes(5).toString('hex').match(/../g)].join(':');
+
+ const made = await page.request.post(`${base}/api/v1/host`, {headers: AJAX, data: {name, macs: [mac]}});
+ if (!made.ok()) throw new Error(`POST /api/v1/host answered ${made.status()}`);
+ const host = await made.json();
+ try {
+ // The enrollment, as the agent sends it: a real CSR over HTTP.
+ const enrolled = await enroll(page, base, {hostname: name, macs: [mac]});
+ if (![200, 202].includes(enrolled.status())) throw new Error(`POST /agent/v1/enroll answered ${enrolled.status()}`);
+
+ await checkPage(page, {path: '/agent-activity', table: true});
+ expect('the visible headings', (await page.locator('#listtable thead th').allInnerTexts()).map((t) => t.trim()), ['When', 'Event', 'Detail', 'Outcome']);
+ const group = page.locator('#listtable tbody tr.dtrg-group', {hasText: name});
+ await group.first().waitFor({timeout: 10000});
+ const header = (await group.first().innerText()).trim();
+ if (!/ - \d+ events?/.test(header)) throw new Error(`the group header is ${JSON.stringify(header)}, expected the event count`);
+
+ // The expand toggle loads the host's events.
+ const loaded = page.waitForResponse((r) => r.url().endsWith('/api/v1/agent-activity/host') && r.request().method() === 'POST', {timeout: 10000});
+ await group.first().locator('.fog-aa-toggle').click();
+ expect('the status of the host events request', (await loaded).status(), 200);
+ await page.locator('#listtable tbody tr.dtrg-group', {hasText: name}).locator('.fog-aa-toggle', {hasText: 'Collapse'}).waitFor({timeout: 10000});
+ const rows = await page.locator('#listtable tbody tr:not(.dtrg-group):not(.dt-empty)').count();
+ if (rows < 1) throw new Error('the expanded host shows no event row');
+ } finally {
+ await page.request.delete(`${base}/api/v1/host`, {headers: AJAX, data: {id: [host.id]}});
+ }
+ }
+};
diff --git a/tools/smoke/checks/agent-admin.js b/tools/smoke/checks/agent-admin.js
index d009c0e..76adbbe 100644
--- a/tools/smoke/checks/agent-admin.js
+++ b/tools/smoke/checks/agent-admin.js
@@ -11,6 +11,8 @@
* runs this file (see the comment at its top).
*/
+const enroll = require('../lib/agent-enroll');
+
const expect = (what, actual, expected) => {
const a = JSON.stringify(actual);
const e = JSON.stringify(expected);
@@ -145,13 +147,7 @@ async function markupAsText(ctx) {
// An enrollment, as the agent sends it: a real CSR over HTTP (no MAC: no pending host is created).
// The agent chooses its version, hostname and serial; each shows in its own cell.
- const crypto = require('crypto');
- const x509 = require('@peculiar/x509');
- const keys = await crypto.webcrypto.subtle.generateKey({name: 'ECDSA', namedCurve: 'P-256'}, true, ['sign', 'verify']);
- const csr = await x509.Pkcs10CertificateRequestGenerator.create({name: '', keys, signingAlgorithm: {name: 'ECDSA', hash: 'SHA-256'}});
- const enrolled = await page.request.post(`${base}/agent/v1/enroll`, {
- data: {protocol: 1, agent_version: img, os: 'linux', arch: 'amd64', hostname: img, identity: {macs: [], system_serial: img}, csr_pem: csr.toString('pem')}
- });
+ const enrolled = await enroll(page, base, {hostname: img, macs: [], agentVersion: img, serial: img});
expect('the status of POST /agent/v1/enroll', enrolled.status(), 202);
const minted = await page.request.post(`${base}/api/v1/agent/token`, {
headers: AJAX, data: {name: bold, uses: 1, expires: Date.now() + 3600 * 1000}
diff --git a/tools/smoke/checks/ui16-03-shell.js b/tools/smoke/checks/ui16-03-shell.js
index 88c3c4f..fa088b2 100644
--- a/tools/smoke/checks/ui16-03-shell.js
+++ b/tools/smoke/checks/ui16-03-shell.js
@@ -37,6 +37,7 @@ const HEADERS = {
'/about/settings': ['FOG Configuration', 'FOG Settings'],
'/audit': ['Audit Log', 'Audit Log'],
'/activity': ['Activity', 'Activity'],
+ '/agent-activity': ['Agent Activity', 'Agent Activity'],
'/snapins': ['Snapin Management', 'All Snapins'],
'/printers': ['Printer Management', 'All Printers'],
'/hosts/create': ['Host Management', 'Create New Host'],
diff --git a/tools/smoke/checks/ui16-04-sidebar.js b/tools/smoke/checks/ui16-04-sidebar.js
index 6a1c3f7..a121b1f 100644
--- a/tools/smoke/checks/ui16-04-sidebar.js
+++ b/tools/smoke/checks/ui16-04-sidebar.js
@@ -86,7 +86,7 @@ async function checkShape({page, base, checkPage}) {
expect('the number of ul.plugin-options', got.pluginLists, 0);
expect('the User Administration children', got.groupChildren, [['Users', 'fas fa-users'], ['Roles', 'fas fa-user-shield']]);
// Issue #206: Activity comes before Audit Log (1.6 FOGPage.php:658-661).
- expect('the Logging children', got.loggingChildren, ['Activity', 'Audit Log']);
+ expect('the Logging children', got.loggingChildren, ['Activity', 'Audit Log', 'Agent Activity']);
// Pending MACs, Export Hosts and Import Hosts have no route, so the sidebar hides them.
expect('the Hosts sub-links', got.hostsSubs, ['List All Hosts', 'Create New Host', 'Pending Hosts', 'Pending Agents', 'Agent Tokens']
.map((text) => [text, 'nav-icon far fa-circle']));
diff --git a/tools/smoke/checks/ui16-06-tables.js b/tools/smoke/checks/ui16-06-tables.js
index a601bce..55e894b 100644
--- a/tools/smoke/checks/ui16-06-tables.js
+++ b/tools/smoke/checks/ui16-06-tables.js
@@ -32,10 +32,12 @@ const TOOLBARS = {
// The Audit Log is not selectable (1.6 AuditManagement $selectable = false).
'/audit': SHARED,
// The Activity page is not selectable either (1.6 ActivityManagement $selectable = false).
- '/activity': SHARED
+ '/activity': SHARED,
+ // The Agent Activity page is not selectable either (1.6 AgentActivityManagement $selectable = false).
+ '/agent-activity': SHARED
};
// The 1.6 title of a list that does not start with "All " (HostManagement::agentTokens).
-const TITLES = {'/hosts/agentTokens': 'Agent Enrollment Tokens', '/audit': 'Audit Log', '/activity': 'Activity'};
+const TITLES = {'/hosts/agentTokens': 'Agent Enrollment Tokens', '/audit': 'Audit Log', '/activity': 'Activity', '/agent-activity': 'Agent Activity'};
// The 1.6 paged length menu (1.6 fog.common.js lengthMenu).
const LENGTH_MENU = ['10', '25', '50', '100', '250', '500', 'All'];
diff --git a/tools/smoke/lib/agent-enroll.js b/tools/smoke/lib/agent-enroll.js
new file mode 100644
index 0000000..2ac5d18
--- /dev/null
+++ b/tools/smoke/lib/agent-enroll.js
@@ -0,0 +1,22 @@
+/**
+ * Enrolls a fog-agent over HTTP, as the agent sends it: a fresh key and a real
+ * CSR to POST /agent/v1/enroll. Shared by the smoke checks that need an
+ * enrollment (agent-admin.js, agent-activity.js).
+ *
+ * enroll(page, base, {hostname, macs, agentVersion, serial}) returns the
+ * Playwright response. `macs` is a list of colon-form MACs (the route drops a
+ * bare 12-digit MAC); an empty list makes no pending host. `agentVersion`
+ * defaults to '1.0.0'; `serial` is the identity system_serial, left out when
+ * unset.
+ */
+const crypto = require('crypto');
+const x509 = require('@peculiar/x509');
+
+module.exports = async function enroll(page, base, {hostname, macs = [], agentVersion = '1.0.0', serial}) {
+ const keys = await crypto.webcrypto.subtle.generateKey({name: 'ECDSA', namedCurve: 'P-256'}, true, ['sign', 'verify']);
+ const csr = await x509.Pkcs10CertificateRequestGenerator.create({name: '', keys, signingAlgorithm: {name: 'ECDSA', hash: 'SHA-256'}});
+ const identity = serial === undefined ? {macs} : {macs, system_serial: serial};
+ return page.request.post(`${base}/agent/v1/enroll`, {
+ data: {protocol: 1, agent_version: agentVersion, os: 'linux', arch: 'amd64', hostname, identity, csr_pem: csr.toString('pem')}
+ });
+};
diff --git a/tools/smoke/ui.js b/tools/smoke/ui.js
index 72e4b31..ed9ffba 100644
--- a/tools/smoke/ui.js
+++ b/tools/smoke/ui.js
@@ -92,6 +92,7 @@ const PAGES = [
{path: '/about/settings'},
{path: '/audit', table: true},
{path: '/activity', table: true},
+ {path: '/agent-activity', table: true},
{path: '/hosts/create'},
{path: '/groups/create'},
{path: '/images/create'},
diff --git a/views/pages/partials/list/agent-activity.js b/views/pages/partials/list/agent-activity.js
new file mode 100644
index 0000000..34702fd
--- /dev/null
+++ b/views/pages/partials/list/agent-activity.js
@@ -0,0 +1,127 @@
+(function($) {
+ // The Agent Activity list (GET /agent-activity; 1.6 fog.agentactivity.list.js):
+ // the fog-agent enrollment events of each host, grouped by host, the host with
+ // the newest event first. The summary endpoint gives one row per host (its
+ // newest event); the expand toggle in a group header loads that host's events.
+ // A server value shows as text, never as markup (window.fogTextCell, and
+ // textContent in the group header).
+
+ // The outcome badge color: the Audit Log page's (views/pages/partials/list/audit.js).
+ const BADGES = {allowed: 'success', denied: 'danger', failed: 'warning', partial: 'warning'};
+ const COLUMN_ORDER = 5;
+
+ const pad = (n, width) => String(n).padStart(width, '0');
+
+ // The summary row of each host, by host id: {pos, row}. `row` is the row that
+ // shows while the host is collapsed. Rebuilt by each load of the summary.
+ let summaries = {};
+ // {total, limit} of each expanded host, by host id (the host endpoint answer).
+ // Emptied by each load of the summary.
+ let expanded = {};
+
+ // The Order value: the host's position in the summary, then the row's position
+ // in the host, so a group stays together and its newest event stays first.
+ function orderOf(pos, index) {
+ return pad(pos, 5) + '-' + pad(index, 3);
+ }
+
+ // A table row of one event of a host.
+ function rowOf(host, pos, index, event) {
+ return $.extend({}, event, {
+ hostId: host.hostId,
+ hostName: host.hostName,
+ count: host.count,
+ order: orderOf(pos, index)
+ });
+ }
+
+ function outcomeCell(data, type) {
+ if (type !== 'display') { return data; }
+ return '' + window.fogTextCell(data, type) + '';
+ }
+
+ // The group header: the host name, the event count, and the expand toggle.
+ function startRender(rows, hostId) {
+ const first = rows.data()[0] || {};
+ const loaded = expanded[hostId];
+ let label = first.hostName + ' - ' + first.count + (first.count === 1 ? ' event' : ' events');
+ if (loaded !== undefined && loaded.total > loaded.limit) { label += ' (showing the newest ' + loaded.limit + ')'; }
+ const toggle = $('')
+ .attr('data-host-id', hostId)
+ .text(loaded === undefined ? 'Expand' : 'Collapse');
+ return $('').append(document.createTextNode(label), toggle);
+ }
+
+ let opts = {
+ // Scroller does not work with RowGroup, so the table has no scroller and no
+ // paging (1.6 fog.agentactivity.list.js:263-264). The summary holds at most
+ // MAX_HOSTS hosts (agent-activity/summary.js), one row each.
+ serverSide: false,
+ scroller: false,
+ deferRender: false,
+ scrollY: '',
+ paging: false,
+ // The order is fixed: the hidden Order column (see orderOf), not a click.
+ order: [[COLUMN_ORDER, 'asc']],
+ columns: [
+ {data: 'createdAt', render: window.fogWhenCell, orderable: false},
+ {data: 'type', render: window.fogTextCell, orderable: false},
+ {data: 'text', render: window.fogTextCell, orderable: false},
+ {data: 'outcome', render: outcomeCell, orderable: false},
+ {data: 'hostId', visible: false, className: 'noVis', orderable: false},
+ {data: 'order', visible: false, className: 'noVis', orderable: false}
+ ],
+ rowId: 'id',
+ // Keyed by hostId, so two hosts with the same name never merge.
+ rowGroup: {dataSrc: 'hostId', startRender: startRender},
+ // A function, so the shared Refresh button (dt.ajax.reload()) reloads the
+ // summary, and every expanded group closes. A bare array would show an
+ // empty table: the callback takes {data: rows}.
+ ajax: function(unusedData, callback) {
+ $.ajax({type: 'post', url: '/api/v1/agent-activity/summary', contentType: 'application/json', data: '{}'})
+ .done((json) => {
+ summaries = {};
+ expanded = {};
+ const rows = (json.hosts || []).map((host, pos) => {
+ const row = rowOf(host, pos, 0, host.latest);
+ summaries[host.hostId] = {pos: pos, row: row};
+ return row;
+ });
+ callback({data: rows});
+ })
+ .fail((xhr) => {
+ window.fogToast('error', 'Could not load the agent activity (HTTP ' + xhr.status + ').');
+ callback({data: []});
+ });
+ }
+ };
+
+ let table = $('#listtable').registerTable(undefined, opts);
+
+ const rowsOf = (hostId) => table.rows((idx, data) => data.hostId === hostId);
+
+ // Expand loads every event of the host; Collapse puts the one summary row back.
+ $('#listtable tbody').on('click', '.fog-aa-toggle', function() {
+ const hostId = $(this).attr('data-host-id');
+ const summary = summaries[hostId];
+ if (!summary) { return; }
+ if (expanded[hostId] !== undefined) {
+ rowsOf(hostId).remove();
+ table.rows.add([summary.row]);
+ delete expanded[hostId];
+ table.draw(false);
+ return;
+ }
+ $.ajax({type: 'post', url: '/api/v1/agent-activity/host', contentType: 'application/json', data: JSON.stringify({hostId: hostId})})
+ .done((json) => {
+ const host = summary.row;
+ rowsOf(hostId).remove();
+ table.rows.add(json.rows.map((event, index) => rowOf(host, summary.pos, index, event)));
+ expanded[hostId] = {total: json.total, limit: json.limit};
+ table.draw(false);
+ })
+ .fail((xhr) => {
+ window.fogToast('error', 'Could not load the events of the host (HTTP ' + xhr.status + ').');
+ });
+ });
+})(jQuery);
|