Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error handling when Agent has no sudo access #8470

Merged
merged 1 commit into from
Jan 27, 2021

Conversation

florimondmanca
Copy link
Contributor

What does this PR do?

Properly handle sudo access error.

Motivation

Found while testing #8159 during QA

To test for sudo access we run:

$ sudo -ln <cmd>

This should output the command path on stdout if sudo access is available, eg:

$ sudo -s
# Enter password...
(root) $ sudo -ln test
/usr/local/opt/coreutils/libexec/gnubin/test

If sudo access is not available, we either get a non-0 error code and/or empty output. Eg on macOS we get both:

$ sudo -ln test
sudo: a password is required

So right now we get an unhelpful "output empty" error:

      Error: get_subprocess_output expected output but had none.
      Traceback (most recent call last):
        File "/opt/datadog-agent/embedded/lib/python3.8/site-packages/datadog_checks/base/checks/base.py", line 901, in run
          self.check(instance)
        File "/home/glusterfs/datadog_checks/glusterfs/check.py", line 56, in check
          test_sudo, err, rcode = get_subprocess_output(['sudo', '-ln', self.gstatus_cmd], self.log)
        File "/opt/datadog-agent/embedded/lib/python3.8/site-packages/datadog_checks/base/utils/subprocess_output.py", line 56, in get_subprocess_output
          out, err, returncode = subprocess_output(cmd_args, raise_on_empty_output, env=env)
      _util.SubprocessOutputEmptyError: get_subprocess_output expected output but had none.

This PR moves things around so we check for either status code or empty output, and raise the proper error message we meant to raise in the first place:

      Error: The dd-agent user does not have sudo access: ''
      Traceback (most recent call last):
        File "/opt/datadog-agent/embedded/lib/python3.8/site-packages/datadog_checks/base/checks/base.py", line 901, in run
          self.check(instance)
        File "/home/glusterfs/datadog_checks/glusterfs/check.py", line 60, in check
          raise Exception('The dd-agent user does not have sudo access: {!r}'.format(err or test_sudo))
      Exception: The dd-agent user does not have sudo access: ''

Additional Notes

Review checklist (to be filled by reviewers)

  • Feature or bugfix MUST have appropriate tests (unit, integration, e2e)
  • PR title must be written as a CHANGELOG entry (see why)
  • Files changes must correspond to the primary purpose of the PR as described in the title (small unrelated changes should have their own PR)
  • PR must have changelog/ and integration/ labels attached

@florimondmanca florimondmanca merged commit effeb0f into master Jan 27, 2021
@florimondmanca florimondmanca deleted the fm/fix-gluster-sudo branch January 27, 2021 16:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants