mirror of
https://github.com/aquasecurity/trivy.git
synced 2026-12-06 02:02:58 +00:00
89 lines
2.9 KiB
Go
89 lines
2.9 KiB
Go
package pom
|
|
|
|
import (
|
|
"errors"
|
|
"net/url"
|
|
|
|
"github.com/aquasecurity/trivy/pkg/log"
|
|
)
|
|
|
|
// mavenCentralRepoID is the conventional <id> for the default Maven Central
|
|
// repository. <mirror> rules use it (e.g. <mirrorOf>central</mirrorOf>), so
|
|
// it must be the same wherever we construct a repository pointing at Central.
|
|
const mavenCentralRepoID = "central"
|
|
|
|
var centralURL, _ = url.Parse("https://repo.maven.apache.org/maven2/")
|
|
|
|
type repository struct {
|
|
id string // <id> from settings.xml/pom.xml; used for <mirror> matching
|
|
url url.URL
|
|
releaseEnabled bool
|
|
snapshotEnabled bool
|
|
}
|
|
|
|
// hasCredentials reports whether requests to the repository carry credentials.
|
|
func (r repository) hasCredentials() bool {
|
|
return r.url.User != nil
|
|
}
|
|
|
|
type repositories struct {
|
|
settings []repository // Repositories from settings.xml files
|
|
defaultRepo repository // Default repository - Maven Central for Release, empty for Snapshot
|
|
}
|
|
|
|
var mavenCentralRepo = repository{
|
|
id: mavenCentralRepoID,
|
|
url: *centralURL,
|
|
releaseEnabled: true,
|
|
}
|
|
|
|
// resolvePomRepos also returns disabled repositories, so that they hide repositories with the same ID, as in Maven.
|
|
func resolvePomRepos(servers []Server, pomRepos []pomRepository) []repository {
|
|
logger := log.WithPrefix("pom")
|
|
var repos []repository
|
|
for _, rep := range pomRepos {
|
|
r := repository{
|
|
id: rep.ID,
|
|
// "<enabled>: true or false for whether this repository is enabled for the respective type (releases or snapshots). By default, this is true."
|
|
// cf. https://maven.apache.org/pom.html#Repositories
|
|
releaseEnabled: rep.ReleasesEnabled == trueString || rep.ReleasesEnabled == "",
|
|
snapshotEnabled: rep.SnapshotsEnabled == trueString || rep.SnapshotsEnabled == "",
|
|
}
|
|
|
|
repoURL, err := url.Parse(rep.URL)
|
|
if err != nil {
|
|
if ue, ok := errors.AsType[*url.Error](err); ok {
|
|
err = ue.Unwrap()
|
|
}
|
|
logger.Debug("Unable to parse remote repository url", log.String("id", rep.ID), log.Err(err))
|
|
continue
|
|
}
|
|
if !repoURL.IsAbs() {
|
|
logger.Debug("Remote repository url must be absolute", log.String("id", rep.ID))
|
|
continue
|
|
}
|
|
|
|
// Get the credentials from settings.xml based on matching server id
|
|
// with the repository id from pom.xml and use it for accessing the repository url
|
|
for _, server := range servers {
|
|
if rep.ID == server.ID && server.Username != "" && server.Password != "" {
|
|
repoURL.User = url.UserPassword(server.Username, server.Password)
|
|
break
|
|
}
|
|
}
|
|
|
|
logger.Debug("Adding repository", log.String("id", rep.ID), log.String("url", repoURL.Redacted()),
|
|
log.Bool("releases", r.releaseEnabled), log.Bool("snapshots", r.snapshotEnabled))
|
|
r.url = *repoURL
|
|
repos = append(repos, r)
|
|
}
|
|
return repos
|
|
}
|
|
|
|
// displayURL returns the URL without the userinfo, query and fragment, which may carry
|
|
// credentials or a token.
|
|
func displayURL(u url.URL) string {
|
|
u.User, u.RawQuery, u.Fragment, u.ForceQuery = nil, "", "", false
|
|
return u.String()
|
|
}
|