Skip to content

Commit

Permalink
Move ConfigurationContext one level up to enable configurable max ali…
Browse files Browse the repository at this point in the history
…ases config

This needs to be a property/env since the context is not yet configurable as it has yet to read any YAML
and it might be blocked due to alias limit.

Adding the reading of the property/env to ConfigurationContext
makes sense to avoid YamlUtils to constantly reading the property on every file read
when it attempts to merge all the sources.
  • Loading branch information
jetersen committed May 12, 2020
1 parent 80b822b commit 57aebdd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,8 @@ public void configureWith(YamlSource source) throws ConfiguratorException {

private void configureWith(List<YamlSource> sources) throws ConfiguratorException {
lastTimeLoaded = System.currentTimeMillis();
configureWith( YamlUtils.loadFrom(sources) );
ConfigurationContext context = new ConfigurationContext(registry);
configureWith(YamlUtils.loadFrom(sources, context), context);
closeSources(sources);
}

Expand All @@ -627,7 +628,8 @@ public Map<Source, String> checkWith(YamlSource source) throws ConfiguratorExcep

private Map<Source, String> checkWith(List<YamlSource> sources) throws ConfiguratorException {
if (sources.isEmpty()) return Collections.emptyMap();
return checkWith( YamlUtils.loadFrom(sources) );
ConfigurationContext context = new ConfigurationContext(registry);
return checkWith(YamlUtils.loadFrom(sources, context), context);
}

private void closeSources(List<YamlSource> sources) {
Expand Down Expand Up @@ -727,27 +729,28 @@ private static void detectVaultPluginMissing() {
}
}

private void configureWith(Mapping entries) throws ConfiguratorException {
private void configureWith(Mapping entries,
ConfigurationContext context) throws ConfiguratorException {
// Initialize secret sources
SecretSource.all().forEach(SecretSource::init);

// Check input before actually applying changes,
// so we don't let master in a weird state after some ConfiguratorException has been thrown
final Mapping clone = entries.clone();
checkWith(clone);
checkWith(clone, context);

final ObsoleteConfigurationMonitor monitor = ObsoleteConfigurationMonitor.get();
monitor.reset();
ConfigurationContext context = new ConfigurationContext(registry);
context.clearListeners();
context.addListener(monitor::record);
try (ACLContext acl = ACL.as(ACL.SYSTEM)) {
invokeWith(entries, (configurator, config) -> configurator.configure(config, context));
}
}

public Map<Source, String> checkWith(Mapping entries) throws ConfiguratorException {
public Map<Source, String> checkWith(Mapping entries,
ConfigurationContext context) throws ConfiguratorException {
Map<Source, String> issues = new HashMap<>();
ConfigurationContext context = new ConfigurationContext(registry);
context.addListener( (node,message) -> issues.put(node.getSource(), message) );
invokeWith(entries, (configurator, config) -> configurator.check(config, context));
return issues;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Util;
import io.jenkins.plugins.casc.model.CNode;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.math.NumberUtils;
import org.kohsuke.stapler.Stapler;

/**
Expand All @@ -14,9 +16,12 @@

public class ConfigurationContext implements ConfiguratorRegistry {

public static final String CASC_YAML_MAX_ALIASES_ENV = "CASC_YAML_MAX_ALIASES";
public static final String CASC_YAML_MAX_ALIASES_PROPERTY = "casc.yaml.max.aliases";
private Deprecation deprecation = Deprecation.reject;
private Restriction restriction = Restriction.reject;
private Unknown unknown = Unknown.reject;
private final int yamlMaxAliasesForCollections;

/**
* the model-introspection model to be applied by configuration-as-code.
Expand All @@ -34,12 +39,21 @@ public class ConfigurationContext implements ConfiguratorRegistry {

public ConfigurationContext(ConfiguratorRegistry registry) {
this.registry = registry;
String prop = Util.fixEmptyAndTrim(System.getProperty(
CASC_YAML_MAX_ALIASES_PROPERTY,
System.getenv(CASC_YAML_MAX_ALIASES_ENV)
));
yamlMaxAliasesForCollections = NumberUtils.toInt(prop, 50);
}

public void addListener(Listener listener) {
listeners.add(listener);
}

public void clearListeners() {
listeners.clear();
}

public void warning(@NonNull CNode node, @NonNull String message) {
for (Listener listener : listeners) {
listener.warning(node, message);
Expand Down Expand Up @@ -72,7 +86,9 @@ public void setMode(String mode) {
this.mode = mode;
}


public int getYamlMaxAliasesForCollections() {
return yamlMaxAliasesForCollections;
}

// --- delegate methods for ConfigurationContext

Expand Down
15 changes: 9 additions & 6 deletions plugin/src/main/java/io/jenkins/plugins/casc/yaml/YamlUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.jenkins.plugins.casc.yaml;

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 java.io.IOException;
Expand All @@ -27,12 +28,13 @@ public final class YamlUtils {

public static final Logger LOGGER = Logger.getLogger(ConfigurationAsCode.class.getName());

public static Node merge(List<YamlSource> configs) throws ConfiguratorException {
public static Node merge(List<YamlSource> configs,
ConfigurationContext context) throws ConfiguratorException {
Node root = null;
for (YamlSource source : configs) {
try (Reader r = source.read()) {

final Node node = read(source);
final Node node = read(source, context);

if (root == null) {
root = node;
Expand All @@ -49,9 +51,9 @@ public static Node merge(List<YamlSource> configs) throws ConfiguratorException
return root;
}

public static Node read(YamlSource source) throws IOException {
public static Node read(YamlSource source, ConfigurationContext context) throws IOException {
LoaderOptions loaderOptions = new LoaderOptions();
loaderOptions.setMaxAliasesForCollections(100);
loaderOptions.setMaxAliasesForCollections(context.getYamlMaxAliasesForCollections());
Composer composer = new Composer(
new ParserImpl(new StreamReaderWithSource(source)),
new Resolver(),
Expand Down Expand Up @@ -108,9 +110,10 @@ private static void merge(Node root, Node node, String source) throws Configurat
/**
* Load configuration-as-code model from a set of Yaml sources, merging documents
*/
public static Mapping loadFrom(List<YamlSource> sources) throws ConfiguratorException {
public static Mapping loadFrom(List<YamlSource> sources,
ConfigurationContext context) throws ConfiguratorException {
if (sources.isEmpty()) return Mapping.EMPTY;
final Node merged = merge(sources);
final Node merged = merge(sources, context);
if (merged == null) {
LOGGER.warning("configuration-as-code yaml source returned an empty document.");
return Mapping.EMPTY;
Expand Down

0 comments on commit 57aebdd

Please sign in to comment.