Skip to content

Commit ce8bd90

Browse files
guwcopybara-github
authored andcommitted
Read authentication information from .netrc
Workaround for #13709 Closes #20417. PiperOrigin-RevId: 588762567 Change-Id: Ie02d7ff6ffaad646bf67059bebc7e0d5894f079e
1 parent dc5dd04 commit ce8bd90

5 files changed

Lines changed: 48 additions & 30 deletions

File tree

MODULE.bazel.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/test/tools/bzlmod/MODULE.bazel.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/build_defs/repo/http.bzl

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,9 @@ load(
4545
)
4646
load(
4747
":utils.bzl",
48+
"get_auth",
4849
"patch",
49-
"read_netrc",
50-
"read_user_netrc",
5150
"update_attrs",
52-
"use_netrc",
5351
"workspace_and_buildfile",
5452
)
5553

@@ -119,16 +117,6 @@ Authorization: Bearer RANDOM-TOKEN
119117
</pre>
120118
"""
121119

122-
def _get_auth(ctx, urls):
123-
"""Given the list of URLs obtain the correct auth dict."""
124-
if ctx.attr.netrc:
125-
netrc = read_netrc(ctx, ctx.attr.netrc)
126-
elif "NETRC" in ctx.os.environ:
127-
netrc = read_netrc(ctx, ctx.os.environ["NETRC"])
128-
else:
129-
netrc = read_user_netrc(ctx)
130-
return use_netrc(netrc, urls, ctx.attr.auth_patterns)
131-
132120
def _update_integrity_attr(ctx, attrs, download_info):
133121
# We don't need to override the integrity attribute if sha256 is already specified.
134122
integrity_override = {} if ctx.attr.sha256 else {"integrity": download_info.integrity}
@@ -140,7 +128,7 @@ def _http_archive_impl(ctx):
140128
fail("Only one of build_file and build_file_content can be provided.")
141129

142130
all_urls = _get_all_urls(ctx)
143-
auth = _get_auth(ctx, all_urls)
131+
auth = get_auth(ctx, all_urls)
144132

145133
download_info = ctx.download_and_extract(
146134
all_urls,
@@ -182,7 +170,7 @@ def _http_file_impl(ctx):
182170
if download_path in forbidden_files or not str(download_path).startswith(str(repo_root)):
183171
fail("'%s' cannot be used as downloaded_file_path in http_file" % ctx.attr.downloaded_file_path)
184172
all_urls = _get_all_urls(ctx)
185-
auth = _get_auth(ctx, all_urls)
173+
auth = get_auth(ctx, all_urls)
186174
download_info = ctx.download(
187175
all_urls,
188176
"file/" + downloaded_file_path,
@@ -219,7 +207,7 @@ filegroup(
219207
def _http_jar_impl(ctx):
220208
"""Implementation of the http_jar rule."""
221209
all_urls = _get_all_urls(ctx)
222-
auth = _get_auth(ctx, all_urls)
210+
auth = get_auth(ctx, all_urls)
223211
downloaded_file_name = ctx.attr.downloaded_file_name
224212
download_info = ctx.download(
225213
all_urls,

tools/build_defs/repo/jvm.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ load(
4444
"DEFAULT_CANONICAL_ID_ENV",
4545
"get_default_canonical_id",
4646
)
47+
load(
48+
":utils.bzl",
49+
"get_auth",
50+
)
4751

4852
_HEADER = "# DO NOT EDIT: generated by jvm_import_external()"
4953

@@ -124,13 +128,15 @@ def _jvm_import_external(repository_ctx):
124128
path,
125129
sha,
126130
canonical_id = repository_ctx.attr.canonical_id or get_default_canonical_id(repository_ctx, urls),
131+
auth = get_auth(repository_ctx, urls),
127132
)
128133
if srcurls and _should_fetch_sources_in_current_env(repository_ctx):
129134
repository_ctx.download(
130135
srcurls,
131136
srcpath,
132137
srcsha,
133138
canonical_id = repository_ctx.attr.canonical_id,
139+
auth = get_auth(repository_ctx, srcurls),
134140
)
135141
repository_ctx.file("BUILD", "\n".join(lines))
136142
repository_ctx.file("%s/BUILD" % extension, "\n".join([

tools/build_defs/repo/utils.bzl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,3 +420,27 @@ def read_user_netrc(ctx):
420420
if not ctx.path(netrcfile).exists:
421421
return {}
422422
return read_netrc(ctx, netrcfile)
423+
424+
def get_auth(ctx, urls):
425+
"""Utility function to obtain the correct auth dict for a list of urls from .netrc file.
426+
427+
Support optional netrc and auth_patterns attributes if available.
428+
429+
Args:
430+
ctx: The repository context of the repository rule calling this utility
431+
function.
432+
urls: the list of urls to read
433+
434+
Returns:
435+
the auth dict which can be passed to repository_ctx.download
436+
"""
437+
if hasattr(ctx.attr, "netrc") and ctx.attr.netrc:
438+
netrc = read_netrc(ctx, ctx.attr.netrc)
439+
elif "NETRC" in ctx.os.environ:
440+
netrc = read_netrc(ctx, ctx.os.environ["NETRC"])
441+
else:
442+
netrc = read_user_netrc(ctx)
443+
auth_patterns = {}
444+
if hasattr(ctx.attr, "auth_patterns") and ctx.attr.auth_patterns:
445+
auth_patterns = ctx.attr.auth_patterns
446+
return use_netrc(netrc, urls, auth_patterns)

0 commit comments

Comments
 (0)