saltstack / salt

Software to automate the management and configuration of any infrastructure or application at scale. Get access to the Salt software package repository here:
https://repo.saltproject.io/
Apache License 2.0
14.15k stars 5.48k forks source link

[BUG] consul module incorrectly reports error on success #61115

Open tomdoherty opened 2 years ago

tomdoherty commented 2 years ago

Description When putting requests to Consul, no response is received. We just get a HTTP 200 to indicate success. This means salt.utils.http.query throws exception Expecting value: line 1 column 1 (char 0) in _query here as there is nothing to decode. Modifying salt.utils.http.query to use decode=False resolves this

Setup Here is an example state that installs/configures consul and can manifest the issue

repo:
  pkgrepo.managed:
    - name: hashicorp
    - humanname: Hashicorp Stable - $basearch
    - baseurl: https://rpm.releases.hashicorp.com/RHEL/$releasever/$basearch/stable
    - gpgcheck: 1
    - gpgkey: https://rpm.releases.hashicorp.com/gpg

consul:
  pkg.installed

config:
  file.managed:
    - name: /etc/consul.d/consul.hcl
    - contents: |
        data_dir = "/opt/consul"
        server = true
        bind_addr = "0.0.0.0"
        advertise_addr = "127.0.0.1"
        bootstrap_expect = 1

consul service:
  service.running:
    - name: consul
    - enable: True
    - reload: True
    - watch:
      - file: /etc/consul.d/consul.hcl

consul.agent_service_register:
  module.run:
    - consul_url: "http://localhost:8500"
    - kwargs:
        name: "testing"
        id: "testing"
        port: 9100
        address: "localhost"
        check:
          interval: "5m"
          http: "http://localhost:9100/metrics"

If you run the above you will see the service is correctly created:

[vagrant@master ~]$ consul catalog services
consul
testing
[vagrant@master ~]$ 

But salt incorrectly reports an error:

----------
          ID: consul.agent_service_register
    Function: module.run
      Result: False
     Comment: Module function consul.agent_service_register threw an exception. Exception: Expecting value: line 1 column 1 (char 0)
     Started: 23:26:17.659090
    Duration: 5842.556 ms
     Changes:   

Steps to Reproduce the behavior See above, or vagrant up https://github.com/tomdoherty/salt-docker

Expected behavior Error should not be thrown

Versions Report

salt --versions-report (Provided by running salt --versions-report. Please also mention any differences in master/minion versions.) ``` [vagrant@master ~]$ salt --versions-report Salt Version: Salt: 3004 Dependency Versions: cffi: 1.11.5 cherrypy: unknown dateutil: 2.6.1 docker-py: Not Installed gitdb: Not Installed gitpython: Not Installed Jinja2: 2.10.1 libgit2: Not Installed M2Crypto: 0.35.2 Mako: Not Installed msgpack: 0.6.2 msgpack-pure: Not Installed mysql-python: Not Installed pycparser: 2.14 pycrypto: Not Installed pycryptodome: Not Installed pygit2: Not Installed Python: 3.6.8 (default, Mar 19 2021, 05:13:41) python-gnupg: Not Installed PyYAML: 3.12 PyZMQ: 19.0.0 smmap: Not Installed timelib: Not Installed Tornado: 4.5.3 ZMQ: 4.3.4 System Versions: dist: centos 8 locale: UTF-8 machine: x86_64 release: 5.10.47-linuxkit system: Linux version: CentOS Linux 8 ```

Additional context

below patch fixes this

diff --git a/salt/modules/consul.py b/salt/modules/consul.py
index 814bc47ff5..13c79715b1 100644
--- a/salt/modules/consul.py
+++ b/salt/modules/consul.py
@@ -84,7 +84,7 @@ def _query(
         method=method,
         params=query_params,
         data=data,
-        decode=True,
+        decode=False,
         status=True,
         header_dict=headers,
         opts=__opts__,
welcome[bot] commented 2 years ago

Hi there! Welcome to the Salt Community! Thank you for making your first contribution. We have a lengthy process for issues and PRs. Someone from the Core Team will follow up as soon as possible. In the meantime, here’s some information that may help as you continue your Salt journey. Please be sure to review our Code of Conduct. Also, check out some of our community resources including:

There are lots of ways to get involved in our community. Every month, there are around a dozen opportunities to meet with other contributors and the Salt Core team and collaborate in real time. The best way to keep track is by subscribing to the Salt Community Events Calendar. If you have additional questions, email us at saltproject@vmware.com. We’re glad you’ve joined our community and look forward to doing awesome things with you!