updating-secrets - v0.3.1
    Preparing search index...

    Class UpdatingSecrets<Secrets>

    This class:

    • Loads all secrets from all given adapters and consolidates the values
    • Automatically reloads all secrets on an interval
    • Aborts automatic reloads after a customizable number of consecutive failures
    • Provides access to all the latest secret values with the UpdatingSecrets.get property

    Make sure to call and await UpdatingSecrets.loadSecrets at least once before using UpdatingSecrets.get for the first time. Alternatively, consider using createUpdatingSecrets which handles that async setup automatically.

    Type Parameters

    Index

    Constructors

    Properties

    adapters: readonly Readonly<BaseSecretsAdapter>[]

    A list of adapters to load secrets from. Order here matters: all values loaded from the last adapters will override the previous values. Meaning, the first adapter can be used as a "default" value source while all subsequent adapters will override its values.

    consecutiveFailureCount: number = 0
    currentSecrets: undefined | SecretValues<Secrets>
    isDestroyed: boolean = false
    loadingSecretsPromise: undefined | Promise<SecretValues<Secrets>>

    Keeps track of the last triggered UpdatingSecrets.loadSecrets call to prevent multiple calls of it from overlapping: only one can be running at a time.

    nextUpdateTimeout: undefined | Timeout
    options: RequiredAndNotNull<UpdatingSecretsOptions>
    processedSecrets: ProcessedSecretDefinitions

    Accessors

    Methods

    • Compare a value underComparison to a secret's currently known secret value that was defined with rotatableSecretShape.

      Parameters

      • underComparison: string

        The uncontrolled string to compare to your secrets.

        This could be, for example, an API key used by someone else trying to authenticate with your service.

      • actualRotatableSecretValue: { current: string; legacy?: string }

        The current value of the secret as obtained by UpdatingSecrets.get.

        • current: string

          The latest up-to-date version of the secret's value.

        • Optionallegacy?: string

          The optional legacy value for the secret. Use for graceful secret rotation.

      Returns boolean

      import {
      createUpdatingSecrets,
      rotatableSecretShape,
      defineSecrets,
      StaticSecretsAdapter,
      } from 'updating-secrets';

      const updatingSecrets = await createUpdatingSecrets(
      defineSecrets({
      apiKey: {
      description: '',
      whereToFind: '',
      shape: rotatableSecretShape,
      },
      }),
      [
      new StaticSecretsAdapter({
      apiKey: {
      current: 'latest key',
      legacy: 'old key',
      },
      }),
      ],
      );

      export async function handleApiRequest(request: Request) {
      const apiKey = request.headers.get('apiKey');
      if (!apiKey) {
      throw new Error('API request missing the required API key.');
      } else if (
      !updatingSecrets.compareRotatableSecret(apiKey, updatingSecrets.get.apiKey)
      ) {
      throw new Error('Invalid API key.');
      }
      }