Files

1188 lines
39 KiB
Go

package pom
import (
"bytes"
"cmp"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/aquasecurity/trivy/pkg/log"
"github.com/aquasecurity/trivy/pkg/set"
)
func Test_mirror_matches(t *testing.T) {
tests := []struct {
name string
patterns []string
repoID string
repoURL url.URL
want bool
}{
{
name: "wildcard matches everything",
patterns: []string{"*"},
repoID: "anything",
repoURL: mustParseURL(t, "https://example.com/repo"),
want: true,
},
{
name: "exact id match",
patterns: []string{"central"},
repoID: "central",
repoURL: mustParseURL(t, "https://repo.maven.apache.org/maven2"),
want: true,
},
{
// Maven's order-sensitive semantics: the exact-id match is terminal,
// so a later exclusion of the same id never gets evaluated.
name: "exact id wins over a later exclusion of the same id",
patterns: []string{"central", "!central"},
repoID: "central",
repoURL: mustParseURL(t, "https://repo.maven.apache.org/maven2"),
want: true,
},
{
name: "exclusion after wildcard",
patterns: []string{"*", "!internal"},
repoID: "internal",
repoURL: mustParseURL(t, "https://example.com/repo"),
want: false,
},
{
name: "exclusion before wildcard still wins",
patterns: []string{"!internal", "*"},
repoID: "internal",
repoURL: mustParseURL(t, "https://example.com/repo"),
want: false,
},
{
name: "wildcard with unrelated exclusion",
patterns: []string{"*", "!internal"},
repoID: "central",
repoURL: mustParseURL(t, "https://repo.maven.apache.org/maven2"),
want: true,
},
{
name: "external matches https",
patterns: []string{"external:*"},
repoID: "central",
repoURL: mustParseURL(t, "https://repo.maven.apache.org/maven2"),
want: true,
},
{
name: "external skips file scheme",
patterns: []string{"external:*"},
repoID: "my-local",
repoURL: mustParseURL(t, "file:///tmp/repo"),
want: false,
},
{
name: "external skips localhost",
patterns: []string{"external:*"},
repoID: "my-local",
repoURL: mustParseURL(t, "http://localhost:8081/repo"),
want: false,
},
{
name: "external skips 127.0.0.1",
patterns: []string{"external:*"},
repoID: "my-local",
repoURL: mustParseURL(t, "http://127.0.0.1/repo"),
want: false,
},
{
name: "external with exclusion — excluded id is not mirrored",
patterns: []string{"external:*", "!internal"},
repoID: "internal",
repoURL: mustParseURL(t, "https://internal.example.com/repo"),
want: false,
},
{
name: "external:http matches http",
patterns: []string{"external:http:*"},
repoID: "legacy",
repoURL: mustParseURL(t, "http://example.com/repo"),
want: true,
},
{
name: "external:http does not match https",
patterns: []string{"external:http:*"},
repoID: "central",
repoURL: mustParseURL(t, "https://repo.maven.apache.org/maven2"),
want: false,
},
{
name: "external:http does not match localhost",
patterns: []string{"external:http:*"},
repoID: "my-local",
repoURL: mustParseURL(t, "http://localhost/repo"),
want: false,
},
{
name: "empty patterns do not match",
patterns: nil,
repoID: "central",
repoURL: mustParseURL(t, "https://repo.maven.apache.org/maven2"),
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := mirror{patterns: tt.patterns}
require.Equal(t, tt.want, m.matches(tt.repoID, &tt.repoURL))
})
}
}
// Test_resolveMirrors covers resolveMirrors for both sources: settings.xml <mirror>
// entries (pattern splitting, <server> credentials, dropping bad entries) and
// config-file mirrors (key normalization, target order, dropping bad entries).
func Test_resolveMirrors(t *testing.T) {
tests := []struct {
name string
mirrors []Mirror
servers []Server
configMirrors map[string][]string
want mirrors
}{
{
name: "settings.xml: split, trim and drop empty patterns",
mirrors: []Mirror{
{
ID: "m1",
URL: "https://mirror.example.com/maven2",
MirrorOf: " central , , !internal ,",
},
},
want: mirrors{
settings: []mirror{
{
id: "m1",
patterns: []string{"central", "!internal"},
url: mustParseURL(t, "https://mirror.example.com/maven2"),
},
},
},
},
{
name: "settings.xml: credentials embedded from <server> matching mirror id",
mirrors: []Mirror{
{
ID: "m1",
URL: "https://mirror.example.com/maven2",
MirrorOf: "*",
},
},
servers: []Server{
{
ID: "m1",
Username: "user",
Password: "pass",
},
},
want: mirrors{
settings: []mirror{
{
id: "m1",
patterns: []string{"*"},
url: mustParseURL(t, "https://user:pass@mirror.example.com/maven2"),
},
},
},
},
{
name: "settings.xml: <server> credentials override userinfo embedded in mirror URL",
mirrors: []Mirror{
{
ID: "m1",
URL: "https://url-user:url-pass@mirror.example.com/maven2",
MirrorOf: "*",
},
},
servers: []Server{
{
ID: "m1",
Username: "server-user",
Password: "server-pass",
},
},
want: mirrors{
settings: []mirror{
{
id: "m1",
patterns: []string{"*"},
url: mustParseURL(t, "https://server-user:server-pass@mirror.example.com/maven2"),
},
},
},
},
{
name: "settings.xml: server with non-matching id is ignored",
mirrors: []Mirror{
{
ID: "m1",
URL: "https://mirror.example.com/maven2",
MirrorOf: "*",
},
},
servers: []Server{
{
ID: "other",
Username: "user",
Password: "pass",
},
},
want: mirrors{
settings: []mirror{
{
id: "m1",
patterns: []string{"*"},
url: mustParseURL(t, "https://mirror.example.com/maven2"),
},
},
},
},
{
name: "settings.xml: mirror with empty mirrorOf is dropped",
mirrors: []Mirror{
{
ID: "m1",
URL: "https://mirror.example.com/maven2",
MirrorOf: "",
},
{
ID: "m2",
URL: "https://other.example.com/maven2",
MirrorOf: "central",
},
},
want: mirrors{
settings: []mirror{
{
id: "m2",
patterns: []string{"central"},
url: mustParseURL(t, "https://other.example.com/maven2"),
},
},
},
},
{
name: "settings.xml: mirror with unparsable URL is dropped",
mirrors: []Mirror{
{
ID: "broken",
URL: "http://[::1",
MirrorOf: "*",
},
{
ID: "ok",
URL: "https://mirror.example.com/maven2",
MirrorOf: "*",
},
},
want: mirrors{
settings: []mirror{
{
id: "ok",
patterns: []string{"*"},
url: mustParseURL(t, "https://mirror.example.com/maven2"),
},
},
},
},
{
name: "no input yields empty mirrors",
want: mirrors{},
},
{
name: "config file: key normalized (trailing slash trimmed), targets kept in order",
configMirrors: map[string][]string{
"https://repo.example.com/maven2/": {
"https://m1.example.com/maven2",
"https://m2.example.com/maven2",
},
},
want: mirrors{
configFile: map[string][]url.URL{
"https://repo.example.com/maven2": {
mustParseURL(t, "https://m1.example.com/maven2"),
mustParseURL(t, "https://m2.example.com/maven2"),
},
},
},
},
{
name: "config file: entry with only an unparsable target is dropped",
configMirrors: map[string][]string{
"https://repo.example.com/maven2": {"http://[::1"},
},
want: mirrors{},
},
{
name: "config file: unparsable target is dropped, valid ones kept",
configMirrors: map[string][]string{
"https://repo.example.com/maven2": {"http://[::1", "https://ok.example.com/maven2"},
},
want: mirrors{
configFile: map[string][]url.URL{
"https://repo.example.com/maven2": {mustParseURL(t, "https://ok.example.com/maven2")},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.want, resolveMirrors(tt.mirrors, tt.servers, tt.configMirrors))
})
}
}
// TestParser_mirrorFor covers both passes of mirrorFor: matching settings.xml
// <mirror> entries, and applying the config-file mirrors on top of the result —
// including cross-source chaining and fallback lists.
func TestParser_mirrorFor(t *testing.T) {
tests := []struct {
name string
settingsMirrors []Mirror
servers []Server
configMirrors map[string][]string
repo repository
want []repository
}{
{
name: "settings.xml: no match — repository returned unchanged",
settingsMirrors: []Mirror{
{ID: "m1", MirrorOf: "internal", URL: "https://mirror.example.com/maven2"},
},
repo: repository{
id: "central",
url: mustParseURL(t, "https://repo.maven.apache.org/maven2"),
releaseEnabled: true,
},
want: []repository{
{
id: "central",
url: mustParseURL(t, "https://repo.maven.apache.org/maven2"),
releaseEnabled: true,
},
},
},
{
name: "settings.xml: match by exact id — release/snapshot flags preserved",
settingsMirrors: []Mirror{
{ID: "m1", MirrorOf: "central", URL: "https://mirror.example.com/maven2"},
},
repo: repository{
id: "central",
url: mustParseURL(t, "https://repo.maven.apache.org/maven2"),
releaseEnabled: true,
snapshotEnabled: false,
},
want: []repository{
{
id: "m1",
url: mustParseURL(t, "https://mirror.example.com/maven2"),
releaseEnabled: true,
snapshotEnabled: false,
},
},
},
{
name: "settings.xml: mirror credentials (not the original repo's) are kept",
settingsMirrors: []Mirror{
{ID: "m1", MirrorOf: "*", URL: "https://mirror-user:mirror-pass@mirror.example.com/maven2"},
},
repo: repository{
id: "central",
url: mustParseURL(t, "https://central-user:central-pass@repo.maven.apache.org/maven2"),
releaseEnabled: true,
},
want: []repository{
{
id: "m1",
url: mustParseURL(t, "https://mirror-user:mirror-pass@mirror.example.com/maven2"),
releaseEnabled: true,
},
},
},
{
name: "settings.xml: first matching mirror wins",
settingsMirrors: []Mirror{
{ID: "first", MirrorOf: "*", URL: "https://first.example.com/maven2"},
{ID: "second", MirrorOf: "*", URL: "https://second.example.com/maven2"},
},
repo: repository{
id: "central",
url: mustParseURL(t, "https://repo.maven.apache.org/maven2"),
releaseEnabled: true,
},
want: []repository{
{
id: "first",
url: mustParseURL(t, "https://first.example.com/maven2"),
releaseEnabled: true,
},
},
},
{
name: "settings.xml: exclusion blocks match, falls through to next mirror",
settingsMirrors: []Mirror{
{ID: "first", MirrorOf: "*,!central", URL: "https://first.example.com/maven2"},
{ID: "second", MirrorOf: "central", URL: "https://second.example.com/maven2"},
},
repo: repository{
id: "central",
url: mustParseURL(t, "https://repo.maven.apache.org/maven2"),
releaseEnabled: true,
},
want: []repository{
{
id: "second",
url: mustParseURL(t, "https://second.example.com/maven2"),
releaseEnabled: true,
},
},
},
{
name: "config file: single mirror — repo1 -> repo3",
configMirrors: map[string][]string{
"https://repo1.example.com/maven2": {"https://repo3.example.com/maven2"},
},
repo: repository{
id: "central",
url: mustParseURL(t, "https://repo1.example.com/maven2"),
releaseEnabled: true,
},
want: []repository{
{
id: "central",
url: mustParseURL(t, "https://repo3.example.com/maven2"),
releaseEnabled: true,
},
},
},
{
name: "config file: trailing slash in key still matches",
configMirrors: map[string][]string{
"https://repo1.example.com/maven2/": {"https://repo3.example.com/maven2"},
},
repo: repository{
id: "central",
url: mustParseURL(t, "https://repo1.example.com/maven2"),
releaseEnabled: true,
},
want: []repository{
{
id: "central",
url: mustParseURL(t, "https://repo3.example.com/maven2"),
releaseEnabled: true,
},
},
},
{
// Trivy embeds <server> credentials into the repository URL, so the lookup
// must ignore them — the configured key never carries a password.
name: "config file: repository credentials are ignored by the lookup",
configMirrors: map[string][]string{
"https://repo1.example.com/maven2": {"https://repo3.example.com/maven2"},
},
repo: repository{
id: "corp",
url: mustParseURL(t, "https://repo-user:repo-pass@repo1.example.com/maven2"),
releaseEnabled: true,
},
want: []repository{
{
id: "corp",
url: mustParseURL(t, "https://repo3.example.com/maven2"),
releaseEnabled: true,
},
},
},
{
// The path is cleaned, so repeated and relative segments don't break the match.
name: "config file: path segments are cleaned before the lookup",
configMirrors: map[string][]string{
"https://repo1.example.com//nexus/./content/../maven2/": {"https://repo3.example.com/maven2"},
},
repo: repository{
id: "central",
url: mustParseURL(t, "https://repo1.example.com/nexus/maven2"),
releaseEnabled: true,
},
want: []repository{
{
id: "central",
url: mustParseURL(t, "https://repo3.example.com/maven2"),
releaseEnabled: true,
},
},
},
{
// RFC 3986 defines the host as case-insensitive, unlike the path.
name: "config file: host case is ignored by the lookup",
configMirrors: map[string][]string{
"https://Repo1.Example.COM/maven2": {"https://repo3.example.com/maven2"},
},
repo: repository{
id: "central",
url: mustParseURL(t, "https://repo1.example.com/maven2"),
releaseEnabled: true,
},
want: []repository{
{
id: "central",
url: mustParseURL(t, "https://repo3.example.com/maven2"),
releaseEnabled: true,
},
},
},
{
// Several mirrors for one repository become ordered fallback candidates.
name: "config file: fallback list — repo1 -> [repo3, repo4] in order",
configMirrors: map[string][]string{
"https://repo1.example.com/maven2": {
"https://repo3.example.com/maven2",
"https://repo4.example.com/maven2",
},
},
repo: repository{
id: "central",
url: mustParseURL(t, "https://repo1.example.com/maven2"),
releaseEnabled: true,
},
want: []repository{
{
id: "central",
url: mustParseURL(t, "https://repo3.example.com/maven2"),
releaseEnabled: true,
},
{
id: "central",
url: mustParseURL(t, "https://repo4.example.com/maven2"),
releaseEnabled: true,
},
},
},
{
// Example 1 from the spec: both sources target repo1; settings.xml wins
// because it rewrites the URL first and the config pass no longer matches.
name: "cross-source: conflict on the same key — settings.xml wins",
settingsMirrors: []Mirror{
{ID: "settings-mirror", MirrorOf: "central", URL: "https://repo2.example.com/maven2"},
},
configMirrors: map[string][]string{
"https://repo1.example.com/maven2": {"https://repo3.example.com/maven2"},
},
repo: repository{
id: "central",
url: mustParseURL(t, "https://repo1.example.com/maven2"),
releaseEnabled: true,
},
want: []repository{
{
id: "settings-mirror",
url: mustParseURL(t, "https://repo2.example.com/maven2"),
releaseEnabled: true,
},
},
},
{
// Example 2 from the spec: cross-source chaining.
// settings.xml repo1 -> repo2, trivy.yaml repo2 -> repo3 => repo1 -> repo3.
name: "cross-source: chaining — repo1 --settings--> repo2 --trivy.yaml--> repo3",
settingsMirrors: []Mirror{
{ID: "settings-mirror", MirrorOf: "central", URL: "https://repo2.example.com/maven2"},
},
configMirrors: map[string][]string{
"https://repo2.example.com/maven2": {"https://repo3.example.com/maven2"},
},
repo: repository{
id: "central",
url: mustParseURL(t, "https://repo1.example.com/maven2"),
releaseEnabled: true,
},
want: []repository{
{
id: "settings-mirror",
url: mustParseURL(t, "https://repo3.example.com/maven2"),
releaseEnabled: true,
},
},
},
{
// Chaining through a mirror that has <server> credentials: pass 1 rewrites the
// repository to the mirror URL with the credentials embedded, so the config-file
// lookup in pass 2 sees them and must still match the plain configured key.
name: "cross-source: chaining through a mirror with credentials",
settingsMirrors: []Mirror{
{ID: "settings-mirror", MirrorOf: "central", URL: "https://repo2.example.com/maven2"},
},
servers: []Server{
{ID: "settings-mirror", Username: "mirror-user", Password: "mirror-pass"},
},
configMirrors: map[string][]string{
"https://repo2.example.com/maven2": {"https://repo3.example.com/maven2"},
},
repo: repository{
id: "central",
url: mustParseURL(t, "https://repo1.example.com/maven2"),
releaseEnabled: true,
},
want: []repository{
{
id: "settings-mirror",
url: mustParseURL(t, "https://repo3.example.com/maven2"),
releaseEnabled: true,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &Parser{mirrors: resolveMirrors(tt.settingsMirrors, tt.servers, tt.configMirrors)}
require.Equal(t, tt.want, p.mirrorFor(tt.repo))
})
}
}
// Test_fetchPOMFromRemoteRepositories_rejectedCredentials verifies that a repository that rejects
// the configured credentials (401/403) is warned about once, and that the warning leaks no secrets.
func Test_fetchPOMFromRemoteRepositories_rejectedCredentials(t *testing.T) {
const (
user = "repo-user"
pass = "repo-pass"
token = "secret-token"
)
const pom = `<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>example-api</artifactId>
<version>1.0.0</version>
</project>`
type repoSpec struct {
status int // answer for the POM request
metadataStatus int // answer for the maven-metadata.xml request; status if zero
withCreds bool // credentials come from a settings.xml <server>
viaMirror bool // reached through a settings.xml mirror instead of a pom.xml <repository>
query string // appended to the mirror URL
}
tests := []struct {
name string
repos []repoSpec
snapshot bool
parsers int // parsers (pom.xml files) fetching the POM
wantHits []int // request count per repository
wantStatus []int // status codes of the expected warnings
wantErr string // non-empty: fetch must fail with an error containing this
}{
{
name: "pom.xml repository with <server> credentials, 401",
repos: []repoSpec{{status: http.StatusUnauthorized, withCreds: true}},
parsers: 1,
wantHits: []int{1},
wantStatus: []int{http.StatusUnauthorized},
wantErr: "not found",
},
{
name: "mirror with <server> credentials and a URL query, 403",
repos: []repoSpec{{status: http.StatusForbidden, withCreds: true, viaMirror: true, query: "?token=" + token}},
parsers: 1,
wantHits: []int{1},
wantStatus: []int{http.StatusForbidden},
wantErr: "not found",
},
{
name: "two parsers, the same repository is warned about once",
repos: []repoSpec{{status: http.StatusUnauthorized, withCreds: true}},
parsers: 2,
wantHits: []int{2},
wantStatus: []int{http.StatusUnauthorized},
wantErr: "not found",
},
{
name: "snapshot, 401 on maven-metadata.xml, the POM is served",
repos: []repoSpec{{status: http.StatusOK, metadataStatus: http.StatusUnauthorized, withCreds: true}},
snapshot: true,
parsers: 1,
wantHits: []int{2},
wantStatus: []int{http.StatusUnauthorized},
},
{
name: "different repositories are warned about separately",
repos: []repoSpec{
{status: http.StatusUnauthorized, withCreds: true},
{status: http.StatusForbidden, withCreds: true},
},
parsers: 1,
wantHits: []int{1, 1},
wantStatus: []int{http.StatusUnauthorized, http.StatusForbidden},
wantErr: "not found",
},
{
name: "404 with credentials is not warned about",
repos: []repoSpec{{status: http.StatusNotFound, withCreds: true}},
parsers: 1,
wantHits: []int{1},
wantErr: "not found",
},
{
name: "401 without credentials is not warned about",
repos: []repoSpec{{status: http.StatusUnauthorized}},
parsers: 1,
wantHits: []int{1},
wantErr: "not found",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// The warning is deduplicated by host:port, and the OS may reuse the port
// of a server closed by an earlier case.
rejectedCredentialsRepos.Range(func(key string, _ struct{}) bool {
rejectedCredentialsRepos.Delete(key)
return true
})
hits := make([]int, len(tt.repos))
gotUsers := make([]string, len(tt.repos))
var pomRepos []pomRepository
var servers []Server
var settingsMirrors []Mirror
for i, spec := range tt.repos {
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
hits[i]++
gotUsers[i], _, _ = r.BasicAuth()
status := spec.status
if strings.HasSuffix(r.URL.Path, "/maven-metadata.xml") && spec.metadataStatus != 0 {
status = spec.metadataStatus
}
if status != http.StatusOK {
w.WriteHeader(status)
return
}
_, _ = w.Write([]byte(pom))
}))
t.Cleanup(s.Close)
id := fmt.Sprintf("repo-%d", i)
repoURL := s.URL
if spec.viaMirror {
// The pom.xml repository is replaced by the mirror and never requested.
repoURL = "https://repo.invalid/maven2"
id = fmt.Sprintf("mirror-%d", i)
settingsMirrors = append(settingsMirrors, Mirror{ID: id, URL: s.URL + spec.query, MirrorOf: fmt.Sprintf("repo-%d", i)})
}
if spec.withCreds {
servers = append(servers, Server{ID: id, Username: user, Password: pass})
}
pomRepos = append(pomRepos, pomRepository{ID: fmt.Sprintf("repo-%d", i), URL: repoURL})
}
var logs bytes.Buffer
logger := log.New(log.NewHandler(&logs, &log.Options{Level: log.LevelWarn}))
repos := resolvePomRepos(servers, pomRepos)
paths := []string{"com", "example", "example-api", "1.0.0", "example-api-1.0.0.pom"}
for range tt.parsers {
p := &Parser{
logger: logger,
mirrors: resolveMirrors(settingsMirrors, servers, nil),
httpClient: http.DefaultClient,
notFoundPOMs: set.New[string](),
}
got, err := p.fetchPOMFromRemoteRepositories(t.Context(), paths, tt.snapshot, repos)
if tt.wantErr != "" {
require.ErrorContains(t, err, tt.wantErr)
} else {
require.NoError(t, err)
require.NotNil(t, got)
}
}
require.Equal(t, tt.wantHits, hits, "request count per repository")
for i, spec := range tt.repos {
wantUser := ""
if spec.withCreds {
wantUser = user
}
require.Equal(t, wantUser, gotUsers[i], "basic auth user of repository %d", i)
}
out := logs.String()
require.Equal(t, len(tt.wantStatus), strings.Count(out, "denied access with the configured credentials"), out)
for _, status := range tt.wantStatus {
require.Contains(t, out, fmt.Sprintf("statusCode=%d", status))
}
for _, secret := range []string{user, pass, token} {
require.NotContains(t, out, secret)
}
})
}
}
// Test_fetchPOMFromRemoteRepositories_mirror verifies that mirrors substitute
// the target URL inside fetchPOMFromRemoteRepositories — i.e. that mirrorFor
// is wired into the fetch loop, not just the matching logic.
func Test_fetchPOMFromRemoteRepositories_mirror(t *testing.T) {
const minimalPOM = `<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>example-api</artifactId>
<version>1.0.0</version>
</project>`
// Matching logic and credential resolution are covered by Test_mirror_matches,
// Test_resolveMirrors and TestParser_mirrorFor. Here we verify the things that
// can only be observed end-to-end through HTTP: that mirrorFor is applied to the
// fetch loop, that mirror credentials reach the remote request as Basic Auth, that
// a settings.xml -> trivy.yaml chain routes the fetch through to the final mirror,
// that the fetch falls back to the next trivy.yaml mirror on 404 or 429, that a
// 429 is returned only when every mirror is rate-limited, and that a POM not found
// or denied with a 403 in a mirror is not requested from it again, while a 503 is retried.
tests := []struct {
name string
mirrorPatterns []string
mirrorWithCreds bool
wantBasicAuth string
configMirrors map[string][]string // trivy.yaml mirrors, by server role: source-role -> []mirror-role
refetch bool // fetch the same POM again after the first fetch
wantHits map[string]int // expected request count per server role
wantErr string // non-empty: fetch must fail with an error containing this
wantWarn string // non-empty: the logged output must contain this text
}{
{
name: "settings.xml <server> credentials reach the mirror as Basic Auth",
mirrorPatterns: []string{"*"},
mirrorWithCreds: true,
wantBasicAuth: "mirror-user",
wantHits: map[string]int{"mirror": 1},
},
{
// Example 2 end-to-end: settings.xml rewrites central(repo1) -> mirror(repo2),
// then trivy.yaml rewrites repo2 -> chain(repo3), so only repo3 is requested.
name: "cross-source chaining: settings.xml then trivy.yaml routes to the final mirror",
mirrorPatterns: []string{"*"},
configMirrors: map[string][]string{"mirror": {"chain"}},
wantHits: map[string]int{"chain": 1},
},
{
// trivy.yaml lists two mirrors for repo2; the first 404s, so the fetch falls
// back to the second and succeeds there.
name: "trivy.yaml fallback: first mirror 404s, second one serves",
mirrorPatterns: []string{"*"},
configMirrors: map[string][]string{"mirror": {"dead", "chain"}},
wantHits: map[string]int{"dead": 1, "chain": 1},
},
{
// The POM is fetched twice; the mirror that 404'd the first time is not requested again.
name: "trivy.yaml fallback: not found POM is not requested again",
mirrorPatterns: []string{"*"},
configMirrors: map[string][]string{"mirror": {"dead", "chain"}},
refetch: true,
wantHits: map[string]int{"dead": 1, "chain": 2},
},
{
// The POM is fetched twice; the mirror that returned 503 is requested again,
// since it may have recovered.
name: "trivy.yaml fallback: POM failed with 503 is requested again",
mirrorPatterns: []string{"*"},
configMirrors: map[string][]string{"mirror": {"unavailable", "chain"}},
refetch: true,
wantHits: map[string]int{"unavailable": 2, "chain": 2},
},
{
// The POM is fetched twice; a 403 is not expected to change, so the mirror that
// returned it is not requested again.
name: "trivy.yaml fallback: POM denied with 403 is not requested again",
mirrorPatterns: []string{"*"},
configMirrors: map[string][]string{"mirror": {"forbidden", "chain"}},
refetch: true,
wantHits: map[string]int{"forbidden": 1, "chain": 2},
},
{
// A POM that fails to parse is not retried against another repository: the
// repository that served it is treated as authoritative, same as Maven.
name: "invalid POM: not retried against another repository",
mirrorPatterns: []string{"*"},
configMirrors: map[string][]string{"mirror": {"broken", "chain"}},
wantHits: map[string]int{"broken": 1},
wantErr: "failed to parse the remote POM",
wantWarn: "Invalid POM",
},
{
// The first mirror returns 429; the fetch skips it and succeeds on the second.
name: "trivy.yaml fallback: first mirror 429s, second one serves",
mirrorPatterns: []string{"*"},
configMirrors: map[string][]string{"mirror": {"limited", "chain"}},
wantHits: map[string]int{"limited": 1, "chain": 1},
},
{
// Every mirror is rate-limited; the 429 is returned rather than "not found".
name: "all mirrors rate-limited: the 429 is returned",
mirrorPatterns: []string{"*"},
configMirrors: map[string][]string{"mirror": {"limited"}},
wantHits: map[string]int{"limited": 1},
wantErr: "429",
},
{
// Multiple mirrors are rate-limited; all are queried and the 429 is returned.
name: "multiple mirrors rate-limited: all are queried and 429 is returned",
mirrorPatterns: []string{"*"},
configMirrors: map[string][]string{"mirror": {"limited", "limit2"}},
wantHits: map[string]int{"limited": 1, "limit2": 1},
wantErr: `429 Too Many Requests for:\n - http://.*/example-api-1\.0\.0\.pom\n - http://.*/example-api-1\.0\.0\.pom`,
},
{
// The first mirror returns 429, the second one serves a broken POM: the failure
// is returned together with the 429, which is not lost.
name: "429 followed by a failure: both are reported",
mirrorPatterns: []string{"*"},
configMirrors: map[string][]string{"mirror": {"limited", "broken"}},
wantHits: map[string]int{"limited": 1, "broken": 1},
wantErr: `(?s)failed to parse the remote POM.*429 Too Many Requests for:\n - http://\S+/example-api-1\.0\.0\.pom$`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
hits := make(map[string]int)
var gotBasicAuth string
// newServer registers an HTTP server for a role. status 200 serves the POM;
// any other status (e.g. 404 or 429) makes the fetch fall back to the next candidate.
// A non-empty body overrides the served POM.
newServer := func(role string, status int, body string) *httptest.Server {
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
hits[role]++
if u, _, ok := r.BasicAuth(); ok {
gotBasicAuth = u
}
if status != http.StatusOK {
w.WriteHeader(status)
return
}
_, _ = w.Write([]byte(cmp.Or(body, minimalPOM)))
}))
t.Cleanup(s.Close)
return s
}
// repo1: original repository; mirror: settings.xml target; chain/dead:
// trivy.yaml targets (dead always 404s).
servers := map[string]*httptest.Server{
"repo1": newServer("repo1", http.StatusOK, ""),
"mirror": newServer("mirror", http.StatusOK, ""),
"chain": newServer("chain", http.StatusOK, ""),
"dead": newServer("dead", http.StatusNotFound, ""),
"limited": newServer("limited", http.StatusTooManyRequests, ""),
"broken": newServer("broken", http.StatusOK, "not a pom"),
"limit2": newServer("limit2", http.StatusTooManyRequests, ""),
"unavailable": newServer("unavailable", http.StatusServiceUnavailable, ""),
"forbidden": newServer("forbidden", http.StatusForbidden, ""),
}
// trivy.yaml mirrors: resolve role names to server URLs.
configFileMirrors := make(map[string][]string)
for srcRole, dstRoles := range tt.configMirrors {
for _, dstRole := range dstRoles {
src := servers[srcRole].URL
configFileMirrors[src] = append(configFileMirrors[src], servers[dstRole].URL)
}
}
// settings.xml mirror central -> "mirror"; creds (if any) come from a <server>.
var srvCreds []Server
if tt.mirrorWithCreds {
srvCreds = []Server{{ID: "m1", Username: "mirror-user", Password: "mirror-pass"}}
}
resolved := resolveMirrors(
[]Mirror{{ID: "m1", URL: servers["mirror"].URL, MirrorOf: strings.Join(tt.mirrorPatterns, ",")}},
srvCreds,
configFileMirrors,
)
origURL, err := url.Parse(servers["repo1"].URL)
require.NoError(t, err)
pomRepo := repository{id: "central", url: *origURL, releaseEnabled: true}
var logBuf bytes.Buffer
p := &Parser{
logger: log.New(log.NewHandler(&logBuf, nil)).With(log.Prefix("pom")),
mirrors: resolved,
httpClient: http.DefaultClient,
notFoundPOMs: set.New[string](),
}
paths := []string{"com", "example", "example-api", "1.0.0", "example-api-1.0.0.pom"}
fetch := func() {
got, err := p.fetchPOMFromRemoteRepositories(t.Context(), paths, false, []repository{pomRepo})
if tt.wantErr != "" {
require.Regexp(t, tt.wantErr, err)
} else {
require.NoError(t, err)
require.NotNil(t, got)
}
}
fetch()
if tt.refetch {
fetch()
}
for role := range servers {
require.Equal(t, tt.wantHits[role], hits[role], "request count for %q", role)
}
require.Equal(t, tt.wantBasicAuth, gotBasicAuth, "basic auth user")
if tt.wantWarn != "" {
require.Contains(t, logBuf.String(), tt.wantWarn)
}
})
}
}
// Test_fetchPOMFromRemoteRepositories_invalidMetadata covers a snapshot lookup where the
// first repository's maven-metadata.xml is invalid: resolution must warn and move on to the
// next repository instead of failing outright. This needs its own server setup (snapshot
// mode, a maven-metadata.xml endpoint distinct from the POM endpoint, and a repository list
// instead of mirrors), so it does not fit the mirror-focused table above.
func Test_fetchPOMFromRemoteRepositories_invalidMetadata(t *testing.T) {
const snapshotPOM = `<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>example-api</artifactId>
<version>1.0.0-SNAPSHOT</version>
</project>`
const validMetadata = `<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<versioning>
<snapshotVersions>
<snapshotVersion>
<extension>pom</extension>
<value>1.0.0-20231001.120000-1</value>
</snapshotVersion>
</snapshotVersions>
</versioning>
</metadata>`
hits := make(map[string]int)
newRepoServer := func(role, metadataBody string) *httptest.Server {
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "maven-metadata.xml") {
hits[role+":metadata"]++
_, _ = w.Write([]byte(metadataBody))
return
}
hits[role+":pom"]++
if strings.HasSuffix(r.URL.Path, "example-api-1.0.0-20231001.120000-1.pom") {
_, _ = w.Write([]byte(snapshotPOM))
return
}
w.WriteHeader(http.StatusNotFound)
}))
t.Cleanup(s.Close)
return s
}
// repoBad serves invalid maven-metadata.xml; repoGood serves valid metadata pointing at
// the timestamped snapshot POM.
badServer := newRepoServer("bad", "not valid metadata")
goodServer := newRepoServer("good", validMetadata)
repoFor := func(id string, s *httptest.Server) repository {
u, err := url.Parse(s.URL)
require.NoError(t, err)
return repository{id: id, url: *u, releaseEnabled: true, snapshotEnabled: true}
}
var logBuf bytes.Buffer
p := &Parser{
logger: log.New(log.NewHandler(&logBuf, nil)).With(log.Prefix("pom")),
httpClient: http.DefaultClient,
notFoundPOMs: set.New[string](),
}
paths := []string{"com", "example", "example-api", "1.0.0-SNAPSHOT", "example-api-1.0.0-SNAPSHOT.pom"}
got, err := p.fetchPOMFromRemoteRepositories(t.Context(), paths, true,
[]repository{repoFor("bad", badServer), repoFor("good", goodServer)})
require.NoError(t, err)
require.NotNil(t, got)
// repoBad: metadata fetched and rejected, then the unresolved (non-snapshot) filename 404s.
// repoGood: metadata fetched and resolves the timestamped filename, which is then served.
require.Equal(t, map[string]int{"bad:metadata": 1, "bad:pom": 1, "good:metadata": 1, "good:pom": 1}, hits)
require.Contains(t, logBuf.String(), "Invalid maven-metadata.xml")
}
// Repositories with different release and snapshot policies can share one mirror.
// The mirror is requested for an artifact that only the second repository accepts.
func Test_fetchPOMFromRemoteRepositories_mirrorPolicy(t *testing.T) {
releasesURL, err := url.Parse("https://releases.example.com/maven2/")
require.NoError(t, err)
snapshotsURL, err := url.Parse("https://snapshots.example.com/maven2/")
require.NoError(t, err)
releasesRepo := repository{id: "releases", url: *releasesURL, releaseEnabled: true}
snapshotsRepo := repository{id: "snapshots", url: *snapshotsURL, snapshotEnabled: true}
tests := []struct {
name string
repos []repository
snapshot bool
version string
wantPaths []string
}{
{
name: "snapshot artifact, release-only repository first",
repos: []repository{releasesRepo, snapshotsRepo},
snapshot: true,
version: "1.0.0-SNAPSHOT",
wantPaths: []string{
"/com/example/example-api/1.0.0-SNAPSHOT/maven-metadata.xml",
"/com/example/example-api/1.0.0-SNAPSHOT/example-api-1.0.0-SNAPSHOT.pom",
},
},
{
name: "release artifact, snapshot-only repository first",
repos: []repository{snapshotsRepo, releasesRepo},
version: "1.0.0",
wantPaths: []string{
"/com/example/example-api/1.0.0/example-api-1.0.0.pom",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var gotPaths []string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPaths = append(gotPaths, r.URL.Path)
_, _ = w.Write([]byte(`<project><artifactId>example-api</artifactId></project>`))
}))
t.Cleanup(srv.Close)
p := &Parser{
logger: log.WithPrefix("pom"),
mirrors: resolveMirrors([]Mirror{{ID: "m1", URL: srv.URL, MirrorOf: "*"}}, nil, nil),
httpClient: http.DefaultClient,
notFoundPOMs: set.New[string](),
}
paths := []string{"com", "example", "example-api", tt.version, "example-api-" + tt.version + ".pom"}
got, err := p.fetchPOMFromRemoteRepositories(t.Context(), paths, tt.snapshot, tt.repos)
require.NoError(t, err)
require.NotNil(t, got)
require.Equal(t, tt.wantPaths, gotPaths)
})
}
}