Class RotateSigningKeysUseCase
java.lang.Object
com.irurueta.authserver.authorization.application.keys.RotateSigningKeysUseCase
Use case that keeps the signing keys rotating. One call, made at start-up and then on a schedule, does whatever is
due: it creates the
active and next keys when they are missing, promotes next to
active and retires the old active key once the rotation interval has elapsed, always leaves a
fresh next key behind, and deletes keys retired for longer than the effective retention (see
SigningKeyRetention). A second call right after the first changes nothing, and the whole call runs under
the repository's rotation lock, so several instances can call it safely. executeForced() is the
administrator's path: it replaces the active key immediately, whatever its age.-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final ClockSource of the current time.private final DurationHow long a key signs before it is replaced (authserver.keys.rotation-interval, default 30 days).private final SigningKeyGeneratorPort that generates keys.private final SigningKeyRepositoryPort that stores the keys.private final SigningKeyRetentionDecides how long a retired key stays published. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidexecute()Does whatever the key lifecycle requires at the current instant: the scheduled path, which only rotates a key that has signed for the whole rotation interval.voidRotates now, whatever the age of the active key: the active key is retired, the next key promoted and a new next key generated (for example after a suspected key compromise).private static SigningKeyfirst(List<SigningKey> keys, SigningKeyStatus status) Finds the first key in a state.private booleanisDue(SigningKey active, Instant now) Tells whether the active key has signed for the whole rotation interval.private static booleanisExpired(SigningKey retired, Instant now, Duration retention) Tells whether a retired key has been retired for longer than the retention.private voidApplies the lifecycle transitions that are due, or forced.private voidrun(boolean force) Applies the lifecycle transitions while holding the rotation lock.
-
Field Details
-
signingKeyRepository
Port that stores the keys. -
signingKeyGenerator
Port that generates keys. -
clock
Source of the current time. -
rotationInterval
How long a key signs before it is replaced (authserver.keys.rotation-interval, default 30 days). -
signingKeyRetention
Decides how long a retired key stays published.
-
-
Constructor Details
-
RotateSigningKeysUseCase
public RotateSigningKeysUseCase()
-
-
Method Details
-
execute
public void execute()Does whatever the key lifecycle requires at the current instant: the scheduled path, which only rotates a key that has signed for the whole rotation interval. -
executeForced
public void executeForced()Rotates now, whatever the age of the active key: the active key is retired, the next key promoted and a new next key generated (for example after a suspected key compromise). Expired retired keys are deleted too. -
run
private void run(boolean force) Applies the lifecycle transitions while holding the rotation lock.- Parameters:
force- whether the active key is replaced even when it is not due.
-
rotate
Applies the lifecycle transitions that are due, or forced.- Parameters:
now- current instant.force- whether the active key is replaced even when it is not due.retention- how long a retired key stays published.
-
first
Finds the first key in a state.- Parameters:
keys- the keys.status- the state.- Returns:
- the key, or
nullwhen none is in that state.
-
isDue
Tells whether the active key has signed for the whole rotation interval.- Parameters:
active- the active key.now- current instant.- Returns:
truewhen it must be replaced.
-
isExpired
Tells whether a retired key has been retired for longer than the retention.- Parameters:
retired- the retired key.now- current instant.retention- how long a retired key stays published.- Returns:
truewhen it can be deleted.
-