-
Notifications
You must be signed in to change notification settings - Fork 374
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
Changes from all commits
Commits
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 hidden or 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 |
---|---|---|
|
@@ -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(); | ||
// 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); | ||
|
@@ -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()), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}", | ||
|
@@ -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()) { | ||
|
@@ -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); | ||
} | ||
|
||
/** | ||
|
@@ -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; | ||
|
||
|
@@ -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); | ||
|
@@ -306,7 +306,7 @@ public AppInstallationToken(String token, long expirationEpochSeconds) { | |
this.staleEpochSeconds = now + secondsUntilStale; | ||
} | ||
|
||
public String getToken() { | ||
public Secret getToken() { | ||
return token; | ||
} | ||
|
||
|
@@ -405,7 +405,6 @@ public String getUsername() { | |
public Secret getPassword() { | ||
JenkinsJVM.checkNotJenkinsJVM(); | ||
try { | ||
String appInstallationToken; | ||
synchronized (this) { | ||
try { | ||
if (cachedToken == null || cachedToken.isStale()) { | ||
|
@@ -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); | ||
} | ||
|
This file contains hidden or 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
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.
This method now only runs on a controller, let's enforce that.