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

Use SecretPatterns from SecretsMasker #1009

Merged
merged 3 commits into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
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
9 changes: 7 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<jenkins.host.address />
<slaveAgentPort />
<java.level>8</java.level>
<jenkins.version>2.263</jenkins.version>
<jenkins.version>2.263.1</jenkins.version>
<no-test-jar>false</no-test-jar>
<pipeline-model-definition.version>1.7.2</pipeline-model-definition.version>
<useBeta>true</useBeta>
Expand Down Expand Up @@ -137,6 +137,11 @@
<artifactId>caffeine-api</artifactId>
<version>2.9.1-23.v51c4e2c879c8</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>credentials-binding</artifactId>
<version>1.26</version>
</dependency>

<!-- for testing -->
<dependency>
Expand Down Expand Up @@ -261,7 +266,7 @@
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.263.x</artifactId>
<version>25</version>
<version>876.vc43b4c6423b6</version>
<scope>import</scope>
<type>pom</type>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.csanchez.jenkins.plugins.kubernetes.pipeline;

import hudson.Extension;
import hudson.console.LineTransformationOutputStream;
import hudson.remoting.Channel;
import hudson.util.LogTaskListener;
import io.fabric8.kubernetes.api.model.Container;
Expand Down Expand Up @@ -45,6 +44,7 @@
import okhttp3.Response;
import org.csanchez.jenkins.plugins.kubernetes.KubernetesComputer;
import org.csanchez.jenkins.plugins.kubernetes.KubernetesSlave;
import org.jenkinsci.plugins.credentialsbinding.masking.SecretPatterns;
import org.jenkinsci.plugins.kubernetes.auth.KubernetesAuthException;
import org.jenkinsci.plugins.workflow.log.TaskListenerDecorator;
import org.jenkinsci.plugins.workflow.steps.DynamicContext;
Expand All @@ -67,28 +67,7 @@ private SecretsMasker(Set<String> values) {

@Override
public OutputStream decorate(OutputStream logger) throws IOException, InterruptedException {
// TODO better to pick up a standard API from credentials-binding (more efficient)
// https://github.com/jenkinsci/credentials-binding-plugin/pull/59#discussion_r288735761
return new LineTransformationOutputStream() {
@Override
protected void eol(byte[] b, int len) throws IOException {
String s = new String(b, 0, len, StandardCharsets.UTF_8);
for (String value : values) {
s = s.replace(value, "********");
}
logger.write(s.getBytes(StandardCharsets.UTF_8));
}
@Override
public void flush() throws IOException {
logger.flush();
}
@Override
public void close() throws IOException {
super.close();
logger.close();
}

};
return new SecretPatterns.MaskingOutputStream(logger, () -> SecretPatterns.getAggregateSecretPattern(values), "UTF-8");
}

@Extension
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public void runInPodFromYaml() throws Exception {
r.assertBuildStatusSuccess(r.waitForCompletion(b));
r.assertLogContains("script file contents: ", b);
r.assertLogNotContains(CONTAINER_ENV_VAR_FROM_SECRET_VALUE, b);
r.assertLogContains("INSIDE_CONTAINER_ENV_VAR_FROM_SECRET = ******** or " + CONTAINER_ENV_VAR_FROM_SECRET_VALUE.toUpperCase(Locale.ROOT) + "\n", b);
r.assertLogContains("INSIDE_CONTAINER_ENV_VAR_FROM_SECRET = **** or " + CONTAINER_ENV_VAR_FROM_SECRET_VALUE.toUpperCase(Locale.ROOT) + "\n", b);
assertFalse("There are pods leftover after test execution, see previous logs",
deletePods(cloud.connect(), getLabels(cloud, this, name), true));
}
Expand Down Expand Up @@ -389,17 +389,17 @@ private void assertEnvVars(JenkinsRuleNonLocalhost r2, WorkflowRun b) throws Exc

r.assertLogContains("INSIDE_CONTAINER_ENV_VAR = " + CONTAINER_ENV_VAR_VALUE + "\n", b);
r.assertLogContains("INSIDE_CONTAINER_ENV_VAR_LEGACY = " + CONTAINER_ENV_VAR_VALUE + "\n", b);
r.assertLogContains("INSIDE_CONTAINER_ENV_VAR_FROM_SECRET = ******** or " + CONTAINER_ENV_VAR_FROM_SECRET_VALUE.toUpperCase(Locale.ROOT) + "\n", b);
r.assertLogContains("INSIDE_CONTAINER_ENV_VAR_FROM_SECRET = **** or " + CONTAINER_ENV_VAR_FROM_SECRET_VALUE.toUpperCase(Locale.ROOT) + "\n", b);
r.assertLogContains("INSIDE_POD_ENV_VAR = " + POD_ENV_VAR_VALUE + "\n", b);
r.assertLogContains("INSIDE_POD_ENV_VAR_FROM_SECRET = ******** or " + POD_ENV_VAR_FROM_SECRET_VALUE.toUpperCase(Locale.ROOT) + "\n", b);
r.assertLogContains("INSIDE_POD_ENV_VAR_FROM_SECRET = **** or " + POD_ENV_VAR_FROM_SECRET_VALUE.toUpperCase(Locale.ROOT) + "\n", b);
r.assertLogContains("INSIDE_EMPTY_POD_ENV_VAR_FROM_SECRET = ''", b);
r.assertLogContains("INSIDE_GLOBAL = " + GLOBAL + "\n", b);

r.assertLogContains("OUTSIDE_CONTAINER_ENV_VAR =\n", b);
r.assertLogContains("OUTSIDE_CONTAINER_ENV_VAR_LEGACY =\n", b);
r.assertLogContains("OUTSIDE_CONTAINER_ENV_VAR_FROM_SECRET = or\n", b);
r.assertLogContains("OUTSIDE_POD_ENV_VAR = " + POD_ENV_VAR_VALUE + "\n", b);
r.assertLogContains("OUTSIDE_POD_ENV_VAR_FROM_SECRET = ******** or " + POD_ENV_VAR_FROM_SECRET_VALUE.toUpperCase(Locale.ROOT) + "\n", b);
r.assertLogContains("OUTSIDE_POD_ENV_VAR_FROM_SECRET = **** or " + POD_ENV_VAR_FROM_SECRET_VALUE.toUpperCase(Locale.ROOT) + "\n", b);
r.assertLogContains("OUTSIDE_EMPTY_POD_ENV_VAR_FROM_SECRET = ''", b);
r.assertLogContains("OUTSIDE_GLOBAL = " + GLOBAL + "\n", b);
}
Expand Down Expand Up @@ -661,8 +661,8 @@ public void secretMaskingWindows() throws Exception {
assumeWindows();
cloud.setDirectConnection(false);
r.assertBuildStatusSuccess(r.waitForCompletion(b));
r.assertLogContains("INSIDE_POD_ENV_VAR_FROM_SECRET = ******** or " + POD_ENV_VAR_FROM_SECRET_VALUE.toUpperCase(Locale.ROOT), b);
r.assertLogContains("INSIDE_CONTAINER_ENV_VAR_FROM_SECRET = ******** or " + CONTAINER_ENV_VAR_FROM_SECRET_VALUE.toUpperCase(Locale.ROOT), b);
r.assertLogContains("INSIDE_POD_ENV_VAR_FROM_SECRET = **** or " + POD_ENV_VAR_FROM_SECRET_VALUE.toUpperCase(Locale.ROOT), b);
r.assertLogContains("INSIDE_CONTAINER_ENV_VAR_FROM_SECRET = **** or " + CONTAINER_ENV_VAR_FROM_SECRET_VALUE.toUpperCase(Locale.ROOT), b);
r.assertLogNotContains(POD_ENV_VAR_FROM_SECRET_VALUE, b);
r.assertLogNotContains(CONTAINER_ENV_VAR_FROM_SECRET_VALUE, b);
}
Expand Down