Documentation
¶
Overview ¶
Package terraformstate is the Terraform backend's storage (decision 0041): the workspaces, state versions and locks of terraformbackend repositories.
Role ¶
artifacts module, core layer. Workspaces, states and locks are items under workspaces/<ws>/, written through the item service (artifacts/core/item), so the Terraform backend format's hooks index them, a delete puts them in the trash can, and the storage API, AQL and replication see them as any other item. The TFE-shaped API that Terraform's remote backend calls is built on it. It checks no permission: the API does, on the paths the format names (Folder, RecordPath, LatestPath), before each call. It imports the Terraform backend format package, never an ecosystem http package or the module's adapters (decision 0044, dependency rule 8).
Inputs ¶
- Driving: the Terraform backend API (workspaces, state versions, locks and uploads), and keys rotate, which re-seals stored states through the composition root (Reseal).
- Driven: the item service (Items), whose store (item.Store) holds the states and whose formats give the Terraform backend's the same Keys; the package index; and the master keyring (platform/crypt.Keyring), which seals a state while the repository's encryptStates is on and opens it again.
Outputs ¶
- Workspaces created, read, listed, locked, unlocked and deleted (Workspace, Lock), and state versions written and read (State, StateWrite, LatestVersion), each an item.
- ResealReport: what a re-seal changed.
- Errors the API maps onto statuses: ErrNoRepository, ErrInvalidName, ErrTaken, ErrWritten, ErrWorkspaceChanged, ErrNoKeyring, LockedError, NotLockedError and InvalidStateError.
Invariants ¶
- A state is sealed under the master key while encryptStates is on and opens only with it (decision 0017); the sealed form is stored, so it stays byte-identical.
- A state version's milliseconds name it, so a write stores it at the time it names, and the current state's creation time is how a reindex reads them back.
- LockedError's text is the one go-tfe reads, so it never changes.
References ¶
- Decision 0041: docs/research/decisions/0041-terraform-backend.md.
- Research 32: docs/research/32-terraform-backend.md.
- Decision 0017: docs/research/decisions/0017-sealed-columns-and-master-key.md.
- Decision 0044: docs/research/decisions/0044-modular-monolith-migration.md, "Dependency rules".
- Target layout: docs/research/architecture-options/5-modular-monolith.md, line 209 (core/terraformstate), line 1036 (the mapping row) and rule 2 at lines 35 to 36.
- Plan: docs/implementation/plan.md#p8-pypi-nuget-helm-terraform-locals (the Terraform backend, #557 and #558) and #p9d-modular-monolith-migration, epic ART (https://github.com/weaveplatform/artifactweave/issues/941).
- https://github.com/weaveplatform/artifactweave/issues/1011 and https://github.com/weaveplatform/artifactweave/issues/1088
Status ¶
Implemented P8 to P9c. Moved here in P9d.
Testing ¶
- workspace_test.go: workspaces, locks, force-unlock and deletes.
- state_test.go: state versions, sealing while encryptStates is on, and re-sealing.
- fault_test.go: every store failure surfaces; helpers_test.go builds the service over a store.
Index ¶
- Constants
- Variables
- type InvalidStateError
- type Lock
- type LockedError
- type NotLockedError
- type ResealReport
- type Service
- func (s *Service) CreateWorkspace(ctx context.Context, c repo.Config, name, terraformVersion, by string) (Workspace, error)
- func (s *Service) CurrentState(ctx context.Context, c repo.Config, name string) (State, error)
- func (s *Service) DeleteWorkspace(ctx context.Context, c repo.Config, name, by string) error
- func (s *Service) ForceUnlock(ctx context.Context, c repo.Config, name string) (Workspace, error)
- func (s *Service) Lock(ctx context.Context, c repo.Config, name, by string) (Workspace, error)
- func (s *Service) NewState(ctx context.Context, c repo.Config, name, by string) (int64, error)
- func (s *Service) ReadState(ctx context.Context, c repo.Config, name string, ms int64) ([]byte, State, error)
- func (s *Service) Repository(ctx context.Context, key string) (repo.Config, error)
- func (s *Service) Reseal(ctx context.Context) (ResealReport, error)
- func (s *Service) ResolveState(ctx context.Context, id string) (repo.Config, State, error)
- func (s *Service) ResolveWorkspace(ctx context.Context, id string) (repo.Config, Workspace, error)
- func (s *Service) State(ctx context.Context, c repo.Config, name string, ms int64) (State, error)
- func (s *Service) States(ctx context.Context, c repo.Config, name string) ([]State, error)
- func (s *Service) Unlock(ctx context.Context, c repo.Config, name, by string) (Workspace, error)
- func (s *Service) Workspace(ctx context.Context, c repo.Config, name string) (Workspace, error)
- func (s *Service) Workspaces(ctx context.Context, c repo.Config) ([]Workspace, error)
- func (s *Service) WriteState(ctx context.Context, c repo.Config, name string, w StateWrite) (State, error)
- type State
- type StateWrite
- type Workspace
Constants ¶
const LatestVersion = "latest"
LatestVersion is the Terraform version of a workspace created without one, which Terraform's version checks accept from any client.
Variables ¶
var ( // ErrNoRepository is a key that names no local terraformbackend // repository: no organisation. ErrNoRepository = fmt.Errorf("terraformstate: no local terraform backend repository: %w", kernel.ErrNotFound) // ErrNoWorkspace is a workspace that does not exist. ErrNoWorkspace = fmt.Errorf("terraformstate: no such workspace: %w", kernel.ErrNotFound) // ErrNoState is a state version that is not stored, or a workspace // without a current state. ErrNoState = fmt.Errorf("terraformstate: no such state version: %w", kernel.ErrNotFound) // ErrInvalidName is a workspace name that cannot name a folder (422). ErrInvalidName = terraformbackend.ErrInvalidName // ErrTaken is a workspace create for a name that exists (422). ErrTaken = errors.New("terraformstate: the workspace name is taken") // ErrWritten is a write of a state version that is stored already (409). ErrWritten = errors.New("terraformstate: the state version is written already") // ErrWorkspaceChanged refuses a capability for an earlier workspace record. ErrWorkspaceChanged = errors.New("terraformstate: the workspace was replaced") // ErrNoKeyring is a state to seal or open without a master key. ErrNoKeyring = terraformbackend.ErrNoKeyring )
Errors the API maps onto statuses. The three that wrap kernel.ErrNotFound answer 404.
Functions ¶
This section is empty.
Types ¶
type InvalidStateError ¶
type InvalidStateError struct {
Message string
}
InvalidStateError is a state write whose content is no Terraform state or differs from what the request declared (422).
func (*InvalidStateError) Error ¶
func (e *InvalidStateError) Error() string
Error returns Message, which says why the state was refused and which the backend API answers with as it is.
type LockedError ¶
type LockedError struct {
Workspace, Holder string
}
LockedError is a workspace whose lock refuses the caller: any holder's for a lock, and another user's for an unlock, a state write or a delete. Its text is the one go-tfe reads: "is locked by User" turns a refused unlock into "unable to unlock workspace locked by user".
func (*LockedError) Error ¶
func (e *LockedError) Error() string
Error returns the text go-tfe reads ("Workspace <name> is locked by User <holder>"), so it stays byte-identical.
type NotLockedError ¶
type NotLockedError struct {
Workspace string
}
NotLockedError is an unlock or a force-unlock of a workspace no one has locked.
func (*NotLockedError) Error ¶
func (e *NotLockedError) Error() string
Error returns "Workspace <name> is not locked", the refusal the backend API answers with.
type ResealReport ¶
type ResealReport struct {
// Resealed counts the states sealed anew under the active key version.
Resealed int
// Unreadable lists the states sealed under an older version that did
// not open, by repository and path. They were lost before the
// rotation: retiring the old version loses nothing more.
Unreadable []string
}
ResealReport is what Reseal did.
type Service ¶
type Service struct {
// Items writes and deletes the items. Its formats must give the
// Terraform backend's the same Keys, so the hooks read what this
// seals.
Items *item.Service
// Keys seals states while encryptStates is on and opens them. Without
// it such a write fails, and a sealed state does not open.
Keys *crypt.Keyring
// Now is the clock; nil means time.Now.
Now func() time.Time
}
Service keeps the Terraform backend's workspaces and states.
func (*Service) CreateWorkspace ¶
func (s *Service) CreateWorkspace( ctx context.Context, c repo.Config, name, terraformVersion, by string, ) (Workspace, error)
CreateWorkspace creates workspace name in c with its record, naming terraformVersion, or LatestVersion when it is empty. A name that cannot be a folder is ErrInvalidName, and one taken ErrTaken.
func (*Service) CurrentState ¶
CurrentState returns workspace name's current state, ErrNoState when it has none.
func (*Service) DeleteWorkspace ¶
DeleteWorkspace deletes workspace name of c with its states, into the trash can while it is on. Another user's lock refuses it with a *LockedError; the caller's own does not, as Terraform locks the workspace it deletes.
func (*Service) ForceUnlock ¶
ForceUnlock releases anyone's lock on workspace name of c: a *NotLockedError when there is none.
func (*Service) Lock ¶
Lock locks workspace name of c for by. Any lock refuses it with a *LockedError, the caller's own included, as Terraform takes a lock once.
func (*Service) NewState ¶
NewState names the next state version of workspace name, for a create that returns an upload URL: WriteState then stores it under that name. Nothing is stored, so the name is the next one only until another write. Another user's lock refuses it with a *LockedError.
func (*Service) ReadState ¶
func (s *Service) ReadState( ctx context.Context, c repo.Config, name string, ms int64, ) ([]byte, State, error)
ReadState returns the plain content of state version ms of workspace name, unsealed when it is sealed, with the version.
func (*Service) Repository ¶
Repository returns the local terraformbackend repository key names: the organisation of decision 0041. Any other key is ErrNoRepository.
func (*Service) Reseal ¶
func (s *Service) Reseal(ctx context.Context) (ResealReport, error)
Reseal seals every state of every terraformbackend repository that is sealed under an older key version anew under the active one, so the older master key can be retired (decision 0041, "encryptStates"), as `artifactweave keys rotate` does for the sealed columns. Each state's item keeps its path, times and properties; only its content, its checksums and its size change, and the index follows.
func (*Service) ResolveState ¶
ResolveState reads a state version ID, "<workspace ID>__<ms>".
func (*Service) ResolveWorkspace ¶
ResolveWorkspace reads a workspace ID, "<repository key>__<workspace name>": the first split from the left whose repository holds a workspace of that name (decision 0041, "Identifiers") [I].
func (*Service) Unlock ¶
Unlock releases by's lock on workspace name of c: a *NotLockedError when there is none, and a *LockedError when another user holds it.
func (*Service) Workspace ¶
Workspace returns workspace name of c, which exists while its record does.
func (*Service) Workspaces ¶
Workspaces lists c's workspaces by name.
func (*Service) WriteState ¶
func (s *Service) WriteState( ctx context.Context, c repo.Config, name string, w StateWrite, ) (State, error)
WriteState stores w's state as workspace name's current state (decision 0041, "Storage"), in one transaction: the current state, if any, moves to its timestamped name, the new one becomes state.latest.json, and the format's hooks index both. The state is sealed under the master key while c's encryptStates is on (or unset, which means on); otherwise it is stored as it is.
The state must be a Terraform state whose MD5 and serial are the declared ones, or an *InvalidStateError. Another user's lock refuses it with a *LockedError; the holder and anyone writing to an unlocked workspace may write. A version w.Ms names that is stored already is ErrWritten. Serial and lineage are not compared with the current state's: Terraform does that, and -force skips it.
type State ¶
type State struct {
Repo, Workspace string
// Ms names the version: the epoch milliseconds at which it was created.
Ms int64
// Current marks the workspace's current state, state.latest.json.
Current bool
// Path is the state file's path in the repository.
Path string
// Meta is what the package index keeps of it: its serial, lineage, MD5
// and size. An Unreadable state does not open under this server's
// master key, or is no state.
Meta tfbe.Meta
CreatedBy string
}
State is one state version of a workspace.
type StateWrite ¶
type StateWrite struct {
// WorkspaceRecordID binds a deferred upload to the workspace that issued
// it. Zero leaves this precondition unused for ordinary inline writes.
WorkspaceRecordID int64
// Ms is the version's milliseconds when an earlier request named it, as
// a create that returned an upload URL does (NewState); zero takes the
// next.
Ms int64
// Body is the raw state.
Body io.Reader
// MD5 and Serial are what the request declared: the hex MD5 of Body,
// and the serial Body's state holds.
MD5 string
Serial int64
By string
}
StateWrite is a state to store as a workspace's current state.
type Workspace ¶
type Workspace struct {
// RecordID identifies this incarnation of workspace.json, including when
// a workspace is deleted and recreated within the same millisecond.
RecordID int64
Repo string
Name string
// TerraformVersion is the version the create named, or LatestVersion.
TerraformVersion string
Created time.Time
CreatedBy string
// Updated is when the workspace was created or its current state
// written, whichever is later.
Updated time.Time
// Lock is the lock, nil while the workspace is unlocked.
Lock *Lock
}
Workspace is one workspace of a repository.