-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
ConfigReplacer.java
59 lines (51 loc) · 2 KB
/
ConfigReplacer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* Copyright (c) 2022 Airbyte, Inc., all rights reserved.
*/
package io.airbyte.workers.utils;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.JsonNode;
import io.airbyte.config.AllowedHosts;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.text.StringSubstitutor;
/**
* This class takes values from a connector's configuration and uses it to fill in template-string
* values. It substitutes strings with ${} access, e.g. "The ${animal} jumped over the ${target}"
* with {animal: fox, target: fence}
*/
public class ConfigReplacer {
/**
* Note: This method does not interact with the secret manager. It is currently expected that all
* replacement values are not secret (e.g. host vs password). This also assumed that the JSON config
* for a connector has a single depth.
*/
public AllowedHosts getAllowedHosts(AllowedHosts allowedHosts, JsonNode config) throws IOException {
if (allowedHosts == null || allowedHosts.getHosts() == null) {
return null;
}
final List<String> resolvedHosts = new ArrayList<>();
final Map<String, String> valuesMap = new HashMap<>();
final JsonParser jsonParser = config.traverse();
while (!jsonParser.isClosed()) {
if (jsonParser.nextToken() == JsonToken.FIELD_NAME) {
final String key = jsonParser.getCurrentName();
if (config.get(key) != null) {
valuesMap.put(key, config.get(key).textValue());
}
}
}
final StringSubstitutor sub = new StringSubstitutor(valuesMap);
final List<String> hosts = allowedHosts.getHosts();
for (String host : hosts) {
final String replacedString = sub.replace(host);
resolvedHosts.add(replacedString);
}
final AllowedHosts resolvedAllowedHosts = new AllowedHosts();
resolvedAllowedHosts.setHosts(resolvedHosts);
return resolvedAllowedHosts;
}
}