-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add audit actions feature and audit login attempts
Refs: #171
- Loading branch information
Showing
12 changed files
with
142 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
'use strict'; | ||
|
||
const config = require('../config/config').globalConfig; | ||
const nanoPromise = require('../util/nanoPromise'); | ||
const debug = require('../util/debug')('audit:actions'); | ||
const { open } = require('../connect'); | ||
|
||
const auditEnabled = !!config.auditActions; | ||
|
||
let _globalNano = null; | ||
let _db = null; | ||
|
||
async function ensureNano() { | ||
const newGlobalNano = await open(); | ||
if (_globalNano !== newGlobalNano) { | ||
_db = newGlobalNano.db.use(config.auditActionsDb); | ||
} | ||
return _db; | ||
} | ||
|
||
async function auditAction(action, username, ip, meta) { | ||
if (!auditEnabled) return; | ||
debug('logAction', action, username, ip); | ||
validateString('action', action); | ||
validateString('username', username); | ||
validateString('ip', ip); | ||
const doc = { | ||
action, | ||
username, | ||
ip, | ||
date: new Date().toISOString() | ||
}; | ||
if (meta) { | ||
doc.meta = meta; | ||
} | ||
const db = await ensureNano(); | ||
await nanoPromise.insertDocument(db, doc); | ||
} | ||
|
||
async function auditLogin(username, success, provider, ctx) { | ||
if (!auditEnabled) return; | ||
const action = success ? 'login.success' : 'login.failed'; | ||
await auditAction(action, username, ctx.ip, { provider }); | ||
} | ||
|
||
function validateString(name, value) { | ||
if (typeof value !== 'string') { | ||
throw new TypeError(`${name} must be a string`); | ||
} | ||
} | ||
|
||
module.exports = { | ||
auditAction, | ||
auditLogin | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict'; | ||
|
||
const config = require('./config/config').globalConfig; | ||
const { open } = require('./connect'); | ||
const loadCouch = require('./util/load'); | ||
const debug = require('./util/debug')('main:initCouch'); | ||
const nanoPromise = require('./util/nanoPromise'); | ||
|
||
async function initCouch() { | ||
const nano = await open(); | ||
if (config.auditActions) { | ||
await setupAuditActions(nano); | ||
} | ||
loadCouch(); | ||
} | ||
|
||
async function setupAuditActions(nano) { | ||
debug('setup audit actions'); | ||
const auditActionsDb = config.auditActionsDb; | ||
// Check if database is accessible | ||
try { | ||
const dbExists = await nanoPromise.getDatabase(nano, auditActionsDb); | ||
if (!dbExists) { | ||
throw new Error( | ||
`audit actions database does not exist: ${auditActionsDb}` | ||
); | ||
} | ||
} catch (e) { | ||
debug.error('failed to get audit actions database: %s', auditActionsDb); | ||
throw e; | ||
} | ||
} | ||
|
||
module.exports = initCouch; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters