Class CredentialsProviderService
UpdatableCredential components when credentials change.
This service implements the credential rotation pattern for Kubernetes environments. It
watches the secrets directory for changes using WatchService (typically mounted by a
secrets manager like HashiCorp Vault, OpenBao, or the External Secrets Operator). When
credentials change, all registered connection pools are notified to update their credentials.
File Structure
The service expects the following files in the secrets directory:
username- Contains the database usernamepassword- Contains the database password
Kubernetes Secret Mounting
Kubernetes mounts secrets via atomic symlink swaps on the ..data directory. Because
individual credential files are symlinks, the watch is registered on the parent directory rather
than the files themselves, so that the directory-level events fired during the symlink swap are
captured.
Configuration Properties
k8s.secrets.path- Base directory for secret files (default:/var/run/secrets/database)k8s.secrets.refreshInterval- Fallback poll timeout in milliseconds; a credential re-check is forced after this interval even if no watch event fires (default: 30000)
Thread Safety
This service is thread-safe. Credential reads and updates are performed atomically, and the list of updatable components is safely managed.
- See Also:
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCredentialsProviderService(String secretsPath, long refreshIntervalMs) Creates a new credentials provider reading from the specified secrets path. -
Method Summary
Modifier and TypeMethodDescriptionvoidsetHikariUpdatable(UpdatableCredential<String> updatable) Registers the HikariCP credentials updater to receive credential change notifications.voidsetUcpUpdatable(UpdatableCredential<String> updatable) Registers the Oracle UCP credentials updater to receive credential change notifications.voidstart()Validates file permissions on startup and starts the directory watch thread.voidstop()Stops the directory watch thread and closes theWatchService.
-
Field Details
-
usernamePath
Path to the file containing the database username. -
passwordPath
Path to the file containing the database password.
-
-
Constructor Details
-
CredentialsProviderService
public CredentialsProviderService(@Value("${k8s.secrets.path:/var/run/secrets/database}") String secretsPath, @Value("${k8s.secrets.refreshInterval:30000}") long refreshIntervalMs) Creates a new credentials provider reading from the specified secrets path.The provider will look for
usernameandpasswordfiles within the specified directory.- Parameters:
secretsPath- base path where Kubernetes mounts the secret files; defaults to/var/run/secrets/databaserefreshIntervalMs- fallback poll timeout in milliseconds; a credential re-check is forced after this interval even if no watch event fires; defaults to 30000
-
-
Method Details
-
start
Validates file permissions on startup and starts the directory watch thread.Checks if secret files are world-readable and logs a security warning if they are. On non-POSIX filesystems, the permission check is skipped.
- Throws:
IOException- if theWatchServicecannot be created or the directory cannot be registered
-
stop
@PreDestroy public void stop()Stops the directory watch thread and closes theWatchService. -
setHikariUpdatable
@Autowired @Qualifier("hikariUpdater") public void setHikariUpdatable(UpdatableCredential<String> updatable) Registers the HikariCP credentials updater to receive credential change notifications.- Parameters:
updatable- the HikariCP credentials updater bean
-
setUcpUpdatable
@Autowired @Qualifier("ucpUpdater") public void setUcpUpdatable(UpdatableCredential<String> updatable) Registers the Oracle UCP credentials updater to receive credential change notifications.- Parameters:
updatable- the Oracle UCP credentials updater bean
-