|
| 1 | +/* |
| 2 | + * Copyright (c) 2016-2024 Team Fangkehou |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.dapr.spring.boot.cloudconfig.configdata.config; |
| 18 | + |
| 19 | +import io.dapr.client.DaprClient; |
| 20 | +import io.dapr.spring.boot.cloudconfig.config.DaprCloudConfigClientManager; |
| 21 | +import io.dapr.spring.boot.cloudconfig.config.DaprCloudConfigProperties; |
| 22 | +import io.dapr.spring.boot.cloudconfig.parser.DaprSecretStoreParserHandler; |
| 23 | +import org.apache.commons.logging.Log; |
| 24 | +import org.springframework.boot.context.config.ConfigData; |
| 25 | +import org.springframework.boot.context.config.ConfigDataLoader; |
| 26 | +import org.springframework.boot.context.config.ConfigDataLoaderContext; |
| 27 | +import org.springframework.boot.context.config.ConfigDataResourceNotFoundException; |
| 28 | +import org.springframework.boot.logging.DeferredLogFactory; |
| 29 | +import org.springframework.core.env.PropertySource; |
| 30 | +import reactor.core.publisher.Mono; |
| 31 | + |
| 32 | +import java.io.IOException; |
| 33 | +import java.time.Duration; |
| 34 | +import java.util.ArrayList; |
| 35 | +import java.util.Collections; |
| 36 | +import java.util.List; |
| 37 | +import java.util.Map; |
| 38 | + |
| 39 | +import static org.springframework.boot.context.config.ConfigData.Option.*; |
| 40 | + |
| 41 | +public class DaprConfigurationConfigDataLoader implements ConfigDataLoader<DaprConfigurationConfigDataResource> { |
| 42 | + |
| 43 | + private final Log log; |
| 44 | + |
| 45 | + private DaprClient daprClient; |
| 46 | + |
| 47 | + private DaprCloudConfigProperties daprSecretStoreConfig; |
| 48 | + |
| 49 | + public DaprConfigurationConfigDataLoader(DeferredLogFactory logFactory, DaprClient daprClient, |
| 50 | + DaprCloudConfigProperties daprSecretStoreConfig) { |
| 51 | + this.log = logFactory.getLog(getClass()); |
| 52 | + this.daprClient = daprClient; |
| 53 | + this.daprSecretStoreConfig = daprSecretStoreConfig; |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + /** |
| 58 | + * Load {@link ConfigData} for the given resource. |
| 59 | + * |
| 60 | + * @param context the loader context |
| 61 | + * @param resource the resource to load |
| 62 | + * @return the loaded config data or {@code null} if the location should be skipped |
| 63 | + * @throws IOException on IO error |
| 64 | + * @throws ConfigDataResourceNotFoundException if the resource cannot be found |
| 65 | + */ |
| 66 | + @Override |
| 67 | + public ConfigData load(ConfigDataLoaderContext context, DaprConfigurationConfigDataResource resource) |
| 68 | + throws IOException, ConfigDataResourceNotFoundException { |
| 69 | + DaprCloudConfigClientManager daprClientSecretStoreConfigManager = |
| 70 | + getBean(context, DaprCloudConfigClientManager.class); |
| 71 | + |
| 72 | + daprClient = DaprCloudConfigClientManager.getDaprClient(); |
| 73 | + daprSecretStoreConfig = daprClientSecretStoreConfigManager.getDaprCloudConfigProperties(); |
| 74 | + |
| 75 | + if (resource.getSecretName() == null) { |
| 76 | + return fetchConfig(resource.getStoreName()); |
| 77 | + } else { |
| 78 | + return fetchConfig(resource.getStoreName(), resource.getSecretName()); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private ConfigData fetchConfig(String storeName) { |
| 83 | + Mono<Map<String, Map<String, String>>> secretMapMono = daprClient.getBulkSecret(storeName); |
| 84 | + |
| 85 | + Map<String, Map<String, String>> secretMap = |
| 86 | + secretMapMono.block(Duration.ofMillis(daprSecretStoreConfig.getTimeout())); |
| 87 | + |
| 88 | + if (secretMap == null) { |
| 89 | + return new ConfigData(Collections.emptyList(), IGNORE_IMPORTS, IGNORE_PROFILES, PROFILE_SPECIFIC); |
| 90 | + } |
| 91 | + |
| 92 | + List<PropertySource<?>> sourceList = new ArrayList<>(); |
| 93 | + |
| 94 | + for (Map.Entry<String, Map<String, String>> entry : secretMap.entrySet()) { |
| 95 | + sourceList.addAll(DaprSecretStoreParserHandler.getInstance().parseDaprSecretStoreData(entry.getKey(), |
| 96 | + entry.getValue())); |
| 97 | + } |
| 98 | + |
| 99 | + return new ConfigData(sourceList, IGNORE_IMPORTS, IGNORE_PROFILES, PROFILE_SPECIFIC); |
| 100 | + } |
| 101 | + |
| 102 | + private ConfigData fetchConfig(String storeName, String secretName) { |
| 103 | + Mono<Map<String, String>> secretMapMono = daprClient.getSecret(storeName, secretName); |
| 104 | + |
| 105 | + Map<String, String> secretMap = secretMapMono.block(Duration.ofMillis(daprSecretStoreConfig.getTimeout())); |
| 106 | + |
| 107 | + if (secretMap == null) { |
| 108 | + return new ConfigData(Collections.emptyList(), IGNORE_IMPORTS, IGNORE_PROFILES, PROFILE_SPECIFIC); |
| 109 | + } |
| 110 | + |
| 111 | + List<PropertySource<?>> sourceList = new ArrayList<>( |
| 112 | + DaprSecretStoreParserHandler.getInstance().parseDaprSecretStoreData(secretName, secretMap)); |
| 113 | + |
| 114 | + return new ConfigData(sourceList, IGNORE_IMPORTS, IGNORE_PROFILES, PROFILE_SPECIFIC); |
| 115 | + } |
| 116 | + |
| 117 | + protected <T> T getBean(ConfigDataLoaderContext context, Class<T> type) { |
| 118 | + if (context.getBootstrapContext().isRegistered(type)) { |
| 119 | + return context.getBootstrapContext().get(type); |
| 120 | + } |
| 121 | + return null; |
| 122 | + } |
| 123 | +} |
0 commit comments