From 8d09d797e6c22be874df9fbe6465b48b50b7b1bc Mon Sep 17 00:00:00 2001 From: kevinvalleau <62773562+kevinvalleau@users.noreply.github.com> Date: Fri, 10 Mar 2023 07:47:19 -0500 Subject: [PATCH] Autoscaling: params not sorted properly when tags are present (#6038) --- moto/core/responses.py | 10 ++++++---- moto/core/utils.py | 14 +++++++++++++- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/moto/core/responses.py b/moto/core/responses.py index 04897ca85..8fc794261 100644 --- a/moto/core/responses.py +++ b/moto/core/responses.py @@ -12,7 +12,11 @@ from collections import defaultdict, OrderedDict from moto import settings from moto.core.common_types import TYPE_RESPONSE, TYPE_IF_NONE from moto.core.exceptions import DryRunClientError -from moto.core.utils import camelcase_to_underscores, method_names_from_class +from moto.core.utils import ( + camelcase_to_underscores, + method_names_from_class, + params_sort_function, +) from moto.utilities.utils import load_resource from jinja2 import Environment, DictLoader, Template from typing import ( @@ -122,7 +126,6 @@ class _TemplateEnvironmentMixin(object): class ActionAuthenticatorMixin(object): - request_count: ClassVar[int] = 0 def _authenticate_and_authorize_action(self, iam_request_cls: type) -> None: @@ -204,7 +207,6 @@ class ActionAuthenticatorMixin(object): class BaseResponse(_TemplateEnvironmentMixin, ActionAuthenticatorMixin): - default_region = "us-east-1" # to extract region, use [^.] # Note that the URL region can be anything, thanks to our MOTO_ALLOW_NONEXISTENT_REGION-config - so we can't have a very specific regex @@ -687,7 +689,7 @@ class BaseResponse(_TemplateEnvironmentMixin, ActionAuthenticatorMixin): } """ params: Dict[str, Any] = {} - for k, v in sorted(self.querystring.items()): + for k, v in sorted(self.querystring.items(), key=params_sort_function): self._parse_param(k, v[0], params) return params diff --git a/moto/core/utils.py b/moto/core/utils.py index 7d4cf2f99..833359fbd 100644 --- a/moto/core/utils.py +++ b/moto/core/utils.py @@ -2,7 +2,7 @@ import datetime import inspect import re from botocore.exceptions import ClientError -from typing import Any, Optional, List, Callable, Dict +from typing import Any, Optional, List, Callable, Dict, Tuple from urllib.parse import urlparse from .common_types import TYPE_RESPONSE @@ -307,3 +307,15 @@ def extract_region_from_aws_authorization(string: str) -> Optional[str]: if region == auth: return None return region + + +def params_sort_function(item: Tuple[str, Any]) -> Tuple[str, Any]: + """ + Comparison function used to sort params appropriately taking tags non + alphabetical order into consideration + """ + key, _ = item + if key.startswith("Tags.member"): + member_num = int(key.split(".")[2]) + return ("Tags.member", member_num) + return item