managedfile

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 21 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package managedfile is the managedfile behavior: files osapi writes on a host and keeps a record of, so it can tell its own files from the rest, leave a file alone when it already holds what was asked, and stop managing one. It is internal: other behaviors require it, such as sysctl, and call its methods on the same host.

Index

Constants

View Source
const Unapplied = "osapi.unapplied"

Unapplied is the key of the metadata that marks a file whose write landed and whose next step did not finish, such as netplan apply or a reload of systemd. No caller writes it, so the record then differs from each write of the file: the next write of the same content changes the record and reports changed, and its caller runs the step again.

Variables

View Source
var (
	// ErrNotManaged means osapi has no record of the file.
	ErrNotManaged = errors.New("not managed")
	// ErrUnreadable means the file's record is there but cannot be read.
	ErrUnreadable = errors.New("record unreadable")
)

The causes Read reports that a caller answers differently.

View Source
var Behavior = behavior.Register(behavior.Define(
	"managedfile", "Files osapi writes on a host and keeps a record of",
	behavior.Title("Managed file"),
	behavior.Node,
	behavior.Internal,
	behavior.Platform(behavior.PlatformDebian, newDebian),

	behavior.Platform(behavior.PlatformLinux, newDebian),

	behavior.Platform(behavior.PlatformDarwin, newDarwin),
	behavior.Sudo("chown"),
	behavior.Runs("stat"),

	behavior.Writes("*"),
	behavior.KeepsState,
	behavior.Read("get", Get).
		Describe("Read the record of a file osapi wrote").
		Example("A file osapi wrote", GetInput{Path: path},
			Record{
				Path:     path,
				SHA256:   "b6ac5bc39377e44ee783dff946f6703a994f5eb31a438e69dc0735b40060d2e8",
				Metadata: map[string]string{"setting": "on"},
			},
			behavior.After(UpdateInput{Path: path, Content: "on\n", Metadata: map[string]string{"setting": "on"}}),
		).
		Example("A file osapi stopped managing", GetInput{Path: path},
			Record{
				Path:       path,
				SHA256:     "b6ac5bc39377e44ee783dff946f6703a994f5eb31a438e69dc0735b40060d2e8",
				Metadata:   map[string]string{"setting": "on"},
				Undeployed: true,
			},
			behavior.After(UpdateInput{Path: path, Content: "on\n", Metadata: map[string]string{"setting": "on"}}),
			behavior.After(DeleteInput{Path: path}),
		).
		Fails("A file osapi never wrote", GetInput{Path: path}, "not managed").
		Fails("A relative path", GetInput{Path: "etc/app.conf"}, "want an absolute path"),
	behavior.Write("update", Update).
		Describe("Write a file and keep a record of it").
		Explain("Leaves a file already holding the content alone and reports changed: false "+
			"when its record has the same metadata and object. A file edited by hand is put "+
			"back. New metadata, or a new object or object checksum, changes only the record.").
		Example("A new file", UpdateInput{Path: path, Content: "on\n"},
			Result{Path: path, Changed: true},
			behavior.Wrote(path, "on\n"),
		).
		Example("A secret only root reads",
			UpdateInput{
				Path: token, Content: "s3cret\n", Mode: "0600",
				Owner: "root", Group: "root", Secret: true,
			},
			Result{Path: token, Changed: true},
			behavior.OnPlatform(behavior.PlatformDebian, behavior.PlatformLinux),
			behavior.Prints("stat -c %U:%G -- "+token, "osapi:osapi"),
			behavior.PrintsAfter("chown -- root:root "+token, "stat -c %U:%G -- "+token, "root:root"),
			behavior.Wrote(token, "s3cret\n"),
			behavior.Ran("chown", "--", "root:root", token),
		).
		Example("A secret only root reads, on a Mac",
			UpdateInput{
				Path: token, Content: "s3cret\n", Mode: "0600",
				Owner: "root", Group: "wheel", Secret: true,
			},
			Result{Path: token, Changed: true},
			behavior.OnPlatform(behavior.PlatformDarwin),
			behavior.Prints("stat -f %Su:%Sg -- "+token, "osapi:staff"),
			behavior.PrintsAfter("chown -- root:wheel "+token, "stat -f %Su:%Sg -- "+token, "root:wheel"),
			behavior.Wrote(token, "s3cret\n"),
			behavior.Ran("chown", "--", "root:wheel", token),
		).
		Fails("A mode that is not octal",
			UpdateInput{Path: path, Content: "on\n", Mode: "rw-------"}, "invalid mode").
		Fails("A content type that is neither raw nor template",
			UpdateInput{Path: path, Content: "on\n", ContentType: notAContentType}, "enum").
		Fails("An owner that is not a user name",
			UpdateInput{Path: path, Content: "on\n", Owner: "root;reboot"}, "invalid owner").
		Fails("A group that is not a group name",
			UpdateInput{
				Path:    path,
				Content: "on\n",
				Group:   "wheel;reboot",
			}, "invalid owner or group").
		Fails("A relative path",
			UpdateInput{Path: "etc/app.conf", Content: "on\n"}, "want an absolute path").
		Example("A file whose mode was changed by hand",
			UpdateInput{Path: path, Content: "on\n", Mode: "0600"},
			Result{Path: path, Changed: true},
			behavior.Exists(path, "on\n"),
			behavior.Wrote(path, "on\n"),
		).
		Example("A file edited by hand", UpdateInput{Path: path, Content: "on\n"},
			Result{Path: path, Changed: true},
			behavior.After(UpdateInput{Path: path, Content: "on\n"}),
			behavior.Exists(path, "off\n"),
			behavior.Wrote(path, "on\n"),
		),
	behavior.Write("delete", Delete).
		Describe("Remove a file osapi wrote and stop managing it").
		Explain("Leaves a file osapi has no record of alone, so it never removes what "+
			"it did not write.").
		Fails("A relative path", DeleteInput{Path: "etc/app.conf"}, "want an absolute path").
		Example("A file osapi wrote", DeleteInput{Path: path},
			Result{Path: path, Changed: true},
			behavior.After(UpdateInput{Path: path, Content: "on\n"}),
			behavior.Removed(path),
		).
		Example("A file osapi never wrote", DeleteInput{Path: path},
			Result{Path: path},
			behavior.Exists(path, "theirs\n"),
		),
))

Behavior is the managedfile definition.

Functions

func StateKey

func StateKey(
	hostname string,
	path string,
) string

StateKey is where a file's State is kept: the host's name and the SHA-256 of the file's path, so a path's characters never reach the key.

Types

type DeleteInput

type DeleteInput struct {
	Path string `json:"path" validate:"required,startswith=/" doc:"Absolute path of the file" telemetry:"attr"`
}

DeleteInput is a file to stop managing.

type File

type File struct {
	Path    string
	Content []byte
	// Mode is the file's permissions. Zero keeps the mode of a file that is
	// there, and gives a new file 0644.
	Mode fs.FileMode
	// Owner and Group own the file when set, applied with chown.
	Owner string
	Group string
	// Secret keeps a checksum of the content out of the record, where a short
	// secret's could be recovered by guessing.
	Secret   bool
	Metadata map[string]string
	// Object, ContentType and ObjectSHA256 are where the content came from,
	// for the record. A secret's record keeps no ObjectSHA256 either.
	Object       string
	ContentType  types.ContentType
	ObjectSHA256 string
}

File is what Write makes a path hold.

type GetInput

type GetInput struct {
	Path string `json:"path" validate:"required,startswith=/" doc:"Absolute path of the file" telemetry:"attr"`
}

GetInput is a file whose record to read.

type Provider

type Provider interface {
	// Write makes f.Path hold f.Content with f's mode and ownership, records
	// it, and reports whether anything changed. A file already holding the
	// content with that mode and owner is left alone, whatever its record
	// says.
	Write(ctx context.Context, f File) (bool, error)
	// Remove deletes path if osapi manages it, records that it no longer
	// does, and reports whether a file was deleted.
	Remove(ctx context.Context, path string) (bool, error)
	// Read is the record of path: ErrNotManaged without one, ErrUnreadable
	// when it cannot be read.
	Read(ctx context.Context, path string) (Record, error)
}

Provider is what each platform implements.

func NewDebian

func NewDebian(
	fs avfs.VFS,
	state provider.FileState,
	bucket string,
	exec host.Exec,
	hostname string,
) Provider

NewDebian is the provider for a filesystem and a file state store. The hand-written domains that write managed files build one with it.

type Record

type Record struct {
	Path       string            `json:"path"       doc:"Absolute path of the file"`
	SHA256     string            `json:"sha256"     doc:"Checksum of the content osapi wrote"`
	Metadata   map[string]string `json:"metadata"   doc:"What the caller keeps in the record"`
	Undeployed bool              `json:"undeployed" doc:"Whether osapi has stopped managing the file"`
	// ObjectSHA256 is the checksum of the object that the content came from,
	// so a caller that puts the file back keeps the stale check as it was.
	ObjectSHA256 string `` /* 138-byte string literal not displayed */
}

Record is what osapi keeps about a file it wrote.

func Get

func Get(
	ctx context.Context,
	p Provider,
	in GetInput,
) (Record, error)

Get reads the record osapi keeps of a file.

type Result

type Result struct {
	Path    string `json:"path"    doc:"Absolute path of the file"`
	Changed bool   `json:"changed" doc:"Whether the host changed"`
}

Result is what a change did.

func Delete

func Delete(
	ctx context.Context,
	p Provider,
	in DeleteInput,
) (Result, error)

Delete removes a file osapi manages and records that it no longer does.

func Update

func Update(
	ctx context.Context,
	p Provider,
	in UpdateInput,
) (Result, error)

Update writes a file and keeps a record of it.

type State

type State struct {
	ObjectName   string            `json:"object_name"`
	Path         string            `json:"path"`
	SHA256       string            `json:"sha256"`
	ObjectSHA256 string            `json:"object_sha256,omitempty"`
	Mode         string            `json:"mode,omitempty"`
	Owner        string            `json:"owner,omitempty"`
	Group        string            `json:"group,omitempty"`
	DeployedAt   string            `json:"deployed_at"`
	ContentType  types.ContentType `json:"content_type"`
	UndeployedAt string            `json:"undeployed_at,omitempty"`
	Metadata     map[string]string `json:"metadata,omitempty"`
}

State is the record osapi keeps of a file it wrote, in the file-state store: what it wrote, where, with what checksum and when. SHA256 is the checksum of the file, for drift. ObjectSHA256 is the checksum of the object the file came from, for the stale check: the file of a template is the object rendered, so the two differ. Every part of osapi that writes files on a host records them this way, and the controller reads the records to report drift, so the format lives with the behavior that owns managed files.

type UpdateInput

type UpdateInput struct {
	Path     string            `` /* 167-byte string literal not displayed */
	Content  string            `json:"content"  validate:"required"               doc:"The file's whole content"`
	Mode     string            `` /* 150-byte string literal not displayed */
	Owner    string            `json:"owner"    validate:"omitempty,account_name" doc:"User that owns the file; left as written when empty"`
	Group    string            `json:"group"    validate:"omitempty,account_name" doc:"Group that owns the file; left as written when empty"`
	Secret   bool              `json:"secret"   validate:"omitempty"              doc:"The content is a secret: its record keeps no checksum of it"`
	Metadata map[string]string `` /* 128-byte string literal not displayed */
	// Object is the object of the object store that the content came from,
	// which the controller's stale check compares with the object now.
	Object string `` /* 133-byte string literal not displayed */
	// ContentType is how the content was made from the object: as it is, or
	// rendered as a template.
	ContentType types.ContentType `json:"content_type" validate:"omitempty,enum" doc:"How the content was made from its object: raw or template"`
	// ObjectSHA256 is the checksum of the object as it was read, which the
	// stale check compares with the object's now. The content's own checksum
	// is not it when the object is a template.
	ObjectSHA256 string `json:"object_sha256" validate:"omitempty" doc:"SHA-256 of the object that the content came from, in hex"`
}

UpdateInput is a file to write and keep a record of.

func MarkUnapplied

func MarkUnapplied(
	in UpdateInput,
) UpdateInput

MarkUnapplied is in with Unapplied in its metadata. A caller whose step after a write failed writes it, so a retry runs the step again.

Directories

Path Synopsis
Package mocks provides mock implementations for testing.
Package mocks provides mock implementations for testing.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL