/
index.go
345 lines (309 loc) · 9.83 KB
/
index.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
// Copyright 2023 Chainguard, Inc.
//
// 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 apk
import (
"archive/tar"
"bytes"
"context"
"errors"
"fmt"
"io"
"io/fs"
"net/http"
"net/url"
"os"
"regexp"
"strings"
"sync"
"time"
"github.com/klauspost/compress/gzip"
sign "github.com/chainguard-dev/go-apk/pkg/signature"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-retryablehttp"
"go.lsp.dev/uri"
"go.opentelemetry.io/otel"
)
var signatureFileRegex = regexp.MustCompile(`^\.SIGN\.RSA\.(.*\.rsa\.pub)$`)
// This is terrible but simpler than plumbing around a cache for now.
// We just hold the parsed index in memory rather than re-parsing it every time,
// which requires gunzipping, which is (somewhat) expensive.
var globalIndexCache = &indexCache{
modtimes: map[string]time.Time{},
}
type indexResult struct {
idx *APKIndex
err error
}
type indexCache struct {
// For remote indexes.
onces sync.Map
// For local indexes.
sync.Mutex
modtimes map[string]time.Time
// repoBase -> indexResult
indexes sync.Map
}
func (i *indexCache) get(ctx context.Context, u string, keys map[string][]byte, arch string, opts *indexOpts) (*APKIndex, error) {
if strings.HasPrefix(u, "https://") {
// We don't want remote indexes to change while we're running.
once, _ := i.onces.LoadOrStore(u, &sync.Once{})
once.(*sync.Once).Do(func() {
idx, err := getRepositoryIndex(ctx, u, keys, arch, opts)
i.indexes.Store(u, indexResult{
idx: idx,
err: err,
})
})
} else {
i.Lock()
defer i.Unlock()
// We do expect local indexes to change, so we check modtimes.
stat, err := os.Stat(u)
if err != nil {
return nil, nil
}
mod := stat.ModTime()
before, ok := i.modtimes[u]
if !ok || mod.After(before) {
// If this is the first time or it has changed since the last time...
idx, err := getRepositoryIndex(ctx, u, keys, arch, opts)
i.indexes.Store(u, indexResult{
idx: idx,
err: err,
})
i.modtimes[u] = mod
}
}
v, ok := i.indexes.Load(u)
if !ok {
panic(fmt.Errorf("did not see index %q after writing it", u))
}
result := v.(indexResult)
return result.idx, result.err
}
// IndexURL full URL to the index file for the given repo and arch
func IndexURL(repo, arch string) string {
return fmt.Sprintf("%s/%s/%s", repo, arch, indexFilename)
}
// GetRepositoryIndexes returns the indexes for the named repositories, keys and archs.
// The signatures for each index are verified unless ignoreSignatures is set to true.
// The key-value pairs in the map for `keys` are the name of the key and the contents of the key.
// The name is just indicative. If it finds a match, it will use it. Else, it will try all keys.
func GetRepositoryIndexes(ctx context.Context, repos []string, keys map[string][]byte, arch string, options ...IndexOption) (indexes []NamedIndex, err error) {
ctx, span := otel.Tracer("go-apk").Start(ctx, "GetRepositoryIndexes")
defer span.End()
opts := &indexOpts{}
for _, opt := range options {
opt(opts)
}
for _, repo := range repos {
// does it start with a pin?
var (
repoName string
repoURL = repo
)
if strings.HasPrefix(repo, "@") {
// it's a pinned repository, get the name
parts := strings.Fields(repo)
if len(parts) < 2 {
return nil, fmt.Errorf("invalid repository line: %q", repo)
}
repoName = parts[0][1:]
repoURL = parts[1]
}
u := IndexURL(repoURL, arch)
repoBase := fmt.Sprintf("%s/%s", repoURL, arch)
index, err := globalIndexCache.get(ctx, u, keys, arch, opts)
if err != nil {
return nil, fmt.Errorf("reading index %s: %w", u, err)
}
// Can happen for fs.ErrNotExist in file scheme, we just ignore it.
if index == nil {
continue
}
repoRef := Repository{URI: repoBase}
indexes = append(indexes, NewNamedRepositoryWithIndex(repoName, repoRef.WithIndex(index)))
}
return indexes, nil
}
func shouldCheckSignatureForIndex(index string, arch string, opts *indexOpts) bool {
if opts.ignoreSignatures {
return false
}
for _, ignoredIndex := range opts.noSignatureIndexes {
if IndexURL(ignoredIndex, arch) == index {
return false
}
}
return true
}
func getRepositoryIndex(ctx context.Context, u string, keys map[string][]byte, arch string, opts *indexOpts) (*APKIndex, error) {
// Normalize the repo as a URI, so that local paths
// are translated into file:// URLs, allowing them to be parsed
// into a url.URL{}.
var (
b []byte
asURL *url.URL
err error
)
if strings.HasPrefix(u, "https://") {
asURL, err = url.Parse(u)
} else {
// Attempt to parse non-https elements into URI's so they are translated into
// file:// URLs allowing them to parse into a url.URL{}
asURL, err = url.Parse(string(uri.New(u)))
}
if err != nil {
return nil, fmt.Errorf("failed to parse repo as URI: %w", err)
}
switch asURL.Scheme {
case "file":
b, err = os.ReadFile(u)
if err != nil {
if !errors.Is(err, fs.ErrNotExist) {
return nil, fmt.Errorf("failed to read repository %s: %w", u, err)
}
return nil, nil
}
case "https":
client := opts.httpClient
if client == nil {
rhttp := retryablehttp.NewClient()
rhttp.Logger = hclog.Default()
client = rhttp.StandardClient()
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, asURL.String(), nil)
if err != nil {
return nil, err
}
// if the repo URL contains HTTP Basic Auth credentials, add them to the request
if asURL.User != nil {
user := asURL.User.Username()
pass, _ := asURL.User.Password()
req.SetBasicAuth(user, pass)
}
// This will return a body that retries requests using Range requests if Read() hits an error.
rrt := newRangeRetryTransport(ctx, client)
res, err := rrt.RoundTrip(req)
if err != nil {
return nil, fmt.Errorf("unable to get repository index at %s: %w", u, err)
}
switch res.StatusCode {
case http.StatusOK:
// this is fine
case http.StatusNotFound:
return nil, fmt.Errorf("repository index not found for architecture %s at %s", arch, u)
default:
return nil, fmt.Errorf("unexpected status code %d when getting repository index for architecture %s at %s", res.StatusCode, arch, u)
}
defer res.Body.Close()
buf := bytes.NewBuffer(nil)
if _, err := io.Copy(buf, res.Body); err != nil {
return nil, fmt.Errorf("unable to read repository index at %s: %w", u, err)
}
b = buf.Bytes()
default:
return nil, fmt.Errorf("repository scheme %s not supported", asURL.Scheme)
}
// validate the signature
if shouldCheckSignatureForIndex(u, arch, opts) {
buf := bytes.NewReader(b)
gzipReader, err := gzip.NewReader(buf)
if err != nil {
return nil, fmt.Errorf("unable to create gzip reader for repository index: %w", err)
}
// set multistream to false, so we can read each part separately;
// the first part is the signature, the second is the index, which should be
// verified.
gzipReader.Multistream(false)
defer gzipReader.Close()
tarReader := tar.NewReader(gzipReader)
// read the signature
signatureFile, err := tarReader.Next()
if err != nil {
return nil, fmt.Errorf("failed to read signature from repository index: %w", err)
}
matches := signatureFileRegex.FindStringSubmatch(signatureFile.Name)
if len(matches) != 2 {
return nil, fmt.Errorf("failed to find key name in signature file name: %s", signatureFile.Name)
}
signature, err := io.ReadAll(tarReader)
if err != nil {
return nil, fmt.Errorf("failed to read signature from repository index: %w", err)
}
// with multistream false, we should read the next one
if _, err := tarReader.Next(); err != nil && !errors.Is(err, io.EOF) {
return nil, fmt.Errorf("unexpected error reading from tgz: %w", err)
}
// we now have the signature bytes and name, get the contents of the rest;
// this should be everything else in the raw gzip file as is.
allBytes := len(b)
unreadBytes := buf.Len()
readBytes := allBytes - unreadBytes
indexData := b[readBytes:]
indexDigest, err := sign.HashData(indexData)
if err != nil {
return nil, err
}
// now we can check the signature
if keys == nil {
return nil, fmt.Errorf("no keys provided to verify signature")
}
var verified bool
keyData, ok := keys[matches[1]]
if ok {
if err := sign.RSAVerifySHA1Digest(indexDigest, signature, keyData); err != nil {
verified = false
}
}
if !verified {
for _, keyData := range keys {
if err := sign.RSAVerifySHA1Digest(indexDigest, signature, keyData); err == nil {
verified = true
break
}
}
}
if !verified {
return nil, fmt.Errorf("no key found to verify signature for keyfile %s; tried all other keys as well", matches[1])
}
}
// with a valid signature, convert it to an ApkIndex
index, err := IndexFromArchive(io.NopCloser(bytes.NewReader(b)))
if err != nil {
return nil, fmt.Errorf("unable to read convert repository index bytes to index struct at %s: %w", u, err)
}
return index, err
}
type indexOpts struct {
ignoreSignatures bool
noSignatureIndexes []string
httpClient *http.Client
}
type IndexOption func(*indexOpts)
func WithIgnoreSignatures(ignoreSignatures bool) IndexOption {
return func(o *indexOpts) {
o.ignoreSignatures = ignoreSignatures
}
}
func WithIgnoreSignatureForIndexes(noSignatureIndexes ...string) IndexOption {
return func(o *indexOpts) {
o.noSignatureIndexes = append(o.noSignatureIndexes, noSignatureIndexes...)
}
}
func WithHTTPClient(c *http.Client) IndexOption {
return func(o *indexOpts) {
o.httpClient = c
}
}