saltstack / saltstack/salt

SDB REST Module does not work without a URI Query Parameter

Open
#44,954 9 comments 0 reactions 1 assignee View on GitHub

@MKLeb is already working on this.

Since Aug 31, 2023.

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

Description

Description of Issue/Question

REST SDB module cannot be used without specifying a request parameter. The documentation indicates that the SDB URI can be used without requiring any query parameters and the SDB REST configuration allows for configuration without a template variable. When attempting to retrieve the value without specifying a query parameter, the following error is given:

Exception occurred in runner sdb.get: Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/salt/client/mixins.py", line 382, in _low
    data['return'] = self.functions[fun](*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/salt/runners/sdb.py", line 27, in get
    return salt.utils.sdb.sdb_get(uri, __opts__, __utils__)
  File "/usr/lib/python2.7/dist-packages/salt/utils/sdb.py", line 46, in sdb_get
    return loaded_db[fun](query, profile=profile)
  File "/usr/lib/python2.7/dist-packages/salt/sdb/rest.py", line 94, in get
    return query(key, None, service, profile)
  File "/usr/lib/python2.7/dist-packages/salt/sdb/rest.py", line 115, in query
    for pair in comps[1].split('&'):
IndexError: list index out of range

Looking at the code for '/salt/sdb/rest.py', the logic is such that it always assumes there is a value from performing the split('?') and does not perform any safety checks to ensure that it is not there:

'''
    Get a value from the REST interface
    '''
    comps = key.split('?')
    key = comps[0]
    key_vars = {}
    for pair in comps[1].split('&'):
        pair_key, pair_val = pair.split('=')
        key_vars[pair_key] = pair_val

In order to get the SDB value to return properly, you have to pass any arbitrary query string to the sdb URI in order for the value to return:

Assuming the SDB configuration in the Setup section below:
This does not work: salt-run sdb.get sdb://my-rest-api/urls
This works: salt-run sdb.get sdb://my-rest-api/urls?foo=bar

Setup

SDB Rest configuration from the rest example:

/etc/salt/master.d/sdb-github.sls

my-rest-api:
  driver: rest
  urls:
    url: https://api.github.com/
  keys:
    url: https://api.github.com/users/{{user}}/keys
    backend: requests
Steps to Reproduce Issue
  1. Add the above SDB configuration to /etc/salt/master.d/github.sls
  2. Execute the following Salt Run:
salt-run sdb.get sdb://my-rest-api/urls
Versions Report
Salt Version:
           Salt: 2017.7.2

Dependency Versions:
           cffi: Not Installed
       cherrypy: 3.5.0
       dateutil: 2.4.2
      docker-py: Not Installed
          gitdb: 0.6.4
      gitpython: 1.0.1
          ioflo: Not Installed
         Jinja2: 2.8
        libgit2: Not Installed
        libnacl: 1.6.1
       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
   pycryptodome: Not Installed
         pygit2: Not Installed
         Python: 2.7.12 (default, Nov 19 2016, 06:48:10)
   python-gnupg: Not Installed
         PyYAML: 3.11
          PyZMQ: 15.2.0
           RAET: Not Installed
          smmap: 0.9.0
        timelib: Not Installed
        Tornado: 4.2.1
            ZMQ: 4.1.4

System Versions:
           dist: Ubuntu 16.04 xenial
         locale: UTF-8
        machine: x86_64
        release: 4.4.0-87-generic
         system: Linux
        version: Ubuntu 16.04 xenial

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.