-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathoci_chart_repository.go
353 lines (302 loc) · 10.8 KB
/
oci_chart_repository.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
Copyright 2022 The Flux authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package repository
import (
"bytes"
"context"
"crypto/tls"
"fmt"
"net/url"
"os"
"path"
"sort"
"strings"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/getter"
"helm.sh/helm/v3/pkg/registry"
"helm.sh/helm/v3/pkg/repo"
"github.com/Masterminds/semver/v3"
"github.com/google/go-containerregistry/pkg/name"
"github.com/fluxcd/pkg/version"
"github.com/fluxcd/source-controller/internal/oci"
"github.com/fluxcd/source-controller/internal/transport"
)
// RegistryClient is an interface for interacting with OCI registries
// It is used by the OCIChartRepository to retrieve chart versions
// from OCI registries
type RegistryClient interface {
Login(host string, opts ...registry.LoginOption) error
Logout(host string, opts ...registry.LogoutOption) error
Tags(url string) ([]string, error)
}
// OCIChartRepository represents a Helm chart repository, and the configuration
// required to download the repository tags and charts from the repository.
// All methods are thread safe unless defined otherwise.
type OCIChartRepository struct {
// URL is the location of the repository.
URL url.URL
// Client to use while accessing the repository's contents.
Client getter.Getter
// Options to configure the Client with while downloading tags
// or a chart from the URL.
Options []getter.Option
tlsConfig *tls.Config
// RegistryClient is a client to use while downloading tags or charts from a registry.
RegistryClient RegistryClient
// credentialsFile is a temporary credentials file to use while downloading tags or charts from a registry.
credentialsFile string
// verifiers is a list of verifiers to use when verifying a chart.
verifiers []oci.Verifier
}
// OCIChartRepositoryOption is a function that can be passed to NewOCIChartRepository
// to configure an OCIChartRepository.
type OCIChartRepositoryOption func(*OCIChartRepository) error
// WithVerifiers returns a ChartRepositoryOption that will set the chart verifiers
func WithVerifiers(verifiers []oci.Verifier) OCIChartRepositoryOption {
return func(r *OCIChartRepository) error {
r.verifiers = verifiers
return nil
}
}
// WithOCIRegistryClient returns a ChartRepositoryOption that will set the registry client
func WithOCIRegistryClient(client RegistryClient) OCIChartRepositoryOption {
return func(r *OCIChartRepository) error {
r.RegistryClient = client
return nil
}
}
// WithOCIGetter returns a ChartRepositoryOption that will set the getter.Getter
func WithOCIGetter(providers getter.Providers) OCIChartRepositoryOption {
return func(r *OCIChartRepository) error {
c, err := providers.ByScheme(r.URL.Scheme)
if err != nil {
return err
}
r.Client = c
return nil
}
}
// WithOCIGetterOptions returns a ChartRepositoryOption that will set the getter.Options
func WithOCIGetterOptions(getterOpts []getter.Option) OCIChartRepositoryOption {
return func(r *OCIChartRepository) error {
r.Options = getterOpts
return nil
}
}
// WithCredentialsFile returns a ChartRepositoryOption that will set the credentials file
func WithCredentialsFile(credentialsFile string) OCIChartRepositoryOption {
return func(r *OCIChartRepository) error {
r.credentialsFile = credentialsFile
return nil
}
}
// NewOCIChartRepository constructs and returns a new ChartRepository with
// the ChartRepository.Client configured to the getter.Getter for the
// repository URL scheme. It returns an error on URL parsing failures.
// It assumes that the url scheme has been validated to be an OCI scheme.
func NewOCIChartRepository(repositoryURL string, chartRepoOpts ...OCIChartRepositoryOption) (*OCIChartRepository, error) {
u, err := url.Parse(repositoryURL)
if err != nil {
return nil, err
}
r := &OCIChartRepository{}
r.URL = *u
for _, opt := range chartRepoOpts {
if err := opt(r); err != nil {
return nil, err
}
}
return r, nil
}
// GetChartVersion returns the repo.ChartVersion for the given name, the version is expected
// to be a semver.Constraints compatible string. If version is empty, the latest
// stable version will be returned and prerelease versions will be ignored.
// adapted from https://github.com/helm/helm/blob/49819b4ef782e80b0c7f78c30bd76b51ebb56dc8/pkg/downloader/chart_downloader.go#L162
func (r *OCIChartRepository) GetChartVersion(name, ver string) (*repo.ChartVersion, error) {
cv, err := r.getChartVersion(name, ver)
if err != nil {
return nil, &ErrExternal{Err: err}
}
return cv, nil
}
func (r *OCIChartRepository) getChartVersion(name, ver string) (*repo.ChartVersion, error) {
cpURL := r.URL
cpURL.Path = path.Join(cpURL.Path, name)
// if ver is a valid semver version, take a shortcut here so we don't need to list all tags which can be an
// expensive operation.
if _, err := version.ParseVersion(ver); err == nil {
return &repo.ChartVersion{
URLs: []string{fmt.Sprintf("%s:%s", cpURL.String(), ver)},
Metadata: &chart.Metadata{
Name: name,
Version: ver,
},
}, nil
}
// ver doesn't denote a concrete version so we interpret it as a semver range and try to find the best-matching
// version from the list of tags in the registry.
cvs, err := r.getTags(cpURL.String())
if err != nil {
return nil, fmt.Errorf("could not get tags for %q: %s", name, err)
}
if len(cvs) == 0 {
return nil, fmt.Errorf("unable to locate any tags in provided repository: %s", name)
}
// Determine if version provided
// If empty, try to get the highest available tag
// If exact version, try to find it
// If semver constraint string, try to find a match
tag, err := getLastMatchingVersionOrConstraint(cvs, ver)
return &repo.ChartVersion{
URLs: []string{fmt.Sprintf("%s:%s", cpURL.String(), tag)},
Metadata: &chart.Metadata{
Name: name,
Version: tag,
},
}, err
}
// This function shall be called for OCI registries only
// It assumes that the ref has been validated to be an OCI reference.
func (r *OCIChartRepository) getTags(ref string) ([]string, error) {
// Retrieve list of repository tags
tags, err := r.RegistryClient.Tags(strings.TrimPrefix(ref, fmt.Sprintf("%s://", registry.OCIScheme)))
if err != nil {
return nil, fmt.Errorf("could not fetch tags for %q: %s", ref, err)
}
if len(tags) == 0 {
return nil, fmt.Errorf("unable to locate any tags in provided repository: %s", ref)
}
return tags, nil
}
// DownloadChart confirms the given repo.ChartVersion has a downloadable URL,
// and then attempts to download the chart using the Client and Options of the
// ChartRepository. It returns a bytes.Buffer containing the chart data.
// In case of an OCI hosted chart, this function assumes that the chartVersion url is valid.
func (r *OCIChartRepository) DownloadChart(chart *repo.ChartVersion) (*bytes.Buffer, error) {
if len(chart.URLs) == 0 {
return nil, fmt.Errorf("chart '%s' has no downloadable URLs", chart.Name)
}
ref := chart.URLs[0]
u, err := url.Parse(ref)
if err != nil {
err = fmt.Errorf("invalid chart URL format '%s': %w", ref, err)
return nil, err
}
t := transport.NewOrIdle(r.tlsConfig)
clientOpts := append(r.Options, getter.WithTransport(t))
defer transport.Release(t)
// trim the oci scheme prefix if needed
b, err := r.Client.Get(strings.TrimPrefix(u.String(), fmt.Sprintf("%s://", registry.OCIScheme)), clientOpts...)
if err != nil {
return nil, fmt.Errorf("failed to get '%s': %w", ref, err)
}
return b, nil
}
// Login attempts to login to the OCI registry.
// It returns an error on failure.
func (r *OCIChartRepository) Login(opts ...registry.LoginOption) error {
err := r.RegistryClient.Login(r.URL.Host, opts...)
if err != nil {
return err
}
return nil
}
// Logout attempts to logout from the OCI registry.
// It returns an error on failure.
func (r *OCIChartRepository) Logout() error {
err := r.RegistryClient.Logout(r.URL.Host)
if err != nil {
return err
}
return nil
}
// HasCredentials returns true if the OCIChartRepository has credentials.
func (r *OCIChartRepository) HasCredentials() bool {
return r.credentialsFile != ""
}
// Clear deletes the OCI registry credentials file.
func (r *OCIChartRepository) Clear() error {
// clean the credentials file if it exists
if r.credentialsFile != "" {
if err := os.Remove(r.credentialsFile); err != nil {
return err
}
}
r.credentialsFile = ""
return nil
}
// getLastMatchingVersionOrConstraint returns the last version that matches the given version string.
// If the version string is empty, the highest available version is returned.
func getLastMatchingVersionOrConstraint(cvs []string, ver string) (string, error) {
// Check for exact matches first
if ver != "" {
for _, cv := range cvs {
if ver == cv {
return cv, nil
}
}
}
// Continue to look for a (semantic) version match
verConstraint, err := semver.NewConstraint("*")
if err != nil {
return "", err
}
latestStable := ver == "" || ver == "*"
if !latestStable {
verConstraint, err = semver.NewConstraint(ver)
if err != nil {
return "", err
}
}
matchingVersions := make([]*semver.Version, 0, len(cvs))
for _, cv := range cvs {
v, err := version.ParseVersion(cv)
if err != nil {
continue
}
if !verConstraint.Check(v) {
continue
}
matchingVersions = append(matchingVersions, v)
}
if len(matchingVersions) == 0 {
return "", fmt.Errorf("could not locate a version matching provided version string %s", ver)
}
// Sort versions
sort.Sort(sort.Reverse(semver.Collection(matchingVersions)))
return matchingVersions[0].Original(), nil
}
// VerifyChart verifies the chart against a signature.
// If no signature is provided, a keyless verification is performed.
// It returns an error on failure.
func (r *OCIChartRepository) VerifyChart(ctx context.Context, chart *repo.ChartVersion) error {
if len(r.verifiers) == 0 {
return fmt.Errorf("no verifiers available")
}
if len(chart.URLs) == 0 {
return fmt.Errorf("chart '%s' has no downloadable URLs", chart.Name)
}
ref, err := name.ParseReference(strings.TrimPrefix(chart.URLs[0], fmt.Sprintf("%s://", registry.OCIScheme)))
if err != nil {
return fmt.Errorf("invalid chart reference: %s", err)
}
// verify the chart
for _, verifier := range r.verifiers {
if verified, err := verifier.Verify(ctx, ref); err != nil {
return fmt.Errorf("failed to verify %s: %w", chart.URLs[0], err)
} else if verified {
return nil
}
}
return fmt.Errorf("no matching signatures were found for '%s'", ref.Name())
}