File: resolve.go

package info (click to toggle)
golang-github-cue-lang-cue 0.12.0.-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 19,072 kB
  • sloc: sh: 57; makefile: 17
file content (533 lines) | stat: -rw-r--r-- 15,650 bytes parent folder | download
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
// Copyright 2024 CUE 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 modresolve

import (
	"cmp"
	"crypto/sha256"
	_ "embed"
	"fmt"
	"net"
	"net/netip"
	"path"
	"slices"
	"strings"
	"sync"

	"cuelabs.dev/go/oci/ociregistry/ociref"

	"cuelang.org/go/cue"
	"cuelang.org/go/cue/cuecontext"
	"cuelang.org/go/cue/errors"
	"cuelang.org/go/cue/token"
	"cuelang.org/go/mod/module"
)

// pathEncoding represents one of the possible types of
// encoding for module paths within a registry.
// It reflects the #registry.pathEncoding disjunction
// in schema.cue.
// TODO it would be nice if this could be auto-generated
// from the schema.
type pathEncoding string

const (
	encPath       pathEncoding = "path"
	encHashAsRepo pathEncoding = "hashAsRepo"
	encHashAsTag  pathEncoding = "hashAsTag"
)

// LocationResolver resolves module paths to a location
// consisting of a host name of a registry and where
// in that registry the module is to be found.
//
// Note: The implementation in this package operates entirely lexically,
// which is why [Location] contains only a host name and not an actual
// [ociregistry.Interface] implementation.
type LocationResolver interface {
	// ResolveToLocation resolves a base module path (without a version
	// suffix, a.k.a. OCI repository name) and optional version to
	// the location for that path. It reports whether it can find
	// appropriate location for the module.
	//
	// If the version is empty, the Tag in the returned Location
	// will hold the prefix that all versions of the module in its
	// repository have. That prefix will be followed by the version
	// itself.
	ResolveToLocation(path string, vers string) (Location, bool)

	// AllHosts returns all the registry hosts that the resolver
	// might resolve to, ordered lexically by hostname.
	AllHosts() []Host
}

// Host represents a registry host name.
type Host struct {
	// Name holds the IP host name of the registry.
	// If it's an IP v6 address, it will be surrounded with
	// square brackets ([, ]).
	Name string
	// Insecure holds whether this host should be connected
	// to insecurely (with an HTTP rather than HTTP connection).
	Insecure bool
}

// Location represents the location for a given module version or versions.
type Location struct {
	// Host holds the host or host:port of the registry.
	Host string

	// Insecure holds whether an insecure connection
	// should be used when connecting to the registry.
	Insecure bool

	// Repository holds the repository to store the module in.
	Repository string

	// Tag holds the tag for the module version.
	// If an empty version was passed to
	// Resolve, it holds the prefix shared by all version
	// tags for the module.
	Tag string
}

// config mirrors the #File definition in schema.cue.
// TODO it would be nice to be able to generate this
// type directly from the schema.
type config struct {
	ModuleRegistries map[string]*registryConfig `json:"moduleRegistries,omitempty"`
	DefaultRegistry  *registryConfig            `json:"defaultRegistry,omitempty"`
}

func (cfg *config) init() error {
	for prefix, reg := range cfg.ModuleRegistries {
		if err := module.CheckPathWithoutVersion(prefix); err != nil {
			return fmt.Errorf("invalid module path %q: %v", prefix, err)
		}
		if err := reg.init(); err != nil {
			return fmt.Errorf("invalid registry configuration in %q: %v", prefix, err)
		}
	}
	if cfg.DefaultRegistry != nil {
		if err := cfg.DefaultRegistry.init(); err != nil {
			return fmt.Errorf("invalid default registry configuration: %v", err)
		}
	}
	return nil
}

type registryConfig struct {
	Registry      string       `json:"registry,omitempty"`
	PathEncoding  pathEncoding `json:"pathEncoding,omitempty"`
	PrefixForTags string       `json:"prefixForTags,omitempty"`
	StripPrefix   bool         `json:"stripPrefix,omitempty"`

	// The following fields are filled in from Registry after parsing.
	none       bool
	host       string
	repository string
	insecure   bool
}

func (r *registryConfig) init() error {
	r1, err := parseRegistry(r.Registry)
	if err != nil {
		return err
	}
	r.none, r.host, r.repository, r.insecure = r1.none, r1.host, r1.repository, r1.insecure

	if r.PrefixForTags != "" {
		if !ociref.IsValidTag(r.PrefixForTags) {
			return fmt.Errorf("invalid tag prefix %q", r.PrefixForTags)
		}
	}
	if r.PathEncoding == "" {
		// Shouldn't happen because default should apply.
		return fmt.Errorf("empty pathEncoding")
	}
	if r.StripPrefix {
		if r.PathEncoding != encPath {
			// TODO we could relax this to allow storing of naked tags
			// when the module path matches exactly and hash tags
			// otherwise.
			return fmt.Errorf("cannot strip prefix unless using path encoding")
		}
		if r.repository == "" {
			return fmt.Errorf("use of stripPrefix requires a non-empty repository within the registry")
		}
	}
	return nil
}

var (
	configSchemaOnce sync.Once // guards the creation of _configSchema
	// TODO remove this mutex when https://cuelang.org/issue/2733 is fixed.
	configSchemaMutex sync.Mutex // guards any use of _configSchema
	_configSchema     cue.Value
)

//go:embed schema.cue
var configSchemaData []byte

// RegistryConfigSchema returns the CUE schema
// for the configuration parsed by [ParseConfig].
func RegistryConfigSchema() string {
	// Cut out the copyright header and the header that's
	// not pure schema.
	schema := string(configSchemaData)
	i := strings.Index(schema, "\n// #file ")
	if i == -1 {
		panic("no file definition found in schema")
	}
	i++
	return schema[i:]
}

// ParseConfig parses the registry configuration with the given contents and file name.
// If there is no default registry, then the single registry specified in catchAllDefault
// will be used as a default.
func ParseConfig(configFile []byte, filename string, catchAllDefault string) (LocationResolver, error) {
	configSchemaOnce.Do(func() {
		ctx := cuecontext.New()
		schemav := ctx.CompileBytes(configSchemaData, cue.Filename("cuelang.org/go/internal/mod/modresolve/schema.cue"))
		schemav = schemav.LookupPath(cue.MakePath(cue.Def("#file")))
		if err := schemav.Validate(); err != nil {
			panic(fmt.Errorf("internal error: invalid CUE registry config schema: %v", errors.Details(err, nil)))
		}
		_configSchema = schemav
	})
	configSchemaMutex.Lock()
	defer configSchemaMutex.Unlock()

	v := _configSchema.Context().CompileBytes(configFile, cue.Filename(filename))
	if err := v.Err(); err != nil {
		return nil, errors.Wrapf(err, token.NoPos, "invalid registry configuration file")
	}
	v = v.Unify(_configSchema)
	if err := v.Err(); err != nil {
		return nil, errors.Wrapf(err, token.NoPos, "invalid configuration file")
	}
	var cfg config
	if err := v.Decode(&cfg); err != nil {
		return nil, errors.Wrapf(err, token.NoPos, "internal error: cannot decode into registry config struct")
	}
	if err := cfg.init(); err != nil {
		return nil, err
	}
	if cfg.DefaultRegistry == nil {
		if catchAllDefault == "" {
			return nil, fmt.Errorf("no default catch-all registry provided")
		}
		// TODO is it too limiting to have the catch-all registry specified as a simple string?
		reg, err := parseRegistry(catchAllDefault)
		if err != nil {
			return nil, fmt.Errorf("invalid catch-all registry %q: %v", catchAllDefault, err)
		}
		cfg.DefaultRegistry = reg
	}
	r := &resolver{
		cfg: cfg,
	}
	if err := r.initHosts(); err != nil {
		return nil, err
	}
	return r, nil
}

// ParseCUERegistry parses a registry routing specification that
// maps module prefixes to the registry that should be used to
// fetch that module.
//
// The specification consists of an order-independent, comma-separated list.
//
// Each element either maps a module prefix to the registry that will be used
// for all modules that have that prefix (prefix=registry), or a catch-all registry to be used
// for modules that do not match any prefix (registry).
//
// For example:
//
//	myorg.com=myregistry.com/m,catchallregistry.example.org
//
// Any module with a matching prefix will be routed to the given registry.
// A prefix only matches whole path elements.
// In the above example, module myorg.com/foo/bar@v0 will be looked up
// in myregistry.com in the repository m/myorg.com/foo/bar,
// whereas github.com/x/y will be looked up in catchallregistry.example.com.
//
// The registry part is syntactically similar to a [docker reference]
// except that the repository is optional and no tag or digest is allowed.
// Additionally, a +secure or +insecure suffix may be used to indicate
// whether to use a secure or insecure connection. Without that,
// localhost, 127.0.0.1 and [::1] will default to insecure, and anything
// else to secure.
//
// If s does not declare a catch-all registry location, catchAllDefault is
// used. It is an error if s fails to declares a catch-all registry location
// and no catchAllDefault is provided.
//
// [docker reference]: https://pkg.go.dev/github.com/distribution/reference
func ParseCUERegistry(s string, catchAllDefault string) (LocationResolver, error) {
	if s == "" && catchAllDefault == "" {
		return nil, fmt.Errorf("no catch-all registry or default")
	}
	if s == "" {
		s = catchAllDefault
	}
	cfg := config{
		ModuleRegistries: make(map[string]*registryConfig),
	}
	parts := strings.Split(s, ",")
	for _, part := range parts {
		key, val, ok := strings.Cut(part, "=")
		if !ok {
			if part == "" {
				// TODO or just ignore it?
				return nil, fmt.Errorf("empty registry part")
			}
			if _, ok := cfg.ModuleRegistries[""]; ok {
				return nil, fmt.Errorf("duplicate catch-all registry")
			}
			key, val = "", part
		} else {
			if key == "" {
				return nil, fmt.Errorf("empty module prefix")
			}
			if val == "" {
				return nil, fmt.Errorf("empty registry reference")
			}
			if err := module.CheckPathWithoutVersion(key); err != nil {
				return nil, fmt.Errorf("invalid module path %q: %v", key, err)
			}
			if _, ok := cfg.ModuleRegistries[key]; ok {
				return nil, fmt.Errorf("duplicate module prefix %q", key)
			}
		}
		reg, err := parseRegistry(val)
		if err != nil {
			return nil, fmt.Errorf("invalid registry %q: %v", val, err)
		}
		cfg.ModuleRegistries[key] = reg
	}
	if _, ok := cfg.ModuleRegistries[""]; !ok {
		if catchAllDefault == "" {
			return nil, fmt.Errorf("no default catch-all registry provided")
		}
		reg, err := parseRegistry(catchAllDefault)
		if err != nil {
			return nil, fmt.Errorf("invalid catch-all registry %q: %v", catchAllDefault, err)
		}
		cfg.ModuleRegistries[""] = reg
	}
	cfg.DefaultRegistry = cfg.ModuleRegistries[""]
	delete(cfg.ModuleRegistries, "")

	r := &resolver{
		cfg: cfg,
	}
	if err := r.initHosts(); err != nil {
		return nil, err
	}
	return r, nil
}

type resolver struct {
	allHosts []Host
	cfg      config
}

func (r *resolver) initHosts() error {
	hosts := make(map[string]bool)
	addHost := func(reg *registryConfig) error {
		if reg.none {
			return nil
		}
		if insecure, ok := hosts[reg.host]; ok {
			if insecure != reg.insecure {
				return fmt.Errorf("registry host %q is specified both as secure and insecure", reg.host)
			}
		} else {
			hosts[reg.host] = reg.insecure
		}
		return nil
	}
	for _, reg := range r.cfg.ModuleRegistries {
		if err := addHost(reg); err != nil {
			return err
		}
	}

	if reg := r.cfg.DefaultRegistry; reg != nil {
		if err := addHost(reg); err != nil {
			return err
		}
	}
	allHosts := make([]Host, 0, len(hosts))
	for host, insecure := range hosts {
		allHosts = append(allHosts, Host{
			Name:     host,
			Insecure: insecure,
		})
	}
	slices.SortFunc(allHosts, func(a, b Host) int {
		return cmp.Compare(a.Name, b.Name)
	})
	r.allHosts = allHosts
	return nil
}

// AllHosts implements Resolver.AllHosts.
func (r *resolver) AllHosts() []Host {
	return r.allHosts
}

func (r *resolver) ResolveToLocation(mpath, vers string) (Location, bool) {
	if mpath == "" {
		return Location{}, false
	}
	bestMatch := ""
	// Note: there's always a wildcard match.
	bestMatchReg := r.cfg.DefaultRegistry
	for pat, reg := range r.cfg.ModuleRegistries {
		if pat == mpath {
			bestMatch = pat
			bestMatchReg = reg
			break
		}
		if !strings.HasPrefix(mpath, pat) {
			continue
		}
		if len(bestMatch) > len(pat) {
			// We've already found a more specific match.
			continue
		}
		if mpath[len(pat)] != '/' {
			// The path doesn't have a separator at the end of
			// the prefix, which means that it doesn't match.
			// For example, foo.com/bar does not match foo.com/ba.
			continue
		}
		// It's a possible match but not necessarily the longest one.
		bestMatch, bestMatchReg = pat, reg
	}
	reg := bestMatchReg
	if reg == nil || reg.none {
		return Location{}, false
	}
	loc := Location{
		Host:     reg.host,
		Insecure: reg.insecure,
		Tag:      vers,
	}
	switch reg.PathEncoding {
	case encPath:
		if reg.StripPrefix {
			mpath = strings.TrimPrefix(mpath, bestMatch)
			mpath = strings.TrimPrefix(mpath, "/")
		}
		loc.Repository = path.Join(reg.repository, mpath)
	case encHashAsRepo:
		loc.Repository = fmt.Sprintf("%s/%x", reg.repository, sha256.Sum256([]byte(mpath)))
	case encHashAsTag:
		loc.Repository = reg.repository
	default:
		panic("unreachable")
	}
	if reg.PathEncoding == encHashAsTag {
		loc.Tag = fmt.Sprintf("%s%x-%s", reg.PrefixForTags, sha256.Sum256([]byte(mpath)), vers)
	} else {
		loc.Tag = reg.PrefixForTags + vers
	}
	return loc, true
}

func parseRegistry(env0 string) (*registryConfig, error) {
	if env0 == "none" {
		return &registryConfig{
			Registry: env0,
			none:     true,
		}, nil
	}
	env := env0
	var suffix string
	if i := strings.LastIndex(env, "+"); i > 0 {
		suffix = env[i:]
		env = env[:i]
	}
	var r ociref.Reference
	if !strings.Contains(env, "/") {
		// OCI references don't allow a host name on its own without a repo,
		// but we do.
		r.Host = env
		if !ociref.IsValidHost(r.Host) {
			return nil, fmt.Errorf("invalid host name %q in registry", r.Host)
		}
	} else {
		var err error
		r, err = ociref.Parse(env)
		if err != nil {
			return nil, err
		}
		if r.Tag != "" || r.Digest != "" {
			return nil, fmt.Errorf("cannot have an associated tag or digest")
		}
	}
	if suffix == "" {
		if isInsecureHost(r.Host) {
			suffix = "+insecure"
		} else {
			suffix = "+secure"
		}
	}
	insecure := false
	switch suffix {
	case "+insecure":
		insecure = true
	case "+secure":
	default:
		return nil, fmt.Errorf("unknown suffix (%q), need +insecure, +secure or no suffix)", suffix)
	}
	return &registryConfig{
		Registry:     env0,
		PathEncoding: encPath,
		host:         r.Host,
		repository:   r.Repository,
		insecure:     insecure,
	}, nil
}

var (
	ipV4Localhost = netip.MustParseAddr("127.0.0.1")
	ipV6Localhost = netip.MustParseAddr("::1")
)

func isInsecureHost(hostPort string) bool {
	host, _, err := net.SplitHostPort(hostPort)
	if err != nil {
		host = hostPort
		if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") {
			host = host[1 : len(host)-1]
		}
	}
	if host == "localhost" {
		return true
	}
	addr, err := netip.ParseAddr(host)
	if err != nil {
		return false
	}
	// TODO other clients have logic for RFC1918 too, amongst other
	// things. Maybe we should do that too.
	return addr == ipV4Localhost || addr == ipV6Localhost
}