Documentation
¶
Overview ¶
Package p2 is the grammar of Eclipse p2 repositories as an update site proxy needs it: the layout of a repository (metadata documents, bundles, features, binaries), the composite repositories that point at other repositories, and the rewriting of the documents that would otherwise send a client around the proxy (the mirror and statistics URLs of artifacts.xml, absolute child locations of a composite repository).
A p2 repository is a directory of static files:
p2.index which metadata files exist content.(jar|xml|xml.xz) installable units artifacts.(jar|xml|xml.xz) artifact descriptors and download rules compositeContent.(jar|xml|xml.xz) composite: child repositories compositeArtifacts.(jar|xml|xml.xz) plugins/<id>_<version>.jar OSGi bundles (also .pack.gz, legacy) features/<id>_<version>.jar features binary/<id>_<version> other artifacts
Nothing here talks to the network.
Index ¶
- Constants
- Variables
- func Coordinates(key string) (string, string, bool)
- func IsArtifact(key string) bool
- func IsMetadata(key string) bool
- func RewriteXML(dst io.Writer, src io.Reader, opts Options) error
- func UpstreamURL(upstream, key string) (string, bool)
- type Codec
- type Doc
- type Options
- type Relocator
Constants ¶
const HostPrefix = "_host"
HostPrefix is the first segment of the keys that address the upstream host outside the proxied base URL. A composite repository may name children in a sibling directory ("../../technology/epp/packages/2024-12/"); the proxy serves them under "_host/<path on the upstream host>" so that the client stays inside the proxy.
const ( // MaxXMLBytes bounds the XML of one document after decompression. A real // artifacts.xml of a large update site is some tens of MiB. MaxXMLBytes int64 = 512 << 20 )
Limits of the documents the proxy rewrites. The documents come from an upstream the operator trusts to publish software, not to be benign: every bound below holds for hostile input.
Variables ¶
var ( // ErrBadArchive is returned for a jar that is not the plain archive of one // XML document. ErrBadArchive = errors.New("p2: unusable metadata archive") // ErrNoEntry is returned when a jar lacks the XML entry its name promises. ErrNoEntry = errors.New("p2: archive has no document entry") )
var ( // ErrNotRepository is returned for a document that is not a p2 repository // (a captive portal page that parses as XML, a second root element). ErrNotRepository = errors.New("p2: not a repository document") // ErrDirective is returned for a DTD or any other <!...> directive. p2 // documents have none, and refusing them keeps entity tricks out. ErrDirective = errors.New("p2: directive in a repository document") // ErrTooDeep is returned for elements nested deeper than p2 documents are. ErrTooDeep = errors.New("p2: document nests too deeply") // ErrTokenTooLarge is returned for a single XML token over its bound. ErrTokenTooLarge = errors.New("p2: XML token too large") // ErrTruncated is returned for a document that ends inside an element. ErrTruncated = errors.New("p2: document ends inside an element") // ErrTooLarge is returned for a document whose XML exceeds MaxXMLBytes. ErrTooLarge = errors.New("p2: document too large") )
var ErrBadURL = errors.New("p2: not an absolute http(s) URL")
ErrBadURL is returned for an upstream or document URL that is not an absolute http(s) URL.
Functions ¶
func Coordinates ¶
Coordinates recovers the id and version of a bundle or feature from the key of its file ("plugins/org.eclipse.core.runtime_3.31.0.v20240101.jar"); ok is false for anything else, binaries included (their names carry no extension, and an id there is a classifier path, not a bundle).
func IsArtifact ¶
IsArtifact reports whether key is an artifact: a file of a plugins, features or binary directory. Artifacts are named by id and version, so they never change once published.
func IsMetadata ¶
IsMetadata reports whether key names a repository metadata file (p2.index, content, artifacts and composite documents, with their checksum files).
func RewriteXML ¶
RewriteXML copies the p2 repository document in src to dst with the repository properties p2.mirrorsURL and p2.statsURI removed (the size of the properties element follows) and the child locations of a composite repository relocated through opts. Everything else is copied byte for byte: the rewriter tracks the offsets of the tokens it reads and passes the bytes between them through, so attribute quoting, whitespace and comments survive.
It reads src once, holding one token at a time, and refuses what a repository document never contains: a root other than <repository>, a directive (DTD), nesting beyond maxDepth, a token over maxTokenBytes, a document that ends inside an element. The decoder does not expand entities, so a document cannot grow in the rewriter; the caller bounds the length of src.
func UpstreamURL ¶
UpstreamURL maps a key of the proxy to the URL it is fetched from: the key below the upstream URL, or for a HostPrefix key the path on the upstream host. ok is false when key addresses the host namespace with nothing below it.
Types ¶
type Doc ¶
type Doc struct {
// Name is the base name of the document without extension ("artifacts",
// "compositeContent", ...); Name + ".xml" is the entry of a jar.
Name string
Codec Codec
// Composite is set for the composite repositories, whose children point at
// other repositories.
Composite bool
}
Doc is a metadata document the proxy rewrites before it caches it.
func ParseDoc ¶
ParseDoc recognises the rewritten metadata documents by the last segment of a key: artifacts, compositeArtifacts and compositeContent in the three encodings.
func (Doc) Rewrite ¶
Rewrite decodes the document d stored in src (size bytes), rewrites it with RewriteXML and writes it to dst in the same encoding. A jar is rewritten into a jar of one deflated entry; an xz document into one xz stream.
Hardening: the decoded XML is bounded by MaxXMLBytes whatever the encoding declares (a bomb is refused, not half-read), a jar is refused when its directory announces more than maxJarEntries entries (before anything is allocated for them) or has no entry of the document's name, and an xz stream that asks for a dictionary over the pkgformat limit, or holds more than one stream, is refused.
type Options ¶
type Options struct {
// Relocate maps the location of a composite repository's child (see
// Relocator.Relocate); nil leaves the children alone.
Relocate func(location string) (string, bool)
}
Options tune RewriteXML.
type Relocator ¶
type Relocator struct {
// contains filtered or unexported fields
}
Relocator maps the locations of a composite repository's children onto keys of the proxy. The proxy serves the files below its upstream URL under the same relative path, and the rest of the upstream host under HostPrefix, so a child that the upstream names by absolute URL, or by a relative path that leaves the repository, is reached through the proxy too.
func NewRelocator ¶
NewRelocator returns the relocator of the document stored under key, which is fetched from docURL, of a proxy whose upstream is upstream.
func (*Relocator) Relocate ¶
Relocate returns the location a client must use for the child at location, and reports whether it differs from location. A child on another host, and any location that carries a query or fragment (a download redirector, not a repository directory), are left as they are: the client reaches them directly.