Skip to content

Modern Docker Management, Designed for Everyone

fix: remove oidc env and args from docker build

Kyle Mendelle462e8bc271c
diff --git a/Dockerfile b/Dockerfile
index bd45bb95d..877cb34be 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -9,15 +9,6 @@ RUN npm ci
FROM node:22-alpine AS builder
WORKDIR /app
-# Set OIDC variables to dummy values for build time if they are checked
-ARG OIDC_CLIENT_ID_BUILD="dummy_client_id"
-ARG OIDC_CLIENT_SECRET_BUILD="dummy_client_secret"
-ARG OIDC_REDIRECT_URI_BUILD="http://localhost/dummy_callback"
-
-ENV OIDC_CLIENT_ID=$OIDC_CLIENT_ID_BUILD
-ENV OIDC_CLIENT_SECRET=$OIDC_CLIENT_SECRET_BUILD
-ENV OIDC_REDIRECT_URI=$OIDC_REDIRECT_URI_BUILD
-
# Copy dependencies from previous stage
COPY --from=deps /app/node_modules ./node_modules
COPY . .
diff --git a/src/hooks.server.ts b/src/hooks.server.ts
index a151ea02b..69d9e5182 100644
--- a/src/hooks.server.ts
+++ b/src/hooks.server.ts
@@ -1,35 +1,45 @@
import { sequence } from '@sveltejs/kit/hooks';
import type { Handle } from '@sveltejs/kit';
import { redirect } from '@sveltejs/kit';
+import { building } from '$app/environment';
import { getUserByUsername } from '$lib/services/user-service';
import { getSettings } from '$lib/services/settings-service';
import { checkFirstRun } from '$lib/utils/onboarding.utils';
import { sessionHandler } from '$lib/services/session-handler';
-import { initComposeService, stackRuntimeUpdater } from '$lib/services/docker/stack-custom-service';
-import { initAutoUpdateScheduler } from '$lib/services/docker/scheduler-service';
-import { initMaturityPollingScheduler } from '$lib/services/docker/image-service';
-import { migrateSettingsToDatabase } from '$lib/services/database/settings-db-service';
-import { migrateUsersToDatabase } from '$lib/services/database/user-db-service';
-import { migrateStacksToDatabase } from '$lib/services/database/compose-db-service';
-import { runMigrations } from './db/migrate';
// Get environment variable
const isTestEnvironment = process.env.APP_ENV === 'TEST';
-// Initialize needed services
-try {
- await runMigrations();
- await Promise.all([migrateSettingsToDatabase(), migrateUsersToDatabase(), migrateStacksToDatabase()]);
- await Promise.all([checkFirstRun(), initComposeService(), initAutoUpdateScheduler(), initMaturityPollingScheduler()]);
-
- stackRuntimeUpdater.start(2);
-} catch (err) {
- console.error('Critical service init failed, exiting:', err);
- process.exit(1);
+// Only initialize services at runtime, not during build
+if (!building) {
+ try {
+ // Dynamic imports to avoid loading these during build
+ const { runMigrations } = await import('./db/migrate');
+ const { migrateSettingsToDatabase } = await import('$lib/services/database/settings-db-service');
+ const { migrateUsersToDatabase } = await import('$lib/services/database/user-db-service');
+ const { migrateStacksToDatabase } = await import('$lib/services/database/compose-db-service');
+ const { initComposeService, stackRuntimeUpdater } = await import('$lib/services/docker/stack-custom-service');
+ const { initAutoUpdateScheduler } = await import('$lib/services/docker/scheduler-service');
+ const { initMaturityPollingScheduler } = await import('$lib/services/docker/image-service');
+
+ await runMigrations();
+ await Promise.all([migrateSettingsToDatabase(), migrateUsersToDatabase(), migrateStacksToDatabase()]);
+ await Promise.all([checkFirstRun(), initComposeService(), initAutoUpdateScheduler(), initMaturityPollingScheduler()]);
+
+ stackRuntimeUpdater.start(2);
+ } catch (err) {
+ console.error('Critical service init failed, exiting:', err);
+ process.exit(1);
+ }
}
// Authentication and authorization handler
const authHandler: Handle = async ({ event, resolve }) => {
+ // Skip auth processing during build
+ if (building) {
+ return resolve(event);
+ }
+
const { url } = event;
const path = url.pathname;
diff --git a/src/lib/services/oidc-service.ts b/src/lib/services/oidc-service.ts
index cc001b6d4..80ce84111 100644
--- a/src/lib/services/oidc-service.ts
+++ b/src/lib/services/oidc-service.ts
@@ -1,58 +1,174 @@
import { OAuth2Client } from 'arctic';
import { env } from '$env/dynamic/private';
+import { building } from '$app/environment';
import { getSettings } from './settings-service';
-const settings = await getSettings();
+// Lazy-loaded configuration
+let oidcConfig: {
+ clientId: string;
+ clientSecret: string;
+ redirectUri: string;
+ authorizationEndpoint?: string;
+ tokenEndpoint?: string;
+ userinfoEndpoint?: string;
+ scopes: string[];
+} | null = null;
-// Resolve OIDC configuration:
-// 1. Environment variables (highest precedence)
-// 2. Settings from getSettings() (if OIDC is enabled in settings and env var is not set)
-// 3. Hardcoded fallback for scopes if neither of the above provides it.
+let oidcClient: OAuth2Client | null = null;
-let resolvedClientId = env.OIDC_CLIENT_ID;
-let resolvedClientSecret = env.OIDC_CLIENT_SECRET;
-let resolvedRedirectUri = env.OIDC_REDIRECT_URI;
-let resolvedAuthEndpoint = env.OIDC_AUTHORIZATION_ENDPOINT;
-let resolvedTokenEndpoint = env.OIDC_TOKEN_ENDPOINT;
-let resolvedUserInfoEndpoint = env.OIDC_USERINFO_ENDPOINT;
-let resolvedScopesString = env.OIDC_SCOPES;
+/**
+ * Get OIDC configuration - only loads at runtime
+ */
+export async function getOIDCConfig() {
+ if (building) {
+ // Return dummy config during build
+ return {
+ enabled: false,
+ clientId: '',
+ clientSecret: '',
+ redirectUri: '',
+ authorizationEndpoint: '',
+ tokenEndpoint: '',
+ userinfoEndpoint: '',
+ scopes: ['openid', 'email', 'profile']
+ };
+ }
-// If OIDC is enabled in settings, use those values as fallbacks if environment variables are not set.
-if (settings.auth.oidcEnabled && settings.auth.oidc) {
- if (resolvedClientId === undefined) resolvedClientId = settings.auth.oidc.clientId;
- if (resolvedClientSecret === undefined) resolvedClientSecret = settings.auth.oidc.clientSecret;
- if (resolvedRedirectUri === undefined) resolvedRedirectUri = settings.auth.oidc.redirectUri;
- if (resolvedAuthEndpoint === undefined) resolvedAuthEndpoint = settings.auth.oidc.authorizationEndpoint;
- if (resolvedTokenEndpoint === undefined) resolvedTokenEndpoint = settings.auth.oidc.tokenEndpoint;
- if (resolvedUserInfoEndpoint === undefined) resolvedUserInfoEndpoint = settings.auth.oidc.userinfoEndpoint;
- if (resolvedScopesString === undefined) resolvedScopesString = settings.auth.oidc.scopes;
+ if (oidcConfig) {
+ return { enabled: true, ...oidcConfig };
+ }
+
+ try {
+ const settings = await getSettings();
+
+ // Resolve OIDC configuration:
+ // 1. Environment variables (highest precedence)
+ // 2. Settings from getSettings() (if OIDC is enabled in settings and env var is not set)
+ // 3. Hardcoded fallback for scopes if neither of the above provides it.
+
+ let resolvedClientId = env.OIDC_CLIENT_ID;
+ let resolvedClientSecret = env.OIDC_CLIENT_SECRET;
+ let resolvedRedirectUri = env.OIDC_REDIRECT_URI;
+ let resolvedAuthEndpoint = env.OIDC_AUTHORIZATION_ENDPOINT;
+ let resolvedTokenEndpoint = env.OIDC_TOKEN_ENDPOINT;
+ let resolvedUserInfoEndpoint = env.OIDC_USERINFO_ENDPOINT;
+ let resolvedScopesString = env.OIDC_SCOPES;
+
+ // If OIDC is enabled in settings, use those values as fallbacks if environment variables are not set.
+ if (settings.auth.oidcEnabled && settings.auth.oidc) {
+ if (resolvedClientId === undefined) resolvedClientId = settings.auth.oidc.clientId;
+ if (resolvedClientSecret === undefined) resolvedClientSecret = settings.auth.oidc.clientSecret;
+ if (resolvedRedirectUri === undefined) resolvedRedirectUri = settings.auth.oidc.redirectUri;
+ if (resolvedAuthEndpoint === undefined) resolvedAuthEndpoint = settings.auth.oidc.authorizationEndpoint;
+ if (resolvedTokenEndpoint === undefined) resolvedTokenEndpoint = settings.auth.oidc.tokenEndpoint;
+ if (resolvedUserInfoEndpoint === undefined) resolvedUserInfoEndpoint = settings.auth.oidc.userinfoEndpoint;
+ if (resolvedScopesString === undefined) resolvedScopesString = settings.auth.oidc.scopes;
+ }
+
+ if (resolvedScopesString === undefined) {
+ resolvedScopesString = 'openid email profile';
+ }
+
+ const scopes = resolvedScopesString.split(' ').filter((s) => s.trim() !== '');
+
+ // Check if OIDC is properly configured
+ if (!resolvedClientId || !resolvedClientSecret || !resolvedRedirectUri) {
+ console.log('OIDC not configured or incomplete configuration, OIDC will be disabled');
+ return {
+ enabled: false,
+ clientId: '',
+ clientSecret: '',
+ redirectUri: '',
+ authorizationEndpoint: '',
+ tokenEndpoint: '',
+ userinfoEndpoint: '',
+ scopes
+ };
+ }
+
+ oidcConfig = {
+ clientId: resolvedClientId,
+ clientSecret: resolvedClientSecret,
+ redirectUri: resolvedRedirectUri,
+ authorizationEndpoint: resolvedAuthEndpoint,
+ tokenEndpoint: resolvedTokenEndpoint,
+ userinfoEndpoint: resolvedUserInfoEndpoint,
+ scopes
+ };
+
+ if (!resolvedAuthEndpoint || !resolvedTokenEndpoint) {
+ console.warn('OIDC Authorization or Token Endpoint is not set (from environment or application settings). OIDC flow will likely fail.');
+ }
+
+ return { enabled: true, ...oidcConfig };
+ } catch (error) {
+ console.warn('Failed to load OIDC configuration:', error);
+ return {
+ enabled: false,
+ clientId: '',
+ clientSecret: '',
+ redirectUri: '',
+ authorizationEndpoint: '',
+ tokenEndpoint: '',
+ userinfoEndpoint: '',
+ scopes: ['openid', 'email', 'profile']
+ };
+ }
}
-if (resolvedScopesString === undefined) {
- resolvedScopesString = 'openid email profile';
+/**
+ * Get OIDC client - lazy initialization
+ */
+export async function getOIDCClient(): Promise<OAuth2Client | null> {
+ if (building) {
+ return null;
+ }
+
+ if (oidcClient) {
+ return oidcClient;
+ }
+
+ const config = await getOIDCConfig();
+ if (!config.enabled) {
+ return null;
+ }
+
+ oidcClient = new OAuth2Client(config.clientId, config.clientSecret, config.redirectUri);
+ return oidcClient;
+}
+
+// Legacy exports for backward compatibility - these will be lazy-loaded
+export async function getOIDCScopes(): Promise<string[]> {
+ const config = await getOIDCConfig();
+ return config.scopes;
}
-export const OIDC_SCOPES = resolvedScopesString.split(' ').filter((s) => s.trim() !== '');
+export async function getOIDCClientId(): Promise<string> {
+ const config = await getOIDCConfig();
+ return config.clientId;
+}
-// Export the resolved configuration values
-export const OIDC_CLIENT_ID = resolvedClientId;
-export const OIDC_CLIENT_SECRET = resolvedClientSecret;
-export const OIDC_REDIRECT_URI = resolvedRedirectUri;
-export const OIDC_AUTHORIZATION_ENDPOINT = resolvedAuthEndpoint;
-export const OIDC_TOKEN_ENDPOINT = resolvedTokenEndpoint;
-export const OIDC_USERINFO_ENDPOINT = resolvedUserInfoEndpoint;
+export async function getOIDCClientSecret(): Promise<string> {
+ const config = await getOIDCConfig();
+ return config.clientSecret;
+}
-// Critical configuration check using the resolved values
-if (!OIDC_CLIENT_ID || !OIDC_CLIENT_SECRET || !OIDC_REDIRECT_URI) {
- const missing: string[] = [];
- if (!OIDC_CLIENT_ID) missing.push('Client ID');
- if (!OIDC_CLIENT_SECRET) missing.push('Client Secret');
- if (!OIDC_REDIRECT_URI) missing.push('Redirect URI');
- throw new Error(`Critical OIDC configuration (${missing.join(', ')}) not found in environment variables or application settings.`);
+export async function getOIDCRedirectUri(): Promise<string> {
+ const config = await getOIDCConfig();
+ return config.redirectUri;
}
-export const oidcClient = new OAuth2Client(OIDC_CLIENT_ID, OIDC_CLIENT_SECRET, OIDC_REDIRECT_URI);
+export async function getOIDCAuthorizationEndpoint(): Promise<string> {
+ const config = await getOIDCConfig();
+ return config.authorizationEndpoint || '';
+}
+
+export async function getOIDCTokenEndpoint(): Promise<string> {
+ const config = await getOIDCConfig();
+ return config.tokenEndpoint || '';
+}
-if (!OIDC_AUTHORIZATION_ENDPOINT || !OIDC_TOKEN_ENDPOINT) {
- console.warn('OIDC Authorization or Token Endpoint is not set (from environment or application settings). OIDC flow will likely fail.');
+export async function getOIDCUserinfoEndpoint(): Promise<string> {
+ const config = await getOIDCConfig();
+ return config.userinfoEndpoint || '';
}
diff --git a/src/routes/auth/oidc/callback/+server.ts b/src/routes/auth/oidc/callback/+server.ts
index 551bd8a02..a72809b1d 100644
--- a/src/routes/auth/oidc/callback/+server.ts
+++ b/src/routes/auth/oidc/callback/+server.ts
@@ -1,6 +1,6 @@
import { redirect } from '@sveltejs/kit';
import { OAuth2RequestError } from 'arctic';
-import { oidcClient, OIDC_TOKEN_ENDPOINT, OIDC_USERINFO_ENDPOINT } from '$lib/services/oidc-service';
+import { getOIDCClient, getOIDCTokenEndpoint, getOIDCUserinfoEndpoint } from '$lib/services/oidc-service';
import { getUserByUsername, saveUser, getUserById, getUserByOidcSubjectId } from '$lib/services/user-service';
import type { User } from '$lib/types/user.type';
import { nanoid } from 'nanoid';
@@ -23,20 +23,30 @@ export const GET: RequestHandler = async ({ url, cookies, locals }) => {
throw redirect(302, '/auth/login?error=oidc_invalid_response');
}
- if (!OIDC_TOKEN_ENDPOINT) {
+ // Get OIDC configuration at runtime
+ const oidcClient = await getOIDCClient();
+ const tokenEndpoint = await getOIDCTokenEndpoint();
+ const userinfoEndpoint = await getOIDCUserinfoEndpoint();
+
+ if (!oidcClient) {
+ console.error('OIDC client is not configured.');
+ throw redirect(302, '/auth/login?error=oidc_misconfigured');
+ }
+
+ if (!tokenEndpoint) {
console.error('OIDC_TOKEN_ENDPOINT is not configured.');
throw redirect(302, '/auth/login?error=oidc_misconfigured');
}
try {
- const tokens = await oidcClient.validateAuthorizationCode(OIDC_TOKEN_ENDPOINT, code, codeVerifier);
+ const tokens = await oidcClient.validateAuthorizationCode(tokenEndpoint, code, codeVerifier);
- if (!OIDC_USERINFO_ENDPOINT) {
+ if (!userinfoEndpoint) {
console.error('OIDC_USERINFO_ENDPOINT is not configured. Cannot fetch user details.');
throw redirect(302, '/auth/login?error=oidc_misconfigured');
}
- const userInfoResponse = await fetch(OIDC_USERINFO_ENDPOINT, {
+ const userInfoResponse = await fetch(userinfoEndpoint, {
headers: {
Authorization: `Bearer ${tokens.accessToken()}`
}
diff --git a/src/routes/auth/oidc/login/+server.ts b/src/routes/auth/oidc/login/+server.ts
index 74137c38b..1139f6908 100644
--- a/src/routes/auth/oidc/login/+server.ts
+++ b/src/routes/auth/oidc/login/+server.ts
@@ -1,10 +1,20 @@
import { redirect } from '@sveltejs/kit';
import { generateState, generateCodeVerifier, CodeChallengeMethod } from 'arctic';
-import { oidcClient, OIDC_AUTHORIZATION_ENDPOINT, OIDC_SCOPES } from '$lib/services/oidc-service';
+import { getOIDCClient, getOIDCAuthorizationEndpoint, getOIDCScopes } from '$lib/services/oidc-service';
import type { RequestHandler } from './$types';
export const GET: RequestHandler = async ({ cookies, url }) => {
- if (!OIDC_AUTHORIZATION_ENDPOINT) {
+ // Get OIDC configuration at runtime
+ const oidcClient = await getOIDCClient();
+ const authorizationEndpoint = await getOIDCAuthorizationEndpoint();
+ const scopes = await getOIDCScopes();
+
+ if (!oidcClient) {
+ console.error('OIDC client is not configured.');
+ throw redirect(302, '/auth/login?error=oidc_misconfigured');
+ }
+
+ if (!authorizationEndpoint) {
console.error('OIDC_AUTHORIZATION_ENDPOINT is not configured.');
throw redirect(302, '/auth/login?error=oidc_misconfigured');
}
@@ -12,7 +22,7 @@ export const GET: RequestHandler = async ({ cookies, url }) => {
const state = generateState();
const codeVerifier = generateCodeVerifier();
- const authUrl = await oidcClient.createAuthorizationURLWithPKCE(OIDC_AUTHORIZATION_ENDPOINT, state, CodeChallengeMethod.S256, codeVerifier, OIDC_SCOPES);
+ const authUrl = await oidcClient.createAuthorizationURLWithPKCE(authorizationEndpoint, state, CodeChallengeMethod.S256, codeVerifier, scopes);
cookies.set('oidc_state', state, {
path: '/',