|
| 1 | +package mirror |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "regexp" |
| 8 | + "strings" |
| 9 | + "sync" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/git-pkgs/proxy/internal/handler" |
| 13 | + "github.com/git-pkgs/registries" |
| 14 | +) |
| 15 | + |
| 16 | +// apkIndexTTL is how long a repository's APKINDEX is reused, so mirroring |
| 17 | +// the packages of an image reads each index once. |
| 18 | +const apkIndexTTL = 5 * time.Minute |
| 19 | + |
| 20 | +var ( |
| 21 | + // apkDistroPattern takes the release from syft's distro qualifier, such |
| 22 | + // as alpine-3.20.3 for v3.20. Edge, as alpine-3.21.0_alpha20240807, |
| 23 | + // isn't a release and doesn't match. |
| 24 | + apkDistroPattern = regexp.MustCompile(`^alpine-(\d+)\.(\d+)(?:\.\d+)?$`) |
| 25 | + apkArchPattern = regexp.MustCompile(`^[a-z0-9_]+$`) |
| 26 | + // apkComponents are the repositories of a release a package is looked |
| 27 | + // up in, in order. |
| 28 | + apkComponents = []string{"main", "community"} |
| 29 | +) |
| 30 | + |
| 31 | +// apkIndexes keeps one registries client per repository directory, which |
| 32 | +// holds the parsed APKINDEX. |
| 33 | +type apkIndexes struct { |
| 34 | + mu sync.Mutex |
| 35 | + regs map[string]apkIndex |
| 36 | +} |
| 37 | + |
| 38 | +type apkIndex struct { |
| 39 | + reg registries.Registry |
| 40 | + fetched time.Time |
| 41 | +} |
| 42 | + |
| 43 | +// mirrorAPK mirrors an Alpine package under the path the handler caches it |
| 44 | +// at, {repository}/v{release}/{component}/{arch}/{file}. The PURL namespace |
| 45 | +// names the upstream.apk repository; the distro and arch qualifiers give |
| 46 | +// the release and architecture, and the package is looked up in the main |
| 47 | +// and community indexes of that release. |
| 48 | +func (m *Mirror) mirrorAPK(ctx context.Context, pv PackageVersion, tracker *progressTracker) { |
| 49 | + fail := func(err error) { m.recordFailure(tracker, pv, err) } |
| 50 | + repository, name, ok := strings.Cut(pv.Name, "/") |
| 51 | + if !ok { |
| 52 | + repository, name = "alpine", pv.Name |
| 53 | + } |
| 54 | + repositoryURL, ok := handler.APKRepositories(m.APKRepositories)[repository] |
| 55 | + if !ok { |
| 56 | + fail(fmt.Errorf("unknown APK repository %q: it isn't in upstream.apk", repository)) |
| 57 | + return |
| 58 | + } |
| 59 | + match := apkDistroPattern.FindStringSubmatch(pv.Qualifiers["distro"]) |
| 60 | + if match == nil { |
| 61 | + fail(fmt.Errorf("apk packages need a distro qualifier with the Alpine release, such as distro=alpine-3.20.3, not %q", pv.Qualifiers["distro"])) |
| 62 | + return |
| 63 | + } |
| 64 | + release := "v" + match[1] + "." + match[2] |
| 65 | + arch := pv.Qualifiers["arch"] |
| 66 | + if !apkArchPattern.MatchString(arch) { |
| 67 | + fail(fmt.Errorf("apk packages need an arch qualifier, such as arch=x86_64, not %q", arch)) |
| 68 | + return |
| 69 | + } |
| 70 | + |
| 71 | + for _, component := range apkComponents { |
| 72 | + dir := release + "/" + component + "/" + arch |
| 73 | + artifact, found, err := m.apkArtifact(ctx, repositoryURL+"/"+dir, name, pv.Version) |
| 74 | + if err != nil { |
| 75 | + fail(fmt.Errorf("reading %s/APKINDEX.tar.gz: %w", dir, err)) |
| 76 | + return |
| 77 | + } |
| 78 | + if !found { |
| 79 | + continue |
| 80 | + } |
| 81 | + cacheFilename := handler.APKCacheFilename(repository, dir+"/"+artifact.Filename) |
| 82 | + result, err := m.proxy.GetOrFetchArtifactFromURL(ctx, pv.Ecosystem, name, pv.Version, cacheFilename, artifact.URL) |
| 83 | + if err != nil { |
| 84 | + fail(err) |
| 85 | + return |
| 86 | + } |
| 87 | + m.recordResult(tracker, pv, result) |
| 88 | + return |
| 89 | + } |
| 90 | + fail(fmt.Errorf("%s %s isn't in %s of %s for %s", name, pv.Version, strings.Join(apkComponents, " or "), release, arch)) |
| 91 | +} |
| 92 | + |
| 93 | +// apkArtifact returns the file of version of name that the index in dirURL |
| 94 | +// lists, and whether it lists one. |
| 95 | +func (m *Mirror) apkArtifact(ctx context.Context, dirURL, name, version string) (registries.Artifact, bool, error) { |
| 96 | + reg, err := m.apkRegistry(dirURL) |
| 97 | + if err != nil { |
| 98 | + return registries.Artifact{}, false, err |
| 99 | + } |
| 100 | + versions, err := reg.FetchVersions(ctx, name) |
| 101 | + if isNotFound(err) { |
| 102 | + return registries.Artifact{}, false, nil |
| 103 | + } |
| 104 | + if err != nil { |
| 105 | + m.apk.forget(dirURL) |
| 106 | + return registries.Artifact{}, false, err |
| 107 | + } |
| 108 | + for _, v := range versions { |
| 109 | + if v.Number == version && len(v.Artifacts) > 0 { |
| 110 | + return v.Artifacts[0], true, nil |
| 111 | + } |
| 112 | + } |
| 113 | + return registries.Artifact{}, false, nil |
| 114 | +} |
| 115 | + |
| 116 | +// apkRegistry returns the client for the repository directory at dirURL, |
| 117 | +// reusing it, and the index it read, for apkIndexTTL. |
| 118 | +func (m *Mirror) apkRegistry(dirURL string) (registries.Registry, error) { //nolint:ireturn // registries.New returns the interface |
| 119 | + m.apk.mu.Lock() |
| 120 | + defer m.apk.mu.Unlock() |
| 121 | + if idx, ok := m.apk.regs[dirURL]; ok && time.Since(idx.fetched) < apkIndexTTL { |
| 122 | + return idx.reg, nil |
| 123 | + } |
| 124 | + r, _ := m.Versions.(RegistryVersions) |
| 125 | + // Naming the v2 index reads it also where Packages.adb is served beside |
| 126 | + // it, which the client would try first and refuse. |
| 127 | + reg, err := registries.New("apk", dirURL+"/APKINDEX.tar.gz", r.client()) |
| 128 | + if err != nil { |
| 129 | + return nil, err |
| 130 | + } |
| 131 | + if m.apk.regs == nil { |
| 132 | + m.apk.regs = map[string]apkIndex{} |
| 133 | + } |
| 134 | + m.apk.regs[dirURL] = apkIndex{reg: reg, fetched: time.Now()} |
| 135 | + return reg, nil |
| 136 | +} |
| 137 | + |
| 138 | +// forget drops the client for dirURL, so an index that failed to load is |
| 139 | +// read again next time. |
| 140 | +func (a *apkIndexes) forget(dirURL string) { |
| 141 | + a.mu.Lock() |
| 142 | + defer a.mu.Unlock() |
| 143 | + delete(a.regs, dirURL) |
| 144 | +} |
| 145 | + |
| 146 | +// isNotFound reports whether err means a package or index doesn't exist, |
| 147 | +// such as a release without a community repository for an architecture. |
| 148 | +func isNotFound(err error) bool { |
| 149 | + if errors.Is(err, registries.ErrNotFound) { |
| 150 | + return true |
| 151 | + } |
| 152 | + var httpErr *registries.HTTPError |
| 153 | + return errors.As(err, &httpErr) && httpErr.IsNotFound() |
| 154 | +} |
0 commit comments