Skip to content

Commit bb55742

Browse files
authored
Merge pull request #335 from bitwiseman/task/token-secret
Store token as Secret instead of String
2 parents cd85b85 + f59647b commit bb55742

2 files changed

Lines changed: 20 additions & 20 deletions

File tree

src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubAppCredentials.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ public void setOwner(String owner) {
115115

116116
@SuppressWarnings("deprecation") // preview features are required for GitHub app integration, GitHub api adds deprecated to all preview methods
117117
static AppInstallationToken generateAppInstallationToken(String appId, String appPrivateKey, String apiUrl, String owner) {
118+
JenkinsJVM.checkJenkinsJVM();
118119
// We expect this to be fast but if anything hangs in here we do not want to block indefinitely
119120
try (Timeout timeout = Timeout.limit(30, TimeUnit.SECONDS)) {
120121
String jwtToken = createJWT(appId, appPrivateKey);
@@ -149,7 +150,8 @@ static AppInstallationToken generateAppInstallationToken(String appId, String ap
149150
.create();
150151

151152
long expiration = getExpirationSeconds(appInstallationToken);
152-
AppInstallationToken token = new AppInstallationToken(appInstallationToken.getToken(),
153+
AppInstallationToken token = new AppInstallationToken(
154+
Secret.fromString(appInstallationToken.getToken()),
153155
expiration);
154156
LOGGER.log(Level.FINER,
155157
"Generated App Installation Token for app ID {0}",
@@ -185,7 +187,6 @@ private static long getExpirationSeconds(GHAppInstallationToken appInstallationT
185187
@NonNull
186188
@Override
187189
public Secret getPassword() {
188-
String appInstallationToken;
189190
synchronized (this) {
190191
try {
191192
if (cachedToken == null || cachedToken.isStale()) {
@@ -208,12 +209,11 @@ public Secret getPassword() {
208209
throw e;
209210
}
210211
}
211-
appInstallationToken = cachedToken.getToken();
212-
}
212+
LOGGER.log(Level.FINEST, "Returned GitHub App Installation Token for app ID {0}", appID);
213213

214-
LOGGER.log(Level.FINEST, "Returned GitHub App Installation Token for app ID {0}", appID);
214+
return cachedToken.getToken();
215+
}
215216

216-
return Secret.fromString(appInstallationToken);
217217
}
218218

219219
/**
@@ -265,7 +265,7 @@ static class AppInstallationToken implements Serializable {
265265
*/
266266
static long NOT_STALE_MINIMUM_SECONDS = Duration.ofMinutes(1).getSeconds();
267267

268-
private final String token;
268+
private final Secret token;
269269
private final long expirationEpochSeconds;
270270
private final long staleEpochSeconds;
271271

@@ -281,7 +281,7 @@ static class AppInstallationToken implements Serializable {
281281
* @param token the token string
282282
* @param expirationEpochSeconds the time in epoch seconds that this token will expire
283283
*/
284-
public AppInstallationToken(String token, long expirationEpochSeconds) {
284+
public AppInstallationToken(Secret token, long expirationEpochSeconds) {
285285
long now = Instant.now().getEpochSecond();
286286
long minimumAllowedAge = Math.max(1, NOT_STALE_MINIMUM_SECONDS);
287287
long maximumAllowedAge = Math.max(1, 1 + STALE_AFTER_SECONDS);
@@ -306,7 +306,7 @@ public AppInstallationToken(String token, long expirationEpochSeconds) {
306306
this.staleEpochSeconds = now + secondsUntilStale;
307307
}
308308

309-
public String getToken() {
309+
public Secret getToken() {
310310
return token;
311311
}
312312

@@ -405,7 +405,6 @@ public String getUsername() {
405405
public Secret getPassword() {
406406
JenkinsJVM.checkNotJenkinsJVM();
407407
try {
408-
String appInstallationToken;
409408
synchronized (this) {
410409
try {
411410
if (cachedToken == null || cachedToken.isStale()) {
@@ -427,12 +426,11 @@ public Secret getPassword() {
427426
throw e;
428427
}
429428
}
430-
appInstallationToken = cachedToken.getToken();
431-
}
429+
LOGGER.log(Level.FINEST, "Returned GitHub App Installation Token for app ID {0} on agent", appID);
432430

433-
LOGGER.log(Level.FINEST, "Returned GitHub App Installation Token for app ID {0} on agent", appID);
431+
return cachedToken.getToken();
432+
}
434433

435-
return Secret.fromString(appInstallationToken);
436434
} catch (IOException | InterruptedException x) {
437435
throw new RuntimeException(x);
438436
}

src/test/java/org/jenkinsci/plugins/github_branch_source/GithubAppCredentialsAppInstallationTokenTest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.jenkinsci.plugins.github_branch_source;
22

3+
import hudson.util.Secret;
34
import org.junit.Test;
45

56
import java.time.Duration;
@@ -17,24 +18,25 @@ public void testAppInstallationTokenStale() throws Exception {
1718
long now;
1819

1920
now = Instant.now().getEpochSecond();
20-
token = new GitHubAppCredentials.AppInstallationToken("", now);
21+
Secret secret = Secret.fromString("secret-token");
22+
token = new GitHubAppCredentials.AppInstallationToken(secret, now);
2123
assertThat(token.isStale(), is(false));
2224
assertThat(token.getTokenStaleEpochSeconds(), equalTo(now + GitHubAppCredentials.AppInstallationToken.NOT_STALE_MINIMUM_SECONDS));
2325

2426
now = Instant.now().getEpochSecond();
25-
token = new GitHubAppCredentials.AppInstallationToken("",
27+
token = new GitHubAppCredentials.AppInstallationToken(secret,
2628
now + Duration.ofMinutes(15).getSeconds());
2729
assertThat(token.isStale(), is(false));
2830
assertThat(token.getTokenStaleEpochSeconds(), equalTo(now + GitHubAppCredentials.AppInstallationToken.NOT_STALE_MINIMUM_SECONDS));
2931

3032
now = Instant.now().getEpochSecond();
31-
token = new GitHubAppCredentials.AppInstallationToken("",
33+
token = new GitHubAppCredentials.AppInstallationToken(secret,
3234
now + GitHubAppCredentials.AppInstallationToken.STALE_BEFORE_EXPIRATION_SECONDS + 2);
3335
assertThat(token.isStale(), is(false));
3436
assertThat(token.getTokenStaleEpochSeconds(), equalTo(now + GitHubAppCredentials.AppInstallationToken.NOT_STALE_MINIMUM_SECONDS));
3537

3638
now = Instant.now().getEpochSecond();
37-
token = new GitHubAppCredentials.AppInstallationToken("",
39+
token = new GitHubAppCredentials.AppInstallationToken(secret,
3840
now + GitHubAppCredentials.AppInstallationToken.STALE_BEFORE_EXPIRATION_SECONDS + Duration
3941
.ofMinutes(7)
4042
.getSeconds());
@@ -43,7 +45,7 @@ public void testAppInstallationTokenStale() throws Exception {
4345
equalTo(now + Duration.ofMinutes(7).getSeconds()));
4446

4547
now = Instant.now().getEpochSecond();
46-
token = new GitHubAppCredentials.AppInstallationToken("",
48+
token = new GitHubAppCredentials.AppInstallationToken(secret,
4749
now + Duration.ofMinutes(90).getSeconds());
4850
assertThat(token.isStale(), is(false));
4951
assertThat(token.getTokenStaleEpochSeconds(),
@@ -55,7 +57,7 @@ public void testAppInstallationTokenStale() throws Exception {
5557
GitHubAppCredentials.AppInstallationToken.NOT_STALE_MINIMUM_SECONDS = -10;
5658

5759
now = Instant.now().getEpochSecond();
58-
token = new GitHubAppCredentials.AppInstallationToken("", now);
60+
token = new GitHubAppCredentials.AppInstallationToken(secret, now);
5961
assertThat(token.isStale(), is(false));
6062
assertThat(token.getTokenStaleEpochSeconds(), equalTo(now + 1));
6163

0 commit comments

Comments
 (0)