Skip to content

Commit ce736f2

Browse files
committed
[Enhance](cdc) Add support for environment variable resolution in parameter mapping
1 parent 8f0052c commit ce736f2

File tree

2 files changed

+114
-1
lines changed

2 files changed

+114
-1
lines changed

flink-doris-connector/src/main/java/org/apache/doris/flink/tools/cdc/CdcTools.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import java.util.List;
3939
import java.util.Map;
4040
import java.util.Objects;
41+
import java.util.regex.Matcher;
42+
import java.util.regex.Pattern;
4143

4244
/** cdc sync tools. */
4345
public class CdcTools {
@@ -221,7 +223,31 @@ public static Map<String, String> getConfigMap(MultipleParameterTool params, Str
221223
for (String param : params.getMultiParameter(key)) {
222224
String[] kv = param.split("=", 2);
223225
if (kv.length == 2) {
224-
map.put(kv[0].trim(), kv[1].trim());
226+
String originalValue = kv[1].trim();
227+
String resolvedValue = originalValue;
228+
229+
// Regex to find environment variables like $VAR or ${VAR}
230+
Pattern pattern = Pattern.compile("\\$(?:([A-Za-z_][A-Za-z0-9_]*)|\\{([A-Za-z_][A-Za-z0-9_]*)\\})");
231+
Matcher matcher = pattern.matcher(originalValue);
232+
StringBuffer sb = new StringBuffer();
233+
boolean varFound = false;
234+
while (matcher.find()) {
235+
varFound = true;
236+
String varName = matcher.group(1) != null ? matcher.group(1) : matcher.group(2);
237+
String envValue = System.getenv(varName);
238+
if (envValue != null) {
239+
// Replace with environment variable value
240+
matcher.appendReplacement(sb, Matcher.quoteReplacement(envValue));
241+
} else {
242+
// If environment variable is not found, keep the original placeholder
243+
matcher.appendReplacement(sb, Matcher.quoteReplacement(matcher.group(0)));
244+
}
245+
}
246+
if (varFound) {
247+
matcher.appendTail(sb);
248+
resolvedValue = sb.toString();
249+
}
250+
map.put(kv[0].trim(), resolvedValue);
225251
continue;
226252
} else if (kv.length == 1 && EMPTY_KEYS.contains(kv[0])) {
227253
map.put(kv[0].trim(), "");

flink-doris-connector/src/test/java/org/apache/doris/flink/tools/cdc/CdcToolsTest.java

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,91 @@ private void assertEquals(
115115
Assert.assertTrue(valueConf.contains(value));
116116
}
117117
}
118+
119+
@Test
120+
public void testGetConfigMapWithEnvironmentVariables() {
121+
// Test cases for environment variable substitution.
122+
// We assume these environment variables are NOT set in the test environment,
123+
// so they should resolve to their placeholder strings.
124+
125+
// Case 1: Simple env var placeholder
126+
MultipleParameterTool params1 =
127+
MultipleParameterTool.fromArgs(
128+
new String[] {"--test-conf", "db.user=$DB_USER_UNSET"});
129+
Map<String, String> expected1 = new HashMap<>();
130+
expected1.put("db.user", "$DB_USER_UNSET");
131+
Assert.assertEquals(
132+
expected1, CdcTools.getConfigMap(params1, "test-conf"));
133+
134+
// Case 2: Env var with braces placeholder
135+
MultipleParameterTool params2 =
136+
MultipleParameterTool.fromArgs(
137+
new String[] {"--test-conf", "db.pass=${DB_PASS_UNSET}"});
138+
Map<String, String> expected2 = new HashMap<>();
139+
expected2.put("db.pass", "${DB_PASS_UNSET}");
140+
Assert.assertEquals(
141+
expected2, CdcTools.getConfigMap(params2, "test-conf"));
142+
143+
// Case 3: Mix of plain string and env var placeholder
144+
MultipleParameterTool params3 =
145+
MultipleParameterTool.fromArgs(
146+
new String[] {
147+
"--test-conf", "db.host=localhost", "--test-conf", "db.port=$DB_PORT_UNSET"
148+
});
149+
Map<String, String> expected3 = new HashMap<>();
150+
expected3.put("db.host", "localhost");
151+
expected3.put("db.port", "$DB_PORT_UNSET");
152+
Assert.assertEquals(
153+
expected3, CdcTools.getConfigMap(params3, "test-conf"));
154+
155+
// Case 4: Env var within a string
156+
MultipleParameterTool params4 =
157+
MultipleParameterTool.fromArgs(
158+
new String[] {
159+
"--test-conf", "conn.string=jdbc:mysql://$DB_HOST_UNSET:3306/mydb"
160+
});
161+
Map<String, String> expected4 = new HashMap<>();
162+
expected4.put("conn.string", "jdbc:mysql://$DB_HOST_UNSET:3306/mydb");
163+
Assert.assertEquals(
164+
expected4, CdcTools.getConfigMap(params4, "test-conf"));
165+
166+
// Case 5: Multiple env vars in one string
167+
MultipleParameterTool params5 =
168+
MultipleParameterTool.fromArgs(
169+
new String[] {"--test-conf", "credentials=user:$USER_UNSET,pass:$PASS_UNSET"});
170+
Map<String, String> expected5 = new HashMap<>();
171+
expected5.put("credentials", "user:$USER_UNSET,pass:$PASS_UNSET");
172+
Assert.assertEquals(
173+
expected5, CdcTools.getConfigMap(params5, "test-conf"));
174+
175+
// Case 6: No env vars (regular behavior)
176+
MultipleParameterTool params6 =
177+
MultipleParameterTool.fromArgs(
178+
new String[] {"--test-conf", "key1=value1", "--test-conf", "key2=value2"});
179+
Map<String, String> expected6 = new HashMap<>();
180+
expected6.put("key1", "value1");
181+
expected6.put("key2", "value2");
182+
Assert.assertEquals(
183+
expected6, CdcTools.getConfigMap(params6, "test-conf"));
184+
185+
// Case 7: Env var for a key that allows empty value (e.g. password), resolves to placeholder
186+
MultipleParameterTool params7 =
187+
MultipleParameterTool.fromArgs(
188+
new String[] {"--test-conf", "password=$PASSWORD_UNSET"});
189+
Map<String, String> expected7 = new HashMap<>();
190+
expected7.put("password", "$PASSWORD_UNSET"); // DatabaseSyncConfig.PASSWORD is in EMPTY_KEYS
191+
Assert.assertEquals(
192+
expected7, CdcTools.getConfigMap(params7, "test-conf"));
193+
194+
// Case 8: Env var that resolves to an empty string (if it were set to empty)
195+
// For this test, we simulate it by having the placeholder itself, as we can't set it to empty easily here.
196+
// If $EMPTY_VAR was set to "", the result for "key" would be "".
197+
// Since it's not set, it remains "$EMPTY_VAR".
198+
MultipleParameterTool params8 =
199+
MultipleParameterTool.fromArgs(new String[] {"--test-conf", "key=$EMPTY_VAR"});
200+
Map<String, String> expected8 = new HashMap<>();
201+
expected8.put("key", "$EMPTY_VAR");
202+
Assert.assertEquals(
203+
expected8, CdcTools.getConfigMap(params8, "test-conf"));
204+
}
118205
}

0 commit comments

Comments
 (0)