writeonlyhash

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: Apache-2.0 Imports: 7 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 writeonlyhash provides bcrypt-backed hashing for write-only secret attributes stored in Terraform resource private state.

Index

Constants

View Source
const (

	// PrivateStateUnavailableSummary and PrivateStateUnavailableDetail are the
	// standard diagnostic shown when a write-only secret cannot be persisted
	// because the resource's private state writer is unavailable.
	PrivateStateUnavailableSummary = "Failed to persist write-only secret hash"
	PrivateStateUnavailableDetail  = "internal: private state writer unavailable; drift detection will not function. This is a bug."
)

Variables

This section is empty.

Functions

func AddPrivateStateUnavailableError

func AddPrivateStateUnavailableError(diags *diag.Diagnostics)

AddPrivateStateUnavailableError appends the standard diagnostic reported when a write-only secret cannot be persisted because the resource's private state writer is unavailable.

Types

type Hasher

type Hasher struct {
	// Salt is the per-resource-type salt derived from the resourceTypeName
	// passed to New. It namespaces values before bcrypt hashing.
	Salt []byte

	// Cost is the bcrypt cost parameter. Zero means defaultCost (10).
	Cost int
}

Hasher manages bcrypt-based private-state hashes for write-only attributes. It binds a per-resource-type salt so the same secret value produces different hashes across resource types. Cost controls bcrypt work factor; when Cost is zero, Compute uses the default of 10. A Hasher must not be shared across resource types—construct one per resource via New with that resource's stable type identifier.

func New

func New(resourceTypeName string) *Hasher

New returns a Hasher whose salt is derived from resourceTypeName. Use a stable, unique string per Terraform resource type (for example "elasticsearch_connector" or "fleet_cloud_connector"). The same plaintext value hashed under different resource type names will not verify on another Hasher.

func (*Hasher) Compute

func (h *Hasher) Compute(value string) ([]byte, error)

Compute returns a bcrypt hash of value suitable for storage in resource private state. When Cost is zero, the default bcrypt cost of 10 is used. Errors never include the input value.

func (*Hasher) ComputeForPrivateState

func (h *Hasher) ComputeForPrivateState(value string) ([]byte, diag.Diagnostics)

ComputeForPrivateState hashes value and encodes it for private-state storage in one step, returning resource-agnostic diagnostics if either step fails.

func (*Hasher) DecodeFromPrivateState

func (h *Hasher) DecodeFromPrivateState(raw []byte) ([]byte, error)

DecodeFromPrivateState reverses EncodeForPrivateState. It returns (nil, nil) for an empty or absent value.

func (*Hasher) EncodeForPrivateState

func (h *Hasher) EncodeForPrivateState(hash []byte) ([]byte, error)

EncodeForPrivateState wraps a bcrypt hash as a JSON string. The Terraform Plugin Framework rejects private-state values that are not valid JSON, so raw bcrypt bytes (which contain '$' and other non-JSON characters) must be JSON-encoded before storage.

func (*Hasher) IsUpToDate

func (h *Hasher) IsUpToDate(value string, storedHash []byte) bool

IsUpToDate reports whether storedHash already verifies value, meaning the caller can skip recomputing a hash. bcrypt is intentionally slow, so callers should skip rehashing whenever the previously stored hash still verifies the configured value (for example on updates driven by other attributes, or no-op applies).

func (*Hasher) Matches

func (h *Hasher) Matches(value string, storedHash []byte) bool

Matches reports whether value corresponds to storedHash as produced by Compute on this Hasher. It returns false when the value does not match or when bcrypt comparison fails for any reason.

func (*Hasher) PrivateStateKey

func (h *Hasher) PrivateStateKey(attributePath string) string

PrivateStateKey returns a stable private-state key for the write-only attribute identified by attributePath. The format is secret_hash:<attributePath> (for example secret_hash:aws.external_id or secret_hash:configuration_values["password"].secret_value). attributePath is not modified; the caller must supply a path that uniquely identifies the attribute, including map key indices in bracket notation.

Jump to

Keyboard shortcuts

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