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-42950] Support more credential masking scenarios #59

Merged
merged 36 commits into from
May 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
927eb26
[JENKINS-42950] Support more credential masking scenarios
jvz Apr 9, 2019
3fb359e
Improve assertions
jvz May 3, 2019
ee51298
Move package and add link to relevant PR
jvz May 3, 2019
2273356
Rename test
jvz May 3, 2019
19fec43
Rename test
jvz May 3, 2019
6dcc230
Rename tests and improve some
jvz May 3, 2019
cd94206
Improve help docs
daniel-beck May 3, 2019
c82bcc3
Simplify set class usage
jvz May 3, 2019
fa4ff53
Improve javadoc
jvz May 3, 2019
5f55aca
Improve help docs
jvz May 3, 2019
a432974
Add batch test for fancier password
jvz May 3, 2019
0dde12f
Fix assertion
jvz May 3, 2019
30d0900
Improve tests to work with bash properly
jvz May 3, 2019
ad1b6aa
Move bash assumption to BeforeClass
jvz May 3, 2019
9463aaf
Fix java 11 build issue
jvz May 7, 2019
c18e1ec
Add tests for /bin/sh
jvz May 7, 2019
394a08e
Simplify batch pattern masker
jvz May 9, 2019
bd82279
Fix bash assumptions in tests and other test fixes
Wadeck May 10, 2019
ee00db0
Clarify character range
jvz May 20, 2019
f07204b
Refactor credentials registration in tests
jvz May 21, 2019
5425b67
Add one more data point to bash test
jvz May 21, 2019
ebb4a53
Improve password generation readability
jvz May 21, 2019
1512830
Fix test regressions
jvz May 22, 2019
c707d92
Remove unneeded TODO
jvz May 30, 2019
7b05cd2
Clean up javadoc
jvz May 30, 2019
f7690e0
Apply suggestions from code review
jvz May 30, 2019
c9938de
Re-add docs on set +x
jvz May 30, 2019
14f8c62
Fix imports
jvz May 30, 2019
4ebbd28
Use @ClassRule JenkinsRule and improve comments
jvz May 30, 2019
9bd38ca
Refactor masking API to use Pattern
jvz May 30, 2019
cf2178c
Remove redundant pattern
jvz May 30, 2019
89d9c74
Remove redundant pattern
jvz May 30, 2019
c7d3cf6
Update log message to reflect masking improvements
jvz May 30, 2019
e2efe9a
Restrict API
jvz May 30, 2019
717d5db
Update test for log message update
jvz May 30, 2019
163fdba
Simplify API
jvz May 30, 2019
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
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>3.42</version>
<version>3.43</version>
<relativePath />
</parent>

Expand Down Expand Up @@ -95,13 +95,13 @@
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-durable-task-step</artifactId>
<version>2.5</version>
<version>2.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>durable-task</artifactId>
<version>1.13</version>
<version>1.14</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,15 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import jenkins.model.Jenkins;
import org.jenkinsci.plugins.credentialsbinding.impl.CredentialNotFoundException;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.DataBoundConstructor;

/**
Expand Down Expand Up @@ -169,32 +162,4 @@ public abstract MultiEnvironment bind(@Nonnull Run<?,?> build,
return (BindingDescriptor<C>) super.getDescriptor();
}

private static final Comparator<String> stringLengthComparator = new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o2.length() - o1.length();
}
};

/**
* Utility method for turning a collection of secret strings into a single {@link String} for pattern compilation.
* @param secrets A collection of secret strings
* @return A {@link String} generated from that collection.
*/
@Restricted(NoExternalUse.class)
public static String getPatternStringForSecrets(Collection<String> secrets) {
StringBuilder b = new StringBuilder();
List<String> sortedByLength = new ArrayList<String>(secrets);
Collections.sort(sortedByLength, stringLengthComparator);

for (String secret : sortedByLength) {
if (!secret.isEmpty()) {
if (b.length() > 0) {
b.append('|');
}
b.append(Pattern.quote(secret));
}
}
return b.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@

import org.apache.commons.codec.Charsets;
import org.jenkinsci.plugins.credentialsbinding.MultiBinding;
import org.jenkinsci.plugins.credentialsbinding.masking.SecretPatterns;
import org.jenkinsci.plugins.workflow.steps.AbstractStepExecutionImpl;
import org.jenkinsci.plugins.workflow.steps.BodyExecutionCallback;
import org.jenkinsci.plugins.workflow.steps.BodyInvoker;
Expand Down Expand Up @@ -137,7 +138,7 @@ private void doStart() throws Exception {
}
if (!overrides.isEmpty()) {
boolean unix = launcher != null ? launcher.isUnix() : true;
listener.getLogger().println("Masking only exact matches of " + overrides.keySet().stream().map(
listener.getLogger().println("Masking supported pattern matches of " + overrides.keySet().stream().map(
v -> unix ? "$" + v : "%" + v + "%"
).collect(Collectors.joining(" or ")));
}
Expand Down Expand Up @@ -195,7 +196,7 @@ private static final class Filter extends ConsoleLogFilter implements Serializab
private String charsetName;

Filter(Collection<String> secrets, String charsetName) {
pattern = Secret.fromString(MultiBinding.getPatternStringForSecrets(secrets));
pattern = Secret.fromString(SecretPatterns.getAggregateSecretPattern(secrets).pattern());
this.charsetName = charsetName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import hudson.tasks.BuildWrapper;
import hudson.tasks.BuildWrapperDescriptor;
import org.jenkinsci.plugins.credentialsbinding.MultiBinding;
import org.jenkinsci.plugins.credentialsbinding.masking.SecretPatterns;
import org.kohsuke.stapler.DataBoundConstructor;

import javax.annotation.CheckForNull;
Expand All @@ -60,7 +61,7 @@ public class SecretBuildWrapper extends BuildWrapper {
*/
public static @CheckForNull Pattern getPatternForBuild(@Nonnull AbstractBuild<?, ?> build) {
if (secretsForBuild.containsKey(build)) {
return Pattern.compile(MultiBinding.getPatternStringForSecrets(secretsForBuild.get(build)));
return SecretPatterns.getAggregateSecretPattern(secretsForBuild.get(build));
} else {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* The MIT License
*
* Copyright (c) 2019 CloudBees, Inc.
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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 org.jenkinsci.plugins.credentialsbinding.masking;

import hudson.Extension;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.HashSet;
import java.util.regex.Pattern;

@Extension
@Restricted(NoExternalUse.class)
public class BashSecretPatternFactory implements SecretPatternFactory {

private static final Pattern QUOTED_CHARS = Pattern.compile("(\\\\)(\\\\?)");

private @Nonnull String getQuotedForm(@Nonnull String input) {
StringBuilder sb = new StringBuilder(input.length());
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (c == '\'') {
sb.append("'\\''");
} else {
sb.append(c);
}
}
return sb.toString();
}

private @Nonnull String surroundWithQuotes(@Nonnull String input) {
return "'" + input + "'";
}

private @Nonnull String getUnquotedForm(@Nonnull String input) {
return QUOTED_CHARS.matcher(input).replaceAll("$2");
}

@Override
public @Nonnull Collection<String> getEncodedForms(@Nonnull String input) {
Collection<String> patterns = new HashSet<>();
String quotedForm = getQuotedForm(input);
patterns.add(quotedForm);
patterns.add(surroundWithQuotes(quotedForm));
patterns.add(getUnquotedForm(input));
return patterns;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or a bit more simply

return Arrays.asList(quotedForm, surroundWithQuotes(quotedForm), getUnquotedForm(input));

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* The MIT License
*
* Copyright (c) 2019 CloudBees, Inc.
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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 org.jenkinsci.plugins.credentialsbinding.masking;

import hudson.Extension;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.Collections;
import java.util.regex.Pattern;

@Extension
@Restricted(NoExternalUse.class)
public class BatchSecretPatternFactory implements SecretPatternFactory {
private static final Pattern QUOTED_CHARS = Pattern.compile("(\\^)(\\^?)");

@Override
public @Nonnull Collection<String> getEncodedForms(@Nonnull String input) {
return input.contains("^")
? Collections.singleton(QUOTED_CHARS.matcher(input).replaceAll("$2"))
: Collections.emptySet();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* The MIT License
*
* Copyright (c) 2019 CloudBees, Inc.
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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 org.jenkinsci.plugins.credentialsbinding.masking;

import hudson.Extension;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.Collections;

/**
* Trivial secret pattern factory that matches the literal value of the secret.
*/
@Extension
@Restricted(NoExternalUse.class)
public class LiteralSecretPatternFactory implements SecretPatternFactory {
@Nonnull
@Override
public Collection<String> getEncodedForms(@Nonnull String input) {
return Collections.singleton(input);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* The MIT License
*
* Copyright (c) 2019 CloudBees, Inc.
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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 org.jenkinsci.plugins.credentialsbinding.masking;

import hudson.ExtensionList;
import hudson.ExtensionPoint;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

import javax.annotation.Nonnull;
import java.util.Collection;

/**
* Provides encoded forms to an input for use in masking those forms in logs.
* These are typically implemented to handle various shell quoting algorithms (sometimes confused with escaping) to
* pass literal string values to an interpreter.
*/
@Restricted(NoExternalUse.class)
public interface SecretPatternFactory extends ExtensionPoint {

/**
* Returns a collection of alternative forms the given input may be encoded as in logs.
*/
@Nonnull Collection<String> getEncodedForms(@Nonnull String input);

/**
* Returns all SecretPatternFactory extensions known at runtime.
*/
static @Nonnull ExtensionList<SecretPatternFactory> all() {
return ExtensionList.lookup(SecretPatternFactory.class);
}

}
Loading