feat: add support to use a bearer token for authenticated backends on client #207
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.
Overview
The changes introduce support for injecting a bearer token into HTTP requests made by the pushprox-client. This token is read from a file and automatically reloaded if the file changes (useful for short-lived Kubernetes tokens).
Key Changes and Reasons
1. Added
--bearer-token-path
Flagbearer-token-path
to specify the file path containing the bearer token.2. Global Variables for Bearer Token and Mutex
bearerToken
(string) to hold the current token.bearerTokenMutex
(sync.RWMutex) to safely handle concurrent access.3. Token Injection in HTTP Requests
doScrape
method, before sending the scrape request, the code:Authorization
header asBearer <token>
if it’s not empty.4. Automatic Token Reloading via File Watcher
watchBearerTokenFile
that:fsnotify
.5. Launching the Token Watcher in
main()
--bearer-token-path
is set, starts a goroutine to runwatchBearerTokenFile
.6. updated main_test file to match the code changes.
Additional Notes
Proper synchronization with a mutex avoids race conditions between the file watcher updating the token and HTTP requests reading it.
The existing TLS and proxy settings remain unchanged.
Logging was added to track loading and reloading of the bearer token.
local tests: