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

can not stop mocking after moto==1.3.14 #2575

Closed
ynaka81 opened this issue Nov 17, 2019 · 9 comments · Fixed by #2578
Closed

can not stop mocking after moto==1.3.14 #2575

ynaka81 opened this issue Nov 17, 2019 · 9 comments · Fixed by #2578
Labels

Comments

@ynaka81
Copy link

ynaka81 commented Nov 17, 2019

versions

$ python --version
Python 3.6.8
$ pip freeze | grep boto
boto==2.49.0
boto3==1.10.19
botocore==1.13.19

Reporting Bugs

I mock only DynamoDB and run S3 client on real environment. But it has not worked since I update moto to 1.3.14.

Here is a minimum sample code.

import boto3
import moto


if __name__ == '__main__':
    print(moto.__version__)
    mock = moto.mock_dynamodb2()
    mock.start()
    boto3.client('dynamodb')
    mock.stop()
    bucket = boto3.resource('s3').Bucket('test-bucket')
    print(list(bucket.objects.filter(Prefix='dummy')))

Although this code works on moto==1.3.13,

$ AWS_ACCESS_KEY_ID=xxx AWS_SECRET_ACCESS_KEY=xxx AWS_DEFAULT_REGION=xxx python main.py 
1.3.13
[s3.ObjectSummary(bucket_name='test-bucket', key='dummy')]

does not work on moto==1.3.14.

$ AWS_ACCESS_KEY_ID=xxx AWS_SECRET_ACCESS_KEY=xxx AWS_DEFAULT_REGION=xxx python main.py 
1.3.14
Traceback (most recent call last):
  File "main.py", line 12, in <module>
    print(list(bucket.objects.filter(Prefix='dummy')))
  File "/home/nakagawa/.pyenv/versions/3.6.8/lib/python3.6/site-packages/boto3/resources/collection.py", line 83, in __iter__
    for page in self.pages():
  File "/home/nakagawa/.pyenv/versions/3.6.8/lib/python3.6/site-packages/boto3/resources/collection.py", line 166, in pages
    for page in pages:
  File "/home/nakagawa/.pyenv/versions/3.6.8/lib/python3.6/site-packages/botocore/paginate.py", line 255, in __iter__
    response = self._make_request(current_kwargs)
  File "/home/nakagawa/.pyenv/versions/3.6.8/lib/python3.6/site-packages/botocore/paginate.py", line 332, in _make_request
    return self._method(**current_kwargs)
  File "/home/nakagawa/.pyenv/versions/3.6.8/lib/python3.6/site-packages/botocore/client.py", line 357, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/home/nakagawa/.pyenv/versions/3.6.8/lib/python3.6/site-packages/botocore/client.py", line 661, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (InvalidAccessKeyId) when calling the ListObjects operation: The AWS Access Key Id you provided does not exist in our records.
@mikegrima
Copy link
Collaborator

What happens when you use the Context Manager: https://github.com/spulec/moto#context-manager ?

@ynaka81
Copy link
Author

ynaka81 commented Nov 18, 2019

You mean using Context Manager like following?

import boto3
import moto


if __name__ == '__main__':
    print(moto.__version__)
    with moto.mock_dynamodb2():
        boto3.client('dynamodb')
    bucket = boto3.resource('s3').Bucket('test-bucket')
    print(list(bucket.objects.filter(Prefix='dummy')))

It does not work as well.

$ AWS_ACCESS_KEY_ID=xxx AWS_SECRET_ACCESS_KEY=xxx AWS_DEFAULT_REGION=xxx python main.py 
1.3.14
Traceback (most recent call last):
  File "main.py", line 10, in <module>
    print(list(bucket.objects.filter(Prefix='dummy')))
  File "/home/nakagawa/.pyenv/versions/3.6.8/lib/python3.6/site-packages/boto3/resources/collection.py", line 83, in __iter__
    for page in self.pages():
  File "/home/nakagawa/.pyenv/versions/3.6.8/lib/python3.6/site-packages/boto3/resources/collection.py", line 166, in pages
    for page in pages:
  File "/home/nakagawa/.pyenv/versions/3.6.8/lib/python3.6/site-packages/botocore/paginate.py", line 255, in __iter__
    response = self._make_request(current_kwargs)
  File "/home/nakagawa/.pyenv/versions/3.6.8/lib/python3.6/site-packages/botocore/paginate.py", line 332, in _make_request
    return self._method(**current_kwargs)
  File "/home/nakagawa/.pyenv/versions/3.6.8/lib/python3.6/site-packages/botocore/client.py", line 357, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/home/nakagawa/.pyenv/versions/3.6.8/lib/python3.6/site-packages/botocore/client.py", line 661, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (InvalidAccessKeyId) when calling the ListObjects operation: The AWS Access Key Id you provided does not exist in our records.

@mikegrima
Copy link
Collaborator

Well that's no good :(

@mikegrima mikegrima added the bug label Nov 18, 2019
@mikegrima
Copy link
Collaborator

Hmm... while doing some debugging, I found that in botocore's credentials.py, the access_key and secret_key are set to foobar_key, and foobar_secret, respectively.

@mikegrima
Copy link
Collaborator

For some reason, the .stop() function is not unsetting the FAKE_KEYS:

    def start(self, reset=True):
        self.env_variables_mocks.start()

        self.__class__.nested_count += 1
        if reset:
            for backend in self.backends.values():
                backend.reset()

        self.enable_patching()

    def stop(self):
        self.env_variables_mocks.stop()
        self.__class__.nested_count -= 1

        if self.__class__.nested_count < 0:
            raise RuntimeError("Called stop() before start().")

        if self.__class__.nested_count == 0:
            self.disable_patching()

@mikegrima
Copy link
Collaborator

I have a hunch that what we need to do is mock the boto3 DEFAULT_SESSION so that it is None after the the mocks stop.

It appears that:

    def _register_credential_provider(self):
        self._components.lazy_register_component(
            'credential_provider',
            lambda:  botocore.credentials.create_credential_resolver(self))

... is being cached and used after the mock has ended.

@mikegrima
Copy link
Collaborator

mikegrima commented Nov 18, 2019

Preliminary PR for fixing this is in #2578.

Will need to do more testing. Feel free to pip install from my repo branch and test that it works for you.

@ynaka81
Copy link
Author

ynaka81 commented Nov 18, 2019

Thank you! It works fine after $ pip install git+https://github.com/mikegrima/moto.git@fixunmock.

@mikegrima
Copy link
Collaborator

Glad to hear that! I will merge it in once I get the tests to pass.

mikegrima pushed a commit to mikegrima/moto that referenced this issue Nov 18, 2019
- Fixes getmoto#2575
- Also upgraded Travis CI to make use of Bionic instead of Xenial
- This may also address concerns raised in getmoto#1793
gruebel pushed a commit to gruebel/moto that referenced this issue Dec 17, 2019
- Fixes getmoto#2575
- Also upgraded Travis CI to make use of Bionic instead of Xenial
- This may also address concerns raised in getmoto#1793
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants