saltstack / saltstack/salt

[BUG] ini.options_present can squash duplicate keys without notifying

Open
#66,159 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug needs-triage
Dominant language
Python
Stars
15.7k
Forks
5.6k
Avg merge
2d 44m
Merged PRs (30d)
80

Description

Description
The ini.options_present state seems to be incapable of detecting duplicate options within a section. I haven't looked at the code yet, but I suspect it's reading the file in as a data structure, and duplicate keys are "squashed" during that process such that the last option in the section "wins".

Steps to Reproduce the behavior
The following code can be used to reproduce the problem:

file_manage_/tmp/test.ini:
  file.managed:
    - name: /tmp/test.ini
    - contents: |
        [main]
        hello=goodbye
        hello=world

ini_manage_/tmp/test.ini:
  ini.options_present:
    - name: /tmp/test.ini
    - strict: true
    - sections:
        main:
          hello: world

cat_/tmp/test.ini:
  cmd.run:
    - name: cat /tmp/test.ini

Note that the ini.options_present state does not report changes, but the file has been changed.

local:                                       
----------                                   
          ID: file_manage_/tmp/test.ini      
    Function: file.managed                   
        Name: /tmp/test.ini                  
      Result: True                           
     Comment: File /tmp/test.ini updated     
     Started: 13:12:12.351370
    Duration: 18.701 ms                      
     Changes:                                
              ----------     
              diff:    
                  New file                   
----------                                   
          ID: ini_manage_/tmp/test.ini       
    Function: ini.options_present            
        Name: /tmp/test.ini
      Result: True
     Comment: No anomaly detected
     Started: 13:12:12.372671
    Duration: 2.419 ms
     Changes:   
----------
          ID: cat_/tmp/test.ini
    Function: cmd.run
        Name: cat /tmp/test.ini
      Result: True
     Comment: Command "cat /tmp/test.ini" run
     Started: 13:12:12.375980
    Duration: 4.859 ms
     Changes:   
              ----------
              pid:
                  736320
              retcode:
                  0
              stderr:
              stdout:
                  [main]
                  hello = world

Summary for local
------------
Succeeded: 3 (changed=2)
Failed:    0
------------
Total states run:     3
Total run time:  25.979 ms

Swapping the first and second occurrence of the option will then show the change.

file_manage_/tmp/test.ini:
  file.managed:
    - name: /tmp/test.ini
    - contents: |
        [main]
        hello=world
        hello=goodbye

ini_manage_/tmp/test.ini:
  ini.options_present:
    - name: /tmp/test.ini
    - strict: true
    - sections:
        main:
          hello: world

cat_/tmp/test.ini:
  cmd.run:
    - name: cat /tmp/test.ini
local:                                       
----------                                   
          ID: file_manage_/tmp/test.ini      
    Function: file.managed                   
        Name: /tmp/test.ini                  
      Result: True                           
     Comment: File /tmp/test.ini updated     
     Started: 13:19:03.324753                
    Duration: 15.315 ms                      
     Changes:                                
              ----------                     
              diff:                          
                  --- 
                  +++ 
                  @@ -1,2 +1,3 @@
                   [main]
                  -hello = world
                  +hello=world
                  +hello=goodbye
----------
          ID: ini_manage_/tmp/test.ini
    Function: ini.options_present
        Name: /tmp/test.ini
      Result: True
     Comment: Changes take effect
     Started: 13:19:03.342683
    Duration: 2.984 ms
     Changes:   
              ----------
              main:
                  ----------
                  hello:
                      ----------
                      after:
                          world
                      before:
                          goodbye
----------
          ID: cat_/tmp/test.ini
    Function: cmd.run
        Name: cat /tmp/test.ini
      Result: True
     Comment: Command "cat /tmp/test.ini" run
     Started: 13:19:03.346327
    Duration: 4.595 ms
     Changes:   
              ----------
              pid:
                  737599
              retcode:
                  0
              stderr:
              stdout:
                  [main]
                  hello = world

Summary for local
------------
Succeeded: 3 (changed=3)
Failed:    0
------------
Total states run:     3
Total run time:  22.894 ms

Expected behavior
Any time modifications are made to a file, they should be reported as a change.

Versions Report

salt --versions-report (Provided by running salt --versions-report. Please also mention any differences in master/minion versions.)
Salt Version:
             Salt: 3006.7
 
Python Version:
           Python: 3.10.13 (main, Feb 19 2024, 03:31:20) [GCC 11.2.0]
 
Dependency Versions:
             cffi: 1.14.6
         cherrypy: 18.6.1
         dateutil: 2.8.1
        docker-py: Not Installed
            gitdb: Not Installed
        gitpython: Not Installed
           Jinja2: 3.1.3
          libgit2: Not Installed
     looseversion: 1.0.2
         M2Crypto: Not Installed
             Mako: Not Installed
          msgpack: 1.0.2
     msgpack-pure: Not Installed
     mysql-python: Not Installed
        packaging: 22.0
        pycparser: 2.21
         pycrypto: Not Installed
     pycryptodome: 3.19.1
           pygit2: Not Installed
     python-gnupg: 0.4.8
           PyYAML: 6.0.1
            PyZMQ: 23.2.0
           relenv: 0.15.1
            smmap: Not Installed
          timelib: 0.2.4
          Tornado: 4.5.3
              ZMQ: 4.3.4
 
System Versions:
             dist: rhel 9.3 Plow
           locale: utf-8
          machine: x86_64
          release: 5.14.0-362.18.1.el9_3.x86_64
           system: Linux
          version: Red Hat Enterprise Linux 9.3 Plow
 

Additional context
Related (at least in part) to the logic and outcome experienced in #65191

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the Salt ini.options_present state entry point and reproduce the issue with the YAML configuration shown, comparing both duplicate-key orderings. Trace how the INI file is read and how changes are detected; done means modifications caused by duplicate options are reported consistently, with regression coverage for the reproduced behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devops, infrastructure
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.