Skip to content
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

[JENKINS-63254][JENKINS-47101] add watch variables to EnvironmentExpander #57

Merged
merged 5 commits into from
Oct 14, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@
import hudson.model.Computer;
import hudson.model.Run;
import hudson.model.TaskListener;

import java.io.IOException;
import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

Expand All @@ -48,6 +52,16 @@ public abstract class EnvironmentExpander implements Serializable {
*/
public abstract void expand(@Nonnull EnvVars env) throws IOException, InterruptedException;

/**
* Get the names of environment variables known to contain sensitive values, such as secrets, in the current context.
* Should be overridden by subclasses that bind secret values to environment variables.
*
* @return a set of environment variables known to contain sensitive values
*/
public @Nonnull Set<String> getSensitiveVariables() {
return Collections.emptySet();
}

/**
* Provides an expander for a constant map of string keys and string values. Supports {@link EnvVars#override(String, String)}
* behavior, such as {@code PATH+XYZ} overrides.
Expand Down Expand Up @@ -92,10 +106,25 @@ private static class MergedEnvironmentExpander extends EnvironmentExpander {
this.original = original;
this.subsequent = subsequent;
}

@Override public void expand(EnvVars env) throws IOException, InterruptedException {
original.expand(env);
subsequent.expand(env);
}

@Override
public Set<String> getSensitiveVariables() {
Set<String> originalSensitive = original.getSensitiveVariables();
Set<String> subsequentSensitive = subsequent.getSensitiveVariables();
if (originalSensitive.isEmpty() && subsequentSensitive.isEmpty()) {
return Collections.emptySet();
} else {
Set<String> mergedWatch = new HashSet<>();
mergedWatch.addAll(originalSensitive);
mergedWatch.addAll(subsequentSensitive);
return mergedWatch;
}
}
}

/**
Expand Down Expand Up @@ -142,5 +171,4 @@ private static class MergedEnvironmentExpander extends EnvironmentExpander {
}
return env;
}

}