Skip to content

Commit 2b2012d

Browse files
committed
Merge branch 'SECURITY-1625' into master
2 parents 01bf857 + b58ebb6 commit 2b2012d

File tree

9 files changed

+55
-23
lines changed

9 files changed

+55
-23
lines changed

src/main/java/org/jenkinsci/plugins/ParameterizedRemoteTrigger/Auth.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import hudson.model.Item;
2727
import hudson.security.ACL;
2828
import hudson.util.ListBoxModel;
29+
import hudson.util.Secret;
2930

3031
/**
3132
* We need to keep this for compatibility - old config deserialization!
@@ -161,7 +162,7 @@ public static Auth auth2ToAuth(Auth2 auth) {
161162
return new Auth(Auth.NONE, null, null, null);
162163
} else if (auth instanceof TokenAuth) {
163164
TokenAuth tokenAuth = (TokenAuth) auth;
164-
return new Auth(Auth.API_TOKEN, tokenAuth.getUserName(), tokenAuth.getApiToken(), null);
165+
return new Auth(Auth.API_TOKEN, tokenAuth.getUserName(), tokenAuth.getApiToken().getPlainText(), null);
165166
} else if (auth instanceof CredentialsAuth) {
166167
CredentialsAuth credAuth = (CredentialsAuth) auth;
167168
try {
@@ -189,7 +190,7 @@ public static Auth2 authToAuth2(Auth oldAuth) {
189190
} else if (Auth.API_TOKEN.equals(authType)) {
190191
TokenAuth newAuth = new TokenAuth();
191192
newAuth.setUserName(oldAuth.getUsername());
192-
newAuth.setApiToken(oldAuth.getApiToken());
193+
newAuth.setApiToken(Secret.fromString(oldAuth.getApiToken()));
193194
return newAuth;
194195
} else if (Auth.CREDENTIALS_PLUGIN.equals(authType)) {
195196
CredentialsAuth newAuth = new CredentialsAuth();

src/main/java/org/jenkinsci/plugins/ParameterizedRemoteTrigger/auth2/BearerTokenAuth.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
import java.net.URLConnection;
55

66
import org.jenkinsci.Symbol;
7-
87
import org.jenkinsci.plugins.ParameterizedRemoteTrigger.BuildContext;
98
import org.kohsuke.stapler.DataBoundConstructor;
109
import org.kohsuke.stapler.DataBoundSetter;
1110

1211
import hudson.Extension;
1312
import hudson.model.Item;
13+
import hudson.util.Secret;
1414

1515

1616
public class BearerTokenAuth extends Auth2 {
@@ -20,25 +20,25 @@ public class BearerTokenAuth extends Auth2 {
2020
@Extension
2121
public static final Auth2Descriptor DESCRIPTOR = new BearerTokenAuthDescriptor();
2222

23-
private String token;
23+
private Secret token;
2424

2525
@DataBoundConstructor
2626
public BearerTokenAuth() {
2727
this.token = null;
2828
}
2929

3030
@DataBoundSetter
31-
public void setToken(String token) {
31+
public void setToken(Secret token) {
3232
this.token = token;
3333
}
3434

35-
public String getToken() {
35+
public Secret getToken() {
3636
return this.token;
3737
}
3838

3939
@Override
4040
public void setAuthorizationHeader(URLConnection connection, BuildContext context) throws IOException {
41-
connection.setRequestProperty("Authorization", "Bearer: " + getToken());
41+
connection.setRequestProperty("Authorization", "Bearer: " + getToken().getPlainText());
4242
}
4343

4444
@Override

src/main/java/org/jenkinsci/plugins/ParameterizedRemoteTrigger/auth2/TokenAuth.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import hudson.Extension;
1515
import hudson.model.Item;
16+
import hudson.util.Secret;
1617

1718
public class TokenAuth extends Auth2 {
1819

@@ -22,7 +23,7 @@ public class TokenAuth extends Auth2 {
2223
public static final Auth2Descriptor DESCRIPTOR = new TokenAuthDescriptor();
2324

2425
private String userName;
25-
private String apiToken;
26+
private Secret apiToken;
2627

2728
@DataBoundConstructor
2829
public TokenAuth() {
@@ -40,17 +41,17 @@ public String getUserName() {
4041
}
4142

4243
@DataBoundSetter
43-
public void setApiToken(String apiToken) {
44+
public void setApiToken(Secret apiToken) {
4445
this.apiToken = apiToken;
4546
}
4647

47-
public String getApiToken() {
48+
public Secret getApiToken() {
4849
return this.apiToken;
4950
}
5051

5152
@Override
5253
public void setAuthorizationHeader(URLConnection connection, BuildContext context) throws IOException {
53-
String authHeaderValue = Base64Utils.generateAuthorizationHeaderValue(AUTHTYPE_BASIC, getUserName(), getApiToken(), context, true);
54+
String authHeaderValue = Base64Utils.generateAuthorizationHeaderValue(AUTHTYPE_BASIC, getUserName(), getApiToken().getPlainText(), context, true);
5455
connection.setRequestProperty("Authorization", authHeaderValue);
5556
}
5657

src/main/resources/org/jenkinsci/plugins/ParameterizedRemoteTrigger/RemoteBuildConfiguration/help-auth2.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
<li><b>No Authentication</b><br/>
1414
No Authorization header will be sent, independent of the global 'remote host' settings.
1515
</li>
16+
<li><b>Bearer Authentication</b><br/>
17+
The bearer token is used.
18+
</li>
1619
</ul>
1720

1821
<b>Note:</b> <i>Jenkins API Tokens</i> are recommended since, if stolen, they allow access only to a specific Jenkins

src/main/resources/org/jenkinsci/plugins/ParameterizedRemoteTrigger/RemoteJenkinsServer/help-auth2.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
<li><b>Credentials Authentication</b><br/>
1111
The specified Jenkins Credentials are used. This can be either user/password or user/API Token.
1212
</li>
13+
<li><b>Bearer Authentication</b><br/>
14+
The bearer token is used.
15+
</li>
1316
</ul>
1417

1518
<b>Note:</b> <i>Jenkins API Tokens</i> are recommended since, if stolen, they allow access only to a specific Jenkins
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?jelly escape-by-default='true'?>
2+
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout"
3+
xmlns:t="/lib/hudson" xmlns:f="/lib/form" xmlns:p="/lib/hudson/project">
4+
5+
<f:entry title="Bearer Token">
6+
<f:password field="token"/>
7+
</f:entry>
8+
9+
</j:jelly>

src/test/java/org/jenkinsci/plugins/ParameterizedRemoteTrigger/RemoteBuildConfigurationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import hudson.security.AuthorizationStrategy.Unsecured;
4545
import hudson.security.csrf.DefaultCrumbIssuer;
4646
import hudson.util.LogTaskListener;
47+
import hudson.util.Secret;
4748
import jenkins.model.Jenkins;
4849

4950
public class RemoteBuildConfigurationTest {
@@ -132,7 +133,7 @@ private void _testRemoteBuild(boolean authenticate, boolean withParam, FreeStyle
132133
if(authenticate) {
133134
TokenAuth tokenAuth = new TokenAuth();
134135
tokenAuth.setUserName(testUser.getId());
135-
tokenAuth.setApiToken(testUserToken);
136+
tokenAuth.setApiToken(Secret.fromString(testUserToken));
136137
configuration.setAuth2(tokenAuth);
137138
}
138139

src/test/java/org/jenkinsci/plugins/ParameterizedRemoteTrigger/RemoteJenkinsServerTest.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88

99
import org.jenkinsci.plugins.ParameterizedRemoteTrigger.auth2.CredentialsAuth;
1010
import org.jenkinsci.plugins.ParameterizedRemoteTrigger.auth2.TokenAuth;
11+
import org.junit.Rule;
1112
import org.junit.Test;
13+
import org.jvnet.hudson.test.JenkinsRule;
14+
15+
import hudson.util.Secret;
1216

1317

1418
public class RemoteJenkinsServerTest {
@@ -18,11 +22,14 @@ public class RemoteJenkinsServerTest {
1822
private final static String ADDRESS = "http://www.example.org:8443";
1923
private final static String DISPLAY_NAME = "My example server.";
2024
private final static boolean HAS_BUILD_TOKEN_ROOT_SUPPORT = true;
25+
26+
@Rule
27+
public JenkinsRule jenkinsRule = new JenkinsRule();
2128

2229
@Test
2330
public void testCloneBehaviour() throws Exception {
2431
TokenAuth auth = new TokenAuth();
25-
auth.setApiToken(TOKEN);
32+
auth.setApiToken(Secret.fromString(TOKEN));
2633
auth.setUserName(USER);
2734

2835
RemoteJenkinsServer server = new RemoteJenkinsServer();
@@ -55,11 +62,11 @@ public void testCloneBehaviour() throws Exception {
5562
//Test if clone is deep-copy or if server fields can be modified
5663
TokenAuth cloneAuth = (TokenAuth)clone.getAuth2();
5764
assertNotNull(cloneAuth);
58-
cloneAuth.setApiToken("changed");
65+
cloneAuth.setApiToken(Secret.fromString("changed"));
5966
cloneAuth.setUserName("changed");
6067
TokenAuth serverAuth = (TokenAuth)server.getAuth2();
6168
assertNotNull(serverAuth);
62-
assertEquals("auth.apiToken", TOKEN, serverAuth.getApiToken());
69+
assertEquals("auth.apiToken", TOKEN, serverAuth.getApiToken().getPlainText());
6370
assertEquals("auth.userName", USER, serverAuth.getUserName());
6471

6572
//Test if clone.setAuth() affects original object

src/test/java/org/jenkinsci/plugins/ParameterizedRemoteTrigger/auth2/Auth2Test.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,29 @@
55
import static org.junit.Assert.assertNotEquals;
66
import static org.junit.Assert.assertTrue;
77

8+
import org.junit.Rule;
89
import org.junit.Test;
10+
import org.jvnet.hudson.test.JenkinsRule;
11+
12+
import hudson.util.Secret;
913

1014
public class Auth2Test {
15+
16+
@Rule
17+
public JenkinsRule jenkinsRule = new JenkinsRule();
1118

1219
@Test
1320
public void testBearerTokenAuthCloneBehaviour() throws CloneNotSupportedException {
1421
BearerTokenAuth original = new BearerTokenAuth();
15-
original.setToken("original");
22+
original.setToken(Secret.fromString("original"));
1623
BearerTokenAuth clone = (BearerTokenAuth)original.clone();
1724
verifyEqualsHashCode(original, clone);
1825

1926
//Test changing clone
20-
clone.setToken("changed");
27+
clone.setToken(Secret.fromString("changed"));
2128
verifyEqualsHashCode(original, clone, false);
22-
assertEquals("original", original.getToken());
23-
assertEquals("changed", clone.getToken());
29+
assertEquals("original", original.getToken().getPlainText());
30+
assertEquals("changed", clone.getToken().getPlainText());
2431
}
2532

2633
@Test
@@ -40,18 +47,18 @@ public void testCredentialsAuthCloneBehaviour() throws CloneNotSupportedExceptio
4047
@Test
4148
public void testTokenAuthCloneBehaviour() throws CloneNotSupportedException {
4249
TokenAuth original = new TokenAuth();
43-
original.setApiToken("original");
50+
original.setApiToken(Secret.fromString("original"));
4451
original.setUserName("original");
4552
TokenAuth clone = (TokenAuth)original.clone();
4653
verifyEqualsHashCode(original, clone);
4754

4855
//Test changing clone
49-
clone.setApiToken("changed");
56+
clone.setApiToken(Secret.fromString("changed"));
5057
clone.setUserName("changed");
5158
verifyEqualsHashCode(original, clone, false);
52-
assertEquals("original", original.getApiToken());
59+
assertEquals("original", original.getApiToken().getPlainText());
5360
assertEquals("original", original.getUserName());
54-
assertEquals("changed", clone.getApiToken());
61+
assertEquals("changed", clone.getApiToken().getPlainText());
5562
assertEquals("changed", clone.getUserName());
5663
}
5764

0 commit comments

Comments
 (0)