salt icon indicating copy to clipboard operation
salt copied to clipboard

salt.util.http.query method post and headers

Open PabloLemos opened this issue 9 years ago • 4 comments

Description of Issue/Question

I have some troubles with the http client in salt.utils to build my own module that ask for ip to the IPAM appliance. I use a proxy configuration to use a REST/APi function that give me some ips. The minion call the givemeip function in the IPAM appliance to obtain the next ip available.

When i use postman to test the REST/Api function works well. But when i translate the call into salt with the salt.util.http.query function i obtain a HTTP 400 Error: Bad Request

Setup

The code of the module is:

  def _query(method, _Object, version = '2.3.1',
             params = None, data = None):
    headers = {}
    headers['Content-Type'] = 'application/json'

    if data:
        data = json.dumps(data)

    HTTPRequest = salt.utils.http.query(szURL + _Object,
                                        method = method,
                                        headers_dict = headers,
                                        username = szUserName,
                                        password = szPassword,
                                        params = params,
                                        data = data,
                                        verify_ssl = False,
                                        decode = True,
                                        opts = __opts__)

    return HTTPRequest

  def reserve(ip = None, network = None):
   jsonData = {'name': __grains__['fqdn'],
                'ipv4addrs': [{'ipv4addr': ''}],
                "extattrs": {"id_inventory": {"value": str(__grains__['inventory'])}}}
    jsonData['ipv4addrs'][0]['ipv4addr'] = 'func:nextavailableip:{0}'.format(network)
    HTTPResponse = _query("POST", "givemeip", data =jsonData)

    return HTTPResponse

Steps to Reproduce Issue

The debug information is:

[DEBUG ] Requesting URL https://xxx.xxx.xxx.xxx/givemeip using POST method [TRACE ] Request POST Data: '{"name": "hostname.localdomain", "ipv4addrs": [{"ipv4addr": "func:nextavailableip:xxx.xxx.xxx.xxx"}], "extattrs": {"id_inventary": {"value": "193096"}}}' [DEBUG ] POST https://xxx.xxx.xxx.xxx/givemeip (username: 'username') [DEBUG ] About to connect() to proxy xxx.xxx.xxx.xxx port 8080 (#0) [DEBUG ] Trying xxx.xxx.xxx.xxx... [DEBUG ] Connected to xxx.xxx.xxx.xxx port 8080 (#0) [DEBUG ] Establish HTTP proxy tunnel to xxx.xxx.xxx.xxx:443 [DEBUG ] Server auth using Basic with user 'username' [DEBUG ] > CONNECT xxx.xxx.xxx.xxx:443 HTTP/1.1 [DEBUG ] > Host: xxx.xxx.xxx.xxx:443 [DEBUG ] > Proxy-Connection: Keep-Alive [DEBUG ] > User-Agent: Salt/2016.3.0 http.query() [DEBUG ] > [DEBUG ] < HTTP/1.0 200 Connection established [DEBUG ] < [DEBUG ] Proxy replied OK to CONNECT request [DEBUG ] Initializing NSS with certpath: sql:/etc/pki/nssdb [DEBUG ] Connected to xxx.xxx.xxx.xxx port 8080 (#0) [DEBUG ] skipping SSL peer certificate verification [DEBUG ] SSL connection using TLS_DHE_RSA_WITH_AES_256_CBC_SHA [DEBUG ] Server certificate: [DEBUG ] start date: Jun 15 12:28:50 2016 GMT [DEBUG ] expire date: Jun 13 12:28:50 2024 GMT [DEBUG ] common name: infoblox.localdomain [DEBUG ] Server auth using Basic with user 'username' [DEBUG ] > POST /givemeip HTTP/1.1 [DEBUG ] > Authorization: Basic cGxlbW9zOjFfUGxlbW9z [DEBUG ] > Host: xxx.xxx.xxx.xxx [DEBUG ] > Accept: / [DEBUG ] > Accept-Encoding: gzip,deflate [DEBUG ] > User-Agent: Salt/2016.3.0 http.query() [DEBUG ] > Content-Length: 142 [DEBUG ] > Content-Type: application/x-www-form-urlencoded [DEBUG ] > [DEBUG ] > '{"name": "hostname.localdomain", "ipv4addrs": [{"ipv4addr": "func:nextavailableip:xxx.xxx.xxx.xxx"}], "extattrs": {"id_inventary": {"value": "193096"}}}' [DEBUG ] < HTTP/1.1 400 Bad Request [DEBUG ] < Date: Thu, 15 Dec 2016 16:18:57 GMT [DEBUG ] < Cache-Control: no-cache, no-store [DEBUG ] < Pragma: no-cache [DEBUG ] < Content-Type: application/json [DEBUG ] < set-cookie: ibapauth="client=API,group=Grupo%20IPAM%20Sistemas,ctime=1481818737,timeout=1800,mtime=1481818737,ip=xxx.xxx.xxx.xxx,auth=LOCAL,user=username,3kh8MhakfgzzNEk7eZuUqfEtumG5/NnjprQ"; httponly; Path=/; secure [DEBUG ] < Connection: close [DEBUG ] < Transfer-Encoding: chunked [DEBUG ] < [DEBUG ] Closing connection #0 [DEBUG ] Initializing new AsyncZeroMQReqChannel for ('/etc/salt/pki/minion', 'hostname.localdomain', 'tcp://xxx.xxx.xxx.xxx:4506', 'aes') [DEBUG ] Initializing new AsyncAuth for ('/etc/salt/pki/minion', 'hostname.localdomain', 'tcp://xxx.xxx.xxx.xxx:4506') [TRACE ] Inserted key into loop_instance_map id 50556224 for key ('/etc/salt/pki/minion', 'hostname.localdomain', 'tcp://xxx.xxx.xxx.xxx:4506', 'aes') and process 52240 [DEBUG ] LazyLoaded nested.output [TRACE ] data = {'local': {'status': 400, 'error': 'HTTP 400: Bad Request'}}

I dont know why if i set the Content-Type to application-json into the headers why the tornado backend send the request as application/x-www-form-urlencoded.

Versions Report

Salt Version: Salt: 2016.3.0

Dependency Versions: cffi: Not Installed cherrypy: Not Installed dateutil: 1.4.1 gitdb: Not Installed gitpython: Not Installed ioflo: Not Installed Jinja2: 2.2.1 libgit2: Not Installed libnacl: Not Installed M2Crypto: 0.20.2 Mako: Not Installed 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.6.6 (r266:84292, Nov 21 2013, 10:50:32) python-gnupg: Not Installed PyYAML: 3.10 PyZMQ: 14.5.0 RAET: Not Installed smmap: Not Installed timelib: Not Installed Tornado: 4.2.1 ZMQ: 4.0.5

System Versions: dist: centos 6.6 Santiago machine: x86_64 release: 2.6.32-504.30.3.el6.x86_64 system: Linux version: Red Hat Enterprise Linux Server 6.6 Santiago

PabloLemos avatar Dec 15 '16 16:12 PabloLemos

ping @cro or @whiteinge im not sure on this one and haven't been able to find an answer. Would either one of you be able to help out @PabloLemos

Ch3LL avatar Dec 20 '16 21:12 Ch3LL

@PabloLemos the function signature for that call is daunting to say the least. It should be header_dict instead of headers_dict. (I made that exact same mistake when testing this.) Changing it to the singular form caused my server to see the correct content-type in the request. Does that do the trick for you too?

whiteinge avatar Dec 21 '16 02:12 whiteinge

Thanks @whiteinge !!! You are right!!. That was the problem. It was a silly mistake but very difficult to debugging. :)

Thanks a lot

PabloLemos avatar Dec 21 '16 05:12 PabloLemos

Thanks for your help on this @whiteinge ! I'll go ahead and close this since it seems the issue is resolved.

Ch3LL avatar Dec 22 '16 21:12 Ch3LL