|
33 | 33 | import java.util.List; |
34 | 34 | import java.util.Map; |
35 | 35 |
|
36 | | -import static org.springframework.boot.context.config.ConfigData.Option.*; |
| 36 | +import static org.springframework.boot.context.config.ConfigData.Option.IGNORE_IMPORTS; |
| 37 | +import static org.springframework.boot.context.config.ConfigData.Option.IGNORE_PROFILES; |
| 38 | +import static org.springframework.boot.context.config.ConfigData.Option.PROFILE_SPECIFIC; |
37 | 39 |
|
38 | 40 | public class DaprConfigurationConfigDataLoader implements ConfigDataLoader<DaprConfigurationConfigDataResource> { |
39 | 41 |
|
40 | | - private final Log log; |
| 42 | + private final Log log; |
| 43 | + |
| 44 | + private DaprClient daprClient; |
| 45 | + |
| 46 | + private DaprCloudConfigProperties daprSecretStoreConfig; |
| 47 | + |
| 48 | + public DaprConfigurationConfigDataLoader(DeferredLogFactory logFactory, DaprClient daprClient, |
| 49 | + DaprCloudConfigProperties daprSecretStoreConfig) { |
| 50 | + this.log = logFactory.getLog(getClass()); |
| 51 | + this.daprClient = daprClient; |
| 52 | + this.daprSecretStoreConfig = daprSecretStoreConfig; |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + /** |
| 57 | + * Load {@link ConfigData} for the given resource. |
| 58 | + * |
| 59 | + * @param context the loader context |
| 60 | + * @param resource the resource to load |
| 61 | + * @return the loaded config data or {@code null} if the location should be skipped |
| 62 | + * @throws IOException on IO error |
| 63 | + * @throws ConfigDataResourceNotFoundException if the resource cannot be found |
| 64 | + */ |
| 65 | + @Override |
| 66 | + public ConfigData load(ConfigDataLoaderContext context, DaprConfigurationConfigDataResource resource) |
| 67 | + throws IOException, ConfigDataResourceNotFoundException { |
| 68 | + DaprCloudConfigClientManager daprClientSecretStoreConfigManager = |
| 69 | + getBean(context, DaprCloudConfigClientManager.class); |
| 70 | + |
| 71 | + daprClient = DaprCloudConfigClientManager.getDaprClient(); |
| 72 | + daprSecretStoreConfig = daprClientSecretStoreConfigManager.getDaprCloudConfigProperties(); |
| 73 | + |
| 74 | + if (resource.getSecretName() == null) { |
| 75 | + return fetchConfig(resource.getStoreName()); |
| 76 | + } else { |
| 77 | + return fetchConfig(resource.getStoreName(), resource.getSecretName()); |
| 78 | + } |
| 79 | + } |
41 | 80 |
|
42 | | - private DaprClient daprClient; |
| 81 | + private ConfigData fetchConfig(String storeName) { |
| 82 | + Mono<Map<String, Map<String, String>>> secretMapMono = daprClient.getBulkSecret(storeName); |
43 | 83 |
|
44 | | - private DaprCloudConfigProperties daprSecretStoreConfig; |
| 84 | + Map<String, Map<String, String>> secretMap = |
| 85 | + secretMapMono.block(Duration.ofMillis(daprSecretStoreConfig.getTimeout())); |
45 | 86 |
|
46 | | - public DaprConfigurationConfigDataLoader(DeferredLogFactory logFactory, DaprClient daprClient, |
47 | | - DaprCloudConfigProperties daprSecretStoreConfig) { |
48 | | - this.log = logFactory.getLog(getClass()); |
49 | | - this.daprClient = daprClient; |
50 | | - this.daprSecretStoreConfig = daprSecretStoreConfig; |
| 87 | + if (secretMap == null) { |
| 88 | + return new ConfigData(Collections.emptyList(), IGNORE_IMPORTS, IGNORE_PROFILES, PROFILE_SPECIFIC); |
51 | 89 | } |
52 | 90 |
|
| 91 | + List<PropertySource<?>> sourceList = new ArrayList<>(); |
53 | 92 |
|
54 | | - /** |
55 | | - * Load {@link ConfigData} for the given resource. |
56 | | - * |
57 | | - * @param context the loader context |
58 | | - * @param resource the resource to load |
59 | | - * @return the loaded config data or {@code null} if the location should be skipped |
60 | | - * @throws IOException on IO error |
61 | | - * @throws ConfigDataResourceNotFoundException if the resource cannot be found |
62 | | - */ |
63 | | - @Override |
64 | | - public ConfigData load(ConfigDataLoaderContext context, DaprConfigurationConfigDataResource resource) |
65 | | - throws IOException, ConfigDataResourceNotFoundException { |
66 | | - DaprCloudConfigClientManager daprClientSecretStoreConfigManager = |
67 | | - getBean(context, DaprCloudConfigClientManager.class); |
68 | | - |
69 | | - daprClient = DaprCloudConfigClientManager.getDaprClient(); |
70 | | - daprSecretStoreConfig = daprClientSecretStoreConfigManager.getDaprCloudConfigProperties(); |
71 | | - |
72 | | - if (resource.getSecretName() == null) { |
73 | | - return fetchConfig(resource.getStoreName()); |
74 | | - } else { |
75 | | - return fetchConfig(resource.getStoreName(), resource.getSecretName()); |
76 | | - } |
| 93 | + for (Map.Entry<String, Map<String, String>> entry : secretMap.entrySet()) { |
| 94 | + sourceList.addAll(DaprSecretStoreParserHandler.getInstance().parseDaprSecretStoreData(entry.getKey(), |
| 95 | + entry.getValue())); |
77 | 96 | } |
78 | 97 |
|
79 | | - private ConfigData fetchConfig(String storeName) { |
80 | | - Mono<Map<String, Map<String, String>>> secretMapMono = daprClient.getBulkSecret(storeName); |
81 | | - |
82 | | - Map<String, Map<String, String>> secretMap = |
83 | | - secretMapMono.block(Duration.ofMillis(daprSecretStoreConfig.getTimeout())); |
| 98 | + return new ConfigData(sourceList, IGNORE_IMPORTS, IGNORE_PROFILES, PROFILE_SPECIFIC); |
| 99 | + } |
84 | 100 |
|
85 | | - if (secretMap == null) { |
86 | | - return new ConfigData(Collections.emptyList(), IGNORE_IMPORTS, IGNORE_PROFILES, PROFILE_SPECIFIC); |
87 | | - } |
| 101 | + private ConfigData fetchConfig(String storeName, String secretName) { |
| 102 | + Mono<Map<String, String>> secretMapMono = daprClient.getSecret(storeName, secretName); |
88 | 103 |
|
89 | | - List<PropertySource<?>> sourceList = new ArrayList<>(); |
| 104 | + Map<String, String> secretMap = secretMapMono.block(Duration.ofMillis(daprSecretStoreConfig.getTimeout())); |
90 | 105 |
|
91 | | - for (Map.Entry<String, Map<String, String>> entry : secretMap.entrySet()) { |
92 | | - sourceList.addAll(DaprSecretStoreParserHandler.getInstance().parseDaprSecretStoreData(entry.getKey(), |
93 | | - entry.getValue())); |
94 | | - } |
95 | | - |
96 | | - return new ConfigData(sourceList, IGNORE_IMPORTS, IGNORE_PROFILES, PROFILE_SPECIFIC); |
| 106 | + if (secretMap == null) { |
| 107 | + return new ConfigData(Collections.emptyList(), IGNORE_IMPORTS, IGNORE_PROFILES, PROFILE_SPECIFIC); |
97 | 108 | } |
98 | 109 |
|
99 | | - private ConfigData fetchConfig(String storeName, String secretName) { |
100 | | - Mono<Map<String, String>> secretMapMono = daprClient.getSecret(storeName, secretName); |
101 | | - |
102 | | - Map<String, String> secretMap = secretMapMono.block(Duration.ofMillis(daprSecretStoreConfig.getTimeout())); |
| 110 | + List<PropertySource<?>> sourceList = new ArrayList<>( |
| 111 | + DaprSecretStoreParserHandler.getInstance().parseDaprSecretStoreData(secretName, secretMap)); |
103 | 112 |
|
104 | | - if (secretMap == null) { |
105 | | - return new ConfigData(Collections.emptyList(), IGNORE_IMPORTS, IGNORE_PROFILES, PROFILE_SPECIFIC); |
106 | | - } |
107 | | - |
108 | | - List<PropertySource<?>> sourceList = new ArrayList<>( |
109 | | - DaprSecretStoreParserHandler.getInstance().parseDaprSecretStoreData(secretName, secretMap)); |
110 | | - |
111 | | - return new ConfigData(sourceList, IGNORE_IMPORTS, IGNORE_PROFILES, PROFILE_SPECIFIC); |
112 | | - } |
| 113 | + return new ConfigData(sourceList, IGNORE_IMPORTS, IGNORE_PROFILES, PROFILE_SPECIFIC); |
| 114 | + } |
113 | 115 |
|
114 | | - protected <T> T getBean(ConfigDataLoaderContext context, Class<T> type) { |
115 | | - if (context.getBootstrapContext().isRegistered(type)) { |
116 | | - return context.getBootstrapContext().get(type); |
117 | | - } |
118 | | - return null; |
| 116 | + protected <T> T getBean(ConfigDataLoaderContext context, Class<T> type) { |
| 117 | + if (context.getBootstrapContext().isRegistered(type)) { |
| 118 | + return context.getBootstrapContext().get(type); |
119 | 119 | } |
| 120 | + return null; |
| 121 | + } |
120 | 122 | } |
0 commit comments