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

SSM: Fix bad state caused by invalid PutParameter request #6484

Merged
merged 3 commits into from
Jul 7, 2023

Conversation

viren-nadkarni
Copy link
Contributor

@viren-nadkarni viren-nadkarni commented Jul 5, 2023

This PR fixes an issue where if PutParameter is called without parameter type, it creates an invalid state within SSM. Consequently, all calls to DescribeParameters break.

It is caused by peeking into a default dict which creates an empty list, which itself is the invalid state.

$ awslocal ssm put-parameter --name foo --value bar

An error occurred (ValidationException) when calling the PutParameter operation: A parameter type is required when you create a parameter.
$ awslocal ssm describe-parameters

An error occurred (InternalError) when calling the DescribeParameters operation (reached max retries: 4): exception while calling ssm.DescribeParameters: Traceback (most recent call last):
  File "/home/viren/repo/localstack/localstack/aws/chain.py", line 90, in handle
    handler(self, self.context, response)
  File "/home/viren/repo/localstack/localstack/aws/handlers/service.py", line 123, in __call__
    handler(chain, context, response)
  File "/home/viren/repo/localstack/localstack/aws/handlers/service.py", line 93, in __call__
    skeleton_response = self.skeleton.invoke(context)
  File "/home/viren/repo/localstack/localstack/aws/skeleton.py", line 154, in invoke
    return self.dispatch_request(context, instance)
  File "/home/viren/repo/localstack/localstack/aws/skeleton.py", line 166, in dispatch_request
    result = handler(context, instance) or {}
  File "/home/viren/repo/localstack/localstack/aws/forwarder.py", line 67, in _call
    return fallthrough_handler(context, req)
  File "/home/viren/repo/localstack/localstack/services/moto.py", line 83, in _proxy_moto
    return call_moto(context)
  File "/home/viren/repo/localstack/localstack/services/moto.py", line 46, in call_moto
    return dispatch_to_backend(context, dispatch_to_moto, include_response_metadata)
  File "/home/viren/repo/localstack/localstack/aws/forwarder.py", line 120, in dispatch_to_backend
    http_response = http_request_dispatcher(context)
  File "/home/viren/repo/localstack/localstack/services/moto.py", line 111, in dispatch_to_moto
    response = dispatch(request, request.url, request.headers)
  File "/home/viren/repo/moto-ext/moto/core/responses.py", line 231, in dispatch
    return cls()._dispatch(*args, **kwargs)
  File "/home/viren/repo/moto-ext/moto/core/responses.py", line 375, in _dispatch
    return self.call_action()
  File "/home/viren/repo/moto-ext/moto/core/responses.py", line 464, in call_action
    response = method()
  File "/home/viren/repo/moto-ext/moto/ssm/responses.py", line 250, in describe_parameters
    result = self.ssm_backend.describe_parameters(filters, parameter_filters)
  File "/home/viren/repo/moto-ext/moto/ssm/models.py", line 1565, in describe_parameters
    ssm_parameter: Parameter = self.get_parameter(param_name)  # type: ignore[assignment]
  File "/home/viren/repo/moto-ext/moto/ssm/models.py", line 1927, in get_parameter
    return self._parameters[name][-1]
IndexError: list index out of range

Related: #6436

@codecov
Copy link

codecov bot commented Jul 6, 2023

Codecov Report

Merging #6484 (7404fd7) into master (d98d8cf) will increase coverage by 0.00%.
The diff coverage is 100.00%.

❗ Current head 7404fd7 differs from pull request most recent head 73bf110. Consider uploading reports for the commit 73bf110 to get more accurate results

@@           Coverage Diff           @@
##           master    #6484   +/-   ##
=======================================
  Coverage   96.18%   96.18%           
=======================================
  Files         800      800           
  Lines       78546    78549    +3     
=======================================
+ Hits        75546    75551    +5     
+ Misses       3000     2998    -2     
Flag Coverage Δ
servertests 36.87% <0.00%> (-0.01%) ⬇️
unittests 96.12% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
moto/ssm/models.py 95.19% <100.00%> (ø)

... and 3 files with indirect coverage changes

Copy link
Collaborator

@bblommers bblommers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this was caused by #6436, but I don't quite understand why/how we could get in this error scenario.

Would you mind adding a test case for this? Or even just extend the test introduced in #6436, if that's easier.

@viren-nadkarni
Copy link
Contributor Author

@bblommers: _parameters is a subclass of DefaultDict. Earlier self._parameters[name] was called which created a value of an empty list for name. This itself is an invalid state. The DescribeParameters operation expects this list to always have atleast one item:

return self._parameters[name][-1]

The fix was to check whether name is in self._parameters before self._parameters[name].

I've added an assertion with 73bf110 which should catch this in the future

Copy link
Collaborator

@bblommers bblommers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @viren-nadkarni!

@bblommers bblommers added this to the 4.1.13 milestone Jul 7, 2023
@bblommers bblommers merged commit 89f2a3c into getmoto:master Jul 7, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Jul 7, 2023

This is now part of moto >= 4.1.13.dev39

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants