Documentation
¶
Overview ¶
Package terraformbackend is the format of Terraform backend repositories (decision 0041): the workspaces and states of Terraform's remote backend, kept as items under workspaces/<ws>/ as 7.x keeps them. Its hooks keep the package index, in which a workspace is a package and each state a version named by its epoch milliseconds, whose metadata answers the API without reading the content. The content itself is written only through the Terraform backend API (internal/service/terraformstate), which marks its writes with Writing: through the item routes a deploy, a folder create, a copy or move into the repository, and a change to the lock's properties are refused, so nothing passes the lock or the seal. Deletes are allowed, into the trash can, as for any item.
Index ¶
- Constants
- Variables
- func CheckName(name string) error
- func Created(ms int64) time.Time
- func Folder(ws string) string
- func LatestPath(ws string) string
- func MD5(b []byte) string
- func Open(keys *crypt.Keyring, key, ws string, stored []byte) ([]byte, error)
- func Parse(plain []byte) (serial int64, lineage string, err error)
- func ReadStored(ctx context.Context, blobs format.Blobs, sha256 string) ([]byte, error)
- func RecordPath(ws string) string
- func Refusal(c repo.Config) *format.RejectionError
- func Row(key, ws string) string
- func SHA256(b []byte) string
- func Seal(keys *crypt.Keyring, key, ws string, plain []byte) ([]byte, error)
- func Serves(t repo.PackageType) bool
- func SplitStateID(id string) (workspaceID string, ms int64, ok bool)
- func Splits(id string) [][2]string
- func StateID(key, ws string, ms int64) string
- func StatePath(ws string, ms int64) string
- func Version(ms int64) string
- func WorkspaceID(key, ws string) string
- func Writing(ctx context.Context, state *Meta) context.Context
- type File
- type Handler
- func (Handler) Admit(ctx context.Context, _ format.Env, c repo.Config, w format.Write) (format.Write, error)
- func (Handler) AdmitAnnotation(ctx context.Context, _ format.Env, c repo.Config, _ types.Item, ...) error
- func (Handler) AdmitFolder(ctx context.Context, _ format.Env, c repo.Config, _ string) error
- func (Handler) Classify(_ repo.Config, p string) format.Class
- func (h Handler) Deleted(ctx context.Context, env format.Env, c repo.Config, removed []types.Item) error
- func (Handler) Deleting(context.Context, format.Env, repo.Config, types.Item) error
- func (h Handler) Deployed(ctx context.Context, env format.Env, c repo.Config, it types.Item) error
- func (Handler) Inspect(ctx context.Context, _ format.Env, _ repo.Config, w format.Write) (any, error)
- func (h Handler) Reindex(ctx context.Context, env format.Env, c repo.Config) error
- type Meta
- type Settings
Constants ¶
const ( // Root is the folder that holds every workspace. Root = "workspaces" // RecordName is the workspace record. The workspace exists while it // does. RecordName = "workspace.json" // LatestName is the current state. LatestName = "state.latest.json" // PropLockedBy and PropLockedAt are the lock, on the workspace folder: // the user holding it and when they took it, in epoch milliseconds. PropLockedBy = "locked.by" PropLockedAt = "locked.at" // Purpose is the purpose a state is sealed under (crypt.Keyring.Seal). Purpose = "terraform-state" )
The layout of a repository (decision 0041, "Storage"): one folder per workspace under Root, holding the workspace record, the current state and each earlier state, named by its state version's epoch milliseconds.
Variables ¶
var ( // ErrNotState is content that is not a Terraform state: not a JSON // object, or one without a whole, non-negative serial. ErrNotState = errors.New("terraformbackend: not a terraform state") // ErrNoKeyring is a sealed state, or a state to seal, on a server // without a master key. ErrNoKeyring = errors.New( "terraformbackend: the state is sealed and no master key is configured", ) )
Errors of reading a state.
var ErrInvalidName = errors.New("terraformbackend: invalid workspace name")
ErrInvalidName is a workspace name that cannot name a folder of its own.
Functions ¶
func CheckName ¶
CheckName refuses a workspace name that is not one path segment go-tfe would send: empty, "." or "..", or holding a slash, white space, or a character no item name may hold (types.SplitItemPath). TFE's narrower rule (letters, digits, "-" and "_") is not applied, so a name that works on 7.x keeps working (decision 0041, "Identifiers").
func LatestPath ¶
LatestPath is the path of workspace ws's current state.
func Open ¶
Open returns the plain state stored as stored in workspace ws of repository key: stored itself unless it is sealed, which no state JSON can be mistaken for, as the envelope starts with a NUL. So a repository whose encryptStates changed reads every state in the form it was written in (decision 0041).
func Parse ¶
Parse reads a state's serial and lineage. Terraform writes both at the top level of every state from format version 3 on.
func ReadStored ¶
ReadStored reads the content stored under sha256 whole: a state is sealed and opened as one message.
func RecordPath ¶
RecordPath is the path of workspace ws's record.
func Refusal ¶
func Refusal(c repo.Config) *format.RejectionError
Refusal is the answer to a write to repository c through the item routes. [I] 7.x's answer is unknown (research 32 section 6, item 11).
func Row ¶
Row is the row a state of workspace ws in repository key is sealed for, so a sealed state opens only in its own workspace's folder, under either of its names (decision 0041, "encryptStates").
func Serves ¶
func Serves(t repo.PackageType) bool
Serves reports whether t is the Terraform backend's package type.
func SplitStateID ¶
SplitStateID reads id as "<workspace ID>__<ms>". The milliseconds hold no "_", so the last "__" ends the workspace's ID however many its name holds.
func Splits ¶
Splits lists the ways id reads as "<repository key>__<workspace name>", splitting at each "__" from the left. A workspace name may hold "__" itself, so the caller takes the first split whose repository holds the workspace (decision 0041, "Identifiers").
func StateID ¶
StateID is the ID of workspace ws's state version ms: the workspace's ID and "__<ms>".
func WorkspaceID ¶
WorkspaceID is workspace ws's ID in repository key: "<key>__<ws>".
func Writing ¶
Writing marks ctx as a write of the Terraform backend API, which Admit and the other checks let through: the workspace record, a state, the move of the current state to its timestamped name, and the lock's properties. state, when not nil, is what the API knows of the state the write stores, which Inspect hands to the hooks so they need not read the content back. Only server code reaches it: a client's request never carries it, whatever route it takes.
Types ¶
type File ¶
type File struct {
Workspace string
// Latest marks the current state, state.latest.json.
Latest bool
// Ms names the state version: an earlier state's name gives it, and the
// current state's creation time does (decision 0041, "The package
// index"), since the state write creates it at that instant.
Ms int64
}
File is a state file's place in a repository.
func FileOf ¶
FileOf reads it as a state file; ok is false for a folder and for any other path, the workspace record included.
type Handler ¶
type Handler struct {
Settings Settings
}
Handler is the Terraform backend format.
func (Handler) Admit ¶
func (Handler) Admit( ctx context.Context, _ format.Env, c repo.Config, w format.Write, ) (format.Write, error)
Admit refuses every file write but the API's own: a deploy, a checksum deploy or sidecar, and the target of a copy or move. Such a write would pass the lock and the seal, and could replace state.latest.json under a running apply. The include and exclude patterns and blackedOut do not apply to the API's writes (decision 0041, "The item routes").
func (Handler) AdmitAnnotation ¶
func (Handler) AdmitAnnotation( ctx context.Context, _ format.Env, c repo.Config, _ types.Item, a format.Annotation, ) error
AdmitAnnotation refuses a change to the lock's properties but the API's own, so the lock has one writer: force-unlock is the way to clear a lock left behind (decision 0041, "Locking") [I].
func (Handler) AdmitFolder ¶
AdmitFolder refuses a folder create but the API's own, as Admit refuses a file.
func (Handler) Classify ¶
Classify reports the current state and the workspace record as metadata, which change under the same path, and every earlier state as unique.
func (Handler) Deleted ¶
func (h Handler) Deleted( ctx context.Context, env format.Env, c repo.Config, removed []types.Item, ) error
Deleted settles each version a delete or a move took a state file from: one whose current and timestamped files named the same version keeps the other. A version left with none is gone already: the item service pruned it.
func (Handler) Deployed ¶
Deployed indexes a state file landed by the API, a move or a restore. A file whose content changed in place may have named another version, as a restore of an earlier state.latest.json does; that version is settled too.
type Meta ¶
type Meta struct {
Serial int64 `json:"serial"`
Lineage string `json:"lineage,omitempty"`
// MD5 is the hex MD5 of the plain state, which Terraform compares with
// the one it computes.
MD5 string `json:"md5,omitempty"`
// Size is the plain state's length; a sealed item is longer.
Size int64 `json:"size"`
// Stored is the SHA-256 of the item's content the rest was read from.
// A hook that finds the item's content unchanged reuses the rest
// rather than unsealing it again.
Stored string `json:"stored"`
// Sealed marks content stored sealed under the master key.
Sealed bool `json:"sealed,omitempty"`
// Unreadable marks content that does not open under this server's
// master key or is no state. It is indexed all the same, so a trash
// restore or a reindex never fails on it: the API refuses it when it
// is read.
Unreadable bool `json:"unreadable,omitempty"`
}
Meta is what the package index keeps of a state version (decision 0041, "The package index"), so the API answers without reading the content: the state's own serial and lineage, the MD5 and size of the plain state, and the stored content it was read from.
func DecodeMeta ¶
DecodeMeta reads what Encode wrote; ok is false for anything else.
func MetaOf ¶
MetaOf is the Meta of a state of workspace ws in repository key stored as stored. Content that does not open or is no state is Unreadable, with the stored size.