|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "cuelabs.dev/go/oci/ociregistry" |
| 10 | + "cuelabs.dev/go/oci/ociregistry/ociclient" |
| 11 | + "cuelabs.dev/go/oci/ociregistry/ocifilter" |
| 12 | + "cuelabs.dev/go/oci/ociregistry/ociref" |
| 13 | + |
| 14 | + "cuelang.org/go/internal/cueexperiment" |
| 15 | +) |
| 16 | + |
| 17 | +func getRegistry() (ociregistry.Interface, error) { |
| 18 | + // TODO document CUE_REGISTRY via a new "cue help environment" subcommand. |
| 19 | + env := os.Getenv("CUE_REGISTRY") |
| 20 | + if !cueexperiment.Flags.Modules { |
| 21 | + if env != "" { |
| 22 | + fmt.Fprintf(os.Stderr, "warning: ignoring CUE_REGISTRY because modules experiment is not enabled. Set CUE_EXPERIMENT=modules to enable it.\n") |
| 23 | + } |
| 24 | + return nil, nil |
| 25 | + } |
| 26 | + if env == "" { |
| 27 | + env = "registry.cuelabs.dev" |
| 28 | + } |
| 29 | + |
| 30 | + host, prefix, insecure, err := parseRegistry(env) |
| 31 | + if err != nil { |
| 32 | + return nil, err |
| 33 | + } |
| 34 | + r, err := ociclient.New(host, &ociclient.Options{ |
| 35 | + Insecure: insecure, |
| 36 | + }) |
| 37 | + if err != nil { |
| 38 | + return nil, fmt.Errorf("cannot make OCI client: %v", err) |
| 39 | + } |
| 40 | + if prefix != "" { |
| 41 | + r = ocifilter.Sub(r, prefix) |
| 42 | + } |
| 43 | + return r, nil |
| 44 | +} |
| 45 | + |
| 46 | +func parseRegistry(env string) (hostPort, prefix string, insecure bool, err error) { |
| 47 | + var suffix string |
| 48 | + if i := strings.LastIndex(env, "+"); i > 0 { |
| 49 | + suffix = env[i:] |
| 50 | + env = env[:i] |
| 51 | + } |
| 52 | + var r ociref.Reference |
| 53 | + if !strings.Contains(env, "/") { |
| 54 | + // OCI references don't allow a host name on its own without a repo, |
| 55 | + // but we do. |
| 56 | + r.Host = env |
| 57 | + if !ociref.IsValidHost(r.Host) { |
| 58 | + return "", "", false, fmt.Errorf("$CUE_REGISTRY %q is not a valid host name", r.Host) |
| 59 | + } |
| 60 | + } else { |
| 61 | + var err error |
| 62 | + r, err = ociref.Parse(env) |
| 63 | + if err != nil { |
| 64 | + return "", "", false, fmt.Errorf("cannot parse $CUE_REGISTRY: %v", err) |
| 65 | + } |
| 66 | + if r.Tag != "" || r.Digest != "" { |
| 67 | + return "", "", false, fmt.Errorf("$CUE_REGISTRY %q cannot have an associated tag or digest", env) |
| 68 | + } |
| 69 | + } |
| 70 | + if suffix == "" { |
| 71 | + if isInsecureHost(r.Host) { |
| 72 | + suffix = "+insecure" |
| 73 | + } else { |
| 74 | + suffix = "+secure" |
| 75 | + } |
| 76 | + } |
| 77 | + switch suffix { |
| 78 | + case "+insecure": |
| 79 | + insecure = true |
| 80 | + case "+secure": |
| 81 | + default: |
| 82 | + return "", "", false, fmt.Errorf("unknown suffix (%q) to CUE_REGISTRY (need +insecure or +secure)", suffix) |
| 83 | + } |
| 84 | + return r.Host, r.Repository, insecure, nil |
| 85 | +} |
| 86 | + |
| 87 | +func isInsecureHost(hostPort string) bool { |
| 88 | + host, _, err := net.SplitHostPort(hostPort) |
| 89 | + if err != nil { |
| 90 | + host = hostPort |
| 91 | + } |
| 92 | + switch host { |
| 93 | + case "localhost", |
| 94 | + "127.0.0.1", |
| 95 | + "::1": |
| 96 | + return true |
| 97 | + } |
| 98 | + // TODO other clients have logic for RFC1918 too, amongst other |
| 99 | + // things. Maybe we should do that too. |
| 100 | + return false |
| 101 | +} |
0 commit comments