mirror of
https://github.com/wekan/wekan.git
synced 2026-11-25 10:48:06 -05:00
LDAP_AUTHENTIFICATION_PASSWORD_FILE and OAUTH2_SECRET_FILE were listed in the Dockerfile, snap, docker-compose and start-wekan since #5724, but no code read them, so a password could only be an environment variable. WeKan now reads the file itself, at each login (a rotated secret applies at once), so the password is never in the environment - the point of Docker / Kubernetes secrets. The order is the Admin Panel, the variable, then its file; one trailing line break is dropped. The application code is the same on every platform, so it works in Docker, the snap (files under /var/snap/wekan/common), bundles, AppImage, Flatpak, Mac and Windows. Admin Panel / People shows when the password comes from the file and when the file cannot be read, never the path or content. The path itself is an environment variable only: settable in the Admin Panel next to the LDAP host or the OAuth2 token endpoint, it would let the server read any file and send it there as the password. DEFAULT_AUTHENTICATION_METHOD is a field of the Login pane's settings form. Choosing a method overrides the variable; Default gives it back. Before, the variable rewrote the stored method at every start, so an administrator's choice was lost on restart. The published method is now the override, else the variable, else password. On upgrade, a method chosen with the old dropdown while no variable was set becomes the override, once, so it is not lost. The old dropdown is removed: two controls for one setting on one page. The snap key for the LDAP password file was spelled ldap-authentification-password-file, unlike its siblings and its help text; it is ldap-authentication-password-file now (it had no effect before). MAIL_SERVICE_PASSWORD_FILE, MONGO_PASSWORD_FILE and S3_SECRET_FILE are still not read by WeKan; secrets/README.md says so. Thanks to Roemer, CrashOverride-lab, ww-daniel-mora and xet7 !
143 lines
5.2 KiB
JavaScript
143 lines
5.2 KiB
JavaScript
import { Meteor } from 'meteor/meteor';
|
|
import { check } from 'meteor/check';
|
|
import Settings from '/models/settings';
|
|
|
|
// Run-time resolution of every login setting in models/lib/authConfigCatalog.js:
|
|
// the Admin Panel override when one is stored, otherwise the environment
|
|
// variable. authEnv(name) is a drop-in for process.env[name] - it returns the
|
|
// same string-or-undefined shape - so a caller switches by changing one
|
|
// expression and keeps its own parsing.
|
|
//
|
|
// The Settings document is held in memory and kept current by an observer.
|
|
// It has to be: these values are read synchronously, inside login handlers and
|
|
// request middleware, and in Meteor 3 a server-side Settings.findOne() throws
|
|
// ("findOne is not available on the server"). The LDAP bridge used exactly that
|
|
// call, so its Admin Panel overrides never reached a login - the exception was
|
|
// caught and the environment used instead. Until the first read completes,
|
|
// authEnv answers from the environment alone, as before.
|
|
//
|
|
// Packages (wekan-ldap, wekan-oidc, wekan-accounts-cas) cannot import this
|
|
// file, so it is also installed as globalThis.__wekanAuthEnv; each package
|
|
// falls back to process.env when it is absent (plain-Node tests).
|
|
|
|
const {
|
|
AUTH_CONFIG_SECTIONS,
|
|
resolveAuthEnv,
|
|
authConfigSources,
|
|
cleanAuthConfigInput,
|
|
} = require('/models/lib/authConfigCatalog');
|
|
|
|
const FIELDS = {};
|
|
for (const { storage } of Object.values(AUTH_CONFIG_SECTIONS)) FIELDS[storage] = 1;
|
|
|
|
let current = null;
|
|
const listeners = {};
|
|
let markReady;
|
|
// Resolves once the stored overrides have been read. A setting that is applied
|
|
// only at startup (ACCOUNTS_COMMON_LOGIN_EXPIRATION_IN_DAYS) waits for it.
|
|
export const authConfigReady = new Promise(resolve => { markReady = resolve; });
|
|
|
|
export function authEnv(name) {
|
|
return resolveAuthEnv(name, current, process.env);
|
|
}
|
|
|
|
// For code that takes a whole environment object (ldapPasswordLoginGuard,
|
|
// oauth2ClientSecretJwt): every property read goes through authEnv.
|
|
export const authEnvObject = new Proxy(process.env, {
|
|
get(target, name) {
|
|
return typeof name === 'string' ? authEnv(name) : undefined;
|
|
},
|
|
});
|
|
|
|
globalThis.__wekanAuthEnv = authEnv;
|
|
|
|
// Called after a section's settings change, and once when the settings are
|
|
// first read, so a login method can reconfigure itself without a restart.
|
|
export function onAuthConfigChange(section, fn) {
|
|
if (!AUTH_CONFIG_SECTIONS[section]) throw new Error(`Unknown login settings section: ${section}`);
|
|
(listeners[section] = listeners[section] || []).push(fn);
|
|
}
|
|
|
|
async function notify(sections) {
|
|
for (const section of sections) {
|
|
for (const fn of listeners[section] || []) {
|
|
try {
|
|
await fn();
|
|
} catch (error) {
|
|
console.error(`Applying ${section} login settings failed:`, error);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
async function reload() {
|
|
current = (await Settings.findOneAsync({}, { fields: FIELDS })) || null;
|
|
}
|
|
|
|
// For a writer outside saveAuthConfigSettings (a one-time migration) that
|
|
// needs authEnv to see its change at once rather than when the observer runs.
|
|
export const reloadAuthConfig = reload;
|
|
|
|
Meteor.startup(async () => {
|
|
try {
|
|
await reload();
|
|
} finally {
|
|
markReady();
|
|
}
|
|
await Settings.find({}, { fields: FIELDS }).observeAsync({
|
|
added(doc) { current = doc; },
|
|
changed(doc) { current = doc; },
|
|
removed() { current = null; },
|
|
});
|
|
await notify(Object.keys(AUTH_CONFIG_SECTIONS));
|
|
});
|
|
|
|
async function requireSiteAdmin() {
|
|
const user = await Meteor.userAsync();
|
|
if (user?.isAdmin !== true || user.loginDisabled === true) {
|
|
throw new Meteor.Error('error-notAuthorized');
|
|
}
|
|
return user;
|
|
}
|
|
|
|
Meteor.methods({
|
|
// Admin Panel / People / <login method>: which value is in effect for each
|
|
// setting and where it comes from. A secret is reported only as
|
|
// { source, hasValue }; every other value has credentials in a URL masked
|
|
// (authConfigSources) before it leaves the server.
|
|
async getAuthConfigSources(section) {
|
|
check(section, String);
|
|
await requireSiteAdmin();
|
|
if (!AUTH_CONFIG_SECTIONS[section]) throw new Meteor.Error('unknown-login-settings-section');
|
|
const doc = await Settings.findOneAsync({}, { fields: FIELDS });
|
|
return authConfigSources(section, doc, process.env);
|
|
},
|
|
|
|
async saveAuthConfigSettings(section, input) {
|
|
check(section, String);
|
|
check(input, Object);
|
|
await requireSiteAdmin();
|
|
if (!AUTH_CONFIG_SECTIONS[section]) throw new Meteor.Error('unknown-login-settings-section');
|
|
let clean;
|
|
try {
|
|
clean = cleanAuthConfigInput(section, input);
|
|
} catch (error) {
|
|
// Validation errors are TypeErrors, which Meteor would report only as
|
|
// "Internal server error".
|
|
if (error instanceof TypeError) throw new Meteor.Error('invalid-login-settings', error.message);
|
|
throw error;
|
|
}
|
|
const setting = await Settings.findOneAsync({});
|
|
if (!setting) throw new Meteor.Error('settings-not-found');
|
|
const modifier = {};
|
|
if (Object.keys(clean.set).length) modifier.$set = clean.set;
|
|
if (Object.keys(clean.unset).length) modifier.$unset = clean.unset;
|
|
if (Object.keys(modifier).length) {
|
|
await Settings.updateAsync(setting._id, modifier);
|
|
}
|
|
await reload();
|
|
await notify([section]);
|
|
return true;
|
|
},
|
|
});
|