SettingsService Architecture
SettingsService (Sources/FynnCloudServer/Services/Settings/) is a Swift actor providing strongly typed, validated, encrypted, and cluster-synchronized dynamic configuration across all 73 AppSettings.
Resolution Precedence
Section titled “Resolution Precedence”Every setting resolves in strict precedence order:
1. Environment variable (or <KEY>_FILE secret file) ──► isManagedByEnv = true (read-only in Admin UI)2. PostgreSQL `app_settings` row (in-memory cached) ──► isManagedByEnv = false3. Compile-time `SettingKey.defaultValue` ──► isManagedByEnv = falseAnySettingKey.envValue calls Environment.first(matching: envKeys), checking each canonical and legacy environment variable in order—and for each name, falling back to <NAME>_FILE (trimmed of whitespace/newlines) for Docker/Kubernetes mounted secrets.
Defining Strongly Typed Keys (SettingKey & RangedSettingKey)
Section titled “Defining Strongly Typed Keys (SettingKey & RangedSettingKey)”Every dynamic setting conforms to SettingKey (or RangedSettingKey for comparable bounded values):
public struct BrandingAppName: SettingKey { public static let key = "brandingAppName" public static let group: SettingGroup = .branding public static let visibility: SettingVisibility = .public public static let defaultValue = "FynnCloud" public static let envKeys = ["BRANDING_APP_NAME", "APP_NAME"]}
public struct StorageMaxFileVersions: RangedSettingKey { public static let key = "storageMaxFileVersions" public static let group: SettingGroup = .storage public static let visibility: SettingVisibility = .internal public static let defaultValue = 30 public static let envKeys = ["STORAGE_MAX_FILE_VERSIONS", "MAX_FILE_VERSIONS"] public static let minValue: Int? = 0 public static let maxValue: Int? = 500}Core Protocol Capabilities
Section titled “Core Protocol Capabilities”SettingGroup(9 groups):.branding,.documentEditing,.rtc,.access,.storage,.sso,.ai,.mail,.rateLimiting.SettingVisibility(3 tiers):.public— Exposed on unauthenticatedGET /api/settings/public..internal— Admin-only (GET /api/settings)..secret— Encrypted at rest inapp_settingsusingSecretBoxand masked on read (value: nil,isConfigured: true).
- Validation (
validate(_ raw:)): Built-in parsing checks forLosslessStringConvertible, optional types (Int?,String?,Date?without string sentinels), inclusive min/max enforcement forRangedSettingKey, and custom validators (e.g.,BrandingPrimaryColorhex/Tailwind check,CustomLogoMimeTypeallowlist). - Backward-compatible aliases (
AppSettings.legacyKeyAliases):AppSettings.find(_:)transparently resolves legacy JSON/DB keys (such as"appName"→"brandingAppName","smtpHost"→"mailSmtpHost") to canonical keys.
Secret Encryption at Rest (SecretBox)
Section titled “Secret Encryption at Rest (SecretBox)”All .secret keys (docsJwtSecret, livekitApiSecret, ssoLdapBindPassword, ssoOidcClientSecret, ssoHeaderSecret, aiApiKey, aiEmbeddingApiKey, mailSmtpPassword) are encrypted before persistence using SecretBox (AES-256-GCM, v2. format):
- Per-key AAD binding:
SecretBoxauthenticates"app_settings:\(key)"as AEAD Associated Data (AAD), preventing ciphertext transplant between different setting keys. - Key rotation: Decrypts existing rows using
SECURITY_ENCRYPTION_KEYor any fallback key inENCRYPTION_PREVIOUS_KEYS, and seals new writes underSECURITY_ENCRYPTION_KEY. - Safe secret updates (
setGuardedAny): Submitting an empty or whitespace-only string for a.secretkey preserves the existing secret rather than wiping it. Attempting to modify an environment-locked setting throws409 Conflictunless the submitted value already equalsenvValue(idempotent no-op).
Caching, Actor Reentrancy & Multi-Replica Invalidation
Section titled “Caching, Actor Reentrancy & Multi-Replica Invalidation”- In-memory snapshot (
ttl = 300s):SettingsServiceloads theapp_settingstable once into memory, deduplicating concurrent callers withinFlightLoadand protecting against stale overwrites during actor suspension via a monotonicgeneration: UInt64counter. - Instant cross-replica invalidation: Every
setRaw/deleteRawpublishes the replica’sinstanceIDto the Redis Pub/Sub channelsettings:invalidate. - Derived-config rebuild (
onRemoteInvalidation): Remote replicas receiving a differentoriginIDcallclearCache()and run theonRemoteInvalidationhook—which reloadsSSOProviderRegistrylive on every node without restarting the server. If a Pub/Sub message is ever missed during a transient Redis hiccup, the 300-second local TTL guarantees eventual consistency.