3232import com .google .common .cache .CacheLoader ;
3333import com .google .common .cache .LoadingCache ;
3434import java .io .IOException ;
35+ import java .sql .Timestamp ;
3536import java .util .Collections ;
3637import java .util .HashMap ;
3738import java .util .List ;
6263import org .openmetadata .schema .security .scim .ScimConfiguration ;
6364import org .openmetadata .schema .settings .Settings ;
6465import org .openmetadata .schema .settings .SettingsType ;
66+ import org .openmetadata .schema .type .ConfigSource ;
6567import org .openmetadata .schema .utils .JsonUtils ;
6668import org .openmetadata .search .IndexMapping ;
6769import org .openmetadata .service .Entity ;
7072import org .openmetadata .service .jdbi3 .EntityRepository ;
7173import org .openmetadata .service .search .SearchRepository ;
7274import org .openmetadata .service .search .indexes .SearchIndex ;
75+ import org .openmetadata .service .util .ConfigSourceResolver ;
7376import org .openmetadata .service .util .EntityUtil ;
7477
7578@ Slf4j
7679public class SettingsCache {
7780 private static volatile boolean initialized = false ;
81+ private static Timestamp applicationStartTime ;
7882 protected static final LoadingCache <String , Settings > CACHE =
7983 CacheBuilder .newBuilder ()
8084 .maximumSize (1000 )
@@ -85,26 +89,21 @@ private SettingsCache() {
8589 // Private constructor for singleton
8690 }
8791
88- // Expected to be called only once from the DefaultAuthorizer
8992 public static void initialize (OpenMetadataApplicationConfig config ) {
9093 if (!initialized ) {
9194 initialized = true ;
95+ applicationStartTime = ConfigSourceResolver .now ();
9296 createDefaultConfiguration (config );
9397 }
9498 }
9599
96100 private static void createDefaultConfiguration (OpenMetadataApplicationConfig applicationConfig ) {
97- // Initialise Email Setting
98- Settings storedSettings =
99- Entity .getSystemRepository ().getConfigWithKey (EMAIL_CONFIGURATION .toString ());
100- if (storedSettings == null ) {
101- // Only in case a config doesn't exist in DB we insert it
102- SmtpSettings emailConfig =
103- applicationConfig .getOperationalApplicationConfigProvider ().getEmailSettings ();
104-
105- Settings setting =
106- new Settings ().withConfigType (EMAIL_CONFIGURATION ).withConfigValue (emailConfig );
107- Entity .getSystemRepository ().createNewSetting (setting );
101+ SmtpSettings emailConfig =
102+ applicationConfig .getOperationalApplicationConfigProvider ().getEmailSettings ();
103+ if (emailConfig != null ) {
104+ ConfigSource configSource =
105+ emailConfig .getConfigSource () != null ? emailConfig .getConfigSource () : ConfigSource .ENV ;
106+ syncConfigWithSource (EMAIL_CONFIGURATION , emailConfig , configSource );
108107 }
109108
110109 // Initialise OM base url setting
@@ -243,41 +242,27 @@ private static void createDefaultConfiguration(OpenMetadataApplicationConfig app
243242 Entity .getSystemRepository ().createNewSetting (setting );
244243 }
245244
246- // Initialize Authentication Configuration
247- Settings storedAuthConfig =
248- Entity .getSystemRepository ().getConfigWithKey (AUTHENTICATION_CONFIGURATION .toString ());
249- if (storedAuthConfig == null ) {
250- AuthenticationConfiguration authConfig = applicationConfig .getAuthenticationConfiguration ();
251- if (authConfig != null ) {
252- Settings setting =
253- new Settings ().withConfigType (AUTHENTICATION_CONFIGURATION ).withConfigValue (authConfig );
254-
255- Entity .getSystemRepository ().createNewSetting (setting );
256- }
245+ AuthenticationConfiguration authConfig = applicationConfig .getAuthenticationConfiguration ();
246+ if (authConfig != null ) {
247+ ConfigSource configSource =
248+ authConfig .getConfigSource () != null ? authConfig .getConfigSource () : ConfigSource .ENV ;
249+ syncConfigWithSource (AUTHENTICATION_CONFIGURATION , authConfig , configSource );
257250 }
258251
259- // Initialize Authorizer Configuration
260- Settings storedAuthzConfig =
261- Entity .getSystemRepository ().getConfigWithKey (AUTHORIZER_CONFIGURATION .toString ());
262- if (storedAuthzConfig == null ) {
263- AuthorizerConfiguration authzConfig = applicationConfig .getAuthorizerConfiguration ();
264- if (authzConfig != null ) {
265- Settings setting =
266- new Settings ().withConfigType (AUTHORIZER_CONFIGURATION ).withConfigValue (authzConfig );
267-
268- Entity .getSystemRepository ().createNewSetting (setting );
269- }
252+ AuthorizerConfiguration authzConfig = applicationConfig .getAuthorizerConfiguration ();
253+ if (authzConfig != null ) {
254+ ConfigSource configSource =
255+ authzConfig .getConfigSource () != null ? authzConfig .getConfigSource () : ConfigSource .ENV ;
256+ syncConfigWithSource (AUTHORIZER_CONFIGURATION , authzConfig , configSource );
270257 }
271258
272- Settings storedScimConfig =
273- Entity .getSystemRepository ().getConfigWithKey (SCIM_CONFIGURATION .toString ());
274- if (storedScimConfig == null ) {
275- ScimConfiguration scimConfiguration = applicationConfig .getScimConfiguration ();
276- if (scimConfiguration != null ) {
277- Settings setting =
278- new Settings ().withConfigType (SCIM_CONFIGURATION ).withConfigValue (scimConfiguration );
279- Entity .getSystemRepository ().createNewSetting (setting );
280- }
259+ ScimConfiguration scimConfiguration = applicationConfig .getScimConfiguration ();
260+ if (scimConfiguration != null ) {
261+ ConfigSource configSource =
262+ scimConfiguration .getConfigSource () != null
263+ ? scimConfiguration .getConfigSource ()
264+ : ConfigSource .ENV ;
265+ syncConfigWithSource (SCIM_CONFIGURATION , scimConfiguration , configSource );
281266 }
282267
283268 Settings entityRulesSettings =
@@ -318,6 +303,54 @@ private static void createDefaultConfiguration(OpenMetadataApplicationConfig app
318303 }
319304 }
320305
306+ private static void syncConfigWithSource (
307+ SettingsType settingsType , Object envConfigValue , ConfigSource configSource ) {
308+ if (envConfigValue == null ) {
309+ return ;
310+ }
311+
312+ String currentEnvHash = ConfigSourceResolver .computeHash (envConfigValue );
313+ String storedEnvHash = Entity .getSystemRepository ().getEnvHash (settingsType .toString ());
314+ Timestamp dbModifiedTimestamp =
315+ Entity .getSystemRepository ().getDbModifiedTimestamp (settingsType .toString ());
316+
317+ Settings storedSettings =
318+ Entity .getSystemRepository ().getConfigWithKey (settingsType .toString ());
319+
320+ if (storedSettings == null ) {
321+ Settings setting =
322+ new Settings ().withConfigType (settingsType ).withConfigValue (envConfigValue );
323+ Entity .getSystemRepository ().createNewSetting (setting );
324+ Entity .getSystemRepository ()
325+ .updateConfigMetadata (
326+ settingsType .toString (), currentEnvHash , applicationStartTime , applicationStartTime );
327+ return ;
328+ }
329+
330+ boolean shouldUseEnv =
331+ ConfigSourceResolver .shouldUseEnvValue (
332+ configSource ,
333+ currentEnvHash ,
334+ storedEnvHash ,
335+ null ,
336+ dbModifiedTimestamp ,
337+ applicationStartTime );
338+
339+ if (shouldUseEnv ) {
340+ LOG .info ("Config source resolution: using ENV value for {}" , settingsType );
341+ Settings setting =
342+ new Settings ().withConfigType (settingsType ).withConfigValue (envConfigValue );
343+ Entity .getSystemRepository ().updateSetting (setting );
344+ Entity .getSystemRepository ()
345+ .updateConfigMetadata (
346+ settingsType .toString (), currentEnvHash , applicationStartTime , applicationStartTime );
347+ } else {
348+ if (storedEnvHash == null || !currentEnvHash .equals (storedEnvHash )) {
349+ Entity .getSystemRepository ().updateEnvHash (settingsType .toString (), currentEnvHash );
350+ }
351+ }
352+ }
353+
321354 public static <T > T getSetting (SettingsType settingName , Class <T > clazz ) {
322355 try {
323356 String json = JsonUtils .pojoToJson (CACHE .get (settingName .toString ()).getConfigValue ());
0 commit comments