-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JENKINS-64338] - mask credentials also outside of log (#174)
fixes SECURITY-2213
- Loading branch information
Showing
9 changed files
with
443 additions
and
185 deletions.
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
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
191 changes: 191 additions & 0 deletions
191
src/main/java/com/datapipe/jenkins/vault/VaultBindingStep.java
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,191 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* <p> | ||
* Copyright (c) 2016 Datapipe, Inc. | ||
* <p> | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* <p> | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* <p> | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package com.datapipe.jenkins.vault; | ||
|
||
import com.datapipe.jenkins.vault.configuration.VaultConfiguration; | ||
import com.datapipe.jenkins.vault.log.MaskingConsoleLogFilter; | ||
import com.datapipe.jenkins.vault.model.VaultSecret; | ||
import com.google.common.annotations.VisibleForTesting; | ||
import edu.umd.cs.findbugs.annotations.CheckForNull; | ||
import hudson.EnvVars; | ||
import hudson.Extension; | ||
import hudson.console.ConsoleLogFilter; | ||
import hudson.model.Run; | ||
import hudson.model.TaskListener; | ||
import hudson.util.Secret; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import javax.annotation.Nonnull; | ||
import org.jenkinsci.plugins.workflow.steps.BodyExecutionCallback.TailCall; | ||
import org.jenkinsci.plugins.workflow.steps.BodyInvoker; | ||
import org.jenkinsci.plugins.workflow.steps.EnvironmentExpander; | ||
import org.jenkinsci.plugins.workflow.steps.GeneralNonBlockingStepExecution; | ||
import org.jenkinsci.plugins.workflow.steps.Step; | ||
import org.jenkinsci.plugins.workflow.steps.StepContext; | ||
import org.jenkinsci.plugins.workflow.steps.StepDescriptor; | ||
import org.jenkinsci.plugins.workflow.steps.StepExecution; | ||
import org.kohsuke.stapler.DataBoundConstructor; | ||
import org.kohsuke.stapler.DataBoundSetter; | ||
|
||
public class VaultBindingStep extends Step { | ||
|
||
private VaultConfiguration configuration; | ||
private List<VaultSecret> vaultSecrets; | ||
|
||
@DataBoundConstructor | ||
public VaultBindingStep(@CheckForNull List<VaultSecret> vaultSecrets) { | ||
this.vaultSecrets = vaultSecrets; | ||
} | ||
|
||
public List<VaultSecret> getVaultSecrets() { | ||
return vaultSecrets; | ||
} | ||
|
||
@DataBoundSetter | ||
public void setConfiguration(VaultConfiguration configuration) { | ||
this.configuration = configuration; | ||
} | ||
|
||
public VaultConfiguration getConfiguration() { | ||
return configuration; | ||
} | ||
|
||
@Override | ||
public StepExecution start(StepContext context) throws Exception { | ||
return new Execution(this, context); | ||
} | ||
|
||
protected static class Execution extends GeneralNonBlockingStepExecution { | ||
|
||
private static final long serialVersionUID = 1; | ||
|
||
private transient VaultBindingStep step; | ||
private transient VaultAccessor vaultAccessor; | ||
|
||
public Execution(VaultBindingStep step, StepContext context) { | ||
super(context); | ||
this.step = step; | ||
} | ||
|
||
@VisibleForTesting | ||
public void setVaultAccessor(VaultAccessor vaultAccessor) { | ||
this.vaultAccessor = vaultAccessor; | ||
} | ||
|
||
@Override | ||
public boolean start() throws Exception { | ||
run(this::doStart); | ||
return false; | ||
} | ||
|
||
private void doStart() throws Exception { | ||
Run<?, ?> run = getContext().get(Run.class); | ||
TaskListener listener = getContext().get(TaskListener.class); | ||
EnvVars envVars = getContext().get(EnvVars.class); | ||
|
||
Map<String, String> overrides = VaultAccessor | ||
.retrieveVaultSecrets(run, listener.getLogger(), envVars, vaultAccessor, | ||
step.getConfiguration(), step.getVaultSecrets()); | ||
|
||
List<String> secretValues = new ArrayList<>(); | ||
secretValues.addAll(overrides.values()); | ||
|
||
getContext().newBodyInvoker() | ||
.withContext(EnvironmentExpander.merge(getContext().get(EnvironmentExpander.class), | ||
new VaultBindingStep.Overrider(overrides))) | ||
.withContext(BodyInvoker | ||
.mergeConsoleLogFilters(getContext().get(ConsoleLogFilter.class), | ||
new MaskingConsoleLogFilter(run.getCharset().name(), secretValues))) | ||
.withCallback(new Callback()) | ||
.start(); | ||
} | ||
} | ||
|
||
private static final class Overrider extends EnvironmentExpander { | ||
|
||
private static final long serialVersionUID = 1; | ||
|
||
private final Map<String, Secret> overrides = new HashMap<String, Secret>(); | ||
|
||
Overrider(Map<String, String> overrides) { | ||
for (Map.Entry<String, String> override : overrides.entrySet()) { | ||
this.overrides.put(override.getKey(), Secret.fromString(override.getValue())); | ||
} | ||
} | ||
|
||
@Override | ||
public void expand(EnvVars env) throws IOException, InterruptedException { | ||
for (Map.Entry<String, Secret> override : overrides.entrySet()) { | ||
env.override(override.getKey(), override.getValue().getPlainText()); | ||
} | ||
} | ||
|
||
@Override | ||
public Set<String> getSensitiveVariables() { | ||
return Collections.unmodifiableSet(overrides.keySet()); | ||
} | ||
} | ||
|
||
private static class Callback extends TailCall { | ||
|
||
@Override | ||
protected void finished(StepContext context) throws Exception { | ||
|
||
} | ||
} | ||
|
||
@Extension | ||
public static final class DescriptorImpl extends StepDescriptor { | ||
|
||
@Override | ||
public Set<? extends Class<?>> getRequiredContext() { | ||
return Collections | ||
.unmodifiableSet( | ||
new HashSet<>(Arrays.asList(TaskListener.class, Run.class, EnvVars.class))); | ||
} | ||
|
||
@Override | ||
public boolean takesImplicitBlockArgument() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public String getFunctionName() { | ||
return "withVault"; | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public String getDisplayName() { | ||
return "Vault Plugin"; | ||
} | ||
} | ||
} |
Oops, something went wrong.