fix: authenticate GitHub API requests with GH_TOKEN#191
fix: authenticate GitHub API requests with GH_TOKEN#191jaeyeom wants to merge 1 commit intogabyx:mainfrom
Conversation
Unauthenticated GitHub API requests are limited to 60 req/hr per IP, which causes "403 API rate limit exceeded" errors during installation. Authenticated requests get 5,000 req/hr. This change uses the GH_TOKEN environment variable (when set) to add an Authorization header to all GitHub API requests made during install and update. Two call sites are patched: 1. GetFile() in download.go: replaced http.Get() with http.NewRequest + http.DefaultClient.Do() so we can attach the Bearer token header. This covers all asset and checksum downloads (used by github.go, gitea.go, and checksums.go). 2. downloadGithub() in github.go: the go-github client now receives a custom *http.Client with a tokenTransport RoundTripper that injects the Authorization header on every request. When GH_TOKEN is unset, nil is passed, preserving the existing default-client behavior. This avoids adding an oauth2 dependency by using a lightweight RoundTripper wrapper instead.
|
This is a kind of minimum change. But accessing an environment variable deep in a library package an anti-pattern, since env var is like a global variable. It makes library functions impure, harder to test, etc. So if more diff lines is acceptable, I also have another change that accesses OS env var in the main package. |
gabyx
left a comment
There was a problem hiding this comment.
Thanks for this addition: could we just use the newest Gh module? v50 to keep it even cleaner ?
Otherwise looks very clean.
Maybe add a Docs entry or a comment at the appropriate deploy.yaml
which is either in the docs or somewhere (?).
Yes. I think making this up to the latest version make sense. I'll create another PR to see if there'a any broken changes, to isolate with this feature. |
|
@jaeyeom : Jeah accessing the env. var is kind of ugly inside the package. I guess since this code is only used in Also to say is that I probably did not account properly for secrets like this PR introduces etc in the Cause any
Option 3 is really complicated, we might go with the first pragmatic approach and maybe we can on |
|
@jaeyeom : rebase for fixing the linting changes, probably =) |
That's why I raised that concern from the very beginning. Maybe you've missed it. :)
I agree with you on your approach. Github already shows the error message in their REST API about this. A good news that, as you already know, So I guess we have consensus on the option 1, using env var. Did I understand it correctly? Option 2 is probably additional feature for interactive shell, but lower priority in my opinion. Option 3 is overly complicated as you said. I guess you can set secret to github like the following without exposing it in I'll create another PR for reading env var in the main package, which touches more files and bigger diffs, but better in terms of architecture. |
Unauthenticated GitHub API requests are limited to 60 req/hr per IP, which causes "403 API rate limit exceeded" errors during installation. Authenticated requests get 5,000 req/hr. This change uses the GH_TOKEN environment variable (when set) to add an Authorization header to all GitHub API requests made during install and update.
Two call sites are patched:
GetFile() in download.go: replaced http.Get() with http.NewRequest + http.DefaultClient.Do() so we can attach the Bearer token header. This covers all asset and checksum downloads (used by github.go, gitea.go, and checksums.go).
downloadGithub() in github.go: the go-github client now receives a custom *http.Client with a tokenTransport RoundTripper that injects the Authorization header on every request. When GH_TOKEN is unset, nil is passed, preserving the existing default-client behavior.
This avoids adding an oauth2 dependency by using a lightweight RoundTripper wrapper instead.