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
62 changes: 62 additions & 0 deletions api/controllers/log/tail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const fs = require('fs');
const path = require('path');
const {LINES, DEFAULT_LINES} = require('../../lib/log-viewer');

/**
* POST /api/v1/log/tail
*
* The last lines of one fog-node log file, for the Log Viewer page
* (GET /logviewer). Body {file, lines, reverse}. Answers {file, lines: [...]},
* oldest first, or newest first when `reverse`. Needs setting.read.
*
* `file` must be a name that the files rule lists (api/helpers/log/files.js).
* The path is built here from the folder and that name, never from the client.
* After the open, fstat must give the same dev and ino as the listed file: a
* file swapped in between (for example for a symlink) answers 400. A rotation
* between the list and the open gives the same 400 for a moment; the next poll
* of the page reads the new file. O_NOFOLLOW does not exist on Windows, so
* this check is the guard.
*/

// A number, or a string of digits, whose value is in LINES; else the default.
const parseLines = (value) => {
const n = typeof value === 'number' ? value : (typeof value === 'string' && /^\d+$/.test(value) ? Number(value) : NaN);
return LINES.includes(n) ? n : DEFAULT_LINES;
};

module.exports = {
friendlyName: 'Log tail',
description: 'The last lines of a fog-node log file.',
fn: async function () {
const req = this.req;
const res = this.res;
if (!sails.helpers.audit.can(req, 'setting.read')) {
return res.forbidden();
}
const body = req.body || {};
const unknown = () => res.status(400).json({error: 'Unknown log file.'});
const {dir, files} = sails.helpers.log.files();
const entry = typeof body.file === 'string' ? files.find((f) => f.name === body.file) : undefined;
if (!entry) { return unknown(); }
const lines = parseLines(body.lines);
const reverse = body.reverse === true || body.reverse === 'true';

let fd;
try {
fd = fs.openSync(path.join(dir, entry.name), 'r');
const st = fs.fstatSync(fd, {bigint: true});
if (!st.isFile() || st.dev !== entry.dev || st.ino !== entry.ino) {
return unknown();
}
const tail = sails.helpers.log.tail.with({fd, size: Number(st.size), lines});
return res.json({file: entry.name, lines: reverse ? tail.lines.reverse() : tail.lines});
} catch (err) {
// The file is gone: an empty answer, as a log that has no lines yet.
if (err.code === 'ENOENT') { return res.json({file: entry.name, lines: []}); }
sails.log.warn(`The Log Viewer cannot read ${entry.name}: ${err.message}`);
return res.status(500).json({error: 'Cannot read the log file.'});
} finally {
if (fd !== undefined) { fs.closeSync(fd); }
}
}
};
35 changes: 35 additions & 0 deletions api/controllers/pages/logviewer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const path = require('path');
const {LINES, DEFAULT_LINES} = require('../../lib/log-viewer');

// GET /logviewer: the FOG 1.6 Log Viewer (LogViewerManagement.php,
// status/logtoview.php), over the fog-node log file (api/lib/file-logger.js).
// It needs setting.read, as 1.6 maps logviewer onto its settings node
// (Authorization.php:87). The page script (views/pages/partials/logviewer.js)
// reads the lines from POST /api/v1/log/tail (api/controllers/log/tail.js).
module.exports = {
friendlyName: 'Log Viewer',
description: 'The Log Viewer page.',
exits: {
forbidden: {
responseType: 'forbidden'
},
success: {
viewTemplatePath: 'pages/logviewer'
}
},
fn: async function () {
if (!sails.helpers.audit.can(this.req, 'setting.read')) {
throw 'forbidden';
}
return {
section: 'Log Viewer',
page: 'Log Viewer',
// The files the files rule lists now: fog-node.log, then .1 to .5.
files: sails.helpers.log.files().files.map((f) => f.name),
// The Lines select and its default, as the tail action accepts them.
lineChoices: LINES,
defaultLines: DEFAULT_LINES,
partialname: path.join(__dirname, '..', '..', '..', 'views', 'pages', 'partials', 'logviewer.js')
};
}
};
44 changes: 44 additions & 0 deletions api/helpers/log/files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const fs = require('fs');
const path = require('path');
const {FILE, KEEP, FALLBACK_DIR, resolveDir} = require('../../lib/file-logger');

/**
* The files rule of the Log Viewer: which log files a browser may read.
*
* Outputs: {dir, files}. `dir` is the log folder, read on each call
* (api/lib/file-logger.js resolveDir). `files` is a list of {name, dev, ino},
* in the order fog-node.log, .1 ... .5. A name is listed only when it matches
* exactly `fog-node.log` with an optional `.1` to `.5`, AND lstat reports a
* regular file there. A symlink, a folder or anything else is not listed.
* dev and ino are bigint (an NTFS file index is 64-bit). The tail action
* compares them with the file it opens, so a file swapped after this call is
* refused. Nothing outside this list is ever read.
*
* The fallback folder is the one config/log.js gives the logger, so the
* viewer reads the folder that the logger writes.
*/
const NAMES = [FILE, ...Array.from({length: KEEP}, (_, i) => `${FILE}.${i + 1}`)];

module.exports = {
friendlyName: 'Log files',
description: 'The log files that the Log Viewer may read.',
sync: true,
exits: {
success: {outputType: 'ref', description: '{dir, files: [{name, dev, ino}]}.'}
},
fn: function () {
const dir = resolveDir({fallbackDir: FALLBACK_DIR});
const files = [];
for (const name of NAMES) {
let st;
try {
st = fs.lstatSync(path.join(dir, name), {bigint: true});
} catch {
// Missing, or not readable: not listed.
continue;
}
if (st.isFile()) files.push({name, dev: st.dev, ino: st.ino});
}
return {dir, files};
}
};
84 changes: 84 additions & 0 deletions api/helpers/log/tail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
const fs = require('fs');

/**
* The last lines of an open file, for the Log Viewer.
*
* Inputs: fd (an open file), size (its size in bytes), lines (how many).
* Outputs: {lines, bytesRead}, the lines oldest first.
*
* It reads backwards from the end in CHUNK-byte reads and keeps the raw
* buffers. It stops when the bytes read hold more than `lines` newline bytes
* (0x0A), at the start of the file, or at MAX_READ bytes. It then joins the
* buffers, splits on 0x0A and decodes each line as UTF-8. A newline byte never
* occurs inside a multibyte character, so no character is split. A trailing
* newline does not make an empty last line. A last line with no newline (still
* being written) counts. When the read did not reach the start of the file,
* the first line may be partial, so it is dropped.
*/
const {CHUNK, MAX_READ} = require('../../lib/log-viewer');
const NEWLINE = 0x0A;

const countNewlines = (buf) => {
let n = 0;
for (let i = buf.indexOf(NEWLINE); i !== -1; i = buf.indexOf(NEWLINE, i + 1)) n++;
return n;
};

module.exports = {
friendlyName: 'Tail a file',
description: 'The last lines of an open file.',
sync: true,
inputs: {
fd: {type: 'number', required: true},
size: {type: 'number', required: true},
lines: {type: 'number', required: true}
},
exits: {
success: {outputType: 'ref', description: '{lines, bytesRead}.'}
},
fn: function ({fd, size, lines}) {
const buffers = [];
let pos = size;
let bytesRead = 0;
let newlines = 0;
let cut = false;
while (pos > 0 && bytesRead < MAX_READ && newlines <= lines) {
const length = Math.min(CHUNK, pos, MAX_READ - bytesRead);
pos -= length;
const buf = Buffer.alloc(length);
// readSync can return fewer bytes than asked: read until the chunk is full.
let got = 0;
while (got < length) {
const n = fs.readSync(fd, buf, got, length - got, pos + got);
if (n === 0) break;
got += n;
}
// The file shrank under the read. This chunk is not whole, and the bytes
// before it are not the bytes that came before it: stop, and keep only
// the chunks already read in full. The first line is then not known to
// be whole, so it is dropped below.
if (got < length) {
cut = true;
break;
}
buffers.unshift(buf);
bytesRead += length;
newlines += countNewlines(buf);
}
if (bytesRead === 0) return {lines: [], bytesRead};

const data = Buffer.concat(buffers);
const parts = [];
let from = 0;
for (let i = data.indexOf(NEWLINE); i !== -1; i = data.indexOf(NEWLINE, from)) {
parts.push(data.subarray(from, i));
from = i + 1;
}
// Bytes after the last newline are a line with no newline yet. None means
// the data ended with a newline: no empty last line.
if (from < data.length) parts.push(data.subarray(from));
// The read began inside the file: the first line may be partial.
if (pos > 0 || cut) parts.shift();
return {lines: parts.slice(-lines).map((part) => part.toString('utf8')), bytesRead};
}
};
2 changes: 2 additions & 0 deletions api/hooks/audit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ const READ_ACTIONS = [
// The Agent Activity reads: the same reason as the Audit Log one.
'agent-activity/summary',
'agent-activity/host',
// The Log Viewer polls this every 10 seconds: a row for each read would flood the trail.
'log/tail',
// Signs a JWT for the user and changes no record.
'auth/token'
];
Expand Down
5 changes: 4 additions & 1 deletion api/lib/file-logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const FILE = 'fog-node.log';
const MAX_BYTES = 10 * 1024 * 1024;
// The number of old files kept: fog-node.log.1 to fog-node.log.5.
const KEEP = 5;
// The folder when custom.logDir is not set: <app>/logs. config/log.js and the
// Log Viewer (api/helpers/log/files.js) both use it.
const FALLBACK_DIR = path.resolve(__dirname, '..', '..', 'logs');
// These levels go to stderr on the console, the rest to stdout.
const STDERR_LEVELS = new Set(['error', 'warn', 'crit']);
const LEVELS = ['log', 'error', 'warn', 'info', 'verbose', 'debug', 'silly', 'crit'];
Expand Down Expand Up @@ -109,4 +112,4 @@ const create = ({fallbackDir, logDir, maxBytes = MAX_BYTES} = {}) => {
return logger;
};

module.exports = {resolveDir, create};
module.exports = {FILE, KEEP, FALLBACK_DIR, resolveDir, create};
12 changes: 12 additions & 0 deletions api/lib/log-viewer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// The constants of the Log Viewer, defined once: the tail helper
// (api/helpers/log/tail.js), the tail action (api/controllers/log/tail.js),
// the page (api/controllers/pages/logviewer.js) and the tests use them.

module.exports = {
// The read size of the tail helper, and the most bytes it reads.
CHUNK: 4096,
MAX_READ: 4 * 1024 * 1024,
// The Lines select (1.6 LogViewerManagement.php:442-450), and its default.
LINES: [10, 25, 50, 100, 250, 500, 1000],
DEFAULT_LINES: 100
};
10 changes: 10 additions & 0 deletions assets/styles/zz-fog-ui.css
Original file line number Diff line number Diff line change
Expand Up @@ -1678,3 +1678,13 @@ body.fog-col-resizing {
--fog-chart-grid: rgba(255,255,255,0.12);
--fog-chart-empty: var(--bs-secondary-color);
}

/* The Log Viewer <pre> (views/pages/logviewer.ejs): a scrolling box of lines. */
.logviewer-pre {
max-height: 70vh;
min-height: 12rem;
overflow: auto;
padding: 0.5rem;
font-size: 0.8rem;
border: 1px solid var(--bs-border-color);
}
3 changes: 2 additions & 1 deletion config/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ module.exports.globals = {
// 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: 'Agent Activity', link: '/agent-activity', icon: 'fas fa-satellite-dish'}
{text: 'Agent Activity', link: '/agent-activity', icon: 'fas fa-satellite-dish'},
{text: 'Log Viewer', link: '/logviewer', icon: 'fas fa-file-lines'}
]
}
],
Expand Down
4 changes: 2 additions & 2 deletions config/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* https://sailsjs.com/docs/concepts/logging
*/

const path = require('path');
const fileLogger = require('../api/lib/file-logger');

module.exports.log = {

Expand All @@ -32,6 +32,6 @@ module.exports.log = {
// (api/lib/file-logger.js). The folder is custom.logDir, else <app>/logs.
// inspect is off so that the logger gets the raw arguments.
inspect: false,
custom: require('../api/lib/file-logger').create({fallbackDir: path.resolve(__dirname, '..', 'logs')})
custom: fileLogger.create({fallbackDir: fileLogger.FALLBACK_DIR})

};
3 changes: 3 additions & 0 deletions config/policies.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ module.exports.policies = {
'agent-activity/summary': ['isLoggedIn','isAuthenticated'],
'agent-activity/host': ['isLoggedIn','isAuthenticated'],

// The Log Viewer lines (the action checks setting.read itself). It only reads.
'log/tail': ['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
8 changes: 8 additions & 0 deletions config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ module.exports.routes = {
'POST /api/v1/agent-activity/summary': {action: 'agent-activity/summary', csrf: false},
'POST /api/v1/agent-activity/host': {action: 'agent-activity/host', csrf: false},

// The Log Viewer page (GET /logviewer) reads the fog-node log files. It is a
// POST read that needs setting.read; it must precede POST /api/v1/:model/...,
// or it is read as :model=log.
'POST /api/v1/log/tail': {action: 'log/tail', 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 @@ -157,6 +162,9 @@ module.exports.routes = {
// The Agent Activity page (1.6 AgentActivityManagement), read-only; it needs agentactivity.view.
'GET /agent-activity': {action: 'pages/list/agent-activity'},

// The Log Viewer page (1.6 LogViewerManagement), read-only; it needs setting.read.
'GET /logviewer': {action: 'pages/logviewer'},

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