-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.go
More file actions
393 lines (346 loc) · 10.6 KB
/
sync.go
File metadata and controls
393 lines (346 loc) · 10.6 KB
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// Package pin is the public Go API for the pin tool: read a manifest,
// resolve assets, write them out, and emit a CycloneDX lockfile.
package pin
import (
"bytes"
"context"
"errors"
"fmt"
"io/fs"
"os"
"path"
"path/filepath"
"slices"
"strings"
"github.com/git-pkgs/pin/lock"
"github.com/git-pkgs/pin/manifest"
"github.com/git-pkgs/pin/pinfs"
"github.com/git-pkgs/pin/source/forge"
"github.com/git-pkgs/pin/source/npm"
"golang.org/x/sync/errgroup"
)
const defaultConcurrency = 8
const (
DefaultManifest = "pin.yaml"
DefaultLock = "pin.lock"
ToolName = "pin"
dirPerm = 0o755
filePerm = 0o644
)
// ToolVersion is overridden at build time via the
// `-X github.com/git-pkgs/pin.ToolVersion=X.Y.Z` ldflag.
var ToolVersion = "dev"
// SyncOptions configures pin.Sync / Client.Sync. RegistryURL, Forge,
// SignatureMode, and VerifyProvenance are honoured by the top-level
// pin.Sync shim only; pass them via ClientOptions when constructing a
// Client directly.
type SyncOptions struct {
Dir string
Manifest string
Lock string
DryRun bool
Frozen bool
RegistryURL string
Forge forge.Options
// Update lists entry names whose lock-is-sticky check is
// bypassed; UpdateAll bypasses it for every entry.
Update []string
UpdateAll bool
// StrictProvenance fails sync when an npm entry resolves to a
// version with no SLSA Provenance attestation recorded.
StrictProvenance bool
// RequirePublisherMatchesRepository fails sync when an
// attestation's build workflow lives on a different repository
// than the package's declared repository.url. The consumer-side
// check against leaked-token attacks: a stolen publish token
// produces an attestation whose source_repository won't match.
RequirePublisherMatchesRepository bool
// VerifyProvenance cryptographically verifies each attestation
// bundle against Sigstore's TUF trust root.
VerifyProvenance bool
SignatureMode npm.SignatureMode
// Concurrency caps parallel resolves; zero = defaultConcurrency.
// Lockfile order is independent of completion order: assets are
// sorted by (name, asset.out) before writing.
Concurrency int
// NoFetch implies Frozen and re-hashes every vendored file on
// disk against the lockfile's recorded integrity. For CI jobs
// that vendored at image-build time. No network, no writes.
NoFetch bool
// FS redirects Sync's outputs (vendored files + pin.lock). nil
// means pinfs.OS(opts.Dir). pinfs.NewMemory() keeps everything in
// process. The manifest and prior lockfile are still read from
// opts.Dir on local disk.
FS pinfs.Writer
}
func (o *SyncOptions) forceResolve(name string) bool {
return o.UpdateAll || slices.Contains(o.Update, name)
}
type SyncResult struct {
Lock *lock.Lock
Changes lock.Changes
Written []string
Removed []string
}
// Sync constructs a Client from opts and runs one Sync. Consumers
// that reuse a Client across calls should use New + Client.Sync.
func Sync(ctx context.Context, opts SyncOptions) (*SyncResult, error) {
c, err := clientFromSyncOptions(opts)
if err != nil {
return nil, err
}
return c.Sync(ctx, opts)
}
// Sync resolves the manifest, fetches assets, and writes the
// lockfile. Per-operation behaviour comes from opts; infrastructure
// config (RegistryURL, SignatureMode, ...) comes from the Client.
func (c *Client) Sync(ctx context.Context, opts SyncOptions) (*SyncResult, error) {
if opts.Manifest == "" {
opts.Manifest = DefaultManifest
}
if opts.Lock == "" {
opts.Lock = DefaultLock
}
m, err := readManifest(filepath.Join(opts.Dir, opts.Manifest))
if err != nil {
return nil, err
}
prev, err := readLock(filepath.Join(opts.Dir, opts.Lock))
if err != nil {
return nil, err
}
prevVersions := lockedVersionsByName(prev)
if opts.NoFetch {
return c.syncNoFetch(opts, m, prev, prevVersions)
}
if opts.Frozen {
if err := checkFrozen(m, prev, prevVersions); err != nil {
return nil, err
}
}
fw := opts.FS
if fw == nil {
fw = pinfs.OS(opts.Dir)
}
next := &lock.Lock{OutDir: m.Out}
var written []string
concurrency := opts.Concurrency
if concurrency <= 0 {
concurrency = defaultConcurrency
}
results := make([]entryResult, len(m.Assets))
g, gctx := errgroup.WithContext(ctx)
g.SetLimit(concurrency)
for i := range m.Assets {
g.Go(func() error {
entry := m.Assets[i]
locked := prevVersions[entry.Name]
if opts.forceResolve(entry.Name) {
locked = ""
}
assets, files, rerr := c.resolveEntry(gctx, m, &entry, locked)
if rerr != nil {
return rerr
}
results[i] = entryResult{assets: assets, files: files}
return nil
})
}
if err := g.Wait(); err != nil {
return nil, err
}
for _, r := range results {
next.Assets = append(next.Assets, r.assets...)
}
if err := checkOutCollisions(next.Assets); err != nil {
return nil, err
}
if !opts.DryRun {
w, werr := writeAllFiles(fw, m.Out, results)
if werr != nil {
return nil, werr
}
written = w
}
changes := lock.Diff(prev, next)
if err := enforceTrust(m, next, opts); err != nil {
return nil, err
}
var removed []string
if !opts.DryRun {
removed, err = removeOrphans(fw, m.Out, changes.Removed)
if err != nil {
return nil, err
}
if err := writeLock(fw, opts.Lock, next); err != nil {
return nil, err
}
}
return &SyncResult{Lock: next, Changes: changes, Written: written, Removed: removed}, nil
}
func (c *Client) syncNoFetch(opts SyncOptions, m *manifest.Manifest, prev *lock.Lock, prevVersions map[string]string) (*SyncResult, error) {
if prev == nil {
return nil, fmt.Errorf("--no-fetch: %w at %s", ErrNoLockfile, filepath.Join(opts.Dir, opts.Lock))
}
if err := checkFrozen(m, prev, prevVersions); err != nil {
return nil, fmt.Errorf("--no-fetch: %w", err)
}
vr, err := c.Verify(VerifyOptions{Dir: opts.Dir, Lock: opts.Lock})
if err != nil {
return nil, fmt.Errorf("--no-fetch: %w", err)
}
if vr.Failed() {
return nil, fmt.Errorf("--no-fetch: %w: %s", ErrVerifyFailed, vr.Summary())
}
return &SyncResult{Lock: prev, Changes: lock.Diff(prev, prev)}, nil
}
type entryResult struct {
assets []lock.Asset
files []fileContent
}
func writeAllFiles(fw pinfs.Writer, outDir string, results []entryResult) ([]string, error) {
var written []string
for _, r := range results {
w, err := writeFiles(fw, outDir, r.files)
if err != nil {
return nil, err
}
written = append(written, w...)
}
return written, nil
}
// safeOut returns a slash-separated path under outDir, or
// ErrPathEscape. Defence in depth on top of manifest validation: the
// joined output path is checked one more time before any bytes leave
// pin's process.
func safeOut(outDir, out string) (string, error) {
cleanDir := path.Clean(outDir)
if cleanDir != "" && cleanDir != "." && !fs.ValidPath(cleanDir) {
return "", fmt.Errorf("%w: outDir %q is not a valid relative slash path", ErrPathEscape, outDir)
}
joined := path.Clean(path.Join(outDir, out))
if !fs.ValidPath(joined) {
return "", fmt.Errorf("%w: out=%q resolves outside outDir", ErrPathEscape, out)
}
if cleanDir == "" || cleanDir == "." {
return joined, nil
}
if joined == cleanDir || strings.HasPrefix(joined, cleanDir+"/") {
return joined, nil
}
return "", fmt.Errorf("%w: out=%q resolves outside outDir %q", ErrPathEscape, out, outDir)
}
func writeFiles(fw pinfs.Writer, outDir string, files []fileContent) ([]string, error) {
var written []string
for _, f := range files {
rel, err := safeOut(outDir, f.out)
if err != nil {
return written, err
}
if err := fw.WriteFile(rel, f.content); err != nil {
return written, err
}
written = append(written, f.out)
}
return written, nil
}
func removeOrphans(fw pinfs.Writer, outDir string, orphans []lock.Asset) ([]string, error) {
var removed []string
for _, a := range orphans {
rel, err := safeOut(outDir, a.Out)
if err != nil {
return removed, err
}
if err := fw.Remove(rel); err != nil {
return removed, fmt.Errorf("remove orphan %s: %w", a.Out, err)
}
removed = append(removed, a.Out)
}
return removed, nil
}
// checkOutCollisions fails closed when two resolved assets share an
// Out path. Hits most often under layout: flat when packages or
// per-entry file lists collide on basename. Reports the first pair so
// the user can fix the manifest before any bytes are written.
func checkOutCollisions(assets []lock.Asset) error {
seen := make(map[string]string, len(assets))
for _, a := range assets {
if prev, ok := seen[a.Out]; ok {
return fmt.Errorf("%w: %q claimed by both %s and %s", ErrPathCollision, a.Out, prev, a.Name)
}
seen[a.Out] = a.Name
}
return nil
}
// checkFrozen fails fast, before any network call, if manifest and
// lockfile disagree.
func checkFrozen(m *manifest.Manifest, prev *lock.Lock, prevVersions map[string]string) error {
if prev == nil {
return fmt.Errorf("%w: no lockfile present; run sync without --frozen first", ErrFrozenDrift)
}
manifestNames := map[string]bool{}
for _, e := range m.Assets {
manifestNames[e.Name] = true
locked := prevVersions[e.Name]
if locked == "" {
return fmt.Errorf("%w: %s is in the manifest but not the lockfile", ErrFrozenDrift, e.Name)
}
if !npm.IsSticky(locked, e.Version) {
return fmt.Errorf("%w: %s is locked at %s which no longer satisfies manifest constraint %q", ErrFrozenDrift, e.Name, locked, e.Version)
}
}
for _, a := range prev.Assets {
if !manifestNames[a.Name] {
return fmt.Errorf("%w: %s is in the lockfile but not the manifest", ErrFrozenDrift, a.Name)
}
}
return nil
}
func lockedVersionsByName(l *lock.Lock) map[string]string {
out := map[string]string{}
if l == nil {
return out
}
for _, a := range l.Assets {
out[a.Name] = a.Version
}
return out
}
func readManifest(path string) (*manifest.Manifest, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer func() { _ = f.Close() }()
return manifest.Read(f)
}
func readLock(path string) (*lock.Lock, error) {
f, err := os.Open(path)
if errors.Is(err, fs.ErrNotExist) {
return nil, nil
}
if err != nil {
return nil, err
}
defer func() { _ = f.Close() }()
return lock.Read(f)
}
func writeLock(fw pinfs.Writer, lockPath string, l *lock.Lock) error {
if !fs.ValidPath(lockPath) {
return fmt.Errorf("%w: lockfile path %q is not a valid relative slash path", ErrPathEscape, lockPath)
}
encoded, err := EncodeLock(l)
if err != nil {
return err
}
return fw.WriteFile(lockPath, encoded)
}
// EncodeLock returns the lockfile bytes for l using the current tool
// name and version.
func EncodeLock(l *lock.Lock) ([]byte, error) {
var buf bytes.Buffer
if err := lock.Write(&buf, l, ToolName, ToolVersion); err != nil {
return nil, err
}
return buf.Bytes(), nil
}