Skip to content

Store token as Secret instead of String #335

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

Merged
merged 1 commit into from
Sep 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public void setOwner(String owner) {

@SuppressWarnings("deprecation") // preview features are required for GitHub app integration, GitHub api adds deprecated to all preview methods
static AppInstallationToken generateAppInstallationToken(String appId, String appPrivateKey, String apiUrl, String owner) {
JenkinsJVM.checkJenkinsJVM();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This method now only runs on a controller, let's enforce that.

// We expect this to be fast but if anything hangs in here we do not want to block indefinitely
try (Timeout timeout = Timeout.limit(30, TimeUnit.SECONDS)) {
String jwtToken = createJWT(appId, appPrivateKey);
Expand Down Expand Up @@ -149,7 +150,8 @@ static AppInstallationToken generateAppInstallationToken(String appId, String ap
.create();

long expiration = getExpirationSeconds(appInstallationToken);
AppInstallationToken token = new AppInstallationToken(appInstallationToken.getToken(),
AppInstallationToken token = new AppInstallationToken(
Secret.fromString(appInstallationToken.getToken()),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The app installation token only exists as a bare string inside this method.

expiration);
LOGGER.log(Level.FINER,
"Generated App Installation Token for app ID {0}",
Expand Down Expand Up @@ -185,7 +187,6 @@ private static long getExpirationSeconds(GHAppInstallationToken appInstallationT
@NonNull
@Override
public Secret getPassword() {
String appInstallationToken;
synchronized (this) {
try {
if (cachedToken == null || cachedToken.isStale()) {
Expand All @@ -208,12 +209,11 @@ public Secret getPassword() {
throw e;
}
}
appInstallationToken = cachedToken.getToken();
}
LOGGER.log(Level.FINEST, "Returned GitHub App Installation Token for app ID {0}", appID);

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

return Secret.fromString(appInstallationToken);
}

/**
Expand Down Expand Up @@ -265,7 +265,7 @@ static class AppInstallationToken implements Serializable {
*/
static long NOT_STALE_MINIMUM_SECONDS = Duration.ofMinutes(1).getSeconds();

private final String token;
private final Secret token;
private final long expirationEpochSeconds;
private final long staleEpochSeconds;

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

public String getToken() {
public Secret getToken() {
return token;
}

Expand Down Expand Up @@ -405,7 +405,6 @@ public String getUsername() {
public Secret getPassword() {
JenkinsJVM.checkNotJenkinsJVM();
try {
String appInstallationToken;
synchronized (this) {
try {
if (cachedToken == null || cachedToken.isStale()) {
Expand All @@ -427,12 +426,11 @@ public Secret getPassword() {
throw e;
}
}
appInstallationToken = cachedToken.getToken();
}
LOGGER.log(Level.FINEST, "Returned GitHub App Installation Token for app ID {0} on agent", appID);

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

return Secret.fromString(appInstallationToken);
} catch (IOException | InterruptedException x) {
throw new RuntimeException(x);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jenkinsci.plugins.github_branch_source;

import hudson.util.Secret;
import org.junit.Test;

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

now = Instant.now().getEpochSecond();
token = new GitHubAppCredentials.AppInstallationToken("", now);
Secret secret = Secret.fromString("secret-token");
token = new GitHubAppCredentials.AppInstallationToken(secret, now);
assertThat(token.isStale(), is(false));
assertThat(token.getTokenStaleEpochSeconds(), equalTo(now + GitHubAppCredentials.AppInstallationToken.NOT_STALE_MINIMUM_SECONDS));

now = Instant.now().getEpochSecond();
token = new GitHubAppCredentials.AppInstallationToken("",
token = new GitHubAppCredentials.AppInstallationToken(secret,
now + Duration.ofMinutes(15).getSeconds());
assertThat(token.isStale(), is(false));
assertThat(token.getTokenStaleEpochSeconds(), equalTo(now + GitHubAppCredentials.AppInstallationToken.NOT_STALE_MINIMUM_SECONDS));

now = Instant.now().getEpochSecond();
token = new GitHubAppCredentials.AppInstallationToken("",
token = new GitHubAppCredentials.AppInstallationToken(secret,
now + GitHubAppCredentials.AppInstallationToken.STALE_BEFORE_EXPIRATION_SECONDS + 2);
assertThat(token.isStale(), is(false));
assertThat(token.getTokenStaleEpochSeconds(), equalTo(now + GitHubAppCredentials.AppInstallationToken.NOT_STALE_MINIMUM_SECONDS));

now = Instant.now().getEpochSecond();
token = new GitHubAppCredentials.AppInstallationToken("",
token = new GitHubAppCredentials.AppInstallationToken(secret,
now + GitHubAppCredentials.AppInstallationToken.STALE_BEFORE_EXPIRATION_SECONDS + Duration
.ofMinutes(7)
.getSeconds());
Expand All @@ -43,7 +45,7 @@ public void testAppInstallationTokenStale() throws Exception {
equalTo(now + Duration.ofMinutes(7).getSeconds()));

now = Instant.now().getEpochSecond();
token = new GitHubAppCredentials.AppInstallationToken("",
token = new GitHubAppCredentials.AppInstallationToken(secret,
now + Duration.ofMinutes(90).getSeconds());
assertThat(token.isStale(), is(false));
assertThat(token.getTokenStaleEpochSeconds(),
Expand All @@ -55,7 +57,7 @@ public void testAppInstallationTokenStale() throws Exception {
GitHubAppCredentials.AppInstallationToken.NOT_STALE_MINIMUM_SECONDS = -10;

now = Instant.now().getEpochSecond();
token = new GitHubAppCredentials.AppInstallationToken("", now);
token = new GitHubAppCredentials.AppInstallationToken(secret, now);
assertThat(token.isStale(), is(false));
assertThat(token.getTokenStaleEpochSeconds(), equalTo(now + 1));

Expand Down