Skip to content

Commit e32531f

Browse files
committed
Add Configuration variable to allow using custom Authorization header
1 parent 8f6daef commit e32531f

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
@@ -221,6 +221,11 @@ This behavior can be disabled by setting the environment variable `BAZELISK_SKIP
221221

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

224+
You can set the Authorization header that Bazelisk sends in all HTTP requests by setting `BAZELISK_AUTH_HEADER` to the desired value.
225+
```shell
226+
export BAZELISK_AUTH_HEADER="bearer <your_token_here>"
227+
```
228+
224229
On Windows, Bazelisk will also consider the following files in addition to `tools/bazel`:
225230

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

core/core.go

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

103104
bazelInstallation, err := GetBazelInstallation(repos, config)
104105
if err != nil {
@@ -300,6 +301,14 @@ func getUserAgent(config config.Config) string {
300301
return fmt.Sprintf("Bazelisk/%s", BazeliskVersion)
301302
}
302303

304+
func getAuthHeader(config config.Config) string {
305+
auth_header := config.Get("BAZELISK_AUTH_HEADER")
306+
if len(auth_header) > 0 {
307+
return auth_header
308+
}
309+
return ""
310+
}
311+
303312
// GetBazelVersion returns the Bazel version that should be used.
304313
func GetBazelVersion(config config.Config) (string, error) {
305314
// 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)