xref: /aosp_15_r20/external/bazelbuild-rules_python/python/private/auth.bzl (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
1*60517a1eSAndroid Build Coastguard Worker# Copyright 2022 The Bazel Authors. All rights reserved.
2*60517a1eSAndroid Build Coastguard Worker#
3*60517a1eSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
4*60517a1eSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
5*60517a1eSAndroid Build Coastguard Worker# You may obtain a copy of the License at
6*60517a1eSAndroid Build Coastguard Worker#
7*60517a1eSAndroid Build Coastguard Worker#    http://www.apache.org/licenses/LICENSE-2.0
8*60517a1eSAndroid Build Coastguard Worker#
9*60517a1eSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*60517a1eSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
11*60517a1eSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*60517a1eSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
13*60517a1eSAndroid Build Coastguard Worker# limitations under the License.
14*60517a1eSAndroid Build Coastguard Worker
15*60517a1eSAndroid Build Coastguard Worker"""Helpers copied from http_file source to be reused here.
16*60517a1eSAndroid Build Coastguard Worker
17*60517a1eSAndroid Build Coastguard WorkerThe implementation below is copied directly from Bazel's implementation of `http_archive`.
18*60517a1eSAndroid Build Coastguard WorkerAccordingly, the return value of this function should be used identically as the `auth` parameter of `http_archive`.
19*60517a1eSAndroid Build Coastguard WorkerReference: https://github.com/bazelbuild/bazel/blob/6.3.2/tools/build_defs/repo/http.bzl#L109
20*60517a1eSAndroid Build Coastguard Worker
21*60517a1eSAndroid Build Coastguard WorkerThe helpers were further modified to support module_ctx.
22*60517a1eSAndroid Build Coastguard Worker"""
23*60517a1eSAndroid Build Coastguard Worker
24*60517a1eSAndroid Build Coastguard Workerload("@bazel_tools//tools/build_defs/repo:utils.bzl", "read_netrc", "read_user_netrc", "use_netrc")
25*60517a1eSAndroid Build Coastguard Worker
26*60517a1eSAndroid Build Coastguard Worker# Copied from https://sourcegraph.com/github.com/bazelbuild/bazel@26c6add3f9809611ad3795bce1e5c0fb37902902/-/blob/tools/build_defs/repo/http.bzl
27*60517a1eSAndroid Build Coastguard Worker_AUTH_PATTERN_DOC = """An optional dict mapping host names to custom authorization patterns.
28*60517a1eSAndroid Build Coastguard Worker
29*60517a1eSAndroid Build Coastguard WorkerIf a URL's host name is present in this dict the value will be used as a pattern when
30*60517a1eSAndroid Build Coastguard Workergenerating the authorization header for the http request. This enables the use of custom
31*60517a1eSAndroid Build Coastguard Workerauthorization schemes used in a lot of common cloud storage providers.
32*60517a1eSAndroid Build Coastguard Worker
33*60517a1eSAndroid Build Coastguard WorkerThe pattern currently supports 2 tokens: <code>&lt;login&gt;</code> and
34*60517a1eSAndroid Build Coastguard Worker<code>&lt;password&gt;</code>, which are replaced with their equivalent value
35*60517a1eSAndroid Build Coastguard Workerin the netrc file for the same host name. After formatting, the result is set
36*60517a1eSAndroid Build Coastguard Workeras the value for the <code>Authorization</code> field of the HTTP request.
37*60517a1eSAndroid Build Coastguard Worker
38*60517a1eSAndroid Build Coastguard WorkerExample attribute and netrc for a http download to an oauth2 enabled API using a bearer token:
39*60517a1eSAndroid Build Coastguard Worker
40*60517a1eSAndroid Build Coastguard Worker<pre>
41*60517a1eSAndroid Build Coastguard Workerauth_patterns = {
42*60517a1eSAndroid Build Coastguard Worker    "storage.cloudprovider.com": "Bearer &lt;password&gt;"
43*60517a1eSAndroid Build Coastguard Worker}
44*60517a1eSAndroid Build Coastguard Worker</pre>
45*60517a1eSAndroid Build Coastguard Worker
46*60517a1eSAndroid Build Coastguard Workernetrc:
47*60517a1eSAndroid Build Coastguard Worker
48*60517a1eSAndroid Build Coastguard Worker<pre>
49*60517a1eSAndroid Build Coastguard Workermachine storage.cloudprovider.com
50*60517a1eSAndroid Build Coastguard Worker        password RANDOM-TOKEN
51*60517a1eSAndroid Build Coastguard Worker</pre>
52*60517a1eSAndroid Build Coastguard Worker
53*60517a1eSAndroid Build Coastguard WorkerThe final HTTP request would have the following header:
54*60517a1eSAndroid Build Coastguard Worker
55*60517a1eSAndroid Build Coastguard Worker<pre>
56*60517a1eSAndroid Build Coastguard WorkerAuthorization: Bearer RANDOM-TOKEN
57*60517a1eSAndroid Build Coastguard Worker</pre>
58*60517a1eSAndroid Build Coastguard Worker"""
59*60517a1eSAndroid Build Coastguard Worker
60*60517a1eSAndroid Build Coastguard Worker# AUTH_ATTRS are used within whl_library and pip bzlmod extension.
61*60517a1eSAndroid Build Coastguard WorkerAUTH_ATTRS = {
62*60517a1eSAndroid Build Coastguard Worker    "auth_patterns": attr.string_dict(
63*60517a1eSAndroid Build Coastguard Worker        doc = _AUTH_PATTERN_DOC,
64*60517a1eSAndroid Build Coastguard Worker    ),
65*60517a1eSAndroid Build Coastguard Worker    "netrc": attr.string(
66*60517a1eSAndroid Build Coastguard Worker        doc = "Location of the .netrc file to use for authentication",
67*60517a1eSAndroid Build Coastguard Worker    ),
68*60517a1eSAndroid Build Coastguard Worker}
69*60517a1eSAndroid Build Coastguard Worker
70*60517a1eSAndroid Build Coastguard Workerdef get_auth(ctx, urls, ctx_attr = None):
71*60517a1eSAndroid Build Coastguard Worker    """Utility for retrieving netrc-based authentication parameters for repository download rules used in python_repository.
72*60517a1eSAndroid Build Coastguard Worker
73*60517a1eSAndroid Build Coastguard Worker    Args:
74*60517a1eSAndroid Build Coastguard Worker        ctx(repository_ctx or module_ctx): The extension module_ctx or
75*60517a1eSAndroid Build Coastguard Worker            repository rule's repository_ctx object.
76*60517a1eSAndroid Build Coastguard Worker        urls: A list of URLs from which assets will be downloaded.
77*60517a1eSAndroid Build Coastguard Worker        ctx_attr(struct): The attributes to get the netrc from. When ctx is
78*60517a1eSAndroid Build Coastguard Worker            repository_ctx, then we will attempt to use repository_ctx.attr
79*60517a1eSAndroid Build Coastguard Worker            if this is not specified, otherwise we will use the specified
80*60517a1eSAndroid Build Coastguard Worker            field. The module_ctx attributes are located in the tag classes
81*60517a1eSAndroid Build Coastguard Worker            so it cannot be retrieved from the context.
82*60517a1eSAndroid Build Coastguard Worker
83*60517a1eSAndroid Build Coastguard Worker    Returns:
84*60517a1eSAndroid Build Coastguard Worker        dict: A map of authentication parameters by URL.
85*60517a1eSAndroid Build Coastguard Worker    """
86*60517a1eSAndroid Build Coastguard Worker
87*60517a1eSAndroid Build Coastguard Worker    # module_ctx does not have attributes, as they are stored in tag classes. Whilst
88*60517a1eSAndroid Build Coastguard Worker    # the correct behaviour should be to pass the `attr` to the
89*60517a1eSAndroid Build Coastguard Worker    ctx_attr = ctx_attr or getattr(ctx, "attr", None)
90*60517a1eSAndroid Build Coastguard Worker    ctx_attr = struct(
91*60517a1eSAndroid Build Coastguard Worker        netrc = getattr(ctx_attr, "netrc", None),
92*60517a1eSAndroid Build Coastguard Worker        auth_patterns = getattr(ctx_attr, "auth_patterns", ""),
93*60517a1eSAndroid Build Coastguard Worker    )
94*60517a1eSAndroid Build Coastguard Worker
95*60517a1eSAndroid Build Coastguard Worker    if ctx_attr.netrc:
96*60517a1eSAndroid Build Coastguard Worker        netrc = read_netrc(ctx, ctx_attr.netrc)
97*60517a1eSAndroid Build Coastguard Worker    elif "NETRC" in ctx.os.environ:
98*60517a1eSAndroid Build Coastguard Worker        # This can be used on newer bazel versions
99*60517a1eSAndroid Build Coastguard Worker        if hasattr(ctx, "getenv"):
100*60517a1eSAndroid Build Coastguard Worker            netrc = read_netrc(ctx, ctx.getenv("NETRC"))
101*60517a1eSAndroid Build Coastguard Worker        else:
102*60517a1eSAndroid Build Coastguard Worker            netrc = read_netrc(ctx, ctx.os.environ["NETRC"])
103*60517a1eSAndroid Build Coastguard Worker    else:
104*60517a1eSAndroid Build Coastguard Worker        netrc = read_user_netrc(ctx)
105*60517a1eSAndroid Build Coastguard Worker
106*60517a1eSAndroid Build Coastguard Worker    return use_netrc(netrc, urls, ctx_attr.auth_patterns)
107