From 1ad4146db29062708df7311f20b2489eba9d9bef Mon Sep 17 00:00:00 2001 From: Sebastien DUMETZ Date: Fri, 18 Oct 2024 11:59:15 +0200 Subject: [PATCH] create a new route: GET /admin/config. add data about currently running build --- Dockerfile | 16 ++++-- source/server/routes/admin/config/get.test.ts | 56 +++++++++++++++++++ source/server/routes/admin/config/get.ts | 19 +++++++ source/server/routes/admin/index.ts | 2 + source/server/utils/config.ts | 1 + 5 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 source/server/routes/admin/config/get.test.ts create mode 100644 source/server/routes/admin/config/get.ts diff --git a/Dockerfile b/Dockerfile index d0bdeb8a..a1fb76db 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,11 @@ +ARG PORT +ARG PUBLIC +ARG BUILD_REF + +################### +# Build source +################### FROM node:16-alpine as build RUN mkdir -p /app/dist /app/source WORKDIR /app @@ -31,14 +38,15 @@ RUN npm run build-ui # The actual container to be published ################### FROM node:16-alpine -LABEL org.opencontainers.image.source=https://github.com/Holusion/e-thesaurus +LABEL org.opencontainers.image.source=https://github.com/Holusion/eCorpus LABEL org.opencontainers.image.description="eCorpus base image" LABEL org.opencontainers.image.licenses=Apache -ARG PORT=8000 -ENV PUBLIC=false -ENV PORT=${PORT} +ENV PUBLIC=${PUBLIC:-false} +ENV PORT=${PORT:-8000} +ENV BUILD_REF=${BUILD_REF:-unknown} + ENV NODE_ENV=production WORKDIR /app diff --git a/source/server/routes/admin/config/get.test.ts b/source/server/routes/admin/config/get.test.ts new file mode 100644 index 00000000..e66a093e --- /dev/null +++ b/source/server/routes/admin/config/get.test.ts @@ -0,0 +1,56 @@ +import { randomBytes } from "crypto"; +import request from "supertest"; +import User from "../../../auth/User.js"; +import UserManager from "../../../auth/UserManager.js"; +import Vfs from "../../../vfs/index.js"; + + + +/** + * Minimal tests as most + */ + +describe("GET /admin/config", function(){ + let vfs :Vfs, userManager :UserManager, user :User, admin :User; + + this.beforeAll(async function(){ + let locals = await createIntegrationContext(this); + vfs = locals.vfs; + userManager = locals.userManager; + user = await userManager.addUser("bob", "12345678"); + admin = await userManager.addUser("alice", "12345678", true); + }); + + this.afterAll(async function(){ + await cleanIntegrationContext(this); + }); + + it("requires admin access", async function(){ + //Anonymous + await request(this.server).get(`/admin/config`) + .expect(401); + //read-only User + await request(this.server).get(`/admin/config`) + .auth(user.username, "12345678") + .expect(401); + + await request(this.server).get(`/admin/config`) + .auth(admin.username, "12345678") + .expect(200); + }); + + it("can return text/plain env file", async function(){ + await request(this.server).get(`/admin/config`) + .auth(admin.username, "12345678") + .accept("text/plain") + .expect(200) + .expect("Content-Type", "text/plain; charset=utf-8"); + }); + it("can return application/json", async function(){ + await request(this.server).get(`/admin/config`) + .auth(admin.username, "12345678") + .accept("application/json") + .expect(200) + .expect("Content-Type", "application/json; charset=utf-8"); + }); +}); \ No newline at end of file diff --git a/source/server/routes/admin/config/get.ts b/source/server/routes/admin/config/get.ts new file mode 100644 index 00000000..4347a7d5 --- /dev/null +++ b/source/server/routes/admin/config/get.ts @@ -0,0 +1,19 @@ +import { Request, Response } from "express"; +import { getLocals, getVfs } from "../../../utils/locals.js"; + + + +export default async function handleGetConfig(req :Request, res :Response){ + let {config} = getLocals(req); + res.status(200) + res.format({ + "application/json": ()=>{ + res.send(config); + }, + "text/plain": ()=>{ + res.send(Object.entries(config).map(([key, value])=>{ + return `${key.toUpperCase()}="${value}"` + }).join("\n")); + } + }) +} \ No newline at end of file diff --git a/source/server/routes/admin/index.ts b/source/server/routes/admin/index.ts index 3ebef2a9..49b72ac9 100644 --- a/source/server/routes/admin/index.ts +++ b/source/server/routes/admin/index.ts @@ -5,6 +5,7 @@ import { isAdministrator } from "../../utils/locals.js"; import wrap from "../../utils/wrapAsync.js"; import handleGetStats from "./stats/index.js"; import handleMailtest from "./mailtest.js"; +import handleGetConfig from "./config/get.js"; const router = Router(); @@ -21,6 +22,7 @@ router.use((req, res, next)=>{ router.get("/stats", isAdministrator, wrap(handleGetStats)); +router.get("/config", isAdministrator, wrap(handleGetConfig)); router.post("/mailtest", isAdministrator, wrap(handleMailtest)); export default router; diff --git a/source/server/utils/config.ts b/source/server/utils/config.ts index dbb12383..3eb8c6c8 100644 --- a/source/server/utils/config.ts +++ b/source/server/utils/config.ts @@ -20,6 +20,7 @@ const values = { hot_reload:[false, toBool], smart_host: ["smtp://localhost", toString], verbose: [false, toBool], + build_ref: ["unknown", toString], experimental: [false, toBool], /// FEATURE FLAGS ///