Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

fix TF Ec2, add + origin_jti in cognito (#332)

Nahuel Nucera8892d7900482
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e21d4986..54a75ec8 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,10 +7,14 @@ Versioning follows [Semantic Versioning](https://semver.org/).
---
-## [Unreleased]
+## [1.2.14] — 2026-04-15
### Added
-- **Cognito federated SAML/OIDC auth flow** — `GET /oauth2/authorize` (redirects to external SAML/OIDC IdP), `POST /saml2/idpresponse` (parses SAML assertion, creates federated user, issues authorization code), and `POST /oauth2/token` now supports `grant_type=authorization_code` for full SSO flow. Also adds `GetIdentityProviderByIdentifier`. Closes #325.
+- **Cognito federated SAML/OIDC auth flow** — `GET /oauth2/authorize` (redirects to external SAML/OIDC IdP), `POST /saml2/idpresponse` (parses SAML assertion, creates federated user, issues authorization code), and `POST /oauth2/token` now supports `grant_type=authorization_code` for full SSO flow. Also adds `GetIdentityProviderByIdentifier`. Contributed by @prandogabriel (#329)
+- **EC2 AuthorizeSecurityGroup returns rules** — `AuthorizeSecurityGroupIngress` and `AuthorizeSecurityGroupEgress` now return `SecurityGroupRules` in the response with rule IDs, group ownership, protocol, port range, and CIDR details. Required by Terraform AWS provider v6. Reported by @mspiller (#325)
+
+### Fixed
+- **Cognito token claims correctness** — `origin_jti` and `auth_time` claims are now only included in `IdToken` and `AccessToken` (not `RefreshToken`), matching real AWS Cognito behavior. Refresh tokens use minimal claims with only `client_id`.
---
diff --git a/ministack/services/cognito.py b/ministack/services/cognito.py
index 100e5110..d0752709 100644
--- a/ministack/services/cognito.py
+++ b/ministack/services/cognito.py
@@ -253,6 +253,7 @@ def _fake_token(sub: str, pool_id: str, client_id: str, token_type: str = "acces
json.dumps({"alg": "RS256", "kid": "ministack-key-1"}).encode()
).rstrip(b"=").decode()
now = int(time.time())
+ origin_jti = new_uuid()
claims = {
"sub": sub,
"iss": f"https://cognito-idp.{REGION}.amazonaws.com/{pool_id}",
@@ -260,12 +261,12 @@ def _fake_token(sub: str, pool_id: str, client_id: str, token_type: str = "acces
"iat": now,
"exp": now + 3600,
"jti": new_uuid(),
- "auth_time": now,
- "origin_jti": new_uuid(),
}
if token_type == "id":
# IdToken uses 'aud' (not 'client_id') per OIDC spec
claims["aud"] = client_id
+ claims["auth_time"] = now
+ claims["origin_jti"] = origin_jti
if username:
claims["cognito:username"] = username
# Include user attributes in IdToken
@@ -276,11 +277,15 @@ def _fake_token(sub: str, pool_id: str, client_id: str, token_type: str = "acces
claims[k] = v
if "email" in user_attrs:
claims.setdefault("email_verified", True)
- else:
- # AccessToken uses 'client_id'
+ elif token_type == "access":
claims["client_id"] = client_id
+ claims["auth_time"] = now
+ claims["origin_jti"] = origin_jti
if username:
claims["username"] = username
+ else:
+ # RefreshToken — opaque in real AWS, but we use a JWT stub for simplicity
+ claims["client_id"] = client_id
payload = base64.urlsafe_b64encode(
json.dumps(claims).encode()
).rstrip(b"=").decode()
diff --git a/ministack/services/ec2.py b/ministack/services/ec2.py
index 94cf3676..3b76ba73 100644
--- a/ministack/services/ec2.py
+++ b/ministack/services/ec2.py
@@ -586,17 +586,62 @@ def _describe_security_groups(p):
f"<securityGroupInfo>{items}</securityGroupInfo>")
+def _sg_rule_xml(sg_id, rule, idx, is_egress=False):
+ """Build <securityGroupRuleSet> items for Authorize responses (provider v6)."""
+ direction = "egress" if is_egress else "ingress"
+ rule_id = f"sgr-{sg_id[3:]}-{direction}-{idx}"
+ items = ""
+ for cidr in rule.get("IpRanges", []):
+ items += (f"<item>"
+ f"<securityGroupRuleId>{rule_id}</securityGroupRuleId>"
+ f"<groupId>{sg_id}</groupId>"
+ f"<groupOwnerId>{get_account_id()}</groupOwnerId>"
+ f"<isEgress>{'true' if is_egress else 'false'}</isEgress>"
+ f"<ipProtocol>{rule.get('IpProtocol', '-1')}</ipProtocol>"
+ f"<fromPort>{rule.get('FromPort', -1)}</fromPort>"
+ f"<toPort>{rule.get('ToPort', -1)}</toPort>"
+ f"<cidrIpv4>{cidr.get('CidrIp', '')}</cidrIpv4>"
+ f"</item>")
+ for cidr6 in rule.get("Ipv6Ranges", []):
+ items += (f"<item>"
+ f"<securityGroupRuleId>{rule_id}</securityGroupRuleId>"
+ f"<groupId>{sg_id}</groupId>"
+ f"<groupOwnerId>{get_account_id()}</groupOwnerId>"
+ f"<isEgress>{'true' if is_egress else 'false'}</isEgress>"
+ f"<ipProtocol>{rule.get('IpProtocol', '-1')}</ipProtocol>"
+ f"<fromPort>{rule.get('FromPort', -1)}</fromPort>"
+ f"<toPort>{rule.get('ToPort', -1)}</toPort>"
+ f"<cidrIpv6>{cidr6.get('CidrIpv6', '')}</cidrIpv6>"
+ f"</item>")
+ if not items:
+ # No CIDR ranges — still return the rule (e.g. referenced group)
+ items = (f"<item>"
+ f"<securityGroupRuleId>{rule_id}</securityGroupRuleId>"
+ f"<groupId>{sg_id}</groupId>"
+ f"<groupOwnerId>{get_account_id()}</groupOwnerId>"
+ f"<isEgress>{'true' if is_egress else 'false'}</isEgress>"
+ f"<ipProtocol>{rule.get('IpProtocol', '-1')}</ipProtocol>"
+ f"<fromPort>{rule.get('FromPort', -1)}</fromPort>"
+ f"<toPort>{rule.get('ToPort', -1)}</toPort>"
+ f"</item>")
+ return items
+
+
def _authorize_sg_ingress(p):
sg_id = _p(p, "GroupId")
sg = _security_groups.get(sg_id)
if not sg:
return _error("InvalidGroup.NotFound", f"Security group {sg_id} not found", 400)
rules = _parse_ip_permissions(p, "IpPermissions")
+ rule_items = ""
for r in rules:
if r in sg["IpPermissions"]:
return _error("InvalidPermission.Duplicate", "The specified rule already exists", 400)
sg["IpPermissions"].append(r)
- return _xml(200, "AuthorizeSecurityGroupIngressResponse", "<return>true</return>")
+ idx = len(sg["IpPermissions"]) - 1
+ rule_items += _sg_rule_xml(sg_id, r, idx, is_egress=False)
+ return _xml(200, "AuthorizeSecurityGroupIngressResponse",
+ f"<return>true</return><securityGroupRuleSet>{rule_items}</securityGroupRuleSet>")
def _revoke_sg_ingress(p):
@@ -619,10 +664,14 @@ def _authorize_sg_egress(p):
if not sg:
return _error("InvalidGroup.NotFound", f"Security group {sg_id} not found", 400)
rules = _parse_ip_permissions(p, "IpPermissions")
+ rule_items = ""
for r in rules:
if r not in sg["IpPermissionsEgress"]:
sg["IpPermissionsEgress"].append(r)
- return _xml(200, "AuthorizeSecurityGroupEgressResponse", "<return>true</return>")
+ idx = len(sg["IpPermissionsEgress"]) - 1
+ rule_items += _sg_rule_xml(sg_id, r, idx, is_egress=True)
+ return _xml(200, "AuthorizeSecurityGroupEgressResponse",
+ f"<return>true</return><securityGroupRuleSet>{rule_items}</securityGroupRuleSet>")
def _revoke_sg_egress(p):
diff --git a/tests/test_ec2.py b/tests/test_ec2.py
index fa9c6ef7..5e0f6269 100644
--- a/tests/test_ec2.py
+++ b/tests/test_ec2.py
@@ -1296,3 +1296,47 @@ def test_ec2_create_default_vpc(ec2):
with pytest.raises(ClientError) as exc:
ec2.create_default_vpc()
assert exc.value.response["Error"]["Code"] == "DefaultVpcAlreadyExists"
+
+
+def test_ec2_authorize_sg_ingress_returns_rules(ec2):
+ """AuthorizeSecurityGroupIngress returns SecurityGroupRules in response (provider v6)."""
+ vpc = ec2.create_vpc(CidrBlock="10.99.0.0/16")["Vpc"]
+ sg = ec2.create_security_group(
+ GroupName="sgr-test", Description="test", VpcId=vpc["VpcId"])
+ resp = ec2.authorize_security_group_ingress(
+ GroupId=sg["GroupId"],
+ IpPermissions=[{
+ "IpProtocol": "tcp", "FromPort": 443, "ToPort": 443,
+ "IpRanges": [{"CidrIp": "10.0.0.0/16"}],
+ }],
+ )
+ assert resp.get("Return") is True
+ rules = resp.get("SecurityGroupRules", [])
+ assert len(rules) >= 1
+ rule = rules[0]
+ assert rule["SecurityGroupRuleId"].startswith("sgr-")
+ assert rule["GroupId"] == sg["GroupId"]
+ assert rule["IsEgress"] is False
+ assert rule["IpProtocol"] == "tcp"
+ assert rule["FromPort"] == 443
+ assert rule["ToPort"] == 443
+ assert rule["CidrIpv4"] == "10.0.0.0/16"
+
+
+def test_ec2_authorize_sg_egress_returns_rules(ec2):
+ """AuthorizeSecurityGroupEgress returns SecurityGroupRules in response (provider v6)."""
+ vpc = ec2.create_vpc(CidrBlock="10.98.0.0/16")["Vpc"]
+ sg = ec2.create_security_group(
+ GroupName="sgr-egress-test", Description="test", VpcId=vpc["VpcId"])
+ resp = ec2.authorize_security_group_egress(
+ GroupId=sg["GroupId"],
+ IpPermissions=[{
+ "IpProtocol": "tcp", "FromPort": 80, "ToPort": 80,
+ "IpRanges": [{"CidrIp": "0.0.0.0/0"}],
+ }],
+ )
+ assert resp.get("Return") is True
+ rules = resp.get("SecurityGroupRules", [])
+ assert len(rules) >= 1
+ assert rules[0]["IsEgress"] is True
+ assert rules[0]["CidrIpv4"] == "0.0.0.0/0"