-
Notifications
You must be signed in to change notification settings - Fork 363
/
maven_registry_client.go
176 lines (149 loc) · 4.86 KB
/
maven_registry_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package client
import (
"context"
"encoding/gob"
"fmt"
"os"
"strings"
"deps.dev/util/maven"
"deps.dev/util/resolve"
"deps.dev/util/resolve/version"
"github.com/google/osv-scanner/internal/resolution/datasource"
mavenutil "github.com/google/osv-scanner/internal/utility/maven"
)
const mavenRegistryCacheExt = ".resolve.maven"
type MavenRegistryClient struct {
api *datasource.MavenRegistryAPIClient
}
func NewMavenRegistryClient(registry string) (*MavenRegistryClient, error) {
client, err := datasource.NewMavenRegistryAPIClient(registry)
if err != nil {
return nil, err
}
return &MavenRegistryClient{api: client}, nil
}
func (c *MavenRegistryClient) Version(ctx context.Context, vk resolve.VersionKey) (resolve.Version, error) {
g, a, found := strings.Cut(vk.Name, ":")
if !found {
return resolve.Version{}, fmt.Errorf("invalid Maven package name %s", vk.Name)
}
proj, err := c.api.GetProject(ctx, g, a, vk.Version)
if err != nil {
return resolve.Version{}, err
}
regs := make([]string, len(proj.Repositories))
// Repositories are served as dependency registries.
// https://github.com/google/deps.dev/blob/main/util/resolve/api.go#L106
for i, repo := range proj.Repositories {
regs[i] = "dep:" + string(repo.URL)
}
var attr version.AttrSet
if len(regs) > 0 {
attr.SetAttr(version.Registries, strings.Join(regs, "|"))
}
return resolve.Version{VersionKey: vk, AttrSet: attr}, nil
}
// TODO: we should also include versions not listed in the metadata file
// There exist versions in the repository but not listed in the metada file,
// for example version 20030203.000550 of package commons-io:commons-io
// https://repo1.maven.org/maven2/commons-io/commons-io/20030203.000550/.
// A package may depend on such version if a soft requirement of this version
// is declared.
// We need to find out if there are such versions and include them in the
// returned versions.
func (c *MavenRegistryClient) Versions(ctx context.Context, pk resolve.PackageKey) ([]resolve.Version, error) {
if pk.System != resolve.Maven {
return nil, fmt.Errorf("wrong system: %v", pk.System)
}
g, a, found := strings.Cut(pk.Name, ":")
if !found {
return nil, fmt.Errorf("invalid Maven package name %s", pk.Name)
}
versions, err := c.api.GetVersions(ctx, g, a)
if err != nil {
return nil, err
}
vks := make([]resolve.Version, len(versions))
for i, v := range versions {
vks[i] = resolve.Version{
VersionKey: resolve.VersionKey{
PackageKey: pk,
Version: string(v),
VersionType: resolve.Concrete,
}}
}
return vks, nil
}
func (c *MavenRegistryClient) Requirements(ctx context.Context, vk resolve.VersionKey) ([]resolve.RequirementVersion, error) {
if vk.System != resolve.Maven {
return nil, fmt.Errorf("wrong system: %v", vk.System)
}
g, a, found := strings.Cut(vk.Name, ":")
if !found {
return nil, fmt.Errorf("invalid Maven package name %s", vk.Name)
}
proj, err := c.api.GetProject(ctx, g, a, vk.Version)
if err != nil {
return nil, err
}
// Only merge default profiles by passing empty JDK and OS information.
if err := proj.MergeProfiles("", maven.ActivationOS{}); err != nil {
return nil, err
}
// We need to merge parents for potential dependencies in parents.
if err := mavenutil.MergeParents(ctx, c.api, &proj, proj.Parent, 1, "", false); err != nil {
return nil, err
}
proj.ProcessDependencies(func(groupID, artifactID, version maven.String) (maven.DependencyManagement, error) {
return mavenutil.GetDependencyManagement(ctx, c.api, groupID, artifactID, version)
})
reqs := make([]resolve.RequirementVersion, 0, len(proj.Dependencies))
for _, d := range proj.Dependencies {
reqs = append(reqs, resolve.RequirementVersion{
VersionKey: resolve.VersionKey{
PackageKey: resolve.PackageKey{
System: resolve.Maven,
Name: d.Name(),
},
VersionType: resolve.Requirement,
Version: string(d.Version),
},
Type: resolve.MavenDepType(d, ""),
})
}
return reqs, nil
}
func (c *MavenRegistryClient) MatchingVersions(ctx context.Context, vk resolve.VersionKey) ([]resolve.Version, error) {
if vk.System != resolve.Maven {
return nil, fmt.Errorf("wrong system: %v", vk.System)
}
versions, err := c.Versions(ctx, vk.PackageKey)
if err != nil {
return nil, err
}
return resolve.MatchRequirement(vk, versions), nil
}
func (c *MavenRegistryClient) AddRegistries(registries []Registry) error {
for _, reg := range registries {
if err := c.api.AddRegistry(reg.URL); err != nil {
return err
}
}
return nil
}
func (c *MavenRegistryClient) WriteCache(path string) error {
f, err := os.Create(path + mavenRegistryCacheExt)
if err != nil {
return err
}
defer f.Close()
return gob.NewEncoder(f).Encode(c.api)
}
func (c *MavenRegistryClient) LoadCache(path string) error {
f, err := os.Open(path + mavenRegistryCacheExt)
if err != nil {
return err
}
defer f.Close()
return gob.NewDecoder(f).Decode(&c.api)
}