diff --git a/README.md b/README.md
index f079c793..b9eb78c3 100644
--- a/README.md
+++ b/README.md
@@ -1651,6 +1651,7 @@ The proxy serves a web UI under `/ui`. No separate frontend build is needed -- t
- **Dashboard** (`/ui/`) -- cache stats, popular packages, recently cached artifacts, and vulnerability overview.
- **Analytics** (`/ui/analytics`) -- accumulated download size as a ring broken down by ecosystem with the total in the middle, the cache size, artifact, package and version counts, a per-ecosystem table, the vulnerability overview, and a Runtime card mirroring every counter `/metrics` exposes. See [Analytics](#analytics).
- **Install guide** (`/ui/install`) -- per-ecosystem configuration instructions, so you don't have to look them up here.
+- **Upstreams** (`/ui/upstreams`) -- the upstreams each ecosystem's requests go to, as configured: the config key, the entry name for a map, and the proxy path clients use. Upstream URLs and whether credentials apply aren't shown, since `/ui` has no auth of its own.
- **Package browser** (`/ui/packages`) -- browse all cached packages with filtering by ecosystem and sorting by hits, size, name, or vulnerability count.
- **Search** (`/ui/search?q=...`) -- search cached packages by name.
- **Package detail** (`/ui/package/{ecosystem}/{name}`) -- metadata, license, vulnerabilities, and version list for a package. You can select two versions to compare.
diff --git a/internal/handler/arch.go b/internal/handler/arch.go
index f3d6cf93..1bcbd6ee 100644
--- a/internal/handler/arch.go
+++ b/internal/handler/arch.go
@@ -45,15 +45,23 @@ func NewArchHandler(proxy *Proxy, proxyURL string, repositories map[string]strin
h := &ArchHandler{
proxy: proxy,
proxyURL: strings.TrimSuffix(proxyURL, "/"),
- repositories: make(map[string]string, len(repositories)),
+ repositories: ArchRepositories(repositories),
}
- for name, repositoryURL := range repositories {
- h.repositories[name] = strings.TrimSuffix(repositoryURL, "/")
+ return h
+}
+
+// ArchRepositories returns the upstream.arch repositories by name without
+// trailing slashes, or the Arch Linux GeoIP mirror as "archlinux" when none
+// are configured.
+func ArchRepositories(configured map[string]string) map[string]string {
+ repositories := make(map[string]string, len(configured))
+ for name, repositoryURL := range configured {
+ repositories[name] = strings.TrimSuffix(repositoryURL, "/")
}
- if len(h.repositories) == 0 {
- h.repositories[defaultArchRepositoryName] = defaultArchUpstream
+ if len(repositories) == 0 {
+ repositories[defaultArchRepositoryName] = defaultArchUpstream
}
- return h
+ return repositories
}
// Routes returns the HTTP handler for pacman requests.
diff --git a/internal/handler/freebsd.go b/internal/handler/freebsd.go
index aceffa4f..9cf9ce7c 100644
--- a/internal/handler/freebsd.go
+++ b/internal/handler/freebsd.go
@@ -67,15 +67,23 @@ func NewFreeBSDHandler(proxy *Proxy, proxyURL string, repositories map[string]st
h := &FreeBSDHandler{
proxy: proxy,
proxyURL: strings.TrimSuffix(proxyURL, "/"),
- repositories: make(map[string]string, len(repositories)),
+ repositories: FreeBSDRepositories(repositories),
}
- for name, repositoryURL := range repositories {
- h.repositories[name] = strings.TrimSuffix(repositoryURL, "/")
+ return h
+}
+
+// FreeBSDRepositories returns the upstream.freebsd repositories by name
+// without trailing slashes, or the official FreeBSD mirror as "freebsd"
+// when none are configured.
+func FreeBSDRepositories(configured map[string]string) map[string]string {
+ repositories := make(map[string]string, len(configured))
+ for name, repositoryURL := range configured {
+ repositories[name] = strings.TrimSuffix(repositoryURL, "/")
}
- if len(h.repositories) == 0 {
- h.repositories[defaultFreeBSDRepositoryName] = defaultFreeBSDUpstream
+ if len(repositories) == 0 {
+ repositories[defaultFreeBSDRepositoryName] = defaultFreeBSDUpstream
}
- return h
+ return repositories
}
// Routes returns the HTTP handler for pkg requests.
diff --git a/internal/handler/go_sumdb.go b/internal/handler/go_sumdb.go
index 1c4c128e..bb2ba355 100644
--- a/internal/handler/go_sumdb.go
+++ b/internal/handler/go_sumdb.go
@@ -100,14 +100,20 @@ func (p *goSumDBProbes) recordFailure(name string, start time.Time) {
// clients have no route to the database of their own.
func (h *GoHandler) SetSumDBs(sumDBs map[string]string, offline bool) {
h.sumDBOffline = offline
- if len(sumDBs) == 0 {
- h.sumDBs = map[string]string{defaultGoSumDBName: defaultGoSumDBURL}
- return
+ h.sumDBs = GoSumDBs(sumDBs)
+}
+
+// GoSumDBs returns the upstream.go_sumdbs checksum databases by name without
+// trailing slashes, or sum.golang.org when none are configured.
+func GoSumDBs(configured map[string]string) map[string]string {
+ if len(configured) == 0 {
+ return map[string]string{defaultGoSumDBName: defaultGoSumDBURL}
}
- h.sumDBs = make(map[string]string, len(sumDBs))
- for name, sumDBURL := range sumDBs {
- h.sumDBs[name] = strings.TrimSuffix(sumDBURL, "/")
+ sumDBs := make(map[string]string, len(configured))
+ for name, sumDBURL := range configured {
+ sumDBs[name] = strings.TrimSuffix(sumDBURL, "/")
}
+ return sumDBs
}
// handleSumDB serves checksum database requests that the go command sends
diff --git a/internal/handler/terraform.go b/internal/handler/terraform.go
index fec13bd4..a3934dfb 100644
--- a/internal/handler/terraform.go
+++ b/internal/handler/terraform.go
@@ -67,14 +67,21 @@ type TerraformHandler struct {
// registry hostnames to base URLs. With none, DefaultTerraformRegistries are
// mirrored.
func NewTerraformHandler(proxy *Proxy, registries map[string]string) *TerraformHandler {
- if len(registries) == 0 {
- registries = DefaultTerraformRegistries
+ return &TerraformHandler{proxy: proxy, registries: TerraformRegistries(registries)}
+}
+
+// TerraformRegistries is upstream.terraform as the mirror uses it:
+// lowercased hostnames mapped to base URLs without a trailing slash, or
+// DefaultTerraformRegistries when none are configured.
+func TerraformRegistries(configured map[string]string) map[string]string {
+ if len(configured) == 0 {
+ configured = DefaultTerraformRegistries
}
- h := &TerraformHandler{proxy: proxy, registries: make(map[string]string, len(registries))}
- for hostname, baseURL := range registries {
- h.registries[strings.ToLower(hostname)] = strings.TrimSuffix(baseURL, "/")
+ registries := make(map[string]string, len(configured))
+ for hostname, baseURL := range configured {
+ registries[strings.ToLower(hostname)] = strings.TrimSuffix(baseURL, "/")
}
- return h
+ return registries
}
// WithGit has module downloads from a Git repository that git serves
diff --git a/internal/handler/terraform_test.go b/internal/handler/terraform_test.go
index 49dcb6a4..961daf9a 100644
--- a/internal/handler/terraform_test.go
+++ b/internal/handler/terraform_test.go
@@ -406,3 +406,13 @@ func TestTerraformProviderResolvesDiscoveryOncePerRequest(t *testing.T) {
t.Errorf("discovery requests after an archive = %d, want 2", got)
}
}
+
+func TestTerraformRegistries(t *testing.T) {
+ got := TerraformRegistries(map[string]string{"Registry.Example.ORG": "https://registry.example.org/"})
+ if len(got) != 1 || got["registry.example.org"] != "https://registry.example.org" {
+ t.Errorf("TerraformRegistries = %v", got)
+ }
+ if got := TerraformRegistries(nil); len(got) != len(DefaultTerraformRegistries) || got["registry.opentofu.org"] != "https://registry.opentofu.org" {
+ t.Errorf("TerraformRegistries(nil) = %v", got)
+ }
+}
diff --git a/internal/server/server.go b/internal/server/server.go
index 297adeae..2061b080 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -380,6 +380,7 @@ func (s *Server) serve(listener net.Listener) error {
ui.Get("/", s.handleRoot)
ui.Get("/analytics", s.handleAnalytics)
ui.Get("/install", s.handleInstall)
+ ui.Get("/upstreams", s.handleUpstreams)
ui.Get("/search", s.handleSearch)
ui.Get("/packages", s.handlePackagesList)
ui.Get("/package/{ecosystem}/*", s.handlePackagePath)
diff --git a/internal/server/templates/layout/header.html b/internal/server/templates/layout/header.html
index a5740103..bb3d790d 100644
--- a/internal/server/templates/layout/header.html
+++ b/internal/server/templates/layout/header.html
@@ -58,6 +58,7 @@
{{if .Projects}}Projects{{end}}
Analytics
Install
+Upstreams
Health
API
{{end}}
diff --git a/internal/server/templates/pages/upstreams.html b/internal/server/templates/pages/upstreams.html
new file mode 100644
index 00000000..6edefea1
--- /dev/null
+++ b/internal/server/templates/pages/upstreams.html
@@ -0,0 +1,24 @@
+{{define "title"}}Upstreams - git-pkgs proxy{{end}}
+
+{{define "content"}}
+
+
Upstreams
+
The upstreams each ecosystem's requests go to, as configured, with the path clients reach them at. Upstream URLs aren't shown.
+
+
+
+ {{range .Ecosystems}}
+
+
{{.Name}}
+
+ {{range .Upstreams}}
+
+
{{.Path}}
+
{{.Setting}}{{if .Name}} · {{.Name}}{{end}}
+
+ {{end}}
+
+
+ {{end}}
+
+{{end}}
diff --git a/internal/server/upstreams_ui.go b/internal/server/upstreams_ui.go
new file mode 100644
index 00000000..ee15093f
--- /dev/null
+++ b/internal/server/upstreams_ui.go
@@ -0,0 +1,161 @@
+package server
+
+import (
+ "net/http"
+ "net/url"
+ "sort"
+ "strings"
+
+ "github.com/git-pkgs/proxy/internal/config"
+ "github.com/git-pkgs/proxy/internal/handler"
+)
+
+// UpstreamsData is the upstreams page: the upstreams each ecosystem's
+// requests go to, as configured.
+type UpstreamsData struct {
+ Layout
+ Ecosystems []UpstreamEcosystem
+}
+
+// UpstreamEcosystem is one ecosystem on the upstreams page.
+type UpstreamEcosystem struct {
+ Name string
+ Upstreams []UpstreamRow
+}
+
+// UpstreamRow is one configured upstream. It names where clients reach it
+// and the config key that sets it, but not its URL or whether credentials
+// apply: /ui has no auth of its own, so that would list internal hosts for
+// anyone who can reach the proxy.
+type UpstreamRow struct {
+ // Setting is the config key that sets the upstream, such as
+ // upstream.apk, and Name the entry's key in it for a map.
+ Setting, Name string
+ // Path is where clients reach the upstream through the proxy.
+ Path string
+}
+
+func (s *Server) handleUpstreams(w http.ResponseWriter, r *http.Request) {
+ data := UpstreamsData{
+ Layout: s.layoutFor(r),
+ Ecosystems: upstreamEcosystems(s.cfg.BaseURL, &s.cfg.Upstream),
+ }
+ if err := s.templates.Render(w, "upstreams", data); err != nil {
+ s.logger.Error("failed to render upstreams", "error", err)
+ }
+}
+
+// upstreamSetting is one config key on the upstreams page: a single URL
+// or, with named set, a map of them. path is where it is served, with
+// {name} for a map entry's key.
+type upstreamSetting struct {
+ key string
+ path string
+ url string
+ named map[string]string
+}
+
+// upstreamEcosystems lists the upstreams u configures, per ecosystem.
+// A map the proxy fills with a default when it is empty, such as
+// upstream.apk, lists that default.
+func upstreamEcosystems(baseURL string, u *config.UpstreamConfig) []UpstreamEcosystem {
+ git := make(map[string]string, len(u.Git))
+ for name, upstream := range u.Git {
+ git[name] = upstream.URL
+ }
+ single := func(key, path, url string) upstreamSetting { return upstreamSetting{key: key, path: path, url: url} }
+ named := func(key, path string, m map[string]string) upstreamSetting {
+ return upstreamSetting{key: key, path: path, named: m}
+ }
+ groups := []struct {
+ name string
+ settings []upstreamSetting
+ }{
+ {"npm", []upstreamSetting{single("upstream.npm", "/npm/", u.NPM), named("upstream.npm_routes", "/npm/", u.NPMRoutes)}},
+ {"Cargo", []upstreamSetting{single("upstream.cargo", "/cargo/", u.Cargo), single("upstream.cargo_download", "/cargo/", u.CargoDownload)}},
+ {"RubyGems", []upstreamSetting{single("upstream.gem", "/gem/", u.Gem)}},
+ {"Go", []upstreamSetting{
+ single("upstream.go", "/go/", u.Go),
+ named("upstream.go_sumdbs", "/go/sumdb/{name}/", handler.GoSumDBs(u.GoSumDBs)),
+ single("upstream.go_vulndb", "/go/vulndb/", u.GoVulnDB),
+ }},
+ {"Hex", []upstreamSetting{single("upstream.hex", "/hex/", u.Hex), single("upstream.hex_api", "/hex/", u.HexAPI)}},
+ {"pub.dev", []upstreamSetting{single("upstream.pub", "/pub/", u.Pub)}},
+ {"PyPI", []upstreamSetting{single("upstream.pypi", "/pypi/", u.PyPI), single("upstream.pypi_download", "/pypi/", u.PyPIDownload)}},
+ {"Maven", []upstreamSetting{
+ single("upstream.maven", "/maven/", u.Maven),
+ named("upstream.maven_repositories", "/maven/{name}/", u.MavenRepositories),
+ single("upstream.gradle_plugin_portal", "/maven/", u.GradlePluginPortal),
+ }},
+ {"NuGet", []upstreamSetting{single("upstream.nuget", "/nuget/", u.NuGet), single("upstream.nuget_search", "/nuget/", u.NuGetSearch)}},
+ {"Composer", []upstreamSetting{
+ single("upstream.composer", "/composer/", u.Composer),
+ single("upstream.composer_repository", "/composer/", u.ComposerRepository),
+ named("upstream.composer_routes", "/composer/", u.ComposerRoutes),
+ }},
+ {"Conan", []upstreamSetting{single("upstream.conan", "/conan/", u.Conan)}},
+ {"Chef", []upstreamSetting{single("upstream.chef", "/chef/", u.Chef)}},
+ {"Conda", []upstreamSetting{single("upstream.conda", "/conda/", u.Conda)}},
+ {"CRAN", []upstreamSetting{single("upstream.cran", "/cran/", u.CRAN)}},
+ {"Julia", []upstreamSetting{single("upstream.julia", "/julia/", u.Julia)}},
+ {"Swift", []upstreamSetting{single("upstream.swift", "/swift/", u.Swift)}},
+ {"Homebrew", []upstreamSetting{
+ single("upstream.homebrew_api", "/homebrew/", u.HomebrewAPI),
+ // Bottles are served by the container handler (see
+ // handler.RegisterHomebrewArtifacts).
+ single("upstream.homebrew_artifact", "/v2/homebrew/core/", u.HomebrewArtifact),
+ }},
+ {"OCI", []upstreamSetting{single("upstream.oci_default", "/v2/", u.OCIDefault), named("upstream.oci", "/v2/upstream/{name}/", u.OCI)}},
+ {"Helm", []upstreamSetting{named("upstream.helm", "/helm/{name}/", u.Helm)}},
+ {"Alpine", []upstreamSetting{named("upstream.apk", "/apk/{name}/", handler.APKRepositories(u.APK))}},
+ {"Arch Linux", []upstreamSetting{named("upstream.arch", "/arch/{name}/", handler.ArchRepositories(u.Arch))}},
+ {"FreeBSD", []upstreamSetting{named("upstream.freebsd", "/freebsd/{name}/", handler.FreeBSDRepositories(u.FreeBSD))}},
+ {"Terraform", []upstreamSetting{named("upstream.terraform", "/terraform/providers/{name}/", handler.TerraformRegistries(u.Terraform))}},
+ {"Debian", []upstreamSetting{single("upstream.debian", "/debian/", u.Debian), named("upstream.debian_repositories", "/debian/{name}/", u.DebianRepositories)}},
+ {"RPM", []upstreamSetting{single("upstream.rpm", "/rpm/", u.RPM)}},
+ {"Generic", []upstreamSetting{named("upstream.generic", "/generic/{name}/", u.Generic)}},
+ {"Git", []upstreamSetting{named("upstream.git", "/git/{name}/", git)}},
+ }
+ base := strings.TrimSuffix(baseURL, "/")
+ ecosystems := make([]UpstreamEcosystem, 0, len(groups))
+ for _, group := range groups {
+ var rows []UpstreamRow
+ for _, setting := range group.settings {
+ rows = append(rows, setting.rows(base)...)
+ }
+ if len(rows) > 0 {
+ ecosystems = append(ecosystems, UpstreamEcosystem{Name: group.name, Upstreams: rows})
+ }
+ }
+ return ecosystems
+}
+
+// rows lists the setting's upstreams, map entries sorted by name, leaving
+// out empty ones.
+func (s upstreamSetting) rows(base string) []UpstreamRow {
+ row := func(name string) UpstreamRow {
+ return UpstreamRow{
+ Setting: s.key,
+ Name: name,
+ // Names are configured unescaped; clients send reserved
+ // characters in them path-escaped.
+ Path: base + strings.ReplaceAll(s.path, "{name}", url.PathEscape(name)),
+ }
+ }
+ if s.named == nil {
+ if s.url == "" {
+ return nil
+ }
+ return []UpstreamRow{row("")}
+ }
+ names := make([]string, 0, len(s.named))
+ for name := range s.named {
+ names = append(names, name)
+ }
+ sort.Strings(names)
+ rows := make([]UpstreamRow, 0, len(names))
+ for _, name := range names {
+ rows = append(rows, row(name))
+ }
+ return rows
+}
diff --git a/internal/server/upstreams_ui_test.go b/internal/server/upstreams_ui_test.go
new file mode 100644
index 00000000..206adfea
--- /dev/null
+++ b/internal/server/upstreams_ui_test.go
@@ -0,0 +1,170 @@
+package server
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+
+ "github.com/git-pkgs/proxy/internal/config"
+)
+
+func TestUpstreamsPage(t *testing.T) {
+ cfg, _ := startGitTestServer(t, func(cfg *config.Config) {
+ cfg.Upstream.Debian = "https://deb.example.org/debian"
+ cfg.Upstream.DebianRepositories = map[string]string{"security": "https://security.example.org/debian-security"}
+ cfg.Upstream.Helm = map[string]string{"charts": "https://charts.example.org"}
+ cfg.Upstream.Auth = map[string]config.AuthConfig{
+ "https://charts.example.org": {Type: "bearer", Token: "chart-token-secret"},
+ }
+ })
+
+ if _, body := request(t, http.MethodGet, cfg.BaseURL+"/ui/", "", nil); !strings.Contains(body, `href="/ui/upstreams"`) {
+ t.Error("navigation doesn't link the upstreams page")
+ }
+ status, body := request(t, http.MethodGet, cfg.BaseURL+"/ui/upstreams", "", nil)
+ if status != http.StatusOK {
+ t.Fatalf("status %d\n%s", status, body)
+ }
+ for _, want := range []string{
+ cfg.BaseURL + "/debian/",
+ cfg.BaseURL + "/debian/security/",
+ cfg.BaseURL + "/helm/charts/",
+ cfg.BaseURL + "/npm/",
+ // upstream.apk is empty, so the page lists the repository the proxy
+ // serves by default.
+ cfg.BaseURL + "/apk/alpine/",
+ // So is the checksum database with upstream.go_sumdbs empty.
+ cfg.BaseURL + "/go/sumdb/sum.golang.org/",
+ cfg.BaseURL + "/v2/homebrew/core/",
+ "upstream.debian_repositories · security",
+ } {
+ if !strings.Contains(body, want) {
+ t.Errorf("page doesn't show %q", want)
+ }
+ }
+ for _, hidden := range []string{"deb.example.org", "security.example.org", "charts.example.org", "registry.npmjs.org", "dl-cdn.alpinelinux.org", "chart-token-secret", "bearer"} {
+ if strings.Contains(body, hidden) {
+ t.Errorf("page shows %q", hidden)
+ }
+ }
+}
+
+func TestUpstreamEcosystems(t *testing.T) {
+ u := config.Default().Upstream
+ u.NPM = "https://user:url-password@npm.example.org/registry?token=query-secret#frag"
+ u.Generic = map[string]string{
+ "files": "https://files.example.org/base",
+ "my files": "https://morefiles.example.org/base",
+ }
+ u.Git = map[string]config.GitUpstream{"github": {URL: "https://github.com"}}
+ u.GoSumDBs = map[string]string{"sum.example.org": "https://sum.example.org"}
+ u.Auth = map[string]config.AuthConfig{
+ "https://npm.example.org": {Type: "basic", Username: "basic-user", Password: "basic-secret"},
+ "https://files.example.org": {Type: "header", HeaderName: "X-Api-Key", HeaderValue: "header-secret"},
+ }
+ ecosystems := upstreamEcosystems("http://proxy.test/", &u)
+
+ rows := map[string]UpstreamRow{}
+ for _, e := range ecosystems {
+ for _, row := range e.Upstreams {
+ rows[row.Setting+" "+row.Name] = row
+ }
+ }
+ for key, want := range map[string]UpstreamRow{
+ "upstream.npm ": {Setting: "upstream.npm", Path: "http://proxy.test/npm/"},
+ "upstream.generic files": {Setting: "upstream.generic", Name: "files", Path: "http://proxy.test/generic/files/"},
+ "upstream.generic my files": {Setting: "upstream.generic", Name: "my files", Path: "http://proxy.test/generic/my%20files/"},
+ "upstream.git github": {Setting: "upstream.git", Name: "github", Path: "http://proxy.test/git/github/"},
+ "upstream.go_sumdbs sum.example.org": {Setting: "upstream.go_sumdbs", Name: "sum.example.org", Path: "http://proxy.test/go/sumdb/sum.example.org/"},
+ "upstream.arch archlinux": {Setting: "upstream.arch", Name: "archlinux", Path: "http://proxy.test/arch/archlinux/"},
+ "upstream.freebsd freebsd": {Setting: "upstream.freebsd", Name: "freebsd", Path: "http://proxy.test/freebsd/freebsd/"},
+ "upstream.cargo_download ": {Setting: "upstream.cargo_download", Path: "http://proxy.test/cargo/"},
+ "upstream.homebrew_api ": {Setting: "upstream.homebrew_api", Path: "http://proxy.test/homebrew/"},
+ "upstream.homebrew_artifact ": {Setting: "upstream.homebrew_artifact", Path: "http://proxy.test/v2/homebrew/core/"},
+ "upstream.terraform registry.opentofu.org": {Setting: "upstream.terraform", Name: "registry.opentofu.org", Path: "http://proxy.test/terraform/providers/registry.opentofu.org/"},
+ } {
+ if got := rows[key]; got != want {
+ t.Errorf("%s = %+v, want %+v", key, got, want)
+ }
+ }
+ if _, ok := rows["upstream.go_sumdbs sum.golang.org"]; ok {
+ t.Error("lists the default checksum database although upstream.go_sumdbs replaces it")
+ }
+ if _, ok := rows["upstream.helm "]; ok {
+ t.Error("lists an empty upstream.helm")
+ }
+ for _, e := range ecosystems {
+ if len(e.Upstreams) == 0 {
+ t.Errorf("lists %s without upstreams", e.Name)
+ }
+ }
+}
+
+func TestUpstreamEcosystemsSortNames(t *testing.T) {
+ u := config.Default().Upstream
+ u.Generic = map[string]string{}
+ for _, name := range []string{"d", "b", "e", "a", "c", "f", "h", "g"} {
+ u.Generic[name] = "https://" + name + ".example.org"
+ }
+ for _, e := range upstreamEcosystems("http://proxy.test", &u) {
+ if e.Name != "Generic" {
+ continue
+ }
+ var names []string
+ for _, row := range e.Upstreams {
+ names = append(names, row.Name)
+ }
+ if got := strings.Join(names, ""); got != "abcdefgh" {
+ t.Errorf("generic upstreams in order %q", got)
+ }
+ }
+}
+
+func TestUpstreamsPageShowsNoSecrets(t *testing.T) {
+ u := config.Default().Upstream
+ u.NPM = "https://user:url-password@npm.example.org?token=query-secret#frag-secret"
+ u.Generic = map[string]string{"files": "https://files.example.org"}
+ u.Auth = map[string]config.AuthConfig{
+ "https://npm.example.org": {Type: "basic", Username: "basic-user", Password: "basic-secret"},
+ "https://files.example.org": {Type: "header", HeaderName: "X-Api-Key", HeaderValue: "header-secret"},
+ "https://pypi.org": {Type: "bearer", Token: "bearer-secret"},
+ }
+ w := httptest.NewRecorder()
+ if err := (&Templates{}).Render(w, "upstreams", UpstreamsData{Ecosystems: upstreamEcosystems("http://proxy.test", &u)}); err != nil {
+ t.Fatal(err)
+ }
+ body := w.Body.String()
+ for _, secret := range []string{"url-password", "user@", "query-secret", "frag-secret", "basic-user", "basic-secret", "X-Api-Key", "header-secret", "bearer-secret"} {
+ if strings.Contains(body, secret) {
+ t.Errorf("page shows %q", secret)
+ }
+ }
+ // The hosts and which of them have credentials attached aren't shown
+ // either: /ui has no auth of its own. ("header" is in the layout.)
+ for _, hidden := range []string{"npm.example.org", "files.example.org", "pypi.org", "basic", "bearer", "auth:"} {
+ if strings.Contains(body, hidden) {
+ t.Errorf("page shows %q", hidden)
+ }
+ }
+ if !strings.Contains(body, "http://proxy.test/generic/files/") {
+ t.Error("page doesn't show the generic upstream's path")
+ }
+}
+
+func TestUpstreamEcosystemsTerraformHostnames(t *testing.T) {
+ u := config.Default().Upstream
+ u.Terraform = map[string]string{"Registry.Example.ORG": "https://registry.example.org/"}
+ var paths []string
+ for _, e := range upstreamEcosystems("http://proxy.test", &u) {
+ if e.Name != "Terraform" {
+ continue
+ }
+ for _, row := range e.Upstreams {
+ paths = append(paths, row.Path)
+ }
+ }
+ if want := "http://proxy.test/terraform/providers/registry.example.org/"; len(paths) != 1 || paths[0] != want {
+ t.Errorf("terraform paths %q, want [%q]", paths, want)
+ }
+}