Class InMemorySecretStore

java.lang.Object
org.apache.juneau.commons.secret.InMemorySecretStore
All Implemented Interfaces:
SecretStore

public class InMemorySecretStore extends Object implements SecretStore
Built-in default SecretStore: a per-process, ConcurrentHashMap-backed key-to-secret map.

This is the zero-config store SecretStores.resolve(org.apache.juneau.commons.inject.BeanStore) falls back to when no store has been contributed. It is honest about its limits:

Process-local only. Secrets live in this JVM's heap and are gone when the process exits — this store is not persistent. Two separate JVMs (or two instances behind a load balancer) each hold their own map and do not see each other's secrets, so it is not cross-node. It is a convenience default and a test double, not a production secret backend; deployments that need durability or sharing must contribute a store backed by an external secret manager.

Defensive copies. store(java.lang.String, char[]) copies the supplied array and find(java.lang.String) returns a fresh copy, so the caller may zero its own arrays without disturbing the stored value and vice-versa. delete(java.lang.String) zeroes the retained array before dropping it. The value is never toString()'d, logged, or dumped.

Thread-safety. All operations are safe for concurrent invocation.

Since:
10.0.0
  • Constructor Details

  • Method Details

    • store

      public void store(String key, char[] secret)
      Description copied from interface: SecretStore
      Stores a secret under the specified key, replacing any existing value.

      The caller retains ownership of secret and may zero it after this call returns; an implementation that needs to retain the value must copy it.

      Specified by:
      store in interface SecretStore
      Parameters:
      key - The key under which to store the secret. Must not be null.
      secret - The secret value. Must not be null.
    • find

      public Optional<char[]> find(String key)
      Description copied from interface: SecretStore
      Returns the secret stored under the specified key.

      The returned array is the caller's to own and zero; it is not a live view of the store's internal state.

      Specified by:
      find in interface SecretStore
      Parameters:
      key - The key to look up. Must not be null.
      Returns:
      The secret value, or Optional.empty() if no secret is stored under the key.
    • exists

      public boolean exists(String key)
      Description copied from interface: SecretStore
      Returns whether a secret is stored under the specified key without materializing its value.
      Specified by:
      exists in interface SecretStore
      Parameters:
      key - The key to check. Must not be null.
      Returns:
      true if a secret is stored under the key.
    • delete

      public boolean delete(String key)
      Description copied from interface: SecretStore
      Deletes the secret stored under the specified key.
      Specified by:
      delete in interface SecretStore
      Parameters:
      key - The key to delete. Must not be null.
      Returns:
      true if a secret was present and removed; false if the key was already absent.