From 903d7ca509a1de36a8eb49be833372505ccde8f8 Mon Sep 17 00:00:00 2001 From: Martin Kosiba Date: Mon, 25 Nov 2024 16:18:12 +0000 Subject: [PATCH] Add .netrc support --- MODULE.bazel | 1 + go.mod | 1 + go.sum | 2 ++ pkg/repo/BUILD.bazel | 1 + pkg/repo/fetch.go | 40 +++++++++++++++++++++++++++++++++++++++- 5 files changed, 44 insertions(+), 1 deletion(-) diff --git a/MODULE.bazel b/MODULE.bazel index 7768f0a..5754b4f 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -27,6 +27,7 @@ use_repo( go_deps, "com_github_bazelbuild_buildtools", "com_github_crillab_gophersat", + "com_github_jdx_go_netrc", "com_github_onsi_gomega", "com_github_sassoftware_go_rpmutils", "com_github_sirupsen_logrus", diff --git a/go.mod b/go.mod index fcddc6c..eb6b84c 100644 --- a/go.mod +++ b/go.mod @@ -17,6 +17,7 @@ require ( github.com/golang/protobuf v1.5.2 // indirect github.com/google/go-cmp v0.5.9 // indirect github.com/inconshreveable/mousetrap v1.0.1 // indirect + github.com/jdx/go-netrc v1.0.0 // indirect github.com/klauspost/compress v1.11.1 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect diff --git a/go.sum b/go.sum index bd57843..5bbddde 100644 --- a/go.sum +++ b/go.sum @@ -15,6 +15,8 @@ github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc= github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/jdx/go-netrc v1.0.0 h1:QbLMLyCZGj0NA8glAhxUpf1zDg6cxnWgMBbjq40W0gQ= +github.com/jdx/go-netrc v1.0.0/go.mod h1:Gh9eFQJnoTNIRHXl2j5bJXA1u84hQWJWgGh569zF3v8= github.com/klauspost/compress v1.11.1 h1:bPb7nMRdOZYDrpPMTA3EInUQrdgoBinqUuSwlGdKDdE= github.com/klauspost/compress v1.11.1/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= github.com/onsi/ginkgo/v2 v2.7.0 h1:/XxtEV3I3Eif/HobnVx9YmJgk8ENdRsuUmM+fLCFNow= diff --git a/pkg/repo/BUILD.bazel b/pkg/repo/BUILD.bazel index 4ce6534..ce68b44 100644 --- a/pkg/repo/BUILD.bazel +++ b/pkg/repo/BUILD.bazel @@ -13,6 +13,7 @@ go_library( "//pkg/api", "//pkg/api/bazeldnf", "//pkg/rpm", + "@com_github_jdx_go_netrc//:go-netrc", "@com_github_sirupsen_logrus//:logrus", "@io_k8s_sigs_yaml//:yaml", ], diff --git a/pkg/repo/fetch.go b/pkg/repo/fetch.go index 15bac70..3777125 100644 --- a/pkg/repo/fetch.go +++ b/pkg/repo/fetch.go @@ -2,6 +2,7 @@ package repo import ( "crypto/sha256" + "encoding/base64" "encoding/hex" "fmt" "hash" @@ -9,10 +10,12 @@ import ( "net/http" "net/url" "os" + "os/user" "path" "path/filepath" "strings" + "github.com/jdx/go-netrc" "github.com/rmohr/bazeldnf/pkg/api" "github.com/rmohr/bazeldnf/pkg/api/bazeldnf" log "github.com/sirupsen/logrus" @@ -229,6 +232,41 @@ func fileGet(filename string) (*http.Response, error) { return resp, nil } +var netrcCached *netrc.Netrc + +func getNetrc() (*netrc.Netrc, error) { + if netrcCached != nil { + return netrcCached, nil + } + usr, err := user.Current() + if err != nil { + return nil, fmt.Errorf("getting current user: %w", err) + } + n, err := netrc.Parse(filepath.Join(usr.HomeDir, ".netrc")) + if err == nil { + netrcCached = n + } + return n, err +} + +func httpGet(rawUrl string) (*http.Response, error) { + url, err := url.Parse(rawUrl) + if err != nil { + return nil, fmt.Errorf("parsing %s as a URL: %w", rawUrl, err) + } + netrc, err := getNetrc() + if err != nil { + return nil, fmt.Errorf("getting netrc: %w", err) + } + m := netrc.Machine(url.Hostname()) + req, err := http.NewRequest("GET", rawUrl, nil) + if m != nil { + auth := m.Get("login") + ":" + m.Get("password") + req.Header.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(auth))) + } + return http.DefaultClient.Do(req) +} + func (*getterImpl) Get(rawURL string) (*http.Response, error) { u, err := url.Parse(rawURL) if err != nil { @@ -237,7 +275,7 @@ func (*getterImpl) Get(rawURL string) (*http.Response, error) { if u.Scheme == "file" { return fileGet(u.Path) } - return http.Get(rawURL) + return httpGet(rawURL) } func toHex(hasher hash.Hash) string {