-
Notifications
You must be signed in to change notification settings - Fork 13
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
Add secret variables expansion from CNode value #3
Open
ziouf
wants to merge
9
commits into
jenkinsci:master
Choose a base branch
from
ziouf:feature/add-secrets-expansion-in-yaml
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8dc7868
Add secret variables expansion from CNode value
ziouf 9f64797
Add demo file for variable/secret expansion feature as requested
ziouf 567e63f
Merge branch 'master' into feature/add-secrets-expansion-in-yaml
jetersen 8609c45
use ordinal -60 to load after JobDSL JCasC configurator
jetersen 6c1c678
bump parent pom, jenkins core and jcasc
jetersen 316f5bf
fix deprecated method and improve readability
jetersen c5cc527
update Jenkinsfile
jetersen 9fee9aa
add test
jetersen 7d88783
Merge branch 'master' into feature/add-secrets-expansion-in-yaml
jetersen 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 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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
# Using jCasC secret expansion feature | ||
# Secrets are loaded from : | ||
# - Docker secrets | ||
# - Kubernetes/Openshift secrets | ||
# - HashiCorp Vault | ||
# - Environment variables | ||
# | ||
# cf: https://github.com/jenkinsci/configuration-as-code-plugin/blob/master/docs/features/secrets.adoc | ||
|
||
groovy: | ||
|
||
# Asuming secret value is stored in variable named API_KEY | ||
- url: https://my.web.site.com/path/to/my/resource?api_key=${API_KEY} | ||
# Same with basic auth example | ||
- url: https://${USER}:${PASS}@my.web.site.com/path/to/my/resource | ||
|
||
# Asuming secret value is stored in variable named SECRET | ||
- script: > | ||
println "Displaying my secret : ${SECRET}"; |
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,36 +1,28 @@ | ||
package io.jenkins.plugins.cascgroovy; | ||
|
||
import jenkins.model.Jenkins; | ||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
import hudson.EnvVars; | ||
import groovy.lang.Binding; | ||
import groovy.lang.GroovyShell; | ||
import hudson.Extension; | ||
import io.jenkins.plugins.casc.Attribute; | ||
import io.jenkins.plugins.casc.ConfigurationContext; | ||
import io.jenkins.plugins.casc.ConfiguratorException; | ||
import io.jenkins.plugins.casc.Configurator; | ||
import io.jenkins.plugins.casc.RootElementConfigurator; | ||
import io.jenkins.plugins.casc.*; | ||
import io.jenkins.plugins.casc.impl.attributes.MultivaluedAttribute; | ||
import io.jenkins.plugins.casc.model.CNode; | ||
import io.jenkins.plugins.casc.model.Sequence; | ||
import io.jenkins.plugins.casc.model.Mapping; | ||
import jenkins.model.Jenkins; | ||
import org.kohsuke.accmod.Restricted; | ||
import org.kohsuke.accmod.restrictions.NoExternalUse; | ||
import groovy.lang.GroovyShell; | ||
import groovy.lang.Binding; | ||
|
||
import java.io.PrintWriter; | ||
import javax.annotation.CheckForNull; | ||
import javax.annotation.Nonnull; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.*; | ||
|
||
import static io.vavr.API.Try; | ||
import static io.vavr.API.unchecked; | ||
|
||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Tomasz Szandala</a> | ||
*/ | ||
@Extension(optional = true) | ||
@Extension(optional = true, ordinal = -50) | ||
jetersen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@Restricted(NoExternalUse.class) | ||
public class GroovyScriptCaller implements RootElementConfigurator<Boolean[]> { | ||
|
||
|
@@ -56,41 +48,36 @@ public Boolean[] getTargetComponent(ConfigurationContext context) { | |
|
||
@Override | ||
public Boolean[] configure(CNode config, ConfigurationContext context) throws ConfiguratorException { | ||
//JenkinsJobManagement mng = new JenkinsJobManagement(System.out, new EnvVars(), null, null, LookupStrategy.JENKINS_ROOT); | ||
final Sequence sources = config.asSequence(); | ||
final Configurator<GroovyScriptSource> con = context.lookup(GroovyScriptSource.class); | ||
List<Boolean> generated = new ArrayList<>(); | ||
for (CNode source : sources) { | ||
final String script; | ||
try { | ||
script = con.configure(source, context).getScript(); | ||
} catch (IOException e) { | ||
throw new ConfiguratorException(this, "Failed to retrieve Groovy script", e); | ||
} | ||
try { | ||
//Binding binding = new Binding(); | ||
//binding.setVariable("foo", new Integer(2)); | ||
//GroovyShell shell = new GroovyShell(); | ||
//shell.evaluate(script); | ||
|
||
Binding binding = new Binding(); | ||
//binding.setProperty("out",new PrintWriter(stdout,true)); | ||
//binding.setProperty("stdin",stdin); | ||
//binding.setProperty("stdout",stdout); | ||
//binding.setProperty("stderr",stderr); | ||
|
||
GroovyShell groovy = new GroovyShell(Jenkins.getActiveInstance().getPluginManager().uberClassLoader, binding); | ||
groovy.run(script, "Configuration-as-Code-Groovy", new ArrayList()); | ||
|
||
generated.add(true); | ||
|
||
} catch (Exception ex) { | ||
throw new ConfiguratorException(this, "Failed to execute script with hash " + script.hashCode(), ex); | ||
} | ||
} | ||
return generated.toArray(new Boolean[generated.size()]); | ||
final Configurator<GroovyScriptSource> c = context.lookup(GroovyScriptSource.class); | ||
return config.asSequence().stream() | ||
.map(source -> getActualValue(source, context)) | ||
.map(source -> Try(() -> c.configure(source, context).getScript()) | ||
.onSuccess(GroovyScriptCaller.this::runGroovyShell) | ||
.isSuccess()) | ||
.toArray(Boolean[]::new); | ||
} | ||
|
||
private CNode getActualValue(CNode source, ConfigurationContext context) { | ||
return unchecked(() -> source.asMapping().entrySet().stream().findFirst()).apply() | ||
.map(entry -> resolveSourceOrGetValue(entry, context)) | ||
.orElse(source); | ||
} | ||
|
||
private CNode resolveSourceOrGetValue(Map.Entry<String, CNode> entry, ConfigurationContext context) { | ||
final Mapping m = new Mapping(); | ||
m.put( | ||
entry.getKey(), | ||
SecretSourceResolver.resolve(context, unchecked(() -> entry.getValue().asScalar().getValue()).apply()) | ||
); | ||
return m; | ||
} | ||
|
||
private void runGroovyShell(String script) { | ||
final GroovyShell s = new GroovyShell(Jenkins.getActiveInstance().getPluginManager().uberClassLoader, new Binding()); | ||
unchecked(() -> s.run(script, "Configuration-as-Code-Groovy", new ArrayList())); | ||
} | ||
|
||
|
||
@Override | ||
public Boolean[] check(CNode config, ConfigurationContext context) throws ConfiguratorException { | ||
// Any way to dry-run a Groovy script ? | ||
|
@@ -99,7 +86,7 @@ public Boolean[] check(CNode config, ConfigurationContext context) throws Config | |
|
||
@Nonnull | ||
@Override | ||
public List<Configurator> getConfigurators(ConfigurationContext context) { | ||
public List<Configurator<Boolean[]>> getConfigurators(ConfigurationContext context) { | ||
return Collections.singletonList(context.lookup(GroovyScriptSource.class)); | ||
} | ||
|
||
|
@@ -108,4 +95,5 @@ public List<Configurator> getConfigurators(ConfigurationContext context) { | |
public CNode describe(Boolean[] instance, ConfigurationContext context) throws Exception { | ||
return null; | ||
} | ||
|
||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest bumping to 1.42 as ordinal was fixed in 1.41 and secret resolver was improved in 1.42
Parent pom should be bumped as well