Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.
Python awsaws-emulatoraws-localaws-sdkdevtoolsdockerdynamodbec2emulatorlambdalocalstacklocalstack-alternativeministackmock-awsopen-sourcepythons3sqsterraform
feat(alb): add listener attributes management with describe and modify functionality (#286)
Johannes Grumböck238b5fce6b3b
diff --git a/ministack/services/alb.py b/ministack/services/alb.pyindex af42d8b5..da2e0528 100644--- a/ministack/services/alb.py+++ b/ministack/services/alb.py@@ -8,7 +8,8 @@ Supports:Target Groups: CreateTargetGroup, DescribeTargetGroups, ModifyTargetGroup,DeleteTargetGroup, DescribeTargetGroupAttributes,ModifyTargetGroupAttributes- Listeners: CreateListener, DescribeListeners, ModifyListener, DeleteListener+ Listeners: CreateListener, DescribeListeners, ModifyListener, DeleteListener,+ DescribeListenerAttributes, ModifyListenerAttributesRules: CreateRule, DescribeRules, ModifyRule, DeleteRule,SetRulePrioritiesTarget Registration: RegisterTargets, DeregisterTargets, DescribeTargetHealth@@ -45,6 +46,7 @@ _targets = AccountScopedDict() # tg_arn -> [target dict]_tags = AccountScopedDict() # res_arn -> [{Key, Value}]_lb_attrs = AccountScopedDict() # lb_arn -> [{Key, Value}]_tg_attrs = AccountScopedDict() # tg_arn -> [{Key, Value}]+_listener_attrs = AccountScopedDict() # l_arn -> [{Key, Value}]def get_state():@@ -57,6 +59,7 @@ def get_state():"_tags": _tags,"_lb_attrs": _lb_attrs,"_tg_attrs": _tg_attrs,+ "_listener_attrs": _listener_attrs,})@@ -69,6 +72,7 @@ def restore_state(data):_tags.update(data.get("_tags", {}))_lb_attrs.update(data.get("_lb_attrs", {}))_tg_attrs.update(data.get("_tg_attrs", {}))+ _listener_attrs.update(data.get("_listener_attrs", {}))_restored = load_state("alb")@@ -578,6 +582,9 @@ def _create_listener(params):"DefaultActions": actions,}_listeners[l_arn] = listener+ _listener_attrs[l_arn] = [+ {"Key": "routing.http.response.server.enabled", "Value": "true"},+ ]_tags[l_arn] = _parse_tags(params)# auto-create default rulerule_id = _short_id()@@ -625,12 +632,44 @@ def _delete_listener(params):if arn not in _listeners:return _error("ListenerNotFound", f"Listener '{arn}' not found", 400)_listeners.pop(arn, None)+ _listener_attrs.pop(arn, None)_tags.pop(arn, None)for rarn in [k for k, v in list(_rules.items()) if v.get("ListenerArn") == arn]:_rules.pop(rarn, None)return _empty("DeleteListener")+def _describe_listener_attrs(params):+ arn = _p(params, "ListenerArn")+ if arn not in _listeners:+ return _error("ListenerNotFound", f"Listener '{arn}' not found.")+ attrs = _listener_attrs.get(arn, [])+ return _xml(200, "DescribeListenerAttributes",+ f"<Attributes>{_attrs_xml(attrs)}</Attributes>")+++def _modify_listener_attrs(params):+ arn = _p(params, "ListenerArn")+ if arn not in _listeners:+ return _error("ListenerNotFound", f"Listener '{arn}' not found.")+ attrs = _listener_attrs.setdefault(arn, [])+ idx = {a["Key"]: i for i, a in enumerate(attrs)}+ i = 1+ while True:+ key = _p(params, f"Attributes.member.{i}.Key")+ if not key:+ break+ val = _p(params, f"Attributes.member.{i}.Value")+ if key in idx:+ attrs[idx[key]]["Value"] = val+ else:+ attrs.append({"Key": key, "Value": val})+ idx[key] = len(attrs) - 1+ i += 1+ return _xml(200, "ModifyListenerAttributes",+ f"<Attributes>{_attrs_xml(attrs)}</Attributes>")++# ---------------------------------------------------------------------------# Rule handlers# ---------------------------------------------------------------------------@@ -814,6 +853,8 @@ _ACTION_MAP = {"ModifyTargetGroupAttributes": _modify_tg_attrs,"CreateListener": _create_listener,"DescribeListeners": _describe_listeners,+ "DescribeListenerAttributes": _describe_listener_attrs,+ "ModifyListenerAttributes": _modify_listener_attrs,"ModifyListener": _modify_listener,"DeleteListener": _delete_listener,"CreateRule": _create_rule,@@ -1093,3 +1134,4 @@ def reset():_tags.clear()_lb_attrs.clear()_tg_attrs.clear()+ _listener_attrs.clear()diff --git a/tests/test_elbv2.py b/tests/test_elbv2.pyindex 59ed4721..b1d33982 100644--- a/tests/test_elbv2.py+++ b/tests/test_elbv2.py@@ -145,6 +145,81 @@ def test_elbv2_listener_crud(elbv2):elbv2.delete_target_group(TargetGroupArn=tg_arn)elbv2.delete_load_balancer(LoadBalancerArn=lb_arn)++def test_elbv2_describe_listener_attributes(elbv2):+ lb_arn = elbv2.create_load_balancer(Name="qa-alb-listener-attrs")["LoadBalancers"][0]["LoadBalancerArn"]+ tg_arn = elbv2.create_target_group(+ Name="qa-tg-la",+ Protocol="HTTP",+ Port=80,+ VpcId="vpc-00000001",+ )["TargetGroups"][0]["TargetGroupArn"]+ l_arn = elbv2.create_listener(+ LoadBalancerArn=lb_arn,+ Protocol="HTTP",+ Port=80,+ DefaultActions=[{"Type": "forward", "TargetGroupArn": tg_arn}],+ )["Listeners"][0]["ListenerArn"]++ resp = elbv2.describe_listener_attributes(ListenerArn=l_arn)+ attrs = {a["Key"]: a["Value"] for a in resp["Attributes"]}+ assert attrs.get("routing.http.response.server.enabled") == "true"++ elbv2.delete_listener(ListenerArn=l_arn)+ elbv2.delete_target_group(TargetGroupArn=tg_arn)+ elbv2.delete_load_balancer(LoadBalancerArn=lb_arn)+++def test_elbv2_describe_listener_attributes_not_found(elbv2):+ with pytest.raises(ClientError) as exc:+ elbv2.describe_listener_attributes(ListenerArn="arn:aws:elasticloadbalancing:us-east-1:000000000000:listener/app/missing/abc/def")+ assert exc.value.response["Error"]["Code"] == "ListenerNotFound"+++def test_elbv2_modify_listener_attributes(elbv2):+ lb_arn = elbv2.create_load_balancer(Name="qa-alb-mod-listener-attrs")["LoadBalancers"][0]["LoadBalancerArn"]+ tg_arn = elbv2.create_target_group(+ Name="qa-tg-mla",+ Protocol="HTTP",+ Port=80,+ VpcId="vpc-00000001",+ )["TargetGroups"][0]["TargetGroupArn"]+ l_arn = elbv2.create_listener(+ LoadBalancerArn=lb_arn,+ Protocol="HTTP",+ Port=80,+ DefaultActions=[{"Type": "forward", "TargetGroupArn": tg_arn}],+ )["Listeners"][0]["ListenerArn"]++ resp = elbv2.modify_listener_attributes(+ ListenerArn=l_arn,+ Attributes=[+ {"Key": "routing.http.response.server.enabled", "Value": "false"},+ {"Key": "routing.http.response.strict_transport_security.header_value", "Value": "max-age=31536000"},+ ],+ )+ attrs = {a["Key"]: a["Value"] for a in resp["Attributes"]}+ assert attrs["routing.http.response.server.enabled"] == "false"+ assert attrs["routing.http.response.strict_transport_security.header_value"] == "max-age=31536000"++ desc = elbv2.describe_listener_attributes(ListenerArn=l_arn)+ desc_attrs = {a["Key"]: a["Value"] for a in desc["Attributes"]}+ assert desc_attrs["routing.http.response.server.enabled"] == "false"+ assert desc_attrs["routing.http.response.strict_transport_security.header_value"] == "max-age=31536000"++ elbv2.delete_listener(ListenerArn=l_arn)+ elbv2.delete_target_group(TargetGroupArn=tg_arn)+ elbv2.delete_load_balancer(LoadBalancerArn=lb_arn)+++def test_elbv2_modify_listener_attributes_not_found(elbv2):+ with pytest.raises(ClientError) as exc:+ elbv2.modify_listener_attributes(+ ListenerArn="arn:aws:elasticloadbalancing:us-east-1:000000000000:listener/app/missing/abc/def",+ Attributes=[{"Key": "routing.http.response.server.enabled", "Value": "false"}],+ )+ assert exc.value.response["Error"]["Code"] == "ListenerNotFound"+def test_elbv2_rule_crud(elbv2):lb_arn = elbv2.create_load_balancer(Name="qa-alb-rules")["LoadBalancers"][0]["LoadBalancerArn"]tg_arn = elbv2.create_target_group(