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

Permission Denied Creating 'elasticsearch_proc' instance #257

Closed
dleister77 opened this issue Jul 31, 2020 · 6 comments · Fixed by #320
Closed

Permission Denied Creating 'elasticsearch_proc' instance #257

dleister77 opened this issue Jul 31, 2020 · 6 comments · Fixed by #320

Comments

@dleister77
Copy link

I am trying to set up an elasticsearch_proc instance to use for tests. I am receiving a subprocess.CalledProcessError when 'usr/share/elasticsearchbin/elasticsearch' is called in check_output. It looks like its due to a permission error. Do you have any configuration suggestions to make this work?

import pytest
from pytest_elasticsearch import factories

es = factories.elasticsearch_proc(port=9200)
es_client = factories.elasticsearch('es')

class TestEs(object):
    def test_es(self, es, es_client):
        assert es.running == True

Results

venv/lib/python3.8/site-packages/pytest_elasticsearch/factories.py:112: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
venv/lib/python3.8/site-packages/pytest_elasticsearch/executor.py:66: in __init__
    self._exec_command(),
venv/lib/python3.8/site-packages/pytest_elasticsearch/executor.py:113: in _exec_command
    if self.version < parse_version('5.0.0'):
venv/lib/python3.8/site-packages/pytest_elasticsearch/executor.py:84: in version
    output = check_output([self.executable, '-Vv']).decode('utf-8')
/usr/local/lib/python3.8/subprocess.py:411: in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

input = None, capture_output = False, timeout = None, check = True
popenargs = (['/usr/share/elasticsearch/bin/elasticsearch', '-Vv'],)
kwargs = {'stdout': -1}, process = <subprocess.Popen object at 0x7f7e7e96f700>
stdout = b'', stderr = None, retcode = 1

    def run(*popenargs,
            input=None, capture_output=False, timeout=None, check=False, **kwargs):
        """Run command with arguments and return a CompletedProcess instance.
    
        The returned instance will have attributes args, returncode, stdout and
        stderr. By default, stdout and stderr are not captured, and those attributes
        will be None. Pass stdout=PIPE and/or stderr=PIPE in order to capture them.
    
        If check is True and the exit code was non-zero, it raises a
        CalledProcessError. The CalledProcessError object will have the return code
        in the returncode attribute, and output & stderr attributes if those streams
        were captured.
    
        If timeout is given, and the process takes too long, a TimeoutExpired
        exception will be raised.
    
        There is an optional argument "input", allowing you to
        pass bytes or a string to the subprocess's stdin.  If you use this argument
        you may not also use the Popen constructor's "stdin" argument, as
        it will be used internally.
    
        By default, all communication is in bytes, and therefore any "input" should
        be bytes, and the stdout and stderr will be bytes. If in text mode, any
        "input" should be a string, and stdout and stderr will be strings decoded
        according to locale encoding, or by "encoding" if set. Text mode is
        triggered by setting any of text, encoding, errors or universal_newlines.
    
        The other arguments are the same as for the Popen constructor.
        """
        if input is not None:
            if kwargs.get('stdin') is not None:
                raise ValueError('stdin and input arguments may not both be used.')
            kwargs['stdin'] = PIPE
    
        if capture_output:
            if kwargs.get('stdout') is not None or kwargs.get('stderr') is not None:
                raise ValueError('stdout and stderr arguments may not be used '
                                 'with capture_output.')
            kwargs['stdout'] = PIPE
            kwargs['stderr'] = PIPE
    
        with Popen(*popenargs, **kwargs) as process:
            try:
                stdout, stderr = process.communicate(input, timeout=timeout)
            except TimeoutExpired as exc:
                process.kill()
                if _mswindows:
                    # Windows accumulates the output in a single blocking
                    # read() call run on child threads, with the timeout
                    # being done in a join() on those threads.  communicate()
                    # _after_ kill() is required to collect that and add it
                    # to the exception.
                    exc.stdout, exc.stderr = process.communicate()
                else:
                    # POSIX _communicate already populated the output so
                    # far into the TimeoutExpired exception.
                    process.wait()
                raise
            except:  # Including KeyboardInterrupt, communicate handled that.
                process.kill()
                # We don't call process.wait() as .__exit__ does that for us.
                raise
            retcode = process.poll()
            if check and retcode:
>               raise CalledProcessError(retcode, process.args,
                                         output=stdout, stderr=stderr)
E               subprocess.CalledProcessError: Command '['/usr/share/elasticsearch/bin/elasticsearch', '-Vv']' returned non-zero exit status 1.

/usr/local/lib/python3.8/subprocess.py:512: CalledProcessError
---------------------------- Captured stderr setup -----------------------------
/usr/share/elasticsearch/bin/elasticsearch-env: line 81: /etc/default/elasticsearch: Permission denied
------------- generated xml file: /tmp/tmp-102560jGYtkaf6T9VL.xml --------------
=========================== short test summary info ============================
ERROR tests/unit/test_elasticsearch.py::TestEs::test_es - subprocess.CalledPr...
=============================== 1 error in 0.28s ===============================

@fizyk
Copy link
Member

fizyk commented Aug 25, 2020

/etc/default/elasticsearch: Permission denied that's what interest us the most.

First run of the elasticsarch process while trying to detect a version... Do you recall which version you have installed?

@fizyk
Copy link
Member

fizyk commented Aug 25, 2020

@fizyk fizyk added the question label Aug 25, 2020
@dleister77
Copy link
Author

I'm using es 7.8. File ownership of /etc/default/elasticsearch is root elasticsearch.

@fizyk
Copy link
Member

fizyk commented Aug 27, 2020

Okay, got it replicated locally...

@fizyk
Copy link
Member

fizyk commented Aug 27, 2020

@dleister77 it's all permission related. Since travis runs all tests as root at the moment, I might have missed the time to change the plugin.

Any help will be welcomed

fizyk added a commit that referenced this issue Aug 27, 2020
Test newer 7.x elasticsearch versions - refs #257
@stanimirovv
Copy link
Contributor

@fizyk I have encountered this before - the solution was to fix the file permissions for the user you are running it.
I don't think conceptually there is anything to fix in the code if the user who has started the process has.

The simple (and insecure!) solution is to change the permissions of the files (excutable, config dir and log dir) to 777.

Alternatively play arround with the groups a bit and it will work.

@fizyk fizyk linked a pull request May 11, 2021 that will close this issue
fizyk added a commit that referenced this issue May 11, 2021
Add note about possible permission issues - refs #257
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants