-
Notifications
You must be signed in to change notification settings - Fork 371
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
Cache app installation token #291
Changes from 4 commits
60d07a0
9da1f5a
6d8b52a
023cdca
5cb7140
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,13 +5,15 @@ | |
import com.cloudbees.plugins.credentials.impl.BaseStandardCredentials; | ||
import edu.umd.cs.findbugs.annotations.CheckForNull; | ||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
import hudson.Extension; | ||
import hudson.Util; | ||
import hudson.util.FormValidation; | ||
import hudson.util.ListBoxModel; | ||
import hudson.util.Secret; | ||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
import org.kohsuke.accmod.Restricted; | ||
import org.kohsuke.accmod.restrictions.NoExternalUse; | ||
import org.kohsuke.github.GHApp; | ||
|
@@ -27,6 +29,7 @@ | |
import static org.jenkinsci.plugins.github_branch_source.GitHubSCMNavigator.DescriptorImpl.getPossibleApiUriItems; | ||
import static org.jenkinsci.plugins.github_branch_source.JwtHelper.createJWT; | ||
|
||
@SuppressFBWarnings(value = "SE_NO_SERIALVERSIONID", justification = "XStream") | ||
public class GitHubAppCredentials extends BaseStandardCredentials implements StandardUsernamePasswordCredentials { | ||
|
||
private static final String ERROR_AUTHENTICATING_GITHUB_APP = "Couldn't authenticate with GitHub app ID %s"; | ||
|
@@ -39,6 +42,9 @@ public class GitHubAppCredentials extends BaseStandardCredentials implements Sta | |
|
||
private String apiUri; | ||
|
||
private transient String cachedToken; | ||
private transient long tokenCacheTime; | ||
|
||
@DataBoundConstructor | ||
@SuppressWarnings("unused") // by stapler | ||
public GitHubAppCredentials( | ||
|
@@ -105,7 +111,15 @@ public Secret getPassword() { | |
apiUri = "https://api.github.com"; | ||
} | ||
|
||
String appInstallationToken = generateAppInstallationToken(appID, privateKey.getPlainText(), apiUri); | ||
long now = System.currentTimeMillis(); | ||
String appInstallationToken; | ||
if (cachedToken != null && now - tokenCacheTime < JwtHelper.VALIDITY_MS /* extra buffer */ / 2) { | ||
appInstallationToken = cachedToken; | ||
} else { | ||
appInstallationToken = generateAppInstallationToken(appID, privateKey.getPlainText(), apiUri); | ||
cachedToken = appInstallationToken; | ||
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. This token is valid for an hour not 10 minutes, and the The generated JWT that's used to get this token can only be valid for up to 10 minutes, but the retrieved token is an hour (I didn't see any documentation on this but I just tried it out locally). 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. Yup, this is shows the same. 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. So I guess you are saying that we could potentially stretch out the interval between token refreshes to, say, 55m? Would be nice to see some docs on this though. 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.
Yes, but probably better to look at expires_at in case they change it, I had a look and couldn't see any docs on it |
||
tokenCacheTime = now; | ||
} | ||
|
||
return Secret.fromString(appInstallationToken); | ||
} | ||
|
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.
is this now valid for 4 minutes?
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.
No, the token should be valid for 8m, but will be replaced after 4m.