diff --git a/lerna.json b/lerna.json index 7186c0ca17..94ca18985e 100644 --- a/lerna.json +++ b/lerna.json @@ -1,5 +1,5 @@ { - "version": "2.22.16", + "version": "2.22.17", "npmClient": "yarn", "packages": [ "packages/*", diff --git a/packages/backend-core/src/users/db.ts b/packages/backend-core/src/users/db.ts index 04d3264e6f..6165a68e57 100644 --- a/packages/backend-core/src/users/db.ts +++ b/packages/backend-core/src/users/db.ts @@ -45,6 +45,7 @@ type GroupFns = { getGroupBuilderAppIds: GroupBuildersFn } type CreateAdminUserOpts = { + password?: string ssoId?: string hashPassword?: boolean requirePassword?: boolean @@ -501,9 +502,9 @@ export class UserDB { static async createAdminUser( email: string, tenantId: string, - password?: string, opts?: CreateAdminUserOpts ) { + const password = opts?.password const user: User = { email: email, password, diff --git a/packages/server/src/app.ts b/packages/server/src/app.ts index aa96a30b00..75da54cbaf 100644 --- a/packages/server/src/app.ts +++ b/packages/server/src/app.ts @@ -21,7 +21,7 @@ async function start() { app = koa.app server = koa.server // startup includes automation runner - if enabled - await startup(app, server) + await startup({ app, server }) } start().catch(err => { diff --git a/packages/server/src/startup.ts b/packages/server/src/startup/index.ts similarity index 80% rename from packages/server/src/startup.ts rename to packages/server/src/startup/index.ts index 2cedda1099..48d500a0cf 100644 --- a/packages/server/src/startup.ts +++ b/packages/server/src/startup/index.ts @@ -1,6 +1,6 @@ -import env from "./environment" -import * as redis from "./utilities/redis" -import { generateApiKey, getChecklist } from "./utilities/workerRequests" +import env from "../environment" +import * as redis from "../utilities/redis" +import { generateApiKey, getChecklist } from "../utilities/workerRequests" import { events, installation, @@ -9,22 +9,22 @@ import { users, cache, } from "@budibase/backend-core" -import fs from "fs" -import { watch } from "./watch" -import * as automations from "./automations" -import * as fileSystem from "./utilities/fileSystem" -import { default as eventEmitter, init as eventInit } from "./events" -import * as migrations from "./migrations" -import * as bullboard from "./automations/bullboard" +import { watch } from "../watch" +import * as automations from "../automations" +import * as fileSystem from "../utilities/fileSystem" +import { default as eventEmitter, init as eventInit } from "../events" +import * as migrations from "../migrations" +import * as bullboard from "../automations/bullboard" import * as pro from "@budibase/pro" -import * as api from "./api" -import sdk from "./sdk" -import { initialise as initialiseWebsockets } from "./websockets" -import { automationsEnabled, printFeatures } from "./features" +import * as api from "../api" +import sdk from "../sdk" +import { initialise as initialiseWebsockets } from "../websockets" +import { automationsEnabled, printFeatures } from "../features" +import * as jsRunner from "../jsRunner" import Koa from "koa" import { Server } from "http" import { AddressInfo } from "net" -import * as jsRunner from "./jsRunner" +import fs from "fs" let STARTUP_RAN = false @@ -61,8 +61,11 @@ function shutdown(server?: Server) { } } -export async function startup(app?: Koa, server?: Server) { - if (STARTUP_RAN) { +export async function startup( + opts: { app?: Koa; server?: Server; rerun?: boolean } = {} +) { + const { app, server, rerun } = opts + if (STARTUP_RAN && !rerun) { return } printFeatures() @@ -139,9 +142,9 @@ export async function startup(app?: Koa, server?: Server) { try { const user = await users.UserDB.createAdminUser( bbAdminEmail, - bbAdminPassword, tenantId, { + password: bbAdminPassword, hashPassword: true, requirePassword: true, skipPasswordValidation: true, diff --git a/packages/server/src/startup/tests/startup.spec.ts b/packages/server/src/startup/tests/startup.spec.ts new file mode 100644 index 0000000000..ed31bc45b7 --- /dev/null +++ b/packages/server/src/startup/tests/startup.spec.ts @@ -0,0 +1,34 @@ +import TestConfiguration from "../../tests/utilities/TestConfiguration" +import { startup } from "../index" +import { users, utils, tenancy } from "@budibase/backend-core" + +describe("check BB_ADMIN environment variables", () => { + const config = new TestConfiguration() + beforeAll(async () => { + await config.init() + }) + + it("should be able to create a user with the BB_ADMIN environment variables", async () => { + const EMAIL = "budibase@budibase.com", + PASSWORD = "budibase" + await tenancy.doInTenant(tenancy.DEFAULT_TENANT_ID, async () => { + await config.withEnv( + { + BB_ADMIN_USER_EMAIL: EMAIL, + BB_ADMIN_USER_PASSWORD: PASSWORD, + MULTI_TENANCY: "0", + SELF_HOSTED: "1", + }, + async () => { + await startup({ rerun: true }) + const user = await users.getGlobalUserByEmail(EMAIL, { + cleanup: false, + }) + expect(user).toBeDefined() + expect(user?.password).toBeDefined() + expect(await utils.compare(PASSWORD, user?.password!)).toEqual(true) + } + ) + }) + }) +}) diff --git a/packages/worker/src/api/controllers/global/users.ts b/packages/worker/src/api/controllers/global/users.ts index 0c1342fa08..4c1af90d38 100644 --- a/packages/worker/src/api/controllers/global/users.ts +++ b/packages/worker/src/api/controllers/global/users.ts @@ -146,16 +146,12 @@ export const adminUser = async ( } try { - const finalUser = await userSdk.db.createAdminUser( - email, - tenantId, + const finalUser = await userSdk.db.createAdminUser(email, tenantId, { password, - { - ssoId, - hashPassword, - requirePassword, - } - ) + ssoId, + hashPassword, + requirePassword, + }) // events let account: CloudAccount | undefined