salt icon indicating copy to clipboard operation
salt copied to clipboard

ACL state module: misleading KeyError when named file system entry doesn't exist

Open mbunkus opened this issue 10 years ago • 5 comments

The documentation for the Linux ACL module suggests that naming the file system entry with a name attribute should work:

/var/log/unattended-upgrades is readable by the NRPE user:
  acl.present:
    - name: /var/log/unatteded-upgrades
    - acl_type: user
    - acl_name: nagios
    - perms: rx

Unfortunately I always get a KeyError exception:

mandorallen:
----------
          ID: /var/log/unattended-upgrades is readable by the NRPE user
    Function: acl.present
        Name: /var/log/unatteded-upgrades
      Result: False
     Comment: An exception occurred in this state: Traceback (most recent call last):
                File "/usr/lib/python2.7/dist-packages/salt/state.py", line 1733, in call
                  **cdata['kwargs'])
                File "/usr/lib/python2.7/dist-packages/salt/loader.py", line 1651, in wrapper
                  return f(*args, **kwargs)
                File "/usr/lib/python2.7/dist-packages/salt/states/linux_acl.py", line 69, in present
                  _current_perms = __current_perms[name]
              KeyError: '/var/log/unatteded-upgrades'
     Started: 10:25:28.737175
    Duration: 8.555 ms
     Changes:

However, if I change the state's name to match the directory name like this…

/var/log/unattended-upgrades:
  acl.present:
    - acl_type: user
    - acl_name: nagios
    - perms: rx

…then things will magically work without a problem.

Version information from the minion:

Salt Version:
           Salt: 2016.3.2

Dependency Versions:
           cffi: Not Installed
       cherrypy: Not Installed
       dateutil: 2.4.2
          gitdb: Not Installed
      gitpython: Not Installed
          ioflo: Not Installed
         Jinja2: 2.8
        libgit2: Not Installed
        libnacl: Not Installed
       M2Crypto: Not Installed
           Mako: 1.0.3
   msgpack-pure: Not Installed
 msgpack-python: 0.4.6
   mysql-python: Not Installed
      pycparser: Not Installed
       pycrypto: 2.6.1
         pygit2: Not Installed
         Python: 2.7.12 (default, Jul  1 2016, 15:12:24)
   python-gnupg: Not Installed
         PyYAML: 3.11
          PyZMQ: 15.2.0
           RAET: Not Installed
          smmap: Not Installed
        timelib: Not Installed
        Tornado: 4.2.1
            ZMQ: 4.1.4

System Versions:
           dist: Ubuntu 16.04 xenial
        machine: x86_64
        release: 4.4.0-34-generic
         system: Linux
        version: Ubuntu 16.04 xenial

Version information from the master:

Salt Version:
           Salt: 2016.3.1

Dependency Versions:
           cffi: Not Installed
       cherrypy: Not Installed
       dateutil: 2.5.3
          gitdb: 0.6.4
      gitpython: 1.0.1
          ioflo: Not Installed
         Jinja2: 2.8
        libgit2: Not Installed
        libnacl: Not Installed
       M2Crypto: 0.24.0
           Mako: 1.0.4
   msgpack-pure: Not Installed
 msgpack-python: 0.4.7
   mysql-python: Not Installed
      pycparser: Not Installed
       pycrypto: 2.6.1
         pygit2: Not Installed
         Python: 2.7.11 (default, Mar 31 2016, 06:18:34)
   python-gnupg: Not Installed
         PyYAML: 3.11
          PyZMQ: 15.2.0
           RAET: Not Installed
          smmap: 0.9.0
        timelib: Not Installed
        Tornado: 4.3
            ZMQ: 4.1.5

System Versions:
           dist:
        machine: x86_64
        release: 4.6.3-1-ARCH
         system: Linux
        version: Not Installed

mbunkus avatar Aug 19 '16 08:08 mbunkus

@mbunkus When using pdb to debug this issue I realized I was actually running into this error because of this: 'getfacl: /var/log/unatteded-upgrades: No such file or directory'. Getfacl was actually returning back with an error and so when the return from the call to acl.getfacl came back it returned nothing for __curent_perms. So when making the call _current_perms = __current_perms[name] name does not exist, because its empty due to the error.

We need to be spitting out a better error then Keyerror when there is an issue with running getfacl so I will definitely label a bug for that reason.

In the meantime lets try to figure out which exact error you are running into. Could you just temporarily try adding a print to the execution module modules/linux_acl.py:

         out = __salt__['cmd.run'](cmd, python_shell=False).splitlines()                                                                                                   
         print('print out {0}'.format(out))

And you should be able to see that output when running the command.

Ch3LL avatar Aug 19 '16 20:08 Ch3LL

@Ch3LL as you can see in the description, I think the root cause is there is no reaction from the function to not-exist acl file.

- name: /var/log/unatteded-upgrades I think this file name has a typo and resolve to a not-exist file.

I just link a PR to fix this. Feel free to take a look.

hu-dabao avatar Aug 20 '16 00:08 hu-dabao

Thanks for the quick feedback. Woops, you're all right, of course, that's a stupid mistake. I thought I had simply copy & pasted that path between the state's name and the name attribute, but obviously I hadn't. Sorry for that.

A nicer error message if the path couldn't be found would be highly appreciated.

Here's the output of the requested modification:

2016-08-20 08:27:01,556 [salt.loaded.int.module.cmdmod][ERROR   ][27035] output: getfacl: /var/log/unatteded-upgrades: No such file or directory

It does indeed confirm that the cause is my spelling mistake.

Edit: after correcting the spelling mistake the following state works nicely:

/var/log/unattended-upgrades must be readable by the NRPE user:
  acl.present:
    - name: /var/log/unattended-upgrades
    - acl_type: user
    - acl_name: nagios
    - perms: rx

mbunkus avatar Aug 20 '16 06:08 mbunkus

@mbunkus yeah, I update the PR to make it return with noop when the file/dir doesn't exist.

hu-dabao avatar Aug 20 '16 19:08 hu-dabao

Since #35615 fixes this and has been merged in to 2016.3, I'll close this.

Thanks everyone!

rallytime avatar Nov 01 '16 16:11 rallytime