Set Redis initialisation back to how it was before I started messing with it.
This commit is contained in:
parent
d6eb2b9452
commit
4c7c10b121
|
@ -37,7 +37,6 @@ export { SearchParams } from "./db"
|
||||||
// circular dependencies
|
// circular dependencies
|
||||||
import * as context from "./context"
|
import * as context from "./context"
|
||||||
import * as _tenancy from "./tenancy"
|
import * as _tenancy from "./tenancy"
|
||||||
import * as redis from "./redis"
|
|
||||||
export const tenancy = {
|
export const tenancy = {
|
||||||
..._tenancy,
|
..._tenancy,
|
||||||
...context,
|
...context,
|
||||||
|
@ -52,7 +51,6 @@ export * from "./constants"
|
||||||
// expose package init function
|
// expose package init function
|
||||||
import * as db from "./db"
|
import * as db from "./db"
|
||||||
|
|
||||||
export const init = async (opts: any = {}) => {
|
export const init = (opts: any = {}) => {
|
||||||
db.init(opts.db)
|
db.init(opts.db)
|
||||||
await redis.init()
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,5 @@ export { default as Client } from "./redis"
|
||||||
export * as utils from "./utils"
|
export * as utils from "./utils"
|
||||||
export * as clients from "./init"
|
export * as clients from "./init"
|
||||||
export * as locks from "./redlockImpl"
|
export * as locks from "./redlockImpl"
|
||||||
export * from "./init"
|
export * as invite from "./invite"
|
||||||
|
export * as passwordReset from "./passwordReset"
|
||||||
export * from "./invite"
|
|
||||||
export * from "./passwordReset"
|
|
||||||
|
|
|
@ -7,11 +7,9 @@ let userClient: Client,
|
||||||
cacheClient: Client,
|
cacheClient: Client,
|
||||||
writethroughClient: Client,
|
writethroughClient: Client,
|
||||||
lockClient: Client,
|
lockClient: Client,
|
||||||
socketClient: Client,
|
socketClient: Client
|
||||||
inviteClient: Client,
|
|
||||||
passwordResetClient: Client
|
|
||||||
|
|
||||||
export async function init() {
|
async function init() {
|
||||||
userClient = await new Client(utils.Databases.USER_CACHE).init()
|
userClient = await new Client(utils.Databases.USER_CACHE).init()
|
||||||
sessionClient = await new Client(utils.Databases.SESSIONS).init()
|
sessionClient = await new Client(utils.Databases.SESSIONS).init()
|
||||||
appClient = await new Client(utils.Databases.APP_METADATA).init()
|
appClient = await new Client(utils.Databases.APP_METADATA).init()
|
||||||
|
@ -22,8 +20,6 @@ export async function init() {
|
||||||
utils.Databases.SOCKET_IO,
|
utils.Databases.SOCKET_IO,
|
||||||
utils.SelectableDatabase.SOCKET_IO
|
utils.SelectableDatabase.SOCKET_IO
|
||||||
).init()
|
).init()
|
||||||
inviteClient = await new Client(utils.Databases.INVITATIONS).init()
|
|
||||||
passwordResetClient = await new Client(utils.Databases.PW_RESETS).init()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function shutdown() {
|
export async function shutdown() {
|
||||||
|
@ -34,8 +30,6 @@ export async function shutdown() {
|
||||||
if (writethroughClient) await writethroughClient.finish()
|
if (writethroughClient) await writethroughClient.finish()
|
||||||
if (lockClient) await lockClient.finish()
|
if (lockClient) await lockClient.finish()
|
||||||
if (socketClient) await socketClient.finish()
|
if (socketClient) await socketClient.finish()
|
||||||
if (inviteClient) await inviteClient.finish()
|
|
||||||
if (passwordResetClient) await passwordResetClient.finish()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
process.on("exit", async () => {
|
process.on("exit", async () => {
|
||||||
|
@ -90,17 +84,3 @@ export async function getSocketClient() {
|
||||||
}
|
}
|
||||||
return socketClient
|
return socketClient
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getInviteClient() {
|
|
||||||
if (!inviteClient) {
|
|
||||||
await init()
|
|
||||||
}
|
|
||||||
return inviteClient
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function getPasswordResetClient() {
|
|
||||||
if (!passwordResetClient) {
|
|
||||||
await init()
|
|
||||||
}
|
|
||||||
return passwordResetClient
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import { utils, tenancy } from "../"
|
import { utils, tenancy, redis } from "../"
|
||||||
import env from "../environment"
|
import env from "../environment"
|
||||||
import { getInviteClient } from "./init"
|
|
||||||
|
|
||||||
const TTL_SECONDS = 60 * 60 * 24 * 7
|
const TTL_SECONDS = 60 * 60 * 24 * 7
|
||||||
|
|
||||||
|
@ -13,13 +12,25 @@ interface InviteWithCode extends Invite {
|
||||||
code: string
|
code: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let client: redis.Client
|
||||||
|
|
||||||
|
export async function init() {
|
||||||
|
if (!client) {
|
||||||
|
client = new redis.Client(redis.utils.Databases.INVITATIONS)
|
||||||
|
}
|
||||||
|
return client
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function shutdown() {
|
||||||
|
if (client) await client.finish()
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Given an invite code and invite body, allow the update an existing/valid invite in redis
|
* Given an invite code and invite body, allow the update an existing/valid invite in redis
|
||||||
* @param inviteCode The invite code for an invite in redis
|
* @param inviteCode The invite code for an invite in redis
|
||||||
* @param value The body of the updated user invitation
|
* @param value The body of the updated user invitation
|
||||||
*/
|
*/
|
||||||
export async function updateInviteCode(code: string, value: Invite) {
|
export async function updateCode(code: string, value: Invite) {
|
||||||
const client = await getInviteClient()
|
|
||||||
await client.store(code, value, TTL_SECONDS)
|
await client.store(code, value, TTL_SECONDS)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,11 +40,7 @@ export async function updateInviteCode(code: string, value: Invite) {
|
||||||
* @param info Information to be carried along with the invitation.
|
* @param info Information to be carried along with the invitation.
|
||||||
* @return returns the code that was stored to redis.
|
* @return returns the code that was stored to redis.
|
||||||
*/
|
*/
|
||||||
export async function createInviteCode(
|
export async function createCode(email: string, info: any): Promise<string> {
|
||||||
email: string,
|
|
||||||
info: any
|
|
||||||
): Promise<string> {
|
|
||||||
const client = await getInviteClient()
|
|
||||||
const code = utils.newid()
|
const code = utils.newid()
|
||||||
await client.store(code, { email, info }, TTL_SECONDS)
|
await client.store(code, { email, info }, TTL_SECONDS)
|
||||||
return code
|
return code
|
||||||
|
@ -44,8 +51,7 @@ export async function createInviteCode(
|
||||||
* @param inviteCode the invite code that was provided as part of the link.
|
* @param inviteCode the invite code that was provided as part of the link.
|
||||||
* @return If the code is valid then an email address will be returned.
|
* @return If the code is valid then an email address will be returned.
|
||||||
*/
|
*/
|
||||||
export async function getInviteCode(code: string): Promise<Invite> {
|
export async function getCode(code: string): Promise<Invite> {
|
||||||
const client = await getInviteClient()
|
|
||||||
const value = (await client.get(code)) as Invite | undefined
|
const value = (await client.get(code)) as Invite | undefined
|
||||||
if (!value) {
|
if (!value) {
|
||||||
throw "Invitation is not valid or has expired, please request a new one."
|
throw "Invitation is not valid or has expired, please request a new one."
|
||||||
|
@ -53,8 +59,7 @@ export async function getInviteCode(code: string): Promise<Invite> {
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function deleteInviteCode(code: string) {
|
export async function deleteCode(code: string) {
|
||||||
const client = await getInviteClient()
|
|
||||||
await client.delete(code)
|
await client.delete(code)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,7 +67,6 @@ export async function deleteInviteCode(code: string) {
|
||||||
Get all currently available user invitations for the current tenant.
|
Get all currently available user invitations for the current tenant.
|
||||||
**/
|
**/
|
||||||
export async function getInviteCodes(): Promise<InviteWithCode[]> {
|
export async function getInviteCodes(): Promise<InviteWithCode[]> {
|
||||||
const client = await getInviteClient()
|
|
||||||
const invites: { key: string; value: Invite }[] = await client.scan()
|
const invites: { key: string; value: Invite }[] = await client.scan()
|
||||||
|
|
||||||
const results: InviteWithCode[] = invites.map(invite => {
|
const results: InviteWithCode[] = invites.map(invite => {
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
import { utils } from "../"
|
import { redis, utils } from "../"
|
||||||
import { getPasswordResetClient } from "./init"
|
|
||||||
|
|
||||||
const TTL_SECONDS = 60 * 60
|
const TTL_SECONDS = 60 * 60
|
||||||
|
|
||||||
|
@ -8,6 +7,19 @@ interface PasswordReset {
|
||||||
info: any
|
info: any
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let client: redis.Client
|
||||||
|
|
||||||
|
export async function init() {
|
||||||
|
if (!client) {
|
||||||
|
client = new redis.Client(redis.utils.Databases.PW_RESETS)
|
||||||
|
}
|
||||||
|
return client
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function shutdown() {
|
||||||
|
if (client) await client.finish()
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Given a user ID this will store a code (that is returned) for an hour in redis.
|
* Given a user ID this will store a code (that is returned) for an hour in redis.
|
||||||
* The user can then return this code for resetting their password (through their reset link).
|
* The user can then return this code for resetting their password (through their reset link).
|
||||||
|
@ -15,11 +27,7 @@ interface PasswordReset {
|
||||||
* @param info Info about the user/the reset process.
|
* @param info Info about the user/the reset process.
|
||||||
* @return returns the code that was stored to redis.
|
* @return returns the code that was stored to redis.
|
||||||
*/
|
*/
|
||||||
export async function createResetPasswordCode(
|
export async function createCode(userId: string, info: any): Promise<string> {
|
||||||
userId: string,
|
|
||||||
info: any
|
|
||||||
): Promise<string> {
|
|
||||||
const client = await getPasswordResetClient()
|
|
||||||
const code = utils.newid()
|
const code = utils.newid()
|
||||||
await client.store(code, { userId, info }, TTL_SECONDS)
|
await client.store(code, { userId, info }, TTL_SECONDS)
|
||||||
return code
|
return code
|
||||||
|
@ -30,10 +38,7 @@ export async function createResetPasswordCode(
|
||||||
* @param code The code provided via the email link.
|
* @param code The code provided via the email link.
|
||||||
* @return returns the user ID if it is found
|
* @return returns the user ID if it is found
|
||||||
*/
|
*/
|
||||||
export async function getResetPasswordCode(
|
export async function getCode(code: string): Promise<PasswordReset> {
|
||||||
code: string
|
|
||||||
): Promise<PasswordReset> {
|
|
||||||
const client = await getPasswordResetClient()
|
|
||||||
const value = (await client.get(code)) as PasswordReset | undefined
|
const value = (await client.get(code)) as PasswordReset | undefined
|
||||||
if (!value) {
|
if (!value) {
|
||||||
throw "Provided information is not valid, cannot reset password - please try again."
|
throw "Provided information is not valid, cannot reset password - please try again."
|
||||||
|
|
|
@ -308,7 +308,7 @@ export const checkInvite = async (ctx: any) => {
|
||||||
const { code } = ctx.params
|
const { code } = ctx.params
|
||||||
let invite
|
let invite
|
||||||
try {
|
try {
|
||||||
invite = await redis.getInviteCode(code)
|
invite = await redis.invite.getCode(code)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.warn("Error getting invite from code", e)
|
console.warn("Error getting invite from code", e)
|
||||||
ctx.throw(400, "There was a problem with the invite")
|
ctx.throw(400, "There was a problem with the invite")
|
||||||
|
@ -322,7 +322,7 @@ export const checkInvite = async (ctx: any) => {
|
||||||
export const getUserInvites = async (ctx: any) => {
|
export const getUserInvites = async (ctx: any) => {
|
||||||
try {
|
try {
|
||||||
// Restricted to the currently authenticated tenant
|
// Restricted to the currently authenticated tenant
|
||||||
ctx.body = await redis.getInviteCodes()
|
ctx.body = await redis.invite.getInviteCodes()
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
ctx.throw(400, "There was a problem fetching invites")
|
ctx.throw(400, "There was a problem fetching invites")
|
||||||
}
|
}
|
||||||
|
@ -336,7 +336,7 @@ export const updateInvite = async (ctx: any) => {
|
||||||
|
|
||||||
let invite
|
let invite
|
||||||
try {
|
try {
|
||||||
invite = await redis.getInviteCode(code)
|
invite = await redis.invite.getCode(code)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
ctx.throw(400, "There was a problem with the invite")
|
ctx.throw(400, "There was a problem with the invite")
|
||||||
return
|
return
|
||||||
|
@ -364,7 +364,7 @@ export const updateInvite = async (ctx: any) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await redis.updateInviteCode(code, updated)
|
await redis.invite.updateCode(code, updated)
|
||||||
ctx.body = { ...invite }
|
ctx.body = { ...invite }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -374,8 +374,8 @@ export const inviteAccept = async (
|
||||||
const { inviteCode, password, firstName, lastName } = ctx.request.body
|
const { inviteCode, password, firstName, lastName } = ctx.request.body
|
||||||
try {
|
try {
|
||||||
// info is an extension of the user object that was stored by global
|
// info is an extension of the user object that was stored by global
|
||||||
const { email, info }: any = await redis.getInviteCode(inviteCode)
|
const { email, info }: any = await redis.invite.getCode(inviteCode)
|
||||||
await redis.deleteInviteCode(inviteCode)
|
await redis.invite.deleteCode(inviteCode)
|
||||||
const user = await tenancy.doInTenant(info.tenantId, async () => {
|
const user = await tenancy.doInTenant(info.tenantId, async () => {
|
||||||
let request: any = {
|
let request: any = {
|
||||||
firstName,
|
firstName,
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import * as core from "@budibase/backend-core"
|
import * as core from "@budibase/backend-core"
|
||||||
import env from "../environment"
|
import env from "../environment"
|
||||||
|
|
||||||
export async function init() {
|
export function init() {
|
||||||
const dbConfig: any = {
|
const dbConfig: any = {
|
||||||
replication: true,
|
replication: true,
|
||||||
find: true,
|
find: true,
|
||||||
|
@ -12,5 +12,5 @@ export async function init() {
|
||||||
dbConfig.allDbs = true
|
dbConfig.allDbs = true
|
||||||
}
|
}
|
||||||
|
|
||||||
await core.init({ db: dbConfig })
|
core.init({ db: dbConfig })
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,8 +16,9 @@ import {
|
||||||
queue,
|
queue,
|
||||||
env as coreEnv,
|
env as coreEnv,
|
||||||
timers,
|
timers,
|
||||||
|
redis,
|
||||||
} from "@budibase/backend-core"
|
} from "@budibase/backend-core"
|
||||||
|
db.init()
|
||||||
import Koa from "koa"
|
import Koa from "koa"
|
||||||
import koaBody from "koa-body"
|
import koaBody from "koa-body"
|
||||||
import http from "http"
|
import http from "http"
|
||||||
|
@ -71,6 +72,8 @@ server.on("close", async () => {
|
||||||
shuttingDown = true
|
shuttingDown = true
|
||||||
console.log("Server Closed")
|
console.log("Server Closed")
|
||||||
timers.cleanup()
|
timers.cleanup()
|
||||||
|
await redis.invite.shutdown()
|
||||||
|
await redis.passwordReset.shutdown()
|
||||||
await events.shutdown()
|
await events.shutdown()
|
||||||
await queue.shutdown()
|
await queue.shutdown()
|
||||||
if (!env.isTest()) {
|
if (!env.isTest()) {
|
||||||
|
@ -85,8 +88,9 @@ const shutdown = () => {
|
||||||
|
|
||||||
export default server.listen(parseInt(env.PORT || "4002"), async () => {
|
export default server.listen(parseInt(env.PORT || "4002"), async () => {
|
||||||
console.log(`Worker running on ${JSON.stringify(server.address())}`)
|
console.log(`Worker running on ${JSON.stringify(server.address())}`)
|
||||||
await db.init()
|
|
||||||
await initPro()
|
await initPro()
|
||||||
|
await redis.invite.init()
|
||||||
|
await redis.passwordReset.init()
|
||||||
// configure events to use the pro audit log write
|
// configure events to use the pro audit log write
|
||||||
// can't integrate directly into backend-core due to cyclic issues
|
// can't integrate directly into backend-core due to cyclic issues
|
||||||
await events.processors.init(proSdk.auditLogs.write)
|
await events.processors.init(proSdk.auditLogs.write)
|
||||||
|
|
|
@ -73,7 +73,7 @@ export const reset = async (email: string) => {
|
||||||
* Perform the user password update if the provided reset code is valid.
|
* Perform the user password update if the provided reset code is valid.
|
||||||
*/
|
*/
|
||||||
export const resetUpdate = async (resetCode: string, password: string) => {
|
export const resetUpdate = async (resetCode: string, password: string) => {
|
||||||
const { userId } = await redis.getResetPasswordCode(resetCode)
|
const { userId } = await redis.passwordReset.getCode(resetCode)
|
||||||
|
|
||||||
let user = await userSdk.db.getUser(userId)
|
let user = await userSdk.db.getUser(userId)
|
||||||
user.password = password
|
user.password = password
|
||||||
|
|
|
@ -7,6 +7,7 @@ mocks.licenses.init(mocks.pro)
|
||||||
mocks.licenses.useUnlimited()
|
mocks.licenses.useUnlimited()
|
||||||
|
|
||||||
import * as dbConfig from "../db"
|
import * as dbConfig from "../db"
|
||||||
|
dbConfig.init()
|
||||||
import env from "../environment"
|
import env from "../environment"
|
||||||
import * as controllers from "./controllers"
|
import * as controllers from "./controllers"
|
||||||
const supertest = require("supertest")
|
const supertest = require("supertest")
|
||||||
|
@ -108,7 +109,6 @@ class TestConfiguration {
|
||||||
// SETUP / TEARDOWN
|
// SETUP / TEARDOWN
|
||||||
|
|
||||||
async beforeAll() {
|
async beforeAll() {
|
||||||
await dbConfig.init()
|
|
||||||
try {
|
try {
|
||||||
await this.createDefaultUser()
|
await this.createDefaultUser()
|
||||||
await this.createSession(this.user!)
|
await this.createSession(this.user!)
|
||||||
|
|
|
@ -3,7 +3,6 @@ import { EmailTemplatePurpose, TemplateType } from "../constants"
|
||||||
import { getTemplateByPurpose, EmailTemplates } from "../constants/templates"
|
import { getTemplateByPurpose, EmailTemplates } from "../constants/templates"
|
||||||
import { getSettingsTemplateContext } from "./templates"
|
import { getSettingsTemplateContext } from "./templates"
|
||||||
import { processString } from "@budibase/string-templates"
|
import { processString } from "@budibase/string-templates"
|
||||||
import { createResetPasswordCode } from "@budibase/backend-core/src/redis/passwordReset"
|
|
||||||
import { redis } from "@budibase/backend-core"
|
import { redis } from "@budibase/backend-core"
|
||||||
import { User, SendEmailOpts, SMTPInnerConfig } from "@budibase/types"
|
import { User, SendEmailOpts, SMTPInnerConfig } from "@budibase/types"
|
||||||
import { configs } from "@budibase/backend-core"
|
import { configs } from "@budibase/backend-core"
|
||||||
|
@ -62,9 +61,9 @@ async function getLinkCode(
|
||||||
) {
|
) {
|
||||||
switch (purpose) {
|
switch (purpose) {
|
||||||
case EmailTemplatePurpose.PASSWORD_RECOVERY:
|
case EmailTemplatePurpose.PASSWORD_RECOVERY:
|
||||||
return createResetPasswordCode(user._id!, info)
|
return redis.passwordReset.createCode(user._id!, info)
|
||||||
case EmailTemplatePurpose.INVITATION:
|
case EmailTemplatePurpose.INVITATION:
|
||||||
return redis.createInviteCode(email, info)
|
return redis.invite.createCode(email, info)
|
||||||
default:
|
default:
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue