Skip to content

Bug: ConsulConfigSource.loadAllConfig() doesn't handle null return from getValues() #232

@sfloess

Description

@sfloess

Description

ConsulConfigSource.loadAllConfig() calls kvClient.getValues() and iterates over the result without checking if it's null. The Consul library may return null if the key prefix doesn't exist, causing NPE.

Location

jplatform-config-consul/src/main/java/org/flossware/jplatform/config/consul/ConsulConfigSource.java:229-244

Current Code

private void loadAllConfig() {
    try {
        KeyValueClient kvClient = consul.keyValueClient();
        List<Value> values = kvClient.getValues(config.getKeyPrefix());  // May return null!
        
        for (Value value : values) {  // NPE if values is null!
            if (value.getValueAsString().isPresent()) {
                String fullKey = value.getKey();
                String key = extractKey(fullKey);
                String val = value.getValueAsString().get();
                configCache.put(key, val);
            }
        }
    } catch (Exception e) {
        logger.warn("Failed to load config from Consul", e);
    }
}

Problem

From Consul Java client library documentation, getValues() can return null if:

  • Key prefix doesn't exist in Consul
  • Consul returns empty result
// First time starting with empty Consul
ConsulConfigSource source = new ConsulConfigSource(config);
source.start();  // Calls loadAllConfig()

// If keyPrefix doesn't exist in Consul:
// kvClient.getValues(config.getKeyPrefix()) returns null
// for (Value value : null) throws NPE!
// Caught by outer try-catch, logged as warning
// But configCache remains empty - silent failure!

Impact

  • NPE when Consul has no keys under the prefix
  • Silent failure - just logged as warning
  • Empty config cache even if some configs should exist
  • Poor error handling for first-time setup
  • Confusing behavior for users

Fix

private void loadAllConfig() {
    try {
        KeyValueClient kvClient = consul.keyValueClient();
        List<Value> values = kvClient.getValues(config.getKeyPrefix());
        
        if (values == null || values.isEmpty()) {
            logger.info("No configuration found at prefix: {}", config.getKeyPrefix());
            return;
        }
        
        for (Value value : values) {
            if (value.getValueAsString().isPresent()) {
                String fullKey = value.getKey();
                String key = extractKey(fullKey);
                String val = value.getValueAsString().get();
                configCache.put(key, val);
            }
        }
        
        logger.debug("Loaded {} config entries from Consul", configCache.size());
    } catch (Exception e) {
        logger.warn("Failed to load config from Consul", e);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions