|
1 |
| -package no.nav.testnav.libs.vault; |
| 1 | +package no.nav.dolly.libs.vault; |
2 | 2 |
|
3 |
| -import lombok.NoArgsConstructor; |
4 |
| -import lombok.extern.slf4j.Slf4j; |
| 3 | +import org.springframework.context.ApplicationContextInitializer; |
| 4 | +import org.springframework.context.ConfigurableApplicationContext; |
5 | 5 | import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
| 6 | +import org.springframework.lang.NonNull; |
6 | 7 |
|
7 | 8 | import java.io.IOException;
|
8 | 9 | import java.nio.file.Files;
|
9 | 10 | import java.nio.file.Paths;
|
10 | 11 | import java.util.Optional;
|
| 12 | +import java.util.stream.Stream; |
11 | 13 |
|
12 |
| -import static lombok.AccessLevel.PRIVATE; |
| 14 | +/** |
| 15 | + * Will set a system property {@code VAULT_TOKEN} if the Spring profile is set to {@link #PROFILE}. |
| 16 | + * Will only work for applications in {@code dev-fss}. |
| 17 | + */ |
| 18 | +public class VaultTokenApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> { |
13 | 19 |
|
14 |
| -@NoArgsConstructor(access = PRIVATE) |
15 |
| -@Slf4j |
16 |
| -public final class VaultUtils { |
| 20 | + private static final String PROFILE = "prod"; |
| 21 | + |
| 22 | + @Override |
| 23 | + public void initialize(@NonNull ConfigurableApplicationContext context) { |
| 24 | + |
| 25 | + Stream |
| 26 | + .of(context.getEnvironment().getActiveProfiles()) |
| 27 | + .filter(profile -> profile.equals(PROFILE)) |
| 28 | + .findAny() |
| 29 | + .ifPresent(profile -> System.setProperty(VAULT_TOKEN_SYSTEM_PROPERTY, getVaultToken())); |
| 30 | + |
| 31 | + } |
17 | 32 |
|
18 | 33 | private static final String NAIS_CLUSTER_SYSTEM_PROPERTY = "NAIS_CLUSTER_NAME";
|
19 | 34 | private static final String VAULT_TOKEN_SYSTEM_PROPERTY = "spring.cloud.vault.token";
|
@@ -49,10 +64,4 @@ private static String getVaultToken() {
|
49 | 64 |
|
50 | 65 | }
|
51 | 66 |
|
52 |
| - public static void initCloudVaultToken(String ifProfileIs) { |
53 |
| - if (ifProfileIs.equals(System.getProperty("spring.profiles.active"))) { |
54 |
| - System.setProperty(VAULT_TOKEN_SYSTEM_PROPERTY, getVaultToken()); |
55 |
| - } |
56 |
| - } |
57 |
| - |
58 | 67 | }
|
0 commit comments