|
| 1 | +package no.nav.testnav.libs.reactivecore.config; |
| 2 | + |
| 3 | +import lombok.extern.slf4j.Slf4j; |
| 4 | +import org.springframework.context.ApplicationContextInitializer; |
| 5 | +import org.springframework.context.ConfigurableApplicationContext; |
| 6 | +import org.springframework.lang.NonNull; |
| 7 | + |
| 8 | +import java.util.Map; |
| 9 | +import java.util.stream.Stream; |
| 10 | + |
| 11 | +@Slf4j |
| 12 | +public class NaisEnvironmentApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> { |
| 13 | + |
| 14 | + private static final String DUMMY = "dummy"; |
| 15 | + |
| 16 | + @Override |
| 17 | + public void initialize(@NonNull ConfigurableApplicationContext context) { |
| 18 | + |
| 19 | + var environment = context.getEnvironment(); |
| 20 | + Stream |
| 21 | + .of(environment.getActiveProfiles()) |
| 22 | + .forEach(profile -> { |
| 23 | + switch (profile) { |
| 24 | + case "local" -> configureForLocalProfile(environment.getSystemProperties()); |
| 25 | + case "test" -> configureForTestProfile(environment.getSystemProperties()); |
| 26 | + default -> { /* Do nothing. */ } |
| 27 | + } |
| 28 | + }); |
| 29 | + |
| 30 | + } |
| 31 | + |
| 32 | + private static void configureForLocalProfile(Map<String, Object> properties) { |
| 33 | + |
| 34 | + log.info("Configuring environment for local profile using Secret Manager"); |
| 35 | + |
| 36 | + // Emulating NAIS provided environment variables. |
| 37 | + properties.putIfAbsent("AZURE_APP_CLIENT_ID", "${sm\\://azure-app-client-id}"); |
| 38 | + properties.putIfAbsent("AZURE_APP_CLIENT_SECRET", "${sm\\://azure-app-client-secret}"); |
| 39 | + properties.putIfAbsent("AZURE_OPENID_CONFIG_ISSUER", "${sm\\://azure-openid-config-issuer}"); |
| 40 | + properties.putIfAbsent("AZURE_OPENID_CONFIG_TOKEN_ENDPOINT", "${sm\\://azure-openid-config-token-endpoint}"); |
| 41 | + properties.putIfAbsent("TOKEN_X_ISSUER", "${sm\\://token-x-issuer}"); |
| 42 | + properties.putIfAbsent("TOKEN_X_JWKS_URI", "${sm\\://token-x-jwks-uri}"); |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + private static void configureForTestProfile(Map<String, Object> properties) { |
| 47 | + |
| 48 | + log.info("Configuring environment for test profile using dummy values"); |
| 49 | + |
| 50 | + // Disabling Secret Manager (not available when running builds on GitHub). |
| 51 | + properties.putIfAbsent("spring.cloud.gcp.secretmanager.enabled", "false"); |
| 52 | + |
| 53 | + // Setting dummy placeholders. |
| 54 | + properties.putIfAbsent("AZURE_OPENID_CONFIG_ISSUER", DUMMY); |
| 55 | + properties.putIfAbsent("AZURE_OPENID_CONFIG_TOKEN_ENDPOINT", DUMMY); |
| 56 | + properties.putIfAbsent("TOKEN_X_ISSUER", DUMMY); |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | +} |
0 commit comments