Documentation
¶
Overview ¶
Package writeonlyhash provides bcrypt-backed hashing for write-only secret attributes stored in Terraform resource private state.
Index ¶
- Constants
- func AddPrivateStateUnavailableError(diags *diag.Diagnostics)
- type Hasher
- func (h *Hasher) Compute(value string) ([]byte, error)
- func (h *Hasher) ComputeForPrivateState(value string) ([]byte, diag.Diagnostics)
- func (h *Hasher) DecodeFromPrivateState(raw []byte) ([]byte, error)
- func (h *Hasher) EncodeForPrivateState(hash []byte) ([]byte, error)
- func (h *Hasher) IsUpToDate(value string, storedHash []byte) bool
- func (h *Hasher) Matches(value string, storedHash []byte) bool
- func (h *Hasher) PrivateStateKey(attributePath string) string
Constants ¶
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. )
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 ¶
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 ¶
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 ¶
DecodeFromPrivateState reverses EncodeForPrivateState. It returns (nil, nil) for an empty or absent value.
func (*Hasher) EncodeForPrivateState ¶
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 ¶
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 ¶
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 ¶
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.