Package org.confng.sources
Class SecretManagerSource
- java.lang.Object
-
- org.confng.sources.SecretManagerSource
-
- All Implemented Interfaces:
ConfigSource
public abstract class SecretManagerSource extends Object implements ConfigSource
Abstract base class for secret manager implementations.This class provides common functionality for secret manager sources such as caching, key mapping, and error handling.
- Since:
- 1.0
- Author:
- Bharat Kumar Malviya, GitHub: github.com/imBharatMalviya
- See Also:
ConfigSource
-
-
Field Summary
Fields Modifier and Type Field Description protected booleancacheEnabledprotected longcacheTimeoutMsprotected Map<String,Long>cacheTimestampsprotected Map<String,String>keyMappingsprotected Map<String,String>secretCache
-
Constructor Summary
Constructors Modifier Constructor Description protectedSecretManagerSource()Creates a new secret manager source with caching disabled.protectedSecretManagerSource(long cacheTimeoutMs)Creates a new secret manager source with caching enabled.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidaddKeyMapping(String configKey, String secretId)Maps a configuration key to a secret identifier.voidclearCache()Clears the secret cache.protected abstract StringfetchSecret(String secretId)Fetches a secret from the secret manager.Optional<String>get(String key)Retrieves a configuration value for the given key.intgetCacheSize()Gets the number of cached secrets.intgetPriority()Returns the priority of this configuration source.protected voidhandleSecretFetchError(String configKey, String secretId, Exception error)Handles errors that occur while fetching secrets.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.confng.sources.ConfigSource
getName
-
-
-
-
Constructor Detail
-
SecretManagerSource
protected SecretManagerSource(long cacheTimeoutMs)
Creates a new secret manager source with caching enabled.- Parameters:
cacheTimeoutMs- cache timeout in milliseconds (0 = no timeout)
-
SecretManagerSource
protected SecretManagerSource()
Creates a new secret manager source with caching disabled.
-
-
Method Detail
-
addKeyMapping
public void addKeyMapping(String configKey, String secretId)
Maps a configuration key to a secret identifier.- Parameters:
configKey- the configuration keysecretId- the secret identifier in the secret manager
-
get
public Optional<String> get(String key)
Description copied from interface:ConfigSourceRetrieves a configuration value for the given key.If the key is not found in this source, an empty Optional should be returned. This allows the ConfNG system to try other sources in the resolution chain.
- Specified by:
getin interfaceConfigSource- Parameters:
key- the configuration key to look up- Returns:
- an Optional containing the value if found, empty otherwise
-
fetchSecret
protected abstract String fetchSecret(String secretId) throws Exception
Fetches a secret from the secret manager.- Parameters:
secretId- the secret identifier- Returns:
- the secret value, or null if not found
- Throws:
Exception- if an error occurs while fetching the secret
-
handleSecretFetchError
protected void handleSecretFetchError(String configKey, String secretId, Exception error)
Handles errors that occur while fetching secrets.- Parameters:
configKey- the configuration key that was requestedsecretId- the secret identifiererror- the error that occurred
-
clearCache
public void clearCache()
Clears the secret cache.
-
getCacheSize
public int getCacheSize()
Gets the number of cached secrets.
-
getPriority
public int getPriority()
Description copied from interface:ConfigSourceReturns the priority of this configuration source.Sources with higher priority values are consulted first in the resolution chain. Default priorities:
- 100+ - Secret managers and secure sources
- 80-99 - TestNG parameters and test-specific sources
- 60-79 - Environment variables
- 40-59 - System properties
- 20-39 - Configuration files (JSON, Properties)
- 0-19 - Default and fallback sources
- Specified by:
getPriorityin interfaceConfigSource- Returns:
- the priority value, higher values have higher priority
-
-