Skip to content

Detect duplicate entries in single-valued Describables #2659

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

shivammm21
Copy link

@shivammm21 shivammm21 commented Apr 5, 2025

Fixes #2589

This PR adds detection for when configuration files contain multiple entries for a single-valued Describable component. Previously the plugin would silently accept this invalid configuration, potentially leading to unexpected behavior as one configuration would override the other without warning.

Problem

When users configure components like the OIC plugin with both wellKnown and manual server configuration entries, the Configuration as Code plugin accepts both entries despite only one being valid. This causes one option to silently override the other without any warning, leading to confusion and unexpected behavior.

Implementation

  • Modified BaseConfigurator.configure() method to check for Describable attributes
  • Added detection logic to count non-null entries in the Mapping for single-valued Describables
  • When multiple entries are detected, the system now:
    • Issues a warning message via context.warning() in normal mode
    • Throws a ConfiguratorException in strict mode (when unknown attributes are set to "reject")
  • Detailed error messages identify which specific entries are conflicting

Technical Details

// Added detection logic for multiple entries in single-valued Describables
if (attribute instanceof DescribableAttribute && !attribute.isMultiple() && sub instanceof Mapping) {
    Mapping mapping = sub.asMapping();
    // Count the number of entries that might be conflicting configurations
    int configurableEntries = 0;
    for (String key : mapping.keySet()) {
        CNode value = mapping.get(key);
        if (value != null && !(value instanceof Scalar && ((Scalar) value).isNull())) {
            configurableEntries++;
        }
    }
    
    if (configurableEntries > 1) {
        String message = String.format("Multiple configurations found for single-valued Describable '%s'. Conflicting entries: %s. Only one can be used.",
                attribute.getName(), String.join(", ", mapping.keySet()));
        context.warning(sub, message);
        if (context.getUnknown() == ConfigurationContext.Unknown.reject) {
            throw new ConfiguratorException(this, message);
        }
        LOGGER.warning(message);
    }
}

Testing

Added DuplicateEntriesDescribableTest.java with two test methods:

  • shouldWarnOnDuplicateEntries(): Verifies warning behavior in normal mode
  • shouldRejectDuplicateEntriesInStrictMode(): Verifies exception behavior in strict mode

Created test components that mimic the described OIC authentication plugin scenario to ensure the fix correctly identifies conflict cases.

Your checklist for this pull request

  • Make sure you are requesting to pull a topic/feature/bugfix branch (right side) and not your master branch!
  • Ensure that the pull request title represents the desired changelog entry
  • Please describe what you did
  • Link to relevant issues in GitHub or in Jenkins JIRA: Fixes CasC allows duplicate entries for single valued Describables without warning. #2589
  • Link to relevant pull requests, esp. upstream and downstream changes
  • Did you provide a test-case? That demonstrates feature works or fixes the issue.

@shivammm21 shivammm21 requested a review from a team as a code owner April 5, 2025 18:10
@timja timja changed the title Fix for issue #2589: Detect duplicate entries in single-valued Describables Detect duplicate entries in single-valued Describables Apr 5, 2025
@timja
Copy link
Member

timja commented Apr 5, 2025

Your tests don't compile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CasC allows duplicate entries for single valued Describables without warning.
2 participants