|
| 1 | +/* |
| 2 | + * Copyright © 2026 DataSQRL (contact@datasqrl.com) |
| 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 | +package com.datasqrl.flinkrunner.utils; |
| 17 | + |
| 18 | +import com.fasterxml.jackson.core.JsonParser; |
| 19 | +import com.fasterxml.jackson.databind.DeserializationContext; |
| 20 | +import com.fasterxml.jackson.databind.JsonDeserializer; |
| 21 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 22 | +import com.fasterxml.jackson.databind.module.SimpleModule; |
| 23 | +import java.io.IOException; |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.HashSet; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.regex.Matcher; |
| 28 | +import java.util.regex.Pattern; |
| 29 | +import lombok.extern.slf4j.Slf4j; |
| 30 | + |
| 31 | +/** |
| 32 | + * Environment variable resolving functionality. |
| 33 | + * |
| 34 | + * <p>Resolver instances replace {@code ${VAR}} placeholders with values from a configured |
| 35 | + * environment map. Missing variables either fail resolution in strict mode or remain unresolved in |
| 36 | + * non-strict mode. |
| 37 | + */ |
| 38 | +@Slf4j |
| 39 | +public class EnvVarResolver { |
| 40 | + |
| 41 | + private static final Pattern ENVIRONMENT_VARIABLE_PATTERN = Pattern.compile("\\$\\{(.*?)\\}"); |
| 42 | + |
| 43 | + private final Map<String, String> envVars; |
| 44 | + private final ObjectMapper objectMapper; |
| 45 | + private final boolean strict; |
| 46 | + |
| 47 | + private EnvVarResolver(Map<String, String> envVars, boolean strict) { |
| 48 | + this.envVars = envVars; |
| 49 | + this.strict = strict; |
| 50 | + objectMapper = initObjectMapper(); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Creates a strict resolver backed by the current process environment. |
| 55 | + * |
| 56 | + * @return a resolver backed by {@link System#getenv()} |
| 57 | + */ |
| 58 | + public static EnvVarResolver of() { |
| 59 | + return of(true); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Creates a resolver backed by the current process environment. |
| 64 | + * |
| 65 | + * @param strict whether missing variables should fail resolution |
| 66 | + * @return a resolver backed by {@link System#getenv()} |
| 67 | + */ |
| 68 | + public static EnvVarResolver of(boolean strict) { |
| 69 | + return of(System.getenv(), strict); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Creates a strict resolver backed by the supplied environment variables. |
| 74 | + * |
| 75 | + * @param envVars environment variables used for placeholder resolution |
| 76 | + * @return a resolver backed by the supplied environment variables |
| 77 | + */ |
| 78 | + public static EnvVarResolver of(Map<String, String> envVars) { |
| 79 | + return of(envVars, true); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Creates a resolver backed by the supplied environment variables. |
| 84 | + * |
| 85 | + * @param envVars environment variables used for placeholder resolution |
| 86 | + * @param strict whether missing variables should fail resolution |
| 87 | + * @return a resolver backed by the supplied environment variables |
| 88 | + */ |
| 89 | + public static EnvVarResolver of(Map<String, String> envVars, boolean strict) { |
| 90 | + return new EnvVarResolver(envVars, strict); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Creates a strict resolver backed by the current process environment plus deployment defaults. |
| 95 | + * |
| 96 | + * @return a resolver with deployment defaults applied |
| 97 | + */ |
| 98 | + public static EnvVarResolver withDeploymentDefaults() { |
| 99 | + return withDeploymentDefaults(true); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Creates a resolver backed by the current process environment plus deployment defaults. |
| 104 | + * |
| 105 | + * @param strict whether missing variables should fail resolution |
| 106 | + * @return a resolver with deployment defaults applied |
| 107 | + */ |
| 108 | + public static EnvVarResolver withDeploymentDefaults(boolean strict) { |
| 109 | + return new EnvVarResolver(EnvUtils.getEnvWithDeploymentDefaults(), strict); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Creates a strict resolver backed by the supplied environment variables plus deployment |
| 114 | + * defaults. |
| 115 | + * |
| 116 | + * @param envVars environment variables used for placeholder resolution |
| 117 | + * @return a resolver with deployment defaults applied |
| 118 | + */ |
| 119 | + public static EnvVarResolver withDeploymentDefaults(Map<String, String> envVars) { |
| 120 | + return withDeploymentDefaults(envVars, true); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Creates a resolver backed by the supplied environment variables plus deployment defaults. |
| 125 | + * |
| 126 | + * <p>Deployment defaults are only added when the supplied map does not already contain those |
| 127 | + * keys. |
| 128 | + * |
| 129 | + * @param envVars environment variables used for placeholder resolution |
| 130 | + * @param strict whether missing variables should fail resolution |
| 131 | + * @return a resolver with deployment defaults applied |
| 132 | + */ |
| 133 | + public static EnvVarResolver withDeploymentDefaults(Map<String, String> envVars, boolean strict) { |
| 134 | + var modifiedEnvVars = new HashMap<>(envVars); |
| 135 | + EnvUtils.getDeploymentDefaults().forEach(modifiedEnvVars::putIfAbsent); |
| 136 | + |
| 137 | + return new EnvVarResolver(Map.copyOf(modifiedEnvVars), strict); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Resolves environment variables referenced in a given source string. Searches for environment |
| 142 | + * variable references based on {@link EnvVarResolver#ENVIRONMENT_VARIABLE_PATTERN}. If a blank |
| 143 | + * source string is passed, it will be returned as is. |
| 144 | + * |
| 145 | + * @param src given source string that may contain environment variable references |
| 146 | + * @return a new string with the resolved environment variables |
| 147 | + * @throws IllegalStateException if strict mode is enabled and any referenced environment variable |
| 148 | + * is not available |
| 149 | + */ |
| 150 | + public String resolve(String src) { |
| 151 | + if (src == null || src.isBlank()) { |
| 152 | + return src; |
| 153 | + } |
| 154 | + |
| 155 | + var res = new StringBuilder(); |
| 156 | + // First pass to replace environment variables |
| 157 | + var matcher = ENVIRONMENT_VARIABLE_PATTERN.matcher(src); |
| 158 | + var missingEnvVars = new HashSet<String>(); |
| 159 | + while (matcher.find()) { |
| 160 | + var rawKey = matcher.group(1); |
| 161 | + String key; |
| 162 | + String defaultValue = null; |
| 163 | + |
| 164 | + // Support bash-style default values: ${VAR:-default} or ${VAR:=default} |
| 165 | + int splitIdx = rawKey.indexOf(":-"); |
| 166 | + if (splitIdx == -1) { |
| 167 | + splitIdx = rawKey.indexOf(":="); |
| 168 | + } |
| 169 | + |
| 170 | + if (splitIdx >= 0) { |
| 171 | + key = rawKey.substring(0, splitIdx); |
| 172 | + defaultValue = rawKey.substring(splitIdx + 2); |
| 173 | + } else { |
| 174 | + key = rawKey; |
| 175 | + } |
| 176 | + |
| 177 | + if (envVars.containsKey(key)) { |
| 178 | + var envValue = envVars.get(key); |
| 179 | + matcher.appendReplacement(res, Matcher.quoteReplacement(envValue)); |
| 180 | + } else if (defaultValue != null) { |
| 181 | + matcher.appendReplacement(res, Matcher.quoteReplacement(defaultValue)); |
| 182 | + } else { |
| 183 | + missingEnvVars.add(key); |
| 184 | + } |
| 185 | + } |
| 186 | + matcher.appendTail(res); |
| 187 | + |
| 188 | + if (strict && !missingEnvVars.isEmpty()) { |
| 189 | + throw new IllegalStateException( |
| 190 | + String.format( |
| 191 | + "The following environment variables were referenced, but not found: %s", |
| 192 | + missingEnvVars)); |
| 193 | + } |
| 194 | + |
| 195 | + return res.toString(); |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * Resolves environment variables referenced in a given JSON source string. Searches for |
| 200 | + * environment variable references in any string leaf nodes based on {@link |
| 201 | + * EnvVarResolver#ENVIRONMENT_VARIABLE_PATTERN}. |
| 202 | + * |
| 203 | + * @param jsonSrc given JSON source string that may contain environment variable references |
| 204 | + * @return JSON string with the resolved environment variables |
| 205 | + * @throws IOException if the JSON processing fails in any way |
| 206 | + */ |
| 207 | + public String resolveInJson(String jsonSrc) throws IOException { |
| 208 | + var res = objectMapper.readValue(jsonSrc, Map.class); |
| 209 | + |
| 210 | + return objectMapper.writeValueAsString(res); |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * Creates an {@link ObjectMapper} configured to resolve environment variables in string values. |
| 215 | + * |
| 216 | + * @return an object mapper with environment-variable resolution enabled for string |
| 217 | + * deserialization |
| 218 | + */ |
| 219 | + public ObjectMapper initObjectMapper() { |
| 220 | + return initObjectMapper(new ObjectMapper()); |
| 221 | + } |
| 222 | + |
| 223 | + /** |
| 224 | + * Configures an {@link ObjectMapper} to resolve environment variables in string values. |
| 225 | + * |
| 226 | + * @param mapper mapper to configure |
| 227 | + * @return the supplied mapper with environment-variable resolution enabled for string |
| 228 | + * deserialization |
| 229 | + */ |
| 230 | + public ObjectMapper initObjectMapper(ObjectMapper mapper) { |
| 231 | + var module = new SimpleModule(); |
| 232 | + module.addDeserializer(String.class, new JsonEnvVarDeserializer()); |
| 233 | + mapper.registerModule(module); |
| 234 | + |
| 235 | + return mapper; |
| 236 | + } |
| 237 | + |
| 238 | + private class JsonEnvVarDeserializer extends JsonDeserializer<String> { |
| 239 | + |
| 240 | + @Override |
| 241 | + public String deserialize(JsonParser parser, DeserializationContext ctx) throws IOException { |
| 242 | + var value = parser.getText(); |
| 243 | + return resolve(value); |
| 244 | + } |
| 245 | + } |
| 246 | +} |
0 commit comments