-
Notifications
You must be signed in to change notification settings - Fork 104
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
Changes from 32 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 3fb359e
Improve assertions
jvz ee51298
Move package and add link to relevant PR
jvz 2273356
Rename test
jvz 19fec43
Rename test
jvz 6dcc230
Rename tests and improve some
jvz cd94206
Improve help docs
daniel-beck c82bcc3
Simplify set class usage
jvz fa4ff53
Improve javadoc
jvz 5f55aca
Improve help docs
jvz a432974
Add batch test for fancier password
jvz 0dde12f
Fix assertion
jvz 30d0900
Improve tests to work with bash properly
jvz ad1b6aa
Move bash assumption to BeforeClass
jvz 9463aaf
Fix java 11 build issue
jvz c18e1ec
Add tests for /bin/sh
jvz 394a08e
Simplify batch pattern masker
jvz bd82279
Fix bash assumptions in tests and other test fixes
Wadeck ee00db0
Clarify character range
jvz f07204b
Refactor credentials registration in tests
jvz 5425b67
Add one more data point to bash test
jvz ebb4a53
Improve password generation readability
jvz 1512830
Fix test regressions
jvz c707d92
Remove unneeded TODO
jvz 7b05cd2
Clean up javadoc
jvz f7690e0
Apply suggestions from code review
jvz c9938de
Re-add docs on set +x
jvz 14f8c62
Fix imports
jvz 4ebbd28
Use @ClassRule JenkinsRule and improve comments
jvz 9bd38ca
Refactor masking API to use Pattern
jvz cf2178c
Remove redundant pattern
jvz 89d9c74
Remove redundant pattern
jvz c7d3cf6
Update log message to reflect masking improvements
jvz e2efe9a
Restrict API
jvz 717d5db
Update test for log message update
jvz 163fdba
Simplify API
jvz 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
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
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
72 changes: 72 additions & 0 deletions
72
src/main/java/org/jenkinsci/plugins/credentialsbinding/masking/BashSecretPatternFactory.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,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<Pattern> getSecretPatterns(@Nonnull String input) { | ||
Collection<Pattern> patterns = new HashSet<>(); | ||
String quotedForm = getQuotedForm(input); | ||
patterns.add(SecretPatternFactory.quotedCompile(quotedForm)); | ||
patterns.add(SecretPatternFactory.quotedCompile(surroundWithQuotes(quotedForm))); | ||
patterns.add(SecretPatternFactory.quotedCompile(getUnquotedForm(input))); | ||
return patterns; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...main/java/org/jenkinsci/plugins/credentialsbinding/masking/BatchSecretPatternFactory.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,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<Pattern> getSecretPatterns(@Nonnull String input) { | ||
return input.contains("^") | ||
? Collections.singleton(SecretPatternFactory.quotedCompile(QUOTED_CHARS.matcher(input).replaceAll("$2"))) | ||
: Collections.emptySet(); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...in/java/org/jenkinsci/plugins/credentialsbinding/masking/LiteralSecretPatternFactory.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,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; | ||
|
||
/** | ||
* 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<Pattern> getSecretPatterns(@Nonnull String input) { | ||
return Collections.singleton(SecretPatternFactory.quotedCompile(input)); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/org/jenkinsci/plugins/credentialsbinding/masking/SecretPatternFactory.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,61 @@ | ||
/* | ||
* 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 javax.annotation.Nonnull; | ||
import java.util.Collection; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Creates regular expressions to match encoded forms of secrets in logs. | ||
* These are typically implemented to handle various shell quoting algorithms (sometimes confused with escaping) to | ||
* pass literal string values to an interpreter. | ||
*/ | ||
public interface SecretPatternFactory extends ExtensionPoint { | ||
|
||
/** | ||
* Returns a collection of alternative forms the given input may show up as in logs. | ||
* Note that these patterns must embed their flags in the pattern rather than as parameters to Pattern. | ||
*/ | ||
@Nonnull Collection<Pattern> getSecretPatterns(@Nonnull String input); | ||
jvz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Returns all SecretPatternFactory extensions known at runtime. | ||
*/ | ||
static @Nonnull ExtensionList<SecretPatternFactory> all() { | ||
return ExtensionList.lookup(SecretPatternFactory.class); | ||
} | ||
|
||
/** | ||
* Composes {@link Pattern#compile(String)} and {@link Pattern#quote(String)} for convenience. | ||
*/ | ||
static @Nonnull Pattern quotedCompile(@Nonnull String input) { | ||
return Pattern.compile(Pattern.quote(input)); | ||
} | ||
|
||
} |
Oops, something went wrong.
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.
or a bit more simply