Skip to content

Commit d0e6447

Browse files
authored
Add Configuration variable to allow using custom Authorization header
1 parent cfa90e9 commit d0e6447

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ This behavior can be disabled by setting the environment variable `BAZELISK_SKIP
179179

180180
You can control the user agent that Bazelisk sends in all HTTP requests by setting `BAZELISK_USER_AGENT` to the desired value.
181181

182+
You can set the Authorization header that Bazelisk sends in all HTTP requests by setting `BAZELISK_AUTH_HEADER` to the desired value.
183+
```shell
184+
export BAZELISK_AUTH_HEADER="bearer <your_token_here>"
185+
```
186+
182187
On Windows, Bazelisk will also consider the following files in addition to `tools/bazel`:
183188

184189
* `tools/bazel.ps1` (PowerShell)

core/core.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ func RunBazeliskWithArgsFuncAndConfig(argsFunc ArgsFunc, repos *Repositories, co
9797
// repositories and config, writing its stdout to the passed writer.
9898
func RunBazeliskWithArgsFuncAndConfigAndOut(argsFunc ArgsFunc, repos *Repositories, config config.Config, out io.Writer) (int, error) {
9999
httputil.UserAgent = getUserAgent(config)
100+
httputil.AuthHeader = getAuthHeader(config)
100101

101102
bazelInstallation, err := GetBazelInstallation(repos, config)
102103
if err != nil {
@@ -289,6 +290,14 @@ func getUserAgent(config config.Config) string {
289290
return fmt.Sprintf("Bazelisk/%s", BazeliskVersion)
290291
}
291292

293+
func getAuthHeader(config config.Config) string {
294+
auth_header := config.Get("BAZELISK_AUTH_HEADER")
295+
if len(auth_header) > 0 {
296+
return auth_header
297+
}
298+
return ""
299+
}
300+
292301
// GetBazelVersion returns the Bazel version that should be used.
293302
func GetBazelVersion(config config.Config) (string, error) {
294303
// Check in this order:

httputil/httputil.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ var (
2727
DefaultTransport = http.DefaultTransport
2828
// UserAgent is passed to every HTTP request as part of the 'User-Agent' header.
2929
UserAgent = "Bazelisk"
30+
// AuthHeader is optionally set to a value that is passed as part of the 'Authorization' header in HTTP requests.
31+
AuthHeader = ""
3032
linkPattern = regexp.MustCompile(`<(.*?)>; rel="(\w+)"`)
3133

3234
// RetryClock is used for waiting between HTTP request retries.
@@ -213,10 +215,16 @@ func DownloadBinary(originURL, destDir, destFile string, config config.Config) (
213215
log.Printf("Downloading %s...", originURL)
214216

215217
var auth string = ""
216-
t, err := tryFindNetrcFileCreds(u.Host)
217-
if err == nil {
218-
// successfully parsed netrc for given host
219-
auth = t
218+
if AuthHeader != "" {
219+
// If AuthHeader is set, use it as the Authorization header.
220+
log.Printf("Authorization header is set using BAZELISK_AUTH_HEADER, using it for %s", u.Host)
221+
auth = AuthHeader
222+
} else {
223+
t, err := tryFindNetrcFileCreds(u.Host)
224+
if err == nil {
225+
// successfully parsed netrc for given host
226+
auth = t
227+
}
220228
}
221229

222230
resp, err := get(originURL, auth)

0 commit comments

Comments
 (0)