From 25b7af30b813f257d4d603d0260ed5723f40bf0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nuno=20G=C3=B3is?= Date: Thu, 23 Feb 2023 10:49:01 +0000 Subject: [PATCH] feat: store login events (#3185) ## About the changes https://linear.app/unleash/issue/2-705/store-sign-on-events Adds a migration for a new `login_events` table. Also adds the new `loginEventLog` feature flag. Relates to [roadmap](https://github.com/orgs/Unleash/projects/10) item: #2951 --------- Co-authored-by: Christopher Kolstad --- .../__snapshots__/create-config.test.ts.snap | 2 ++ src/lib/types/experimental.ts | 4 +++ .../20230222084211-add-login-events-table.js | 27 +++++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 src/migrations/20230222084211-add-login-events-table.js diff --git a/src/lib/__snapshots__/create-config.test.ts.snap b/src/lib/__snapshots__/create-config.test.ts.snap index 1e9e7e484b..eaf23ee16f 100644 --- a/src/lib/__snapshots__/create-config.test.ts.snap +++ b/src/lib/__snapshots__/create-config.test.ts.snap @@ -74,6 +74,7 @@ exports[`should create default config 1`] = ` "embedProxy": true, "embedProxyFrontend": true, "featuresExportImport": false, + "loginEventLog": false, "maintenance": false, "maintenanceMode": false, "messageBanner": false, @@ -96,6 +97,7 @@ exports[`should create default config 1`] = ` "embedProxy": true, "embedProxyFrontend": true, "featuresExportImport": false, + "loginEventLog": false, "maintenance": false, "maintenanceMode": false, "messageBanner": false, diff --git a/src/lib/types/experimental.ts b/src/lib/types/experimental.ts index c16b28931d..f2b5d844f6 100644 --- a/src/lib/types/experimental.ts +++ b/src/lib/types/experimental.ts @@ -67,6 +67,10 @@ const flags = { false, ), notifications: parseEnvVarBoolean(process.env.NOTIFICATIONS, false), + loginEventLog: parseEnvVarBoolean( + process.env.UNLEASH_LOGIN_EVENT_LOG, + false, + ), }; export const defaultExperimentalOptions: IExperimentalOptions = { diff --git a/src/migrations/20230222084211-add-login-events-table.js b/src/migrations/20230222084211-add-login-events-table.js new file mode 100644 index 0000000000..f369fdba0a --- /dev/null +++ b/src/migrations/20230222084211-add-login-events-table.js @@ -0,0 +1,27 @@ +exports.up = function (db, cb) { + db.runSql( + ` + CREATE TABLE IF NOT EXISTS login_events ( + id SERIAL PRIMARY KEY NOT NULL, + username TEXT NOT NULL, + auth_type TEXT NOT NULL, + created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), + successful BOOLEAN NOT NULL, + ip INET, + failure_reason TEXT + ); + CREATE INDEX IF NOT EXISTS login_events_ip_idx ON login_events(ip); + `, + cb, + ); +}; + +exports.down = function (db, cb) { + db.runSql( + ` + DROP INDEX IF EXISTS login_events_ip_idx; + DROP TABLE IF EXISTS login_events; + `, + cb, + ); +};