From 0402c92abeaefca0c8c4b177a5cbab1d0851347f Mon Sep 17 00:00:00 2001 From: Roman Mohr Date: Sat, 29 Mar 2025 10:52:18 -0700 Subject: [PATCH] Add auth_patterns support to RPM rule Sometimes RPM rules are on non-private locations. Add .netrc support to RPM rules. Signed-off-by: Roman Mohr --- cmd/lockfile.go | 8 ++++---- cmd/resolve.go | 2 +- cmd/rpmtree.go | 16 ++++++++-------- internal/rpm.bzl | 12 +++++++++++- pkg/reducer/helper_test.go | 2 +- pkg/sat/sat_test.go | 12 ++++++------ 6 files changed, 31 insertions(+), 21 deletions(-) diff --git a/cmd/lockfile.go b/cmd/lockfile.go index e74bb9f5..b717b7e5 100644 --- a/cmd/lockfile.go +++ b/cmd/lockfile.go @@ -10,9 +10,9 @@ import ( ) type lockfileOpts struct { - repofiles []string - configname string - lockfile string + repofiles []string + configname string + lockfile string } var lockfileopts = lockfileOpts{} @@ -22,7 +22,7 @@ func NewLockFileCmd() *cobra.Command { lockfileCmd := &cobra.Command{ Use: "lockfile", Short: "Manage bazeldnf lock file", - Long: `Keep the bazeldnf lock file up to date using a set of dependencies`, + Long: `Keep the bazeldnf lock file up to date using a set of dependencies`, Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, required []string) error { repos, err := repo.LoadRepoFiles(lockfileopts.repofiles) diff --git a/cmd/resolve.go b/cmd/resolve.go index 42a3a657..fa0e9078 100644 --- a/cmd/resolve.go +++ b/cmd/resolve.go @@ -10,7 +10,7 @@ import ( ) type resolveOpts struct { - repofiles []string + repofiles []string } var resolveopts = resolveOpts{} diff --git a/cmd/rpmtree.go b/cmd/rpmtree.go index 6a3d177f..cb7e3aba 100644 --- a/cmd/rpmtree.go +++ b/cmd/rpmtree.go @@ -14,14 +14,14 @@ import ( ) type rpmtreeOpts struct { - repofiles []string - workspace string - toMacro string - buildfile string - configname string - lockfile string - name string - public bool + repofiles []string + workspace string + toMacro string + buildfile string + configname string + lockfile string + name string + public bool } var rpmtreeopts = rpmtreeOpts{} diff --git a/internal/rpm.bzl b/internal/rpm.bzl index 9b7cda7e..c3740964 100644 --- a/internal/rpm.bzl +++ b/internal/rpm.bzl @@ -14,7 +14,7 @@ "Exposes rpm files to a Bazel workspace" -load("@bazel_tools//tools/build_defs/repo:utils.bzl", "update_attrs") +load("@bazel_tools//tools/build_defs/repo:utils.bzl", "read_netrc", "read_user_netrc", "update_attrs", "use_netrc") RpmInfo = provider( doc = """\ @@ -29,6 +29,14 @@ RpmInfo = provider( }, ) +def _get_auth(ctx, urls): + """Given the list of URLs obtain the correct auth dict.""" + if "NETRC" in ctx.os.environ: + netrc = read_netrc(ctx, ctx.os.environ["NETRC"]) + else: + netrc = read_user_netrc(ctx) + return use_netrc(netrc, urls, ctx.attr.auth_patterns) + def _rpm_rule_impl(ctx): """\ Implementation for the rpm rule @@ -82,6 +90,7 @@ def _rpm_impl(ctx): ctx.download( url = ctx.attr.urls, output = "rpm/" + downloaded_file_path, + auth = _get_auth(ctx, ctx.attr.urls), **args ) else: @@ -104,6 +113,7 @@ _rpm_attrs = { mandatory = False, providers = [RpmInfo], ), + "auth_patterns": attr.string_dict(), } rpm = repository_rule( diff --git a/pkg/reducer/helper_test.go b/pkg/reducer/helper_test.go index d1926164..a6317aa3 100644 --- a/pkg/reducer/helper_test.go +++ b/pkg/reducer/helper_test.go @@ -5,7 +5,7 @@ import ( "github.com/rmohr/bazeldnf/pkg/api/bazeldnf" ) -func withRepository(packages []api.Package) []api.Package{ +func withRepository(packages []api.Package) []api.Package { r := []api.Package{} for _, p := range packages { p.Repository = &bazeldnf.Repository{} diff --git a/pkg/sat/sat_test.go b/pkg/sat/sat_test.go index 7871e74e..eafe3641 100644 --- a/pkg/sat/sat_test.go +++ b/pkg/sat/sat_test.go @@ -1354,9 +1354,9 @@ func TestNewResolver(t *testing.T) { "testa", }, ignoreRegex: []string{"testb.*"}, - install: []string{"testa-0:1", "testc-0:1"}, - exclude: []string{}, - solvable: true, + install: []string{"testa-0:1", "testc-0:1"}, + exclude: []string{}, + solvable: true, }, {name: "only allow package", packages: []*api.Package{ newPkg("testa", "1", []string{}, []string{"b", "c"}, []string{}), @@ -1366,9 +1366,9 @@ func TestNewResolver(t *testing.T) { "testa", }, allowRegex: []string{"testa.*"}, - install: []string{"testa-0:1"}, - exclude: []string{}, - solvable: true, + install: []string{"testa-0:1"}, + exclude: []string{}, + solvable: true, }, // TODO: Add test cases. }