-
Notifications
You must be signed in to change notification settings - Fork 909
Load file config YAML using core schema, ensure that env var substiut… #6436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jack-berg
merged 3 commits into
open-telemetry:main
from
jack-berg:file-config-core-schema
May 20, 2024
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,9 +24,15 @@ | |
import java.util.regex.Pattern; | ||
import org.snakeyaml.engine.v2.api.Load; | ||
import org.snakeyaml.engine.v2.api.LoadSettings; | ||
import org.snakeyaml.engine.v2.common.ScalarStyle; | ||
import org.snakeyaml.engine.v2.constructor.StandardConstructor; | ||
import org.snakeyaml.engine.v2.exceptions.ConstructorException; | ||
import org.snakeyaml.engine.v2.exceptions.YamlEngineException; | ||
import org.snakeyaml.engine.v2.nodes.MappingNode; | ||
import org.yaml.snakeyaml.Yaml; | ||
import org.snakeyaml.engine.v2.nodes.Node; | ||
import org.snakeyaml.engine.v2.nodes.NodeTuple; | ||
import org.snakeyaml.engine.v2.nodes.ScalarNode; | ||
import org.snakeyaml.engine.v2.schema.CoreSchema; | ||
|
||
/** | ||
* Configure {@link OpenTelemetrySdk} from YAML configuration files conforming to the schema in <a | ||
|
@@ -127,7 +133,7 @@ static OpenTelemetryConfiguration parse( | |
|
||
// Visible for testing | ||
static Object loadYaml(InputStream inputStream, Map<String, String> environmentVariables) { | ||
LoadSettings settings = LoadSettings.builder().build(); | ||
LoadSettings settings = LoadSettings.builder().setSchema(new CoreSchema()).build(); | ||
Load yaml = new Load(settings, new EnvSubstitutionConstructor(settings, environmentVariables)); | ||
return yaml.loadFromInputStream(inputStream); | ||
} | ||
|
@@ -146,51 +152,93 @@ static Object loadYaml(InputStream inputStream, Map<String, String> environmentV | |
private static final class EnvSubstitutionConstructor extends StandardConstructor { | ||
|
||
// Yaml is not thread safe but this instance is always used on the same thread | ||
private final Yaml yaml = new Yaml(); | ||
private final Load load; | ||
private final Map<String, String> environmentVariables; | ||
|
||
private EnvSubstitutionConstructor( | ||
LoadSettings loadSettings, Map<String, String> environmentVariables) { | ||
super(loadSettings); | ||
load = new Load(loadSettings); | ||
this.environmentVariables = environmentVariables; | ||
} | ||
|
||
/** | ||
* Implementation is same as {@link | ||
* org.snakeyaml.engine.v2.constructor.BaseConstructor#constructMapping(MappingNode)} except we | ||
* override the resolution of values with our custom {@link #constructValueObject(Node)}, which | ||
* performs environment variable substitution. | ||
*/ | ||
@Override | ||
@SuppressWarnings({"ReturnValueIgnored", "CatchingUnchecked"}) | ||
protected Map<Object, Object> constructMapping(MappingNode node) { | ||
// First call the super to construct mapping from MappingNode as usual | ||
Map<Object, Object> result = super.constructMapping(node); | ||
|
||
// Iterate through the map entries, and: | ||
// 1. Identify entries which are scalar strings eligible for environment variable substitution | ||
// 2. Apply environment variable substitution | ||
// 3. Re-parse substituted value so it has correct type (i.e. yaml.load(newVal)) | ||
for (Map.Entry<Object, Object> entry : result.entrySet()) { | ||
Object value = entry.getValue(); | ||
if (!(value instanceof String)) { | ||
continue; | ||
Map<Object, Object> mapping = settings.getDefaultMap().apply(node.getValue().size()); | ||
List<NodeTuple> nodeValue = node.getValue(); | ||
for (NodeTuple tuple : nodeValue) { | ||
Node keyNode = tuple.getKeyNode(); | ||
Node valueNode = tuple.getValueNode(); | ||
|
||
Object key = constructObject(keyNode); | ||
if (key != null) { | ||
try { | ||
key.hashCode(); // check circular dependencies | ||
} catch (Exception e) { | ||
throw new ConstructorException( | ||
"while constructing a mapping", | ||
node.getStartMark(), | ||
"found unacceptable key " + key, | ||
tuple.getKeyNode().getStartMark(), | ||
e); | ||
} | ||
} | ||
|
||
String val = (String) value; | ||
Matcher matcher = ENV_VARIABLE_REFERENCE.matcher(val); | ||
if (!matcher.find()) { | ||
continue; | ||
Object value = constructValueObject(valueNode); | ||
if (keyNode.isRecursive()) { | ||
if (settings.getAllowRecursiveKeys()) { | ||
postponeMapFilling(mapping, key, value); | ||
} else { | ||
throw new YamlEngineException( | ||
"Recursive key for mapping is detected but it is not configured to be allowed."); | ||
} | ||
} else { | ||
mapping.put(key, value); | ||
} | ||
} | ||
|
||
int offset = 0; | ||
StringBuilder newVal = new StringBuilder(); | ||
do { | ||
MatchResult matchResult = matcher.toMatchResult(); | ||
String replacement = environmentVariables.getOrDefault(matcher.group(1), ""); | ||
newVal.append(val, offset, matchResult.start()).append(replacement); | ||
offset = matchResult.end(); | ||
} while (matcher.find()); | ||
if (offset != val.length()) { | ||
newVal.append(val, offset, val.length()); | ||
} | ||
entry.setValue(yaml.load(newVal.toString())); | ||
return mapping; | ||
} | ||
|
||
private Object constructValueObject(Node node) { | ||
if (!(node instanceof ScalarNode)) { | ||
return super.constructObject(node); | ||
jack-berg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
Object value = super.constructObject(node); | ||
jack-berg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
jack-berg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if (!(value instanceof String)) { | ||
return value; | ||
} | ||
|
||
String val = (String) value; | ||
Matcher matcher = ENV_VARIABLE_REFERENCE.matcher(val); | ||
if (!matcher.find()) { | ||
return value; | ||
} | ||
|
||
return result; | ||
int offset = 0; | ||
StringBuilder newVal = new StringBuilder(); | ||
ScalarStyle scalarStyle = ((ScalarNode) node).getScalarStyle(); | ||
do { | ||
MatchResult matchResult = matcher.toMatchResult(); | ||
String replacement = environmentVariables.getOrDefault(matcher.group(1), ""); | ||
newVal.append(val, offset, matchResult.start()).append(replacement); | ||
offset = matchResult.end(); | ||
} while (matcher.find()); | ||
if (offset != val.length()) { | ||
newVal.append(val, offset, val.length()); | ||
} | ||
// If the value was double quoted, retain the double quotes so we don't change a value | ||
// intended to be a string to a different type after environment variable substitution | ||
if (scalarStyle == ScalarStyle.DOUBLE_QUOTED && newVal.length() != 0) { | ||
newVal.insert(0, "\""); | ||
newVal.append("\""); | ||
} | ||
return load.loadFromString(newVal.toString()); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.