Skip to content

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.

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 = false
3. Compile-time `SettingKey.defaultValue` ──► isManagedByEnv = false

AnySettingKey.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
}
  • SettingGroup (9 groups): .branding, .documentEditing, .rtc, .access, .storage, .sso, .ai, .mail, .rateLimiting.
  • SettingVisibility (3 tiers):
    • .public — Exposed on unauthenticated GET /api/settings/public.
    • .internal — Admin-only (GET /api/settings).
    • .secret — Encrypted at rest in app_settings using SecretBox and masked on read (value: nil, isConfigured: true).
  • Validation (validate(_ raw:)): Built-in parsing checks for LosslessStringConvertible, optional types (Int?, String?, Date? without string sentinels), inclusive min/max enforcement for RangedSettingKey, and custom validators (e.g., BrandingPrimaryColor hex/Tailwind check, CustomLogoMimeType allowlist).
  • Backward-compatible aliases (AppSettings.legacyKeyAliases): AppSettings.find(_:) transparently resolves legacy JSON/DB keys (such as "appName" → "brandingAppName", "smtpHost" → "mailSmtpHost") to canonical keys.

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: SecretBox authenticates "app_settings:\(key)" as AEAD Associated Data (AAD), preventing ciphertext transplant between different setting keys.
  • Key rotation: Decrypts existing rows using SECURITY_ENCRYPTION_KEY or any fallback key in ENCRYPTION_PREVIOUS_KEYS, and seals new writes under SECURITY_ENCRYPTION_KEY.
  • Safe secret updates (setGuardedAny): Submitting an empty or whitespace-only string for a .secret key preserves the existing secret rather than wiping it. Attempting to modify an environment-locked setting throws 409 Conflict unless the submitted value already equals envValue (idempotent no-op).

Caching, Actor Reentrancy & Multi-Replica Invalidation

Section titled “Caching, Actor Reentrancy & Multi-Replica Invalidation”
  1. In-memory snapshot (ttl = 300s): SettingsService loads the app_settings table once into memory, deduplicating concurrent callers with inFlightLoad and protecting against stale overwrites during actor suspension via a monotonic generation: UInt64 counter.
  2. Instant cross-replica invalidation: Every setRaw / deleteRaw publishes the replica’s instanceID to the Redis Pub/Sub channel settings:invalidate.
  3. Derived-config rebuild (onRemoteInvalidation): Remote replicas receiving a different originID call clearCache() and run the onRemoteInvalidation hook—which reloads SSOProviderRegistry live 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.