8000 feat: Support netrc-based authentication for python_repository rule (… · bazel-contrib/rules_python@0d0e183 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0d0e183

Browse files
authored
feat: Support netrc-based authentication for python_repository rule (#1417)
This change introduces support for `netrc` and `auth_patterns` attributes in `python_repository` (and by extension, `python_register_toolchains`). This allows consuming projects to fetch custom Python toolchain binaries from a private/authenticated HTTP host when specified directly by URL in `python_register_toolchains`. The implementation proposed here mirrors that of `http_archive`: https://github.com/bazelbuild/bazel/blob/1cf392ff3918386858b8c038f82c013b1e04be98/tools/build_defs/repo/http.bzl#L116 Fixes #1215.
1 parent a07f300 commit 0d0e183

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ A brief description of the categories of changes:
4444
* (gazelle) New `# gazelle:python_generation_mode file` directive to support
4545
generating one `py_library` per file.
4646

47+
* (python_repository) Support `netrc` and `auth_patterns` attributes to enable
48+
authentication against private HTTP hosts serving Python toolchain binaries.
49+
4750
### Removed
4851

4952
* (bzlmod) The `entry_point` macro is no longer supported and has been removed
@@ -118,5 +121,3 @@ A brief description of the categories of changes:
118121
* Expose Python C headers through the toolchain.
119122

120123
[0.24.0]: https://github.com/bazelbuild/rules_python/releases/tag/0.24.0
121-
122-

python/repositories.bzl

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ For historic reasons, pip_repositories() is defined in //python:pip.bzl.
1818
"""
1919

2020
load("@bazel_tools//tools/build_defs/repo:http.bzl", _http_archive = "http_archive")
21-
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
21+
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe", "read_netrc", "read_user_netrc", "use_netrc")
2222
load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED")
2323
load("//python/private:coverage_deps.bzl", "coverage_dep")
2424
load(
@@ -85,6 +85,28 @@ def is_standalone_interpreter(rctx, python_interpreter_path):
8585
),
8686
]).return_code == 0
8787

88+
def _get_auth(rctx, urls):
89+
"""Utility for retrieving netrc-based authentication parameters for repository download rules used in python_repository.
90+
91+
The implementation below is copied directly from Bazel's implementation of `http_archive`.
92+
Accordingly, the return value of this function should be used identically as the `auth` parameter of `http_archive`.
93+
Reference: https://github.com/bazelbuild/bazel/blob/6.3.2/tools/build_defs/repo/http.bzl#L109
94+
95+
Args:
96+
rctx (repository_ctx): The repository rule's context object.
97+
urls: A list of URLs from which assets will be downloaded.
98+
99+
Returns:
100+
dict: A map of authentication parameters by URL.
101+
"""
102+
if rctx.attr.netrc:
103+
netrc = read_netrc(rctx, rctx.attr.netrc)
104+
elif "NETRC" in rctx.os.environ:
105+
netrc = read_netrc(rctx, rctx.os.environ["NETRC"])
106+
else:
107+
netrc = read_user_netrc(rctx)
108+
return use_netrc(netrc, urls, rctx.attr.auth_patterns)
109+
88110
def _python_repository_impl(rctx):
89111
if rctx.attr.distutils and rctx.attr.distutils_content:
90112
fail("Only one of (distutils, distutils_content) should be set.")
@@ -96,19 +118,22 @@ def _python_repository_impl(rctx):
96118
python_short_version = python_version.rpartition(".")[0]
97119
release_filename = rctx.attr.release_filename
98120
urls = rctx.attr.urls or [rctx.attr.url]
121+
auth = _get_auth(rctx, urls)
99122

100123
if release_filename.endswith(".zst"):
101124
rctx.download(
102125
url = urls,
103126
sha256 = rctx.attr.sha256,
104127
output = release_filename,
128+
auth = auth,
105129
)
106130
unzstd = rctx.which("unzstd")
107131
if not unzstd:
108132
url = rctx.attr.zstd_url.format(version = rctx.attr.zstd_version)
109133
rctx.download_and_extract(
110134
url = url,
111135
sha256 = rctx.attr.zstd_sha256,
136+
auth = auth,
112137
)
113138
working_directory = "zstd-{version}".format(version = rctx.attr.zstd_version)
114139

@@ -146,6 +171,7 @@ def _python_repository_impl(rctx):
146171
url = urls,
147172
sha256 = rctx.attr.sha256,
148173
stripPrefix = rctx.attr.strip_prefix,
174+
auth = auth,
149175
)
150176

151177
patches = rctx.attr.patches
@@ -348,11 +374,13 @@ py_cc_toolchain(
348374
rctx.file("BUILD.bazel", build_content)
349375

350376
attrs = {
377+
"auth_patterns": rctx.attr.auth_patterns,
351378
"coverage_tool": rctx.attr.coverage_tool,
352379
"distutils": rctx.attr.distutils,
353380
"distutils_content": rctx.attr.distutils_content,
354381
"ignore_root_user_error": rctx.attr.ignore_root_user_error,
355382
"name": rctx.attr.name,
383+
"netrc": rctx.attr.netrc,
356384
"patches": rctx.attr.patches,
357385
"platform": platform,
358386
"python_version": python_version,
@@ -372,6 +400,9 @@ python_repository = repository_rule(
372400
_python_repository_impl,
373401
doc = "Fetches the external tools needed for the Python toolchain.",
374402
attrs = {
403+
"auth_patterns": attr.string_dict(
404+
doc = "Override mapping of hostnames to authorization patterns; mirrors the eponymous attribute from http_archive",
405+
),
375406
"coverage_tool": attr.string(
376407
# Mirrors the definition at
377408
# https://github.com/bazelbuild/bazel/blob/master/src/main/starlark/builtins_bzl/common/python/py_runtime_rule.bzl
@@ -412,6 +443,9 @@ For more information see the official bazel docs
412443
doc = "Whether the check for root should be ignored or not. This causes cache misses with .pyc files.",
413444
mandatory = False,
414445
),
446+
"netrc": attr.string(
447+
doc = ".netrc file to use for authentication; mirrors the eponymous attribute from http_archive",
448+
),
415449
"patches": attr.label_list(
416450
doc = "A list of patch files to apply to the unpacked interpreter",
417451
mandatory = False,

0 commit comments

Comments
 (0)
0