Documentation
¶
Overview ¶
Package osmodule reads the manifest of a ReCasaOS module: a component that is installed on its own (a service of its own, packages of its own) and that the core shows only when it is there. The manifest ships in the release archive of the module, and the installer copies it to /etc/casaos/modules/<name>.json.
The file is data that the core can trust only as far as it validates it. Every name, unit, path and number in it is checked against a fixed pattern, an unknown field is an error, and nothing in it is ever run as a command, interpolated into a shell or opened as a path outside what the pattern allows: a manifest that does not pass is a module that cannot be told, and the core says so instead of guessing.
Index ¶
- Constants
- Variables
- func FileName(name string) string
- func FormatDirs(paths []string) ([]byte, error)
- func FormatFilesList(entries []FileEntry) ([]byte, error)
- func PackagesFor(m Manifest, arch string) []string
- func ParseDirs(raw []byte) ([]string, error)
- func ParseFiles(raw []byte) ([]string, error)
- func PlanID(name, tag, archiveSHA256, osID, osVersion string, toInstall []string) string
- func ValidDirPath(p string) bool
- func ValidFilePath(p string) bool
- func ValidName(s string) bool
- func ValidPackageName(s string) bool
- func ValidUnit(s string) bool
- type AptResult
- type Asset
- type Assets
- type Box
- type Catalogue
- type Check
- type Destination
- type Entry
- type File
- type FileEntry
- type FilesList
- type Invalid
- type Maintenance
- type Manifest
- type OSUpgrade
- type Options
- type Packages
- type PlanArchive
- type PlanPackages
- type PlanResult
- type Refusal
- type Refused
- type Result
- type System
- type UnpackPlan
- type Want
Constants ¶
const ( // MaxCatalogueBytes is the most of a catalogue that is read. MaxCatalogueBytes = 1 << 20 // MaxCatalogueModules is the most entries in a catalogue. MaxCatalogueModules = 64 )
the bounds of a catalogue
const ( TierCore = "core" TierCommunity = "community" )
the tiers of a module: core is maintained here and proven on a real guest, community is proven in CI against a test double and said so to whoever installs it.
const ( // MaxEntries is the most entries of an archive, and so the most paths of a list. MaxEntries = 5000 // MaxPathBytes is the longest path of a file of a module (relative to the root), MaxElementBytes the longest name in it. MaxPathBytes = 1024 MaxElementBytes = 255 // MaxFilesBytes is the most of a list that is read: the longest line of every entry, and the first line. MaxFilesBytes = len(FilesHeader) + 1 + MaxEntries*maxLineBytes )
the bounds of a list and of a path
const ( ReasonUnreadable = "unreadable" ReasonTooLarge = "too_large" ReasonFile = "file_name" ReasonSchema = "schema" ReasonValue = "value" ReasonMissing = "missing" ReasonTooMany = "too_many" )
the reasons of an Invalid
const ( CheckCatalogue = "catalogue" CheckOS = "os" CheckArch = "arch" CheckReCasaOS = "recasaos" CheckInstalled = "already_installed" CheckForeignUnit = "foreign_unit" CheckBusy = "busy" CheckDpkgState = "dpkg_state" CheckDisk = "disk" CheckApt = "apt" )
The checks of a plan, in the order they are listed in.
const ( RefusedDiskShort = "disk_short" RefusedDiskUnknown = "disk_unknown" RefusedAptMissing = "apt_missing" RefusedAptFailed = "apt_failed" RefusedAptRemoves = "apt_removes" RefusedAptDowngrades = "apt_downgrades" RefusedHeld = "held_package" RefusedPackageNotExact = "package_not_exact" )
The codes of a refusal. A plan is refused with the code of the first check that failed, which is the code of that check, but for the checks of the disk and of apt, which say more: there is not room or it cannot be told, apt is not there, gave no answer, would remove or downgrade a package, finds one held, or would install something other than the packages that were asked for (apt reads a name with a "." or a "+" in it as a regular expression when no package has that name, and a name that a package provides as that package).
const ( TmpDir = "/var/lib/casaos/modules/tmp" UsrDir = "/usr" DiskArchiveFactor int64 = 3 DiskAptDownloadFactor int64 = 1 DiskAptExtraFactor int64 = 1 // MaxArchiveBytes is the largest archive that is downloaded, and what the plan takes the archive to be when the Box does not know its size. MaxArchiveBytes int64 = 200 << 20 )
The paths whose file systems must have room for an install, and the factors of what the install writes on each of them: the archive is downloaded, and read back as a tar, and its files are written (three times its size, whatever it unpacks to), then what apt downloads and what apt leaves on the disk. These constants are the one place the numbers are.
const ( // MaxTotalBytes is the most the files of an archive come to once unpacked, MaxStreamBytes the most the tar stream itself is (the headers and the padding of MaxEntries entries are in the // difference). MaxTotalBytes int64 = 400 << 20 MaxStreamBytes int64 = MaxTotalBytes + 64<<20 )
the limits of an archive
const ( // UnpackPath: an entry that is not allowed, by its name, its type, its link, or where it would be put; a destination that is a link or behind one. UnpackPath = "unpack_path" // FileExists: a file or a directory that is in the way and is not the module's own. FileExists = "file_exists" // UnpackLimits: more entries, directories or bytes than an archive of a module has. UnpackLimits = "unpack_limits" // UnpackCorrupt: an archive that is not a tar, is cut, has no end, or is not the same the second time it is read, or the third. UnpackCorrupt = "unpack_corrupt" )
the codes of a refusal of an archive
const CatalogueSchema = 1
CatalogueSchema is the one version of the catalogue that is read.
const DetailNeedsRoot = DetailNotChecked + ": needs root, the install checks it as root"
DetailNeedsRoot is what the check of the package manager says when the user who asks for the plan is not root, who is the only one that can look at the locks of dpkg and apt: it was not made, it is not the reason of a refusal, and the install makes it again. The words start with the mark of a check that was not made.
const DetailNotChecked = "not_checked"
DetailNotChecked is what a check says when it was not made, because a check before it had already failed and made the answer of the plan certain (apt is not asked, nor the disk, for a system that the module does not support). Such a check is not ok, and is never the reason of the refusal. It is the plan that says it, with a mark of its own: the same words in the answer of apt or of a Box are the words of a check that failed.
const FilesHeader = "casaos-module-files 1"
FilesHeader is the first line of a list that records the contents.
const MaxBytes = 64 << 10
MaxBytes is the most of a manifest that is read.
const Schema = 1
Schema is the one version of the manifest that is read: a change of the format is a new number, so that it is never guessed at.
Variables ¶
var AllowedDirs = []string{"usr/bin", "usr/lib/systemd/system", "lib/systemd/system", "etc/casaos"}
AllowedDirs are the directories, relative to the root of the system, that a module may put files in: the unpack refuses any other destination, and the list holds no path outside them. Not all of what is in them: a module puts down its program in usr/bin, its unit in a directory of units, and the sample of its configuration in etc/casaos, each directly in the directory (placeOK). Nothing of /var/lib/casaos or /usr/share/casaos is in the list on purpose: the core keeps its state there (alerts, telemetry, the choices of the automatic updates, the catalogue, the lists of the modules), and a module has no file in them that it should be allowed to write over.
var ErrDpkgMissing = errors.New("dpkg is not there")
ErrDpkgMissing is what a Box answers when there is no dpkg to ask: the check of the state of dpkg was not made.
var ErrNeedsRoot = errors.New("needs root")
ErrNeedsRoot is what a Box answers when it cannot tell whether a package manager holds the machine because the user is not root (and not for another reason): the plan does not take it for a machine that is busy, nor for one that is not.
var ReservedPrograms = []string{
"systemctl", "systemd-run", "journalctl", "apt-get", "apt", "apt-cache", "apt-config", "apt-key", "apt-mark", "dpkg", "dpkg-query", "dpkg-deb", "dpkg-divert", "dpkg-statoverride",
"update-rc.d", "invoke-rc.d", "deb-systemd-helper", "deb-systemd-invoke", "sh", "dash", "bash", "env",
}
ReservedPrograms are the programs of the system that the assistant runs as root, or that what it runs runs by name (apt, dpkg and their scripts, systemd): an archive may not put a program of one of these names in usr/bin, where on a system whose /bin is not /usr/bin it would come before the real one in the path. The assistant finds each of them once, before anything is unpacked, and runs them by the path it found. (ponytail: a list of names; a program of another name that exists in /bin and not in /usr/bin is not covered.)
Functions ¶
func FormatDirs ¶ added in v0.4.75
func FormatFilesList ¶ added in v0.4.75
FormatFilesList is the content of the list of the entries, in the order given: the entries are checked as ParseFilesList would, so that what is written is what can be read.
func PackagesFor ¶ added in v0.4.75
PackagesFor are the packages a module needs on an architecture: the common ones and the architecture's, sorted, none twice.
func ParseDirs ¶ added in v0.4.75
ParseDirs and FormatDirs are the same for the list of the directories that the install of a module made (<name>.dirs), which the remove deletes, if they are empty, after its files.
func ParseFiles ¶ added in v0.4.75
ParseFiles is the paths of a list, in either format.
func PlanID ¶ added in v0.4.75
PlanID is the id that binds an install to the plan that was shown: "m1-" and the first 32 hexadecimal digits of the SHA-256 of the module, its tag, the sha256 of the archive for this architecture, the packages to install (sorted) and the operating system, written as a JSON object with its keys in this order so that no two sets of values are the same text.
func ValidDirPath ¶ added in v0.4.75
ValidDirPath says whether p is a directory that the unpack of a module may have made, and so that a remove may try to delete: an allowed directory or one of those above it, which are the only ones it makes.
func ValidFilePath ¶ added in v0.4.75
ValidFilePath says whether p is a path that may be in the list of a module: absolute, already clean, made of safe names, and a place where an unpack puts a file (any unit name stands for the unit: which one it is, the unpack knows).
func ValidName ¶ added in v0.4.75
ValidName says whether a text is the name of a module: the rule of the name in a manifest, which is also the rule of the name in a catalogue, of a unit's name and of the name in an address.
func ValidPackageName ¶ added in v0.4.75
ValidPackageName says whether a name is one that may be handed to apt as a package: lower case letters, digits, "+", "." and "-", two to 64 characters, the first a letter or a digit. A name that ends in "-" or "+" is refused although the pattern of Debian takes it, because the command line of apt reads that last character as an order ("pkg-" removes pkg), and so is one that ends in ".deb", which apt reads as the path of a file.
Types ¶
type AptResult ¶ added in v0.4.75
type AptResult struct {
ToInstall, ToUpgrade []string
Removes, Downgrades, Held []string
DownloadBytes, ExtraBytes int64
Known bool
Failure string
Detail string
}
An AptResult is what apt says of installing the packages of a module, as the simulation of it reads: the packages it would install and upgrade, the sizes, and the three things that refuse a plan (a package removed, a package downgraded, a package held). Failure is why there is no answer at all (RefusedAptFailed, or RefusedHeld when apt itself says a held package is in the way, or RefusedPackageNotExact when it would install something other than the packages that were asked for); Detail is a few words of apt's own for whoever reads the plan, of which at most maxAptDetail characters are shown.
type Assets ¶ added in v0.4.75
Assets are the archives of an entry, by architecture; an architecture with no archive is a pointer that is nil.
type Box ¶ added in v0.4.75
type Box interface {
// OS is the ID and the VERSION_ID of /etc/os-release ("debian", "12"); empty when they cannot be read.
OS() (id, version string)
// Arch is the architecture of the machine as the archives name it ("amd64", "arm64").
Arch() string
// ReCasaOSVersion is the version of the DISTRIBUTION that is installed ("0.5.29", with or without a v): what the installer writes to /var/lib/casaos/fork-release and
// version.CurrentVersion() reads. It is not the version of the core (common.VERSION, which a release of the distribution need not move): min_recasaos in a manifest is a number of
// the distribution.
ReCasaOSVersion() string
// InstalledManifests are the manifests of /etc/casaos/modules that read, by the NAME of the module ("virt"), and the modules whose manifest does not read, in the same spelling: the
// name, not the name of the file ("virt.json") or its path.
InstalledManifests() (installed map[string]Manifest, broken []string, err error)
// FilesLists are the lists of files of every module, by name (the lists of /var/lib/casaos/modules/*.files).
FilesLists() (map[string][]string, error)
// UnitFiles are the paths of the files of a unit that exist, in every directory systemd reads units from, ONE spelling for each file: the one the unpack records in a list (a system
// whose /lib is a link to usr/lib has the file under both names, and the answer has it once). Ownership is compared after "/lib/" is read as "/usr/lib/" on both sides, so that a list
// that was written with one spelling owns the file that is listed with the other.
UnitFiles(unit string) ([]string, error)
// MergedUsr says whether /lib is a link to usr/lib on this box (a system with a merged /usr), which is the only case in which "/lib/x" and "/usr/lib/x" are one file; the unpack decides
// the same way, on the same condition.
MergedUsr() (bool, error)
// PackageManagerBusy says whether a package manager (dpkg, apt, the units that update) holds the machine; OSUpgradeBusy whether the upgrade of the operating system does (its busy flag,
// read so that a flag that cannot be read is an error).
PackageManagerBusy() (bool, error)
OSUpgradeBusy() (bool, error)
// DpkgAudit is the packages that dpkg says it has not finished (dpkg --audit: unpacked and not configured, half installed, ...), none for a dpkg that is in a state it can go on from.
// It reads and takes no lock. ErrDpkgMissing when there is no dpkg to ask; any other error when it gave no answer.
DpkgAudit() ([]string, error)
// FreeBytes is the room on the file system that holds a path (the nearest directory that exists).
FreeBytes(path string) (int64, error)
// AptGetExists says whether apt-get is there.
AptGetExists() bool
// ArchiveBytes is the size of the archive at an address when it is known without downloading it.
ArchiveBytes(url string) (int64, bool)
// SimulateApt asks apt what installing the packages (sorted, none twice, valid names) would do, without doing it.
SimulateApt(packages []string) AptResult
}
A Box is the world, as far as a plan has to ask. Every method reads. An error is never a "no": the plan treats what it cannot read as a reason to refuse.
type Catalogue ¶ added in v0.4.75
A Catalogue is the list of the modules a release of ReCasaOS can install.
func ParseCatalogue ¶ added in v0.4.75
ParseCatalogue reads a catalogue and checks everything in it. The error is an *Invalid that names the place; the Catalogue is empty whenever there is an error.
type Check ¶ added in v0.4.75
type Check struct {
Code string `json:"code"`
OK bool `json:"ok"`
Detail string `json:"detail"`
// contains filtered or unexported fields
}
A Check is one question of the plan and its answer: the code of the question, whether it is ok, and a few words that are safe to show (printable ASCII, cut).
func SystemChecks ¶ added in v0.4.75
SystemChecks are the three checks that depend on the system only, in the order the plan lists them (os, arch, recasaos): the dashboard shows them for every module in the list, and the plan makes them again.
type Destination ¶ added in v0.4.75
type Destination interface {
// Lstat is the type and the permissions of what is at the path, without following a link at its end; fs.ErrNotExist when there is nothing.
Lstat(path string) (fs.FileMode, error)
// Mkdir makes one directory, whose parent is there, and fails with an error that is fs.ErrExist if anything is at the path. perm is EXACT: it is what the directory has once Mkdir
// returns (a chmod after the creation), not what the umask leaves of it.
Mkdir(path string, perm fs.FileMode) error
// NewFile starts a file for path: what is written to it is not at the path until Commit; Abort drops it, and may be called after a Commit that failed. perm is EXACT, as for Mkdir (the
// file is chmoded, it is not made with perm and the umask). The temporary name is short (a path of the file system can be at its longest) and in the directory of the file, so that the
// publication is one rename in a file system.
// When replace is false, Commit publishes the file only if nothing is at the path then, in one step that cannot be raced (renameat2 with RENAME_NOREPLACE, or a link then an unlink), and
// otherwise fails with an error that is fs.ErrExist and leaves what is there as it is. When replace is true it replaces what is at the path, a file or a link, in one step (a rename).
NewFile(path string, perm fs.FileMode, replace bool) (File, error)
// Symlink makes the link, with the same rules as a file: when replace is false it fails with an error that is fs.ErrExist if anything is at the path, and when it is true it replaces
// what is there in one step (a link made under a temporary name, then a rename).
Symlink(target, path string, replace bool) error
}
A Destination is the file system that an archive is unpacked onto. Paths are absolute. It is the seam of the tests, and the real one is the machine. The unpack has checked, from the allowed directory down, that none of the directories that exist is a link; what is above the allowed directory is the system's own (on a system with a merged /usr, /lib is a link to /usr/lib). It makes nothing that was not asked for, and none of its writes goes through a link.
type Entry ¶ added in v0.4.75
type Entry struct {
Name string `json:"name"`
Component string `json:"component"`
Tier string `json:"tier"`
Tag string `json:"tag"`
Commit string `json:"commit"`
Assets Assets `json:"assets"`
Manifest Manifest `json:"manifest"`
}
An Entry is a module at a version: where its release is (the repository, the tag and the commit it was built from), the archive of each architecture with the sha256 it must have, and the manifest of the module, which the archive holds too: the box needs it before it has the archive (to say whether the system is supported, which packages the module needs, what to show of it), and the release build that writes the catalogue reads it from the archive, so that the two are the same text.
type FileEntry ¶ added in v0.4.75
A FileEntry is a path of a list and what it should hold: for a file the sha256 of its bytes in lower case hex, for a link its target. Want has two contents only while an install replaces a file of its own: the one that was there and the one that comes.
type FilesList ¶ added in v0.4.75
A FilesList is a list as it was read. Verified says that it records the contents; a list of the earlier format names paths and nothing to check them against, and has entries with no Want.
func ParseFilesList ¶ added in v0.4.75
ParseFilesList reads the content of a list, in either format: one valid line to an entry, each line ended by a new line, no path twice, at least one entry and at most MaxEntries. Anything else is an error and no list; an empty list is one too, since a list that was cut short at its start reads as one and must not read as a module that has no files.
type Invalid ¶
An Invalid is why a manifest was not taken: the field that is wrong (a path through the manifest, from a fixed alphabet) and a code.
type Maintenance ¶
type Maintenance struct {
Prepare string `json:"prepare"`
Status string `json:"status"`
Resume string `json:"resume"`
}
Maintenance are the three calls: prepare (POST, idempotent) asks the module to get ready, status says whether it is, and resume is made once the unit is back after the restart and the module has checked what it depends on.
type Manifest ¶
type Manifest struct {
Schema int `json:"schema"`
// Name is the short name of the module ("virt"): the name of its file, and what the core calls it.
Name string `json:"name"`
// Title and Description are what the dashboard shows of the module, as text: printable characters and nothing else.
Title string `json:"title"`
Description string `json:"description,omitempty"`
// Component is the name of the component that is released (the repository), Unit its systemd unit, GatewayRoute where the gateway reaches it.
Component string `json:"component"`
Unit string `json:"unit"`
GatewayRoute string `json:"gateway_route"`
Packages Packages `json:"packages"`
// MinReCasaOS is the oldest release of ReCasaOS that the module works with; OS the operating systems it supports ("debian:12", "ubuntu:24.04").
MinReCasaOS string `json:"min_recasaos"`
OS []string `json:"os"`
// OSUpgrade is what the module needs around an upgrade of the operating system; a module that does not say is one that the upgrade refuses.
OSUpgrade *OSUpgrade `json:"os_upgrade,omitempty"`
}
A Manifest is what a module says of itself.
type OSUpgrade ¶
type OSUpgrade struct {
StopUnits []string `json:"stop_units"`
StopTimeoutSeconds int `json:"stop_timeout_seconds"`
Maintenance Maintenance `json:"maintenance"`
}
An OSUpgrade is what the core does around an upgrade of the operating system, declared and never scripted: the units it stops (after the module has said that it is ready), how long it waits for that, and three paths of the module's own API that it calls through the gateway with the internal secret.
type Options ¶ added in v0.4.75
type Options struct {
// Unit is the unit of the module, as its manifest names it: the one file an archive may put in a directory of units. Empty, an archive may put none.
Unit string
// OnPlanned, when it is not nil, is called once, after the checking and before anything is written, with every path of a file or a link that the unpack may write (in the order they
// are put down, the units last), what each is to hold, and every directory it may make (the parents first): the caller records them, so that a kill half way leaves a record that names
// everything that the unpack might have put down, and if it fails nothing is written. What the module owns in the end is the Result's, which can be less: a path that was planned and not
// written may be a file that is not the module's (it is what made the unpack stop), so a caller that deletes after a stop deletes what the Result holds, and only what still holds what
// it was to hold, never what it was told in advance.
OnPlanned func(files []string, want map[string]Want, dirs []string) error
}
Options are what an unpack is told besides the archive and the file system.
type Packages ¶
type Packages struct {
Common []string `json:"common,omitempty"`
AMD64 []string `json:"amd64,omitempty"`
ARM64 []string `json:"arm64,omitempty"`
}
Packages are the packages of the operating system that the module needs, for every architecture and for each.
type PlanArchive ¶ added in v0.4.75
PlanArchive is the archive that would be downloaded, and its size when it is known.
type PlanPackages ¶ added in v0.4.75
type PlanPackages struct {
ToInstall []string `json:"to_install"`
ToUpgrade []string `json:"to_upgrade"`
DownloadBytes int64 `json:"download_bytes"`
ExtraBytes int64 `json:"extra_bytes"`
Known bool `json:"known"`
}
PlanPackages are what apt would do. The sizes are apt's own, and Known says whether apt gave them.
type PlanResult ¶ added in v0.4.75
type PlanResult struct {
Name string `json:"name"`
Tag string `json:"tag"`
Tier string `json:"tier"`
Checks []Check `json:"checks"`
Refused Refusal `json:"refused"`
Packages PlanPackages `json:"packages"`
Archive PlanArchive `json:"archive"`
PlanID string `json:"plan_id"`
}
A PlanResult is the answer of a plan, as the command says it in JSON.
func Plan ¶ added in v0.4.75
func Plan(cat Catalogue, name string, box Box) PlanResult
Plan makes the plan of installing the module called name from the catalogue on the box. It asks the Box only what the checks before it have not already made certain: apt is not asked to simulate for a system the module does not support, and the room is not looked at for a plan that is going to be refused.
type Refusal ¶ added in v0.4.75
type Refusal string
A Refusal is the code that a plan was refused with, and null in JSON when it was not.
func (Refusal) MarshalJSON ¶ added in v0.4.75
type Refused ¶ added in v0.4.75
type Refused struct {
Code, Where string
}
A Refused is why an archive was not unpacked: a code, and where (the name of the entry, cut and made printable, since it is the archive's own text). When the code comes before the writing, nothing was written; when it comes from the writing (a file that appeared in the way, bytes that are not the ones that were checked), the Result of Unpack says what was.
type Result ¶ added in v0.4.75
A Result is what an unpack did. Files is the list of the files and links the module owns after it, in the order they were put down, with the units last: what goes in the <name>.files list, and what a rollback deletes; Want says what each of them holds. Dirs are the directories the unpack made, and no other, the deepest first, for a rollback or a remove to delete (when they are empty). All are given when the unpack stops half way too. Manifest is the module.json of the archive, not yet read as a manifest.
func Unpack ¶ added in v0.4.75
func Unpack(src io.ReadSeeker, dst Destination, own []string, opts Options) (Result, error)
Unpack unpacks the archive onto dst, after checking all of it. own are the files that this module's earlier install wrote (its list of files): they may be replaced, and no other file may, not even one that appears after the checking. When the unpack stops half way the Result holds what was written and the directories it made, with the error, and the caller rolls back with it; the unpack deletes nothing.
type System ¶ added in v0.4.75
type System struct {
OSID, OSVersion, Arch, ReCasaOS string
}
A System is what the box says of itself that decides whether a module is for it.
type UnpackPlan ¶ added in v0.4.75
type UnpackPlan struct {
Manifest []byte
// contains filtered or unexported fields
}
An UnpackPlan is what the checking of an archive leaves: what to write, in the order of the archive, the directories to make (all with 0755: they are only on the way to the allowed directory), parents first, and the manifest.
func ValidateArchive ¶ added in v0.4.75
func ValidateArchive(src io.ReadSeeker, dst Destination, own []string, opts Options) (*UnpackPlan, error)
ValidateArchive reads the whole archive and the whole destination and says whether Unpack would write it; it writes nothing.
type Want ¶ added in v0.4.75
A Want is what a path that the unpack writes should hold: the sha256 of the bytes of a file in lower case hex, or the target of a link. It is what the list of the files records, and what a remove, a rollback and a later install check before they delete or replace the path.