27
27
import edu .umd .cs .findbugs .annotations .CheckForNull ;
28
28
import edu .umd .cs .findbugs .annotations .NonNull ;
29
29
import hudson .console .LineTransformationOutputStream ;
30
+ import java .util .Arrays ;
31
+ import jenkins .util .JenkinsJVM ;
32
+
30
33
import java .io .IOException ;
31
34
import java .io .OutputStream ;
32
35
import java .util .Collection ;
33
36
import java .util .Comparator ;
37
+ import java .util .List ;
34
38
import java .util .function .Supplier ;
39
+ import java .util .logging .Level ;
40
+ import java .util .logging .Logger ;
35
41
import java .util .regex .Matcher ;
36
42
import java .util .regex .Pattern ;
37
43
import java .util .stream .Collectors ;
38
44
39
45
public class SecretPatterns {
40
46
47
+ private static final Logger LOGGER = Logger .getLogger (SecretPatterns .class .getName ());
48
+
41
49
private static final Comparator <String > BY_LENGTH_DESCENDING =
42
50
Comparator .comparingInt (String ::length ).reversed ().thenComparing (String ::compareTo );
43
51
@@ -51,10 +59,11 @@ public class SecretPatterns {
51
59
* absence of quoting, the longer form is masked.
52
60
*/
53
61
public static @ NonNull Pattern getAggregateSecretPattern (@ NonNull Collection <String > inputs ) {
62
+ List <SecretPatternFactory > secretPatternFactories = getSecretPatternFactories ();
54
63
String pattern = inputs .stream ()
55
64
.filter (input -> !input .isEmpty ())
56
65
.flatMap (input ->
57
- SecretPatternFactory . all () .stream ().flatMap (factory ->
66
+ secretPatternFactories .stream ().flatMap (factory ->
58
67
factory .getEncodedForms (input ).stream ()))
59
68
.sorted (BY_LENGTH_DESCENDING )
60
69
.distinct ()
@@ -63,6 +72,31 @@ public class SecretPatterns {
63
72
return Pattern .compile (pattern );
64
73
}
65
74
75
+ private static List <SecretPatternFactory > getSecretPatternFactories () {
76
+ if (JenkinsJVM .isJenkinsJVM ()) {
77
+ return SecretPatternFactory .all ();
78
+ } else {
79
+ // TODO Change this to a hard fail in future, e.g. JenkinsJVM.checkJenkinsJVM();
80
+ LOGGER .log (
81
+ Level .WARNING ,
82
+ "An agent attempted to look up secret patterns from the controller, which is unsupported. " +
83
+ "Falling back to basic implementation that may not mask common transformations of the secret. " +
84
+ "This workaround will be removed in a future release. " +
85
+ "This is a bug in the plugin calling SecretPatterns#getAggregateSecretPattern(String) " +
86
+ "and should be reported to its maintainers. " +
87
+ "The plugin can be identified through the stacktrace below." ,
88
+ new RuntimeException ()
89
+ );
90
+ return Arrays .asList (
91
+ new AlmquistShellSecretPatternFactory (),
92
+ new BashSecretPatternFactory (),
93
+ new BatchSecretPatternFactory (),
94
+ new DollarSecretPatternFactory (),
95
+ new LiteralSecretPatternFactory ()
96
+ );
97
+ }
98
+ }
99
+
66
100
/**
67
101
* Delegating output stream that masks occurrences of a set of secrets.
68
102
*/
0 commit comments