Member Junction
    Preparing search index...

    Class EncryptionKeySourceBaseAbstract

    Abstract base class for encryption key source providers.

    Key sources are responsible for securely retrieving encryption key material from various backends. The MemberJunction encryption system uses the ClassFactory pattern to instantiate the appropriate provider based on database configuration.

    • Never log or expose key material - Key bytes should only be returned via the GetKey() method and immediately used for crypto operations.

    • Validate inputs - Always validate lookupValue parameters to prevent injection attacks against your backend.

    • Use secure connections - For network-based sources (vaults, KMS), always use TLS and verify certificates.

    • Handle errors securely - Don't expose internal details in error messages that could help attackers.

    1. Construction - Provider is instantiated with config
    2. Initialize() - Called once before first use (async setup)
    3. GetKey()/KeyExists() - Called for each operation
    4. Dispose() - Called during cleanup (close connections)

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    Configuration passed during instantiation. Contains lookupValue and any source-specific additional config.

    Accessors

    • get SourceName(): string

      Human-readable name of this key source.

      Used for logging, error messages, and UI display. Should be concise but descriptive.

      Returns string

      'Environment Variable', 'AWS KMS', 'HashiCorp Vault'
      

    Methods

    • Optional cleanup for sources with connections or resources.

      Called during graceful shutdown to release resources. Use this for:

      • Closing vault connections
      • Releasing pooled resources
      • Flushing any pending operations

      The default implementation is a no-op for stateless sources.

      Override in subclasses that hold resources

      Returns Promise<void>

    • Retrieves the raw key material for the given lookup value.

      • Return key bytes directly, don't cache them in the provider
      • Never log the key material
      • Throw descriptive errors on failure (without exposing secrets)
      • Validate lookupValue format before using it

      Keys should be base64-encoded in the source storage. The provider decodes and returns raw bytes.

      Parameters

      • lookupValue: string

        Identifier for the key in this source

        • Env vars: the variable name (e.g., 'MJ_ENCRYPTION_KEY_PII')
        • Config files: the key name (e.g., 'pii_master_key')
        • Vaults: the secret path (e.g., '/secrets/encryption/pii')
      • OptionalkeyVersion: string

        Optional version for versioned key stores. Some sources (like vaults) maintain multiple versions. If not specified, returns the current/latest version.

      Returns Promise<Buffer>

      Promise resolving to raw key bytes as a Buffer. The buffer length must match the algorithm's KeyLengthBits/8.

      Error if the key cannot be retrieved with a descriptive message

      // Simple retrieval
      const key = await source.GetKey('MJ_ENCRYPTION_KEY_PII');

      // With version for rotation
      const oldKey = await source.GetKey('MJ_ENCRYPTION_KEY_PII', '1');
      const newKey = await source.GetKey('MJ_ENCRYPTION_KEY_PII', '2');
    • Optional async initialization for sources that need setup.

      Called once by the encryption engine before first use. Use this for:

      • Loading config files
      • Establishing connections to vault services
      • Authenticating with cloud key management
      • Caching frequently-accessed metadata

      The default implementation is a no-op for simple sources.

      Override in subclasses that need async initialization

      Returns Promise<void>

    • Checks if a key exists without retrieving it.

      Used for validation before operations that would fail on missing keys. More efficient than GetKey() when you only need existence check.

      Parameters

      • lookupValue: string

        Identifier for the key in this source

      Returns Promise<boolean>

      Promise resolving to true if key exists, false otherwise

      if (await source.KeyExists('NEW_ROTATION_KEY')) {
      // Safe to proceed with key rotation
      } else {
      throw new Error('New key must be set before rotation');
      }
    • Validates that the source is properly configured.

      Called before attempting key operations to fail fast on misconfiguration. Check for:

      • Required config values are present
      • Config values are in expected format
      • Connectivity to backend (for network sources)

      Returns boolean

      true if configuration is valid, false otherwise

    • Validates that a specific key is accessible and usable from this source.

      Each provider implements source-specific validation logic and returns actionable remediation messages on failure. This keeps key-source-specific knowledge encapsulated within the provider rather than in the engine.

      • MUST NOT expose key material to the caller — key bytes stay inside the provider
      • MUST return { IsAccessible: true } only if key material can be retrieved and passes all validation (exists, valid format, correct length if expectedKeyLengthBytes provided)
      • MUST return a human-readable Error string with remediation steps on failure
      • SHOULD catch all exceptions internally and return them as Error strings

      Parameters

      • lookupValue: string

        Identifier for the key in this source

      • OptionalkeyVersion: string

        Optional version for versioned key stores

      • OptionalexpectedKeyLengthBytes: number

        Optional expected key length in bytes for validation. When provided, the provider should retrieve the key and verify its length matches.

      Returns Promise<KeyValidationResult>

      Promise resolving to a validation result

      const result = await source.ValidateKeyAccessibility('MJ_BASE_ENCRYPTION_KEY', '1', 32);
      if (!result.IsAccessible) {
      console.error(result.Error);
      // "Environment variable "MJ_BASE_ENCRYPTION_KEY" is not set.
      // Set it with: export MJ_BASE_ENCRYPTION_KEY=$(openssl rand -base64 32)"
      }