public class SecretsProvider extends Object
Purpose
at a given point in time. The active secret may
change over time as the old secret is revoked or rotated. Previously active secrets can be looked up using
SecretsProvider.getNamedSecret(Purpose, String)
using the secret's stable id
. Alternatively,
all still-valid secrets for a given purpose can be obtained via the SecretsProvider.getValidSecrets(Purpose)
method.
As not all secret storage backends are appropriate for storing all kinds of secrets, the provider allows different
SecretStore
s to be configured for one or more different purposes, via the
SecretsProvider.setActiveStore(SecretStore, Purpose[])
method. This installs the provided store as the preferred storage
backend for that purpose. All requests for active secrets for that purpose will be routed to that backend. Any
previously configured stores for that purpose will be retained but only queried to find existing named or valid
secrets.
A set of default stores can be configured using SecretsProvider.setDefaultStores(SecretStore, SecretStore[])
.
These stores will be consulted if no specific store has been configured for a particular purpose. By default there
is no default store and so a NoSuchSecretException
is thrown by any attempt to access a secret for a
purpose without a specific store configured.
Constructor and Description |
---|
SecretsProvider() |
Modifier and Type | Method and Description |
---|---|
<S extends Secret> |
getActiveSecret(Purpose<S> purpose)
Gets the currently active secret for the given purpose.
|
<S extends Secret> |
getNamedOrValidSecrets(Purpose<S> purpose,
String id)
If the given id is not null, then this returns the single named secret that corresponds to that stable id (or
a stream of valid secrets for the given
purpose if no such secret exists), otherwise it returns all
valid secrets for the given purpose. |
<S extends Secret> |
getNamedSecret(Purpose<S> purpose,
String id)
Gets the secret for the given purpose with the given stable secret id.
|
<S extends Secret> |
getValidSecrets(Purpose<S> purpose)
Returns all secrets for the given purpose which have not yet expired.
|
<T extends Secret> |
setActiveStore(SecretStore<? super T> store,
Purpose<? extends T>... purposes)
Sets the active store to use for the given purpose.
|
protected <T extends Secret> |
setActiveStore(SecretStore<? super T> store,
Purpose<? extends T> purpose)
Sets the active store to use for the given purpose.
|
SecretsProvider |
setDefaultStores(SecretStore<?> activeStore,
SecretStore<?>... defaultStores)
Sets the default store(s) to use if there is no specific store configured for a particular purpose.
|
@SafeVarargs public final <T extends Secret> SecretsProvider setActiveStore(SecretStore<? super T> store, Purpose<? extends T>... purposes)
This method delegates to SecretsProvider.setActiveStore(SecretStore, Purpose)
to make the change.
T
- the type of secrets required by this purpose.purposes
- the purpose(s) to change the active store for.store
- the new secret store to make active for this purpose.protected <T extends Secret> void setActiveStore(SecretStore<? super T> store, Purpose<? extends T> purpose)
T
- the type of secrets required by this purpose.purpose
- the purpose(s) to change the active store for.store
- the new secret store to make active for this purpose.public SecretsProvider setDefaultStores(SecretStore<?> activeStore, SecretStore<?>... defaultStores)
activeStore
- the store to use for all requests for active secrets.defaultStores
- remaining valid stores to consult for existing named/valid secrets.public <S extends Secret> Promise<S,NoSuchSecretException> getActiveSecret(Purpose<S> purpose)
NoSuchSecretException
is thrown instead.
The active secret is found by first consulting the currently active store for the purpose label. If no active stores exist for the purpose, all default stores are consulted, and the first matching secret is used.
S
- the type of secret to return.purpose
- the purpose for which the secret is intended to be used.NoSuchSecretException
if
one cannot be found.public <S extends Secret> Promise<S,NoSuchSecretException> getNamedSecret(Purpose<S> purpose, String id)
S
- the type of secret to returnpurpose
- the purpose for which the secret is intended to be used.id
- the stable id of the particular secret to get.Secret.getStableId()
public <S extends Secret> Promise<Stream<S>,NeverThrowsException> getValidSecrets(Purpose<S> purpose)
S
- the type of secret to return.purpose
- the purpose for which the secrets are intended for.public <S extends Secret> Promise<Stream<S>,NeverThrowsException> getNamedOrValidSecrets(Purpose<S> purpose, String id)
purpose
if no such secret exists), otherwise it returns all
valid secrets for the given purpose. This is a convenience method for a frequent case where you want to
process an incoming message (e.g., to decrypt or verify it) and the message may or may not have a secret/key
identifier.
For example, to verify a JSON Web Token that might have a "kid" claim, we can do:
SignedJwt jwt = ...;
secrets.getNamedOrValidSecrets(Purpose.VERIFY, jwt.getHeader().getKeyId())
.map(rethrowFunction(key -> signingManager.newVerificationHandler(key)))
.anyMatch(jwt::verify);
S
- the type of secrets to return.purpose
- the purpose for which the secrets are intended.id
- the optional stable id of the secret, or null if not known.Copyright © 2010-2018, ForgeRock All Rights Reserved.