-
Notifications
You must be signed in to change notification settings - Fork 748
Expand file tree
/
Copy pathYamlUtils.java
More file actions
453 lines (394 loc) · 18.5 KB
/
YamlUtils.java
File metadata and controls
453 lines (394 loc) · 18.5 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package io.jenkins.plugins.casc.yaml;
import static java.nio.charset.StandardCharsets.UTF_8;
import io.jenkins.plugins.casc.ConfigurationAsCode;
import io.jenkins.plugins.casc.ConfigurationContext;
import io.jenkins.plugins.casc.ConfiguratorException;
import io.jenkins.plugins.casc.model.Mapping;
import jakarta.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.composer.Composer;
import org.yaml.snakeyaml.error.YAMLException;
import org.yaml.snakeyaml.nodes.MappingNode;
import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.nodes.NodeTuple;
import org.yaml.snakeyaml.nodes.ScalarNode;
import org.yaml.snakeyaml.nodes.SequenceNode;
import org.yaml.snakeyaml.parser.ParserImpl;
import org.yaml.snakeyaml.reader.StreamReader;
import org.yaml.snakeyaml.resolver.Resolver;
/**
* @author <a href="mailto:[email protected]">Nicolas De Loof</a>
*/
public final class YamlUtils {
public static final Logger LOGGER = Logger.getLogger(ConfigurationAsCode.class.getName());
public static Node merge(List<YamlSource> sources, ConfigurationContext context) throws ConfiguratorException {
Node root = null;
MergeStrategy mergeStrategy = MergeStrategyFactory.getMergeStrategyOrDefault(context.getMergeStrategy());
Map<String, Node> parsedCache = new HashMap<>();
for (YamlSource<?> source : sources) {
try (Reader reader = reader(source)) {
Node node = read(source, reader, context);
if (node != null) {
Set<String> visited = new HashSet<>();
visited.add(getCanonicalId(source));
node = resolveExtends(node, source, context, visited, parsedCache);
}
if (root == null) {
root = node;
} else {
if (node != null) {
mergeStrategy.merge(root, node, source.toString());
}
}
} catch (IOException io) {
throw new ConfiguratorException("Failed to read " + source, io);
}
}
return root;
}
public static Node read(YamlSource source, Reader reader, ConfigurationContext context) throws IOException {
LoaderOptions loaderOptions = new LoaderOptions();
loaderOptions.setCodePointLimit(context.getYamlCodePointLimit());
loaderOptions.setMaxAliasesForCollections(context.getYamlMaxAliasesForCollections());
Composer composer = new Composer(
new ParserImpl(new StreamReaderWithSource(source, reader), loaderOptions),
new Resolver(),
loaderOptions);
try {
return composer.getSingleNode();
} catch (YAMLException e) {
if (e.getMessage().startsWith("Number of aliases for non-scalar nodes exceeds the specified max")) {
throw new ConfiguratorException(String.format(
"%s%nYou can increase the maximum by setting an environment variable or property%n ENV: %s=\"100\"%n PROPERTY: -D%s=\"100\"",
e.getMessage(),
ConfigurationContext.CASC_YAML_MAX_ALIASES_ENV,
ConfigurationContext.CASC_YAML_MAX_ALIASES_PROPERTY));
}
throw e;
}
}
public static Reader reader(YamlSource<?> source) throws IOException {
Object src = source.source;
if (src instanceof String) {
final URL url = URI.create((String) src).toURL();
return new InputStreamReader(url.openStream(), UTF_8);
} else if (src instanceof InputStream) {
return new InputStreamReader((InputStream) src, UTF_8);
} else if (src instanceof HttpServletRequest) {
return new InputStreamReader(((HttpServletRequest) src).getInputStream(), UTF_8);
} else if (src instanceof Path) {
return Files.newBufferedReader((Path) src);
}
throw new IOException(String.format("Unknown %s", source));
}
/**
* Load configuration-as-code model from a set of Yaml sources, merging documents
*/
public static Mapping loadFrom(List<YamlSource> sources, ConfigurationContext context)
throws ConfiguratorException {
if (sources.isEmpty()) {
return Mapping.EMPTY;
}
final Node merged = merge(sources, context);
if (merged == null) {
LOGGER.warning("configuration-as-code yaml source returned an empty document.");
return Mapping.EMPTY;
}
return loadFrom(merged, context);
}
/**
* Load configuration-as-code model from a snakeyaml Node
*/
private static Mapping loadFrom(Node node, ConfigurationContext context) {
final LoaderOptions loaderOptions = new LoaderOptions();
loaderOptions.setMaxAliasesForCollections(context.getYamlMaxAliasesForCollections());
loaderOptions.setCodePointLimit(context.getYamlCodePointLimit());
final ModelConstructor constructor = new ModelConstructor(loaderOptions);
constructor.setComposer(
new Composer(
new ParserImpl(new StreamReader(Reader.nullReader()), loaderOptions),
new Resolver(),
loaderOptions) {
@Override
public Node getSingleNode() {
return node;
}
});
return (Mapping) constructor.getSingleData(Mapping.class);
}
private static Node resolveExtends(
Node node,
YamlSource<?> currentSource,
ConfigurationContext context,
Set<String> visited,
Map<String, Node> parsedCache)
throws ConfiguratorException {
if (node instanceof MappingNode mapNode) {
List<NodeTuple> originalTuples = mapNode.getValue();
List<NodeTuple> resolvedTuples = new ArrayList<>();
List<String> extendsPaths = new ArrayList<>();
boolean hasChanges = false;
for (NodeTuple tuple : originalTuples) {
Node keyNode = tuple.getKeyNode();
Node valueNode = tuple.getValueNode();
if (keyNode instanceof ScalarNode key && "_extends".equals(key.getValue())) {
if (valueNode instanceof ScalarNode scalar) {
if (scalar.getValue() == null
|| scalar.getValue().trim().isEmpty()) {
throw new ConfiguratorException("The '_extends' property cannot be empty.");
}
extendsPaths.add(scalar.getValue());
} else if (valueNode instanceof SequenceNode seq) {
if (seq.getValue().isEmpty()) {
throw new ConfiguratorException("The '_extends' list cannot be empty.");
}
for (Node item : seq.getValue()) {
if (item instanceof ScalarNode scalarItem) {
String path = scalarItem.getValue();
if (path == null || path.trim().isEmpty()) {
throw new ConfiguratorException(
"Items in the '_extends' list cannot be null or empty strings.");
}
extendsPaths.add(path);
} else {
throw new ConfiguratorException(String.format(
"Invalid item in '_extends': expected string but got %s in %s",
item.getNodeId(), currentSource));
}
}
} else {
throw new ConfiguratorException(String.format(
"Invalid value for '_extends' key. Expected string or list of strings, but found: %s",
valueNode.getNodeId()));
}
hasChanges = true;
continue;
}
Node resolvedValue =
resolveExtends(valueNode, currentSource, context, Set.copyOf(visited), parsedCache);
if (resolvedValue != valueNode) {
resolvedTuples.add(new NodeTuple(keyNode, resolvedValue));
hasChanges = true;
} else {
resolvedTuples.add(tuple);
}
}
if (!hasChanges) {
return node;
}
MappingNode newMapNode = new MappingNode(
mapNode.getTag(),
true,
resolvedTuples,
mapNode.getStartMark(),
mapNode.getEndMark(),
mapNode.getFlowStyle());
if (!extendsPaths.isEmpty()) {
Node baseNode = null;
for (String path : extendsPaths) {
YamlSource<?> parentSource = resolveRelativeSource(currentSource, path);
String parentId = getCanonicalId(parentSource);
if (visited.contains(parentId)) {
throw new ConfiguratorException("Circular _extends dependency detected: " + parentId);
}
Node parentNode;
if (parsedCache.containsKey(parentId)) {
parentNode = cloneNode(parsedCache.get(parentId));
} else {
Set<String> newVisited = new HashSet<>(visited);
newVisited.add(parentId);
try (Reader parentReader = reader(parentSource)) {
parentNode = read(parentSource, parentReader, context);
} catch (IOException | YAMLException e) {
throw new ConfiguratorException("Failed to read extended config: " + path, e);
}
parentNode =
resolveExtends(parentNode, parentSource, context, Set.copyOf(newVisited), parsedCache);
parentNode = cloneNode(parentNode);
parsedCache.put(parentId, parentNode);
}
if (baseNode == null) {
baseNode = parentNode;
} else {
baseNode = deepMergeNodes(baseNode, parentNode);
}
}
return deepMergeNodes(baseNode, newMapNode);
}
return newMapNode;
} else if (node instanceof SequenceNode seqNode) {
List<Node> originalChildren = seqNode.getValue();
List<Node> resolvedChildren = new ArrayList<>();
boolean hasChanges = false;
for (Node child : originalChildren) {
Node resolvedChild = resolveExtends(child, currentSource, context, Set.copyOf(visited), parsedCache);
resolvedChildren.add(resolvedChild);
if (resolvedChild != child) {
hasChanges = true;
}
}
if (hasChanges) {
return new SequenceNode(
seqNode.getTag(),
true,
resolvedChildren,
seqNode.getStartMark(),
seqNode.getEndMark(),
seqNode.getFlowStyle());
}
}
return node;
}
private static YamlSource<?> resolveRelativeSource(YamlSource<?> currentSource, String extendsPath)
throws ConfiguratorException {
if (ConfigurationAsCode.isSupportedURI(extendsPath)) {
return YamlSource.of(extendsPath);
}
Object src = currentSource.source;
if (src instanceof Path currentPath) {
Path resolvedPath = currentPath.resolveSibling(extendsPath).normalize();
if (!Files.exists(resolvedPath)) {
throw new ConfiguratorException("Extended configuration file does not exist: " + resolvedPath);
}
return YamlSource.of(resolvedPath);
} else if (src instanceof String) {
try {
URI currentUri = new URI((String) src);
URI resolvedUri = currentUri.resolve(extendsPath).normalize();
return YamlSource.of(resolvedUri.toString());
} catch (URISyntaxException e) {
throw new ConfiguratorException("Invalid base URI to resolve against: " + src, e);
}
} else if (src instanceof HttpServletRequest || src instanceof InputStream) {
throw new ConfiguratorException(
"Relative `_extends` paths ('" + extendsPath + "') are not supported for inline configurations. "
+ "Use an absolute file: or http(s): URL instead.");
}
throw new ConfiguratorException("Cannot resolve relative path '" + extendsPath + "' for source type: "
+ src.getClass().getSimpleName());
}
private static String extractKey(Node keyNode) throws ConfiguratorException {
if (keyNode instanceof ScalarNode scalarKey) {
return scalarKey.getValue();
}
throw new ConfiguratorException(String.format(
"Invalid YAML key type: %s. JCasC only supports scalar (string) keys.", keyNode.getNodeId()));
}
private static Node deepMergeNodes(Node base, Node override) throws ConfiguratorException {
if (base != null && override != null) {
boolean isBaseMap = base instanceof MappingNode;
boolean isBaseSeq = base instanceof SequenceNode;
boolean isOverrideMap = override instanceof MappingNode;
boolean isOverrideSeq = override instanceof SequenceNode;
if ((isBaseMap && isOverrideSeq) || (isBaseSeq && isOverrideMap)) {
throw new ConfiguratorException(String.format(
"Type mismatch during merge: Cannot merge a %s and a %s. "
+ "Check your '_extends' hierarchy for incompatible data structures.",
override.getNodeId(), base.getNodeId()));
}
}
if (base instanceof MappingNode baseMap && override instanceof MappingNode overrideMap) {
Map<String, NodeTuple> mergedTuples = new LinkedHashMap<>();
for (NodeTuple bTuple : baseMap.getValue()) {
String key = extractKey(bTuple.getKeyNode());
mergedTuples.put(key, bTuple);
}
for (NodeTuple oTuple : overrideMap.getValue()) {
String key = extractKey(oTuple.getKeyNode());
if (mergedTuples.containsKey(key)) {
Node bValue = mergedTuples.get(key).getValueNode();
Node oValue = oTuple.getValueNode();
if (bValue instanceof MappingNode && oValue instanceof MappingNode) {
Node mergedValue = deepMergeNodes(bValue, oValue);
mergedTuples.put(key, new NodeTuple(oTuple.getKeyNode(), mergedValue));
} else {
mergedTuples.put(key, new NodeTuple(oTuple.getKeyNode(), cloneNode(oValue)));
}
} else {
mergedTuples.put(key, new NodeTuple(oTuple.getKeyNode(), cloneNode(oTuple.getValueNode())));
}
}
return new MappingNode(
overrideMap.getTag(),
true,
new ArrayList<>(mergedTuples.values()),
baseMap.getStartMark(),
overrideMap.getEndMark(),
overrideMap.getFlowStyle());
}
return cloneNode(override);
}
private static Node cloneNode(Node node) {
if (node == null) {
return null;
}
if (node instanceof MappingNode mapNode) {
List<NodeTuple> clonedTuples = new ArrayList<>();
for (NodeTuple tuple : mapNode.getValue()) {
clonedTuples.add(new NodeTuple(cloneNode(tuple.getKeyNode()), cloneNode(tuple.getValueNode())));
}
return new MappingNode(
mapNode.getTag(),
true,
clonedTuples,
mapNode.getStartMark(),
mapNode.getEndMark(),
mapNode.getFlowStyle());
} else if (node instanceof SequenceNode seqNode) {
List<Node> clonedChildren = new ArrayList<>();
for (Node child : seqNode.getValue()) {
clonedChildren.add(cloneNode(child));
}
return new SequenceNode(
seqNode.getTag(),
true,
clonedChildren,
seqNode.getStartMark(),
seqNode.getEndMark(),
seqNode.getFlowStyle());
}
if (node instanceof ScalarNode scalarNode) {
return new ScalarNode(
scalarNode.getTag(),
scalarNode.getValue(),
scalarNode.getStartMark(),
scalarNode.getEndMark(),
scalarNode.getScalarStyle());
}
return node;
}
private static String getCanonicalId(YamlSource<?> source) {
Object src = source.source;
if (src instanceof Path path) {
try {
return path.toRealPath().toString();
} catch (IOException e) {
return path.toAbsolutePath().normalize().toString();
}
} else if (src instanceof String url) {
try {
return new URI(url).normalize().toString();
} catch (URISyntaxException e) {
return url;
}
}
return source.toString();
}
}