salt icon indicating copy to clipboard operation
salt copied to clipboard

chattr in file.managed applied at the wrong time

Open styx-tdo opened this issue 7 years ago • 11 comments

Description of Issue

when using file.managed, an immutable file causes an error. When using attrs: "" - that removes immutable, but only AFTER the content change (that fails, immutable is never removed)

Setup

echo "aa" > /somefile chattr +i /somefile

Steps to Reproduce Issue

state: some file: file.managed: - name: /somefile - attr: "" - content: "xx"

salt runs with: Failed to commit change: [Errno 1] Operation not permitted if the file content is ok, immutable flag gets properly removed

Versions Report

Salt Version: Salt: 2018.3.4

Dependency Versions: cffi: 1.6.0 cherrypy: unknown dateutil: Not Installed docker-py: Not Installed gitdb: 0.6.4 gitpython: 1.0.1 ioflo: Not Installed Jinja2: 2.7.2 libgit2: 0.26.3 libnacl: Not Installed M2Crypto: 0.28.2 Mako: Not Installed msgpack-pure: Not Installed msgpack-python: 0.5.6 mysql-python: 1.2.5 pycparser: 2.14 pycrypto: 2.6.1 pycryptodome: Not Installed pygit2: 0.26.4 Python: 2.7.5 (default, Oct 30 2018, 23:45:53) python-gnupg: Not Installed PyYAML: 3.11 PyZMQ: 15.3.0 RAET: Not Installed smmap: 0.9.0 timelib: Not Installed Tornado: 4.2.1 ZMQ: 4.1.4

System Versions: dist: centos 7.6.1810 Core locale: UTF-8 machine: x86_64 release: 4.15.0-54-generic system: Linux version: CentOS Linux 7.6.1810 Core

styx-tdo avatar Dec 02 '19 15:12 styx-tdo

So by default the attr function seems to be intended for the state of the file after as shown, so Salt can edit it, then you add the immutable bit for example. This causes an issue if you want to edit a file and remove immutable, but also if you just want to update a file where you you have attrs: i

There likely needs to be some additional logic that breaks attr into 2 opts/codepaths like attrs_before and attrs_after. Without this there is no way to remove the immutable bit from a file in Salt without writing 2 states. It might be that if we are editing the file, attrs_before should exclude i by default, but this behavior has been accepted for so long we might not want to make that change.

jwolfe-ns avatar Nov 20 '20 18:11 jwolfe-ns

remove-immutable:
  file.managed:
    - name: /tmp/moo
    - attrs: ''

edit-contents:
  file.managed:
    - name: /tmp/moo
    - mode: '0440'
    - attrs: i
    - contents: 'stuff!'

jwolfe-ns avatar Nov 20 '20 18:11 jwolfe-ns

@styx-tdo did this solve your issue?

sagetherage avatar Jun 16 '21 01:06 sagetherage

I think this needs a fix. We have the case where the corporate group policy forces PermitRootLogin no, but on most of our servers, we are running kubernetes and need root ssh from the management clusters. That was approved by security, but corporate will not enter an exception in the group policy for us. So, we added an immutable attribute to the sshd_config file. I'm trying:

remove_immutable:
  file.managed:
    - name: /etc/ssh/sshd_config
    - replace: false
    - attrs: ''
    - prereq:
      - file: etc_ssh_sshd_config

etc_ssh_sshd_config:
  file.managed:
    - name: /etc/ssh/sshd_config
    - source: salt://{{ DIR }}/sshd_config.template
    - template: jinja
    - mode: 600
    - user: root
    - group: root
    - attrs: i
    - backup: minion
    - defaults:
      hsts: {{ hsts }}
      yesno: "{{ yesno }}"

Restart_sshd_After_Update:
  cmd.wait:
    - watch:
      - file: /etc/ssh/sshd_config
    - name: /sbin/service sshd restart
    - cwd: /

Which causes the immutable to be removed even if no change is made to the sshd_config file. So, all three stanzas fire every time. I only want the first and last one to fire if there is going to be change to the middle one. I'm not really sure how to achieve that? Maybe have the middle one twice and the prereq stanza depend on the first copy failing? Can I do that?

bradawk1 avatar Oct 19 '23 16:10 bradawk1

something else must be changing in that file @bradawk1 or something else is changing the immutable.

cause here is an example showing that

  1. prereq does work at least in 3006.3 for what you are trying.
  2. that all three don't show corrections. even when using a template.
  3. is based off your example to be as close as possible without breaking my own system. the only changes are to the files and the command being run.

note in the example below in the output the immutable is not being removed and readded.

root@salt00:/srv/salt/tests# salt-call state.apply tests.test
local:
----------
          ID: remove_immutable
    Function: file.managed
        Name: /tmp/test
      Result: True
     Comment: No changes detected
     Started: 17:54:54.740528
    Duration: 0.008 ms
     Changes:
----------
          ID: etc_ssh_sshd_config
    Function: file.managed
        Name: /tmp/test
      Result: True
     Comment: File /tmp/test is in the correct state
     Started: 17:54:54.740842
    Duration: 61.067 ms
     Changes:
----------
          ID: Restart_sshd_After_Update
    Function: cmd.wait
        Name: echo test
      Result: True
     Comment:
     Started: 17:54:54.803127
    Duration: 0.98 ms
     Changes:

Summary for local
------------
Succeeded: 3
Failed:    0
------------
Total states run:     3
Total run time:  62.055 ms
root@salt00:/srv/salt/tests# cat /srv/salt/tests/test.sls
remove_immutable:
  file.managed:
    - name: /tmp/test
    - replace: false
    - attrs: ''
    - prereq:
      - file: etc_ssh_sshd_config

etc_ssh_sshd_config:
  file.managed:
    - name: /tmp/test
    - source: salt://tests/files/foobar.jinja
    - template: jinja
    - mode: 600
    - user: root
    - group: root
    - attrs: i
    - backup: minion
    - defaults:
        foobar: no

Restart_sshd_After_Update:
  cmd.wait:
    - watch:
      - file: /tmp/test
    - name: echo test
    - cwd: /

whytewolf avatar Oct 19 '23 18:10 whytewolf

Consider this simpler case:

/tmp/test-file_v1:
  file.managed:
    - name: /tmp/test-file
    - mode: 666
    - attrs: i
    - contents: foo

/tmp/test-file_v2:
  file.managed:
    - name: /tmp/test-file
    - mode: 666
    - attrs: i
    - contents: bar

My expectation is that when the state completed I would end up with a file named /tmp/test-file that contained 'bar'. In reality I get an error:

$ salt-call state.apply test.immutable 
[ERROR   ] File changed:
--- 
+++ 
@@ -1 +1 @@
-foo
+bar

local:
----------
          ID: /tmp/test-file_v1
    Function: file.managed
        Name: /tmp/test-file
      Result: True
     Comment: File /tmp/test-file is in the correct state
     Started: 12:35:56.472659
    Duration: 18.606 ms
     Changes:   
----------
          ID: /tmp/test-file_v2
    Function: file.managed
        Name: /tmp/test-file
      Result: False
     Comment: Failed to commit change: [Errno 1] Operation not permitted: '/tmp/test-file'
     Started: 12:35:56.491423
    Duration: 10.838 ms
     Changes:   
              ----------
              diff:
                  --- 
                  +++ 
                  @@ -1 +1 @@
                  -foo
                  +bar

Summary for local
------------
Succeeded: 1 (changed=1)
Failed:    1
------------
Total states run:     2
Total run time:  29.444 ms

This isn't quite the same issue as was originally reported but it is closely related.

For reference:

$ salt-call --version
salt-call 3006.3 (Sulfur)

ender8282 avatar Oct 19 '23 18:10 ender8282

@ender8282 I never said the original issue wasn't an issue. and your simplification is actually a more complicated version of the original issue and in no way related to what @bradawk1 was doing.

I was pointing out @bradawk1 version of the issue is NOT the same thing as this issue. and also does not seem to be an issue in the latest released version. his version is using prereq to remove the immutable option before hand. which is perfectly reasonable workaround. and works as expected in the latest 3006.3. if it is showing files changed in his example then something else is changing the files on 3006.3, another state somewhere, or something outside of salt.

whytewolf avatar Oct 19 '23 23:10 whytewolf

First, we are running 3005.1. Converting to 3006 is an upcoming task. Also, our sshd_config file is a jinja template and not a simple flat file. I do some jinja before hand to set some variables. If a minion is not a member of a kubernetes cluster then we want: Match Host all PermitRootLogin no set. If a minion is a member of clusters 1,3,4,8, or 9 then we want: Match Host mc1* PermitRootLogin yes If a minion is a member of clusters 2,5,6,7 then we want: Match Host mc2* PermitRootLogin yes With my current code, even if no change is required, the remove chattr bit happens, then it is reapplied and the ssh daemon is restarted. I wanted to avoid that. If you say this is fixed in 3006, then I'll hold off and re-try once we finish converting. Thanks!

bradawk1 avatar Oct 23 '23 09:10 bradawk1

@bradawk1 in my example of prereq working the example was also a jinja template, not a flat file. I would say try 3006.3

whytewolf avatar Oct 23 '23 16:10 whytewolf

Is someone still working on this issue? If not, can I work on this issue?

timothy-choi avatar Oct 13 '26 04:10 timothy-choi

Thanks for the original report. This issue has been open for more than three years without recent activity and carries a severity label below severity-high. As part of a backlog cleanup, we're closing older low- and medium-severity issues so triage can focus on reports against currently supported releases.

Closing isn't a judgement that the problem wasn't real — only that the codebase has moved through several releases since this was filed, and much of the surrounding code has changed.

If you — or anyone else — can still reproduce this on the 3008.x LTS release, please leave a comment with a reproduction and a maintainer will reopen. The original reporter can also reopen directly.

dwoz avatar Oct 15 '26 23:10 dwoz