terraformbackend

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 23 Imported by: 0

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

View Source
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

View Source
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.

View Source
var ErrInvalidName = errors.New("terraformbackend: invalid workspace name")

ErrInvalidName is a workspace name that cannot name a folder of its own.

Functions

func CheckName

func CheckName(name string) error

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 Created

func Created(ms int64) time.Time

Created is when the state version ms was created.

func Folder

func Folder(ws string) string

Folder is the path of workspace ws's folder.

func LatestPath

func LatestPath(ws string) string

LatestPath is the path of workspace ws's current state.

func MD5

func MD5(b []byte) string

MD5 is the hex MD5 of b.

func Open

func Open(keys *crypt.Keyring, key, ws string, stored []byte) ([]byte, error)

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

func Parse(plain []byte) (serial int64, lineage string, err error)

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

func ReadStored(ctx context.Context, blobs format.Blobs, sha256 string) ([]byte, error)

ReadStored reads the content stored under sha256 whole: a state is sealed and opened as one message.

func RecordPath

func RecordPath(ws string) string

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

func Row(key, ws string) string

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 SHA256

func SHA256(b []byte) string

SHA256 is the hex SHA-256 of b, the key the blob store files content by.

func Seal

func Seal(keys *crypt.Keyring, key, ws string, plain []byte) ([]byte, error)

Seal seals a plain state of workspace ws in repository key.

func Serves

func Serves(t repo.PackageType) bool

Serves reports whether t is the Terraform backend's package type.

func SplitStateID

func SplitStateID(id string) (workspaceID string, ms int64, ok bool)

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

func Splits(id string) [][2]string

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

func StateID(key, ws string, ms int64) string

StateID is the ID of workspace ws's state version ms: the workspace's ID and "__<ms>".

func StatePath

func StatePath(ws string, ms int64) string

StatePath is the path of workspace ws's earlier state of version ms.

func Version

func Version(ms int64) string

Version is the package index's name of the state version ms.

func WorkspaceID

func WorkspaceID(key, ws string) string

WorkspaceID is workspace ws's ID in repository key: "<key>__<ws>".

func Writing

func Writing(ctx context.Context, state *Meta) context.Context

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

func FileOf(it types.Item) (File, bool)

FileOf reads it as a state file; ok is false for a folder and for any other path, the workspace record included.

func ParseFile

func ParseFile(p string) (File, bool)

ParseFile reads a repository path as a state file. The current state's Ms is the item's creation time, which the path does not give: it is zero here, and FileOf fills it in. An earlier state's milliseconds are canonical decimal, so two names never give one version.

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

func (Handler) AdmitFolder(ctx context.Context, _ format.Env, c repo.Config, _ string) error

AdmitFolder refuses a folder create but the API's own, as Admit refuses a file.

func (Handler) Classify

func (Handler) Classify(_ repo.Config, p string) format.Class

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) Deleting

Deleting runs before a file is removed; the item service unlinks it.

func (Handler) Deployed

func (h Handler) Deployed(ctx context.Context, env format.Env, c repo.Config, it types.Item) error

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.

func (Handler) Inspect

func (Handler) Inspect(
	ctx context.Context,
	_ format.Env,
	_ repo.Config,
	w format.Write,
) (any, error)

Inspect hands the hooks what the API knows of the state it writes. It reads nothing: any other write is refused by Admit, and a hook reads what it is not handed.

func (Handler) Reindex

func (h Handler) Reindex(ctx context.Context, env format.Env, c repo.Config) error

Reindex rebuilds the repository's index from its tree in one transaction, reading every state, unsealed, for its serial and lineage.

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

func DecodeMeta(b []byte) (Meta, bool)

DecodeMeta reads what Encode wrote; ok is false for anything else.

func MetaOf

func MetaOf(keys *crypt.Keyring, key, ws string, stored []byte) Meta

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.

func PlainMeta

func PlainMeta(plain, stored []byte) (Meta, error)

PlainMeta is the Meta of plain, a state stored as stored: plain itself, or sealed.

func (Meta) Encode

func (m Meta) Encode() []byte

Encode is m as the package index stores it.

type Settings

type Settings struct {
	// Keys opens sealed states, so the index holds their serial and
	// lineage. Without it a sealed state is indexed as unreadable.
	Keys *crypt.Keyring
}

Settings are the format's server-wide options.

Jump to

Keyboard shortcuts

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