Class 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 Detail

      • cacheEnabled

        protected final boolean cacheEnabled
      • cacheTimeoutMs

        protected final long cacheTimeoutMs
      • cacheTimestamps

        protected final Map<String,​Long> cacheTimestamps
    • 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 key
        secretId - the secret identifier in the secret manager
      • get

        public Optional<String> get​(String key)
        Description copied from interface: ConfigSource
        Retrieves 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:
        get in interface ConfigSource
        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 requested
        secretId - the secret identifier
        error - 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: ConfigSource
        Returns 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:
        getPriority in interface ConfigSource
        Returns:
        the priority value, higher values have higher priority