Creates a new key source instance.
Note: The config may be empty/undefined when using ClassFactory. Configuration is typically loaded from the database and passed when retrieving keys.
Optionalconfig: EncryptionKeySourceConfigOptional configuration for the key source
Protected_Configuration passed during instantiation. Contains lookupValue and any source-specific additional config.
Optional cleanup for sources with connections or resources.
Called during graceful shutdown to release resources. Use this for:
The default implementation is a no-op for stateless sources.
Override in subclasses that hold resources
Retrieves key material from an environment variable.
The environment variable should contain a base64-encoded key. For versioned keys, the version is appended with an underscore:
KEY_NAME for version 1 (default)KEY_NAME_V2 for version 2The environment variable name
OptionalkeyVersion: stringOptional version number (defaults to '1')
Promise resolving to the decoded key bytes
Optional async initialization for sources that need setup.
Called once by the encryption engine before first use. Use this for:
The default implementation is a no-op for simple sources.
Override in subclasses that need async initialization
Checks if an environment variable containing a key exists.
The environment variable name to check
Promise resolving to true if the variable is defined
Validates the source configuration.
For environment variables, configuration is always valid as keys are validated at lookup time. This allows the source to be used dynamically for any environment variable.
Always returns true
Validates that the encryption key in the specified environment variable is accessible and usable.
Checks: env var exists, is non-empty, contains valid base64, and (if specified) has correct length. Key material is decoded internally for validation only — never returned to the caller.
OptionalkeyVersion: stringOptionalexpectedKeyLengthBytes: number
Encryption key source that retrieves keys from environment variables.
This is the default and recommended key source for:
Keys are expected to be base64-encoded strings in the environment. The provider decodes them to raw bytes for crypto operations.
Example