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
13 changes: 3 additions & 10 deletions api/controllers/agent/poll.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
*/
const {jsonBody, version} = require('../../lib/agent-pki');

// The 1.6 default of FOG_CLIENT_CHECKIN_TIME, in seconds.
const DEFAULT_POLL_INTERVAL = 60;

module.exports = {
friendlyName: 'Agent poll',
description: 'Record the check-in of a fog-agent and answer with the minimal poll state.',
Expand All @@ -33,20 +30,16 @@ module.exports = {
if (agentVersion) { set.agentVersion = agentVersion; }
await sails.models.host.updateOne({id: host.id}).set(set);

const setting = await sails.models.setting.findOne({name: 'FOG_CLIENT_CHECKIN_TIME'});
// An integer, because the Go agent's PollInterval is an int and fails to
// decode 90.5. 1.6 casts with (int) and passes 0 or a negative number on:
// the agent then keeps its own default. A missing or non-numeric value
// gives 60.
const interval = parseInt(setting && setting.value, 10);
// An integer, with the 1.6 default of 60: config/fog-settings.js holds the rule.
const interval = await sails.helpers.settings.get('FOG_CLIENT_CHECKIN_TIME');

return res.status(200).json({
status: 'ok',
protocol: 1,
host: {id: host.agentId, name: host.name},
revision: '',
state: {revision: '', capabilities: []},
poll_interval: Number.isNaN(interval) ? DEFAULT_POLL_INTERVAL : interval,
poll_interval: interval,
// ISO 8601 with an offset, as 1.6 sends it (Route.php:3207, format('c')).
server_time: new Date().toISOString().replace(/\.\d{3}Z$/, '+00:00')
});
Expand Down
13 changes: 9 additions & 4 deletions api/controllers/pages/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ const path = require('path');
// saves on change through POST /api/v1/setting/save
// (api/controllers/setting/save.js, views/pages/partials/settings.js).

// The control types this page renders. Both catalog keys are `select`. The
// change that adds the first number, text or checkbox key adds its control.
const RENDERED_TYPES = ['select'];
// The control types this page renders. A `text` entry has no control yet: the
// change that adds the first text key adds it.
const RENDERED_TYPES = ['select', 'number', 'checkbox'];

// The 1.6 reload note, added to the label tooltip of a `reload: true` row
// (FOGConfigurationPage.php _renderSettings, $refreshtip).
Expand Down Expand Up @@ -66,7 +66,12 @@ module.exports = {
reload: entry.reload,
// One tooltip on the label: the description, then the reload note.
tip: entry.reload ? `${entry.description} — ${RELOAD_TIP}` : entry.description,
options: selectOptions(entry, value)
// The view picks the control off the type. A select has options with
// the stored value selected; a number shows the value; a checkbox is
// checked when the value is true.
type: entry.type,
value,
options: entry.type === 'select' ? selectOptions(entry, value) : []
});
}
return {
Expand Down
5 changes: 1 addition & 4 deletions api/helpers/agent/auto-approve.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
* Usage: let issued = await sails.helpers.agent.autoApprove.with({row, token})
*/
const HOUR_MS = 60 * 60 * 1000;
const DEFAULT_WINDOW_HOURS = 24;

module.exports = {
friendlyName: 'Auto-approve an agent enrollment',
Expand Down Expand Up @@ -63,9 +62,7 @@ module.exports = {
}

if (!via && ['known-host-no-agent', 'rebind'].includes(row.reason)) {
const setting = await sails.models.setting.findOne({name: 'FOG_AGENT_ENROLL_DEPLOY_WINDOW'});
const window = setting ? Number(setting.value) : DEFAULT_WINDOW_HOURS;
const hours = Number.isNaN(window) ? DEFAULT_WINDOW_HOURS : window;
const hours = await sails.helpers.settings.get('FOG_AGENT_ENROLL_DEPLOY_WINDOW');
const host = hours >= 1 ? await sails.models.host.findOne({id: row.host}) : undefined;
const deployed = host ? Date.parse(host.deployed) : NaN;
if (!Number.isNaN(deployed) && deployed <= Date.now() && Date.now() - deployed <= hours * HOUR_MS) {
Expand Down
3 changes: 1 addition & 2 deletions api/helpers/agent/resolve-host.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ module.exports = {

// The Host model stores guid, serial and asset in lower case.
let smbiosId = '';
const setting = await sails.models.setting.findOne({name: 'FOG_HOST_IDENTIFY_SMBIOS'});
const smbiosOff = setting && String(setting.value).trim().toLowerCase() === 'off';
const smbiosOff = (await sails.helpers.settings.get('FOG_HOST_IDENTIFY_SMBIOS')) === 'off';
if (!smbiosOff && (smbios.guid || smbios.serial)) {
const guid = smbios.guid.toLowerCase();
const serial = smbios.serial.toLowerCase();
Expand Down
9 changes: 1 addition & 8 deletions api/helpers/form-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ function esc(s) {
.replace(/>/g, '&gt;').replace(/"/g, '&quot;');
}

// A FOG setting value is on when it is truthy and not the string "0" or
// "false". FOG stores its settings as strings, so "0" means off there.
function settingOn(value) {
return !!value && value !== '0' && value !== 'false';
}

const MODES = ['create', 'create-modal', 'edit'];

module.exports = {
Expand Down Expand Up @@ -81,8 +75,7 @@ module.exports = {
// only for a form with a password field, and never created here.
let showPasswords = false;
if (Object.keys(formItems).some((k) => formItems[k].type === 'password')) {
let setting = await sails.models.setting.findOne({name: 'FOG_ENABLE_SHOW_PASSWORDS'});
showPasswords = !!setting && settingOn(setting.value);
showPasswords = await sails.helpers.settings.get('FOG_ENABLE_SHOW_PASSWORDS');
}

// One field row on the FOG 1.6 grid (FOGPage.php:5654-5667): the label in
Expand Down
91 changes: 90 additions & 1 deletion config/fog-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
* Each entry, keyed by the FOG 1.6 setting name:
*
* category the FOG 1.6 category name, e.g. 'FOG View Settings'
* description the FOG 1.6 settingDesc text, as plain text (no HTML)
* description the FOG 1.6 settingDesc text, as plain text (no HTML).
* Where fog-node behaves differently from 1.6, the text says
* what fog-node does: a wrong description on a trust setting
* misleads the admin who sets it.
* type 'select', 'number', 'text' or 'checkbox'
* options for a 'select' only: an array of {value, label}
* default the value when no record exists or the stored value is bad
Expand Down Expand Up @@ -89,6 +92,92 @@ module.exports.fogSettings = {
return /^(-1|[1-9][0-9]*)$/.test(text) ? Number(text) : undefined;
},
reload: true
},

// Issue #201: the readers below used to call Setting.findOne themselves.
// Each normalize() and default copies the rule of the reader it replaced,
// so no stored value, missing record or bad value changes a decision. The
// test in test/helpers/settings-catalog.test.js holds the old rules.

// How api/helpers/agent/resolve-host.js matches an enrolling agent
// (1.6 schema.php). That reader acts only on 'off'. 'log' and
// 'enforce' both keep the SMBIOS-first match, as the 1.6 enrollment path
// does with either, and a missing record reads 'log' (not 'off').
FOG_HOST_IDENTIFY_SMBIOS: {
category: 'General Settings',
description: 'How much to trust the SMBIOS identity (UUID, system serial, '
+ 'chassis asset tag) a fog-agent reports when it enrolls. Off: the MAC '
+ 'addresses alone find the host. Log or Enforce: a unique firmware '
+ 'match wins over the MAC. fog-node has no boot identity path yet, so '
+ 'Log and Enforce act the same.',
type: 'select',
options: [
{value: 'off', label: 'Off'},
{value: 'log', label: 'Log'},
{value: 'enforce', label: 'Enforce'}
],
default: 'log',
// 'off', 'log' or 'enforce', in any case and with surrounding spaces.
normalize: (value) => {
let mode = String(value).trim().toLowerCase();
return (mode === 'off' || mode === 'log' || mode === 'enforce') ? mode : undefined;
},
reload: false
},

// The deploy shortcut of api/helpers/agent/auto-approve.js (1.6 schema.php
// step 416, moved to FOG Agent by a later step). A value below 1 disables
// the shortcut, so a stored 0 is a good value and stays 0. Only a value
// that is not a number reads as the default.
FOG_AGENT_ENROLL_DEPLOY_WINDOW: {
category: 'FOG Agent',
description: 'Hours after this server completes a deploy to a host during '
+ 'which an agent presenting that host\'s firmware identity is enrolled '
+ 'without an admin approving it. A value below 1 disables the '
+ 'shortcut. fog-node does not record deploys yet, so the shortcut does '
+ 'not fire until it does.',
type: 'number',
default: 24,
// Number(), as the reader did: '' and null are 0, which disables.
normalize: (value) => {
let hours = Number(value);
return Number.isNaN(hours) ? undefined : hours;
},
reload: false
},

// The poll_interval of POST /agent/v1/poll (api/controllers/agent/poll.js,
// 1.6 schema.php step 116), in seconds.
FOG_CLIENT_CHECKIN_TIME: {
category: 'FOG Client',
description: 'This setting returns the client service checkin times to '
+ 'the server.',
type: 'number',
default: 60,
// An integer, because the Go agent's PollInterval is an int and fails to
// decode 90.5. 1.6 casts with (int) and passes 0 or a negative number
// on: the agent then keeps its own default. A missing or non-numeric
// value gives the default.
normalize: (value) => {
let interval = parseInt(value, 10);
return Number.isNaN(interval) ? undefined : interval;
},
reload: false
},

// The password reveal control of api/helpers/form-generator.js (1.6
// schema.php step 271). 1.6 defaults to on. fog-node is off when no record
// exists, and stays so.
FOG_ENABLE_SHOW_PASSWORDS: {
category: 'General Settings',
description: 'Show a control that displays a password field as plain '
+ 'text. Off when no value is set.',
type: 'checkbox',
default: false,
// FOG stores its settings as strings, so '0' and 'false' mean off. Always
// a boolean, so no stored value is bad.
normalize: (value) => !!value && value !== '0' && value !== 'false',
reload: false
}

};
68 changes: 68 additions & 0 deletions test/helpers/settings-catalog.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,72 @@ describe('Settings catalog::', () => {
it('rejects an inherited property name as unknown', async () => {
await assert.rejects(get('constructor'), /Unknown setting: constructor/);
});

// Issue #201: four readers moved onto the catalog. The HARD rule is that
// each reader makes the same decision as before, for every stored value.
// Each OLD function is the reader's own rule, copied from the code it
// replaced (the file and line are named). `row` is the Setting record, or
// null for none, as findOne answered.
describe('the four moved readers::', () => {
const NO_ROW = Symbol('no row');
const INPUTS = [NO_ROW, '', '0', 0, '1', 1, true, false, 'false', 'abc', '90.5', -5,
'OFF', ' off ', 'log', 'enforce', null, {}];

// api/helpers/agent/resolve-host.js: smbiosOff.
const oldSmbiosOff = (row) => !!row && String(row.value).trim().toLowerCase() === 'off';
// api/helpers/agent/auto-approve.js: hours (DEFAULT_WINDOW_HOURS was 24).
const oldHours = (row) => {
const window = row ? Number(row.value) : 24;
return Number.isNaN(window) ? 24 : window;
};
// api/controllers/agent/poll.js: poll_interval (DEFAULT_POLL_INTERVAL was 60).
const oldInterval = (row) => {
const interval = parseInt(row && row.value, 10);
return Number.isNaN(interval) ? 60 : interval;
};
// api/helpers/form-generator.js: showPasswords, with its settingOn rule.
const oldShowPasswords = (row) => !!row && (!!row.value && row.value !== '0' && row.value !== 'false');

const CASES = [
['FOG_HOST_IDENTIFY_SMBIOS', oldSmbiosOff, async () => (await get('FOG_HOST_IDENTIFY_SMBIOS')) === 'off'],
['FOG_AGENT_ENROLL_DEPLOY_WINDOW', oldHours, () => get('FOG_AGENT_ENROLL_DEPLOY_WINDOW')],
['FOG_CLIENT_CHECKIN_TIME', oldInterval, () => get('FOG_CLIENT_CHECKIN_TIME')],
['FOG_ENABLE_SHOW_PASSWORDS', oldShowPasswords, () => get('FOG_ENABLE_SHOW_PASSWORDS')]
];

// Setting.value is json and required, so Waterline refuses '' and null.
// The fixture row goes through the raw collection.
const coll = () => sails.getDatastore(sails.models.setting.datastore).manager.collection(sails.models.setting.tableName);
const names = CASES.map(([key]) => key);
// The records as they were, put back after (a dev database may hold some).
let before;
const wipe = () => coll().deleteMany({name: {$in: names}});
beforeEach(async () => {
before = await coll().find({name: {$in: names}}).toArray();
await wipe();
});
afterEach(async () => {
await wipe();
if (before.length) { await coll().insertMany(before); }
});

it('holds the four keys', () => {
names.forEach((key) => assert.ok(Object.hasOwn(catalog(), key), `no ${key}`));
});

for (const [key, oldRule, newRule] of CASES) {
it(`${key}: the catalog path decides as the old reader did, for every input`, async () => {
for (const input of INPUTS) {
await wipe();
let row = null;
if (input !== NO_ROW) {
await coll().insertOne({name: key, value: input});
row = {value: input};
}
const label = input === NO_ROW ? 'no row' : JSON.stringify(input);
assert.deepStrictEqual(await newRule(), oldRule(row), `${key} with ${label}`);
}
});
}
});
});
17 changes: 17 additions & 0 deletions test/integration/agent-mtls.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,23 @@ describe('Agent mTLS::', function () {
if (saved) { await sails.models.setting.create(saved); }
}
});

// Issue #201: the FOG Settings page route stores the value, and the next
// poll reads it through the settings catalog.
it('answers the poll_interval saved through POST /api/v1/setting/save', async () => {
const a = await enrolled();
const name = 'FOG_CLIENT_CHECKIN_TIME';
const saved = await sails.models.setting.findOne({name});
try {
await sails.models.setting.destroy({name});
await admin.agent.post('/api/v1/setting/save').set(...AJAX).set('X-CSRF-Token', admin.csrf)
.set('Accept', 'application/json').send({key: name, value: 120}).expect(202);
assert.strictEqual((await poll(a)).json.poll_interval, 120);
} finally {
await sails.models.setting.destroy({name});
if (saved) { await sails.models.setting.create(saved); }
}
});
});

describe('POST /agent/v1/renew', () => {
Expand Down
Loading
Loading