Skip to content
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

401 Unauthorized errors every hour #637

Closed
tommilligan opened this issue Jul 27, 2018 · 11 comments
Closed

401 Unauthorized errors every hour #637

tommilligan opened this issue Jul 27, 2018 · 11 comments

Comments

@tommilligan
Copy link
Contributor

Bug Report

https://github.com/tommilligan/prowl-github-app/

Current Behavior
I have a probot app running full time on AWS. The app

  • listens for events
  • performs some long-running (~2 min) checks
  • responds to the GitHub API

Occasionally, I get 401 Unauthorized errors back from the API when performing actions:

HttpError: {"message":"Bad credentials","documentation_url":"https://developer.github.com/v3"}
    at response.text.then.message (/usr/src/app/node_modules/@octokit/rest/lib/request/request.js:78:19)
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:228:7)

These errors always happen at the same point in the hour - e.g. for a single instance, at 10:05, 11:05, 14:05 and 21:05.

I suspect this is because in the ~2 min check time, the OAuth token attached to the event context expires. This then results in unauthorized requests when calling context.github. New events are received with valid tokens, which do not error.

Expected behavior/code
Not to get Unauthorized errors sproradically

Environment

  • Probot version(s): 7.0.0
  • Node/npm version: node 8.9.4/npm 5.6.0
  • OS: Ubuntu 18.04.1 LTS

Possible Solution

Requests to either:

  • pass first time by always using the latest token
  • on Unauthorized, refresh access token and retry

Additional context/Screenshots

                                    |
+-----------------------------------+------------------------+
|                                                            |
| GitHub API                                                 |
|                                                            |
+-+-----+--------^--------------+---+---+---------^----------+
  |     |        |              |   |   |         |
  v     |event   |action        |       v         |
token   |        |(Authorized)  |   | token       |
  +   +-v------------+          |                 |
  +--->   context    |          |   |             |
  |   +--------------+          |                 |action
  |                             |   |             |(Unauthorized)
  |                            +v---+------------------+
  +---------------------------->  context              |
                               +----+------------------+
                                    |

                                    |
                        probot access_token expires
                                    |

                                    |
@tommilligan
Copy link
Contributor Author

Having read a bit more through Application construction, the simplest fix for me would be to have the installationToken cache ttl customiseable:

}, { ttl: 60 * 59 }) // Cache for 1 minute less than GitHub expiry

I would reduce this to ~ 5 min before expiry and it would fix my issues.

@hiimbex
Copy link
Contributor

hiimbex commented Jul 27, 2018

I'm definitely not opposed to adding an env option here, but I'm wondering what is happening in your app that takes 2 minutes?

performs some long-running (~2 min) checks

We have some pretty complex apps and we've yet to run into this issue. 🤔 I'll jump into the PR now.

@tommilligan
Copy link
Contributor Author

@hiimbex if you're interested in the actual scenario: one of the things my app does is check whether commit status is success after a CI update. I delay for a short amount of time by default (say 5s) to allow other CI systems to have started running and set pending statuses.

At work we use Concourse CI, which takes significantly longer to start. Unless I delay API actions, there's a window where the commit status has some CI passed, and some CI not started, and would cause incorrect app behaviour.

+---------------------------+
|  new pull request commit  |
+-----+--------------+------+
      |              |
+-----v------+       |
| codeclimate+----------------> pending
|            |       |
|            |       |
|            |       |
|            |       |
|            +----------------> success
+------------+       |
                     |
                     | (don't want to call API here
                     |  as there are unstarted checks)
                     |
                     |
               +-----v------+
               | concourse  +-> pending
               |            |
               |            |
               |            |
               |            |
               |            +-> success
               +------------+

@rthadur
Copy link

rthadur commented Feb 16, 2019

@tommilligan iam getting this error for every event in Google cloud functions , do you have any solution ?

@tommilligan
Copy link
Contributor Author

If it's for every event, it sounds like you have bad credentials, or all of your events take longer than an hour to process. If you have a minimal reproducible example I can take a look

@gr2m
Copy link
Contributor

gr2m commented Feb 16, 2019

@tommilligan can you check with latest Probot (npm install probot@beta)? We have new thorttling built-in as well as authenticating as installation right before the request, it definitely helped with the problem on WIP

@tommilligan
Copy link
Contributor Author

@gr2m could you clarify what your request is here? The issue is closed (I merged a PR a while ago), and I haven't experienced this issue since

@gr2m
Copy link
Contributor

gr2m commented Feb 18, 2019

All good then, sorry for the noise :) I keep having the "Bad credentials" errors from time to time with the WIP app, and upgrading to Probot 8 beta helped reduce these drastically. So I thought I’d let you know. But if you don’t see them at all anymore then don’t worry about it

@probotbot
Copy link

🎉 This issue has been resolved in version 7.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@gr2m
Copy link
Contributor

gr2m commented Nov 18, 2020

@tommilligan the custom INSTALLATION_TOKEN_TTL setting should no longer be required with the Probot v10. Installation access tokens are renewed on demand at the time of the request via https://github.com/octokit/auth-app.js/, they are no longer set statically at the time when an event is handled.

Let me know if you see a problem with removing the environment variable

GitHub
GitHub App authentication for JavaScript. Contribute to octokit/auth-app.js development by creating an account on GitHub.

@tommilligan
Copy link
Contributor Author

Thanks for the update! I don't see any issue with removing the environment variable.

I no longer maintain the app I built (although I hear it's still kicking at my old workplace). Happy to see probot is still actively developed 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants