From 0391ef4e43fecac5b63518082294e2151f74aeb4 Mon Sep 17 00:00:00 2001 From: Dmitry Ivankov Date: Mon, 2 Oct 2023 11:32:48 +0200 Subject: [PATCH] Add support for netrc in jvm_maven_import_external (#1509) `auth=` parameter for `repository_ctx.download` is both required to fetch from private registries and is not well-documented https://github.com/bazelbuild/bazel/issues/13709#issuecomment-1336699672 With this change ```python rules_scala_toolchain_deps_repositories( maven_servers = ["some_private_artifactory_url"] ) ``` would be able to authenticate using default .netrc (like `~/.netrc`) --- scala/scala_maven_import_external.bzl | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/scala/scala_maven_import_external.bzl b/scala/scala_maven_import_external.bzl index 51b1f962c..63d5e3b75 100644 --- a/scala/scala_maven_import_external.bzl +++ b/scala/scala_maven_import_external.bzl @@ -35,6 +35,17 @@ the following macros are defined below that utilize jvm_import_external: - java_import_external - to demonstrate that the original functionality of `java_import_external` stayed intact. """ +load("@bazel_tools//tools/build_defs/repo:utils.bzl", "read_netrc", "read_user_netrc", "use_netrc") + +# https://github.com/bazelbuild/bazel/issues/13709#issuecomment-1336699672 +def _get_auth(ctx, urls): + """Given the list of URLs obtain the correct auth dict.""" + if ctx.attr.netrc: + netrc = read_netrc(ctx, ctx.attr.netrc) + else: + netrc = read_user_netrc(ctx) + return use_netrc(netrc, urls, ctx.attr.auth_patterns) + _HEADER = "# DO NOT EDIT: generated by jvm_import_external()" _PASS_PROPS = ( "neverlink", @@ -109,7 +120,7 @@ def _jvm_import_external(repository_ctx): lines.append(extra) if not extra.endswith("\n"): lines.append("") - repository_ctx.download(urls, path, sha) + repository_ctx.download(urls, path, sha, auth = _get_auth(repository_ctx, urls)) if srcurls and _should_fetch_sources_in_current_env(repository_ctx): repository_ctx.download(srcurls, srcpath, srcsha) repository_ctx.file("BUILD", "\n".join(lines)) @@ -227,6 +238,8 @@ jvm_import_external = repository_rule( default = ["//visibility:public"], ), "extra_build_file_content": attr.string(), + "auth_patterns": attr.string_dict(), + "netrc": attr.string(), }, environ = [_FETCH_SOURCES_ENV_VAR_NAME], )