-
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
Cache app installation token #291
Conversation
src/main/java/org/jenkinsci/plugins/github_branch_source/JwtHelper.java
Outdated
Show resolved
Hide resolved
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.
Looks good, I haven't tested interactively
String appInstallationToken = generateAppInstallationToken(appID, privateKey.getPlainText(), apiUri); | ||
long now = System.currentTimeMillis(); | ||
String appInstallationToken; | ||
if (cachedToken != null && now - tokenCacheTime < JwtHelper.VALIDITY_MS /* extra buffer */ / 2) { |
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.
Plugin bug
|
isn't it a remoting issue? |
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.
LGTM 👍
This seems to work for me, but has anyone else actually tried running it? |
@jglick Could you resolve the conflict here? |
(with #290 FTR) |
…ugin into GitHubAppCredentials.cachedToken
github-api GitHubWireMockRule makes it easy to do this. It is on my list to port that to this project, but I haven't had time yet. EDIT: oh, you meant Jenkins Logger not rather than test logger. |
@jglick @timja Currently each call to This change makes it so the same key is used for 4 minutes and then a new one is is created. The only key is still valid for a total of 8 minutes but that means may become invalid in as little as 4 minutes. What happens if that GitHub object is still being used when the key expires?
If there is bad behavior in the above situation, it may now start happening as soon as 4 minutes after a I understand we create new Is there a point in the code that we're handling these errors and I'm not seeing it? |
@timja Also, this seems like functionality the would be useful for anyone that uses github-api with GitHub Apps. |
Is there any code hanging on to an instance of |
appInstallationToken = cachedToken; | ||
} else { | ||
appInstallationToken = generateAppInstallationToken(appID, privateKey.getPlainText(), apiUri, owner); | ||
cachedToken = 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.
This token is valid for an hour not 10 minutes, and the expires_at
field should be looked at (in case it changes in the future) https://github.com/jglick/github-branch-source-plugin-1/blob/GitHubAppCredentials.cachedToken/src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubAppCredentials.java#L125-L129
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, this is shows the same.
https://github.com/github-api/github-api/blob/3cbddf1de9e6a0f261a55247a54f8afdfd168fef/src/test/resources/org/kohsuke/github/GHAppTest/wiremock/createToken/mappings/mapping-app-installations-3755540-access_tokens-7W6Uy.json#L22
https://github.com/github-api/github-api/blob/3cbddf1de9e6a0f261a55247a54f8afdfd168fef/src/test/resources/org/kohsuke/github/GHAppTest/wiremock/createToken/__files/body-app-installations-3755540-access_tokens-7W6Uy.json#L3
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.
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 comment
The 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.
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
yes it would be great for caching of tokens to be handled there and possibly the authentication as a github app. the complexities are around the key format and having to pull in JWT libraries and possibly bouncy castle if you want to handle the key format that github gives you the key in (here we work around that by asking the user to convert it to a modern format). |
Can we make those optional dependencies similar to Okhttp? |
should be able to |
Description
Amends #269 by caching the app installation token for almost the duration of its validity. Otherwise every time the token is requested, which would be for every API call to GitHub, Jenkins would by my count be making three additional HTTP requests:
/app
,/app/installations
, and/app/installations/:id/access_tokens
.Tested directly via
jshell
(note that this requires jenkinsci/jenkins#4603):I am not sure if there is a simple way to create a logger which records every GH API access.
Have been running some simple multibranch projects with this test and have not noticed any issues.
Submitter checklist
Reviewer checklist
Documentation changes
Users/aliases to notify