Documentation
¶
Overview ¶
Package gems validates RubyGems archives and renders the indexes RubyGems and Bundler read. A gem is one archive, stored at gems/<name>-<version>[-<platform>].gem, whose embedded gemspec names the gem, its version, its platform and its runtime dependencies; that gemspec, read from the stored bytes, is what indexes the version and fills the compact index (names, versions and info/<name>) and the legacy Marshal 4.8 indexes (specs.4.8.gz, its latest and prerelease forms, and quick/Marshal.4.8/<file>.gemspec.rz). A local or federated repository stores no index file: the protocol renders them from the stored gems.
Role ¶
artifacts module, ecosystem layer: the RubyGems package, index and Marshal rules of the RubyGems slice. Its Handler plugs into the item service through the format hooks (decision 0032), so every write in a gems repository reaches it whichever API carried it; the slice's http package (artifacts/ecosystem/gems/http) serves the RubyGems and Bundler protocol over what it keeps.
Inputs ¶
- Driving: the format registry (artifacts/ecosystem), which hands the item service's hooks for gems repositories to Handler, and the work queue, which runs every format's reindex through it (JFrog's /api/gems/{repoKey}/reindex schedules one); and the protocol, which reads pushed, stored and upstream gems (Read, MaxGemBytes), lists a repository's stored files (Files), checks names and paths (ValidName, ValidPath, Filename), renders the indexes (CompactNames, CompactVersions, CompactInfo, InfoMD5, Specs and QuickSpec) and checks an upstream's compact index before a remote caches it (ParseCompactVersions, ParseCompactInfo, VersionToken and MaxIndexBytes).
- Driven: the format hooks' environment (format.Env): its blobs hold the gems, and its store holds the items and the package index (artifacts/core/pkgindex), which Reindex rebuilds from an AQL search of the stored files (Files). The generic deploy policy (artifacts/ecosystem/generic), which Handler embeds, is the first check Admit applies.
- Settings: none.
Outputs ¶
- Handler: the hooks that read a gem once before its transaction (Inspect), admit only a valid gem whose identity gives its path and keep the index paths out of local and federated repositories (Admit), link a stored gem to its version (Deployed), class the index paths as derived content (Classify) and rebuild the index from the stored gems (Reindex). Deletes need no hook: the item service drops a deleted gem's link and prunes a version left without files.
- The package index: a package per gem name, with a version per version string, whose files are its gems for every platform.
- The rules the protocol shares: Package, Dependency, Read, ValidName, ValidVersion, ValidPath, Filename, DerivedPath, Files, CompareVersions, the index renderers and parsers with CompactVersionEntry, Marshal, GzipMarshal and the bounds (MaxGemBytes and MaxIndexBytes).
Invariants ¶
- A gem is read whole within fixed bounds (256 MiB, 2 MiB for its unpacked gemspec and checksums, and four times 256 MiB for its unpacked data) before its gemspec is trusted. It is a TAR holding metadata.gz and data.tar.gz, and optionally checksums.yaml.gz, each a regular file once; the data holds only regular files, folders and symlinks, with no absolute or traversing path or link target; the checksums, when present, give the SHA-256 of both other parts; and the gemspec is a YAML Gem::Specification.
- A gem's name, version and platform give its path, with no platform suffix for ruby; only its runtime dependencies are indexed.
- The index paths (DerivedPath) of a local or federated repository are generated, so a write to one is refused (409).
- An upstream's compact index is taken only in bounded, complete rows (32 MiB a file) whose names, versions, platforms, dependencies and SHA-256 checksums validate; a yanked version (a token starting "-") leaves the versions list.
- The index is derived from the stored gems, never from a client's claim.
References ¶
- Decision 0032: docs/research/decisions/0032-package-format-handlers.md (the format hooks); decision 0007: docs/research/decisions/0007-remote-repositories.md (remote repositories, whose upstream index and gems this package checks).
- docs/testing/gems.md (connecting gem and Bundler, the archive and index checks, and the evidence).
- JFrog RubyGems repositories: https://docs.jfrog.com/artifactory/docs/rubygems-repositories, and the RubyGems compact index specification: https://guides.rubygems.org/rubygems-org-compact-index-api/.
- Target layout: docs/research/architecture-options/5-modular-monolith.md, line 233 (ecosystem/gems), line 379 (the slice row) and line 997 (the mapping row).
- Plan: docs/implementation/plan.md#p30-language-package-repositories and #p9d-modular-monolith-migration, epic ECO (https://github.com/weaveplatform/artifactweave/issues/943).
- https://github.com/weaveplatform/artifactweave/issues/764 and https://github.com/weaveplatform/artifactweave/issues/1049
Status ¶
Implemented P9c, which delivered the plan's P30 group (issues #762 to #768). Moved here in P9d.
Testing ¶
- package_test.go: the pinned rake gem, the archive bounds, unsafe data, malformed archives and gemspecs, optional gemspec shapes, runtime dependencies, the compact index round trip and version order; and Ruby loading the Marshal indexes and gemspecs (TestAccRubyClientLoadsMarshalIndexes, TestAccRubyClientLoadsRichGemspec and TestAccRubyClientLoadsDefaultPlatformAndRequirements).
- coverage_test.go: live versions and yanks, dependencies and platforms, malformed wire rows, Ruby's version order, admission that protects the generated indexes and a gem's identity, reindex of the stored gems, and latest_specs.4.8.gz keeping a release when a newer prerelease exists (TestAccRubyLatestSpecsRetainsStableWhenPrereleaseIsNewer).
The Ruby tests skip unless ruby is installed.
Index ¶
- Constants
- func CompactInfo(name string, packages []Package) []byte
- func CompactNames(packages []Package) []byte
- func CompactVersions(packages []Package) []byte
- func CompareVersions(a, b string) int
- func DerivedPath(p string) bool
- func Filename(p Package) string
- func Files(ctx context.Context, st format.Store, c repo.Config) ([]model.Item, error)
- func GzipMarshal(v any) ([]byte, error)
- func InfoMD5(body []byte) string
- func Marshal(v any) []byte
- func ParseCompactVersions(b []byte) (map[string]CompactVersionEntry, error)
- func QuickSpec(p Package, at time.Time) ([]byte, error)
- func Serves(t repo.PackageType) bool
- func Specs(packages []Package, latest, prerelease bool) ([]byte, error)
- func ValidName(name string) bool
- func ValidPath(path string) bool
- func ValidVersion(version string) bool
- func VersionToken(p Package) string
- type CompactVersionEntry
- type Dependency
- type Handler
- func (Handler) Admit(ctx context.Context, env format.Env, c repo.Config, w format.Write) (format.Write, error)
- func (Handler) Classify(_ repo.Config, p string) format.Class
- func (Handler) Deployed(ctx context.Context, env format.Env, c repo.Config, it model.Item) error
- func (Handler) Inspect(ctx context.Context, env format.Env, _ repo.Config, w format.Write) (any, error)
- func (Handler) Reindex(ctx context.Context, env format.Env, c repo.Config) error
- type Package
Constants ¶
const ( // MaxGemBytes bounds a gem at 256 MiB, however it arrives: a push, an // item write or a remote's fetch. Its unpacked data may reach four // times as much. MaxGemBytes = 256 << 20 )
const MaxIndexBytes = 32 << 20
MaxIndexBytes bounds a compact index file, versions or info/<name>, at 32 MiB, whether a parser reads it or a remote fetches it.
Variables ¶
This section is empty.
Functions ¶
func CompactInfo ¶
CompactInfo renders the compact index's info file for the gem name from the rows of packages that name it: "---", then a line per version and platform, ordered by CompareVersions and then platform. A line is the version token (VersionToken), the runtime dependencies as name:requirements, then "|" and the gem's SHA-256 as checksum, with any ruby and rubygems requirements; several requirements of one field are joined with "&".
func CompactNames ¶
CompactNames renders the compact index's names file: "---", then each distinct gem name in packages, one a line, sorted.
func CompactVersions ¶
CompactVersions renders the compact index's versions file: a fixed created_at line, "---", then a line per gem name, sorted, of its distinct version tokens (VersionToken), sorted, and the MD5 of its info file (InfoMD5 of CompactInfo), which a client compares with the info file it fetches.
func CompareVersions ¶
CompareVersions compares the numeric and prerelease parts used by RubyGems. A release ranks above a prerelease at the same numeric version.
func DerivedPath ¶
DerivedPath reports whether p is one of the index paths the protocol generates: names, versions, specs.4.8.gz, latest_specs.4.8.gz, prerelease_specs.4.8.gz, or a path under info/ or quick/Marshal.4.8/.
func Filename ¶
Filename is the file name p's gem is stored under, <name>-<version>[-<platform>].gem, with no platform when it is ruby or empty.
func Files ¶
Files lists every file, gem or not, in the tree the package index reads (pkgindex.Stored: a remote repository's cache, and any other repository's own items), through one AQL search of the store.
func GzipMarshal ¶
GzipMarshal returns Marshal(v) compressed with gzip, the form of the specs.4.8.gz indexes.
func Marshal ¶
Marshal encodes v in Ruby's Marshal 4.8 format, which the legacy indexes carry. It supports nil, bool, int, string, []any, []string, map[string]string and the Ruby objects this package builds (Gem::Version, Gem::Requirement, Gem::Dependency and Gem::Specification), and panics on any other type.
func ParseCompactVersions ¶
func ParseCompactVersions(b []byte) (map[string]CompactVersionEntry, error)
ParseCompactVersions accepts only bounded, complete index rows. The MD5 is checked against each info file before virtual metadata can disclose it.
func QuickSpec ¶
QuickSpec renders p as a quick/Marshal.4.8/<file>.gemspec.rz body: a Marshal 4.8 Gem::Specification dated at, compressed with zlib.
func Serves ¶
func Serves(t repo.PackageType) bool
Serves reports whether t is the RubyGems package type.
func Specs ¶
Specs renders a legacy specs index of packages as gzip-compressed Marshal 4.8: a [name, version, platform] row for each release, or for each prerelease (a version holding a letter) when prerelease is true. When latest is true, only the newest of each name and platform is kept. The protocol serves specs.4.8.gz with neither, latest_specs.4.8.gz with latest and prerelease_specs.4.8.gz with prerelease.
func ValidName ¶
ValidName reports whether name is a gem name: 1 to 128 letters, digits, dots, underscores and hyphens, starting with a letter or digit, with no "..".
func ValidPath ¶
ValidPath reports whether path is a gem's path, gems/<base>.gem one level below gems/, where base is 1 to 128 letters, digits, dots, underscores and hyphens, starting with a letter or digit, with no "..".
func ValidVersion ¶
ValidVersion reports whether version is a gem version of at most 128 bytes: digits, then any dot-separated runs of letters and digits.
func VersionToken ¶
VersionToken is p's token in the compact index: its version, followed by -<platform> unless the platform is ruby or empty.
Types ¶
type CompactVersionEntry ¶
CompactVersionEntry binds the listed versions to their compact info body.
type Dependency ¶
Dependency is one runtime dependency of a gem: the gem it names and its requirements, joined with ", " (">= 1.0, < 2").
type Handler ¶
Handler is the RubyGems format's hooks: it admits a gem only when it reads as valid and its identity gives its path, keeps the generated index paths out of local and federated repositories, and indexes each stored gem as a version of its name. It embeds the generic hooks for deletes, which need nothing more: the item service drops a deleted gem's link and prunes a version left without files.
func (Handler) Admit ¶
func (Handler) Admit( ctx context.Context, env format.Env, c repo.Config, w format.Write, ) (format.Write, error)
Admit applies the generic deploy policy, wrapping its refusal, then passes a checksum deploy. In a local or federated repository, a write to a generated index path (DerivedPath) is refused (409), since the protocol renders those from the stored gems. Any other write passes unless it is to a .gem path, which must be a valid gem path (400) holding a valid gem whose identity gives that path (400). It reuses Inspect's reading when it covers the same path and content, and reads the stored gem itself otherwise.
func (Handler) Classify ¶
Classify classes the generated index paths (DerivedPath) as derived, so replication and federation skip them and each side renders its own, and every other path as unique content.
func (Handler) Deployed ¶
Deployed links a stored gem at a valid gem path (ValidPath) to its version in the package index, creating the package and version when they are new, with the name and version read from the stored gem. Folders and files at other paths change nothing; a gem that no longer reads as valid fails with 400.
func (Handler) Inspect ¶
func (Handler) Inspect( ctx context.Context, env format.Env, _ repo.Config, w format.Write, ) (any, error)
Inspect reads and validates the stored gem of a write to a .gem path before the write's transaction, so Admit need not read it again. A checksum deploy and any other path inspect nothing. A write without content, or of a gem that does not read as valid or whose identity does not give its path, is refused with 400; a gem that cannot be opened fails the write.
func (Handler) Reindex ¶
Reindex rebuilds the package index from the stored gems Files finds: a package per gem name, with a version per version string whose files are its gems for every platform and whose creation time is the earliest of theirs. Files at other paths are skipped, but a gem that does not read as valid fails the reindex with 400.
type Package ¶
type Package struct {
Name string `json:"name"`
Version string `json:"version"`
Platform string `json:"platform"`
Summary string `json:"summary"`
Description string `json:"description"`
Homepage string `json:"homepage"`
Authors []string `json:"authors"`
Licenses []string `json:"licenses"`
Ruby string `json:"ruby"`
RubyGems string `json:"rubygems"`
Dependencies []Dependency `json:"dependencies"`
Path string `json:"path"`
SHA256 string `json:"sha256"`
}
Package is what a gem's gemspec and archive say of it: its name, version and platform (ruby when the gemspec names none), the descriptive fields the indexes serve, its ruby and rubygems requirements and runtime dependencies, and the gem's path and SHA-256.
func ParseCompactInfo ¶
ParseCompactInfo parses an upstream's info file for the gem name, of at most MaxIndexBytes, taking only complete, well-formed rows: a valid version and platform, dependencies on valid names, a 64-digit hexadecimal checksum, and no field after "|" but checksum, ruby, rubygems and created_at. Each row becomes a Package with its Path set; a row whose gem path is invalid or repeats an earlier one fails the whole file.
func Read ¶
Read reads a whole gem of size bytes, at most MaxGemBytes, from r and returns what its gemspec says, with the gem's SHA-256. The gem must be a TAR holding metadata.gz and data.tar.gz, and optionally checksums.yaml.gz, each a regular file once. The data must hold only regular files, folders and symlinks, with no absolute or traversing path or link target, of at most four times MaxGemBytes unpacked; the checksums, when present, must give the SHA-256 of both other parts and nothing else; and the gemspec, at most 2 MiB unpacked, must be a YAML Gem::Specification whose name, version, platform and runtime dependency names are valid and, when path is not empty, give path. Development dependencies are dropped. A refusal names its fault; when ctx ends, the error wraps ctx's.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package gemshttp serves RubyGems and Bundler's native repository protocol on Artifactory's /artifactory/api/gems/{repo} prefix, on gems local, federated, remote and virtual repositories: gem push and yank, the gems themselves, the compact index Bundler reads, the legacy Marshal indexes gem reads, and the gem, versions and search JSON.
|
Package gemshttp serves RubyGems and Bundler's native repository protocol on Artifactory's /artifactory/api/gems/{repo} prefix, on gems local, federated, remote and virtual repositories: gem push and yank, the gems themselves, the compact index Bundler reads, the legacy Marshal indexes gem reads, and the gem, versions and search JSON. |