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: 13 additions & 0 deletions config/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@

module.exports.bootstrap = async function() {

// A test run (test/lifecycle.test.js, tools/smoke/ui.js) sets
// custom.testDatabase to the database it must use (tools/lib/test-db.js).
// Report the database the models really use, read from the live
// connection, and refuse to lift on any other one. This runs before the
// index writes below. Nothing else sets custom.testDatabase.
if (sails.config.custom.testDatabase) {
const name = User.getDatastore().manager.databaseName;
console.log(`fog-node models database: ${name}`);
if (name !== sails.config.custom.testDatabase) {
throw new Error(`Refusing to lift: the models use the database "${name}", not the test database "${sails.config.custom.testDatabase}".`);
}
}

// The ?v= value on the production bundle links (views/layouts/layout.ejs).
// A production lift rebuilds the bundles, so each lift gets a new value and
// browsers fetch the new bundle despite production's one-year cache.
Expand Down
14 changes: 5 additions & 9 deletions docs/adr/0003-audit-trail.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,8 @@ enumeration (1.6 stores none either).
- A full-page save that fails answers 302. `general/save` sets
`req.audit.outcome` (`denied` for a refused group change, `failed` for any other
error), and the seam uses it in place of the status.
- At the end of a run, the test suite (`test/lifecycle.test.js`) deletes the
`AuditLog` rows that meet both conditions:
- `createdAt` is at or after the start of the run.
- `createdBy` is `mochatestadmin` or starts with one of `PREFIXES`, or
`subjectLabel` starts with one of `PREFIXES`.
The one test that checks the `agent-<fingerprint>` fallback host name also deletes
the rows of the hosts it made, by `subjectId`. A dev server that shares the
database keeps all its other rows. The rows of anonymous denied requests and the
`auth.token.rejected` rows of the suite have no name, so they stay.
- The test suite (`test/lifecycle.test.js`) runs on `<db>_test`, never on the
configured database, and the suite empties that database before each run. It
stops with an error if the name does not end in `_test`. The suite deletes no
`AuditLog` rows itself, so a dev server that uses the configured database keeps
all its rows.
2 changes: 0 additions & 2 deletions test/integration/agent-admin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ describe('Agent admin::', function () {
agent_version: '1.0.0',
os: 'linux',
arch: 'amd64',
// A prefixed name, so the audit rows of the test are removable
// (test/lifecycle.test.js).
hostname: uniq(),
identity: {system_uuid: '', system_serial: '', board_serial: '', chassis_asset: '', macs},
csr_pem: c.pem
Expand Down
21 changes: 6 additions & 15 deletions test/integration/agent-enroll.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ describe('Agent enroll::', function () {
agent_version: 'v0.0.1',
os: 'linux',
arch: 'amd64',
// A prefixed name, so the audit rows of the test are removable
// (test/lifecycle.test.js). The fallback name test passes its own.
// The fallback name test passes its own.
hostname: uniq(),
identity: {system_uuid: '', system_serial: '', board_serial: '', chassis_asset: '', macs: []},
csr_pem: c.pem
Expand Down Expand Up @@ -190,19 +189,11 @@ describe('Agent enroll::', function () {
});

it('unknown-host: a hostname that fails the name rule or is over 15 characters becomes agent-<fingerprint>', async () => {
const hostIds = [];
try {
for (const hostname of ['bad name!', 'x'.repeat(16), '']) {
const c = await newCsr();
await enroll(c, {hostname, identity: identity({macs: [mac()]})}).expect(202);
const host = await hostOf((await rowOf(c)).host);
hostIds.push(host.id);
assert.strictEqual(host.name, 'agent-' + c.fingerprint.slice(0, 8), `hostname ${JSON.stringify(hostname)}`);
}
} finally {
// The fallback name has no suite prefix, so remove the audit rows of
// these hosts by their id (the lifecycle cleanup cannot attribute them).
await AuditLog.destroy({subjectType: 'host', subjectId: hostIds});
for (const hostname of ['bad name!', 'x'.repeat(16), '']) {
const c = await newCsr();
await enroll(c, {hostname, identity: identity({macs: [mac()]})}).expect(202);
const host = await hostOf((await rowOf(c)).host);
assert.strictEqual(host.name, 'agent-' + c.fingerprint.slice(0, 8), `hostname ${JSON.stringify(hostname)}`);
}
});

Expand Down
4 changes: 2 additions & 2 deletions test/integration/agent-mtls.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ describe('Agent mTLS::', function () {
const fixtureDir = support.useFixture();
const app = () => sails.hooks.http.app;
const AJAX = ['X-Requested-With', 'XMLHttpRequest'];
// Every name this file creates starts with PREFIX, which starts with one of
// the PREFIXES in test/lifecycle.test.js, so the audit rows are removable.
// Every name this file creates starts with PREFIX, so the names never
// collide with another test file's names in the same run.
const RUN = crypto.randomBytes(3).toString('hex');
const PREFIX = 'mcm' + RUN;
let seq = 0;
Expand Down
10 changes: 4 additions & 6 deletions test/integration/audit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const path = require('path');
const supertest = require('supertest');
const agentCsr = require('../support/agent-csr');
const support = require('../support/pki');
const {PREFIXES} = require('../lifecycle.test');

// The audit trail core (docs/adr/0003-audit-trail.md): the AuditLog model, the
// record helper, the request seam (api/hooks/audit), the sign-in events and the
Expand All @@ -14,9 +13,8 @@ const {PREFIXES} = require('../lifecycle.test');
// The seam writes after the response finishes, 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 (a denied GET by the same actor), waits for the
// sentinel row, and only then asserts the absence. Every name a test creates
// starts with a lifecycle PREFIXES entry. The correlation id is not exposed to
// a client, so no test filters on it.
// sentinel row, and only then asserts the absence. The correlation id is not
// exposed to a client, so no test filters on it.
describe('Audit trail::', function () {
this.timeout(120000);

Expand All @@ -26,9 +24,9 @@ describe('Audit trail::', function () {
const AJAX = ['X-Requested-With', 'XMLHttpRequest'];
const RUN = crypto.randomBytes(3).toString('hex');
// Users, roles and attempted sign-in names.
const PREFIX = PREFIXES[0] + '-aud' + RUN;
const PREFIX = 'mocha-aud' + RUN;
// Hosts, tokens and agent hostnames: a host name is at most 15 characters.
const SHORT = PREFIXES[1] + RUN;
const SHORT = 'mc' + RUN;
const PASSWORD = 'mocha-audit-pw';
let seq = 0;
const uniq = () => PREFIX + '-' + (seq++);
Expand Down
100 changes: 45 additions & 55 deletions test/lifecycle.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
const assert = require('assert');
const sails = require('sails');
const support = require('./support/pki');
const {freePort} = require('./support/port');
const testDb = require('../tools/lib/test-db');

// The names the suite creates. Every test names its users, hosts, tokens,
// attempted sign-in names and agent hostnames with one of the PREFIXES, and
// the suite admin below is the other actor. The `after` below removes the
// AuditLog rows that carry one of them and nothing else: the suite may run
// while a dev server on 1337 writes real audit rows to the same database
// (package.json custom-tests), and those rows are never touched.
const ADMIN = 'mochatestadmin';
const PREFIXES = ['mocha', 'mc', 'mpa', 'mph', 'testmochauser'];
// When the run started: only a row at or after it is the suite's.
let startedAt;
// The suite never runs on the configured database (tools/lib/test-db.js). It
// lifts on the same name plus `_test`, emptied before each run.
let target;

// The agent listener (api/hooks/agent-tls) needs a PKI and a free port. 8443 is
// often taken (a dev server, test:ui, test:install), so the suite asks the OS
Expand All @@ -24,66 +19,61 @@ let agentTlsPort;
// fixture. RSA-4096 generation is slow, hence the long timeout.
before(async function() {
this.timeout(180000);
startedAt = Date.now();
pkiDir = await support.fixture();
agentTlsPort = await freePort();
});

before(function(done) {
before(async function() {
this.timeout(11000);
sails.lift({
// These were PORT=1338 and sails_hooks__grunt=false in the npm script.
// That syntax fails in Windows cmd, so the options live here instead.
port: 1338,
hooks: {grunt: false},
log: {level: 'debug'},
custom: {pkiDir, agentTlsPort, agentTlsSans: support.SANS}
}, (err) => {
if (err) return done(err);
Role.findOrCreate(
{name: 'Mocha_Test_Admin'},
{
name: 'Mocha_Test_Admin',
isAdmin: true,
permissions: {}
}, (err, role) => {
if (err) return done(err);
User.findOrCreate(
{username: 'mochatestadmin'},
{
username: 'mochatestadmin',
password: 'mochatestadmin',
email: 'mochatest@admin.com',
roles: [role.id]
}, (err) => {
if (err) return done(err);
done();
}
);
}
);
target = await testDb.prepare();
await new Promise((resolve, reject) => {
sails.lift({
// These were PORT=1338 and sails_hooks__grunt=false in the npm script.
// That syntax fails in Windows cmd, so the options live here instead.
port: 1338,
hooks: {grunt: false},
log: {level: 'debug'},
datastores: {[target.datastore]: target.overrides},
// config/bootstrap.js refuses to lift on any other database than
// testDatabase, before its index writes.
custom: {pkiDir, agentTlsPort, agentTlsSans: support.SANS, testDatabase: target.database}
}, (err) => (err ? reject(err) : resolve()));
});
// The guard, before any test file runs: the database the models really
// write to, read from the live connection. A failed root hook stops mocha.
const name = User.getDatastore().manager.databaseName;
if (!name.endsWith(testDb.SUFFIX) || name === target.original) {
await new Promise((resolve) => sails.lower(() => resolve()));
throw new Error(`Refusing to run: the models use the database "${name}", which must end in ${testDb.SUFFIX} and differ from the configured database.`);
}
const role = await Role.findOrCreate(
{name: 'Mocha_Test_Admin'},
{name: 'Mocha_Test_Admin', isAdmin: true, permissions: {}}
);
await User.findOrCreate(
{username: 'mochatestadmin'},
{
username: 'mochatestadmin',
password: 'mochatestadmin',
email: 'mochatest@admin.com',
roles: [role.id]
}
);
});
after(async () => {
// The seam writes after the response finishes, so let the last write land.
await new Promise((resolve) => setTimeout(resolve, 200));
await AuditLog.destroy({and: [
{createdAt: {'>=': startedAt}},
{or: [
{createdBy: {in: [ADMIN]}},
...PREFIXES.map((prefix) => ({createdBy: {startsWith: prefix}})),
...PREFIXES.map((prefix) => ({subjectLabel: {startsWith: prefix}}))
]}
]});
await Role.destroy({name: 'Mocha_Test_Admin'});
await User.destroy({username: ADMIN});
await new Promise((resolve, reject) => sails.lower((err) => (err ? reject(err) : resolve())));
});

describe('Basic Tests ::', () => {
it('Sails did not crash', () => {
return true;
});
});

module.exports = {ADMIN, PREFIXES};
// The before() guard stops the run on a wrong database. This reports the
// database that the run used.
it('runs on the test database, not the configured one', () => {
assert.strictEqual(User.getDatastore().manager.databaseName, target.database);
});
});
4 changes: 2 additions & 2 deletions test/migrations/2.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const assert = require('assert');
const {MongoClient} = require('mongodb');
const {datastoreUrl} = require('../../tools/lib/database');
const migration = require('../../tools/migrate/lib/migration');
const revision2 = require('../../migrations/2');

Expand All @@ -18,8 +19,7 @@ describe('Schema revision 2::', () => {
const url = new URL(ds.url);
return {url: ds.url, name: `${decodeURIComponent(url.pathname.slice(1)) || 'fog'}_migration_test`};
}
const auth = ds.user ? `${ds.user}:${ds.password || ''}@` : '';
return {url: `mongodb://${auth}${ds.host}:${ds.port}/${ds.database}`, name: `${decodeURIComponent(ds.database)}_migration_test`};
return {url: datastoreUrl(ds), name: `${decodeURIComponent(ds.database)}_migration_test`};
}

const run = (from, to) => new Promise((resolve, reject) => {
Expand Down
87 changes: 87 additions & 0 deletions test/setup/test-db.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const assert = require('assert');
const {resolve} = require('../../tools/lib/test-db');
const {datastoreUrl} = require('../../tools/lib/database');

// The test database name (tools/lib/test-db.js). The URLs here are fake.
describe('Test database name::', () => {
it('appends _test to the url database and keeps host, port and query', () => {
const t = resolve({url: 'mongodb://127.0.0.1:27017/fog?replicaSet=rs0'});
assert.strictEqual(t.original, 'fog');
assert.strictEqual(t.database, 'fog_test');
assert.strictEqual(t.url, 'mongodb://127.0.0.1:27017/fog_test?replicaSet=rs0');
assert.deepStrictEqual(t.overrides, {url: t.url, database: 'fog_test'});
});

// sails-mongo lets a top-level `database` win over the url, so a url-only
// override would leave the run on the configured database.
it('overrides the top-level database of a url datastore, and names the configured one by it', () => {
const same = resolve({url: 'mongodb://h:1/fog', database: 'fog'});
assert.deepStrictEqual(same.overrides, {url: 'mongodb://h:1/fog_test', database: 'fog_test'});
const other = resolve({url: 'mongodb://h:1/fog', database: 'fog_test'});
assert.strictEqual(other.original, 'fog_test');
assert.strictEqual(other.database, 'fog_test_test');
assert.deepStrictEqual(other.overrides, {url: 'mongodb://h:1/fog_test_test', database: 'fog_test_test'});
});

it('appends the suffix again to a name that already ends in _test', () => {
const t = resolve({url: 'mongodb://127.0.0.1:27017/fog_test'});
assert.strictEqual(t.database, 'fog_test_test');
assert.notStrictEqual(t.database, t.original);
});

it('never returns the configured name', () => {
for (const name of ['fog', 'fog_test', 'fog_mtls', '_test', 'a_test_test']) {
const url = resolve({url: `mongodb://h:1/${name}`});
const parts = resolve({host: 'h', port: 1, database: name});
for (const t of [url, parts]) {
assert.notStrictEqual(t.database, t.original);
assert.strictEqual(t.original, name);
assert.ok(t.database.endsWith('_test'));
}
}
});

it('adds authSource=<configured database> when the url has credentials and no authSource', () => {
assert.strictEqual(resolve({url: 'mongodb://fakeuser:fakepass@h:1/fog'}).url,
'mongodb://fakeuser:fakepass@h:1/fog_test?authSource=fog');
assert.strictEqual(resolve({url: 'mongodb://fakeuser:fakepass@h:1/fog?tls=true'}).url,
'mongodb://fakeuser:fakepass@h:1/fog_test?tls=true&authSource=fog');
});

it('keeps an authSource that the url has, and adds none without credentials', () => {
assert.strictEqual(resolve({url: 'mongodb://fakeuser:fakepass@h:1/fog?authSource=admin'}).url,
'mongodb://fakeuser:fakepass@h:1/fog_test?authSource=admin');
assert.ok(!resolve({url: 'mongodb://h:1/fog'}).url.includes('authSource'));
});

it('builds the target from host, port and database (the installer shape)', () => {
const t = resolve({host: '10.0.0.5', port: 27017, database: 'fog'});
assert.strictEqual(t.original, 'fog');
assert.strictEqual(t.database, 'fog_test');
assert.strictEqual(t.url, 'mongodb://10.0.0.5:27017/fog_test');
assert.deepStrictEqual(t.overrides, {url: t.url, database: 'fog_test'});
});

it('adds the credentials and authSource in the installer shape, and decodes the name', () => {
const t = resolve({host: 'h', port: 1, user: 'fakeuser', password: 'fake%40pass', database: 'my%20fog'});
assert.strictEqual(t.original, 'my fog');
assert.strictEqual(t.database, 'my fog_test');
assert.strictEqual(t.url, 'mongodb://fakeuser:fake%40pass@h:1/my%20fog_test?authSource=my%20fog');
assert.strictEqual(t.overrides.database, 'my%20fog_test');
});

it('throws when there is no database name', () => {
assert.throws(() => resolve({url: 'mongodb://h:1'}));
assert.throws(() => resolve({url: 'mongodb://h:1/'}));
assert.throws(() => resolve({host: 'h'}));
});

// The installer-shape URL (tools/lib/database.js datastoreUrl), as
// sails-mongo builds it: no `user:@` without a password, no `:undefined`
// without a port.
it('builds the installer-shape url without an empty password or a missing port', () => {
assert.strictEqual(datastoreUrl({host: 'h', database: 'd', user: 'u'}), 'mongodb://u@h/d');
assert.strictEqual(datastoreUrl({host: 'h', database: 'd'}), 'mongodb://h/d');
assert.strictEqual(datastoreUrl({host: 'h', port: 1, database: 'd', user: 'u', password: 'p'}), 'mongodb://u:p@h:1/d');
});
});
24 changes: 14 additions & 10 deletions tools/lib/database.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
const MongoClient = require('mongodb').MongoClient;

// The URL of a datastore in the installer shape: {host, port, database, user,
// password} (tools/setup/index.js). The installer writes each value
// URL-encoded, and sails-mongo puts them in its URL as given
// (normalize-datastore-config), so this does the same, with the same rules: a
// port only when set, and credentials only when a username was provided. An
// empty auth part makes the driver attempt authentication with blank
// credentials, which fails against a MongoDB with no authentication.
function datastoreUrl(ds) {
const auth = ds.user ? `${ds.user}${ds.password ? ':' + ds.password : ''}@` : '';
return `mongodb://${auth}${ds.host}${ds.port ? ':' + ds.port : ''}/${ds.database}`;
}

module.exports = {
datastoreUrl,
// `datastore` is a fogdb datastore object, as config/local.js holds it:
// {url} as `npm run setup:dev` writes it, or {host, port, database, user,
// password} as the installer writes it (tools/setup/index.js). The callback
// gets the MongoClient; client.db() is the database.
connect: (datastore, next) => {
let dbUri = datastore.url;
if (!dbUri) {
// The installer writes each value URL-encoded, and sails-mongo puts
// them in its URL as given (normalize-datastore-config), so this does
// the same. Credentials only when a username was provided: an empty
// auth part makes the driver attempt authentication with blank
// credentials, which fails against a MongoDB with no authentication.
let auth = datastore.user ? `${datastore.user}:${datastore.password || ''}@` : '';
dbUri = `mongodb://${auth}${datastore.host}:${datastore.port}/${datastore.database}`;
}
let dbUri = datastore.url || datastoreUrl(datastore);
let opts = {
useUnifiedTopology: true,
useNewUrlParser: true,
Expand Down
Loading
Loading