Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support loading http credentials from netrc #2623

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion rust/private/repository_utils.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
"""Utility macros for use in rules_rust repository rules"""

load(
"@bazel_tools//tools/build_defs/repo:utils.bzl",
"read_netrc",
"read_user_netrc",
"use_netrc",
)
load("//rust:known_shas.bzl", "FILE_KEY_TO_SHA")
load(
"//rust/platform:triple_mappings.bzl",
Expand Down Expand Up @@ -804,10 +810,39 @@ def load_arbitrary_tool(

return {archive_path: sha256}

# The function is copied from the main branch of bazel_tools.
# It should become available there from version 8.0.0,
# We should remove this function when we upgrade minimum supported
# version to 8.0.0.
golovasteek marked this conversation as resolved.
Show resolved Hide resolved
# https://github.com/bazelbuild/bazel/blob/d37762b494a4e122d46a5a71e3a8cc77fa15aa25/tools/build_defs/repo/utils.bzl#L424-L446
def _get_auth(ctx, urls):
"""Utility function to obtain the correct auth dict for a list of urls from .netrc file.

Support optional netrc and auth_patterns attributes if available.

Args:
ctx: The repository context of the repository rule calling this utility
function.
urls: the list of urls to read

Returns:
the auth dict which can be passed to repository_ctx.download
"""
if hasattr(ctx.attr, "netrc") and ctx.attr.netrc:
netrc = read_netrc(ctx, ctx.attr.netrc)
elif "NETRC" in ctx.os.environ:
netrc = read_netrc(ctx, ctx.os.environ["NETRC"])
else:
netrc = read_user_netrc(ctx)
auth_patterns = {}
if hasattr(ctx.attr, "auth_patterns") and ctx.attr.auth_patterns:
auth_patterns = ctx.attr.auth_patterns
return use_netrc(netrc, urls, auth_patterns)

def _make_auth_dict(ctx, urls):
auth = getattr(ctx.attr, "auth", {})
if not auth:
return {}
return _get_auth(ctx, urls)
ret = {}
for url in urls:
ret[url] = auth
Expand Down
Loading