From 8027e400a444de67a96a14e53723d6c40e499dd4 Mon Sep 17 00:00:00 2001 From: Gajendra Malviya Date: Fri, 16 Oct 2026 17:16:43 +0530 Subject: [PATCH 01/31] fix(aws-networkfirewall): close real-cloud parity gaps in #1455 --- .../enforce_authz_networkfirewall_test.go | 78 ++ docs/coverage/aws/networkfirewall.md | 100 ++- docs/coverage/coverage.json | 142 ++++ docs/coverage/nongoals/networkfirewall.md | 9 + docs/services.md | 4 +- internal/settle/settle.go | 2 + .../aws/networkfirewall/analysis_report.go | 114 +++ providers/aws/networkfirewall/capacity.go | 77 ++ .../aws/networkfirewall/concurrency_test.go | 248 ++++++ .../aws/networkfirewall/families_test.go | 528 +++++++++++++ providers/aws/networkfirewall/firewall.go | 557 ++++++++++++++ .../aws/networkfirewall/flow_operation.go | 222 ++++++ providers/aws/networkfirewall/logging.go | 221 ++++++ providers/aws/networkfirewall/metadata.go | 123 +++ .../aws/networkfirewall/networkfirewall.go | 713 ++---------------- .../networkfirewall/networkfirewall_test.go | 303 ++++---- providers/aws/networkfirewall/pagination.go | 34 + providers/aws/networkfirewall/policy.go | 278 +++++++ .../aws/networkfirewall/policy_actions.go | 246 ++++++ .../aws/networkfirewall/reference_test.go | 395 ++++++++++ .../aws/networkfirewall/resource_policy.go | 194 +++++ providers/aws/networkfirewall/rule_group.go | 318 ++++++++ providers/aws/networkfirewall/snapshot.go | 149 +++- .../aws/networkfirewall/snapshot_test.go | 135 ++-- providers/aws/networkfirewall/tags.go | 131 ++++ .../aws/networkfirewall/tls_inspection.go | 226 ++++++ providers/aws/networkfirewall/token.go | 25 + providers/aws/networkfirewall/token_test.go | 324 ++++++++ providers/aws/networkfirewall/validation.go | 115 +++ .../aws/networkfirewall/validation_test.go | 324 ++++++++ .../vpc_endpoint_association.go | 157 ++++ .../aws/networkfirewall_discovery_test.go | 67 ++ providers/aws/rgt_discovery.go | 91 ++- server/aws/networkfirewall/analysis_report.go | 96 +++ .../aws/networkfirewall/availability_zones.go | 48 ++ .../aws/networkfirewall/change_protection.go | 121 +++ server/aws/networkfirewall/depth.go | 258 +++---- server/aws/networkfirewall/dispatch_test.go | 210 ++++++ server/aws/networkfirewall/errors.go | 45 ++ server/aws/networkfirewall/flow_operation.go | 194 +++++ server/aws/networkfirewall/handler.go | 137 ++-- server/aws/networkfirewall/logging.go | 120 +++ server/aws/networkfirewall/metadata.go | 111 +++ server/aws/networkfirewall/operations.go | 449 +++-------- server/aws/networkfirewall/policy.go | 258 +++++++ server/aws/networkfirewall/resource_policy.go | 75 ++ server/aws/networkfirewall/rule_group.go | 219 ++++++ server/aws/networkfirewall/sdk_parity_test.go | 708 +++++++++++++++++ .../aws/networkfirewall/sdk_roundtrip_test.go | 28 +- server/aws/networkfirewall/tls_inspection.go | 181 +++++ .../vpc_endpoint_association.go | 156 ++++ .../driver/availability_zones.go | 43 ++ .../networkfirewall/driver/capabilities.go | 195 +++++ services/networkfirewall/driver/driver.go | 214 ++++-- services/networkfirewall/driver/errors.go | 59 ++ 55 files changed, 9083 insertions(+), 1492 deletions(-) create mode 100644 contrib/server/enforce_authz_networkfirewall_test.go create mode 100644 docs/coverage/nongoals/networkfirewall.md create mode 100644 providers/aws/networkfirewall/analysis_report.go create mode 100644 providers/aws/networkfirewall/capacity.go create mode 100644 providers/aws/networkfirewall/concurrency_test.go create mode 100644 providers/aws/networkfirewall/families_test.go create mode 100644 providers/aws/networkfirewall/firewall.go create mode 100644 providers/aws/networkfirewall/flow_operation.go create mode 100644 providers/aws/networkfirewall/logging.go create mode 100644 providers/aws/networkfirewall/metadata.go create mode 100644 providers/aws/networkfirewall/pagination.go create mode 100644 providers/aws/networkfirewall/policy.go create mode 100644 providers/aws/networkfirewall/policy_actions.go create mode 100644 providers/aws/networkfirewall/reference_test.go create mode 100644 providers/aws/networkfirewall/resource_policy.go create mode 100644 providers/aws/networkfirewall/rule_group.go create mode 100644 providers/aws/networkfirewall/tags.go create mode 100644 providers/aws/networkfirewall/tls_inspection.go create mode 100644 providers/aws/networkfirewall/token.go create mode 100644 providers/aws/networkfirewall/token_test.go create mode 100644 providers/aws/networkfirewall/validation.go create mode 100644 providers/aws/networkfirewall/validation_test.go create mode 100644 providers/aws/networkfirewall/vpc_endpoint_association.go create mode 100644 providers/aws/networkfirewall_discovery_test.go create mode 100644 server/aws/networkfirewall/analysis_report.go create mode 100644 server/aws/networkfirewall/availability_zones.go create mode 100644 server/aws/networkfirewall/change_protection.go create mode 100644 server/aws/networkfirewall/dispatch_test.go create mode 100644 server/aws/networkfirewall/errors.go create mode 100644 server/aws/networkfirewall/flow_operation.go create mode 100644 server/aws/networkfirewall/logging.go create mode 100644 server/aws/networkfirewall/metadata.go create mode 100644 server/aws/networkfirewall/policy.go create mode 100644 server/aws/networkfirewall/resource_policy.go create mode 100644 server/aws/networkfirewall/rule_group.go create mode 100644 server/aws/networkfirewall/sdk_parity_test.go create mode 100644 server/aws/networkfirewall/tls_inspection.go create mode 100644 server/aws/networkfirewall/vpc_endpoint_association.go create mode 100644 services/networkfirewall/driver/availability_zones.go create mode 100644 services/networkfirewall/driver/capabilities.go create mode 100644 services/networkfirewall/driver/errors.go diff --git a/contrib/server/enforce_authz_networkfirewall_test.go b/contrib/server/enforce_authz_networkfirewall_test.go new file mode 100644 index 000000000..7c14a8865 --- /dev/null +++ b/contrib/server/enforce_authz_networkfirewall_test.go @@ -0,0 +1,78 @@ +package main + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "io" + "net/http" + "strings" + "testing" + "time" + + "github.com/aws/aws-sdk-go-v2/aws" + v4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4" +) + +// signedJSONTarget sends a SigV4-signed AWS JSON 1.0 request for the given +// X-Amz-Target and returns the status and body. +func signedJSONTarget(t *testing.T, endpoint string, c aws.Credentials, service, target, body string) (int, string) { + t.Helper() + + req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, endpoint+"/", strings.NewReader(body)) + if err != nil { + t.Fatalf("new request: %v", err) + } + + req.Header.Set("Content-Type", "application/x-amz-json-1.0") + req.Header.Set("X-Amz-Target", target) + + sum := sha256.Sum256([]byte(body)) + if err := v4.NewSigner().SignHTTP(context.Background(), c, req, hex.EncodeToString(sum[:]), service, "us-east-1", time.Now()); err != nil { + t.Fatalf("sign: %v", err) + } + + resp, err := http.DefaultClient.Do(req) + if err != nil { + t.Fatalf("do: %v", err) + } + defer resp.Body.Close() + + raw, _ := io.ReadAll(resp.Body) + + return resp.StatusCode, string(raw) +} + +// TestEnforceAuthNetworkFirewall proves the operations added for the Network +// Firewall parity work are authorised per IAM action under --enforce-auth: a +// user allowed only network-firewall:DescribeFirewall passes the gate on that +// operation (the handler then answers its own 400 for the unknown firewall) and +// is refused on the new ones, while an administrator is allowed everywhere. +func TestEnforceAuthNetworkFirewall(t *testing.T) { + endpoint, stop := enforceAuthServer(t) + defer stop() + + boot := clientsFor(t, endpoint, seedBootUser(t, endpoint)) + reader := boot.newUser(t, "fw-reader", allowDoc("network-firewall:DescribeFirewall")) + const prefix = "NetworkFirewall_20201112." + + for _, op := range []string{ + "UpdateFirewallDescription", "UpdateSubnetChangeProtection", "CreateTLSInspectionConfiguration", + "CreateVpcEndpointAssociation", "PutResourcePolicy", "StartFlowCapture", "StartAnalysisReport", + } { + status, body := signedJSONTarget(t, endpoint, reader, "network-firewall", prefix+op, `{}`) + if status != http.StatusForbidden || !strings.Contains(body, "AccessDenied") { + t.Fatalf("%s as a describe-only user: %d %s, want 403 AccessDenied", op, status, body) + } + + status, body = signedJSONTarget(t, endpoint, boot.cred, "network-firewall", prefix+op, `{}`) + if status == http.StatusForbidden { + t.Fatalf("%s as the administrator: %d %s, want the handler's own answer", op, status, body) + } + } + + status, body := signedJSONTarget(t, endpoint, reader, "network-firewall", prefix+"DescribeFirewall", `{"FirewallName":"ghost"}`) + if status == http.StatusForbidden || !strings.Contains(body, "ResourceNotFoundException") { + t.Fatalf("DescribeFirewall as the allowed user: %d %s, want the handler's ResourceNotFoundException", status, body) + } +} diff --git a/docs/coverage/aws/networkfirewall.md b/docs/coverage/aws/networkfirewall.md index 6afd375a9..5a1d27706 100644 --- a/docs/coverage/aws/networkfirewall.md +++ b/docs/coverage/aws/networkfirewall.md @@ -31,6 +31,104 @@ AWS's `networkfirewall` service ยท portable interface `driver.NetworkFirewall` | `UpdateLoggingConfiguration` | | | `UpdateRuleGroup` | | +## Optional capabilities + +Discovered by type assertion; only some providers implement these. + +### AnalysisReports + +AnalysisReports covers the analysis report family. + +| Operation | Description | +| --- | --- | +| `GetAnalysisReport` | GetAnalysisReport returns one report (its results are always empty). | +| `ListAnalysisReports` | | +| `StartAnalysisReport` | | + +### AvailabilityZones + +AvailabilityZones covers the Availability-Zone associations of a transit + +| Operation | Description | +| --- | --- | +| `AssociateAvailabilityZones` | | +| `DisassociateAvailabilityZones` | | + +### FirewallSettings + +FirewallSettings covers the firewall-level setters. + +| Operation | Description | +| --- | --- | +| `UpdateAvailabilityZoneChangeProtection` | | +| `UpdateFirewallAnalysisSettings` | | +| `UpdateFirewallDescription` | | +| `UpdateFirewallPolicyChangeProtection` | | +| `UpdateSubnetChangeProtection` | | + +### FlowOperations + +FlowOperations covers the flow capture / flush family. + +| Operation | Description | +| --- | --- | +| `DescribeFlowOperation` | | +| `ListFlowOperationResults` | ListFlowOperationResults returns the operation and its (always empty, there is no | +| `ListFlowOperations` | | +| `StartFlowCapture` | | +| `StartFlowFlush` | | + +### Metadata + +Metadata covers the read-only metadata/summary operations. + +| Operation | Description | +| --- | --- | +| `DescribeFirewallMetadata` | | +| `DescribeRuleGroupMetadata` | | +| `DescribeRuleGroupSummary` | | + +### ResourcePolicies + +ResourcePolicies covers the resource-sharing IAM policies attached to a rule + +| Operation | Description | +| --- | --- | +| `DeleteResourcePolicy` | | +| `DescribeResourcePolicy` | | +| `PutResourcePolicy` | | + +### TLSInspection + +TLSInspection covers the TLS inspection configuration family. + +| Operation | Description | +| --- | --- | +| `CreateTLSInspectionConfiguration` | | +| `DeleteTLSInspectionConfiguration` | | +| `DescribeTLSInspectionConfiguration` | | +| `ListTLSInspectionConfigurations` | | +| `UpdateTLSInspectionConfiguration` | | + +### VPCEndpointAssociations + +VPCEndpointAssociations covers the VPC endpoint association family. + +| Operation | Description | +| --- | --- | +| `CreateVpcEndpointAssociation` | | +| `DeleteVpcEndpointAssociation` | | +| `DescribeVpcEndpointAssociation` | | +| `ListVpcEndpointAssociations` | | + ## Not in scope -_Not documented yet. See the [emulator boundary](../../../README.md) for cloudemu-wide non-goals._ +- Not implemented (the operations answer `InvalidRequestException: unknown operation`): the proxy family (`CreateProxy`, `CreateProxyConfiguration`, `CreateProxyRuleGroup`, `CreateProxyRules`, the matching describe, update, delete and list operations, `AttachRuleGroupsToProxyConfiguration`, `DetachRuleGroupsFromProxyConfiguration`, `UpdateProxyRulePriorities`, `UpdateProxyRuleGroupPriorities`), the container association family (`CreateContainerAssociation` and its describe, update, delete and list operations), the transit gateway attachment operations (`AcceptNetworkFirewallTransitGatewayAttachment`, `RejectNetworkFirewallTransitGatewayAttachment`, `DeleteNetworkFirewallTransitGatewayAttachment`) and `UpdateFirewallEncryptionConfiguration`. `EncryptionConfiguration` on create and update requests is accepted and ignored. +- There is no packet data plane. Flow capture and flush operations are accepted and complete (immediately, or after the settle window under `--async-settle`) but their results list no flows. Analysis reports complete with no results. Nothing is published to the `AWS/NetworkFirewall` CloudWatch namespace, and the logging configuration is validated and stored but no alert, flow or TLS log is delivered to S3, CloudWatch Logs or Kinesis Data Firehose. +- Checks that need another service are not made: the VPC, subnet and Availability Zone ids a firewall or VPC endpoint association names are syntax-checked only, and the certificates, certificate authorities, KMS keys, log destinations and IAM principals named in TLS inspection configurations, logging configurations and resource policies are not looked up. Resource policies are validated (JSON, allowed actions per resource kind) and stored but never evaluated; sharing through Resource Access Manager is not modeled. +- Availability Zones are synthesized. A VPC firewall gets one zone per subnet in the order the subnets were added (`a`, `b`, ...) and a VPC endpoint association gets a zone derived from its subnet id; the real zone of a subnet is not known. +- A transit gateway-attached firewall can be created, described and have Availability Zones associated and disassociated, but no transit gateway attachment is created, so its status never reports a transit gateway attachment sync state. +- Firewalls, policies, rule groups and TLS inspection configurations are `READY` or `ACTIVE` as soon as they are created; there is no `PROVISIONING` period. `DryRun` and `AnalyzeRuleGroup` on create and update requests are accepted and ignored, so a dry run really makes the change and no rule group analysis results are produced. +- Rule content is stored and returned byte for byte. Only the parts needed for capacity and summaries are read: stateful rules (one unit per rule in `RulesString`, `StatefulRules` and `RulesSourceList` targets) and stateless rules (the product of the element counts of the match settings, as the CreateRuleGroup reference describes). Suricata syntax, rule variables and reference sets are not validated, and a rule group whose rules need more than its `Capacity` is rejected with `InvalidRequestException` (the exception real AWS uses for this is unconfirmed). +- Behaviours the AWS reference does not state were chosen conservatively and are unconfirmed against the live service: a duplicate name answers `InvalidRequestException` (the one exception every Create operation models); deleting a firewall that still has VPC endpoint associations is refused with `InvalidOperationException`; describing or deleting a resource policy that does not exist answers `ResourceNotFoundException`; `DescribeRuleGroupSummary` on a stateless rule group answers `InvalidRequestException` and reports only the options named in the rule group's `SummaryConfiguration`; tagging a resource does not change its update token; a request that gives both a name and an ARN uses the name. +- List operations take `MaxResults` (1 to 100, default 100) and `NextToken`, an opaque offset over results sorted by name, so it is only stable while the list does not change between pages. `ListRuleGroups` filters on `Type`, `Scope` and `ManagedType`; the emulator holds no AWS-managed rule groups, so `Scope=MANAGED` or any `ManagedType` returns an empty list. diff --git a/docs/coverage/coverage.json b/docs/coverage/coverage.json index 2ae466bf1..6b6c3109e 100644 --- a/docs/coverage/coverage.json +++ b/docs/coverage/coverage.json @@ -12531,6 +12531,148 @@ "name": "UpdateRuleGroup" } ], + "optionalCapabilities": [ + { + "name": "AnalysisReports", + "doc": "AnalysisReports covers the analysis report family.", + "operations": [ + { + "name": "GetAnalysisReport", + "doc": "GetAnalysisReport returns one report (its results are always empty)." + }, + { + "name": "ListAnalysisReports" + }, + { + "name": "StartAnalysisReport" + } + ] + }, + { + "name": "AvailabilityZones", + "doc": "AvailabilityZones covers the Availability-Zone associations of a transit", + "operations": [ + { + "name": "AssociateAvailabilityZones" + }, + { + "name": "DisassociateAvailabilityZones" + } + ] + }, + { + "name": "FirewallSettings", + "doc": "FirewallSettings covers the firewall-level setters.", + "operations": [ + { + "name": "UpdateAvailabilityZoneChangeProtection" + }, + { + "name": "UpdateFirewallAnalysisSettings" + }, + { + "name": "UpdateFirewallDescription" + }, + { + "name": "UpdateFirewallPolicyChangeProtection" + }, + { + "name": "UpdateSubnetChangeProtection" + } + ] + }, + { + "name": "FlowOperations", + "doc": "FlowOperations covers the flow capture / flush family.", + "operations": [ + { + "name": "DescribeFlowOperation" + }, + { + "name": "ListFlowOperationResults", + "doc": "ListFlowOperationResults returns the operation and its (always empty, there is no" + }, + { + "name": "ListFlowOperations" + }, + { + "name": "StartFlowCapture" + }, + { + "name": "StartFlowFlush" + } + ] + }, + { + "name": "Metadata", + "doc": "Metadata covers the read-only metadata/summary operations.", + "operations": [ + { + "name": "DescribeFirewallMetadata" + }, + { + "name": "DescribeRuleGroupMetadata" + }, + { + "name": "DescribeRuleGroupSummary" + } + ] + }, + { + "name": "ResourcePolicies", + "doc": "ResourcePolicies covers the resource-sharing IAM policies attached to a rule", + "operations": [ + { + "name": "DeleteResourcePolicy" + }, + { + "name": "DescribeResourcePolicy" + }, + { + "name": "PutResourcePolicy" + } + ] + }, + { + "name": "TLSInspection", + "doc": "TLSInspection covers the TLS inspection configuration family.", + "operations": [ + { + "name": "CreateTLSInspectionConfiguration" + }, + { + "name": "DeleteTLSInspectionConfiguration" + }, + { + "name": "DescribeTLSInspectionConfiguration" + }, + { + "name": "ListTLSInspectionConfigurations" + }, + { + "name": "UpdateTLSInspectionConfiguration" + } + ] + }, + { + "name": "VPCEndpointAssociations", + "doc": "VPCEndpointAssociations covers the VPC endpoint association family.", + "operations": [ + { + "name": "CreateVpcEndpointAssociation" + }, + { + "name": "DeleteVpcEndpointAssociation" + }, + { + "name": "DescribeVpcEndpointAssociation" + }, + { + "name": "ListVpcEndpointAssociations" + } + ] + } + ], "providers": { "aws": "NetworkFirewall" } diff --git a/docs/coverage/nongoals/networkfirewall.md b/docs/coverage/nongoals/networkfirewall.md new file mode 100644 index 000000000..c01c4f30b --- /dev/null +++ b/docs/coverage/nongoals/networkfirewall.md @@ -0,0 +1,9 @@ +- Not implemented (the operations answer `InvalidRequestException: unknown operation`): the proxy family (`CreateProxy`, `CreateProxyConfiguration`, `CreateProxyRuleGroup`, `CreateProxyRules`, the matching describe, update, delete and list operations, `AttachRuleGroupsToProxyConfiguration`, `DetachRuleGroupsFromProxyConfiguration`, `UpdateProxyRulePriorities`, `UpdateProxyRuleGroupPriorities`), the container association family (`CreateContainerAssociation` and its describe, update, delete and list operations), the transit gateway attachment operations (`AcceptNetworkFirewallTransitGatewayAttachment`, `RejectNetworkFirewallTransitGatewayAttachment`, `DeleteNetworkFirewallTransitGatewayAttachment`) and `UpdateFirewallEncryptionConfiguration`. `EncryptionConfiguration` on create and update requests is accepted and ignored. +- There is no packet data plane. Flow capture and flush operations are accepted and complete (immediately, or after the settle window under `--async-settle`) but their results list no flows. Analysis reports complete with no results. Nothing is published to the `AWS/NetworkFirewall` CloudWatch namespace, and the logging configuration is validated and stored but no alert, flow or TLS log is delivered to S3, CloudWatch Logs or Kinesis Data Firehose. +- Checks that need another service are not made: the VPC, subnet and Availability Zone ids a firewall or VPC endpoint association names are syntax-checked only, and the certificates, certificate authorities, KMS keys, log destinations and IAM principals named in TLS inspection configurations, logging configurations and resource policies are not looked up. Resource policies are validated (JSON, allowed actions per resource kind) and stored but never evaluated; sharing through Resource Access Manager is not modeled. +- Availability Zones are synthesized. A VPC firewall gets one zone per subnet in the order the subnets were added (`a`, `b`, ...) and a VPC endpoint association gets a zone derived from its subnet id; the real zone of a subnet is not known. +- A transit gateway-attached firewall can be created, described and have Availability Zones associated and disassociated, but no transit gateway attachment is created, so its status never reports a transit gateway attachment sync state. +- Firewalls, policies, rule groups and TLS inspection configurations are `READY` or `ACTIVE` as soon as they are created; there is no `PROVISIONING` period. `DryRun` and `AnalyzeRuleGroup` on create and update requests are accepted and ignored, so a dry run really makes the change and no rule group analysis results are produced. +- Rule content is stored and returned byte for byte. Only the parts needed for capacity and summaries are read: stateful rules (one unit per rule in `RulesString`, `StatefulRules` and `RulesSourceList` targets) and stateless rules (the product of the element counts of the match settings, as the CreateRuleGroup reference describes). Suricata syntax, rule variables and reference sets are not validated, and a rule group whose rules need more than its `Capacity` is rejected with `InvalidRequestException` (the exception real AWS uses for this is unconfirmed). +- Behaviours the AWS reference does not state were chosen conservatively and are unconfirmed against the live service: a duplicate name answers `InvalidRequestException` (the one exception every Create operation models); deleting a firewall that still has VPC endpoint associations is refused with `InvalidOperationException`; describing or deleting a resource policy that does not exist answers `ResourceNotFoundException`; `DescribeRuleGroupSummary` on a stateless rule group answers `InvalidRequestException` and reports only the options named in the rule group's `SummaryConfiguration`; tagging a resource does not change its update token; a request that gives both a name and an ARN uses the name. +- List operations take `MaxResults` (1 to 100, default 100) and `NextToken`, an opaque offset over results sorted by name, so it is only stable while the list does not change between pages. `ListRuleGroups` filters on `Type`, `Scope` and `ManagedType`; the emulator holds no AWS-managed rule groups, so `Scope=MANAGED` or any `ManagedType` returns an empty list. diff --git a/docs/services.md b/docs/services.md index a52a5315b..6927450ee 100644 --- a/docs/services.md +++ b/docs/services.md @@ -3959,7 +3959,7 @@ delete) is still enforced. | Serverless | 26 | | Networking | 51 | | Networking: AWS-specific (Transit Gateway / VPN / DHCP / prefix lists / egress-only IGW / endpoint services / Client VPN / Traffic Mirroring / Network Insights / VPC Block Public Access / IPAM full incl. discovery/BYOASN/BYOIP/resolver/policy + AWS/IPAM metrics) | 162 | -| Network Firewall: AWS | 20 | +| Network Firewall: AWS | 23 (+30 optional) | | Monitoring | 12 | | IAM | 35 | | DNS | 15 | @@ -4005,7 +4005,7 @@ delete) is still enforced. | Data Integration: AWS Glue | 299 | | Threat Detection: Amazon GuardDuty | 87 | | Streaming: Amazon MSK | 59 | -| **Grand Total** | **2842** (+227 optional) | +| **Grand Total** | **2845** (+257 optional) | Optional operations are capabilities a driver may implement but is not required to; see the sections marked "optional capability". They are counted separately diff --git a/internal/settle/settle.go b/internal/settle/settle.go index dcbffda5a..0a093742a 100644 --- a/internal/settle/settle.go +++ b/internal/settle/settle.go @@ -74,6 +74,8 @@ const ( DefaultBedrockIngestionSettle = 2 * time.Second // Bedrock KB ingestion job IN_PROGRESS->COMPLETE + DefaultFlowOperationSettle = 2 * time.Second // Network Firewall flow operation IN_PROGRESS->COMPLETED + // S3 RestoreObject ongoing-request="true"->"false", by retrieval tier. DefaultRestoreExpeditedSettle = 1 * time.Second DefaultRestoreStandardSettle = 3 * time.Second diff --git a/providers/aws/networkfirewall/analysis_report.go b/providers/aws/networkfirewall/analysis_report.go new file mode 100644 index 000000000..016e3e4e3 --- /dev/null +++ b/providers/aws/networkfirewall/analysis_report.go @@ -0,0 +1,114 @@ +package networkfirewall + +import ( + "context" + "sort" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/internal/idgen" + nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" +) + +const reportCompleted = "COMPLETED" + +// StartAnalysisReport requests a traffic analysis report. The analysis type has +// to be enabled on the firewall; with no data plane the report has no results. +func (m *Mock) StartAnalysisReport(_ context.Context, firewallName, firewallARN, analysisType string) (*nfdriver.AnalysisReport, error) { + m.mu.Lock() + defer m.mu.Unlock() + + fw, err := m.reportFirewall(firewallName, firewallARN) + if err != nil { + return nil, err + } + + if !oneOf(analysisType, analysisTypes...) { + return nil, invalid("AnalysisType %q is not valid", analysisType) + } + + if !oneOf(analysisType, fw.EnabledAnalysisTypes...) { + return nil, invalid("analysis type %s is not enabled on firewall %q", analysisType, fw.Name) + } + + rep := &nfdriver.AnalysisReport{ + ID: idgen.UUID(), FirewallARN: fw.ARN, AnalysisType: analysisType, + Status: reportCompleted, ReportTime: m.now(), + } + m.analysisReports.Set(rep.ID, rep) + + out := *rep + + return &out, nil +} + +// ListAnalysisReports lists the analysis reports of a firewall, oldest first. +func (m *Mock) ListAnalysisReports( + _ context.Context, firewallName, firewallARN string, opts nfdriver.ListOptions, +) ([]nfdriver.AnalysisReport, string, error) { + m.mu.RLock() + defer m.mu.RUnlock() + + fw, err := m.reportFirewall(firewallName, firewallARN) + if err != nil { + return nil, "", err + } + + var matched []*nfdriver.AnalysisReport + + for _, r := range m.analysisReports.SortedValues() { + if r.FirewallARN == fw.ARN { + matched = append(matched, r) + } + } + + sort.SliceStable(matched, func(i, j int) bool { return matched[i].ReportTime.Before(matched[j].ReportTime) }) + + items, next, err := paginate(matched, opts) + if err != nil { + return nil, "", err + } + + out := make([]nfdriver.AnalysisReport, 0, len(items)) + for _, r := range items { + out = append(out, *r) + } + + return out, next, nil +} + +// GetAnalysisReport returns one analysis report (it never has results). +func (m *Mock) GetAnalysisReport(_ context.Context, firewallName, firewallARN, reportID string) (*nfdriver.AnalysisReport, error) { + m.mu.RLock() + defer m.mu.RUnlock() + + fw, err := m.reportFirewall(firewallName, firewallARN) + if err != nil { + return nil, err + } + + if reportID == "" { + return nil, invalid("AnalysisReportId is required") + } + + rep, ok := m.analysisReports.Get(reportID) + if !ok || rep.FirewallARN != fw.ARN { + return nil, cerrors.Newf(cerrors.NotFound, "analysis report %q not found", reportID) + } + + out := *rep + + return &out, nil +} + +func (m *Mock) reportFirewall(name, arn string) (*nfdriver.Firewall, error) { + if err := requireIdentifier("Firewall", name, arn); err != nil { + return nil, err + } + + fw, ok := m.lookupFirewall(name, arn) + if !ok { + return nil, cerrors.Newf(cerrors.NotFound, "firewall %q not found", nonEmpty(name, arn)) + } + + return fw, nil +} diff --git a/providers/aws/networkfirewall/capacity.go b/providers/aws/networkfirewall/capacity.go new file mode 100644 index 000000000..61573705b --- /dev/null +++ b/providers/aws/networkfirewall/capacity.go @@ -0,0 +1,77 @@ +package networkfirewall + +import ( + "encoding/json" + "strings" +) + +// ruleSource is the part of a rule group payload the emulator reads to work out +// the capacity the rules consume (everything else is stored untouched). +type ruleSource struct { + RulesSource struct { + RulesString string `json:"RulesString"` + StatefulRules []json.RawMessage `json:"StatefulRules"` + RulesSourceList struct { + Targets []string `json:"Targets"` + } `json:"RulesSourceList"` + Stateless struct { + Rules []struct { + RuleDefinition struct { + MatchAttributes map[string][]json.RawMessage `json:"MatchAttributes"` + } `json:"RuleDefinition"` + } `json:"StatelessRules"` + } `json:"StatelessRulesAndCustomActions"` + } `json:"RulesSource"` +} + +// maxRulesStringLen is the AWS limit on a Suricata rules string. +const maxRulesStringLen = 2000000 + +// consumedCapacity validates a rule group payload and returns the capacity units its rules consume, per the AWS +// CreateRuleGroup reference: a stateful rule group needs one unit per rule; a +// stateless rule needs the product of the element counts of its match settings +// (an unset setting counts 1) and the group needs the sum over its rules. +func consumedCapacity(raw json.RawMessage) (int, error) { + if len(raw) == 0 { + return 0, nil + } + + var src ruleSource + if err := json.Unmarshal(raw, &src); err != nil { + return 0, invalid("RuleGroup is not valid: %v", err) + } + + if len(src.RulesSource.RulesString) > maxRulesStringLen { + return 0, invalid("RulesString exceeds %d characters", maxRulesStringLen) + } + + total := len(src.RulesSource.StatefulRules) + len(src.RulesSource.RulesSourceList.Targets) + + for _, line := range strings.Split(src.RulesSource.RulesString, "\n") { + if isRuleLine(line) { + total++ + } + } + + for _, r := range src.RulesSource.Stateless.Rules { + cost := 1 + + for _, elems := range r.RuleDefinition.MatchAttributes { + if len(elems) > 1 { + cost *= len(elems) + } + } + + total += cost + } + + return total, nil +} + +// isRuleLine reports whether a Suricata rules-string line is a rule (not blank +// and not a comment). +func isRuleLine(line string) bool { + t := strings.TrimSpace(line) + + return t != "" && !strings.HasPrefix(t, "#") +} diff --git a/providers/aws/networkfirewall/concurrency_test.go b/providers/aws/networkfirewall/concurrency_test.go new file mode 100644 index 000000000..a483f9b29 --- /dev/null +++ b/providers/aws/networkfirewall/concurrency_test.go @@ -0,0 +1,248 @@ +package networkfirewall + +import ( + "fmt" + "sync" + "sync/atomic" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" +) + +const racers = 50 + +// runRacers calls fn from racers goroutines and counts successes and +// "already exists" losers. +func runRacers(fn func() error) (wins, losers int32) { + var ( + wg sync.WaitGroup + w, l atomic.Int32 + ) + + for i := 0; i < racers; i++ { + wg.Add(1) + + go func() { + defer wg.Done() + + switch err := fn(); { + case err == nil: + w.Add(1) + case cerrors.IsAlreadyExists(err): + l.Add(1) + } + }() + } + + wg.Wait() + + return w.Load(), l.Load() +} + +func TestCreate_50SameName_ExactlyOneWins(t *testing.T) { + m := newMock() + pol := seedPolicy(t, m, "pol-1") + + creates := map[string]func() error{ + "firewall": func() error { + _, err := m.CreateFirewall(bg, nfdriver.CreateFirewallConfig{Name: "fw", PolicyARN: pol.ARN, VPCID: "vpc-1a"}) + return err + }, + "policy": func() error { + _, err := m.CreateFirewallPolicy(bg, refPolicy("same", nil, nil)) + return err + }, + "rule group": func() error { + _, err := m.CreateRuleGroup(bg, nfdriver.CreateRuleGroupConfig{Name: "rg", Type: "STATEFUL", Capacity: 1}) + return err + }, + "tls": func() error { + _, err := m.CreateTLSInspectionConfiguration(bg, nfdriver.CreateTLSInspectionConfig{Name: "tls", Configuration: tlsConfigJSON("c")}) + return err + }, + } + + for name, create := range creates { + t.Run(name, func(t *testing.T) { + wins, losers := runRacers(create) + assert.EqualValues(t, 1, wins) + assert.EqualValues(t, racers-1, losers) + }) + } +} + +func TestRuleGroup_DeleteVsPolicyReference_Race(t *testing.T) { + for round := 0; round < 25; round++ { + m := newMock() + rg := seedRuleGroup(t, m, "rg", "STATEFUL", 5) + pol := seedPolicy(t, m, "pol") + + var wg sync.WaitGroup + + wg.Add(2) + + go func() { + defer wg.Done() + + _, _ = m.UpdateFirewallPolicy(bg, "pol", "", nfdriver.UpdateFirewallPolicyConfig{ + UpdateToken: pol.UpdateToken, StatelessDefaultActions: standardActions, StatelessFragmentDefaultActions: standardActions, + StatefulRuleGroupReferences: []nfdriver.RuleGroupReference{{ResourceARN: rg.ARN}}, + }) + }() + + go func() { + defer wg.Done() + + _, _ = m.DeleteRuleGroup(bg, "rg", "", "STATEFUL") + }() + + wg.Wait() + + // Whatever order won, a policy never points at a deleted rule group. + got, err := m.DescribeFirewallPolicy(bg, "pol", "") + require.NoError(t, err) + + for _, ref := range got.StatefulRuleGroupReferences { + _, err := m.DescribeRuleGroup(bg, "", ref.ResourceARN, "") + assert.NoError(t, err, "round %d: dangling reference", round) + } + } +} + +func TestCreateVsDelete_NoOrphans(t *testing.T) { + for round := 0; round < 25; round++ { + m := newMock() + _, fw := seedStack(t, m) + + var wg sync.WaitGroup + + wg.Add(2) + + go func() { + defer wg.Done() + + _, _ = m.CreateVpcEndpointAssociation(bg, nfdriver.CreateVPCEndpointAssociationConfig{ + FirewallARN: fw.ARN, VPCID: "vpc-2b", SubnetID: "subnet-9", + }) + }() + + go func() { + defer wg.Done() + + _, _ = m.DeleteFirewall(bg, "fw-1", "") + }() + + wg.Wait() + + // Either the association exists and the firewall survived (delete refused), + // or the firewall is gone and no association points at it. + _, fwErr := m.DescribeFirewall(bg, "fw-1", "") + assocs, _, err := m.ListVpcEndpointAssociations(bg, "", nfdriver.ListOptions{}) + require.NoError(t, err) + + if fwErr != nil { + assert.Empty(t, assocs, "round %d: association orphaned by firewall delete", round) + } + } +} + +func TestDeepCopy_MutateReturned_NoAlias(t *testing.T) { + m := newMock() + pol, fw := seedStack(t, m) + + _, err := m.CreateFirewallPolicy(bg, nfdriver.CreateFirewallPolicyConfig{ + Name: "deep", StatelessDefaultActions: []string{"aws:pass"}, StatelessFragmentDefaultActions: standardActions, + Tags: map[string]string{"k": "v"}, Extra: nfdriver.PolicyExtra{StatelessCustomActions: []byte(`[]`)}, + }) + require.NoError(t, err) + + rg, err := m.CreateRuleGroup(bg, nfdriver.CreateRuleGroupConfig{ + Name: "rg", Type: "STATEFUL", Capacity: 5, Rules: rulesString("r1"), Tags: map[string]string{"k": "v"}, + SummaryRuleOptions: []string{"SID"}, + }) + require.NoError(t, err) + + require.NoError(t, m.UpdateLoggingConfiguration(bg, "fw-1", &nfdriver.LoggingConfiguration{ + Destinations: []nfdriver.LogDestinationConfig{s3Dest("FLOW", "b")}, + })) + + var wg sync.WaitGroup + + for i := 0; i < 20; i++ { + wg.Add(2) + + go func() { + defer wg.Done() + + // Mutators scribble on everything they were handed. + got, _ := m.DescribeFirewall(bg, "fw-1", "") + got.SubnetIDs[0] = "scribbled" + got.Tags = map[string]string{"x": "y"} + + p, _ := m.DescribeFirewallPolicy(bg, "deep", "") + p.StatelessDefaultActions[0] = "scribbled" + p.Tags["k"] = "scribbled" + p.Extra.StatelessCustomActions[0] = 'X' + + r, _ := m.DescribeRuleGroup(bg, "rg", "", "STATEFUL") + r.Rules[0] = 'X' + r.Tags["k"] = "scribbled" + r.SummaryRuleOptions[0] = "scribbled" + + l, _ := m.DescribeLoggingConfiguration(bg, "fw-1") + l.Destinations[0].LogDestination["bucketName"] = "scribbled" + }() + + go func() { + defer wg.Done() + + _, _ = m.DescribeFirewall(bg, "fw-1", "") + _, _, _ = m.ListFirewallPolicies(bg, nfdriver.ListOptions{}) + _, _, _ = m.ListRuleGroups(bg, nfdriver.RuleGroupFilter{}, nfdriver.ListOptions{}) + _, _ = m.DescribeLoggingConfiguration(bg, "fw-1") + }() + } + + wg.Wait() + + gotFw, _ := m.DescribeFirewall(bg, "fw-1", "") + assert.Equal(t, []string{"subnet-1"}, gotFw.SubnetIDs) + assert.Empty(t, gotFw.Tags) + + gotPol, _ := m.DescribeFirewallPolicy(bg, "deep", "") + assert.Equal(t, []string{"aws:pass"}, gotPol.StatelessDefaultActions) + assert.Equal(t, map[string]string{"k": "v"}, gotPol.Tags) + assert.Equal(t, "[]", string(gotPol.Extra.StatelessCustomActions)) + + gotRg, _ := m.DescribeRuleGroup(bg, "rg", "", "STATEFUL") + assert.Equal(t, rg.Rules, gotRg.Rules) + assert.Equal(t, map[string]string{"k": "v"}, gotRg.Tags) + assert.Equal(t, []string{"SID"}, gotRg.SummaryRuleOptions) + + gotLog, _ := m.DescribeLoggingConfiguration(bg, "fw-1") + assert.Equal(t, "b", gotLog.Destinations[0].LogDestination["bucketName"]) + + _, _ = pol, fw +} + +func TestCreateFirewall_CallerSliceNotRetained(t *testing.T) { + m := newMock() + pol := seedPolicy(t, m, "pol-1") + + subnets := []string{"subnet-1"} + tags := map[string]string{"a": "b"} + + _, err := m.CreateFirewall(bg, nfdriver.CreateFirewallConfig{Name: "fw", PolicyARN: pol.ARN, VPCID: "vpc-1a", SubnetIDs: subnets, Tags: tags}) + require.NoError(t, err) + + subnets[0] = fmt.Sprint("mutated") + tags["a"] = "mutated" + + got, _ := m.DescribeFirewall(bg, "fw", "") + assert.Equal(t, []string{"subnet-1"}, got.SubnetIDs) + assert.Equal(t, "b", got.Tags["a"]) +} diff --git a/providers/aws/networkfirewall/families_test.go b/providers/aws/networkfirewall/families_test.go new file mode 100644 index 000000000..4b24a7669 --- /dev/null +++ b/providers/aws/networkfirewall/families_test.go @@ -0,0 +1,528 @@ +package networkfirewall + +import ( + "encoding/json" + "strings" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/stackshy/cloudemu/v2/config" + cerrors "github.com/stackshy/cloudemu/v2/errors" + nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" +) + +func tlsConfigJSON(certARNs ...string) json.RawMessage { + certs := make([]map[string]string, 0, len(certARNs)) + for _, a := range certARNs { + certs = append(certs, map[string]string{"ResourceArn": a}) + } + + b, _ := json.Marshal(map[string]any{"ServerCertificateConfigurations": []map[string]any{{"ServerCertificates": certs}}}) + + return b +} + +func policyDoc(actions ...string) string { + b, _ := json.Marshal(map[string]any{ + "Version": "2012-10-17", + "Statement": []map[string]any{{ + "Effect": "Allow", "Principal": map[string]string{"AWS": "arn:aws:iam::111122223333:root"}, + "Action": actions, "Resource": "*", + }}, + }) + + return string(b) +} + +func TestResourcePolicy_PutDescribeDelete_AllTypes(t *testing.T) { + m := newMock() + pol, fw := seedStack(t, m) + rg := seedRuleGroup(t, m, "rg-1", "STATEFUL", 5) + sl := seedRuleGroup(t, m, "rg-sl", "STATELESS", 5) + + cases := map[string]struct{ arn, action string }{ + "rule group": {rg.ARN, "network-firewall:CreateFirewallPolicy"}, + "stateless rule group": {sl.ARN, "network-firewall:ListRuleGroups"}, + "firewall policy": {pol.ARN, "network-firewall:AssociateFirewallPolicy"}, + "firewall": {fw.ARN, "network-firewall:ListFirewalls"}, + } + + for name, c := range cases { + t.Run(name, func(t *testing.T) { + doc := policyDoc(c.action) + require.NoError(t, m.PutResourcePolicy(bg, c.arn, doc)) + + got, err := m.DescribeResourcePolicy(bg, c.arn) + require.NoError(t, err) + assert.JSONEq(t, doc, got) + + require.NoError(t, m.DeleteResourcePolicy(bg, c.arn)) + + _, err = m.DescribeResourcePolicy(bg, c.arn) + assert.True(t, cerrors.IsNotFound(err), "after delete: %v", err) + }) + } +} + +func TestResourcePolicy_Validation(t *testing.T) { + m := newMock() + pol, fw := seedStack(t, m) + + err := m.PutResourcePolicy(bg, pol.ARN, "{not json") + requireException(t, err, nfdriver.ExInvalidResourcePolicy) + + err = m.PutResourcePolicy(bg, pol.ARN, policyDoc("network-firewall:ListFirewalls")) + requireException(t, err, nfdriver.ExInvalidResourcePolicy) + + err = m.PutResourcePolicy(bg, pol.ARN, `{"Version":"2012-10-17","Statement":[]}`) + requireException(t, err, nfdriver.ExInvalidResourcePolicy) + + err = m.PutResourcePolicy(bg, fw.ARN, `{"Statement":[{"Effect":"Allow"}]}`) + requireException(t, err, nfdriver.ExInvalidResourcePolicy) + + err = m.PutResourcePolicy(bg, "arn:aws:network-firewall:us-east-1:000000000000:firewall/ghost", policyDoc("network-firewall:ListFirewalls")) + assert.True(t, cerrors.IsNotFound(err), "%v", err) + + err = m.PutResourcePolicy(bg, "not-an-arn", policyDoc("network-firewall:ListFirewalls")) + assert.True(t, cerrors.IsInvalidArgument(err), "%v", err) + + err = m.PutResourcePolicy(bg, fw.ARN, "") + assert.True(t, cerrors.IsInvalidArgument(err), "%v", err) + + err = m.PutResourcePolicy(bg, fw.ARN, strings.Repeat("x", 395001)) + assert.True(t, cerrors.IsInvalidArgument(err), "%v", err) + + tls, tlsErr := m.CreateTLSInspectionConfiguration(bg, nfdriver.CreateTLSInspectionConfig{Name: "t", Configuration: tlsConfigJSON("c")}) + require.NoError(t, tlsErr) + + err = m.PutResourcePolicy(bg, tls.ARN, policyDoc("network-firewall:ListFirewalls")) + assert.True(t, cerrors.IsInvalidArgument(err), "a TLS configuration cannot be shared: %v", err) +} + +func TestResourcePolicy_RemovedWithResource(t *testing.T) { + m := newMock() + pol := seedPolicy(t, m, "pol-1") + + require.NoError(t, m.PutResourcePolicy(bg, pol.ARN, policyDoc("network-firewall:AssociateFirewallPolicy"))) + _, err := m.DeleteFirewallPolicy(bg, "pol-1", "") + require.NoError(t, err) + + assert.Empty(t, m.resourcePolicies, "no orphaned policy after the resource is deleted") +} + +func TestTLS_CRUD_List_Tagging(t *testing.T) { + m, clock := newClockMock(t) + + created, err := m.CreateTLSInspectionConfiguration(bg, nfdriver.CreateTLSInspectionConfig{ + Name: "tls-1", Description: "d", Configuration: tlsConfigJSON("arn:aws:acm:us-east-1:000000000000:certificate/c1"), + Tags: map[string]string{"a": "b"}, + }) + require.NoError(t, err) + assert.Equal(t, "ACTIVE", created.Status) + assert.Equal(t, []string{"arn:aws:acm:us-east-1:000000000000:certificate/c1"}, created.CertificateARNs) + assert.Regexp(t, uuidPattern, created.UpdateToken) + + clock.Advance(time.Minute) + + updated, err := m.UpdateTLSInspectionConfiguration(bg, "tls-1", "", nfdriver.UpdateTLSInspectionConfig{ + UpdateToken: created.UpdateToken, Description: "d2", Configuration: tlsConfigJSON("c2", "c3"), + }) + require.NoError(t, err) + assert.NotEqual(t, created.UpdateToken, updated.UpdateToken) + assert.True(t, updated.LastModified.After(created.LastModified)) + assert.Len(t, updated.CertificateARNs, 2) + + byARN, err := m.DescribeTLSInspectionConfiguration(bg, "", created.ARN) + require.NoError(t, err) + assert.Equal(t, "d2", byARN.Description) + + require.NoError(t, m.TagResource(bg, created.ARN, map[string]string{"c": "d"})) + + tags, err := m.ListTagsForResource(bg, created.ARN) + require.NoError(t, err) + assert.Equal(t, map[string]string{"a": "b", "c": "d"}, tags) + + items, _, err := m.ListTLSInspectionConfigurations(bg, nfdriver.ListOptions{}) + require.NoError(t, err) + assert.Len(t, items, 1) + + deleted, err := m.DeleteTLSInspectionConfiguration(bg, "tls-1", "") + require.NoError(t, err) + assert.Equal(t, "DELETING", deleted.Status) + + _, err = m.DescribeTLSInspectionConfiguration(bg, "tls-1", "") + assert.True(t, cerrors.IsNotFound(err)) +} + +func TestTLS_Validation(t *testing.T) { + m := newMock() + + cases := map[string]nfdriver.CreateTLSInspectionConfig{ + "no configuration": {Name: "t"}, + "empty configuration": {Name: "t", Configuration: []byte(`{"ServerCertificateConfigurations":[]}`)}, + "malformed": {Name: "t", Configuration: []byte(`{`)}, + "bad name": {Name: "t_1", Configuration: tlsConfigJSON("c")}, + "long description": {Name: "t", Description: strings.Repeat("d", 513), Configuration: tlsConfigJSON("c")}, + } + + for name, cfg := range cases { + t.Run(name, func(t *testing.T) { + _, err := m.CreateTLSInspectionConfiguration(bg, cfg) + assert.True(t, cerrors.IsInvalidArgument(err), "want InvalidArgument, got %v", err) + }) + } + + created, err := m.CreateTLSInspectionConfiguration(bg, nfdriver.CreateTLSInspectionConfig{Name: "t", Configuration: tlsConfigJSON("c")}) + require.NoError(t, err) + + _, err = m.CreateTLSInspectionConfiguration(bg, nfdriver.CreateTLSInspectionConfig{Name: "t", Configuration: tlsConfigJSON("c")}) + assert.True(t, cerrors.IsAlreadyExists(err)) + + _, err = m.UpdateTLSInspectionConfiguration(bg, "t", "", nfdriver.UpdateTLSInspectionConfig{Configuration: tlsConfigJSON("c")}) + assert.True(t, cerrors.IsInvalidArgument(err), "update needs a token: %v", err) + + _, err = m.UpdateTLSInspectionConfiguration(bg, "t", "", nfdriver.UpdateTLSInspectionConfig{ + UpdateToken: "11111111-1111-1111-1111-111111111111", Configuration: tlsConfigJSON("c"), + }) + requireException(t, err, nfdriver.ExInvalidToken) + + _ = created +} + +func TestTLS_InUseByPolicy_BlockedDelete(t *testing.T) { + m := newMock() + + tlsCfg, err := m.CreateTLSInspectionConfiguration(bg, nfdriver.CreateTLSInspectionConfig{Name: "t", Configuration: tlsConfigJSON("c")}) + require.NoError(t, err) + + pol, err := m.CreateFirewallPolicy(bg, nfdriver.CreateFirewallPolicyConfig{ + Name: "p", StatelessDefaultActions: standardActions, StatelessFragmentDefaultActions: standardActions, + TLSInspectionConfigurationARN: tlsCfg.ARN, + }) + require.NoError(t, err) + + got, _ := m.DescribeTLSInspectionConfiguration(bg, "t", "") + assert.Equal(t, 1, got.NumberOfAssociations) + + _, err = m.DeleteTLSInspectionConfiguration(bg, "t", "") + requireException(t, err, nfdriver.ExInvalidOperation) + + _, err = m.DeleteFirewallPolicy(bg, pol.Name, "") + require.NoError(t, err) + + _, err = m.DeleteTLSInspectionConfiguration(bg, "t", "") + require.NoError(t, err) +} + +func TestTLS_NoPrivateMaterialInResponse(t *testing.T) { + m := newMock() + + cfg := []byte(`{"ServerCertificateConfigurations":[{"CertificateAuthorityArn":"arn:aws:acm-pca:us-east-1:000000000000:certificate-authority/ca",` + + `"ServerCertificates":[{"ResourceArn":"arn:aws:acm:us-east-1:000000000000:certificate/c1"}]}]}`) + + got, err := m.CreateTLSInspectionConfiguration(bg, nfdriver.CreateTLSInspectionConfig{Name: "t", Configuration: cfg}) + require.NoError(t, err) + + // Only ARNs are stored and echoed; there is no key or certificate body to leak. + assert.NotContains(t, string(got.Configuration), "PRIVATE KEY") + assert.Equal(t, []string{"arn:aws:acm:us-east-1:000000000000:certificate/c1"}, got.CertificateARNs) +} + +func vpcAssociation(t *testing.T, m *Mock, firewallARN, subnet string) *nfdriver.VPCEndpointAssociation { + t.Helper() + + a, err := m.CreateVpcEndpointAssociation(bg, nfdriver.CreateVPCEndpointAssociationConfig{ + FirewallARN: firewallARN, VPCID: "vpc-2b", SubnetID: subnet, Description: "extra", + }) + require.NoError(t, err) + + return a +} + +func TestVPCEndpointAssociation_CRUD_List_FilterByFirewall(t *testing.T) { + m := newMock() + pol, fw := seedStack(t, m) + other := seedFirewall(t, m, "fw-2", pol.ARN) + + a1 := vpcAssociation(t, m, fw.ARN, "subnet-a") + vpcAssociation(t, m, other.ARN, "subnet-b") + + assert.Equal(t, "READY", a1.Status) + assert.NotEmpty(t, a1.AvailabilityZone) + + got, err := m.DescribeVpcEndpointAssociation(bg, a1.ARN) + require.NoError(t, err) + assert.Equal(t, a1.ID, got.ID) + + again, _ := m.DescribeVpcEndpointAssociation(bg, a1.ARN) + assert.Equal(t, got.AvailabilityZone, again.AvailabilityZone, "stable across reads") + + all, _, err := m.ListVpcEndpointAssociations(bg, "", nfdriver.ListOptions{}) + require.NoError(t, err) + assert.Len(t, all, 2) + + mine, _, err := m.ListVpcEndpointAssociations(bg, fw.ARN, nfdriver.ListOptions{}) + require.NoError(t, err) + require.Len(t, mine, 1) + assert.Equal(t, a1.ARN, mine[0].ARN) + + fwNow, _ := m.DescribeFirewall(bg, "fw-1", "") + assert.Equal(t, 1, fwNow.NumberOfAssociations) + + require.NoError(t, m.TagResource(bg, a1.ARN, map[string]string{"k": "v"})) + + deleted, err := m.DeleteVpcEndpointAssociation(bg, a1.ARN) + require.NoError(t, err) + assert.Equal(t, "DELETING", deleted.Status) + + _, err = m.DescribeVpcEndpointAssociation(bg, a1.ARN) + assert.True(t, cerrors.IsNotFound(err)) +} + +func TestVPCEndpointAssociation_Validation(t *testing.T) { + m := newMock() + _, fw := seedStack(t, m) + + cases := map[string]nfdriver.CreateVPCEndpointAssociationConfig{ + "bad vpc": {FirewallARN: fw.ARN, VPCID: "nope", SubnetID: "s"}, + "no subnet": {FirewallARN: fw.ARN, VPCID: "vpc-2b"}, + "bad ip type": {FirewallARN: fw.ARN, VPCID: "vpc-2b", SubnetID: "s", IPAddressType: "IPV5"}, + "malformed arn": {FirewallARN: "x", VPCID: "vpc-2b", SubnetID: "s"}, + "long description": {FirewallARN: fw.ARN, VPCID: "vpc-2b", SubnetID: "s", Description: strings.Repeat("d", 513)}, + } + + for name, cfg := range cases { + t.Run(name, func(t *testing.T) { + _, err := m.CreateVpcEndpointAssociation(bg, cfg) + assert.True(t, cerrors.IsInvalidArgument(err), "want InvalidArgument, got %v", err) + }) + } + + _, err := m.CreateVpcEndpointAssociation(bg, nfdriver.CreateVPCEndpointAssociationConfig{ + FirewallARN: "arn:aws:network-firewall:us-east-1:000000000000:firewall/ghost", VPCID: "vpc-2b", SubnetID: "s", + }) + assert.True(t, cerrors.IsNotFound(err), "%v", err) +} + +func TestDeleteFirewall_WithAssociations_Blocked(t *testing.T) { + m := newMock() + _, fw := seedStack(t, m) + + a := vpcAssociation(t, m, fw.ARN, "subnet-a") + + _, err := m.DeleteFirewall(bg, "fw-1", "") + requireException(t, err, nfdriver.ExInvalidOperation) + + _, err = m.DeleteVpcEndpointAssociation(bg, a.ARN) + require.NoError(t, err) + + _, err = m.DeleteFirewall(bg, "fw-1", "") + require.NoError(t, err) +} + +func TestMetadata_Firewall_RuleGroup(t *testing.T) { + m, _ := newClockMock(t) + pol, fw := seedStack(t, m) + rg := seedRuleGroup(t, m, "rg-1", "STATEFUL", 7) + + meta, err := m.DescribeFirewallMetadata(bg, fw.ARN) + require.NoError(t, err) + assert.Equal(t, fw.ARN, meta.FirewallARN) + assert.Equal(t, pol.ARN, meta.PolicyARN) + assert.Equal(t, "READY", meta.Status) + assert.Equal(t, []string{"us-east-1a"}, meta.AvailabilityZones) + + _, err = m.DescribeFirewallMetadata(bg, "arn:aws:network-firewall:us-east-1:000000000000:firewall/ghost") + assert.True(t, cerrors.IsNotFound(err)) + + got, err := m.DescribeRuleGroupMetadata(bg, "", rg.ARN, "") + require.NoError(t, err) + assert.Equal(t, 7, got.Capacity) +} + +func TestRuleGroupSummary_ParsesMsgSIDMetadata(t *testing.T) { + m := newMock() + + _, err := m.CreateRuleGroup(bg, nfdriver.CreateRuleGroupConfig{ + Name: "rg", Type: "STATEFUL", Capacity: 10, SummaryRuleOptions: []string{"MSG", "SID", "METADATA"}, + Rules: rulesString( + `# a comment`, + `alert tcp any any -> any 80 (msg:"web hit"; metadata:author tester; sid:100;)`, + `drop udp any any -> any 53 (msg:"dns"; sid:101;)`), + }) + require.NoError(t, err) + + _, sums, err := m.DescribeRuleGroupSummary(bg, "rg", "", "STATEFUL") + require.NoError(t, err) + require.Len(t, sums, 2) + assert.Equal(t, nfdriver.RuleSummary{Msg: "web hit", SID: "100", Metadata: "author tester"}, sums[0]) + assert.Equal(t, nfdriver.RuleSummary{Msg: "dns", SID: "101"}, sums[1]) + + // Only the options named in SummaryConfiguration are populated. + _, err = m.CreateRuleGroup(bg, nfdriver.CreateRuleGroupConfig{ + Name: "rg2", Type: "STATEFUL", Capacity: 10, SummaryRuleOptions: []string{"SID"}, + Rules: rulesString(`alert tcp any any -> any 80 (msg:"web hit"; sid:7;)`), + }) + require.NoError(t, err) + + _, sums, err = m.DescribeRuleGroupSummary(bg, "rg2", "", "STATEFUL") + require.NoError(t, err) + assert.Equal(t, []nfdriver.RuleSummary{{SID: "7"}}, sums) +} + +func TestRuleGroupSummary_Stateless_InvalidRequest(t *testing.T) { + m := newMock() + seedRuleGroup(t, m, "sl", "STATELESS", 3) + + _, _, err := m.DescribeRuleGroupSummary(bg, "sl", "", "STATELESS") + assert.True(t, cerrors.IsInvalidArgument(err), "%v", err) +} + +func TestFlowOperations_StartDescribeListResults(t *testing.T) { + m, clock := newClockMock(t) + _, fw := seedStack(t, m) + scope := nfdriver.FlowOperationScope{FirewallARN: fw.ARN, AvailabilityZone: "us-east-1a"} + + capture, err := m.StartFlowCapture(bg, nfdriver.FlowOperationRequest{FirewallARN: fw.ARN, AvailabilityZone: "us-east-1a"}) + require.NoError(t, err) + assert.Regexp(t, uuidPattern, capture.ID) + assert.Equal(t, "COMPLETED", capture.Status) + assert.Equal(t, "FLOW_CAPTURE", capture.Type) + + clock.Advance(time.Second) + + flush, err := m.StartFlowFlush(bg, nfdriver.FlowOperationRequest{FirewallARN: fw.ARN, AvailabilityZone: "us-east-1a"}) + require.NoError(t, err) + assert.Equal(t, "FLOW_FLUSH", flush.Type) + + got, err := m.DescribeFlowOperation(bg, scope, capture.ID) + require.NoError(t, err) + assert.Equal(t, capture.RequestedAt, got.RequestedAt, "timestamp is stable") + + all, _, err := m.ListFlowOperations(bg, scope, "", nfdriver.ListOptions{}) + require.NoError(t, err) + require.Len(t, all, 2) + assert.Equal(t, capture.ID, all[0].ID, "oldest first") + + onlyFlush, _, err := m.ListFlowOperations(bg, scope, "FLOW_FLUSH", nfdriver.ListOptions{}) + require.NoError(t, err) + require.Len(t, onlyFlush, 1) + assert.Equal(t, flush.ID, onlyFlush[0].ID) + + op, next, err := m.ListFlowOperationResults(bg, scope, capture.ID, nfdriver.ListOptions{}) + require.NoError(t, err) + assert.Equal(t, capture.ID, op.ID) + assert.Empty(t, next, "no data plane, so no flows") +} + +func TestFlowOperations_Validation(t *testing.T) { + m := newMock() + pol, fw := seedStack(t, m) + assoc := vpcAssociation(t, m, fw.ARN, "subnet-a") + other := seedFirewall(t, m, "fw-2", pol.ARN) + + _, err := m.StartFlowCapture(bg, nfdriver.FlowOperationRequest{FirewallARN: "arn:aws:network-firewall:us-east-1:000000000000:firewall/ghost", AvailabilityZone: "us-east-1a"}) + assert.True(t, cerrors.IsNotFound(err), "%v", err) + + _, err = m.StartFlowCapture(bg, nfdriver.FlowOperationRequest{FirewallARN: fw.ARN}) + assert.True(t, cerrors.IsInvalidArgument(err), "no endpoint selector: %v", err) + + _, err = m.StartFlowCapture(bg, nfdriver.FlowOperationRequest{FirewallARN: fw.ARN, AvailabilityZone: "eu-west-1a"}) + assert.True(t, cerrors.IsInvalidArgument(err), "zone without an endpoint: %v", err) + + _, err = m.StartFlowCapture(bg, nfdriver.FlowOperationRequest{FirewallARN: other.ARN, VPCEndpointAssociationARN: assoc.ARN}) + assert.True(t, cerrors.IsInvalidArgument(err), "association of another firewall: %v", err) + + neg := int32(-1) + _, err = m.StartFlowFlush(bg, nfdriver.FlowOperationRequest{FirewallARN: fw.ARN, AvailabilityZone: "us-east-1a", MinimumFlowAgeSeconds: &neg}) + assert.True(t, cerrors.IsInvalidArgument(err), "%v", err) + + _, _, err = m.ListFlowOperations(bg, nfdriver.FlowOperationScope{FirewallARN: fw.ARN, AvailabilityZone: "us-east-1a"}, "BOGUS", nfdriver.ListOptions{}) + assert.True(t, cerrors.IsInvalidArgument(err), "%v", err) + + _, err = m.DescribeFlowOperation(bg, nfdriver.FlowOperationScope{FirewallARN: fw.ARN, AvailabilityZone: "us-east-1a"}, "no-such-op") + assert.True(t, cerrors.IsNotFound(err), "%v", err) + + ok, err := m.StartFlowCapture(bg, nfdriver.FlowOperationRequest{FirewallARN: fw.ARN, VPCEndpointAssociationARN: assoc.ARN}) + require.NoError(t, err) + + _, err = m.DescribeFlowOperation(bg, nfdriver.FlowOperationScope{FirewallARN: fw.ARN, AvailabilityZone: "us-east-1a"}, ok.ID) + assert.True(t, cerrors.IsNotFound(err), "an operation of another endpoint is not visible: %v", err) +} + +func TestFlowOperations_AsyncSettle(t *testing.T) { + clock := config.NewFakeClock(time.Date(2026, 1, 2, 3, 4, 5, 0, time.UTC)) + m := New(config.NewOptions(config.WithClock(clock), config.WithAsyncSettle())) + _, fw := seedStack(t, m) + + op, err := m.StartFlowFlush(bg, nfdriver.FlowOperationRequest{FirewallARN: fw.ARN, AvailabilityZone: "us-east-1a"}) + require.NoError(t, err) + assert.Equal(t, "IN_PROGRESS", op.Status) + + clock.Advance(10 * time.Second) + + got, err := m.DescribeFlowOperation(bg, nfdriver.FlowOperationScope{FirewallARN: fw.ARN, AvailabilityZone: "us-east-1a"}, op.ID) + require.NoError(t, err) + assert.Equal(t, "COMPLETED", got.Status) +} + +func TestAnalysisReport_Start_List_Get(t *testing.T) { + m, clock := newClockMock(t) + _, fw := seedStack(t, m) + + _, err := m.UpdateFirewallAnalysisSettings(bg, "fw-1", []string{"TLS_SNI", "HTTP_HOST"}, "") + require.NoError(t, err) + + r1, err := m.StartAnalysisReport(bg, "fw-1", "", "TLS_SNI") + require.NoError(t, err) + assert.Equal(t, "COMPLETED", r1.Status) + + clock.Advance(time.Minute) + + r2, err := m.StartAnalysisReport(bg, "", fw.ARN, "HTTP_HOST") + require.NoError(t, err) + + list, _, err := m.ListAnalysisReports(bg, "fw-1", "", nfdriver.ListOptions{}) + require.NoError(t, err) + require.Len(t, list, 2) + assert.Equal(t, r1.ID, list[0].ID) + + got, err := m.GetAnalysisReport(bg, "fw-1", "", r2.ID) + require.NoError(t, err) + assert.Equal(t, "HTTP_HOST", got.AnalysisType) + + _, err = m.GetAnalysisReport(bg, "fw-1", "", "ghost") + assert.True(t, cerrors.IsNotFound(err)) +} + +func TestAnalysisReport_TypeNotEnabled(t *testing.T) { + m := newMock() + seedStack(t, m) + + _, err := m.StartAnalysisReport(bg, "fw-1", "", "TLS_SNI") + assert.True(t, cerrors.IsInvalidArgument(err), "analysis not enabled: %v", err) + + _, err = m.UpdateFirewallAnalysisSettings(bg, "fw-1", []string{"NOPE"}, "") + assert.True(t, cerrors.IsInvalidArgument(err), "%v", err) + + _, err = m.StartAnalysisReport(bg, "fw-1", "", "BOGUS") + assert.True(t, cerrors.IsInvalidArgument(err), "%v", err) +} + +func TestFlowAndReportOps_UnknownFirewall(t *testing.T) { + m := newMock() + + _, err := m.StartAnalysisReport(bg, "ghost", "", "TLS_SNI") + assert.True(t, cerrors.IsNotFound(err), "%v", err) + + _, _, err = m.ListAnalysisReports(bg, "ghost", "", nfdriver.ListOptions{}) + assert.True(t, cerrors.IsNotFound(err), "%v", err) + + _, err = m.StartFlowFlush(bg, nfdriver.FlowOperationRequest{ + FirewallARN: "arn:aws:network-firewall:us-east-1:000000000000:firewall/ghost", AvailabilityZone: "a", + }) + assert.True(t, cerrors.IsNotFound(err), "%v", err) +} diff --git a/providers/aws/networkfirewall/firewall.go b/providers/aws/networkfirewall/firewall.go new file mode 100644 index 000000000..1e847f867 --- /dev/null +++ b/providers/aws/networkfirewall/firewall.go @@ -0,0 +1,557 @@ +package networkfirewall + +import ( + "context" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/internal/idgen" + nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" +) + +// Enum values copied verbatim from the AWS SDK (networkfirewall types). +// +//nolint:gochecknoglobals // immutable enum value sets. +var ( + analysisTypes = []string{"TLS_SNI", "HTTP_HOST"} + ipAddressType = []string{"DUALSTACK", "IPV4", "IPV6"} +) + +//nolint:gocritic // cfg matches the driver signature. +func (m *Mock) CreateFirewall(_ context.Context, cfg nfdriver.CreateFirewallConfig) (*nfdriver.Firewall, error) { + m.mu.Lock() + defer m.mu.Unlock() + + if err := m.validateCreateFirewall(&cfg); err != nil { + return nil, err + } + + fw := &nfdriver.Firewall{ + Name: cfg.Name, + ARN: m.arn("firewall", cfg.Name), + ID: idgen.GenerateID(""), + PolicyARN: cfg.PolicyARN, + VPCID: cfg.VPCID, + SubnetIDs: cloneStrings(cfg.SubnetIDs), + AvailabilityZones: cloneStrings(cfg.AvailabilityZones), + TransitGatewayID: cfg.TransitGatewayID, + Description: cfg.Description, + DeleteProtection: cfg.DeleteProtection, + SubnetChangeProtection: cfg.SubnetChangeProtection, + FirewallPolicyChangeProtection: cfg.FirewallPolicyChangeProtection, + AvailabilityZoneChangeProtection: cfg.AvailabilityZoneChangeProtection, + EnabledAnalysisTypes: cloneStrings(cfg.EnabledAnalysisTypes), + Status: statusReady, + UpdateToken: newToken(), + Tags: copyTags(cfg.Tags), + } + if !m.firewalls.SetIfAbsent(cfg.Name, fw) { + return nil, cerrors.Newf(cerrors.AlreadyExists, "firewall %q already exists", cfg.Name) + } + + return m.outFirewall(fw), nil +} + +func (m *Mock) validateCreateFirewall(cfg *nfdriver.CreateFirewallConfig) error { + if err := validateName("FirewallName", cfg.Name); err != nil { + return err + } + + if err := validateDescription(cfg.Description); err != nil { + return err + } + + if err := validateTags(cfg.Tags); err != nil { + return err + } + + if err := validateARN("FirewallPolicyArn", cfg.PolicyARN); err != nil { + return err + } + + if !m.policyExists(cfg.PolicyARN) { + return invalid("firewall policy %q not found", cfg.PolicyARN) + } + + if err := validateFirewallPlacement(cfg); err != nil { + return err + } + + for _, t := range cfg.EnabledAnalysisTypes { + if !oneOf(t, analysisTypes...) { + return invalid("EnabledAnalysisTypes value %q is not valid", t) + } + } + + if m.firewalls.Has(cfg.Name) { + return cerrors.Newf(cerrors.AlreadyExists, "firewall %q already exists", cfg.Name) + } + + return nil +} + +// validateFirewallPlacement checks where the firewall lives: a VPC (VpcId plus +// subnets) or a transit gateway (TransitGatewayId plus Availability Zones). +func validateFirewallPlacement(cfg *nfdriver.CreateFirewallConfig) error { + if cfg.TransitGatewayID != "" { + if cfg.VPCID != "" || len(cfg.SubnetIDs) > 0 { + return invalid("a transit gateway-attached firewall takes AvailabilityZoneMappings, not VpcId or SubnetMappings") + } + + if len(cfg.AvailabilityZones) == 0 { + return invalid("AvailabilityZoneMappings is required for a transit gateway-attached firewall") + } + + return nil + } + + if len(cfg.AvailabilityZones) > 0 { + return invalid("AvailabilityZoneMappings is only valid with TransitGatewayId") + } + + if cfg.VPCID == "" { + return invalid("VpcId is required") + } + + return validateVPCID(cfg.VPCID) +} + +func (m *Mock) DescribeFirewall(_ context.Context, name, arn string) (*nfdriver.Firewall, error) { + m.mu.RLock() + defer m.mu.RUnlock() + + if err := requireIdentifier("Firewall", name, arn); err != nil { + return nil, err + } + + fw, ok := m.lookupFirewall(name, arn) + if !ok { + return nil, cerrors.Newf(cerrors.NotFound, "firewall %q not found", nonEmpty(name, arn)) + } + + return m.outFirewall(fw), nil +} + +func (m *Mock) DeleteFirewall(_ context.Context, name, arn string) (*nfdriver.Firewall, error) { + m.mu.Lock() + defer m.mu.Unlock() + + if err := requireIdentifier("Firewall", name, arn); err != nil { + return nil, err + } + + fw, ok := m.lookupFirewall(name, arn) + if !ok { + return nil, cerrors.Newf(cerrors.NotFound, "firewall %q not found", nonEmpty(name, arn)) + } + + if fw.DeleteProtection { + return nil, nfdriver.InvalidOperation("firewall %q has delete protection enabled", fw.Name) + } + + if n := m.associationCount(fw.ARN); n > 0 { + return nil, nfdriver.InvalidOperation("firewall %q still has %d VPC endpoint association(s)", fw.Name, n) + } + + out := m.outFirewall(fw) + out.Status = statusDeleting + + m.firewalls.Delete(fw.Name) + m.dropFirewallState(fw) + + return out, nil +} + +// dropFirewallState removes everything keyed by a deleted firewall so no entry +// is orphaned: logging, resource policy, flow operations and analysis reports. +func (m *Mock) dropFirewallState(fw *nfdriver.Firewall) { + delete(m.logging, fw.Name) + delete(m.resourcePolicies, fw.ARN) + + for id, op := range m.flowOperations.All() { + if op.FirewallARN == fw.ARN { + m.flowOperations.Delete(id) + } + } + + for id, rep := range m.analysisReports.All() { + if rep.FirewallARN == fw.ARN { + m.analysisReports.Delete(id) + } + } +} + +func (m *Mock) ListFirewalls(_ context.Context, vpcIDs []string, opts nfdriver.ListOptions) ([]nfdriver.Firewall, string, error) { + m.mu.RLock() + defer m.mu.RUnlock() + + for _, v := range vpcIDs { + if err := validateVPCID(v); err != nil { + return nil, "", err + } + } + + var matched []*nfdriver.Firewall + + for _, f := range m.firewalls.SortedValues() { + if len(vpcIDs) == 0 || oneOf(f.VPCID, vpcIDs...) { + matched = append(matched, f) + } + } + + items, next, err := paginate(matched, opts) + if err != nil { + return nil, "", err + } + + out := make([]nfdriver.Firewall, 0, len(items)) + for _, f := range items { + out = append(out, *m.outFirewall(f)) + } + + return out, next, nil +} + +func (m *Mock) lookupFirewall(name, arn string) (*nfdriver.Firewall, bool) { + if name != "" { + return m.firewalls.Get(name) + } + + for _, f := range m.firewalls.SortedValues() { + if f.ARN == arn { + return f, true + } + } + + return nil, false +} + +// outFirewall returns a deep copy of the stored firewall with its computed +// fields filled in. +func (m *Mock) outFirewall(f *nfdriver.Firewall) *nfdriver.Firewall { + out := *f + out.SubnetIDs = cloneStrings(f.SubnetIDs) + out.AvailabilityZones = cloneStrings(f.AvailabilityZones) + out.EnabledAnalysisTypes = cloneStrings(f.EnabledAnalysisTypes) + out.Tags = copyTags(f.Tags) + out.NumberOfAssociations = m.associationCount(f.ARN) + + return &out +} + +// mutateFirewall runs one update on a firewall as a single critical section: +// existence, the optimistic-lock token (optional: "" is an unconditional +// update), the change itself, then the token rotation. +func (m *Mock) mutateFirewall(name, token string, change func(*nfdriver.Firewall) error) (*nfdriver.Firewall, error) { + m.mu.Lock() + defer m.mu.Unlock() + + fw, err := m.firewallForUpdate(name, token) + if err != nil { + return nil, err + } + + if err := change(fw); err != nil { + return nil, err + } + + fw.UpdateToken = newToken() + + return m.outFirewall(fw), nil +} + +func (m *Mock) firewallForUpdate(name, token string) (*nfdriver.Firewall, error) { + if err := validateName("FirewallName", name); err != nil { + return nil, err + } + + fw, ok := m.firewalls.Get(name) + if !ok { + return nil, cerrors.Newf(cerrors.NotFound, "firewall %q not found", name) + } + + if err := checkToken(fw.UpdateToken, token); err != nil { + return nil, err + } + + return fw, nil +} + +// AssociateFirewallPolicy attaches a firewall policy to a firewall. +func (m *Mock) AssociateFirewallPolicy(_ context.Context, firewallName, policyARN, updateToken string) (*nfdriver.Firewall, error) { + return m.mutateFirewall(firewallName, updateToken, func(fw *nfdriver.Firewall) error { + if fw.FirewallPolicyChangeProtection { + return nfdriver.InvalidOperation("firewall %q has firewall policy change protection enabled", fw.Name) + } + + if err := validateARN("FirewallPolicyArn", policyARN); err != nil { + return err + } + + if !m.policyExists(policyARN) { + return invalid("firewall policy %q not found", policyARN) + } + + fw.PolicyARN = policyARN + + return nil + }) +} + +// AssociateSubnets adds subnet mappings to a VPC-attached firewall. +func (m *Mock) AssociateSubnets( + _ context.Context, + firewallName string, + subnetIDs []string, + updateToken string, +) (*nfdriver.Firewall, error) { + return m.mutateFirewall(firewallName, updateToken, func(fw *nfdriver.Firewall) error { + if err := subnetOpAllowed(fw); err != nil { + return err + } + + if len(subnetIDs) == 0 { + return invalid("SubnetMappings is required") + } + + seen := make(map[string]bool, len(fw.SubnetIDs)) + for _, s := range fw.SubnetIDs { + seen[s] = true + } + + for _, s := range subnetIDs { + if !seen[s] { + fw.SubnetIDs = append(fw.SubnetIDs, s) + seen[s] = true + } + } + + return nil + }) +} + +// DisassociateSubnets removes subnet mappings from a VPC-attached firewall. +func (m *Mock) DisassociateSubnets( + _ context.Context, + firewallName string, + subnetIDs []string, + updateToken string, +) (*nfdriver.Firewall, error) { + return m.mutateFirewall(firewallName, updateToken, func(fw *nfdriver.Firewall) error { + if err := subnetOpAllowed(fw); err != nil { + return err + } + + if len(subnetIDs) == 0 { + return invalid("SubnetIds is required") + } + + remove := make(map[string]bool, len(subnetIDs)) + for _, s := range subnetIDs { + remove[s] = true + } + + kept := fw.SubnetIDs[:0:0] + + for _, s := range fw.SubnetIDs { + if !remove[s] { + kept = append(kept, s) + } + } + + fw.SubnetIDs = kept + + return nil + }) +} + +// subnetOpAllowed gates the subnet associations: a transit gateway-attached +// firewall has no subnets, and a protected firewall refuses changes. +func subnetOpAllowed(fw *nfdriver.Firewall) error { + if fw.TransitGatewayID != "" { + return nfdriver.InvalidOperation("firewall %q is transit gateway-attached; use the Availability Zone operations", fw.Name) + } + + if fw.SubnetChangeProtection { + return nfdriver.InvalidOperation("firewall %q has subnet change protection enabled", fw.Name) + } + + return nil +} + +// UpdateFirewallDeleteProtection toggles delete protection on a firewall. +func (m *Mock) UpdateFirewallDeleteProtection( + _ context.Context, + firewallName string, + enabled bool, + updateToken string, +) (*nfdriver.Firewall, error) { + return m.mutateFirewall(firewallName, updateToken, func(fw *nfdriver.Firewall) error { + fw.DeleteProtection = enabled + + return nil + }) +} + +// UpdateFirewallDescription sets (or, when empty, clears) the description. +func (m *Mock) UpdateFirewallDescription(_ context.Context, firewallName, description, updateToken string) (*nfdriver.Firewall, error) { + return m.mutateFirewall(firewallName, updateToken, func(fw *nfdriver.Firewall) error { + if err := validateDescription(description); err != nil { + return err + } + + fw.Description = description + + return nil + }) +} + +// UpdateSubnetChangeProtection toggles the subnet change protection flag. +func (m *Mock) UpdateSubnetChangeProtection( + _ context.Context, + firewallName string, + enabled bool, + updateToken string, +) (*nfdriver.Firewall, error) { + return m.mutateFirewall(firewallName, updateToken, func(fw *nfdriver.Firewall) error { + fw.SubnetChangeProtection = enabled + + return nil + }) +} + +// UpdateFirewallPolicyChangeProtection toggles the policy change protection flag. +func (m *Mock) UpdateFirewallPolicyChangeProtection( + _ context.Context, + firewallName string, + enabled bool, + updateToken string, +) (*nfdriver.Firewall, error) { + return m.mutateFirewall(firewallName, updateToken, func(fw *nfdriver.Firewall) error { + fw.FirewallPolicyChangeProtection = enabled + + return nil + }) +} + +// UpdateAvailabilityZoneChangeProtection toggles the AZ change protection flag. +func (m *Mock) UpdateAvailabilityZoneChangeProtection( + _ context.Context, + firewallName string, + enabled bool, + updateToken string, +) (*nfdriver.Firewall, error) { + return m.mutateFirewall(firewallName, updateToken, func(fw *nfdriver.Firewall) error { + fw.AvailabilityZoneChangeProtection = enabled + + return nil + }) +} + +// UpdateFirewallAnalysisSettings sets the analysis types enabled on a firewall +// (an empty list disables analysis). +func (m *Mock) UpdateFirewallAnalysisSettings( + _ context.Context, + firewallName string, + types []string, + updateToken string, +) (*nfdriver.Firewall, error) { + return m.mutateFirewall(firewallName, updateToken, func(fw *nfdriver.Firewall) error { + for _, t := range types { + if !oneOf(t, analysisTypes...) { + return invalid("EnabledAnalysisTypes value %q is not valid", t) + } + } + + fw.EnabledAnalysisTypes = cloneStrings(types) + + return nil + }) +} + +// AssociateAvailabilityZones adds Availability Zones to a transit +// gateway-attached firewall. +func (m *Mock) AssociateAvailabilityZones( + _ context.Context, + firewallName string, + zones []string, + updateToken string, +) (*nfdriver.Firewall, error) { + return m.mutateFirewall(firewallName, updateToken, func(fw *nfdriver.Firewall) error { + if err := zoneOpAllowed(fw, zones); err != nil { + return err + } + + seen := make(map[string]bool, len(fw.AvailabilityZones)) + for _, z := range fw.AvailabilityZones { + seen[z] = true + } + + for _, z := range zones { + if !seen[z] { + fw.AvailabilityZones = append(fw.AvailabilityZones, z) + seen[z] = true + } + } + + return nil + }) +} + +// DisassociateAvailabilityZones removes Availability Zones from a transit +// gateway-attached firewall. +func (m *Mock) DisassociateAvailabilityZones( + _ context.Context, + firewallName string, + zones []string, + updateToken string, +) (*nfdriver.Firewall, error) { + return m.mutateFirewall(firewallName, updateToken, func(fw *nfdriver.Firewall) error { + if err := zoneOpAllowed(fw, zones); err != nil { + return err + } + + remove := make(map[string]bool, len(zones)) + + for _, z := range zones { + if !oneOf(z, fw.AvailabilityZones...) { + return invalid("Availability Zone %q is not associated with firewall %q", z, fw.Name) + } + + remove[z] = true + } + + kept := fw.AvailabilityZones[:0:0] + + for _, z := range fw.AvailabilityZones { + if !remove[z] { + kept = append(kept, z) + } + } + + fw.AvailabilityZones = kept + + return nil + }) +} + +func zoneOpAllowed(fw *nfdriver.Firewall, zones []string) error { + if fw.TransitGatewayID == "" { + return nfdriver.InvalidOperation("firewall %q is VPC-attached; use the subnet operations", fw.Name) + } + + if fw.AvailabilityZoneChangeProtection { + return nfdriver.InvalidOperation("firewall %q has Availability Zone change protection enabled", fw.Name) + } + + if len(zones) == 0 { + return invalid("AvailabilityZoneMappings is required") + } + + for _, z := range zones { + if z == "" { + return invalid("AvailabilityZone must not be empty") + } + } + + return nil +} diff --git a/providers/aws/networkfirewall/flow_operation.go b/providers/aws/networkfirewall/flow_operation.go new file mode 100644 index 000000000..8a66b559d --- /dev/null +++ b/providers/aws/networkfirewall/flow_operation.go @@ -0,0 +1,222 @@ +package networkfirewall + +import ( + "context" + "sort" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/internal/idgen" + "github.com/stackshy/cloudemu/v2/internal/settle" + nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" +) + +const ( + flowCapture = "FLOW_CAPTURE" + flowFlush = "FLOW_FLUSH" + + flowCompleted = "COMPLETED" + flowInProgress = "IN_PROGRESS" +) + +// StartFlowCapture records a flow capture request on a firewall endpoint. +// +//nolint:gocritic // req matches the driver signature. +func (m *Mock) StartFlowCapture(_ context.Context, req nfdriver.FlowOperationRequest) (*nfdriver.FlowOperation, error) { + return m.startFlowOperation(flowCapture, &req) +} + +// StartFlowFlush records a flow flush request on a firewall endpoint. +// +//nolint:gocritic // req matches the driver signature. +func (m *Mock) StartFlowFlush(_ context.Context, req nfdriver.FlowOperationRequest) (*nfdriver.FlowOperation, error) { + return m.startFlowOperation(flowFlush, &req) +} + +func (m *Mock) startFlowOperation(opType string, req *nfdriver.FlowOperationRequest) (*nfdriver.FlowOperation, error) { + m.mu.Lock() + defer m.mu.Unlock() + + scope := nfdriver.FlowOperationScope{ + FirewallARN: req.FirewallARN, AvailabilityZone: req.AvailabilityZone, + VPCEndpointID: req.VPCEndpointID, VPCEndpointAssociationARN: req.VPCEndpointAssociationARN, + } + if err := m.validateFlowScope(scope); err != nil { + return nil, err + } + + if req.MinimumFlowAgeSeconds != nil && *req.MinimumFlowAgeSeconds < 0 { + return nil, invalid("MinimumFlowAgeInSeconds must not be negative") + } + + op := &nfdriver.FlowOperation{ + ID: idgen.UUID(), FirewallARN: req.FirewallARN, Type: opType, Status: flowCompleted, + AvailabilityZone: req.AvailabilityZone, VPCEndpointID: req.VPCEndpointID, + VPCEndpointAssociationARN: req.VPCEndpointAssociationARN, + Filters: cloneRawJSON(req.Filters), RequestedAt: m.now(), + } + + if req.MinimumFlowAgeSeconds != nil { + v := *req.MinimumFlowAgeSeconds + op.MinimumFlowAgeSeconds = &v + } + + m.flowOperations.Set(op.ID, op) + + return m.outFlowOperation(op), nil +} + +// validateFlowScope checks the firewall exists and that the endpoint selector +// (an Availability Zone of the firewall, or a VPC endpoint association of it) +// is valid. +func (m *Mock) validateFlowScope(s nfdriver.FlowOperationScope) error { + if err := validateARN("FirewallArn", s.FirewallARN); err != nil { + return err + } + + fw, ok := m.firewallByARN(s.FirewallARN) + if !ok { + return cerrors.Newf(cerrors.NotFound, "firewall %q not found", s.FirewallARN) + } + + if s.VPCEndpointAssociationARN != "" { + a, err := m.findAssociation(s.VPCEndpointAssociationARN) + if err != nil { + return err + } + + if a.FirewallARN != s.FirewallARN { + return invalid("VPC endpoint association %q does not belong to firewall %q", a.ARN, s.FirewallARN) + } + + return nil + } + + if s.AvailabilityZone == "" { + return invalid("either AvailabilityZone or VpcEndpointAssociationArn is required") + } + + if !oneOf(s.AvailabilityZone, nfdriver.SyncZones(fw, m.opts.Region)...) { + return invalid("firewall %q has no endpoint in Availability Zone %q", fw.Name, s.AvailabilityZone) + } + + return nil +} + +// DescribeFlowOperation returns one flow operation. +func (m *Mock) DescribeFlowOperation(_ context.Context, scope nfdriver.FlowOperationScope, id string) (*nfdriver.FlowOperation, error) { + m.mu.RLock() + defer m.mu.RUnlock() + + op, err := m.findFlowOperation(scope, id) + if err != nil { + return nil, err + } + + return m.outFlowOperation(op), nil +} + +// ListFlowOperations lists the flow operations of a firewall endpoint, optionally +// filtered by type, oldest first. +func (m *Mock) ListFlowOperations( + _ context.Context, scope nfdriver.FlowOperationScope, opType string, opts nfdriver.ListOptions, +) ([]nfdriver.FlowOperation, string, error) { + m.mu.RLock() + defer m.mu.RUnlock() + + if err := m.validateFlowScope(scope); err != nil { + return nil, "", err + } + + if opType != "" && !oneOf(opType, flowFlush, flowCapture) { + return nil, "", invalid("FlowOperationType %q is not valid", opType) + } + + var matched []*nfdriver.FlowOperation + + for _, op := range m.flowOperations.SortedValues() { + if flowInScope(op, scope) && (opType == "" || op.Type == opType) { + matched = append(matched, op) + } + } + + sort.SliceStable(matched, func(i, j int) bool { return matched[i].RequestedAt.Before(matched[j].RequestedAt) }) + + items, next, err := paginate(matched, opts) + if err != nil { + return nil, "", err + } + + out := make([]nfdriver.FlowOperation, 0, len(items)) + for _, op := range items { + out = append(out, *m.outFlowOperation(op)) + } + + return out, next, nil +} + +// ListFlowOperationResults returns the operation; its flows are always empty +// because the emulator has no data plane. The page arguments are validated so a +// caller sees the same errors as against AWS. +func (m *Mock) ListFlowOperationResults( + _ context.Context, scope nfdriver.FlowOperationScope, id string, opts nfdriver.ListOptions, +) (*nfdriver.FlowOperation, string, error) { + m.mu.RLock() + defer m.mu.RUnlock() + + op, err := m.findFlowOperation(scope, id) + if err != nil { + return nil, "", err + } + + if _, _, err := paginate([]struct{}{}, opts); err != nil { + return nil, "", err + } + + return m.outFlowOperation(op), "", nil +} + +func (m *Mock) findFlowOperation(scope nfdriver.FlowOperationScope, id string) (*nfdriver.FlowOperation, error) { + if err := m.validateFlowScope(scope); err != nil { + return nil, err + } + + if id == "" { + return nil, invalid("FlowOperationId is required") + } + + op, ok := m.flowOperations.Get(id) + if !ok || !flowInScope(op, scope) { + return nil, cerrors.Newf(cerrors.NotFound, "flow operation %q not found", id) + } + + return op, nil +} + +func flowInScope(op *nfdriver.FlowOperation, s nfdriver.FlowOperationScope) bool { + if op.FirewallARN != s.FirewallARN { + return false + } + + if s.VPCEndpointAssociationARN != "" { + return op.VPCEndpointAssociationARN == s.VPCEndpointAssociationARN + } + + return op.VPCEndpointAssociationARN == "" && op.AvailabilityZone == s.AvailabilityZone +} + +// outFlowOperation returns a copy whose status reflects --async-settle: the +// operation reports IN_PROGRESS until the settle window has elapsed. +func (m *Mock) outFlowOperation(op *nfdriver.FlowOperation) *nfdriver.FlowOperation { + out := *op + out.Filters = cloneRawJSON(op.Filters) + + if op.MinimumFlowAgeSeconds != nil { + v := *op.MinimumFlowAgeSeconds + out.MinimumFlowAgeSeconds = &v + } + + window := settle.Pending(flowInProgress, op.RequestedAt, m.opts.SettleDuration(settle.DefaultFlowOperationSettle)) + out.Status = window.Observe(m.now(), flowCompleted) + + return &out +} diff --git a/providers/aws/networkfirewall/logging.go b/providers/aws/networkfirewall/logging.go new file mode 100644 index 000000000..f4220a8c2 --- /dev/null +++ b/providers/aws/networkfirewall/logging.go @@ -0,0 +1,221 @@ +package networkfirewall + +import ( + "context" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" +) + +const ( + logDestS3 = "S3" + logDestCloudWatch = "CloudWatchLogs" + logDestFirehose = "KinesisDataFirehose" + maxLogDestConfigs = 3 + + // typeChangeCost counts a LogDestinationType change as the remove plus the add it is. + typeChangeCost = 2 +) + +// Enum values copied verbatim from the AWS SDK (LogType, LogDestinationType). +// +//nolint:gochecknoglobals // immutable enum value sets. +var ( + logTypes = []string{"ALERT", "FLOW", "TLS"} + logDestTypes = []string{logDestS3, logDestCloudWatch, logDestFirehose} + + // logDestKeys lists, per destination type, the required key and the + // optional keys of LogDestination. + logDestRequired = map[string]string{logDestS3: "bucketName", logDestCloudWatch: "logGroup", logDestFirehose: "deliveryStream"} + logDestOptional = map[string]string{logDestS3: "prefix"} +) + +// UpdateLoggingConfiguration sets the firewall's log destinations. As in AWS, +// a single call may add one destination, remove one, or change the destination +// of one entry; an omitted configuration disables logging. +func (m *Mock) UpdateLoggingConfiguration(_ context.Context, firewallName string, cfg *nfdriver.LoggingConfiguration) error { + m.mu.Lock() + defer m.mu.Unlock() + + if err := validateName("FirewallName", firewallName); err != nil { + return err + } + + if !m.firewalls.Has(firewallName) { + return cerrors.Newf(cerrors.NotFound, "firewall %q not found", firewallName) + } + + var next nfdriver.LoggingConfiguration + if cfg != nil { + next = *cfg + } + + if err := validateLogDestinations(next.Destinations); err != nil { + return err + } + + current := m.logging[firewallName] + if current == nil { + current = &nfdriver.LoggingConfiguration{} + } + + if err := checkSingleLogChange(current.Destinations, next.Destinations); err != nil { + return err + } + + dashboard := current.EnableMonitoringDashboard + if next.EnableMonitoringDashboard != nil { + dashboard = next.EnableMonitoringDashboard + } + + if len(next.Destinations) == 0 && dashboard == nil { + delete(m.logging, firewallName) + + return nil + } + + m.logging[firewallName] = &nfdriver.LoggingConfiguration{ + Destinations: cloneLogConfigs(next.Destinations), + EnableMonitoringDashboard: cloneBool(dashboard), + } + + return nil +} + +// DescribeLoggingConfiguration returns the firewall's logging configuration. +func (m *Mock) DescribeLoggingConfiguration(_ context.Context, firewallName string) (*nfdriver.LoggingConfiguration, error) { + m.mu.RLock() + defer m.mu.RUnlock() + + if err := validateName("FirewallName", firewallName); err != nil { + return nil, err + } + + if !m.firewalls.Has(firewallName) { + return nil, cerrors.Newf(cerrors.NotFound, "firewall %q not found", firewallName) + } + + cur := m.logging[firewallName] + if cur == nil { + return &nfdriver.LoggingConfiguration{}, nil + } + + return &nfdriver.LoggingConfiguration{ + Destinations: cloneLogConfigs(cur.Destinations), + EnableMonitoringDashboard: cloneBool(cur.EnableMonitoringDashboard), + }, nil +} + +// validateLogDestinations checks every entry against the SDK enums and the +// per-type destination keys, and enforces one entry per log type, max three. +func validateLogDestinations(cfgs []nfdriver.LogDestinationConfig) error { + if len(cfgs) > maxLogDestConfigs { + return invalid("at most %d log destination configs are allowed, got %d", maxLogDestConfigs, len(cfgs)) + } + + seen := make(map[string]bool, len(cfgs)) + + for _, c := range cfgs { + if !oneOf(c.LogType, logTypes...) { + return invalid("LogType %q is not valid (want one of %v)", c.LogType, logTypes) + } + + if seen[c.LogType] { + return invalid("more than one log destination config for LogType %s", c.LogType) + } + + seen[c.LogType] = true + + if !oneOf(c.LogDestinationType, logDestTypes...) { + return invalid("LogDestinationType %q is not valid (want one of %v)", c.LogDestinationType, logDestTypes) + } + + if err := validateLogDestinationKeys(c); err != nil { + return err + } + } + + return nil +} + +func validateLogDestinationKeys(c nfdriver.LogDestinationConfig) error { + required := logDestRequired[c.LogDestinationType] + if c.LogDestination[required] == "" { + return invalid("LogDestination for %s requires %q", c.LogDestinationType, required) + } + + for k := range c.LogDestination { + if k != required && k != logDestOptional[c.LogDestinationType] { + return invalid("LogDestination key %q is not valid for %s", k, c.LogDestinationType) + } + } + + return nil +} + +// checkSingleLogChange enforces AWS's "one change per call" rule: add one entry, +// remove one entry, or change the LogDestination of one entry. Changing the +// LogDestinationType (or LogType) of an entry is a remove plus an add, so it +// needs two calls. +func checkSingleLogChange(current, next []nfdriver.LogDestinationConfig) error { + have := make(map[string]nfdriver.LogDestinationConfig, len(current)) + for _, c := range current { + have[c.LogType] = c + } + + changes := 0 + + for _, n := range next { + c, ok := have[n.LogType] + + switch { + case !ok: + changes++ + case c.LogDestinationType != n.LogDestinationType: + changes += typeChangeCost + case !sameDestination(c.LogDestination, n.LogDestination): + changes++ + } + + delete(have, n.LogType) + } + + changes += len(have) // entries dropped from the configuration + + if changes > 1 { + return invalid("UpdateLoggingConfiguration accepts one change per call (add one destination, remove one, or change one destination)") + } + + return nil +} + +func sameDestination(a, b map[string]string) bool { + if len(a) != len(b) { + return false + } + + for k, v := range a { + if b[k] != v { + return false + } + } + + return true +} + +func cloneLogConfigs(cfgs []nfdriver.LogDestinationConfig) []nfdriver.LogDestinationConfig { + if len(cfgs) == 0 { + return nil + } + + out := make([]nfdriver.LogDestinationConfig, len(cfgs)) + for i, c := range cfgs { + out[i] = nfdriver.LogDestinationConfig{ + LogType: c.LogType, + LogDestinationType: c.LogDestinationType, + LogDestination: copyTags(c.LogDestination), + } + } + + return out +} diff --git a/providers/aws/networkfirewall/metadata.go b/providers/aws/networkfirewall/metadata.go new file mode 100644 index 000000000..499062af9 --- /dev/null +++ b/providers/aws/networkfirewall/metadata.go @@ -0,0 +1,123 @@ +package networkfirewall + +import ( + "context" + "encoding/json" + "regexp" + "strings" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" +) + +// Suricata rule options a summary reports. +var ( + ruleMsgRE = regexp.MustCompile(`msg:\s*"([^"]*)"\s*;`) + ruleSIDRE = regexp.MustCompile(`sid:\s*(\d+)\s*;`) + ruleMetadataRE = regexp.MustCompile(`metadata:\s*([^;]*);`) +) + +// DescribeFirewallMetadata returns the firewall's metadata, as visible to an +// account a firewall is shared with. +func (m *Mock) DescribeFirewallMetadata(_ context.Context, firewallARN string) (*nfdriver.FirewallMetadata, error) { + m.mu.RLock() + defer m.mu.RUnlock() + + if err := validateARN("FirewallArn", firewallARN); err != nil { + return nil, err + } + + fw, ok := m.firewallByARN(firewallARN) + if !ok { + return nil, cerrors.Newf(cerrors.NotFound, "firewall %q not found", firewallARN) + } + + return &nfdriver.FirewallMetadata{ + FirewallARN: fw.ARN, + PolicyARN: fw.PolicyARN, + Description: fw.Description, + Status: fw.Status, + AvailabilityZones: nfdriver.SyncZones(fw, m.opts.Region), + }, nil +} + +// DescribeRuleGroupMetadata returns the rule group (the wire layer reports its +// metadata subset). +func (m *Mock) DescribeRuleGroupMetadata(_ context.Context, name, arn, ruleType string) (*nfdriver.RuleGroup, error) { + m.mu.RLock() + defer m.mu.RUnlock() + + rg, err := m.findRuleGroup(name, arn, ruleType) + if err != nil { + return nil, err + } + + return m.outRuleGroup(rg), nil +} + +// DescribeRuleGroupSummary returns the rule group and the per-rule summaries +// parsed from its Suricata rules. Only the options listed in the rule group's +// SummaryConfiguration are populated. Only stateful rule groups carry rules text. +func (m *Mock) DescribeRuleGroupSummary( + _ context.Context, name, arn, ruleType string, +) (*nfdriver.RuleGroup, []nfdriver.RuleSummary, error) { + m.mu.RLock() + defer m.mu.RUnlock() + + rg, err := m.findRuleGroup(name, arn, ruleType) + if err != nil { + return nil, nil, err + } + + if rg.Type == ruleTypeStateless { + return nil, nil, invalid("rule group summaries are only available for stateful rule groups") + } + + return m.outRuleGroup(rg), ruleSummaries(rg), nil +} + +func ruleSummaries(rg *nfdriver.RuleGroup) []nfdriver.RuleSummary { + var src ruleSource + if err := json.Unmarshal(rg.Rules, &src); err != nil { + return nil + } + + want := make(map[string]bool, len(rg.SummaryRuleOptions)) + for _, o := range rg.SummaryRuleOptions { + want[o] = true + } + + lines := strings.Split(src.RulesSource.RulesString, "\n") + out := make([]nfdriver.RuleSummary, 0, len(lines)) + + for _, line := range lines { + if !isRuleLine(line) { + continue + } + + var s nfdriver.RuleSummary + if want["MSG"] { + s.Msg = firstGroup(ruleMsgRE, line) + } + + if want["SID"] { + s.SID = firstGroup(ruleSIDRE, line) + } + + if want["METADATA"] { + s.Metadata = strings.TrimSpace(firstGroup(ruleMetadataRE, line)) + } + + out = append(out, s) + } + + return out +} + +func firstGroup(re *regexp.Regexp, s string) string { + if g := re.FindStringSubmatch(s); len(g) > 1 { + return g[1] + } + + return "" +} diff --git a/providers/aws/networkfirewall/networkfirewall.go b/providers/aws/networkfirewall/networkfirewall.go index 63df8be63..281e31953 100644 --- a/providers/aws/networkfirewall/networkfirewall.go +++ b/providers/aws/networkfirewall/networkfirewall.go @@ -2,48 +2,75 @@ package networkfirewall import ( - "context" - "encoding/json" - "fmt" + "strings" "sync" + "time" "github.com/stackshy/cloudemu/v2/config" - cerrors "github.com/stackshy/cloudemu/v2/errors" "github.com/stackshy/cloudemu/v2/internal/idgen" "github.com/stackshy/cloudemu/v2/internal/memstore" nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" ) const ( - ruleTypeStateful = "STATEFUL" - ruleTypeStateless = "STATELESS" + ruleTypeStateful = "STATEFUL" + ruleTypeStateless = "STATELESS" + ruleTypeStatefulDomain = "STATEFUL_DOMAIN" + + statusReady = "READY" + statusDeleting = "DELETING" + statusActive = "ACTIVE" ) -var _ nfdriver.NetworkFirewall = (*Mock)(nil) +var ( + _ nfdriver.NetworkFirewall = (*Mock)(nil) + _ nfdriver.FirewallSettings = (*Mock)(nil) + _ nfdriver.AvailabilityZones = (*Mock)(nil) + _ nfdriver.ResourcePolicies = (*Mock)(nil) + _ nfdriver.TLSInspection = (*Mock)(nil) + _ nfdriver.VPCEndpointAssociations = (*Mock)(nil) + _ nfdriver.Metadata = (*Mock)(nil) + _ nfdriver.FlowOperations = (*Mock)(nil) + _ nfdriver.AnalysisReports = (*Mock)(nil) +) // Mock is the in-memory AWS Network Firewall implementation. Resources are -// keyed by name (unique per account/region, as in the real service). +// keyed by name (unique per account/region, as in the real service); VPC +// endpoint associations by ARN, flow operations and analysis reports by id. // // The stores hand back shared pointers whose fields are mutated in place -// (associate/tag/protection/subnets) and logging is a plain map, so every -// public method serializes on mu: mutators take Lock, readers take RLock. +// (associate/tag/protection/subnets) and logging/resource policies are plain +// maps, so every public method serializes on mu: mutators take Lock, readers +// take RLock, and everything handed out is a deep copy. That single lock also +// makes every create/delete (including its reference checks) atomic, so no +// concurrent pair can both pass a check. type Mock struct { - mu sync.RWMutex - firewalls *memstore.Store[*nfdriver.Firewall] - policies *memstore.Store[*nfdriver.FirewallPolicy] - ruleGroups *memstore.Store[*nfdriver.RuleGroup] - logging map[string][]nfdriver.LogDestinationConfig - opts *config.Options + mu sync.RWMutex + firewalls *memstore.Store[*nfdriver.Firewall] + policies *memstore.Store[*nfdriver.FirewallPolicy] + ruleGroups *memstore.Store[*nfdriver.RuleGroup] + tlsConfigs *memstore.Store[*nfdriver.TLSInspectionConfig] + associations *memstore.Store[*nfdriver.VPCEndpointAssociation] + flowOperations *memstore.Store[*nfdriver.FlowOperation] + analysisReports *memstore.Store[*nfdriver.AnalysisReport] + logging map[string]*nfdriver.LoggingConfiguration + resourcePolicies map[string]string + opts *config.Options } // New creates a new Network Firewall mock. func New(opts *config.Options) *Mock { return &Mock{ - firewalls: memstore.New[*nfdriver.Firewall](), - policies: memstore.New[*nfdriver.FirewallPolicy](), - ruleGroups: memstore.New[*nfdriver.RuleGroup](), - logging: map[string][]nfdriver.LogDestinationConfig{}, - opts: opts, + firewalls: memstore.New[*nfdriver.Firewall](), + policies: memstore.New[*nfdriver.FirewallPolicy](), + ruleGroups: memstore.New[*nfdriver.RuleGroup](), + tlsConfigs: memstore.New[*nfdriver.TLSInspectionConfig](), + associations: memstore.New[*nfdriver.VPCEndpointAssociation](), + flowOperations: memstore.New[*nfdriver.FlowOperation](), + analysisReports: memstore.New[*nfdriver.AnalysisReport](), + logging: map[string]*nfdriver.LoggingConfiguration{}, + resourcePolicies: map[string]string{}, + opts: opts, } } @@ -51,6 +78,24 @@ func (m *Mock) arn(kind, name string) string { return idgen.AWSARN("network-firewall", m.opts.Region, m.opts.AccountID, kind+"/"+name) } +func (m *Mock) now() time.Time { return m.opts.Clock.Now() } + +// newToken mints an update token (an RFC 4122 UUID, the shape AWS documents). +func newToken() string { return idgen.UUID() } + +// arnKind returns the resource segment of a Network Firewall ARN +// ("firewall", "stateful-rulegroup", ...), or "" for anything else. +func arnKind(arn string) string { + parts := strings.SplitN(arn, ":", 6) //nolint:mnd // an ARN has six colon-separated fields + if len(parts) < 6 || parts[2] != "network-firewall" { + return "" + } + + kind, _, _ := strings.Cut(parts[5], "/") + + return kind +} + func copyTags(src map[string]string) map[string]string { if len(src) == 0 { return nil @@ -72,416 +117,25 @@ func cloneStrings(s []string) []string { return append([]string(nil), s...) } -func cloneRawJSON(r json.RawMessage) json.RawMessage { +func cloneRawJSON(r []byte) []byte { if len(r) == 0 { return nil } - out := make(json.RawMessage, len(r)) + out := make([]byte, len(r)) copy(out, r) return out } -func cloneRuleGroupRefs(refs []nfdriver.RuleGroupReference) []nfdriver.RuleGroupReference { - if len(refs) == 0 { +func cloneBool(b *bool) *bool { + if b == nil { return nil } - return append([]nfdriver.RuleGroupReference(nil), refs...) -} - -// ---- Firewalls ---- - -//nolint:gocritic // cfg matches the driver signature. -func (m *Mock) CreateFirewall(_ context.Context, cfg nfdriver.CreateFirewallConfig) (*nfdriver.Firewall, error) { - m.mu.Lock() - defer m.mu.Unlock() - - if cfg.Name == "" { - return nil, cerrors.New(cerrors.InvalidArgument, "FirewallName is required") - } - - if m.firewalls.Has(cfg.Name) { - return nil, cerrors.Newf(cerrors.AlreadyExists, "firewall %q already exists", cfg.Name) - } - - if cfg.PolicyARN != "" && !m.policyExists(cfg.PolicyARN) { - return nil, cerrors.Newf(cerrors.InvalidArgument, "firewall policy %q not found", cfg.PolicyARN) - } - - fw := &nfdriver.Firewall{ - Name: cfg.Name, - ARN: m.arn("firewall", cfg.Name), - ID: idgen.GenerateID(""), - PolicyARN: cfg.PolicyARN, - VPCID: cfg.VPCID, - SubnetIDs: cloneStrings(cfg.SubnetIDs), - Description: cfg.Description, - DeleteProtection: cfg.DeleteProtection, - Status: "READY", - Tags: copyTags(cfg.Tags), - } - m.firewalls.Set(cfg.Name, fw) - - out := cloneFirewall(fw) - - return &out, nil -} - -func (m *Mock) DescribeFirewall(_ context.Context, name, arn string) (*nfdriver.Firewall, error) { - m.mu.RLock() - defer m.mu.RUnlock() - - fw, ok := m.lookupFirewall(name, arn) - if !ok { - return nil, cerrors.Newf(cerrors.NotFound, "firewall %q not found", nonEmpty(name, arn)) - } - - out := cloneFirewall(fw) - - return &out, nil -} - -func (m *Mock) DeleteFirewall(_ context.Context, name, arn string) (*nfdriver.Firewall, error) { - m.mu.Lock() - defer m.mu.Unlock() - - fw, ok := m.lookupFirewall(name, arn) - if !ok { - return nil, cerrors.Newf(cerrors.NotFound, "firewall %q not found", nonEmpty(name, arn)) - } - - if fw.DeleteProtection { - return nil, cerrors.Newf(cerrors.FailedPrecondition, "firewall %q has delete protection enabled", fw.Name) - } - - fw.Status = "DELETING" - m.firewalls.Delete(fw.Name) - - out := cloneFirewall(fw) - - return &out, nil -} - -func (m *Mock) ListFirewalls(_ context.Context) ([]nfdriver.Firewall, error) { - m.mu.RLock() - defer m.mu.RUnlock() - - all := m.firewalls.SortedValues() - out := make([]nfdriver.Firewall, 0, len(all)) - - for _, f := range all { - out = append(out, cloneFirewall(f)) - } - - return out, nil -} - -func (m *Mock) lookupFirewall(name, arn string) (*nfdriver.Firewall, bool) { - if name != "" { - return m.firewalls.Get(name) - } - - for _, f := range m.firewalls.SortedValues() { - if f.ARN == arn { - return f, true - } - } - - return nil, false -} - -func cloneFirewall(f *nfdriver.Firewall) nfdriver.Firewall { - out := *f - out.SubnetIDs = cloneStrings(f.SubnetIDs) - out.Tags = copyTags(f.Tags) - - return out -} - -// ---- Firewall Policies ---- - -//nolint:gocritic // cfg matches the driver signature. -func (m *Mock) CreateFirewallPolicy(_ context.Context, cfg nfdriver.CreateFirewallPolicyConfig) (*nfdriver.FirewallPolicy, error) { - m.mu.Lock() - defer m.mu.Unlock() - - if cfg.Name == "" { - return nil, cerrors.New(cerrors.InvalidArgument, "FirewallPolicyName is required") - } - - if m.policies.Has(cfg.Name) { - return nil, cerrors.Newf(cerrors.AlreadyExists, "firewall policy %q already exists", cfg.Name) - } - - p := &nfdriver.FirewallPolicy{ - Name: cfg.Name, - ARN: m.arn("firewall-policy", cfg.Name), - ID: idgen.GenerateID(""), - Description: cfg.Description, - StatelessDefaultActions: cloneStrings(cfg.StatelessDefaultActions), - StatelessFragmentDefaultActions: cloneStrings(cfg.StatelessFragmentDefaultActions), - StatefulRuleGroupReferences: cloneRuleGroupRefs(cfg.StatefulRuleGroupReferences), - StatelessRuleGroupReferences: cloneRuleGroupRefs(cfg.StatelessRuleGroupReferences), - Tags: copyTags(cfg.Tags), - } - m.policies.Set(cfg.Name, p) - - out := cloneFirewallPolicy(p) + v := *b - return &out, nil -} - -func (m *Mock) DescribeFirewallPolicy(_ context.Context, name, arn string) (*nfdriver.FirewallPolicy, error) { - m.mu.RLock() - defer m.mu.RUnlock() - - p, ok := m.lookupPolicy(name, arn) - if !ok { - return nil, cerrors.Newf(cerrors.NotFound, "firewall policy %q not found", nonEmpty(name, arn)) - } - - out := cloneFirewallPolicy(p) - - return &out, nil -} - -//nolint:gocritic // cfg matches the driver signature. -func (m *Mock) UpdateFirewallPolicy( - _ context.Context, name, arn string, cfg nfdriver.UpdateFirewallPolicyConfig, -) (*nfdriver.FirewallPolicy, error) { - m.mu.Lock() - defer m.mu.Unlock() - - p, ok := m.lookupPolicy(name, arn) - if !ok { - return nil, cerrors.Newf(cerrors.NotFound, "firewall policy %q not found", nonEmpty(name, arn)) - } - - p.Description = cfg.Description - p.StatelessDefaultActions = cloneStrings(cfg.StatelessDefaultActions) - p.StatelessFragmentDefaultActions = cloneStrings(cfg.StatelessFragmentDefaultActions) - p.StatefulRuleGroupReferences = cloneRuleGroupRefs(cfg.StatefulRuleGroupReferences) - p.StatelessRuleGroupReferences = cloneRuleGroupRefs(cfg.StatelessRuleGroupReferences) - - out := cloneFirewallPolicy(p) - - return &out, nil -} - -func (m *Mock) DeleteFirewallPolicy(_ context.Context, name, arn string) (*nfdriver.FirewallPolicy, error) { - m.mu.Lock() - defer m.mu.Unlock() - - p, ok := m.lookupPolicy(name, arn) - if !ok { - return nil, cerrors.Newf(cerrors.NotFound, "firewall policy %q not found", nonEmpty(name, arn)) - } - - if m.policyInUse(p.ARN) { - return nil, cerrors.Newf(cerrors.FailedPrecondition, "firewall policy %q is in use by a firewall", p.Name) - } - - m.policies.Delete(p.Name) - - out := cloneFirewallPolicy(p) - - return &out, nil -} - -func (m *Mock) ListFirewallPolicies(_ context.Context) ([]nfdriver.FirewallPolicy, error) { - m.mu.RLock() - defer m.mu.RUnlock() - - all := m.policies.SortedValues() - out := make([]nfdriver.FirewallPolicy, 0, len(all)) - - for _, p := range all { - out = append(out, cloneFirewallPolicy(p)) - } - - return out, nil -} - -func (m *Mock) lookupPolicy(name, arn string) (*nfdriver.FirewallPolicy, bool) { - if name != "" { - return m.policies.Get(name) - } - - for _, p := range m.policies.SortedValues() { - if p.ARN == arn { - return p, true - } - } - - return nil, false -} - -// policyExists reports whether a firewall policy with the given ARN is present. -func (m *Mock) policyExists(arn string) bool { - for _, p := range m.policies.SortedValues() { - if p.ARN == arn { - return true - } - } - - return false -} - -// policyInUse reports whether any firewall references the policy ARN. -func (m *Mock) policyInUse(arn string) bool { - for _, f := range m.firewalls.SortedValues() { - if f.PolicyARN == arn { - return true - } - } - - return false -} - -func cloneFirewallPolicy(p *nfdriver.FirewallPolicy) nfdriver.FirewallPolicy { - out := *p - out.StatelessDefaultActions = cloneStrings(p.StatelessDefaultActions) - out.StatelessFragmentDefaultActions = cloneStrings(p.StatelessFragmentDefaultActions) - out.StatefulRuleGroupReferences = cloneRuleGroupRefs(p.StatefulRuleGroupReferences) - out.StatelessRuleGroupReferences = cloneRuleGroupRefs(p.StatelessRuleGroupReferences) - out.Tags = copyTags(p.Tags) - - return out -} - -// ---- Rule Groups ---- - -func (m *Mock) CreateRuleGroup(_ context.Context, cfg nfdriver.CreateRuleGroupConfig) (*nfdriver.RuleGroup, error) { - m.mu.Lock() - defer m.mu.Unlock() - - if cfg.Name == "" { - return nil, cerrors.New(cerrors.InvalidArgument, "RuleGroupName is required") - } - - if cfg.Type != ruleTypeStateful && cfg.Type != ruleTypeStateless { - return nil, cerrors.New(cerrors.InvalidArgument, "Type must be STATEFUL or STATELESS") - } - - key := ruleGroupKey(cfg.Name, cfg.Type) - if m.ruleGroups.Has(key) { - return nil, cerrors.Newf(cerrors.AlreadyExists, "rule group %q already exists", cfg.Name) - } - - rgKind := "stateful-rulegroup" - if cfg.Type == ruleTypeStateless { - rgKind = "stateless-rulegroup" - } - - rg := &nfdriver.RuleGroup{ - Name: cfg.Name, - ARN: m.arn(rgKind, cfg.Name), - ID: idgen.GenerateID(""), - Type: cfg.Type, - Capacity: cfg.Capacity, - Description: cfg.Description, - Rules: cloneRawJSON(cfg.Rules), - Tags: copyTags(cfg.Tags), - } - m.ruleGroups.Set(key, rg) - - out := cloneRuleGroup(rg) - - return &out, nil -} - -func (m *Mock) DescribeRuleGroup(_ context.Context, name, arn, ruleType string) (*nfdriver.RuleGroup, error) { - m.mu.RLock() - defer m.mu.RUnlock() - - rg, ok := m.lookupRuleGroup(name, arn, ruleType) - if !ok { - return nil, cerrors.Newf(cerrors.NotFound, "rule group %q not found", nonEmpty(name, arn)) - } - - out := cloneRuleGroup(rg) - - return &out, nil -} - -//nolint:gocritic // cfg matches the driver signature. -func (m *Mock) UpdateRuleGroup( - _ context.Context, name, arn, ruleType string, cfg nfdriver.UpdateRuleGroupConfig, -) (*nfdriver.RuleGroup, error) { - m.mu.Lock() - defer m.mu.Unlock() - - rg, ok := m.lookupRuleGroup(name, arn, ruleType) - if !ok { - return nil, cerrors.Newf(cerrors.NotFound, "rule group %q not found", nonEmpty(name, arn)) - } - - rg.Description = cfg.Description - if len(cfg.Rules) > 0 { - rg.Rules = cloneRawJSON(cfg.Rules) - } - - out := cloneRuleGroup(rg) - - return &out, nil -} - -func (m *Mock) DeleteRuleGroup(_ context.Context, name, arn, ruleType string) (*nfdriver.RuleGroup, error) { - m.mu.Lock() - defer m.mu.Unlock() - - rg, ok := m.lookupRuleGroup(name, arn, ruleType) - if !ok { - return nil, cerrors.Newf(cerrors.NotFound, "rule group %q not found", nonEmpty(name, arn)) - } - - m.ruleGroups.Delete(ruleGroupKey(rg.Name, rg.Type)) - - out := cloneRuleGroup(rg) - - return &out, nil -} - -func (m *Mock) ListRuleGroups(_ context.Context) ([]nfdriver.RuleGroup, error) { - m.mu.RLock() - defer m.mu.RUnlock() - - all := m.ruleGroups.SortedValues() - out := make([]nfdriver.RuleGroup, 0, len(all)) - - for _, rg := range all { - out = append(out, cloneRuleGroup(rg)) - } - - return out, nil -} - -func (m *Mock) lookupRuleGroup(name, arn, ruleType string) (*nfdriver.RuleGroup, bool) { - if name != "" && ruleType != "" { - return m.ruleGroups.Get(ruleGroupKey(name, ruleType)) - } - - for _, rg := range m.ruleGroups.SortedValues() { - if (name != "" && rg.Name == name) || (arn != "" && rg.ARN == arn) { - return rg, true - } - } - - return nil, false -} - -func ruleGroupKey(name, ruleType string) string { - return fmt.Sprintf("%s/%s", ruleType, name) -} - -func cloneRuleGroup(rg *nfdriver.RuleGroup) nfdriver.RuleGroup { - out := *rg - out.Rules = cloneRawJSON(rg.Rules) - out.Tags = copyTags(rg.Tags) - - return out + return &v } func nonEmpty(a, b string) string { @@ -491,226 +145,3 @@ func nonEmpty(a, b string) string { return b } - -// ---- Firewall depth: associations, protection, logging, tags ---- - -// AssociateFirewallPolicy attaches a firewall policy to a firewall. -func (m *Mock) AssociateFirewallPolicy(_ context.Context, firewallName, policyARN string) (*nfdriver.Firewall, error) { - m.mu.Lock() - defer m.mu.Unlock() - - fw, ok := m.firewalls.Get(firewallName) - if !ok { - return nil, cerrors.Newf(cerrors.NotFound, "firewall %q not found", firewallName) - } - - if !m.policyExists(policyARN) { - return nil, cerrors.Newf(cerrors.InvalidArgument, "firewall policy %q not found", policyARN) - } - - fw.PolicyARN = policyARN - - out := cloneFirewall(fw) - - return &out, nil -} - -// AssociateSubnets adds subnet mappings to a firewall. -func (m *Mock) AssociateSubnets(_ context.Context, firewallName string, subnetIDs []string) (*nfdriver.Firewall, error) { - m.mu.Lock() - defer m.mu.Unlock() - - fw, ok := m.firewalls.Get(firewallName) - if !ok { - return nil, cerrors.Newf(cerrors.NotFound, "firewall %q not found", firewallName) - } - - seen := make(map[string]bool, len(fw.SubnetIDs)) - for _, s := range fw.SubnetIDs { - seen[s] = true - } - - for _, s := range subnetIDs { - if !seen[s] { - fw.SubnetIDs = append(fw.SubnetIDs, s) - seen[s] = true - } - } - - out := cloneFirewall(fw) - - return &out, nil -} - -// DisassociateSubnets removes subnet mappings from a firewall. -func (m *Mock) DisassociateSubnets(_ context.Context, firewallName string, subnetIDs []string) (*nfdriver.Firewall, error) { - m.mu.Lock() - defer m.mu.Unlock() - - fw, ok := m.firewalls.Get(firewallName) - if !ok { - return nil, cerrors.Newf(cerrors.NotFound, "firewall %q not found", firewallName) - } - - remove := make(map[string]bool, len(subnetIDs)) - for _, s := range subnetIDs { - remove[s] = true - } - - kept := fw.SubnetIDs[:0:0] - - for _, s := range fw.SubnetIDs { - if !remove[s] { - kept = append(kept, s) - } - } - - fw.SubnetIDs = kept - - out := cloneFirewall(fw) - - return &out, nil -} - -// UpdateFirewallDeleteProtection toggles delete protection on a firewall. -func (m *Mock) UpdateFirewallDeleteProtection(_ context.Context, firewallName string, enabled bool) (*nfdriver.Firewall, error) { - m.mu.Lock() - defer m.mu.Unlock() - - fw, ok := m.firewalls.Get(firewallName) - if !ok { - return nil, cerrors.Newf(cerrors.NotFound, "firewall %q not found", firewallName) - } - - fw.DeleteProtection = enabled - - out := cloneFirewall(fw) - - return &out, nil -} - -// UpdateLoggingConfiguration sets the firewall's log destination configs. -func (m *Mock) UpdateLoggingConfiguration(_ context.Context, firewallName string, configs []nfdriver.LogDestinationConfig) error { - m.mu.Lock() - defer m.mu.Unlock() - - if !m.firewalls.Has(firewallName) { - return cerrors.Newf(cerrors.NotFound, "firewall %q not found", firewallName) - } - - m.logging[firewallName] = cloneLogConfigs(configs) - - return nil -} - -// DescribeLoggingConfiguration returns the firewall's log destination configs. -func (m *Mock) DescribeLoggingConfiguration(_ context.Context, firewallName string) ([]nfdriver.LogDestinationConfig, error) { - m.mu.RLock() - defer m.mu.RUnlock() - - if !m.firewalls.Has(firewallName) { - return nil, cerrors.Newf(cerrors.NotFound, "firewall %q not found", firewallName) - } - - return cloneLogConfigs(m.logging[firewallName]), nil -} - -func cloneLogConfigs(cfgs []nfdriver.LogDestinationConfig) []nfdriver.LogDestinationConfig { - if len(cfgs) == 0 { - return nil - } - - out := make([]nfdriver.LogDestinationConfig, len(cfgs)) - for i, c := range cfgs { - out[i] = nfdriver.LogDestinationConfig{ - LogType: c.LogType, - LogDestinationType: c.LogDestinationType, - LogDestination: copyTags(c.LogDestination), - } - } - - return out -} - -// TagResource adds tags to a firewall / policy / rule group by ARN. -func (m *Mock) TagResource(_ context.Context, arn string, tags map[string]string) error { - m.mu.Lock() - defer m.mu.Unlock() - - if t := m.taggableByARN(arn); t != nil { - for k, v := range tags { - t[k] = v - } - - return nil - } - - return cerrors.Newf(cerrors.NotFound, "resource %q not found", arn) -} - -// UntagResource removes tags from a firewall / policy / rule group by ARN. -func (m *Mock) UntagResource(_ context.Context, arn string, keys []string) error { - m.mu.Lock() - defer m.mu.Unlock() - - t := m.taggableByARN(arn) - if t == nil { - return cerrors.Newf(cerrors.NotFound, "resource %q not found", arn) - } - - for _, k := range keys { - delete(t, k) - } - - return nil -} - -// ListTagsForResource returns a copy of the tags on the resource with the given -// ARN (firewall, policy, or rule group). -func (m *Mock) ListTagsForResource(_ context.Context, arn string) (map[string]string, error) { - m.mu.Lock() - defer m.mu.Unlock() - - t := m.taggableByARN(arn) - if t == nil { - return nil, cerrors.Newf(cerrors.NotFound, "resource %q not found", arn) - } - - return copyTags(t), nil -} - -// taggableByARN returns the mutable Tags map of the resource with the given ARN, -// creating it if nil, or nil when no resource matches. -func (m *Mock) taggableByARN(arn string) map[string]string { - for _, f := range m.firewalls.SortedValues() { - if f.ARN == arn { - if f.Tags == nil { - f.Tags = map[string]string{} - } - - return f.Tags - } - } - - for _, p := range m.policies.SortedValues() { - if p.ARN == arn { - if p.Tags == nil { - p.Tags = map[string]string{} - } - - return p.Tags - } - } - - for _, rg := range m.ruleGroups.SortedValues() { - if rg.ARN == arn { - if rg.Tags == nil { - rg.Tags = map[string]string{} - } - - return rg.Tags - } - } - - return nil -} diff --git a/providers/aws/networkfirewall/networkfirewall_test.go b/providers/aws/networkfirewall/networkfirewall_test.go index f9d41a43b..821d9d542 100644 --- a/providers/aws/networkfirewall/networkfirewall_test.go +++ b/providers/aws/networkfirewall/networkfirewall_test.go @@ -2,8 +2,13 @@ package networkfirewall import ( "context" + "encoding/json" "sync" "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "github.com/stackshy/cloudemu/v2/config" cerrors "github.com/stackshy/cloudemu/v2/errors" @@ -12,174 +17,205 @@ import ( func newMock() *Mock { return New(config.NewOptions()) } -// logConfigs builds LogDestinationConfig entries carrying only LogType, for -// tests that don't care about the destination fields. -func logConfigs(logTypes ...string) []nfdriver.LogDestinationConfig { - out := make([]nfdriver.LogDestinationConfig, 0, len(logTypes)) - for _, lt := range logTypes { - out = append(out, nfdriver.LogDestinationConfig{LogType: lt}) +// newClockMock returns a mock on a FakeClock so time-dependent output is exact. +func newClockMock(t *testing.T) (*Mock, *config.FakeClock) { + t.Helper() + + clock := config.NewFakeClock(time.Date(2026, 1, 2, 3, 4, 5, 0, time.UTC)) + + return New(config.NewOptions(config.WithClock(clock))), clock +} + +var ( + bg = context.Background() + + standardActions = []string{"aws:forward_to_sfe"} +) + +func seedPolicy(t *testing.T, m *Mock, name string) *nfdriver.FirewallPolicy { + t.Helper() + + p, err := m.CreateFirewallPolicy(bg, nfdriver.CreateFirewallPolicyConfig{ + Name: name, StatelessDefaultActions: standardActions, StatelessFragmentDefaultActions: standardActions, + }) + require.NoError(t, err) + + return p +} + +func seedFirewall(t *testing.T, m *Mock, name, policyARN string) *nfdriver.Firewall { + t.Helper() + + fw, err := m.CreateFirewall(bg, nfdriver.CreateFirewallConfig{ + Name: name, PolicyARN: policyARN, VPCID: "vpc-1a2b", SubnetIDs: []string{"subnet-1"}, + }) + require.NoError(t, err) + + return fw +} + +func seedRuleGroup(t *testing.T, m *Mock, name, ruleType string, capacity int) *nfdriver.RuleGroup { + t.Helper() + + rg, err := m.CreateRuleGroup(bg, nfdriver.CreateRuleGroupConfig{Name: name, Type: ruleType, Capacity: capacity}) + require.NoError(t, err) + + return rg +} + +// seedStack creates a policy and a firewall using it. +func seedStack(t *testing.T, m *Mock) (*nfdriver.FirewallPolicy, *nfdriver.Firewall) { + t.Helper() + + p := seedPolicy(t, m, "pol-1") + + return p, seedFirewall(t, m, "fw-1", p.ARN) +} + +// requireException asserts err carries the given Network Firewall exception. +func requireException(t *testing.T, err error, want string) { + t.Helper() + + got, ok := nfdriver.ExceptionOf(err) + require.True(t, ok, "expected typed exception %s, got %v", want, err) + assert.Equal(t, want, got) +} + +func logDest(logType, destType string, dest map[string]string) nfdriver.LogDestinationConfig { + return nfdriver.LogDestinationConfig{LogType: logType, LogDestinationType: destType, LogDestination: dest} +} + +func s3Dest(logType, bucket string) nfdriver.LogDestinationConfig { + return logDest(logType, "S3", map[string]string{"bucketName": bucket}) +} + +func rulesString(lines ...string) json.RawMessage { + body := "" + for _, l := range lines { + body += l + "\n" } - return out + b, _ := json.Marshal(map[string]any{"RulesSource": map[string]any{"RulesString": body}}) + + return b } func TestFirewallLifecycle(t *testing.T) { m := newMock() - ctx := context.Background() + pol := seedPolicy(t, m, "pol-1") - if _, err := m.CreateFirewall(ctx, nfdriver.CreateFirewallConfig{}); !cerrors.IsInvalidArgument(err) { - t.Fatalf("firewall without name: got %v, want InvalidArgument", err) - } + _, err := m.CreateFirewall(bg, nfdriver.CreateFirewallConfig{}) + assert.True(t, cerrors.IsInvalidArgument(err), "firewall without name: %v", err) - fw, err := m.CreateFirewall(ctx, nfdriver.CreateFirewallConfig{ - Name: "fw-1", VPCID: "vpc-1", SubnetIDs: []string{"subnet-1"}, DeleteProtection: true, + fw, err := m.CreateFirewall(bg, nfdriver.CreateFirewallConfig{ + Name: "fw-1", PolicyARN: pol.ARN, VPCID: "vpc-1a", SubnetIDs: []string{"subnet-1"}, DeleteProtection: true, }) - if err != nil || fw.ARN == "" || fw.Status != "READY" { - t.Fatalf("CreateFirewall: %v %+v", err, fw) - } + require.NoError(t, err) + assert.NotEmpty(t, fw.ARN) + assert.Equal(t, "READY", fw.Status) - if _, err := m.CreateFirewall(ctx, nfdriver.CreateFirewallConfig{Name: "fw-1"}); !cerrors.IsAlreadyExists(err) { - t.Fatalf("duplicate firewall: got %v, want AlreadyExists", err) - } + _, err = m.CreateFirewall(bg, nfdriver.CreateFirewallConfig{Name: "fw-1", PolicyARN: pol.ARN, VPCID: "vpc-1a"}) + assert.True(t, cerrors.IsAlreadyExists(err), "duplicate firewall: %v", err) - // Delete protection blocks deletion. - if _, err := m.DeleteFirewall(ctx, "fw-1", ""); !cerrors.IsFailedPrecondition(err) { - t.Fatalf("delete protected firewall: got %v, want FailedPrecondition", err) - } + _, err = m.DeleteFirewall(bg, "fw-1", "") + requireException(t, err, nfdriver.ExInvalidOperation) - // Lookup by ARN works too. - got, err := m.DescribeFirewall(ctx, "", fw.ARN) - if err != nil || got.Name != "fw-1" { - t.Fatalf("DescribeFirewall by ARN: %v %+v", err, got) - } + got, err := m.DescribeFirewall(bg, "", fw.ARN) + require.NoError(t, err) + assert.Equal(t, "fw-1", got.Name) - list, _ := m.ListFirewalls(ctx) - if len(list) != 1 { - t.Fatalf("ListFirewalls: %+v", list) - } + list, next, err := m.ListFirewalls(bg, nil, nfdriver.ListOptions{}) + require.NoError(t, err) + assert.Len(t, list, 1) + assert.Empty(t, next) } func TestFirewallPolicyAndRuleGroup(t *testing.T) { m := newMock() - ctx := context.Background() - pol, err := m.CreateFirewallPolicy(ctx, nfdriver.CreateFirewallPolicyConfig{ - Name: "pol-1", StatelessDefaultActions: []string{"aws:forward_to_sfe"}, - }) - if err != nil || pol.ID == "" { - t.Fatalf("CreateFirewallPolicy: %v %+v", err, pol) - } + pol := seedPolicy(t, m, "pol-1") + assert.NotEmpty(t, pol.ID) - if _, err := m.DescribeFirewallPolicy(ctx, "pol-1", ""); err != nil { - t.Fatalf("DescribeFirewallPolicy: %v", err) - } + _, err := m.DescribeFirewallPolicy(bg, "pol-1", "") + require.NoError(t, err) - if _, err := m.DeleteFirewallPolicy(ctx, "pol-1", ""); err != nil { - t.Fatalf("DeleteFirewallPolicy: %v", err) - } + _, err = m.DeleteFirewallPolicy(bg, "pol-1", "") + require.NoError(t, err) - // Rule group: type is validated. - if _, err := m.CreateRuleGroup(ctx, nfdriver.CreateRuleGroupConfig{Name: "rg", Type: "BOGUS"}); !cerrors.IsInvalidArgument(err) { - t.Fatalf("bad rule group type: got %v, want InvalidArgument", err) - } + _, err = m.CreateRuleGroup(bg, nfdriver.CreateRuleGroupConfig{Name: "rg", Type: "BOGUS", Capacity: 1}) + assert.True(t, cerrors.IsInvalidArgument(err), "bad rule group type: %v", err) - rg, err := m.CreateRuleGroup(ctx, nfdriver.CreateRuleGroupConfig{Name: "rg-1", Type: "STATEFUL", Capacity: 100}) - if err != nil { - t.Fatalf("CreateRuleGroup: %v", err) - } + rg := seedRuleGroup(t, m, "rg-1", "STATEFUL", 100) - got, err := m.DescribeRuleGroup(ctx, "rg-1", "", "STATEFUL") - if err != nil || got.Capacity != 100 { - t.Fatalf("DescribeRuleGroup: %v %+v", err, got) - } + got, err := m.DescribeRuleGroup(bg, "rg-1", "", "STATEFUL") + require.NoError(t, err) + assert.Equal(t, 100, got.Capacity) - if _, err := m.DeleteRuleGroup(ctx, rg.Name, "", "STATEFUL"); err != nil { - t.Fatalf("DeleteRuleGroup: %v", err) - } + _, err = m.DeleteRuleGroup(bg, rg.Name, "", "STATEFUL") + require.NoError(t, err) - if _, err := m.DescribeRuleGroup(ctx, "rg-1", "", "STATEFUL"); !cerrors.IsNotFound(err) { - t.Fatalf("describe deleted rule group: got %v, want NotFound", err) - } + _, err = m.DescribeRuleGroup(bg, "rg-1", "", "STATEFUL") + assert.True(t, cerrors.IsNotFound(err), "describe deleted rule group: %v", err) } func TestFirewallDepth(t *testing.T) { m := newMock() - ctx := context.Background() + pol, fw := seedStack(t, m) + other := seedPolicy(t, m, "pol-2") - pol, err := m.CreateFirewallPolicy(ctx, nfdriver.CreateFirewallPolicyConfig{Name: "pol-dep"}) - if err != nil { - t.Fatalf("CreateFirewallPolicy: %v", err) - } + _, err := m.AssociateFirewallPolicy(bg, "fw-1", "arn:aws:network-firewall:us-east-1:000000000000:firewall-policy/nope", "") + assert.True(t, cerrors.IsInvalidArgument(err), "associate missing policy: %v", err) - fw, err := m.CreateFirewall(ctx, nfdriver.CreateFirewallConfig{Name: "fw-1", SubnetIDs: []string{"subnet-1"}}) - if err != nil { - t.Fatalf("CreateFirewall: %v", err) - } + _, err = m.AssociateFirewallPolicy(bg, "fw-1", other.ARN, "") + require.NoError(t, err) - // Associating a nonexistent policy is rejected. - if _, err := m.AssociateFirewallPolicy(ctx, "fw-1", "arn:nope"); !cerrors.IsInvalidArgument(err) { - t.Fatalf("associate missing policy: got %v, want InvalidArgument", err) - } + // pol-1 is free again; pol-2 is now in use. + _, err = m.DeleteFirewallPolicy(bg, "pol-2", "") + requireException(t, err, nfdriver.ExInvalidOperation) - if _, err := m.AssociateFirewallPolicy(ctx, "fw-1", pol.ARN); err != nil { - t.Fatalf("AssociateFirewallPolicy: %v", err) - } + _, err = m.DeleteFirewallPolicy(bg, "pol-1", "") + require.NoError(t, err) - // The policy is now in use, so deleting it is blocked. - if _, err := m.DeleteFirewallPolicy(ctx, "pol-dep", ""); !cerrors.IsFailedPrecondition(err) { - t.Fatalf("delete in-use policy: got %v, want FailedPrecondition", err) - } + _ = pol - assoc, err := m.AssociateSubnets(ctx, "fw-1", []string{"subnet-1", "subnet-2"}) - if err != nil || len(assoc.SubnetIDs) != 2 { - t.Fatalf("AssociateSubnets: %v %+v", err, assoc) - } + assoc, err := m.AssociateSubnets(bg, "fw-1", []string{"subnet-1", "subnet-2"}, "") + require.NoError(t, err) + assert.Len(t, assoc.SubnetIDs, 2) - dis, err := m.DisassociateSubnets(ctx, "fw-1", []string{"subnet-1"}) - if err != nil || len(dis.SubnetIDs) != 1 || dis.SubnetIDs[0] != "subnet-2" { - t.Fatalf("DisassociateSubnets: %v %+v", err, dis) - } + dis, err := m.DisassociateSubnets(bg, "fw-1", []string{"subnet-1"}, "") + require.NoError(t, err) + assert.Equal(t, []string{"subnet-2"}, dis.SubnetIDs) - prot, err := m.UpdateFirewallDeleteProtection(ctx, "fw-1", true) - if err != nil || !prot.DeleteProtection { - t.Fatalf("UpdateFirewallDeleteProtection: %v %+v", err, prot) - } + prot, err := m.UpdateFirewallDeleteProtection(bg, "fw-1", true, "") + require.NoError(t, err) + assert.True(t, prot.DeleteProtection) - if err := m.UpdateLoggingConfiguration(ctx, "fw-1", logConfigs("FLOW", "ALERT")); err != nil { - t.Fatalf("UpdateLoggingConfiguration: %v", err) - } + require.NoError(t, m.UpdateLoggingConfiguration(bg, "fw-1", &nfdriver.LoggingConfiguration{ + Destinations: []nfdriver.LogDestinationConfig{s3Dest("FLOW", "b")}, + })) + require.NoError(t, m.UpdateLoggingConfiguration(bg, "fw-1", &nfdriver.LoggingConfiguration{ + Destinations: []nfdriver.LogDestinationConfig{s3Dest("FLOW", "b"), s3Dest("ALERT", "b")}, + })) - logs, err := m.DescribeLoggingConfiguration(ctx, "fw-1") - if err != nil || len(logs) != 2 { - t.Fatalf("DescribeLoggingConfiguration: %v %+v", err, logs) - } + logs, err := m.DescribeLoggingConfiguration(bg, "fw-1") + require.NoError(t, err) + assert.Len(t, logs.Destinations, 2) - if err := m.TagResource(ctx, fw.ARN, map[string]string{"env": "prod"}); err != nil { - t.Fatalf("TagResource: %v", err) - } + require.NoError(t, m.TagResource(bg, fw.ARN, map[string]string{"env": "prod"})) - got, _ := m.DescribeFirewall(ctx, "fw-1", "") - if got.Tags["env"] != "prod" { - t.Fatalf("tag not applied: %+v", got.Tags) - } + got, _ := m.DescribeFirewall(bg, "fw-1", "") + assert.Equal(t, "prod", got.Tags["env"]) - if err := m.UntagResource(ctx, fw.ARN, []string{"env"}); err != nil { - t.Fatalf("UntagResource: %v", err) - } + require.NoError(t, m.UntagResource(bg, fw.ARN, []string{"env"})) - got, _ = m.DescribeFirewall(ctx, "fw-1", "") - if _, ok := got.Tags["env"]; ok { - t.Fatalf("tag not removed: %+v", got.Tags) - } + got, _ = m.DescribeFirewall(bg, "fw-1", "") + assert.NotContains(t, got.Tags, "env") - // Unknown firewall / ARN error paths. - if _, err := m.AssociateFirewallPolicy(ctx, "missing", "x"); !cerrors.IsNotFound(err) { - t.Fatalf("associate missing firewall: got %v, want NotFound", err) - } + _, err = m.AssociateFirewallPolicy(bg, "missing", pol.ARN, "") + assert.True(t, cerrors.IsNotFound(err), "associate missing firewall: %v", err) - if err := m.TagResource(ctx, "arn:nope", map[string]string{"a": "b"}); !cerrors.IsNotFound(err) { - t.Fatalf("tag unknown arn: got %v, want NotFound", err) - } + err = m.TagResource(bg, "arn:aws:network-firewall:us-east-1:000000000000:firewall/nope", map[string]string{"a": "b"}) + assert.True(t, cerrors.IsNotFound(err), "tag unknown arn: %v", err) } // TestFirewallConcurrentAccess exercises the mutex under the race detector: @@ -187,26 +223,23 @@ func TestFirewallDepth(t *testing.T) { // "concurrent map writes". Run with -race for it to be meaningful. func TestFirewallConcurrentAccess(t *testing.T) { m := newMock() - ctx := context.Background() - - if _, err := m.CreateFirewall(ctx, nfdriver.CreateFirewallConfig{Name: "fw-1"}); err != nil { - t.Fatalf("CreateFirewall: %v", err) - } - - fw, _ := m.DescribeFirewall(ctx, "fw-1", "") + _, fw := seedStack(t, m) var wg sync.WaitGroup + for i := 0; i < 20; i++ { wg.Add(1) go func() { defer wg.Done() - _ = m.UpdateLoggingConfiguration(ctx, "fw-1", logConfigs("FLOW", "ALERT")) - _, _ = m.DescribeLoggingConfiguration(ctx, "fw-1") - _, _ = m.DescribeFirewall(ctx, "fw-1", "") - _ = m.TagResource(ctx, fw.ARN, map[string]string{"k": "v"}) - _, _ = m.ListFirewalls(ctx) + _ = m.UpdateLoggingConfiguration(bg, "fw-1", &nfdriver.LoggingConfiguration{ + Destinations: []nfdriver.LogDestinationConfig{s3Dest("FLOW", "b")}, + }) + _, _ = m.DescribeLoggingConfiguration(bg, "fw-1") + _, _ = m.DescribeFirewall(bg, "fw-1", "") + _ = m.TagResource(bg, fw.ARN, map[string]string{"k": "v"}) + _, _, _ = m.ListFirewalls(bg, nil, nfdriver.ListOptions{}) }() } diff --git a/providers/aws/networkfirewall/pagination.go b/providers/aws/networkfirewall/pagination.go new file mode 100644 index 000000000..4f64cc06a --- /dev/null +++ b/providers/aws/networkfirewall/pagination.go @@ -0,0 +1,34 @@ +package networkfirewall + +import ( + cerrors "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/internal/pagination" + nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" +) + +const ( + defaultPageSize = 100 + maxPageSize = 100 +) + +// paginate slices one page out of items (already in deterministic order) using +// the caller's MaxResults (1-100, default 100) and NextToken. A bad range or an +// undecodable token is a request error, never "page 1". +func paginate[T any](items []T, opts nfdriver.ListOptions) (page []T, nextToken string, err error) { + size := opts.MaxResults + + switch { + case size == 0: + size = defaultPageSize + case size < 1 || size > maxPageSize: + return nil, "", cerrors.Newf(cerrors.InvalidArgument, "MaxResults must be between 1 and %d", maxPageSize) + } + + pg, err := pagination.Paginate(items, opts.NextToken, size) + if err != nil { + // The only way Paginate fails is an undecodable token. + return nil, "", cerrors.New(cerrors.InvalidArgument, "NextToken is not valid") + } + + return pg.Items, pg.NextPageToken, nil +} diff --git a/providers/aws/networkfirewall/policy.go b/providers/aws/networkfirewall/policy.go new file mode 100644 index 000000000..f5b3ae6b6 --- /dev/null +++ b/providers/aws/networkfirewall/policy.go @@ -0,0 +1,278 @@ +package networkfirewall + +import ( + "context" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/internal/idgen" + nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" +) + +//nolint:gocritic // cfg matches the driver signature. +func (m *Mock) CreateFirewallPolicy(_ context.Context, cfg nfdriver.CreateFirewallPolicyConfig) (*nfdriver.FirewallPolicy, error) { + m.mu.Lock() + defer m.mu.Unlock() + + if err := validateName("FirewallPolicyName", cfg.Name); err != nil { + return nil, err + } + + if err := validateTags(cfg.Tags); err != nil { + return nil, err + } + + spec := policySpec{ + description: cfg.Description, statelessDefault: cfg.StatelessDefaultActions, + statelessFragmentDefault: cfg.StatelessFragmentDefaultActions, statefulDefault: cfg.StatefulDefaultActions, + statefulRefs: cfg.StatefulRuleGroupReferences, statelessRefs: cfg.StatelessRuleGroupReferences, + tlsARN: cfg.TLSInspectionConfigurationARN, extra: cfg.Extra, + } + if err := m.validatePolicy(&spec, nil); err != nil { + return nil, err + } + + p := &nfdriver.FirewallPolicy{ + Name: cfg.Name, + ARN: m.arn("firewall-policy", cfg.Name), + ID: idgen.GenerateID(""), + Description: cfg.Description, + StatelessDefaultActions: cloneStrings(cfg.StatelessDefaultActions), + StatelessFragmentDefaultActions: cloneStrings(cfg.StatelessFragmentDefaultActions), + StatefulDefaultActions: cloneStrings(cfg.StatefulDefaultActions), + StatefulRuleGroupReferences: cloneRuleGroupRefs(cfg.StatefulRuleGroupReferences), + StatelessRuleGroupReferences: cloneRuleGroupRefs(cfg.StatelessRuleGroupReferences), + TLSInspectionConfigurationARN: cfg.TLSInspectionConfigurationARN, + Extra: clonePolicyExtra(&cfg.Extra), + Status: statusActive, + LastModified: m.now(), + UpdateToken: newToken(), + Tags: copyTags(cfg.Tags), + } + if !m.policies.SetIfAbsent(cfg.Name, p) { + return nil, cerrors.Newf(cerrors.AlreadyExists, "firewall policy %q already exists", cfg.Name) + } + + return m.outPolicy(p), nil +} + +func (m *Mock) DescribeFirewallPolicy(_ context.Context, name, arn string) (*nfdriver.FirewallPolicy, error) { + m.mu.RLock() + defer m.mu.RUnlock() + + if err := requireIdentifier("FirewallPolicy", name, arn); err != nil { + return nil, err + } + + p, ok := m.lookupPolicy(name, arn) + if !ok { + return nil, cerrors.Newf(cerrors.NotFound, "firewall policy %q not found", nonEmpty(name, arn)) + } + + return m.outPolicy(p), nil +} + +//nolint:gocritic // cfg matches the driver signature. +func (m *Mock) UpdateFirewallPolicy( + _ context.Context, name, arn string, cfg nfdriver.UpdateFirewallPolicyConfig, +) (*nfdriver.FirewallPolicy, error) { + m.mu.Lock() + defer m.mu.Unlock() + + if err := requireIdentifier("FirewallPolicy", name, arn); err != nil { + return nil, err + } + + if err := requireToken(cfg.UpdateToken); err != nil { + return nil, err + } + + p, ok := m.lookupPolicy(name, arn) + if !ok { + return nil, cerrors.Newf(cerrors.NotFound, "firewall policy %q not found", nonEmpty(name, arn)) + } + + if err := checkToken(p.UpdateToken, cfg.UpdateToken); err != nil { + return nil, err + } + + spec := policySpec{ + description: cfg.Description, statelessDefault: cfg.StatelessDefaultActions, + statelessFragmentDefault: cfg.StatelessFragmentDefaultActions, statefulDefault: cfg.StatefulDefaultActions, + statefulRefs: cfg.StatefulRuleGroupReferences, statelessRefs: cfg.StatelessRuleGroupReferences, + tlsARN: cfg.TLSInspectionConfigurationARN, extra: cfg.Extra, + } + if err := m.validatePolicy(&spec, p); err != nil { + return nil, err + } + + p.Description = cfg.Description + p.StatelessDefaultActions = cloneStrings(cfg.StatelessDefaultActions) + p.StatelessFragmentDefaultActions = cloneStrings(cfg.StatelessFragmentDefaultActions) + p.StatefulDefaultActions = cloneStrings(cfg.StatefulDefaultActions) + p.StatefulRuleGroupReferences = cloneRuleGroupRefs(cfg.StatefulRuleGroupReferences) + p.StatelessRuleGroupReferences = cloneRuleGroupRefs(cfg.StatelessRuleGroupReferences) + p.TLSInspectionConfigurationARN = cfg.TLSInspectionConfigurationARN + p.Extra = clonePolicyExtra(&cfg.Extra) + p.LastModified = m.now() + p.UpdateToken = newToken() + + return m.outPolicy(p), nil +} + +func (m *Mock) DeleteFirewallPolicy(_ context.Context, name, arn string) (*nfdriver.FirewallPolicy, error) { + m.mu.Lock() + defer m.mu.Unlock() + + if err := requireIdentifier("FirewallPolicy", name, arn); err != nil { + return nil, err + } + + p, ok := m.lookupPolicy(name, arn) + if !ok { + return nil, cerrors.Newf(cerrors.NotFound, "firewall policy %q not found", nonEmpty(name, arn)) + } + + if m.policyInUse(p.ARN) { + return nil, nfdriver.InvalidOperation("firewall policy %q is in use by a firewall", p.Name) + } + + out := m.outPolicy(p) + out.Status = statusDeleting + + m.policies.Delete(p.Name) + delete(m.resourcePolicies, p.ARN) + + return out, nil +} + +func (m *Mock) ListFirewallPolicies(_ context.Context, opts nfdriver.ListOptions) ([]nfdriver.FirewallPolicy, string, error) { + m.mu.RLock() + defer m.mu.RUnlock() + + items, next, err := paginate(m.policies.SortedValues(), opts) + if err != nil { + return nil, "", err + } + + out := make([]nfdriver.FirewallPolicy, 0, len(items)) + for _, p := range items { + out = append(out, *m.outPolicy(p)) + } + + return out, next, nil +} + +func (m *Mock) lookupPolicy(name, arn string) (*nfdriver.FirewallPolicy, bool) { + if name != "" { + return m.policies.Get(name) + } + + for _, p := range m.policies.SortedValues() { + if p.ARN == arn { + return p, true + } + } + + return nil, false +} + +// policyExists reports whether a firewall policy with the given ARN is present. +func (m *Mock) policyExists(arn string) bool { + for _, p := range m.policies.SortedValues() { + if p.ARN == arn { + return true + } + } + + return false +} + +// policyInUse reports whether any firewall references the policy ARN. +func (m *Mock) policyInUse(arn string) bool { + return m.policyAssociationCount(arn) > 0 +} + +func (m *Mock) policyAssociationCount(arn string) int { + n := 0 + + for _, f := range m.firewalls.SortedValues() { + if f.PolicyARN == arn { + n++ + } + } + + return n +} + +// outPolicy returns a deep copy of the stored policy with the computed +// capacity, association count and status filled in. +func (m *Mock) outPolicy(p *nfdriver.FirewallPolicy) *nfdriver.FirewallPolicy { + out := *p + out.StatelessDefaultActions = cloneStrings(p.StatelessDefaultActions) + out.StatelessFragmentDefaultActions = cloneStrings(p.StatelessFragmentDefaultActions) + out.StatefulDefaultActions = cloneStrings(p.StatefulDefaultActions) + out.StatefulRuleGroupReferences = cloneRuleGroupRefs(p.StatefulRuleGroupReferences) + out.StatelessRuleGroupReferences = cloneRuleGroupRefs(p.StatelessRuleGroupReferences) + out.Extra = clonePolicyExtra(&p.Extra) + out.Tags = copyTags(p.Tags) + out.NumberOfAssociations = m.policyAssociationCount(p.ARN) + out.ConsumedStatelessRuleCapacity = m.referencedCapacity(p.StatelessRuleGroupReferences) + + stateful, domain := 0, 0 + + for _, r := range p.StatefulRuleGroupReferences { + rg, ok := m.ruleGroupByARN(r.ResourceARN) + if !ok { + continue + } + + if rg.Type == ruleTypeStatefulDomain { + domain += rg.Capacity + } else { + stateful += rg.Capacity + } + } + + out.ConsumedStatefulRuleCapacity = stateful + out.ConsumedStatefulDomainCapacity = domain + + return &out +} + +// referencedCapacity sums the capacity AWS reserves for referenced rule groups. +func (m *Mock) referencedCapacity(refs []nfdriver.RuleGroupReference) int { + total := 0 + + for _, r := range refs { + if rg, ok := m.ruleGroupByARN(r.ResourceARN); ok { + total += rg.Capacity + } + } + + return total +} + +func cloneRuleGroupRefs(refs []nfdriver.RuleGroupReference) []nfdriver.RuleGroupReference { + if len(refs) == 0 { + return nil + } + + out := make([]nfdriver.RuleGroupReference, len(refs)) + for i, r := range refs { + out[i] = nfdriver.RuleGroupReference{ + ResourceARN: r.ResourceARN, Priority: r.Priority, + Override: cloneRawJSON(r.Override), DeepThreatInspection: cloneBool(r.DeepThreatInspection), + } + } + + return out +} + +func clonePolicyExtra(e *nfdriver.PolicyExtra) nfdriver.PolicyExtra { + return nfdriver.PolicyExtra{ + StatelessCustomActions: cloneRawJSON(e.StatelessCustomActions), + PolicyVariables: cloneRawJSON(e.PolicyVariables), + StatefulEngineOptions: cloneRawJSON(e.StatefulEngineOptions), + EnableTLSSessionHolding: cloneBool(e.EnableTLSSessionHolding), + } +} diff --git a/providers/aws/networkfirewall/policy_actions.go b/providers/aws/networkfirewall/policy_actions.go new file mode 100644 index 000000000..147757fa2 --- /dev/null +++ b/providers/aws/networkfirewall/policy_actions.go @@ -0,0 +1,246 @@ +package networkfirewall + +import ( + "encoding/json" + + nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" +) + +const ( + ruleOrderStrict = "STRICT_ORDER" + + minPriority = 1 + maxPriority = 65535 +) + +// Standard stateless default actions and the documented stateful default +// actions (AWS FirewallPolicy reference). +// +//nolint:gochecknoglobals // immutable enum value sets. +var ( + statelessStandardActions = []string{"aws:pass", "aws:drop", "aws:forward_to_sfe"} + statefulDefaultActions = []string{ + "aws:drop_strict", "aws:drop_established", "aws:alert_strict", "aws:alert_established", + "aws:drop_established_app_layer", "aws:alert_established_app_layer", + "aws:drop_established_app_layer_to_server", "aws:alert_established_app_layer_to_server", + } +) + +// policySpec is the validated shape shared by create and update. +type policySpec struct { + description string + statelessDefault []string + statelessFragmentDefault []string + statefulDefault []string + statefulRefs []nfdriver.RuleGroupReference + statelessRefs []nfdriver.RuleGroupReference + tlsARN string + extra nfdriver.PolicyExtra +} + +// validatePolicy applies the AWS rules for a firewall policy body. current is +// the stored policy on update (nil on create). +func (m *Mock) validatePolicy(s *policySpec, current *nfdriver.FirewallPolicy) error { + if err := validateDescription(s.description); err != nil { + return err + } + + strict, err := validatePolicyActions(s) + if err != nil { + return err + } + + return m.validatePolicyReferences(s, strict, current) +} + +// validatePolicyReferences checks the rule group references and the TLS +// inspection configuration the policy names. +func (m *Mock) validatePolicyReferences(s *policySpec, strict bool, current *nfdriver.FirewallPolicy) error { + if err := m.validateRefs("StatelessRuleGroupReferences", s.statelessRefs, true, []string{ruleTypeStateless}); err != nil { + return err + } + + if err := m.validateRefs("StatefulRuleGroupReferences", s.statefulRefs, strict, + []string{ruleTypeStateful, ruleTypeStatefulDomain}); err != nil { + return err + } + + return m.validatePolicyTLS(s, current) +} + +// validatePolicyActions checks the stateless and stateful default actions and +// reports whether the policy uses strict stateful rule ordering. +func validatePolicyActions(s *policySpec) (bool, error) { + custom, err := customActionNames(s.extra.StatelessCustomActions) + if err != nil { + return false, err + } + + strict, err := isStrictOrder(s.extra.StatefulEngineOptions) + if err != nil { + return false, err + } + + return strict, checkDefaultActions(s, custom, strict) +} + +func checkDefaultActions(s *policySpec, custom map[string]bool, strict bool) error { + if err := validateStatelessActions("StatelessDefaultActions", s.statelessDefault, custom); err != nil { + return err + } + + if err := validateStatelessActions("StatelessFragmentDefaultActions", s.statelessFragmentDefault, custom); err != nil { + return err + } + + return validateStatefulDefaults(s.statefulDefault, strict) +} + +// validateStatelessActions requires exactly one standard action; any other entry +// must name a custom action defined in the same policy. +func validateStatelessActions(field string, actions []string, custom map[string]bool) error { + if len(actions) == 0 { + return invalid("%s is required", field) + } + + standard := 0 + + for _, a := range actions { + switch { + case oneOf(a, statelessStandardActions...): + standard++ + case custom[a]: + default: + return invalid("%s entry %q is neither a standard action nor a defined custom action", field, a) + } + } + + if standard != 1 { + return invalid("%s must contain exactly one of %v", field, statelessStandardActions) + } + + return nil +} + +func validateStatefulDefaults(actions []string, strict bool) error { + if len(actions) == 0 { + return nil + } + + if !strict { + return invalid("StatefulDefaultActions is only valid with StatefulEngineOptions.RuleOrder %s", ruleOrderStrict) + } + + for _, a := range actions { + if !oneOf(a, statefulDefaultActions...) { + return invalid("StatefulDefaultActions entry %q is not valid", a) + } + } + + return nil +} + +// validateRefs checks rule group references: the group must exist with an +// allowed type, a priority is required where order matters, and priorities are +// unique within the list. +func (m *Mock) validateRefs(field string, refs []nfdriver.RuleGroupReference, needPriority bool, types []string) error { + seen := make(map[int]bool, len(refs)) + + for _, r := range refs { + if err := validateARN(field+" ResourceArn", r.ResourceARN); err != nil { + return err + } + + rg, ok := m.ruleGroupByARN(r.ResourceARN) + if !ok { + return invalid("%s: rule group %q not found", field, r.ResourceARN) + } + + if !oneOf(rg.Type, types...) { + return invalid("%s: rule group %q has type %s", field, r.ResourceARN, rg.Type) + } + + if !needPriority { + continue + } + + if r.Priority < minPriority || r.Priority > maxPriority { + return invalid("%s: Priority must be between %d and %d", field, minPriority, maxPriority) + } + + if seen[r.Priority] { + return invalid("%s: duplicate Priority %d", field, r.Priority) + } + + seen[r.Priority] = true + } + + return nil +} + +// validatePolicyTLS checks the TLS inspection configuration reference: it must +// exist, can only be replaced (never added or removed after create), and TLS +// session holding needs one. +func (m *Mock) validatePolicyTLS(s *policySpec, current *nfdriver.FirewallPolicy) error { + if s.tlsARN != "" { + if err := validateARN("TLSInspectionConfigurationArn", s.tlsARN); err != nil { + return err + } + + if _, ok := m.tlsByARN(s.tlsARN); !ok { + return invalid("TLS inspection configuration %q not found", s.tlsARN) + } + } + + if current != nil && (current.TLSInspectionConfigurationARN == "") != (s.tlsARN == "") { + return invalid("a TLS inspection configuration can only be replaced, not added or removed, after the policy is created") + } + + if s.extra.EnableTLSSessionHolding != nil && *s.extra.EnableTLSSessionHolding && s.tlsARN == "" { + return invalid("EnableTLSSessionHolding requires a TLS inspection configuration") + } + + return nil +} + +func customActionNames(raw json.RawMessage) (map[string]bool, error) { + names := map[string]bool{} + + if len(raw) == 0 { + return names, nil + } + + var actions []struct { + ActionName string `json:"ActionName"` + } + + if err := json.Unmarshal(raw, &actions); err != nil { + return nil, invalid("StatelessCustomActions is not valid: %v", err) + } + + for _, a := range actions { + names[a.ActionName] = true + } + + return names, nil +} + +func isStrictOrder(raw json.RawMessage) (bool, error) { + if len(raw) == 0 { + return false, nil + } + + var opts struct { + RuleOrder string `json:"RuleOrder"` + } + + if err := json.Unmarshal(raw, &opts); err != nil { + return false, invalid("StatefulEngineOptions is not valid: %v", err) + } + + if opts.RuleOrder != "" && !oneOf(opts.RuleOrder, "DEFAULT_ACTION_ORDER", ruleOrderStrict) { + return false, invalid("StatefulEngineOptions.RuleOrder %q is not valid", opts.RuleOrder) + } + + return opts.RuleOrder == ruleOrderStrict, nil +} diff --git a/providers/aws/networkfirewall/reference_test.go b/providers/aws/networkfirewall/reference_test.go new file mode 100644 index 000000000..cd62a108d --- /dev/null +++ b/providers/aws/networkfirewall/reference_test.go @@ -0,0 +1,395 @@ +package networkfirewall + +import ( + "fmt" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" +) + +func refPolicy(name string, stateless, stateful []nfdriver.RuleGroupReference) nfdriver.CreateFirewallPolicyConfig { + return nfdriver.CreateFirewallPolicyConfig{ + Name: name, StatelessDefaultActions: standardActions, StatelessFragmentDefaultActions: standardActions, + StatelessRuleGroupReferences: stateless, StatefulRuleGroupReferences: stateful, + } +} + +func TestDeleteGuards_InvalidOperation(t *testing.T) { + m := newMock() + pol, fw := seedStack(t, m) + + _, err := m.UpdateFirewallDeleteProtection(bg, "fw-1", true, "") + require.NoError(t, err) + + _, err = m.DeleteFirewall(bg, "fw-1", "") + requireException(t, err, nfdriver.ExInvalidOperation) + + _, err = m.DeleteFirewallPolicy(bg, "pol-1", "") + requireException(t, err, nfdriver.ExInvalidOperation) + + rg := seedRuleGroup(t, m, "rg-1", "STATEFUL", 5) + _, err = m.CreateFirewallPolicy(bg, refPolicy("p2", nil, []nfdriver.RuleGroupReference{{ResourceARN: rg.ARN}})) + require.NoError(t, err) + + _, err = m.DeleteRuleGroup(bg, "rg-1", "", "STATEFUL") + requireException(t, err, nfdriver.ExInvalidOperation) + + _ = pol + _ = fw +} + +func TestDeleteRuleGroup_InUseByPolicy_Stateful_Stateless(t *testing.T) { + m := newMock() + sl := seedRuleGroup(t, m, "rg-sl", "STATELESS", 5) + sf := seedRuleGroup(t, m, "rg-sf", "STATEFUL", 5) + + p, err := m.CreateFirewallPolicy(bg, refPolicy("p", + []nfdriver.RuleGroupReference{{ResourceARN: sl.ARN, Priority: 10}}, + []nfdriver.RuleGroupReference{{ResourceARN: sf.ARN}})) + require.NoError(t, err) + + _, err = m.DeleteRuleGroup(bg, "rg-sl", "", "STATELESS") + requireException(t, err, nfdriver.ExInvalidOperation) + + _, err = m.DeleteRuleGroup(bg, "", sf.ARN, "") + requireException(t, err, nfdriver.ExInvalidOperation) + + // Dropping the references frees the rule groups. + _, err = m.UpdateFirewallPolicy(bg, "p", "", nfdriver.UpdateFirewallPolicyConfig{ + UpdateToken: p.UpdateToken, StatelessDefaultActions: standardActions, StatelessFragmentDefaultActions: standardActions, + }) + require.NoError(t, err) + + _, err = m.DeleteRuleGroup(bg, "rg-sl", "", "STATELESS") + require.NoError(t, err) + + _, err = m.DeleteRuleGroup(bg, "rg-sf", "", "STATEFUL") + require.NoError(t, err) +} + +func TestComputed_RuleGroupFields(t *testing.T) { + m, clock := newClockMock(t) + + stateless := []byte(`{"RulesSource":{"StatelessRulesAndCustomActions":{"StatelessRules":[` + + `{"Priority":1,"RuleDefinition":{"Actions":["aws:pass"],"MatchAttributes":{"Protocols":[6,17],` + + `"Sources":[{"AddressDefinition":"10.0.0.0/24"},{"AddressDefinition":"10.0.1.0/24"},{"AddressDefinition":"10.0.2.0/24"}]}}},` + + `{"Priority":2,"RuleDefinition":{"Actions":["aws:drop"],"MatchAttributes":{}}}]}}}`) + + sl, err := m.CreateRuleGroup(bg, nfdriver.CreateRuleGroupConfig{Name: "sl", Type: "STATELESS", Capacity: 10, Rules: stateless}) + require.NoError(t, err) + assert.Equal(t, 7, sl.ConsumedCapacity, "2 protocols x 3 sources = 6, plus 1 for the unconstrained rule") + assert.Equal(t, "ACTIVE", sl.Status) + assert.Equal(t, clock.Now(), sl.LastModified) + + sf, err := m.CreateRuleGroup(bg, nfdriver.CreateRuleGroupConfig{ + Name: "sf", Type: "STATEFUL", Capacity: 10, Rules: rulesString("r1", "# note", "", "r2"), + }) + require.NoError(t, err) + assert.Equal(t, 2, sf.ConsumedCapacity, "one unit per rule, comments and blanks excluded") + + structured, err := m.CreateRuleGroup(bg, nfdriver.CreateRuleGroupConfig{ + Name: "sf2", Type: "STATEFUL", Capacity: 10, Rules: []byte(`{"RulesSource":{"StatefulRules":[{"Action":"PASS"},{"Action":"DROP"},{"Action":"ALERT"}]}}`), + }) + require.NoError(t, err) + assert.Equal(t, 3, structured.ConsumedCapacity) + + _, err = m.CreateFirewallPolicy(bg, refPolicy("p", []nfdriver.RuleGroupReference{{ResourceARN: sl.ARN, Priority: 1}}, nil)) + require.NoError(t, err) + + got, _ := m.DescribeRuleGroup(bg, "sl", "", "STATELESS") + assert.Equal(t, 1, got.NumberOfAssociations) + + clock.Advance(time.Hour) + + later, _ := m.DescribeRuleGroup(bg, "sl", "", "STATELESS") + assert.Equal(t, got.LastModified, later.LastModified, "reads never move the timestamp") + + updated, err := m.UpdateRuleGroup(bg, "sl", "", "STATELESS", nfdriver.UpdateRuleGroupConfig{UpdateToken: got.UpdateToken, Description: "x"}) + require.NoError(t, err) + assert.True(t, updated.LastModified.After(got.LastModified), "a mutation does") +} + +func TestComputed_PolicyFields(t *testing.T) { + m := newMock() + sl := seedRuleGroup(t, m, "rg-sl", "STATELESS", 20) + sf := seedRuleGroup(t, m, "rg-sf", "STATEFUL", 30) + + domain, err := m.CreateRuleGroup(bg, nfdriver.CreateRuleGroupConfig{Name: "rg-d", Type: "STATEFUL_DOMAIN", Capacity: 40}) + require.NoError(t, err) + + p, err := m.CreateFirewallPolicy(bg, refPolicy("p", + []nfdriver.RuleGroupReference{{ResourceARN: sl.ARN, Priority: 1}}, + []nfdriver.RuleGroupReference{{ResourceARN: sf.ARN}, {ResourceARN: domain.ARN}})) + require.NoError(t, err) + + assert.Equal(t, "ACTIVE", p.Status) + assert.Equal(t, 20, p.ConsumedStatelessRuleCapacity) + assert.Equal(t, 30, p.ConsumedStatefulRuleCapacity) + assert.Equal(t, 40, p.ConsumedStatefulDomainCapacity) + assert.Equal(t, 0, p.NumberOfAssociations) + + seedFirewall(t, m, "fw-a", p.ARN) + seedFirewall(t, m, "fw-b", p.ARN) + + got, _ := m.DescribeFirewallPolicy(bg, "p", "") + assert.Equal(t, 2, got.NumberOfAssociations) +} + +func TestComputed_FirewallFields(t *testing.T) { + m := newMock() + _, fw := seedStack(t, m) + + assert.Equal(t, "READY", fw.Status) + assert.Equal(t, 0, fw.NumberOfAssociations) + + vpcAssociation(t, m, fw.ARN, "subnet-a") + vpcAssociation(t, m, fw.ARN, "subnet-b") + + got, _ := m.DescribeFirewall(bg, "fw-1", "") + assert.Equal(t, 2, got.NumberOfAssociations) +} + +func TestListFirewalls_VpcIdsFilter(t *testing.T) { + m := newMock() + pol := seedPolicy(t, m, "pol-1") + + for name, vpc := range map[string]string{"fw-a": "vpc-aa", "fw-b": "vpc-bb", "fw-c": "vpc-aa"} { + _, err := m.CreateFirewall(bg, nfdriver.CreateFirewallConfig{Name: name, PolicyARN: pol.ARN, VPCID: vpc}) + require.NoError(t, err) + } + + names := func(vpcs ...string) []string { + items, _, err := m.ListFirewalls(bg, vpcs, nfdriver.ListOptions{}) + require.NoError(t, err) + + out := make([]string, 0, len(items)) + for _, f := range items { + out = append(out, f.Name) + } + + return out + } + + assert.Equal(t, []string{"fw-a", "fw-b", "fw-c"}, names()) + assert.Equal(t, []string{"fw-a", "fw-c"}, names("vpc-aa")) + assert.Equal(t, []string{"fw-a", "fw-b", "fw-c"}, names("vpc-aa", "vpc-bb")) + assert.Empty(t, names("vpc-ff")) + + _, _, err := m.ListFirewalls(bg, []string{"not-a-vpc"}, nfdriver.ListOptions{}) + assert.True(t, cerrors.IsInvalidArgument(err), "%v", err) +} + +func TestListRuleGroups_TypeScopeFilters(t *testing.T) { + m := newMock() + seedRuleGroup(t, m, "a-sl", "STATELESS", 1) + seedRuleGroup(t, m, "b-sf", "STATEFUL", 1) + + count := func(f nfdriver.RuleGroupFilter) int { + items, _, err := m.ListRuleGroups(bg, f, nfdriver.ListOptions{}) + require.NoError(t, err) + + return len(items) + } + + assert.Equal(t, 2, count(nfdriver.RuleGroupFilter{})) + assert.Equal(t, 1, count(nfdriver.RuleGroupFilter{Type: "STATELESS"})) + assert.Equal(t, 2, count(nfdriver.RuleGroupFilter{Scope: "ACCOUNT"})) + assert.Equal(t, 0, count(nfdriver.RuleGroupFilter{Scope: "MANAGED"}), "the emulator has no AWS-managed rule groups") + assert.Equal(t, 0, count(nfdriver.RuleGroupFilter{ManagedType: "AWS_MANAGED_DOMAIN_LISTS"})) + + for _, bad := range []nfdriver.RuleGroupFilter{{Type: "X"}, {Scope: "X"}, {ManagedType: "X"}} { + _, _, err := m.ListRuleGroups(bg, bad, nfdriver.ListOptions{}) + assert.True(t, cerrors.IsInvalidArgument(err), "%+v: %v", bad, err) + } +} + +func TestARNFormats(t *testing.T) { + m := newMock() + pol, fw := seedStack(t, m) + sl := seedRuleGroup(t, m, "rg-sl", "STATELESS", 1) + sf := seedRuleGroup(t, m, "rg-sf", "STATEFUL", 1) + + prefix := "arn:aws:network-firewall:us-east-1:123456789012:" + assert.Equal(t, prefix+"firewall/fw-1", fw.ARN) + assert.Equal(t, prefix+"firewall-policy/pol-1", pol.ARN) + assert.Equal(t, prefix+"stateless-rulegroup/rg-sl", sl.ARN) + assert.Equal(t, prefix+"stateful-rulegroup/rg-sf", sf.ARN) + + assoc := vpcAssociation(t, m, fw.ARN, "subnet-a") + assert.Contains(t, assoc.ARN, prefix+"vpc-endpoint-association/") + + for arn, kind := range map[string]string{fw.ARN: "firewall", pol.ARN: "firewall-policy", sl.ARN: "stateless-rulegroup", "x": ""} { + assert.Equal(t, kind, arnKind(arn)) + } +} + +func TestMalformedARN_BeforeLookup(t *testing.T) { + m := newMock() + seedStack(t, m) + + for name, call := range map[string]func() error{ + "describe firewall": func() error { _, err := m.DescribeFirewall(bg, "", "bogus"); return err }, + "delete firewall": func() error { _, err := m.DeleteFirewall(bg, "", "bogus"); return err }, + "describe policy": func() error { _, err := m.DescribeFirewallPolicy(bg, "", "bogus"); return err }, + "delete policy": func() error { _, err := m.DeleteFirewallPolicy(bg, "", "bogus"); return err }, + "describe rule group": func() error { _, err := m.DescribeRuleGroup(bg, "", "bogus", ""); return err }, + "delete rule group": func() error { _, err := m.DeleteRuleGroup(bg, "", "bogus", ""); return err }, + "describe tls": func() error { _, err := m.DescribeTLSInspectionConfiguration(bg, "", "bogus"); return err }, + "describe assoc": func() error { _, err := m.DescribeVpcEndpointAssociation(bg, "bogus"); return err }, + "tag": func() error { return m.TagResource(bg, "bogus", map[string]string{"a": "b"}) }, + "untag": func() error { return m.UntagResource(bg, "bogus", nil) }, + "list tags": func() error { _, err := m.ListTagsForResource(bg, "bogus"); return err }, + "no identifier": func() error { _, err := m.DescribeFirewall(bg, "", ""); return err }, + "metadata": func() error { _, err := m.DescribeFirewallMetadata(bg, "bogus"); return err }, + } { + t.Run(name, func(t *testing.T) { + assert.True(t, cerrors.IsInvalidArgument(call()), "a malformed ARN is a request error, not NotFound") + }) + } + + _, err := m.DescribeFirewall(bg, "", "arn:aws:network-firewall:us-east-1:000000000000:firewall/ghost") + assert.True(t, cerrors.IsNotFound(err), "a well-formed unknown ARN is NotFound: %v", err) +} + +func TestNameAndARNDisagree(t *testing.T) { + m := newMock() + pol := seedPolicy(t, m, "pol-1") + seedFirewall(t, m, "fw-a", pol.ARN) + other := seedFirewall(t, m, "fw-b", pol.ARN) + + // When both are supplied the name selects the resource, as before this change. + got, err := m.DescribeFirewall(bg, "fw-a", other.ARN) + require.NoError(t, err) + assert.Equal(t, "fw-a", got.Name) +} + +func TestPagination_AllLists_PageSize1_Walk(t *testing.T) { + m := newMock() + pol := seedPolicy(t, m, "pol-b") + seedPolicy(t, m, "pol-c") + + for i := 0; i < 3; i++ { + seedFirewall(t, m, fmt.Sprintf("fw-%d", 3-i), pol.ARN) + seedRuleGroup(t, m, fmt.Sprintf("rg-%d", 3-i), "STATEFUL", 1) + + _, err := m.CreateTLSInspectionConfiguration(bg, nfdriver.CreateTLSInspectionConfig{ + Name: fmt.Sprintf("tls-%d", 3-i), Configuration: tlsConfigJSON("c"), + }) + require.NoError(t, err) + } + + // A policy created last sorts first, proving order is by name, not insertion. + seedPolicy(t, m, "pol-a") + + fw, _ := m.DescribeFirewall(bg, "fw-1", "") + for i := 0; i < 3; i++ { + vpcAssociation(t, m, fw.ARN, fmt.Sprintf("subnet-%d", i)) + } + + _, err := m.UpdateFirewallAnalysisSettings(bg, "fw-1", []string{"TLS_SNI"}, "") + require.NoError(t, err) + + for i := 0; i < 3; i++ { + _, err := m.StartAnalysisReport(bg, "fw-1", "", "TLS_SNI") + require.NoError(t, err) + } + + scope := nfdriver.FlowOperationScope{FirewallARN: fw.ARN, AvailabilityZone: "us-east-1a"} + for i := 0; i < 3; i++ { + _, err := m.StartFlowFlush(bg, nfdriver.FlowOperationRequest{FirewallARN: fw.ARN, AvailabilityZone: "us-east-1a"}) + require.NoError(t, err) + } + + lists := map[string]struct { + want int + page func(opts nfdriver.ListOptions) (int, string, error) + }{ + "firewalls": {3, func(o nfdriver.ListOptions) (int, string, error) { + v, n, err := m.ListFirewalls(bg, nil, o) + return len(v), n, err + }}, + "policies": {3, func(o nfdriver.ListOptions) (int, string, error) { + v, n, err := m.ListFirewallPolicies(bg, o) + return len(v), n, err + }}, + "rule groups": {3, func(o nfdriver.ListOptions) (int, string, error) { + v, n, err := m.ListRuleGroups(bg, nfdriver.RuleGroupFilter{}, o) + return len(v), n, err + }}, + "tls": {3, func(o nfdriver.ListOptions) (int, string, error) { + v, n, err := m.ListTLSInspectionConfigurations(bg, o) + return len(v), n, err + }}, + "associations": {3, func(o nfdriver.ListOptions) (int, string, error) { + v, n, err := m.ListVpcEndpointAssociations(bg, fw.ARN, o) + return len(v), n, err + }}, + "analysis reports": {3, func(o nfdriver.ListOptions) (int, string, error) { + v, n, err := m.ListAnalysisReports(bg, "fw-1", "", o) + return len(v), n, err + }}, + "flow operations": {3, func(o nfdriver.ListOptions) (int, string, error) { + v, n, err := m.ListFlowOperations(bg, scope, "", o) + return len(v), n, err + }}, + } + + for name, l := range lists { + t.Run(name, func(t *testing.T) { + seen, token := 0, "" + + for pages := 0; pages < 10; pages++ { + n, next, err := l.page(nfdriver.ListOptions{MaxResults: 1, NextToken: token}) + require.NoError(t, err) + assert.Equal(t, 1, n) + + seen += n + + if next == "" { + break + } + + token = next + } + + assert.Equal(t, l.want, seen, "every item exactly once") + }) + } + + // Firewalls come back sorted by name regardless of creation order. + items, _, err := m.ListFirewalls(bg, nil, nfdriver.ListOptions{}) + require.NoError(t, err) + assert.Equal(t, "fw-1", items[0].Name) + + pols, _, err := m.ListFirewallPolicies(bg, nfdriver.ListOptions{}) + require.NoError(t, err) + assert.Equal(t, "pol-a", pols[0].Name) +} + +func TestPagination_InvalidTokenAndRange(t *testing.T) { + m := newMock() + seedPolicy(t, m, "pol-1") + + for _, opts := range []nfdriver.ListOptions{{MaxResults: -1}, {MaxResults: 101}, {NextToken: "!!!not-a-token"}} { + _, _, err := m.ListFirewallPolicies(bg, opts) + assert.True(t, cerrors.IsInvalidArgument(err), "%+v: %v", opts, err) + } + + items, next, err := m.ListFirewallPolicies(bg, nfdriver.ListOptions{MaxResults: 100}) + require.NoError(t, err) + assert.Len(t, items, 1) + assert.Empty(t, next, "the last page has no token") +} + +func TestPagination_HugeMaxResults_Bounded(t *testing.T) { + m := newMock() + seedPolicy(t, m, "pol-1") + + _, _, err := m.ListFirewallPolicies(bg, nfdriver.ListOptions{MaxResults: 1 << 30}) + assert.True(t, cerrors.IsInvalidArgument(err), "MaxResults above 100 is rejected, never used to size an allocation: %v", err) +} diff --git a/providers/aws/networkfirewall/resource_policy.go b/providers/aws/networkfirewall/resource_policy.go new file mode 100644 index 000000000..5a4582766 --- /dev/null +++ b/providers/aws/networkfirewall/resource_policy.go @@ -0,0 +1,194 @@ +package networkfirewall + +import ( + "context" + "encoding/json" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" +) + +const maxResourcePolicyLen = 395000 + +// allowedPolicyActions are the actions a resource-sharing policy may grant, per +// shareable resource kind (AWS PutResourcePolicy reference). +// +//nolint:gochecknoglobals // immutable lookup table. +var allowedPolicyActions = map[string][]string{ + "stateful-rulegroup": ruleGroupPolicyActions, + "stateless-rulegroup": ruleGroupPolicyActions, + "firewall-policy": {"network-firewall:AssociateFirewallPolicy", "network-firewall:ListFirewallPolicies"}, + "firewall": { + "network-firewall:CreateVpcEndpointAssociation", "network-firewall:DescribeFirewallMetadata", "network-firewall:ListFirewalls", + }, +} + +//nolint:gochecknoglobals // immutable lookup table. +var ruleGroupPolicyActions = []string{ + "network-firewall:CreateFirewallPolicy", "network-firewall:UpdateFirewallPolicy", "network-firewall:ListRuleGroups", +} + +// PutResourcePolicy creates or replaces the resource-sharing policy of a rule +// group, firewall policy or firewall. +func (m *Mock) PutResourcePolicy(_ context.Context, resourceARN, policy string) error { + m.mu.Lock() + defer m.mu.Unlock() + + kind, err := m.shareableKind(resourceARN) + if err != nil { + return err + } + + if policy == "" || len(policy) > maxResourcePolicyLen { + return invalid("Policy must be 1 to %d characters", maxResourcePolicyLen) + } + + if err := validatePolicyDocument(policy, allowedPolicyActions[kind]); err != nil { + return err + } + + m.resourcePolicies[resourceARN] = policy + + return nil +} + +// DescribeResourcePolicy returns the resource-sharing policy of a resource. +func (m *Mock) DescribeResourcePolicy(_ context.Context, resourceARN string) (string, error) { + m.mu.RLock() + defer m.mu.RUnlock() + + if _, err := m.shareableKind(resourceARN); err != nil { + return "", err + } + + p, ok := m.resourcePolicies[resourceARN] + if !ok { + return "", cerrors.Newf(cerrors.NotFound, "no resource policy is attached to %q", resourceARN) + } + + return p, nil +} + +// DeleteResourcePolicy removes the resource-sharing policy of a resource. +func (m *Mock) DeleteResourcePolicy(_ context.Context, resourceARN string) error { + m.mu.Lock() + defer m.mu.Unlock() + + if _, err := m.shareableKind(resourceARN); err != nil { + return err + } + + if _, ok := m.resourcePolicies[resourceARN]; !ok { + return cerrors.Newf(cerrors.NotFound, "no resource policy is attached to %q", resourceARN) + } + + delete(m.resourcePolicies, resourceARN) + + return nil +} + +// shareableKind validates the ARN and returns its resource kind when it names an +// existing rule group, firewall policy or firewall. +func (m *Mock) shareableKind(arn string) (string, error) { + if err := validateARN("ResourceArn", arn); err != nil { + return "", err + } + + kind := arnKind(arn) + if _, ok := allowedPolicyActions[kind]; !ok { + return "", invalid("ResourceArn %q is not a rule group, firewall policy or firewall", arn) + } + + exists := false + + switch kind { + case "firewall": + _, exists = m.firewallByARN(arn) + case "firewall-policy": + exists = m.policyExists(arn) + default: + _, exists = m.ruleGroupByARN(arn) + } + + if !exists { + return "", cerrors.Newf(cerrors.NotFound, "resource %q not found", arn) + } + + return kind, nil +} + +func (m *Mock) firewallByARN(arn string) (*nfdriver.Firewall, bool) { + return m.lookupFirewall("", arn) +} + +// validatePolicyDocument parses an IAM policy document and checks that every +// statement grants only the allowed actions. +func validatePolicyDocument(doc string, allowed []string) error { + var p struct { + Statement json.RawMessage `json:"Statement"` + } + + if err := json.Unmarshal([]byte(doc), &p); err != nil { + return nfdriver.InvalidResourcePolicy("the policy is not valid JSON: %v", err) + } + + stmts, err := policyStatements(p.Statement) + if err != nil { + return err + } + + if len(stmts) == 0 { + return nfdriver.InvalidResourcePolicy("the policy has no statements") + } + + for _, s := range stmts { + actions, err := stringOrList(s.Action) + if err != nil || len(actions) == 0 { + return nfdriver.InvalidResourcePolicy("every statement needs an Action") + } + + for _, a := range actions { + if !oneOf(a, allowed...) { + return nfdriver.InvalidResourcePolicy("action %q cannot be granted on this resource (allowed: %v)", a, allowed) + } + } + } + + return nil +} + +type policyStatement struct { + Action json.RawMessage `json:"Action"` +} + +func policyStatements(raw json.RawMessage) ([]policyStatement, error) { + if len(raw) == 0 { + return nil, nil + } + + var many []policyStatement + if err := json.Unmarshal(raw, &many); err == nil { + return many, nil + } + + var one policyStatement + if err := json.Unmarshal(raw, &one); err != nil { + return nil, nfdriver.InvalidResourcePolicy("the policy Statement is not valid") + } + + return []policyStatement{one}, nil +} + +func stringOrList(raw json.RawMessage) ([]string, error) { + var many []string + if err := json.Unmarshal(raw, &many); err == nil { + return many, nil + } + + var one string + if err := json.Unmarshal(raw, &one); err != nil { + return nil, err //nolint:wrapcheck // caller only tests for failure + } + + return []string{one}, nil +} diff --git a/providers/aws/networkfirewall/rule_group.go b/providers/aws/networkfirewall/rule_group.go new file mode 100644 index 000000000..f44d9f119 --- /dev/null +++ b/providers/aws/networkfirewall/rule_group.go @@ -0,0 +1,318 @@ +package networkfirewall + +import ( + "context" + "fmt" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/internal/idgen" + nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" +) + +// summaryRuleOptions are the SDK SummaryRuleOption values. +// +//nolint:gochecknoglobals // immutable enum value sets. +var ( + summaryRuleOptions = []string{"SID", "MSG", "METADATA"} + managedTypes = []string{"AWS_MANAGED_THREAT_SIGNATURES", "AWS_MANAGED_DOMAIN_LISTS", "ACTIVE_THREAT_DEFENSE", "PARTNER_MANAGED"} +) + +//nolint:gocritic // cfg matches the driver signature. +func (m *Mock) CreateRuleGroup(_ context.Context, cfg nfdriver.CreateRuleGroupConfig) (*nfdriver.RuleGroup, error) { + m.mu.Lock() + defer m.mu.Unlock() + + if err := validateName("RuleGroupName", cfg.Name); err != nil { + return nil, err + } + + if err := validateRuleGroupBody(cfg.Description, cfg.Tags, cfg.SummaryRuleOptions); err != nil { + return nil, err + } + + if !oneOf(cfg.Type, ruleTypeStateful, ruleTypeStateless, ruleTypeStatefulDomain) { + return nil, invalid("Type must be one of STATELESS, STATEFUL, STATEFUL_DOMAIN") + } + + if cfg.Capacity < 1 { + return nil, invalid("Capacity must be at least 1, got %d", cfg.Capacity) + } + + consumed, err := consumedCapacity(cfg.Rules) + if err != nil { + return nil, err + } + + if consumed > cfg.Capacity { + return nil, invalid("the rules need %d capacity units but Capacity is %d", consumed, cfg.Capacity) + } + + rgKind := "stateful-rulegroup" + if cfg.Type == ruleTypeStateless { + rgKind = "stateless-rulegroup" + } + + rg := &nfdriver.RuleGroup{ + Name: cfg.Name, + ARN: m.arn(rgKind, cfg.Name), + ID: idgen.GenerateID(""), + Type: cfg.Type, + Capacity: cfg.Capacity, + Description: cfg.Description, + Rules: cloneRawJSON(cfg.Rules), + SummaryRuleOptions: cloneStrings(cfg.SummaryRuleOptions), + Status: statusActive, + LastModified: m.now(), + UpdateToken: newToken(), + Tags: copyTags(cfg.Tags), + } + if !m.ruleGroups.SetIfAbsent(ruleGroupKey(cfg.Name, cfg.Type), rg) { + return nil, cerrors.Newf(cerrors.AlreadyExists, "rule group %q already exists", cfg.Name) + } + + return m.outRuleGroup(rg), nil +} + +func validateRuleGroupBody(description string, tags map[string]string, summaryOpts []string) error { + if err := validateDescription(description); err != nil { + return err + } + + if err := validateTags(tags); err != nil { + return err + } + + for _, o := range summaryOpts { + if !oneOf(o, summaryRuleOptions...) { + return invalid("SummaryConfiguration.RuleOptions value %q is not valid", o) + } + } + + return nil +} + +func (m *Mock) DescribeRuleGroup(_ context.Context, name, arn, ruleType string) (*nfdriver.RuleGroup, error) { + m.mu.RLock() + defer m.mu.RUnlock() + + rg, err := m.findRuleGroup(name, arn, ruleType) + if err != nil { + return nil, err + } + + return m.outRuleGroup(rg), nil +} + +//nolint:gocritic // cfg matches the driver signature. +func (m *Mock) UpdateRuleGroup( + _ context.Context, name, arn, ruleType string, cfg nfdriver.UpdateRuleGroupConfig, +) (*nfdriver.RuleGroup, error) { + m.mu.Lock() + defer m.mu.Unlock() + + if err := requireToken(cfg.UpdateToken); err != nil { + return nil, err + } + + rg, err := m.findRuleGroup(name, arn, ruleType) + if err != nil { + return nil, err + } + + if err = checkToken(rg.UpdateToken, cfg.UpdateToken); err != nil { + return nil, err + } + + if err = validateRuleGroupBody(cfg.Description, nil, cfg.SummaryRuleOptions); err != nil { + return nil, err + } + + rules := rg.Rules + if len(cfg.Rules) > 0 { + rules = cfg.Rules + } + + consumed, err := consumedCapacity(rules) + if err != nil { + return nil, err + } + + if consumed > rg.Capacity { + return nil, invalid("the rules need %d capacity units but the rule group capacity is %d", consumed, rg.Capacity) + } + + rg.Description = cfg.Description + rg.Rules = cloneRawJSON(rules) + + if cfg.SummaryRuleOptions != nil { + rg.SummaryRuleOptions = cloneStrings(cfg.SummaryRuleOptions) + } + + rg.LastModified = m.now() + rg.UpdateToken = newToken() + + return m.outRuleGroup(rg), nil +} + +func (m *Mock) DeleteRuleGroup(_ context.Context, name, arn, ruleType string) (*nfdriver.RuleGroup, error) { + m.mu.Lock() + defer m.mu.Unlock() + + rg, err := m.findRuleGroup(name, arn, ruleType) + if err != nil { + return nil, err + } + + if n := m.ruleGroupRefCount(rg.ARN); n > 0 { + return nil, nfdriver.InvalidOperation("rule group %q is referenced by %d firewall policy(ies)", rg.Name, n) + } + + out := m.outRuleGroup(rg) + out.Status = statusDeleting + + m.ruleGroups.Delete(ruleGroupKey(rg.Name, rg.Type)) + delete(m.resourcePolicies, rg.ARN) + + return out, nil +} + +func (m *Mock) ListRuleGroups( + _ context.Context, filter nfdriver.RuleGroupFilter, opts nfdriver.ListOptions, +) ([]nfdriver.RuleGroup, string, error) { + m.mu.RLock() + defer m.mu.RUnlock() + + if err := validateRuleGroupFilter(filter); err != nil { + return nil, "", err + } + + items, next, err := paginate(m.matchRuleGroups(filter), opts) + if err != nil { + return nil, "", err + } + + out := make([]nfdriver.RuleGroup, 0, len(items)) + for _, rg := range items { + out = append(out, *m.outRuleGroup(rg)) + } + + return out, next, nil +} + +func validateRuleGroupFilter(f nfdriver.RuleGroupFilter) error { + if f.Type != "" && !oneOf(f.Type, ruleTypeStateful, ruleTypeStateless, ruleTypeStatefulDomain) { + return invalid("Type %q is not valid", f.Type) + } + + if f.Scope != "" && !oneOf(f.Scope, "MANAGED", "ACCOUNT") { + return invalid("Scope %q is not valid", f.Scope) + } + + if f.ManagedType != "" && !oneOf(f.ManagedType, managedTypes...) { + return invalid("ManagedType %q is not valid", f.ManagedType) + } + + return nil +} + +// matchRuleGroups returns the rule groups a filter selects, in name order. +// Every rule group the emulator holds is account-owned, so a MANAGED scope or a +// ManagedType selects none. +func (m *Mock) matchRuleGroups(f nfdriver.RuleGroupFilter) []*nfdriver.RuleGroup { + if f.Scope == "MANAGED" || f.ManagedType != "" { + return nil + } + + var matched []*nfdriver.RuleGroup + + for _, rg := range m.ruleGroups.SortedValues() { + if f.Type == "" || rg.Type == f.Type { + matched = append(matched, rg) + } + } + + return matched +} + +// findRuleGroup resolves a rule group by name+type or by ARN, validating the +// identifier first. +func (m *Mock) findRuleGroup(name, arn, ruleType string) (*nfdriver.RuleGroup, error) { + if err := requireIdentifier("RuleGroup", name, arn); err != nil { + return nil, err + } + + if name != "" && ruleType != "" && !oneOf(ruleType, ruleTypeStateful, ruleTypeStateless, ruleTypeStatefulDomain) { + return nil, invalid("Type %q is not valid", ruleType) + } + + rg, ok := m.lookupRuleGroup(name, arn, ruleType) + if !ok { + return nil, cerrors.Newf(cerrors.NotFound, "rule group %q not found", nonEmpty(name, arn)) + } + + return rg, nil +} + +func (m *Mock) lookupRuleGroup(name, arn, ruleType string) (*nfdriver.RuleGroup, bool) { + if name != "" && ruleType != "" { + return m.ruleGroups.Get(ruleGroupKey(name, ruleType)) + } + + for _, rg := range m.ruleGroups.SortedValues() { + if (name != "" && rg.Name == name) || (arn != "" && rg.ARN == arn) { + return rg, true + } + } + + return nil, false +} + +func (m *Mock) ruleGroupByARN(arn string) (*nfdriver.RuleGroup, bool) { + for _, rg := range m.ruleGroups.SortedValues() { + if rg.ARN == arn { + return rg, true + } + } + + return nil, false +} + +// ruleGroupRefCount counts the firewall policies that reference the rule group. +func (m *Mock) ruleGroupRefCount(arn string) int { + n := 0 + + for _, p := range m.policies.SortedValues() { + if refsContain(p.StatefulRuleGroupReferences, arn) || refsContain(p.StatelessRuleGroupReferences, arn) { + n++ + } + } + + return n +} + +func refsContain(refs []nfdriver.RuleGroupReference, arn string) bool { + for _, r := range refs { + if r.ResourceARN == arn { + return true + } + } + + return false +} + +func ruleGroupKey(name, ruleType string) string { + return fmt.Sprintf("%s/%s", ruleType, name) +} + +// outRuleGroup returns a deep copy of the stored rule group with the computed +// consumed capacity and association count filled in. +func (m *Mock) outRuleGroup(rg *nfdriver.RuleGroup) *nfdriver.RuleGroup { + out := *rg + out.Rules = cloneRawJSON(rg.Rules) + out.SummaryRuleOptions = cloneStrings(rg.SummaryRuleOptions) + out.Tags = copyTags(rg.Tags) + out.NumberOfAssociations = m.ruleGroupRefCount(rg.ARN) + out.ConsumedCapacity, _ = consumedCapacity(rg.Rules) + + return &out +} diff --git a/providers/aws/networkfirewall/snapshot.go b/providers/aws/networkfirewall/snapshot.go index 684d66868..0a355e5b1 100644 --- a/providers/aws/networkfirewall/snapshot.go +++ b/providers/aws/networkfirewall/snapshot.go @@ -13,38 +13,57 @@ var _ snapshot.Snapshottable = (*Mock)(nil) // networkfirewallSnapshot is the full serialized state of the AWS Network // Firewall mock. Every memstore store holds a fully-exported *nfdriver pointer -// type (Firewall, FirewallPolicy, RuleGroup), so each round-trips through the -// generic memstore helper under its exact key: firewalls/policies by name, rule -// groups by the "TYPE/name" composite key, so DescribeRuleGroup's type-qualified -// lookup keeps working after a restore. The mu-guarded logging map (firewall -// name -> log types) is captured beside the stores. The wired opts is -// intentionally not serialized. +// type, so each round-trips through the generic memstore helper under its exact +// key: firewalls/policies/TLS configs by name, rule groups by the "TYPE/name" +// composite key (so DescribeRuleGroup's type-qualified lookup keeps working +// after a restore), VPC endpoint associations by ARN, flow operations and +// analysis reports by id. The mu-guarded logging and resource-policy maps are +// captured beside the stores. The wired opts is intentionally not serialized. +// Fields added after the first release are additive, so older snapshots load. type networkfirewallSnapshot struct { - Firewalls json.RawMessage `json:"firewalls,omitempty"` - Policies json.RawMessage `json:"policies,omitempty"` - RuleGroups json.RawMessage `json:"ruleGroups,omitempty"` - Logging map[string][]nfdriver.LogDestinationConfig `json:"logging,omitempty"` + Firewalls json.RawMessage `json:"firewalls,omitempty"` + Policies json.RawMessage `json:"policies,omitempty"` + RuleGroups json.RawMessage `json:"ruleGroups,omitempty"` + TLSConfigs json.RawMessage `json:"tlsConfigs,omitempty"` + Associations json.RawMessage `json:"associations,omitempty"` + FlowOperations json.RawMessage `json:"flowOperations,omitempty"` + AnalysisReports json.RawMessage `json:"analysisReports,omitempty"` + // Logging maps a firewall name to its logging configuration. Snapshots + // written before the monitoring-dashboard switch stored a bare destination + // array per firewall; Restore accepts both shapes. + Logging map[string]json.RawMessage `json:"logging,omitempty"` + ResourcePolicies map[string]string `json:"resourcePolicies,omitempty"` } // Snapshot captures the mock's entire state as JSON. includeAssets is unused. Network Firewall is // control-plane only and holds no bulk object bodies. func (m *Mock) Snapshot(_ context.Context, _ bool) (json.RawMessage, error) { + m.mu.RLock() + defer m.mu.RUnlock() + var snap networkfirewallSnapshot if err := m.snapshotStores(&snap); err != nil { return nil, err } - m.mu.RLock() - logging := make(map[string][]nfdriver.LogDestinationConfig, len(m.logging)) + if len(m.logging) > 0 { + snap.Logging = make(map[string]json.RawMessage, len(m.logging)) - for name, configs := range m.logging { - logging[name] = cloneLogConfigs(configs) - } + for name, cfg := range m.logging { + b, err := json.Marshal(cfg) + if err != nil { + return nil, fmt.Errorf("networkfirewall: snapshot logging: %w", err) + } - m.mu.RUnlock() + snap.Logging[name] = b + } + } - if len(logging) > 0 { - snap.Logging = logging + if len(m.resourcePolicies) > 0 { + snap.ResourcePolicies = make(map[string]string, len(m.resourcePolicies)) + for arn, p := range m.resourcePolicies { + snap.ResourcePolicies[arn] = p + } } return json.Marshal(snap) @@ -58,6 +77,10 @@ func (m *Mock) snapshotStores(snap *networkfirewallSnapshot) error { {&snap.Firewalls, m.firewalls.Snapshot}, {&snap.Policies, m.policies.Snapshot}, {&snap.RuleGroups, m.ruleGroups.Snapshot}, + {&snap.TLSConfigs, m.tlsConfigs.Snapshot}, + {&snap.Associations, m.associations.Snapshot}, + {&snap.FlowOperations, m.flowOperations.Snapshot}, + {&snap.AnalysisReports, m.analysisReports.Snapshot}, } for _, d := range dumps { @@ -73,27 +96,66 @@ func (m *Mock) snapshotStores(snap *networkfirewallSnapshot) error { } // Restore rebuilds the mock's state under the original identities: every -// firewall/policy/rule-group name and ARN (and the ARN cross-references records -// hold) is preserved, so a restore is transparent to clients. +// resource name and ARN (and the ARN cross-references records hold) is +// preserved, so a restore is transparent to clients. State written before update +// tokens existed gets a freshly minted token per resource. func (m *Mock) Restore(_ context.Context, data json.RawMessage) error { var snap networkfirewallSnapshot if err := json.Unmarshal(data, &snap); err != nil { return fmt.Errorf("networkfirewall: parse snapshot: %w", err) } + m.mu.Lock() + defer m.mu.Unlock() + if err := m.restoreStores(&snap); err != nil { return err } - m.mu.Lock() - if snap.Logging != nil { - m.logging = snap.Logging + logging, err := decodeLogging(snap.Logging) + if err != nil { + return err } - m.mu.Unlock() + + if logging != nil { + m.logging = logging + } + + if snap.ResourcePolicies != nil { + m.resourcePolicies = snap.ResourcePolicies + } + + m.backfillRestoredState() return nil } +// decodeLogging parses the snapshot's logging map, accepting the current object +// shape and the older bare destination array. +func decodeLogging(raw map[string]json.RawMessage) (map[string]*nfdriver.LoggingConfiguration, error) { + if raw == nil { + return nil, nil //nolint:nilnil // nil means "no logging in the snapshot" + } + + out := make(map[string]*nfdriver.LoggingConfiguration, len(raw)) + + for name, b := range raw { + var cfg nfdriver.LoggingConfiguration + + if len(b) > 0 && b[0] == '[' { + if err := json.Unmarshal(b, &cfg.Destinations); err != nil { + return nil, fmt.Errorf("networkfirewall: restore logging: %w", err) + } + } else if err := json.Unmarshal(b, &cfg); err != nil { + return nil, fmt.Errorf("networkfirewall: restore logging: %w", err) + } + + out[name] = &cfg + } + + return out, nil +} + func (m *Mock) restoreStores(snap *networkfirewallSnapshot) error { loads := []struct { src json.RawMessage @@ -102,6 +164,10 @@ func (m *Mock) restoreStores(snap *networkfirewallSnapshot) error { {snap.Firewalls, m.firewalls.LoadSnapshot}, {snap.Policies, m.policies.LoadSnapshot}, {snap.RuleGroups, m.ruleGroups.LoadSnapshot}, + {snap.TLSConfigs, m.tlsConfigs.LoadSnapshot}, + {snap.Associations, m.associations.LoadSnapshot}, + {snap.FlowOperations, m.flowOperations.LoadSnapshot}, + {snap.AnalysisReports, m.analysisReports.LoadSnapshot}, } for _, l := range loads { @@ -116,3 +182,38 @@ func (m *Mock) restoreStores(snap *networkfirewallSnapshot) error { return nil } + +// backfillRestoredState gives state saved before this release what it lacks: an +// update token and an ACTIVE status on every resource that has them. +func (m *Mock) backfillRestoredState() { + for _, f := range m.firewalls.SortedValues() { + ensureToken(&f.UpdateToken) + } + + for _, p := range m.policies.SortedValues() { + ensureToken(&p.UpdateToken) + ensureActive(&p.Status) + } + + for _, rg := range m.ruleGroups.SortedValues() { + ensureToken(&rg.UpdateToken) + ensureActive(&rg.Status) + } + + for _, t := range m.tlsConfigs.SortedValues() { + ensureToken(&t.UpdateToken) + ensureActive(&t.Status) + } +} + +func ensureToken(token *string) { + if *token == "" { + *token = newToken() + } +} + +func ensureActive(status *string) { + if *status == "" { + *status = statusActive + } +} diff --git a/providers/aws/networkfirewall/snapshot_test.go b/providers/aws/networkfirewall/snapshot_test.go index 109a50fb7..fcefe29de 100644 --- a/providers/aws/networkfirewall/snapshot_test.go +++ b/providers/aws/networkfirewall/snapshot_test.go @@ -1,72 +1,115 @@ package networkfirewall import ( - "context" + "encoding/json" "testing" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" ) // TestSnapshotRoundTripNetworkFirewall proves a snapshot/restore round-trip -// preserves the firewall/policy/rule-group stores and the mu-guarded logging map -// under their original identities. +// preserves every store, the logging and resource-policy maps and the update +// tokens under their original identities. func TestSnapshotRoundTripNetworkFirewall(t *testing.T) { - ctx := context.Background() src := newMock() + pol, fw := seedStack(t, src) + + _, err := src.CreateRuleGroup(bg, nfdriver.CreateRuleGroupConfig{Name: "rg-1", Type: "STATEFUL", Capacity: 100}) + require.NoError(t, err) - pol, err := src.CreateFirewallPolicy(ctx, nfdriver.CreateFirewallPolicyConfig{ - Name: "pol-1", Description: "policy one", + _, err = src.CreateTLSInspectionConfiguration(bg, nfdriver.CreateTLSInspectionConfig{ + Name: "tls-1", Configuration: tlsConfigJSON("arn:aws:acm:us-east-1:000000000000:certificate/c1"), }) - if err != nil { - t.Fatalf("create policy: %v", err) - } + require.NoError(t, err) - fw, err := src.CreateFirewall(ctx, nfdriver.CreateFirewallConfig{ - Name: "fw-1", VPCID: "vpc-1", SubnetIDs: []string{"subnet-1"}, PolicyARN: pol.ARN, + assoc, err := src.CreateVpcEndpointAssociation(bg, nfdriver.CreateVPCEndpointAssociationConfig{ + FirewallARN: fw.ARN, VPCID: "vpc-2b", SubnetID: "subnet-9", }) - if err != nil { - t.Fatalf("create firewall: %v", err) - } + require.NoError(t, err) - if _, err := src.CreateRuleGroup(ctx, nfdriver.CreateRuleGroupConfig{ - Name: "rg-1", Type: "STATEFUL", Capacity: 100, - }); err != nil { - t.Fatalf("create rule group: %v", err) - } + require.NoError(t, src.UpdateLoggingConfiguration(bg, "fw-1", &nfdriver.LoggingConfiguration{ + Destinations: []nfdriver.LogDestinationConfig{s3Dest("FLOW", "b")}, + })) + require.NoError(t, src.PutResourcePolicy(bg, pol.ARN, policyDoc("network-firewall:AssociateFirewallPolicy"))) - if err := src.UpdateLoggingConfiguration(ctx, "fw-1", logConfigs("FLOW", "ALERT")); err != nil { - t.Fatalf("update logging: %v", err) - } + flow, err := src.StartFlowFlush(bg, nfdriver.FlowOperationRequest{FirewallARN: fw.ARN, AvailabilityZone: "us-east-1a"}) + require.NoError(t, err) - raw, err := src.Snapshot(ctx, true) - if err != nil { - t.Fatalf("snapshot: %v", err) - } + _, err = src.UpdateFirewallAnalysisSettings(bg, "fw-1", []string{"TLS_SNI"}, "") + require.NoError(t, err) + + rep, err := src.StartAnalysisReport(bg, "fw-1", "", "TLS_SNI") + require.NoError(t, err) + + raw, err := src.Snapshot(bg, true) + require.NoError(t, err) dst := newMock() - if err := dst.Restore(ctx, raw); err != nil { - t.Fatalf("restore: %v", err) - } + require.NoError(t, dst.Restore(bg, raw)) - gotFw, err := dst.DescribeFirewall(ctx, "fw-1", "") - if err != nil || gotFw.ARN != fw.ARN || gotFw.PolicyARN != pol.ARN { - t.Fatalf("restored firewall = %+v, err %v", gotFw, err) - } + gotFw, err := dst.DescribeFirewall(bg, "fw-1", "") + require.NoError(t, err) + assert.Equal(t, pol.ARN, gotFw.PolicyARN) + assert.Equal(t, 1, gotFw.NumberOfAssociations) - gotPol, err := dst.DescribeFirewallPolicy(ctx, "pol-1", "") - if err != nil || gotPol.Description != "policy one" { - t.Fatalf("restored policy = %+v, err %v", gotPol, err) - } + srcFw, _ := src.DescribeFirewall(bg, "fw-1", "") + assert.Equal(t, srcFw.UpdateToken, gotFw.UpdateToken, "tokens survive a restore") - // Rule group keyed by "TYPE/name" resolves through the type-qualified lookup. - gotRg, err := dst.DescribeRuleGroup(ctx, "rg-1", "", "STATEFUL") - if err != nil || gotRg.Capacity != 100 { - t.Fatalf("restored rule group = %+v, err %v", gotRg, err) - } + gotRg, err := dst.DescribeRuleGroup(bg, "rg-1", "", "STATEFUL") + require.NoError(t, err) + assert.Equal(t, 100, gotRg.Capacity) + + _, err = dst.DescribeTLSInspectionConfiguration(bg, "tls-1", "") + require.NoError(t, err) + + _, err = dst.DescribeVpcEndpointAssociation(bg, assoc.ARN) + require.NoError(t, err) + + logs, err := dst.DescribeLoggingConfiguration(bg, "fw-1") + require.NoError(t, err) + require.Len(t, logs.Destinations, 1) + assert.Equal(t, "FLOW", logs.Destinations[0].LogType) + + policy, err := dst.DescribeResourcePolicy(bg, pol.ARN) + require.NoError(t, err) + assert.Contains(t, policy, "AssociateFirewallPolicy") + + _, err = dst.DescribeFlowOperation(bg, nfdriver.FlowOperationScope{FirewallARN: fw.ARN, AvailabilityZone: "us-east-1a"}, flow.ID) + require.NoError(t, err) - // Logging (mu-guarded) survived. - logs, err := dst.DescribeLoggingConfiguration(ctx, "fw-1") - if err != nil || len(logs) != 2 || logs[0].LogType != "FLOW" || logs[1].LogType != "ALERT" { - t.Fatalf("restored logging = %+v, err %v", logs, err) + _, err = dst.GetAnalysisReport(bg, "fw-1", "", rep.ID) + require.NoError(t, err) +} + +// TestRestoreLegacySnapshot loads state written before update tokens, status +// fields and the logging-configuration object existed. +func TestRestoreLegacySnapshot(t *testing.T) { + legacy := map[string]any{ + "firewalls": json.RawMessage(`{"fw-old":{"Name":"fw-old","ARN":"arn:aws:network-firewall:us-east-1:000000000000:firewall/fw-old","VPCID":"vpc-1","Status":"READY"}}`), + "policies": json.RawMessage(`{"pol-old":{"Name":"pol-old","ARN":"arn:aws:network-firewall:us-east-1:000000000000:firewall-policy/pol-old"}}`), + "logging": json.RawMessage(`{"fw-old":[{"LogType":"FLOW","LogDestinationType":"S3","LogDestination":{"bucketName":"b"}}]}`), } + raw, err := json.Marshal(legacy) + require.NoError(t, err) + + m := newMock() + require.NoError(t, m.Restore(bg, raw)) + + fw, err := m.DescribeFirewall(bg, "fw-old", "") + require.NoError(t, err) + assert.Regexp(t, `^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$`, fw.UpdateToken) + assert.False(t, fw.SubnetChangeProtection) + + pol, err := m.DescribeFirewallPolicy(bg, "pol-old", "") + require.NoError(t, err) + assert.NotEmpty(t, pol.UpdateToken) + assert.Equal(t, "ACTIVE", pol.Status) + + logs, err := m.DescribeLoggingConfiguration(bg, "fw-old") + require.NoError(t, err) + require.Len(t, logs.Destinations, 1) + assert.Equal(t, "S3", logs.Destinations[0].LogDestinationType) } diff --git a/providers/aws/networkfirewall/tags.go b/providers/aws/networkfirewall/tags.go new file mode 100644 index 000000000..1ddf8d1e5 --- /dev/null +++ b/providers/aws/networkfirewall/tags.go @@ -0,0 +1,131 @@ +package networkfirewall + +import ( + "context" + + cerrors "github.com/stackshy/cloudemu/v2/errors" +) + +// TagResource adds tags to any taggable resource (firewall, policy, rule group, +// TLS inspection configuration, VPC endpoint association) by ARN. Tagging does +// not rotate the resource's update token. +func (m *Mock) TagResource(_ context.Context, arn string, tags map[string]string) error { + m.mu.Lock() + defer m.mu.Unlock() + + if err := validateARN("ResourceArn", arn); err != nil { + return err + } + + t := m.taggableByARN(arn) + if t == nil { + return cerrors.Newf(cerrors.NotFound, "resource %q not found", arn) + } + + if len(t)+len(tags) > maxTags { + return invalid("at most %d tags are allowed", maxTags) + } + + for k, v := range tags { + t[k] = v + } + + return nil +} + +// UntagResource removes tags from a resource by ARN. +func (m *Mock) UntagResource(_ context.Context, arn string, keys []string) error { + m.mu.Lock() + defer m.mu.Unlock() + + if err := validateARN("ResourceArn", arn); err != nil { + return err + } + + t := m.taggableByARN(arn) + if t == nil { + return cerrors.Newf(cerrors.NotFound, "resource %q not found", arn) + } + + for _, k := range keys { + delete(t, k) + } + + return nil +} + +// ListTagsForResource returns a copy of the tags on the resource with the given ARN. +func (m *Mock) ListTagsForResource(_ context.Context, arn string) (map[string]string, error) { + m.mu.Lock() + defer m.mu.Unlock() + + if err := validateARN("ResourceArn", arn); err != nil { + return nil, err + } + + t := m.taggableByARN(arn) + if t == nil { + return nil, cerrors.Newf(cerrors.NotFound, "resource %q not found", arn) + } + + return copyTags(t), nil +} + +// taggableByARN returns the mutable Tags map of the resource with the given ARN, +// creating it if nil, or nil when no resource matches. +func (m *Mock) taggableByARN(arn string) map[string]string { + for _, f := range m.firewalls.SortedValues() { + if f.ARN == arn { + if f.Tags == nil { + f.Tags = map[string]string{} + } + + return f.Tags + } + } + + for _, p := range m.policies.SortedValues() { + if p.ARN == arn { + if p.Tags == nil { + p.Tags = map[string]string{} + } + + return p.Tags + } + } + + for _, rg := range m.ruleGroups.SortedValues() { + if rg.ARN == arn { + if rg.Tags == nil { + rg.Tags = map[string]string{} + } + + return rg.Tags + } + } + + return m.taggableExtra(arn) +} + +// taggableExtra covers the resource families added after the original three. +func (m *Mock) taggableExtra(arn string) map[string]string { + for _, t := range m.tlsConfigs.SortedValues() { + if t.ARN == arn { + if t.Tags == nil { + t.Tags = map[string]string{} + } + + return t.Tags + } + } + + if a, ok := m.associations.Get(arn); ok { + if a.Tags == nil { + a.Tags = map[string]string{} + } + + return a.Tags + } + + return nil +} diff --git a/providers/aws/networkfirewall/tls_inspection.go b/providers/aws/networkfirewall/tls_inspection.go new file mode 100644 index 000000000..e0c78629d --- /dev/null +++ b/providers/aws/networkfirewall/tls_inspection.go @@ -0,0 +1,226 @@ +package networkfirewall + +import ( + "context" + "encoding/json" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/internal/idgen" + nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" +) + +// tlsPayload is the part of a TLSInspectionConfiguration the emulator reads: the +// server certificate ARNs it reports back as Certificates. The payload itself +// is stored untouched. +type tlsPayload struct { + ServerCertificateConfigurations []struct { + ServerCertificates []struct { + ResourceArn string `json:"ResourceArn"` + } `json:"ServerCertificates"` + } `json:"ServerCertificateConfigurations"` +} + +func (m *Mock) CreateTLSInspectionConfiguration( + _ context.Context, cfg nfdriver.CreateTLSInspectionConfig, +) (*nfdriver.TLSInspectionConfig, error) { + m.mu.Lock() + defer m.mu.Unlock() + + if err := validateName("TLSInspectionConfigurationName", cfg.Name); err != nil { + return nil, err + } + + if err := validateDescription(cfg.Description); err != nil { + return nil, err + } + + if err := validateTags(cfg.Tags); err != nil { + return nil, err + } + + if _, err := tlsCertificates(cfg.Configuration); err != nil { + return nil, err + } + + t := &nfdriver.TLSInspectionConfig{ + Name: cfg.Name, + ARN: m.arn("tls-configuration", cfg.Name), + ID: idgen.GenerateID(""), + Description: cfg.Description, + Configuration: cloneRawJSON(cfg.Configuration), + Status: statusActive, + LastModified: m.now(), + UpdateToken: newToken(), + Tags: copyTags(cfg.Tags), + } + if !m.tlsConfigs.SetIfAbsent(cfg.Name, t) { + return nil, cerrors.Newf(cerrors.AlreadyExists, "TLS inspection configuration %q already exists", cfg.Name) + } + + return m.outTLS(t), nil +} + +func (m *Mock) DescribeTLSInspectionConfiguration(_ context.Context, name, arn string) (*nfdriver.TLSInspectionConfig, error) { + m.mu.RLock() + defer m.mu.RUnlock() + + t, err := m.findTLS(name, arn) + if err != nil { + return nil, err + } + + return m.outTLS(t), nil +} + +func (m *Mock) UpdateTLSInspectionConfiguration( + _ context.Context, name, arn string, cfg nfdriver.UpdateTLSInspectionConfig, +) (*nfdriver.TLSInspectionConfig, error) { + m.mu.Lock() + defer m.mu.Unlock() + + if err := requireToken(cfg.UpdateToken); err != nil { + return nil, err + } + + t, err := m.findTLS(name, arn) + if err != nil { + return nil, err + } + + if err := checkToken(t.UpdateToken, cfg.UpdateToken); err != nil { + return nil, err + } + + if err := validateDescription(cfg.Description); err != nil { + return nil, err + } + + if _, err := tlsCertificates(cfg.Configuration); err != nil { + return nil, err + } + + t.Description = cfg.Description + t.Configuration = cloneRawJSON(cfg.Configuration) + t.LastModified = m.now() + t.UpdateToken = newToken() + + return m.outTLS(t), nil +} + +func (m *Mock) DeleteTLSInspectionConfiguration(_ context.Context, name, arn string) (*nfdriver.TLSInspectionConfig, error) { + m.mu.Lock() + defer m.mu.Unlock() + + t, err := m.findTLS(name, arn) + if err != nil { + return nil, err + } + + if n := m.tlsRefCount(t.ARN); n > 0 { + return nil, nfdriver.InvalidOperation("TLS inspection configuration %q is used by %d firewall policy(ies)", t.Name, n) + } + + out := m.outTLS(t) + out.Status = statusDeleting + + m.tlsConfigs.Delete(t.Name) + + return out, nil +} + +func (m *Mock) ListTLSInspectionConfigurations( + _ context.Context, opts nfdriver.ListOptions, +) ([]nfdriver.TLSInspectionConfig, string, error) { + m.mu.RLock() + defer m.mu.RUnlock() + + items, next, err := paginate(m.tlsConfigs.SortedValues(), opts) + if err != nil { + return nil, "", err + } + + out := make([]nfdriver.TLSInspectionConfig, 0, len(items)) + for _, t := range items { + out = append(out, *m.outTLS(t)) + } + + return out, next, nil +} + +func (m *Mock) findTLS(name, arn string) (*nfdriver.TLSInspectionConfig, error) { + if err := requireIdentifier("TLSInspectionConfiguration", name, arn); err != nil { + return nil, err + } + + if name != "" { + if t, ok := m.tlsConfigs.Get(name); ok { + return t, nil + } + } else if t, ok := m.tlsByARN(arn); ok { + return t, nil + } + + return nil, cerrors.Newf(cerrors.NotFound, "TLS inspection configuration %q not found", nonEmpty(name, arn)) +} + +func (m *Mock) tlsByARN(arn string) (*nfdriver.TLSInspectionConfig, bool) { + for _, t := range m.tlsConfigs.SortedValues() { + if t.ARN == arn { + return t, true + } + } + + return nil, false +} + +// tlsRefCount counts the firewall policies that use the TLS inspection configuration. +func (m *Mock) tlsRefCount(arn string) int { + n := 0 + + for _, p := range m.policies.SortedValues() { + if p.TLSInspectionConfigurationARN == arn { + n++ + } + } + + return n +} + +func (m *Mock) outTLS(t *nfdriver.TLSInspectionConfig) *nfdriver.TLSInspectionConfig { + out := *t + out.Configuration = cloneRawJSON(t.Configuration) + out.Tags = copyTags(t.Tags) + out.NumberOfAssociations = m.tlsRefCount(t.ARN) + out.CertificateARNs, _ = tlsCertificates(t.Configuration) + + return &out +} + +// tlsCertificates validates a TLSInspectionConfiguration payload (at least one +// server certificate configuration) and returns the server certificate ARNs. +func tlsCertificates(raw json.RawMessage) ([]string, error) { + if len(raw) == 0 { + return nil, invalid("TLSInspectionConfiguration is required") + } + + var p tlsPayload + if err := json.Unmarshal(raw, &p); err != nil { + return nil, invalid("TLSInspectionConfiguration is not valid: %v", err) + } + + if len(p.ServerCertificateConfigurations) == 0 { + return nil, invalid("TLSInspectionConfiguration needs at least one ServerCertificateConfiguration") + } + + var arns []string + + for _, c := range p.ServerCertificateConfigurations { + for _, s := range c.ServerCertificates { + if s.ResourceArn != "" { + arns = append(arns, s.ResourceArn) + } + } + } + + return arns, nil +} diff --git a/providers/aws/networkfirewall/token.go b/providers/aws/networkfirewall/token.go new file mode 100644 index 000000000..10ee49ad0 --- /dev/null +++ b/providers/aws/networkfirewall/token.go @@ -0,0 +1,25 @@ +package networkfirewall + +import nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" + +// checkToken compares the update token a caller sent with the resource's +// current one. An empty token is an unconditional update (allowed only where +// AWS marks the token optional; use requireToken where it is required). A stale +// token fails with InvalidTokenException, as in real Network Firewall. +func checkToken(current, supplied string) error { + if supplied != "" && supplied != current { + return nfdriver.InvalidToken("the update token is stale; describe the resource again to get a current token") + } + + return nil +} + +// requireToken fails with a request error when a token AWS marks required is +// missing (UpdateFirewallPolicy, UpdateRuleGroup, UpdateTLSInspectionConfiguration). +func requireToken(supplied string) error { + if supplied == "" { + return invalid("UpdateToken is required") + } + + return nil +} diff --git a/providers/aws/networkfirewall/token_test.go b/providers/aws/networkfirewall/token_test.go new file mode 100644 index 000000000..afd49a156 --- /dev/null +++ b/providers/aws/networkfirewall/token_test.go @@ -0,0 +1,324 @@ +package networkfirewall + +import ( + "sync" + "sync/atomic" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" +) + +const uuidPattern = `^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$` + +func TestToken_MintedAtCreate_UUID_StableAcrossReads(t *testing.T) { + m := newMock() + pol, fw := seedStack(t, m) + rg := seedRuleGroup(t, m, "rg-1", "STATEFUL", 10) + + assert.Regexp(t, uuidPattern, fw.UpdateToken) + assert.Regexp(t, uuidPattern, pol.UpdateToken) + assert.Regexp(t, uuidPattern, rg.UpdateToken) + + for i := 0; i < 3; i++ { + got, err := m.DescribeFirewall(bg, "fw-1", "") + require.NoError(t, err) + assert.Equal(t, fw.UpdateToken, got.UpdateToken) + } + + assert.NotEqual(t, pol.UpdateToken, rg.UpdateToken) +} + +func TestToken_RotatesOnEveryMutation(t *testing.T) { + m := newMock() + pol, fw := seedStack(t, m) + + other := seedPolicy(t, m, "pol-2") + before := fw.UpdateToken + + updated, err := m.UpdateFirewallDeleteProtection(bg, "fw-1", true, "") + require.NoError(t, err) + assert.NotEqual(t, before, updated.UpdateToken) + + again, err := m.UpdateFirewallDeleteProtection(bg, "fw-1", false, updated.UpdateToken) + require.NoError(t, err) + assert.NotEqual(t, updated.UpdateToken, again.UpdateToken) + + // Other resources are untouched by a firewall mutation. + gotPol, _ := m.DescribeFirewallPolicy(bg, "pol-1", "") + assert.Equal(t, pol.UpdateToken, gotPol.UpdateToken) + + gotOther, _ := m.DescribeFirewallPolicy(bg, "pol-2", "") + assert.Equal(t, other.UpdateToken, gotOther.UpdateToken) +} + +func TestToken_StaleRejected_InvalidTokenException(t *testing.T) { + m := newMock() + pol, fw := seedStack(t, m) + + _, err := m.UpdateFirewallDeleteProtection(bg, "fw-1", true, "") + require.NoError(t, err) + + // The token read at create time is now stale. + _, err = m.UpdateFirewallDeleteProtection(bg, "fw-1", false, fw.UpdateToken) + requireException(t, err, nfdriver.ExInvalidToken) + + got, _ := m.DescribeFirewall(bg, "fw-1", "") + assert.True(t, got.DeleteProtection, "a rejected update changes nothing") + + _, err = m.UpdateFirewallPolicy(bg, "pol-1", "", nfdriver.UpdateFirewallPolicyConfig{ + UpdateToken: "00000000-0000-0000-0000-000000000000", StatelessDefaultActions: standardActions, + StatelessFragmentDefaultActions: standardActions, + }) + requireException(t, err, nfdriver.ExInvalidToken) + + gotPol, _ := m.DescribeFirewallPolicy(bg, "pol-1", "") + assert.Equal(t, pol.UpdateToken, gotPol.UpdateToken) +} + +func TestToken_RequiredOnPolicyAndRuleGroupUpdate(t *testing.T) { + m := newMock() + seedPolicy(t, m, "pol-1") + seedRuleGroup(t, m, "rg-1", "STATEFUL", 10) + + _, err := m.UpdateFirewallPolicy(bg, "pol-1", "", nfdriver.UpdateFirewallPolicyConfig{ + StatelessDefaultActions: standardActions, StatelessFragmentDefaultActions: standardActions, + }) + assert.True(t, cerrors.IsInvalidArgument(err), "policy update without token: %v", err) + + _, err = m.UpdateRuleGroup(bg, "rg-1", "", "STATEFUL", nfdriver.UpdateRuleGroupConfig{Description: "d"}) + assert.True(t, cerrors.IsInvalidArgument(err), "rule group update without token: %v", err) +} + +func TestToken_OptionalOnFirewallUpdates(t *testing.T) { + m := newMock() + _, fw := seedStack(t, m) + + _, err := m.UpdateFirewallDeleteProtection(bg, "fw-1", true, "") + require.NoError(t, err, "omitted token = unconditional") + + cur, _ := m.DescribeFirewall(bg, "fw-1", "") + + _, err = m.UpdateFirewallDeleteProtection(bg, "fw-1", false, cur.UpdateToken) + require.NoError(t, err, "matching token succeeds") + + _ = fw +} + +func TestToken_TagOpsDoNotRotate(t *testing.T) { + m := newMock() + _, fw := seedStack(t, m) + + require.NoError(t, m.TagResource(bg, fw.ARN, map[string]string{"a": "b"})) + + got, _ := m.DescribeFirewall(bg, "fw-1", "") + assert.Equal(t, fw.UpdateToken, got.UpdateToken) +} + +func TestToken_Race_50Writers_ExactlyOneWins(t *testing.T) { + m := newMock() + _, fw := seedStack(t, m) + + var ( + wg sync.WaitGroup + wins atomic.Int32 + invalid atomic.Int32 + ) + + for i := 0; i < 50; i++ { + wg.Add(1) + + go func() { + defer wg.Done() + + _, err := m.UpdateFirewallDeleteProtection(bg, "fw-1", true, fw.UpdateToken) + + switch ex, _ := nfdriver.ExceptionOf(err); { + case err == nil: + wins.Add(1) + case ex == nfdriver.ExInvalidToken: + invalid.Add(1) + } + }() + } + + wg.Wait() + + assert.EqualValues(t, 1, wins.Load()) + assert.EqualValues(t, 49, invalid.Load()) +} + +func TestChangeProtection_DefaultsFalse_EchoedOnDescribe(t *testing.T) { + m := newMock() + pol := seedPolicy(t, m, "pol-1") + + plain := seedFirewall(t, m, "fw-plain", pol.ARN) + assert.False(t, plain.SubnetChangeProtection || plain.FirewallPolicyChangeProtection || plain.AvailabilityZoneChangeProtection) + + _, err := m.CreateFirewall(bg, nfdriver.CreateFirewallConfig{ + Name: "fw-prot", PolicyARN: pol.ARN, VPCID: "vpc-1a", SubnetChangeProtection: true, + FirewallPolicyChangeProtection: true, AvailabilityZoneChangeProtection: true, + }) + require.NoError(t, err) + + got, _ := m.DescribeFirewall(bg, "fw-prot", "") + assert.True(t, got.SubnetChangeProtection && got.FirewallPolicyChangeProtection && got.AvailabilityZoneChangeProtection) +} + +func TestChangeProtection_Update_Toggles(t *testing.T) { + m := newMock() + seedStack(t, m) + + fw, err := m.UpdateSubnetChangeProtection(bg, "fw-1", true, "") + require.NoError(t, err) + assert.True(t, fw.SubnetChangeProtection) + assert.False(t, fw.FirewallPolicyChangeProtection) + assert.False(t, fw.AvailabilityZoneChangeProtection) + + fw, err = m.UpdateFirewallPolicyChangeProtection(bg, "fw-1", true, fw.UpdateToken) + require.NoError(t, err) + assert.True(t, fw.SubnetChangeProtection && fw.FirewallPolicyChangeProtection) + + fw, err = m.UpdateAvailabilityZoneChangeProtection(bg, "fw-1", true, "") + require.NoError(t, err) + assert.True(t, fw.AvailabilityZoneChangeProtection) + + fw, err = m.UpdateSubnetChangeProtection(bg, "fw-1", false, "") + require.NoError(t, err) + assert.False(t, fw.SubnetChangeProtection) +} + +func TestChangeProtection_BlocksSubnetAssociateDisassociate(t *testing.T) { + m := newMock() + seedStack(t, m) + + _, err := m.UpdateSubnetChangeProtection(bg, "fw-1", true, "") + require.NoError(t, err) + + _, err = m.AssociateSubnets(bg, "fw-1", []string{"subnet-2"}, "") + requireException(t, err, nfdriver.ExInvalidOperation) + + _, err = m.DisassociateSubnets(bg, "fw-1", []string{"subnet-1"}, "") + requireException(t, err, nfdriver.ExInvalidOperation) + + _, err = m.UpdateSubnetChangeProtection(bg, "fw-1", false, "") + require.NoError(t, err) + + _, err = m.AssociateSubnets(bg, "fw-1", []string{"subnet-2"}, "") + require.NoError(t, err) +} + +func TestChangeProtection_BlocksPolicyAssociate(t *testing.T) { + m := newMock() + seedStack(t, m) + other := seedPolicy(t, m, "pol-2") + + _, err := m.UpdateFirewallPolicyChangeProtection(bg, "fw-1", true, "") + require.NoError(t, err) + + _, err = m.AssociateFirewallPolicy(bg, "fw-1", other.ARN, "") + requireException(t, err, nfdriver.ExInvalidOperation) + + _, err = m.UpdateFirewallPolicyChangeProtection(bg, "fw-1", false, "") + require.NoError(t, err) + + _, err = m.AssociateFirewallPolicy(bg, "fw-1", other.ARN, "") + require.NoError(t, err) +} + +func tgwFirewall(t *testing.T, m *Mock, name string, protect bool) *nfdriver.Firewall { + t.Helper() + + pol := seedPolicy(t, m, name+"-pol") + + fw, err := m.CreateFirewall(bg, nfdriver.CreateFirewallConfig{ + Name: name, PolicyARN: pol.ARN, TransitGatewayID: "tgw-0abc", AvailabilityZones: []string{"us-east-1a"}, + AvailabilityZoneChangeProtection: protect, + }) + require.NoError(t, err) + + return fw +} + +func TestChangeProtection_BlocksAZAssociateDisassociate(t *testing.T) { + m := newMock() + tgwFirewall(t, m, "fw-tgw", true) + + _, err := m.AssociateAvailabilityZones(bg, "fw-tgw", []string{"us-east-1b"}, "") + requireException(t, err, nfdriver.ExInvalidOperation) + + _, err = m.DisassociateAvailabilityZones(bg, "fw-tgw", []string{"us-east-1a"}, "") + requireException(t, err, nfdriver.ExInvalidOperation) + + _, err = m.UpdateAvailabilityZoneChangeProtection(bg, "fw-tgw", false, "") + require.NoError(t, err) + + _, err = m.AssociateAvailabilityZones(bg, "fw-tgw", []string{"us-east-1b"}, "") + require.NoError(t, err) +} + +func TestAvailabilityZones_AssociateDisassociate_TGWFirewall(t *testing.T) { + m := newMock() + fw := tgwFirewall(t, m, "fw-tgw", false) + + got, err := m.AssociateAvailabilityZones(bg, "fw-tgw", []string{"us-east-1b", "us-east-1a"}, "") + require.NoError(t, err) + assert.Equal(t, []string{"us-east-1a", "us-east-1b"}, got.AvailabilityZones, "deduplicated") + assert.NotEqual(t, fw.UpdateToken, got.UpdateToken) + + got, err = m.DisassociateAvailabilityZones(bg, "fw-tgw", []string{"us-east-1a"}, "") + require.NoError(t, err) + assert.Equal(t, []string{"us-east-1b"}, got.AvailabilityZones) + + _, err = m.DisassociateAvailabilityZones(bg, "fw-tgw", []string{"us-east-1z"}, "") + assert.True(t, cerrors.IsInvalidArgument(err), "unknown zone: %v", err) +} + +func TestAvailabilityZones_OnVPCFirewall_Unsupported(t *testing.T) { + m := newMock() + tgw := tgwFirewall(t, m, "fw-tgw", false) + _, vpc := seedStack(t, m) + + _, err := m.AssociateAvailabilityZones(bg, vpc.Name, []string{"us-east-1b"}, "") + requireException(t, err, nfdriver.ExInvalidOperation) + + _, err = m.AssociateSubnets(bg, tgw.Name, []string{"subnet-1"}, "") + requireException(t, err, nfdriver.ExInvalidOperation) +} + +func TestChangeProtection_NotFoundFirewall(t *testing.T) { + m := newMock() + + _, err := m.UpdateSubnetChangeProtection(bg, "ghost", true, "") + assert.True(t, cerrors.IsNotFound(err), "%v", err) +} + +func TestUpdateFirewallDescription_SetAndClear(t *testing.T) { + m := newMock() + _, fw := seedStack(t, m) + + got, err := m.UpdateFirewallDescription(bg, "fw-1", "edge firewall", "") + require.NoError(t, err) + assert.Equal(t, "edge firewall", got.Description) + assert.NotEqual(t, fw.UpdateToken, got.UpdateToken) + + got, err = m.UpdateFirewallDescription(bg, "fw-1", "", got.UpdateToken) + require.NoError(t, err) + assert.Empty(t, got.Description, "an omitted description clears it") +} + +func TestUpdateFirewallDescription_TooLong(t *testing.T) { + m := newMock() + seedStack(t, m) + + long := make([]byte, 513) + for i := range long { + long[i] = 'x' + } + + _, err := m.UpdateFirewallDescription(bg, "fw-1", string(long), "") + assert.True(t, cerrors.IsInvalidArgument(err), "%v", err) +} diff --git a/providers/aws/networkfirewall/validation.go b/providers/aws/networkfirewall/validation.go new file mode 100644 index 000000000..8778d2b62 --- /dev/null +++ b/providers/aws/networkfirewall/validation.go @@ -0,0 +1,115 @@ +package networkfirewall + +import ( + "strings" + + cerrors "github.com/stackshy/cloudemu/v2/errors" +) + +// Limits from the AWS Network Firewall API reference. +const ( + maxNameLen = 128 + maxDescriptionLen = 512 + maxTags = 200 + maxARNLen = 256 + arnPrefix = "arn:aws" + vpcIDPrefix = "vpc-" +) + +func invalid(format string, args ...any) error { + return cerrors.Newf(cerrors.InvalidArgument, format, args...) +} + +// validateName checks a resource name: 1-128 characters of [a-zA-Z0-9-]. +func validateName(what, name string) error { + if name == "" { + return invalid("%s is required", what) + } + + if len(name) > maxNameLen { + return invalid("%s %q exceeds %d characters", what, name, maxNameLen) + } + + for _, r := range name { + if !isNameRune(r) { + return invalid("%s %q must match ^[a-zA-Z0-9-]+$", what, name) + } + } + + return nil +} + +func isNameRune(r rune) bool { + return r == '-' || (r >= '0' && r <= '9') || (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') +} + +func validateDescription(d string) error { + if len(d) > maxDescriptionLen { + return invalid("Description exceeds %d characters", maxDescriptionLen) + } + + return nil +} + +func validateTags(tags map[string]string) error { + if len(tags) > maxTags { + return invalid("at most %d tags are allowed, got %d", maxTags, len(tags)) + } + + return nil +} + +// validateARN rejects an ARN that is empty, too long or not an AWS ARN, before +// any store lookup (a malformed ARN is a request error, not "not found"). +func validateARN(what, arn string) error { + if arn == "" { + return invalid("%s is required", what) + } + + if len(arn) > maxARNLen || !strings.HasPrefix(arn, arnPrefix) { + return invalid("%s %q is not a valid ARN", what, arn) + } + + return nil +} + +// validateVPCID checks a VPC id against ^vpc-[0-9a-f]+$ (max 128 characters). +func validateVPCID(id string) error { + hex, ok := strings.CutPrefix(id, vpcIDPrefix) + if !ok || hex == "" || len(id) > maxNameLen { + return invalid("VpcId %q must match ^vpc-[0-9a-f]+$", id) + } + + for _, r := range hex { + if (r < '0' || r > '9') && (r < 'a' || r > 'f') { + return invalid("VpcId %q must match ^vpc-[0-9a-f]+$", id) + } + } + + return nil +} + +// requireIdentifier checks that a name-or-ARN lookup carries one of them and +// that a supplied ARN is well formed. +func requireIdentifier(what, name, arn string) error { + if name == "" && arn == "" { + return invalid("either %sName or %sArn is required", what, what) + } + + if name == "" { + return validateARN(what+"Arn", arn) + } + + return nil +} + +// oneOf reports whether v is one of allowed (SDK enum values, copied verbatim). +func oneOf(v string, allowed ...string) bool { + for _, a := range allowed { + if v == a { + return true + } + } + + return false +} diff --git a/providers/aws/networkfirewall/validation_test.go b/providers/aws/networkfirewall/validation_test.go new file mode 100644 index 000000000..c1062a03a --- /dev/null +++ b/providers/aws/networkfirewall/validation_test.go @@ -0,0 +1,324 @@ +package networkfirewall + +import ( + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" +) + +func TestCreateFirewall_Validation(t *testing.T) { + m := newMock() + pol := seedPolicy(t, m, "pol-1") + + base := func() nfdriver.CreateFirewallConfig { + return nfdriver.CreateFirewallConfig{Name: "fw", PolicyARN: pol.ARN, VPCID: "vpc-1a"} + } + + tags := map[string]string{} + for i := 0; i < 201; i++ { + tags[strings.Repeat("k", i+1)] = "v" + } + + cases := map[string]func(*nfdriver.CreateFirewallConfig){ + "no vpc id": func(c *nfdriver.CreateFirewallConfig) { c.VPCID = "" }, + "bad vpc id": func(c *nfdriver.CreateFirewallConfig) { c.VPCID = "vpc-XYZ" }, + "bad name": func(c *nfdriver.CreateFirewallConfig) { c.Name = "fw_1" }, + "long name": func(c *nfdriver.CreateFirewallConfig) { c.Name = strings.Repeat("a", 129) }, + "long description": func(c *nfdriver.CreateFirewallConfig) { c.Description = strings.Repeat("d", 513) }, + "too many tags": func(c *nfdriver.CreateFirewallConfig) { c.Tags = tags }, + "no policy": func(c *nfdriver.CreateFirewallConfig) { c.PolicyARN = "" }, + "unknown policy": func(c *nfdriver.CreateFirewallConfig) { + c.PolicyARN = "arn:aws:network-firewall:us-east-1:000000000000:firewall-policy/x" + }, + "malformed policy": func(c *nfdriver.CreateFirewallConfig) { c.PolicyARN = "not-an-arn" }, + "bad analysis type": func(c *nfdriver.CreateFirewallConfig) { c.EnabledAnalysisTypes = []string{"BOGUS"} }, + "vpc with az": func(c *nfdriver.CreateFirewallConfig) { c.AvailabilityZones = []string{"us-east-1a"} }, + "tgw with vpc": func(c *nfdriver.CreateFirewallConfig) { + c.TransitGatewayID = "tgw-1" + c.AvailabilityZones = []string{"a"} + }, + "tgw without azs": func(c *nfdriver.CreateFirewallConfig) { c.TransitGatewayID = "tgw-1"; c.VPCID = "" }, + } + + for name, mutate := range cases { + t.Run(name, func(t *testing.T) { + cfg := base() + mutate(&cfg) + + _, err := m.CreateFirewall(bg, cfg) + assert.True(t, cerrors.IsInvalidArgument(err), "want InvalidArgument, got %v", err) + }) + } + + _, err := m.CreateFirewall(bg, base()) + require.NoError(t, err) +} + +func TestCreateFirewall_TGWWithoutVPC_OK(t *testing.T) { + m := newMock() + + fw := tgwFirewall(t, m, "fw-tgw", false) + assert.Empty(t, fw.VPCID) + assert.Equal(t, "tgw-0abc", fw.TransitGatewayID) +} + +func TestLogging_Validation(t *testing.T) { + m := newMock() + seedStack(t, m) + + bad := map[string][]nfdriver.LogDestinationConfig{ + "bogus log type": {s3Dest("BOGUS", "b")}, + "bogus dest type": {logDest("FLOW", "Kafka", map[string]string{"x": "y"})}, + "missing bucket": {logDest("FLOW", "S3", map[string]string{"prefix": "p"})}, + "missing log group": {logDest("FLOW", "CloudWatchLogs", map[string]string{})}, + "missing stream": {logDest("FLOW", "KinesisDataFirehose", nil)}, + "unknown key": {logDest("FLOW", "S3", map[string]string{"bucketName": "b", "oops": "1"})}, + "duplicate log type": {s3Dest("FLOW", "a"), s3Dest("FLOW", "b")}, + "more than three": {s3Dest("FLOW", "b"), s3Dest("ALERT", "b"), s3Dest("TLS", "b"), s3Dest("TLS", "c")}, + "prefix on cloudwatch": {logDest("FLOW", "CloudWatchLogs", map[string]string{"logGroup": "g", "prefix": "p"})}, + } + + for name, dests := range bad { + t.Run(name, func(t *testing.T) { + err := m.UpdateLoggingConfiguration(bg, "fw-1", &nfdriver.LoggingConfiguration{Destinations: dests}) + assert.True(t, cerrors.IsInvalidArgument(err), "want InvalidArgument, got %v", err) + }) + } + + logs, err := m.DescribeLoggingConfiguration(bg, "fw-1") + require.NoError(t, err) + assert.Empty(t, logs.Destinations, "rejected updates store nothing") +} + +func TestLogging_OneChangePerCall(t *testing.T) { + m := newMock() + seedStack(t, m) + + two := &nfdriver.LoggingConfiguration{Destinations: []nfdriver.LogDestinationConfig{s3Dest("FLOW", "b"), s3Dest("ALERT", "b")}} + err := m.UpdateLoggingConfiguration(bg, "fw-1", two) + assert.True(t, cerrors.IsInvalidArgument(err), "adding two destinations at once: %v", err) + + one := &nfdriver.LoggingConfiguration{Destinations: []nfdriver.LogDestinationConfig{s3Dest("FLOW", "b")}} + require.NoError(t, m.UpdateLoggingConfiguration(bg, "fw-1", one)) + + // Changing the destination TYPE of an existing entry is a remove plus an add. + typeChange := &nfdriver.LoggingConfiguration{Destinations: []nfdriver.LogDestinationConfig{ + logDest("FLOW", "CloudWatchLogs", map[string]string{"logGroup": "g"}), + }} + err = m.UpdateLoggingConfiguration(bg, "fw-1", typeChange) + assert.True(t, cerrors.IsInvalidArgument(err), "type change: %v", err) + + // Replacing one entry with another log type is also two changes. + swap := &nfdriver.LoggingConfiguration{Destinations: []nfdriver.LogDestinationConfig{s3Dest("ALERT", "b")}} + err = m.UpdateLoggingConfiguration(bg, "fw-1", swap) + assert.True(t, cerrors.IsInvalidArgument(err), "swap: %v", err) +} + +func TestLogging_Add_Remove_ChangeDestination_OmitDisables(t *testing.T) { + m := newMock() + seedStack(t, m) + + update := func(dests ...nfdriver.LogDestinationConfig) error { + return m.UpdateLoggingConfiguration(bg, "fw-1", &nfdriver.LoggingConfiguration{Destinations: dests}) + } + + require.NoError(t, update(s3Dest("FLOW", "b1"))) + require.NoError(t, update(s3Dest("FLOW", "b1"), s3Dest("ALERT", "b1"))) + require.NoError(t, update(s3Dest("FLOW", "b2"), s3Dest("ALERT", "b1")), "change one destination") + require.NoError(t, update(s3Dest("FLOW", "b2")), "remove one") + + logs, _ := m.DescribeLoggingConfiguration(bg, "fw-1") + require.Len(t, logs.Destinations, 1) + assert.Equal(t, "b2", logs.Destinations[0].LogDestination["bucketName"]) + + dash := true + require.NoError(t, m.UpdateLoggingConfiguration(bg, "fw-1", &nfdriver.LoggingConfiguration{ + Destinations: []nfdriver.LogDestinationConfig{s3Dest("FLOW", "b2")}, EnableMonitoringDashboard: &dash, + })) + + logs, _ = m.DescribeLoggingConfiguration(bg, "fw-1") + require.NotNil(t, logs.EnableMonitoringDashboard) + assert.True(t, *logs.EnableMonitoringDashboard) + + // An omitted configuration disables logging (the dashboard flag is kept). + require.NoError(t, m.UpdateLoggingConfiguration(bg, "fw-1", nil)) + + logs, _ = m.DescribeLoggingConfiguration(bg, "fw-1") + assert.Empty(t, logs.Destinations) + + err := m.UpdateLoggingConfiguration(bg, "ghost", nil) + assert.True(t, cerrors.IsNotFound(err)) +} + +func TestCreateRuleGroup_Validation(t *testing.T) { + m := newMock() + + rules := rulesString(`alert tcp any any -> any any (msg:"x"; sid:1;)`) + both := map[string]nfdriver.CreateRuleGroupConfig{ + "capacity 0": {Name: "rg", Type: "STATEFUL", Capacity: 0}, + "capacity negative": {Name: "rg", Type: "STATEFUL", Capacity: -3}, + "bad type": {Name: "rg", Type: "NOPE", Capacity: 5}, + "bad name": {Name: "rg 1", Type: "STATEFUL", Capacity: 5}, + "no name": {Type: "STATEFUL", Capacity: 5}, + "long description": {Name: "rg", Type: "STATEFUL", Capacity: 5, Description: strings.Repeat("d", 513)}, + "bad summary opt": {Name: "rg", Type: "STATEFUL", Capacity: 5, SummaryRuleOptions: []string{"BOGUS"}}, + "malformed rules": {Name: "rg", Type: "STATEFUL", Capacity: 5, Rules: []byte(`{not json`)}, + "over capacity": {Name: "rg", Type: "STATEFUL", Capacity: 1, Rules: rulesString("a", "b")}, + } + + for name, cfg := range both { + t.Run(name, func(t *testing.T) { + _, err := m.CreateRuleGroup(bg, cfg) + assert.True(t, cerrors.IsInvalidArgument(err), "want InvalidArgument, got %v", err) + }) + } + + _, err := m.CreateRuleGroup(bg, nfdriver.CreateRuleGroupConfig{Name: "rg", Type: "STATEFUL", Capacity: 1, Rules: rules}) + require.NoError(t, err) +} + +func TestCreateRuleGroup_StatefulDomainType(t *testing.T) { + m := newMock() + + rg, err := m.CreateRuleGroup(bg, nfdriver.CreateRuleGroupConfig{Name: "domains", Type: "STATEFUL_DOMAIN", Capacity: 5}) + require.NoError(t, err) + assert.Equal(t, "STATEFUL_DOMAIN", rg.Type) +} + +func TestRuleGroup_ConsumedExceedsCapacity(t *testing.T) { + m := newMock() + + rg, err := m.CreateRuleGroup(bg, nfdriver.CreateRuleGroupConfig{ + Name: "rg", Type: "STATEFUL", Capacity: 2, Rules: rulesString("r1", "r2"), + }) + require.NoError(t, err) + assert.Equal(t, 2, rg.ConsumedCapacity) + + _, err = m.UpdateRuleGroup(bg, "rg", "", "STATEFUL", nfdriver.UpdateRuleGroupConfig{ + UpdateToken: rg.UpdateToken, Rules: rulesString("r1", "r2", "r3"), + }) + assert.True(t, cerrors.IsInvalidArgument(err), "update past capacity: %v", err) + + got, _ := m.DescribeRuleGroup(bg, "rg", "", "STATEFUL") + assert.Equal(t, 2, got.Capacity, "capacity is fixed at creation") + assert.Equal(t, rg.UpdateToken, got.UpdateToken, "a rejected update changes nothing") +} + +func TestPolicy_DefaultActionValidation(t *testing.T) { + m := newMock() + + strict := []byte(`{"RuleOrder":"STRICT_ORDER"}`) + custom := []byte(`[{"ActionName":"mine","ActionDefinition":{}}]`) + + cases := map[string]nfdriver.CreateFirewallPolicyConfig{ + "no actions": {Name: "p"}, + "no fragment actions": {Name: "p", StatelessDefaultActions: standardActions}, + "unknown action": {Name: "p", StatelessDefaultActions: []string{"aws:nope"}, StatelessFragmentDefaultActions: standardActions}, + "two standard actions": {Name: "p", StatelessDefaultActions: []string{"aws:pass", "aws:drop"}, StatelessFragmentDefaultActions: standardActions}, + "only a custom action": {Name: "p", StatelessDefaultActions: []string{"mine"}, StatelessFragmentDefaultActions: standardActions, Extra: nfdriver.PolicyExtra{StatelessCustomActions: custom}}, + "undefined custom action": {Name: "p", StatelessDefaultActions: []string{"aws:pass", "ghost"}, StatelessFragmentDefaultActions: standardActions}, + "stateful default no strict": {Name: "p", StatelessDefaultActions: standardActions, StatelessFragmentDefaultActions: standardActions, StatefulDefaultActions: []string{"aws:drop_strict"}}, + "bad stateful default": {Name: "p", StatelessDefaultActions: standardActions, StatelessFragmentDefaultActions: standardActions, StatefulDefaultActions: []string{"aws:what"}, Extra: nfdriver.PolicyExtra{StatefulEngineOptions: strict}}, + "bad rule order": {Name: "p", StatelessDefaultActions: standardActions, StatelessFragmentDefaultActions: standardActions, Extra: nfdriver.PolicyExtra{StatefulEngineOptions: []byte(`{"RuleOrder":"RANDOM"}`)}}, + "tls holding without tls": {Name: "p", StatelessDefaultActions: standardActions, StatelessFragmentDefaultActions: standardActions, Extra: nfdriver.PolicyExtra{EnableTLSSessionHolding: ptr(true)}}, + "bad name": {Name: "p_1", StatelessDefaultActions: standardActions, StatelessFragmentDefaultActions: standardActions}, + } + + for name, cfg := range cases { + t.Run(name, func(t *testing.T) { + _, err := m.CreateFirewallPolicy(bg, cfg) + assert.True(t, cerrors.IsInvalidArgument(err), "want InvalidArgument, got %v", err) + }) + } + + ok := nfdriver.CreateFirewallPolicyConfig{ + Name: "good", StatelessDefaultActions: []string{"aws:pass", "mine"}, StatelessFragmentDefaultActions: standardActions, + StatefulDefaultActions: []string{"aws:drop_strict"}, + Extra: nfdriver.PolicyExtra{StatelessCustomActions: custom, StatefulEngineOptions: strict}, + } + _, err := m.CreateFirewallPolicy(bg, ok) + require.NoError(t, err) +} + +func ptr[T any](v T) *T { return &v } + +func TestPolicy_ReferenceValidation(t *testing.T) { + m := newMock() + stateless := seedRuleGroup(t, m, "rg-sl", "STATELESS", 5) + stateful := seedRuleGroup(t, m, "rg-sf", "STATEFUL", 5) + + policy := func(sl, sf []nfdriver.RuleGroupReference, extra nfdriver.PolicyExtra) nfdriver.CreateFirewallPolicyConfig { + return nfdriver.CreateFirewallPolicyConfig{ + Name: "p", StatelessDefaultActions: standardActions, StatelessFragmentDefaultActions: standardActions, + StatelessRuleGroupReferences: sl, StatefulRuleGroupReferences: sf, Extra: extra, + } + } + ref := func(arn string, prio int) nfdriver.RuleGroupReference { + return nfdriver.RuleGroupReference{ResourceARN: arn, Priority: prio} + } + strict := nfdriver.PolicyExtra{StatefulEngineOptions: []byte(`{"RuleOrder":"STRICT_ORDER"}`)} + ghost := "arn:aws:network-firewall:us-east-1:000000000000:stateless-rulegroup/ghost" + + cases := map[string]nfdriver.CreateFirewallPolicyConfig{ + "stateless without priority": policy([]nfdriver.RuleGroupReference{ref(stateless.ARN, 0)}, nil, nfdriver.PolicyExtra{}), + "duplicate priority": policy([]nfdriver.RuleGroupReference{ref(stateless.ARN, 1), ref(stateless.ARN, 1)}, nil, nfdriver.PolicyExtra{}), + "missing rule group": policy([]nfdriver.RuleGroupReference{ref(ghost, 1)}, nil, nfdriver.PolicyExtra{}), + "stateful in stateless list": policy([]nfdriver.RuleGroupReference{ref(stateful.ARN, 1)}, nil, nfdriver.PolicyExtra{}), + "stateless in stateful list": policy(nil, []nfdriver.RuleGroupReference{ref(stateless.ARN, 1)}, nfdriver.PolicyExtra{}), + "strict stateful no priority": policy(nil, []nfdriver.RuleGroupReference{ref(stateful.ARN, 0)}, strict), + "malformed arn": policy([]nfdriver.RuleGroupReference{ref("nope", 1)}, nil, nfdriver.PolicyExtra{}), + } + + for name, cfg := range cases { + t.Run(name, func(t *testing.T) { + _, err := m.CreateFirewallPolicy(bg, cfg) + assert.True(t, cerrors.IsInvalidArgument(err), "want InvalidArgument, got %v", err) + }) + } + + _, err := m.CreateFirewallPolicy(bg, policy( + []nfdriver.RuleGroupReference{ref(stateless.ARN, 1)}, []nfdriver.RuleGroupReference{ref(stateful.ARN, 0)}, nfdriver.PolicyExtra{})) + require.NoError(t, err, "default-order stateful references need no priority") +} + +func TestPolicy_ExtrasRoundTrip(t *testing.T) { + m := newMock() + + tlsCfg, err := m.CreateTLSInspectionConfiguration(bg, nfdriver.CreateTLSInspectionConfig{ + Name: "tls-1", Configuration: tlsConfigJSON("arn:aws:acm:us-east-1:000000000000:certificate/c1"), + }) + require.NoError(t, err) + + extra := nfdriver.PolicyExtra{ + StatelessCustomActions: []byte(`[{"ActionName":"mine","ActionDefinition":{"PublishMetricAction":{"Dimensions":[{"Value":"v"}]}}}]`), + PolicyVariables: []byte(`{"RuleVariables":{"HOME_NET":{"Definition":["10.0.0.0/8"]}}}`), + StatefulEngineOptions: []byte(`{"RuleOrder":"STRICT_ORDER","StreamExceptionPolicy":"DROP"}`), + EnableTLSSessionHolding: ptr(true), + } + + created, err := m.CreateFirewallPolicy(bg, nfdriver.CreateFirewallPolicyConfig{ + Name: "full", StatelessDefaultActions: []string{"aws:pass", "mine"}, StatelessFragmentDefaultActions: standardActions, + StatefulDefaultActions: []string{"aws:alert_established"}, TLSInspectionConfigurationARN: tlsCfg.ARN, Extra: extra, + }) + require.NoError(t, err) + + got, err := m.DescribeFirewallPolicy(bg, "full", "") + require.NoError(t, err) + assert.JSONEq(t, string(extra.StatelessCustomActions), string(got.Extra.StatelessCustomActions)) + assert.JSONEq(t, string(extra.PolicyVariables), string(got.Extra.PolicyVariables)) + assert.JSONEq(t, string(extra.StatefulEngineOptions), string(got.Extra.StatefulEngineOptions)) + assert.True(t, *got.Extra.EnableTLSSessionHolding) + assert.Equal(t, tlsCfg.ARN, got.TLSInspectionConfigurationARN) + assert.Equal(t, []string{"aws:alert_established"}, got.StatefulDefaultActions) + + // A TLS configuration can be replaced but not removed after create. + _, err = m.UpdateFirewallPolicy(bg, "full", "", nfdriver.UpdateFirewallPolicyConfig{ + UpdateToken: created.UpdateToken, StatelessDefaultActions: standardActions, StatelessFragmentDefaultActions: standardActions, + }) + assert.True(t, cerrors.IsInvalidArgument(err), "removing the TLS configuration: %v", err) +} diff --git a/providers/aws/networkfirewall/vpc_endpoint_association.go b/providers/aws/networkfirewall/vpc_endpoint_association.go new file mode 100644 index 000000000..f7e2582b5 --- /dev/null +++ b/providers/aws/networkfirewall/vpc_endpoint_association.go @@ -0,0 +1,157 @@ +package networkfirewall + +import ( + "context" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/internal/idgen" + nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" +) + +//nolint:gocritic // cfg matches the driver signature. +func (m *Mock) CreateVpcEndpointAssociation( + _ context.Context, cfg nfdriver.CreateVPCEndpointAssociationConfig, +) (*nfdriver.VPCEndpointAssociation, error) { + m.mu.Lock() + defer m.mu.Unlock() + + if err := validateARN("FirewallArn", cfg.FirewallARN); err != nil { + return nil, err + } + + if err := validateVPCID(cfg.VPCID); err != nil { + return nil, err + } + + if cfg.SubnetID == "" { + return nil, invalid("SubnetMapping.SubnetId is required") + } + + if cfg.IPAddressType != "" && !oneOf(cfg.IPAddressType, ipAddressType...) { + return nil, invalid("SubnetMapping.IPAddressType %q is not valid", cfg.IPAddressType) + } + + if err := validateDescription(cfg.Description); err != nil { + return nil, err + } + + if err := validateTags(cfg.Tags); err != nil { + return nil, err + } + + if _, ok := m.firewallByARN(cfg.FirewallARN); !ok { + return nil, cerrors.Newf(cerrors.NotFound, "firewall %q not found", cfg.FirewallARN) + } + + id := idgen.GenerateID("") + a := &nfdriver.VPCEndpointAssociation{ + ARN: m.arn("vpc-endpoint-association", id), + ID: id, + FirewallARN: cfg.FirewallARN, + VPCID: cfg.VPCID, + SubnetID: cfg.SubnetID, + IPAddressType: cfg.IPAddressType, + AvailabilityZone: nfdriver.ZoneForKey(m.opts.Region, cfg.SubnetID), + Description: cfg.Description, + Status: statusReady, + Tags: copyTags(cfg.Tags), + } + m.associations.Set(a.ARN, a) + + return cloneAssociation(a), nil +} + +func (m *Mock) DescribeVpcEndpointAssociation(_ context.Context, arn string) (*nfdriver.VPCEndpointAssociation, error) { + m.mu.RLock() + defer m.mu.RUnlock() + + a, err := m.findAssociation(arn) + if err != nil { + return nil, err + } + + return cloneAssociation(a), nil +} + +func (m *Mock) DeleteVpcEndpointAssociation(_ context.Context, arn string) (*nfdriver.VPCEndpointAssociation, error) { + m.mu.Lock() + defer m.mu.Unlock() + + a, err := m.findAssociation(arn) + if err != nil { + return nil, err + } + + out := cloneAssociation(a) + out.Status = statusDeleting + + m.associations.Delete(a.ARN) + + return out, nil +} + +func (m *Mock) ListVpcEndpointAssociations( + _ context.Context, firewallARN string, opts nfdriver.ListOptions, +) ([]nfdriver.VPCEndpointAssociation, string, error) { + m.mu.RLock() + defer m.mu.RUnlock() + + if firewallARN != "" { + if err := validateARN("FirewallArn", firewallARN); err != nil { + return nil, "", err + } + } + + var matched []*nfdriver.VPCEndpointAssociation + + for _, a := range m.associations.SortedValues() { + if firewallARN == "" || a.FirewallARN == firewallARN { + matched = append(matched, a) + } + } + + items, next, err := paginate(matched, opts) + if err != nil { + return nil, "", err + } + + out := make([]nfdriver.VPCEndpointAssociation, 0, len(items)) + for _, a := range items { + out = append(out, *cloneAssociation(a)) + } + + return out, next, nil +} + +func (m *Mock) findAssociation(arn string) (*nfdriver.VPCEndpointAssociation, error) { + if err := validateARN("VpcEndpointAssociationArn", arn); err != nil { + return nil, err + } + + a, ok := m.associations.Get(arn) + if !ok { + return nil, cerrors.Newf(cerrors.NotFound, "VPC endpoint association %q not found", arn) + } + + return a, nil +} + +// associationCount counts the VPC endpoint associations of a firewall. +func (m *Mock) associationCount(firewallARN string) int { + n := 0 + + for _, a := range m.associations.SortedValues() { + if a.FirewallARN == firewallARN { + n++ + } + } + + return n +} + +func cloneAssociation(a *nfdriver.VPCEndpointAssociation) *nfdriver.VPCEndpointAssociation { + out := *a + out.Tags = copyTags(a.Tags) + + return &out +} diff --git a/providers/aws/networkfirewall_discovery_test.go b/providers/aws/networkfirewall_discovery_test.go new file mode 100644 index 000000000..8f20441db --- /dev/null +++ b/providers/aws/networkfirewall_discovery_test.go @@ -0,0 +1,67 @@ +package aws + +import ( + "context" + "testing" + + nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" + "github.com/stackshy/cloudemu/v2/services/resourcediscovery" +) + +// Every Network Firewall resource family the emulator holds must surface in the +// cross-service inventory with its live tags: firewalls and policies, rule +// groups, TLS inspection configurations and VPC endpoint associations. +func TestResourceDiscoverySurfacesNetworkFirewall(t *testing.T) { + ctx := context.Background() + p := New() + nf := p.NetworkFirewall + + pol, err := nf.CreateFirewallPolicy(ctx, nfdriver.CreateFirewallPolicyConfig{ + Name: "pol", StatelessDefaultActions: []string{"aws:pass"}, StatelessFragmentDefaultActions: []string{"aws:pass"}, + Tags: map[string]string{"env": "prod"}, + }) + if err != nil { + t.Fatalf("CreateFirewallPolicy: %v", err) + } + + fw, err := nf.CreateFirewall(ctx, nfdriver.CreateFirewallConfig{Name: "fw", PolicyARN: pol.ARN, VPCID: "vpc-1a"}) + if err != nil { + t.Fatalf("CreateFirewall: %v", err) + } + + if _, err := nf.CreateRuleGroup(ctx, nfdriver.CreateRuleGroupConfig{Name: "rg", Type: "STATEFUL", Capacity: 5}); err != nil { + t.Fatalf("CreateRuleGroup: %v", err) + } + + if _, err := nf.CreateTLSInspectionConfiguration(ctx, nfdriver.CreateTLSInspectionConfig{ + Name: "tls", Configuration: []byte(`{"ServerCertificateConfigurations":[{}]}`), + }); err != nil { + t.Fatalf("CreateTLSInspectionConfiguration: %v", err) + } + + if _, err := nf.CreateVpcEndpointAssociation(ctx, nfdriver.CreateVPCEndpointAssociationConfig{ + FirewallARN: fw.ARN, VPCID: "vpc-2b", SubnetID: "subnet-1", + }); err != nil { + t.Fatalf("CreateVpcEndpointAssociation: %v", err) + } + + res, err := p.ResourceDiscovery.List(ctx, resourcediscovery.Query{Services: []string{"network-firewall"}}) + if err != nil { + t.Fatalf("discovery List: %v", err) + } + + types := map[string]resourcediscovery.Resource{} + for _, r := range res { + types[r.Type] = r + } + + for _, want := range []string{"firewall", "firewall-policy", "rule-group", "tls-configuration", "vpc-endpoint-association"} { + if _, ok := types[want]; !ok { + t.Errorf("%s missing from discovery output (%d resources)", want, len(res)) + } + } + + if got := types["firewall-policy"].Tags["env"]; got != "prod" { + t.Errorf("firewall policy tags not surfaced: %+v", types["firewall-policy"].Tags) + } +} diff --git a/providers/aws/rgt_discovery.go b/providers/aws/rgt_discovery.go index 2c263bba6..d54b786d6 100644 --- a/providers/aws/rgt_discovery.go +++ b/providers/aws/rgt_discovery.go @@ -141,14 +141,97 @@ func (d taggedDiscovery) discoverKMS(ctx context.Context) ([]resourcediscovery.D } func (d taggedDiscovery) discoverNetworkFirewall(ctx context.Context) ([]resourcediscovery.DiscoveredResource, error) { - firewalls, err := d.p.NetworkFirewall.ListFirewalls(ctx) + nf := d.p.NetworkFirewall + + collectors := []func() ([]resourcediscovery.DiscoveredResource, error){ + func() ([]resourcediscovery.DiscoveredResource, error) { + return pageRows(func(token string) ([]nfdriver.Firewall, string, error) { + return nf.ListFirewalls(ctx, nil, nfdriver.ListOptions{NextToken: token}) + }, func(f *nfdriver.Firewall) resourcediscovery.DiscoveredResource { + return row("network-firewall", "firewall", f.Name, f.ARN, f.Tags) + }) + }, + func() ([]resourcediscovery.DiscoveredResource, error) { + return pageRows(func(token string) ([]nfdriver.FirewallPolicy, string, error) { + return nf.ListFirewallPolicies(ctx, nfdriver.ListOptions{NextToken: token}) + }, func(p *nfdriver.FirewallPolicy) resourcediscovery.DiscoveredResource { + return row("network-firewall", "firewall-policy", p.Name, p.ARN, p.Tags) + }) + }, + func() ([]resourcediscovery.DiscoveredResource, error) { + return pageRows(func(token string) ([]nfdriver.RuleGroup, string, error) { + return nf.ListRuleGroups(ctx, nfdriver.RuleGroupFilter{}, nfdriver.ListOptions{NextToken: token}) + }, func(g *nfdriver.RuleGroup) resourcediscovery.DiscoveredResource { + return row("network-firewall", "rule-group", g.Name, g.ARN, g.Tags) + }) + }, + func() ([]resourcediscovery.DiscoveredResource, error) { + return pageRows(func(token string) ([]nfdriver.TLSInspectionConfig, string, error) { + return nf.ListTLSInspectionConfigurations(ctx, nfdriver.ListOptions{NextToken: token}) + }, func(c *nfdriver.TLSInspectionConfig) resourcediscovery.DiscoveredResource { + return row("network-firewall", "tls-configuration", c.Name, c.ARN, c.Tags) + }) + }, + func() ([]resourcediscovery.DiscoveredResource, error) { + return pageRows(func(token string) ([]nfdriver.VPCEndpointAssociation, string, error) { + return nf.ListVpcEndpointAssociations(ctx, "", nfdriver.ListOptions{NextToken: token}) + }, func(a *nfdriver.VPCEndpointAssociation) resourcediscovery.DiscoveredResource { + return row("network-firewall", "vpc-endpoint-association", a.ID, a.ARN, a.Tags) + }) + }, + } + + var out []resourcediscovery.DiscoveredResource + + for _, collect := range collectors { + rows, err := collect() + if err != nil { + return nil, err + } + + out = append(out, rows...) + } + + return out, nil +} + +// pageRows drains a NextToken-paginated list call and maps every item to a row. +func pageRows[T any]( + list func(token string) ([]T, string, error), toRow func(*T) resourcediscovery.DiscoveredResource, +) ([]resourcediscovery.DiscoveredResource, error) { + items, err := allPages(list) if err != nil { return nil, err } - return project(ctx, firewalls, func(_ context.Context, f nfdriver.Firewall) (resourcediscovery.DiscoveredResource, error) { - return row("network-firewall", "firewall", f.Name, f.ARN, f.Tags), nil - }) + rows := make([]resourcediscovery.DiscoveredResource, 0, len(items)) + for i := range items { + rows = append(rows, toRow(&items[i])) + } + + return rows, nil +} + +// allPages drains a NextToken-paginated list call. +func allPages[T any](list func(token string) ([]T, string, error)) ([]T, error) { + var all []T + + token := "" + + for { + items, next, err := list(token) + if err != nil { + return nil, err + } + + all = append(all, items...) + + if next == "" { + return all, nil + } + + token = next + } } func (d taggedDiscovery) discoverSESv2(ctx context.Context) ([]resourcediscovery.DiscoveredResource, error) { diff --git a/server/aws/networkfirewall/analysis_report.go b/server/aws/networkfirewall/analysis_report.go new file mode 100644 index 000000000..b4e737461 --- /dev/null +++ b/server/aws/networkfirewall/analysis_report.go @@ -0,0 +1,96 @@ +package networkfirewall + +import ( + "net/http" + + "github.com/stackshy/cloudemu/v2/server/wire" + nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" +) + +type startReportRequest struct { + nameArnRequest + AnalysisType string `json:"AnalysisType"` +} + +func (h *Handler) startAnalysisReport(w http.ResponseWriter, r *http.Request) { + var req startReportRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + reports, ok := capability[nfdriver.AnalysisReports](h, w) + if !ok { + return + } + + rep, err := reports.StartAnalysisReport(r.Context(), req.FirewallName, req.FirewallArn, req.AnalysisType) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, map[string]any{"AnalysisReportId": rep.ID}) +} + +type listReportsRequest struct { + nameArnRequest + listRequest +} + +func (h *Handler) listAnalysisReports(w http.ResponseWriter, r *http.Request) { + var req listReportsRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + reports, ok := capability[nfdriver.AnalysisReports](h, w) + if !ok { + return + } + + items, next, err := reports.ListAnalysisReports(r.Context(), req.FirewallName, req.FirewallArn, req.options()) + if err != nil { + writeErr(w, err) + return + } + + out := make([]map[string]any, 0, len(items)) + for i := range items { + out = append(out, map[string]any{ + "AnalysisReportId": items[i].ID, "AnalysisType": items[i].AnalysisType, + "ReportTime": epochSeconds(items[i].ReportTime), "Status": items[i].Status, + }) + } + + wire.WriteJSON(w, withNextToken(map[string]any{"AnalysisReports": out}, next)) +} + +type getReportRequest struct { + nameArnRequest + listRequest + AnalysisReportID string `json:"AnalysisReportId"` +} + +func (h *Handler) getAnalysisReportResults(w http.ResponseWriter, r *http.Request) { + var req getReportRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + reports, ok := capability[nfdriver.AnalysisReports](h, w) + if !ok { + return + } + + rep, err := reports.GetAnalysisReport(r.Context(), req.FirewallName, req.FirewallArn, req.AnalysisReportID) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, map[string]any{ + "AnalysisType": rep.AnalysisType, "Status": rep.Status, "ReportTime": epochSeconds(rep.ReportTime), + "StartTime": epochSeconds(rep.ReportTime), "EndTime": epochSeconds(rep.ReportTime), + "AnalysisReportResults": []map[string]any{}, + }) +} diff --git a/server/aws/networkfirewall/availability_zones.go b/server/aws/networkfirewall/availability_zones.go new file mode 100644 index 000000000..e031fb9ee --- /dev/null +++ b/server/aws/networkfirewall/availability_zones.go @@ -0,0 +1,48 @@ +package networkfirewall + +import ( + "net/http" + + nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" +) + +type zonesRequest struct { + firewallUpdateRequest + AvailabilityZoneMappings []zoneMapping `json:"AvailabilityZoneMappings"` +} + +func zoneMappingsOf(fw *nfdriver.Firewall) []zoneMapping { + out := make([]zoneMapping, 0, len(fw.AvailabilityZones)) + for _, z := range fw.AvailabilityZones { + out = append(out, zoneMapping{AvailabilityZone: z}) + } + + return out +} + +// zoneChange is the Associate/DisassociateAvailabilityZones driver call. +type zoneChange func(z nfdriver.AvailabilityZones, name string, zones []string, token string) (*nfdriver.Firewall, error) + +func (h *Handler) changeZones(w http.ResponseWriter, r *http.Request, change zoneChange) { + serveFirewallOp[zonesRequest](h, w, r, + func(z nfdriver.AvailabilityZones, name string, req *zonesRequest) (*nfdriver.Firewall, map[string]any, error) { + fw, err := change(z, name, zoneNames(req.AvailabilityZoneMappings), req.UpdateToken) + if err != nil { + return nil, nil, err + } + + return fw, map[string]any{"AvailabilityZoneMappings": zoneMappingsOf(fw)}, nil + }) +} + +func (h *Handler) associateAvailabilityZones(w http.ResponseWriter, r *http.Request) { + h.changeZones(w, r, func(z nfdriver.AvailabilityZones, name string, zones []string, token string) (*nfdriver.Firewall, error) { + return z.AssociateAvailabilityZones(r.Context(), name, zones, token) + }) +} + +func (h *Handler) disassociateAvailabilityZones(w http.ResponseWriter, r *http.Request) { + h.changeZones(w, r, func(z nfdriver.AvailabilityZones, name string, zones []string, token string) (*nfdriver.Firewall, error) { + return z.DisassociateAvailabilityZones(r.Context(), name, zones, token) + }) +} diff --git a/server/aws/networkfirewall/change_protection.go b/server/aws/networkfirewall/change_protection.go new file mode 100644 index 000000000..237edc2e5 --- /dev/null +++ b/server/aws/networkfirewall/change_protection.go @@ -0,0 +1,121 @@ +package networkfirewall + +import ( + "net/http" + + nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" +) + +// firewallSettingCall is a FirewallSettings operation: it returns the updated +// firewall and the response member that echoes the changed setting (a nil value +// is left out, as AWS omits an absent member). +type firewallSettingCall[R any] func(s nfdriver.FirewallSettings, name string, req *R) (*nfdriver.Firewall, string, any, error) + +// serveFirewallSetting adapts a FirewallSettings operation to serveFirewallOp. +func serveFirewallSetting[R any, P firewallRequest[R]](h *Handler, w http.ResponseWriter, r *http.Request, call firewallSettingCall[R]) { + serveFirewallOp[R, P](h, w, r, func(s nfdriver.FirewallSettings, name string, req *R) (*nfdriver.Firewall, map[string]any, error) { + fw, member, value, err := call(s, name, req) + if err != nil { + return nil, nil, err + } + + extra := map[string]any{} + if value != nil { + extra[member] = value + } + + return fw, extra, nil + }) +} + +// memberDescription is the response member UpdateFirewallDescription echoes. +const memberDescription = "Description" + +type descriptionRequest struct { + firewallUpdateRequest + Description string `json:"Description"` +} + +func (h *Handler) updateFirewallDescription(w http.ResponseWriter, r *http.Request) { + serveFirewallSetting[descriptionRequest](h, w, r, + func(s nfdriver.FirewallSettings, name string, req *descriptionRequest) (*nfdriver.Firewall, string, any, error) { + fw, err := s.UpdateFirewallDescription(r.Context(), name, req.Description, req.UpdateToken) + if err != nil { + return nil, "", nil, err + } + + if fw.Description == "" { + return fw, memberDescription, nil, nil + } + + return fw, memberDescription, fw.Description, nil + }) +} + +type subnetProtectionRequest struct { + firewallUpdateRequest + SubnetChangeProtection bool `json:"SubnetChangeProtection"` +} + +func (h *Handler) updateSubnetChangeProtection(w http.ResponseWriter, r *http.Request) { + serveFirewallSetting[subnetProtectionRequest](h, w, r, + func(s nfdriver.FirewallSettings, name string, req *subnetProtectionRequest) (*nfdriver.Firewall, string, any, error) { + fw, err := s.UpdateSubnetChangeProtection(r.Context(), name, req.SubnetChangeProtection, req.UpdateToken) + if err != nil { + return nil, "", nil, err + } + + return fw, "SubnetChangeProtection", fw.SubnetChangeProtection, nil + }) +} + +type policyProtectionRequest struct { + firewallUpdateRequest + FirewallPolicyChangeProtection bool `json:"FirewallPolicyChangeProtection"` +} + +func (h *Handler) updateFirewallPolicyChangeProtection(w http.ResponseWriter, r *http.Request) { + serveFirewallSetting[policyProtectionRequest](h, w, r, + func(s nfdriver.FirewallSettings, name string, req *policyProtectionRequest) (*nfdriver.Firewall, string, any, error) { + fw, err := s.UpdateFirewallPolicyChangeProtection(r.Context(), name, req.FirewallPolicyChangeProtection, req.UpdateToken) + if err != nil { + return nil, "", nil, err + } + + return fw, "FirewallPolicyChangeProtection", fw.FirewallPolicyChangeProtection, nil + }) +} + +type zoneProtectionRequest struct { + firewallUpdateRequest + AvailabilityZoneChangeProtection bool `json:"AvailabilityZoneChangeProtection"` +} + +func (h *Handler) updateAvailabilityZoneChangeProtection(w http.ResponseWriter, r *http.Request) { + serveFirewallSetting[zoneProtectionRequest](h, w, r, + func(s nfdriver.FirewallSettings, name string, req *zoneProtectionRequest) (*nfdriver.Firewall, string, any, error) { + fw, err := s.UpdateAvailabilityZoneChangeProtection(r.Context(), name, req.AvailabilityZoneChangeProtection, req.UpdateToken) + if err != nil { + return nil, "", nil, err + } + + return fw, "AvailabilityZoneChangeProtection", fw.AvailabilityZoneChangeProtection, nil + }) +} + +type analysisSettingsRequest struct { + firewallUpdateRequest + EnabledAnalysisTypes []string `json:"EnabledAnalysisTypes"` +} + +func (h *Handler) updateFirewallAnalysisSettings(w http.ResponseWriter, r *http.Request) { + serveFirewallSetting[analysisSettingsRequest](h, w, r, + func(s nfdriver.FirewallSettings, name string, req *analysisSettingsRequest) (*nfdriver.Firewall, string, any, error) { + fw, err := s.UpdateFirewallAnalysisSettings(r.Context(), name, req.EnabledAnalysisTypes, req.UpdateToken) + if err != nil { + return nil, "", nil, err + } + + return fw, "EnabledAnalysisTypes", fw.EnabledAnalysisTypes, nil + }) +} diff --git a/server/aws/networkfirewall/depth.go b/server/aws/networkfirewall/depth.go index 2fb072085..6ae06d546 100644 --- a/server/aws/networkfirewall/depth.go +++ b/server/aws/networkfirewall/depth.go @@ -7,197 +7,150 @@ import ( nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" ) -type associatePolicyRequest struct { - FirewallName string `json:"FirewallName"` - FirewallArn string `json:"FirewallArn"` - FirewallPolicyArn string `json:"FirewallPolicyArn"` -} - -func (h *Handler) associateFirewallPolicy(w http.ResponseWriter, r *http.Request) { - var req associatePolicyRequest - if !wire.DecodeJSON(w, r, &req) { - return +// resolveFirewall returns the firewall's name from a name-or-ARN request (the +// driver mutators are keyed by name), writing the error response on failure. +func (h *Handler) resolveFirewall(w http.ResponseWriter, r *http.Request, name, arn string) (string, bool) { + if name != "" { + return name, true } - fw, err := h.db.AssociateFirewallPolicy(r.Context(), firewallName(req.FirewallName, req.FirewallArn), req.FirewallPolicyArn) + fw, err := h.db.DescribeFirewall(r.Context(), "", arn) if err != nil { writeErr(w, err) - return + return "", false } - wire.WriteJSON(w, map[string]any{ - "FirewallArn": fw.ARN, "FirewallName": fw.Name, - "FirewallPolicyArn": fw.PolicyARN, "UpdateToken": updateToken, - }) + return fw.Name, true } -type subnetsRequest struct { - FirewallName string `json:"FirewallName"` - FirewallArn string `json:"FirewallArn"` - SubnetMappings []subnetMapping `json:"SubnetMappings"` - SubnetIDs []string `json:"SubnetIds"` +type firewallUpdateRequest struct { + FirewallName string `json:"FirewallName"` + FirewallArn string `json:"FirewallArn"` + UpdateToken string `json:"UpdateToken"` } -func (h *Handler) associateSubnets(w http.ResponseWriter, r *http.Request) { - var req subnetsRequest - if !wire.DecodeJSON(w, r, &req) { - return - } - - fw, err := h.db.AssociateSubnets(r.Context(), firewallName(req.FirewallName, req.FirewallArn), subnetIDs(req.SubnetMappings)) - if err != nil { - writeErr(w, err) - return - } +// base lets the generic firewall-operation helper reach the shared members of +// any request that embeds firewallUpdateRequest. +func (f *firewallUpdateRequest) base() *firewallUpdateRequest { return f } - writeSubnetsResult(w, fw) +// firewallRequest is a request body that embeds firewallUpdateRequest. +type firewallRequest[R any] interface { + *R + base() *firewallUpdateRequest } -func (h *Handler) disassociateSubnets(w http.ResponseWriter, r *http.Request) { - var req subnetsRequest +// firewallCall performs one firewall mutation on a driver capability C for the +// resolved firewall name and returns the updated firewall plus the response +// members specific to the operation. +type firewallCall[C, R any] func(c C, name string, req *R) (*nfdriver.Firewall, map[string]any, error) + +// serveFirewallOp decodes the request body into R, resolves the driver +// capability C (UnsupportedOperationException when the driver lacks it) and the +// firewall name, runs call and writes the common response members +// (FirewallArn, FirewallName, UpdateToken) plus the operation-specific ones. +func serveFirewallOp[R any, P firewallRequest[R], C any](h *Handler, w http.ResponseWriter, r *http.Request, call firewallCall[C, R]) { + var req R if !wire.DecodeJSON(w, r, &req) { return } - fw, err := h.db.DisassociateSubnets(r.Context(), firewallName(req.FirewallName, req.FirewallArn), req.SubnetIDs) - if err != nil { - writeErr(w, err) + c, ok := capability[C](h, w) + if !ok { return } - writeSubnetsResult(w, fw) -} - -func writeSubnetsResult(w http.ResponseWriter, fw *nfdriver.Firewall) { - mappings := make([]subnetMapping, 0, len(fw.SubnetIDs)) - for _, s := range fw.SubnetIDs { - mappings = append(mappings, subnetMapping{SubnetID: s}) - } + base := P(&req).base() - wire.WriteJSON(w, map[string]any{ - "FirewallArn": fw.ARN, "FirewallName": fw.Name, - "SubnetMappings": mappings, "UpdateToken": updateToken, - }) -} - -type deleteProtectionRequest struct { - FirewallName string `json:"FirewallName"` - FirewallArn string `json:"FirewallArn"` - DeleteProtection bool `json:"DeleteProtection"` -} - -func (h *Handler) updateFirewallDeleteProtection(w http.ResponseWriter, r *http.Request) { - var req deleteProtectionRequest - if !wire.DecodeJSON(w, r, &req) { + name, ok := h.resolveFirewall(w, r, base.FirewallName, base.FirewallArn) + if !ok { return } - fw, err := h.db.UpdateFirewallDeleteProtection(r.Context(), firewallName(req.FirewallName, req.FirewallArn), req.DeleteProtection) + fw, extra, err := call(c, name, &req) if err != nil { writeErr(w, err) return } - wire.WriteJSON(w, map[string]any{ - "FirewallArn": fw.ARN, "FirewallName": fw.Name, - "DeleteProtection": fw.DeleteProtection, "UpdateToken": updateToken, - }) -} + resp := map[string]any{"FirewallArn": fw.ARN, "FirewallName": fw.Name, "UpdateToken": fw.UpdateToken} + for k, v := range extra { + resp[k] = v + } -type logDestinationConfig struct { - LogType string `json:"LogType"` - LogDestinationType string `json:"LogDestinationType,omitempty"` - LogDestination map[string]string `json:"LogDestination,omitempty"` + wire.WriteJSON(w, resp) } -type loggingConfiguration struct { - LogDestinationConfigs []logDestinationConfig `json:"LogDestinationConfigs"` +type associatePolicyRequest struct { + firewallUpdateRequest + FirewallPolicyArn string `json:"FirewallPolicyArn"` } -type loggingRequest struct { - FirewallName string `json:"FirewallName"` - FirewallArn string `json:"FirewallArn"` - LoggingConfiguration loggingConfiguration `json:"LoggingConfiguration"` +func (h *Handler) associateFirewallPolicy(w http.ResponseWriter, r *http.Request) { + serveFirewallOp[associatePolicyRequest](h, w, r, + func(c nfdriver.NetworkFirewall, name string, req *associatePolicyRequest) (*nfdriver.Firewall, map[string]any, error) { + fw, err := c.AssociateFirewallPolicy(r.Context(), name, req.FirewallPolicyArn, req.UpdateToken) + if err != nil { + return nil, nil, err + } + + return fw, map[string]any{"FirewallPolicyArn": fw.PolicyARN}, nil + }) } -func (h *Handler) updateLoggingConfiguration(w http.ResponseWriter, r *http.Request) { - var req loggingRequest - if !wire.DecodeJSON(w, r, &req) { - return - } - - name := firewallName(req.FirewallName, req.FirewallArn) - if err := h.db.UpdateLoggingConfiguration(r.Context(), name, toLogConfigs(req.LoggingConfiguration.LogDestinationConfigs)); err != nil { - writeErr(w, err) - return - } - - fw, err := h.db.DescribeFirewall(r.Context(), name, "") - if err != nil { - writeErr(w, err) - return - } - - wire.WriteJSON(w, map[string]any{ - "FirewallArn": fw.ARN, "FirewallName": name, - "LoggingConfiguration": req.LoggingConfiguration, - }) +type subnetsRequest struct { + firewallUpdateRequest + SubnetMappings []subnetMapping `json:"SubnetMappings"` + SubnetIDs []string `json:"SubnetIds"` } -func (h *Handler) describeLoggingConfiguration(w http.ResponseWriter, r *http.Request) { - var req nameArnRequest - if !wire.DecodeJSON(w, r, &req) { - return - } - - name := firewallName(req.FirewallName, req.FirewallArn) - - configs, err := h.db.DescribeLoggingConfiguration(r.Context(), name) - if err != nil { - writeErr(w, err) - return - } - - fw, err := h.db.DescribeFirewall(r.Context(), name, "") - if err != nil { - writeErr(w, err) - return - } - - wire.WriteJSON(w, map[string]any{ - "FirewallArn": fw.ARN, - "LoggingConfiguration": loggingConfiguration{LogDestinationConfigs: fromLogConfigs(configs)}, - }) +func (h *Handler) associateSubnets(w http.ResponseWriter, r *http.Request) { + serveFirewallOp[subnetsRequest](h, w, r, + func(c nfdriver.NetworkFirewall, name string, req *subnetsRequest) (*nfdriver.Firewall, map[string]any, error) { + fw, err := c.AssociateSubnets(r.Context(), name, subnetIDs(req.SubnetMappings), req.UpdateToken) + if err != nil { + return nil, nil, err + } + + return fw, map[string]any{"SubnetMappings": subnetMappingsOf(fw)}, nil + }) } -func toLogConfigs(cs []logDestinationConfig) []nfdriver.LogDestinationConfig { - if len(cs) == 0 { - return nil - } - - out := make([]nfdriver.LogDestinationConfig, 0, len(cs)) - for _, c := range cs { - out = append(out, nfdriver.LogDestinationConfig{ - LogType: c.LogType, LogDestinationType: c.LogDestinationType, LogDestination: c.LogDestination, +func (h *Handler) disassociateSubnets(w http.ResponseWriter, r *http.Request) { + serveFirewallOp[subnetsRequest](h, w, r, + func(c nfdriver.NetworkFirewall, name string, req *subnetsRequest) (*nfdriver.Firewall, map[string]any, error) { + fw, err := c.DisassociateSubnets(r.Context(), name, req.SubnetIDs, req.UpdateToken) + if err != nil { + return nil, nil, err + } + + return fw, map[string]any{"SubnetMappings": subnetMappingsOf(fw)}, nil }) +} + +func subnetMappingsOf(fw *nfdriver.Firewall) []subnetMapping { + mappings := make([]subnetMapping, 0, len(fw.SubnetIDs)) + for _, s := range fw.SubnetIDs { + mappings = append(mappings, subnetMapping{SubnetID: s}) } - return out + return mappings } -func fromLogConfigs(cs []nfdriver.LogDestinationConfig) []logDestinationConfig { - if len(cs) == 0 { - return nil - } +type deleteProtectionRequest struct { + firewallUpdateRequest + DeleteProtection bool `json:"DeleteProtection"` +} - out := make([]logDestinationConfig, 0, len(cs)) - for _, c := range cs { - out = append(out, logDestinationConfig{ - LogType: c.LogType, LogDestinationType: c.LogDestinationType, LogDestination: c.LogDestination, +func (h *Handler) updateFirewallDeleteProtection(w http.ResponseWriter, r *http.Request) { + serveFirewallOp[deleteProtectionRequest](h, w, r, + func(c nfdriver.NetworkFirewall, name string, req *deleteProtectionRequest) (*nfdriver.Firewall, map[string]any, error) { + fw, err := c.UpdateFirewallDeleteProtection(r.Context(), name, req.DeleteProtection, req.UpdateToken) + if err != nil { + return nil, nil, err + } + + return fw, map[string]any{"DeleteProtection": fw.DeleteProtection}, nil }) - } - - return out } type tagResourceRequest struct { @@ -256,20 +209,3 @@ func (h *Handler) listTagsForResource(w http.ResponseWriter, r *http.Request) { wire.WriteJSON(w, map[string]any{"Tags": mapToTags(tags)}) } - -// firewallName resolves the firewall's store key (its name) from either the -// name or the ARN provided by the caller. Firewalls are keyed by name. -func firewallName(name, arn string) string { - if name != "" { - return name - } - - // ARN tail after "firewall/" is the name. - for i := len(arn) - 1; i >= 0; i-- { - if arn[i] == '/' { - return arn[i+1:] - } - } - - return arn -} diff --git a/server/aws/networkfirewall/dispatch_test.go b/server/aws/networkfirewall/dispatch_test.go new file mode 100644 index 000000000..ea8984409 --- /dev/null +++ b/server/aws/networkfirewall/dispatch_test.go @@ -0,0 +1,210 @@ +package networkfirewall + +import ( + "errors" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/stackshy/cloudemu/v2/config" + cerrors "github.com/stackshy/cloudemu/v2/errors" + nfmock "github.com/stackshy/cloudemu/v2/providers/aws/networkfirewall" + nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" +) + +// supportedOps is every Network Firewall operation the emulator implements +// (issue #1455 scope); the proxy, container-association, transit-gateway +// attachment and encryption families are documented non-goals. +// +//nolint:gochecknoglobals // immutable test fixture. +var supportedOps = []string{ + "CreateFirewall", "DescribeFirewall", "DeleteFirewall", "ListFirewalls", + "CreateFirewallPolicy", "DescribeFirewallPolicy", "UpdateFirewallPolicy", "DeleteFirewallPolicy", "ListFirewallPolicies", + "CreateRuleGroup", "DescribeRuleGroup", "UpdateRuleGroup", "DeleteRuleGroup", "ListRuleGroups", + "AssociateFirewallPolicy", "AssociateSubnets", "DisassociateSubnets", "UpdateFirewallDeleteProtection", + "UpdateLoggingConfiguration", "DescribeLoggingConfiguration", "TagResource", "UntagResource", "ListTagsForResource", + "UpdateFirewallDescription", "UpdateSubnetChangeProtection", "UpdateFirewallPolicyChangeProtection", + "UpdateAvailabilityZoneChangeProtection", "UpdateFirewallAnalysisSettings", + "AssociateAvailabilityZones", "DisassociateAvailabilityZones", + "PutResourcePolicy", "DescribeResourcePolicy", "DeleteResourcePolicy", + "CreateTLSInspectionConfiguration", "DescribeTLSInspectionConfiguration", "UpdateTLSInspectionConfiguration", + "DeleteTLSInspectionConfiguration", "ListTLSInspectionConfigurations", + "CreateVpcEndpointAssociation", "DescribeVpcEndpointAssociation", "DeleteVpcEndpointAssociation", "ListVpcEndpointAssociations", + "DescribeFirewallMetadata", "DescribeRuleGroupMetadata", "DescribeRuleGroupSummary", + "StartFlowCapture", "StartFlowFlush", "DescribeFlowOperation", "ListFlowOperations", "ListFlowOperationResults", + "StartAnalysisReport", "ListAnalysisReports", "GetAnalysisReportResults", +} + +func newHandler() *Handler { return New(nfmock.New(config.NewOptions())) } + +func call(h http.Handler, target, body string) *httptest.ResponseRecorder { + req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(body)) + if target != "" { + req.Header.Set("X-Amz-Target", target) + } + + rec := httptest.NewRecorder() + h.ServeHTTP(rec, req) + + return rec +} + +func TestMatches_TargetPrefix_And_RoutesTable(t *testing.T) { + h := newHandler() + + for _, tc := range []struct { + target string + want bool + }{ + {"NetworkFirewall_20201112.CreateFirewall", true}, + {"NetworkFirewall_20201112.", true}, + {"NetworkFirewall_2020.CreateFirewall", false}, + {"AWSKinesisService.ListStreams", false}, + {"", false}, + } { + req := httptest.NewRequest(http.MethodPost, "/", nil) + if tc.target != "" { + req.Header.Set("X-Amz-Target", tc.target) + } + + assert.Equal(t, tc.want, h.Matches(req), tc.target) + } + + // A plain S3-style request is never claimed (the handler cannot shadow S3). + assert.False(t, h.Matches(httptest.NewRequest(http.MethodGet, "/bucket/key", nil))) + + assert.Len(t, h.routes, len(supportedOps), "route table and supported-operation list agree") + + for _, op := range supportedOps { + assert.Contains(t, h.routes, op) + } + + rec := call(h, targetPrefix+"CreateProxy", "{}") + assert.Equal(t, http.StatusBadRequest, rec.Code) + assert.Contains(t, rec.Body.String(), "InvalidRequestException") + assert.Contains(t, rec.Body.String(), "unknown operation") + + assert.Equal(t, "network-firewall", h.IAMService()) +} + +func TestWriteErr_Mapping(t *testing.T) { + typed := nfdriver.InvalidToken("stale") + wrapped := errors.Join(errors.New("context"), typed) //nolint:err113 // test fixture + + for name, tc := range map[string]struct { + err error + status int + kind string + }{ + "typed": {typed, http.StatusBadRequest, "InvalidTokenException"}, + "typed through a wrap": {wrapped, http.StatusBadRequest, "InvalidTokenException"}, + "invalid operation": {nfdriver.InvalidOperation("x"), http.StatusBadRequest, "InvalidOperationException"}, + "resource policy": {nfdriver.InvalidResourcePolicy("x"), http.StatusBadRequest, "InvalidResourcePolicyException"}, + "not found": {cerrors.New(cerrors.NotFound, "x"), http.StatusBadRequest, "ResourceNotFoundException"}, + "already exists": {cerrors.New(cerrors.AlreadyExists, "x"), http.StatusBadRequest, "InvalidRequestException"}, + "invalid argument": {cerrors.New(cerrors.InvalidArgument, "x"), http.StatusBadRequest, "InvalidRequestException"}, + "failed precondition": {cerrors.New(cerrors.FailedPrecondition, "x"), http.StatusBadRequest, "InvalidOperationException"}, + "unexpected": {errors.New("boom"), http.StatusInternalServerError, "InternalServerError"}, //nolint:err113 // test fixture + } { + t.Run(name, func(t *testing.T) { + rec := httptest.NewRecorder() + writeErr(rec, tc.err) + + assert.Equal(t, tc.status, rec.Code) + assert.Contains(t, rec.Body.String(), tc.kind) + }) + } +} + +// baseOnly hides every optional capability of the wrapped driver: embedding the +// interface promotes only the base NetworkFirewall methods. +type baseOnly struct{ nfdriver.NetworkFirewall } + +func TestOptionalCapabilities_UnsupportedWhenAbsent(t *testing.T) { + h := New(baseOnly{nfmock.New(config.NewOptions())}) + + for _, op := range []string{ + "UpdateFirewallDescription", "UpdateSubnetChangeProtection", "AssociateAvailabilityZones", "PutResourcePolicy", + "CreateTLSInspectionConfiguration", "CreateVpcEndpointAssociation", "DescribeFirewallMetadata", "StartFlowCapture", "StartAnalysisReport", + } { + rec := call(h, targetPrefix+op, "{}") + + assert.Equal(t, http.StatusBadRequest, rec.Code, op) + assert.Contains(t, rec.Body.String(), "UnsupportedOperationException", op) + } + + // The base operations still work. + rec := call(h, targetPrefix+"ListFirewalls", "{}") + assert.Equal(t, http.StatusOK, rec.Code) +} + +func TestDecodeErrors_AreRequestErrors(t *testing.T) { + h := newHandler() + + rec := call(h, targetPrefix+"CreateFirewall", "{not json") + require.Equal(t, http.StatusBadRequest, rec.Code) +} + +func TestDuplicateCreate_IsInvalidRequest(t *testing.T) { + h := newHandler() + body := `{"FirewallPolicyName":"p","FirewallPolicy":{"StatelessDefaultActions":["aws:pass"],"StatelessFragmentDefaultActions":["aws:pass"]}}` + + require.Equal(t, http.StatusOK, call(h, targetPrefix+"CreateFirewallPolicy", body).Code) + + rec := call(h, targetPrefix+"CreateFirewallPolicy", body) + assert.Equal(t, http.StatusBadRequest, rec.Code) + assert.Contains(t, rec.Body.String(), "InvalidRequestException") +} + +func TestRuleGroupBody_Resolution(t *testing.T) { + for name, tc := range map[string]struct { + group string + rules string + wantErr bool + wantNil bool + }{ + "both": {`{"RulesSource":{}}`, "pass any", true, true}, + "neither": {"", "", false, true}, + "null": {"null", "", false, true}, + "group": {`{"RulesSource":{}}`, "", false, false}, + "rules": {"", "pass any", false, false}, + } { + t.Run(name, func(t *testing.T) { + got, err := ruleGroupBody([]byte(tc.group), tc.rules) + + assert.Equal(t, tc.wantErr, err != nil) + assert.Equal(t, tc.wantNil, got == nil) + }) + } +} + +// TestEveryOp_BadInput_IsRequestError sends every operation an empty body (no +// identifiers) and a malformed body: neither may succeed, panic or answer 5xx. +func TestEveryOp_BadInput_IsRequestError(t *testing.T) { + h := newHandler() + + // Operations with no required identifier answer an empty body successfully. + listOps := map[string]bool{ + "ListFirewalls": true, "ListFirewallPolicies": true, "ListRuleGroups": true, + "ListTLSInspectionConfigurations": true, "ListVpcEndpointAssociations": true, + } + + for _, op := range supportedOps { + t.Run(op, func(t *testing.T) { + rec := call(h, targetPrefix+op, "{}") + + if listOps[op] { + assert.Equal(t, http.StatusOK, rec.Code) + } else { + assert.Equal(t, http.StatusBadRequest, rec.Code, rec.Body.String()) + } + + bad := call(h, targetPrefix+op, "{not json") + assert.Equal(t, http.StatusBadRequest, bad.Code) + }) + } +} diff --git a/server/aws/networkfirewall/errors.go b/server/aws/networkfirewall/errors.go new file mode 100644 index 000000000..3c49558cc --- /dev/null +++ b/server/aws/networkfirewall/errors.go @@ -0,0 +1,45 @@ +package networkfirewall + +import ( + "net/http" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/server/wire" + nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" +) + +// writeErr renders err as the Network Firewall exception it represents: a typed +// driver error carries its exact exception name; everything else maps by code. +func writeErr(w http.ResponseWriter, err error) { + msg := cerrors.Message(err) + + if ex, ok := nfdriver.ExceptionOf(err); ok { + wire.WriteJSONError(w, http.StatusBadRequest, ex, msg) + return + } + + switch { + case cerrors.IsNotFound(err): + wire.WriteJSONError(w, http.StatusBadRequest, "ResourceNotFoundException", msg) + case cerrors.IsFailedPrecondition(err): + wire.WriteJSONError(w, http.StatusBadRequest, nfdriver.ExInvalidOperation, msg) + case cerrors.IsAlreadyExists(err), cerrors.IsInvalidArgument(err): + // A duplicate name is a request error: InvalidRequestException is the one + // exception every Create operation models. + wire.WriteJSONError(w, http.StatusBadRequest, "InvalidRequestException", msg) + default: + wire.WriteJSONError(w, http.StatusInternalServerError, "InternalServerError", msg) + } +} + +// capability returns the optional operation family T the driver implements, or +// writes UnsupportedOperationException and returns false. +func capability[T any](h *Handler, w http.ResponseWriter) (T, bool) { + c, ok := h.db.(T) + if !ok { + wire.WriteJSONError(w, http.StatusBadRequest, nfdriver.ExUnsupportedOperation, + "the operation is not supported by this Network Firewall implementation") + } + + return c, ok +} diff --git a/server/aws/networkfirewall/flow_operation.go b/server/aws/networkfirewall/flow_operation.go new file mode 100644 index 000000000..422cd74e7 --- /dev/null +++ b/server/aws/networkfirewall/flow_operation.go @@ -0,0 +1,194 @@ +package networkfirewall + +import ( + "encoding/json" + "net/http" + + "github.com/stackshy/cloudemu/v2/server/wire" + nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" +) + +type flowScopeRequest struct { + FirewallArn string `json:"FirewallArn"` + AvailabilityZone string `json:"AvailabilityZone"` + VpcEndpointID string `json:"VpcEndpointId"` + VpcEndpointAssociationArn string `json:"VpcEndpointAssociationArn"` +} + +func (s *flowScopeRequest) scope() nfdriver.FlowOperationScope { + return nfdriver.FlowOperationScope{ + FirewallARN: s.FirewallArn, AvailabilityZone: s.AvailabilityZone, + VPCEndpointID: s.VpcEndpointID, VPCEndpointAssociationARN: s.VpcEndpointAssociationArn, + } +} + +type startFlowRequest struct { + flowScopeRequest + FlowFilters json.RawMessage `json:"FlowFilters"` + MinimumFlowAgeInSeconds *int32 `json:"MinimumFlowAgeInSeconds"` +} + +func (s *startFlowRequest) request() nfdriver.FlowOperationRequest { + return nfdriver.FlowOperationRequest{ + FirewallARN: s.FirewallArn, AvailabilityZone: s.AvailabilityZone, VPCEndpointID: s.VpcEndpointID, + VPCEndpointAssociationARN: s.VpcEndpointAssociationArn, + MinimumFlowAgeSeconds: s.MinimumFlowAgeInSeconds, Filters: s.FlowFilters, + } +} + +func (h *Handler) startFlowCapture(w http.ResponseWriter, r *http.Request) { + h.startFlow(w, r, func(f nfdriver.FlowOperations, req nfdriver.FlowOperationRequest) (*nfdriver.FlowOperation, error) { + return f.StartFlowCapture(r.Context(), req) + }) +} + +func (h *Handler) startFlowFlush(w http.ResponseWriter, r *http.Request) { + h.startFlow(w, r, func(f nfdriver.FlowOperations, req nfdriver.FlowOperationRequest) (*nfdriver.FlowOperation, error) { + return f.StartFlowFlush(r.Context(), req) + }) +} + +func (h *Handler) startFlow( + w http.ResponseWriter, r *http.Request, + start func(nfdriver.FlowOperations, nfdriver.FlowOperationRequest) (*nfdriver.FlowOperation, error), +) { + var req startFlowRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + flows, ok := capability[nfdriver.FlowOperations](h, w) + if !ok { + return + } + + op, err := start(flows, req.request()) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, map[string]any{ + "FirewallArn": op.FirewallARN, "FlowOperationId": op.ID, "FlowOperationStatus": op.Status, + }) +} + +type describeFlowRequest struct { + flowScopeRequest + FlowOperationID string `json:"FlowOperationId"` +} + +func (h *Handler) describeFlowOperation(w http.ResponseWriter, r *http.Request) { + var req describeFlowRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + flows, ok := capability[nfdriver.FlowOperations](h, w) + if !ok { + return + } + + op, err := flows.DescribeFlowOperation(r.Context(), req.scope(), req.FlowOperationID) + if err != nil { + writeErr(w, err) + return + } + + resp := flowOperationFields(op) + resp["FlowOperation"] = map[string]any{ + "FlowFilters": op.Filters, "MinimumFlowAgeInSeconds": op.MinimumFlowAgeSeconds, + } + resp["FlowOperationType"] = op.Type + + wire.WriteJSON(w, resp) +} + +// flowOperationFields are the response members DescribeFlowOperation and +// ListFlowOperationResults share. +func flowOperationFields(op *nfdriver.FlowOperation) map[string]any { + resp := map[string]any{ + "FirewallArn": op.FirewallARN, "FlowOperationId": op.ID, "FlowOperationStatus": op.Status, + "FlowRequestTimestamp": epochSeconds(op.RequestedAt), + } + if op.AvailabilityZone != "" { + resp["AvailabilityZone"] = op.AvailabilityZone + } + + if op.VPCEndpointID != "" { + resp["VpcEndpointId"] = op.VPCEndpointID + } + + if op.VPCEndpointAssociationARN != "" { + resp["VpcEndpointAssociationArn"] = op.VPCEndpointAssociationARN + } + + if op.StatusMessage != "" { + resp["StatusMessage"] = op.StatusMessage + } + + return resp +} + +type listFlowOperationsRequest struct { + flowScopeRequest + listRequest + FlowOperationType string `json:"FlowOperationType"` +} + +func (h *Handler) listFlowOperations(w http.ResponseWriter, r *http.Request) { + var req listFlowOperationsRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + flows, ok := capability[nfdriver.FlowOperations](h, w) + if !ok { + return + } + + items, next, err := flows.ListFlowOperations(r.Context(), req.scope(), req.FlowOperationType, req.options()) + if err != nil { + writeErr(w, err) + return + } + + metas := make([]map[string]any, 0, len(items)) + for i := range items { + metas = append(metas, map[string]any{ + "FlowOperationId": items[i].ID, "FlowOperationType": items[i].Type, + "FlowOperationStatus": items[i].Status, "FlowRequestTimestamp": epochSeconds(items[i].RequestedAt), + }) + } + + wire.WriteJSON(w, withNextToken(map[string]any{"FlowOperations": metas}, next)) +} + +type listFlowResultsRequest struct { + flowScopeRequest + listRequest + FlowOperationID string `json:"FlowOperationId"` +} + +func (h *Handler) listFlowOperationResults(w http.ResponseWriter, r *http.Request) { + var req listFlowResultsRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + flows, ok := capability[nfdriver.FlowOperations](h, w) + if !ok { + return + } + + op, next, err := flows.ListFlowOperationResults(r.Context(), req.scope(), req.FlowOperationID, req.options()) + if err != nil { + writeErr(w, err) + return + } + + resp := flowOperationFields(op) + resp["Flows"] = []map[string]any{} + + wire.WriteJSON(w, withNextToken(resp, next)) +} diff --git a/server/aws/networkfirewall/handler.go b/server/aws/networkfirewall/handler.go index 555c996f0..f580dec5c 100644 --- a/server/aws/networkfirewall/handler.go +++ b/server/aws/networkfirewall/handler.go @@ -8,7 +8,6 @@ import ( "net/http" "strings" - cerrors "github.com/stackshy/cloudemu/v2/errors" "github.com/stackshy/cloudemu/v2/server/wire" nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" ) @@ -16,13 +15,20 @@ import ( const targetPrefix = "NetworkFirewall_20201112." // Handler serves Network Firewall requests against a networkfirewall driver. +// Operation families beyond the base driver interface are optional capability +// interfaces; an operation whose family the driver lacks answers +// UnsupportedOperationException. type Handler struct { - db nfdriver.NetworkFirewall + db nfdriver.NetworkFirewall + routes map[string]http.HandlerFunc } // New returns a Network Firewall handler backed by db. func New(db nfdriver.NetworkFirewall) *Handler { - return &Handler{db: db} + h := &Handler{db: db} + h.routes = h.buildRoutes() + + return h } // Matches claims requests whose X-Amz-Target names a Network Firewall operation. @@ -31,77 +37,74 @@ func (*Handler) Matches(r *http.Request) bool { } // ServeHTTP dispatches on the operation named in X-Amz-Target. -// -//nolint:gocyclo // a flat operation switch is the clearest dispatch shape. func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { op := strings.TrimPrefix(r.Header.Get("X-Amz-Target"), targetPrefix) - switch op { - case "CreateFirewall": - h.createFirewall(w, r) - case "DescribeFirewall": - h.describeFirewall(w, r) - case "DeleteFirewall": - h.deleteFirewall(w, r) - case "ListFirewalls": - h.listFirewalls(w, r) - case "CreateFirewallPolicy": - h.createFirewallPolicy(w, r) - case "DescribeFirewallPolicy": - h.describeFirewallPolicy(w, r) - case "UpdateFirewallPolicy": - h.updateFirewallPolicy(w, r) - case "DeleteFirewallPolicy": - h.deleteFirewallPolicy(w, r) - case "ListFirewallPolicies": - h.listFirewallPolicies(w, r) - case "CreateRuleGroup": - h.createRuleGroup(w, r) - case "DescribeRuleGroup": - h.describeRuleGroup(w, r) - case "UpdateRuleGroup": - h.updateRuleGroup(w, r) - case "DeleteRuleGroup": - h.deleteRuleGroup(w, r) - case "ListRuleGroups": - h.listRuleGroups(w, r) - case "AssociateFirewallPolicy": - h.associateFirewallPolicy(w, r) - case "AssociateSubnets": - h.associateSubnets(w, r) - case "DisassociateSubnets": - h.disassociateSubnets(w, r) - case "UpdateFirewallDeleteProtection": - h.updateFirewallDeleteProtection(w, r) - case "UpdateLoggingConfiguration": - h.updateLoggingConfiguration(w, r) - case "DescribeLoggingConfiguration": - h.describeLoggingConfiguration(w, r) - case "TagResource": - h.tagResource(w, r) - case "UntagResource": - h.untagResource(w, r) - case "ListTagsForResource": - h.listTagsForResource(w, r) - default: + route, ok := h.routes[op] + if !ok { wire.WriteJSONError(w, http.StatusBadRequest, "InvalidRequestException", "unknown operation: "+op) + return } -} -const updateToken = "00000000-0000-0000-0000-000000000000" - -func writeErr(w http.ResponseWriter, err error) { - msg := cerrors.Message(err) + route(w, r) +} - switch { - case cerrors.IsNotFound(err): - wire.WriteJSONError(w, http.StatusBadRequest, "ResourceNotFoundException", msg) - case cerrors.IsAlreadyExists(err): - wire.WriteJSONError(w, http.StatusBadRequest, "InvalidOperationException", msg) - case cerrors.IsInvalidArgument(err), cerrors.IsFailedPrecondition(err): - wire.WriteJSONError(w, http.StatusBadRequest, "InvalidRequestException", msg) - default: - wire.WriteJSONError(w, http.StatusInternalServerError, "InternalServerError", msg) +// buildRoutes is the operation table: one entry per supported operation. +func (h *Handler) buildRoutes() map[string]http.HandlerFunc { + return map[string]http.HandlerFunc{ + "CreateFirewall": h.createFirewall, + "DescribeFirewall": h.describeFirewall, + "DeleteFirewall": h.deleteFirewall, + "ListFirewalls": h.listFirewalls, + "CreateFirewallPolicy": h.createFirewallPolicy, + "DescribeFirewallPolicy": h.describeFirewallPolicy, + "UpdateFirewallPolicy": h.updateFirewallPolicy, + "DeleteFirewallPolicy": h.deleteFirewallPolicy, + "ListFirewallPolicies": h.listFirewallPolicies, + "CreateRuleGroup": h.createRuleGroup, + "DescribeRuleGroup": h.describeRuleGroup, + "UpdateRuleGroup": h.updateRuleGroup, + "DeleteRuleGroup": h.deleteRuleGroup, + "ListRuleGroups": h.listRuleGroups, + "AssociateFirewallPolicy": h.associateFirewallPolicy, + "AssociateSubnets": h.associateSubnets, + "DisassociateSubnets": h.disassociateSubnets, + "UpdateFirewallDeleteProtection": h.updateFirewallDeleteProtection, + "UpdateLoggingConfiguration": h.updateLoggingConfiguration, + "DescribeLoggingConfiguration": h.describeLoggingConfiguration, + "TagResource": h.tagResource, + "UntagResource": h.untagResource, + "ListTagsForResource": h.listTagsForResource, + "UpdateFirewallDescription": h.updateFirewallDescription, + "UpdateSubnetChangeProtection": h.updateSubnetChangeProtection, + "UpdateFirewallPolicyChangeProtection": h.updateFirewallPolicyChangeProtection, + "UpdateAvailabilityZoneChangeProtection": h.updateAvailabilityZoneChangeProtection, + "UpdateFirewallAnalysisSettings": h.updateFirewallAnalysisSettings, + "AssociateAvailabilityZones": h.associateAvailabilityZones, + "DisassociateAvailabilityZones": h.disassociateAvailabilityZones, + "PutResourcePolicy": h.putResourcePolicy, + "DescribeResourcePolicy": h.describeResourcePolicy, + "DeleteResourcePolicy": h.deleteResourcePolicy, + "CreateTLSInspectionConfiguration": h.createTLSInspectionConfiguration, + "DescribeTLSInspectionConfiguration": h.describeTLSInspectionConfiguration, + "UpdateTLSInspectionConfiguration": h.updateTLSInspectionConfiguration, + "DeleteTLSInspectionConfiguration": h.deleteTLSInspectionConfiguration, + "ListTLSInspectionConfigurations": h.listTLSInspectionConfigurations, + "CreateVpcEndpointAssociation": h.createVpcEndpointAssociation, + "DescribeVpcEndpointAssociation": h.describeVpcEndpointAssociation, + "DeleteVpcEndpointAssociation": h.deleteVpcEndpointAssociation, + "ListVpcEndpointAssociations": h.listVpcEndpointAssociations, + "DescribeFirewallMetadata": h.describeFirewallMetadata, + "DescribeRuleGroupMetadata": h.describeRuleGroupMetadata, + "DescribeRuleGroupSummary": h.describeRuleGroupSummary, + "StartFlowCapture": h.startFlowCapture, + "StartFlowFlush": h.startFlowFlush, + "DescribeFlowOperation": h.describeFlowOperation, + "ListFlowOperations": h.listFlowOperations, + "ListFlowOperationResults": h.listFlowOperationResults, + "StartAnalysisReport": h.startAnalysisReport, + "ListAnalysisReports": h.listAnalysisReports, + "GetAnalysisReportResults": h.getAnalysisReportResults, } } diff --git a/server/aws/networkfirewall/logging.go b/server/aws/networkfirewall/logging.go new file mode 100644 index 000000000..2bec7cdb6 --- /dev/null +++ b/server/aws/networkfirewall/logging.go @@ -0,0 +1,120 @@ +package networkfirewall + +import ( + "net/http" + + "github.com/stackshy/cloudemu/v2/server/wire" + nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" +) + +type logDestinationConfig struct { + LogType string `json:"LogType"` + LogDestinationType string `json:"LogDestinationType,omitempty"` + LogDestination map[string]string `json:"LogDestination,omitempty"` +} + +type loggingConfiguration struct { + LogDestinationConfigs []logDestinationConfig `json:"LogDestinationConfigs"` +} + +type loggingRequest struct { + FirewallName string `json:"FirewallName"` + FirewallArn string `json:"FirewallArn"` + LoggingConfiguration *loggingConfiguration `json:"LoggingConfiguration"` + EnableMonitoringDashboard *bool `json:"EnableMonitoringDashboard"` +} + +func (h *Handler) updateLoggingConfiguration(w http.ResponseWriter, r *http.Request) { + var req loggingRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + name, ok := h.resolveFirewall(w, r, req.FirewallName, req.FirewallArn) + if !ok { + return + } + + cfg := &nfdriver.LoggingConfiguration{EnableMonitoringDashboard: req.EnableMonitoringDashboard} + if req.LoggingConfiguration != nil { + cfg.Destinations = toLogConfigs(req.LoggingConfiguration.LogDestinationConfigs) + } + + if err := h.db.UpdateLoggingConfiguration(r.Context(), name, cfg); err != nil { + writeErr(w, err) + return + } + + h.writeLoggingConfiguration(w, r, name, true) +} + +func (h *Handler) describeLoggingConfiguration(w http.ResponseWriter, r *http.Request) { + var req nameArnRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + name, ok := h.resolveFirewall(w, r, req.FirewallName, req.FirewallArn) + if !ok { + return + } + + h.writeLoggingConfiguration(w, r, name, false) +} + +// writeLoggingConfiguration reads the stored configuration back and renders it, +// so both the update and describe responses reflect what is stored (the update +// response also names the firewall). +func (h *Handler) writeLoggingConfiguration(w http.ResponseWriter, r *http.Request, name string, withName bool) { + cfg, err := h.db.DescribeLoggingConfiguration(r.Context(), name) + if err != nil { + writeErr(w, err) + return + } + + fw, err := h.db.DescribeFirewall(r.Context(), name, "") + if err != nil { + writeErr(w, err) + return + } + + resp := map[string]any{ + "FirewallArn": fw.ARN, + "LoggingConfiguration": loggingConfiguration{LogDestinationConfigs: fromLogConfigs(cfg.Destinations)}, + } + if cfg.EnableMonitoringDashboard != nil { + resp["EnableMonitoringDashboard"] = *cfg.EnableMonitoringDashboard + } + + if withName { + resp["FirewallName"] = name + } + + wire.WriteJSON(w, resp) +} + +func toLogConfigs(cs []logDestinationConfig) []nfdriver.LogDestinationConfig { + if len(cs) == 0 { + return nil + } + + out := make([]nfdriver.LogDestinationConfig, 0, len(cs)) + for _, c := range cs { + out = append(out, nfdriver.LogDestinationConfig{ + LogType: c.LogType, LogDestinationType: c.LogDestinationType, LogDestination: c.LogDestination, + }) + } + + return out +} + +func fromLogConfigs(cs []nfdriver.LogDestinationConfig) []logDestinationConfig { + out := make([]logDestinationConfig, 0, len(cs)) + for _, c := range cs { + out = append(out, logDestinationConfig{ + LogType: c.LogType, LogDestinationType: c.LogDestinationType, LogDestination: c.LogDestination, + }) + } + + return out +} diff --git a/server/aws/networkfirewall/metadata.go b/server/aws/networkfirewall/metadata.go new file mode 100644 index 000000000..7a1a7d0c1 --- /dev/null +++ b/server/aws/networkfirewall/metadata.go @@ -0,0 +1,111 @@ +package networkfirewall + +import ( + "net/http" + + "github.com/stackshy/cloudemu/v2/server/wire" + nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" +) + +type firewallArnRequest struct { + FirewallArn string `json:"FirewallArn"` +} + +func (h *Handler) describeFirewallMetadata(w http.ResponseWriter, r *http.Request) { + var req firewallArnRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + meta, ok := capability[nfdriver.Metadata](h, w) + if !ok { + return + } + + m, err := meta.DescribeFirewallMetadata(r.Context(), req.FirewallArn) + if err != nil { + writeErr(w, err) + return + } + + zones := make(map[string]map[string]string, len(m.AvailabilityZones)) + for _, z := range m.AvailabilityZones { + zones[z] = map[string]string{} + } + + resp := map[string]any{ + "FirewallArn": m.FirewallARN, "FirewallPolicyArn": m.PolicyARN, + "Status": m.Status, "SupportedAvailabilityZones": zones, + } + withDescription(resp, m.Description) + + wire.WriteJSON(w, resp) +} + +func (h *Handler) describeRuleGroupMetadata(w http.ResponseWriter, r *http.Request) { + var req ruleGroupNameArnRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + meta, ok := capability[nfdriver.Metadata](h, w) + if !ok { + return + } + + rg, err := meta.DescribeRuleGroupMetadata(r.Context(), req.RuleGroupName, req.RuleGroupArn, req.Type) + if err != nil { + writeErr(w, err) + return + } + + resp := map[string]any{ + "RuleGroupArn": rg.ARN, "RuleGroupName": rg.Name, "Capacity": rg.Capacity, + "LastModifiedTime": epochSeconds(rg.LastModified), "Type": rg.Type, + } + withDescription(resp, rg.Description) + + wire.WriteJSON(w, resp) +} + +type ruleSummaryJSON struct { + Msg string `json:"Msg,omitempty"` + SID string `json:"SID,omitempty"` + Metadata string `json:"Metadata,omitempty"` +} + +func (h *Handler) describeRuleGroupSummary(w http.ResponseWriter, r *http.Request) { + var req ruleGroupNameArnRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + meta, ok := capability[nfdriver.Metadata](h, w) + if !ok { + return + } + + rg, summaries, err := meta.DescribeRuleGroupSummary(r.Context(), req.RuleGroupName, req.RuleGroupArn, req.Type) + if err != nil { + writeErr(w, err) + return + } + + out := make([]ruleSummaryJSON, 0, len(summaries)) + for _, s := range summaries { + out = append(out, ruleSummaryJSON{Msg: s.Msg, SID: s.SID, Metadata: s.Metadata}) + } + + resp := map[string]any{"RuleGroupName": rg.Name, "Summary": map[string]any{"RuleSummaries": out}} + withDescription(resp, rg.Description) + + wire.WriteJSON(w, resp) +} + +// withDescription adds Description to a response only when one is set, as AWS +// omits an absent description rather than returning an empty string. +func withDescription(resp map[string]any, description string) { + if description != "" { + resp["Description"] = description + } +} diff --git a/server/aws/networkfirewall/operations.go b/server/aws/networkfirewall/operations.go index 086fe8579..8b4791f49 100644 --- a/server/aws/networkfirewall/operations.go +++ b/server/aws/networkfirewall/operations.go @@ -4,32 +4,53 @@ import ( "encoding/json" "net/http" "strings" + "time" "github.com/stackshy/cloudemu/v2/server/wire" nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" ) +const millisPerSecond = 1000 + type tag struct { Key string `json:"Key"` Value string `json:"Value"` } type subnetMapping struct { - SubnetID string `json:"SubnetId"` + SubnetID string `json:"SubnetId"` + IPAddressType string `json:"IPAddressType,omitempty"` +} + +type zoneMapping struct { + AvailabilityZone string `json:"AvailabilityZone"` +} + +// epochSeconds renders a timestamp the way AWS JSON 1.0 does (seconds since the +// epoch, fractional), which is what the SDK deserializer expects. +func epochSeconds(t time.Time) float64 { + return float64(t.UnixMilli()) / millisPerSecond } // ---- Firewall ---- type firewallJSON struct { - FirewallName string `json:"FirewallName"` - FirewallArn string `json:"FirewallArn"` - FirewallID string `json:"FirewallId,omitempty"` - FirewallPolicyArn string `json:"FirewallPolicyArn,omitempty"` - VpcID string `json:"VpcId,omitempty"` - SubnetMappings []subnetMapping `json:"SubnetMappings,omitempty"` - Description string `json:"Description,omitempty"` - DeleteProtection bool `json:"DeleteProtection"` - Tags []tag `json:"Tags,omitempty"` + FirewallName string `json:"FirewallName"` + FirewallArn string `json:"FirewallArn"` + FirewallID string `json:"FirewallId,omitempty"` + FirewallPolicyArn string `json:"FirewallPolicyArn,omitempty"` + VpcID string `json:"VpcId,omitempty"` + SubnetMappings []subnetMapping `json:"SubnetMappings,omitempty"` + AvailabilityZoneMappings []zoneMapping `json:"AvailabilityZoneMappings,omitempty"` + TransitGatewayID string `json:"TransitGatewayId,omitempty"` + Description string `json:"Description,omitempty"` + DeleteProtection bool `json:"DeleteProtection"` + SubnetChangeProtection bool `json:"SubnetChangeProtection"` + FirewallPolicyChangeProtection bool `json:"FirewallPolicyChangeProtection"` + AvailabilityZoneChangeProtection bool `json:"AvailabilityZoneChangeProtection"` + EnabledAnalysisTypes []string `json:"EnabledAnalysisTypes,omitempty"` + NumberOfAssociations int `json:"NumberOfAssociations"` + Tags []tag `json:"Tags,omitempty"` } type attachmentJSON struct { @@ -48,12 +69,6 @@ type firewallStatusJSON struct { SyncStates map[string]syncStateJSON `json:"SyncStates,omitempty"` } -// azSuffixes indexes subnets to Availability Zone letters for synthetic -// SyncStates keys. -// -//nolint:gochecknoglobals // immutable lookup table. -var azSuffixes = [...]string{"a", "b", "c", "d", "e", "f"} - // toFirewallStatusJSON builds the FirewallStatus block. A firewall in the // emulator is always fully provisioned, so it reports READY / IN_SYNC with one // SyncState per attached subnet keyed by a synthetic Availability Zone. @@ -62,14 +77,17 @@ func toFirewallStatusJSON(f *nfdriver.Firewall) firewallStatusJSON { states := make(map[string]syncStateJSON, len(f.SubnetIDs)) for i, subnet := range f.SubnetIDs { - az := region + azSuffixes[i%len(azSuffixes)] - states[az] = syncStateJSON{Attachment: attachmentJSON{ + states[nfdriver.AvailabilityZoneFor(region, i)] = syncStateJSON{Attachment: attachmentJSON{ SubnetID: subnet, EndpointID: "vpce-" + f.ID, Status: "READY", }} } + for _, az := range f.AvailabilityZones { + states[az] = syncStateJSON{Attachment: attachmentJSON{EndpointID: "vpce-" + f.ID, Status: "READY"}} + } + if len(states) == 0 { states = nil } @@ -93,13 +111,19 @@ func arnRegion(arn string) string { } type createFirewallRequest struct { - FirewallName string `json:"FirewallName"` - FirewallPolicyArn string `json:"FirewallPolicyArn"` - VpcID string `json:"VpcId"` - SubnetMappings []subnetMapping `json:"SubnetMappings"` - Description string `json:"Description"` - DeleteProtection bool `json:"DeleteProtection"` - Tags []tag `json:"Tags"` + FirewallName string `json:"FirewallName"` + FirewallPolicyArn string `json:"FirewallPolicyArn"` + VpcID string `json:"VpcId"` + SubnetMappings []subnetMapping `json:"SubnetMappings"` + AvailabilityZoneMappings []zoneMapping `json:"AvailabilityZoneMappings"` + TransitGatewayID string `json:"TransitGatewayId"` + Description string `json:"Description"` + DeleteProtection bool `json:"DeleteProtection"` + SubnetChangeProtection bool `json:"SubnetChangeProtection"` + FirewallPolicyChangeProtection bool `json:"FirewallPolicyChangeProtection"` + AvailabilityZoneChangeProtection bool `json:"AvailabilityZoneChangeProtection"` + EnabledAnalysisTypes []string `json:"EnabledAnalysisTypes"` + Tags []tag `json:"Tags"` } type nameArnRequest struct { @@ -114,13 +138,19 @@ func (h *Handler) createFirewall(w http.ResponseWriter, r *http.Request) { } fw, err := h.db.CreateFirewall(r.Context(), nfdriver.CreateFirewallConfig{ - Name: req.FirewallName, - PolicyARN: req.FirewallPolicyArn, - VPCID: req.VpcID, - SubnetIDs: subnetIDs(req.SubnetMappings), - Description: req.Description, - DeleteProtection: req.DeleteProtection, - Tags: tagsToMap(req.Tags), + Name: req.FirewallName, + PolicyARN: req.FirewallPolicyArn, + VPCID: req.VpcID, + SubnetIDs: subnetIDs(req.SubnetMappings), + AvailabilityZones: zoneNames(req.AvailabilityZoneMappings), + TransitGatewayID: req.TransitGatewayID, + Description: req.Description, + DeleteProtection: req.DeleteProtection, + SubnetChangeProtection: req.SubnetChangeProtection, + FirewallPolicyChangeProtection: req.FirewallPolicyChangeProtection, + AvailabilityZoneChangeProtection: req.AvailabilityZoneChangeProtection, + EnabledAnalysisTypes: req.EnabledAnalysisTypes, + Tags: tagsToMap(req.Tags), }) if err != nil { writeErr(w, err) @@ -146,7 +176,7 @@ func (h *Handler) describeFirewall(w http.ResponseWriter, r *http.Request) { } wire.WriteJSON(w, map[string]any{ - "UpdateToken": updateToken, + "UpdateToken": fw.UpdateToken, "Firewall": toFirewallJSON(fw), "FirewallStatus": toFirewallStatusJSON(fw), }) @@ -170,168 +200,19 @@ func (h *Handler) deleteFirewall(w http.ResponseWriter, r *http.Request) { }) } -func (h *Handler) listFirewalls(w http.ResponseWriter, r *http.Request) { - items, err := h.db.ListFirewalls(r.Context()) - if err != nil { - writeErr(w, err) - return - } - - metas := make([]map[string]string, 0, len(items)) - for i := range items { - metas = append(metas, map[string]string{"FirewallName": items[i].Name, "FirewallArn": items[i].ARN}) - } - - wire.WriteJSON(w, map[string]any{"Firewalls": metas}) +type listFirewallsRequest struct { + MaxResults int `json:"MaxResults"` + NextToken string `json:"NextToken"` + VpcIDs []string `json:"VpcIds"` } -func toFirewallJSON(f *nfdriver.Firewall) firewallJSON { - mappings := make([]subnetMapping, 0, len(f.SubnetIDs)) - for _, s := range f.SubnetIDs { - mappings = append(mappings, subnetMapping{SubnetID: s}) - } - - return firewallJSON{ - FirewallName: f.Name, FirewallArn: f.ARN, FirewallID: f.ID, FirewallPolicyArn: f.PolicyARN, - VpcID: f.VPCID, SubnetMappings: mappings, Description: f.Description, - DeleteProtection: f.DeleteProtection, Tags: mapToTags(f.Tags), - } -} - -// ---- Firewall Policy ---- - -type firewallPolicyResponseJSON struct { - FirewallPolicyName string `json:"FirewallPolicyName"` - FirewallPolicyArn string `json:"FirewallPolicyArn"` - FirewallPolicyID string `json:"FirewallPolicyId"` - Description string `json:"Description,omitempty"` - Tags []tag `json:"Tags,omitempty"` -} - -type ruleGroupReferenceJSON struct { - ResourceArn string `json:"ResourceArn"` - Priority int `json:"Priority,omitempty"` -} - -type firewallPolicyDetailJSON struct { - StatelessDefaultActions []string `json:"StatelessDefaultActions,omitempty"` - StatelessFragmentDefaultActions []string `json:"StatelessFragmentDefaultActions,omitempty"` - StatefulRuleGroupReferences []ruleGroupReferenceJSON `json:"StatefulRuleGroupReferences,omitempty"` - StatelessRuleGroupReferences []ruleGroupReferenceJSON `json:"StatelessRuleGroupReferences,omitempty"` -} - -type createFirewallPolicyRequest struct { - FirewallPolicyName string `json:"FirewallPolicyName"` - FirewallPolicy firewallPolicyDetailJSON `json:"FirewallPolicy"` - Description string `json:"Description"` - Tags []tag `json:"Tags"` -} - -type policyNameArnRequest struct { - FirewallPolicyName string `json:"FirewallPolicyName"` - FirewallPolicyArn string `json:"FirewallPolicyArn"` -} - -func (h *Handler) createFirewallPolicy(w http.ResponseWriter, r *http.Request) { - var req createFirewallPolicyRequest - if !wire.DecodeJSON(w, r, &req) { - return - } - - p, err := h.db.CreateFirewallPolicy(r.Context(), nfdriver.CreateFirewallPolicyConfig{ - Name: req.FirewallPolicyName, - Description: req.Description, - StatelessDefaultActions: req.FirewallPolicy.StatelessDefaultActions, - StatelessFragmentDefaultActions: req.FirewallPolicy.StatelessFragmentDefaultActions, - StatefulRuleGroupReferences: toRuleGroupRefs(req.FirewallPolicy.StatefulRuleGroupReferences), - StatelessRuleGroupReferences: toRuleGroupRefs(req.FirewallPolicy.StatelessRuleGroupReferences), - Tags: tagsToMap(req.Tags), - }) - if err != nil { - writeErr(w, err) - return - } - - wire.WriteJSON(w, map[string]any{ - "UpdateToken": updateToken, - "FirewallPolicyResponse": toPolicyResponseJSON(p), - }) -} - -func (h *Handler) describeFirewallPolicy(w http.ResponseWriter, r *http.Request) { - var req policyNameArnRequest - if !wire.DecodeJSON(w, r, &req) { - return - } - - p, err := h.db.DescribeFirewallPolicy(r.Context(), req.FirewallPolicyName, req.FirewallPolicyArn) - if err != nil { - writeErr(w, err) - return - } - - wire.WriteJSON(w, map[string]any{ - "UpdateToken": updateToken, - "FirewallPolicyResponse": toPolicyResponseJSON(p), - "FirewallPolicy": firewallPolicyDetailJSON{ - StatelessDefaultActions: p.StatelessDefaultActions, - StatelessFragmentDefaultActions: p.StatelessFragmentDefaultActions, - StatefulRuleGroupReferences: fromRuleGroupRefs(p.StatefulRuleGroupReferences), - StatelessRuleGroupReferences: fromRuleGroupRefs(p.StatelessRuleGroupReferences), - }, - }) -} - -type updateFirewallPolicyRequest struct { - UpdateToken string `json:"UpdateToken"` - FirewallPolicyName string `json:"FirewallPolicyName"` - FirewallPolicyArn string `json:"FirewallPolicyArn"` - FirewallPolicy firewallPolicyDetailJSON `json:"FirewallPolicy"` - Description string `json:"Description"` -} - -func (h *Handler) updateFirewallPolicy(w http.ResponseWriter, r *http.Request) { - var req updateFirewallPolicyRequest - if !wire.DecodeJSON(w, r, &req) { - return - } - - p, err := h.db.UpdateFirewallPolicy(r.Context(), req.FirewallPolicyName, req.FirewallPolicyArn, - nfdriver.UpdateFirewallPolicyConfig{ - Description: req.Description, - StatelessDefaultActions: req.FirewallPolicy.StatelessDefaultActions, - StatelessFragmentDefaultActions: req.FirewallPolicy.StatelessFragmentDefaultActions, - StatefulRuleGroupReferences: toRuleGroupRefs(req.FirewallPolicy.StatefulRuleGroupReferences), - StatelessRuleGroupReferences: toRuleGroupRefs(req.FirewallPolicy.StatelessRuleGroupReferences), - }) - if err != nil { - writeErr(w, err) - return - } - - wire.WriteJSON(w, map[string]any{ - "UpdateToken": updateToken, - "FirewallPolicyResponse": toPolicyResponseJSON(p), - }) -} - -func (h *Handler) deleteFirewallPolicy(w http.ResponseWriter, r *http.Request) { - var req policyNameArnRequest +func (h *Handler) listFirewalls(w http.ResponseWriter, r *http.Request) { + var req listFirewallsRequest if !wire.DecodeJSON(w, r, &req) { return } - p, err := h.db.DeleteFirewallPolicy(r.Context(), req.FirewallPolicyName, req.FirewallPolicyArn) - if err != nil { - writeErr(w, err) - return - } - - wire.WriteJSON(w, map[string]any{"FirewallPolicyResponse": toPolicyResponseJSON(p)}) -} - -func (h *Handler) listFirewallPolicies(w http.ResponseWriter, r *http.Request) { - items, err := h.db.ListFirewallPolicies(r.Context()) + items, next, err := h.db.ListFirewalls(r.Context(), req.VpcIDs, nfdriver.ListOptions{MaxResults: req.MaxResults, NextToken: req.NextToken}) if err != nil { writeErr(w, err) return @@ -339,165 +220,67 @@ func (h *Handler) listFirewallPolicies(w http.ResponseWriter, r *http.Request) { metas := make([]map[string]string, 0, len(items)) for i := range items { - metas = append(metas, map[string]string{"Name": items[i].Name, "Arn": items[i].ARN}) - } - - wire.WriteJSON(w, map[string]any{"FirewallPolicies": metas}) -} - -func toPolicyResponseJSON(p *nfdriver.FirewallPolicy) firewallPolicyResponseJSON { - return firewallPolicyResponseJSON{ - FirewallPolicyName: p.Name, FirewallPolicyArn: p.ARN, FirewallPolicyID: p.ID, - Description: p.Description, Tags: mapToTags(p.Tags), + metas = append(metas, map[string]string{"FirewallName": items[i].Name, "FirewallArn": items[i].ARN}) } -} - -// ---- Rule Group ---- - -type ruleGroupResponseJSON struct { - RuleGroupName string `json:"RuleGroupName"` - RuleGroupArn string `json:"RuleGroupArn"` - RuleGroupID string `json:"RuleGroupId"` - Type string `json:"Type"` - Capacity int `json:"Capacity,omitempty"` - Description string `json:"Description,omitempty"` - Tags []tag `json:"Tags,omitempty"` -} - -type createRuleGroupRequest struct { - RuleGroupName string `json:"RuleGroupName"` - RuleGroup json.RawMessage `json:"RuleGroup"` - Rules string `json:"Rules"` - Type string `json:"Type"` - Capacity int `json:"Capacity"` - Description string `json:"Description"` - Tags []tag `json:"Tags"` -} -type ruleGroupNameArnRequest struct { - RuleGroupName string `json:"RuleGroupName"` - RuleGroupArn string `json:"RuleGroupArn"` - Type string `json:"Type"` + wire.WriteJSON(w, withNextToken(map[string]any{"Firewalls": metas}, next)) } -func (h *Handler) createRuleGroup(w http.ResponseWriter, r *http.Request) { - var req createRuleGroupRequest - if !wire.DecodeJSON(w, r, &req) { - return +// withNextToken adds NextToken to a list response when there is another page. +func withNextToken(resp map[string]any, next string) map[string]any { + if next != "" { + resp["NextToken"] = next } - rg, err := h.db.CreateRuleGroup(r.Context(), nfdriver.CreateRuleGroupConfig{ - Name: req.RuleGroupName, Type: req.Type, Capacity: req.Capacity, - Description: req.Description, Rules: ruleGroupPayload(req.RuleGroup, req.Rules), Tags: tagsToMap(req.Tags), - }) - if err != nil { - writeErr(w, err) - return - } - - wire.WriteJSON(w, map[string]any{ - "UpdateToken": updateToken, - "RuleGroupResponse": toRuleGroupResponseJSON(rg), - "RuleGroup": rg.Rules, - }) + return resp } -func (h *Handler) describeRuleGroup(w http.ResponseWriter, r *http.Request) { - var req ruleGroupNameArnRequest - if !wire.DecodeJSON(w, r, &req) { - return - } - - rg, err := h.db.DescribeRuleGroup(r.Context(), req.RuleGroupName, req.RuleGroupArn, req.Type) - if err != nil { - writeErr(w, err) - return +func toFirewallJSON(f *nfdriver.Firewall) firewallJSON { + mappings := make([]subnetMapping, 0, len(f.SubnetIDs)) + for _, s := range f.SubnetIDs { + mappings = append(mappings, subnetMapping{SubnetID: s}) } - wire.WriteJSON(w, map[string]any{ - "UpdateToken": updateToken, - "RuleGroupResponse": toRuleGroupResponseJSON(rg), - "RuleGroup": rg.Rules, - }) -} - -type updateRuleGroupRequest struct { - UpdateToken string `json:"UpdateToken"` - RuleGroupName string `json:"RuleGroupName"` - RuleGroupArn string `json:"RuleGroupArn"` - RuleGroup json.RawMessage `json:"RuleGroup"` - Rules string `json:"Rules"` - Type string `json:"Type"` - Description string `json:"Description"` -} - -func (h *Handler) updateRuleGroup(w http.ResponseWriter, r *http.Request) { - var req updateRuleGroupRequest - if !wire.DecodeJSON(w, r, &req) { - return + zones := make([]zoneMapping, 0, len(f.AvailabilityZones)) + for _, z := range f.AvailabilityZones { + zones = append(zones, zoneMapping{AvailabilityZone: z}) } - rg, err := h.db.UpdateRuleGroup(r.Context(), req.RuleGroupName, req.RuleGroupArn, req.Type, - nfdriver.UpdateRuleGroupConfig{Description: req.Description, Rules: ruleGroupPayload(req.RuleGroup, req.Rules)}) - if err != nil { - writeErr(w, err) - return + return firewallJSON{ + FirewallName: f.Name, FirewallArn: f.ARN, FirewallID: f.ID, FirewallPolicyArn: f.PolicyARN, + VpcID: f.VPCID, SubnetMappings: mappings, AvailabilityZoneMappings: zones, + TransitGatewayID: f.TransitGatewayID, Description: f.Description, + DeleteProtection: f.DeleteProtection, SubnetChangeProtection: f.SubnetChangeProtection, + FirewallPolicyChangeProtection: f.FirewallPolicyChangeProtection, + AvailabilityZoneChangeProtection: f.AvailabilityZoneChangeProtection, + EnabledAnalysisTypes: f.EnabledAnalysisTypes, NumberOfAssociations: f.NumberOfAssociations, + Tags: mapToTags(f.Tags), } - - wire.WriteJSON(w, map[string]any{ - "UpdateToken": updateToken, - "RuleGroupResponse": toRuleGroupResponseJSON(rg), - "RuleGroup": rg.Rules, - }) } -func (h *Handler) deleteRuleGroup(w http.ResponseWriter, r *http.Request) { - var req ruleGroupNameArnRequest - if !wire.DecodeJSON(w, r, &req) { - return - } - - rg, err := h.db.DeleteRuleGroup(r.Context(), req.RuleGroupName, req.RuleGroupArn, req.Type) - if err != nil { - writeErr(w, err) - return - } - - wire.WriteJSON(w, map[string]any{"RuleGroupResponse": toRuleGroupResponseJSON(rg)}) -} +// ---- helpers ---- -func (h *Handler) listRuleGroups(w http.ResponseWriter, r *http.Request) { - items, err := h.db.ListRuleGroups(r.Context()) - if err != nil { - writeErr(w, err) - return +func subnetIDs(mappings []subnetMapping) []string { + if len(mappings) == 0 { + return nil } - metas := make([]map[string]string, 0, len(items)) - for i := range items { - metas = append(metas, map[string]string{"Name": items[i].Name, "Arn": items[i].ARN}) + out := make([]string, 0, len(mappings)) + for _, m := range mappings { + out = append(out, m.SubnetID) } - wire.WriteJSON(w, map[string]any{"RuleGroups": metas}) -} - -func toRuleGroupResponseJSON(rg *nfdriver.RuleGroup) ruleGroupResponseJSON { - return ruleGroupResponseJSON{ - RuleGroupName: rg.Name, RuleGroupArn: rg.ARN, RuleGroupID: rg.ID, Type: rg.Type, - Capacity: rg.Capacity, Description: rg.Description, Tags: mapToTags(rg.Tags), - } + return out } -// ---- helpers ---- - -func subnetIDs(mappings []subnetMapping) []string { +func zoneNames(mappings []zoneMapping) []string { if len(mappings) == 0 { return nil } out := make([]string, 0, len(mappings)) for _, m := range mappings { - out = append(out, m.SubnetID) + out = append(out, m.AvailabilityZone) } return out @@ -554,29 +337,3 @@ func ruleGroupPayload(ruleGroup json.RawMessage, legacyRules string) json.RawMes return b } - -func toRuleGroupRefs(refs []ruleGroupReferenceJSON) []nfdriver.RuleGroupReference { - if len(refs) == 0 { - return nil - } - - out := make([]nfdriver.RuleGroupReference, 0, len(refs)) - for _, r := range refs { - out = append(out, nfdriver.RuleGroupReference{ResourceARN: r.ResourceArn, Priority: r.Priority}) - } - - return out -} - -func fromRuleGroupRefs(refs []nfdriver.RuleGroupReference) []ruleGroupReferenceJSON { - if len(refs) == 0 { - return nil - } - - out := make([]ruleGroupReferenceJSON, 0, len(refs)) - for _, r := range refs { - out = append(out, ruleGroupReferenceJSON{ResourceArn: r.ResourceARN, Priority: r.Priority}) - } - - return out -} diff --git a/server/aws/networkfirewall/policy.go b/server/aws/networkfirewall/policy.go new file mode 100644 index 000000000..0fad596cb --- /dev/null +++ b/server/aws/networkfirewall/policy.go @@ -0,0 +1,258 @@ +package networkfirewall + +import ( + "encoding/json" + "net/http" + + "github.com/stackshy/cloudemu/v2/server/wire" + nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" +) + +type firewallPolicyResponseJSON struct { + FirewallPolicyName string `json:"FirewallPolicyName"` + FirewallPolicyArn string `json:"FirewallPolicyArn"` + FirewallPolicyID string `json:"FirewallPolicyId"` + Description string `json:"Description,omitempty"` + FirewallPolicyStatus string `json:"FirewallPolicyStatus,omitempty"` + NumberOfAssociations int `json:"NumberOfAssociations"` + ConsumedStatelessRuleCapacity int `json:"ConsumedStatelessRuleCapacity"` + ConsumedStatefulRuleCapacity int `json:"ConsumedStatefulRuleCapacity"` + ConsumedStatefulDomainCapacity int `json:"ConsumedStatefulDomainCapacity"` + LastModifiedTime *float64 `json:"LastModifiedTime,omitempty"` + Tags []tag `json:"Tags,omitempty"` +} + +type ruleGroupReferenceJSON struct { + ResourceArn string `json:"ResourceArn"` + Priority int `json:"Priority,omitempty"` + Override json.RawMessage `json:"Override,omitempty"` + DeepThreatInspection *bool `json:"DeepThreatInspection,omitempty"` +} + +type firewallPolicyDetailJSON struct { + StatelessDefaultActions []string `json:"StatelessDefaultActions,omitempty"` + StatelessFragmentDefaultActions []string `json:"StatelessFragmentDefaultActions,omitempty"` + StatefulDefaultActions []string `json:"StatefulDefaultActions,omitempty"` + StatefulRuleGroupReferences []ruleGroupReferenceJSON `json:"StatefulRuleGroupReferences,omitempty"` + StatelessRuleGroupReferences []ruleGroupReferenceJSON `json:"StatelessRuleGroupReferences,omitempty"` + TLSInspectionConfigurationArn string `json:"TLSInspectionConfigurationArn,omitempty"` + StatelessCustomActions json.RawMessage `json:"StatelessCustomActions,omitempty"` + PolicyVariables json.RawMessage `json:"PolicyVariables,omitempty"` + StatefulEngineOptions json.RawMessage `json:"StatefulEngineOptions,omitempty"` + EnableTLSSessionHolding *bool `json:"EnableTLSSessionHolding,omitempty"` +} + +type createFirewallPolicyRequest struct { + FirewallPolicyName string `json:"FirewallPolicyName"` + FirewallPolicy firewallPolicyDetailJSON `json:"FirewallPolicy"` + Description string `json:"Description"` + Tags []tag `json:"Tags"` +} + +type policyNameArnRequest struct { + FirewallPolicyName string `json:"FirewallPolicyName"` + FirewallPolicyArn string `json:"FirewallPolicyArn"` +} + +func (h *Handler) createFirewallPolicy(w http.ResponseWriter, r *http.Request) { + var req createFirewallPolicyRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + d := req.FirewallPolicy + + p, err := h.db.CreateFirewallPolicy(r.Context(), nfdriver.CreateFirewallPolicyConfig{ + Name: req.FirewallPolicyName, + Description: req.Description, + StatelessDefaultActions: d.StatelessDefaultActions, + StatelessFragmentDefaultActions: d.StatelessFragmentDefaultActions, + StatefulDefaultActions: d.StatefulDefaultActions, + StatefulRuleGroupReferences: toRuleGroupRefs(d.StatefulRuleGroupReferences), + StatelessRuleGroupReferences: toRuleGroupRefs(d.StatelessRuleGroupReferences), + TLSInspectionConfigurationARN: d.TLSInspectionConfigurationArn, + Extra: policyExtra(&d), + Tags: tagsToMap(req.Tags), + }) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, map[string]any{ + "UpdateToken": p.UpdateToken, + "FirewallPolicyResponse": toPolicyResponseJSON(p), + }) +} + +func (h *Handler) describeFirewallPolicy(w http.ResponseWriter, r *http.Request) { + var req policyNameArnRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + p, err := h.db.DescribeFirewallPolicy(r.Context(), req.FirewallPolicyName, req.FirewallPolicyArn) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, map[string]any{ + "UpdateToken": p.UpdateToken, + "FirewallPolicyResponse": toPolicyResponseJSON(p), + "FirewallPolicy": toPolicyDetailJSON(p), + }) +} + +type updateFirewallPolicyRequest struct { + UpdateToken string `json:"UpdateToken"` + FirewallPolicyName string `json:"FirewallPolicyName"` + FirewallPolicyArn string `json:"FirewallPolicyArn"` + FirewallPolicy firewallPolicyDetailJSON `json:"FirewallPolicy"` + Description string `json:"Description"` +} + +func (h *Handler) updateFirewallPolicy(w http.ResponseWriter, r *http.Request) { + var req updateFirewallPolicyRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + d := req.FirewallPolicy + + p, err := h.db.UpdateFirewallPolicy(r.Context(), req.FirewallPolicyName, req.FirewallPolicyArn, + nfdriver.UpdateFirewallPolicyConfig{ + UpdateToken: req.UpdateToken, + Description: req.Description, + StatelessDefaultActions: d.StatelessDefaultActions, + StatelessFragmentDefaultActions: d.StatelessFragmentDefaultActions, + StatefulDefaultActions: d.StatefulDefaultActions, + StatefulRuleGroupReferences: toRuleGroupRefs(d.StatefulRuleGroupReferences), + StatelessRuleGroupReferences: toRuleGroupRefs(d.StatelessRuleGroupReferences), + TLSInspectionConfigurationARN: d.TLSInspectionConfigurationArn, + Extra: policyExtra(&d), + }) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, map[string]any{ + "UpdateToken": p.UpdateToken, + "FirewallPolicyResponse": toPolicyResponseJSON(p), + }) +} + +func (h *Handler) deleteFirewallPolicy(w http.ResponseWriter, r *http.Request) { + var req policyNameArnRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + p, err := h.db.DeleteFirewallPolicy(r.Context(), req.FirewallPolicyName, req.FirewallPolicyArn) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, map[string]any{"FirewallPolicyResponse": toPolicyResponseJSON(p)}) +} + +type listRequest struct { + MaxResults int `json:"MaxResults"` + NextToken string `json:"NextToken"` +} + +func (l listRequest) options() nfdriver.ListOptions { + return nfdriver.ListOptions{MaxResults: l.MaxResults, NextToken: l.NextToken} +} + +func (h *Handler) listFirewallPolicies(w http.ResponseWriter, r *http.Request) { + var req listRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + items, next, err := h.db.ListFirewallPolicies(r.Context(), req.options()) + if err != nil { + writeErr(w, err) + return + } + + metas := make([]map[string]string, 0, len(items)) + for i := range items { + metas = append(metas, map[string]string{"Name": items[i].Name, "Arn": items[i].ARN}) + } + + wire.WriteJSON(w, withNextToken(map[string]any{"FirewallPolicies": metas}, next)) +} + +func toPolicyResponseJSON(p *nfdriver.FirewallPolicy) firewallPolicyResponseJSON { + modified := epochSeconds(p.LastModified) + + return firewallPolicyResponseJSON{ + FirewallPolicyName: p.Name, FirewallPolicyArn: p.ARN, FirewallPolicyID: p.ID, + Description: p.Description, FirewallPolicyStatus: p.Status, + NumberOfAssociations: p.NumberOfAssociations, + ConsumedStatelessRuleCapacity: p.ConsumedStatelessRuleCapacity, + ConsumedStatefulRuleCapacity: p.ConsumedStatefulRuleCapacity, + ConsumedStatefulDomainCapacity: p.ConsumedStatefulDomainCapacity, + LastModifiedTime: &modified, Tags: mapToTags(p.Tags), + } +} + +func toPolicyDetailJSON(p *nfdriver.FirewallPolicy) firewallPolicyDetailJSON { + return firewallPolicyDetailJSON{ + StatelessDefaultActions: p.StatelessDefaultActions, + StatelessFragmentDefaultActions: p.StatelessFragmentDefaultActions, + StatefulDefaultActions: p.StatefulDefaultActions, + StatefulRuleGroupReferences: fromRuleGroupRefs(p.StatefulRuleGroupReferences), + StatelessRuleGroupReferences: fromRuleGroupRefs(p.StatelessRuleGroupReferences), + TLSInspectionConfigurationArn: p.TLSInspectionConfigurationARN, + StatelessCustomActions: p.Extra.StatelessCustomActions, + PolicyVariables: p.Extra.PolicyVariables, + StatefulEngineOptions: p.Extra.StatefulEngineOptions, + EnableTLSSessionHolding: p.Extra.EnableTLSSessionHolding, + } +} + +func policyExtra(d *firewallPolicyDetailJSON) nfdriver.PolicyExtra { + return nfdriver.PolicyExtra{ + StatelessCustomActions: d.StatelessCustomActions, + PolicyVariables: d.PolicyVariables, + StatefulEngineOptions: d.StatefulEngineOptions, + EnableTLSSessionHolding: d.EnableTLSSessionHolding, + } +} + +func toRuleGroupRefs(refs []ruleGroupReferenceJSON) []nfdriver.RuleGroupReference { + if len(refs) == 0 { + return nil + } + + out := make([]nfdriver.RuleGroupReference, 0, len(refs)) + for _, r := range refs { + out = append(out, nfdriver.RuleGroupReference{ + ResourceARN: r.ResourceArn, Priority: r.Priority, + Override: r.Override, DeepThreatInspection: r.DeepThreatInspection, + }) + } + + return out +} + +func fromRuleGroupRefs(refs []nfdriver.RuleGroupReference) []ruleGroupReferenceJSON { + if len(refs) == 0 { + return nil + } + + out := make([]ruleGroupReferenceJSON, 0, len(refs)) + for _, r := range refs { + out = append(out, ruleGroupReferenceJSON{ + ResourceArn: r.ResourceARN, Priority: r.Priority, + Override: r.Override, DeepThreatInspection: r.DeepThreatInspection, + }) + } + + return out +} diff --git a/server/aws/networkfirewall/resource_policy.go b/server/aws/networkfirewall/resource_policy.go new file mode 100644 index 000000000..8567dcf23 --- /dev/null +++ b/server/aws/networkfirewall/resource_policy.go @@ -0,0 +1,75 @@ +package networkfirewall + +import ( + "net/http" + + "github.com/stackshy/cloudemu/v2/server/wire" + nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" +) + +type putResourcePolicyRequest struct { + ResourceArn string `json:"ResourceArn"` + Policy string `json:"Policy"` +} + +type resourceArnRequest struct { + ResourceArn string `json:"ResourceArn"` +} + +func (h *Handler) putResourcePolicy(w http.ResponseWriter, r *http.Request) { + var req putResourcePolicyRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + policies, ok := capability[nfdriver.ResourcePolicies](h, w) + if !ok { + return + } + + if err := policies.PutResourcePolicy(r.Context(), req.ResourceArn, req.Policy); err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, map[string]any{}) +} + +func (h *Handler) describeResourcePolicy(w http.ResponseWriter, r *http.Request) { + var req resourceArnRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + policies, ok := capability[nfdriver.ResourcePolicies](h, w) + if !ok { + return + } + + policy, err := policies.DescribeResourcePolicy(r.Context(), req.ResourceArn) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, map[string]any{"Policy": policy}) +} + +func (h *Handler) deleteResourcePolicy(w http.ResponseWriter, r *http.Request) { + var req resourceArnRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + policies, ok := capability[nfdriver.ResourcePolicies](h, w) + if !ok { + return + } + + if err := policies.DeleteResourcePolicy(r.Context(), req.ResourceArn); err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, map[string]any{}) +} diff --git a/server/aws/networkfirewall/rule_group.go b/server/aws/networkfirewall/rule_group.go new file mode 100644 index 000000000..9eceaadc6 --- /dev/null +++ b/server/aws/networkfirewall/rule_group.go @@ -0,0 +1,219 @@ +package networkfirewall + +import ( + "encoding/json" + "net/http" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/server/wire" + nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" +) + +type summaryConfigurationJSON struct { + RuleOptions []string `json:"RuleOptions"` +} + +type ruleGroupResponseJSON struct { + RuleGroupName string `json:"RuleGroupName"` + RuleGroupArn string `json:"RuleGroupArn"` + RuleGroupID string `json:"RuleGroupId"` + Type string `json:"Type"` + Capacity int `json:"Capacity,omitempty"` + ConsumedCapacity int `json:"ConsumedCapacity"` + NumberOfAssociations int `json:"NumberOfAssociations"` + Description string `json:"Description,omitempty"` + RuleGroupStatus string `json:"RuleGroupStatus,omitempty"` + LastModifiedTime *float64 `json:"LastModifiedTime,omitempty"` + SummaryConfiguration *summaryConfigurationJSON `json:"SummaryConfiguration,omitempty"` + Tags []tag `json:"Tags,omitempty"` +} + +type createRuleGroupRequest struct { + RuleGroupName string `json:"RuleGroupName"` + RuleGroup json.RawMessage `json:"RuleGroup"` + Rules string `json:"Rules"` + Type string `json:"Type"` + Capacity int `json:"Capacity"` + Description string `json:"Description"` + SummaryConfiguration *summaryConfigurationJSON `json:"SummaryConfiguration"` + Tags []tag `json:"Tags"` +} + +type ruleGroupNameArnRequest struct { + RuleGroupName string `json:"RuleGroupName"` + RuleGroupArn string `json:"RuleGroupArn"` + Type string `json:"Type"` +} + +// ruleGroupBody resolves the rules of a create/update request. RuleGroup and +// Rules are alternatives (AWS: "either this or Rules, but not both"); giving +// neither stores an empty rule group, as the emulator always has. +func ruleGroupBody(ruleGroup json.RawMessage, rules string) (json.RawMessage, error) { + hasGroup := len(ruleGroup) > 0 && string(ruleGroup) != "null" + + if hasGroup && rules != "" { + return nil, cerrors.New(cerrors.InvalidArgument, "provide either RuleGroup or Rules, not both") + } + + if !hasGroup && rules == "" { + return nil, nil + } + + if !hasGroup { + ruleGroup = nil + } + + return ruleGroupPayload(ruleGroup, rules), nil +} + +func summaryOptions(c *summaryConfigurationJSON) []string { + if c == nil { + return nil + } + + return append([]string{}, c.RuleOptions...) +} + +func (h *Handler) createRuleGroup(w http.ResponseWriter, r *http.Request) { + var req createRuleGroupRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + body, err := ruleGroupBody(req.RuleGroup, req.Rules) + if err != nil { + writeErr(w, err) + return + } + + rg, err := h.db.CreateRuleGroup(r.Context(), nfdriver.CreateRuleGroupConfig{ + Name: req.RuleGroupName, Type: req.Type, Capacity: req.Capacity, Description: req.Description, + Rules: body, SummaryRuleOptions: summaryOptions(req.SummaryConfiguration), Tags: tagsToMap(req.Tags), + }) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, map[string]any{ + "UpdateToken": rg.UpdateToken, + "RuleGroupResponse": toRuleGroupResponseJSON(rg), + }) +} + +func (h *Handler) describeRuleGroup(w http.ResponseWriter, r *http.Request) { + var req ruleGroupNameArnRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + rg, err := h.db.DescribeRuleGroup(r.Context(), req.RuleGroupName, req.RuleGroupArn, req.Type) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, map[string]any{ + "UpdateToken": rg.UpdateToken, + "RuleGroupResponse": toRuleGroupResponseJSON(rg), + "RuleGroup": rg.Rules, + }) +} + +type updateRuleGroupRequest struct { + UpdateToken string `json:"UpdateToken"` + RuleGroupName string `json:"RuleGroupName"` + RuleGroupArn string `json:"RuleGroupArn"` + RuleGroup json.RawMessage `json:"RuleGroup"` + Rules string `json:"Rules"` + Type string `json:"Type"` + Description string `json:"Description"` + SummaryConfiguration *summaryConfigurationJSON `json:"SummaryConfiguration"` +} + +func (h *Handler) updateRuleGroup(w http.ResponseWriter, r *http.Request) { + var req updateRuleGroupRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + body, err := ruleGroupBody(req.RuleGroup, req.Rules) + if err != nil { + writeErr(w, err) + return + } + + rg, err := h.db.UpdateRuleGroup(r.Context(), req.RuleGroupName, req.RuleGroupArn, req.Type, + nfdriver.UpdateRuleGroupConfig{ + UpdateToken: req.UpdateToken, Description: req.Description, Rules: body, + SummaryRuleOptions: summaryOptions(req.SummaryConfiguration), + }) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, map[string]any{ + "UpdateToken": rg.UpdateToken, + "RuleGroupResponse": toRuleGroupResponseJSON(rg), + }) +} + +func (h *Handler) deleteRuleGroup(w http.ResponseWriter, r *http.Request) { + var req ruleGroupNameArnRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + rg, err := h.db.DeleteRuleGroup(r.Context(), req.RuleGroupName, req.RuleGroupArn, req.Type) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, map[string]any{"RuleGroupResponse": toRuleGroupResponseJSON(rg)}) +} + +type listRuleGroupsRequest struct { + listRequest + Type string `json:"Type"` + Scope string `json:"Scope"` + ManagedType string `json:"ManagedType"` +} + +func (h *Handler) listRuleGroups(w http.ResponseWriter, r *http.Request) { + var req listRuleGroupsRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + filter := nfdriver.RuleGroupFilter{Type: req.Type, Scope: req.Scope, ManagedType: req.ManagedType} + + items, next, err := h.db.ListRuleGroups(r.Context(), filter, req.options()) + if err != nil { + writeErr(w, err) + return + } + + metas := make([]map[string]string, 0, len(items)) + for i := range items { + metas = append(metas, map[string]string{"Name": items[i].Name, "Arn": items[i].ARN}) + } + + wire.WriteJSON(w, withNextToken(map[string]any{"RuleGroups": metas}, next)) +} + +func toRuleGroupResponseJSON(rg *nfdriver.RuleGroup) ruleGroupResponseJSON { + modified := epochSeconds(rg.LastModified) + + out := ruleGroupResponseJSON{ + RuleGroupName: rg.Name, RuleGroupArn: rg.ARN, RuleGroupID: rg.ID, Type: rg.Type, + Capacity: rg.Capacity, ConsumedCapacity: rg.ConsumedCapacity, NumberOfAssociations: rg.NumberOfAssociations, + Description: rg.Description, RuleGroupStatus: rg.Status, LastModifiedTime: &modified, Tags: mapToTags(rg.Tags), + } + if len(rg.SummaryRuleOptions) > 0 { + out.SummaryConfiguration = &summaryConfigurationJSON{RuleOptions: rg.SummaryRuleOptions} + } + + return out +} diff --git a/server/aws/networkfirewall/sdk_parity_test.go b/server/aws/networkfirewall/sdk_parity_test.go new file mode 100644 index 000000000..fbaf0e579 --- /dev/null +++ b/server/aws/networkfirewall/sdk_parity_test.go @@ -0,0 +1,708 @@ +package networkfirewall_test + +import ( + "context" + "encoding/json" + "fmt" + "testing" + + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/networkfirewall" + nftypes "github.com/aws/aws-sdk-go-v2/service/networkfirewall/types" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +var bg = context.Background() + +// stack is a policy plus a firewall using it, created through the SDK. +type stack struct { + c *networkfirewall.Client + policyARN string + firewallID string + fwARN string +} + +func newStack(t *testing.T) *stack { + t.Helper() + + c := newClient(t) + + pol, err := c.CreateFirewallPolicy(bg, &networkfirewall.CreateFirewallPolicyInput{ + FirewallPolicyName: aws.String("pol-1"), + FirewallPolicy: &nftypes.FirewallPolicy{ + StatelessDefaultActions: []string{"aws:forward_to_sfe"}, StatelessFragmentDefaultActions: []string{"aws:forward_to_sfe"}, + }, + }) + require.NoError(t, err) + + fw, err := c.CreateFirewall(bg, &networkfirewall.CreateFirewallInput{ + FirewallName: aws.String("fw-1"), FirewallPolicyArn: pol.FirewallPolicyResponse.FirewallPolicyArn, + VpcId: aws.String("vpc-1a"), SubnetMappings: []nftypes.SubnetMapping{{SubnetId: aws.String("subnet-1")}}, + }) + require.NoError(t, err) + + return &stack{c: c, policyARN: aws.ToString(pol.FirewallPolicyResponse.FirewallPolicyArn), fwARN: aws.ToString(fw.Firewall.FirewallArn)} +} + +func (s *stack) token(t *testing.T) *string { + t.Helper() + + d, err := s.c.DescribeFirewall(bg, &networkfirewall.DescribeFirewallInput{FirewallName: aws.String("fw-1")}) + require.NoError(t, err) + + return d.UpdateToken +} + +func TestSDK_UpdateToken_Flow(t *testing.T) { + s := newStack(t) + stale := s.token(t) + + first, err := s.c.UpdateFirewallDeleteProtection(bg, &networkfirewall.UpdateFirewallDeleteProtectionInput{ + FirewallName: aws.String("fw-1"), DeleteProtection: true, UpdateToken: stale, + }) + require.NoError(t, err) + assert.NotEqual(t, aws.ToString(stale), aws.ToString(first.UpdateToken), "the token rotates on every mutation") + + _, err = s.c.UpdateFirewallDeleteProtection(bg, &networkfirewall.UpdateFirewallDeleteProtectionInput{ + FirewallName: aws.String("fw-1"), DeleteProtection: false, UpdateToken: stale, + }) + + var invalidToken *nftypes.InvalidTokenException + require.ErrorAs(t, err, &invalidToken) + + _, err = s.c.UpdateFirewallDeleteProtection(bg, &networkfirewall.UpdateFirewallDeleteProtectionInput{ + FirewallName: aws.String("fw-1"), DeleteProtection: false, + }) + require.NoError(t, err, "an update without a token is unconditional") + + // Policies and rule groups need a current token too. + _, err = s.c.UpdateFirewallPolicy(bg, &networkfirewall.UpdateFirewallPolicyInput{ + FirewallPolicyArn: aws.String(s.policyARN), UpdateToken: aws.String("11111111-1111-1111-1111-111111111111"), + FirewallPolicy: &nftypes.FirewallPolicy{ + StatelessDefaultActions: []string{"aws:drop"}, StatelessFragmentDefaultActions: []string{"aws:drop"}, + }, + }) + require.ErrorAs(t, err, &invalidToken) +} + +func TestSDK_ChangeProtection_AllOps(t *testing.T) { + s := newStack(t) + + sub, err := s.c.UpdateSubnetChangeProtection(bg, &networkfirewall.UpdateSubnetChangeProtectionInput{ + FirewallName: aws.String("fw-1"), SubnetChangeProtection: true, + }) + require.NoError(t, err) + assert.True(t, sub.SubnetChangeProtection) + + pol, err := s.c.UpdateFirewallPolicyChangeProtection(bg, &networkfirewall.UpdateFirewallPolicyChangeProtectionInput{ + FirewallArn: aws.String(s.fwARN), FirewallPolicyChangeProtection: true, UpdateToken: sub.UpdateToken, + }) + require.NoError(t, err) + assert.True(t, pol.FirewallPolicyChangeProtection) + + az, err := s.c.UpdateAvailabilityZoneChangeProtection(bg, &networkfirewall.UpdateAvailabilityZoneChangeProtectionInput{ + FirewallName: aws.String("fw-1"), AvailabilityZoneChangeProtection: true, + }) + require.NoError(t, err) + assert.True(t, az.AvailabilityZoneChangeProtection) + + d, err := s.c.DescribeFirewall(bg, &networkfirewall.DescribeFirewallInput{FirewallName: aws.String("fw-1")}) + require.NoError(t, err) + assert.True(t, d.Firewall.SubnetChangeProtection && d.Firewall.FirewallPolicyChangeProtection && d.Firewall.AvailabilityZoneChangeProtection) + + var invalidOp *nftypes.InvalidOperationException + + _, err = s.c.AssociateSubnets(bg, &networkfirewall.AssociateSubnetsInput{ + FirewallName: aws.String("fw-1"), SubnetMappings: []nftypes.SubnetMapping{{SubnetId: aws.String("subnet-2")}}, + }) + require.ErrorAs(t, err, &invalidOp) + + _, err = s.c.DisassociateSubnets(bg, &networkfirewall.DisassociateSubnetsInput{FirewallName: aws.String("fw-1"), SubnetIds: []string{"subnet-1"}}) + require.ErrorAs(t, err, &invalidOp) + + other, err := s.c.CreateFirewallPolicy(bg, &networkfirewall.CreateFirewallPolicyInput{ + FirewallPolicyName: aws.String("pol-2"), + FirewallPolicy: &nftypes.FirewallPolicy{ + StatelessDefaultActions: []string{"aws:pass"}, StatelessFragmentDefaultActions: []string{"aws:pass"}, + }, + }) + require.NoError(t, err) + + _, err = s.c.AssociateFirewallPolicy(bg, &networkfirewall.AssociateFirewallPolicyInput{ + FirewallName: aws.String("fw-1"), FirewallPolicyArn: other.FirewallPolicyResponse.FirewallPolicyArn, + }) + require.ErrorAs(t, err, &invalidOp) + + // Availability Zone operations only apply to transit gateway-attached firewalls. + _, err = s.c.AssociateAvailabilityZones(bg, &networkfirewall.AssociateAvailabilityZonesInput{ + FirewallName: aws.String("fw-1"), AvailabilityZoneMappings: []nftypes.AvailabilityZoneMapping{{AvailabilityZone: aws.String("us-east-1b")}}, + }) + require.ErrorAs(t, err, &invalidOp) + + tgw, err := s.c.CreateFirewall(bg, &networkfirewall.CreateFirewallInput{ + FirewallName: aws.String("fw-tgw"), FirewallPolicyArn: aws.String(s.policyARN), TransitGatewayId: aws.String("tgw-0abc"), + AvailabilityZoneMappings: []nftypes.AvailabilityZoneMapping{{AvailabilityZone: aws.String("us-east-1a")}}, + }) + require.NoError(t, err) + assert.Equal(t, "tgw-0abc", aws.ToString(tgw.Firewall.TransitGatewayId)) + + added, err := s.c.AssociateAvailabilityZones(bg, &networkfirewall.AssociateAvailabilityZonesInput{ + FirewallName: aws.String("fw-tgw"), AvailabilityZoneMappings: []nftypes.AvailabilityZoneMapping{{AvailabilityZone: aws.String("us-east-1b")}}, + }) + require.NoError(t, err) + assert.Len(t, added.AvailabilityZoneMappings, 2) + + removed, err := s.c.DisassociateAvailabilityZones(bg, &networkfirewall.DisassociateAvailabilityZonesInput{ + FirewallName: aws.String("fw-tgw"), AvailabilityZoneMappings: []nftypes.AvailabilityZoneMapping{{AvailabilityZone: aws.String("us-east-1a")}}, + }) + require.NoError(t, err) + require.Len(t, removed.AvailabilityZoneMappings, 1) + assert.Equal(t, "us-east-1b", aws.ToString(removed.AvailabilityZoneMappings[0].AvailabilityZone)) +} + +func TestSDK_UpdateFirewallDescription(t *testing.T) { + s := newStack(t) + + set, err := s.c.UpdateFirewallDescription(bg, &networkfirewall.UpdateFirewallDescriptionInput{ + FirewallName: aws.String("fw-1"), Description: aws.String("edge"), UpdateToken: s.token(t), + }) + require.NoError(t, err) + assert.Equal(t, "edge", aws.ToString(set.Description)) + assert.Equal(t, s.fwARN, aws.ToString(set.FirewallArn)) + assert.Equal(t, "fw-1", aws.ToString(set.FirewallName)) + + cleared, err := s.c.UpdateFirewallDescription(bg, &networkfirewall.UpdateFirewallDescriptionInput{FirewallArn: aws.String(s.fwARN)}) + require.NoError(t, err) + assert.Empty(t, aws.ToString(cleared.Description)) + + d, _ := s.c.DescribeFirewall(bg, &networkfirewall.DescribeFirewallInput{FirewallName: aws.String("fw-1")}) + assert.Empty(t, aws.ToString(d.Firewall.Description)) +} + +func TestSDK_CreateFirewall_NoVpcId(t *testing.T) { + s := newStack(t) + + _, err := s.c.CreateFirewall(bg, &networkfirewall.CreateFirewallInput{ + FirewallName: aws.String("fw-novpc"), FirewallPolicyArn: aws.String(s.policyARN), + SubnetMappings: []nftypes.SubnetMapping{{SubnetId: aws.String("subnet-1")}}, + }) + + var invalid *nftypes.InvalidRequestException + require.ErrorAs(t, err, &invalid) +} + +func TestSDK_DeleteGuards_TypedErrors(t *testing.T) { + s := newStack(t) + + var invalidOp *nftypes.InvalidOperationException + + _, err := s.c.DeleteFirewallPolicy(bg, &networkfirewall.DeleteFirewallPolicyInput{FirewallPolicyName: aws.String("pol-1")}) + require.ErrorAs(t, err, &invalidOp, "policy in use by a firewall") + + _, err = s.c.UpdateFirewallDeleteProtection(bg, &networkfirewall.UpdateFirewallDeleteProtectionInput{FirewallName: aws.String("fw-1"), DeleteProtection: true}) + require.NoError(t, err) + + _, err = s.c.DeleteFirewall(bg, &networkfirewall.DeleteFirewallInput{FirewallName: aws.String("fw-1")}) + require.ErrorAs(t, err, &invalidOp, "delete protection") + + rg, err := s.c.CreateRuleGroup(bg, &networkfirewall.CreateRuleGroupInput{RuleGroupName: aws.String("rg-1"), Type: nftypes.RuleGroupTypeStateful, Capacity: aws.Int32(5)}) + require.NoError(t, err) + + _, err = s.c.CreateFirewallPolicy(bg, &networkfirewall.CreateFirewallPolicyInput{ + FirewallPolicyName: aws.String("pol-rg"), + FirewallPolicy: &nftypes.FirewallPolicy{ + StatelessDefaultActions: []string{"aws:pass"}, StatelessFragmentDefaultActions: []string{"aws:pass"}, + StatefulRuleGroupReferences: []nftypes.StatefulRuleGroupReference{{ResourceArn: rg.RuleGroupResponse.RuleGroupArn}}, + }, + }) + require.NoError(t, err) + + _, err = s.c.DeleteRuleGroup(bg, &networkfirewall.DeleteRuleGroupInput{RuleGroupName: aws.String("rg-1"), Type: nftypes.RuleGroupTypeStateful}) + require.ErrorAs(t, err, &invalidOp, "rule group referenced by a policy") +} + +func TestSDK_LoggingConfiguration_RoundTrip(t *testing.T) { + s := newStack(t) + + cfg := func(dests ...nftypes.LogDestinationConfig) *networkfirewall.UpdateLoggingConfigurationInput { + return &networkfirewall.UpdateLoggingConfigurationInput{ + FirewallName: aws.String("fw-1"), LoggingConfiguration: &nftypes.LoggingConfiguration{LogDestinationConfigs: dests}, + } + } + s3 := func(lt nftypes.LogType, bucket string) nftypes.LogDestinationConfig { + return nftypes.LogDestinationConfig{LogType: lt, LogDestinationType: nftypes.LogDestinationTypeS3, LogDestination: map[string]string{"bucketName": bucket}} + } + + var invalid *nftypes.InvalidRequestException + + _, err := s.c.UpdateLoggingConfiguration(bg, cfg(nftypes.LogDestinationConfig{ + LogType: nftypes.LogType("BOGUS"), LogDestinationType: nftypes.LogDestinationTypeS3, LogDestination: map[string]string{"bucketName": "b"}, + })) + require.ErrorAs(t, err, &invalid) + + _, err = s.c.UpdateLoggingConfiguration(bg, cfg(s3(nftypes.LogTypeFlow, "b1"))) + require.NoError(t, err) + + _, err = s.c.UpdateLoggingConfiguration(bg, cfg(s3(nftypes.LogTypeFlow, "b1"), s3(nftypes.LogTypeAlert, "b1"), s3(nftypes.LogTypeTls, "b1"))) + require.ErrorAs(t, err, &invalid, "two additions in one call") + + dash := true + in := cfg(s3(nftypes.LogTypeFlow, "b1"), s3(nftypes.LogTypeAlert, "b2")) + in.EnableMonitoringDashboard = &dash + + up, err := s.c.UpdateLoggingConfiguration(bg, in) + require.NoError(t, err) + assert.Len(t, up.LoggingConfiguration.LogDestinationConfigs, 2) + assert.True(t, aws.ToBool(up.EnableMonitoringDashboard)) + + d, err := s.c.DescribeLoggingConfiguration(bg, &networkfirewall.DescribeLoggingConfigurationInput{FirewallName: aws.String("fw-1")}) + require.NoError(t, err) + assert.Equal(t, up.LoggingConfiguration.LogDestinationConfigs, d.LoggingConfiguration.LogDestinationConfigs) + assert.True(t, aws.ToBool(d.EnableMonitoringDashboard)) + + // An omitted configuration disables logging. + _, err = s.c.UpdateLoggingConfiguration(bg, &networkfirewall.UpdateLoggingConfigurationInput{FirewallName: aws.String("fw-1")}) + require.ErrorAs(t, err, &invalid, "removing two destinations at once") +} + +func TestSDK_CreateRuleGroup_Capacity0(t *testing.T) { + c := newClient(t) + + _, err := c.CreateRuleGroup(bg, &networkfirewall.CreateRuleGroupInput{RuleGroupName: aws.String("rg"), Type: nftypes.RuleGroupTypeStateful, Capacity: aws.Int32(0)}) + + var invalid *nftypes.InvalidRequestException + require.ErrorAs(t, err, &invalid) + + _, err = c.CreateRuleGroup(bg, &networkfirewall.CreateRuleGroupInput{ + RuleGroupName: aws.String("rg"), Type: nftypes.RuleGroupTypeStateful, Capacity: aws.Int32(5), + RuleGroup: &nftypes.RuleGroup{RulesSource: &nftypes.RulesSource{RulesString: aws.String("pass tcp any any -> any any (sid:1;)")}}, + Rules: aws.String("pass tcp any any -> any any (sid:2;)"), + }) + require.ErrorAs(t, err, &invalid, "RuleGroup and Rules together") +} + +func TestSDK_DeleteRuleGroup_InUse(t *testing.T) { + TestSDK_DeleteGuards_TypedErrors(t) +} + +func TestSDK_ComputedFieldsDeserialize(t *testing.T) { + s := newStack(t) + + rg, err := s.c.CreateRuleGroup(bg, &networkfirewall.CreateRuleGroupInput{ + RuleGroupName: aws.String("rg-c"), Type: nftypes.RuleGroupTypeStateful, Capacity: aws.Int32(10), + Rules: aws.String("pass tcp any any -> any any (sid:1;)\npass udp any any -> any any (sid:2;)\n"), + }) + require.NoError(t, err) + assert.Equal(t, int32(2), aws.ToInt32(rg.RuleGroupResponse.ConsumedCapacity)) + assert.Equal(t, nftypes.ResourceStatusActive, rg.RuleGroupResponse.RuleGroupStatus) + assert.NotNil(t, rg.RuleGroupResponse.LastModifiedTime) + + pol, err := s.c.CreateFirewallPolicy(bg, &networkfirewall.CreateFirewallPolicyInput{ + FirewallPolicyName: aws.String("pol-c"), + FirewallPolicy: &nftypes.FirewallPolicy{ + StatelessDefaultActions: []string{"aws:pass"}, StatelessFragmentDefaultActions: []string{"aws:pass"}, + StatefulRuleGroupReferences: []nftypes.StatefulRuleGroupReference{{ResourceArn: rg.RuleGroupResponse.RuleGroupArn}}, + }, + }) + require.NoError(t, err) + assert.Equal(t, int32(10), aws.ToInt32(pol.FirewallPolicyResponse.ConsumedStatefulRuleCapacity)) + + d, err := s.c.DescribeFirewallPolicy(bg, &networkfirewall.DescribeFirewallPolicyInput{FirewallPolicyName: aws.String("pol-1")}) + require.NoError(t, err) + assert.Equal(t, int32(1), aws.ToInt32(d.FirewallPolicyResponse.NumberOfAssociations)) + assert.Equal(t, nftypes.ResourceStatusActive, d.FirewallPolicyResponse.FirewallPolicyStatus) + assert.NotNil(t, d.FirewallPolicyResponse.LastModifiedTime) + + rgd, err := s.c.DescribeRuleGroup(bg, &networkfirewall.DescribeRuleGroupInput{RuleGroupName: aws.String("rg-c"), Type: nftypes.RuleGroupTypeStateful}) + require.NoError(t, err) + assert.Equal(t, int32(1), aws.ToInt32(rgd.RuleGroupResponse.NumberOfAssociations)) +} + +func TestSDK_ResourcePolicy(t *testing.T) { + s := newStack(t) + + doc, _ := json.Marshal(map[string]any{ + "Version": "2012-10-17", + "Statement": []map[string]any{{ + "Effect": "Allow", "Principal": map[string]string{"AWS": "arn:aws:iam::111122223333:root"}, + "Action": []string{"network-firewall:AssociateFirewallPolicy"}, "Resource": s.policyARN, + }}, + }) + + _, err := s.c.PutResourcePolicy(bg, &networkfirewall.PutResourcePolicyInput{ResourceArn: aws.String(s.policyARN), Policy: aws.String(string(doc))}) + require.NoError(t, err) + + got, err := s.c.DescribeResourcePolicy(bg, &networkfirewall.DescribeResourcePolicyInput{ResourceArn: aws.String(s.policyARN)}) + require.NoError(t, err) + assert.JSONEq(t, string(doc), aws.ToString(got.Policy)) + + var badPolicy *nftypes.InvalidResourcePolicyException + + _, err = s.c.PutResourcePolicy(bg, &networkfirewall.PutResourcePolicyInput{ + ResourceArn: aws.String(s.policyARN), Policy: aws.String(`{"Statement":[{"Action":"network-firewall:ListFirewalls"}]}`), + }) + require.ErrorAs(t, err, &badPolicy) + + _, err = s.c.DeleteResourcePolicy(bg, &networkfirewall.DeleteResourcePolicyInput{ResourceArn: aws.String(s.policyARN)}) + require.NoError(t, err) + + var notFound *nftypes.ResourceNotFoundException + + _, err = s.c.DescribeResourcePolicy(bg, &networkfirewall.DescribeResourcePolicyInput{ResourceArn: aws.String(s.policyARN)}) + require.ErrorAs(t, err, ¬Found) +} + +func tlsConfig(certARN string) *nftypes.TLSInspectionConfiguration { + return &nftypes.TLSInspectionConfiguration{ + ServerCertificateConfigurations: []nftypes.ServerCertificateConfiguration{{ + ServerCertificates: []nftypes.ServerCertificate{{ResourceArn: aws.String(certARN)}}, + }}, + } +} + +func TestSDK_TLSInspection_RoundTrip(t *testing.T) { + c := newClient(t) + certARN := "arn:aws:acm:us-east-1:123456789012:certificate/c1" + + created, err := c.CreateTLSInspectionConfiguration(bg, &networkfirewall.CreateTLSInspectionConfigurationInput{ + TLSInspectionConfigurationName: aws.String("tls-1"), TLSInspectionConfiguration: tlsConfig(certARN), + Description: aws.String("d"), Tags: []nftypes.Tag{{Key: aws.String("k"), Value: aws.String("v")}}, + }) + require.NoError(t, err) + + resp := created.TLSInspectionConfigurationResponse + assert.Equal(t, nftypes.ResourceStatusActive, resp.TLSInspectionConfigurationStatus) + require.Len(t, resp.Certificates, 1) + assert.Equal(t, certARN, aws.ToString(resp.Certificates[0].CertificateArn)) + assert.NotNil(t, resp.LastModifiedTime) + + d, err := c.DescribeTLSInspectionConfiguration(bg, &networkfirewall.DescribeTLSInspectionConfigurationInput{TLSInspectionConfigurationArn: resp.TLSInspectionConfigurationArn}) + require.NoError(t, err) + assert.Equal(t, aws.ToString(created.UpdateToken), aws.ToString(d.UpdateToken)) + require.NotNil(t, d.TLSInspectionConfiguration) + assert.Len(t, d.TLSInspectionConfiguration.ServerCertificateConfigurations, 1) + + _, err = c.UpdateTLSInspectionConfiguration(bg, &networkfirewall.UpdateTLSInspectionConfigurationInput{ + TLSInspectionConfigurationName: aws.String("tls-1"), TLSInspectionConfiguration: tlsConfig(certARN), UpdateToken: aws.String("11111111-1111-1111-1111-111111111111"), + }) + + var invalidToken *nftypes.InvalidTokenException + require.ErrorAs(t, err, &invalidToken) + + up, err := c.UpdateTLSInspectionConfiguration(bg, &networkfirewall.UpdateTLSInspectionConfigurationInput{ + TLSInspectionConfigurationName: aws.String("tls-1"), TLSInspectionConfiguration: tlsConfig("arn:aws:acm:us-east-1:123456789012:certificate/c2"), + UpdateToken: d.UpdateToken, Description: aws.String("d2"), + }) + require.NoError(t, err) + assert.Equal(t, "d2", aws.ToString(up.TLSInspectionConfigurationResponse.Description)) + + list, err := c.ListTLSInspectionConfigurations(bg, &networkfirewall.ListTLSInspectionConfigurationsInput{}) + require.NoError(t, err) + require.Len(t, list.TLSInspectionConfigurations, 1) + + _, err = c.DeleteTLSInspectionConfiguration(bg, &networkfirewall.DeleteTLSInspectionConfigurationInput{TLSInspectionConfigurationName: aws.String("tls-1")}) + require.NoError(t, err) +} + +func TestSDK_VPCEndpointAssociation_RoundTrip(t *testing.T) { + s := newStack(t) + + created, err := s.c.CreateVpcEndpointAssociation(bg, &networkfirewall.CreateVpcEndpointAssociationInput{ + FirewallArn: aws.String(s.fwARN), VpcId: aws.String("vpc-2b"), SubnetMapping: &nftypes.SubnetMapping{SubnetId: aws.String("subnet-9")}, + Description: aws.String("extra"), Tags: []nftypes.Tag{{Key: aws.String("k"), Value: aws.String("v")}}, + }) + require.NoError(t, err) + + arn := aws.ToString(created.VpcEndpointAssociation.VpcEndpointAssociationArn) + assert.Equal(t, nftypes.FirewallStatusValueReady, created.VpcEndpointAssociationStatus.Status) + assert.NotEmpty(t, created.VpcEndpointAssociationStatus.AssociationSyncState) + + d, err := s.c.DescribeVpcEndpointAssociation(bg, &networkfirewall.DescribeVpcEndpointAssociationInput{VpcEndpointAssociationArn: aws.String(arn)}) + require.NoError(t, err) + assert.Equal(t, "subnet-9", aws.ToString(d.VpcEndpointAssociation.SubnetMapping.SubnetId)) + + list, err := s.c.ListVpcEndpointAssociations(bg, &networkfirewall.ListVpcEndpointAssociationsInput{FirewallArn: aws.String(s.fwARN)}) + require.NoError(t, err) + assert.Len(t, list.VpcEndpointAssociations, 1) + + tags, err := s.c.ListTagsForResource(bg, &networkfirewall.ListTagsForResourceInput{ResourceArn: aws.String(arn)}) + require.NoError(t, err) + assert.Len(t, tags.Tags, 1) + + fwd, _ := s.c.DescribeFirewall(bg, &networkfirewall.DescribeFirewallInput{FirewallName: aws.String("fw-1")}) + assert.Equal(t, int32(1), aws.ToInt32(fwd.Firewall.NumberOfAssociations)) + + var invalidOp *nftypes.InvalidOperationException + + _, err = s.c.DeleteFirewall(bg, &networkfirewall.DeleteFirewallInput{FirewallName: aws.String("fw-1")}) + require.ErrorAs(t, err, &invalidOp, "firewall with associations") + + _, err = s.c.DeleteVpcEndpointAssociation(bg, &networkfirewall.DeleteVpcEndpointAssociationInput{VpcEndpointAssociationArn: aws.String(arn)}) + require.NoError(t, err) + + var notFound *nftypes.ResourceNotFoundException + + _, err = s.c.DescribeVpcEndpointAssociation(bg, &networkfirewall.DescribeVpcEndpointAssociationInput{VpcEndpointAssociationArn: aws.String(arn)}) + require.ErrorAs(t, err, ¬Found) +} + +func TestSDK_Metadata_Ops(t *testing.T) { + s := newStack(t) + + rg, err := s.c.CreateRuleGroup(bg, &networkfirewall.CreateRuleGroupInput{ + RuleGroupName: aws.String("rg-m"), Type: nftypes.RuleGroupTypeStateful, Capacity: aws.Int32(10), + Rules: aws.String(`alert tcp any any -> any 80 (msg:"web"; sid:7;)`), + SummaryConfiguration: &nftypes.SummaryConfiguration{RuleOptions: []nftypes.SummaryRuleOption{nftypes.SummaryRuleOptionMsg, nftypes.SummaryRuleOptionSid}}, + }) + require.NoError(t, err) + + fm, err := s.c.DescribeFirewallMetadata(bg, &networkfirewall.DescribeFirewallMetadataInput{FirewallArn: aws.String(s.fwARN)}) + require.NoError(t, err) + assert.Equal(t, s.policyARN, aws.ToString(fm.FirewallPolicyArn)) + assert.Equal(t, nftypes.FirewallStatusValueReady, fm.Status) + assert.Contains(t, fm.SupportedAvailabilityZones, "us-east-1a") + + rm, err := s.c.DescribeRuleGroupMetadata(bg, &networkfirewall.DescribeRuleGroupMetadataInput{RuleGroupArn: rg.RuleGroupResponse.RuleGroupArn}) + require.NoError(t, err) + assert.Equal(t, int32(10), aws.ToInt32(rm.Capacity)) + assert.Equal(t, nftypes.RuleGroupTypeStateful, rm.Type) + + sum, err := s.c.DescribeRuleGroupSummary(bg, &networkfirewall.DescribeRuleGroupSummaryInput{RuleGroupName: aws.String("rg-m"), Type: nftypes.RuleGroupTypeStateful}) + require.NoError(t, err) + require.Len(t, sum.Summary.RuleSummaries, 1) + assert.Equal(t, "web", aws.ToString(sum.Summary.RuleSummaries[0].Msg)) + assert.Equal(t, "7", aws.ToString(sum.Summary.RuleSummaries[0].SID)) +} + +func TestSDK_FlowAndAnalysis_Ops(t *testing.T) { + s := newStack(t) + + _, err := s.c.UpdateFirewallAnalysisSettings(bg, &networkfirewall.UpdateFirewallAnalysisSettingsInput{ + FirewallName: aws.String("fw-1"), EnabledAnalysisTypes: []nftypes.EnabledAnalysisType{nftypes.EnabledAnalysisTypeTlsSni}, + }) + require.NoError(t, err) + + capture, err := s.c.StartFlowCapture(bg, &networkfirewall.StartFlowCaptureInput{FirewallArn: aws.String(s.fwARN), AvailabilityZone: aws.String("us-east-1a"), FlowFilters: []nftypes.FlowFilter{{SourcePort: aws.String("80")}}}) + require.NoError(t, err) + assert.Equal(t, nftypes.FlowOperationStatusCompleted, capture.FlowOperationStatus) + + flush, err := s.c.StartFlowFlush(bg, &networkfirewall.StartFlowFlushInput{FirewallArn: aws.String(s.fwARN), AvailabilityZone: aws.String("us-east-1a"), FlowFilters: []nftypes.FlowFilter{{SourcePort: aws.String("80")}}}) + require.NoError(t, err) + + d, err := s.c.DescribeFlowOperation(bg, &networkfirewall.DescribeFlowOperationInput{ + FirewallArn: aws.String(s.fwARN), AvailabilityZone: aws.String("us-east-1a"), FlowOperationId: capture.FlowOperationId, + }) + require.NoError(t, err) + assert.Equal(t, nftypes.FlowOperationTypeFlowCapture, d.FlowOperationType) + assert.NotNil(t, d.FlowRequestTimestamp) + + ops, err := s.c.ListFlowOperations(bg, &networkfirewall.ListFlowOperationsInput{ + FirewallArn: aws.String(s.fwARN), AvailabilityZone: aws.String("us-east-1a"), FlowOperationType: nftypes.FlowOperationTypeFlowFlush, + }) + require.NoError(t, err) + require.Len(t, ops.FlowOperations, 1) + assert.Equal(t, aws.ToString(flush.FlowOperationId), aws.ToString(ops.FlowOperations[0].FlowOperationId)) + + res, err := s.c.ListFlowOperationResults(bg, &networkfirewall.ListFlowOperationResultsInput{ + FirewallArn: aws.String(s.fwARN), AvailabilityZone: aws.String("us-east-1a"), FlowOperationId: capture.FlowOperationId, + }) + require.NoError(t, err) + assert.Empty(t, res.Flows, "no data plane, no flows") + + started, err := s.c.StartAnalysisReport(bg, &networkfirewall.StartAnalysisReportInput{FirewallName: aws.String("fw-1"), AnalysisType: nftypes.EnabledAnalysisTypeTlsSni}) + require.NoError(t, err) + + reports, err := s.c.ListAnalysisReports(bg, &networkfirewall.ListAnalysisReportsInput{FirewallArn: aws.String(s.fwARN)}) + require.NoError(t, err) + require.Len(t, reports.AnalysisReports, 1) + assert.Equal(t, nftypes.EnabledAnalysisTypeTlsSni, reports.AnalysisReports[0].AnalysisType) + + got, err := s.c.GetAnalysisReportResults(bg, &networkfirewall.GetAnalysisReportResultsInput{FirewallName: aws.String("fw-1"), AnalysisReportId: started.AnalysisReportId}) + require.NoError(t, err) + assert.Equal(t, nftypes.EnabledAnalysisTypeTlsSni, got.AnalysisType) + assert.Empty(t, got.AnalysisReportResults) + + var invalid *nftypes.InvalidRequestException + + _, err = s.c.StartAnalysisReport(bg, &networkfirewall.StartAnalysisReportInput{FirewallName: aws.String("fw-1"), AnalysisType: nftypes.EnabledAnalysisTypeHttpHost}) + require.ErrorAs(t, err, &invalid, "HTTP_HOST analysis is not enabled") +} + +func TestSDK_Pagination_Paginators(t *testing.T) { + s := newStack(t) + + for i := 2; i <= 4; i++ { + _, err := s.c.CreateFirewall(bg, &networkfirewall.CreateFirewallInput{ + FirewallName: aws.String(fmt.Sprintf("fw-%d", i)), FirewallPolicyArn: aws.String(s.policyARN), VpcId: aws.String(fmt.Sprintf("vpc-%da", i)), + }) + require.NoError(t, err) + } + + pager := networkfirewall.NewListFirewallsPaginator(s.c, &networkfirewall.ListFirewallsInput{MaxResults: aws.Int32(1)}) + + var names []string + + for pager.HasMorePages() { + page, err := pager.NextPage(bg) + require.NoError(t, err) + require.Len(t, page.Firewalls, 1) + + names = append(names, aws.ToString(page.Firewalls[0].FirewallName)) + } + + assert.Equal(t, []string{"fw-1", "fw-2", "fw-3", "fw-4"}, names) + + filtered, err := s.c.ListFirewalls(bg, &networkfirewall.ListFirewallsInput{VpcIds: []string{"vpc-3a"}}) + require.NoError(t, err) + require.Len(t, filtered.Firewalls, 1) + assert.Equal(t, "fw-3", aws.ToString(filtered.Firewalls[0].FirewallName)) + + var invalid *nftypes.InvalidRequestException + + _, err = s.c.ListFirewalls(bg, &networkfirewall.ListFirewallsInput{NextToken: aws.String("zzz!")}) + require.ErrorAs(t, err, &invalid) + + _, err = s.c.ListFirewallPolicies(bg, &networkfirewall.ListFirewallPoliciesInput{MaxResults: aws.Int32(101)}) + require.ErrorAs(t, err, &invalid) + + pols := networkfirewall.NewListFirewallPoliciesPaginator(s.c, &networkfirewall.ListFirewallPoliciesInput{MaxResults: aws.Int32(1)}) + + seen := 0 + + for pols.HasMorePages() { + page, err := pols.NextPage(bg) + require.NoError(t, err) + + seen += len(page.FirewallPolicies) + } + + assert.Equal(t, 1, seen) +} + +func TestSDK_Policy_FullRoundTrip(t *testing.T) { + c := newClient(t) + + tls, err := c.CreateTLSInspectionConfiguration(bg, &networkfirewall.CreateTLSInspectionConfigurationInput{ + TLSInspectionConfigurationName: aws.String("tls-p"), TLSInspectionConfiguration: tlsConfig("arn:aws:acm:us-east-1:123456789012:certificate/c1"), + }) + require.NoError(t, err) + + _, err = c.CreateFirewallPolicy(bg, &networkfirewall.CreateFirewallPolicyInput{ + FirewallPolicyName: aws.String("pol-full"), + FirewallPolicy: &nftypes.FirewallPolicy{ + StatelessDefaultActions: []string{"aws:pass", "mine"}, + StatelessFragmentDefaultActions: []string{"aws:drop"}, + StatelessCustomActions: []nftypes.CustomAction{{ + ActionName: aws.String("mine"), + ActionDefinition: &nftypes.ActionDefinition{PublishMetricAction: &nftypes.PublishMetricAction{ + Dimensions: []nftypes.Dimension{{Value: aws.String("v")}}, + }}, + }}, + StatefulDefaultActions: []string{"aws:drop_strict"}, + StatefulEngineOptions: &nftypes.StatefulEngineOptions{RuleOrder: nftypes.RuleOrderStrictOrder}, + PolicyVariables: &nftypes.PolicyVariables{RuleVariables: map[string]nftypes.IPSet{"HOME_NET": {Definition: []string{"10.0.0.0/8"}}}}, + TLSInspectionConfigurationArn: tls.TLSInspectionConfigurationResponse.TLSInspectionConfigurationArn, + EnableTLSSessionHolding: aws.Bool(true), + }, + }) + require.NoError(t, err) + + d, err := c.DescribeFirewallPolicy(bg, &networkfirewall.DescribeFirewallPolicyInput{FirewallPolicyName: aws.String("pol-full")}) + require.NoError(t, err) + + p := d.FirewallPolicy + assert.Equal(t, []string{"aws:pass", "mine"}, p.StatelessDefaultActions) + assert.Equal(t, []string{"aws:drop_strict"}, p.StatefulDefaultActions) + assert.Equal(t, nftypes.RuleOrderStrictOrder, p.StatefulEngineOptions.RuleOrder) + require.Len(t, p.StatelessCustomActions, 1) + assert.Equal(t, "mine", aws.ToString(p.StatelessCustomActions[0].ActionName)) + assert.Equal(t, []string{"10.0.0.0/8"}, p.PolicyVariables.RuleVariables["HOME_NET"].Definition) + assert.True(t, aws.ToBool(p.EnableTLSSessionHolding)) + assert.Equal(t, aws.ToString(tls.TLSInspectionConfigurationResponse.TLSInspectionConfigurationArn), aws.ToString(p.TLSInspectionConfigurationArn)) + + var invalid *nftypes.InvalidRequestException + + _, err = c.CreateFirewallPolicy(bg, &networkfirewall.CreateFirewallPolicyInput{ + FirewallPolicyName: aws.String("pol-bad"), + FirewallPolicy: &nftypes.FirewallPolicy{ + StatelessDefaultActions: []string{"aws:nope"}, StatelessFragmentDefaultActions: []string{"aws:drop"}, + }, + }) + require.ErrorAs(t, err, &invalid) +} + +func TestSDK_ListRuleGroups_FiltersAndTags(t *testing.T) { + s := newStack(t) + + for _, rg := range []struct { + name string + typ nftypes.RuleGroupType + }{{"rg-a", nftypes.RuleGroupTypeStateless}, {"rg-b", nftypes.RuleGroupTypeStateful}} { + _, err := s.c.CreateRuleGroup(bg, &networkfirewall.CreateRuleGroupInput{RuleGroupName: aws.String(rg.name), Type: rg.typ, Capacity: aws.Int32(3)}) + require.NoError(t, err) + } + + all, err := s.c.ListRuleGroups(bg, &networkfirewall.ListRuleGroupsInput{}) + require.NoError(t, err) + assert.Len(t, all.RuleGroups, 2) + + stateless, err := s.c.ListRuleGroups(bg, &networkfirewall.ListRuleGroupsInput{Type: nftypes.RuleGroupTypeStateless}) + require.NoError(t, err) + require.Len(t, stateless.RuleGroups, 1) + assert.Equal(t, "rg-a", aws.ToString(stateless.RuleGroups[0].Name)) + + managed, err := s.c.ListRuleGroups(bg, &networkfirewall.ListRuleGroupsInput{Scope: nftypes.ResourceManagedStatusManaged}) + require.NoError(t, err) + assert.Empty(t, managed.RuleGroups) + + // Tags work on every resource kind, addressed by ARN. + for _, arn := range []string{s.policyARN, s.fwARN, aws.ToString(all.RuleGroups[0].Arn)} { + _, err := s.c.TagResource(bg, &networkfirewall.TagResourceInput{ResourceArn: aws.String(arn), Tags: []nftypes.Tag{{Key: aws.String("k"), Value: aws.String("v")}}}) + require.NoError(t, err) + + got, err := s.c.ListTagsForResource(bg, &networkfirewall.ListTagsForResourceInput{ResourceArn: aws.String(arn)}) + require.NoError(t, err) + assert.Len(t, got.Tags, 1) + } + + fw, err := s.c.DescribeFirewall(bg, &networkfirewall.DescribeFirewallInput{FirewallName: aws.String("fw-1")}) + require.NoError(t, err) + assert.Len(t, fw.Firewall.Tags, 1) +} + +func TestSDK_DescribeByARN_And_Deletes(t *testing.T) { + s := newStack(t) + + rg, err := s.c.CreateRuleGroup(bg, &networkfirewall.CreateRuleGroupInput{RuleGroupName: aws.String("rg"), Type: nftypes.RuleGroupTypeStateful, Capacity: aws.Int32(3)}) + require.NoError(t, err) + + byARN, err := s.c.DescribeRuleGroup(bg, &networkfirewall.DescribeRuleGroupInput{RuleGroupArn: rg.RuleGroupResponse.RuleGroupArn}) + require.NoError(t, err) + assert.Equal(t, "rg", aws.ToString(byARN.RuleGroupResponse.RuleGroupName)) + + _, err = s.c.DeleteRuleGroup(bg, &networkfirewall.DeleteRuleGroupInput{RuleGroupArn: rg.RuleGroupResponse.RuleGroupArn}) + require.NoError(t, err) + + var notFound *nftypes.ResourceNotFoundException + + _, err = s.c.DescribeRuleGroup(bg, &networkfirewall.DescribeRuleGroupInput{RuleGroupArn: rg.RuleGroupResponse.RuleGroupArn}) + require.ErrorAs(t, err, ¬Found) + + _, err = s.c.DeleteFirewall(bg, &networkfirewall.DeleteFirewallInput{FirewallArn: aws.String(s.fwARN)}) + require.NoError(t, err) + + _, err = s.c.DeleteFirewallPolicy(bg, &networkfirewall.DeleteFirewallPolicyInput{FirewallPolicyArn: aws.String(s.policyARN)}) + require.NoError(t, err) + + var invalid *nftypes.InvalidRequestException + + _, err = s.c.DescribeFirewall(bg, &networkfirewall.DescribeFirewallInput{FirewallArn: aws.String("not-an-arn")}) + require.ErrorAs(t, err, &invalid) + + _, err = s.c.CreateRuleGroup(bg, &networkfirewall.CreateRuleGroupInput{RuleGroupName: aws.String("dup"), Type: nftypes.RuleGroupTypeStateful, Capacity: aws.Int32(1)}) + require.NoError(t, err) + + _, err = s.c.CreateRuleGroup(bg, &networkfirewall.CreateRuleGroupInput{RuleGroupName: aws.String("dup"), Type: nftypes.RuleGroupTypeStateful, Capacity: aws.Int32(1)}) + require.ErrorAs(t, err, &invalid, "a duplicate name is InvalidRequestException") +} diff --git a/server/aws/networkfirewall/sdk_roundtrip_test.go b/server/aws/networkfirewall/sdk_roundtrip_test.go index 9f41b5d19..82299f0e2 100644 --- a/server/aws/networkfirewall/sdk_roundtrip_test.go +++ b/server/aws/networkfirewall/sdk_roundtrip_test.go @@ -36,6 +36,26 @@ func newClient(t *testing.T) *networkfirewall.Client { }) } +// policyToken reads the current update token of a firewall policy. +func policyToken(t *testing.T, c *networkfirewall.Client, name string) *string { + t.Helper() + + d, err := c.DescribeFirewallPolicy(context.Background(), &networkfirewall.DescribeFirewallPolicyInput{FirewallPolicyName: aws.String(name)}) + require.NoError(t, err) + + return d.UpdateToken +} + +// ruleGroupToken reads the current update token of a rule group. +func ruleGroupToken(t *testing.T, c *networkfirewall.Client, name string, typ nftypes.RuleGroupType) *string { + t.Helper() + + d, err := c.DescribeRuleGroup(context.Background(), &networkfirewall.DescribeRuleGroupInput{RuleGroupName: aws.String(name), Type: typ}) + require.NoError(t, err) + + return d.UpdateToken +} + func TestSDKNetworkFirewall(t *testing.T) { client := newClient(t) ctx := context.Background() @@ -222,7 +242,7 @@ func TestSDKUpdateFirewallPolicy(t *testing.T) { _, err = client.UpdateFirewallPolicy(ctx, &networkfirewall.UpdateFirewallPolicyInput{ FirewallPolicyName: aws.String("pol-upd"), - UpdateToken: aws.String("00000000-0000-0000-0000-000000000000"), + UpdateToken: policyToken(t, client, "pol-upd"), Description: aws.String("updated"), FirewallPolicy: &nftypes.FirewallPolicy{ StatelessDefaultActions: []string{"aws:drop"}, @@ -256,7 +276,7 @@ func TestSDKUpdateRuleGroup(t *testing.T) { _, err = client.UpdateRuleGroup(ctx, &networkfirewall.UpdateRuleGroupInput{ RuleGroupName: aws.String("rg-upd"), Type: nftypes.RuleGroupTypeStateful, - UpdateToken: aws.String("00000000-0000-0000-0000-000000000000"), + UpdateToken: ruleGroupToken(t, client, "rg-upd", nftypes.RuleGroupTypeStateful), Description: aws.String("after"), }) require.NoError(t, err) @@ -338,7 +358,7 @@ func TestSDKCreateRuleGroupWithRules(t *testing.T) { _, err = client.UpdateRuleGroup(ctx, &networkfirewall.UpdateRuleGroupInput{ RuleGroupName: aws.String("rg-stateful"), Type: nftypes.RuleGroupTypeStateful, - UpdateToken: aws.String("00000000-0000-0000-0000-000000000000"), + UpdateToken: ruleGroupToken(t, client, "rg-stateful", nftypes.RuleGroupTypeStateful), RuleGroup: &nftypes.RuleGroup{ RulesSource: &nftypes.RulesSource{ RulesString: aws.String(`drop tcp any any -> any any (msg:"blocked"; sid:2;)`), @@ -407,7 +427,7 @@ func TestSDKFirewallPolicyRuleGroupReferences(t *testing.T) { // UpdateFirewallPolicy with a different reference set round-trips too. _, err = client.UpdateFirewallPolicy(ctx, &networkfirewall.UpdateFirewallPolicyInput{ FirewallPolicyName: aws.String("pol-refs"), - UpdateToken: aws.String("00000000-0000-0000-0000-000000000000"), + UpdateToken: policyToken(t, client, "pol-refs"), FirewallPolicy: &nftypes.FirewallPolicy{ StatelessDefaultActions: []string{"aws:forward_to_sfe"}, StatelessFragmentDefaultActions: []string{"aws:forward_to_sfe"}, diff --git a/server/aws/networkfirewall/tls_inspection.go b/server/aws/networkfirewall/tls_inspection.go new file mode 100644 index 000000000..b819f6b5b --- /dev/null +++ b/server/aws/networkfirewall/tls_inspection.go @@ -0,0 +1,181 @@ +package networkfirewall + +import ( + "encoding/json" + "net/http" + + "github.com/stackshy/cloudemu/v2/server/wire" + nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" +) + +type certificateJSON struct { + CertificateArn string `json:"CertificateArn"` +} + +type tlsResponseJSON struct { + TLSInspectionConfigurationName string `json:"TLSInspectionConfigurationName"` + TLSInspectionConfigurationArn string `json:"TLSInspectionConfigurationArn"` + TLSInspectionConfigurationID string `json:"TLSInspectionConfigurationId"` + TLSInspectionConfigurationStatus string `json:"TLSInspectionConfigurationStatus,omitempty"` + Description string `json:"Description,omitempty"` + NumberOfAssociations int `json:"NumberOfAssociations"` + LastModifiedTime *float64 `json:"LastModifiedTime,omitempty"` + Certificates []certificateJSON `json:"Certificates,omitempty"` + Tags []tag `json:"Tags,omitempty"` +} + +type createTLSRequest struct { + TLSInspectionConfigurationName string `json:"TLSInspectionConfigurationName"` + TLSInspectionConfiguration json.RawMessage `json:"TLSInspectionConfiguration"` + Description string `json:"Description"` + Tags []tag `json:"Tags"` +} + +type tlsNameArnRequest struct { + TLSInspectionConfigurationName string `json:"TLSInspectionConfigurationName"` + TLSInspectionConfigurationArn string `json:"TLSInspectionConfigurationArn"` +} + +type updateTLSRequest struct { + tlsNameArnRequest + UpdateToken string `json:"UpdateToken"` + TLSInspectionConfiguration json.RawMessage `json:"TLSInspectionConfiguration"` + Description string `json:"Description"` +} + +func toTLSResponseJSON(t *nfdriver.TLSInspectionConfig) tlsResponseJSON { + modified := epochSeconds(t.LastModified) + + certs := make([]certificateJSON, 0, len(t.CertificateARNs)) + for _, arn := range t.CertificateARNs { + certs = append(certs, certificateJSON{CertificateArn: arn}) + } + + return tlsResponseJSON{ + TLSInspectionConfigurationName: t.Name, TLSInspectionConfigurationArn: t.ARN, + TLSInspectionConfigurationID: t.ID, TLSInspectionConfigurationStatus: t.Status, + Description: t.Description, NumberOfAssociations: t.NumberOfAssociations, + LastModifiedTime: &modified, Certificates: certs, Tags: mapToTags(t.Tags), + } +} + +func (h *Handler) createTLSInspectionConfiguration(w http.ResponseWriter, r *http.Request) { + var req createTLSRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + tls, ok := capability[nfdriver.TLSInspection](h, w) + if !ok { + return + } + + t, err := tls.CreateTLSInspectionConfiguration(r.Context(), nfdriver.CreateTLSInspectionConfig{ + Name: req.TLSInspectionConfigurationName, Description: req.Description, + Configuration: req.TLSInspectionConfiguration, Tags: tagsToMap(req.Tags), + }) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, map[string]any{ + "UpdateToken": t.UpdateToken, + "TLSInspectionConfigurationResponse": toTLSResponseJSON(t), + }) +} + +func (h *Handler) describeTLSInspectionConfiguration(w http.ResponseWriter, r *http.Request) { + var req tlsNameArnRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + tls, ok := capability[nfdriver.TLSInspection](h, w) + if !ok { + return + } + + t, err := tls.DescribeTLSInspectionConfiguration(r.Context(), req.TLSInspectionConfigurationName, req.TLSInspectionConfigurationArn) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, map[string]any{ + "UpdateToken": t.UpdateToken, + "TLSInspectionConfigurationResponse": toTLSResponseJSON(t), + "TLSInspectionConfiguration": t.Configuration, + }) +} + +func (h *Handler) updateTLSInspectionConfiguration(w http.ResponseWriter, r *http.Request) { + var req updateTLSRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + tls, ok := capability[nfdriver.TLSInspection](h, w) + if !ok { + return + } + + t, err := tls.UpdateTLSInspectionConfiguration(r.Context(), req.TLSInspectionConfigurationName, req.TLSInspectionConfigurationArn, + nfdriver.UpdateTLSInspectionConfig{ + UpdateToken: req.UpdateToken, Description: req.Description, Configuration: req.TLSInspectionConfiguration, + }) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, map[string]any{ + "UpdateToken": t.UpdateToken, + "TLSInspectionConfigurationResponse": toTLSResponseJSON(t), + }) +} + +func (h *Handler) deleteTLSInspectionConfiguration(w http.ResponseWriter, r *http.Request) { + var req tlsNameArnRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + tls, ok := capability[nfdriver.TLSInspection](h, w) + if !ok { + return + } + + t, err := tls.DeleteTLSInspectionConfiguration(r.Context(), req.TLSInspectionConfigurationName, req.TLSInspectionConfigurationArn) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, map[string]any{"TLSInspectionConfigurationResponse": toTLSResponseJSON(t)}) +} + +func (h *Handler) listTLSInspectionConfigurations(w http.ResponseWriter, r *http.Request) { + var req listRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + tls, ok := capability[nfdriver.TLSInspection](h, w) + if !ok { + return + } + + items, next, err := tls.ListTLSInspectionConfigurations(r.Context(), req.options()) + if err != nil { + writeErr(w, err) + return + } + + metas := make([]map[string]string, 0, len(items)) + for i := range items { + metas = append(metas, map[string]string{"Name": items[i].Name, "Arn": items[i].ARN}) + } + + wire.WriteJSON(w, withNextToken(map[string]any{"TLSInspectionConfigurations": metas}, next)) +} diff --git a/server/aws/networkfirewall/vpc_endpoint_association.go b/server/aws/networkfirewall/vpc_endpoint_association.go new file mode 100644 index 000000000..66bd58e74 --- /dev/null +++ b/server/aws/networkfirewall/vpc_endpoint_association.go @@ -0,0 +1,156 @@ +package networkfirewall + +import ( + "net/http" + + "github.com/stackshy/cloudemu/v2/server/wire" + nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" +) + +type vpcEndpointAssociationJSON struct { + VpcEndpointAssociationArn string `json:"VpcEndpointAssociationArn"` + VpcEndpointAssociationID string `json:"VpcEndpointAssociationId"` + FirewallArn string `json:"FirewallArn"` + VpcID string `json:"VpcId"` + SubnetMapping *subnetMapping `json:"SubnetMapping,omitempty"` + Description string `json:"Description,omitempty"` + Tags []tag `json:"Tags,omitempty"` +} + +type associationStatusJSON struct { + Status string `json:"Status"` + AssociationSyncState map[string]syncStateJSON `json:"AssociationSyncState,omitempty"` +} + +func toAssociationJSON(a *nfdriver.VPCEndpointAssociation) vpcEndpointAssociationJSON { + return vpcEndpointAssociationJSON{ + VpcEndpointAssociationArn: a.ARN, VpcEndpointAssociationID: a.ID, FirewallArn: a.FirewallARN, VpcID: a.VPCID, + SubnetMapping: &subnetMapping{SubnetID: a.SubnetID, IPAddressType: a.IPAddressType}, + Description: a.Description, Tags: mapToTags(a.Tags), + } +} + +func toAssociationStatusJSON(a *nfdriver.VPCEndpointAssociation) associationStatusJSON { + return associationStatusJSON{ + Status: a.Status, + AssociationSyncState: map[string]syncStateJSON{ + a.AvailabilityZone: {Attachment: attachmentJSON{SubnetID: a.SubnetID, EndpointID: "vpce-" + a.ID, Status: "READY"}}, + }, + } +} + +func writeAssociation(w http.ResponseWriter, a *nfdriver.VPCEndpointAssociation) { + wire.WriteJSON(w, map[string]any{ + "VpcEndpointAssociation": toAssociationJSON(a), + "VpcEndpointAssociationStatus": toAssociationStatusJSON(a), + }) +} + +type createAssociationRequest struct { + FirewallArn string `json:"FirewallArn"` + VpcID string `json:"VpcId"` + SubnetMapping *subnetMapping `json:"SubnetMapping"` + Description string `json:"Description"` + Tags []tag `json:"Tags"` +} + +func (h *Handler) createVpcEndpointAssociation(w http.ResponseWriter, r *http.Request) { + var req createAssociationRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + assoc, ok := capability[nfdriver.VPCEndpointAssociations](h, w) + if !ok { + return + } + + cfg := nfdriver.CreateVPCEndpointAssociationConfig{ + FirewallARN: req.FirewallArn, VPCID: req.VpcID, Description: req.Description, Tags: tagsToMap(req.Tags), + } + if req.SubnetMapping != nil { + cfg.SubnetID, cfg.IPAddressType = req.SubnetMapping.SubnetID, req.SubnetMapping.IPAddressType + } + + a, err := assoc.CreateVpcEndpointAssociation(r.Context(), cfg) + if err != nil { + writeErr(w, err) + return + } + + writeAssociation(w, a) +} + +type associationArnRequest struct { + VpcEndpointAssociationArn string `json:"VpcEndpointAssociationArn"` +} + +func (h *Handler) describeVpcEndpointAssociation(w http.ResponseWriter, r *http.Request) { + var req associationArnRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + assoc, ok := capability[nfdriver.VPCEndpointAssociations](h, w) + if !ok { + return + } + + a, err := assoc.DescribeVpcEndpointAssociation(r.Context(), req.VpcEndpointAssociationArn) + if err != nil { + writeErr(w, err) + return + } + + writeAssociation(w, a) +} + +func (h *Handler) deleteVpcEndpointAssociation(w http.ResponseWriter, r *http.Request) { + var req associationArnRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + assoc, ok := capability[nfdriver.VPCEndpointAssociations](h, w) + if !ok { + return + } + + a, err := assoc.DeleteVpcEndpointAssociation(r.Context(), req.VpcEndpointAssociationArn) + if err != nil { + writeErr(w, err) + return + } + + writeAssociation(w, a) +} + +type listAssociationsRequest struct { + listRequest + FirewallArn string `json:"FirewallArn"` +} + +func (h *Handler) listVpcEndpointAssociations(w http.ResponseWriter, r *http.Request) { + var req listAssociationsRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + assoc, ok := capability[nfdriver.VPCEndpointAssociations](h, w) + if !ok { + return + } + + items, next, err := assoc.ListVpcEndpointAssociations(r.Context(), req.FirewallArn, req.options()) + if err != nil { + writeErr(w, err) + return + } + + metas := make([]map[string]string, 0, len(items)) + for i := range items { + metas = append(metas, map[string]string{"VpcEndpointAssociationArn": items[i].ARN}) + } + + wire.WriteJSON(w, withNextToken(map[string]any{"VpcEndpointAssociations": metas}, next)) +} diff --git a/services/networkfirewall/driver/availability_zones.go b/services/networkfirewall/driver/availability_zones.go new file mode 100644 index 000000000..71870263e --- /dev/null +++ b/services/networkfirewall/driver/availability_zones.go @@ -0,0 +1,43 @@ +package driver + +import "hash/fnv" + +// azLetters are the zone suffixes used to give a firewall's subnets stable, +// synthetic Availability Zone names (the emulator has no real subnet to AZ +// mapping). +const azLetters = "abcdef" + +// AvailabilityZoneFor returns the synthetic Availability Zone name for the +// index-th subnet of a firewall in region (index 0 -> "a"). +func AvailabilityZoneFor(region string, index int) string { + if index < 0 { + index = -index + } + + return region + string(azLetters[index%len(azLetters)]) +} + +// SyncZones returns the Availability Zones a firewall runs in: its explicit +// zones for a transit gateway-attached firewall, else one synthetic zone per +// subnet. +func SyncZones(f *Firewall, region string) []string { + if len(f.AvailabilityZones) > 0 { + return append([]string(nil), f.AvailabilityZones...) + } + + zones := make([]string, 0, len(f.SubnetIDs)) + for i := range f.SubnetIDs { + zones = append(zones, AvailabilityZoneFor(region, i)) + } + + return zones +} + +// ZoneForKey returns a stable synthetic Availability Zone name for an opaque key +// (a subnet id): the same key always maps to the same zone. +func ZoneForKey(region, key string) string { + h := fnv.New32a() + _, _ = h.Write([]byte(key)) + + return AvailabilityZoneFor(region, int(h.Sum32()%uint32(len(azLetters)))) +} diff --git a/services/networkfirewall/driver/capabilities.go b/services/networkfirewall/driver/capabilities.go new file mode 100644 index 000000000..3f24b4430 --- /dev/null +++ b/services/networkfirewall/driver/capabilities.go @@ -0,0 +1,195 @@ +package driver + +import ( + "context" + "encoding/json" + "time" +) + +// The interfaces below are optional capability groups. A driver implements the +// ones it supports; the wire handler finds them by type assertion and answers +// UnsupportedOperationException for a group the driver lacks, so the base +// NetworkFirewall interface is not widened for every implementer. + +// FirewallSettings covers the firewall-level setters. +type FirewallSettings interface { + UpdateFirewallDescription(ctx context.Context, firewallName, description, updateToken string) (*Firewall, error) + UpdateSubnetChangeProtection(ctx context.Context, firewallName string, enabled bool, updateToken string) (*Firewall, error) + UpdateFirewallPolicyChangeProtection(ctx context.Context, firewallName string, enabled bool, updateToken string) (*Firewall, error) + UpdateAvailabilityZoneChangeProtection(ctx context.Context, firewallName string, enabled bool, updateToken string) (*Firewall, error) + UpdateFirewallAnalysisSettings(ctx context.Context, firewallName string, analysisTypes []string, updateToken string) (*Firewall, error) +} + +// AvailabilityZones covers the Availability-Zone associations of a transit +// gateway-attached firewall. +type AvailabilityZones interface { + AssociateAvailabilityZones(ctx context.Context, firewallName string, zones []string, updateToken string) (*Firewall, error) + DisassociateAvailabilityZones(ctx context.Context, firewallName string, zones []string, updateToken string) (*Firewall, error) +} + +// ResourcePolicies covers the resource-sharing IAM policies attached to a rule +// group, firewall policy or firewall. +type ResourcePolicies interface { + PutResourcePolicy(ctx context.Context, resourceARN, policy string) error + DescribeResourcePolicy(ctx context.Context, resourceARN string) (string, error) + DeleteResourcePolicy(ctx context.Context, resourceARN string) error +} + +// TLSInspectionConfig is a TLS inspection configuration a firewall policy can +// reference. Configuration holds the "TLSInspectionConfiguration" payload +// (ServerCertificateConfigurations) exactly as the caller provided it. +type TLSInspectionConfig struct { + Name string + ARN string + ID string + Description string + Configuration json.RawMessage + CertificateARNs []string + Status string + NumberOfAssociations int + LastModified time.Time + UpdateToken string + Tags map[string]string +} + +// CreateTLSInspectionConfig is the input to CreateTLSInspectionConfiguration. +type CreateTLSInspectionConfig struct { + Name string + Description string + Configuration json.RawMessage + Tags map[string]string +} + +// UpdateTLSInspectionConfig is the input to UpdateTLSInspectionConfiguration. +// UpdateToken is required. +type UpdateTLSInspectionConfig struct { + UpdateToken string + Description string + Configuration json.RawMessage +} + +// TLSInspection covers the TLS inspection configuration family. +type TLSInspection interface { + CreateTLSInspectionConfiguration(ctx context.Context, cfg CreateTLSInspectionConfig) (*TLSInspectionConfig, error) + DescribeTLSInspectionConfiguration(ctx context.Context, name, arn string) (*TLSInspectionConfig, error) + UpdateTLSInspectionConfiguration(ctx context.Context, name, arn string, cfg UpdateTLSInspectionConfig) (*TLSInspectionConfig, error) + DeleteTLSInspectionConfiguration(ctx context.Context, name, arn string) (*TLSInspectionConfig, error) + ListTLSInspectionConfigurations(ctx context.Context, opts ListOptions) ([]TLSInspectionConfig, string, error) +} + +// VPCEndpointAssociation is an extra firewall endpoint in another VPC/subnet. +type VPCEndpointAssociation struct { + ARN string + ID string + FirewallARN string + VPCID string + SubnetID string + IPAddressType string + AvailabilityZone string + Description string + Status string + Tags map[string]string +} + +// CreateVPCEndpointAssociationConfig is the input to CreateVpcEndpointAssociation. +type CreateVPCEndpointAssociationConfig struct { + FirewallARN string + VPCID string + SubnetID string + IPAddressType string + Description string + Tags map[string]string +} + +// VPCEndpointAssociations covers the VPC endpoint association family. +type VPCEndpointAssociations interface { + CreateVpcEndpointAssociation(ctx context.Context, cfg CreateVPCEndpointAssociationConfig) (*VPCEndpointAssociation, error) + DescribeVpcEndpointAssociation(ctx context.Context, arn string) (*VPCEndpointAssociation, error) + DeleteVpcEndpointAssociation(ctx context.Context, arn string) (*VPCEndpointAssociation, error) + ListVpcEndpointAssociations(ctx context.Context, firewallARN string, opts ListOptions) ([]VPCEndpointAssociation, string, error) +} + +// FirewallMetadata is the DescribeFirewallMetadata result. +type FirewallMetadata struct { + FirewallARN string + PolicyARN string + Description string + Status string + AvailabilityZones []string +} + +// RuleSummary is one entry of a rule group summary. +type RuleSummary struct { + Msg string + SID string + Metadata string +} + +// Metadata covers the read-only metadata/summary operations. +type Metadata interface { + DescribeFirewallMetadata(ctx context.Context, firewallARN string) (*FirewallMetadata, error) + DescribeRuleGroupMetadata(ctx context.Context, name, arn, ruleType string) (*RuleGroup, error) + DescribeRuleGroupSummary(ctx context.Context, name, arn, ruleType string) (*RuleGroup, []RuleSummary, error) +} + +// FlowOperation is a flow capture or flush request. The emulator has no data +// plane, so an operation completes without results. +type FlowOperation struct { + ID string + FirewallARN string + Type string // FLOW_CAPTURE | FLOW_FLUSH + Status string + StatusMessage string + AvailabilityZone string + VPCEndpointID string + VPCEndpointAssociationARN string + MinimumFlowAgeSeconds *int32 + Filters json.RawMessage + RequestedAt time.Time +} + +// FlowOperationRequest is the input to StartFlowCapture / StartFlowFlush. +type FlowOperationRequest struct { + FirewallARN string + AvailabilityZone string + VPCEndpointID string + VPCEndpointAssociationARN string + MinimumFlowAgeSeconds *int32 + Filters json.RawMessage +} + +// FlowOperationScope identifies the firewall endpoint a flow operation ran on. +type FlowOperationScope struct { + FirewallARN string + AvailabilityZone string + VPCEndpointID string + VPCEndpointAssociationARN string +} + +// FlowOperations covers the flow capture / flush family. +type FlowOperations interface { + StartFlowCapture(ctx context.Context, req FlowOperationRequest) (*FlowOperation, error) + StartFlowFlush(ctx context.Context, req FlowOperationRequest) (*FlowOperation, error) + DescribeFlowOperation(ctx context.Context, scope FlowOperationScope, id string) (*FlowOperation, error) + ListFlowOperations(ctx context.Context, scope FlowOperationScope, opType string, opts ListOptions) ([]FlowOperation, string, error) + // ListFlowOperationResults returns the operation and its (always empty, there is no + // data plane) flows. + ListFlowOperationResults(ctx context.Context, scope FlowOperationScope, id string, opts ListOptions) (*FlowOperation, string, error) +} + +// AnalysisReport is a traffic analysis report request. +type AnalysisReport struct { + ID string + FirewallARN string + AnalysisType string + Status string + ReportTime time.Time +} + +// AnalysisReports covers the analysis report family. +type AnalysisReports interface { + StartAnalysisReport(ctx context.Context, firewallName, firewallARN, analysisType string) (*AnalysisReport, error) + ListAnalysisReports(ctx context.Context, firewallName, firewallARN string, opts ListOptions) ([]AnalysisReport, string, error) + // GetAnalysisReport returns one report (its results are always empty). + GetAnalysisReport(ctx context.Context, firewallName, firewallARN, reportID string) (*AnalysisReport, error) +} diff --git a/services/networkfirewall/driver/driver.go b/services/networkfirewall/driver/driver.go index aafcf24ea..d95f9bf4e 100644 --- a/services/networkfirewall/driver/driver.go +++ b/services/networkfirewall/driver/driver.go @@ -6,42 +6,76 @@ package driver import ( "context" "encoding/json" + "time" ) -// Firewall is a Network Firewall attached to a VPC across subnets. +// Firewall is a Network Firewall attached to a VPC across subnets, or to a +// transit gateway across Availability Zones. +// +// UpdateToken is the optimistic-locking token of the firewall: it is minted at +// create and rotated by every mutation. NumberOfAssociations is computed (the +// firewall's VPC endpoint associations). type Firewall struct { - Name string - ARN string - ID string - PolicyARN string - VPCID string - SubnetIDs []string - Description string - DeleteProtection bool - Status string - Tags map[string]string + Name string + ARN string + ID string + PolicyARN string + VPCID string + SubnetIDs []string + AvailabilityZones []string + TransitGatewayID string + Description string + DeleteProtection bool + SubnetChangeProtection bool + FirewallPolicyChangeProtection bool + AvailabilityZoneChangeProtection bool + EnabledAnalysisTypes []string + NumberOfAssociations int + Status string + UpdateToken string + Tags map[string]string } // CreateFirewallConfig is the input to CreateFirewall. type CreateFirewallConfig struct { - Name string - PolicyARN string - VPCID string - SubnetIDs []string - Description string - DeleteProtection bool - Tags map[string]string + Name string + PolicyARN string + VPCID string + SubnetIDs []string + AvailabilityZones []string + TransitGatewayID string + Description string + DeleteProtection bool + SubnetChangeProtection bool + FirewallPolicyChangeProtection bool + AvailabilityZoneChangeProtection bool + EnabledAnalysisTypes []string + Tags map[string]string } // RuleGroupReference links a firewall policy to a rule group, by ARN. Priority // orders stateless rule groups (required by AWS) and, for STRICT_ORDER -// stateful policies, stateful rule groups too. +// stateful policies, stateful rule groups too. Override and +// DeepThreatInspection are stateful-only settings kept byte-faithfully. type RuleGroupReference struct { - ResourceARN string - Priority int + ResourceARN string + Priority int + Override json.RawMessage + DeepThreatInspection *bool +} + +// PolicyExtra holds the firewall-policy settings the emulator stores and +// returns verbatim without interpreting them. +type PolicyExtra struct { + StatelessCustomActions json.RawMessage + PolicyVariables json.RawMessage + StatefulEngineOptions json.RawMessage + EnableTLSSessionHolding *bool } -// FirewallPolicy groups stateless/stateful rule-group references + default actions. +// FirewallPolicy groups stateless/stateful rule-group references + default +// actions. The Consumed* capacities, NumberOfAssociations, Status and +// LastModified are computed by the provider. type FirewallPolicy struct { Name string ARN string @@ -49,8 +83,18 @@ type FirewallPolicy struct { Description string StatelessDefaultActions []string StatelessFragmentDefaultActions []string + StatefulDefaultActions []string StatefulRuleGroupReferences []RuleGroupReference StatelessRuleGroupReferences []RuleGroupReference + TLSInspectionConfigurationARN string + Extra PolicyExtra + Status string + NumberOfAssociations int + ConsumedStatelessRuleCapacity int + ConsumedStatefulRuleCapacity int + ConsumedStatefulDomainCapacity int + LastModified time.Time + UpdateToken string Tags map[string]string } @@ -60,54 +104,72 @@ type CreateFirewallPolicyConfig struct { Description string StatelessDefaultActions []string StatelessFragmentDefaultActions []string + StatefulDefaultActions []string StatefulRuleGroupReferences []RuleGroupReference StatelessRuleGroupReferences []RuleGroupReference + TLSInspectionConfigurationARN string + Extra PolicyExtra Tags map[string]string } +// UpdateFirewallPolicyConfig is the input to UpdateFirewallPolicy. UpdateToken +// is required (AWS marks it required on this operation). +type UpdateFirewallPolicyConfig struct { + UpdateToken string + Description string + StatelessDefaultActions []string + StatelessFragmentDefaultActions []string + StatefulDefaultActions []string + StatefulRuleGroupReferences []RuleGroupReference + StatelessRuleGroupReferences []RuleGroupReference + TLSInspectionConfigurationARN string + Extra PolicyExtra +} + // RuleGroup is a reusable collection of stateful or stateless rules. // // Rules holds the full "RuleGroup" payload (RulesSource, RuleVariables, // ReferenceSets, StatefulRuleOptions) exactly as the caller provided it. The -// emulator doesn't interpret Suricata/stateful/stateless rule content, so it -// is stored and returned byte-faithfully rather than modeled field-by-field. +// emulator doesn't interpret Suricata/stateful/stateless rule content beyond +// computing the capacity it consumes and the rule summaries, so it is stored +// and returned byte-faithfully rather than modeled field-by-field. type RuleGroup struct { - Name string - ARN string - ID string - Type string // STATEFUL | STATELESS - Capacity int - Description string - Rules json.RawMessage - Tags map[string]string + Name string + ARN string + ID string + Type string // STATEFUL | STATELESS | STATEFUL_DOMAIN + Capacity int + ConsumedCapacity int + NumberOfAssociations int + Description string + Rules json.RawMessage + SummaryRuleOptions []string + Status string + LastModified time.Time + UpdateToken string + Tags map[string]string } // CreateRuleGroupConfig is the input to CreateRuleGroup. type CreateRuleGroupConfig struct { - Name string - Type string - Capacity int - Description string - Rules json.RawMessage - Tags map[string]string -} - -// UpdateFirewallPolicyConfig is the input to UpdateFirewallPolicy. -type UpdateFirewallPolicyConfig struct { - Description string - StatelessDefaultActions []string - StatelessFragmentDefaultActions []string - StatefulRuleGroupReferences []RuleGroupReference - StatelessRuleGroupReferences []RuleGroupReference + Name string + Type string + Capacity int + Description string + Rules json.RawMessage + SummaryRuleOptions []string + Tags map[string]string } // UpdateRuleGroupConfig is the input to UpdateRuleGroup. Capacity is immutable // in real Network Firewall. Rules is left nil when the caller didn't send a // RuleGroup/Rules payload, so an update that only changes the description -// doesn't wipe existing rule content. +// doesn't wipe existing rule content. UpdateToken is required. type UpdateRuleGroupConfig struct { - Description string - Rules json.RawMessage + UpdateToken string + Description string + Rules json.RawMessage + SummaryRuleOptions []string } // LogDestinationConfig configures where one log type (ALERT/FLOW/TLS) is @@ -119,33 +181,63 @@ type LogDestinationConfig struct { LogDestination map[string]string } -// NetworkFirewall is the AWS Network Firewall control plane. +// LoggingConfiguration is a firewall's log destinations plus the monitoring +// dashboard switch. A nil EnableMonitoringDashboard on update leaves the stored +// switch unchanged. +type LoggingConfiguration struct { + Destinations []LogDestinationConfig + EnableMonitoringDashboard *bool +} + +// ListOptions carries MaxResults/NextToken for a list operation. A zero +// MaxResults means "the service default" (100). +type ListOptions struct { + MaxResults int + NextToken string +} + +// RuleGroupFilter narrows ListRuleGroups. Scope (MANAGED | ACCOUNT) and +// ManagedType select AWS-managed rule groups, which the emulator does not have, +// so MANAGED or a ManagedType yields an empty list. +type RuleGroupFilter struct { + Type string + Scope string + ManagedType string +} + +// NetworkFirewall is the AWS Network Firewall control plane (the operations +// every driver implements). Further operation families are optional +// capability interfaces in capabilities.go. +// +// Mutations take the update token the caller last read ("" = unconditional, +// permitted only where AWS marks the token optional): a stale token fails with +// InvalidToken. // //nolint:interfacebloat // mirrors the network-firewall API surface. type NetworkFirewall interface { CreateFirewall(ctx context.Context, cfg CreateFirewallConfig) (*Firewall, error) DescribeFirewall(ctx context.Context, name, arn string) (*Firewall, error) DeleteFirewall(ctx context.Context, name, arn string) (*Firewall, error) - ListFirewalls(ctx context.Context) ([]Firewall, error) + ListFirewalls(ctx context.Context, vpcIDs []string, opts ListOptions) ([]Firewall, string, error) CreateFirewallPolicy(ctx context.Context, cfg CreateFirewallPolicyConfig) (*FirewallPolicy, error) DescribeFirewallPolicy(ctx context.Context, name, arn string) (*FirewallPolicy, error) UpdateFirewallPolicy(ctx context.Context, name, arn string, cfg UpdateFirewallPolicyConfig) (*FirewallPolicy, error) DeleteFirewallPolicy(ctx context.Context, name, arn string) (*FirewallPolicy, error) - ListFirewallPolicies(ctx context.Context) ([]FirewallPolicy, error) + ListFirewallPolicies(ctx context.Context, opts ListOptions) ([]FirewallPolicy, string, error) CreateRuleGroup(ctx context.Context, cfg CreateRuleGroupConfig) (*RuleGroup, error) DescribeRuleGroup(ctx context.Context, name, arn, ruleType string) (*RuleGroup, error) UpdateRuleGroup(ctx context.Context, name, arn, ruleType string, cfg UpdateRuleGroupConfig) (*RuleGroup, error) DeleteRuleGroup(ctx context.Context, name, arn, ruleType string) (*RuleGroup, error) - ListRuleGroups(ctx context.Context) ([]RuleGroup, error) - - AssociateFirewallPolicy(ctx context.Context, firewallName, policyARN string) (*Firewall, error) - AssociateSubnets(ctx context.Context, firewallName string, subnetIDs []string) (*Firewall, error) - DisassociateSubnets(ctx context.Context, firewallName string, subnetIDs []string) (*Firewall, error) - UpdateFirewallDeleteProtection(ctx context.Context, firewallName string, enabled bool) (*Firewall, error) - UpdateLoggingConfiguration(ctx context.Context, firewallName string, configs []LogDestinationConfig) error - DescribeLoggingConfiguration(ctx context.Context, firewallName string) ([]LogDestinationConfig, error) + ListRuleGroups(ctx context.Context, filter RuleGroupFilter, opts ListOptions) ([]RuleGroup, string, error) + + AssociateFirewallPolicy(ctx context.Context, firewallName, policyARN, updateToken string) (*Firewall, error) + AssociateSubnets(ctx context.Context, firewallName string, subnetIDs []string, updateToken string) (*Firewall, error) + DisassociateSubnets(ctx context.Context, firewallName string, subnetIDs []string, updateToken string) (*Firewall, error) + UpdateFirewallDeleteProtection(ctx context.Context, firewallName string, enabled bool, updateToken string) (*Firewall, error) + UpdateLoggingConfiguration(ctx context.Context, firewallName string, cfg *LoggingConfiguration) error + DescribeLoggingConfiguration(ctx context.Context, firewallName string) (*LoggingConfiguration, error) TagResource(ctx context.Context, arn string, tags map[string]string) error UntagResource(ctx context.Context, arn string, keys []string) error ListTagsForResource(ctx context.Context, arn string) (map[string]string, error) diff --git a/services/networkfirewall/driver/errors.go b/services/networkfirewall/driver/errors.go new file mode 100644 index 000000000..666c82bd2 --- /dev/null +++ b/services/networkfirewall/driver/errors.go @@ -0,0 +1,59 @@ +package driver + +import ( + "errors" + "fmt" +) + +// Network Firewall exception names, as they appear in the "__type" member of an +// AWS JSON 1.0 error. The wire handler renders these verbatim, so the SDK +// surfaces the exact typed exception a real firewall returns. +const ( + ExInvalidOperation = "InvalidOperationException" + ExInvalidToken = "InvalidTokenException" + ExInvalidResourcePolicy = "InvalidResourcePolicyException" + ExUnsupportedOperation = "UnsupportedOperationException" +) + +// APIError tags an error with the Network Firewall exception it must surface +// as. Errors without a tag fall back to the generic code mapping in the wire +// handler (NotFound -> ResourceNotFoundException and so on). +type APIError struct { + Exception string + Err error +} + +func (e *APIError) Error() string { return e.Err.Error() } +func (e *APIError) Unwrap() error { return e.Err } + +func newAPIError(exception, format string, args ...any) error { + return &APIError{Exception: exception, Err: fmt.Errorf(format, args...)} //nolint:err113 // typed API error carrying a formatted message +} + +// InvalidOperation returns an InvalidOperationException: the request is valid +// but not allowed in the resource's current state (delete protection, a +// resource that is still in use, change protection). +func InvalidOperation(format string, args ...any) error { + return newAPIError(ExInvalidOperation, format, args...) +} + +// InvalidToken returns an InvalidTokenException: the update token is stale. +func InvalidToken(format string, args ...any) error { + return newAPIError(ExInvalidToken, format, args...) +} + +// InvalidResourcePolicy returns an InvalidResourcePolicyException: a resource +// policy statement failed validation. +func InvalidResourcePolicy(format string, args ...any) error { + return newAPIError(ExInvalidResourcePolicy, format, args...) +} + +// ExceptionOf returns the exception tagged on err and true, or "" and false. +func ExceptionOf(err error) (string, bool) { + var api *APIError + if errors.As(err, &api) { + return api.Exception, true + } + + return "", false +} From 417f486aad8334d11508a9a9b5ed3746d392b559 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Patel Date: Thu, 22 Oct 2026 04:09:09 +0530 Subject: [PATCH 02/31] fix(aws-cloudwatchlogs): retention, ingest window, ListLogGroups and KMS keys (#1655) Add DeleteRetentionPolicy, ListLogGroups, Associate/DisassociateKmsKey and kmsKeyId/logGroupClass on CreateLogGroup. PutLogEvents now drops events older than 14 days, past retention or more than 2h ahead and reports them in rejectedLogEventsInfo. Expired events are purged so GetLogEvents, FilterLogEvents and storedBytes agree. Part of #1476. --- .../fixtures/cloudwatch-logs/main.tf | 26 ++ .../fixtures/cloudwatch-logs/provider.tf | 29 ++ contrib/terraform/terraform_test.go | 2 +- docs/coverage/aws/cloudwatchlogs.md | 18 + docs/coverage/coverage.json | 28 ++ providers/aws/aws.go | 3 + .../aws/cloudwatchlogs/cloudwatchlogs.go | 200 ++--------- .../aws/cloudwatchlogs/cloudwatchlogs_test.go | 28 +- .../cloudwatchlogs/event_retrieval_test.go | 20 +- providers/aws/cloudwatchlogs/ingest.go | 213 +++++++++++ .../aws/cloudwatchlogs/ingest_window_test.go | 196 +++++++++++ providers/aws/cloudwatchlogs/kms.go | 138 ++++++++ providers/aws/cloudwatchlogs/log_groups.go | 177 ++++++++++ .../aws/cloudwatchlogs/log_groups_test.go | 197 +++++++++++ providers/aws/cloudwatchlogs/retention.go | 127 +++++++ .../cloudwatchlogs/retention_policy_test.go | 114 ++++++ providers/aws/cloudwatchlogs/snapshot.go | 29 +- providers/aws/cloudwatchlogs/snapshot_test.go | 3 +- .../aws/cloudwatchlogs/validation_test.go | 3 +- providers/aws/lambda/execlogs_test.go | 7 +- server/aws/cloudwatchlogs/handler.go | 81 ++--- .../aws/cloudwatchlogs/log_group_settings.go | 178 ++++++++++ .../log_group_settings_sdk_test.go | 332 ++++++++++++++++++ server/aws/cloudwatchlogs/operations.go | 62 +++- .../aws/cloudwatchlogs/sdk_roundtrip_test.go | 4 +- server/aws/cloudwatchlogs/types.go | 36 +- services/logging/driver/aws.go | 77 ++++ services/logging/logging_test.go | 10 +- 28 files changed, 2078 insertions(+), 260 deletions(-) create mode 100644 contrib/terraform/fixtures/cloudwatch-logs/main.tf create mode 100644 contrib/terraform/fixtures/cloudwatch-logs/provider.tf create mode 100644 providers/aws/cloudwatchlogs/ingest.go create mode 100644 providers/aws/cloudwatchlogs/ingest_window_test.go create mode 100644 providers/aws/cloudwatchlogs/kms.go create mode 100644 providers/aws/cloudwatchlogs/log_groups.go create mode 100644 providers/aws/cloudwatchlogs/log_groups_test.go create mode 100644 providers/aws/cloudwatchlogs/retention.go create mode 100644 providers/aws/cloudwatchlogs/retention_policy_test.go create mode 100644 server/aws/cloudwatchlogs/log_group_settings.go create mode 100644 server/aws/cloudwatchlogs/log_group_settings_sdk_test.go create mode 100644 services/logging/driver/aws.go diff --git a/contrib/terraform/fixtures/cloudwatch-logs/main.tf b/contrib/terraform/fixtures/cloudwatch-logs/main.tf new file mode 100644 index 000000000..126f37c48 --- /dev/null +++ b/contrib/terraform/fixtures/cloudwatch-logs/main.tf @@ -0,0 +1,26 @@ +resource "aws_kms_key" "logs" { + description = "cloudemu-tf-logs" + deletion_window_in_days = 7 +} + +resource "aws_cloudwatch_log_group" "encrypted" { + name = "/cloudemu-tf/encrypted" + retention_in_days = 7 + kms_key_id = aws_kms_key.logs.arn + log_group_class = "STANDARD" + + tags = { + env = "test" + } +} + +resource "aws_cloudwatch_log_group" "infrequent" { + name = "/cloudemu-tf/infrequent" + retention_in_days = 0 + log_group_class = "INFREQUENT_ACCESS" +} + +resource "aws_cloudwatch_log_stream" "s1" { + name = "s1" + log_group_name = aws_cloudwatch_log_group.encrypted.name +} diff --git a/contrib/terraform/fixtures/cloudwatch-logs/provider.tf b/contrib/terraform/fixtures/cloudwatch-logs/provider.tf new file mode 100644 index 000000000..cd0af6889 --- /dev/null +++ b/contrib/terraform/fixtures/cloudwatch-logs/provider.tf @@ -0,0 +1,29 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 5.0" + } + } +} + +variable "endpoint" { + type = string + description = "CloudEmu AWS endpoint URL" +} + +provider "aws" { + access_key = "test" + secret_key = "test" + region = "us-east-1" + + skip_credentials_validation = true + skip_metadata_api_check = true + skip_requesting_account_id = true + + endpoints { + cloudwatchlogs = var.endpoint + kms = var.endpoint + sts = var.endpoint + } +} diff --git a/contrib/terraform/terraform_test.go b/contrib/terraform/terraform_test.go index 48caff04b..5fb9aaedc 100644 --- a/contrib/terraform/terraform_test.go +++ b/contrib/terraform/terraform_test.go @@ -32,7 +32,7 @@ import ( func TestTerraformApplyIsIdempotent(t *testing.T) { bin := tfBinary(t) - for _, fixture := range []string{"basic", "networking", "core-services", "cloudwatch-alarms"} { + for _, fixture := range []string{"basic", "networking", "core-services", "cloudwatch-alarms", "cloudwatch-logs"} { t.Run(fixture, func(t *testing.T) { applyFixture(t, fixture, bin) }) diff --git a/docs/coverage/aws/cloudwatchlogs.md b/docs/coverage/aws/cloudwatchlogs.md index 55912a8af..b9bfe0106 100644 --- a/docs/coverage/aws/cloudwatchlogs.md +++ b/docs/coverage/aws/cloudwatchlogs.md @@ -25,6 +25,24 @@ AWS's `logging` service ยท portable interface `driver.Logging` ยท [AWS index](./ | `PutSubscriptionFilter` | | | `UpdateLogGroup` | UpdateLogGroup replaces the mutable fields (retention, tags) of an | +## Optional capabilities + +Discovered by type assertion; only some providers implement these. + +### AWSLogging + +AWSLogging is an optional interface implemented by the AWS CloudWatch Logs + +| Operation | Description | +| --- | --- | +| `AssociateKmsKey` | AssociateKmsKey and DisassociateKmsKey target exactly one of a log group | +| `CreateAWSLogGroup` | | +| `DeleteRetentionPolicy` | | +| `DisassociateKmsKey` | | +| `ListAWSLogGroups` | | +| `PutLogEventsWithResult` | | +| `PutRetentionPolicy` | | + ## Not in scope _Not documented yet. See the [emulator boundary](../../../README.md) for cloudemu-wide non-goals._ diff --git a/docs/coverage/coverage.json b/docs/coverage/coverage.json index 2ae466bf1..e2552a9e6 100644 --- a/docs/coverage/coverage.json +++ b/docs/coverage/coverage.json @@ -11558,6 +11558,34 @@ } ], "optionalCapabilities": [ + { + "name": "AWSLogging", + "doc": "AWSLogging is an optional interface implemented by the AWS CloudWatch Logs", + "operations": [ + { + "name": "AssociateKmsKey", + "doc": "AssociateKmsKey and DisassociateKmsKey target exactly one of a log group" + }, + { + "name": "CreateAWSLogGroup" + }, + { + "name": "DeleteRetentionPolicy" + }, + { + "name": "DisassociateKmsKey" + }, + { + "name": "ListAWSLogGroups" + }, + { + "name": "PutLogEventsWithResult" + }, + { + "name": "PutRetentionPolicy" + } + ] + }, { "name": "GCPLogging", "doc": "GCPLogging is an optional interface implemented by the GCP logging backend for", diff --git a/providers/aws/aws.go b/providers/aws/aws.go index 819ff4607..c8e0336f1 100644 --- a/providers/aws/aws.go +++ b/providers/aws/aws.go @@ -431,6 +431,9 @@ func newProvider(o *config.Options, shared *GlobalServices) *Provider { p.ElastiCache.SetMonitoring(p.CloudWatch) p.MemoryDB.SetMonitoring(p.CloudWatch) p.CloudWatchLogs.SetMonitoring(p.CloudWatch) + // CloudWatch Logs -> KMS: a log group key (CreateLogGroup kmsKeyId, + // AssociateKmsKey) must be an existing, enabled KMS key. + p.CloudWatchLogs.SetKeyResolver(p.KMS) p.SNS.SetMonitoring(p.CloudWatch) p.ECR.SetMonitoring(p.CloudWatch) p.ECS.SetMonitoring(p.CloudWatch) diff --git a/providers/aws/cloudwatchlogs/cloudwatchlogs.go b/providers/aws/cloudwatchlogs/cloudwatchlogs.go index 3004b91b6..001bdb31e 100644 --- a/providers/aws/cloudwatchlogs/cloudwatchlogs.go +++ b/providers/aws/cloudwatchlogs/cloudwatchlogs.go @@ -12,9 +12,7 @@ import ( "github.com/stackshy/cloudemu/v2/config" "github.com/stackshy/cloudemu/v2/errors" - "github.com/stackshy/cloudemu/v2/internal/idgen" "github.com/stackshy/cloudemu/v2/internal/memstore" - "github.com/stackshy/cloudemu/v2/internal/regionctx" "github.com/stackshy/cloudemu/v2/services/logging/driver" mondriver "github.com/stackshy/cloudemu/v2/services/monitoring/driver" "github.com/stackshy/cloudemu/v2/services/scope" @@ -22,23 +20,26 @@ import ( const defaultLogLimit = 100 -// putLogEventsMaxBatchSpan bounds how far apart the oldest and newest event -// timestamps in a single PutLogEvents batch may be. Real CloudWatch Logs: a -// batch of log events in a single request cannot span more than 24 hours, -// otherwise the PutLogEvents operation fails. -const putLogEventsMaxBatchSpan = 24 * time.Hour - -// Compile-time check that Mock implements driver.Logging. -var _ driver.Logging = (*Mock)(nil) +// Compile-time checks that Mock implements driver.Logging and the +// CloudWatch Logs-only driver.AWSLogging surface. +var ( + _ driver.Logging = (*Mock)(nil) + _ driver.AWSLogging = (*Mock)(nil) +) type logStream struct { info driver.LogStreamInfo events []driver.LogEvent - mu sync.RWMutex + // uploads counts accepted PutLogEvents calls; it seeds the (deprecated, + // ignored-on-input) nextSequenceToken so the token changes per upload. + uploads uint64 + mu sync.RWMutex } type logGroup struct { info driver.LogGroupInfo + kmsKeyID string + class string streams *memstore.Store[*logStream] metricFilters *memstore.Store[*driver.MetricFilterInfo] subFilters *memstore.Store[*driver.SubscriptionFilterInfo] @@ -57,6 +58,12 @@ type Mock struct { opts *config.Options monitoring mondriver.Monitoring lambda LambdaInvoker + keys KeyResolver + + // queryResultKmsKey is the account-wide key associated with Logs Insights + // query results (AssociateKmsKey on the query-result resource). + queryResultKmsKey string + mu sync.RWMutex } // SetMonitoring sets the monitoring backend for auto-metric generation. @@ -89,48 +96,15 @@ func New(opts *config.Options) *Mock { } } -// CreateLogGroup creates a new CloudWatch log group. +// CreateLogGroup creates a new CloudWatch log group in the STANDARD class +// with no KMS key. func (m *Mock) CreateLogGroup(ctx context.Context, cfg driver.LogGroupConfig) (*driver.LogGroupInfo, error) { - if err := validateLogGroupName(cfg.Name); err != nil { + g, err := m.CreateAWSLogGroup(ctx, &cfg, driver.AWSLogGroupOptions{}) + if err != nil { return nil, err } - if m.groups.Has(cfg.Name) { - return nil, errors.Newf(errors.AlreadyExists, "log group %q already exists", cfg.Name) - } - - // A log group created without a retention policy never expires: AWS leaves - // retentionInDays unset (nil on the SDK) rather than defaulting to 30 days. - // The zero value flows through to DescribeLogGroups, which omits the field. - arn := idgen.AWSARN("logs", regionctx.RegionOr(ctx, m.opts.Region), m.opts.AccountID, "log-group:"+cfg.Name) - - tags := make(map[string]string, len(cfg.Tags)) - for k, v := range cfg.Tags { - tags[k] = v - } - - info := driver.LogGroupInfo{ - Name: cfg.Name, - Scope: cfg.Scope, - ResourceID: arn, - RetentionDays: cfg.RetentionDays, - CreatedAt: m.opts.Clock.Now().UTC().Format(time.RFC3339), - StoredBytes: 0, - Tags: tags, - } - - g := &logGroup{ - info: info, - streams: memstore.New[*logStream](), - metricFilters: memstore.New[*driver.MetricFilterInfo](), - subFilters: memstore.New[*driver.SubscriptionFilterInfo](), - } - - m.groups.Set(cfg.Name, g) - - result := info - - return &result, nil + return &g.LogGroupInfo, nil } // DeleteLogGroup deletes a CloudWatch log group by name. @@ -235,111 +209,13 @@ func (m *Mock) ListLogStreams(_ context.Context, logGroup string) ([]driver.LogS return streams, nil } -// PutLogEvents writes log events to a stream. +// PutLogEvents writes log events to a stream. Events outside the ingest +// window are dropped exactly as PutLogEventsWithResult drops them; the +// rejection details are discarded. func (m *Mock) PutLogEvents(ctx context.Context, groupName, streamName string, events []driver.LogEvent) error { - g, ok := m.groups.Get(groupName) - if !ok { - return errors.Newf(errors.NotFound, "log group %q not found", groupName) - } - - s, ok := g.streams.Get(streamName) - if !ok { - return errors.Newf(errors.NotFound, "log stream %q not found in group %q", streamName, groupName) - } - - if len(events) == 0 { - return errors.New(errors.InvalidArgument, "logEvents cannot be empty") - } - - if err := validatePutLogEventsBatch(events); err != nil { - return err - } - - ingestedAt := m.opts.Clock.Now().UTC() - - copied := make([]driver.LogEvent, len(events)) - for i := range events { - copied[i] = events[i] - copied[i].IngestionTime = ingestedAt - } + _, err := m.PutLogEventsWithResult(ctx, groupName, streamName, events) - var totalBytes int64 - for _, e := range events { - totalBytes += int64(len(e.Message)) - } - - // The stream lock only guards the stream's own events/metadata. It is - // released before cross-service delivery (metric filters, subscription - // filters) so a delivered handler that writes back into CloudWatch Logs โ€” - // re-entering PutLogEvents โ€” cannot deadlock on this same lock. - s.mu.Lock() - - s.events = append(s.events, copied...) - - // CloudWatch Logs returns events in timestamp order regardless of the order - // they were ingested, so keep the stream's backing slice sorted on insert. - // The sort is stable, so equal timestamps retain ingestion order. - sortLogEventsAscending(s.events) - - // events is already validated non-empty and chronologically ascending (see - // validatePutLogEventsBatch above), so events[0] is this batch's earliest - // timestamp and events[len(events)-1] its latest. - if s.info.FirstEvent == "" { - s.info.FirstEvent = events[0].Timestamp.UTC().Format(time.RFC3339) - } - - s.info.LastEvent = events[len(events)-1].Timestamp.UTC().Format(time.RFC3339) - - s.mu.Unlock() - - m.groups.Update(groupName, func(lg *logGroup) *logGroup { - lg.info.StoredBytes += totalBytes - return lg - }) - - dims := map[string]string{"LogGroupName": groupName} - m.emitMetric("IncomingLogEvents", float64(len(events)), "Count", dims) - m.emitMetric("IncomingBytes", float64(totalBytes), "Bytes", dims) - - m.evaluateMetricFilters(g, events) - m.deliverToSubscriptions(ctx, g, streamName, copied) - - return nil -} - -// validatePutLogEventsBatch enforces the two structural constraints real -// CloudWatch Logs applies to a PutLogEvents batch before ingesting anything: -// the events must be in chronological (non-decreasing) timestamp order, and -// the oldest and newest event in the batch must not be more than 24 hours -// apart. Both are hard failures โ€” the whole batch is rejected and nothing is -// written โ€” unlike the per-event too-old/too-new rejection real CloudWatch -// Logs also applies (surfaced via PutLogEventsOutput.RejectedLogEventsInfo), -// which this emulator does not model: that check is relative to wall-clock -// time, and event timestamps here are caller-supplied and not tied to the -// mock's clock, so silently dropping "future" events would reject events -// legitimate callers routinely backdate or postdate in tests. -// -// It also applies the batch size limits: at most 10,000 events, each message -// at least 1 character, and at most 1,048,576 bytes in total, where each event -// counts as its UTF-8 message bytes plus 26. -func validatePutLogEventsBatch(events []driver.LogEvent) error { - if err := validatePutLogEventsSize(events); err != nil { - return err - } - - for i := 1; i < len(events); i++ { - if events[i].Timestamp.Before(events[i-1].Timestamp) { - return errors.New(errors.InvalidArgument, - "log events in a single PutLogEvents request must be in chronological order") - } - } - - if events[len(events)-1].Timestamp.Sub(events[0].Timestamp) > putLogEventsMaxBatchSpan { - return errors.New(errors.InvalidArgument, - "the log events in a single PutLogEvents request cannot span more than 24 hours") - } - - return nil + return err } // evaluateMetricFilters turns matching log events into CloudWatch metric data. @@ -413,18 +289,13 @@ func (m *Mock) GetLogEvents(_ context.Context, input *driver.LogQueryInput) ([]d return nil, errors.Newf(errors.NotFound, "log group %q not found", input.LogGroup) } + m.purgeExpired(input.LogGroup, g) + limit := input.Limit if limit == 0 { limit = defaultLogLimit } - // Only a configured retention policy drops old events. A group with no - // retention (RetentionDays == 0) never expires, so the cutoff stays zero. - var retentionCutoff time.Time - if g.info.RetentionDays > 0 { - retentionCutoff = m.opts.Clock.Now().AddDate(0, 0, -g.info.RetentionDays) - } - var results []driver.LogEvent if input.LogStream != "" { @@ -433,10 +304,10 @@ func (m *Mock) GetLogEvents(_ context.Context, input *driver.LogQueryInput) ([]d return nil, errors.Newf(errors.NotFound, "log stream %q not found in group %q", input.LogStream, input.LogGroup) } - results = append(results, m.filterEvents(s, input, retentionCutoff)...) + results = append(results, m.filterEvents(s, input)...) } else { for _, s := range g.streams.SortedValues() { - events := m.filterEvents(s, input, retentionCutoff) + events := m.filterEvents(s, input) results = append(results, events...) } @@ -468,7 +339,6 @@ func sortLogEventsAscending(events []driver.LogEvent) { func (*Mock) filterEvents( s *logStream, input *driver.LogQueryInput, - retentionCutoff time.Time, ) []driver.LogEvent { s.mu.RLock() defer s.mu.RUnlock() @@ -476,10 +346,6 @@ func (*Mock) filterEvents( var results []driver.LogEvent for _, e := range s.events { - if !retentionCutoff.IsZero() && e.Timestamp.Before(retentionCutoff) { - continue - } - if !input.StartTime.IsZero() && e.Timestamp.Before(input.StartTime) { continue } @@ -510,6 +376,8 @@ func (m *Mock) FilterLogEvents( ) } + m.purgeExpired(input.LogGroup, g) + limit := input.Limit if limit == 0 { limit = defaultLogLimit diff --git a/providers/aws/cloudwatchlogs/cloudwatchlogs_test.go b/providers/aws/cloudwatchlogs/cloudwatchlogs_test.go index c1dd3e5c4..8d8b35d1e 100644 --- a/providers/aws/cloudwatchlogs/cloudwatchlogs_test.go +++ b/providers/aws/cloudwatchlogs/cloudwatchlogs_test.go @@ -13,8 +13,20 @@ import ( "github.com/stretchr/testify/require" ) +// testClockStart is the FakeClock start every provider test mock uses. +func testClockStart() time.Time { + return time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC) +} + +// testEventTime is a base timestamp for test events: three hours before the +// mock clock, so offsets of up to a few hours stay inside the PutLogEvents +// ingest window (no more than 14 days old, no more than 2 hours ahead). +func testEventTime() time.Time { + return testClockStart().Add(-3 * time.Hour) +} + func newTestMock() *Mock { - fc := config.NewFakeClock(time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)) + fc := config.NewFakeClock(testClockStart()) opts := config.NewOptions(config.WithClock(fc), config.WithRegion("us-east-1")) return New(opts) @@ -287,7 +299,7 @@ func TestListLogStreams(t *testing.T) { func TestPutLogEvents(t *testing.T) { ctx := context.Background() - baseTime := time.Date(2025, 6, 1, 12, 0, 0, 0, time.UTC) + baseTime := testEventTime() t.Run("success", func(t *testing.T) { m := newTestMock() @@ -386,8 +398,8 @@ func TestPutLogEvents(t *testing.T) { setupGroupAndStream(t, m) err := m.PutLogEvents(ctx, "test-group", "test-stream", []driver.LogEvent{ - {Timestamp: baseTime, Message: "early"}, - {Timestamp: baseTime.Add(25 * time.Hour), Message: "too late"}, + {Timestamp: baseTime.Add(-25 * time.Hour), Message: "early"}, + {Timestamp: baseTime, Message: "too late"}, }) require.Error(t, err) assert.True(t, errors.IsInvalidArgument(err)) @@ -398,8 +410,8 @@ func TestPutLogEvents(t *testing.T) { setupGroupAndStream(t, m) err := m.PutLogEvents(ctx, "test-group", "test-stream", []driver.LogEvent{ - {Timestamp: baseTime, Message: "early"}, - {Timestamp: baseTime.Add(24 * time.Hour), Message: "late"}, + {Timestamp: baseTime.Add(-24 * time.Hour), Message: "early"}, + {Timestamp: baseTime, Message: "late"}, }) require.NoError(t, err) }) @@ -418,7 +430,7 @@ func TestPutLogEvents(t *testing.T) { func TestGetLogEvents(t *testing.T) { ctx := context.Background() - baseTime := time.Date(2025, 6, 1, 12, 0, 0, 0, time.UTC) + baseTime := testEventTime() t.Run("all events from all streams", func(t *testing.T) { m := newTestMock() @@ -603,7 +615,7 @@ func TestGetLogEvents(t *testing.T) { func TestFilterLogEvents(t *testing.T) { ctx := context.Background() - baseTime := time.Date(2025, 6, 1, 12, 0, 0, 0, time.UTC) + baseTime := testEventTime() t.Run("filter by pattern", func(t *testing.T) { m := newTestMock() diff --git a/providers/aws/cloudwatchlogs/event_retrieval_test.go b/providers/aws/cloudwatchlogs/event_retrieval_test.go index 076603879..085405532 100644 --- a/providers/aws/cloudwatchlogs/event_retrieval_test.go +++ b/providers/aws/cloudwatchlogs/event_retrieval_test.go @@ -5,6 +5,7 @@ import ( "testing" "time" + "github.com/stackshy/cloudemu/v2/config" "github.com/stackshy/cloudemu/v2/services/logging/driver" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -22,7 +23,7 @@ func TestGetLogEventsTimestampOrder(t *testing.T) { _, err = m.CreateLogStream(ctx, "g", "s") require.NoError(t, err) - base := time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC) + base := testClockStart() require.NoError(t, m.PutLogEvents(ctx, "g", "s", []driver.LogEvent{ {Timestamp: base.Add(5000 * time.Millisecond), Message: "late"}, @@ -52,14 +53,25 @@ func TestRetentionUnsetNeverDrops(t *testing.T) { _, err = m.CreateLogStream(ctx, "g", "s") require.NoError(t, err) - // Clock is 2025-01-01; this event is ~1 year old. - old := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC) + // Ingest at the clock's current time, then let a year pass: with no + // retention policy the event must still be there. require.NoError(t, m.PutLogEvents(ctx, "g", "s", []driver.LogEvent{ - {Timestamp: old, Message: "ancient"}, + {Timestamp: m.opts.Clock.Now(), Message: "ancient"}, })) + advanceClock(t, m, 365*24*time.Hour) + got, err := m.GetLogEvents(ctx, &driver.LogQueryInput{LogGroup: "g", LogStream: "s"}) require.NoError(t, err) require.Len(t, got, 1) assert.Equal(t, "ancient", got[0].Message) } + +// advanceClock moves the test mock's FakeClock forward. +func advanceClock(t *testing.T, m *Mock, d time.Duration) { + t.Helper() + + fc, ok := m.opts.Clock.(*config.FakeClock) + require.True(t, ok, "test mock must use a FakeClock") + fc.Advance(d) +} diff --git a/providers/aws/cloudwatchlogs/ingest.go b/providers/aws/cloudwatchlogs/ingest.go new file mode 100644 index 000000000..942a4381e --- /dev/null +++ b/providers/aws/cloudwatchlogs/ingest.go @@ -0,0 +1,213 @@ +package cloudwatchlogs + +import ( + "context" + "fmt" + "time" + + "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/services/logging/driver" +) + +// PutLogEvents ingest window and batch limits, per the API reference. +const ( + // putLogEventsMaxAge: events older than 14 days are rejected. + putLogEventsMaxAge = 14 * 24 * time.Hour + // putLogEventsMaxFuture: events more than 2 hours in the future are rejected. + putLogEventsMaxFuture = 2 * time.Hour + // putLogEventsMaxBatchSpan: the accepted events of one batch cannot span + // more than 24 hours, otherwise the whole call fails. + putLogEventsMaxBatchSpan = 24 * time.Hour +) + +// sequenceTokenPrefix gives nextSequenceToken the 56-digit shape AWS returns; +// the per-stream upload count fills the low 16 digits. +const sequenceTokenPrefix = "4953670125153982633102568327403296938495" + +// PutLogEventsWithResult writes a batch to a stream the way CloudWatch Logs +// does. A batch that is out of order, too large, or whose accepted events span +// more than 24 hours fails as a whole. Otherwise events older than 14 days or +// older than the group's retention, and events more than 2 hours in the +// future, are dropped and reported in Rejected while the rest are stored. +func (m *Mock) PutLogEventsWithResult( + ctx context.Context, groupName, streamName string, events []driver.LogEvent, +) (*driver.PutLogEventsResult, error) { + g, ok := m.groups.Get(groupName) + if !ok { + return nil, errors.Newf(errors.NotFound, "log group %q not found", groupName) + } + + s, ok := g.streams.Get(streamName) + if !ok { + return nil, errors.Newf(errors.NotFound, "log stream %q not found in group %q", streamName, groupName) + } + + if len(events) == 0 { + return nil, errors.New(errors.InvalidArgument, "logEvents cannot be empty") + } + + if err := validatePutLogEventsBatch(events); err != nil { + return nil, err + } + + m.purgeExpired(groupName, g) + + accepted, rejected := m.ingestWindow(events, g.info.RetentionDays) + + if len(accepted) > 0 && + accepted[len(accepted)-1].Timestamp.Sub(accepted[0].Timestamp) > putLogEventsMaxBatchSpan { + return nil, errors.New(errors.InvalidArgument, + "the log events in a single PutLogEvents request cannot span more than 24 hours") + } + + token := m.storeEvents(ctx, g, s, streamName, accepted) + + return &driver.PutLogEventsResult{NextSequenceToken: token, Rejected: rejected}, nil +} + +// validatePutLogEventsBatch applies the checks that fail a whole batch before +// any event is stored: the size limits and chronological order. +func validatePutLogEventsBatch(events []driver.LogEvent) error { + if err := validatePutLogEventsSize(events); err != nil { + return err + } + + for i := 1; i < len(events); i++ { + if events[i].Timestamp.Before(events[i-1].Timestamp) { + return errors.New(errors.InvalidArgument, + "log events in a single PutLogEvents request must be in chronological order") + } + } + + return nil +} + +// ingestWindow splits a chronological batch into the events CloudWatch Logs +// keeps and a report of the ones it drops. Rejected events are always a prefix +// (too old or past retention) and a suffix (too far in the future), so the +// kept events are the slice between them. rejected is nil when nothing was +// dropped. +func (m *Mock) ingestWindow(events []driver.LogEvent, retentionDays int) ([]driver.LogEvent, *driver.RejectedLogEventsInfo) { + now := m.opts.Clock.Now() + + tooOld := countBefore(events, now.Add(-putLogEventsMaxAge)) + expired := countBefore(events, m.retentionCutoff(retentionDays)) + tooNew := firstAfter(events, now.Add(putLogEventsMaxFuture)) + + start := max(tooOld, expired) + end := max(tooNew, start) + + if start == 0 && end == len(events) { + return events, nil + } + + info := &driver.RejectedLogEventsInfo{} + + if tooOld > 0 { + info.TooOldLogEventEndIndex = &tooOld + } + + if expired > 0 { + info.ExpiredLogEventEndIndex = &expired + } + + if tooNew < len(events) { + info.TooNewLogEventStartIndex = &tooNew + } + + return events[start:end], info +} + +// countBefore returns how many leading events of a chronological batch are +// older than cutoff. A zero cutoff matches nothing. +func countBefore(events []driver.LogEvent, cutoff time.Time) int { + if cutoff.IsZero() { + return 0 + } + + n := 0 + for n < len(events) && events[n].Timestamp.Before(cutoff) { + n++ + } + + return n +} + +// firstAfter returns the index of the first event of the trailing run newer +// than cutoff, or len(events) when there is none. +func firstAfter(events []driver.LogEvent, cutoff time.Time) int { + i := len(events) + for i > 0 && events[i-1].Timestamp.After(cutoff) { + i-- + } + + return i +} + +// storeEvents appends accepted events to the stream, updates the group's +// counters, and runs metric filters and subscriptions. It returns the +// stream's next sequence token. Every upload, even one whose events were all +// rejected, advances the token. +func (m *Mock) storeEvents( + ctx context.Context, g *logGroup, s *logStream, streamName string, events []driver.LogEvent, +) string { + ingestedAt := m.opts.Clock.Now().UTC() + + copied := make([]driver.LogEvent, len(events)) + + var totalBytes int64 + + for i := range events { + copied[i] = events[i] + copied[i].IngestionTime = ingestedAt + totalBytes += int64(len(events[i].Message)) + } + + // The stream lock only guards the stream's own events/metadata. It is + // released before cross-service delivery (metric filters, subscription + // filters) so a delivered handler that writes back into CloudWatch Logs, + // re-entering PutLogEvents, cannot deadlock on this same lock. + s.mu.Lock() + + s.uploads++ + token := fmt.Sprintf("%s%016d", sequenceTokenPrefix, s.uploads) + + if len(copied) > 0 { + s.events = append(s.events, copied...) + + // CloudWatch Logs returns events in timestamp order regardless of the + // order they were ingested, so keep the backing slice sorted on + // insert. The sort is stable, so equal timestamps keep ingestion order. + sortLogEventsAscending(s.events) + + // events is chronological, so events[0] is this batch's earliest + // timestamp and the last element its latest. + if s.info.FirstEvent == "" { + s.info.FirstEvent = events[0].Timestamp.UTC().Format(time.RFC3339) + } + + s.info.LastEvent = events[len(events)-1].Timestamp.UTC().Format(time.RFC3339) + } + + s.mu.Unlock() + + if len(copied) == 0 { + return token + } + + groupName := g.info.Name + + m.groups.Update(groupName, func(lg *logGroup) *logGroup { + lg.info.StoredBytes += totalBytes + return lg + }) + + dims := map[string]string{"LogGroupName": groupName} + m.emitMetric("IncomingLogEvents", float64(len(events)), "Count", dims) + m.emitMetric("IncomingBytes", float64(totalBytes), "Bytes", dims) + + m.evaluateMetricFilters(g, events) + m.deliverToSubscriptions(ctx, g, streamName, copied) + + return token +} diff --git a/providers/aws/cloudwatchlogs/ingest_window_test.go b/providers/aws/cloudwatchlogs/ingest_window_test.go new file mode 100644 index 000000000..d5efff73c --- /dev/null +++ b/providers/aws/cloudwatchlogs/ingest_window_test.go @@ -0,0 +1,196 @@ +package cloudwatchlogs + +import ( + "context" + "strings" + "testing" + "time" + + "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/services/logging/driver" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func newWindowMock(t *testing.T, retentionDays int) *Mock { + t.Helper() + + m := newTestMock() + ctx := context.Background() + + _, err := m.CreateLogGroup(ctx, driver.LogGroupConfig{Name: "g"}) + require.NoError(t, err) + _, err = m.CreateLogStream(ctx, "g", "s") + require.NoError(t, err) + + if retentionDays > 0 { + require.NoError(t, m.PutRetentionPolicy(ctx, "g", retentionDays)) + } + + return m +} + +func intPtr(v int) *int { return &v } + +func storedMessages(t *testing.T, m *Mock) []string { + t.Helper() + + got, err := m.GetLogEvents(context.Background(), &driver.LogQueryInput{LogGroup: "g", LogStream: "s"}) + require.NoError(t, err) + + out := make([]string, 0, len(got)) + for _, e := range got { + out = append(out, e.Message) + } + + return out +} + +func TestPutLogEventsIngestWindow(t *testing.T) { + now := testClockStart() + + tests := []struct { + name string + retention int + events []driver.LogEvent + wantKept []string + wantRej *driver.RejectedLogEventsInfo + }{ + { + name: "all inside the window", + events: []driver.LogEvent{{Timestamp: now.Add(-time.Hour), Message: "a"}, {Timestamp: now, Message: "b"}}, + wantKept: []string{"a", "b"}, + }, + { + name: "too old prefix dropped", + events: []driver.LogEvent{ + {Timestamp: now.Add(-15 * 24 * time.Hour), Message: "old1"}, + {Timestamp: now.Add(-15*24*time.Hour + time.Minute), Message: "old2"}, + {Timestamp: now, Message: "ok"}, + }, + wantKept: []string{"ok"}, + wantRej: &driver.RejectedLogEventsInfo{TooOldLogEventEndIndex: intPtr(2)}, + }, + { + name: "too new suffix dropped", + events: []driver.LogEvent{ + {Timestamp: now, Message: "ok"}, + {Timestamp: now.Add(2*time.Hour + time.Second), Message: "future"}, + }, + wantKept: []string{"ok"}, + wantRej: &driver.RejectedLogEventsInfo{TooNewLogEventStartIndex: intPtr(1)}, + }, + { + name: "exactly two hours ahead is accepted", + events: []driver.LogEvent{{Timestamp: now.Add(2 * time.Hour), Message: "edge"}}, + wantKept: []string{"edge"}, + }, + { + name: "older than retention dropped as expired", + retention: 1, + events: []driver.LogEvent{ + {Timestamp: now.Add(-2 * 24 * time.Hour), Message: "expired"}, + {Timestamp: now, Message: "ok"}, + }, + wantKept: []string{"ok"}, + wantRej: &driver.RejectedLogEventsInfo{ExpiredLogEventEndIndex: intPtr(1)}, + }, + { + name: "old, expired and future together", + retention: 3, + events: []driver.LogEvent{ + {Timestamp: now.Add(-20 * 24 * time.Hour), Message: "old"}, + {Timestamp: now.Add(-5 * 24 * time.Hour), Message: "expired"}, + {Timestamp: now, Message: "ok"}, + {Timestamp: now.Add(3 * time.Hour), Message: "future"}, + }, + wantKept: []string{"ok"}, + wantRej: &driver.RejectedLogEventsInfo{ + TooOldLogEventEndIndex: intPtr(1), + ExpiredLogEventEndIndex: intPtr(2), + TooNewLogEventStartIndex: intPtr(3), + }, + }, + { + name: "every event rejected still succeeds", + events: []driver.LogEvent{{Timestamp: now.Add(5 * time.Hour), Message: "future"}}, + wantKept: []string{}, + wantRej: &driver.RejectedLogEventsInfo{TooNewLogEventStartIndex: intPtr(0)}, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + m := newWindowMock(t, tc.retention) + + res, err := m.PutLogEventsWithResult(context.Background(), "g", "s", tc.events) + require.NoError(t, err) + assert.Equal(t, tc.wantRej, res.Rejected) + assert.Equal(t, tc.wantKept, storedMessages(t, m)) + }) + } +} + +func TestPutLogEventsSpanAppliesToAcceptedEventsOnly(t *testing.T) { + now := testClockStart() + m := newWindowMock(t, 0) + + // 20 days old (dropped) to now would span far more than 24h, but only the + // accepted event counts, so the batch succeeds. + res, err := m.PutLogEventsWithResult(context.Background(), "g", "s", []driver.LogEvent{ + {Timestamp: now.Add(-20 * 24 * time.Hour), Message: "old"}, + {Timestamp: now, Message: "ok"}, + }) + require.NoError(t, err) + assert.Equal(t, intPtr(1), res.Rejected.TooOldLogEventEndIndex) + + // Two accepted events 25h apart still fail the whole batch. + _, err = m.PutLogEventsWithResult(context.Background(), "g", "s", []driver.LogEvent{ + {Timestamp: now.Add(-25 * time.Hour), Message: "a"}, + {Timestamp: now, Message: "b"}, + }) + require.Error(t, err) + assert.True(t, errors.IsInvalidArgument(err)) + assert.Equal(t, []string{"ok"}, storedMessages(t, m)) +} + +func TestPutLogEventsRejectedEventsAreNotCounted(t *testing.T) { + now := testClockStart() + m := newWindowMock(t, 0) + + _, err := m.PutLogEventsWithResult(context.Background(), "g", "s", []driver.LogEvent{ + {Timestamp: now, Message: "12345"}, + {Timestamp: now.Add(3 * time.Hour), Message: "future-bytes"}, + }) + require.NoError(t, err) + + g, err := m.GetLogGroup(context.Background(), "g") + require.NoError(t, err) + assert.Equal(t, int64(5), g.StoredBytes) +} + +func TestPutLogEventsSequenceToken(t *testing.T) { + ctx := context.Background() + m := newWindowMock(t, 0) + ev := []driver.LogEvent{{Timestamp: testClockStart(), Message: "m"}} + + first, err := m.PutLogEventsWithResult(ctx, "g", "s", ev) + require.NoError(t, err) + second, err := m.PutLogEventsWithResult(ctx, "g", "s", ev) + require.NoError(t, err) + + assert.Len(t, first.NextSequenceToken, 56) + assert.Equal(t, -1, strings.IndexFunc(first.NextSequenceToken, func(r rune) bool { return r < '0' || r > '9' })) + assert.NotEqual(t, first.NextSequenceToken, second.NextSequenceToken) + + // The upload count survives a snapshot, so tokens keep moving forward. + raw, err := m.Snapshot(ctx, true) + require.NoError(t, err) + + restored := newTestMock() + require.NoError(t, restored.Restore(ctx, raw)) + + third, err := restored.PutLogEventsWithResult(ctx, "g", "s", ev) + require.NoError(t, err) + assert.Greater(t, third.NextSequenceToken, second.NextSequenceToken) +} diff --git a/providers/aws/cloudwatchlogs/kms.go b/providers/aws/cloudwatchlogs/kms.go new file mode 100644 index 000000000..37fc964ef --- /dev/null +++ b/providers/aws/cloudwatchlogs/kms.go @@ -0,0 +1,138 @@ +package cloudwatchlogs + +import ( + "context" + "strings" + + "github.com/stackshy/cloudemu/v2/errors" + kmsdriver "github.com/stackshy/cloudemu/v2/services/kms/driver" +) + +// KeyResolver looks up a KMS key by ID, ARN, or alias. The KMS mock satisfies +// it, so a key named on CreateLogGroup or AssociateKmsKey must really exist. +type KeyResolver interface { + DescribeKey(ctx context.Context, keyID string) (*kmsdriver.KeyMetadata, error) +} + +// SetKeyResolver wires KMS so log group keys are checked against real keys. +// Without it any well-formed key ARN is accepted. +func (m *Mock) SetKeyResolver(r KeyResolver) { + m.keys = r +} + +const ( + logGroupARNMarker = ":log-group:" + queryResultARNPart = ":query-result:" + queryResultWildcard = "*" +) + +// validateKmsKey applies the CloudWatch Logs key rules: the key must be a KMS +// key ARN and, when KMS is wired, an existing enabled key. Any failure is an +// InvalidParameterException, which is what AWS returns for a missing or +// disabled key. +func (m *Mock) validateKmsKey(ctx context.Context, keyID, targetARN string) error { + bad := errors.Newf(errors.InvalidArgument, + "Specified KMS key %s does not exist or is not allowed to be used with Arn '%s'", keyID, targetARN) + + if !strings.HasPrefix(keyID, "arn:") || !strings.Contains(keyID, ":kms:") { + return bad + } + + if m.keys == nil { + return nil + } + + meta, err := m.keys.DescribeKey(ctx, keyID) + if err != nil || meta == nil || !meta.Enabled || meta.KeyState != kmsdriver.StateEnabled { + return bad + } + + return nil +} + +// AssociateKmsKey points a log group, or the account's Logs Insights query +// results, at a KMS key. +func (m *Mock) AssociateKmsKey(ctx context.Context, logGroup, resourceIdentifier, kmsKeyID string) error { + if kmsKeyID == "" { + return errors.New(errors.InvalidArgument, "kmsKeyId is required") + } + + target, err := m.resolveKmsTarget(logGroup, resourceIdentifier) + if err != nil { + return err + } + + if err := m.validateKmsKey(ctx, kmsKeyID, target.arn); err != nil { + return err + } + + return m.setKmsKey(target, kmsKeyID) +} + +// DisassociateKmsKey removes the key from a log group or from query results. +// Disassociating a target that has no key is not an error. +func (m *Mock) DisassociateKmsKey(_ context.Context, logGroup, resourceIdentifier string) error { + target, err := m.resolveKmsTarget(logGroup, resourceIdentifier) + if err != nil { + return err + } + + return m.setKmsKey(target, "") +} + +// kmsTarget is either a log group (group set) or the query-result resource. +type kmsTarget struct { + group string + arn string +} + +func (m *Mock) resolveKmsTarget(logGroup, resourceIdentifier string) (kmsTarget, error) { + if (logGroup == "") == (resourceIdentifier == "") { + return kmsTarget{}, errors.New(errors.InvalidArgument, + "Exactly one of logGroupName or resourceIdentifier must be specified") + } + + if logGroup == "" { + switch { + case strings.Contains(resourceIdentifier, queryResultARNPart): + if !strings.HasSuffix(resourceIdentifier, queryResultARNPart+queryResultWildcard) { + return kmsTarget{}, errors.Newf(errors.InvalidArgument, + "Invalid query-result resource identifier %q", resourceIdentifier) + } + + return kmsTarget{arn: resourceIdentifier}, nil + case strings.Contains(resourceIdentifier, logGroupARNMarker): + i := strings.LastIndex(resourceIdentifier, logGroupARNMarker) + logGroup = strings.TrimSuffix(resourceIdentifier[i+len(logGroupARNMarker):], ":*") + default: + return kmsTarget{}, errors.Newf(errors.InvalidArgument, + "Invalid resource identifier %q", resourceIdentifier) + } + } + + g, ok := m.groups.Get(logGroup) + if !ok { + return kmsTarget{}, errors.Newf(errors.NotFound, "log group %q not found", logGroup) + } + + return kmsTarget{group: logGroup, arn: g.info.ResourceID}, nil +} + +func (m *Mock) setKmsKey(t kmsTarget, keyID string) error { + if t.group == "" { + m.mu.Lock() + m.queryResultKmsKey = keyID + m.mu.Unlock() + + return nil + } + + if !m.groups.Update(t.group, func(g *logGroup) *logGroup { + g.kmsKeyID = keyID + return g + }) { + return errors.Newf(errors.NotFound, "log group %q not found", t.group) + } + + return nil +} diff --git a/providers/aws/cloudwatchlogs/log_groups.go b/providers/aws/cloudwatchlogs/log_groups.go new file mode 100644 index 000000000..8a570cff1 --- /dev/null +++ b/providers/aws/cloudwatchlogs/log_groups.go @@ -0,0 +1,177 @@ +package cloudwatchlogs + +import ( + "context" + "maps" + "regexp" + "slices" + "strings" + "time" + + "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/internal/idgen" + "github.com/stackshy/cloudemu/v2/internal/memstore" + "github.com/stackshy/cloudemu/v2/internal/regionctx" + "github.com/stackshy/cloudemu/v2/services/logging/driver" +) + +// logGroupNamePatternRE is the ListLogGroups logGroupNamePattern constraint: +// up to five 3-24 character alternatives separated by '|', each optionally +// anchored with '^'. +var logGroupNamePatternRE = regexp.MustCompile( + `^(\^?[\.\-_/#A-Za-z0-9]{3,24})(\|\^?[\.\-_/#A-Za-z0-9]{3,24}){0,4}$`) + +// CreateAWSLogGroup creates a log group with the CloudWatch Logs-only +// CreateLogGroup fields. The class defaults to STANDARD and cannot change +// later; a KMS key must be an enabled key ARN. +func (m *Mock) CreateAWSLogGroup( + ctx context.Context, cfg *driver.LogGroupConfig, opts driver.AWSLogGroupOptions, +) (*driver.AWSLogGroup, error) { + if err := validateLogGroupName(cfg.Name); err != nil { + return nil, err + } + + class, err := resolveLogGroupClass(opts.LogGroupClass) + if err != nil { + return nil, err + } + + if m.groups.Has(cfg.Name) { + return nil, errors.Newf(errors.AlreadyExists, "log group %q already exists", cfg.Name) + } + + arn := idgen.AWSARN("logs", regionctx.RegionOr(ctx, m.opts.Region), m.opts.AccountID, "log-group:"+cfg.Name) + + if opts.KmsKeyID != "" { + if err := m.validateKmsKey(ctx, opts.KmsKeyID, arn); err != nil { + return nil, err + } + } + + // A log group created without a retention policy never expires: AWS leaves + // retentionInDays unset rather than defaulting to 30 days. + info := driver.LogGroupInfo{ + Name: cfg.Name, + Scope: cfg.Scope, + ResourceID: arn, + RetentionDays: cfg.RetentionDays, + CreatedAt: m.opts.Clock.Now().UTC().Format(time.RFC3339), + Tags: maps.Clone(cfg.Tags), + } + + if info.Tags == nil { + info.Tags = map[string]string{} + } + + g := &logGroup{ + info: info, + kmsKeyID: opts.KmsKeyID, + class: class, + streams: memstore.New[*logStream](), + metricFilters: memstore.New[*driver.MetricFilterInfo](), + subFilters: memstore.New[*driver.SubscriptionFilterInfo](), + } + + if !m.groups.SetIfAbsent(cfg.Name, g) { + return nil, errors.Newf(errors.AlreadyExists, "log group %q already exists", cfg.Name) + } + + out := g.awsView() + + return &out, nil +} + +func resolveLogGroupClass(class string) (string, error) { + switch class { + case "": + return driver.LogGroupClassStandard, nil + case driver.LogGroupClassStandard, driver.LogGroupClassInfrequentAccess, driver.LogGroupClassDelivery: + return class, nil + default: + return "", errors.Newf(errors.InvalidArgument, + "1 validation error detected: Value '%s' at 'logGroupClass' failed to satisfy constraint: "+ + "Member must satisfy enum value set: [STANDARD, INFREQUENT_ACCESS, DELIVERY]", class) + } +} + +// awsView copies the group into its CloudWatch Logs-shaped form. The tag map +// is cloned so callers cannot mutate stored state. +func (g *logGroup) awsView() driver.AWSLogGroup { + info := g.info + info.Tags = maps.Clone(g.info.Tags) + + class := g.class + if class == "" { + class = driver.LogGroupClassStandard + } + + return driver.AWSLogGroup{LogGroupInfo: info, KmsKeyID: g.kmsKeyID, LogGroupClass: class} +} + +// ListAWSLogGroups returns every log group matching filter, ordered by name. +// It backs both DescribeLogGroups (empty filter) and ListLogGroups. +func (m *Mock) ListAWSLogGroups(_ context.Context, filter driver.ListLogGroupsFilter) ([]driver.AWSLogGroup, error) { + if filter.NamePattern != "" && !logGroupNamePatternRE.MatchString(filter.NamePattern) { + return nil, errors.New(errors.InvalidArgument, + "1 validation error detected: Value at 'logGroupNamePattern' failed to satisfy constraint: "+ + `Member must satisfy regular expression pattern: (\^?[\.\-_\/#A-Za-z0-9]{3,24})(\|\^?[\.\-_\/#A-Za-z0-9]{3,24}){0,4}`) + } + + if filter.LogGroupClass != "" { + if _, err := resolveLogGroupClass(filter.LogGroupClass); err != nil { + return nil, err + } + } + + all := m.groups.SortedValues() + out := make([]driver.AWSLogGroup, 0, len(all)) + + for _, g := range all { + v := g.awsView() + if matchesLogGroupFilter(&v, filter) { + out = append(out, v) + } + } + + return out, nil +} + +func matchesLogGroupFilter(g *driver.AWSLogGroup, f driver.ListLogGroupsFilter) bool { + if f.LogGroupClass != "" && g.LogGroupClass != f.LogGroupClass { + return false + } + + if f.NamePattern != "" && !matchesNamePattern(g.Name, f.NamePattern) { + return false + } + + for _, tf := range f.Tags { + v, ok := g.Tags[tf.Key] + if !ok || (len(tf.Values) > 0 && !slices.Contains(tf.Values, v)) { + return false + } + } + + return true +} + +// matchesNamePattern applies a logGroupNamePattern: any alternative matches, +// '^' anchors it to the start of the name, otherwise it is a case-sensitive +// substring. +func matchesNamePattern(name, pattern string) bool { + for _, alt := range strings.Split(pattern, "|") { + if prefix, anchored := strings.CutPrefix(alt, "^"); anchored { + if strings.HasPrefix(name, prefix) { + return true + } + + continue + } + + if strings.Contains(name, alt) { + return true + } + } + + return false +} diff --git a/providers/aws/cloudwatchlogs/log_groups_test.go b/providers/aws/cloudwatchlogs/log_groups_test.go new file mode 100644 index 000000000..51c997f18 --- /dev/null +++ b/providers/aws/cloudwatchlogs/log_groups_test.go @@ -0,0 +1,197 @@ +package cloudwatchlogs + +import ( + "context" + stderrors "errors" + "testing" + + "github.com/stackshy/cloudemu/v2/errors" + kmsdriver "github.com/stackshy/cloudemu/v2/services/kms/driver" + "github.com/stackshy/cloudemu/v2/services/logging/driver" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const testKeyARN = "arn:aws:kms:us-east-1:123456789012:key/1234abcd-12ab-34cd-56ef-1234567890ab" + +// fakeKeys is a KeyResolver with a fixed set of keys. +type fakeKeys map[string]kmsdriver.KeyMetadata + +func (f fakeKeys) DescribeKey(_ context.Context, id string) (*kmsdriver.KeyMetadata, error) { + k, ok := f[id] + if !ok { + return nil, stderrors.New("not found") + } + + return &k, nil +} + +func TestCreateAWSLogGroupClassAndKey(t *testing.T) { + ctx := context.Background() + m := newTestMock() + + g, err := m.CreateAWSLogGroup(ctx, &driver.LogGroupConfig{Name: "plain"}, driver.AWSLogGroupOptions{}) + require.NoError(t, err) + assert.Equal(t, driver.LogGroupClassStandard, g.LogGroupClass) + assert.Empty(t, g.KmsKeyID) + + g, err = m.CreateAWSLogGroup(ctx, &driver.LogGroupConfig{Name: "ia"}, driver.AWSLogGroupOptions{ + LogGroupClass: driver.LogGroupClassInfrequentAccess, KmsKeyID: testKeyARN, + }) + require.NoError(t, err) + assert.Equal(t, driver.LogGroupClassInfrequentAccess, g.LogGroupClass) + assert.Equal(t, testKeyARN, g.KmsKeyID) + + _, err = m.CreateAWSLogGroup(ctx, &driver.LogGroupConfig{Name: "bad-class"}, driver.AWSLogGroupOptions{LogGroupClass: "COLD"}) + require.Error(t, err) + assert.True(t, errors.IsInvalidArgument(err)) + + _, err = m.CreateAWSLogGroup(ctx, &driver.LogGroupConfig{Name: "bad-key"}, driver.AWSLogGroupOptions{KmsKeyID: "not-an-arn"}) + require.Error(t, err) + assert.True(t, errors.IsInvalidArgument(err)) + assert.False(t, m.groups.Has("bad-key"), "a rejected create must not leave a group behind") +} + +func TestKmsKeyMustExistAndBeEnabled(t *testing.T) { + ctx := context.Background() + m := newTestMock() + + disabled := "arn:aws:kms:us-east-1:123456789012:key/disabled" + m.SetKeyResolver(fakeKeys{ + testKeyARN: {ARN: testKeyARN, Enabled: true, KeyState: kmsdriver.StateEnabled}, + disabled: {ARN: disabled, Enabled: false, KeyState: kmsdriver.StateDisabled}, + }) + + _, err := m.CreateAWSLogGroup(ctx, &driver.LogGroupConfig{Name: "g"}, driver.AWSLogGroupOptions{KmsKeyID: testKeyARN}) + require.NoError(t, err) + + for _, key := range []string{disabled, "arn:aws:kms:us-east-1:123456789012:key/missing"} { + err = m.AssociateKmsKey(ctx, "g", "", key) + require.Error(t, err, key) + assert.True(t, errors.IsInvalidArgument(err), key) + } +} + +func TestAssociateAndDisassociateKmsKey(t *testing.T) { + ctx := context.Background() + m := newTestMock() + + _, err := m.CreateLogGroup(ctx, driver.LogGroupConfig{Name: "g"}) + require.NoError(t, err) + + keyOf := func() string { + gs, lerr := m.ListAWSLogGroups(ctx, driver.ListLogGroupsFilter{}) + require.NoError(t, lerr) + require.Len(t, gs, 1) + + return gs[0].KmsKeyID + } + + require.NoError(t, m.AssociateKmsKey(ctx, "g", "", testKeyARN)) + assert.Equal(t, testKeyARN, keyOf()) + + require.NoError(t, m.DisassociateKmsKey(ctx, "g", "")) + assert.Empty(t, keyOf()) + + // The resource identifier form takes the log group ARN, with or without ":*". + arn := "arn:aws:logs:us-east-1:123456789012:log-group:g:*" + require.NoError(t, m.AssociateKmsKey(ctx, "", arn, testKeyARN)) + assert.Equal(t, testKeyARN, keyOf()) + + // Query results are an account-wide target. + qr := "arn:aws:logs:us-east-1:123456789012:query-result:*" + require.NoError(t, m.AssociateKmsKey(ctx, "", qr, testKeyARN)) + assert.Equal(t, testKeyARN, m.queryResultKmsKey) + require.NoError(t, m.DisassociateKmsKey(ctx, "", qr)) + assert.Empty(t, m.queryResultKmsKey) + + cases := []struct { + name, group, resource string + notFound bool + }{ + {name: "neither target", group: "", resource: ""}, + {name: "both targets", group: "g", resource: arn}, + {name: "unknown resource", group: "", resource: "arn:aws:s3:::bucket"}, + {name: "missing group", group: "nope", notFound: true}, + } + + for _, tc := range cases { + err = m.AssociateKmsKey(ctx, tc.group, tc.resource, testKeyARN) + require.Error(t, err, tc.name) + + if tc.notFound { + assert.True(t, errors.IsNotFound(err), tc.name) + } else { + assert.True(t, errors.IsInvalidArgument(err), tc.name) + } + } +} + +func TestListAWSLogGroupsFilters(t *testing.T) { + ctx := context.Background() + m := newTestMock() + + create := func(name, class string, tags map[string]string) { + _, err := m.CreateAWSLogGroup(ctx, &driver.LogGroupConfig{Name: name, Tags: tags}, + driver.AWSLogGroupOptions{LogGroupClass: class}) + require.NoError(t, err) + } + + create("/aws/lambda/fn", "", map[string]string{"team": "a"}) + create("/app/api", driver.LogGroupClassInfrequentAccess, map[string]string{"team": "b"}) + create("/app/discovery", "", nil) + + names := func(f driver.ListLogGroupsFilter) []string { + gs, err := m.ListAWSLogGroups(ctx, f) + require.NoError(t, err) + + out := make([]string, 0, len(gs)) + for i := range gs { + out = append(out, gs[i].Name) + } + + return out + } + + assert.Equal(t, []string{"/app/api", "/app/discovery", "/aws/lambda/fn"}, names(driver.ListLogGroupsFilter{})) + assert.Equal(t, []string{"/aws/lambda/fn"}, names(driver.ListLogGroupsFilter{NamePattern: "^/aws"})) + assert.Equal(t, []string{"/app/discovery", "/aws/lambda/fn"}, + names(driver.ListLogGroupsFilter{NamePattern: "^/aws/lambda|discovery"})) + assert.Empty(t, names(driver.ListLogGroupsFilter{NamePattern: "^api"}), "^ anchors to the start") + assert.Equal(t, []string{"/app/api"}, + names(driver.ListLogGroupsFilter{LogGroupClass: driver.LogGroupClassInfrequentAccess})) + assert.Equal(t, []string{"/app/api"}, + names(driver.ListLogGroupsFilter{Tags: []driver.LogGroupTagFilter{{Key: "team", Values: []string{"b"}}}})) + assert.Equal(t, []string{"/app/api", "/aws/lambda/fn"}, + names(driver.ListLogGroupsFilter{Tags: []driver.LogGroupTagFilter{{Key: "team"}}})) + + for _, bad := range []string{"ab", "^/aws|x", "a|b|c|d|e|f", "has space"} { + _, err := m.ListAWSLogGroups(ctx, driver.ListLogGroupsFilter{NamePattern: bad}) + require.Error(t, err, bad) + assert.True(t, errors.IsInvalidArgument(err), bad) + } +} + +func TestSnapshotKeepsKmsKeyAndClass(t *testing.T) { + ctx := context.Background() + m := newTestMock() + + _, err := m.CreateAWSLogGroup(ctx, &driver.LogGroupConfig{Name: "g"}, driver.AWSLogGroupOptions{ + KmsKeyID: testKeyARN, LogGroupClass: driver.LogGroupClassInfrequentAccess, + }) + require.NoError(t, err) + require.NoError(t, m.AssociateKmsKey(ctx, "", "arn:aws:logs:us-east-1:123456789012:query-result:*", testKeyARN)) + + raw, err := m.Snapshot(ctx, true) + require.NoError(t, err) + + restored := newTestMock() + require.NoError(t, restored.Restore(ctx, raw)) + + gs, err := restored.ListAWSLogGroups(ctx, driver.ListLogGroupsFilter{}) + require.NoError(t, err) + require.Len(t, gs, 1) + assert.Equal(t, testKeyARN, gs[0].KmsKeyID) + assert.Equal(t, driver.LogGroupClassInfrequentAccess, gs[0].LogGroupClass) + assert.Equal(t, testKeyARN, restored.queryResultKmsKey) +} diff --git a/providers/aws/cloudwatchlogs/retention.go b/providers/aws/cloudwatchlogs/retention.go new file mode 100644 index 000000000..110e344fb --- /dev/null +++ b/providers/aws/cloudwatchlogs/retention.go @@ -0,0 +1,127 @@ +package cloudwatchlogs + +import ( + "context" + "sort" + "time" + + "github.com/stackshy/cloudemu/v2/errors" +) + +// validRetentionInDays is the closed set of retentionInDays values CloudWatch +// Logs accepts; any other value is rejected with InvalidParameterException. +// +//nolint:gochecknoglobals // fixed lookup table for a closed enum. +var validRetentionInDays = map[int]bool{ + 1: true, 3: true, 5: true, 7: true, 14: true, 30: true, 60: true, 90: true, + 120: true, 150: true, 180: true, 365: true, 400: true, 545: true, 731: true, + 1096: true, 1827: true, 2192: true, 2557: true, 2922: true, 3288: true, 3653: true, +} + +// PutRetentionPolicy sets how many days a log group keeps its events. Events +// already past the new retention are purged straight away. +func (m *Mock) PutRetentionPolicy(_ context.Context, name string, retentionDays int) error { + if !validRetentionInDays[retentionDays] { + return errors.Newf(errors.InvalidArgument, + "1 validation error detected: Value '%d' at 'retentionInDays' failed to satisfy constraint: "+ + "Member must satisfy enum value set: [1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, "+ + "731, 1096, 1827, 2192, 2557, 2922, 3288, 3653]", retentionDays) + } + + if !m.groups.Update(name, func(g *logGroup) *logGroup { + g.info.RetentionDays = retentionDays + return g + }) { + return errors.Newf(errors.NotFound, "log group %q not found", name) + } + + if g, ok := m.groups.Get(name); ok { + m.purgeExpired(name, g) + } + + return nil +} + +// DeleteRetentionPolicy removes a log group's retention policy so its events +// never expire again. +func (m *Mock) DeleteRetentionPolicy(_ context.Context, name string) error { + if !m.groups.Update(name, func(g *logGroup) *logGroup { + g.info.RetentionDays = 0 + return g + }) { + return errors.Newf(errors.NotFound, "log group %q not found", name) + } + + return nil +} + +// retentionCutoff is the instant before which a group's events have expired, +// or the zero time when the group never expires. +func (m *Mock) retentionCutoff(retentionDays int) time.Time { + if retentionDays <= 0 { + return time.Time{} + } + + return m.opts.Clock.Now().AddDate(0, 0, -retentionDays) +} + +// purgeExpired deletes every event older than the group's retention and +// takes their bytes off storedBytes, so every read API sees the same data. +// +// Simplification: real CloudWatch Logs decouples billing from physical +// deletion. An event stops counting toward storedBytes once it passes +// retention, but the background deletion that removes it from read APIs can +// lag up to 72 hours, with no documented lower bound. This emulator collapses +// both into one synchronous purge at the retention boundary, which is +// deterministic under FakeClock. It does NOT model the deletion lag. +// +// Locking: only one stream lock is held at a time, and never together with +// the groups store lock. storedBytes is adjusted through groups.Update after +// every stream lock is released, the same order PutLogEvents uses. Purging +// makes no calls into other services. +func (m *Mock) purgeExpired(name string, g *logGroup) { + cutoff := m.retentionCutoff(g.info.RetentionDays) + if cutoff.IsZero() { + return + } + + var purged int64 + + for _, s := range g.streams.All() { + purged += s.purgeBefore(cutoff) + } + + if purged == 0 { + return + } + + m.groups.Update(name, func(lg *logGroup) *logGroup { + lg.info.StoredBytes = max(lg.info.StoredBytes-purged, 0) + return lg + }) +} + +// purgeBefore drops the stream's events older than cutoff and returns the +// bytes they held. Events are kept sorted ascending, so the expired ones are a +// prefix. The stream's first/last event timestamps are left alone: AWS keeps +// reporting them after the events themselves expire. +func (s *logStream) purgeBefore(cutoff time.Time) int64 { + s.mu.Lock() + defer s.mu.Unlock() + + n := sort.Search(len(s.events), func(i int) bool { + return !s.events[i].Timestamp.Before(cutoff) + }) + if n == 0 { + return 0 + } + + var bytes int64 + for i := range n { + bytes += int64(len(s.events[i].Message)) + } + + s.events = append(s.events[:0:0], s.events[n:]...) + + return bytes +} diff --git a/providers/aws/cloudwatchlogs/retention_policy_test.go b/providers/aws/cloudwatchlogs/retention_policy_test.go new file mode 100644 index 000000000..82da4f292 --- /dev/null +++ b/providers/aws/cloudwatchlogs/retention_policy_test.go @@ -0,0 +1,114 @@ +package cloudwatchlogs + +import ( + "context" + "testing" + "time" + + "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/services/logging/driver" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestPutRetentionPolicyValidation(t *testing.T) { + ctx := context.Background() + m := newWindowMock(t, 0) + + err := m.PutRetentionPolicy(ctx, "g", 2) + require.Error(t, err) + assert.True(t, errors.IsInvalidArgument(err)) + + err = m.PutRetentionPolicy(ctx, "missing", 7) + require.Error(t, err) + assert.True(t, errors.IsNotFound(err)) + + require.NoError(t, m.PutRetentionPolicy(ctx, "g", 7)) + + g, err := m.GetLogGroup(ctx, "g") + require.NoError(t, err) + assert.Equal(t, 7, g.RetentionDays) +} + +func TestDeleteRetentionPolicy(t *testing.T) { + ctx := context.Background() + m := newWindowMock(t, 7) + + require.NoError(t, m.DeleteRetentionPolicy(ctx, "g")) + + g, err := m.GetLogGroup(ctx, "g") + require.NoError(t, err) + assert.Equal(t, 0, g.RetentionDays) + + // Never-expire again: a year later the event is still there. + _, err = m.PutLogEventsWithResult(ctx, "g", "s", []driver.LogEvent{{Timestamp: testClockStart(), Message: "keep"}}) + require.NoError(t, err) + advanceClock(t, m, 365*24*time.Hour) + assert.Equal(t, []string{"keep"}, storedMessages(t, m)) + + err = m.DeleteRetentionPolicy(ctx, "missing") + require.Error(t, err) + assert.True(t, errors.IsNotFound(err)) +} + +// TestRetentionPurgeIsConsistent is the regression for GetLogEvents and +// FilterLogEvents disagreeing about expired events: both now read the same +// purged data, and storedBytes drops with the purge. +func TestRetentionPurgeIsConsistent(t *testing.T) { + ctx := context.Background() + m := newWindowMock(t, 1) + now := testClockStart() + + _, err := m.PutLogEventsWithResult(ctx, "g", "s", []driver.LogEvent{ + {Timestamp: now.Add(-time.Hour), Message: "older"}, + {Timestamp: now, Message: "newer"}, + }) + require.NoError(t, err) + + before, err := m.ListLogStreams(ctx, "g") + require.NoError(t, err) + + // 23h later only "older" (now 24h old) has passed the 1-day retention. + advanceClock(t, m, 23*time.Hour+time.Minute) + + assert.Equal(t, []string{"newer"}, storedMessages(t, m)) + + filtered, err := m.FilterLogEvents(ctx, &driver.FilterLogEventsInput{LogGroup: "g"}) + require.NoError(t, err) + require.Len(t, filtered, 1) + assert.Equal(t, "newer", filtered[0].Message) + + g, err := m.GetLogGroup(ctx, "g") + require.NoError(t, err) + assert.Equal(t, int64(len("newer")), g.StoredBytes) + + // AWS keeps reporting the stream's first event time after expiry. + after, err := m.ListLogStreams(ctx, "g") + require.NoError(t, err) + assert.Equal(t, before[0].FirstEvent, after[0].FirstEvent) +} + +// TestPutRetentionPolicyPurgesImmediately covers shortening retention on a +// group that already holds older events. +func TestPutRetentionPolicyPurgesImmediately(t *testing.T) { + ctx := context.Background() + m := newWindowMock(t, 0) + now := testClockStart() + + // Two uploads: one batch may not span more than 24 hours. + _, err := m.PutLogEventsWithResult(ctx, "g", "s", []driver.LogEvent{{Timestamp: now.Add(-3 * 24 * time.Hour), Message: "old"}}) + require.NoError(t, err) + _, err = m.PutLogEventsWithResult(ctx, "g", "s", []driver.LogEvent{{Timestamp: now, Message: "new"}}) + require.NoError(t, err) + + require.NoError(t, m.PutRetentionPolicy(ctx, "g", 1)) + + g, err := m.GetLogGroup(ctx, "g") + require.NoError(t, err) + assert.Equal(t, int64(len("new")), g.StoredBytes) + + filtered, err := m.FilterLogEvents(ctx, &driver.FilterLogEventsInput{LogGroup: "g"}) + require.NoError(t, err) + require.Len(t, filtered, 1) + assert.Equal(t, "new", filtered[0].Message) +} diff --git a/providers/aws/cloudwatchlogs/snapshot.go b/providers/aws/cloudwatchlogs/snapshot.go index e0ee300f4..9a7b85c96 100644 --- a/providers/aws/cloudwatchlogs/snapshot.go +++ b/providers/aws/cloudwatchlogs/snapshot.go @@ -18,7 +18,8 @@ var _ snapshot.Snapshottable = (*Mock)(nil) // an exported snapshot form keyed by group name. The wired deps (opts, // monitoring, lambda) are not serialized. type logsSnapshot struct { - Groups map[string]*logGroupSnapshot `json:"groups,omitempty"` + Groups map[string]*logGroupSnapshot `json:"groups,omitempty"` + QueryResultKmsKey string `json:"queryResultKmsKey,omitempty"` } // logGroupSnapshot mirrors logGroup. Its metric- and subscription-filter stores @@ -27,6 +28,8 @@ type logsSnapshot struct { // has an unexported mutex. type logGroupSnapshot struct { Info driver.LogGroupInfo `json:"info"` + KmsKeyID string `json:"kmsKeyId,omitempty"` + LogGroupClass string `json:"logGroupClass,omitempty"` Streams map[string]*logStreamSnapshot `json:"streams,omitempty"` MetricFilters json.RawMessage `json:"metricFilters,omitempty"` SubFilters json.RawMessage `json:"subFilters,omitempty"` @@ -35,14 +38,18 @@ type logGroupSnapshot struct { // logStreamSnapshot mirrors logStream, promoting its fields past the unexported // mutex. type logStreamSnapshot struct { - Info driver.LogStreamInfo `json:"info"` - Events []driver.LogEvent `json:"events,omitempty"` + Info driver.LogStreamInfo `json:"info"` + Events []driver.LogEvent `json:"events,omitempty"` + Uploads uint64 `json:"uploads,omitempty"` } // Snapshot captures every log group's state as JSON. includeAssets is unused โ€” // log events are the payload and are always captured. func (m *Mock) Snapshot(_ context.Context, _ bool) (json.RawMessage, error) { - snap := logsSnapshot{} + m.mu.RLock() + snap := logsSnapshot{QueryResultKmsKey: m.queryResultKmsKey} + m.mu.RUnlock() + if m.groups.Len() == 0 { return json.Marshal(snap) } @@ -62,7 +69,7 @@ func (m *Mock) Snapshot(_ context.Context, _ bool) (json.RawMessage, error) { } func snapshotGroup(g *logGroup) (*logGroupSnapshot, error) { - gs := &logGroupSnapshot{Info: g.info} + gs := &logGroupSnapshot{Info: g.info, KmsKeyID: g.kmsKeyID, LogGroupClass: g.class} mf, err := g.metricFilters.Snapshot() if err != nil { @@ -82,7 +89,9 @@ func snapshotGroup(g *logGroup) (*logGroupSnapshot, error) { for sName, s := range g.streams.All() { s.mu.RLock() - gs.Streams[sName] = &logStreamSnapshot{Info: s.info, Events: append([]driver.LogEvent(nil), s.events...)} + gs.Streams[sName] = &logStreamSnapshot{ + Info: s.info, Events: append([]driver.LogEvent(nil), s.events...), Uploads: s.uploads, + } s.mu.RUnlock() } } @@ -98,6 +107,10 @@ func (m *Mock) Restore(_ context.Context, data json.RawMessage) error { return fmt.Errorf("cloudwatchlogs: parse snapshot: %w", err) } + m.mu.Lock() + m.queryResultKmsKey = snap.QueryResultKmsKey + m.mu.Unlock() + for name, gs := range snap.Groups { g, err := restoreGroup(gs) if err != nil { @@ -113,6 +126,8 @@ func (m *Mock) Restore(_ context.Context, data json.RawMessage) error { func restoreGroup(gs *logGroupSnapshot) (*logGroup, error) { g := &logGroup{ info: gs.Info, + kmsKeyID: gs.KmsKeyID, + class: gs.LogGroupClass, streams: memstore.New[*logStream](), metricFilters: memstore.New[*driver.MetricFilterInfo](), subFilters: memstore.New[*driver.SubscriptionFilterInfo](), @@ -131,7 +146,7 @@ func restoreGroup(gs *logGroupSnapshot) (*logGroup, error) { } for sName, ss := range gs.Streams { - g.streams.Set(sName, &logStream{info: ss.Info, events: ss.Events}) + g.streams.Set(sName, &logStream{info: ss.Info, events: ss.Events, uploads: ss.Uploads}) } return g, nil diff --git a/providers/aws/cloudwatchlogs/snapshot_test.go b/providers/aws/cloudwatchlogs/snapshot_test.go index a7ad61361..52ca67ace 100644 --- a/providers/aws/cloudwatchlogs/snapshot_test.go +++ b/providers/aws/cloudwatchlogs/snapshot_test.go @@ -3,7 +3,6 @@ package cloudwatchlogs import ( "context" "testing" - "time" "github.com/stackshy/cloudemu/v2/services/logging/driver" ) @@ -23,7 +22,7 @@ func TestSnapshotRoundTripCloudWatchLogs(t *testing.T) { } if err := src.PutLogEvents(ctx, "/svc/app", "s1", []driver.LogEvent{ - {Timestamp: time.Unix(1, 0), Message: "hello"}, + {Timestamp: testEventTime(), Message: "hello"}, }); err != nil { t.Fatalf("put log events: %v", err) } diff --git a/providers/aws/cloudwatchlogs/validation_test.go b/providers/aws/cloudwatchlogs/validation_test.go index 28cc50244..b708bf79c 100644 --- a/providers/aws/cloudwatchlogs/validation_test.go +++ b/providers/aws/cloudwatchlogs/validation_test.go @@ -4,7 +4,6 @@ import ( "context" "strings" "testing" - "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -72,7 +71,7 @@ func TestCreateLogStreamNameRules(t *testing.T) { } func TestPutLogEventsBatchLimits(t *testing.T) { - base := time.Date(2025, 6, 1, 12, 0, 0, 0, time.UTC) + base := testEventTime() manyEvents := func(n int) []driver.LogEvent { out := make([]driver.LogEvent, n) diff --git a/providers/aws/lambda/execlogs_test.go b/providers/aws/lambda/execlogs_test.go index 5144a0167..32de03afd 100644 --- a/providers/aws/lambda/execlogs_test.go +++ b/providers/aws/lambda/execlogs_test.go @@ -5,7 +5,6 @@ import ( "strings" "testing" - "github.com/stackshy/cloudemu/v2/config" "github.com/stackshy/cloudemu/v2/providers/aws/cloudwatchlogs" logdriver "github.com/stackshy/cloudemu/v2/services/logging/driver" "github.com/stackshy/cloudemu/v2/services/serverless/driver" @@ -17,7 +16,8 @@ import ( func TestInvokeSurfacesLogsToCloudWatch(t *testing.T) { ctx := context.Background() m := newTestMock() - cwl := cloudwatchlogs.New(config.NewOptions(config.WithRegion("us-east-1"))) + // Same clock as the Lambda mock: CloudWatch Logs drops events outside its ingest window. + cwl := cloudwatchlogs.New(m.opts) m.SetLogSink(cwl) _, err := m.CreateFunction(ctx, defaultFuncConfig()) @@ -42,7 +42,8 @@ func TestInvokeSurfacesLogsToCloudWatch(t *testing.T) { func TestInvokeSurfacesHandlerErrorLog(t *testing.T) { ctx := context.Background() m := newTestMock() - cwl := cloudwatchlogs.New(config.NewOptions(config.WithRegion("us-east-1"))) + // Same clock as the Lambda mock: CloudWatch Logs drops events outside its ingest window. + cwl := cloudwatchlogs.New(m.opts) m.SetLogSink(cwl) _, err := m.CreateFunction(ctx, defaultFuncConfig()) diff --git a/server/aws/cloudwatchlogs/handler.go b/server/aws/cloudwatchlogs/handler.go index 555eeb050..bb899e952 100644 --- a/server/aws/cloudwatchlogs/handler.go +++ b/server/aws/cloudwatchlogs/handler.go @@ -16,6 +16,12 @@ // PutLogEvents GetLogEvents FilterLogEvents // PutMetricFilter DescribeMetricFilters DeleteMetricFilter // PutSubscriptionFilter DescribeSubscriptionFilters DeleteSubscriptionFilter +// PutRetentionPolicy DeleteRetentionPolicy ListLogGroups +// AssociateKmsKey DisassociateKmsKey +// TagResource / UntagResource / ListTagsForResource (and the legacy *LogGroup forms) +// +// CloudWatch Logs-only operations go through the optional driver.AWSLogging +// interface, which the AWS backend implements. package cloudwatchlogs import ( @@ -80,13 +86,11 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } -// dispatchManagement handles the retention, metric-filter, and -// subscription-filter operations, delegating tagging (and the -// unknown-operation error) to dispatchTags. +// dispatchManagement handles the metric-filter and subscription-filter +// operations, delegating the log-group settings, tagging, and the +// unknown-operation error to dispatchGroupSettings. func (h *Handler) dispatchManagement(w http.ResponseWriter, r *http.Request, op string) { switch op { - case "PutRetentionPolicy": - h.putRetentionPolicy(w, r) case "PutMetricFilter": h.putMetricFilter(w, r) case "DescribeMetricFilters": @@ -99,6 +103,25 @@ func (h *Handler) dispatchManagement(w http.ResponseWriter, r *http.Request, op h.describeSubscriptionFilters(w, r) case "DeleteSubscriptionFilter": h.deleteSubscriptionFilter(w, r) + default: + h.dispatchGroupSettings(w, r, op) + } +} + +// dispatchGroupSettings handles retention, ListLogGroups, and KMS key +// association, delegating tagging to dispatchTags. +func (h *Handler) dispatchGroupSettings(w http.ResponseWriter, r *http.Request, op string) { + switch op { + case "PutRetentionPolicy": + h.putRetentionPolicy(w, r) + case "DeleteRetentionPolicy": + h.deleteRetentionPolicy(w, r) + case "ListLogGroups": + h.listLogGroups(w, r) + case "AssociateKmsKey": + h.associateKmsKey(w, r) + case "DisassociateKmsKey": + h.disassociateKmsKey(w, r) default: h.dispatchTags(w, r, op) } @@ -116,47 +139,25 @@ func (h *Handler) dispatchTags(w http.ResponseWriter, r *http.Request, op string case "ListTagsForResource", "ListTagsLogGroup": h.listTagsForResource(w, r) default: - wire.WriteJSONError(w, http.StatusBadRequest, - "UnknownOperationException", "unknown CloudWatch Logs operation: "+op) + writeUnknownOperation(w, op) } } -// putRetentionPolicy sets a log group's retention (SSM PutRetentionPolicy), -// backed by the driver's UpdateLogGroup. -func (h *Handler) putRetentionPolicy(w http.ResponseWriter, r *http.Request) { - var req struct { - LogGroupName string `json:"logGroupName"` - RetentionInDays int `json:"retentionInDays"` - } - - if !wire.DecodeJSON(w, r, &req) { - return - } - - if !validRetentionInDays[req.RetentionInDays] { - wire.WriteJSONError(w, http.StatusBadRequest, "InvalidParameterException", - "retentionInDays is not a valid value") - return - } +func writeUnknownOperation(w http.ResponseWriter, op string) { + wire.WriteJSONError(w, http.StatusBadRequest, + "UnknownOperationException", "unknown CloudWatch Logs operation: "+op) +} - if _, err := h.logs.UpdateLogGroup(r.Context(), logdriver.LogGroupConfig{ - Name: req.LogGroupName, RetentionDays: req.RetentionInDays, - }); err != nil { - writeErr(w, err) - return +// awsLogs returns the CloudWatch Logs-only driver surface. When the backend +// does not implement it, the operation is answered as unknown, which is what +// a backend without the operation would do. +func (h *Handler) awsLogs(w http.ResponseWriter, op string) (logdriver.AWSLogging, bool) { + ext, ok := h.logs.(logdriver.AWSLogging) + if !ok { + writeUnknownOperation(w, op) } - wire.WriteJSON(w, struct{}{}) -} - -// validRetentionInDays is the closed set of retentionInDays values CloudWatch -// Logs accepts; any other value is rejected with InvalidParameterException. -// -//nolint:gochecknoglobals // fixed lookup table for a closed enum. -var validRetentionInDays = map[int]bool{ - 1: true, 3: true, 5: true, 7: true, 14: true, 30: true, 60: true, 90: true, - 120: true, 150: true, 180: true, 365: true, 400: true, 545: true, 731: true, - 1096: true, 1827: true, 2192: true, 2557: true, 2922: true, 3288: true, 3653: true, + return ext, ok } // writeErr maps canonical cloudemu errors to CloudWatch Logs JSON error diff --git a/server/aws/cloudwatchlogs/log_group_settings.go b/server/aws/cloudwatchlogs/log_group_settings.go new file mode 100644 index 000000000..ab4aec82c --- /dev/null +++ b/server/aws/cloudwatchlogs/log_group_settings.go @@ -0,0 +1,178 @@ +package cloudwatchlogs + +import ( + "net/http" + + "github.com/stackshy/cloudemu/v2/server/wire" + logdriver "github.com/stackshy/cloudemu/v2/services/logging/driver" +) + +// maxListLogGroupsLimit is the largest ListLogGroups limit AWS accepts. +const maxListLogGroupsLimit = 1000 + +func (h *Handler) putRetentionPolicy(w http.ResponseWriter, r *http.Request) { + ext, ok := h.awsLogs(w, "PutRetentionPolicy") + if !ok { + return + } + + var req struct { + LogGroupName string `json:"logGroupName"` + RetentionInDays int `json:"retentionInDays"` + } + + if !wire.DecodeJSON(w, r, &req) { + return + } + + if err := ext.PutRetentionPolicy(r.Context(), req.LogGroupName, req.RetentionInDays); err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, struct{}{}) +} + +func (h *Handler) deleteRetentionPolicy(w http.ResponseWriter, r *http.Request) { + ext, ok := h.awsLogs(w, "DeleteRetentionPolicy") + if !ok { + return + } + + var req struct { + LogGroupName string `json:"logGroupName"` + } + + if !wire.DecodeJSON(w, r, &req) { + return + } + + if err := ext.DeleteRetentionPolicy(r.Context(), req.LogGroupName); err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, struct{}{}) +} + +type logGroupTagFilterJSON struct { + Key string `json:"key"` + Values []string `json:"values"` +} + +type listLogGroupsRequest struct { + LogGroupNamePattern string `json:"logGroupNamePattern"` + LogGroupClass string `json:"logGroupClass"` + LogGroupTags []logGroupTagFilterJSON `json:"logGroupTags"` + Limit int32 `json:"limit"` + NextToken string `json:"nextToken"` +} + +type logGroupSummaryJSON struct { + LogGroupName string `json:"logGroupName"` + LogGroupArn string `json:"logGroupArn"` + LogGroupClass string `json:"logGroupClass"` +} + +type listLogGroupsResponse struct { + LogGroups []logGroupSummaryJSON `json:"logGroups"` + NextToken string `json:"nextToken,omitempty"` +} + +// listLogGroups serves the account-level ListLogGroups. Linked-account +// parameters are accepted and ignored: there is a single account here. +func (h *Handler) listLogGroups(w http.ResponseWriter, r *http.Request) { + ext, ok := h.awsLogs(w, "ListLogGroups") + if !ok { + return + } + + var req listLogGroupsRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + if req.Limit < 0 || req.Limit > maxListLogGroupsLimit { + wire.WriteJSONError(w, http.StatusBadRequest, "InvalidParameterException", + "1 validation error detected: Value at 'limit' failed to satisfy constraint: "+ + "Member must have value between 1 and 1000") + + return + } + + filter := logdriver.ListLogGroupsFilter{ + NamePattern: req.LogGroupNamePattern, + LogGroupClass: req.LogGroupClass, + } + + for _, t := range req.LogGroupTags { + filter.Tags = append(filter.Tags, logdriver.LogGroupTagFilter{Key: t.Key, Values: t.Values}) + } + + groups, err := ext.ListAWSLogGroups(r.Context(), filter) + if err != nil { + writeErr(w, err) + return + } + + from, to, next := pageBounds(len(groups), decodeOffsetToken(req.NextToken), req.Limit) + + resp := listLogGroupsResponse{LogGroups: make([]logGroupSummaryJSON, 0, to-from)} + for i := from; i < to; i++ { + resp.LogGroups = append(resp.LogGroups, logGroupSummaryJSON{ + LogGroupName: groups[i].Name, + LogGroupArn: groups[i].ResourceID, + LogGroupClass: groups[i].LogGroupClass, + }) + } + + if next > 0 { + resp.NextToken = encodeOffsetToken(next) + } + + wire.WriteJSON(w, resp) +} + +type kmsKeyRequest struct { + LogGroupName string `json:"logGroupName"` + ResourceIdentifier string `json:"resourceIdentifier"` + KmsKeyID string `json:"kmsKeyId"` +} + +func (h *Handler) associateKmsKey(w http.ResponseWriter, r *http.Request) { + ext, ok := h.awsLogs(w, "AssociateKmsKey") + if !ok { + return + } + + var req kmsKeyRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + if err := ext.AssociateKmsKey(r.Context(), req.LogGroupName, req.ResourceIdentifier, req.KmsKeyID); err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, struct{}{}) +} + +func (h *Handler) disassociateKmsKey(w http.ResponseWriter, r *http.Request) { + ext, ok := h.awsLogs(w, "DisassociateKmsKey") + if !ok { + return + } + + var req kmsKeyRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + if err := ext.DisassociateKmsKey(r.Context(), req.LogGroupName, req.ResourceIdentifier); err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, struct{}{}) +} diff --git a/server/aws/cloudwatchlogs/log_group_settings_sdk_test.go b/server/aws/cloudwatchlogs/log_group_settings_sdk_test.go new file mode 100644 index 000000000..e0885aa34 --- /dev/null +++ b/server/aws/cloudwatchlogs/log_group_settings_sdk_test.go @@ -0,0 +1,332 @@ +package cloudwatchlogs_test + +import ( + "context" + "errors" + "net/http/httptest" + "strings" + "testing" + "time" + + "github.com/aws/aws-sdk-go-v2/aws" + awsconfig "github.com/aws/aws-sdk-go-v2/config" + "github.com/aws/aws-sdk-go-v2/credentials" + cwl "github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs" + cwltypes "github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs/types" + + "github.com/stackshy/cloudemu/v2" + awsserver "github.com/stackshy/cloudemu/v2/server/aws" + kmsdriver "github.com/stackshy/cloudemu/v2/services/kms/driver" +) + +// newLogsClientWithKey returns a CloudWatch Logs client plus the ARN of an +// enabled KMS key and a disabled one, both created in the same provider. +func newLogsClientWithKey(t *testing.T) (client *cwl.Client, enabledKey, disabledKey string) { + t.Helper() + + cloud := cloudemu.NewAWS() + ctx := context.Background() + + enabled, err := cloud.KMS.CreateKey(ctx, kmsdriver.CreateKeyInput{}) + if err != nil { + t.Fatalf("CreateKey: %v", err) + } + + disabled, err := cloud.KMS.CreateKey(ctx, kmsdriver.CreateKeyInput{}) + if err != nil { + t.Fatalf("CreateKey: %v", err) + } + + if err := cloud.KMS.DisableKey(ctx, disabled.KeyID); err != nil { + t.Fatalf("DisableKey: %v", err) + } + + ts := httptest.NewServer(awsserver.New(awsserver.Drivers{CloudWatchLogs: cloud.CloudWatchLogs})) + t.Cleanup(ts.Close) + + cfg, err := awsconfig.LoadDefaultConfig(ctx, + awsconfig.WithRegion("us-east-1"), + awsconfig.WithCredentialsProvider(credentials.NewStaticCredentialsProvider("test", "test", "")), + ) + if err != nil { + t.Fatalf("aws config: %v", err) + } + + client = cwl.NewFromConfig(cfg, func(o *cwl.Options) { o.BaseEndpoint = aws.String(ts.URL) }) + + return client, enabled.ARN, disabled.ARN +} + +func describeOne(t *testing.T, c *cwl.Client, name string) cwltypes.LogGroup { + t.Helper() + + out, err := c.DescribeLogGroups(context.Background(), &cwl.DescribeLogGroupsInput{LogGroupNamePrefix: aws.String(name)}) + if err != nil { + t.Fatalf("DescribeLogGroups: %v", err) + } + + if len(out.LogGroups) != 1 { + t.Fatalf("DescribeLogGroups(%q) = %d groups, want 1", name, len(out.LogGroups)) + } + + return out.LogGroups[0] +} + +func TestSDKDeleteRetentionPolicy(t *testing.T) { + c := newLogsClient(t) + ctx := context.Background() + g := aws.String("/ret/del") + + if _, err := c.CreateLogGroup(ctx, &cwl.CreateLogGroupInput{LogGroupName: g}); err != nil { + t.Fatalf("CreateLogGroup: %v", err) + } + + if _, err := c.PutRetentionPolicy(ctx, &cwl.PutRetentionPolicyInput{LogGroupName: g, RetentionInDays: aws.Int32(7)}); err != nil { + t.Fatalf("PutRetentionPolicy: %v", err) + } + + if _, err := c.DeleteRetentionPolicy(ctx, &cwl.DeleteRetentionPolicyInput{LogGroupName: g}); err != nil { + t.Fatalf("DeleteRetentionPolicy: %v", err) + } + + if got := describeOne(t, c, "/ret/del"); got.RetentionInDays != nil { + t.Fatalf("RetentionInDays = %d after DeleteRetentionPolicy, want unset", aws.ToInt32(got.RetentionInDays)) + } + + var nf *cwltypes.ResourceNotFoundException + + _, err := c.DeleteRetentionPolicy(ctx, &cwl.DeleteRetentionPolicyInput{LogGroupName: aws.String("/ret/missing")}) + if !errors.As(err, &nf) { + t.Fatalf("DeleteRetentionPolicy on missing group: got %v, want ResourceNotFoundException", err) + } + + var invalid *cwltypes.InvalidParameterException + + _, err = c.PutRetentionPolicy(ctx, &cwl.PutRetentionPolicyInput{LogGroupName: g, RetentionInDays: aws.Int32(2)}) + if !errors.As(err, &invalid) { + t.Fatalf("PutRetentionPolicy(2): got %v, want InvalidParameterException", err) + } +} + +func TestSDKListLogGroups(t *testing.T) { + c := newLogsClient(t) + ctx := context.Background() + + for _, in := range []*cwl.CreateLogGroupInput{ + {LogGroupName: aws.String("/aws/lambda/fn")}, + {LogGroupName: aws.String("/app/api"), LogGroupClass: cwltypes.LogGroupClassInfrequentAccess}, + {LogGroupName: aws.String("/app/web")}, + } { + if _, err := c.CreateLogGroup(ctx, in); err != nil { + t.Fatalf("CreateLogGroup: %v", err) + } + } + + all, err := c.ListLogGroups(ctx, &cwl.ListLogGroupsInput{Limit: aws.Int32(2)}) + if err != nil { + t.Fatalf("ListLogGroups: %v", err) + } + + if len(all.LogGroups) != 2 || all.NextToken == nil { + t.Fatalf("first page = %d groups, token %v; want 2 and a token", len(all.LogGroups), all.NextToken) + } + + first := all.LogGroups[0] + if aws.ToString(first.LogGroupName) != "/app/api" || first.LogGroupClass != cwltypes.LogGroupClassInfrequentAccess || + strings.HasSuffix(aws.ToString(first.LogGroupArn), ":*") || !strings.Contains(aws.ToString(first.LogGroupArn), ":log-group:/app/api") { + t.Fatalf("first summary = %+v", first) + } + + next, err := c.ListLogGroups(ctx, &cwl.ListLogGroupsInput{Limit: aws.Int32(2), NextToken: all.NextToken}) + if err != nil { + t.Fatalf("ListLogGroups page 2: %v", err) + } + + if len(next.LogGroups) != 1 || next.NextToken != nil { + t.Fatalf("second page = %+v", next) + } + + byPattern, err := c.ListLogGroups(ctx, &cwl.ListLogGroupsInput{LogGroupNamePattern: aws.String("^/aws")}) + if err != nil { + t.Fatalf("ListLogGroups pattern: %v", err) + } + + if len(byPattern.LogGroups) != 1 || aws.ToString(byPattern.LogGroups[0].LogGroupName) != "/aws/lambda/fn" { + t.Fatalf("pattern ^/aws = %+v", byPattern.LogGroups) + } + + var invalid *cwltypes.InvalidParameterException + + if _, err := c.ListLogGroups(ctx, &cwl.ListLogGroupsInput{LogGroupNamePattern: aws.String("ab")}); !errors.As(err, &invalid) { + t.Fatalf("too-short pattern: got %v, want InvalidParameterException", err) + } +} + +func TestSDKPutLogEventsIngestWindow(t *testing.T) { + c := newLogsClient(t) + ctx := context.Background() + g, s := aws.String("/win/g"), aws.String("s") + + if _, err := c.CreateLogGroup(ctx, &cwl.CreateLogGroupInput{LogGroupName: g}); err != nil { + t.Fatalf("CreateLogGroup: %v", err) + } + + if _, err := c.CreateLogStream(ctx, &cwl.CreateLogStreamInput{LogGroupName: g, LogStreamName: s}); err != nil { + t.Fatalf("CreateLogStream: %v", err) + } + + now := time.Now() + ms := func(d time.Duration) *int64 { return aws.Int64(now.Add(d).UnixMilli()) } + + out, err := c.PutLogEvents(ctx, &cwl.PutLogEventsInput{LogGroupName: g, LogStreamName: s, LogEvents: []cwltypes.InputLogEvent{ + {Timestamp: ms(-20 * 24 * time.Hour), Message: aws.String("too old")}, + {Timestamp: ms(0), Message: aws.String("kept")}, + {Timestamp: ms(3 * time.Hour), Message: aws.String("too new")}, + }}) + if err != nil { + t.Fatalf("PutLogEvents: %v", err) + } + + rej := out.RejectedLogEventsInfo + if rej == nil || aws.ToInt32(rej.TooOldLogEventEndIndex) != 1 || aws.ToInt32(rej.TooNewLogEventStartIndex) != 2 || + rej.ExpiredLogEventEndIndex != nil { + t.Fatalf("RejectedLogEventsInfo = %+v, want tooOld=1 tooNew=2", rej) + } + + second, err := c.PutLogEvents(ctx, &cwl.PutLogEventsInput{LogGroupName: g, LogStreamName: s, LogEvents: []cwltypes.InputLogEvent{ + {Timestamp: ms(time.Second), Message: aws.String("kept too")}, + }}) + if err != nil { + t.Fatalf("PutLogEvents: %v", err) + } + + if second.RejectedLogEventsInfo != nil { + t.Fatalf("clean batch RejectedLogEventsInfo = %+v, want nil", second.RejectedLogEventsInfo) + } + + tok1, tok2 := aws.ToString(out.NextSequenceToken), aws.ToString(second.NextSequenceToken) + if len(tok1) != 56 || tok1 == tok2 || tok1 == "0" { + t.Fatalf("sequence tokens %q, %q: want distinct 56-digit tokens", tok1, tok2) + } + + got, err := c.GetLogEvents(ctx, &cwl.GetLogEventsInput{LogGroupName: g, LogStreamName: s, StartFromHead: aws.Bool(true)}) + if err != nil { + t.Fatalf("GetLogEvents: %v", err) + } + + if len(got.Events) != 2 || aws.ToString(got.Events[0].Message) != "kept" { + t.Fatalf("stored events = %+v, want only the accepted two", got.Events) + } +} + +// TestSDKRetentionReadsAgree is the regression for GetLogEvents hiding expired +// events while FilterLogEvents still returned them. +func TestSDKRetentionReadsAgree(t *testing.T) { + c := newLogsClient(t) + ctx := context.Background() + g, s := aws.String("/win/ret"), aws.String("s") + + if _, err := c.CreateLogGroup(ctx, &cwl.CreateLogGroupInput{LogGroupName: g}); err != nil { + t.Fatalf("CreateLogGroup: %v", err) + } + + if _, err := c.CreateLogStream(ctx, &cwl.CreateLogStreamInput{LogGroupName: g, LogStreamName: s}); err != nil { + t.Fatalf("CreateLogStream: %v", err) + } + + now := time.Now() + + for _, d := range []time.Duration{-3 * 24 * time.Hour, 0} { + if _, err := c.PutLogEvents(ctx, &cwl.PutLogEventsInput{LogGroupName: g, LogStreamName: s, LogEvents: []cwltypes.InputLogEvent{ + {Timestamp: aws.Int64(now.Add(d).UnixMilli()), Message: aws.String("m")}, + }}); err != nil { + t.Fatalf("PutLogEvents: %v", err) + } + } + + if _, err := c.PutRetentionPolicy(ctx, &cwl.PutRetentionPolicyInput{LogGroupName: g, RetentionInDays: aws.Int32(1)}); err != nil { + t.Fatalf("PutRetentionPolicy: %v", err) + } + + get, err := c.GetLogEvents(ctx, &cwl.GetLogEventsInput{LogGroupName: g, LogStreamName: s}) + if err != nil { + t.Fatalf("GetLogEvents: %v", err) + } + + filter, err := c.FilterLogEvents(ctx, &cwl.FilterLogEventsInput{LogGroupName: g}) + if err != nil { + t.Fatalf("FilterLogEvents: %v", err) + } + + if len(get.Events) != 1 || len(filter.Events) != 1 { + t.Fatalf("after 1-day retention: Get=%d Filter=%d, want 1 and 1", len(get.Events), len(filter.Events)) + } + + if sb := aws.ToInt64(describeOne(t, c, "/win/ret").StoredBytes); sb != 1 { + t.Fatalf("storedBytes = %d, want 1 after the purge", sb) + } +} + +func TestSDKLogGroupKmsKeyAndClass(t *testing.T) { + c, key, disabledKey := newLogsClientWithKey(t) + ctx := context.Background() + + if _, err := c.CreateLogGroup(ctx, &cwl.CreateLogGroupInput{ + LogGroupName: aws.String("/kms/g"), KmsKeyId: aws.String(key), LogGroupClass: cwltypes.LogGroupClassInfrequentAccess, + }); err != nil { + t.Fatalf("CreateLogGroup: %v", err) + } + + got := describeOne(t, c, "/kms/g") + if aws.ToString(got.KmsKeyId) != key || got.LogGroupClass != cwltypes.LogGroupClassInfrequentAccess { + t.Fatalf("described group = kms %q class %q", aws.ToString(got.KmsKeyId), got.LogGroupClass) + } + + if arn := aws.ToString(got.Arn); !strings.HasSuffix(arn, ":log-group:/kms/g:*") || aws.ToString(got.LogGroupArn)+":*" != arn { + t.Fatalf("arn = %q, logGroupArn = %q", arn, aws.ToString(got.LogGroupArn)) + } + + if _, err := c.DisassociateKmsKey(ctx, &cwl.DisassociateKmsKeyInput{LogGroupName: aws.String("/kms/g")}); err != nil { + t.Fatalf("DisassociateKmsKey: %v", err) + } + + if k := describeOne(t, c, "/kms/g").KmsKeyId; k != nil { + t.Fatalf("kmsKeyId = %q after disassociate, want unset", aws.ToString(k)) + } + + if _, err := c.AssociateKmsKey(ctx, &cwl.AssociateKmsKeyInput{ + ResourceIdentifier: got.LogGroupArn, KmsKeyId: aws.String(key), + }); err != nil { + t.Fatalf("AssociateKmsKey by ARN: %v", err) + } + + if aws.ToString(describeOne(t, c, "/kms/g").KmsKeyId) != key { + t.Fatal("kmsKeyId not set by AssociateKmsKey") + } + + var invalid *cwltypes.InvalidParameterException + + if _, err := c.AssociateKmsKey(ctx, &cwl.AssociateKmsKeyInput{ + LogGroupName: aws.String("/kms/g"), KmsKeyId: aws.String(disabledKey), + }); !errors.As(err, &invalid) { + t.Fatalf("disabled key: got %v, want InvalidParameterException", err) + } + + if _, err := c.CreateLogGroup(ctx, &cwl.CreateLogGroupInput{ + LogGroupName: aws.String("/kms/missing"), KmsKeyId: aws.String("arn:aws:kms:us-east-1:123456789012:key/nope"), + }); !errors.As(err, &invalid) { + t.Fatalf("missing key on create: got %v, want InvalidParameterException", err) + } + + var nf *cwltypes.ResourceNotFoundException + + if _, err := c.AssociateKmsKey(ctx, &cwl.AssociateKmsKeyInput{ + LogGroupName: aws.String("/kms/none"), KmsKeyId: aws.String(key), + }); !errors.As(err, &nf) { + t.Fatalf("missing group: got %v, want ResourceNotFoundException", err) + } + + if got := describeOne(t, c, "/kms/g"); got.LogGroupClass != cwltypes.LogGroupClassInfrequentAccess { + t.Fatalf("class changed to %q", got.LogGroupClass) + } +} diff --git a/server/aws/cloudwatchlogs/operations.go b/server/aws/cloudwatchlogs/operations.go index a7220fea4..80e64f44a 100644 --- a/server/aws/cloudwatchlogs/operations.go +++ b/server/aws/cloudwatchlogs/operations.go @@ -97,10 +97,18 @@ func (h *Handler) createLogGroup(w http.ResponseWriter, r *http.Request) { return } - if _, err := h.logs.CreateLogGroup(r.Context(), logdriver.LogGroupConfig{ - Name: req.LogGroupName, - Tags: req.Tags, - }); err != nil { + cfg := logdriver.LogGroupConfig{Name: req.LogGroupName, Tags: req.Tags} + + var err error + if ext, ok := h.logs.(logdriver.AWSLogging); ok { + _, err = ext.CreateAWSLogGroup(r.Context(), &cfg, logdriver.AWSLogGroupOptions{ + KmsKeyID: req.KmsKeyID, LogGroupClass: req.LogGroupClass, + }) + } else { + _, err = h.logs.CreateLogGroup(r.Context(), cfg) + } + + if err != nil { writeErr(w, err) return } @@ -115,13 +123,13 @@ func (h *Handler) describeLogGroups(w http.ResponseWriter, r *http.Request) { return } - infos, err := h.logs.ListLogGroups(r.Context(), scope.Scope{}) + infos, err := h.describableLogGroups(r) if err != nil { writeErr(w, err) return } - matched := make([]logdriver.LogGroupInfo, 0, len(infos)) + matched := make([]logdriver.AWSLogGroup, 0, len(infos)) for i := range infos { if req.LogGroupNamePrefix != "" && !strings.HasPrefix(infos[i].Name, req.LogGroupNamePrefix) { @@ -146,6 +154,27 @@ func (h *Handler) describeLogGroups(w http.ResponseWriter, r *http.Request) { wire.WriteJSON(w, resp) } +// describableLogGroups lists every log group with its CloudWatch Logs-only +// attributes, falling back to the portable listing (STANDARD, no key) for a +// backend without driver.AWSLogging. +func (h *Handler) describableLogGroups(r *http.Request) ([]logdriver.AWSLogGroup, error) { + if ext, ok := h.logs.(logdriver.AWSLogging); ok { + return ext.ListAWSLogGroups(r.Context(), logdriver.ListLogGroupsFilter{}) + } + + infos, err := h.logs.ListLogGroups(r.Context(), scope.Scope{}) + if err != nil { + return nil, err + } + + out := make([]logdriver.AWSLogGroup, 0, len(infos)) + for i := range infos { + out = append(out, logdriver.AWSLogGroup{LogGroupInfo: infos[i], LogGroupClass: logdriver.LogGroupClassStandard}) + } + + return out, nil +} + func (h *Handler) deleteLogGroup(w http.ResponseWriter, r *http.Request) { var req deleteLogGroupRequest if !wire.DecodeJSON(w, r, &req) { @@ -281,14 +310,27 @@ func (h *Handler) putLogEvents(w http.ResponseWriter, r *http.Request) { }) } - if err := h.logs.PutLogEvents(r.Context(), req.LogGroupName, req.LogStreamName, events); err != nil { + ext, ok := h.awsLogs(w, "PutLogEvents") + if !ok { + return + } + + res, err := ext.PutLogEventsWithResult(r.Context(), req.LogGroupName, req.LogStreamName, events) + if err != nil { writeErr(w, err) return } - // The sequence token is deprecated and ignored by the modern SDK, but a - // non-empty value keeps older callers happy. - wire.WriteJSON(w, putLogEventsResponse{NextSequenceToken: "0"}) + resp := putLogEventsResponse{NextSequenceToken: res.NextSequenceToken} + if rej := res.Rejected; rej != nil { + resp.RejectedLogEventsInfo = &rejectedLogEventsInfoJSON{ + TooOldLogEventEndIndex: rej.TooOldLogEventEndIndex, + ExpiredLogEventEndIndex: rej.ExpiredLogEventEndIndex, + TooNewLogEventStartIndex: rej.TooNewLogEventStartIndex, + } + } + + wire.WriteJSON(w, resp) } // getLogEventsWindow picks the [start,end) slice bounds for a GetLogEvents page diff --git a/server/aws/cloudwatchlogs/sdk_roundtrip_test.go b/server/aws/cloudwatchlogs/sdk_roundtrip_test.go index 21a09f054..0dcefe22c 100644 --- a/server/aws/cloudwatchlogs/sdk_roundtrip_test.go +++ b/server/aws/cloudwatchlogs/sdk_roundtrip_test.go @@ -715,8 +715,8 @@ func TestSDKPutLogEventsBatchValidation(t *testing.T) { LogGroupName: aws.String("/pv/g"), LogStreamName: aws.String("s1"), LogEvents: []cwltypes.InputLogEvent{ - {Timestamp: aws.Int64(base.UnixMilli()), Message: aws.String("early")}, - {Timestamp: aws.Int64(base.Add(25 * time.Hour).UnixMilli()), Message: aws.String("too late")}, + {Timestamp: aws.Int64(base.Add(-25 * time.Hour).UnixMilli()), Message: aws.String("early")}, + {Timestamp: aws.Int64(base.UnixMilli()), Message: aws.String("too late")}, }, }) if !errors.As(err, &invalid) { diff --git a/server/aws/cloudwatchlogs/types.go b/server/aws/cloudwatchlogs/types.go index 68ad28edb..e37a61bf3 100644 --- a/server/aws/cloudwatchlogs/types.go +++ b/server/aws/cloudwatchlogs/types.go @@ -9,8 +9,10 @@ import ( // --- request envelopes --- type createLogGroupRequest struct { - LogGroupName string `json:"logGroupName"` - Tags map[string]string `json:"tags"` + LogGroupName string `json:"logGroupName"` + Tags map[string]string `json:"tags"` + KmsKeyID string `json:"kmsKeyId"` + LogGroupClass string `json:"logGroupClass"` } type describeLogGroupsRequest struct { @@ -78,13 +80,17 @@ type filterLogEventsRequest struct { // logGroupJSON is the CloudWatch Logs LogGroup shape. Timestamps are epoch // milliseconds, which is what the AWS JSON protocol uses for the SDK's -// *int64 CreationTime field. +// *int64 CreationTime field. arn carries AWS's trailing ":*"; logGroupArn is +// the bare group ARN. type logGroupJSON struct { LogGroupName string `json:"logGroupName"` Arn string `json:"arn"` + LogGroupArn string `json:"logGroupArn"` CreationTime int64 `json:"creationTime"` RetentionInDays int32 `json:"retentionInDays,omitempty"` StoredBytes int64 `json:"storedBytes"` + KmsKeyID string `json:"kmsKeyId,omitempty"` + LogGroupClass string `json:"logGroupClass,omitempty"` } type describeLogGroupsResponse struct { @@ -107,8 +113,15 @@ type describeLogStreamsResponse struct { NextToken string `json:"nextToken,omitempty"` } +type rejectedLogEventsInfoJSON struct { + TooOldLogEventEndIndex *int `json:"tooOldLogEventEndIndex,omitempty"` + ExpiredLogEventEndIndex *int `json:"expiredLogEventEndIndex,omitempty"` + TooNewLogEventStartIndex *int `json:"tooNewLogEventStartIndex,omitempty"` +} + type putLogEventsResponse struct { - NextSequenceToken string `json:"nextSequenceToken"` + NextSequenceToken string `json:"nextSequenceToken"` + RejectedLogEventsInfo *rejectedLogEventsInfoJSON `json:"rejectedLogEventsInfo,omitempty"` } type outputLogEvent struct { @@ -191,13 +204,16 @@ func isoToEpochMillis(iso string) int64 { return t.UnixMilli() } -func toLogGroupJSON(info *logdriver.LogGroupInfo) logGroupJSON { +func toLogGroupJSON(g *logdriver.AWSLogGroup) logGroupJSON { return logGroupJSON{ - LogGroupName: info.Name, - Arn: info.ResourceID, - CreationTime: isoToEpochMillis(info.CreatedAt), - RetentionInDays: int32(info.RetentionDays), //nolint:gosec // retention days is a small positive value - StoredBytes: info.StoredBytes, + LogGroupName: g.Name, + Arn: g.ResourceID + ":*", + LogGroupArn: g.ResourceID, + CreationTime: isoToEpochMillis(g.CreatedAt), + RetentionInDays: int32(g.RetentionDays), //nolint:gosec // retention days is a small positive value + StoredBytes: g.StoredBytes, + KmsKeyID: g.KmsKeyID, + LogGroupClass: g.LogGroupClass, } } diff --git a/services/logging/driver/aws.go b/services/logging/driver/aws.go new file mode 100644 index 000000000..6082640a5 --- /dev/null +++ b/services/logging/driver/aws.go @@ -0,0 +1,77 @@ +package driver + +import "context" + +// Log group classes CloudWatch Logs accepts on CreateLogGroup. A group's class +// is fixed at creation; no API changes it afterwards. +const ( + LogGroupClassStandard = "STANDARD" + LogGroupClassInfrequentAccess = "INFREQUENT_ACCESS" + LogGroupClassDelivery = "DELIVERY" +) + +// AWSLogGroupOptions carries the CreateLogGroup fields that only CloudWatch +// Logs has: the KMS key that encrypts ingested events and the log group class. +type AWSLogGroupOptions struct { + KmsKeyID string + LogGroupClass string +} + +// AWSLogGroup is a log group with its CloudWatch Logs-only attributes. +type AWSLogGroup struct { + LogGroupInfo + KmsKeyID string + LogGroupClass string +} + +// LogGroupTagFilter matches log groups that carry Key with any of Values (or +// any value when Values is empty). +type LogGroupTagFilter struct { + Key string + Values []string +} + +// ListLogGroupsFilter narrows ListAWSLogGroups. Every non-zero field must +// match. NamePattern uses the ListLogGroups logGroupNamePattern syntax. +type ListLogGroupsFilter struct { + NamePattern string + LogGroupClass string + Tags []LogGroupTagFilter +} + +// RejectedLogEventsInfo reports which events of a PutLogEvents batch were +// dropped. Each index is nil when no event fell in that category. The "end" +// indexes are exclusive ends of a rejected prefix; TooNewLogEventStartIndex is +// the first index of a rejected suffix. +type RejectedLogEventsInfo struct { + TooOldLogEventEndIndex *int + ExpiredLogEventEndIndex *int + TooNewLogEventStartIndex *int +} + +// PutLogEventsResult is the CloudWatch Logs PutLogEvents response. +type PutLogEventsResult struct { + NextSequenceToken string + // Rejected is nil when every event in the batch was accepted. + Rejected *RejectedLogEventsInfo +} + +// AWSLogging is an optional interface implemented by the AWS CloudWatch Logs +// backend for operations with no cross-provider equivalent. The CloudWatch +// Logs wire handler type-asserts for it; Azure, GCP and OCI backends do not +// implement it. +type AWSLogging interface { + CreateAWSLogGroup(ctx context.Context, cfg *LogGroupConfig, opts AWSLogGroupOptions) (*AWSLogGroup, error) + ListAWSLogGroups(ctx context.Context, filter ListLogGroupsFilter) ([]AWSLogGroup, error) + + PutRetentionPolicy(ctx context.Context, logGroup string, retentionDays int) error + DeleteRetentionPolicy(ctx context.Context, logGroup string) error + + PutLogEventsWithResult(ctx context.Context, logGroup, stream string, events []LogEvent) (*PutLogEventsResult, error) + + // AssociateKmsKey and DisassociateKmsKey target exactly one of a log group + // name or a resource identifier (a log group ARN, or the account's + // query-result ARN). + AssociateKmsKey(ctx context.Context, logGroup, resourceIdentifier, kmsKeyID string) error + DisassociateKmsKey(ctx context.Context, logGroup, resourceIdentifier string) error +} diff --git a/services/logging/logging_test.go b/services/logging/logging_test.go index 06115f767..e0c315e7c 100644 --- a/services/logging/logging_test.go +++ b/services/logging/logging_test.go @@ -172,7 +172,7 @@ func TestListLogStreams(t *testing.T) { func TestPutLogEvents(t *testing.T) { ctx := context.Background() - baseTime := time.Date(2025, 6, 1, 12, 0, 0, 0, time.UTC) + baseTime := time.Date(2024, 12, 31, 21, 0, 0, 0, time.UTC) // 3h before the mock clock, inside the ingest window t.Run("success", func(t *testing.T) { l := newTestLogging() @@ -194,7 +194,7 @@ func TestPutLogEvents(t *testing.T) { func TestGetLogEvents(t *testing.T) { ctx := context.Background() - baseTime := time.Date(2025, 6, 1, 12, 0, 0, 0, time.UTC) + baseTime := time.Date(2024, 12, 31, 21, 0, 0, 0, time.UTC) // 3h before the mock clock, inside the ingest window t.Run("success", func(t *testing.T) { l := newTestLogging() @@ -396,7 +396,7 @@ func TestAllOperationsRecorded(t *testing.T) { ctx := context.Background() rec := recorder.New() l := newTestLogging(WithRecorder(rec)) - baseTime := time.Date(2025, 6, 1, 12, 0, 0, 0, time.UTC) + baseTime := time.Date(2024, 12, 31, 21, 0, 0, 0, time.UTC) // 3h before the mock clock, inside the ingest window _, _ = l.CreateLogGroup(ctx, driver.LogGroupConfig{Name: "grp"}) _, _ = l.GetLogGroup(ctx, "grp") @@ -478,7 +478,7 @@ func TestListLogStreamsError(t *testing.T) { func TestFilterLogEventsPortable(t *testing.T) { ctx := context.Background() - baseTime := time.Date(2025, 6, 1, 12, 0, 0, 0, time.UTC) + baseTime := time.Date(2024, 12, 31, 21, 0, 0, 0, time.UTC) // 3h before the mock clock, inside the ingest window t.Run("success", func(t *testing.T) { l := newTestLogging() @@ -611,7 +611,7 @@ func TestAllOperationsMetrics(t *testing.T) { ctx := context.Background() mc := metrics.NewCollector() l := newTestLogging(WithMetrics(mc)) - baseTime := time.Date(2025, 6, 1, 12, 0, 0, 0, time.UTC) + baseTime := time.Date(2024, 12, 31, 21, 0, 0, 0, time.UTC) // 3h before the mock clock, inside the ingest window _, _ = l.CreateLogGroup(ctx, driver.LogGroupConfig{Name: "grp"}) _, _ = l.GetLogGroup(ctx, "grp") From a695e809819517a08f6ca81ea7e78292d6b12d56 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Patel Date: Thu, 22 Oct 2026 04:43:22 +0530 Subject: [PATCH 03/31] fix(aws-cloudwatchlogs): filter pattern language for log, metric and subscription filters (#1656) Parse unstructured, regex and JSON filter patterns instead of substring matching, validate them at PutMetricFilter/PutSubscriptionFilter, enforce the 5-regex-filters-per-group and wildcard quotas, and add TestMetricFilter. Part of #1476. --- docs/coverage/aws/cloudwatchlogs.md | 1 + docs/coverage/coverage.json | 4 + .../aws/cloudwatchlogs/cloudwatchlogs.go | 40 ++- .../aws/cloudwatchlogs/filter_patterns.go | 111 +++++++ .../cloudwatchlogs/filter_patterns_test.go | 213 +++++++++++++ .../cloudwatchlogs/filterpattern/json_eval.go | 227 +++++++++++++ .../filterpattern/json_lexer.go | 241 ++++++++++++++ .../filterpattern/json_parse.go | 299 ++++++++++++++++++ .../cloudwatchlogs/filterpattern/pattern.go | 109 +++++++ .../filterpattern/pattern_test.go | 182 +++++++++++ .../aws/cloudwatchlogs/filterpattern/regex.go | 49 +++ .../aws/cloudwatchlogs/filterpattern/terms.go | 176 +++++++++++ .../cloudwatchlogs/subscription_filters.go | 19 +- .../cloudwatchlogs/filter_pattern_sdk_test.go | 145 +++++++++ server/aws/cloudwatchlogs/handler.go | 4 +- .../cloudwatchlogs/metric_filter_tester.go | 51 +++ services/logging/driver/aws.go | 11 + 17 files changed, 1860 insertions(+), 22 deletions(-) create mode 100644 providers/aws/cloudwatchlogs/filter_patterns.go create mode 100644 providers/aws/cloudwatchlogs/filter_patterns_test.go create mode 100644 providers/aws/cloudwatchlogs/filterpattern/json_eval.go create mode 100644 providers/aws/cloudwatchlogs/filterpattern/json_lexer.go create mode 100644 providers/aws/cloudwatchlogs/filterpattern/json_parse.go create mode 100644 providers/aws/cloudwatchlogs/filterpattern/pattern.go create mode 100644 providers/aws/cloudwatchlogs/filterpattern/pattern_test.go create mode 100644 providers/aws/cloudwatchlogs/filterpattern/regex.go create mode 100644 providers/aws/cloudwatchlogs/filterpattern/terms.go create mode 100644 server/aws/cloudwatchlogs/filter_pattern_sdk_test.go create mode 100644 server/aws/cloudwatchlogs/metric_filter_tester.go diff --git a/docs/coverage/aws/cloudwatchlogs.md b/docs/coverage/aws/cloudwatchlogs.md index b9bfe0106..98655f009 100644 --- a/docs/coverage/aws/cloudwatchlogs.md +++ b/docs/coverage/aws/cloudwatchlogs.md @@ -42,6 +42,7 @@ AWSLogging is an optional interface implemented by the AWS CloudWatch Logs | `ListAWSLogGroups` | | | `PutLogEventsWithResult` | | | `PutRetentionPolicy` | | +| `TestMetricFilter` | TestMetricFilter runs a filter pattern over sample log messages. | ## Not in scope diff --git a/docs/coverage/coverage.json b/docs/coverage/coverage.json index e2552a9e6..90ae49f3a 100644 --- a/docs/coverage/coverage.json +++ b/docs/coverage/coverage.json @@ -11583,6 +11583,10 @@ }, { "name": "PutRetentionPolicy" + }, + { + "name": "TestMetricFilter", + "doc": "TestMetricFilter runs a filter pattern over sample log messages." } ] }, diff --git a/providers/aws/cloudwatchlogs/cloudwatchlogs.go b/providers/aws/cloudwatchlogs/cloudwatchlogs.go index 001bdb31e..a2619d267 100644 --- a/providers/aws/cloudwatchlogs/cloudwatchlogs.go +++ b/providers/aws/cloudwatchlogs/cloudwatchlogs.go @@ -13,6 +13,7 @@ import ( "github.com/stackshy/cloudemu/v2/config" "github.com/stackshy/cloudemu/v2/errors" "github.com/stackshy/cloudemu/v2/internal/memstore" + "github.com/stackshy/cloudemu/v2/providers/aws/cloudwatchlogs/filterpattern" "github.com/stackshy/cloudemu/v2/services/logging/driver" mondriver "github.com/stackshy/cloudemu/v2/services/monitoring/driver" "github.com/stackshy/cloudemu/v2/services/scope" @@ -222,14 +223,8 @@ func (m *Mock) PutLogEvents(ctx context.Context, groupName, streamName string, e // For each metric filter on the group, every event whose message matches the // filter pattern contributes the filter's metric value; the total is published // to the filter's configured namespace/metric, mirroring how CloudWatch Logs -// extracts custom metrics from ingested events. -// -// Pattern matching only supports plain-substring "count occurrences of a -// term" matching (strings.Contains). Real CloudWatch Logs filter pattern -// syntax โ€” quoted phrases, JSON-field patterns (e.g. { $.level = "ERROR" }), -// ?OR-style alternation, and -exclusion terms โ€” is not parsed, so a more -// elaborate real filter pattern will not behave like real CloudWatch Logs -// here. +// extracts custom metrics from ingested events. Patterns use the CloudWatch +// Logs filter pattern language (see the filterpattern package). func (m *Mock) evaluateMetricFilters(g *logGroup, events []driver.LogEvent) { if m.monitoring == nil { return @@ -241,8 +236,15 @@ func (m *Mock) evaluateMetricFilters(g *logGroup, events []driver.LogEvent) { total float64 ) + // Patterns are validated when the filter is put; one that no longer + // parses (restored from an older snapshot) matches nothing. + pattern, err := filterpattern.Parse(mf.FilterPattern) + if err != nil { + continue + } + for i := range events { - if mf.FilterPattern == "" || strings.Contains(events[i].Message, mf.FilterPattern) { + if pattern.Match(events[i].Message) { matched++ total += metricFilterValue(mf.MetricValue) } @@ -376,6 +378,11 @@ func (m *Mock) FilterLogEvents( ) } + pattern, err := filterpattern.Parse(input.FilterPattern) + if err != nil { + return nil, err + } + m.purgeExpired(input.LogGroup, g) limit := input.Limit @@ -386,10 +393,10 @@ func (m *Mock) FilterLogEvents( var results []driver.FilteredLogEvent if input.LogStream != "" { - results = m.filterStreamEvents(g, input.LogStream, input) + results = m.filterStreamEvents(g, input.LogStream, input, pattern) } else { for name, s := range g.streams.All() { - results = append(results, m.matchEvents(s, name, input)...) + results = append(results, m.matchEvents(s, name, input, pattern)...) } // CloudWatch Logs interleaves matches across streams in ascending @@ -422,19 +429,21 @@ func (m *Mock) filterStreamEvents( g *logGroup, streamName string, input *driver.FilterLogEventsInput, + pattern *filterpattern.Pattern, ) []driver.FilteredLogEvent { s, ok := g.streams.Get(streamName) if !ok { return nil } - return m.matchEvents(s, streamName, input) + return m.matchEvents(s, streamName, input, pattern) } func (*Mock) matchEvents( s *logStream, streamName string, input *driver.FilterLogEventsInput, + pattern *filterpattern.Pattern, ) []driver.FilteredLogEvent { s.mu.RLock() defer s.mu.RUnlock() @@ -450,8 +459,7 @@ func (*Mock) matchEvents( continue } - if input.FilterPattern != "" && - !strings.Contains(e.Message, input.FilterPattern) { + if !pattern.Match(e.Message) { continue } @@ -484,6 +492,10 @@ func (m *Mock) PutMetricFilter( ) } + if err := validateFilterPattern(g, metricFilterKind, cfg.Name, cfg.FilterPattern); err != nil { + return err + } + info := &driver.MetricFilterInfo{ Name: cfg.Name, LogGroup: cfg.LogGroup, diff --git a/providers/aws/cloudwatchlogs/filter_patterns.go b/providers/aws/cloudwatchlogs/filter_patterns.go new file mode 100644 index 000000000..6a6e2f13e --- /dev/null +++ b/providers/aws/cloudwatchlogs/filter_patterns.go @@ -0,0 +1,111 @@ +package cloudwatchlogs + +import ( + "context" + + "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/providers/aws/cloudwatchlogs/filterpattern" + "github.com/stackshy/cloudemu/v2/services/logging/driver" +) + +const ( + // maxRegexFiltersPerGroup is the number of metric and subscription + // filters, together, a log group may have whose pattern contains a regex. + maxRegexFiltersPerGroup = 5 + // maxTestMetricFilterMessages bounds TestMetricFilter logEventMessages. + maxTestMetricFilterMessages = 50 +) + +func patternHasRegex(pattern string) bool { + p, err := filterpattern.Parse(pattern) + + return err == nil && p.HasRegex() +} + +// filterKind names the filter store a filter lives in, so replacing a filter +// does not count against the regex quota twice. +type filterKind int + +const ( + metricFilterKind filterKind = iota + subscriptionFilterKind +) + +// validateFilterPattern parses a metric or subscription filter pattern and +// enforces the per-group quota on patterns that contain a regex. name and +// kind identify the filter being put; an existing filter with the same name +// and kind is being replaced and is left out of the count. +func validateFilterPattern(g *logGroup, kind filterKind, name, pattern string) error { + p, err := filterpattern.Parse(pattern) + if err != nil { + return err + } + + if !p.HasRegex() { + return nil + } + + if countRegexFilters(g, kind, name) >= maxRegexFiltersPerGroup { + return errors.Newf(errors.ResourceExhausted, + "log group %q already has the maximum of %d metric and subscription filters whose pattern contains a regex", + g.info.Name, maxRegexFiltersPerGroup) + } + + return nil +} + +// TestMetricFilter runs a filter pattern over sample messages and returns the +// ones it matches, numbered from 0 in input order, with the values a JSON +// pattern extracts. +func (*Mock) TestMetricFilter( + _ context.Context, pattern string, messages []string, +) ([]driver.MetricFilterMatch, error) { + if len(messages) == 0 || len(messages) > maxTestMetricFilterMessages { + return nil, errors.Newf(errors.InvalidArgument, + "1 validation error detected: Value at 'logEventMessages' failed to satisfy constraint: "+ + "Member must have length between 1 and %d", maxTestMetricFilterMessages) + } + + p, err := filterpattern.Parse(pattern) + if err != nil { + return nil, err + } + + out := make([]driver.MetricFilterMatch, 0, len(messages)) + + for i, msg := range messages { + if msg == "" { + return nil, errors.Newf(errors.InvalidArgument, + "1 validation error detected: Value '' at 'logEventMessages.%d.member' failed to satisfy constraint: "+ + "Member must have length greater than or equal to 1", i+1) + } + + if p.Match(msg) { + out = append(out, driver.MetricFilterMatch{ + EventNumber: int64(i), EventMessage: msg, ExtractedValues: p.ExtractedValues(msg), + }) + } + } + + return out, nil +} + +// countRegexFilters counts the group's metric and subscription filters whose +// pattern contains a regex, leaving out the filter named name of kind kind. +func countRegexFilters(g *logGroup, kind filterKind, name string) int { + n := 0 + + for fname, mf := range g.metricFilters.All() { + if (kind != metricFilterKind || fname != name) && patternHasRegex(mf.FilterPattern) { + n++ + } + } + + for fname, sf := range g.subFilters.All() { + if (kind != subscriptionFilterKind || fname != name) && patternHasRegex(sf.FilterPattern) { + n++ + } + } + + return n +} diff --git a/providers/aws/cloudwatchlogs/filter_patterns_test.go b/providers/aws/cloudwatchlogs/filter_patterns_test.go new file mode 100644 index 000000000..07f501db7 --- /dev/null +++ b/providers/aws/cloudwatchlogs/filter_patterns_test.go @@ -0,0 +1,213 @@ +package cloudwatchlogs + +import ( + "context" + "fmt" + "sync" + "testing" + "time" + + "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/services/logging/driver" + mondriver "github.com/stackshy/cloudemu/v2/services/monitoring/driver" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// recordingMonitoring captures PutMetricData calls; every other Monitoring +// method is left to the nil embedded interface and must not be called. +type recordingMonitoring struct { + mondriver.Monitoring + + mu sync.Mutex + data []mondriver.MetricDatum +} + +func (r *recordingMonitoring) PutMetricData(_ context.Context, data []mondriver.MetricDatum) error { + r.mu.Lock() + defer r.mu.Unlock() + + r.data = append(r.data, data...) + + return nil +} + +func (r *recordingMonitoring) valueOf(namespace, name string) float64 { + r.mu.Lock() + defer r.mu.Unlock() + + var total float64 + + for _, d := range r.data { + if d.Namespace == namespace && d.MetricName == name { + total += d.Value + } + } + + return total +} + +func putMessages(t *testing.T, m *Mock, msgs ...string) { + t.Helper() + + base := testEventTime() + events := make([]driver.LogEvent, 0, len(msgs)) + + for i, msg := range msgs { + events = append(events, driver.LogEvent{Timestamp: base.Add(time.Duration(i) * time.Second), Message: msg}) + } + + _, err := m.PutLogEventsWithResult(context.Background(), "g", "s", events) + require.NoError(t, err) +} + +func filterMessages(t *testing.T, m *Mock, pattern string) []string { + t.Helper() + + got, err := m.FilterLogEvents(context.Background(), &driver.FilterLogEventsInput{LogGroup: "g", FilterPattern: pattern}) + require.NoError(t, err) + + out := make([]string, 0, len(got)) + for _, e := range got { + out = append(out, e.Message) + } + + return out +} + +// TestFilterLogEventsPatternLanguage is the regression for FilterLogEvents +// treating the whole pattern as one substring. +func TestFilterLogEventsPatternLanguage(t *testing.T) { + m := newWindowMock(t, 0) + putMessages(t, m, + "[ERROR 419] MISSING ARGUMENTS", + "[ERROR 400] BAD REQUEST", + `{"level":"ERROR","latency":50}`, + `{"level":"INFO","latency":5}`, + ) + + assert.Equal(t, []string{"[ERROR 400] BAD REQUEST"}, filterMessages(t, m, "ERROR -ARGUMENTS -level")) + assert.Equal(t, []string{"[ERROR 419] MISSING ARGUMENTS"}, filterMessages(t, m, `"MISSING ARGUMENTS"`)) + assert.Len(t, filterMessages(t, m, "?BAD ?MISSING"), 2) + assert.Equal(t, []string{`{"level":"ERROR","latency":50}`}, filterMessages(t, m, `{ $.level = "ERROR" && $.latency > 10 }`)) + assert.Len(t, filterMessages(t, m, "%ERR.R%"), 3) + + _, err := m.FilterLogEvents(context.Background(), &driver.FilterLogEventsInput{LogGroup: "g", FilterPattern: "{ $.a IS NOT NULL }"}) + require.Error(t, err) + assert.True(t, errors.IsInvalidArgument(err)) +} + +func TestPutFilterValidatesPattern(t *testing.T) { + ctx := context.Background() + m := newWindowMock(t, 0) + + err := m.PutMetricFilter(ctx, &driver.MetricFilterConfig{ + Name: "bad", LogGroup: "g", FilterPattern: "{ $.a EXISTS }", MetricName: "M", MetricNamespace: "N", + }) + require.Error(t, err) + assert.True(t, errors.IsInvalidArgument(err)) + + err = m.PutSubscriptionFilter(ctx, &driver.SubscriptionFilterConfig{ + Name: "bad", LogGroup: "g", FilterPattern: "%(x)%", DestinationARN: lambdaDestARN, + }) + require.Error(t, err) + assert.True(t, errors.IsInvalidArgument(err)) +} + +func TestRegexFilterQuotaPerGroup(t *testing.T) { + ctx := context.Background() + m := newWindowMock(t, 0) + + for i := range 4 { + require.NoError(t, m.PutMetricFilter(ctx, &driver.MetricFilterConfig{ + Name: fmt.Sprintf("mf%d", i), LogGroup: "g", FilterPattern: "%ERR%", MetricName: "M", MetricNamespace: "N", + })) + } + + require.NoError(t, m.PutSubscriptionFilter(ctx, &driver.SubscriptionFilterConfig{ + Name: "sf", LogGroup: "g", FilterPattern: "%ERR%", DestinationARN: lambdaDestARN, + })) + + // A sixth regex pattern, from either kind of filter, is over the quota. + err := m.PutMetricFilter(ctx, &driver.MetricFilterConfig{ + Name: "mf9", LogGroup: "g", FilterPattern: "%WARN%", MetricName: "M", MetricNamespace: "N", + }) + require.Error(t, err) + assert.Equal(t, errors.ResourceExhausted, errors.GetCode(err)) + + // Replacing an existing regex filter, or adding a plain one, is fine. + require.NoError(t, m.PutMetricFilter(ctx, &driver.MetricFilterConfig{ + Name: "mf0", LogGroup: "g", FilterPattern: "%WARN%", MetricName: "M", MetricNamespace: "N", + })) + require.NoError(t, m.PutMetricFilter(ctx, &driver.MetricFilterConfig{ + Name: "plain", LogGroup: "g", FilterPattern: "WARN", MetricName: "M", MetricNamespace: "N", + })) +} + +// TestSubscriptionFilterUsesPatternLanguage delivers only events a JSON +// pattern matches, where substring matching would have delivered none. +func TestSubscriptionFilterUsesPatternLanguage(t *testing.T) { + ctx := context.Background() + m := newWindowMock(t, 0) + inv := &recordingInvoker{} + m.SetLambdaInvoker(inv) + + require.NoError(t, m.PutSubscriptionFilter(ctx, &driver.SubscriptionFilterConfig{ + Name: "errors", LogGroup: "g", FilterPattern: `{ $.level = "ERROR" }`, DestinationARN: lambdaDestARN, + })) + + putMessages(t, m, `{"level":"ERROR","msg":"boom"}`, `{"level":"INFO"}`) + + inv.mu.Lock() + defer inv.mu.Unlock() + + require.Len(t, inv.payloads, 1) + + ev := decodeAWSLogs(t, inv.payloads[0]) + require.Len(t, ev.LogEvents, 1) + assert.Equal(t, `{"level":"ERROR","msg":"boom"}`, ev.LogEvents[0].Message) +} + +// TestMetricFilterUsesPatternLanguage counts events with a ?OR pattern, which +// substring matching never matched. +func TestMetricFilterUsesPatternLanguage(t *testing.T) { + ctx := context.Background() + m := newWindowMock(t, 0) + mon := &recordingMonitoring{} + m.SetMonitoring(mon) + + require.NoError(t, m.PutMetricFilter(ctx, &driver.MetricFilterConfig{ + Name: "errs", LogGroup: "g", FilterPattern: "?ERROR ?FATAL", MetricName: "Errors", MetricNamespace: "App", + })) + + putMessages(t, m, "ERROR a", "FATAL b", "INFO c") + + assert.InDelta(t, 2.0, mon.valueOf("App", "Errors"), 0) +} + +func TestTestMetricFilter(t *testing.T) { + ctx := context.Background() + m := newTestMock() + + got, err := m.TestMetricFilter(ctx, `{ $.latency > 10 }`, []string{`{"latency":5}`, `{"latency":50}`, "plain"}) + require.NoError(t, err) + require.Len(t, got, 1) + assert.Equal(t, int64(1), got[0].EventNumber) + assert.Equal(t, map[string]string{"$.latency": "50"}, got[0].ExtractedValues) + + got, err = m.TestMetricFilter(ctx, `"[ERROR]" Exception`, []string{"[INFO] ok", "[ERROR] InvalidQueryException", "[ERROR] stop"}) + require.NoError(t, err) + require.Len(t, got, 1) + assert.Equal(t, int64(1), got[0].EventNumber) + assert.Empty(t, got[0].ExtractedValues) + + for _, bad := range [][]string{nil, {""}} { + _, err = m.TestMetricFilter(ctx, "x", bad) + require.Error(t, err) + assert.True(t, errors.IsInvalidArgument(err)) + } + + _, err = m.TestMetricFilter(ctx, "{ $.a IS NOT NULL }", []string{"x"}) + require.Error(t, err) + assert.True(t, errors.IsInvalidArgument(err)) +} diff --git a/providers/aws/cloudwatchlogs/filterpattern/json_eval.go b/providers/aws/cloudwatchlogs/filterpattern/json_eval.go new file mode 100644 index 000000000..0bb7af7ca --- /dev/null +++ b/providers/aws/cloudwatchlogs/filterpattern/json_eval.go @@ -0,0 +1,227 @@ +package filterpattern + +import ( + "encoding/json" + "regexp" + "strconv" +) + +// valueSpec is the right-hand side of a comparison. +type valueSpec struct { + text string + re *regexp.Regexp // %regex% value + glob *regexp.Regexp // literal or * wildcard value + isNum bool + num float64 +} + +type cmpNode struct { + sel token + op string + val *valueSpec +} + +func (n cmpNode) selectors() []token { return []token{n.sel} } + +// eval is true when any value the selector reaches satisfies the comparison. +// A selector that reaches nothing, or only objects and arrays, never matches. +func (n cmpNode) eval(doc any) bool { + for _, v := range resolve(doc, n.sel.steps) { + if isScalar(v) && n.holds(v) { + return true + } + } + + return false +} + +func (n cmpNode) holds(v any) bool { + switch n.op { + case opEq: + return n.equals(v) + case opNe: + return !n.equals(v) + } + + f, ok := v.(float64) + if !ok { + return false + } + + switch n.op { + case "<": + return f < n.val.num + case "<=": + return f <= n.val.num + case ">": + return f > n.val.num + default: + return f >= n.val.num + } +} + +func (n cmpNode) equals(v any) bool { + if n.val.re != nil { + return n.val.re.MatchString(scalarString(v)) + } + + if f, ok := v.(float64); ok && n.val.isNum { + return f == n.val.num + } + + return n.val.glob.MatchString(scalarString(v)) +} + +type isNode struct { + sel token + kind string +} + +func (n isNode) selectors() []token { return []token{n.sel} } + +func (n isNode) eval(doc any) bool { + for _, v := range resolve(doc, n.sel.steps) { + switch n.kind { + case kwNull: + if v == nil { + return true + } + default: + if b, ok := v.(bool); ok && b == (n.kind == kwTrue) { + return true + } + } + } + + return false +} + +type notExistsNode struct{ sel token } + +func (n notExistsNode) selectors() []token { return []token{n.sel} } + +func (n notExistsNode) eval(doc any) bool { return len(resolve(doc, n.sel.steps)) == 0 } + +// match parses the message as JSON; a message that is not a JSON document +// never matches a JSON pattern. +func (p *jsonPattern) match(message string) bool { + doc, ok := decodeJSON(message) + + return ok && p.root.eval(doc) +} + +// extract returns the scalar each non-wildcard selector reaches in message. +func (p *jsonPattern) extract(message string) map[string]string { + out := map[string]string{} + + doc, ok := decodeJSON(message) + if !ok { + return out + } + + for _, sel := range p.root.selectors() { + vals := resolve(doc, sel.steps) + if len(vals) == 1 && isScalar(vals[0]) && !hasWildcard(sel.steps) { + out[sel.text] = scalarString(vals[0]) + } + } + + return out +} + +func decodeJSON(message string) (any, bool) { + var doc any + if err := json.Unmarshal([]byte(message), &doc); err != nil { + return nil, false + } + + return doc, true +} + +// resolve walks the selector steps from doc and returns every value reached. +// A JSON null that is present is returned as nil, so it differs from a +// missing property, which contributes nothing. +func resolve(doc any, steps []step) []any { + cur := []any{doc} + + for _, st := range steps { + next := make([]any, 0, len(cur)) + + for _, v := range cur { + next = append(next, applyStep(v, st)...) + } + + cur = next + } + + return cur +} + +func applyStep(v any, st step) []any { + switch st.kind { + case stepField: + if m, ok := v.(map[string]any); ok { + if child, found := m[st.field]; found { + return []any{child} + } + } + case stepIndex: + if a, ok := v.([]any); ok && st.index < len(a) { + return []any{a[st.index]} + } + case stepWildcard: + return children(v) + } + + return nil +} + +func children(v any) []any { + switch c := v.(type) { + case map[string]any: + out := make([]any, 0, len(c)) + for _, x := range c { + out = append(out, x) + } + + return out + case []any: + return c + } + + return nil +} + +func hasWildcard(steps []step) bool { + for _, st := range steps { + if st.kind == stepWildcard { + return true + } + } + + return false +} + +func isScalar(v any) bool { + switch v.(type) { + case map[string]any, []any: + return false + } + + return true +} + +func scalarString(v any) string { + switch x := v.(type) { + case string: + return x + case float64: + return strconv.FormatFloat(x, 'f', -1, 64) + case bool: + return strconv.FormatBool(x) + case nil: + return "null" + } + + return "" +} diff --git a/providers/aws/cloudwatchlogs/filterpattern/json_lexer.go b/providers/aws/cloudwatchlogs/filterpattern/json_lexer.go new file mode 100644 index 000000000..4adc13532 --- /dev/null +++ b/providers/aws/cloudwatchlogs/filterpattern/json_lexer.go @@ -0,0 +1,241 @@ +package filterpattern + +import ( + "strconv" + "strings" +) + +type tokenKind int + +const ( + tokLParen tokenKind = iota + tokRParen + tokAnd + tokOr + tokOp + tokSelector + tokString + tokRegex + tokWord +) + +type token struct { + kind tokenKind + text string + // steps is set for tokSelector. + steps []step +} + +// stepKind is one hop of a JSON property selector. +type stepKind int + +const ( + stepField stepKind = iota + stepIndex + stepWildcard +) + +type step struct { + kind stepKind + field string + index int +} + +// wordStop ends a bare word inside a JSON pattern. +const wordStop = " \t()&|=!<>" + +// twoCharToken is the width of &&, || and the two-character comparisons. +const twoCharToken = 2 + +// lexJSON splits the body of a JSON pattern (the text between the braces) +// into tokens. +func lexJSON(s string) ([]token, error) { + var toks []token + + for i := 0; i < len(s); { + c := s[i] + + if c == ' ' || c == '\t' { + i++ + continue + } + + tok, next, err := lexOne(s, i) + if err != nil { + return nil, err + } + + toks = append(toks, tok) + i = next + } + + return toks, nil +} + +func lexOne(s string, i int) (token, int, error) { + switch s[i] { + case '(': + return token{kind: tokLParen, text: "("}, i + 1, nil + case ')': + return token{kind: tokRParen, text: ")"}, i + 1, nil + case '&', '|': + return lexLogical(s, i) + case '=', '!', '<', '>': + return lexOperator(s, i) + case '$': + return lexSelector(s, i) + case '"': + return lexQuoted(s, i) + case '%': + return lexRegex(s, i) + } + + end := strings.IndexAny(s[i:], wordStop) + if end < 0 { + end = len(s) - i + } + + return token{kind: tokWord, text: s[i : i+end]}, i + end, nil +} + +func lexLogical(s string, i int) (token, int, error) { + c := s[i] + if i+1 >= len(s) || s[i+1] != c { + return token{}, 0, invalid("Invalid filter pattern: expected '" + string([]byte{c, c}) + "'") + } + + if c == '&' { + return token{kind: tokAnd, text: "&&"}, i + twoCharToken, nil + } + + return token{kind: tokOr, text: "||"}, i + twoCharToken, nil +} + +func lexRegex(s string, i int) (token, int, error) { + end := strings.IndexByte(s[i+1:], '%') + if end < 0 { + return token{}, 0, invalid("Invalid filter pattern: unterminated regex") + } + + return token{kind: tokRegex, text: s[i+1 : i+1+end]}, i + end + twoCharToken, nil +} + +func lexOperator(s string, i int) (token, int, error) { + if i+1 < len(s) && s[i+1] == '=' && s[i] != '=' { + return token{kind: tokOp, text: s[i : i+twoCharToken]}, i + twoCharToken, nil + } + + if s[i] == '!' { + return token{}, 0, invalid("Invalid filter pattern: expected '!='") + } + + return token{kind: tokOp, text: s[i : i+1]}, i + 1, nil +} + +func lexQuoted(s string, i int) (token, int, error) { + var b strings.Builder + + for j := i + 1; j < len(s); j++ { + switch s[j] { + case '\\': + if j+1 < len(s) { + j++ + b.WriteByte(s[j]) + } + case '"': + return token{kind: tokString, text: b.String()}, j + 1, nil + default: + b.WriteByte(s[j]) + } + } + + return token{}, 0, invalid("Invalid filter pattern: unterminated quoted string") +} + +// lexSelector reads a property selector such as $.a.b[0], $.*, $.list[*] or +// $.['dotted.key']. +func lexSelector(s string, start int) (token, int, error) { + var steps []step + + i := start + 1 + + for i < len(s) { + var ( + st step + err error + ) + + switch { + case s[i] == '.' && i+1 < len(s) && s[i+1] == '[': + st, i, err = lexBracket(s, i+1) + case s[i] == '.': + st, i, err = lexDotStep(s, i+1) + case s[i] == '[': + st, i, err = lexBracket(s, i) + default: + return selectorToken(s, start, i, steps) + } + + if err != nil { + return token{}, 0, err + } + + steps = append(steps, st) + } + + return selectorToken(s, start, i, steps) +} + +func selectorToken(s string, start, end int, steps []step) (token, int, error) { + if len(steps) == 0 { + return token{}, 0, invalid("Invalid property selector '" + s[start:end] + "'") + } + + return token{kind: tokSelector, text: s[start:end], steps: steps}, end, nil +} + +func lexDotStep(s string, i int) (step, int, error) { + if i < len(s) && s[i] == '*' { + return step{kind: stepWildcard}, i + 1, nil + } + + j := i + for j < len(s) && isSelectorChar(s[j]) { + j++ + } + + if j == i { + return step{}, 0, invalid("Invalid property selector near '" + s[i-1:] + "'") + } + + return step{kind: stepField, field: s[i:j]}, j, nil +} + +func isSelectorChar(c byte) bool { + return c == '_' || c == '-' || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') +} + +// lexBracket reads [n], [*] or ['key'] starting at the '['. +func lexBracket(s string, i int) (step, int, error) { + end := strings.IndexByte(s[i:], ']') + if end < 0 { + return step{}, 0, invalid("Invalid property selector: missing ']'") + } + + inner := s[i+1 : i+end] + next := i + end + 1 + + switch { + case inner == "*": + return step{kind: stepWildcard}, next, nil + case len(inner) >= 2 && inner[0] == '\'' && inner[len(inner)-1] == '\'': + return step{kind: stepField, field: inner[1 : len(inner)-1]}, next, nil + } + + n, err := strconv.Atoi(inner) + if err != nil || n < 0 { + return step{}, 0, invalid("Invalid property selector index [" + inner + "]") + } + + return step{kind: stepIndex, index: n}, next, nil +} diff --git a/providers/aws/cloudwatchlogs/filterpattern/json_parse.go b/providers/aws/cloudwatchlogs/filterpattern/json_parse.go new file mode 100644 index 000000000..fed9cefac --- /dev/null +++ b/providers/aws/cloudwatchlogs/filterpattern/json_parse.go @@ -0,0 +1,299 @@ +package filterpattern + +import ( + "regexp" + "strconv" + "strings" +) + +// Operators and keywords of the JSON pattern language. +const ( + opEq = "=" + opNe = "!=" + kwIs = "IS" + kwNot = "NOT" + kwExists = "EXISTS" + kwNull = "NULL" + kwTrue = "TRUE" + kwFalse = "FALSE" +) + +// jsonNode is one node of a parsed JSON pattern expression. +type jsonNode interface { + eval(doc any) bool + selectors() []token +} + +type orNode struct{ l, r jsonNode } + +func (n orNode) eval(doc any) bool { return n.l.eval(doc) || n.r.eval(doc) } + +func (n orNode) selectors() []token { return append(n.l.selectors(), n.r.selectors()...) } + +type andNode struct{ l, r jsonNode } + +func (n andNode) eval(doc any) bool { return n.l.eval(doc) && n.r.eval(doc) } + +func (n andNode) selectors() []token { return append(n.l.selectors(), n.r.selectors()...) } + +// jsonPattern is a parsed { ... } pattern. +type jsonPattern struct { + root jsonNode + regexCount int +} + +type jsonParser struct { + toks []token + pos int + regexes int + wildcards int +} + +func parseJSONPattern(s string) (*jsonPattern, error) { + if !strings.HasSuffix(s, "}") { + return nil, invalid("Invalid filter pattern: JSON pattern must end with '}'") + } + + toks, err := lexJSON(s[1 : len(s)-1]) + if err != nil { + return nil, err + } + + if len(toks) == 0 { + return nil, invalid("Invalid filter pattern: empty JSON pattern") + } + + p := &jsonParser{toks: toks} + + root, err := p.parseOr() + if err != nil { + return nil, err + } + + if p.pos != len(p.toks) { + return nil, invalid("Invalid filter pattern: unexpected '" + p.toks[p.pos].text + "'") + } + + if p.regexes > maxRegexPerStructuredPattern { + return nil, invalid("Invalid filter pattern: at most 2 regular expressions are allowed in a JSON filter pattern") + } + + if p.wildcards > maxWildcardsPerPattern { + return nil, invalid("Invalid filter pattern: at most 3 wildcard selectors are allowed in a filter pattern") + } + + return &jsonPattern{root: root, regexCount: p.regexes}, nil +} + +func (p *jsonParser) peek() *token { + if p.pos >= len(p.toks) { + return nil + } + + return &p.toks[p.pos] +} + +func (p *jsonParser) next() *token { + t := p.peek() + if t != nil { + p.pos++ + } + + return t +} + +// parseOr and parseAnd give && higher precedence than ||, and parentheses +// the highest, as documented. +func (p *jsonParser) parseOr() (jsonNode, error) { + left, err := p.parseAnd() + if err != nil { + return nil, err + } + + for t := p.peek(); t != nil && t.kind == tokOr; t = p.peek() { + p.pos++ + + right, rerr := p.parseAnd() + if rerr != nil { + return nil, rerr + } + + left = orNode{l: left, r: right} + } + + return left, nil +} + +func (p *jsonParser) parseAnd() (jsonNode, error) { + left, err := p.parseUnary() + if err != nil { + return nil, err + } + + for t := p.peek(); t != nil && t.kind == tokAnd; t = p.peek() { + p.pos++ + + right, rerr := p.parseUnary() + if rerr != nil { + return nil, rerr + } + + left = andNode{l: left, r: right} + } + + return left, nil +} + +func (p *jsonParser) parseUnary() (jsonNode, error) { + t := p.peek() + if t == nil { + return nil, invalid("Invalid filter pattern: expression expected") + } + + if t.kind != tokLParen { + return p.parseCondition() + } + + p.pos++ + + inner, err := p.parseOr() + if err != nil { + return nil, err + } + + if c := p.next(); c == nil || c.kind != tokRParen { + return nil, invalid("Invalid filter pattern: missing ')'") + } + + return inner, nil +} + +// parseCondition parses `selector op value`, `selector IS NULL|TRUE|FALSE` or +// `selector NOT EXISTS`. `IS NOT` and a bare `EXISTS` are not part of the +// language and are rejected. +func (p *jsonParser) parseCondition() (jsonNode, error) { + sel := p.next() + if sel.kind != tokSelector { + return nil, invalid("Invalid filter pattern: property selector expected, got '" + sel.text + "'") + } + + if err := p.countWildcards(sel); err != nil { + return nil, err + } + + t := p.next() + if t == nil { + return nil, invalid("Invalid filter pattern: operator expected after " + sel.text) + } + + if t.kind == tokOp { + return p.parseComparison(sel, t.text) + } + + if t.kind == tokWord { + return p.parseKeyword(sel, strings.ToUpper(t.text)) + } + + return nil, invalid("Invalid filter pattern: operator expected after " + sel.text) +} + +func (p *jsonParser) countWildcards(sel *token) error { + n := 0 + + for _, st := range sel.steps { + if st.kind == stepWildcard { + n++ + } + } + + if n > maxWildcardsPerSelector { + return invalid("Invalid filter pattern: only one wildcard is allowed in property selector " + sel.text) + } + + p.wildcards += n + + return nil +} + +func (p *jsonParser) parseKeyword(sel *token, kw string) (jsonNode, error) { + switch kw { + case kwIs: + v := p.next() + if v == nil { + return nil, invalid("Invalid filter pattern: NULL, TRUE or FALSE expected after IS") + } + + switch kind := strings.ToUpper(v.text); kind { + case kwNull, kwTrue, kwFalse: + return isNode{sel: *sel, kind: kind}, nil + case kwNot: + return nil, invalid("Invalid filter pattern: IS NOT is not supported") + default: + return nil, invalid("Invalid filter pattern: NULL, TRUE or FALSE expected after IS") + } + case kwNot: + if v := p.next(); v == nil || !strings.EqualFold(v.text, kwExists) { + return nil, invalid("Invalid filter pattern: EXISTS expected after NOT") + } + + return notExistsNode{sel: *sel}, nil + case kwExists: + return nil, invalid("Invalid filter pattern: EXISTS is not supported; use NOT EXISTS") + } + + return nil, invalid("Invalid filter pattern: operator expected after " + sel.text) +} + +func (p *jsonParser) parseComparison(sel *token, op string) (jsonNode, error) { + t := p.next() + if t == nil || (t.kind != tokString && t.kind != tokRegex && t.kind != tokWord) { + return nil, invalid("Invalid filter pattern: value expected after " + sel.text + " " + op) + } + + v, err := p.newValue(t) + if err != nil { + return nil, err + } + + if op != opEq && op != opNe && !v.isNum { + return nil, invalid("Invalid filter pattern: " + op + " requires a number, got '" + t.text + "'") + } + + return cmpNode{sel: *sel, op: op, val: v}, nil +} + +func (p *jsonParser) newValue(t *token) (*valueSpec, error) { + v := &valueSpec{text: t.text} + + if t.kind == tokRegex { + re, err := compileRegex(t.text) + if err != nil { + return nil, err + } + + p.regexes++ + v.re = re + + return v, nil + } + + if t.kind == tokWord { + if n, err := strconv.ParseFloat(t.text, 64); err == nil && !strings.Contains(t.text, "*") { + v.isNum, v.num = true, n + } + } + + v.glob = globRegexp(t.text) + + return v, nil +} + +// globRegexp turns a value with * wildcards into an anchored regexp. +func globRegexp(s string) *regexp.Regexp { + parts := strings.Split(s, "*") + for i := range parts { + parts[i] = regexp.QuoteMeta(parts[i]) + } + + return regexp.MustCompile("^" + strings.Join(parts, ".*") + "$") +} diff --git a/providers/aws/cloudwatchlogs/filterpattern/pattern.go b/providers/aws/cloudwatchlogs/filterpattern/pattern.go new file mode 100644 index 000000000..c4b34ea17 --- /dev/null +++ b/providers/aws/cloudwatchlogs/filterpattern/pattern.go @@ -0,0 +1,109 @@ +// Package filterpattern implements the CloudWatch Logs filter pattern +// language used by FilterLogEvents, metric filters, subscription filters and +// TestMetricFilter: unstructured terms, %regex% terms, and JSON property +// patterns. See +// https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/FilterAndPatternSyntax.html. +// +// Space-delimited patterns ([a, b, ...]) are not supported yet and are +// rejected rather than matched as plain text. +package filterpattern + +import ( + "strings" + "unicode/utf8" + + "github.com/stackshy/cloudemu/v2/errors" +) + +// Limits from the filter pattern syntax reference. +const ( + // maxPatternLength is the longest filterPattern the API accepts. + maxPatternLength = 1024 + // maxRegexPerStructuredPattern caps %regex% values in one JSON pattern. + maxRegexPerStructuredPattern = 2 + // maxWildcardsPerSelector caps * selectors in one property selector. + maxWildcardsPerSelector = 1 + // maxWildcardsPerPattern caps * selectors across a compound JSON pattern. + maxWildcardsPerPattern = 3 +) + +// matcher is one parsed pattern form. +type matcher interface { + match(message string) bool +} + +// Pattern is a parsed filter pattern. The zero value is not usable; build one +// with Parse. +type Pattern struct { + m matcher + hasRegex bool + json *jsonPattern +} + +// Parse parses a filter pattern. An empty or blank pattern matches every +// event. A malformed pattern is an InvalidArgument error, which the wire layer +// reports as InvalidParameterException. +func Parse(raw string) (*Pattern, error) { + if len(raw) > maxPatternLength { + return nil, errors.Newf(errors.InvalidArgument, + "1 validation error detected: Value at 'filterPattern' failed to satisfy constraint: "+ + "Member must have length less than or equal to %d", maxPatternLength) + } + + if !utf8.ValidString(raw) { + return nil, invalid("Invalid filter pattern: not valid UTF-8") + } + + trimmed := strings.TrimSpace(raw) + + switch { + case trimmed == "": + return &Pattern{m: matchAll{}}, nil + case strings.HasPrefix(trimmed, "{"): + jp, err := parseJSONPattern(trimmed) + if err != nil { + return nil, err + } + + return &Pattern{m: jp, hasRegex: jp.regexCount > 0, json: jp}, nil + case strings.HasPrefix(trimmed, "["): + return nil, invalid("space-delimited filter patterns are not supported yet: " + trimmed) + default: + tp, err := parseTerms(trimmed) + if err != nil { + return nil, err + } + + return &Pattern{m: tp, hasRegex: tp.hasRegex}, nil + } +} + +// Match reports whether a log event message satisfies the pattern. +func (p *Pattern) Match(message string) bool { + return p.m.match(message) +} + +// HasRegex reports whether the pattern contains a %regex% term. CloudWatch +// Logs limits how many such patterns a log group may hold. +func (p *Pattern) HasRegex() bool { + return p.hasRegex +} + +// ExtractedValues returns the values of the JSON properties a JSON pattern +// names, keyed by selector (for example "$.latency"), for a message the +// pattern matched. Other pattern forms extract nothing. +func (p *Pattern) ExtractedValues(message string) map[string]string { + if p.json == nil { + return map[string]string{} + } + + return p.json.extract(message) +} + +type matchAll struct{} + +func (matchAll) match(string) bool { return true } + +func invalid(msg string) error { + return errors.New(errors.InvalidArgument, msg) +} diff --git a/providers/aws/cloudwatchlogs/filterpattern/pattern_test.go b/providers/aws/cloudwatchlogs/filterpattern/pattern_test.go new file mode 100644 index 000000000..059236719 --- /dev/null +++ b/providers/aws/cloudwatchlogs/filterpattern/pattern_test.go @@ -0,0 +1,182 @@ +package filterpattern + +import ( + "strings" + "testing" + + "github.com/stackshy/cloudemu/v2/errors" +) + +// Example events from the filter pattern syntax reference. +const ( + err400 = "[ERROR 400] BAD REQUEST" + err401 = "[ERROR 401] UNAUTHORIZED REQUEST" + err419 = "[ERROR 419] MISSING ARGUMENTS" + err420 = "[ERROR 420] INVALID ARGUMENTS" + err500 = "[ERROR 500] INTERNAL SERVER ERROR" + success = "[SUCCESS 200] AUTHORIZED REQUEST" + info = "[INFO] REQUEST FAILED" + + trailEvent = `{"eventType":"UpdateTrail","sourceIPAddress":"111.111.111.111","arrayKey":["value","another value"],` + + `"objectList":[{"name":"a","id":1},{"name":"b","id":2}],"SomeObject":null,"cluster.name":"c"}` + userEvent = `{"user":{"id":1,"email":"John.Stiles@example.com"},` + + `"users":[{"id":2,"email":"John.Doe@example.com"},{"id":3,"email":"Jane.Doe@example.com"}],` + + `"actions":["GET","PUT","DELETE"],"coordinates":[[0,1,2],[4,5,6],[7,8,9]]}` + metricEvent = `{"bandwidth":80,"latency":40,"refreshRate":60,"responseTime":5,"errorCode":400,` + + `"number":[1000],"ok":true,"flag":false}` +) + +func mustParse(t *testing.T, pattern string) *Pattern { + t.Helper() + + p, err := Parse(pattern) + if err != nil { + t.Fatalf("Parse(%q): %v", pattern, err) + } + + return p +} + +func TestUnstructuredPatterns(t *testing.T) { + events := []string{err400, err401, err419, err420, err500, success, info} + + tests := []struct { + pattern string + want []string + }{ + {"ERROR", []string{err400, err401, err419, err420, err500}}, + {"ERROR ARGUMENTS", []string{err419, err420}}, + {"?ERROR ?ARGUMENTS", []string{err400, err401, err419, err420, err500}}, + {"?ERROR ?ARGUMENTS REQUEST", []string{err400, err401, success, info}}, + {`"INTERNAL SERVER ERROR"`, []string{err500}}, + {"ERROR -ARGUMENTS", []string{err400, err401, err500}}, + {`" "`, events}, + {"", events}, + {"%AUTHORIZED%", []string{err401, success}}, + {"%^\\[ERROR 4%", []string{err400, err401, err419, err420}}, + {"%BAD|INVALID%", []string{err400, err420}}, + {"error", nil}, + } + + for _, tc := range tests { + t.Run(tc.pattern, func(t *testing.T) { + p := mustParse(t, tc.pattern) + + var got []string + + for _, e := range events { + if p.Match(e) { + got = append(got, e) + } + } + + if strings.Join(got, "|") != strings.Join(tc.want, "|") { + t.Fatalf("%q matched %q, want %q", tc.pattern, got, tc.want) + } + }) + } +} + +func TestJSONPatterns(t *testing.T) { + tests := []struct { + pattern, event string + want bool + }{ + {`{ $.eventType = "UpdateTrail" }`, trailEvent, true}, + {`{ $.eventType = "Other" }`, trailEvent, false}, + {`{ $.sourceIPAddress != 123.123.* }`, trailEvent, true}, + {`{ $.sourceIPAddress != 111.111.* }`, trailEvent, false}, + {`{ $.arrayKey[0] = "value" }`, trailEvent, true}, + {`{ $.eventType = %Trail% }`, trailEvent, true}, + {`{ $.arrayKey[*] = %val.{2}% }`, trailEvent, true}, + {`{ $.* = %111\.111\.111\.1[0-9]{1,2}% }`, trailEvent, true}, + {`{ $.['cluster.name'] = "c" }`, trailEvent, true}, + {`{ $.SomeObject IS NULL }`, trailEvent, true}, + {`{ $.eventType IS NULL }`, trailEvent, false}, + {`{ $.SomeOtherObject NOT EXISTS }`, trailEvent, true}, + {`{ $.SomeObject NOT EXISTS }`, trailEvent, false}, + {`{ $.objectList = "a" }`, trailEvent, false}, + {`{ $.objectList[1].id = 2 }`, trailEvent, true}, + {`{ $.eventType = "*" && $.sourceIPAddress != 123.123.* }`, trailEvent, true}, + {`{ ($.user.id = 1) && ($.users[0].email = "John.Doe@example.com") }`, userEvent, true}, + {`{ $.user.email = "John.Stiles@example.com" || $.coordinates[0][1] = "nonmatch" && $.actions[2] = "nonmatch" }`, userEvent, true}, + {`{ ($.user.email = "John.Stiles@example.com" || $.coordinates[0][1] = "nonmatch") && $.actions[2] = "nonmatch" }`, userEvent, false}, + {`{ ($.user.id = 2 && $.users[0].email = "nonmatch") || $.actions[2] = "GET" }`, userEvent, false}, + {`{ $.bandwidth > 75 }`, metricEvent, true}, + {`{ $.latency < 50 }`, metricEvent, true}, + {`{ $.refreshRate >= 60 }`, metricEvent, true}, + {`{ $.responseTime <= 5 }`, metricEvent, true}, + {`{ $.errorCode = 400 }`, metricEvent, true}, + {`{ $.errorCode != 500 }`, metricEvent, true}, + {`{ $.number[0] = 1e+3 }`, metricEvent, true}, + {`{ $.number[0] != 1e-3 }`, metricEvent, true}, + {`{ $.ok IS TRUE }`, metricEvent, true}, + {`{ $.flag IS FALSE }`, metricEvent, true}, + {`{ $.ok IS FALSE }`, metricEvent, false}, + {`{$.latency=40}`, metricEvent, true}, + {`{ $.latency = 40 }`, "latency 40", false}, + } + + for _, tc := range tests { + t.Run(tc.pattern, func(t *testing.T) { + if got := mustParse(t, tc.pattern).Match(tc.event); got != tc.want { + t.Fatalf("Match = %v, want %v", got, tc.want) + } + }) + } +} + +func TestInvalidPatterns(t *testing.T) { + for _, pattern := range []string{ + `{ $.a IS NOT NULL }`, + `{ $.a EXISTS }`, + `{ $.a = 1`, + `{ $.a = }`, + `{ $.a > abc }`, + `{ $.a & $.b }`, + `{ $.["a"] = 1 }`, + `{ $.*.b[*] = 1 }`, + `{ $.a[*] = 1 && $.b[*] = 1 && $.c[*] = 1 && $.d[*] = 1 }`, + `{ $.a = %x% && $.b = %y% && $.c = %z% }`, + `{ ($.a = 1 }`, + `{ a = 1 }`, + `%(ab)%`, + `%ab!%`, + `%unterminated`, + `"unterminated`, + `error:{boom}`, + `[ip, user]`, + strings.Repeat("a", maxPatternLength+1), + } { + _, err := Parse(pattern) + if err == nil || !errors.IsInvalidArgument(err) { + t.Errorf("Parse(%.40q) = %v, want InvalidArgument", pattern, err) + } + } +} + +func TestQuotasAccepted(t *testing.T) { + for _, pattern := range []string{ + `{ $.a[*] = 1 && $.b[*] = 1 && $.c[*] = 1 }`, + `{ $.a = %x% && $.b = %y% }`, + } { + if _, err := Parse(pattern); err != nil { + t.Errorf("Parse(%q): %v", pattern, err) + } + } +} + +func TestHasRegexAndExtractedValues(t *testing.T) { + if mustParse(t, "ERROR").HasRegex() || !mustParse(t, "%ERR%").HasRegex() || !mustParse(t, `{ $.a = %x% }`).HasRegex() { + t.Fatal("HasRegex mismatch") + } + + got := mustParse(t, `{ $.latency = * && $.arr[*] = 1 }`).ExtractedValues(`{"latency":50,"arr":[1]}`) + if len(got) != 1 || got["$.latency"] != "50" { + t.Fatalf("ExtractedValues = %v, want only $.latency=50", got) + } + + if v := mustParse(t, "ERROR").ExtractedValues("ERROR"); len(v) != 0 { + t.Fatalf("unstructured ExtractedValues = %v, want empty", v) + } +} diff --git a/providers/aws/cloudwatchlogs/filterpattern/regex.go b/providers/aws/cloudwatchlogs/filterpattern/regex.go new file mode 100644 index 000000000..f5fc55ccd --- /dev/null +++ b/providers/aws/cloudwatchlogs/filterpattern/regex.go @@ -0,0 +1,49 @@ +package filterpattern + +import ( + "regexp" + "strings" + "unicode" +) + +// regexAllowedSymbols are the non-alphanumeric characters a %regex% may use: +// the documented symbol set, the operators, the escape character, and space. +const regexAllowedSymbols = ":_#=@/;,-^$?[]{}|\\*+. " + +// compileRegex validates a %...% body against the CloudWatch Logs regex +// subset (no parentheses, no multi-byte characters, a fixed symbol set) and +// compiles it. The match is unanchored unless the body uses ^ or $. +func compileRegex(body string) (*regexp.Regexp, error) { + if body == "" { + return nil, invalid("Invalid regex in filter pattern: empty expression") + } + + if err := checkRegexChars(body); err != nil { + return nil, err + } + + re, err := regexp.Compile(body) + if err != nil { + return nil, invalid("Invalid regex in filter pattern: %" + body + "%") + } + + return re, nil +} + +func checkRegexChars(body string) error { + for _, r := range body { + if r > unicode.MaxASCII { + return invalid("Invalid regex in filter pattern: multi-byte characters are not supported") + } + + if !isASCIIAlnum(r) && !strings.ContainsRune(regexAllowedSymbols, r) { + return invalid("Invalid character(s) in regex %" + body + "%") + } + } + + return nil +} + +func isASCIIAlnum(r rune) bool { + return (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') +} diff --git a/providers/aws/cloudwatchlogs/filterpattern/terms.go b/providers/aws/cloudwatchlogs/filterpattern/terms.go new file mode 100644 index 000000000..a563fca3c --- /dev/null +++ b/providers/aws/cloudwatchlogs/filterpattern/terms.go @@ -0,0 +1,176 @@ +package filterpattern + +import ( + "regexp" + "strings" +) + +// termKind is how a term in an unstructured pattern combines with the rest. +type termKind int + +const ( + termInclude termKind = iota // plain or quoted term: must be present + termExclude // -term: must be absent + termOptional // ?term: any one of the optional terms +) + +// unquotedForbidden are characters an unquoted term cannot contain; terms +// with such characters must be double-quoted. +const unquotedForbidden = `{}[]()"%<>!&|` + +// delimiterPair is the opening and closing quote (or %) around a term. +const delimiterPair = 2 + +type term struct { + kind termKind + text string + re *regexp.Regexp + // any is set for a quoted term that is only whitespace (`" "`), which + // matches every event. + any bool +} + +func (t *term) present(message string) bool { + switch { + case t.any: + return true + case t.re != nil: + return t.re.MatchString(message) + default: + return strings.Contains(message, t.text) + } +} + +// termPattern is an unstructured pattern: terms are matched as +// case-sensitive substrings of the message. +type termPattern struct { + include, exclude, optional []*term + hasRegex bool +} + +// match applies the documented rules: every include term present and no +// exclude term present. Optional (?) terms only count when the pattern has no +// other terms; combined with other terms they are ignored. +func (p *termPattern) match(message string) bool { + if len(p.include) == 0 && len(p.exclude) == 0 { + for _, t := range p.optional { + if t.present(message) { + return true + } + } + + return false + } + + for _, t := range p.include { + if !t.present(message) { + return false + } + } + + for _, t := range p.exclude { + if t.present(message) { + return false + } + } + + return true +} + +func parseTerms(s string) (*termPattern, error) { + p := &termPattern{} + + for i := 0; i < len(s); { + if s[i] == ' ' || s[i] == '\t' { + i++ + continue + } + + t, next, err := readTerm(s, i) + if err != nil { + return nil, err + } + + i = next + + if t.re != nil { + p.hasRegex = true + } + + switch t.kind { + case termExclude: + p.exclude = append(p.exclude, t) + case termOptional: + p.optional = append(p.optional, t) + case termInclude: + p.include = append(p.include, t) + } + } + + return p, nil +} + +// readTerm reads one term starting at s[i] and returns it with the index just +// past it. +func readTerm(s string, i int) (*term, int, error) { + t := &term{kind: termInclude} + + if s[i] == '-' || s[i] == '?' { + t.kind = termOptional + if s[i] == '-' { + t.kind = termExclude + } + + i++ + if i >= len(s) || s[i] == ' ' { + return nil, 0, invalid("Invalid filter pattern: '" + s[i-1:i] + "' must prefix a term") + } + } + + switch s[i] { + case '"': + return readQuotedTerm(t, s, i) + case '%': + return readRegexTerm(t, s, i) + } + + end := strings.IndexAny(s[i:], " \t") + if end < 0 { + end = len(s) - i + } + + t.text = s[i : i+end] + if strings.ContainsAny(t.text, unquotedForbidden) { + return nil, 0, invalid("Invalid character(s) in term '" + t.text + "'") + } + + return t, i + end, nil +} + +func readQuotedTerm(t *term, s string, i int) (*term, int, error) { + end := strings.IndexByte(s[i+1:], '"') + if end < 0 { + return nil, 0, invalid("Invalid filter pattern: unterminated quoted term") + } + + t.text = s[i+1 : i+1+end] + t.any = strings.TrimSpace(t.text) == "" + + return t, i + end + delimiterPair, nil +} + +func readRegexTerm(t *term, s string, i int) (*term, int, error) { + end := strings.IndexByte(s[i+1:], '%') + if end < 0 { + return nil, 0, invalid("Invalid filter pattern: unterminated regex") + } + + re, err := compileRegex(s[i+1 : i+1+end]) + if err != nil { + return nil, 0, err + } + + t.re = re + + return t, i + end + delimiterPair, nil +} diff --git a/providers/aws/cloudwatchlogs/subscription_filters.go b/providers/aws/cloudwatchlogs/subscription_filters.go index 9188b484a..cb4fcdd9f 100644 --- a/providers/aws/cloudwatchlogs/subscription_filters.go +++ b/providers/aws/cloudwatchlogs/subscription_filters.go @@ -11,6 +11,7 @@ import ( "github.com/stackshy/cloudemu/v2/errors" "github.com/stackshy/cloudemu/v2/internal/recursionguard" + "github.com/stackshy/cloudemu/v2/providers/aws/cloudwatchlogs/filterpattern" "github.com/stackshy/cloudemu/v2/services/logging/driver" ) @@ -36,6 +37,10 @@ func (m *Mock) PutSubscriptionFilter(_ context.Context, cfg *driver.Subscription return errors.New(errors.InvalidArgument, "destinationArn is required") } + if err := validateFilterPattern(g, subscriptionFilterKind, cfg.Name, cfg.FilterPattern); err != nil { + return err + } + // A new filter (not an update of an existing name) must fit within the // per-group quota; real CloudWatch Logs caps a group at two filters. if !g.subFilters.Has(cfg.Name) && g.subFilters.Len() >= maxSubscriptionFilters { @@ -138,18 +143,18 @@ func (m *Mock) deliverSubscription( } // matchSubscriptionEvents returns the events whose message satisfies the filter -// pattern. An empty pattern matches every event; otherwise substring matching is -// used (the same simplification the metric-filter path applies โ€” the richer -// CloudWatch Logs filter grammar is tracked separately). -func matchSubscriptionEvents(pattern string, events []driver.LogEvent) []driver.LogEvent { - if pattern == "" { - return events +// pattern. A pattern that no longer parses (restored from an older snapshot) +// matches nothing; patterns are validated when the filter is put. +func matchSubscriptionEvents(raw string, events []driver.LogEvent) []driver.LogEvent { + pattern, err := filterpattern.Parse(raw) + if err != nil { + return nil } matched := make([]driver.LogEvent, 0, len(events)) for i := range events { - if strings.Contains(events[i].Message, pattern) { + if pattern.Match(events[i].Message) { matched = append(matched, events[i]) } } diff --git a/server/aws/cloudwatchlogs/filter_pattern_sdk_test.go b/server/aws/cloudwatchlogs/filter_pattern_sdk_test.go new file mode 100644 index 000000000..5476f05eb --- /dev/null +++ b/server/aws/cloudwatchlogs/filter_pattern_sdk_test.go @@ -0,0 +1,145 @@ +package cloudwatchlogs_test + +import ( + "context" + "errors" + "fmt" + "testing" + "time" + + "github.com/aws/aws-sdk-go-v2/aws" + cwl "github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs" + cwltypes "github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs/types" +) + +func TestSDKFilterLogEventsPatternLanguage(t *testing.T) { + c := newLogsClient(t) + ctx := context.Background() + g, s := aws.String("/fp/g"), aws.String("s") + + if _, err := c.CreateLogGroup(ctx, &cwl.CreateLogGroupInput{LogGroupName: g}); err != nil { + t.Fatalf("CreateLogGroup: %v", err) + } + + if _, err := c.CreateLogStream(ctx, &cwl.CreateLogStreamInput{LogGroupName: g, LogStreamName: s}); err != nil { + t.Fatalf("CreateLogStream: %v", err) + } + + now := time.Now() + msgs := []string{ + "[ERROR 419] MISSING ARGUMENTS", + "[ERROR 400] BAD REQUEST", + `{"level":"ERROR","latency":50,"user":null}`, + `{"level":"INFO","latency":5}`, + } + + events := make([]cwltypes.InputLogEvent, 0, len(msgs)) + for i, m := range msgs { + events = append(events, cwltypes.InputLogEvent{ + Timestamp: aws.Int64(now.Add(time.Duration(i) * time.Millisecond).UnixMilli()), Message: aws.String(m), + }) + } + + if _, err := c.PutLogEvents(ctx, &cwl.PutLogEventsInput{LogGroupName: g, LogStreamName: s, LogEvents: events}); err != nil { + t.Fatalf("PutLogEvents: %v", err) + } + + for pattern, want := range map[string]int{ + "ERROR": 3, + "ERROR -ARGUMENTS": 2, + "?BAD ?MISSING": 2, + `"BAD REQUEST"`: 1, + "%ERR.R%": 3, + `{ $.level = "ERROR" }`: 1, + `{ $.latency > 10 || $.level = "INFO" }`: 2, + `{ $.user IS NULL && $.level = ERR* }`: 1, + `{ $.missing NOT EXISTS && $.latency < 9 }`: 1, + } { + out, err := c.FilterLogEvents(ctx, &cwl.FilterLogEventsInput{LogGroupName: g, FilterPattern: aws.String(pattern)}) + if err != nil { + t.Fatalf("FilterLogEvents(%q): %v", pattern, err) + } + + if len(out.Events) != want { + t.Errorf("FilterLogEvents(%q) = %d events, want %d", pattern, len(out.Events), want) + } + } + + var invalid *cwltypes.InvalidParameterException + + for _, bad := range []string{`{ $.a IS NOT NULL }`, `{ $.a EXISTS }`, `{ $.a = `} { + _, err := c.FilterLogEvents(ctx, &cwl.FilterLogEventsInput{LogGroupName: g, FilterPattern: aws.String(bad)}) + if !errors.As(err, &invalid) { + t.Errorf("FilterLogEvents(%q): got %v, want InvalidParameterException", bad, err) + } + } +} + +func TestSDKTestMetricFilter(t *testing.T) { + c := newLogsClient(t) + ctx := context.Background() + + out, err := c.TestMetricFilter(ctx, &cwl.TestMetricFilterInput{ + FilterPattern: aws.String(`{ $.latency > 10 }`), + LogEventMessages: []string{`{"latency":5}`, `{"latency":50}`, "plain text"}, + }) + if err != nil { + t.Fatalf("TestMetricFilter: %v", err) + } + + if len(out.Matches) != 1 || out.Matches[0].EventNumber != 1 || out.Matches[0].ExtractedValues["$.latency"] != "50" { + t.Fatalf("matches = %+v", out.Matches) + } + + var invalid *cwltypes.InvalidParameterException + + if _, err := c.TestMetricFilter(ctx, &cwl.TestMetricFilterInput{ + FilterPattern: aws.String(`{ $.a IS NOT NULL }`), LogEventMessages: []string{"x"}, + }); !errors.As(err, &invalid) { + t.Fatalf("bad pattern: got %v, want InvalidParameterException", err) + } +} + +func TestSDKPutFilterPatternValidationAndQuota(t *testing.T) { + c := newLogsClient(t) + ctx := context.Background() + g := aws.String("/fp/quota") + + if _, err := c.CreateLogGroup(ctx, &cwl.CreateLogGroupInput{LogGroupName: g}); err != nil { + t.Fatalf("CreateLogGroup: %v", err) + } + + mf := func(name, pattern string) error { + _, err := c.PutMetricFilter(ctx, &cwl.PutMetricFilterInput{ + LogGroupName: g, FilterName: aws.String(name), FilterPattern: aws.String(pattern), + MetricTransformations: []cwltypes.MetricTransformation{{ + MetricName: aws.String("M"), MetricNamespace: aws.String("N"), MetricValue: aws.String("1"), + }}, + }) + + return err + } + + var invalid *cwltypes.InvalidParameterException + if err := mf("bad", "{ $.a EXISTS }"); !errors.As(err, &invalid) { + t.Fatalf("bad metric filter pattern: got %v, want InvalidParameterException", err) + } + + if _, err := c.PutSubscriptionFilter(ctx, &cwl.PutSubscriptionFilterInput{ + LogGroupName: g, FilterName: aws.String("bad"), FilterPattern: aws.String("%(x)%"), + DestinationArn: aws.String("arn:aws:lambda:us-east-1:000000000000:function:f"), + }); !errors.As(err, &invalid) { + t.Fatalf("bad subscription filter pattern: got %v, want InvalidParameterException", err) + } + + for i := range 5 { + if err := mf(fmt.Sprintf("re%d", i), "%ERR%"); err != nil { + t.Fatalf("regex filter %d: %v", i, err) + } + } + + var limit *cwltypes.LimitExceededException + if err := mf("re5", "%WARN%"); !errors.As(err, &limit) { + t.Fatalf("sixth regex filter: got %v, want LimitExceededException", err) + } +} diff --git a/server/aws/cloudwatchlogs/handler.go b/server/aws/cloudwatchlogs/handler.go index bb899e952..b4b37c877 100644 --- a/server/aws/cloudwatchlogs/handler.go +++ b/server/aws/cloudwatchlogs/handler.go @@ -17,7 +17,7 @@ // PutMetricFilter DescribeMetricFilters DeleteMetricFilter // PutSubscriptionFilter DescribeSubscriptionFilters DeleteSubscriptionFilter // PutRetentionPolicy DeleteRetentionPolicy ListLogGroups -// AssociateKmsKey DisassociateKmsKey +// AssociateKmsKey DisassociateKmsKey TestMetricFilter // TagResource / UntagResource / ListTagsForResource (and the legacy *LogGroup forms) // // CloudWatch Logs-only operations go through the optional driver.AWSLogging @@ -97,6 +97,8 @@ func (h *Handler) dispatchManagement(w http.ResponseWriter, r *http.Request, op h.describeMetricFilters(w, r) case "DeleteMetricFilter": h.deleteMetricFilter(w, r) + case "TestMetricFilter": + h.testMetricFilter(w, r) case "PutSubscriptionFilter": h.putSubscriptionFilter(w, r) case "DescribeSubscriptionFilters": diff --git a/server/aws/cloudwatchlogs/metric_filter_tester.go b/server/aws/cloudwatchlogs/metric_filter_tester.go new file mode 100644 index 000000000..d6144741d --- /dev/null +++ b/server/aws/cloudwatchlogs/metric_filter_tester.go @@ -0,0 +1,51 @@ +package cloudwatchlogs + +import ( + "net/http" + + "github.com/stackshy/cloudemu/v2/server/wire" +) + +type testMetricFilterRequest struct { + FilterPattern string `json:"filterPattern"` + LogEventMessages []string `json:"logEventMessages"` +} + +type metricFilterMatchJSON struct { + EventNumber int64 `json:"eventNumber"` + EventMessage string `json:"eventMessage"` + ExtractedValues map[string]string `json:"extractedValues"` +} + +type testMetricFilterResponse struct { + Matches []metricFilterMatchJSON `json:"matches"` +} + +// testMetricFilter runs a filter pattern over sample messages +// (Logs_20140328.TestMetricFilter). +func (h *Handler) testMetricFilter(w http.ResponseWriter, r *http.Request) { + ext, ok := h.awsLogs(w, "TestMetricFilter") + if !ok { + return + } + + var req testMetricFilterRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + matches, err := ext.TestMetricFilter(r.Context(), req.FilterPattern, req.LogEventMessages) + if err != nil { + writeErr(w, err) + return + } + + resp := testMetricFilterResponse{Matches: make([]metricFilterMatchJSON, 0, len(matches))} + for _, m := range matches { + resp.Matches = append(resp.Matches, metricFilterMatchJSON{ + EventNumber: m.EventNumber, EventMessage: m.EventMessage, ExtractedValues: m.ExtractedValues, + }) + } + + wire.WriteJSON(w, resp) +} diff --git a/services/logging/driver/aws.go b/services/logging/driver/aws.go index 6082640a5..f757263fe 100644 --- a/services/logging/driver/aws.go +++ b/services/logging/driver/aws.go @@ -56,6 +56,14 @@ type PutLogEventsResult struct { Rejected *RejectedLogEventsInfo } +// MetricFilterMatch is one TestMetricFilter result: a sample message the +// pattern matched, its 0-based position, and the values the pattern extracts. +type MetricFilterMatch struct { + EventNumber int64 + EventMessage string + ExtractedValues map[string]string +} + // AWSLogging is an optional interface implemented by the AWS CloudWatch Logs // backend for operations with no cross-provider equivalent. The CloudWatch // Logs wire handler type-asserts for it; Azure, GCP and OCI backends do not @@ -74,4 +82,7 @@ type AWSLogging interface { // query-result ARN). AssociateKmsKey(ctx context.Context, logGroup, resourceIdentifier, kmsKeyID string) error DisassociateKmsKey(ctx context.Context, logGroup, resourceIdentifier string) error + + // TestMetricFilter runs a filter pattern over sample log messages. + TestMetricFilter(ctx context.Context, filterPattern string, logEventMessages []string) ([]MetricFilterMatch, error) } From e36ce6ebd61b83b538261018b380e112988b22f5 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Patel Date: Thu, 22 Oct 2026 05:03:59 +0530 Subject: [PATCH 04/31] fix(aws-eventbridge): PutEvents validation, size limit, EventId alignment and PutTargets input exclusivity (#1657) Validate Source, DetailType and Detail per entry, enforce the 1 MB request limit (256 KB in the regions without the increase), accept entries for a missing bus the way AWS does, keep result entries aligned with the request, and reject targets that combine Input, InputPath and InputTransformer. --- providers/aws/eventbridge/eventbridge.go | 47 +--- providers/aws/eventbridge/eventbridge_test.go | 21 +- providers/aws/eventbridge/put_events.go | 201 +++++++++++++++ .../eventbridge/put_events_validation_test.go | 242 ++++++++++++++++++ providers/aws/eventbridge/target_input.go | 28 +- providers/aws/lifecycle_events_test.go | 6 +- server/aws/eventbridge/operations.go | 63 ++--- .../put_events_validation_sdk_test.go | 165 ++++++++++++ server/aws/eventbridge/types.go | 13 - services/eventbus/driver/driver.go | 9 + 10 files changed, 691 insertions(+), 104 deletions(-) create mode 100644 providers/aws/eventbridge/put_events.go create mode 100644 providers/aws/eventbridge/put_events_validation_test.go create mode 100644 server/aws/eventbridge/put_events_validation_sdk_test.go diff --git a/providers/aws/eventbridge/eventbridge.go b/providers/aws/eventbridge/eventbridge.go index 8a912b43c..30185037c 100644 --- a/providers/aws/eventbridge/eventbridge.go +++ b/providers/aws/eventbridge/eventbridge.go @@ -431,6 +431,10 @@ func (m *Mock) setRuleState(eventBus, ruleName, state string) error { // PutTargets adds targets to a rule. func (m *Mock) PutTargets(_ context.Context, eventBus, ruleName string, targets []driver.Target) error { + if err := validateTargetInputs(targets); err != nil { + return err + } + busName := eventBus if busName == "" { busName = defaultBusName @@ -503,47 +507,6 @@ func (m *Mock) ListTargets(_ context.Context, eventBus, ruleName string) ([]driv return targetsFromStore(rd.targets), nil } -// PutEvents publishes events to the event bus. -func (m *Mock) PutEvents(ctx context.Context, events []driver.Event) (*driver.PublishResult, error) { - result := &driver.PublishResult{ - EventIDs: make([]string, 0, len(events)), - } - - for i := range events { - eventID := generateEventID(&events[i], m.opts.Clock.Now(), i) - events[i].ID = eventID - - if events[i].Time.IsZero() { - events[i].Time = m.opts.Clock.Now() - } - - busName := events[i].EventBus - if busName == "" { - busName = defaultBusName - } - - bd, ok := m.buses.Get(busName) - if !ok { - result.FailCount++ - - continue - } - - m.storeEvent(bd, &events[i]) - matched := m.MatchedRules(&events[i]) - m.deliverToTargets(ctx, matched, &events[i]) - - dims := map[string]string{"EventBusName": busName} - m.emitMetric(ctx, "PutEventsRequestCount", 1, dims) - m.emitMetric(ctx, "MatchedEvents", float64(len(matched)), dims) - - result.SuccessCount++ - result.EventIDs = append(result.EventIDs, eventID) - } - - return result, nil -} - // PublishServiceEvent puts a native AWS service lifecycle event (EC2 instance // state change, ECS task state change, ...) on the default event bus, the way // real AWS services publish to the account's default bus automatically. The @@ -623,7 +586,7 @@ func (m *Mock) deliverToTarget( return } - dims := map[string]string{"RuleName": rule.Name, "EventBusName": busName} + dims := map[string]string{"RuleName": rule.Name, busDimension: busName} m.emitMetric(ctx, "FailedInvocations", 1, dims) dlqARN := deadLetterARN(t.DeadLetterConfig) diff --git a/providers/aws/eventbridge/eventbridge_test.go b/providers/aws/eventbridge/eventbridge_test.go index 4795c2f52..4ec87e549 100644 --- a/providers/aws/eventbridge/eventbridge_test.go +++ b/providers/aws/eventbridge/eventbridge_test.go @@ -533,13 +533,16 @@ func TestPutEvents(t *testing.T) { assert.Equal(t, 2, len(result.EventIDs)) }) - t.Run("events to nonexistent bus counted as failures", func(t *testing.T) { + t.Run("events to nonexistent bus succeed and are dropped", func(t *testing.T) { + // Real EventBridge answers an entry for a missing bus with an EventId + // and no error, and drops the event. result, err := m.PutEvents(ctx, []driver.Event{ {Source: "my.app", DetailType: "Test", Detail: "{}", EventBus: "nonexistent"}, }) require.NoError(t, err) - assert.Equal(t, 0, result.SuccessCount) - assert.Equal(t, 1, result.FailCount) + assert.Equal(t, 1, result.SuccessCount) + assert.Equal(t, 0, result.FailCount) + assert.Len(t, result.EventIDs, 1) }) t.Run("history stores events", func(t *testing.T) { @@ -548,16 +551,20 @@ func TestPutEvents(t *testing.T) { assert.Equal(t, 2, len(history)) }) - t.Run("mixed success and failure", func(t *testing.T) { + t.Run("existing and missing bus in one batch both succeed", func(t *testing.T) { createTestBus(t, m, "custom-bus") result, err := m.PutEvents(ctx, []driver.Event{ {Source: "my.app", DetailType: "OK", Detail: "{}", EventBus: "custom-bus"}, - {Source: "my.app", DetailType: "Fail", Detail: "{}", EventBus: "ghost-bus"}, + {Source: "my.app", DetailType: "Dropped", Detail: "{}", EventBus: "ghost-bus"}, }) require.NoError(t, err) - assert.Equal(t, 1, result.SuccessCount) - assert.Equal(t, 1, result.FailCount) + assert.Equal(t, 2, result.SuccessCount) + assert.Equal(t, 0, result.FailCount) + + history, err := m.GetEventHistory(ctx, "custom-bus", 0) + require.NoError(t, err) + assert.Len(t, history, 1) }) t.Run("byte-identical events in one call get unique ids", func(t *testing.T) { diff --git a/providers/aws/eventbridge/put_events.go b/providers/aws/eventbridge/put_events.go new file mode 100644 index 000000000..cf6b3d53c --- /dev/null +++ b/providers/aws/eventbridge/put_events.go @@ -0,0 +1,201 @@ +package eventbridge + +import ( + "context" + "encoding/json" + + "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/services/eventbus/driver" +) + +const ( + // maxPutEventsEntries is the most entries one PutEvents request may carry. + maxPutEventsEntries = 10 + + // putEventsSizeLimit is the total entry size a PutEvents request must stay + // under (1 MB since January 2026). + putEventsSizeLimit = 1 << 20 + // legacyPutEventsSizeLimit is the 256 KB limit still in force in the regions + // that did not get the 1 MB increase. + legacyPutEventsSizeLimit = 256 << 10 + // timeEntrySize is what a set Time field adds to an entry's size. + timeEntrySize = 14 + + // busDimension is the AWS/Events metric dimension naming the event bus. + busDimension = "EventBusName" + + errCodeInvalidArgument = "InvalidArgument" + errCodeMalformedDetail = "MalformedDetail" +) + +const ( + putEventsEntryCountMessage = "1 validation error detected: Value at 'entries' failed to satisfy " + + "constraint: Member must have length less than or equal to 10 and greater than or equal to 1." + putEventsSizeMessage = "Total size of the entries in the request is over the limit." + + msgSourceRequired = "Parameter Source is not valid. Reason: Source is a required argument." + msgDetailTypeRequired = "Parameter DetailType is not valid. Reason: DetailType is a required argument." + msgDetailRequired = "Parameter Detail is not valid. Reason: Detail is a required argument." + msgMalformedDetail = "Detail is malformed." +) + +// legacySizeLimitRegion reports whether region is one where the 1 MB PutEvents +// payload increase is not available: Malaysia, New Zealand, Thailand, Taipei and +// Mexico (Central). +func legacySizeLimitRegion(region string) bool { + switch region { + case "ap-southeast-5", "ap-southeast-6", "ap-southeast-7", "ap-east-2", "mx-central-1": + return true + default: + return false + } +} + +func sizeLimitFor(region string) int { + if legacySizeLimitRegion(region) { + return legacyPutEventsSizeLimit + } + + return putEventsSizeLimit +} + +// entrySize computes a PutEvents entry's size the way EventBridge documents it: +// 14 bytes for a set Time plus the UTF-8 bytes of Source, DetailType, Detail and +// each Resource. +func entrySize(e *driver.Event) int { + size := len(e.Source) + len(e.DetailType) + len(e.Detail) + if !e.Time.IsZero() { + size += timeEntrySize + } + + for _, r := range e.Resources { + size += len(r) + } + + return size +} + +// validatePutEventsRequest applies the request-wide limits that fail the whole +// call: 1 to 10 entries and a total size under the region's limit. +func validatePutEventsRequest(events []driver.Event, region string) error { + if n := len(events); n < 1 || n > maxPutEventsEntries { + return errors.New(errors.InvalidArgument, putEventsEntryCountMessage) + } + + total := 0 + for i := range events { + total += entrySize(&events[i]) + } + + if total >= sizeLimitFor(region) { + return errors.New(errors.InvalidArgument, putEventsSizeMessage) + } + + return nil +} + +// validateEntry checks one entry in the order EventBridge does, returning the +// per-entry error code and message, or an empty code when the entry is valid. +func validateEntry(e *driver.Event) (code, msg string) { + switch { + case e.Source == "": + return errCodeInvalidArgument, msgSourceRequired + case e.DetailType == "": + return errCodeInvalidArgument, msgDetailTypeRequired + case e.Detail == "": + return errCodeInvalidArgument, msgDetailRequired + case !isJSONObject(e.Detail): + return errCodeMalformedDetail, msgMalformedDetail + default: + return "", "" + } +} + +// isJSONObject reports whether s is a well-formed JSON object. EventBridge fails +// any other Detail (string, array, null, malformed) with MalformedDetail. +func isJSONObject(s string) bool { + var obj map[string]json.RawMessage + + return json.Unmarshal([]byte(s), &obj) == nil && obj != nil +} + +// PutEventEntries publishes a PutEvents batch and reports each entry's outcome, +// aligned by index with events. Request-wide violations (entry count, total +// size) fail the whole call; an invalid entry fails only its own slot. An entry +// addressed to a bus that does not exist still gets an EventId and counts as a +// success, but is dropped: that is what real EventBridge does. +func (m *Mock) PutEventEntries(ctx context.Context, events []driver.Event) ([]driver.PutEventsEntryResult, error) { + if err := validatePutEventsRequest(events, m.opts.Region); err != nil { + return nil, err + } + + results := make([]driver.PutEventsEntryResult, len(events)) + + for i := range events { + if code, msg := validateEntry(&events[i]); code != "" { + results[i] = driver.PutEventsEntryResult{ErrorCode: code, ErrorMessage: msg} + + continue + } + + results[i] = driver.PutEventsEntryResult{EventID: m.publishEvent(ctx, &events[i], i)} + } + + return results, nil +} + +// PutEvents publishes events to the event bus. It is the portable form of +// PutEventEntries, summarizing the per-entry outcomes into counts. +func (m *Mock) PutEvents(ctx context.Context, events []driver.Event) (*driver.PublishResult, error) { + entries, err := m.PutEventEntries(ctx, events) + if err != nil { + return nil, err + } + + result := &driver.PublishResult{EventIDs: make([]string, 0, len(entries))} + + for _, e := range entries { + if e.ErrorCode != "" { + result.FailCount++ + + continue + } + + result.SuccessCount++ + result.EventIDs = append(result.EventIDs, e.EventID) + } + + return result, nil +} + +// publishEvent assigns the event its id and time, then stores, matches and +// delivers it on its bus. It returns the event id even when the bus does not +// exist, in which case the event is dropped without being stored or delivered. +func (m *Mock) publishEvent(ctx context.Context, event *driver.Event, index int) string { + now := m.opts.Clock.Now() + + event.ID = generateEventID(event, now, index) + if event.Time.IsZero() { + event.Time = now + } + + busName := event.EventBus + if busName == "" { + busName = defaultBusName + } + + bd, ok := m.buses.Get(busName) + if !ok { + return event.ID + } + + m.storeEvent(bd, event) + matched := m.MatchedRules(event) + m.deliverToTargets(ctx, matched, event) + + dims := map[string]string{busDimension: busName} + m.emitMetric(ctx, "PutEventsRequestCount", 1, dims) + m.emitMetric(ctx, "MatchedEvents", float64(len(matched)), dims) + + return event.ID +} diff --git a/providers/aws/eventbridge/put_events_validation_test.go b/providers/aws/eventbridge/put_events_validation_test.go new file mode 100644 index 000000000..a953d01a0 --- /dev/null +++ b/providers/aws/eventbridge/put_events_validation_test.go @@ -0,0 +1,242 @@ +package eventbridge + +import ( + "context" + "strings" + "testing" + "time" + + "github.com/stackshy/cloudemu/v2/config" + cerrors "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/services/eventbus/driver" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestPutEventEntriesRequiredFields(t *testing.T) { + tests := []struct { + name string + event driver.Event + code string + message string + }{ + { + name: "missing source", + event: driver.Event{DetailType: "t", Detail: "{}"}, + code: "InvalidArgument", + message: "Parameter Source is not valid. Reason: Source is a required argument.", + }, + { + name: "missing detail type", + event: driver.Event{Source: "s", Detail: "{}"}, + code: "InvalidArgument", + message: "Parameter DetailType is not valid. Reason: DetailType is a required argument.", + }, + { + name: "missing detail", + event: driver.Event{Source: "s", DetailType: "t"}, + code: "InvalidArgument", + message: "Parameter Detail is not valid. Reason: Detail is a required argument.", + }, + { + name: "source checked before detail type", + event: driver.Event{}, + code: "InvalidArgument", + message: "Parameter Source is not valid. Reason: Source is a required argument.", + }, + {name: "detail is an array", event: driver.Event{Source: "s", DetailType: "t", Detail: "[]"}, code: "MalformedDetail", message: "Detail is malformed."}, + {name: "detail is null", event: driver.Event{Source: "s", DetailType: "t", Detail: "null"}, code: "MalformedDetail", message: "Detail is malformed."}, + {name: "detail is a string", event: driver.Event{Source: "s", DetailType: "t", Detail: `"x"`}, code: "MalformedDetail", message: "Detail is malformed."}, + {name: "detail is not json", event: driver.Event{Source: "s", DetailType: "t", Detail: "{"}, code: "MalformedDetail", message: "Detail is malformed."}, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + m, _ := newTestMock() + + results, err := m.PutEventEntries(context.Background(), []driver.Event{tc.event}) + require.NoError(t, err) + require.Len(t, results, 1) + assert.Empty(t, results[0].EventID) + assert.Equal(t, tc.code, results[0].ErrorCode) + assert.Equal(t, tc.message, results[0].ErrorMessage) + + history, err := m.GetEventHistory(context.Background(), "", 0) + require.NoError(t, err) + assert.Empty(t, history) + }) + } +} + +func TestPutEventEntriesAlignedByIndex(t *testing.T) { + m, _ := newTestMock() + ctx := context.Background() + + results, err := m.PutEventEntries(ctx, []driver.Event{ + {Source: "s", DetailType: "t", Detail: "{}", EventBus: "ghost"}, + {DetailType: "t", Detail: "{}"}, + {Source: "s", DetailType: "kept", Detail: `{"k":1}`}, + {Source: "s", DetailType: "t", Detail: "nope"}, + }) + require.NoError(t, err) + require.Len(t, results, 4) + + assert.NotEmpty(t, results[0].EventID) + assert.Empty(t, results[0].ErrorCode) + assert.Equal(t, "InvalidArgument", results[1].ErrorCode) + assert.NotEmpty(t, results[2].EventID) + assert.Equal(t, "MalformedDetail", results[3].ErrorCode) + + history, err := m.GetEventHistory(ctx, "", 0) + require.NoError(t, err) + require.Len(t, history, 1) + assert.Equal(t, results[2].EventID, history[0].ID) +} + +func TestPutEventsNonexistentBusNotDelivered(t *testing.T) { + m, _ := newTestMock() + ctx := context.Background() + + result, err := m.PutEvents(ctx, []driver.Event{ + {Source: "s", DetailType: "t", Detail: "{}", EventBus: "ghost"}, + }) + require.NoError(t, err) + assert.Equal(t, 1, result.SuccessCount) + assert.Equal(t, 0, result.FailCount) + require.Len(t, result.EventIDs, 1) + + _, err = m.GetEventHistory(ctx, "ghost", 0) + assert.True(t, cerrors.IsNotFound(err), "the dropped event must not create the bus") +} + +func TestPutEventEntriesEntryCount(t *testing.T) { + m, _ := newTestMock() + + _, err := m.PutEventEntries(context.Background(), nil) + require.Error(t, err) + assert.True(t, cerrors.IsInvalidArgument(err)) + + events := make([]driver.Event, 11) + for i := range events { + events[i] = driver.Event{Source: "s", DetailType: "t", Detail: "{}"} + } + + _, err = m.PutEventEntries(context.Background(), events) + require.Error(t, err) + assert.True(t, cerrors.IsInvalidArgument(err)) +} + +// detailOfSize returns a JSON object Detail whose encoded length is n bytes. +func detailOfSize(n int) string { + const wrapper = len(`{"p":""}`) + + return `{"p":"` + strings.Repeat("x", n-wrapper) + `"}` +} + +func TestPutEventEntriesSizeLimit(t *testing.T) { + const limit = 1 << 20 + + tests := []struct { + name string + region string + size int // total request size in bytes + withErr bool + }{ + {name: "just under 1 MB", region: "us-east-1", size: limit - 1}, + {name: "exactly 1 MB", region: "us-east-1", size: limit, withErr: true}, + {name: "over 256 KB is fine in a 1 MB region", region: "eu-west-1", size: 300 << 10}, + {name: "256 KB region rejects at 256 KB", region: "ap-southeast-5", size: 256 << 10, withErr: true}, + {name: "256 KB region accepts under 256 KB", region: "mx-central-1", size: (256 << 10) - 1}, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + opts := config.NewOptions(config.WithRegion(tc.region)) + m := New(opts) + + // Source "s" and DetailType "t" take 2 bytes of the total. + event := driver.Event{Source: "s", DetailType: "t", Detail: detailOfSize(tc.size - 2)} + + results, err := m.PutEventEntries(context.Background(), []driver.Event{event}) + if !tc.withErr { + require.NoError(t, err) + require.Len(t, results, 1) + assert.NotEmpty(t, results[0].EventID) + + return + } + + require.Error(t, err) + assert.True(t, cerrors.IsInvalidArgument(err)) + assert.Equal(t, "Total size of the entries in the request is over the limit.", cerrors.Message(err)) + }) + } +} + +func TestPutEventEntriesSizeCountsAllEntriesAndTime(t *testing.T) { + m, _ := newTestMock() + + // Two entries that each fit, but not together. + half := driver.Event{Source: "s", DetailType: "t", Detail: detailOfSize(1 << 19)} + + _, err := m.PutEventEntries(context.Background(), []driver.Event{half, half}) + require.Error(t, err) + + // A set Time adds 14 bytes, which tips an entry sitting 10 bytes under the limit. + event := driver.Event{Source: "s", DetailType: "t", Detail: detailOfSize((1 << 20) - 12)} + + _, err = m.PutEventEntries(context.Background(), []driver.Event{event}) + require.NoError(t, err) + + event.Time = time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC) + + _, err = m.PutEventEntries(context.Background(), []driver.Event{event}) + require.Error(t, err) +} + +func TestPutTargetsInputModesMutuallyExclusive(t *testing.T) { + const transformer = `{"InputTemplate":"\"x\""}` + + tests := []struct { + name string + target driver.Target + }{ + {name: "input and input path", target: driver.Target{ID: "t1", ARN: "arn", Input: "{}", InputPath: "$.detail"}}, + {name: "input and transformer", target: driver.Target{ID: "t1", ARN: "arn", Input: "{}", InputTransformer: transformer}}, + {name: "input path and transformer", target: driver.Target{ID: "t1", ARN: "arn", InputPath: "$.detail", InputTransformer: transformer}}, + { + name: "all three", + target: driver.Target{ID: "t1", ARN: "arn", Input: "{}", InputPath: "$.detail", InputTransformer: transformer}, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + m, _ := newTestMock() + ctx := context.Background() + createTestRule(t, m, "", "r1") + + err := m.PutTargets(ctx, "", "r1", []driver.Target{{ID: "ok", ARN: "arn", Input: "{}"}, tc.target}) + require.Error(t, err) + assert.True(t, cerrors.IsInvalidArgument(err)) + assert.Equal(t, "Only one of Input, InputPath, or InputTransformer must be provided for target t1.", cerrors.Message(err)) + + targets, err := m.ListTargets(ctx, "", "r1") + require.NoError(t, err) + assert.Empty(t, targets, "a rejected request must not store any target") + }) + } + + t.Run("one mode per target is accepted", func(t *testing.T) { + m, _ := newTestMock() + createTestRule(t, m, "", "r1") + + err := m.PutTargets(context.Background(), "", "r1", []driver.Target{ + {ID: "a", ARN: "arn", Input: "{}"}, + {ID: "b", ARN: "arn", InputPath: "$.detail"}, + {ID: "c", ARN: "arn", InputTransformer: transformer}, + {ID: "d", ARN: "arn"}, + }) + require.NoError(t, err) + }) +} diff --git a/providers/aws/eventbridge/target_input.go b/providers/aws/eventbridge/target_input.go index 9124f9e67..3981022b5 100644 --- a/providers/aws/eventbridge/target_input.go +++ b/providers/aws/eventbridge/target_input.go @@ -4,13 +4,39 @@ import ( "encoding/json" "strings" + "github.com/stackshy/cloudemu/v2/errors" "github.com/stackshy/cloudemu/v2/services/eventbus/driver" ) +// validateTargetInputs rejects a PutTargets request in which any target sets +// more than one of Input, InputPath and InputTransformer. EventBridge treats the +// three as mutually exclusive and fails the whole request, storing no target. +func validateTargetInputs(targets []driver.Target) error { + for i := range targets { + t := &targets[i] + + set := 0 + + for _, v := range []string{t.Input, t.InputPath, t.InputTransformer} { + if v != "" { + set++ + } + } + + if set > 1 { + return errors.Newf(errors.InvalidArgument, + "Only one of Input, InputPath, or InputTransformer must be provided for target %s.", t.ID) + } + } + + return nil +} + // targetBody computes the payload EventBridge delivers to a single target given // the rendered event envelope. Precedence mirrors real EventBridge, where a // target carries at most one of these: InputTransformer > Input > InputPath > -// the raw envelope. +// the raw envelope. PutTargets rejects a target with more than one of them, so +// the order only matters for targets restored from older state. func targetBody(t *driver.Target, envelope []byte, reserved map[string]json.RawMessage) string { if t.InputTransformer != "" { if body, ok := applyInputTransformer(t.InputTransformer, envelope, reserved); ok { diff --git a/providers/aws/lifecycle_events_test.go b/providers/aws/lifecycle_events_test.go index 0558994ab..f94597212 100644 --- a/providers/aws/lifecycle_events_test.go +++ b/providers/aws/lifecycle_events_test.go @@ -848,7 +848,11 @@ func TestLifecycleEventLoopIsBounded(t *testing.T) { t.Fatalf("PutRule: %v", err) } - if err := p.EventBridge.PutTargets(ctx, "", "restart", []ebdriver.Target{{ID: "sm", ARN: smArn}}); err != nil { + // A constant Input keeps each execution's input small. With the default + // envelope input, every hop nests the previous event inside the next one, + // so the event grows until PutEvents rejects it at the 1 MB request limit + // before the recursion guard is reached. + if err := p.EventBridge.PutTargets(ctx, "", "restart", []ebdriver.Target{{ID: "sm", ARN: smArn, Input: "{}"}}); err != nil { t.Fatalf("PutTargets: %v", err) } diff --git a/server/aws/eventbridge/operations.go b/server/aws/eventbridge/operations.go index 5ff18adc0..213104b0e 100644 --- a/server/aws/eventbridge/operations.go +++ b/server/aws/eventbridge/operations.go @@ -1,24 +1,24 @@ package eventbridge import ( + "context" "encoding/json" "net/http" "strings" + cerrors "github.com/stackshy/cloudemu/v2/errors" "github.com/stackshy/cloudemu/v2/internal/eventmatch" "github.com/stackshy/cloudemu/v2/server/wire" ebdriver "github.com/stackshy/cloudemu/v2/services/eventbus/driver" "github.com/stackshy/cloudemu/v2/services/scope" ) -// maxPutEventsEntries is the maximum number of entries EventBridge accepts in a -// single PutEvents request. -const maxPutEventsEntries = 10 - -// putEventsEntryCountMessage is the ValidationException detail EventBridge -// returns when a PutEvents request carries fewer than 1 or more than 10 entries. -const putEventsEntryCountMessage = "1 validation error detected: Value at 'entries' failed to satisfy " + - "constraint: Member must have length less than or equal to 10 and greater than or equal to 1." +// entryPublisher is the AWS-specific PutEvents surface that reports each +// entry's outcome by index, asserted against the provider (the portable +// EventBus driver only returns counts). +type entryPublisher interface { + PutEventEntries(ctx context.Context, events []ebdriver.Event) ([]ebdriver.PutEventsEntryResult, error) +} // --- event buses --- @@ -316,63 +316,46 @@ func (h *Handler) listTargetsByRule(w http.ResponseWriter, r *http.Request) { // --- events --- func (h *Handler) putEvents(w http.ResponseWriter, r *http.Request) { - var req putEventsRequest - if !wire.DecodeJSON(w, r, &req) { + publisher, ok := h.bus.(entryPublisher) + if !ok { + writeErr(w, cerrors.New(cerrors.Unimplemented, "PutEvents not supported")) return } - // EventBridge bounds a PutEvents request to between 1 and 10 entries; an - // out-of-range batch is rejected wholesale with a ValidationException. - if n := len(req.Entries); n < 1 || n > maxPutEventsEntries { - wire.WriteJSONError(w, http.StatusBadRequest, "ValidationException", putEventsEntryCountMessage) + var req putEventsRequest + if !wire.DecodeJSON(w, r, &req) { return } - // Each entry is aligned to a result slot; entries with a malformed Detail - // fail up front (ErrorCode=MalformedDetail) and never reach the bus, while - // well-formed entries are published and back-filled with their EventId. - results := make([]putEventsResultEntry, len(req.Entries)) - - events := make([]ebdriver.Event, 0, len(req.Entries)) - slots := make([]int, 0, len(req.Entries)) - failed := 0 + events := make([]ebdriver.Event, len(req.Entries)) for i := range req.Entries { e := &req.Entries[i] - if !isValidDetail(e.Detail) { - results[i] = putEventsResultEntry{ErrorCode: "MalformedDetail", ErrorMessage: "Detail is malformed."} - failed++ - - continue - } - - slots = append(slots, i) - events = append(events, ebdriver.Event{ + events[i] = ebdriver.Event{ Source: e.Source, DetailType: e.DetailType, Detail: e.Detail, EventBus: busNameOrDefault(e.EventBusName), Resources: e.Resources, Time: entryTime(e.Time), - }) + } } - res, err := h.bus.PutEvents(r.Context(), events) + results, err := publisher.PutEventEntries(r.Context(), events) if err != nil { writeErr(w, err) return } - for i, id := range res.EventIDs { - if i < len(slots) { - results[slots[i]] = putEventsResultEntry{EventID: id} + resp := putEventsResponse{Entries: make([]putEventsResultEntry, len(results))} + for i, res := range results { + resp.Entries[i] = putEventsResultEntry{EventID: res.EventID, ErrorCode: res.ErrorCode, ErrorMessage: res.ErrorMessage} + if res.ErrorCode != "" { + resp.FailedEntryCount++ } } - wire.WriteJSON(w, putEventsResponse{ - FailedEntryCount: failed + res.FailCount, - Entries: results, - }) + wire.WriteJSON(w, resp) } // testEventPattern evaluates a sample event against an event pattern using the diff --git a/server/aws/eventbridge/put_events_validation_sdk_test.go b/server/aws/eventbridge/put_events_validation_sdk_test.go new file mode 100644 index 000000000..466b7c32f --- /dev/null +++ b/server/aws/eventbridge/put_events_validation_sdk_test.go @@ -0,0 +1,165 @@ +package eventbridge_test + +import ( + "context" + "encoding/json" + "errors" + "strings" + "testing" + + "github.com/aws/aws-sdk-go-v2/aws" + awseb "github.com/aws/aws-sdk-go-v2/service/eventbridge" + ebtypes "github.com/aws/aws-sdk-go-v2/service/eventbridge/types" + "github.com/aws/smithy-go" +) + +// TestSDKPutEventsEntryIDsAlignWithRequest checks that each result entry belongs +// to the request entry at the same index. An entry for a missing bus succeeds +// with its own EventId (real EventBridge drops it silently), and the EventId at +// a later index is the id of the event that was actually delivered. +func TestSDKPutEventsEntryIDsAlignWithRequest(t *testing.T) { + eb, sqs := newEBAndSQS(t) + ctx := context.Background() + + url, arn := makeQueue(t, sqs, "eb-align") + + if _, err := eb.PutRule(ctx, &awseb.PutRuleInput{ + Name: aws.String("r"), EventPattern: aws.String(`{"source":["my.app"]}`), + }); err != nil { + t.Fatalf("PutRule: %v", err) + } + + if _, err := eb.PutTargets(ctx, &awseb.PutTargetsInput{ + Rule: aws.String("r"), Targets: []ebtypes.Target{{Id: aws.String("1"), Arn: aws.String(arn)}}, + }); err != nil { + t.Fatalf("PutTargets: %v", err) + } + + out, err := eb.PutEvents(ctx, &awseb.PutEventsInput{Entries: []ebtypes.PutEventsRequestEntry{ + {Source: aws.String("my.app"), DetailType: aws.String("t"), Detail: aws.String("{}"), EventBusName: aws.String("ghost")}, + {DetailType: aws.String("t"), Detail: aws.String("{}")}, + {Source: aws.String("my.app"), DetailType: aws.String("t"), Detail: aws.String(`{"n":1}`)}, + }}) + if err != nil { + t.Fatalf("PutEvents: %v", err) + } + + if out.FailedEntryCount != 1 || len(out.Entries) != 3 { + t.Fatalf("FailedEntryCount=%d entries=%d, want 1 and 3", out.FailedEntryCount, len(out.Entries)) + } + + if aws.ToString(out.Entries[0].EventId) == "" || out.Entries[0].ErrorCode != nil { + t.Fatalf("entry 0 (missing bus) = %+v, want an EventId and no error", out.Entries[0]) + } + + if aws.ToString(out.Entries[1].ErrorCode) != "InvalidArgument" { + t.Fatalf("entry 1 ErrorCode = %q, want InvalidArgument", aws.ToString(out.Entries[1].ErrorCode)) + } + + body, n := receiveOne(t, sqs, url) + if n != 1 { + t.Fatalf("delivered %d messages, want 1", n) + } + + var env struct { + ID string `json:"id"` + } + if err := json.Unmarshal([]byte(body), &env); err != nil { + t.Fatalf("body not JSON: %v", err) + } + + if got := aws.ToString(out.Entries[2].EventId); got != env.ID { + t.Fatalf("entry 2 EventId = %q, delivered event id = %q", got, env.ID) + } +} + +func TestSDKPutEventsRequiredFields(t *testing.T) { + client := newEventBridgeClient(t) + + out, err := client.PutEvents(context.Background(), &awseb.PutEventsInput{Entries: []ebtypes.PutEventsRequestEntry{ + {DetailType: aws.String("t"), Detail: aws.String("{}")}, + {Source: aws.String("s"), Detail: aws.String("{}")}, + {Source: aws.String("s"), DetailType: aws.String("t")}, + }}) + if err != nil { + t.Fatalf("PutEvents: %v", err) + } + + want := []string{ + "Parameter Source is not valid. Reason: Source is a required argument.", + "Parameter DetailType is not valid. Reason: DetailType is a required argument.", + "Parameter Detail is not valid. Reason: Detail is a required argument.", + } + + if out.FailedEntryCount != 3 { + t.Fatalf("FailedEntryCount = %d, want 3", out.FailedEntryCount) + } + + for i, msg := range want { + e := out.Entries[i] + if aws.ToString(e.ErrorCode) != "InvalidArgument" || aws.ToString(e.ErrorMessage) != msg { + t.Fatalf("entry %d = %q/%q, want InvalidArgument/%q", i, aws.ToString(e.ErrorCode), aws.ToString(e.ErrorMessage), msg) + } + } +} + +func requireValidationException(t *testing.T, err error, wantMsg string) { + t.Helper() + + if err == nil { + t.Fatal("call succeeded, want ValidationException") + } + + var apiErr smithy.APIError + if !errors.As(err, &apiErr) { + t.Fatalf("error is not a smithy.APIError: %v", err) + } + + if apiErr.ErrorCode() != "ValidationException" || apiErr.ErrorMessage() != wantMsg { + t.Fatalf("error = %s: %q, want ValidationException: %q", apiErr.ErrorCode(), apiErr.ErrorMessage(), wantMsg) + } +} + +func TestSDKPutEventsOverSizeLimit(t *testing.T) { + client := newEventBridgeClient(t) + + detail := `{"p":"` + strings.Repeat("x", 1<<20) + `"}` + + _, err := client.PutEvents(context.Background(), &awseb.PutEventsInput{Entries: []ebtypes.PutEventsRequestEntry{ + {Source: aws.String("s"), DetailType: aws.String("t"), Detail: aws.String(detail)}, + }}) + + requireValidationException(t, err, "Total size of the entries in the request is over the limit.") +} + +func TestSDKPutTargetsConflictingInputs(t *testing.T) { + client := newEventBridgeClient(t) + ctx := context.Background() + + if _, err := client.PutRule(ctx, &awseb.PutRuleInput{ + Name: aws.String("r"), EventPattern: aws.String(`{"source":["s"]}`), + }); err != nil { + t.Fatalf("PutRule: %v", err) + } + + _, err := client.PutTargets(ctx, &awseb.PutTargetsInput{ + Rule: aws.String("r"), + Targets: []ebtypes.Target{{ + Id: aws.String("t1"), + Arn: aws.String("arn:aws:sqs:us-east-1:123456789012:q"), + Input: aws.String("{}"), + InputPath: aws.String("$.detail"), + }}, + }) + + requireValidationException(t, err, "Only one of Input, InputPath, or InputTransformer must be provided for target t1.") + + list, err := client.ListTargetsByRule(ctx, &awseb.ListTargetsByRuleInput{Rule: aws.String("r")}) + if err != nil { + t.Fatalf("ListTargetsByRule: %v", err) + } + + if len(list.Targets) != 0 { + t.Fatalf("targets stored after a rejected PutTargets: %+v", list.Targets) + } +} diff --git a/server/aws/eventbridge/types.go b/server/aws/eventbridge/types.go index 043c51237..d3cf5b19d 100644 --- a/server/aws/eventbridge/types.go +++ b/server/aws/eventbridge/types.go @@ -295,19 +295,6 @@ func paginateByCursor[T any](items []T, nextToken string, limit int, keyOf func( return items, "" } -// isValidDetail reports whether a PutEvents entry's Detail is acceptable: -// empty (defaulted downstream) or a well-formed JSON object. Real EventBridge -// fails any other Detail with ErrorCode=MalformedDetail. -func isValidDetail(detail string) bool { - if detail == "" { - return true - } - - var obj map[string]json.RawMessage - - return json.Unmarshal([]byte(detail), &obj) == nil -} - // entryTime converts a PutEvents entry's optional wire-form timestamp (Unix // epoch seconds) to a time.Time. A nil entry (the caller omitted Time) yields // the zero time, which the driver defaults to the PutEvents call's own time, diff --git a/services/eventbus/driver/driver.go b/services/eventbus/driver/driver.go index 743bb997f..98f5677f1 100644 --- a/services/eventbus/driver/driver.go +++ b/services/eventbus/driver/driver.go @@ -156,6 +156,15 @@ type PublishResult struct { EventIDs []string } +// PutEventsEntryResult is the outcome of one entry in an EventBridge PutEvents +// call, aligned by index with the request entries. A published entry carries +// EventID; a rejected entry carries ErrorCode and ErrorMessage instead. +type PutEventsEntryResult struct { + EventID string + ErrorCode string + ErrorMessage string +} + // EventBus is the interface that event bus provider implementations must satisfy. type EventBus interface { // Bus management From 5aacca2e711a13f6f753ed3492d0336f7ecb4c48 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Patel Date: Thu, 22 Oct 2026 05:20:15 +0530 Subject: [PATCH 05/31] fix(aws-cloudwatchlogs): space-delimited filter patterns and metric value extraction (#1658) * fix(aws-cloudwatchlogs): space-delimited filter patterns and $field metric values Parse [field, ..., name=value] patterns for FilterLogEvents, metric filters, subscription filters and TestMetricFilter, resolve $name/$N/$.path metric values from the matched event, and stop accepting a literal space in %regex% bodies. Part of #1476. * fix(aws-cloudwatchlogs): allow a literal space in %regex% filter patterns The AWS syntax page's own example %^starting|^initializing|^shutting down% contains one. --- .../aws/cloudwatchlogs/cloudwatchlogs.go | 36 ++- .../cloudwatchlogs/filterpattern/delimited.go | 293 ++++++++++++++++++ .../filterpattern/delimited_test.go | 123 ++++++++ .../filterpattern/field_cond.go | 156 ++++++++++ .../cloudwatchlogs/filterpattern/json_eval.go | 27 +- .../filterpattern/json_parse.go | 3 + .../cloudwatchlogs/filterpattern/pattern.go | 46 ++- .../filterpattern/pattern_test.go | 18 +- .../aws/cloudwatchlogs/filterpattern/regex.go | 4 +- .../cloudwatchlogs/space_delimited_test.go | 61 ++++ .../space_delimited_sdk_test.go | 90 ++++++ 11 files changed, 828 insertions(+), 29 deletions(-) create mode 100644 providers/aws/cloudwatchlogs/filterpattern/delimited.go create mode 100644 providers/aws/cloudwatchlogs/filterpattern/delimited_test.go create mode 100644 providers/aws/cloudwatchlogs/filterpattern/field_cond.go create mode 100644 providers/aws/cloudwatchlogs/space_delimited_test.go create mode 100644 server/aws/cloudwatchlogs/space_delimited_sdk_test.go diff --git a/providers/aws/cloudwatchlogs/cloudwatchlogs.go b/providers/aws/cloudwatchlogs/cloudwatchlogs.go index a2619d267..dcb8e5c7a 100644 --- a/providers/aws/cloudwatchlogs/cloudwatchlogs.go +++ b/providers/aws/cloudwatchlogs/cloudwatchlogs.go @@ -244,9 +244,13 @@ func (m *Mock) evaluateMetricFilters(g *logGroup, events []driver.LogEvent) { } for i := range events { - if pattern.Match(events[i].Message) { + if !pattern.Match(events[i].Message) { + continue + } + + if v, ok := metricFilterValue(mf.MetricValue, pattern, events[i].Message); ok { matched++ - total += metricFilterValue(mf.MetricValue) + total += v } } @@ -268,20 +272,32 @@ func (m *Mock) evaluateMetricFilters(g *logGroup, events []driver.LogEvent) { } } -// metricFilterValue resolves a metric filter's metricValue to the number each -// matching event contributes. A literal number is used directly; anything else -// (an empty value, or a "$field" token this emulator does not extract) counts -// as 1 per matching event, which is the common "count occurrences" filter. -func metricFilterValue(raw string) float64 { +// metricFilterValue resolves a metric filter's metricValue to the number one +// matching event contributes. A literal number is used directly; a "$field" +// reference ("$.latency", "$size", "$3") takes the value the pattern extracts +// from the event, and an event whose referenced value is missing or not a +// number contributes nothing. An empty value counts the event as 1. +func metricFilterValue(raw string, pattern *filterpattern.Pattern, message string) (float64, bool) { if raw == "" { - return 1 + return 1, true } if v, err := strconv.ParseFloat(raw, 64); err == nil { - return v + return v, true + } + + if !strings.HasPrefix(raw, "$") { + return 1, true } - return 1 + field, ok := pattern.Value(message, raw) + if !ok { + return 0, false + } + + v, err := strconv.ParseFloat(field, 64) + + return v, err == nil } // GetLogEvents retrieves log events matching the query. diff --git a/providers/aws/cloudwatchlogs/filterpattern/delimited.go b/providers/aws/cloudwatchlogs/filterpattern/delimited.go new file mode 100644 index 000000000..b7ba1545c --- /dev/null +++ b/providers/aws/cloudwatchlogs/filterpattern/delimited.go @@ -0,0 +1,293 @@ +package filterpattern + +import ( + "strconv" + "strings" +) + +// ellipsis stands for any number of unnamed fields in a space-delimited +// pattern. +const ellipsis = "..." + +// delimField is one comma-separated entry of a [ ... ] pattern: a named or +// unnamed field with an optional condition, or an ellipsis. +type delimField struct { + name string + ellipsis bool + cond fieldCond +} + +// delimPattern is a parsed space-delimited pattern such as +// [ip, user, ..., status_code=4*, bytes>1000]. +type delimPattern struct { + fields []delimField + hasEllipsis bool + regexCount int +} + +// span is the half-open range of message fields bound to a pattern field. +type span struct{ start, end int } + +func parseDelimitedPattern(s string) (*delimPattern, error) { + if !strings.HasSuffix(s, "]") { + return nil, invalid("Invalid filter pattern: space-delimited pattern must end with ']'") + } + + parts, err := splitFields(s[1 : len(s)-1]) + if err != nil { + return nil, err + } + + p := &delimPattern{} + + for _, part := range parts { + f, regexes, ferr := parseDelimField(part) + if ferr != nil { + return nil, ferr + } + + p.regexCount += regexes + p.hasEllipsis = p.hasEllipsis || f.ellipsis + p.fields = append(p.fields, f) + } + + if p.regexCount > maxRegexPerStructuredPattern { + return nil, invalid("Invalid filter pattern: at most 2 regular expressions are allowed in a space-delimited filter pattern") + } + + return p, nil +} + +// splitFields splits a pattern body on commas that are not inside a %regex% +// or a quoted string. An empty body yields no fields; "[]" matches any event. +func splitFields(body string) ([]string, error) { + if strings.TrimSpace(body) == "" { + return nil, nil + } + + var ( + parts []string + sc fieldScanner + start int + ) + + for i := 0; i < len(body); i++ { + if sc.step(body[i]) { + parts = append(parts, body[start:i]) + start = i + 1 + } + } + + if sc.inRegex || sc.inQuote { + return nil, invalid("Invalid filter pattern: unterminated regex or quoted string") + } + + return append(parts, body[start:]), nil +} + +// fieldScanner tracks whether the scan is inside a %regex% or a quoted +// string, where a comma does not separate fields. +type fieldScanner struct{ inRegex, inQuote bool } + +// step consumes one byte and reports whether it is a field separator. +func (f *fieldScanner) step(c byte) bool { + switch { + case c == '"' && !f.inRegex: + f.inQuote = !f.inQuote + case c == '%' && !f.inQuote: + f.inRegex = !f.inRegex + case c == ',': + return !f.inRegex && !f.inQuote + } + + return false +} + +func parseDelimField(text string) (delimField, int, error) { + text = strings.TrimSpace(text) + + switch text { + case "": + return delimField{}, 0, nil + case ellipsis: + return delimField{ellipsis: true}, 0, nil + } + + toks, err := lexJSON(text) + if err != nil { + return delimField{}, 0, err + } + + if toks[0].kind != tokWord || !isFieldName(toks[0].text) { + return delimField{}, 0, invalid("Invalid field '" + text + "' in space-delimited filter pattern") + } + + f := delimField{name: toks[0].text} + if len(toks) == 1 { + return f, 0, nil + } + + cp := &condParser{toks: toks, name: f.name} + + cond, err := cp.parseOr() + if err != nil { + return delimField{}, 0, err + } + + if cp.pos != len(cp.toks) { + return delimField{}, 0, invalid("Invalid field '" + text + "' in space-delimited filter pattern") + } + + f.cond = cond + + return f, cp.regexes, nil +} + +func isFieldName(s string) bool { + for i := range len(s) { + if !isSelectorChar(s[i]) || s[i] == '-' { + return false + } + } + + return s != "" +} + +func (p *delimPattern) match(message string) bool { + _, _, ok := p.bind(message) + + return ok +} + +// bind assigns the message's fields to the pattern's fields. Without an +// ellipsis the last pattern field takes the rest of the message, which is why +// the docs ask for a trailing blank field ([w1=ERROR, w2]). With an ellipsis +// each named field takes exactly one message field. +func (p *delimPattern) bind(message string) ([]string, []span, bool) { + tokens := tokenizeMessage(message) + if len(p.fields) == 0 { + return tokens, nil, true + } + + spans := make([]span, len(p.fields)) + + return tokens, spans, p.bindFrom(tokens, spans, 0, 0) +} + +func (p *delimPattern) bindFrom(tokens []string, spans []span, fi, ti int) bool { + if fi == len(p.fields) { + return ti == len(tokens) + } + + f := p.fields[fi] + + if f.ellipsis { + for j := ti; j <= len(tokens); j++ { + spans[fi] = span{ti, j} + if p.bindFrom(tokens, spans, fi+1, j) { + return true + } + } + + return false + } + + if ti >= len(tokens) { + return false + } + + end := ti + 1 + if !p.hasEllipsis && fi == len(p.fields)-1 { + end = len(tokens) + } + + if f.cond != nil && !f.cond.eval(strings.Join(tokens[ti:end], " ")) { + return false + } + + spans[fi] = span{ti, end} + + return p.bindFrom(tokens, spans, fi+1, end) +} + +// extract names each bound message field: "$name" for a named field and +// "$N" (1-based position in the message) for unnamed fields and the fields an +// ellipsis covers. +func (p *delimPattern) extract(message string) map[string]string { + out := map[string]string{} + + tokens, spans, ok := p.bind(message) + if !ok { + return out + } + + if len(p.fields) == 0 { + for i, t := range tokens { + out[positional(i)] = t + } + + return out + } + + for i, f := range p.fields { + sp := spans[i] + + switch { + case f.ellipsis: + for t := sp.start; t < sp.end; t++ { + out[positional(t)] = tokens[t] + } + case f.name != "": + out["$"+f.name] = strings.Join(tokens[sp.start:sp.end], " ") + default: + out[positional(sp.start)] = strings.Join(tokens[sp.start:sp.end], " ") + } + } + + return out +} + +func positional(i int) string { return "$" + strconv.Itoa(i+1) } + +// tokenizeMessage splits a log event into space-delimited fields. Text in +// double quotes or square brackets is one field, without its delimiters. +func tokenizeMessage(message string) []string { + var tokens []string + + for i := 0; i < len(message); { + if message[i] == ' ' || message[i] == '\t' { + i++ + continue + } + + tok, next := readField(message, i) + tokens = append(tokens, tok) + i = next + } + + return tokens +} + +func readField(message string, i int) (field string, next int) { + closing := byte(0) + + switch message[i] { + case '"': + closing = '"' + case '[': + closing = ']' + } + + if closing != 0 { + if end := strings.IndexByte(message[i+1:], closing); end >= 0 { + return message[i+1 : i+1+end], i + end + delimiterPair + } + } + + end := strings.IndexAny(message[i:], " \t") + if end < 0 { + end = len(message) - i + } + + return message[i : i+end], i + end +} diff --git a/providers/aws/cloudwatchlogs/filterpattern/delimited_test.go b/providers/aws/cloudwatchlogs/filterpattern/delimited_test.go new file mode 100644 index 000000000..57050483b --- /dev/null +++ b/providers/aws/cloudwatchlogs/filterpattern/delimited_test.go @@ -0,0 +1,123 @@ +package filterpattern + +import ( + "maps" + "testing" +) + +// Space-delimited example events from the filter pattern syntax reference +// and the TestMetricFilter API examples. +const ( + apacheHTML = `127.0.0.1 Prod frank [10/Oct/2000:13:25:15 -0700] "GET /index.html HTTP/1.0" 404 1534` + apache200 = `127.0.0.1 - frank [10/Oct/2000:13:25:15 -0700] "GET /apache_pb.gif HTTP/1.0" 200 1534` + apache500 = `127.0.0.1 - frank [10/Oct/2000:13:35:22 -0700] "GET /apache_pb.gif HTTP/1.0" 500 5324` + apacheIdx = `127.0.0.1 - frank [10/Oct/2000:13:25:15 -0700] "GET /index.html HTTP/1.0" 404 1534` + apacheAbt = `127.0.0.1 - frank [10/Oct/2000:13:35:22 -0700] "GET /about-us/index.html HTTP/1.0" 200 5324` + + infoGet = "INFO 09/25/2014 12:00:00 GET /service/resource/67 1200" + infoPost = "INFO 09/25/2014 12:00:01 POST /service/resource/67/part/111 1310" + warnLine = "WARNING 09/25/2014 12:00:02 Invalid user request" + errLine = "ERROR 09/25/2014 12:00:02 Failed to process request" +) + +func TestSpaceDelimitedMatch(t *testing.T) { + tests := []struct { + pattern, event string + want bool + }{ + {`[ip=%127\.0\.0\.[1-9]%, user, username, timestamp, request =*.html*, status_code = 4*, bytes]`, apacheHTML, true}, + {`[ip=%127\.0\.0\.[1-9]%, user, username, timestamp, request =*.html*, status_code = 5*, bytes]`, apacheHTML, false}, + {`[..., request =*.html*, status_code = 4*, bytes]`, apacheHTML, true}, + {`[ip, user, username, timestamp, request =*.html*, status_code = 404 || status_code = 410, bytes]`, apacheHTML, true}, + {`[ip, server, username, timestamp, request, status_code, bytes > 1000]`, apacheHTML, true}, + {`[ip, server, username, timestamp, request, status_code, bytes > 2000]`, apacheHTML, false}, + {`[ip, server, username, timestamp, request, status_code, bytes >= 1534]`, apacheHTML, true}, + {`[ip, server, username, timestamp, request, status_code != 404, bytes]`, apacheHTML, false}, + {`[ip, server]`, apacheHTML, true}, + {`[ip, server, username, timestamp, request, status_code, bytes, extra]`, apacheHTML, false}, + {`[w1=ERROR, w2]`, errLine, true}, + {`[w1=ERROR, w2]`, warnLine, false}, + {`[w1=ERROR]`, errLine, false}, + {`[w1=ERROR || w1=WARNING, w2]`, warnLine, true}, + {`[w1=ERROR || w1=%WARN%, w2]`, warnLine, true}, + {`[w1!=ERROR && w1!=WARNING, w2]`, infoGet, true}, + {`[w1!=ERROR && w1!=WARNING, w2]`, errLine, false}, + {`[logLevel, date, time, method, url=%/service/resource/[0-9]+$%, response_time]`, infoGet, true}, + {`[logLevel, date, time, method, url=%/service/resource/[0-9]+$%, response_time]`, infoPost, false}, + {`[logLevel, date, time, method, url=%/service/resource/[0-9]+/part/[0-9]+$%, response_time]`, infoPost, true}, + {`[..., status_code=200, size]`, apache200, true}, + {`[..., status_code=200, size]`, apache500, false}, + {`[..., request=*.html*, status_code=4*,]`, apacheIdx, true}, + {`[..., request=*.html*, status_code=4*,]`, apacheAbt, false}, + {`[]`, apache200, true}, + {`[a, ..., b="1534"]`, apache200, true}, + } + + for _, tc := range tests { + t.Run(tc.pattern+" "+tc.event, func(t *testing.T) { + if got := mustParse(t, tc.pattern).Match(tc.event); got != tc.want { + t.Fatalf("Match = %v, want %v", got, tc.want) + } + }) + } +} + +// TestSpaceDelimitedExtractedValues follows the TestMetricFilter API examples. +func TestSpaceDelimitedExtractedValues(t *testing.T) { + tests := []struct { + pattern string + want map[string]string + }{ + {`[ip, identity, user_id, timestamp, request, status_code, size]`, map[string]string{ + "$ip": "127.0.0.1", "$identity": "-", "$user_id": "frank", "$timestamp": "10/Oct/2000:13:25:15 -0700", + "$request": "GET /apache_pb.gif HTTP/1.0", "$status_code": "200", "$size": "1534", + }}, + {`[..., size]`, map[string]string{ + "$1": "127.0.0.1", "$2": "-", "$3": "frank", "$4": "10/Oct/2000:13:25:15 -0700", + "$5": "GET /apache_pb.gif HTTP/1.0", "$6": "200", "$size": "1534", + }}, + {`[]`, map[string]string{ + "$1": "127.0.0.1", "$2": "-", "$3": "frank", "$4": "10/Oct/2000:13:25:15 -0700", + "$5": "GET /apache_pb.gif HTTP/1.0", "$6": "200", "$7": "1534", + }}, + {`[..., status_code=200, size]`, map[string]string{ + "$1": "127.0.0.1", "$2": "-", "$3": "frank", "$4": "10/Oct/2000:13:25:15 -0700", + "$5": "GET /apache_pb.gif HTTP/1.0", "$status_code": "200", "$size": "1534", + }}, + } + + for _, tc := range tests { + t.Run(tc.pattern, func(t *testing.T) { + got := mustParse(t, tc.pattern).ExtractedValues(apache200) + if !maps.Equal(got, tc.want) { + t.Fatalf("ExtractedValues = %v\nwant %v", got, tc.want) + } + }) + } +} + +func TestValueReferences(t *testing.T) { + delim := mustParse(t, `[ip, user, ..., status, bytes>1000]`) + + if v, ok := delim.Value(apache200, "$bytes"); !ok || v != "1534" { + t.Fatalf("$bytes = %q, %v", v, ok) + } + + if v, ok := delim.Value(apache200, "$3"); !ok || v != "frank" { + t.Fatalf("$3 = %q, %v", v, ok) + } + + if _, ok := delim.Value(apache200, "$missing"); ok { + t.Fatal("$missing resolved") + } + + js := mustParse(t, `{ $.level = "ERROR" }`) + + if v, ok := js.Value(`{"level":"ERROR","latency":42}`, "$.latency"); !ok || v != "42" { + t.Fatalf("$.latency = %q, %v", v, ok) + } + + if _, ok := js.Value(`{"level":"ERROR"}`, "$.latency"); ok { + t.Fatal("absent $.latency resolved") + } +} diff --git a/providers/aws/cloudwatchlogs/filterpattern/field_cond.go b/providers/aws/cloudwatchlogs/filterpattern/field_cond.go new file mode 100644 index 000000000..f6d17ecd0 --- /dev/null +++ b/providers/aws/cloudwatchlogs/filterpattern/field_cond.go @@ -0,0 +1,156 @@ +package filterpattern + +import "strconv" + +// fieldCond is a condition on one space-delimited field's value. +type fieldCond interface { + eval(value string) bool +} + +type orCond struct{ l, r fieldCond } + +func (c orCond) eval(v string) bool { return c.l.eval(v) || c.r.eval(v) } + +type andCond struct{ l, r fieldCond } + +func (c andCond) eval(v string) bool { return c.l.eval(v) && c.r.eval(v) } + +// cmpCond compares a field value with =, !=, <, <=, > or >=. Equality uses +// a numeric compare when both sides are numbers, else a * wildcard or %regex% +// match; ordering compares need a numeric field value. +type cmpCond struct { + op string + val *valueSpec +} + +func (c cmpCond) eval(v string) bool { + switch c.op { + case opEq: + return c.equals(v) + case opNe: + return !c.equals(v) + } + + f, err := strconv.ParseFloat(v, 64) + if err != nil { + return false + } + + switch c.op { + case opLt: + return f < c.val.num + case opLe: + return f <= c.val.num + case opGt: + return f > c.val.num + default: + return f >= c.val.num + } +} + +func (c cmpCond) equals(v string) bool { + if c.val.re != nil { + return c.val.re.MatchString(v) + } + + if c.val.isNum { + if f, err := strconv.ParseFloat(v, 64); err == nil { + return f == c.val.num + } + } + + return c.val.glob.MatchString(v) +} + +// condParser parses `name op value` terms joined by && and || (&& binds +// tighter). Every term must name the field the condition belongs to. +type condParser struct { + toks []token + pos int + name string + regexes int +} + +func (p *condParser) parseOr() (fieldCond, error) { + left, err := p.parseAnd() + if err != nil { + return nil, err + } + + for p.pos < len(p.toks) && p.toks[p.pos].kind == tokOr { + p.pos++ + + right, rerr := p.parseAnd() + if rerr != nil { + return nil, rerr + } + + left = orCond{l: left, r: right} + } + + return left, nil +} + +func (p *condParser) parseAnd() (fieldCond, error) { + left, err := p.parseTerm() + if err != nil { + return nil, err + } + + for p.pos < len(p.toks) && p.toks[p.pos].kind == tokAnd { + p.pos++ + + right, rerr := p.parseTerm() + if rerr != nil { + return nil, rerr + } + + left = andCond{l: left, r: right} + } + + return left, nil +} + +func (p *condParser) parseTerm() (fieldCond, error) { + const termLen = 3 // name, operator, value + + if p.pos+termLen > len(p.toks) { + return nil, invalid("Invalid condition on field '" + p.name + "' in space-delimited filter pattern") + } + + name, op, val := p.toks[p.pos], p.toks[p.pos+1], p.toks[p.pos+2] + + if err := p.checkTerm(&name, &op, &val); err != nil { + return nil, err + } + + p.pos += termLen + + jp := &jsonParser{} + + v, err := jp.newValue(&val) + if err != nil { + return nil, err + } + + p.regexes += jp.regexes + + if op.text != opEq && op.text != opNe && !v.isNum { + return nil, invalid("Invalid filter pattern: " + op.text + " requires a number, got '" + val.text + "'") + } + + return cmpCond{op: op.text, val: v}, nil +} + +// checkTerm validates the shape of one `name op value` term. +func (p *condParser) checkTerm(name, op, val *token) error { + if name.kind != tokWord || name.text != p.name { + return invalid("Invalid condition on field '" + p.name + "': it must compare " + p.name) + } + + if op.kind != tokOp || (val.kind != tokWord && val.kind != tokString && val.kind != tokRegex) { + return invalid("Invalid condition on field '" + p.name + "' in space-delimited filter pattern") + } + + return nil +} diff --git a/providers/aws/cloudwatchlogs/filterpattern/json_eval.go b/providers/aws/cloudwatchlogs/filterpattern/json_eval.go index 0bb7af7ca..ed564c6e4 100644 --- a/providers/aws/cloudwatchlogs/filterpattern/json_eval.go +++ b/providers/aws/cloudwatchlogs/filterpattern/json_eval.go @@ -49,11 +49,11 @@ func (n cmpNode) holds(v any) bool { } switch n.op { - case "<": + case opLt: return f < n.val.num - case "<=": + case opLe: return f <= n.val.num - case ">": + case opGt: return f > n.val.num default: return f >= n.val.num @@ -225,3 +225,24 @@ func scalarString(v any) string { return "" } + +// jsonValue resolves a "$.path" reference against message, which need not +// be a selector the pattern itself names. +func jsonValue(message, ref string) (string, bool) { + sel, end, err := lexSelector(ref, 0) + if err != nil || end != len(ref) || hasWildcard(sel.steps) { + return "", false + } + + doc, ok := decodeJSON(message) + if !ok { + return "", false + } + + vals := resolve(doc, sel.steps) + if len(vals) != 1 || !isScalar(vals[0]) { + return "", false + } + + return scalarString(vals[0]), true +} diff --git a/providers/aws/cloudwatchlogs/filterpattern/json_parse.go b/providers/aws/cloudwatchlogs/filterpattern/json_parse.go index fed9cefac..5a8bd4af7 100644 --- a/providers/aws/cloudwatchlogs/filterpattern/json_parse.go +++ b/providers/aws/cloudwatchlogs/filterpattern/json_parse.go @@ -10,6 +10,9 @@ import ( const ( opEq = "=" opNe = "!=" + opLt = "<" + opLe = "<=" + opGt = ">" kwIs = "IS" kwNot = "NOT" kwExists = "EXISTS" diff --git a/providers/aws/cloudwatchlogs/filterpattern/pattern.go b/providers/aws/cloudwatchlogs/filterpattern/pattern.go index c4b34ea17..9f4878a2a 100644 --- a/providers/aws/cloudwatchlogs/filterpattern/pattern.go +++ b/providers/aws/cloudwatchlogs/filterpattern/pattern.go @@ -1,11 +1,8 @@ // Package filterpattern implements the CloudWatch Logs filter pattern // language used by FilterLogEvents, metric filters, subscription filters and -// TestMetricFilter: unstructured terms, %regex% terms, and JSON property -// patterns. See +// TestMetricFilter: unstructured terms, %regex% terms, JSON property patterns, +// and space-delimited field patterns. See // https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/FilterAndPatternSyntax.html. -// -// Space-delimited patterns ([a, b, ...]) are not supported yet and are -// rejected rather than matched as plain text. package filterpattern import ( @@ -19,7 +16,8 @@ import ( const ( // maxPatternLength is the longest filterPattern the API accepts. maxPatternLength = 1024 - // maxRegexPerStructuredPattern caps %regex% values in one JSON pattern. + // maxRegexPerStructuredPattern caps %regex% values in one JSON or + // space-delimited pattern. maxRegexPerStructuredPattern = 2 // maxWildcardsPerSelector caps * selectors in one property selector. maxWildcardsPerSelector = 1 @@ -38,6 +36,7 @@ type Pattern struct { m matcher hasRegex bool json *jsonPattern + delim *delimPattern } // Parse parses a filter pattern. An empty or blank pattern matches every @@ -67,7 +66,12 @@ func Parse(raw string) (*Pattern, error) { return &Pattern{m: jp, hasRegex: jp.regexCount > 0, json: jp}, nil case strings.HasPrefix(trimmed, "["): - return nil, invalid("space-delimited filter patterns are not supported yet: " + trimmed) + dp, err := parseDelimitedPattern(trimmed) + if err != nil { + return nil, err + } + + return &Pattern{m: dp, hasRegex: dp.regexCount > 0, delim: dp}, nil default: tp, err := parseTerms(trimmed) if err != nil { @@ -89,15 +93,31 @@ func (p *Pattern) HasRegex() bool { return p.hasRegex } -// ExtractedValues returns the values of the JSON properties a JSON pattern -// names, keyed by selector (for example "$.latency"), for a message the -// pattern matched. Other pattern forms extract nothing. +// ExtractedValues returns the values a structured pattern pulls out of a +// message it matched: JSON properties keyed by selector (for example +// "$.latency"), and space-delimited fields keyed "$name" or, for unnamed +// fields, "$N" by position. Unstructured patterns extract nothing. func (p *Pattern) ExtractedValues(message string) map[string]string { - if p.json == nil { - return map[string]string{} + switch { + case p.json != nil: + return p.json.extract(message) + case p.delim != nil: + return p.delim.extract(message) } - return p.json.extract(message) + return map[string]string{} +} + +// Value resolves a metric filter field reference ("$.latency" for a JSON +// pattern, "$size" or "$3" for a space-delimited one) in a matched message. +func (p *Pattern) Value(message, ref string) (string, bool) { + if p.json != nil { + return jsonValue(message, ref) + } + + v, ok := p.ExtractedValues(message)[ref] + + return v, ok } type matchAll struct{} diff --git a/providers/aws/cloudwatchlogs/filterpattern/pattern_test.go b/providers/aws/cloudwatchlogs/filterpattern/pattern_test.go index 059236719..6cd6655b0 100644 --- a/providers/aws/cloudwatchlogs/filterpattern/pattern_test.go +++ b/providers/aws/cloudwatchlogs/filterpattern/pattern_test.go @@ -53,7 +53,7 @@ func TestUnstructuredPatterns(t *testing.T) { {`" "`, events}, {"", events}, {"%AUTHORIZED%", []string{err401, success}}, - {"%^\\[ERROR 4%", []string{err400, err401, err419, err420}}, + {"%^\\[ERROR\\s4%", []string{err400, err401, err419, err420}}, {"%BAD|INVALID%", []string{err400, err420}}, {"error", nil}, } @@ -145,7 +145,11 @@ func TestInvalidPatterns(t *testing.T) { `%unterminated`, `"unterminated`, `error:{boom}`, - `[ip, user]`, + `[ip, user`, + `[a, b=%x%, c=%y%, d=%z%]`, + `[a, b=1 || c=2]`, + `[a, b>abc]`, + `[a-b]`, strings.Repeat("a", maxPatternLength+1), } { _, err := Parse(pattern) @@ -155,6 +159,16 @@ func TestInvalidPatterns(t *testing.T) { } } +// TestRegexAllowsLiteralSpace follows the syntax page's own example, which +// has a space inside %...%. +func TestRegexAllowsLiteralSpace(t *testing.T) { + p := mustParse(t, `%^starting|^initializing|^shutting down%`) + + if !p.Match("shutting down") || !p.Match("starting up") || p.Match("skipping initializing") { + t.Fatal("regex with a literal space did not match like the documented example") + } +} + func TestQuotasAccepted(t *testing.T) { for _, pattern := range []string{ `{ $.a[*] = 1 && $.b[*] = 1 && $.c[*] = 1 }`, diff --git a/providers/aws/cloudwatchlogs/filterpattern/regex.go b/providers/aws/cloudwatchlogs/filterpattern/regex.go index f5fc55ccd..be5dfbc60 100644 --- a/providers/aws/cloudwatchlogs/filterpattern/regex.go +++ b/providers/aws/cloudwatchlogs/filterpattern/regex.go @@ -7,7 +7,9 @@ import ( ) // regexAllowedSymbols are the non-alphanumeric characters a %regex% may use: -// the documented symbol set, the operators, the escape character, and space. +// the documented symbol set, the operators, the escape character, and space +// (the syntax page's own example %^starting|^initializing|^shutting down% +// contains one). const regexAllowedSymbols = ":_#=@/;,-^$?[]{}|\\*+. " // compileRegex validates a %...% body against the CloudWatch Logs regex diff --git a/providers/aws/cloudwatchlogs/space_delimited_test.go b/providers/aws/cloudwatchlogs/space_delimited_test.go new file mode 100644 index 000000000..47b112da7 --- /dev/null +++ b/providers/aws/cloudwatchlogs/space_delimited_test.go @@ -0,0 +1,61 @@ +package cloudwatchlogs + +import ( + "context" + "testing" + + "github.com/stackshy/cloudemu/v2/services/logging/driver" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const ( + accessOK = `127.0.0.1 - frank [10/Oct/2000:13:25:15 -0700] "GET /index.html HTTP/1.0" 200 1534` + accessMiss = `127.0.0.1 - frank [10/Oct/2000:13:35:22 -0700] "GET /about.html HTTP/1.0" 404 2000` + accessGif = `127.0.0.1 - frank [10/Oct/2000:13:50:35 -0700] "GET /logo.gif HTTP/1.0" 404 500` +) + +// TestFilterLogEventsSpaceDelimited is the regression for space-delimited +// patterns being rejected. +func TestFilterLogEventsSpaceDelimited(t *testing.T) { + m := newWindowMock(t, 0) + putMessages(t, m, accessOK, accessMiss, accessGif) + + assert.Equal(t, []string{accessMiss}, filterMessages(t, m, `[..., request=*.html*, status_code=4*, bytes]`)) + assert.Equal(t, []string{accessMiss, accessGif}, filterMessages(t, m, `[ip, id, user, ts, request, status_code=404, bytes]`)) + assert.Equal(t, []string{accessOK, accessMiss}, filterMessages(t, m, `[..., bytes >= 1000]`)) +} + +// TestMetricFilterSpaceDelimitedValue publishes the sum of an extracted field, +// where the old code counted each match as 1. +func TestMetricFilterSpaceDelimitedValue(t *testing.T) { + ctx := context.Background() + m := newWindowMock(t, 0) + mon := &recordingMonitoring{} + m.SetMonitoring(mon) + + require.NoError(t, m.PutMetricFilter(ctx, &driver.MetricFilterConfig{ + Name: "bytes", LogGroup: "g", FilterPattern: `[ip, id, user, ts, request, status_code=404, bytes]`, + MetricName: "Bytes404", MetricNamespace: "Web", MetricValue: "$bytes", + })) + require.NoError(t, m.PutMetricFilter(ctx, &driver.MetricFilterConfig{ + Name: "latency", LogGroup: "g", FilterPattern: `{ $.level = "ERROR" }`, + MetricName: "Latency", MetricNamespace: "Web", MetricValue: "$.latency", + })) + + putMessages(t, m, accessOK, accessMiss, accessGif, `{"level":"ERROR","latency":40}`, `{"level":"ERROR"}`) + + assert.InDelta(t, 2500.0, mon.valueOf("Web", "Bytes404"), 0) + assert.InDelta(t, 40.0, mon.valueOf("Web", "Latency"), 0, "an event without the field contributes nothing") +} + +func TestTestMetricFilterSpaceDelimited(t *testing.T) { + got, err := newTestMock().TestMetricFilter(context.Background(), `[..., status_code=404, size]`, + []string{accessOK, accessMiss}) + require.NoError(t, err) + require.Len(t, got, 1) + assert.Equal(t, int64(1), got[0].EventNumber) + assert.Equal(t, "404", got[0].ExtractedValues["$status_code"]) + assert.Equal(t, "2000", got[0].ExtractedValues["$size"]) + assert.Equal(t, "GET /about.html HTTP/1.0", got[0].ExtractedValues["$5"]) +} diff --git a/server/aws/cloudwatchlogs/space_delimited_sdk_test.go b/server/aws/cloudwatchlogs/space_delimited_sdk_test.go new file mode 100644 index 000000000..e417f6ddd --- /dev/null +++ b/server/aws/cloudwatchlogs/space_delimited_sdk_test.go @@ -0,0 +1,90 @@ +package cloudwatchlogs_test + +import ( + "context" + "errors" + "testing" + "time" + + "github.com/aws/aws-sdk-go-v2/aws" + cwl "github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs" + cwltypes "github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs/types" +) + +func TestSDKSpaceDelimitedPatterns(t *testing.T) { + c := newLogsClient(t) + ctx := context.Background() + g, s := aws.String("/sd/g"), aws.String("s") + + if _, err := c.CreateLogGroup(ctx, &cwl.CreateLogGroupInput{LogGroupName: g}); err != nil { + t.Fatalf("CreateLogGroup: %v", err) + } + + if _, err := c.CreateLogStream(ctx, &cwl.CreateLogStreamInput{LogGroupName: g, LogStreamName: s}); err != nil { + t.Fatalf("CreateLogStream: %v", err) + } + + now := time.Now() + msgs := []string{ + `127.0.0.1 - frank [10/Oct/2000:13:25:15 -0700] "GET /index.html HTTP/1.0" 200 1534`, + `127.0.0.1 - frank [10/Oct/2000:13:35:22 -0700] "GET /about.html HTTP/1.0" 404 2000`, + "ERROR 09/25/2014 12:00:02 Failed to process request", + } + + events := make([]cwltypes.InputLogEvent, 0, len(msgs)) + for i, m := range msgs { + events = append(events, cwltypes.InputLogEvent{ + Timestamp: aws.Int64(now.Add(time.Duration(i) * time.Millisecond).UnixMilli()), Message: aws.String(m), + }) + } + + if _, err := c.PutLogEvents(ctx, &cwl.PutLogEventsInput{LogGroupName: g, LogStreamName: s, LogEvents: events}); err != nil { + t.Fatalf("PutLogEvents: %v", err) + } + + for pattern, want := range map[string]int{ + `[..., request=*.html*, status_code=4*, bytes]`: 1, + `[ip, id, user, ts, request, status_code, bytes > 1000]`: 2, + `[w1=ERROR, w2]`: 1, + `[w1=ERROR]`: 0, + `[]`: 3, + } { + out, err := c.FilterLogEvents(ctx, &cwl.FilterLogEventsInput{LogGroupName: g, FilterPattern: aws.String(pattern)}) + if err != nil { + t.Fatalf("FilterLogEvents(%q): %v", pattern, err) + } + + if len(out.Events) != want { + t.Errorf("FilterLogEvents(%q) = %d events, want %d", pattern, len(out.Events), want) + } + } + + tm, err := c.TestMetricFilter(ctx, &cwl.TestMetricFilterInput{ + FilterPattern: aws.String(`[..., status_code=404, size]`), LogEventMessages: msgs, + }) + if err != nil { + t.Fatalf("TestMetricFilter: %v", err) + } + + if len(tm.Matches) != 1 || tm.Matches[0].EventNumber != 1 || tm.Matches[0].ExtractedValues["$size"] != "2000" || + tm.Matches[0].ExtractedValues["$4"] != "10/Oct/2000:13:35:22 -0700" { + t.Fatalf("TestMetricFilter matches = %+v", tm.Matches) + } + + if _, err := c.PutMetricFilter(ctx, &cwl.PutMetricFilterInput{ + LogGroupName: g, FilterName: aws.String("bytes"), FilterPattern: aws.String(`[..., status_code=404, bytes]`), + MetricTransformations: []cwltypes.MetricTransformation{{ + MetricName: aws.String("Bytes"), MetricNamespace: aws.String("Web"), MetricValue: aws.String("$bytes"), + }}, + }); err != nil { + t.Fatalf("PutMetricFilter: %v", err) + } + + var invalid *cwltypes.InvalidParameterException + + if _, err := c.FilterLogEvents(ctx, &cwl.FilterLogEventsInput{ + LogGroupName: g, FilterPattern: aws.String(`[a, b=1 || c=2]`), + }); !errors.As(err, &invalid) { + t.Fatalf("condition on another field: got %v, want InvalidParameterException", err) + } +} From f8e78eca5cb844804a57c6f2e5ce78dfdb3cda45 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Patel Date: Thu, 22 Oct 2026 06:17:15 +0530 Subject: [PATCH 06/31] fix(aws-cloudwatchlogs): metric filter dimensions, default values, units and DescribeMetricFilters filters (#1660) * fix(aws-cloudwatchlogs): metric filter dimensions, default value, unit and validation Publish one datum per dimension set with the filter's unit, emit defaultValue for batches with no match, validate metric transformations and the 100-filter quota like PutMetricFilter, and serve DescribeMetricFilters account-wide with metric name/namespace filters and paging. Part of #1476. * fix(aws-cloudwatchlogs): publish metric filter matches as samples, default value once per period Each matching event is its own sample (StatisticValues per dimension set), defaultValue is published at most once per one-minute period and never for a period with a match, and $N metric values past a space-delimited pattern's fields are rejected. --- docs/coverage/aws/cloudwatchlogs.md | 1 + docs/coverage/coverage.json | 4 + .../aws/cloudwatchlogs/cloudwatchlogs.go | 154 +------ .../aws/cloudwatchlogs/cloudwatchlogs_test.go | 30 +- .../cloudwatchlogs/filter_patterns_test.go | 18 +- .../cloudwatchlogs/filterpattern/delimited.go | 23 + .../cloudwatchlogs/filterpattern/pattern.go | 22 + .../metric_filter_fields_test.go | 17 +- .../aws/cloudwatchlogs/metric_filters.go | 427 ++++++++++++++++++ .../metric_transformations_test.go | 249 ++++++++++ providers/aws/cloudwatchlogs/snapshot.go | 7 +- server/aws/cloudwatchlogs/metric_filters.go | 83 ++-- .../metric_transformation_sdk_test.go | 226 +++++++++ services/logging/driver/aws.go | 13 + services/logging/logging_test.go | 40 +- 15 files changed, 1099 insertions(+), 215 deletions(-) create mode 100644 providers/aws/cloudwatchlogs/metric_filters.go create mode 100644 providers/aws/cloudwatchlogs/metric_transformations_test.go create mode 100644 server/aws/cloudwatchlogs/metric_transformation_sdk_test.go diff --git a/docs/coverage/aws/cloudwatchlogs.md b/docs/coverage/aws/cloudwatchlogs.md index 98655f009..dcdfcb403 100644 --- a/docs/coverage/aws/cloudwatchlogs.md +++ b/docs/coverage/aws/cloudwatchlogs.md @@ -38,6 +38,7 @@ AWSLogging is an optional interface implemented by the AWS CloudWatch Logs | `AssociateKmsKey` | AssociateKmsKey and DisassociateKmsKey target exactly one of a log group | | `CreateAWSLogGroup` | | | `DeleteRetentionPolicy` | | +| `DescribeAWSMetricFilters` | DescribeAWSMetricFilters lists metric filters, sorted by filter name. | | `DisassociateKmsKey` | | | `ListAWSLogGroups` | | | `PutLogEventsWithResult` | | diff --git a/docs/coverage/coverage.json b/docs/coverage/coverage.json index 90ae49f3a..b8016bf00 100644 --- a/docs/coverage/coverage.json +++ b/docs/coverage/coverage.json @@ -11572,6 +11572,10 @@ { "name": "DeleteRetentionPolicy" }, + { + "name": "DescribeAWSMetricFilters", + "doc": "DescribeAWSMetricFilters lists metric filters, sorted by filter name." + }, { "name": "DisassociateKmsKey" }, diff --git a/providers/aws/cloudwatchlogs/cloudwatchlogs.go b/providers/aws/cloudwatchlogs/cloudwatchlogs.go index dcb8e5c7a..5a4be955a 100644 --- a/providers/aws/cloudwatchlogs/cloudwatchlogs.go +++ b/providers/aws/cloudwatchlogs/cloudwatchlogs.go @@ -5,7 +5,6 @@ import ( "context" "maps" "sort" - "strconv" "strings" "sync" "time" @@ -64,7 +63,10 @@ type Mock struct { // queryResultKmsKey is the account-wide key associated with Logs Insights // query results (AssociateKmsKey on the query-result resource). queryResultKmsKey string - mu sync.RWMutex + // periods holds each metric filter's per-period default-value state, + // keyed by filterPeriodKey. + periods map[string]filterPeriod + mu sync.RWMutex } // SetMonitoring sets the monitoring backend for auto-metric generation. @@ -92,8 +94,9 @@ func (m *Mock) emitMetric(metricName string, value float64, unit string, dims ma // New creates a new CloudWatch Logs mock with the given configuration options. func New(opts *config.Options) *Mock { return &Mock{ - groups: memstore.New[*logGroup](), - opts: opts, + groups: memstore.New[*logGroup](), + opts: opts, + periods: map[string]filterPeriod{}, } } @@ -219,87 +222,6 @@ func (m *Mock) PutLogEvents(ctx context.Context, groupName, streamName string, e return err } -// evaluateMetricFilters turns matching log events into CloudWatch metric data. -// For each metric filter on the group, every event whose message matches the -// filter pattern contributes the filter's metric value; the total is published -// to the filter's configured namespace/metric, mirroring how CloudWatch Logs -// extracts custom metrics from ingested events. Patterns use the CloudWatch -// Logs filter pattern language (see the filterpattern package). -func (m *Mock) evaluateMetricFilters(g *logGroup, events []driver.LogEvent) { - if m.monitoring == nil { - return - } - - for _, mf := range g.metricFilters.All() { - var ( - matched int - total float64 - ) - - // Patterns are validated when the filter is put; one that no longer - // parses (restored from an older snapshot) matches nothing. - pattern, err := filterpattern.Parse(mf.FilterPattern) - if err != nil { - continue - } - - for i := range events { - if !pattern.Match(events[i].Message) { - continue - } - - if v, ok := metricFilterValue(mf.MetricValue, pattern, events[i].Message); ok { - matched++ - total += v - } - } - - if matched == 0 { - continue - } - - namespace := mf.MetricNamespace - if namespace == "" { - namespace = "LogMetrics" - } - - _ = m.monitoring.PutMetricData(context.Background(), []mondriver.MetricDatum{{ - Namespace: namespace, - MetricName: mf.MetricName, - Value: total, - Timestamp: m.opts.Clock.Now(), - }}) - } -} - -// metricFilterValue resolves a metric filter's metricValue to the number one -// matching event contributes. A literal number is used directly; a "$field" -// reference ("$.latency", "$size", "$3") takes the value the pattern extracts -// from the event, and an event whose referenced value is missing or not a -// number contributes nothing. An empty value counts the event as 1. -func metricFilterValue(raw string, pattern *filterpattern.Pattern, message string) (float64, bool) { - if raw == "" { - return 1, true - } - - if v, err := strconv.ParseFloat(raw, 64); err == nil { - return v, true - } - - if !strings.HasPrefix(raw, "$") { - return 1, true - } - - field, ok := pattern.Value(message, raw) - if !ok { - return 0, false - } - - v, err := strconv.ParseFloat(field, 64) - - return v, err == nil -} - // GetLogEvents retrieves log events matching the query. func (m *Mock) GetLogEvents(_ context.Context, input *driver.LogQueryInput) ([]driver.LogEvent, error) { g, ok := m.groups.Get(input.LogGroup) @@ -490,46 +412,6 @@ func (*Mock) matchEvents( return results } -// PutMetricFilter creates or updates a metric filter for a log group. -func (m *Mock) PutMetricFilter( - _ context.Context, - cfg *driver.MetricFilterConfig, -) error { - g, ok := m.groups.Get(cfg.LogGroup) - if !ok { - return errors.Newf( - errors.NotFound, "log group %q not found", cfg.LogGroup, - ) - } - - if cfg.Name == "" { - return errors.New( - errors.InvalidArgument, "metric filter name is required", - ) - } - - if err := validateFilterPattern(g, metricFilterKind, cfg.Name, cfg.FilterPattern); err != nil { - return err - } - - info := &driver.MetricFilterInfo{ - Name: cfg.Name, - LogGroup: cfg.LogGroup, - FilterPattern: cfg.FilterPattern, - MetricName: cfg.MetricName, - MetricNamespace: cfg.MetricNamespace, - MetricValue: cfg.MetricValue, - DefaultValue: cfg.DefaultValue, - Unit: cfg.Unit, - Dimensions: maps.Clone(cfg.Dimensions), - CreatedAt: m.opts.Clock.Now().UTC(), - } - - g.metricFilters.Set(cfg.Name, info) - - return nil -} - // DeleteMetricFilter deletes a metric filter from a log group. func (m *Mock) DeleteMetricFilter( _ context.Context, @@ -553,28 +435,6 @@ func (m *Mock) DeleteMetricFilter( return nil } -// DescribeMetricFilters lists all metric filters for a log group. -func (m *Mock) DescribeMetricFilters( - _ context.Context, - logGroup string, -) ([]driver.MetricFilterInfo, error) { - g, ok := m.groups.Get(logGroup) - if !ok { - return nil, errors.Newf( - errors.NotFound, "log group %q not found", logGroup, - ) - } - - all := g.metricFilters.All() - results := make([]driver.MetricFilterInfo, 0, len(all)) - - for _, mf := range all { - results = append(results, *mf) - } - - return results, nil -} - // UpdateLogGroup replaces the mutable fields of an existing log group โ€” // ARM CreateOrUpdate-on-existing semantics (retention and tags come from // the request; identity and CreatedAt are preserved). diff --git a/providers/aws/cloudwatchlogs/cloudwatchlogs_test.go b/providers/aws/cloudwatchlogs/cloudwatchlogs_test.go index 8d8b35d1e..c2f078a1c 100644 --- a/providers/aws/cloudwatchlogs/cloudwatchlogs_test.go +++ b/providers/aws/cloudwatchlogs/cloudwatchlogs_test.go @@ -806,10 +806,12 @@ func TestDeleteMetricFilter(t *testing.T) { require.NoError(t, err) err = m.PutMetricFilter(ctx, &driver.MetricFilterConfig{ - Name: "del-filter", - LogGroup: "grp", - FilterPattern: "ERROR", - MetricName: "ErrorCount", + Name: "del-filter", + LogGroup: "grp", + FilterPattern: "ERROR", + MetricName: "ErrorCount", + MetricNamespace: "App", + MetricValue: "1", }) require.NoError(t, err) @@ -850,18 +852,22 @@ func TestDescribeMetricFilters(t *testing.T) { require.NoError(t, err) err = m.PutMetricFilter(ctx, &driver.MetricFilterConfig{ - Name: "filter-1", - LogGroup: "grp", - FilterPattern: "ERROR", - MetricName: "ErrorCount", + Name: "filter-1", + LogGroup: "grp", + FilterPattern: "ERROR", + MetricName: "ErrorCount", + MetricNamespace: "App", + MetricValue: "1", }) require.NoError(t, err) err = m.PutMetricFilter(ctx, &driver.MetricFilterConfig{ - Name: "filter-2", - LogGroup: "grp", - FilterPattern: "WARN", - MetricName: "WarnCount", + Name: "filter-2", + LogGroup: "grp", + FilterPattern: "WARN", + MetricName: "WarnCount", + MetricNamespace: "App", + MetricValue: "1", }) require.NoError(t, err) diff --git a/providers/aws/cloudwatchlogs/filter_patterns_test.go b/providers/aws/cloudwatchlogs/filter_patterns_test.go index 07f501db7..232588a4e 100644 --- a/providers/aws/cloudwatchlogs/filter_patterns_test.go +++ b/providers/aws/cloudwatchlogs/filter_patterns_test.go @@ -40,7 +40,11 @@ func (r *recordingMonitoring) valueOf(namespace, name string) float64 { for _, d := range r.data { if d.Namespace == namespace && d.MetricName == name { - total += d.Value + if d.StatisticValues != nil { + total += d.StatisticValues.Sum + } else { + total += d.Value + } } } @@ -102,7 +106,7 @@ func TestPutFilterValidatesPattern(t *testing.T) { m := newWindowMock(t, 0) err := m.PutMetricFilter(ctx, &driver.MetricFilterConfig{ - Name: "bad", LogGroup: "g", FilterPattern: "{ $.a EXISTS }", MetricName: "M", MetricNamespace: "N", + Name: "bad", LogGroup: "g", FilterPattern: "{ $.a EXISTS }", MetricName: "M", MetricNamespace: "N", MetricValue: "1", }) require.Error(t, err) assert.True(t, errors.IsInvalidArgument(err)) @@ -120,7 +124,7 @@ func TestRegexFilterQuotaPerGroup(t *testing.T) { for i := range 4 { require.NoError(t, m.PutMetricFilter(ctx, &driver.MetricFilterConfig{ - Name: fmt.Sprintf("mf%d", i), LogGroup: "g", FilterPattern: "%ERR%", MetricName: "M", MetricNamespace: "N", + Name: fmt.Sprintf("mf%d", i), LogGroup: "g", FilterPattern: "%ERR%", MetricName: "M", MetricNamespace: "N", MetricValue: "1", })) } @@ -130,17 +134,17 @@ func TestRegexFilterQuotaPerGroup(t *testing.T) { // A sixth regex pattern, from either kind of filter, is over the quota. err := m.PutMetricFilter(ctx, &driver.MetricFilterConfig{ - Name: "mf9", LogGroup: "g", FilterPattern: "%WARN%", MetricName: "M", MetricNamespace: "N", + Name: "mf9", LogGroup: "g", FilterPattern: "%WARN%", MetricName: "M", MetricNamespace: "N", MetricValue: "1", }) require.Error(t, err) assert.Equal(t, errors.ResourceExhausted, errors.GetCode(err)) // Replacing an existing regex filter, or adding a plain one, is fine. require.NoError(t, m.PutMetricFilter(ctx, &driver.MetricFilterConfig{ - Name: "mf0", LogGroup: "g", FilterPattern: "%WARN%", MetricName: "M", MetricNamespace: "N", + Name: "mf0", LogGroup: "g", FilterPattern: "%WARN%", MetricName: "M", MetricNamespace: "N", MetricValue: "1", })) require.NoError(t, m.PutMetricFilter(ctx, &driver.MetricFilterConfig{ - Name: "plain", LogGroup: "g", FilterPattern: "WARN", MetricName: "M", MetricNamespace: "N", + Name: "plain", LogGroup: "g", FilterPattern: "WARN", MetricName: "M", MetricNamespace: "N", MetricValue: "1", })) } @@ -177,7 +181,7 @@ func TestMetricFilterUsesPatternLanguage(t *testing.T) { m.SetMonitoring(mon) require.NoError(t, m.PutMetricFilter(ctx, &driver.MetricFilterConfig{ - Name: "errs", LogGroup: "g", FilterPattern: "?ERROR ?FATAL", MetricName: "Errors", MetricNamespace: "App", + Name: "errs", LogGroup: "g", FilterPattern: "?ERROR ?FATAL", MetricName: "Errors", MetricNamespace: "App", MetricValue: "1", })) putMessages(t, m, "ERROR a", "FATAL b", "INFO c") diff --git a/providers/aws/cloudwatchlogs/filterpattern/delimited.go b/providers/aws/cloudwatchlogs/filterpattern/delimited.go index b7ba1545c..5697edede 100644 --- a/providers/aws/cloudwatchlogs/filterpattern/delimited.go +++ b/providers/aws/cloudwatchlogs/filterpattern/delimited.go @@ -249,6 +249,29 @@ func (p *delimPattern) extract(message string) map[string]string { func positional(i int) string { return "$" + strconv.Itoa(i+1) } +// validRef reports whether "$name" names one of the pattern's fields, or +// "$N" is a position the pattern can bind. Without an ellipsis (and with at +// least one field) a message binds exactly as many fields as the pattern +// lists; an ellipsis or the match-all "[]" leaves the count open. +func (p *delimPattern) validRef(ref string) bool { + name, ok := strings.CutPrefix(ref, "$") + if !ok || name == "" { + return false + } + + if n, err := strconv.Atoi(name); err == nil { + return n >= 1 && (p.hasEllipsis || len(p.fields) == 0 || n <= len(p.fields)) + } + + for _, f := range p.fields { + if f.name == name { + return true + } + } + + return false +} + // tokenizeMessage splits a log event into space-delimited fields. Text in // double quotes or square brackets is one field, without its delimiters. func tokenizeMessage(message string) []string { diff --git a/providers/aws/cloudwatchlogs/filterpattern/pattern.go b/providers/aws/cloudwatchlogs/filterpattern/pattern.go index 9f4878a2a..f05a91dd9 100644 --- a/providers/aws/cloudwatchlogs/filterpattern/pattern.go +++ b/providers/aws/cloudwatchlogs/filterpattern/pattern.go @@ -108,6 +108,28 @@ func (p *Pattern) ExtractedValues(message string) map[string]string { return map[string]string{} } +// HasFields reports whether the pattern selects fields (a JSON or +// space-delimited pattern), which metric filter dimensions require. +func (p *Pattern) HasFields() bool { + return p.json != nil || p.delim != nil +} + +// ValidRef reports whether ref is a field reference the pattern can resolve: +// a "$.path" selector without wildcards for a JSON pattern, or "$name" (a +// named field) or "$N" (a 1-based position) for a space-delimited one. +func (p *Pattern) ValidRef(ref string) bool { + switch { + case p.json != nil: + sel, end, err := lexSelector(ref, 0) + + return err == nil && end == len(ref) && strings.HasPrefix(ref, "$.") && !hasWildcard(sel.steps) + case p.delim != nil: + return p.delim.validRef(ref) + } + + return false +} + // Value resolves a metric filter field reference ("$.latency" for a JSON // pattern, "$size" or "$3" for a space-delimited one) in a matched message. func (p *Pattern) Value(message, ref string) (string, bool) { diff --git a/providers/aws/cloudwatchlogs/metric_filter_fields_test.go b/providers/aws/cloudwatchlogs/metric_filter_fields_test.go index 1ab56cb22..07145699c 100644 --- a/providers/aws/cloudwatchlogs/metric_filter_fields_test.go +++ b/providers/aws/cloudwatchlogs/metric_filter_fields_test.go @@ -19,6 +19,8 @@ func TestMetricFilterFieldsRoundTrip(t *testing.T) { _, err := m.CreateLogGroup(ctx, driver.LogGroupConfig{Name: "g"}) require.NoError(t, err) + // A default value and dimensions cannot be combined, so each goes on its + // own filter. def := 0.0 require.NoError(t, m.PutMetricFilter(ctx, &driver.MetricFilterConfig{ Name: "f", @@ -29,15 +31,24 @@ func TestMetricFilterFieldsRoundTrip(t *testing.T) { MetricValue: "1", DefaultValue: &def, Unit: "Count", - Dimensions: map[string]string{"Service": "api"}, + })) + require.NoError(t, m.PutMetricFilter(ctx, &driver.MetricFilterConfig{ + Name: "g", + LogGroup: "g", + FilterPattern: `{ $.service = * }`, + MetricName: "ByService", + MetricNamespace: "MyApp", + MetricValue: "1", + Dimensions: map[string]string{"Service": "$.service"}, })) filters, err := m.DescribeMetricFilters(ctx, "g") require.NoError(t, err) - require.Len(t, filters, 1) + require.Len(t, filters, 2) require.NotNil(t, filters[0].DefaultValue) assert.InDelta(t, 0.0, *filters[0].DefaultValue, 0) assert.Equal(t, "Count", filters[0].Unit) - assert.Equal(t, "api", filters[0].Dimensions["Service"]) + assert.Equal(t, "$.service", filters[1].Dimensions["Service"]) + assert.Equal(t, "None", filters[1].Unit, "an omitted unit is stored as None") } diff --git a/providers/aws/cloudwatchlogs/metric_filters.go b/providers/aws/cloudwatchlogs/metric_filters.go new file mode 100644 index 000000000..e79524764 --- /dev/null +++ b/providers/aws/cloudwatchlogs/metric_filters.go @@ -0,0 +1,427 @@ +package cloudwatchlogs + +import ( + "context" + "maps" + "slices" + "sort" + "strconv" + "strings" + "time" + + "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/providers/aws/cloudwatchlogs/filterpattern" + "github.com/stackshy/cloudemu/v2/services/logging/driver" + mondriver "github.com/stackshy/cloudemu/v2/services/monitoring/driver" +) + +// PutMetricFilter limits, per the PutMetricFilter and MetricTransformation +// API references. +const ( + maxMetricFiltersPerGroup = 100 + maxFilterNameLength = 512 + maxMetricNameLength = 255 + maxMetricValueLength = 100 + maxMetricDimensions = 3 + maxDimensionLength = 255 + // unitNone is the unit AWS assigns when a transformation omits one. + unitNone = "None" +) + +// validMetricUnits is the MetricTransformation unit enum. +// +//nolint:gochecknoglobals // fixed lookup table for a closed enum. +var validMetricUnits = map[string]bool{ + "Seconds": true, "Microseconds": true, "Milliseconds": true, "Bytes": true, "Kilobytes": true, + "Megabytes": true, "Gigabytes": true, "Terabytes": true, "Bits": true, "Kilobits": true, + "Megabits": true, "Gigabits": true, "Terabits": true, "Percent": true, "Count": true, + "Bytes/Second": true, "Kilobytes/Second": true, "Megabytes/Second": true, "Gigabytes/Second": true, + "Terabytes/Second": true, "Bits/Second": true, "Kilobits/Second": true, "Megabits/Second": true, + "Gigabits/Second": true, "Terabits/Second": true, "Count/Second": true, unitNone: true, +} + +// PutMetricFilter creates or updates a metric filter for a log group. The +// filter, its pattern and its single metric transformation are validated +// the way CloudWatch Logs validates them before anything is stored. +func (m *Mock) PutMetricFilter(_ context.Context, cfg *driver.MetricFilterConfig) error { + g, ok := m.groups.Get(cfg.LogGroup) + if !ok { + return errors.Newf(errors.NotFound, "log group %q not found", cfg.LogGroup) + } + + if err := validateFilterName(cfg.Name); err != nil { + return err + } + + if err := validateFilterPattern(g, metricFilterKind, cfg.Name, cfg.FilterPattern); err != nil { + return err + } + + pattern, err := filterpattern.Parse(cfg.FilterPattern) + if err != nil { + return err + } + + if err := validateTransformation(cfg, pattern); err != nil { + return err + } + + if !g.metricFilters.Has(cfg.Name) && g.metricFilters.Len() >= maxMetricFiltersPerGroup { + return errors.Newf(errors.ResourceExhausted, + "log group %q already has the maximum of %d metric filters", cfg.LogGroup, maxMetricFiltersPerGroup) + } + + unit := cfg.Unit + if unit == "" { + unit = unitNone + } + + g.metricFilters.Set(cfg.Name, &driver.MetricFilterInfo{ + Name: cfg.Name, + LogGroup: cfg.LogGroup, + FilterPattern: cfg.FilterPattern, + MetricName: cfg.MetricName, + MetricNamespace: cfg.MetricNamespace, + MetricValue: cfg.MetricValue, + DefaultValue: cfg.DefaultValue, + Unit: unit, + Dimensions: maps.Clone(cfg.Dimensions), + CreatedAt: m.opts.Clock.Now().UTC(), + }) + + return nil +} + +func validateFilterName(name string) error { + if name == "" || len(name) > maxFilterNameLength || strings.ContainsAny(name, ":*") { + return errors.Newf(errors.InvalidArgument, + "1 validation error detected: Value '%s' at 'filterName' failed to satisfy constraint: "+ + "Member must have length between 1 and %d and satisfy regular expression pattern: [^:*]*", + name, maxFilterNameLength) + } + + return nil +} + +// validateTransformation applies the MetricTransformation rules: a metric +// name and namespace, a metric value that is a number or a field the pattern +// extracts, at most three dimensions taken from pattern fields, no default +// value alongside dimensions, and a known unit. +func validateTransformation(cfg *driver.MetricFilterConfig, pattern *filterpattern.Pattern) error { + for field, v := range map[string]string{"metricName": cfg.MetricName, "metricNamespace": cfg.MetricNamespace} { + if v == "" || len(v) > maxMetricNameLength || strings.ContainsAny(v, ":*$") { + return errors.Newf(errors.InvalidArgument, + "1 validation error detected: Value '%s' at 'metricTransformations.1.member.%s' failed to satisfy constraint: "+ + "Member must have length between 1 and %d and satisfy regular expression pattern: [^:*$]*", + v, field, maxMetricNameLength) + } + } + + if err := validateMetricValue(cfg.MetricValue, pattern); err != nil { + return err + } + + if cfg.Unit != "" && !validMetricUnits[cfg.Unit] { + return errors.Newf(errors.InvalidArgument, + "1 validation error detected: Value '%s' at 'metricTransformations.1.member.unit' failed to satisfy "+ + "constraint: Member must satisfy enum value set", cfg.Unit) + } + + return validateDimensions(cfg, pattern) +} + +func validateMetricValue(value string, pattern *filterpattern.Pattern) error { + if value == "" || len(value) > maxMetricValueLength { + return errors.Newf(errors.InvalidArgument, + "1 validation error detected: Value '%s' at 'metricTransformations.1.member.metricValue' failed to "+ + "satisfy constraint: Member must have length between 1 and %d", value, maxMetricValueLength) + } + + if _, err := strconv.ParseFloat(value, 64); err == nil { + return nil + } + + if strings.HasPrefix(value, "$") && pattern.ValidRef(value) { + return nil + } + + return errors.Newf(errors.InvalidArgument, + "Invalid metric value '%s': use a number or a field the filter pattern selects", value) +} + +func validateDimensions(cfg *driver.MetricFilterConfig, pattern *filterpattern.Pattern) error { + if len(cfg.Dimensions) == 0 { + return nil + } + + switch { + case len(cfg.Dimensions) > maxMetricDimensions: + return errors.Newf(errors.InvalidArgument, + "A metric filter can have at most %d dimensions", maxMetricDimensions) + case cfg.DefaultValue != nil: + return errors.New(errors.InvalidArgument, + "A metric filter with dimensions cannot have a default value") + case !pattern.HasFields(): + return errors.New(errors.InvalidArgument, + "Dimensions are only supported for JSON and space-delimited filter patterns") + } + + for k, v := range cfg.Dimensions { + if k == "" || len(k) > maxDimensionLength || len(v) > maxDimensionLength || !pattern.ValidRef(v) { + return errors.Newf(errors.InvalidArgument, + "Invalid dimension %s=%s: the value must be a field the filter pattern selects", k, v) + } + } + + return nil +} + +// DescribeAWSMetricFilters lists metric filters, ASCII-sorted by filter name. +// Without a log group it lists every group's filters, and the name prefix is +// ignored, as CloudWatch Logs documents. +func (m *Mock) DescribeAWSMetricFilters( + _ context.Context, in driver.DescribeMetricFiltersInput, +) ([]driver.MetricFilterInfo, error) { + if (in.MetricName == "") != (in.MetricNamespace == "") { + return nil, errors.New(errors.InvalidArgument, + "metricName and metricNamespace must be specified together") + } + + groups := m.groups.SortedValues() + + if in.LogGroup != "" { + g, ok := m.groups.Get(in.LogGroup) + if !ok { + return nil, errors.Newf(errors.NotFound, "log group %q not found", in.LogGroup) + } + + groups = []*logGroup{g} + } + + var out []driver.MetricFilterInfo + + for _, g := range groups { + for _, mf := range g.metricFilters.SortedValues() { + if metricFilterSelected(mf, in) { + out = append(out, *mf) + } + } + } + + sort.SliceStable(out, func(i, j int) bool { return out[i].Name < out[j].Name }) + + return out, nil +} + +func metricFilterSelected(mf *driver.MetricFilterInfo, in driver.DescribeMetricFiltersInput) bool { + if in.LogGroup != "" && in.FilterNamePrefix != "" && !strings.HasPrefix(mf.Name, in.FilterNamePrefix) { + return false + } + + return in.MetricName == "" || (mf.MetricName == in.MetricName && mf.MetricNamespace == in.MetricNamespace) +} + +// DescribeMetricFilters lists a log group's metric filters, sorted by name. +func (m *Mock) DescribeMetricFilters(ctx context.Context, logGroup string) ([]driver.MetricFilterInfo, error) { + out, err := m.DescribeAWSMetricFilters(ctx, driver.DescribeMetricFiltersInput{LogGroup: logGroup}) + if out == nil && err == nil { + out = []driver.MetricFilterInfo{} + } + + return out, err +} + +// metricPeriod is the CloudWatch period metric filters report in. A default +// value stands for "logs arrived in this period and none matched". +const metricPeriod = time.Minute + +// filterPeriod records, per metric filter, the last period that saw a match +// and the last period a default value was published for, so a default is +// published at most once per period and never for a period with a match. +type filterPeriod struct { + Matched time.Time `json:"matched,omitzero"` + Defaulted time.Time `json:"defaulted,omitzero"` +} + +func filterPeriodKey(group, filter string) string { return group + "\x00" + filter } + +// evaluateMetricFilters turns the events of one PutLogEvents batch into +// CloudWatch metric data, one PutMetricData call per metric filter. +func (m *Mock) evaluateMetricFilters(g *logGroup, events []driver.LogEvent) { + if m.monitoring == nil { + return + } + + now := m.opts.Clock.Now() + + for _, mf := range g.metricFilters.SortedValues() { + data, matched := metricFilterData(mf, events, now) + + if m.trackPeriod(filterPeriodKey(g.info.Name, mf.Name), now, matched, mf.DefaultValue != nil) { + data = []mondriver.MetricDatum{*newMetricDatum(mf, *mf.DefaultValue, nil, now)} + } + + if len(data) > 0 { + _ = m.monitoring.PutMetricData(context.Background(), data) + } + } +} + +// trackPeriod records a batch's outcome for one filter and reports whether +// its default value should be published: only for a batch with no match, +// when the current period has neither a match nor an earlier default. +// +// A default published early in a period is not withdrawn if a match arrives +// later in the same period; AWS aggregates the whole period before deciding, +// which a synchronous emulator cannot do without delaying every datapoint. +func (m *Mock) trackPeriod(key string, now time.Time, matched, hasDefault bool) bool { + period := now.Truncate(metricPeriod) + + m.mu.Lock() + defer m.mu.Unlock() + + st := m.periods[key] + + if matched { + st.Matched = period + m.periods[key] = st + + return false + } + + if !hasDefault || st.Matched.Equal(period) || st.Defaulted.Equal(period) { + return false + } + + st.Defaulted = period + m.periods[key] = st + + return true +} + +// metricFilterData aggregates the matching events of one batch into one +// datum per distinct set of dimension values, as a statistic set (sample +// count, sum, minimum, maximum), so each match counts as its own observation +// the way AWS publishes the metric value each time a matching log is found. +// It also reports whether any event matched. A matching event whose metric +// value or dimension fields cannot be resolved contributes nothing. +func metricFilterData( + mf *driver.MetricFilterInfo, events []driver.LogEvent, now time.Time, +) (data []mondriver.MetricDatum, matched bool) { + // Patterns are validated when the filter is put; one that no longer + // parses (restored from an older snapshot) emits nothing. + pattern, err := filterpattern.Parse(mf.FilterPattern) + if err != nil { + return nil, false + } + + sets := map[string]*mondriver.MetricDatum{} + + for i := range events { + msg := events[i].Message + if !pattern.Match(msg) { + continue + } + + matched = true + + v, ok := metricFilterValue(mf.MetricValue, pattern, msg) + if !ok { + continue + } + + dims, ok := metricDimensions(mf.Dimensions, pattern, msg) + if !ok { + continue + } + + key := dimensionKey(dims) + if d, seen := sets[key]; seen { + addObservation(d.StatisticValues, v) + continue + } + + d := newMetricDatum(mf, 0, dims, now) + d.StatisticValues = &mondriver.StatisticSet{SampleCount: 1, Sum: v, Minimum: v, Maximum: v} + sets[key] = d + } + + data = make([]mondriver.MetricDatum, 0, len(sets)) + for _, k := range slices.Sorted(maps.Keys(sets)) { + data = append(data, *sets[k]) + } + + return data, matched +} + +func addObservation(s *mondriver.StatisticSet, v float64) { + s.SampleCount++ + s.Sum += v + s.Minimum = min(s.Minimum, v) + s.Maximum = max(s.Maximum, v) +} + +func newMetricDatum(mf *driver.MetricFilterInfo, v float64, dims map[string]string, now time.Time) *mondriver.MetricDatum { + unit := mf.Unit + if unit == "" { + unit = unitNone + } + + return &mondriver.MetricDatum{ + Namespace: mf.MetricNamespace, MetricName: mf.MetricName, Value: v, Unit: unit, + Dimensions: dims, Timestamp: now, + } +} + +// metricDimensions resolves each dimension's field reference in message. +func metricDimensions(spec map[string]string, pattern *filterpattern.Pattern, message string) (map[string]string, bool) { + if len(spec) == 0 { + return nil, true + } + + dims := make(map[string]string, len(spec)) + + for name, ref := range spec { + v, ok := pattern.Value(message, ref) + if !ok || v == "" { + return nil, false + } + + dims[name] = v + } + + return dims, true +} + +func dimensionKey(dims map[string]string) string { + var b strings.Builder + + for _, k := range slices.Sorted(maps.Keys(dims)) { + b.WriteString(k) + b.WriteByte(0) + b.WriteString(dims[k]) + b.WriteByte(0) + } + + return b.String() +} + +// metricFilterValue resolves a metric filter's metricValue to the number one +// matching event contributes. A literal number is used directly; a "$field" +// reference ("$.latency", "$size", "$3") takes the value the pattern extracts +// from the event, and an event whose referenced value is missing or not a +// number contributes nothing. +func metricFilterValue(raw string, pattern *filterpattern.Pattern, message string) (float64, bool) { + if v, err := strconv.ParseFloat(raw, 64); err == nil { + return v, true + } + + field, ok := pattern.Value(message, raw) + if !ok { + return 0, false + } + + v, err := strconv.ParseFloat(field, 64) + + return v, err == nil +} diff --git a/providers/aws/cloudwatchlogs/metric_transformations_test.go b/providers/aws/cloudwatchlogs/metric_transformations_test.go new file mode 100644 index 000000000..ac5413e1f --- /dev/null +++ b/providers/aws/cloudwatchlogs/metric_transformations_test.go @@ -0,0 +1,249 @@ +package cloudwatchlogs + +import ( + "context" + "fmt" + "testing" + + "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/services/logging/driver" + mondriver "github.com/stackshy/cloudemu/v2/services/monitoring/driver" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func metricFilter(name, pattern, value string) *driver.MetricFilterConfig { + return &driver.MetricFilterConfig{ + Name: name, LogGroup: "g", FilterPattern: pattern, + MetricName: "M", MetricNamespace: "App", MetricValue: value, + } +} + +func TestPutMetricFilterValidation(t *testing.T) { + def := 0.0 + + tests := []struct { + name string + mutate func(c *driver.MetricFilterConfig) + code errors.Code + }{ + {"name with colon", func(c *driver.MetricFilterConfig) { c.Name = "a:b" }, errors.InvalidArgument}, + {"missing metric name", func(c *driver.MetricFilterConfig) { c.MetricName = "" }, errors.InvalidArgument}, + {"namespace with $", func(c *driver.MetricFilterConfig) { c.MetricNamespace = "a$b" }, errors.InvalidArgument}, + {"missing metric value", func(c *driver.MetricFilterConfig) { c.MetricValue = "" }, errors.InvalidArgument}, + {"word metric value", func(c *driver.MetricFilterConfig) { c.MetricValue = "lots" }, errors.InvalidArgument}, + {"field value on unstructured pattern", func(c *driver.MetricFilterConfig) { c.MetricValue = "$size" }, errors.InvalidArgument}, + {"unknown field value", func(c *driver.MetricFilterConfig) { + c.FilterPattern, c.MetricValue = "[ip, size]", "$bytes" + }, errors.InvalidArgument}, + {"position past the pattern's fields", func(c *driver.MetricFilterConfig) { + c.FilterPattern, c.MetricValue = "[ip, user, size]", "$4" + }, errors.InvalidArgument}, + {"bad unit", func(c *driver.MetricFilterConfig) { c.Unit = "Apples" }, errors.InvalidArgument}, + {"dimensions on unstructured pattern", func(c *driver.MetricFilterConfig) { + c.Dimensions = map[string]string{"Level": "$.level"} + }, errors.InvalidArgument}, + {"four dimensions", func(c *driver.MetricFilterConfig) { + c.FilterPattern = "[a, b, c, d]" + c.Dimensions = map[string]string{"A": "$a", "B": "$b", "C": "$c", "D": "$d"} + }, errors.InvalidArgument}, + {"dimensions with default value", func(c *driver.MetricFilterConfig) { + c.FilterPattern, c.DefaultValue = `{ $.level = * }`, &def + c.Dimensions = map[string]string{"Level": "$.level"} + }, errors.InvalidArgument}, + {"dimension not a field", func(c *driver.MetricFilterConfig) { + c.FilterPattern = `{ $.level = * }` + c.Dimensions = map[string]string{"Level": "static"} + }, errors.InvalidArgument}, + {"missing group", func(c *driver.MetricFilterConfig) { c.LogGroup = "nope" }, errors.NotFound}, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + m := newWindowMock(t, 0) + cfg := metricFilter("f", "ERROR", "1") + tc.mutate(cfg) + + err := m.PutMetricFilter(context.Background(), cfg) + require.Error(t, err) + assert.Equal(t, tc.code, errors.GetCode(err), err.Error()) + }) + } +} + +func TestPutMetricFilterAccepts(t *testing.T) { + ctx := context.Background() + m := newWindowMock(t, 0) + + for i, cfg := range []*driver.MetricFilterConfig{ + metricFilter("literal", "ERROR", "2.5"), + metricFilter("json", `{ $.latency > 0 }`, "$.latency"), + metricFilter("delim-named", "[ip, user, size]", "$size"), + metricFilter("delim-positional", "[..., size]", "$3"), + } { + cfg.Unit = "Milliseconds" + require.NoError(t, m.PutMetricFilter(ctx, cfg), "case %d", i) + } +} + +func TestMetricFilterLimitPerGroup(t *testing.T) { + ctx := context.Background() + m := newWindowMock(t, 0) + + for i := range maxMetricFiltersPerGroup { + require.NoError(t, m.PutMetricFilter(ctx, metricFilter(fmt.Sprintf("f%03d", i), "ERROR", "1"))) + } + + err := m.PutMetricFilter(ctx, metricFilter("one-too-many", "ERROR", "1")) + require.Error(t, err) + assert.Equal(t, errors.ResourceExhausted, errors.GetCode(err)) + + // Updating an existing filter at the limit is fine. + require.NoError(t, m.PutMetricFilter(ctx, metricFilter("f000", "WARN", "1"))) +} + +// inNamespace returns the recorded data of one namespace, leaving out the +// AWS/Logs ingestion metrics every upload emits. +func (r *recordingMonitoring) inNamespace(ns string) []mondriver.MetricDatum { + r.mu.Lock() + defer r.mu.Unlock() + + var out []mondriver.MetricDatum + + for _, d := range r.data { + if d.Namespace == ns { + out = append(out, d) + } + } + + return out +} + +// TestMetricFilterDimensionsAndUnit publishes one datum per distinct set of +// dimension values, each with the filter's unit. +func TestMetricFilterDimensionsAndUnit(t *testing.T) { + ctx := context.Background() + m := newWindowMock(t, 0) + mon := &recordingMonitoring{} + m.SetMonitoring(mon) + + cfg := metricFilter("latency", `{ $.latency > 0 }`, "$.latency") + cfg.Unit = "Milliseconds" + cfg.Dimensions = map[string]string{"Service": "$.service"} + require.NoError(t, m.PutMetricFilter(ctx, cfg)) + + putMessages(t, m, + `{"service":"api","latency":10}`, + `{"service":"api","latency":20}`, + `{"service":"api","latency":30}`, + `{"service":"web","latency":7}`, + `{"latency":3}`, + ) + + data := mon.inNamespace("App") + require.Len(t, data, 2, "the event without a service dimension is dropped") + + // Each match is its own observation: three samples, not one summed value. + assert.Equal(t, map[string]string{"Service": "api"}, data[0].Dimensions) + assert.Equal(t, &mondriver.StatisticSet{SampleCount: 3, Sum: 60, Minimum: 10, Maximum: 30}, data[0].StatisticValues) + assert.Equal(t, map[string]string{"Service": "web"}, data[1].Dimensions) + assert.Equal(t, &mondriver.StatisticSet{SampleCount: 1, Sum: 7, Minimum: 7, Maximum: 7}, data[1].StatisticValues) + + for _, d := range data { + assert.Equal(t, "Milliseconds", d.Unit) + assert.Equal(t, "App", d.Namespace) + } +} + +// TestMetricFilterDefaultValuePerPeriod publishes the default value at most +// once per one-minute period, and never for a period that had a match. +func TestMetricFilterDefaultValuePerPeriod(t *testing.T) { + ctx := context.Background() + m := newWindowMock(t, 0) + mon := &recordingMonitoring{} + m.SetMonitoring(mon) + + def := 0.0 + cfg := metricFilter("errors", "ERROR", "1") + cfg.DefaultValue = &def + require.NoError(t, m.PutMetricFilter(ctx, cfg)) + + // Minute 1: no match, then no match again: one default only. + putMessages(t, m, "INFO ok") + putMessages(t, m, "INFO fine") + + data := mon.inNamespace("App") + require.Len(t, data, 1) + assert.InDelta(t, 0.0, data[0].Value, 0) + assert.Nil(t, data[0].StatisticValues) + assert.Equal(t, "None", data[0].Unit) + + // Minute 2: a match, then a non-matching batch: no default for the period. + advanceClock(t, m, metricPeriod) + putMessages(t, m, "ERROR a") + putMessages(t, m, "INFO b") + + data = mon.inNamespace("App") + require.Len(t, data, 2) + assert.InDelta(t, 1.0, data[1].StatisticValues.Sum, 0) + + // Minute 3: only non-matching logs, so the default is published again, + // and it survives a snapshot round trip without repeating. + advanceClock(t, m, metricPeriod) + putMessages(t, m, "INFO c") + require.Len(t, mon.inNamespace("App"), 3) + + raw, err := m.Snapshot(ctx, true) + require.NoError(t, err) + require.NoError(t, m.Restore(ctx, raw)) + + putMessages(t, m, "INFO d") + assert.Len(t, mon.inNamespace("App"), 3) +} + +func TestDescribeAWSMetricFilters(t *testing.T) { + ctx := context.Background() + m := newWindowMock(t, 0) + + _, err := m.CreateLogGroup(ctx, driver.LogGroupConfig{Name: "other"}) + require.NoError(t, err) + + for _, cfg := range []*driver.MetricFilterConfig{ + metricFilter("b-errors", "ERROR", "1"), + metricFilter("a-warn", "WARN", "1"), + } { + require.NoError(t, m.PutMetricFilter(ctx, cfg)) + } + + other := metricFilter("c-other", "ERROR", "1") + other.LogGroup, other.MetricName = "other", "Other" + require.NoError(t, m.PutMetricFilter(ctx, other)) + + names := func(in driver.DescribeMetricFiltersInput) []string { + got, derr := m.DescribeAWSMetricFilters(ctx, in) + require.NoError(t, derr) + + out := make([]string, 0, len(got)) + for _, f := range got { + out = append(out, f.Name) + } + + return out + } + + assert.Equal(t, []string{"a-warn", "b-errors", "c-other"}, names(driver.DescribeMetricFiltersInput{})) + assert.Equal(t, []string{"a-warn", "b-errors"}, names(driver.DescribeMetricFiltersInput{LogGroup: "g"})) + assert.Equal(t, []string{"b-errors"}, names(driver.DescribeMetricFiltersInput{LogGroup: "g", FilterNamePrefix: "b"})) + assert.Equal(t, []string{"a-warn", "b-errors", "c-other"}, + names(driver.DescribeMetricFiltersInput{FilterNamePrefix: "b"}), "the prefix needs a log group") + assert.Equal(t, []string{"c-other"}, + names(driver.DescribeMetricFiltersInput{MetricName: "Other", MetricNamespace: "App"})) + + _, err = m.DescribeAWSMetricFilters(ctx, driver.DescribeMetricFiltersInput{MetricName: "M"}) + require.Error(t, err) + assert.True(t, errors.IsInvalidArgument(err)) + + _, err = m.DescribeAWSMetricFilters(ctx, driver.DescribeMetricFiltersInput{LogGroup: "nope"}) + require.Error(t, err) + assert.True(t, errors.IsNotFound(err)) +} diff --git a/providers/aws/cloudwatchlogs/snapshot.go b/providers/aws/cloudwatchlogs/snapshot.go index 9a7b85c96..33fd1fc92 100644 --- a/providers/aws/cloudwatchlogs/snapshot.go +++ b/providers/aws/cloudwatchlogs/snapshot.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "maps" "github.com/stackshy/cloudemu/v2/internal/memstore" "github.com/stackshy/cloudemu/v2/internal/snapshot" @@ -20,6 +21,7 @@ var _ snapshot.Snapshottable = (*Mock)(nil) type logsSnapshot struct { Groups map[string]*logGroupSnapshot `json:"groups,omitempty"` QueryResultKmsKey string `json:"queryResultKmsKey,omitempty"` + FilterPeriods map[string]filterPeriod `json:"filterPeriods,omitempty"` } // logGroupSnapshot mirrors logGroup. Its metric- and subscription-filter stores @@ -47,7 +49,7 @@ type logStreamSnapshot struct { // log events are the payload and are always captured. func (m *Mock) Snapshot(_ context.Context, _ bool) (json.RawMessage, error) { m.mu.RLock() - snap := logsSnapshot{QueryResultKmsKey: m.queryResultKmsKey} + snap := logsSnapshot{QueryResultKmsKey: m.queryResultKmsKey, FilterPeriods: maps.Clone(m.periods)} m.mu.RUnlock() if m.groups.Len() == 0 { @@ -109,6 +111,9 @@ func (m *Mock) Restore(_ context.Context, data json.RawMessage) error { m.mu.Lock() m.queryResultKmsKey = snap.QueryResultKmsKey + + m.periods = map[string]filterPeriod{} + maps.Copy(m.periods, snap.FilterPeriods) m.mu.Unlock() for name, gs := range snap.Groups { diff --git a/server/aws/cloudwatchlogs/metric_filters.go b/server/aws/cloudwatchlogs/metric_filters.go index 1875837e8..589996e77 100644 --- a/server/aws/cloudwatchlogs/metric_filters.go +++ b/server/aws/cloudwatchlogs/metric_filters.go @@ -2,7 +2,6 @@ package cloudwatchlogs import ( "net/http" - "strings" "github.com/stackshy/cloudemu/v2/server/wire" logdriver "github.com/stackshy/cloudemu/v2/services/logging/driver" @@ -65,9 +64,11 @@ func (h *Handler) putMetricFilter(w http.ResponseWriter, r *http.Request) { return } - if len(req.MetricTransformations) == 0 { + if len(req.MetricTransformations) != 1 { wire.WriteJSONError(w, http.StatusBadRequest, "InvalidParameterException", - "metricTransformations is required and must contain exactly one item") + "1 validation error detected: Value at 'metricTransformations' failed to satisfy constraint: "+ + "Member must have length less than or equal to 1 and greater than or equal to 1") + return } @@ -91,45 +92,69 @@ func (h *Handler) putMetricFilter(w http.ResponseWriter, r *http.Request) { wire.WriteJSON(w, struct{}{}) } -// describeMetricFilters lists a log group's metric filters, ASCII-sorted by -// filter name and optionally narrowed by filterNamePrefix. +// maxDescribeMetricFiltersLimit is the largest DescribeMetricFilters limit. +const maxDescribeMetricFiltersLimit = 50 + +// describeMetricFilters lists metric filters (Logs_20140328.DescribeMetricFilters), +// for one log group or the whole account, ASCII-sorted by filter name and +// narrowed by filterNamePrefix or by metricName plus metricNamespace. func (h *Handler) describeMetricFilters(w http.ResponseWriter, r *http.Request) { + ext, ok := h.awsLogs(w, "DescribeMetricFilters") + if !ok { + return + } + var req describeMetricFiltersRequest if !wire.DecodeJSON(w, r, &req) { return } - filters, err := h.logs.DescribeMetricFilters(r.Context(), req.LogGroupName) + if req.Limit < 0 || req.Limit > maxDescribeMetricFiltersLimit { + wire.WriteJSONError(w, http.StatusBadRequest, "InvalidParameterException", + "1 validation error detected: Value at 'limit' failed to satisfy constraint: "+ + "Member must have value between 1 and 50") + + return + } + + filters, err := ext.DescribeAWSMetricFilters(r.Context(), logdriver.DescribeMetricFiltersInput{ + LogGroup: req.LogGroupName, FilterNamePrefix: req.FilterNamePrefix, + MetricName: req.MetricName, MetricNamespace: req.MetricNamespace, + }) if err != nil { writeErr(w, err) return } - out := make([]metricFilterJSON, 0, len(filters)) - - for i := range filters { - mf := &filters[i] - if req.FilterNamePrefix != "" && !strings.HasPrefix(mf.Name, req.FilterNamePrefix) { - continue - } - - out = append(out, metricFilterJSON{ - FilterName: mf.Name, - FilterPattern: mf.FilterPattern, - LogGroupName: mf.LogGroup, - CreationTime: epochMillis(mf.CreatedAt), - MetricTransformations: []metricTransformationJSON{{ - MetricName: mf.MetricName, - MetricNamespace: mf.MetricNamespace, - MetricValue: mf.MetricValue, - DefaultValue: mf.DefaultValue, - Unit: mf.Unit, - Dimensions: mf.Dimensions, - }}, - }) + from, to, next := pageBounds(len(filters), decodeOffsetToken(req.NextToken), req.Limit) + + resp := describeMetricFiltersResponse{MetricFilters: make([]metricFilterJSON, 0, to-from)} + for i := from; i < to; i++ { + resp.MetricFilters = append(resp.MetricFilters, toMetricFilterJSON(&filters[i])) + } + + if next > 0 { + resp.NextToken = encodeOffsetToken(next) } - wire.WriteJSON(w, describeMetricFiltersResponse{MetricFilters: out}) + wire.WriteJSON(w, resp) +} + +func toMetricFilterJSON(mf *logdriver.MetricFilterInfo) metricFilterJSON { + return metricFilterJSON{ + FilterName: mf.Name, + FilterPattern: mf.FilterPattern, + LogGroupName: mf.LogGroup, + CreationTime: epochMillis(mf.CreatedAt), + MetricTransformations: []metricTransformationJSON{{ + MetricName: mf.MetricName, + MetricNamespace: mf.MetricNamespace, + MetricValue: mf.MetricValue, + DefaultValue: mf.DefaultValue, + Unit: mf.Unit, + Dimensions: mf.Dimensions, + }}, + } } // deleteMetricFilter removes a metric filter from a log group. A successful diff --git a/server/aws/cloudwatchlogs/metric_transformation_sdk_test.go b/server/aws/cloudwatchlogs/metric_transformation_sdk_test.go new file mode 100644 index 000000000..2ed3cbd09 --- /dev/null +++ b/server/aws/cloudwatchlogs/metric_transformation_sdk_test.go @@ -0,0 +1,226 @@ +package cloudwatchlogs_test + +import ( + "context" + "errors" + "fmt" + "net/http/httptest" + "testing" + "time" + + "github.com/aws/aws-sdk-go-v2/aws" + awsconfig "github.com/aws/aws-sdk-go-v2/config" + "github.com/aws/aws-sdk-go-v2/credentials" + "github.com/aws/aws-sdk-go-v2/service/cloudwatch" + cwtypes "github.com/aws/aws-sdk-go-v2/service/cloudwatch/types" + cwl "github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs" + cwltypes "github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs/types" + + "github.com/stackshy/cloudemu/v2" + awsserver "github.com/stackshy/cloudemu/v2/server/aws" +) + +// newLogsAndMetricsClients serves CloudWatch Logs and CloudWatch from one +// provider, so metric filter output can be read back with GetMetricStatistics. +func newLogsAndMetricsClients(t *testing.T) (*cwl.Client, *cloudwatch.Client) { + t.Helper() + + p := cloudemu.NewAWS() + ts := httptest.NewServer(awsserver.New(awsserver.Drivers{CloudWatchLogs: p.CloudWatchLogs, CloudWatch: p.CloudWatch})) + t.Cleanup(ts.Close) + + cfg, err := awsconfig.LoadDefaultConfig(context.Background(), + awsconfig.WithRegion("us-east-1"), + awsconfig.WithCredentialsProvider(credentials.NewStaticCredentialsProvider("test", "test", "")), + ) + if err != nil { + t.Fatalf("aws config: %v", err) + } + + cfg.BaseEndpoint = aws.String(ts.URL) + + return cwl.NewFromConfig(cfg), cloudwatch.NewFromConfig(cfg) +} + +func metricSum(t *testing.T, cw *cloudwatch.Client, name string, dims ...cwtypes.Dimension) (float64, cwtypes.StandardUnit) { + t.Helper() + + now := time.Now() + + out, err := cw.GetMetricStatistics(context.Background(), &cloudwatch.GetMetricStatisticsInput{ + Namespace: aws.String("E2E/App"), MetricName: aws.String(name), Dimensions: dims, + StartTime: aws.Time(now.Add(-time.Hour)), EndTime: aws.Time(now.Add(time.Hour)), + Period: aws.Int32(7200), Statistics: []cwtypes.Statistic{cwtypes.StatisticSum}, + }) + if err != nil { + t.Fatalf("GetMetricStatistics(%s): %v", name, err) + } + + if len(out.Datapoints) == 0 { + return 0, "" + } + + return aws.ToFloat64(out.Datapoints[0].Sum), out.Datapoints[0].Unit +} + +func TestSDKMetricTransformationDimensionsUnitDefault(t *testing.T) { + logs, cw := newLogsAndMetricsClients(t) + ctx := context.Background() + g, s := aws.String("/mt/g"), aws.String("s") + + if _, err := logs.CreateLogGroup(ctx, &cwl.CreateLogGroupInput{LogGroupName: g}); err != nil { + t.Fatalf("CreateLogGroup: %v", err) + } + + if _, err := logs.CreateLogStream(ctx, &cwl.CreateLogStreamInput{LogGroupName: g, LogStreamName: s}); err != nil { + t.Fatalf("CreateLogStream: %v", err) + } + + put := func(name, pattern, value string, mt cwltypes.MetricTransformation) error { + mt.MetricName, mt.MetricNamespace, mt.MetricValue = aws.String(name), aws.String("E2E/App"), aws.String(value) + _, err := logs.PutMetricFilter(ctx, &cwl.PutMetricFilterInput{ + LogGroupName: g, FilterName: aws.String(name), FilterPattern: aws.String(pattern), + MetricTransformations: []cwltypes.MetricTransformation{mt}, + }) + + return err + } + + if err := put("Latency", `{ $.latency > 0 }`, "$.latency", cwltypes.MetricTransformation{ + Unit: cwltypes.StandardUnitMilliseconds, Dimensions: map[string]string{"Service": "$.service"}, + }); err != nil { + t.Fatalf("PutMetricFilter Latency: %v", err) + } + + if err := put("Fatal", "FATAL", "1", cwltypes.MetricTransformation{DefaultValue: aws.Float64(0)}); err != nil { + t.Fatalf("PutMetricFilter Fatal: %v", err) + } + + now := time.Now() + msgs := []string{`{"service":"api","latency":10}`, `{"service":"api","latency":5}`, `{"service":"web","latency":7}`} + + events := make([]cwltypes.InputLogEvent, 0, len(msgs)) + for i, m := range msgs { + events = append(events, cwltypes.InputLogEvent{ + Timestamp: aws.Int64(now.Add(time.Duration(i) * time.Millisecond).UnixMilli()), Message: aws.String(m), + }) + } + + if _, err := logs.PutLogEvents(ctx, &cwl.PutLogEventsInput{LogGroupName: g, LogStreamName: s, LogEvents: events}); err != nil { + t.Fatalf("PutLogEvents: %v", err) + } + + api := cwtypes.Dimension{Name: aws.String("Service"), Value: aws.String("api")} + if sum, unit := metricSum(t, cw, "Latency", api); sum != 15 || unit != cwtypes.StandardUnitMilliseconds { + t.Fatalf("Latency{Service=api} = %v %s, want 15 Milliseconds", sum, unit) + } + + // Each matching event is its own sample, not one summed value. + all, err := cw.GetMetricStatistics(ctx, &cloudwatch.GetMetricStatisticsInput{ + Namespace: aws.String("E2E/App"), MetricName: aws.String("Latency"), Dimensions: []cwtypes.Dimension{api}, + StartTime: aws.Time(now.Add(-time.Hour)), EndTime: aws.Time(now.Add(time.Hour)), Period: aws.Int32(7200), + Statistics: []cwtypes.Statistic{cwtypes.StatisticSampleCount, cwtypes.StatisticMinimum, cwtypes.StatisticMaximum}, + }) + if err != nil || len(all.Datapoints) != 1 { + t.Fatalf("Latency{api} statistics = %+v, %v", all, err) + } + + if dp := all.Datapoints[0]; aws.ToFloat64(dp.SampleCount) != 2 || aws.ToFloat64(dp.Minimum) != 5 || aws.ToFloat64(dp.Maximum) != 10 { + t.Fatalf("Latency{api} SampleCount/Min/Max = %v/%v/%v, want 2/5/10", + aws.ToFloat64(dp.SampleCount), aws.ToFloat64(dp.Minimum), aws.ToFloat64(dp.Maximum)) + } + + web := cwtypes.Dimension{Name: aws.String("Service"), Value: aws.String("web")} + if sum, _ := metricSum(t, cw, "Latency", web); sum != 7 { + t.Fatalf("Latency{Service=web} = %v, want 7", sum) + } + + // No FATAL in the batch: the default value is published instead. + stats, err := cw.GetMetricStatistics(ctx, &cloudwatch.GetMetricStatisticsInput{ + Namespace: aws.String("E2E/App"), MetricName: aws.String("Fatal"), + StartTime: aws.Time(now.Add(-time.Hour)), EndTime: aws.Time(now.Add(time.Hour)), + Period: aws.Int32(7200), Statistics: []cwtypes.Statistic{cwtypes.StatisticSampleCount}, + }) + if err != nil || len(stats.Datapoints) != 1 || aws.ToFloat64(stats.Datapoints[0].SampleCount) != 1 { + t.Fatalf("Fatal default value datapoints = %+v, %v; want one sample", stats.Datapoints, err) + } + + var invalid *cwltypes.InvalidParameterException + + if err := put("Bad", `{ $.level = * }`, "1", cwltypes.MetricTransformation{ + DefaultValue: aws.Float64(0), Dimensions: map[string]string{"Level": "$.level"}, + }); !errors.As(err, &invalid) { + t.Fatalf("dimensions with default value: got %v, want InvalidParameterException", err) + } + + if err := put("Bad", "ERROR", "$size", cwltypes.MetricTransformation{}); !errors.As(err, &invalid) { + t.Fatalf("field value on unstructured pattern: got %v, want InvalidParameterException", err) + } +} + +func TestSDKDescribeMetricFiltersFiltersAndPaging(t *testing.T) { + logs := newLogsClient(t) + ctx := context.Background() + + for _, group := range []string{"/dmf/a", "/dmf/b"} { + if _, err := logs.CreateLogGroup(ctx, &cwl.CreateLogGroupInput{LogGroupName: aws.String(group)}); err != nil { + t.Fatalf("CreateLogGroup: %v", err) + } + + for i := range 3 { + if _, err := logs.PutMetricFilter(ctx, &cwl.PutMetricFilterInput{ + LogGroupName: aws.String(group), FilterName: aws.String(fmt.Sprintf("f%d", i)), + FilterPattern: aws.String("ERROR"), + MetricTransformations: []cwltypes.MetricTransformation{{ + MetricName: aws.String(fmt.Sprintf("M%d", i)), MetricNamespace: aws.String("NS"), MetricValue: aws.String("1"), + }}, + }); err != nil { + t.Fatalf("PutMetricFilter: %v", err) + } + } + } + + page, err := logs.DescribeMetricFilters(ctx, &cwl.DescribeMetricFiltersInput{Limit: aws.Int32(4)}) + if err != nil { + t.Fatalf("DescribeMetricFilters: %v", err) + } + + if len(page.MetricFilters) != 4 || page.NextToken == nil { + t.Fatalf("first page = %d filters, token %v; want 4 and a token", len(page.MetricFilters), page.NextToken) + } + + rest, err := logs.DescribeMetricFilters(ctx, &cwl.DescribeMetricFiltersInput{Limit: aws.Int32(4), NextToken: page.NextToken}) + if err != nil || len(rest.MetricFilters) != 2 || rest.NextToken != nil { + t.Fatalf("second page = %+v, %v", rest, err) + } + + byMetric, err := logs.DescribeMetricFilters(ctx, &cwl.DescribeMetricFiltersInput{ + MetricName: aws.String("M1"), MetricNamespace: aws.String("NS"), + }) + if err != nil || len(byMetric.MetricFilters) != 2 { + t.Fatalf("metric filter by metric = %+v, %v; want one per group", byMetric, err) + } + + byPrefix, err := logs.DescribeMetricFilters(ctx, &cwl.DescribeMetricFiltersInput{ + LogGroupName: aws.String("/dmf/a"), FilterNamePrefix: aws.String("f2"), + }) + if err != nil || len(byPrefix.MetricFilters) != 1 || aws.ToString(byPrefix.MetricFilters[0].FilterName) != "f2" { + t.Fatalf("by prefix = %+v, %v", byPrefix, err) + } + + if u := byPrefix.MetricFilters[0].MetricTransformations[0].Unit; u != cwltypes.StandardUnitNone { + t.Fatalf("unit = %q, want None when the filter omits one", u) + } + + var invalid *cwltypes.InvalidParameterException + + if _, err := logs.DescribeMetricFilters(ctx, &cwl.DescribeMetricFiltersInput{MetricName: aws.String("M1")}); !errors.As(err, &invalid) { + t.Fatalf("metricName without namespace: got %v, want InvalidParameterException", err) + } + + var nf *cwltypes.ResourceNotFoundException + + if _, err := logs.DescribeMetricFilters(ctx, &cwl.DescribeMetricFiltersInput{LogGroupName: aws.String("/dmf/none")}); !errors.As(err, &nf) { + t.Fatalf("missing group: got %v, want ResourceNotFoundException", err) + } +} diff --git a/services/logging/driver/aws.go b/services/logging/driver/aws.go index f757263fe..f279e304c 100644 --- a/services/logging/driver/aws.go +++ b/services/logging/driver/aws.go @@ -64,6 +64,16 @@ type MetricFilterMatch struct { ExtractedValues map[string]string } +// DescribeMetricFiltersInput narrows DescribeAWSMetricFilters. LogGroup is +// optional (empty lists every log group); FilterNamePrefix only applies with +// a LogGroup; MetricName and MetricNamespace must be given together. +type DescribeMetricFiltersInput struct { + LogGroup string + FilterNamePrefix string + MetricName string + MetricNamespace string +} + // AWSLogging is an optional interface implemented by the AWS CloudWatch Logs // backend for operations with no cross-provider equivalent. The CloudWatch // Logs wire handler type-asserts for it; Azure, GCP and OCI backends do not @@ -83,6 +93,9 @@ type AWSLogging interface { AssociateKmsKey(ctx context.Context, logGroup, resourceIdentifier, kmsKeyID string) error DisassociateKmsKey(ctx context.Context, logGroup, resourceIdentifier string) error + // DescribeAWSMetricFilters lists metric filters, sorted by filter name. + DescribeAWSMetricFilters(ctx context.Context, input DescribeMetricFiltersInput) ([]MetricFilterInfo, error) + // TestMetricFilter runs a filter pattern over sample log messages. TestMetricFilter(ctx context.Context, filterPattern string, logEventMessages []string) ([]MetricFilterMatch, error) } diff --git a/services/logging/logging_test.go b/services/logging/logging_test.go index e0c315e7c..702ddec37 100644 --- a/services/logging/logging_test.go +++ b/services/logging/logging_test.go @@ -520,10 +520,12 @@ func TestPutMetricFilterPortable(t *testing.T) { require.NoError(t, err) err = l.PutMetricFilter(ctx, &driver.MetricFilterConfig{ - Name: "mf", - LogGroup: "grp", - FilterPattern: "ERROR", - MetricName: "ErrorCount", + Name: "mf", + LogGroup: "grp", + FilterPattern: "ERROR", + MetricName: "ErrorCount", + MetricNamespace: "App", + MetricValue: "1", }) require.NoError(t, err) }) @@ -549,10 +551,12 @@ func TestDeleteMetricFilterPortable(t *testing.T) { require.NoError(t, err) err = l.PutMetricFilter(ctx, &driver.MetricFilterConfig{ - Name: "mf", - LogGroup: "grp", - FilterPattern: "ERROR", - MetricName: "ErrorCount", + Name: "mf", + LogGroup: "grp", + FilterPattern: "ERROR", + MetricName: "ErrorCount", + MetricNamespace: "App", + MetricValue: "1", }) require.NoError(t, err) @@ -578,18 +582,22 @@ func TestDescribeMetricFiltersPortable(t *testing.T) { require.NoError(t, err) err = l.PutMetricFilter(ctx, &driver.MetricFilterConfig{ - Name: "mf1", - LogGroup: "grp", - FilterPattern: "ERROR", - MetricName: "ErrorCount", + Name: "mf1", + LogGroup: "grp", + FilterPattern: "ERROR", + MetricName: "ErrorCount", + MetricNamespace: "App", + MetricValue: "1", }) require.NoError(t, err) err = l.PutMetricFilter(ctx, &driver.MetricFilterConfig{ - Name: "mf2", - LogGroup: "grp", - FilterPattern: "WARN", - MetricName: "WarnCount", + Name: "mf2", + LogGroup: "grp", + FilterPattern: "WARN", + MetricName: "WarnCount", + MetricNamespace: "App", + MetricValue: "1", }) require.NoError(t, err) From bcf2800289b8603150d39851ec70fd94700d5527 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Patel Date: Thu, 22 Oct 2026 06:24:42 +0530 Subject: [PATCH 07/31] fix(aws-eventbridge): archives and replay (#1659) * feat(eventbridge): archives and replay Add CreateArchive, DescribeArchive, UpdateArchive, DeleteArchive and ListArchives, with archives capturing matching events on PutEvents and honoring retention. Add StartReplay, DescribeReplay, CancelReplay and ListReplays: a replay re-delivers archived events in its time window to the listed rules (or every rule on the bus) with a replay-name field, and replayed events are never archived again. Archives and replays are included in snapshots. * fix(eventbridge): match replay FilterArns on the full rule ARN --- providers/aws/eventbridge/archives.go | 364 ++++++++++++++++++ providers/aws/eventbridge/archives_test.go | 224 +++++++++++ providers/aws/eventbridge/eventbridge.go | 80 +++- providers/aws/eventbridge/put_events.go | 1 + providers/aws/eventbridge/replay.go | 284 ++++++++++++++ providers/aws/eventbridge/replay_test.go | 338 ++++++++++++++++ providers/aws/eventbridge/snapshot.go | 59 ++- providers/aws/eventbridge/snapshot_test.go | 43 +++ providers/aws/eventbridge/validation.go | 107 +++++ .../eventbridge/archive_replay_sdk_test.go | 190 +++++++++ server/aws/eventbridge/archives.go | 287 ++++++++++++++ server/aws/eventbridge/delivery_sdk_test.go | 5 +- server/aws/eventbridge/handler.go | 19 +- server/aws/eventbridge/replays.go | 143 +++++++ services/eventbus/driver/archive.go | 105 +++++ 15 files changed, 2222 insertions(+), 27 deletions(-) create mode 100644 providers/aws/eventbridge/archives.go create mode 100644 providers/aws/eventbridge/archives_test.go create mode 100644 providers/aws/eventbridge/replay.go create mode 100644 providers/aws/eventbridge/replay_test.go create mode 100644 providers/aws/eventbridge/validation.go create mode 100644 server/aws/eventbridge/archive_replay_sdk_test.go create mode 100644 server/aws/eventbridge/archives.go create mode 100644 server/aws/eventbridge/replays.go create mode 100644 services/eventbus/driver/archive.go diff --git a/providers/aws/eventbridge/archives.go b/providers/aws/eventbridge/archives.go new file mode 100644 index 000000000..6b1d99a69 --- /dev/null +++ b/providers/aws/eventbridge/archives.go @@ -0,0 +1,364 @@ +package eventbridge + +import ( + "context" + "fmt" + "sort" + "strings" + "sync" + "time" + + "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/internal/eventmatch" + "github.com/stackshy/cloudemu/v2/internal/idgen" + "github.com/stackshy/cloudemu/v2/services/eventbus/driver" +) + +const ( + archiveStateEnabled = "ENABLED" + + maxArchiveNameLen = 48 + maxDescriptionLen = 512 + maxPatternLen = 4096 + + hoursPerDay = 24 +) + +// archiveStates is the ArchiveState enum, in the order EventBridge lists it. +func archiveStates() []string { + return []string{"ENABLED", "DISABLED", "CREATING", "UPDATING", "CREATE_FAILED", "UPDATE_FAILED"} +} + +// archivedEvent is one event held by an archive, with the time it was archived +// (retention counts from it) and its envelope size in bytes. +type archivedEvent struct { + Event driver.Event `json:"event"` + Archived time.Time `json:"archived"` + Size int `json:"size"` +} + +// archiveData is an archive and the events it holds. busName is the event bus +// it captures from. mu guards info and events. +type archiveData struct { + mu sync.RWMutex + info driver.Archive + busName string + events []archivedEvent +} + +func errBusNotExist(name string) error { + return errors.Newf(errors.NotFound, "Event bus %s does not exist.", name) +} + +func errArchiveNotExist(name string) error { + return errors.Newf(errors.NotFound, "Archive %s does not exist.", name) +} + +// validatePattern checks an archive event pattern, reporting a failure as +// InvalidEventPatternException. +func validatePattern(pattern string) error { + if pattern == "" { + return nil + } + + if err := eventmatch.ValidatePattern(pattern); err != nil { + return fmt.Errorf("%w: %w", driver.ErrInvalidEventPattern, errors.New(errors.InvalidArgument, err.Error())) + } + + return nil +} + +func validateArchiveConfig(cfg *driver.ArchiveConfig) error { + var v violations + + v.name(cfg.Name, "archiveName", maxArchiveNameLen) + v.eventsARN(cfg.EventSourceARN, "eventSourceArn") + v.maxLen(cfg.Description, "description", maxDescriptionLen) + v.maxLen(cfg.EventPattern, "eventPattern", maxPatternLen) + + if cfg.RetentionDays < 0 { + v.add(fmt.Sprint(cfg.RetentionDays), "retentionDays", "Member must have value greater than or equal to 0") + } + + if err := v.err(); err != nil { + return err + } + + return validatePattern(cfg.EventPattern) +} + +// CreateArchive creates an archive that captures the events put on the event +// bus named by cfg.EventSourceARN. +func (m *Mock) CreateArchive(_ context.Context, cfg *driver.ArchiveConfig) (*driver.Archive, error) { + if err := validateArchiveConfig(cfg); err != nil { + return nil, err + } + + busName := busNameFromARN(cfg.EventSourceARN) + if !m.buses.Has(busName) { + return nil, errBusNotExist(busName) + } + + ad := &archiveData{ + busName: busName, + info: driver.Archive{ + Name: cfg.Name, + ARN: idgen.AWSARN("events", m.opts.Region, m.opts.AccountID, "archive/"+cfg.Name), + EventSourceARN: cfg.EventSourceARN, + Description: cfg.Description, + EventPattern: compactPattern(cfg.EventPattern), + State: archiveStateEnabled, + KMSKeyIdentifier: cfg.KMSKeyIdentifier, + RetentionDays: cfg.RetentionDays, + CreatedAt: m.opts.Clock.Now().UTC(), + }, + } + + if !m.archives.SetIfAbsent(cfg.Name, ad) { + return nil, errors.Newf(errors.AlreadyExists, "Archive %s already exists.", cfg.Name) + } + + out := ad.info + + return &out, nil +} + +// DescribeArchive returns an archive with its current event count and size. +func (m *Mock) DescribeArchive(_ context.Context, name string) (*driver.Archive, error) { + ad, ok := m.archives.Get(name) + if !ok { + return nil, errArchiveNotExist(name) + } + + out := m.archiveView(ad) + + return &out, nil +} + +// archiveView prunes expired events and returns a copy of the archive's info +// with EventCount and SizeBytes filled in. +func (m *Mock) archiveView(ad *archiveData) driver.Archive { + ad.mu.Lock() + defer ad.mu.Unlock() + + m.pruneLocked(ad) + + out := ad.info + out.EventCount = int64(len(ad.events)) + + for i := range ad.events { + out.SizeBytes += int64(ad.events[i].Size) + } + + return out +} + +// pruneLocked drops events older than the archive's retention period. The +// caller holds ad.mu for writing. +func (m *Mock) pruneLocked(ad *archiveData) { + if ad.info.RetentionDays <= 0 || len(ad.events) == 0 { + return + } + + cutoff := m.opts.Clock.Now().Add(-time.Duration(ad.info.RetentionDays) * hoursPerDay * time.Hour) + + kept := ad.events[:0] + + for i := range ad.events { + if ad.events[i].Archived.After(cutoff) { + kept = append(kept, ad.events[i]) + } + } + + ad.events = kept +} + +// msgArchiveUpdateEmpty is EventBridge's reply to an UpdateArchive call that +// changes nothing. +const msgArchiveUpdateEmpty = "At least one of EventPattern, RetentionDays or Description must be provided." + +func validateArchiveUpdate(upd *driver.ArchiveUpdate) error { + if upd.Description == nil && upd.EventPattern == nil && upd.RetentionDays == nil && upd.KMSKeyIdentifier == nil { + return errors.New(errors.InvalidArgument, msgArchiveUpdateEmpty) + } + + if err := archiveUpdateViolations(upd).err(); err != nil { + return err + } + + if upd.EventPattern != nil { + return validatePattern(*upd.EventPattern) + } + + return nil +} + +func archiveUpdateViolations(upd *driver.ArchiveUpdate) violations { + var v violations + + if upd.Description != nil { + v.maxLen(*upd.Description, "description", maxDescriptionLen) + } + + if upd.EventPattern != nil { + v.maxLen(*upd.EventPattern, "eventPattern", maxPatternLen) + } + + if upd.RetentionDays != nil && *upd.RetentionDays < 0 { + v.add(fmt.Sprint(*upd.RetentionDays), "retentionDays", "Member must have value greater than or equal to 0") + } + + return v +} + +// UpdateArchive changes the description, event pattern, retention period or KMS +// key of an archive. Omitted fields keep their values. +func (m *Mock) UpdateArchive(_ context.Context, upd *driver.ArchiveUpdate) (*driver.Archive, error) { + if err := validateArchiveUpdate(upd); err != nil { + return nil, err + } + + ad, ok := m.archives.Get(upd.Name) + if !ok { + return nil, errArchiveNotExist(upd.Name) + } + + ad.mu.Lock() + + if upd.Description != nil { + ad.info.Description = *upd.Description + } + + if upd.EventPattern != nil { + ad.info.EventPattern = compactPattern(*upd.EventPattern) + } + + if upd.RetentionDays != nil { + ad.info.RetentionDays = *upd.RetentionDays + } + + if upd.KMSKeyIdentifier != nil { + ad.info.KMSKeyIdentifier = *upd.KMSKeyIdentifier + } + + ad.mu.Unlock() + + out := m.archiveView(ad) + + return &out, nil +} + +// DeleteArchive deletes an archive and the events it holds. +func (m *Mock) DeleteArchive(_ context.Context, name string) error { + if !m.archives.Delete(name) { + return errArchiveNotExist(name) + } + + return nil +} + +// ListArchives lists archives sorted by name, narrowed by at most one filter. +func (m *Mock) ListArchives(_ context.Context, filter driver.ArchiveFilter) ([]driver.Archive, error) { + if err := m.validateListFilter("ListArchives", filter.NamePrefix, filter.EventSourceARN, filter.State, archiveStates()); err != nil { + return nil, err + } + + busName := busNameFromARN(filter.EventSourceARN) + if filter.EventSourceARN != "" && !m.buses.Has(busName) { + return nil, errBusNotExist(busName) + } + + out := make([]driver.Archive, 0, m.archives.Len()) + + for _, ad := range m.archives.All() { + a := m.archiveView(ad) + + if !strings.HasPrefix(a.Name, filter.NamePrefix) || + (filter.State != "" && a.State != filter.State) || + (filter.EventSourceARN != "" && busNameFromARN(a.EventSourceARN) != busName) { + continue + } + + out = append(out, a) + } + + sort.Slice(out, func(i, j int) bool { return out[i].Name < out[j].Name }) + + return out, nil +} + +// validateListFilter enforces the ListArchives/ListReplays rule that at most +// one of NamePrefix, EventSourceArn and State is given, and that State is one +// of the resource's states. +func (*Mock) validateListFilter(op, prefix, sourceARN, state string, states []string) error { + if countSet(prefix, sourceARN, state) > 1 { + return errors.Newf(errors.InvalidArgument, + "At most one filter is allowed for %s. Use either : State, EventSourceArn, or NamePrefix.", op) + } + + if state == "" { + return nil + } + + var v violations + + v.enum(state, "state", states) + + return v.err() +} + +// captureToArchives adds an event just put on busName to every enabled archive +// on that bus whose pattern it matches. Replayed events never come through here. +// No other lock is held while an archive's lock is taken. +func (m *Mock) captureToArchives(busName string, event *driver.Event) { + if m.archives.Len() == 0 { + return + } + + var size int + + for _, ad := range m.archives.All() { + if ad.busName != busName { + continue + } + + if size == 0 { + size = len(m.eventEnvelope(event)) + } + + m.captureOne(ad, event, size) + } +} + +func (m *Mock) captureOne(ad *archiveData, event *driver.Event, size int) { + ad.mu.Lock() + defer ad.mu.Unlock() + + if ad.info.State != archiveStateEnabled || !matchesPattern(event, ad.info.EventPattern) { + return + } + + m.pruneLocked(ad) + + ad.events = append(ad.events, archivedEvent{Event: *event, Archived: m.opts.Clock.Now().UTC(), Size: size}) +} + +// archiveEventsBetween returns a copy of the archive's events whose time falls +// in [start, end), in archive order. +func (m *Mock) archiveEventsBetween(ad *archiveData, start, end time.Time) []driver.Event { + ad.mu.Lock() + defer ad.mu.Unlock() + + m.pruneLocked(ad) + + out := make([]driver.Event, 0, len(ad.events)) + + for i := range ad.events { + t := ad.events[i].Event.Time + if !t.Before(start) && t.Before(end) { + out = append(out, ad.events[i].Event) + } + } + + return out +} diff --git a/providers/aws/eventbridge/archives_test.go b/providers/aws/eventbridge/archives_test.go new file mode 100644 index 000000000..afaf348b1 --- /dev/null +++ b/providers/aws/eventbridge/archives_test.go @@ -0,0 +1,224 @@ +package eventbridge + +import ( + "context" + "errors" + "testing" + "time" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/services/eventbus/driver" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func busARN(name string) string { + return "arn:aws:events:us-east-1:123456789012:event-bus/" + name +} + +func createTestArchive(t *testing.T, m *Mock, name, bus, pattern string) *driver.Archive { + t.Helper() + + a, err := m.CreateArchive(context.Background(), &driver.ArchiveConfig{ + Name: name, EventSourceARN: busARN(bus), EventPattern: pattern, + }) + require.NoError(t, err) + + return a +} + +func putTestEvent(t *testing.T, m *Mock, bus, source string) { + t.Helper() + + _, err := m.PutEvents(context.Background(), []driver.Event{ + {Source: source, DetailType: "t", Detail: `{"k":"v"}`, EventBus: bus}, + }) + require.NoError(t, err) +} + +func TestCreateArchive(t *testing.T) { + m, _ := newTestMock() + ctx := context.Background() + + a, err := m.CreateArchive(ctx, &driver.ArchiveConfig{ + Name: "a1", EventSourceARN: busARN("default"), Description: "d", + EventPattern: `{ "source": ["s"] }`, RetentionDays: 3, + }) + require.NoError(t, err) + assert.Equal(t, "arn:aws:events:us-east-1:123456789012:archive/a1", a.ARN) + assert.Equal(t, "ENABLED", a.State) + assert.Equal(t, `{"source":["s"]}`, a.EventPattern) + assert.Equal(t, 3, a.RetentionDays) + assert.False(t, a.CreatedAt.IsZero()) + + t.Run("duplicate", func(t *testing.T) { + _, err := m.CreateArchive(ctx, &driver.ArchiveConfig{Name: "a1", EventSourceARN: busARN("default")}) + require.True(t, cerrors.IsAlreadyExists(err)) + assert.Equal(t, "Archive a1 already exists.", cerrors.Message(err)) + }) + + t.Run("unknown bus", func(t *testing.T) { + _, err := m.CreateArchive(ctx, &driver.ArchiveConfig{Name: "a2", EventSourceARN: busARN("ghost")}) + require.True(t, cerrors.IsNotFound(err)) + assert.Equal(t, "Event bus ghost does not exist.", cerrors.Message(err)) + }) + + t.Run("invalid name", func(t *testing.T) { + _, err := m.CreateArchive(ctx, &driver.ArchiveConfig{Name: "bad name", EventSourceARN: busARN("default")}) + require.True(t, cerrors.IsInvalidArgument(err)) + assert.Equal(t, "1 validation error detected: Value 'bad name' at 'archiveName' failed to satisfy constraint: "+ + `Member must satisfy regular expression pattern: [\.\-_A-Za-z0-9]+`, cerrors.Message(err)) + }) + + t.Run("several violations are joined", func(t *testing.T) { + _, err := m.CreateArchive(ctx, &driver.ArchiveConfig{Name: "bad name", EventSourceARN: "nope", RetentionDays: -1}) + require.True(t, cerrors.IsInvalidArgument(err)) + assert.Contains(t, cerrors.Message(err), "3 validation errors detected: ") + }) + + t.Run("invalid pattern", func(t *testing.T) { + _, err := m.CreateArchive(ctx, &driver.ArchiveConfig{Name: "a3", EventSourceARN: busARN("default"), EventPattern: `{"source":"s"}`}) + require.Error(t, err) + assert.True(t, errors.Is(err, driver.ErrInvalidEventPattern)) + }) +} + +func TestArchiveCapturesMatchingEvents(t *testing.T) { + m, _ := newTestMock() + ctx := context.Background() + createTestBus(t, m, "b1") + + createTestArchive(t, m, "filtered", "b1", `{"source":["keep"]}`) + createTestArchive(t, m, "all", "b1", "") + createTestArchive(t, m, "other-bus", "default", "") + + putTestEvent(t, m, "b1", "keep") + putTestEvent(t, m, "b1", "drop") + + filtered, err := m.DescribeArchive(ctx, "filtered") + require.NoError(t, err) + assert.Equal(t, int64(1), filtered.EventCount) + assert.Positive(t, filtered.SizeBytes) + + all, err := m.DescribeArchive(ctx, "all") + require.NoError(t, err) + assert.Equal(t, int64(2), all.EventCount) + + other, err := m.DescribeArchive(ctx, "other-bus") + require.NoError(t, err) + assert.Equal(t, int64(0), other.EventCount) +} + +func TestArchiveRetention(t *testing.T) { + m, fc := newTestMock() + ctx := context.Background() + + _, err := m.CreateArchive(ctx, &driver.ArchiveConfig{Name: "a", EventSourceARN: busARN("default"), RetentionDays: 1}) + require.NoError(t, err) + + putTestEvent(t, m, "", "s") + fc.Advance(23 * time.Hour) + + a, err := m.DescribeArchive(ctx, "a") + require.NoError(t, err) + assert.Equal(t, int64(1), a.EventCount) + + fc.Advance(2 * time.Hour) + + a, err = m.DescribeArchive(ctx, "a") + require.NoError(t, err) + assert.Equal(t, int64(0), a.EventCount) + assert.Equal(t, int64(0), a.SizeBytes) +} + +func TestUpdateArchive(t *testing.T) { + m, _ := newTestMock() + ctx := context.Background() + createTestArchive(t, m, "a", "default", "") + + t.Run("no field given is rejected before the archive is looked up", func(t *testing.T) { + _, err := m.UpdateArchive(ctx, &driver.ArchiveUpdate{Name: "ghost"}) + require.True(t, cerrors.IsInvalidArgument(err)) + assert.Equal(t, "At least one of EventPattern, RetentionDays or Description must be provided.", cerrors.Message(err)) + }) + + t.Run("unknown archive", func(t *testing.T) { + desc := "x" + _, err := m.UpdateArchive(ctx, &driver.ArchiveUpdate{Name: "ghost", Description: &desc}) + require.True(t, cerrors.IsNotFound(err)) + assert.Equal(t, "Archive ghost does not exist.", cerrors.Message(err)) + }) + + t.Run("fields are updated and zero retention is honored", func(t *testing.T) { + desc, pattern, days := "new", `{"source":["n"]}`, 7 + _, err := m.UpdateArchive(ctx, &driver.ArchiveUpdate{Name: "a", Description: &desc, EventPattern: &pattern, RetentionDays: &days}) + require.NoError(t, err) + + zero := 0 + out, err := m.UpdateArchive(ctx, &driver.ArchiveUpdate{Name: "a", RetentionDays: &zero}) + require.NoError(t, err) + assert.Equal(t, "new", out.Description) + assert.Equal(t, pattern, out.EventPattern) + assert.Equal(t, 0, out.RetentionDays) + }) + + t.Run("invalid pattern", func(t *testing.T) { + bad := `{"source":1}` + _, err := m.UpdateArchive(ctx, &driver.ArchiveUpdate{Name: "a", EventPattern: &bad}) + assert.True(t, errors.Is(err, driver.ErrInvalidEventPattern)) + }) +} + +func TestDeleteArchive(t *testing.T) { + m, _ := newTestMock() + ctx := context.Background() + createTestArchive(t, m, "a", "default", "") + + require.NoError(t, m.DeleteArchive(ctx, "a")) + + _, err := m.DescribeArchive(ctx, "a") + assert.True(t, cerrors.IsNotFound(err)) + + err = m.DeleteArchive(ctx, "a") + require.True(t, cerrors.IsNotFound(err)) + assert.Equal(t, "Archive a does not exist.", cerrors.Message(err)) +} + +func TestListArchives(t *testing.T) { + m, _ := newTestMock() + ctx := context.Background() + createTestBus(t, m, "b1") + createTestArchive(t, m, "beta", "default", "") + createTestArchive(t, m, "alpha", "b1", "") + + all, err := m.ListArchives(ctx, driver.ArchiveFilter{}) + require.NoError(t, err) + require.Len(t, all, 2) + assert.Equal(t, "alpha", all[0].Name) + + byPrefix, err := m.ListArchives(ctx, driver.ArchiveFilter{NamePrefix: "be"}) + require.NoError(t, err) + require.Len(t, byPrefix, 1) + assert.Equal(t, "beta", byPrefix[0].Name) + + byBus, err := m.ListArchives(ctx, driver.ArchiveFilter{EventSourceARN: busARN("b1")}) + require.NoError(t, err) + require.Len(t, byBus, 1) + assert.Equal(t, "alpha", byBus[0].Name) + + byState, err := m.ListArchives(ctx, driver.ArchiveFilter{State: "DISABLED"}) + require.NoError(t, err) + assert.Empty(t, byState) + + _, err = m.ListArchives(ctx, driver.ArchiveFilter{NamePrefix: "a", State: "ENABLED"}) + require.True(t, cerrors.IsInvalidArgument(err)) + assert.Equal(t, "At most one filter is allowed for ListArchives. Use either : State, EventSourceArn, or NamePrefix.", cerrors.Message(err)) + + _, err = m.ListArchives(ctx, driver.ArchiveFilter{EventSourceARN: busARN("ghost")}) + require.True(t, cerrors.IsNotFound(err)) + assert.Equal(t, "Event bus ghost does not exist.", cerrors.Message(err)) + + _, err = m.ListArchives(ctx, driver.ArchiveFilter{State: "BOGUS"}) + require.True(t, cerrors.IsInvalidArgument(err)) + assert.Contains(t, cerrors.Message(err), "Member must satisfy enum value set: [ENABLED, DISABLED, CREATING, UPDATING, CREATE_FAILED, UPDATE_FAILED]") +} diff --git a/providers/aws/eventbridge/eventbridge.go b/providers/aws/eventbridge/eventbridge.go index 30185037c..c98a78a2c 100644 --- a/providers/aws/eventbridge/eventbridge.go +++ b/providers/aws/eventbridge/eventbridge.go @@ -10,6 +10,7 @@ import ( "maps" "strings" "sync" + "sync/atomic" "time" "github.com/stackshy/cloudemu/v2/config" @@ -95,6 +96,12 @@ type Mock struct { sns SNSPublisher sfn StepFunctionsStarter tagsByARN tagStore + + archives *memstore.Store[*archiveData] + replays *memstore.Store[driver.Replay] + // activeReplays counts replays currently delivering, bounded by + // maxActiveReplays. + activeReplays atomic.Int32 } // SetMonitoring sets the monitoring backend for auto-metric generation. @@ -139,8 +146,10 @@ func (m *Mock) emitMetric(ctx context.Context, metricName string, value float64, // New creates a new EventBridge mock with the given configuration options. func New(opts *config.Options) *Mock { m := &Mock{ - buses: memstore.New[*busData](), - opts: opts, + buses: memstore.New[*busData](), + opts: opts, + archives: memstore.New[*archiveData](), + replays: memstore.New[driver.Replay](), } // Create the default event bus automatically. @@ -550,14 +559,19 @@ func (m *Mock) PublishServiceEvent(ctx context.Context, source, detailType strin // A target whose dispatch fails is routed to its configured DeadLetterConfig // queue (see deliverToTarget), matching real EventBridge's DLQ behavior. func (m *Mock) deliverToTargets(ctx context.Context, matched []driver.Rule, event *driver.Event) { + m.deliverEnvelope(ctx, matched, event, m.eventEnvelope(event)) +} + +// deliverEnvelope delivers an already-rendered envelope for event to the +// targets of the matched rules. Replays use it to deliver an envelope that +// carries the replay name. +func (m *Mock) deliverEnvelope(ctx context.Context, matched []driver.Rule, event *driver.Event, envelope []byte) { // Decouple delivery from the caller's cancellation/deadline (real EventBridge // delivery is asynchronous) while carrying forward the re-entrant delivery // depth so a Lambda target that re-publishes an event stays bounded (the guard // lives in lambda.InvokeExternal). dctx := recursionguard.WithDepth(context.Background(), recursionguard.Depth(ctx)) - envelope := m.eventEnvelope(event) - busName := event.EventBus if busName == "" { busName = defaultBusName @@ -731,8 +745,37 @@ func envelopeWithoutDetail(envelope []byte) json.RawMessage { return out } +// Envelope keys shared by the delivered envelope and the object patterns match. +const ( + envKeySource = "source" + envKeyDetailType = "detail-type" +) + // eventEnvelope renders the standard EventBridge delivery envelope for an event. func (m *Mock) eventEnvelope(event *driver.Event) []byte { + return marshalEnvelope(m.envelopeFields(event)) +} + +// replayEnvelope renders the envelope of a replayed event, which carries the +// name of the replay that sent it in a top-level "replay-name" field. +func (m *Mock) replayEnvelope(event *driver.Event, replayName string) []byte { + fields := m.envelopeFields(event) + fields["replay-name"] = replayName + + return marshalEnvelope(fields) +} + +func marshalEnvelope(fields map[string]any) []byte { + body, err := json.Marshal(fields) + if err != nil { + return []byte("{}") + } + + return body +} + +// envelopeFields returns the fields of the standard EventBridge envelope. +func (m *Mock) envelopeFields(event *driver.Event) map[string]any { detail := json.RawMessage(event.Detail) if len(detail) == 0 { detail = json.RawMessage("{}") @@ -746,22 +789,17 @@ func (m *Mock) eventEnvelope(event *driver.Event) []byte { resources = []string{} } - body, err := json.Marshal(map[string]any{ - "version": "0", - "id": event.ID, - "detail-type": event.DetailType, - "source": event.Source, - "account": m.opts.AccountID, - "time": event.Time.UTC().Format(time.RFC3339), - "region": m.opts.Region, - "resources": resources, - "detail": detail, - }) - if err != nil { - return []byte("{}") + return map[string]any{ + "version": "0", + "id": event.ID, + envKeyDetailType: event.DetailType, + envKeySource: event.Source, + "account": m.opts.AccountID, + "time": event.Time.UTC().Format(time.RFC3339), + "region": m.opts.Region, + "resources": resources, + "detail": detail, } - - return body } // GetEventHistory retrieves event history for an event bus. @@ -862,8 +900,8 @@ func matchesPattern(event *driver.Event, pattern string) bool { // reach into it; an unparsable detail is treated as an empty object. func eventObject(event *driver.Event) map[string]any { obj := map[string]any{ - "source": event.Source, - "detail-type": event.DetailType, + envKeySource: event.Source, + envKeyDetailType: event.DetailType, } if len(event.Resources) > 0 { diff --git a/providers/aws/eventbridge/put_events.go b/providers/aws/eventbridge/put_events.go index cf6b3d53c..d7049fb52 100644 --- a/providers/aws/eventbridge/put_events.go +++ b/providers/aws/eventbridge/put_events.go @@ -190,6 +190,7 @@ func (m *Mock) publishEvent(ctx context.Context, event *driver.Event, index int) } m.storeEvent(bd, event) + m.captureToArchives(busName, event) matched := m.MatchedRules(event) m.deliverToTargets(ctx, matched, event) diff --git a/providers/aws/eventbridge/replay.go b/providers/aws/eventbridge/replay.go new file mode 100644 index 000000000..9019720ca --- /dev/null +++ b/providers/aws/eventbridge/replay.go @@ -0,0 +1,284 @@ +package eventbridge + +import ( + "context" + "fmt" + "sort" + "strings" + + "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/internal/idgen" + "github.com/stackshy/cloudemu/v2/services/eventbus/driver" +) + +const ( + replayStateStarting = "STARTING" + replayStateRunning = "RUNNING" + replayStateCancelling = "CANCELLING" //nolint:misspell // AWS ReplayState enum value + replayStateCompleted = "COMPLETED" + replayStateCancelled = "CANCELLED" //nolint:misspell // AWS ReplayState enum value + + maxReplayNameLen = 64 + + // maxActiveReplays is how many replays may run at once in an account. + maxActiveReplays = 10 + + archiveARNInfix = ":archive/" +) + +// StartReplay validation messages, as EventBridge words them. +const ( + msgDestinationNotBus = "Parameter Destination.Arn is not valid. Reason: Must contain an event bus ARN." + msgCrossBusReplay = "Parameter Destination.Arn is not valid. Reason: Cross event bus replay is not permitted." + msgReplayWindow = "Parameter EventEndTime is not valid. Reason: EventStartTime must be before EventEndTime." + msgReplayLimit = "The requested resource exceeds the maximum number allowed." +) + +// replayStates is the ReplayState enum, in the order EventBridge lists it. +func replayStates() []string { + return []string{ + replayStateStarting, replayStateRunning, replayStateCancelling, + replayStateCompleted, replayStateCancelled, "FAILED", + } +} + +func errReplayNotExist(name string) error { + return errors.Newf(errors.NotFound, "Replay %s does not exist.", name) +} + +func validateReplayConfig(cfg *driver.ReplayConfig) error { + var v violations + + v.name(cfg.Name, "replayName", maxReplayNameLen) + v.eventsARN(cfg.EventSourceARN, "eventSourceArn") + v.maxLen(cfg.Description, "description", maxDescriptionLen) + + if cfg.Destination.ARN == "" { + v.add("", "destination.arn", "Member must have length greater than or equal to 1") + } + + return v.err() +} + +// replayTarget resolves and checks what a StartReplay call refers to: the +// destination bus must exist, the archive must exist, the destination must be +// the archive's own bus, and the time window must be non-empty. +func (m *Mock) replayTarget(cfg *driver.ReplayConfig) (*archiveData, error) { + destBus := busNameFromARN(cfg.Destination.ARN) + if destBus == "" { + return nil, errors.New(errors.InvalidArgument, msgDestinationNotBus) + } + + if !m.buses.Has(destBus) { + return nil, errBusNotExist(destBus) + } + + archiveName := cfg.EventSourceARN[strings.LastIndex(cfg.EventSourceARN, "/")+1:] + + ad, ok := m.archives.Get(archiveName) + if !ok || !strings.Contains(cfg.EventSourceARN, archiveARNInfix) { + return nil, errors.Newf(errors.InvalidArgument, + "Parameter EventSourceArn is not valid. Reason: Archive %s does not exist.", archiveName) + } + + if ad.busName != destBus { + return nil, errors.New(errors.InvalidArgument, msgCrossBusReplay) + } + + if !cfg.EventStartTime.Before(cfg.EventEndTime) { + return nil, errors.New(errors.InvalidArgument, msgReplayWindow) + } + + return ad, nil +} + +// StartReplay replays the archived events in [EventStartTime, EventEndTime) to +// the archive's event bus, delivering each only to the rules in +// Destination.FilterARNs (or every enabled rule on the bus when none are given). +// Replayed events carry a "replay-name" field and are never archived again. +// +// The replay runs to completion before StartReplay returns, so the response +// reports STARTING while DescribeReplay right after reports COMPLETED. +func (m *Mock) StartReplay(ctx context.Context, cfg *driver.ReplayConfig) (*driver.Replay, error) { + if err := validateReplayConfig(cfg); err != nil { + return nil, err + } + + ad, err := m.replayTarget(cfg) + if err != nil { + return nil, err + } + + if m.activeReplays.Add(1) > maxActiveReplays { + m.activeReplays.Add(-1) + + return nil, errors.New(errors.ResourceExhausted, msgReplayLimit) + } + defer m.activeReplays.Add(-1) + + replay := driver.Replay{ + Name: cfg.Name, + ARN: idgen.AWSARN("events", m.opts.Region, m.opts.AccountID, "replay/"+cfg.Name), + Description: cfg.Description, + State: replayStateStarting, + EventSourceARN: cfg.EventSourceARN, + Destination: driver.ReplayDestination{ARN: cfg.Destination.ARN, FilterARNs: append([]string(nil), cfg.Destination.FilterARNs...)}, + EventStartTime: cfg.EventStartTime.UTC(), + EventEndTime: cfg.EventEndTime.UTC(), + ReplayStartTime: m.opts.Clock.Now().UTC(), + } + + if !m.replays.SetIfAbsent(cfg.Name, replay) { + return nil, errors.Newf(errors.AlreadyExists, "Replay %s already exists.", cfg.Name) + } + + started := replay + + m.runReplay(ctx, &replay, ad) + + return &started, nil +} + +// runReplay delivers the replay's events with no lock held, stopping early once +// a stop request has been recorded, then records the final state. +func (m *Mock) runReplay(ctx context.Context, replay *driver.Replay, ad *archiveData) { + m.setReplayState(replay.Name, replayStateStarting, replayStateRunning) + + events := m.archiveEventsBetween(ad, replay.EventStartTime, replay.EventEndTime) + filter := make(map[string]bool, len(replay.Destination.FilterARNs)) + + for _, arn := range replay.Destination.FilterARNs { + filter[arn] = true + } + + var last driver.Event + + for i := range events { + if r, ok := m.replays.Get(replay.Name); !ok || r.State == replayStateCancelling { + break + } + + m.replayEvent(ctx, &events[i], replay.Name, filter) + last = events[i] + } + + now := m.opts.Clock.Now().UTC() + + m.replays.Update(replay.Name, func(r driver.Replay) driver.Replay { + r.ReplayEndTime = now + if !last.Time.IsZero() { + r.EventLastReplayedTime = last.Time.UTC() + } + + if r.State == replayStateCancelling { + r.State = replayStateCancelled + } else { + r.State = replayStateCompleted + } + + return r + }) +} + +// replayEvent delivers one archived event to the matching rules of its bus that +// the replay's filter allows, with the replay name added to the envelope. A +// FilterArns entry must be the rule's full ARN, so one naming the same bus and +// rule in another account or region selects nothing. +func (m *Mock) replayEvent(ctx context.Context, event *driver.Event, replayName string, filter map[string]bool) { + matched := m.MatchedRules(event) + + if len(filter) > 0 { + kept := matched[:0] + + for i := range matched { + if filter[m.ruleARN(matched[i].EventBus, matched[i].Name)] { + kept = append(kept, matched[i]) + } + } + + matched = kept + } + + m.deliverEnvelope(ctx, matched, event, m.replayEnvelope(event, replayName)) +} + +// setReplayState moves a replay from one state to another, doing nothing when +// it is no longer in the expected state. +func (m *Mock) setReplayState(name, from, to string) { + m.replays.Update(name, func(r driver.Replay) driver.Replay { + if r.State == from { + r.State = to + } + + return r + }) +} + +// DescribeReplay returns a replay and its progress. +func (m *Mock) DescribeReplay(_ context.Context, name string) (*driver.Replay, error) { + r, ok := m.replays.Get(name) + if !ok { + return nil, errReplayNotExist(name) + } + + return &r, nil +} + +// CancelReplay asks a starting or running replay to stop. The replay reports the +// stopping state until its runner exits, then the final stopped state; a replay +// in any other state cannot be stopped. +func (m *Mock) CancelReplay(_ context.Context, name string) (*driver.Replay, error) { + var ( + out driver.Replay + illegal bool + ) + + found := m.replays.Update(name, func(r driver.Replay) driver.Replay { + if r.State != replayStateStarting && r.State != replayStateRunning { + illegal = true + } else { + r.State = replayStateCancelling + } + + out = r + + return r + }) + + if !found { + return nil, errReplayNotExist(name) + } + + if illegal { + return nil, fmt.Errorf("%w: %w", driver.ErrIllegalStatus, + errors.Newf(errors.FailedPrecondition, "Replay %s is not in a valid state for this operation.", name)) + } + + return &out, nil +} + +// ListReplays lists replays sorted by name, narrowed by at most one filter. +func (m *Mock) ListReplays(_ context.Context, filter driver.ReplayFilter) ([]driver.Replay, error) { + if err := m.validateListFilter("ListReplays", filter.NamePrefix, filter.EventSourceARN, filter.State, replayStates()); err != nil { + return nil, err + } + + out := make([]driver.Replay, 0, m.replays.Len()) + + all := m.replays.All() + + for name := range all { + r := all[name] + if !strings.HasPrefix(r.Name, filter.NamePrefix) || + (filter.State != "" && r.State != filter.State) || + (filter.EventSourceARN != "" && r.EventSourceARN != filter.EventSourceARN) { + continue + } + + out = append(out, r) + } + + sort.Slice(out, func(i, j int) bool { return out[i].Name < out[j].Name }) + + return out, nil +} diff --git a/providers/aws/eventbridge/replay_test.go b/providers/aws/eventbridge/replay_test.go new file mode 100644 index 000000000..4d7a6e6de --- /dev/null +++ b/providers/aws/eventbridge/replay_test.go @@ -0,0 +1,338 @@ +package eventbridge + +import ( + "context" + "encoding/json" + "errors" + "sync" + "testing" + "time" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/services/eventbus/driver" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// recordingSQS records every body delivered to each queue ARN. +type recordingSQS struct { + mu sync.Mutex + bodies map[string][]string +} + +func (r *recordingSQS) DeliverExternal(_ context.Context, queueARN, body string) error { + r.mu.Lock() + defer r.mu.Unlock() + + if r.bodies == nil { + r.bodies = map[string][]string{} + } + + r.bodies[queueARN] = append(r.bodies[queueARN], body) + + return nil +} + +func (r *recordingSQS) got(queueARN string) []map[string]any { + r.mu.Lock() + defer r.mu.Unlock() + + out := make([]map[string]any, 0, len(r.bodies[queueARN])) + + for _, b := range r.bodies[queueARN] { + var env map[string]any + if json.Unmarshal([]byte(b), &env) == nil { + out = append(out, env) + } + } + + return out +} + +const ( + queueA = "arn:aws:sqs:us-east-1:123456789012:qa" + queueB = "arn:aws:sqs:us-east-1:123456789012:qb" +) + +// replayFixture builds a bus "b1" with two rules (ra -> queueA, rb -> queueB) +// matching source "s", an archive on it, and two archived events. +func replayFixture(t *testing.T) (*Mock, *recordingSQS, *driver.Archive, time.Time) { + t.Helper() + + m, fc := newTestMock() + sqs := &recordingSQS{} + m.SetSQSDeliverer(sqs) + ctx := context.Background() + + createTestBus(t, m, "b1") + + for rule, queue := range map[string]string{"ra": queueA, "rb": queueB} { + _, err := m.PutRule(ctx, &driver.RuleConfig{Name: rule, EventBus: "b1", EventPattern: `{"source":["s"]}`}) + require.NoError(t, err) + require.NoError(t, m.PutTargets(ctx, "b1", rule, []driver.Target{{ID: "q", ARN: queue}})) + } + + a := createTestArchive(t, m, "arch", "b1", "") + start := fc.Now() + + putTestEvent(t, m, "b1", "s") + fc.Advance(time.Minute) + putTestEvent(t, m, "b1", "s") + fc.Advance(time.Minute) + + // Clear what the original puts delivered so tests see only the replay. + sqs.bodies = nil + + return m, sqs, a, start +} + +func replayConfig(name string, a *driver.Archive, start time.Time, filters ...string) *driver.ReplayConfig { + return &driver.ReplayConfig{ + Name: name, EventSourceARN: a.ARN, + EventStartTime: start, EventEndTime: start.Add(time.Hour), + Destination: driver.ReplayDestination{ARN: busARN("b1"), FilterARNs: filters}, + } +} + +func TestStartReplayDeliversToFilteredRules(t *testing.T) { + m, sqs, a, start := replayFixture(t) + ctx := context.Background() + + out, err := m.StartReplay(ctx, replayConfig("rp", a, start, m.ruleARN("b1", "ra"))) + require.NoError(t, err) + assert.Equal(t, "STARTING", out.State) + assert.Equal(t, "arn:aws:events:us-east-1:123456789012:replay/rp", out.ARN) + + got := sqs.got(queueA) + require.Len(t, got, 2) + assert.Equal(t, "rp", got[0]["replay-name"]) + assert.Empty(t, sqs.got(queueB), "a rule outside FilterArns must not receive replayed events") + + desc, err := m.DescribeReplay(ctx, "rp") + require.NoError(t, err) + assert.Equal(t, "COMPLETED", desc.State) + assert.False(t, desc.ReplayEndTime.IsZero()) + assert.Equal(t, start.Add(time.Minute).UTC(), desc.EventLastReplayedTime) + + arch, err := m.DescribeArchive(ctx, "arch") + require.NoError(t, err) + assert.Equal(t, int64(2), arch.EventCount, "replayed events must not be archived again") + + history, err := m.GetEventHistory(ctx, "b1", 0) + require.NoError(t, err) + assert.Len(t, history, 2, "replayed events are not added to the bus history") +} + +func TestStartReplayFilterMatchesFullRuleARN(t *testing.T) { + m, sqs, a, start := replayFixture(t) + ctx := context.Background() + + // Same bus and rule name as rule ra, but in another account and region. + others := []string{ + "arn:aws:events:us-east-1:999999999999:rule/b1/ra", + "arn:aws:events:eu-west-1:123456789012:rule/b1/ra", + } + + _, err := m.StartReplay(ctx, replayConfig("rp", a, start, others...)) + require.NoError(t, err) + assert.Empty(t, sqs.got(queueA), "a FilterArn for another account or region must not select the local rule") + assert.Empty(t, sqs.got(queueB)) +} + +func TestStartReplayWithoutFiltersUsesAllRules(t *testing.T) { + m, sqs, a, start := replayFixture(t) + + _, err := m.StartReplay(context.Background(), replayConfig("rp", a, start)) + require.NoError(t, err) + assert.Len(t, sqs.got(queueA), 2) + assert.Len(t, sqs.got(queueB), 2) +} + +func TestStartReplayTimeWindow(t *testing.T) { + m, sqs, a, start := replayFixture(t) + + cfg := replayConfig("rp", a, start.Add(30*time.Second)) + _, err := m.StartReplay(context.Background(), cfg) + require.NoError(t, err) + assert.Len(t, sqs.got(queueA), 1, "only the event inside the window is replayed") +} + +func TestStartReplayErrors(t *testing.T) { + m, _, a, start := replayFixture(t) + ctx := context.Background() + createTestBus(t, m, "b2") + + tests := []struct { + name string + mutate func(c *driver.ReplayConfig) + check func(error) bool + message string + }{ + { + name: "unknown destination bus", + mutate: func(c *driver.ReplayConfig) { c.Destination.ARN = busARN("ghost") }, + check: cerrors.IsNotFound, + message: "Event bus ghost does not exist.", + }, + { + name: "unknown archive", + mutate: func(c *driver.ReplayConfig) { c.EventSourceARN = "arn:aws:events:us-east-1:123456789012:archive/nope" }, + check: cerrors.IsInvalidArgument, + message: "Parameter EventSourceArn is not valid. Reason: Archive nope does not exist.", + }, + { + name: "cross bus", + mutate: func(c *driver.ReplayConfig) { c.Destination.ARN = busARN("b2") }, + check: cerrors.IsInvalidArgument, + message: "Parameter Destination.Arn is not valid. Reason: Cross event bus replay is not permitted.", + }, + { + name: "start equals end", + mutate: func(c *driver.ReplayConfig) { c.EventEndTime = c.EventStartTime }, + check: cerrors.IsInvalidArgument, + message: "Parameter EventEndTime is not valid. Reason: EventStartTime must be before EventEndTime.", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + cfg := replayConfig("rp-"+tc.name[:4], a, start) + tc.mutate(cfg) + + _, err := m.StartReplay(ctx, cfg) + require.True(t, tc.check(err), "got %v", err) + assert.Equal(t, tc.message, cerrors.Message(err)) + }) + } + + t.Run("duplicate name", func(t *testing.T) { + _, err := m.StartReplay(ctx, replayConfig("dup", a, start)) + require.NoError(t, err) + + _, err = m.StartReplay(ctx, replayConfig("dup", a, start)) + require.True(t, cerrors.IsAlreadyExists(err)) + assert.Equal(t, "Replay dup already exists.", cerrors.Message(err)) + }) +} + +func TestCancelReplay(t *testing.T) { + m, _, a, start := replayFixture(t) + ctx := context.Background() + + _, err := m.CancelReplay(ctx, "ghost") + require.True(t, cerrors.IsNotFound(err)) + assert.Equal(t, "Replay ghost does not exist.", cerrors.Message(err)) + + _, err = m.StartReplay(ctx, replayConfig("rp", a, start)) + require.NoError(t, err) + + _, err = m.CancelReplay(ctx, "rp") + require.True(t, errors.Is(err, driver.ErrIllegalStatus)) + assert.Equal(t, "Replay rp is not in a valid state for this operation.", cerrors.Message(err)) + + // A replay still in progress can be cancelled and ends CANCELLED. + m.replays.Set("live", driver.Replay{Name: "live", State: "RUNNING"}) + + out, err := m.CancelReplay(ctx, "live") + require.NoError(t, err) + assert.Equal(t, "CANCELLING", out.State) +} + +func TestReplayStopsWhenCancelled(t *testing.T) { + m, sqs, a, start := replayFixture(t) + + replay := driver.Replay{ + Name: "rp", State: "CANCELLING", Destination: driver.ReplayDestination{ARN: busARN("b1")}, + EventStartTime: start, EventEndTime: start.Add(time.Hour), + } + m.replays.Set("rp", replay) + + ad, ok := m.archives.Get(a.Name) + require.True(t, ok) + + m.runReplay(context.Background(), &replay, ad) + + assert.Empty(t, sqs.got(queueA)) + + got, err := m.DescribeReplay(context.Background(), "rp") + require.NoError(t, err) + assert.Equal(t, "CANCELLED", got.State) +} + +func TestReplayLimit(t *testing.T) { + m, _, a, start := replayFixture(t) + m.activeReplays.Store(maxActiveReplays) + + _, err := m.StartReplay(context.Background(), replayConfig("rp", a, start)) + require.Equal(t, cerrors.ResourceExhausted, cerrors.GetCode(err)) + assert.Equal(t, "The requested resource exceeds the maximum number allowed.", cerrors.Message(err)) + + _, err = m.DescribeReplay(context.Background(), "rp") + assert.True(t, cerrors.IsNotFound(err), "a rejected replay must not be recorded") +} + +func TestListReplays(t *testing.T) { + m, _, a, start := replayFixture(t) + ctx := context.Background() + + for _, name := range []string{"rb", "ra"} { + _, err := m.StartReplay(ctx, replayConfig(name, a, start)) + require.NoError(t, err) + } + + all, err := m.ListReplays(ctx, driver.ReplayFilter{}) + require.NoError(t, err) + require.Len(t, all, 2) + assert.Equal(t, "ra", all[0].Name) + + byState, err := m.ListReplays(ctx, driver.ReplayFilter{State: "COMPLETED"}) + require.NoError(t, err) + assert.Len(t, byState, 2) + + bySource, err := m.ListReplays(ctx, driver.ReplayFilter{EventSourceARN: a.ARN}) + require.NoError(t, err) + assert.Len(t, bySource, 2) + + _, err = m.ListReplays(ctx, driver.ReplayFilter{NamePrefix: "r", EventSourceARN: a.ARN}) + require.True(t, cerrors.IsInvalidArgument(err)) + assert.Equal(t, "At most one filter is allowed for ListReplays. Use either : State, EventSourceArn, or NamePrefix.", cerrors.Message(err)) +} + +// TestArchiveAndReplayConcurrent exercises capture, update, list and replay at +// the same time so the race detector can check the locking. +func TestArchiveAndReplayConcurrent(t *testing.T) { + m, _, a, start := replayFixture(t) + ctx := context.Background() + + var wg sync.WaitGroup + + for _, name := range []string{"rp-a", "rp-b", "rp-c", "rp-d"} { + wg.Add(3) + + go func() { + defer wg.Done() + + _, _ = m.PutEvents(ctx, []driver.Event{{Source: "s", DetailType: "t", Detail: "{}", EventBus: "b1"}}) + }() + + go func() { + defer wg.Done() + + _, _ = m.StartReplay(ctx, replayConfig(name, a, start)) + }() + + go func() { + defer wg.Done() + + days := 2 + _, _ = m.UpdateArchive(ctx, &driver.ArchiveUpdate{Name: "arch", RetentionDays: &days}) + _, _ = m.ListArchives(ctx, driver.ArchiveFilter{}) + }() + } + + wg.Wait() + + replays, err := m.ListReplays(ctx, driver.ReplayFilter{State: "COMPLETED"}) + require.NoError(t, err) + assert.Len(t, replays, 4) +} diff --git a/providers/aws/eventbridge/snapshot.go b/providers/aws/eventbridge/snapshot.go index ea00be141..626a5bac9 100644 --- a/providers/aws/eventbridge/snapshot.go +++ b/providers/aws/eventbridge/snapshot.go @@ -21,8 +21,17 @@ var _ snapshot.Snapshottable = (*Mock)(nil) // mutexes, the wired SQS/Lambda/SNS/StepFunctions targets, the monitoring // backend, and *config.Options are intentionally not captured. type ebSnapshot struct { - Buses map[string]*busSnapshot `json:"buses,omitempty"` - Tags map[string]map[string]string `json:"tags,omitempty"` + Buses map[string]*busSnapshot `json:"buses,omitempty"` + Tags map[string]map[string]string `json:"tags,omitempty"` + Archives map[string]*archiveSnapshot `json:"archives,omitempty"` + Replays json.RawMessage `json:"replays,omitempty"` +} + +// archiveSnapshot mirrors archiveData, promoting its unexported fields. +type archiveSnapshot struct { + Info driver.Archive `json:"info"` + BusName string `json:"busName"` + Events []archivedEvent `json:"events,omitempty"` } // busSnapshot mirrors busData, promoting its nested rule store, its buffered @@ -59,6 +68,16 @@ func (m *Mock) Snapshot(_ context.Context, _ bool) (json.RawMessage, error) { } snap.Tags = m.snapshotTags() + snap.Archives = m.snapshotArchives() + + if m.replays.Len() > 0 { + replays, err := m.replays.Snapshot() + if err != nil { + return nil, fmt.Errorf("eventbridge: snapshot replays: %w", err) + } + + snap.Replays = replays + } return json.Marshal(snap) } @@ -118,7 +137,7 @@ func (m *Mock) snapshotTags() map[string]map[string]string { } // Restore rebuilds every event bus under its original name (rules under their -// names, targets under their ids) plus the per-ARN tag store. +// names, targets under their ids), the per-ARN tag store, archives and replays. func (m *Mock) Restore(_ context.Context, data json.RawMessage) error { var snap ebSnapshot if err := json.Unmarshal(data, &snap); err != nil { @@ -136,9 +155,43 @@ func (m *Mock) Restore(_ context.Context, data json.RawMessage) error { m.restoreTags(snap.Tags) + for name, as := range snap.Archives { + m.archives.Set(name, &archiveData{ + info: as.Info, + busName: as.BusName, + events: append([]archivedEvent(nil), as.Events...), + }) + } + + if len(snap.Replays) > 0 { + if err := m.replays.LoadSnapshot(snap.Replays); err != nil { + return fmt.Errorf("eventbridge: restore replays: %w", err) + } + } + return nil } +func (m *Mock) snapshotArchives() map[string]*archiveSnapshot { + if m.archives.Len() == 0 { + return nil + } + + out := make(map[string]*archiveSnapshot, m.archives.Len()) + + for name, ad := range m.archives.All() { + ad.mu.RLock() + out[name] = &archiveSnapshot{ + Info: ad.info, + BusName: ad.busName, + Events: append([]archivedEvent(nil), ad.events...), + } + ad.mu.RUnlock() + } + + return out +} + func restoreBus(bs *busSnapshot) (*busData, error) { bd := &busData{ info: bs.Info, diff --git a/providers/aws/eventbridge/snapshot_test.go b/providers/aws/eventbridge/snapshot_test.go index 016e7780a..66bd8e716 100644 --- a/providers/aws/eventbridge/snapshot_test.go +++ b/providers/aws/eventbridge/snapshot_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "testing" + "time" "github.com/stackshy/cloudemu/v2/services/eventbus/driver" "github.com/stretchr/testify/assert" @@ -68,3 +69,45 @@ func TestSnapshotEmpty(t *testing.T) { assert.True(t, bytes.Equal(data, data2)) } + +// TestSnapshotArchivesAndReplays proves archives (with their archived events) +// and replays survive a snapshot and restore. +func TestSnapshotArchivesAndReplays(t *testing.T) { + ctx := context.Background() + + src, _ := newTestMock() + a := createTestArchive(t, src, "arch", "default", `{"source":["s"]}`) + putTestEvent(t, src, "", "s") + + _, err := src.StartReplay(ctx, &driver.ReplayConfig{ + Name: "rp", EventSourceARN: a.ARN, + EventStartTime: a.CreatedAt.Add(-time.Hour), EventEndTime: a.CreatedAt.Add(time.Hour), + Destination: driver.ReplayDestination{ARN: busARN("default")}, + }) + require.NoError(t, err) + + data, err := src.Snapshot(ctx, true) + require.NoError(t, err) + + dst, _ := newTestMock() + require.NoError(t, dst.Restore(ctx, data)) + + data2, err := dst.Snapshot(ctx, true) + require.NoError(t, err) + assert.JSONEq(t, string(data), string(data2)) + + arch, err := dst.DescribeArchive(ctx, "arch") + require.NoError(t, err) + assert.Equal(t, int64(1), arch.EventCount) + + // The restored archive keeps capturing from its bus. + putTestEvent(t, dst, "", "s") + + arch, err = dst.DescribeArchive(ctx, "arch") + require.NoError(t, err) + assert.Equal(t, int64(2), arch.EventCount) + + rp, err := dst.DescribeReplay(ctx, "rp") + require.NoError(t, err) + assert.Equal(t, "COMPLETED", rp.State) +} diff --git a/providers/aws/eventbridge/validation.go b/providers/aws/eventbridge/validation.go new file mode 100644 index 000000000..356f6b634 --- /dev/null +++ b/providers/aws/eventbridge/validation.go @@ -0,0 +1,107 @@ +package eventbridge + +import ( + "fmt" + "regexp" + "strings" + + "github.com/stackshy/cloudemu/v2/errors" +) + +// resourceNamePattern is the character set EventBridge allows in archive, +// replay, connection and API destination names, as its error messages quote it. +const resourceNamePattern = `[\.\-_A-Za-z0-9]+` + +var resourceNameRE = regexp.MustCompile(`^[.\-_A-Za-z0-9]+$`) + +// eventsARNPattern is the shape EventBridge requires of an event bus or archive +// ARN parameter. +const eventsARNPattern = `^arn:aws([a-z]|\-)*:events:([a-z]|\d|\-)*:([0-9]{12})?:.+\/.+$` + +var eventsARNRE = regexp.MustCompile(eventsARNPattern) + +// violations collects AWS input-constraint failures and renders them the way +// EventBridge reports them: "N validation error(s) detected: Value 'v' at +// 'field' failed to satisfy constraint: ...", joined with "; ". +type violations []string + +func (v *violations) add(value, field, constraint string) { + *v = append(*v, fmt.Sprintf("Value '%s' at '%s' failed to satisfy constraint: %s", value, field, constraint)) +} + +// name checks a resource name against its maximum length and the name pattern. +func (v *violations) name(value, field string, maxLen int) { + if value == "" { + v.add(value, field, "Member must have length greater than or equal to 1") + + return + } + + if len(value) > maxLen { + v.add(value, field, fmt.Sprintf("Member must have length less than or equal to %d", maxLen)) + } + + if !resourceNameRE.MatchString(value) { + v.add(value, field, "Member must satisfy regular expression pattern: "+resourceNamePattern) + } +} + +func (v *violations) maxLen(value, field string, limit int) { + if len(value) > limit { + v.add(value, field, fmt.Sprintf("Member must have length less than or equal to %d", limit)) + } +} + +func (v *violations) enum(value, field string, allowed []string) { + for _, a := range allowed { + if value == a { + return + } + } + + v.add(value, field, "Member must satisfy enum value set: ["+strings.Join(allowed, ", ")+"]") +} + +func (v *violations) eventsARN(value, field string) { + if !eventsARNRE.MatchString(value) { + v.add(value, field, "Member must satisfy regular expression pattern: "+eventsARNPattern) + } +} + +// err returns the combined ValidationException, or nil when nothing failed. +func (v violations) err() error { + switch len(v) { + case 0: + return nil + case 1: + return errors.New(errors.InvalidArgument, "1 validation error detected: "+v[0]) + default: + return errors.Newf(errors.InvalidArgument, "%d validation errors detected: %s", len(v), strings.Join(v, "; ")) + } +} + +// busNameFromARN returns the bus name an event bus ARN refers to, or "" when the +// ARN does not name an event bus. +func busNameFromARN(arn string) string { + const infix = ":event-bus/" + + idx := strings.LastIndex(arn, infix) + if idx == -1 { + return "" + } + + return arn[idx+len(infix):] +} + +// countSet reports how many of the given strings are non-empty. +func countSet(values ...string) int { + n := 0 + + for _, s := range values { + if s != "" { + n++ + } + } + + return n +} diff --git a/server/aws/eventbridge/archive_replay_sdk_test.go b/server/aws/eventbridge/archive_replay_sdk_test.go new file mode 100644 index 000000000..accd458f7 --- /dev/null +++ b/server/aws/eventbridge/archive_replay_sdk_test.go @@ -0,0 +1,190 @@ +package eventbridge_test + +import ( + "context" + "encoding/json" + "errors" + "testing" + "time" + + "github.com/aws/aws-sdk-go-v2/aws" + awseb "github.com/aws/aws-sdk-go-v2/service/eventbridge" + ebtypes "github.com/aws/aws-sdk-go-v2/service/eventbridge/types" + "github.com/aws/smithy-go" +) + +const sdkBusARN = "arn:aws:events:us-east-1:123456789012:event-bus/" + +func requireAPIError(t *testing.T, err error, code, msg string) { + t.Helper() + + var apiErr smithy.APIError + if !errors.As(err, &apiErr) { + t.Fatalf("error = %v, want %s", err, code) + } + + if apiErr.ErrorCode() != code || (msg != "" && apiErr.ErrorMessage() != msg) { + t.Fatalf("error = %s: %q, want %s: %q", apiErr.ErrorCode(), apiErr.ErrorMessage(), code, msg) + } +} + +// TestSDKArchiveAndReplayLifecycle drives archives and replays through the real +// SDK: events put on a bus are archived, a replay re-delivers them to the one +// rule it names with a replay-name field, and the replay completes. +func TestSDKArchiveAndReplayLifecycle(t *testing.T) { + eb, sqs := newEBAndSQS(t) + ctx := context.Background() + start := time.Now().Add(-time.Minute) + + if _, err := eb.CreateEventBus(ctx, &awseb.CreateEventBusInput{Name: aws.String("b1")}); err != nil { + t.Fatalf("CreateEventBus: %v", err) + } + + created, err := eb.CreateArchive(ctx, &awseb.CreateArchiveInput{ + ArchiveName: aws.String("arch"), EventSourceArn: aws.String(sdkBusARN + "b1"), + EventPattern: aws.String(`{"source":["app"]}`), RetentionDays: aws.Int32(1), + }) + if err != nil { + t.Fatalf("CreateArchive: %v", err) + } + + if created.State != ebtypes.ArchiveStateEnabled || aws.ToString(created.ArchiveArn) == "" { + t.Fatalf("CreateArchive = %+v", created) + } + + url, arn := makeQueue(t, sqs, "replay-q") + + rule, err := eb.PutRule(ctx, &awseb.PutRuleInput{ + Name: aws.String("r"), EventBusName: aws.String("b1"), EventPattern: aws.String(`{"source":["app"]}`), + }) + if err != nil { + t.Fatalf("PutRule: %v", err) + } + + if _, err := eb.PutTargets(ctx, &awseb.PutTargetsInput{ + Rule: aws.String("r"), EventBusName: aws.String("b1"), + Targets: []ebtypes.Target{{Id: aws.String("q"), Arn: aws.String(arn)}}, + }); err != nil { + t.Fatalf("PutTargets: %v", err) + } + + if _, err := eb.PutEvents(ctx, &awseb.PutEventsInput{Entries: []ebtypes.PutEventsRequestEntry{ + {Source: aws.String("app"), DetailType: aws.String("t"), Detail: aws.String("{}"), EventBusName: aws.String("b1")}, + {Source: aws.String("other"), DetailType: aws.String("t"), Detail: aws.String("{}"), EventBusName: aws.String("b1")}, + }}); err != nil { + t.Fatalf("PutEvents: %v", err) + } + + if _, n := receiveOne(t, sqs, url); n != 1 { + t.Fatalf("original delivery count = %d, want 1", n) + } + + desc, err := eb.DescribeArchive(ctx, &awseb.DescribeArchiveInput{ArchiveName: aws.String("arch")}) + if err != nil { + t.Fatalf("DescribeArchive: %v", err) + } + + if desc.EventCount != 1 || desc.SizeBytes == 0 || aws.ToString(desc.EventPattern) != `{"source":["app"]}` { + t.Fatalf("DescribeArchive = count %d size %d pattern %q", desc.EventCount, desc.SizeBytes, aws.ToString(desc.EventPattern)) + } + + started, err := eb.StartReplay(ctx, &awseb.StartReplayInput{ + ReplayName: aws.String("rp"), EventSourceArn: created.ArchiveArn, + EventStartTime: aws.Time(start), EventEndTime: aws.Time(time.Now().Add(time.Minute)), + Destination: &ebtypes.ReplayDestination{Arn: aws.String(sdkBusARN + "b1"), FilterArns: []string{aws.ToString(rule.RuleArn)}}, + }) + if err != nil { + t.Fatalf("StartReplay: %v", err) + } + + if started.State != ebtypes.ReplayStateStarting { + t.Fatalf("StartReplay state = %s, want STARTING", started.State) + } + + body, n := receiveOne(t, sqs, url) + if n != 1 { + t.Fatalf("replayed delivery count = %d, want 1", n) + } + + var env map[string]any + if err := json.Unmarshal([]byte(body), &env); err != nil || env["replay-name"] != "rp" { + t.Fatalf("replayed body = %s, want replay-name rp", body) + } + + rp, err := eb.DescribeReplay(ctx, &awseb.DescribeReplayInput{ReplayName: aws.String("rp")}) + if err != nil { + t.Fatalf("DescribeReplay: %v", err) + } + + if rp.State != ebtypes.ReplayStateCompleted || rp.ReplayEndTime == nil || len(rp.Destination.FilterArns) != 1 { + t.Fatalf("DescribeReplay = %+v", rp) + } + + _, err = eb.CancelReplay(ctx, &awseb.CancelReplayInput{ReplayName: aws.String("rp")}) + + var illegal *ebtypes.IllegalStatusException + if !errors.As(err, &illegal) { + t.Fatalf("CancelReplay on a completed replay = %v, want IllegalStatusException", err) + } + + replays, err := eb.ListReplays(ctx, &awseb.ListReplaysInput{NamePrefix: aws.String("r")}) + if err != nil || len(replays.Replays) != 1 { + t.Fatalf("ListReplays = %+v, %v", replays, err) + } + + archives, err := eb.ListArchives(ctx, &awseb.ListArchivesInput{EventSourceArn: aws.String(sdkBusARN + "b1")}) + if err != nil || len(archives.Archives) != 1 || archives.Archives[0].EventCount != 1 { + t.Fatalf("ListArchives = %+v, %v", archives, err) + } + + if _, err := eb.UpdateArchive(ctx, &awseb.UpdateArchiveInput{ArchiveName: aws.String("arch"), RetentionDays: aws.Int32(0)}); err != nil { + t.Fatalf("UpdateArchive: %v", err) + } + + if _, err := eb.DeleteArchive(ctx, &awseb.DeleteArchiveInput{ArchiveName: aws.String("arch")}); err != nil { + t.Fatalf("DeleteArchive: %v", err) + } + + _, err = eb.DescribeArchive(ctx, &awseb.DescribeArchiveInput{ArchiveName: aws.String("arch")}) + + var notFound *ebtypes.ResourceNotFoundException + if !errors.As(err, ¬Found) { + t.Fatalf("DescribeArchive after delete = %v, want ResourceNotFoundException", err) + } +} + +func TestSDKArchiveErrors(t *testing.T) { + client := newEventBridgeClient(t) + ctx := context.Background() + + _, err := client.CreateArchive(ctx, &awseb.CreateArchiveInput{ + ArchiveName: aws.String("a"), EventSourceArn: aws.String(sdkBusARN + "ghost"), + }) + requireAPIError(t, err, "ResourceNotFoundException", "Event bus ghost does not exist.") + + _, err = client.CreateArchive(ctx, &awseb.CreateArchiveInput{ + ArchiveName: aws.String("a"), EventSourceArn: aws.String(sdkBusARN + "default"), EventPattern: aws.String(`{"source":"x"}`), + }) + + var badPattern *ebtypes.InvalidEventPatternException + if !errors.As(err, &badPattern) { + t.Fatalf("CreateArchive with a bad pattern = %v, want InvalidEventPatternException", err) + } + + _, err = client.UpdateArchive(ctx, &awseb.UpdateArchiveInput{ArchiveName: aws.String("ghost")}) + requireAPIError(t, err, "ValidationException", "At least one of EventPattern, RetentionDays or Description must be provided.") + + _, err = client.ListArchives(ctx, &awseb.ListArchivesInput{NamePrefix: aws.String("a"), State: ebtypes.ArchiveStateEnabled}) + requireAPIError(t, err, "ValidationException", + "At most one filter is allowed for ListArchives. Use either : State, EventSourceArn, or NamePrefix.") + + _, err = client.StartReplay(ctx, &awseb.StartReplayInput{ + ReplayName: aws.String("rp"), EventSourceArn: aws.String("arn:aws:events:us-east-1:123456789012:archive/nope"), + EventStartTime: aws.Time(time.Now().Add(-time.Hour)), EventEndTime: aws.Time(time.Now()), + Destination: &ebtypes.ReplayDestination{Arn: aws.String(sdkBusARN + "default")}, + }) + requireAPIError(t, err, "ValidationException", "Parameter EventSourceArn is not valid. Reason: Archive nope does not exist.") + + _, err = client.DescribeReplay(ctx, &awseb.DescribeReplayInput{ReplayName: aws.String("nope")}) + requireAPIError(t, err, "ResourceNotFoundException", "Replay nope does not exist.") +} diff --git a/server/aws/eventbridge/archives.go b/server/aws/eventbridge/archives.go new file mode 100644 index 000000000..ffcd0debc --- /dev/null +++ b/server/aws/eventbridge/archives.go @@ -0,0 +1,287 @@ +package eventbridge + +import ( + "context" + "math" + "net/http" + "time" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/server/wire" + ebdriver "github.com/stackshy/cloudemu/v2/services/eventbus/driver" +) + +// archiveManager is the AWS-specific EventBridge archive and replay surface, +// asserted against the provider (not part of the portable EventBus driver). +type archiveManager interface { + CreateArchive(ctx context.Context, cfg *ebdriver.ArchiveConfig) (*ebdriver.Archive, error) + DescribeArchive(ctx context.Context, name string) (*ebdriver.Archive, error) + UpdateArchive(ctx context.Context, upd *ebdriver.ArchiveUpdate) (*ebdriver.Archive, error) + DeleteArchive(ctx context.Context, name string) error + ListArchives(ctx context.Context, filter ebdriver.ArchiveFilter) ([]ebdriver.Archive, error) + StartReplay(ctx context.Context, cfg *ebdriver.ReplayConfig) (*ebdriver.Replay, error) + DescribeReplay(ctx context.Context, name string) (*ebdriver.Replay, error) + CancelReplay(ctx context.Context, name string) (*ebdriver.Replay, error) + ListReplays(ctx context.Context, filter ebdriver.ReplayFilter) ([]ebdriver.Replay, error) +} + +type archiveOp func(mgr archiveManager, w http.ResponseWriter, r *http.Request) + +// archiveOps maps each archive and replay operation to its handler. +func archiveOps() map[string]archiveOp { + return map[string]archiveOp{ + "CreateArchive": createArchive, + "DescribeArchive": describeArchive, + "UpdateArchive": updateArchive, + "DeleteArchive": deleteArchive, + "ListArchives": listOp(fetchArchives, archiveListItem, func(a archiveJSON) string { return a.ArchiveName }, archivePage), + "StartReplay": startReplay, + "DescribeReplay": describeReplay, + "CancelReplay": cancelReplay, + "ListReplays": listOp(fetchReplays, replayListItem, func(rp replayJSON) string { return rp.ReplayName }, replayPage), + } +} + +// serveArchiveOp serves op when it is an archive or replay operation, reporting +// whether it did. +func (h *Handler) serveArchiveOp(w http.ResponseWriter, r *http.Request, op string) bool { + fn, ok := archiveOps()[op] + if !ok { + return false + } + + mgr, ok := h.bus.(archiveManager) + if !ok { + writeErr(w, cerrors.New(cerrors.Unimplemented, "archives not supported")) + return true + } + + fn(mgr, w, r) + + return true +} + +// epochToTime converts an AWS JSON timestamp (epoch seconds, possibly +// fractional) to a time.Time; nil gives the zero time. +func epochToTime(sec *float64) time.Time { + if sec == nil { + return time.Time{} + } + + whole, frac := math.Modf(*sec) + + return time.Unix(int64(whole), int64(frac*float64(time.Second))).UTC() +} + +// timeToEpoch converts a time to its AWS JSON wire form, omitting the zero time. +func timeToEpoch(t time.Time) *float64 { + if t.IsZero() { + return nil + } + + sec := float64(t.UnixNano()) / float64(time.Second) + + return &sec +} + +type archiveNameRequest struct { + ArchiveName string `json:"ArchiveName"` +} + +type createArchiveRequest struct { + ArchiveName string `json:"ArchiveName"` + EventSourceArn string `json:"EventSourceArn"` + Description string `json:"Description"` + EventPattern string `json:"EventPattern"` + RetentionDays int `json:"RetentionDays"` + KmsKeyIdentifier string `json:"KmsKeyIdentifier"` +} + +type updateArchiveRequest struct { + ArchiveName string `json:"ArchiveName"` + Description *string `json:"Description"` + EventPattern *string `json:"EventPattern"` + RetentionDays *int `json:"RetentionDays"` + KmsKeyIdentifier *string `json:"KmsKeyIdentifier"` +} + +// archiveStateResponse is the reply of CreateArchive and UpdateArchive. +type archiveStateResponse struct { + ArchiveArn string `json:"ArchiveArn"` + State string `json:"State"` + StateReason string `json:"StateReason,omitempty"` + CreationTime *float64 `json:"CreationTime,omitempty"` +} + +type archiveJSON struct { + ArchiveArn string `json:"ArchiveArn,omitempty"` + ArchiveName string `json:"ArchiveName"` + EventSourceArn string `json:"EventSourceArn"` + Description string `json:"Description,omitempty"` + EventPattern string `json:"EventPattern,omitempty"` + State string `json:"State"` + StateReason string `json:"StateReason,omitempty"` + KmsKeyIdentifier string `json:"KmsKeyIdentifier,omitempty"` + RetentionDays int `json:"RetentionDays"` + SizeBytes int64 `json:"SizeBytes"` + EventCount int64 `json:"EventCount"` + CreationTime *float64 `json:"CreationTime,omitempty"` +} + +type listArchivesResponse struct { + Archives []archiveJSON `json:"Archives"` + NextToken string `json:"NextToken,omitempty"` +} + +func toArchiveState(a *ebdriver.Archive) archiveStateResponse { + return archiveStateResponse{ + ArchiveArn: a.ARN, State: a.State, StateReason: a.StateReason, CreationTime: timeToEpoch(a.CreatedAt), + } +} + +// listFilterRequest is the request shape ListArchives and ListReplays share. +type listFilterRequest struct { + NamePrefix string `json:"NamePrefix"` + EventSourceArn string `json:"EventSourceArn"` + State string `json:"State"` + Limit int `json:"Limit"` + NextToken string `json:"NextToken"` +} + +// listOp builds the handler of a list operation: decode the shared filter +// request, fetch the items, convert them to their wire form, and write one page +// using EventBridge's cursor pagination. +func listOp[T, J any]( + fetch func(ctx context.Context, mgr archiveManager, req *listFilterRequest) ([]T, error), + conv func(*T) J, + key func(J) string, + wrap func(page []J, next string) any, +) archiveOp { + return func(mgr archiveManager, w http.ResponseWriter, r *http.Request) { + var req listFilterRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + items, err := fetch(r.Context(), mgr, &req) + if err != nil { + writeErr(w, err) + return + } + + out := make([]J, 0, len(items)) + for i := range items { + out = append(out, conv(&items[i])) + } + + page, next := paginateByCursor(out, req.NextToken, req.Limit, key) + + wire.WriteJSON(w, wrap(page, next)) + } +} + +func toArchiveJSON(a *ebdriver.Archive, full bool) archiveJSON { + out := archiveJSON{ + ArchiveName: a.Name, + EventSourceArn: a.EventSourceARN, + State: a.State, + StateReason: a.StateReason, + RetentionDays: a.RetentionDays, + SizeBytes: a.SizeBytes, + EventCount: a.EventCount, + CreationTime: timeToEpoch(a.CreatedAt), + } + + if full { + out.ArchiveArn = a.ARN + out.Description = a.Description + out.EventPattern = a.EventPattern + out.KmsKeyIdentifier = a.KMSKeyIdentifier + } + + return out +} + +func createArchive(mgr archiveManager, w http.ResponseWriter, r *http.Request) { + var req createArchiveRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + a, err := mgr.CreateArchive(r.Context(), &ebdriver.ArchiveConfig{ + Name: req.ArchiveName, + EventSourceARN: req.EventSourceArn, + Description: req.Description, + EventPattern: req.EventPattern, + RetentionDays: req.RetentionDays, + KMSKeyIdentifier: req.KmsKeyIdentifier, + }) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, toArchiveState(a)) +} + +func describeArchive(mgr archiveManager, w http.ResponseWriter, r *http.Request) { + var req archiveNameRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + a, err := mgr.DescribeArchive(r.Context(), req.ArchiveName) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, toArchiveJSON(a, true)) +} + +func updateArchive(mgr archiveManager, w http.ResponseWriter, r *http.Request) { + var req updateArchiveRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + a, err := mgr.UpdateArchive(r.Context(), &ebdriver.ArchiveUpdate{ + Name: req.ArchiveName, + Description: req.Description, + EventPattern: req.EventPattern, + RetentionDays: req.RetentionDays, + KMSKeyIdentifier: req.KmsKeyIdentifier, + }) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, toArchiveState(a)) +} + +func deleteArchive(mgr archiveManager, w http.ResponseWriter, r *http.Request) { + var req archiveNameRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + if err := mgr.DeleteArchive(r.Context(), req.ArchiveName); err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, struct{}{}) +} + +func fetchArchives(ctx context.Context, mgr archiveManager, req *listFilterRequest) ([]ebdriver.Archive, error) { + return mgr.ListArchives(ctx, ebdriver.ArchiveFilter{ + NamePrefix: req.NamePrefix, EventSourceARN: req.EventSourceArn, State: req.State, + }) +} + +func archiveListItem(a *ebdriver.Archive) archiveJSON { return toArchiveJSON(a, false) } + +func archivePage(page []archiveJSON, next string) any { + return listArchivesResponse{Archives: page, NextToken: next} +} diff --git a/server/aws/eventbridge/delivery_sdk_test.go b/server/aws/eventbridge/delivery_sdk_test.go index 9a77fd29b..0a05dba69 100644 --- a/server/aws/eventbridge/delivery_sdk_test.go +++ b/server/aws/eventbridge/delivery_sdk_test.go @@ -24,7 +24,10 @@ func newEBAndSQS(t *testing.T) (*awseb.Client, *awssqs.Client) { t.Helper() cloud := cloudemu.NewAWS() - srv := awsserver.New(awsserver.Drivers{EventBridge: cloud.EventBridge, SQS: cloud.SQS}) + srv := awsserver.New(awsserver.Drivers{ + EventBridge: cloud.EventBridge, SQS: cloud.SQS, + AccountID: "123456789012", Region: "us-east-1", + }) ts := httptest.NewServer(srv) t.Cleanup(ts.Close) diff --git a/server/aws/eventbridge/handler.go b/server/aws/eventbridge/handler.go index c68c50e5d..69b895ae4 100644 --- a/server/aws/eventbridge/handler.go +++ b/server/aws/eventbridge/handler.go @@ -10,6 +10,7 @@ package eventbridge import ( + "errors" "net/http" "strings" @@ -87,11 +88,21 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { case "ListTagsForResource": h.listTagsForResource(w, r) default: - wire.WriteJSONError(w, http.StatusBadRequest, - "UnknownOperationException", "unknown EventBridge operation: "+op) + h.serveExtendedOp(w, r, op) } } +// serveExtendedOp serves the operations outside the main switch (archives and +// replays) and answers UnknownOperationException for anything else. +func (h *Handler) serveExtendedOp(w http.ResponseWriter, r *http.Request, op string) { + if h.serveArchiveOp(w, r, op) { + return + } + + wire.WriteJSONError(w, http.StatusBadRequest, + "UnknownOperationException", "unknown EventBridge operation: "+op) +} + // writeErr maps canonical cloudemu errors to EventBridge JSON error responses. // EventBridge returns errors as HTTP 400 with a "__type" body the SDK maps to a // typed exception. @@ -102,6 +113,10 @@ func writeErr(w http.ResponseWriter, err error) { msg := cerrors.Message(err) switch { + case errors.Is(err, ebdriver.ErrInvalidEventPattern): + wire.WriteJSONError(w, http.StatusBadRequest, "InvalidEventPatternException", msg) + case errors.Is(err, ebdriver.ErrIllegalStatus): + wire.WriteJSONError(w, http.StatusBadRequest, "IllegalStatusException", msg) case cerrors.IsNotFound(err): wire.WriteJSONError(w, http.StatusBadRequest, "ResourceNotFoundException", msg) case cerrors.IsAlreadyExists(err): diff --git a/server/aws/eventbridge/replays.go b/server/aws/eventbridge/replays.go new file mode 100644 index 000000000..36298990e --- /dev/null +++ b/server/aws/eventbridge/replays.go @@ -0,0 +1,143 @@ +package eventbridge + +import ( + "context" + "net/http" + + "github.com/stackshy/cloudemu/v2/server/wire" + ebdriver "github.com/stackshy/cloudemu/v2/services/eventbus/driver" +) + +type replayNameRequest struct { + ReplayName string `json:"ReplayName"` +} + +type replayDestinationJSON struct { + Arn string `json:"Arn"` + FilterArns []string `json:"FilterArns,omitempty"` +} + +type startReplayRequest struct { + ReplayName string `json:"ReplayName"` + Description string `json:"Description"` + EventSourceArn string `json:"EventSourceArn"` + EventStartTime *float64 `json:"EventStartTime"` + EventEndTime *float64 `json:"EventEndTime"` + Destination replayDestinationJSON `json:"Destination"` +} + +// replayStateResponse is the reply of StartReplay and CancelReplay. +type replayStateResponse struct { + ReplayArn string `json:"ReplayArn"` + State string `json:"State"` + StateReason string `json:"StateReason,omitempty"` + ReplayStartTime *float64 `json:"ReplayStartTime,omitempty"` +} + +type replayJSON struct { + ReplayName string `json:"ReplayName"` + ReplayArn string `json:"ReplayArn,omitempty"` + Description string `json:"Description,omitempty"` + State string `json:"State"` + StateReason string `json:"StateReason,omitempty"` + EventSourceArn string `json:"EventSourceArn"` + Destination *replayDestinationJSON `json:"Destination,omitempty"` + EventStartTime *float64 `json:"EventStartTime,omitempty"` + EventEndTime *float64 `json:"EventEndTime,omitempty"` + EventLastReplayedTime *float64 `json:"EventLastReplayedTime,omitempty"` + ReplayStartTime *float64 `json:"ReplayStartTime,omitempty"` + ReplayEndTime *float64 `json:"ReplayEndTime,omitempty"` +} + +type listReplaysResponse struct { + Replays []replayJSON `json:"Replays"` + NextToken string `json:"NextToken,omitempty"` +} + +func toReplayJSON(rp *ebdriver.Replay, full bool) replayJSON { + out := replayJSON{ + ReplayName: rp.Name, + State: rp.State, + StateReason: rp.StateReason, + EventSourceArn: rp.EventSourceARN, + EventStartTime: timeToEpoch(rp.EventStartTime), + EventEndTime: timeToEpoch(rp.EventEndTime), + EventLastReplayedTime: timeToEpoch(rp.EventLastReplayedTime), + ReplayStartTime: timeToEpoch(rp.ReplayStartTime), + ReplayEndTime: timeToEpoch(rp.ReplayEndTime), + } + + if full { + out.ReplayArn = rp.ARN + out.Description = rp.Description + out.Destination = &replayDestinationJSON{Arn: rp.Destination.ARN, FilterArns: rp.Destination.FilterARNs} + } + + return out +} + +func startReplay(mgr archiveManager, w http.ResponseWriter, r *http.Request) { + var req startReplayRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + rp, err := mgr.StartReplay(r.Context(), &ebdriver.ReplayConfig{ + Name: req.ReplayName, + Description: req.Description, + EventSourceARN: req.EventSourceArn, + EventStartTime: epochToTime(req.EventStartTime), + EventEndTime: epochToTime(req.EventEndTime), + Destination: ebdriver.ReplayDestination{ARN: req.Destination.Arn, FilterARNs: req.Destination.FilterArns}, + }) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, replayStateResponse{ + ReplayArn: rp.ARN, State: rp.State, StateReason: rp.StateReason, ReplayStartTime: timeToEpoch(rp.ReplayStartTime), + }) +} + +func describeReplay(mgr archiveManager, w http.ResponseWriter, r *http.Request) { + var req replayNameRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + rp, err := mgr.DescribeReplay(r.Context(), req.ReplayName) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, toReplayJSON(rp, true)) +} + +func cancelReplay(mgr archiveManager, w http.ResponseWriter, r *http.Request) { + var req replayNameRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + rp, err := mgr.CancelReplay(r.Context(), req.ReplayName) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, replayStateResponse{ReplayArn: rp.ARN, State: rp.State, StateReason: rp.StateReason}) +} + +func fetchReplays(ctx context.Context, mgr archiveManager, req *listFilterRequest) ([]ebdriver.Replay, error) { + return mgr.ListReplays(ctx, ebdriver.ReplayFilter{ + NamePrefix: req.NamePrefix, EventSourceARN: req.EventSourceArn, State: req.State, + }) +} + +func replayListItem(rp *ebdriver.Replay) replayJSON { return toReplayJSON(rp, false) } + +func replayPage(page []replayJSON, next string) any { + return listReplaysResponse{Replays: page, NextToken: next} +} diff --git a/services/eventbus/driver/archive.go b/services/eventbus/driver/archive.go new file mode 100644 index 000000000..7663c739e --- /dev/null +++ b/services/eventbus/driver/archive.go @@ -0,0 +1,105 @@ +package driver + +import ( + "errors" + "time" +) + +// ErrInvalidEventPattern marks an event pattern that fails validation. AWS +// EventBridge answers it as InvalidEventPatternException rather than the generic +// ValidationException. +var ErrInvalidEventPattern = errors.New("event pattern is not valid") + +// ErrIllegalStatus marks an operation on a resource whose current state does +// not allow it (e.g. stopping a finished replay). AWS EventBridge answers it as +// IllegalStatusException. +var ErrIllegalStatus = errors.New("resource is not in a valid state for this operation") + +// Archive describes an EventBridge archive: a store of the events put on one +// event bus that match its pattern, kept for later replay. Archives are an +// AWS-only concept, consumed through an optional interface, not the portable +// driver. +type Archive struct { + Name string + ARN string + EventSourceARN string + Description string + EventPattern string + State string + StateReason string + KMSKeyIdentifier string + // RetentionDays is how long archived events are kept; 0 keeps them forever. + RetentionDays int + SizeBytes int64 + EventCount int64 + CreatedAt time.Time +} + +// ArchiveConfig carries the parameters of an EventBridge CreateArchive call. +type ArchiveConfig struct { + Name string + EventSourceARN string + Description string + EventPattern string + KMSKeyIdentifier string + RetentionDays int +} + +// ArchiveUpdate carries the parameters of an EventBridge UpdateArchive call. A +// nil field was omitted from the request and leaves the archive unchanged. +type ArchiveUpdate struct { + Name string + Description *string + EventPattern *string + KMSKeyIdentifier *string + RetentionDays *int +} + +// ArchiveFilter narrows ListArchives; EventBridge accepts at most one of its +// fields per call. +type ArchiveFilter struct { + NamePrefix string + EventSourceARN string + State string +} + +// ReplayDestination is where a replay sends its events: the archive's own event +// bus, optionally limited to the rules in FilterARNs. +type ReplayDestination struct { + ARN string + FilterARNs []string +} + +// ReplayConfig carries the parameters of an EventBridge StartReplay call. +type ReplayConfig struct { + Name string + Description string + EventSourceARN string // the archive ARN + EventStartTime time.Time + EventEndTime time.Time + Destination ReplayDestination +} + +// Replay describes an EventBridge replay and its progress. +type Replay struct { + Name string + ARN string + Description string + State string + StateReason string + EventSourceARN string + Destination ReplayDestination + EventStartTime time.Time + EventEndTime time.Time + EventLastReplayedTime time.Time + ReplayStartTime time.Time + ReplayEndTime time.Time +} + +// ReplayFilter narrows ListReplays; EventBridge accepts at most one of its +// fields per call. +type ReplayFilter struct { + NamePrefix string + EventSourceARN string + State string +} From df9833f6a68817ec02403c6fddb34e47d6e78edd Mon Sep 17 00:00:00 2001 From: Nitin Kumar Patel Date: Thu, 22 Oct 2026 07:09:18 +0530 Subject: [PATCH 08/31] fix(aws-cloudwatchlogs): Logs Insights queries and query engine core (#1661) * feat(aws-cloudwatchlogs): Logs Insights queries Add StartQuery, GetQueryResults, StopQuery, DescribeQueries, GetLogRecord and GetLogGroupFields with a query engine for fields, filter, sort, limit and display over system and JSON fields. Queries run on their first poll, StopQuery cancels a Running query, and malformed queries return MalformedQueryException with the error location. Part of #1476. * fix(aws-cloudwatchlogs): guard substr float args and make query admission atomic --- docs/coverage/aws/cloudwatchlogs.md | 6 + docs/coverage/coverage.json | 20 + .../aws/cloudwatchlogs/cloudwatchlogs.go | 5 + .../aws/cloudwatchlogs/insights/errors.go | 14 + providers/aws/cloudwatchlogs/insights/expr.go | 231 ++++++++ .../aws/cloudwatchlogs/insights/funcs.go | 210 +++++++ .../aws/cloudwatchlogs/insights/lexer.go | 248 ++++++++ .../aws/cloudwatchlogs/insights/parse_expr.go | 355 ++++++++++++ .../aws/cloudwatchlogs/insights/query.go | 414 ++++++++++++++ .../aws/cloudwatchlogs/insights/query_test.go | 173 ++++++ .../aws/cloudwatchlogs/insights/record.go | 120 ++++ .../cloudwatchlogs/insights/substr_test.go | 71 +++ .../aws/cloudwatchlogs/insights_queries.go | 532 ++++++++++++++++++ .../cloudwatchlogs/insights_queries_test.go | 281 +++++++++ providers/aws/cloudwatchlogs/snapshot.go | 14 + server/aws/cloudwatchlogs/handler.go | 22 + server/aws/cloudwatchlogs/insights.go | 319 +++++++++++ .../aws/cloudwatchlogs/insights_sdk_test.go | 137 +++++ services/logging/driver/aws.go | 92 ++- 19 files changed, 3263 insertions(+), 1 deletion(-) create mode 100644 providers/aws/cloudwatchlogs/insights/errors.go create mode 100644 providers/aws/cloudwatchlogs/insights/expr.go create mode 100644 providers/aws/cloudwatchlogs/insights/funcs.go create mode 100644 providers/aws/cloudwatchlogs/insights/lexer.go create mode 100644 providers/aws/cloudwatchlogs/insights/parse_expr.go create mode 100644 providers/aws/cloudwatchlogs/insights/query.go create mode 100644 providers/aws/cloudwatchlogs/insights/query_test.go create mode 100644 providers/aws/cloudwatchlogs/insights/record.go create mode 100644 providers/aws/cloudwatchlogs/insights/substr_test.go create mode 100644 providers/aws/cloudwatchlogs/insights_queries.go create mode 100644 providers/aws/cloudwatchlogs/insights_queries_test.go create mode 100644 server/aws/cloudwatchlogs/insights.go create mode 100644 server/aws/cloudwatchlogs/insights_sdk_test.go diff --git a/docs/coverage/aws/cloudwatchlogs.md b/docs/coverage/aws/cloudwatchlogs.md index dcdfcb403..0516d82ca 100644 --- a/docs/coverage/aws/cloudwatchlogs.md +++ b/docs/coverage/aws/cloudwatchlogs.md @@ -39,10 +39,16 @@ AWSLogging is an optional interface implemented by the AWS CloudWatch Logs | `CreateAWSLogGroup` | | | `DeleteRetentionPolicy` | | | `DescribeAWSMetricFilters` | DescribeAWSMetricFilters lists metric filters, sorted by filter name. | +| `DescribeQueries` | | | `DisassociateKmsKey` | | +| `GetLogGroupFields` | GetLogGroupFields samples the 8 minutes either side of at, or the last | +| `GetLogRecord` | | +| `GetQueryResults` | | | `ListAWSLogGroups` | | | `PutLogEventsWithResult` | | | `PutRetentionPolicy` | | +| `StartQuery` | Logs Insights. A query is Running until its first GetQueryResults, | +| `StopQuery` | | | `TestMetricFilter` | TestMetricFilter runs a filter pattern over sample log messages. | ## Not in scope diff --git a/docs/coverage/coverage.json b/docs/coverage/coverage.json index b8016bf00..c4b9f15f7 100644 --- a/docs/coverage/coverage.json +++ b/docs/coverage/coverage.json @@ -11576,9 +11576,22 @@ "name": "DescribeAWSMetricFilters", "doc": "DescribeAWSMetricFilters lists metric filters, sorted by filter name." }, + { + "name": "DescribeQueries" + }, { "name": "DisassociateKmsKey" }, + { + "name": "GetLogGroupFields", + "doc": "GetLogGroupFields samples the 8 minutes either side of at, or the last" + }, + { + "name": "GetLogRecord" + }, + { + "name": "GetQueryResults" + }, { "name": "ListAWSLogGroups" }, @@ -11588,6 +11601,13 @@ { "name": "PutRetentionPolicy" }, + { + "name": "StartQuery", + "doc": "Logs Insights. A query is Running until its first GetQueryResults," + }, + { + "name": "StopQuery" + }, { "name": "TestMetricFilter", "doc": "TestMetricFilter runs a filter pattern over sample log messages." diff --git a/providers/aws/cloudwatchlogs/cloudwatchlogs.go b/providers/aws/cloudwatchlogs/cloudwatchlogs.go index 5a4be955a..8027ef3e7 100644 --- a/providers/aws/cloudwatchlogs/cloudwatchlogs.go +++ b/providers/aws/cloudwatchlogs/cloudwatchlogs.go @@ -59,6 +59,7 @@ type Mock struct { monitoring mondriver.Monitoring lambda LambdaInvoker keys KeyResolver + queries *memstore.Store[queryRecord] // queryResultKmsKey is the account-wide key associated with Logs Insights // query results (AssociateKmsKey on the query-result resource). @@ -67,6 +68,9 @@ type Mock struct { // keyed by filterPeriodKey. periods map[string]filterPeriod mu sync.RWMutex + // admitMu makes StartQuery's concurrent-query count and its insert one + // step. Settling only moves queries out of Running, so it needs no lock. + admitMu sync.Mutex } // SetMonitoring sets the monitoring backend for auto-metric generation. @@ -95,6 +99,7 @@ func (m *Mock) emitMetric(metricName string, value float64, unit string, dims ma func New(opts *config.Options) *Mock { return &Mock{ groups: memstore.New[*logGroup](), + queries: memstore.New[queryRecord](), opts: opts, periods: map[string]filterPeriod{}, } diff --git a/providers/aws/cloudwatchlogs/insights/errors.go b/providers/aws/cloudwatchlogs/insights/errors.go new file mode 100644 index 000000000..6670f71ce --- /dev/null +++ b/providers/aws/cloudwatchlogs/insights/errors.go @@ -0,0 +1,14 @@ +package insights + +// CompileError is a query that does not parse, with the character offsets of +// the offending text, the shape of the API's QueryCompileError. +type CompileError struct { + Start, End int + Message string +} + +func (e *CompileError) Error() string { return e.Message } + +func compileErr(start, end int, msg string) *CompileError { + return &CompileError{Start: start, End: end, Message: msg} +} diff --git a/providers/aws/cloudwatchlogs/insights/expr.go b/providers/aws/cloudwatchlogs/insights/expr.go new file mode 100644 index 000000000..0131d3f43 --- /dev/null +++ b/providers/aws/cloudwatchlogs/insights/expr.go @@ -0,0 +1,231 @@ +package insights + +import ( + "regexp" + "strconv" + "strings" +) + +// Record is one log event's fields: strings, float64 numbers, bools, and +// nested JSON already flattened to dotted names. A missing field is absent. +type Record map[string]any + +type expr interface { + eval(rec Record) any +} + +type fieldRef struct{ name string } + +func (f fieldRef) eval(rec Record) any { return rec[f.name] } + +type literal struct{ v any } + +func (l literal) eval(Record) any { return l.v } + +type regexLit struct{ re *regexp.Regexp } + +func (r regexLit) eval(Record) any { return r.re } + +type notExpr struct{ x expr } + +func (n notExpr) eval(rec Record) any { return !truthy(n.x.eval(rec)) } + +type negExpr struct{ x expr } + +func (n negExpr) eval(rec Record) any { + f, ok := toNum(n.x.eval(rec)) + if !ok { + return nil + } + + return -f +} + +type logicExpr struct { + and bool + l, r expr +} + +func (e logicExpr) eval(rec Record) any { + if e.and { + return truthy(e.l.eval(rec)) && truthy(e.r.eval(rec)) + } + + return truthy(e.l.eval(rec)) || truthy(e.r.eval(rec)) +} + +// cmpExpr compares two values. Both sides must be present; numbers (and +// numeric strings) compare numerically, anything else as strings. +type cmpExpr struct { + op string + l, r expr +} + +func (e cmpExpr) eval(rec Record) any { + a, b := e.l.eval(rec), e.r.eval(rec) + if a == nil || b == nil { + return false + } + + if e.op == opMatch || e.op == opNotMatch { + return regexMatch(a, b) == (e.op == opMatch) + } + + c := compare(a, b) + + switch e.op { + case opEq: + return c == 0 + case opNe: + return c != 0 + case opLt: + return c < 0 + case opLe: + return c <= 0 + case opGt: + return c > 0 + default: + return c >= 0 + } +} + +type arithExpr struct { + op string + l, r expr +} + +func (e arithExpr) eval(rec Record) any { + a, aok := toNum(e.l.eval(rec)) + b, bok := toNum(e.r.eval(rec)) + + if !aok || !bok { + return nil + } + + return arith(e.op, a, b) +} + +// likeExpr is `x like "text"` (a case-sensitive substring match) or +// `x like /regex/`, optionally negated with `not like`. +type likeExpr struct { + x, pattern expr + negate bool +} + +func (e likeExpr) eval(rec Record) any { + v := e.x.eval(rec) + if v == nil { + return false + } + + var matched bool + if re, ok := e.pattern.eval(rec).(*regexp.Regexp); ok { + matched = re.MatchString(toStr(v)) + } else { + matched = strings.Contains(toStr(v), toStr(e.pattern.eval(rec))) + } + + return matched != e.negate +} + +type inExpr struct { + x expr + list []expr + negate bool +} + +func (e inExpr) eval(rec Record) any { + v := e.x.eval(rec) + if v == nil { + return false + } + + for _, item := range e.list { + if iv := item.eval(rec); iv != nil && compare(v, iv) == 0 { + return !e.negate + } + } + + return e.negate +} + +func regexMatch(v, pattern any) bool { + if re, ok := pattern.(*regexp.Regexp); ok { + return re.MatchString(toStr(v)) + } + + re, err := regexp.Compile(toStr(pattern)) + + return err == nil && re.MatchString(toStr(v)) +} + +func truthy(v any) bool { + switch x := v.(type) { + case bool: + return x + case float64: + return x != 0 + case string: + return x != "" + } + + return false +} + +func toNum(v any) (float64, bool) { + switch x := v.(type) { + case float64: + return x, true + case string: + f, err := strconv.ParseFloat(strings.TrimSpace(x), 64) + + return f, err == nil + case bool: + if x { + return 1, true + } + + return 0, true + } + + return 0, false +} + +// toStr renders a value the way query results show it. +func toStr(v any) string { + switch x := v.(type) { + case string: + return x + case float64: + return strconv.FormatFloat(x, 'f', -1, 64) + case bool: + if x { + return "1" + } + + return "0" + case *regexp.Regexp: + return x.String() + } + + return "" +} + +// compare orders two present values: numerically when both are numbers, +// otherwise as strings. +func compare(a, b any) int { + if x, ok := toNum(a); ok { + if y, ok2 := toNum(b); ok2 { + switch { + case x < y: + return -1 + case x > y: + return 1 + default: + return 0 + } + } + } + + return strings.Compare(toStr(a), toStr(b)) +} diff --git a/providers/aws/cloudwatchlogs/insights/funcs.go b/providers/aws/cloudwatchlogs/insights/funcs.go new file mode 100644 index 000000000..c8240b6e1 --- /dev/null +++ b/providers/aws/cloudwatchlogs/insights/funcs.go @@ -0,0 +1,210 @@ +package insights + +import ( + "math" + "strings" +) + +func arith(op string, a, b float64) any { + switch op { + case "+": + return a + b + case "-": + return a - b + case "*": + return a * b + case "/": + if b == 0 { + return nil + } + + return a / b + case "%": + if b == 0 { + return nil + } + + return math.Mod(a, b) + default: + return math.Pow(a, b) + } +} + +// fn evaluates a function call over its argument values. +type fn func(args []any) any + +type fnSpec struct { + minArgs, maxArgs int // maxArgs < 0 means variadic + call fn +} + +// functions is the supported subset of the general, string and numeric +// functions. +// +//nolint:gochecknoglobals // fixed function table. +var functions = map[string]fnSpec{ + "ispresent": {1, 1, func(a []any) any { return a[0] != nil }}, + "isempty": {1, 1, func(a []any) any { return a[0] == nil || toStr(a[0]) == "" }}, + "isblank": {1, 1, func(a []any) any { return a[0] == nil || strings.TrimSpace(toStr(a[0])) == "" }}, + "coalesce": {1, -1, coalesce}, + "concat": {1, -1, concat}, + "strlen": {1, 1, strArg(func(s string) any { return float64(len([]rune(s))) })}, + "toupper": {1, 1, strArg(func(s string) any { return strings.ToUpper(s) })}, + "tolower": {1, 1, strArg(func(s string) any { return strings.ToLower(s) })}, + "trim": {1, 1, strArg(func(s string) any { return strings.TrimSpace(s) })}, + "ltrim": {1, 1, strArg(func(s string) any { return strings.TrimLeft(s, " \t") })}, + "rtrim": {1, 1, strArg(func(s string) any { return strings.TrimRight(s, " \t") })}, + "strcontains": {2, 2, func(a []any) any { + return a[0] != nil && strings.Contains(toStr(a[0]), toStr(a[1])) + }}, + "replace": {3, 3, func(a []any) any { + if a[0] == nil { + return nil + } + + return strings.ReplaceAll(toStr(a[0]), toStr(a[1]), toStr(a[2])) + }}, + "substr": {2, 3, substr}, + "abs": {1, 1, numArg(math.Abs)}, + "ceil": {1, 1, numArg(math.Ceil)}, + "floor": {1, 1, numArg(math.Floor)}, + "round": {1, 1, numArg(math.Round)}, + "sqrt": {1, 1, numArg(math.Sqrt)}, + "log": {1, 1, numArg(math.Log)}, + "tonumber": {1, 1, numArg(func(f float64) float64 { return f })}, + "greatest": {1, -1, extreme(1)}, + "least": {1, -1, extreme(-1)}, +} + +func strArg(f func(string) any) fn { + return func(a []any) any { + if a[0] == nil { + return nil + } + + return f(toStr(a[0])) + } +} + +func numArg(f func(float64) float64) fn { + return func(a []any) any { + x, ok := toNum(a[0]) + if !ok { + return nil + } + + return f(x) + } +} + +func coalesce(a []any) any { + for _, v := range a { + if v != nil { + return v + } + } + + return nil +} + +func concat(a []any) any { + var b strings.Builder + + for _, v := range a { + b.WriteString(toStr(v)) + } + + return b.String() +} + +func substr(a []any) any { + if a[0] == nil { + return nil + } + + r := []rune(toStr(a[0])) + + f, ok := toNum(a[1]) + if !ok { + return nil + } + + start, ok := clampIndex(f, len(r)) + if !ok || f < 0 || f > float64(len(r)) { + return nil + } + + end := len(r) + + const lengthArg = 2 + + if len(a) > lengthArg { + n, nok := toNum(a[lengthArg]) + if !nok { + return string(r[start:]) + } + + count, cok := clampIndex(n, len(r)-start) + if !cok { + return nil + } + + end = start + count + } + + return string(r[start:end]) +} + +// clampIndex converts f to an int in [0, maxIdx], truncating toward zero. +// It reports false for NaN and ยฑInf, whose int conversion is +// implementation-defined (int(NaN) is MinInt64 on amd64). +func clampIndex(f float64, maxIdx int) (int, bool) { + if math.IsNaN(f) || math.IsInf(f, 0) { + return 0, false + } + + switch { + case f <= 0: + return 0, true + case f >= float64(maxIdx): + return maxIdx, true + default: + return int(f), true + } +} + +// extreme returns greatest (sign 1) or least (sign -1) of numeric arguments. +func extreme(sign float64) fn { + return func(a []any) any { + var ( + best float64 + found bool + ) + + for _, v := range a { + if x, ok := toNum(v); ok && (!found || sign*(x-best) > 0) { + best, found = x, true + } + } + + if !found { + return nil + } + + return best + } +} + +type callExpr struct { + spec fnSpec + args []expr +} + +func (c callExpr) eval(rec Record) any { + vals := make([]any, len(c.args)) + for i, a := range c.args { + vals[i] = a.eval(rec) + } + + return c.spec.call(vals) +} diff --git a/providers/aws/cloudwatchlogs/insights/lexer.go b/providers/aws/cloudwatchlogs/insights/lexer.go new file mode 100644 index 000000000..ef7b4cbc9 --- /dev/null +++ b/providers/aws/cloudwatchlogs/insights/lexer.go @@ -0,0 +1,248 @@ +// Package insights implements the core of the CloudWatch Logs Insights query +// language: the fields, filter (where), sort, limit and display commands, the +// expression grammar they share, and the system and auto-discovered JSON +// fields of a log event. See +// https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_QuerySyntax.html. +package insights + +import ( + "strings" + "unicode" +) + +type tokKind int + +const ( + tIdent tokKind = iota + tNumber + tString + tRegex + tOp + tComma + tLParen + tRParen + tLBracket + tRBracket + tPipe +) + +type token struct { + kind tokKind + text string + start, end int +} + +// Keywords and operators of the query language. +const ( + kwAnd = "and" + kwOr = "or" + kwNot = "not" + kwLike = "like" + kwIn = "in" + kwAs = "as" + cmdFilter = "filter" + cmdWhere = "where" + + opEq = "=" + opNe = "!=" + opLt = "<" + opLe = "<=" + opGt = ">" + opGe = ">=" + opMatch = "=~" + opNotMatch = "!~" +) + +// keywords that may precede a /regex/ literal; after any other identifier a +// slash is division. +// +//nolint:gochecknoglobals // fixed keyword set. +var regexKeywords = map[string]bool{kwLike: true, kwAnd: true, kwOr: true, kwNot: true, cmdFilter: true, cmdWhere: true} + +// twoCharOps are the operators spelled with two characters. +// +//nolint:gochecknoglobals // fixed operator set. +var twoCharOps = map[string]bool{opNe: true, opLe: true, opGe: true, opMatch: true, opNotMatch: true} + +const singleCharOps = "=<>+-*/%^" + +// twoChars is the width of a two-character operator and of the pair of +// delimiters around a quoted name. +const twoChars = 2 + +// lex splits a query into tokens. # starts a comment that runs to the end of +// the line. +func lex(q string) ([]token, error) { + var toks []token + + for i := 0; i < len(q); { + switch q[i] { + case ' ', '\t', '\n', '\r': + i++ + continue + case '#': + for i < len(q) && q[i] != '\n' { + i++ + } + + continue + } + + t, next, err := lexToken(q, i, toks) + if err != nil { + return nil, err + } + + toks = append(toks, t) + i = next + } + + return toks, nil +} + +func lexToken(q string, i int, prev []token) (token, int, error) { + c := q[i] + + if t, ok := punct(c, i); ok { + return t, i + 1, nil + } + + switch { + case c == '"' || c == '\'': + return lexString(q, i) + case c == '`': + return lexBacktick(q, i) + case c == '/' && regexAllowed(prev): + return lexRegex(q, i) + case c >= '0' && c <= '9': + return lexNumber(q, i), endOfNumber(q, i), nil + case isIdentStart(c): + return lexIdent(q, i) + } + + return lexOperator(q, i) +} + +func lexIdent(q string, i int) (token, int, error) { + end := i + for end < len(q) && isIdentChar(q[end]) { + end++ + } + + return token{kind: tIdent, text: q[i:end], start: i, end: end}, end, nil +} + +func lexOperator(q string, i int) (token, int, error) { + if i+1 < len(q) && twoCharOps[q[i:i+twoChars]] { + return token{kind: tOp, text: q[i : i+twoChars], start: i, end: i + twoChars}, i + twoChars, nil + } + + if strings.IndexByte(singleCharOps, q[i]) >= 0 { + return token{kind: tOp, text: q[i : i+1], start: i, end: i + 1}, i + 1, nil + } + + return token{}, 0, compileErr(i, i+1, "unexpected character '"+string(q[i])+"'") +} + +func punct(c byte, i int) (token, bool) { + kinds := map[byte]tokKind{',': tComma, '(': tLParen, ')': tRParen, '[': tLBracket, ']': tRBracket, '|': tPipe} + + k, ok := kinds[c] + + return token{kind: k, text: string(c), start: i, end: i + 1}, ok +} + +// regexAllowed reports whether a slash here starts a /regex/ literal rather +// than a division. +func regexAllowed(prev []token) bool { + if len(prev) == 0 { + return true + } + + p := prev[len(prev)-1] + + switch p.kind { + case tIdent: + return regexKeywords[strings.ToLower(p.text)] + case tOp, tComma, tLParen, tLBracket, tPipe: + return true + case tNumber, tString, tRegex, tRParen, tRBracket: + return false + } + + return false +} + +func lexString(q string, i int) (token, int, error) { + quote := q[i] + + var b strings.Builder + + for j := i + 1; j < len(q); j++ { + switch q[j] { + case '\\': + if j+1 < len(q) { + j++ + b.WriteByte(q[j]) + } + case quote: + return token{kind: tString, text: b.String(), start: i, end: j + 1}, j + 1, nil + default: + b.WriteByte(q[j]) + } + } + + return token{}, 0, compileErr(i, len(q), "unterminated string") +} + +func lexBacktick(q string, i int) (token, int, error) { + end := strings.IndexByte(q[i+1:], '`') + if end < 0 { + return token{}, 0, compileErr(i, len(q), "unterminated backtick field name") + } + + return token{kind: tIdent, text: q[i+1 : i+1+end], start: i, end: i + end + twoChars}, i + end + twoChars, nil +} + +func lexRegex(q string, i int) (token, int, error) { + for j := i + 1; j < len(q); j++ { + if q[j] == '\\' { + j++ + continue + } + + if q[j] == '/' { + return token{kind: tRegex, text: q[i+1 : j], start: i, end: j + 1}, j + 1, nil + } + } + + return token{}, 0, compileErr(i, len(q), "unterminated regular expression") +} + +func lexNumber(q string, i int) token { + end := endOfNumber(q, i) + + return token{kind: tNumber, text: q[i:end], start: i, end: end} +} + +func endOfNumber(q string, i int) int { + end := i + for end < len(q) && (q[end] >= '0' && q[end] <= '9' || q[end] == '.') { + end++ + } + + // A duration such as 5m or 30s lexes as one token. + for end < len(q) && unicode.IsLetter(rune(q[end])) { + end++ + } + + return end +} + +func isIdentStart(c byte) bool { + return c == '@' || c == '_' || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') +} + +func isIdentChar(c byte) bool { + return isIdentStart(c) || c == '.' || (c >= '0' && c <= '9') +} diff --git a/providers/aws/cloudwatchlogs/insights/parse_expr.go b/providers/aws/cloudwatchlogs/insights/parse_expr.go new file mode 100644 index 000000000..ed8e76ceb --- /dev/null +++ b/providers/aws/cloudwatchlogs/insights/parse_expr.go @@ -0,0 +1,355 @@ +package insights + +import ( + "regexp" + "strconv" + "strings" +) + +// exprParser parses one command's expressions from its tokens. +type exprParser struct { + toks []token + pos int + // end is the query offset just past the command, for errors at its end. + end int +} + +func (p *exprParser) peek() *token { + if p.pos >= len(p.toks) { + return nil + } + + return &p.toks[p.pos] +} + +func (p *exprParser) atKeyword(kw string) bool { + t := p.peek() + + return t != nil && t.kind == tIdent && strings.EqualFold(t.text, kw) +} + +func (p *exprParser) errHere(msg string) error { + if t := p.peek(); t != nil { + return compileErr(t.start, t.end, msg) + } + + return compileErr(p.end, p.end, msg) +} + +func (p *exprParser) parseOr() (expr, error) { + left, err := p.parseAnd() + if err != nil { + return nil, err + } + + for p.atKeyword(kwOr) { + p.pos++ + + right, rerr := p.parseAnd() + if rerr != nil { + return nil, rerr + } + + left = logicExpr{l: left, r: right} + } + + return left, nil +} + +func (p *exprParser) parseAnd() (expr, error) { + left, err := p.parseNot() + if err != nil { + return nil, err + } + + for p.atKeyword(kwAnd) { + p.pos++ + + right, rerr := p.parseNot() + if rerr != nil { + return nil, rerr + } + + left = logicExpr{and: true, l: left, r: right} + } + + return left, nil +} + +func (p *exprParser) parseNot() (expr, error) { + if p.atKeyword(kwNot) { + p.pos++ + + x, err := p.parseNot() + if err != nil { + return nil, err + } + + return notExpr{x: x}, nil + } + + return p.parseCmp() +} + +//nolint:gochecknoglobals // fixed operator set. +var cmpOps = map[string]bool{ + opEq: true, opNe: true, opLt: true, opLe: true, opGt: true, opGe: true, opMatch: true, opNotMatch: true, +} + +// parseCmp parses a comparison, `like`, `in`, or their `not` forms. +func (p *exprParser) parseCmp() (expr, error) { + left, err := p.parseAdd() + if err != nil { + return nil, err + } + + negate := p.negatedMatch() + + switch { + case p.atKeyword(kwLike): + p.pos++ + + pat, perr := p.parsePrimary() + if perr != nil { + return nil, perr + } + + return likeExpr{x: left, pattern: pat, negate: negate}, nil + case p.atKeyword(kwIn): + p.pos++ + + return p.parseInList(left, negate) + } + + t := p.peek() + if t == nil || t.kind != tOp || !cmpOps[t.text] { + return left, nil + } + + p.pos++ + + right, err := p.parseAdd() + if err != nil { + return nil, err + } + + return cmpExpr{op: t.text, l: left, r: right}, nil +} + +// negatedMatch consumes the `not` of `not like` / `not in`. +func (p *exprParser) negatedMatch() bool { + if !p.atKeyword(kwNot) || p.pos+1 >= len(p.toks) { + return false + } + + next := strings.ToLower(p.toks[p.pos+1].text) + if next != kwLike && next != kwIn { + return false + } + + p.pos++ + + return true +} + +func (p *exprParser) parseInList(left expr, negate bool) (expr, error) { + if t := p.peek(); t == nil || t.kind != tLBracket { + return nil, p.errHere("expected '[' after in") + } + + p.pos++ + + var list []expr + + for { + item, err := p.parseAdd() + if err != nil { + return nil, err + } + + list = append(list, item) + + t := p.peek() + if t != nil && t.kind == tComma { + p.pos++ + continue + } + + if t == nil || t.kind != tRBracket { + return nil, p.errHere("expected ',' or ']' in list") + } + + p.pos++ + + return inExpr{x: left, list: list, negate: negate}, nil + } +} + +func (p *exprParser) parseAdd() (expr, error) { + return p.parseBinary(p.parseMul, "+", "-") +} + +func (p *exprParser) parseMul() (expr, error) { + return p.parseBinary(p.parseUnary, "*", "/", "%", "^") +} + +func (p *exprParser) parseBinary(next func() (expr, error), ops ...string) (expr, error) { + left, err := next() + if err != nil { + return nil, err + } + + for { + t := p.peek() + if t == nil || t.kind != tOp || !contains(ops, t.text) { + return left, nil + } + + p.pos++ + + right, rerr := next() + if rerr != nil { + return nil, rerr + } + + left = arithExpr{op: t.text, l: left, r: right} + } +} + +func contains(list []string, s string) bool { + for _, x := range list { + if x == s { + return true + } + } + + return false +} + +func (p *exprParser) parseUnary() (expr, error) { + if t := p.peek(); t != nil && t.kind == tOp && t.text == "-" { + p.pos++ + + x, err := p.parseUnary() + if err != nil { + return nil, err + } + + return negExpr{x: x}, nil + } + + return p.parsePrimary() +} + +//nolint:gochecknoglobals // fixed reserved-word set. +var reserved = map[string]bool{kwAnd: true, kwOr: true, kwNot: true, kwLike: true, kwIn: true, kwAs: true} + +func (p *exprParser) parsePrimary() (expr, error) { + t := p.peek() + if t == nil { + return nil, p.errHere("expression expected") + } + + p.pos++ + + switch t.kind { + case tNumber: + f, err := strconv.ParseFloat(t.text, 64) + if err != nil { + return nil, compileErr(t.start, t.end, "invalid number '"+t.text+"'") + } + + return literal{v: f}, nil + case tString: + return literal{v: t.text}, nil + case tRegex: + re, err := regexp.Compile(t.text) + if err != nil { + return nil, compileErr(t.start, t.end, "invalid regular expression /"+t.text+"/") + } + + return regexLit{re: re}, nil + case tLParen: + return p.parseParen() + case tIdent: + return p.parseIdent(t) + case tOp, tComma, tRParen, tLBracket, tRBracket, tPipe: + } + + return nil, compileErr(t.start, t.end, "unexpected '"+t.text+"'") +} + +func (p *exprParser) parseParen() (expr, error) { + inner, err := p.parseOr() + if err != nil { + return nil, err + } + + if t := p.peek(); t == nil || t.kind != tRParen { + return nil, p.errHere("expected ')'") + } + + p.pos++ + + return inner, nil +} + +func (p *exprParser) parseIdent(t *token) (expr, error) { + if reserved[strings.ToLower(t.text)] { + return nil, compileErr(t.start, t.end, "unexpected keyword '"+t.text+"'") + } + + if next := p.peek(); next == nil || next.kind != tLParen { + return fieldRef{name: t.text}, nil + } + + spec, ok := functions[strings.ToLower(t.text)] + if !ok { + return nil, compileErr(t.start, t.end, "unknown function '"+t.text+"'") + } + + p.pos++ + + args, err := p.parseArgs() + if err != nil { + return nil, err + } + + if len(args) < spec.minArgs || (spec.maxArgs >= 0 && len(args) > spec.maxArgs) { + return nil, compileErr(t.start, t.end, "wrong number of arguments to "+t.text) + } + + return callExpr{spec: spec, args: args}, nil +} + +func (p *exprParser) parseArgs() ([]expr, error) { + var args []expr + + if t := p.peek(); t != nil && t.kind == tRParen { + p.pos++ + return args, nil + } + + for { + a, err := p.parseOr() + if err != nil { + return nil, err + } + + args = append(args, a) + + t := p.peek() + if t != nil && t.kind == tComma { + p.pos++ + continue + } + + if t == nil || t.kind != tRParen { + return nil, p.errHere("expected ',' or ')' in function call") + } + + p.pos++ + + return args, nil + } +} diff --git a/providers/aws/cloudwatchlogs/insights/query.go b/providers/aws/cloudwatchlogs/insights/query.go new file mode 100644 index 000000000..b2f8b01c3 --- /dev/null +++ b/providers/aws/cloudwatchlogs/insights/query.go @@ -0,0 +1,414 @@ +package insights + +import ( + "sort" + "strconv" + "strings" +) + +// System fields every log event carries. +const ( + FieldTimestamp = "@timestamp" + FieldMessage = "@message" + FieldLogStream = "@logStream" + FieldLog = "@log" + FieldPtr = "@ptr" + FieldIngestionTime = "@ingestionTime" +) + +// Field is one field/value pair of a result row. +type Field struct{ Name, Value string } + +type command interface { + apply(recs []Record) []Record +} + +type fieldItem struct { + name string + x expr +} + +type fieldsCmd struct{ items []fieldItem } + +func (c fieldsCmd) apply(recs []Record) []Record { + for _, r := range recs { + for _, it := range c.items { + if v := it.x.eval(r); v != nil { + r[it.name] = v + } else { + delete(r, it.name) + } + } + } + + return recs +} + +type filterCmd struct{ x expr } + +func (c filterCmd) apply(recs []Record) []Record { + out := recs[:0] + + for _, r := range recs { + if truthy(c.x.eval(r)) { + out = append(out, r) + } + } + + return out +} + +type sortKey struct { + field string + desc bool +} + +type sortCmd struct{ keys []sortKey } + +func (c sortCmd) apply(recs []Record) []Record { + sort.SliceStable(recs, func(i, j int) bool { + for _, k := range c.keys { + if d := sortCompare(recs[i][k.field], recs[j][k.field]); d != 0 { + return (d < 0) != k.desc + } + } + + return false + }) + + return recs +} + +// sortCompare orders values for sort: non-numbers before numbers, numbers +// numerically, missing values last. +func sortCompare(a, b any) int { + if a == nil || b == nil { + return boolOrder(a == nil, b == nil) + } + + _, an := toNum(a) + _, bn := toNum(b) + + if an != bn { + return boolOrder(an, bn) + } + + return compare(a, b) +} + +// boolOrder sorts false before true. +func boolOrder(a, b bool) int { + switch { + case a == b: + return 0 + case a: + return 1 + default: + return -1 + } +} + +type limitCmd struct{ n int } + +func (c limitCmd) apply(recs []Record) []Record { + if len(recs) > c.n { + return recs[:c.n] + } + + return recs +} + +// Query is a parsed Logs Insights query. +type Query struct { + cmds []command + shown []string + display []string +} + +// Result is the output of running a query. +type Result struct { + Rows [][]Field + // Matched counts the records that passed every filter. + Matched int +} + +// Run evaluates the query over records and keeps at most limit rows. Without +// a sort command, records are taken newest first, which is the order the +// console's default query uses. +func (q *Query) Run(recs []Record, limit int) Result { + recs, matched := q.pipeline(recs) + + if limit > 0 && len(recs) > limit { + recs = recs[:limit] + } + + cols := q.columns() + rows := make([][]Field, 0, len(recs)) + + for _, r := range recs { + row := make([]Field, 0, len(cols)+1) + + for _, c := range cols { + if v, ok := r[c]; ok && v != nil { + row = append(row, Field{Name: c, Value: toStr(v)}) + } + } + + rows = append(rows, append(row, Field{Name: FieldPtr, Value: toStr(r[FieldPtr])})) + } + + return Result{Rows: rows, Matched: matched} +} + +// pipeline applies the commands in order, newest record first unless a sort +// says otherwise. matched counts the records left when the first limit +// command applies (or at the end), which is what recordsMatched reports. +func (q *Query) pipeline(recs []Record) (out []Record, matched int) { + sortCmd{keys: []sortKey{{field: FieldTimestamp, desc: true}}}.apply(recs) + + matched = -1 + + for _, c := range q.cmds { + if _, isLimit := c.(limitCmd); isLimit && matched < 0 { + matched = len(recs) + } + + recs = c.apply(recs) + } + + if matched < 0 { + matched = len(recs) + } + + return recs, matched +} + +// columns are the displayed fields: the last display command, else every +// fields command's fields, else @timestamp and @message. +func (q *Query) columns() []string { + switch { + case len(q.display) > 0: + return q.display + case len(q.shown) > 0: + return q.shown + } + + return []string{FieldTimestamp, FieldMessage} +} + +// Parse parses a Logs Insights query. A query that does not parse returns a +// *CompileError. +func Parse(query string) (*Query, error) { + toks, err := lex(query) + if err != nil { + return nil, err + } + + q := &Query{} + + for _, part := range splitPipes(toks) { + if len(part) == 0 { + return nil, compileErr(len(query), len(query), "empty command") + } + + if err := q.addCommand(part, len(query)); err != nil { + return nil, err + } + } + + if len(q.cmds) == 0 && len(q.display) == 0 { + return nil, compileErr(0, len(query), "query is empty") + } + + return q, nil +} + +func splitPipes(toks []token) [][]token { + parts := [][]token{{}} + + for _, t := range toks { + if t.kind == tPipe { + parts = append(parts, []token{}) + continue + } + + parts[len(parts)-1] = append(parts[len(parts)-1], t) + } + + return parts +} + +func (q *Query) addCommand(part []token, end int) error { + head := part[0] + p := &exprParser{toks: part[1:], end: end} + + switch strings.ToLower(head.text) { + case "fields": + return q.addFields(p) + case cmdFilter, cmdWhere: + x, err := p.parseOr() + if err != nil { + return err + } + + q.cmds = append(q.cmds, filterCmd{x: x}) + + return p.done() + case "sort": + return q.addSort(p) + case "limit": + return q.addLimit(p) + case "display": + return q.addDisplay(p) + } + + return compileErr(head.start, head.end, "unsupported or unknown command '"+head.text+"'") +} + +func (p *exprParser) done() error { + if t := p.peek(); t != nil { + return compileErr(t.start, t.end, "unexpected '"+t.text+"'") + } + + return nil +} + +func (q *Query) addFields(p *exprParser) error { + var items []fieldItem + + for { + startTok := p.peek() + + x, err := p.parseOr() + if err != nil { + return err + } + + name := exprName(x, startTok, p) + + if p.atKeyword(kwAs) { + p.pos++ + + t := p.peek() + if t == nil || t.kind != tIdent { + return p.errHere("field name expected after as") + } + + name = t.text + p.pos++ + } + + items = append(items, fieldItem{name: name, x: x}) + + if !p.comma() { + break + } + } + + q.cmds = append(q.cmds, fieldsCmd{items: items}) + + for _, it := range items { + if !contains(q.shown, it.name) { + q.shown = append(q.shown, it.name) + } + } + + return p.done() +} + +// exprName names an unaliased fields expression: a field keeps its own name, +// anything else is named by its source tokens. +func exprName(x expr, start *token, p *exprParser) string { + if f, ok := x.(fieldRef); ok { + return f.name + } + + var parts []string + + for _, t := range p.toks { + if start != nil && t.start >= start.start && (p.pos >= len(p.toks) || t.start < p.toks[p.pos].start) { + parts = append(parts, t.text) + } + } + + return strings.Join(parts, "") +} + +func (p *exprParser) comma() bool { + if t := p.peek(); t != nil && t.kind == tComma { + p.pos++ + return true + } + + return false +} + +func (q *Query) addSort(p *exprParser) error { + var keys []sortKey + + for { + t := p.peek() + if t == nil || t.kind != tIdent { + return p.errHere("field name expected in sort") + } + + p.pos++ + + k := sortKey{field: t.text, desc: true} + + if p.atKeyword("asc") || p.atKeyword("desc") { + k.desc = p.atKeyword("desc") + p.pos++ + } + + keys = append(keys, k) + + if !p.comma() { + break + } + } + + q.cmds = append(q.cmds, sortCmd{keys: keys}) + + return p.done() +} + +func (q *Query) addLimit(p *exprParser) error { + t := p.peek() + if t == nil || t.kind != tNumber { + return p.errHere("number expected after limit") + } + + n, err := strconv.Atoi(t.text) + if err != nil || n <= 0 { + return compileErr(t.start, t.end, "limit must be a positive integer") + } + + p.pos++ + + q.cmds = append(q.cmds, limitCmd{n: n}) + + return p.done() +} + +func (q *Query) addDisplay(p *exprParser) error { + var names []string + + for { + t := p.peek() + if t == nil || t.kind != tIdent { + return p.errHere("field name expected in display") + } + + names = append(names, t.text) + p.pos++ + + if !p.comma() { + break + } + } + + q.display = names + + return p.done() +} diff --git a/providers/aws/cloudwatchlogs/insights/query_test.go b/providers/aws/cloudwatchlogs/insights/query_test.go new file mode 100644 index 000000000..4f8f60a43 --- /dev/null +++ b/providers/aws/cloudwatchlogs/insights/query_test.go @@ -0,0 +1,173 @@ +package insights + +import ( + "errors" + "strings" + "testing" + "time" +) + +func sampleRecords() []Record { + base := time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC) + msgs := []string{ + `{"level":"ERROR","latency":120,"user":{"id":"u1"},"tags":["a","b"]}`, + `{"level":"INFO","latency":15,"user":{"id":"u2"}}`, + "plain ERROR text Exception happened", + `{"level":"WARN","latency":80}`, + } + + recs := make([]Record, 0, len(msgs)) + for i, m := range msgs { + recs = append(recs, NewRecord(&Event{ + Account: "123456789012", LogGroup: "/app", LogStream: "s", + Timestamp: base.Add(time.Duration(i) * time.Second), Ingestion: base, Message: m, Ptr: "p" + string(rune('0'+i)), + })) + } + + return recs +} + +func run(t *testing.T, q string) Result { + t.Helper() + + parsed, err := Parse(q) + if err != nil { + t.Fatalf("Parse(%q): %v", q, err) + } + + return parsed.Run(sampleRecords(), 0) +} + +func col(rows [][]Field, name string) []string { + var out []string + + for _, row := range rows { + for _, f := range row { + if f.Name == name { + out = append(out, f.Value) + } + } + } + + return out +} + +func TestQueries(t *testing.T) { + tests := []struct { + q string + field string + want string + }{ + {"fields @timestamp, @message | sort @timestamp desc | limit 2", "@timestamp", + "2025-01-01 00:00:03.000|2025-01-01 00:00:02.000"}, + {"fields level | filter level = 'ERROR'", "level", "ERROR"}, + {"filter latency > 50 | sort latency asc | fields latency", "latency", "80|120"}, + {"filter latency >= 15 and level != 'WARN' | sort latency | fields latency", "latency", "120|15"}, + {"filter @message like /Exception/ | fields @message", "@message", "plain ERROR text Exception happened"}, + {`filter @message like "ERROR" | fields @logStream`, "@logStream", "s|s"}, + {"filter @message not like /ERROR/ | fields level", "level", "WARN|INFO"}, + {"filter level in ['WARN', 'INFO'] | fields level", "level", "WARN|INFO"}, + {"filter level not in ['WARN', 'INFO'] and ispresent(level) | fields level", "level", "ERROR"}, + {"filter level =~ /^E/ | fields user.id", "user.id", "u1"}, + {"filter not ispresent(level) | fields @log", "@log", "123456789012:/app"}, + {"filter tags.1 = 'b' | fields tags.0", "tags.0", "a"}, + {"fields latency * 2 as double | filter double > 200 | display double", "double", "240"}, + {"fields toupper(level) as lv, strlen(level) as n | filter n = 4 | sort lv asc | display lv", "lv", "INFO|WARN"}, + {"fields concat(level, '-', user.id) as k | filter ispresent(user.id) | sort k asc | display k", "k", "ERROR-u1|INFO-u2"}, + {"# comment\nfields level | limit 1", "level", "WARN"}, + {"filter (latency < 20 or latency > 100) | fields abs(latency - 100) as d | sort d asc | display d", "d", "20|85"}, + } + + for _, tc := range tests { + t.Run(tc.q, func(t *testing.T) { + got := strings.Join(col(run(t, tc.q).Rows, tc.field), "|") + if got != tc.want { + t.Fatalf("%s = %q, want %q", tc.field, got, tc.want) + } + }) + } +} + +func TestDefaultColumnsPtrAndMatched(t *testing.T) { + res := run(t, "filter ispresent(latency) | limit 1") + + if res.Matched != 3 { + t.Fatalf("Matched = %d, want 3 (before the limit)", res.Matched) + } + + if len(res.Rows) != 1 { + t.Fatalf("rows = %d, want 1", len(res.Rows)) + } + + var names []string + for _, f := range res.Rows[0] { + names = append(names, f.Name) + } + + if strings.Join(names, ",") != "@timestamp,@message,@ptr" { + t.Fatalf("columns = %v, want @timestamp, @message and @ptr", names) + } +} + +func TestRunLimitParam(t *testing.T) { + q, err := Parse("fields @message") + if err != nil { + t.Fatal(err) + } + + if got := len(q.Run(sampleRecords(), 2).Rows); got != 2 { + t.Fatalf("rows = %d, want 2", got) + } +} + +func TestCompileErrors(t *testing.T) { + for _, q := range []string{ + "", + "fields", + "filter latency >", + "filter level = 'x", + "stats count(*) by level", + "pattern @message", + "fields level |", + "filter nosuch(level)", + "sort", + "limit x", + "filter level in 'a'", + "filter @message like /unterminated", + "fields level as", + "filter (level = 'x'", + } { + _, err := Parse(q) + + var ce *CompileError + if !errors.As(err, &ce) { + t.Errorf("Parse(%q) = %v, want a CompileError", q, err) + continue + } + + if ce.Start < 0 || ce.End < ce.Start || ce.End > len(q) { + t.Errorf("Parse(%q) offsets %d..%d out of range", q, ce.Start, ce.End) + } + } +} + +func TestFieldShares(t *testing.T) { + shares := FieldShares(sampleRecords()) + + got := map[string]int{} + for _, s := range shares { + got[s.Name] = s.Percent + } + + if got["@message"] != 100 || got["level"] != 75 || got["user.id"] != 50 || got["tags.0"] != 25 { + t.Fatalf("shares = %v", got) + } + + if _, ok := got["@ptr"]; ok { + t.Fatal("@ptr listed as a field") + } + + if shares[0].Percent != 100 { + t.Fatalf("not sorted by percent: %v", shares) + } +} diff --git a/providers/aws/cloudwatchlogs/insights/record.go b/providers/aws/cloudwatchlogs/insights/record.go new file mode 100644 index 000000000..343e10ca8 --- /dev/null +++ b/providers/aws/cloudwatchlogs/insights/record.go @@ -0,0 +1,120 @@ +package insights + +import ( + "encoding/json" + "sort" + "strconv" + "strings" + "time" +) + +// timestampLayout is how Logs Insights renders @timestamp and @ingestionTime. +const timestampLayout = "2006-01-02 15:04:05.000" + +// Event is a log event as the query engine sees it. +type Event struct { + Account string + LogGroup string + LogStream string + Timestamp time.Time + Ingestion time.Time + Message string + Ptr string +} + +// NewRecord builds an event's fields: the system fields plus, for a JSON +// message, its properties flattened to dotted names (arrays by index), the +// way Logs Insights discovers fields in JSON logs. +func NewRecord(e *Event) Record { + r := Record{ + FieldTimestamp: e.Timestamp.UTC().Format(timestampLayout), + FieldMessage: e.Message, + FieldLogStream: e.LogStream, + FieldLog: e.Account + ":" + e.LogGroup, + FieldPtr: e.Ptr, + FieldIngestionTime: e.Ingestion.UTC().Format(timestampLayout), + } + + var doc any + if strings.HasPrefix(strings.TrimSpace(e.Message), "{") && json.Unmarshal([]byte(e.Message), &doc) == nil { + flatten(r, "", doc) + } + + return r +} + +func flatten(r Record, prefix string, v any) { + join := func(k string) string { + if prefix == "" { + return k + } + + return prefix + "." + k + } + + switch x := v.(type) { + case map[string]any: + for k, child := range x { + flatten(r, join(k), child) + } + case []any: + for i, child := range x { + flatten(r, join(strconv.Itoa(i)), child) + } + case nil: + // JSON null is not a value; the field is left out. + default: + if prefix != "" { + r[prefix] = x + } + } +} + +// Strings renders a record's fields as strings, the GetLogRecord shape. +func (r Record) Strings() map[string]string { + out := make(map[string]string, len(r)) + + for k, v := range r { + out[k] = toStr(v) + } + + return out +} + +// FieldShare is how often a field occurs across a set of events. +type FieldShare struct { + Name string + Percent int +} + +// percentScale turns a fraction into a whole percentage. +const percentScale = 100 + +// FieldShares returns every field that occurs in recs with the percentage +// of records that carry it, most common first. @ptr is internal and left out. +func FieldShares(recs []Record) []FieldShare { + counts := map[string]int{} + + for _, r := range recs { + for k := range r { + if k != FieldPtr { + counts[k]++ + } + } + } + + out := make([]FieldShare, 0, len(counts)) + for k, n := range counts { + out = append(out, FieldShare{Name: k, Percent: n * percentScale / len(recs)}) + } + + sort.Slice(out, func(i, j int) bool { + if out[i].Percent != out[j].Percent { + return out[i].Percent > out[j].Percent + } + + return out[i].Name < out[j].Name + }) + + return out +} diff --git a/providers/aws/cloudwatchlogs/insights/substr_test.go b/providers/aws/cloudwatchlogs/insights/substr_test.go new file mode 100644 index 000000000..3c5adaf2c --- /dev/null +++ b/providers/aws/cloudwatchlogs/insights/substr_test.go @@ -0,0 +1,71 @@ +package insights + +import ( + "math" + "testing" +) + +func TestClampIndex(t *testing.T) { + tests := []struct { + name string + f float64 + max int + want int + wantOK bool + }{ + {"nan", math.NaN(), 5, 0, false}, + {"plus inf", math.Inf(1), 5, 0, false}, + {"minus inf", math.Inf(-1), 5, 0, false}, + {"huge", 1e300, 5, 5, true}, + {"huge negative", -1e300, 5, 0, true}, + {"beyond int64", float64(math.MaxInt64) * 4, 3, 3, true}, + {"fraction truncates", 2.9, 5, 2, true}, + {"in range", 3, 5, 3, true}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, ok := clampIndex(tt.f, tt.max) + if got != tt.want || ok != tt.wantOK { + t.Fatalf("clampIndex(%v, %d) = (%d, %v), want (%d, %v)", tt.f, tt.max, got, ok, tt.want, tt.wantOK) + } + }) + } +} + +func TestSubstrInvalidArgs(t *testing.T) { + tests := []struct { + name string + args []any + want any + }{ + {"nan start", []any{"abcdef", "nan"}, nil}, + {"inf start", []any{"abcdef", "inf"}, nil}, + {"minus inf start", []any{"abcdef", "-inf"}, nil}, + {"huge start", []any{"abcdef", 1e300}, nil}, + {"negative start", []any{"abcdef", -1.0}, nil}, + {"nan length", []any{"abcdef", 1.0, "nan"}, nil}, + {"inf length", []any{"abcdef", 1.0, "+Inf"}, nil}, + {"huge length", []any{"abcdef", 1.0, 1e300}, "bcdef"}, + {"negative length", []any{"abcdef", 2.0, -5.0}, ""}, + {"huge negative length", []any{"abcdef", 2.0, -1e300}, ""}, + {"start at end", []any{"abc", 3.0}, ""}, + {"normal", []any{"abcdef", 1.0, 3.0}, "bcd"}, + {"non-numeric length", []any{"abcdef", 4.0, "x"}, "ef"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := substr(tt.args); got != tt.want { + t.Fatalf("substr(%v) = %#v, want %#v", tt.args, got, tt.want) + } + }) + } +} + +func TestSubstrNaNQueryDoesNotPanic(t *testing.T) { + res := run(t, `fields substr(@message, "nan") as x, substr(@message, 0, "-inf") as y`) + if len(res.Rows) == 0 { + t.Fatal("expected rows") + } +} diff --git a/providers/aws/cloudwatchlogs/insights_queries.go b/providers/aws/cloudwatchlogs/insights_queries.go new file mode 100644 index 000000000..120a79273 --- /dev/null +++ b/providers/aws/cloudwatchlogs/insights_queries.go @@ -0,0 +1,532 @@ +package cloudwatchlogs + +import ( + "context" + "encoding/base64" + "errors" + "sort" + "strconv" + "strings" + "time" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/internal/idgen" + "github.com/stackshy/cloudemu/v2/providers/aws/cloudwatchlogs/insights" + "github.com/stackshy/cloudemu/v2/services/logging/driver" +) + +// Logs Insights limits, per the StartQuery API reference. +const ( + // insightsQueryTimeout is the StartQuery doc's "Queries time out after + // 60 minutes of runtime". + insightsQueryTimeout = 60 * time.Minute + // maxConcurrentInsightsQueries: up to 100 concurrent queries. + maxConcurrentInsightsQueries = 100 + maxQueryLogGroups = 50 + maxQueryStringLength = 10000 + maxQueryLimit = 100000 + // defaultQueryLimit is the row cap when StartQuery has no limit, the most + // one GetQueryResults page returns. + defaultQueryLimit = 10000 + // queryLanguageCWLI is the Logs Insights query language, the only one + // the emulator runs. + queryLanguageCWLI = "CWLI" + // fieldDiscoveryWindow is how far either side of GetLogGroupFields' time + // it samples; fieldDiscoveryRecent is the window when no time is given. + fieldDiscoveryWindow = 8 * time.Minute + fieldDiscoveryRecent = 15 * time.Minute +) + +// queryRecord is one Logs Insights query. The request is kept so a Running +// query can be evaluated on its first poll, including after a restore. +type queryRecord struct { + ID string `json:"id"` + QueryString string `json:"queryString"` + QueryLanguage string `json:"queryLanguage"` + LogGroups []string `json:"logGroups"` + Start time.Time `json:"start"` + End time.Time `json:"end"` + Limit int `json:"limit"` + Status string `json:"status"` + CreatedAt time.Time `json:"createdAt"` + DurationMS int64 `json:"durationMs,omitempty"` + Results [][]driver.QueryResultField `json:"results,omitempty"` + Stats driver.QueryStatistics `json:"stats"` +} + +// StartQuery validates and records a Logs Insights query as Running. The +// query runs on its first GetQueryResults. +func (m *Mock) StartQuery(_ context.Context, in *driver.StartQueryInput) (string, error) { + groups, err := m.queryLogGroups(in) + if err != nil { + return "", err + } + + if err := validateQueryRequest(in); err != nil { + return "", err + } + + if _, err := insights.Parse(in.QueryString); err != nil { + return "", malformed(err) + } + + rec := queryRecord{ + ID: idgen.UUID(), QueryString: in.QueryString, QueryLanguage: queryLanguageCWLI, + LogGroups: groups, Start: in.StartTime, End: in.EndTime, Limit: in.Limit, + Status: driver.QueryStatusRunning, CreatedAt: m.opts.Clock.Now(), + } + + m.admitMu.Lock() + defer m.admitMu.Unlock() + + if m.runningQueries() >= maxConcurrentInsightsQueries { + return "", cerrors.Newf(cerrors.ResourceExhausted, + "the account already has the maximum of %d concurrent queries", maxConcurrentInsightsQueries) + } + + m.queries.Set(rec.ID, rec) + + return rec.ID, nil +} + +func malformed(err error) error { + var ce *insights.CompileError + if errors.As(err, &ce) { + return &driver.MalformedQueryError{Message: ce.Message, Start: ce.Start, End: ce.End} + } + + return err +} + +// queryLogGroups resolves the one log group selector StartQuery allows to +// existing log group names. +func (m *Mock) queryLogGroups(in *driver.StartQueryInput) ([]string, error) { + set := 0 + + for _, has := range []bool{in.LogGroupName != "", len(in.LogGroupNames) > 0, len(in.LogGroupIdentifiers) > 0} { + if has { + set++ + } + } + + if set != 1 { + return nil, cerrors.New(cerrors.InvalidArgument, + "exactly one of logGroupName, logGroupNames or logGroupIdentifiers must be specified") + } + + names := in.LogGroupNames + if in.LogGroupName != "" { + names = []string{in.LogGroupName} + } + + for _, id := range in.LogGroupIdentifiers { + names = append(names, logGroupFromIdentifier(id)) + } + + if len(names) > maxQueryLogGroups { + return nil, cerrors.Newf(cerrors.InvalidArgument, "a query can include at most %d log groups", maxQueryLogGroups) + } + + for _, n := range names { + if !m.groups.Has(n) { + return nil, cerrors.Newf(cerrors.NotFound, "log group %q not found", n) + } + } + + return names, nil +} + +// logGroupFromIdentifier accepts a log group name or ARN. +func logGroupFromIdentifier(id string) string { + if i := strings.LastIndex(id, logGroupARNMarker); i >= 0 { + return strings.TrimSuffix(id[i+len(logGroupARNMarker):], ":*") + } + + return id +} + +func validateQueryRequest(in *driver.StartQueryInput) error { + switch { + case len(in.QueryString) > maxQueryStringLength: + return cerrors.Newf(cerrors.InvalidArgument, "queryString must be at most %d characters", maxQueryStringLength) + case in.QueryLanguage != "" && in.QueryLanguage != queryLanguageCWLI: + return cerrors.Newf(cerrors.InvalidArgument, "query language %s is not supported", in.QueryLanguage) + case in.EndTime.Before(in.StartTime): + return cerrors.New(cerrors.InvalidArgument, "endTime must not be before startTime") + case in.Limit < 0 || in.Limit > maxQueryLimit: + return cerrors.Newf(cerrors.InvalidArgument, "limit must be between 1 and %d", maxQueryLimit) + } + + return nil +} + +func (m *Mock) runningQueries() int { + n := 0 + + all := m.queries.All() + for id := range all { + if all[id].Status == driver.QueryStatusRunning { + n++ + } + } + + return n +} + +// GetQueryResults returns a query's results. The first poll of a Running +// query runs it and settles it to Complete, or to Timeout when it was +// started more than an hour ago. +func (m *Mock) GetQueryResults(_ context.Context, queryID string) (*driver.QueryResults, error) { + q, ok := m.queries.Get(queryID) + if !ok { + return nil, cerrors.Newf(cerrors.NotFound, "query %s not found", queryID) + } + + if q.Status == driver.QueryStatusRunning { + q = m.settleQuery(&q) + } + + m.mu.RLock() + key := m.queryResultKmsKey + m.mu.RUnlock() + + return &driver.QueryResults{ + Status: q.Status, QueryLanguage: q.QueryLanguage, EncryptionKey: key, + Results: q.Results, Statistics: q.Stats, + }, nil +} + +// settleQuery runs a Running query and records the outcome. The query runs +// outside the queries store lock; if a concurrent poll or StopQuery settled +// it first, that outcome stands. +func (m *Mock) settleQuery(q *queryRecord) queryRecord { + now := m.opts.Clock.Now() + timedOut := now.Sub(q.CreatedAt) > insightsQueryTimeout + + var ( + results [][]driver.QueryResultField + stats driver.QueryStatistics + ) + + if !timedOut { + results, stats = m.runQuery(q) + } + + m.queries.Update(q.ID, func(cur queryRecord) queryRecord { + if cur.Status != driver.QueryStatusRunning { + return cur + } + + if timedOut { + cur.Status = driver.QueryStatusTimeout + return cur + } + + cur.Results, cur.Stats = results, stats + cur.Status = driver.QueryStatusComplete + cur.DurationMS = now.Sub(cur.CreatedAt).Milliseconds() + + return cur + }) + + if cur, ok := m.queries.Get(q.ID); ok { + return cur + } + + return *q +} + +// runQuery evaluates a recorded query over its log groups' events in the +// query's time range. The range is in whole seconds and inclusive. +func (m *Mock) runQuery(q *queryRecord) ([][]driver.QueryResultField, driver.QueryStatistics) { + parsed, err := insights.Parse(q.QueryString) + if err != nil { + return nil, driver.QueryStatistics{} + } + + recs, bytes := m.queryRecords(q.LogGroups, q.Start, q.End.Add(time.Second)) + + limit := q.Limit + if limit == 0 { + limit = defaultQueryLimit + } + + scanned := len(recs) + res := parsed.Run(recs, limit) + + rows := make([][]driver.QueryResultField, 0, len(res.Rows)) + + for _, row := range res.Rows { + out := make([]driver.QueryResultField, 0, len(row)) + for _, f := range row { + out = append(out, driver.QueryResultField{Field: f.Name, Value: f.Value}) + } + + rows = append(rows, out) + } + + return rows, driver.QueryStatistics{ + RecordsMatched: float64(res.Matched), RecordsScanned: float64(scanned), + BytesScanned: float64(bytes), LogGroupsScanned: float64(len(q.LogGroups)), + } +} + +// queryRecords collects the events with a timestamp in [from, to) from the +// named log groups as query records, with their scanned byte count. Expired +// events are purged first, so queries see what every other read API sees. +func (m *Mock) queryRecords(groups []string, from, to time.Time) (recs []insights.Record, bytes int64) { + for _, name := range groups { + g, ok := m.groups.Get(name) + if !ok { + continue + } + + m.purgeExpired(name, g) + + for _, s := range g.streams.SortedValues() { + r, b := m.streamRecords(name, s, from, to) + recs = append(recs, r...) + bytes += b + } + } + + return recs, bytes +} + +func (m *Mock) streamRecords(group string, s *logStream, from, to time.Time) (recs []insights.Record, bytes int64) { + s.mu.RLock() + defer s.mu.RUnlock() + + seen := map[eventTimes]int{} + + for i := range s.events { + e := &s.events[i] + key := eventTimes{e.Timestamp.UnixMilli(), e.IngestionTime.UnixNano()} + + // Events are sorted by timestamp, so equal timestamps are + // contiguous and the ordinal map only ever holds one timestamp. + if i > 0 && !s.events[i-1].Timestamp.Equal(e.Timestamp) { + clear(seen) + } + + ordinal := seen[key] + seen[key]++ + + if e.Timestamp.Before(from) || !e.Timestamp.Before(to) { + continue + } + + bytes += int64(len(e.Message)) + recs = append(recs, insights.NewRecord(&insights.Event{ + Account: m.opts.AccountID, LogGroup: group, LogStream: s.info.Name, + Timestamp: e.Timestamp, Ingestion: e.IngestionTime, Message: e.Message, + Ptr: encodePtr(group, s.info.Name, key, ordinal), + })) + } + + return recs, bytes +} + +// eventTimes is an event's timestamp (milliseconds) and ingestion time +// (nanoseconds), the identity a log record pointer carries. +type eventTimes struct{ ts, ingest int64 } + +// ptrSep separates the parts of a log record pointer. +const ptrSep = "\x00" + +// encodePtr identifies an event by its group, stream, times and its position +// among the stream's events sharing both times. Unlike a slice index, that +// survives the retention purge removing older events. +func encodePtr(group, stream string, t eventTimes, ordinal int) string { + raw := strings.Join([]string{ + group, stream, + strconv.FormatInt(t.ts, 10), + strconv.FormatInt(t.ingest, 10), + strconv.Itoa(ordinal), + }, ptrSep) + + return base64.RawURLEncoding.EncodeToString([]byte(raw)) +} + +// ptrParts is the number of fields encodePtr joins. +const ptrParts = 5 + +// GetLogRecord returns every field of the log event a query result's @ptr +// points to. Times are epoch milliseconds, the unit of the event's timestamp +// and ingestionTime. The API reference's sample shows a 10-digit @timestamp, +// but that sample is hand-written (pointer "123456789", invalid JSON); the +// SDKs carry no fixture and LocalStack does not implement GetLogRecord. +func (m *Mock) GetLogRecord(_ context.Context, ptr string) (map[string]string, error) { + raw, err := base64.RawURLEncoding.DecodeString(ptr) + parts := strings.Split(string(raw), ptrSep) + + if err != nil || len(parts) != ptrParts { + return nil, cerrors.New(cerrors.InvalidArgument, "logRecordPointer is not valid") + } + + tsMillis, err1 := strconv.ParseInt(parts[2], 10, 64) + ingestNanos, err2 := strconv.ParseInt(parts[3], 10, 64) + ordinal, err3 := strconv.Atoi(parts[4]) + + if err1 != nil || err2 != nil || err3 != nil { + return nil, cerrors.New(cerrors.InvalidArgument, "logRecordPointer is not valid") + } + + e, ok := m.findEvent(parts[0], parts[1], tsMillis, ingestNanos, ordinal) + if !ok { + return nil, cerrors.New(cerrors.NotFound, "the log record does not exist") + } + + rec := insights.NewRecord(&insights.Event{ + Account: m.opts.AccountID, LogGroup: parts[0], LogStream: parts[1], + Timestamp: e.Timestamp, Ingestion: e.IngestionTime, Message: e.Message, + }).Strings() + + delete(rec, insights.FieldPtr) + rec[insights.FieldTimestamp] = strconv.FormatInt(e.Timestamp.UnixMilli(), 10) + rec[insights.FieldIngestionTime] = strconv.FormatInt(e.IngestionTime.UnixMilli(), 10) + + return rec, nil +} + +func (m *Mock) findEvent(group, stream string, tsMillis, ingestNanos int64, ordinal int) (ev driver.LogEvent, found bool) { + g, ok := m.groups.Get(group) + if !ok { + return driver.LogEvent{}, false + } + + m.purgeExpired(group, g) + + s, ok := g.streams.Get(stream) + if !ok { + return driver.LogEvent{}, false + } + + s.mu.RLock() + defer s.mu.RUnlock() + + seen := 0 + + for _, e := range s.events { + if e.Timestamp.UnixMilli() != tsMillis || e.IngestionTime.UnixNano() != ingestNanos { + continue + } + + if seen == ordinal { + return e, true + } + + seen++ + } + + return driver.LogEvent{}, false +} + +// StopQuery cancels a query that is still Running. +func (m *Mock) StopQuery(_ context.Context, queryID string) error { + if !m.queries.Has(queryID) { + return cerrors.Newf(cerrors.NotFound, "query %s not found", queryID) + } + + stopped := false + + m.queries.Update(queryID, func(q queryRecord) queryRecord { + if q.Status == driver.QueryStatusRunning { + q.Status = driver.QueryStatusCancelled + stopped = true + } + + return q + }) + + if !stopped { + return cerrors.Newf(cerrors.InvalidArgument, "query %s is not running", queryID) + } + + return nil +} + +// DescribeQueries lists queries, newest first. +func (m *Mock) DescribeQueries(_ context.Context, in driver.DescribeQueriesInput) ([]driver.QueryInfo, error) { + var out []driver.QueryInfo + + all := m.queries.All() + for id := range all { + q := all[id] + if !queryMatches(&q, in) { + continue + } + + info := driver.QueryInfo{ + QueryID: q.ID, QueryString: q.QueryString, QueryLanguage: q.QueryLanguage, Status: q.Status, + CreateTime: q.CreatedAt, QueryDuration: q.DurationMS, BytesScanned: q.Stats.BytesScanned, + } + + if len(q.LogGroups) > 0 { + info.LogGroupName = q.LogGroups[0] + } + + out = append(out, info) + } + + sort.Slice(out, func(i, j int) bool { + if !out[i].CreateTime.Equal(out[j].CreateTime) { + return out[i].CreateTime.After(out[j].CreateTime) + } + + return out[i].QueryID < out[j].QueryID + }) + + return out, nil +} + +func queryMatches(q *queryRecord, in driver.DescribeQueriesInput) bool { + if in.Status != "" && q.Status != in.Status { + return false + } + + if in.QueryLanguage != "" && q.QueryLanguage != in.QueryLanguage { + return false + } + + if in.LogGroupName == "" { + return true + } + + for _, g := range q.LogGroups { + if g == in.LogGroupName { + return true + } + } + + return false +} + +// GetLogGroupFields reports the fields in a log group's events, with the +// share of events carrying each, over the 8 minutes either side of at, or +// the last 15 minutes when at is zero. +func (m *Mock) GetLogGroupFields(_ context.Context, logGroup string, at time.Time) ([]driver.LogGroupField, error) { + if !m.groups.Has(logGroup) { + return nil, cerrors.Newf(cerrors.NotFound, "log group %q not found", logGroup) + } + + from, to := at.Add(-fieldDiscoveryWindow), at.Add(fieldDiscoveryWindow) + + if at.IsZero() { + now := m.opts.Clock.Now() + from, to = now.Add(-fieldDiscoveryRecent), now.Add(time.Nanosecond) + } + + recs, _ := m.queryRecords([]string{logGroup}, from, to) + + out := make([]driver.LogGroupField, 0) + if len(recs) == 0 { + return out, nil + } + + for _, s := range insights.FieldShares(recs) { + out = append(out, driver.LogGroupField{Name: s.Name, Percent: s.Percent}) + } + + return out, nil +} diff --git a/providers/aws/cloudwatchlogs/insights_queries_test.go b/providers/aws/cloudwatchlogs/insights_queries_test.go new file mode 100644 index 000000000..c825509c2 --- /dev/null +++ b/providers/aws/cloudwatchlogs/insights_queries_test.go @@ -0,0 +1,281 @@ +package cloudwatchlogs + +import ( + "context" + stderrors "errors" + "sync" + "testing" + "time" + + "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/services/logging/driver" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func newInsightsMock(t *testing.T) *Mock { + t.Helper() + + m := newWindowMock(t, 0) + putMessages(t, m, + `{"level":"ERROR","latency":120,"user":{"id":"u1"}}`, + `{"level":"INFO","latency":15}`, + "plain ERROR Exception text", + ) + + return m +} + +func startQuery(t *testing.T, m *Mock, q string) string { + t.Helper() + + now := m.opts.Clock.Now() + + id, err := m.StartQuery(context.Background(), &driver.StartQueryInput{ + LogGroupName: "g", StartTime: now.Add(-6 * time.Hour), EndTime: now, QueryString: q, + }) + require.NoError(t, err) + + return id +} + +func rowField(row []driver.QueryResultField, name string) string { + for _, f := range row { + if f.Field == name { + return f.Value + } + } + + return "" +} + +func TestInsightsQueryLifecycle(t *testing.T) { + ctx := context.Background() + m := newInsightsMock(t) + + id := startQuery(t, m, "fields @timestamp, level, latency | filter latency > 10 | sort latency desc | limit 1") + + infos, err := m.DescribeQueries(ctx, driver.DescribeQueriesInput{Status: driver.QueryStatusRunning}) + require.NoError(t, err) + require.Len(t, infos, 1, "a query is Running until its first poll") + + res, err := m.GetQueryResults(ctx, id) + require.NoError(t, err) + assert.Equal(t, driver.QueryStatusComplete, res.Status) + assert.Equal(t, "CWLI", res.QueryLanguage) + require.Len(t, res.Results, 1) + assert.Equal(t, "120", rowField(res.Results[0], "latency")) + assert.Equal(t, "ERROR", rowField(res.Results[0], "level")) + assert.NotEmpty(t, rowField(res.Results[0], "@ptr")) + assert.InDelta(t, 2.0, res.Statistics.RecordsMatched, 0) + assert.InDelta(t, 3.0, res.Statistics.RecordsScanned, 0) + assert.Positive(t, res.Statistics.BytesScanned) + + // A completed query can no longer be stopped. + err = m.StopQuery(ctx, id) + require.Error(t, err) + assert.True(t, errors.IsInvalidArgument(err)) + + rec, err := m.GetLogRecord(ctx, rowField(res.Results[0], "@ptr")) + require.NoError(t, err) + assert.Equal(t, `{"level":"ERROR","latency":120,"user":{"id":"u1"}}`, rec["@message"]) + assert.Equal(t, "u1", rec["user.id"]) + assert.Equal(t, "s", rec["@logStream"]) + assert.Equal(t, "123456789012:g", rec["@log"]) + assert.NotContains(t, rec, "@ptr") + + done, err := m.DescribeQueries(ctx, driver.DescribeQueriesInput{LogGroupName: "g", Status: driver.QueryStatusComplete}) + require.NoError(t, err) + require.Len(t, done, 1) + assert.Equal(t, "g", done[0].LogGroupName) +} + +func TestInsightsStopAndTimeout(t *testing.T) { + ctx := context.Background() + m := newInsightsMock(t) + + stopped := startQuery(t, m, "fields @message") + require.NoError(t, m.StopQuery(ctx, stopped)) + + res, err := m.GetQueryResults(ctx, stopped) + require.NoError(t, err) + assert.Equal(t, driver.QueryStatusCancelled, res.Status) + assert.Empty(t, res.Results) + + slow := startQuery(t, m, "fields @message") + advanceClock(t, m, insightsQueryTimeout+time.Minute) + + res, err = m.GetQueryResults(ctx, slow) + require.NoError(t, err) + assert.Equal(t, driver.QueryStatusTimeout, res.Status) + + err = m.StopQuery(ctx, "nope") + require.Error(t, err) + assert.True(t, errors.IsNotFound(err)) + + _, err = m.GetQueryResults(ctx, "nope") + require.Error(t, err) + assert.True(t, errors.IsNotFound(err)) +} + +func TestInsightsStartQueryValidation(t *testing.T) { + ctx := context.Background() + m := newInsightsMock(t) + now := m.opts.Clock.Now() + + base := func() *driver.StartQueryInput { + return &driver.StartQueryInput{LogGroupName: "g", StartTime: now.Add(-time.Hour), EndTime: now, QueryString: "fields @message"} + } + + cases := map[string]func(in *driver.StartQueryInput){ + "no log group": func(in *driver.StartQueryInput) { in.LogGroupName = "" }, + "two selectors": func(in *driver.StartQueryInput) { in.LogGroupNames = []string{"g"} }, + "end before start": func(in *driver.StartQueryInput) { in.EndTime = in.StartTime.Add(-time.Second) }, + "limit too large": func(in *driver.StartQueryInput) { in.Limit = maxQueryLimit + 1 }, + "sql not supported": func(in *driver.StartQueryInput) { in.QueryLanguage = "SQL" }, + "too many log group": func(in *driver.StartQueryInput) { in.LogGroupName, in.LogGroupNames = "", make([]string, 51) }, + } + + for name, mutate := range cases { + in := base() + mutate(in) + + _, err := m.StartQuery(ctx, in) + require.Error(t, err, name) + assert.True(t, errors.IsInvalidArgument(err), "%s: %v", name, err) + } + + in := base() + in.LogGroupName = "missing" + _, err := m.StartQuery(ctx, in) + assert.True(t, errors.IsNotFound(err)) + + in = base() + in.LogGroupName, in.LogGroupIdentifiers = "", []string{"arn:aws:logs:us-east-1:123456789012:log-group:g:*"} + _, err = m.StartQuery(ctx, in) + require.NoError(t, err, "a log group ARN identifies the group") + + in = base() + in.QueryString = "fields @message | stats count(*)" + _, err = m.StartQuery(ctx, in) + + var mq *driver.MalformedQueryError + require.True(t, stderrors.As(err, &mq), "got %v", err) + assert.Equal(t, len("fields @message | "), mq.Start) +} + +func TestInsightsConcurrentQueryLimit(t *testing.T) { + m := newInsightsMock(t) + + for range maxConcurrentInsightsQueries { + startQuery(t, m, "fields @message") + } + + now := m.opts.Clock.Now() + + _, err := m.StartQuery(context.Background(), &driver.StartQueryInput{ + LogGroupName: "g", StartTime: now.Add(-time.Hour), EndTime: now, QueryString: "fields @message", + }) + require.Error(t, err) + assert.Equal(t, errors.ResourceExhausted, errors.GetCode(err)) +} + +func TestInsightsConcurrentQueryLimitParallel(t *testing.T) { + const callers = 150 + + m := newInsightsMock(t) + now := m.opts.Clock.Now() + + var ( + wg sync.WaitGroup + mu sync.Mutex + started, limited int + start = make(chan struct{}) + ) + + for range callers { + wg.Add(1) + + go func() { + defer wg.Done() + <-start + + _, err := m.StartQuery(context.Background(), &driver.StartQueryInput{ + LogGroupName: "g", StartTime: now.Add(-time.Hour), EndTime: now, QueryString: "fields @message", + }) + + mu.Lock() + defer mu.Unlock() + + switch { + case err == nil: + started++ + case errors.GetCode(err) == errors.ResourceExhausted: + limited++ + default: + t.Errorf("unexpected error: %v", err) + } + }() + } + + close(start) + wg.Wait() + + assert.Equal(t, maxConcurrentInsightsQueries, started) + assert.Equal(t, callers-maxConcurrentInsightsQueries, limited) + assert.Equal(t, maxConcurrentInsightsQueries, m.runningQueries()) +} + +func TestInsightsTimeRangeAndSnapshot(t *testing.T) { + ctx := context.Background() + m := newInsightsMock(t) + ev := testEventTime() // putMessages starts here, one second apart + + // A range that ends on the first event's second includes it (inclusive). + id, err := m.StartQuery(ctx, &driver.StartQueryInput{ + LogGroupName: "g", StartTime: ev, EndTime: ev, QueryString: "fields @message", + }) + require.NoError(t, err) + + // A Running query survives a snapshot and runs on its first poll after. + raw, err := m.Snapshot(ctx, true) + require.NoError(t, err) + + restored := newTestMock() + require.NoError(t, restored.Restore(ctx, raw)) + + res, err := restored.GetQueryResults(ctx, id) + require.NoError(t, err) + require.Equal(t, driver.QueryStatusComplete, res.Status) + require.Len(t, res.Results, 1) + assert.Equal(t, `{"level":"ERROR","latency":120,"user":{"id":"u1"}}`, rowField(res.Results[0], "@message")) +} + +func TestGetLogGroupFields(t *testing.T) { + ctx := context.Background() + m := newInsightsMock(t) + + fields, err := m.GetLogGroupFields(ctx, "g", testEventTime()) + require.NoError(t, err) + + got := map[string]int{} + for _, f := range fields { + got[f.Name] = f.Percent + } + + assert.Equal(t, 100, got["@message"]) + assert.Equal(t, 66, got["level"]) + assert.Equal(t, 33, got["user.id"]) + assert.Equal(t, 100, fields[0].Percent) + + // Without a time it samples the last 15 minutes; the events are 3h old. + recent, err := m.GetLogGroupFields(ctx, "g", time.Time{}) + require.NoError(t, err) + assert.Empty(t, recent) + + _, err = m.GetLogGroupFields(ctx, "nope", time.Time{}) + assert.True(t, errors.IsNotFound(err)) + + _, err = m.GetLogRecord(ctx, "not-a-pointer") + assert.True(t, errors.IsInvalidArgument(err)) +} diff --git a/providers/aws/cloudwatchlogs/snapshot.go b/providers/aws/cloudwatchlogs/snapshot.go index 33fd1fc92..4a03d7780 100644 --- a/providers/aws/cloudwatchlogs/snapshot.go +++ b/providers/aws/cloudwatchlogs/snapshot.go @@ -22,6 +22,7 @@ type logsSnapshot struct { Groups map[string]*logGroupSnapshot `json:"groups,omitempty"` QueryResultKmsKey string `json:"queryResultKmsKey,omitempty"` FilterPeriods map[string]filterPeriod `json:"filterPeriods,omitempty"` + Queries json.RawMessage `json:"queries,omitempty"` } // logGroupSnapshot mirrors logGroup. Its metric- and subscription-filter stores @@ -52,6 +53,13 @@ func (m *Mock) Snapshot(_ context.Context, _ bool) (json.RawMessage, error) { snap := logsSnapshot{QueryResultKmsKey: m.queryResultKmsKey, FilterPeriods: maps.Clone(m.periods)} m.mu.RUnlock() + queries, err := m.queries.Snapshot() + if err != nil { + return nil, fmt.Errorf("cloudwatchlogs: snapshot queries: %w", err) + } + + snap.Queries = queries + if m.groups.Len() == 0 { return json.Marshal(snap) } @@ -116,6 +124,12 @@ func (m *Mock) Restore(_ context.Context, data json.RawMessage) error { maps.Copy(m.periods, snap.FilterPeriods) m.mu.Unlock() + if len(snap.Queries) > 0 { + if err := m.queries.LoadSnapshot(snap.Queries); err != nil { + return fmt.Errorf("cloudwatchlogs: restore queries: %w", err) + } + } + for name, gs := range snap.Groups { g, err := restoreGroup(gs) if err != nil { diff --git a/server/aws/cloudwatchlogs/handler.go b/server/aws/cloudwatchlogs/handler.go index b4b37c877..b9a240329 100644 --- a/server/aws/cloudwatchlogs/handler.go +++ b/server/aws/cloudwatchlogs/handler.go @@ -18,6 +18,7 @@ // PutSubscriptionFilter DescribeSubscriptionFilters DeleteSubscriptionFilter // PutRetentionPolicy DeleteRetentionPolicy ListLogGroups // AssociateKmsKey DisassociateKmsKey TestMetricFilter +// StartQuery GetQueryResults StopQuery DescribeQueries GetLogRecord GetLogGroupFields // TagResource / UntagResource / ListTagsForResource (and the legacy *LogGroup forms) // // CloudWatch Logs-only operations go through the optional driver.AWSLogging @@ -124,6 +125,27 @@ func (h *Handler) dispatchGroupSettings(w http.ResponseWriter, r *http.Request, h.associateKmsKey(w, r) case "DisassociateKmsKey": h.disassociateKmsKey(w, r) + default: + h.dispatchInsights(w, r, op) + } +} + +// dispatchInsights handles the Logs Insights operations, delegating tagging +// to dispatchTags. +func (h *Handler) dispatchInsights(w http.ResponseWriter, r *http.Request, op string) { + switch op { + case "StartQuery": + h.startQuery(w, r) + case "GetQueryResults": + h.getQueryResults(w, r) + case "StopQuery": + h.stopQuery(w, r) + case "DescribeQueries": + h.describeQueries(w, r) + case "GetLogRecord": + h.getLogRecord(w, r) + case "GetLogGroupFields": + h.getLogGroupFields(w, r) default: h.dispatchTags(w, r, op) } diff --git a/server/aws/cloudwatchlogs/insights.go b/server/aws/cloudwatchlogs/insights.go new file mode 100644 index 000000000..7328a4179 --- /dev/null +++ b/server/aws/cloudwatchlogs/insights.go @@ -0,0 +1,319 @@ +package cloudwatchlogs + +import ( + "errors" + "net/http" + "time" + + "github.com/stackshy/cloudemu/v2/server/wire" + logdriver "github.com/stackshy/cloudemu/v2/services/logging/driver" +) + +// Logs Insights paging limits. +const ( + maxQueryResultsPage = 10000 + maxDescribeQueries = 1000 + defaultDescribeLength = 1000 +) + +type startQueryRequest struct { + LogGroupName string `json:"logGroupName"` + LogGroupNames []string `json:"logGroupNames"` + LogGroupIdentifiers []string `json:"logGroupIdentifiers"` + StartTime int64 `json:"startTime"` + EndTime int64 `json:"endTime"` + QueryString string `json:"queryString"` + QueryLanguage string `json:"queryLanguage"` + Limit int `json:"limit"` +} + +// startQuery starts a Logs Insights query (Logs_20140328.StartQuery). The +// time range is in epoch seconds. +func (h *Handler) startQuery(w http.ResponseWriter, r *http.Request) { + ext, ok := h.awsLogs(w, "StartQuery") + if !ok { + return + } + + var req startQueryRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + id, err := ext.StartQuery(r.Context(), &logdriver.StartQueryInput{ + LogGroupName: req.LogGroupName, LogGroupNames: req.LogGroupNames, LogGroupIdentifiers: req.LogGroupIdentifiers, + StartTime: time.Unix(req.StartTime, 0).UTC(), EndTime: time.Unix(req.EndTime, 0).UTC(), + QueryString: req.QueryString, QueryLanguage: req.QueryLanguage, Limit: req.Limit, + }) + if err != nil { + writeQueryErr(w, err) + return + } + + wire.WriteJSON(w, map[string]string{"queryId": id}) +} + +// writeQueryErr reports a query that does not compile as +// MalformedQueryException with its queryCompileError location, and anything +// else as writeErr does. +func writeQueryErr(w http.ResponseWriter, err error) { + var mq *logdriver.MalformedQueryError + if !errors.As(err, &mq) { + writeErr(w, err) + return + } + + wire.WriteJSONErrorFields(w, http.StatusBadRequest, "MalformedQueryException", mq.Message, map[string]any{ + "queryCompileError": map[string]any{ + "location": map[string]int{"startCharOffset": mq.Start, "endCharOffset": mq.End}, + "message": mq.Message, + }, + }) +} + +type resultFieldJSON struct { + Field string `json:"field"` + Value string `json:"value"` +} + +type queryStatisticsJSON struct { + RecordsMatched float64 `json:"recordsMatched"` + RecordsScanned float64 `json:"recordsScanned"` + BytesScanned float64 `json:"bytesScanned"` + LogGroupsScanned float64 `json:"logGroupsScanned"` +} + +type getQueryResultsResponse struct { + Results [][]resultFieldJSON `json:"results"` + Statistics queryStatisticsJSON `json:"statistics"` + Status string `json:"status"` + QueryLanguage string `json:"queryLanguage,omitempty"` + EncryptionKey string `json:"encryptionKey,omitempty"` + NextToken string `json:"nextToken,omitempty"` +} + +func (h *Handler) getQueryResults(w http.ResponseWriter, r *http.Request) { + ext, ok := h.awsLogs(w, "GetQueryResults") + if !ok { + return + } + + var req struct { + QueryID string `json:"queryId"` + MaxItems int32 `json:"maxItems"` + NextToken string `json:"nextToken"` + } + + if !wire.DecodeJSON(w, r, &req) { + return + } + + if req.MaxItems < 0 || req.MaxItems > maxQueryResultsPage { + wire.WriteJSONError(w, http.StatusBadRequest, "InvalidParameterException", + "1 validation error detected: Value at 'maxItems' failed to satisfy constraint: "+ + "Member must have value between 0 and 10000") + + return + } + + res, err := ext.GetQueryResults(r.Context(), req.QueryID) + if err != nil { + writeErr(w, err) + return + } + + limit := req.MaxItems + if limit == 0 { + limit = maxQueryResultsPage + } + + from, to, next := pageBounds(len(res.Results), decodeOffsetToken(req.NextToken), limit) + + resp := getQueryResultsResponse{ + Results: make([][]resultFieldJSON, 0, to-from), Status: res.Status, + QueryLanguage: res.QueryLanguage, EncryptionKey: res.EncryptionKey, + Statistics: queryStatisticsJSON{ + RecordsMatched: res.Statistics.RecordsMatched, RecordsScanned: res.Statistics.RecordsScanned, + BytesScanned: res.Statistics.BytesScanned, LogGroupsScanned: res.Statistics.LogGroupsScanned, + }, + } + + for _, row := range res.Results[from:to] { + out := make([]resultFieldJSON, 0, len(row)) + for _, f := range row { + out = append(out, resultFieldJSON{Field: f.Field, Value: f.Value}) + } + + resp.Results = append(resp.Results, out) + } + + if next > 0 { + resp.NextToken = encodeOffsetToken(next) + } + + wire.WriteJSON(w, resp) +} + +func (h *Handler) stopQuery(w http.ResponseWriter, r *http.Request) { + ext, ok := h.awsLogs(w, "StopQuery") + if !ok { + return + } + + var req struct { + QueryID string `json:"queryId"` + } + + if !wire.DecodeJSON(w, r, &req) { + return + } + + if err := ext.StopQuery(r.Context(), req.QueryID); err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, map[string]bool{"success": true}) +} + +type queryInfoJSON struct { + QueryID string `json:"queryId"` + QueryString string `json:"queryString"` + QueryLanguage string `json:"queryLanguage,omitempty"` + Status string `json:"status"` + CreateTime int64 `json:"createTime"` + LogGroupName string `json:"logGroupName,omitempty"` + QueryDuration int64 `json:"queryDuration,omitempty"` + BytesScanned float64 `json:"bytesScanned"` +} + +func (h *Handler) describeQueries(w http.ResponseWriter, r *http.Request) { + ext, ok := h.awsLogs(w, "DescribeQueries") + if !ok { + return + } + + var req struct { + LogGroupName string `json:"logGroupName"` + Status string `json:"status"` + QueryLanguage string `json:"queryLanguage"` + MaxResults int32 `json:"maxResults"` + NextToken string `json:"nextToken"` + } + + if !wire.DecodeJSON(w, r, &req) { + return + } + + if req.MaxResults < 0 || req.MaxResults > maxDescribeQueries { + wire.WriteJSONError(w, http.StatusBadRequest, "InvalidParameterException", + "1 validation error detected: Value at 'maxResults' failed to satisfy constraint: "+ + "Member must have value between 1 and 1000") + + return + } + + infos, err := ext.DescribeQueries(r.Context(), logdriver.DescribeQueriesInput{ + LogGroupName: req.LogGroupName, Status: req.Status, QueryLanguage: req.QueryLanguage, + }) + if err != nil { + writeErr(w, err) + return + } + + limit := req.MaxResults + if limit == 0 { + limit = defaultDescribeLength + } + + from, to, next := pageBounds(len(infos), decodeOffsetToken(req.NextToken), limit) + + out := make([]queryInfoJSON, 0, to-from) + for _, q := range infos[from:to] { + out = append(out, queryInfoJSON{ + QueryID: q.QueryID, QueryString: q.QueryString, QueryLanguage: q.QueryLanguage, Status: q.Status, + CreateTime: epochMillis(q.CreateTime), LogGroupName: q.LogGroupName, + QueryDuration: q.QueryDuration, BytesScanned: q.BytesScanned, + }) + } + + resp := map[string]any{"queries": out} + if next > 0 { + resp["nextToken"] = encodeOffsetToken(next) + } + + wire.WriteJSON(w, resp) +} + +func (h *Handler) getLogRecord(w http.ResponseWriter, r *http.Request) { + ext, ok := h.awsLogs(w, "GetLogRecord") + if !ok { + return + } + + var req struct { + LogRecordPointer string `json:"logRecordPointer"` + } + + if !wire.DecodeJSON(w, r, &req) { + return + } + + rec, err := ext.GetLogRecord(r.Context(), req.LogRecordPointer) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, map[string]any{"logRecord": rec}) +} + +type logGroupFieldJSON struct { + Name string `json:"name"` + Percent int `json:"percent"` +} + +// getLogGroupFields lists a log group's fields (Logs_20140328.GetLogGroupFields). +// Exactly one of logGroupName or logGroupIdentifier names the group; time is +// in epoch seconds. +func (h *Handler) getLogGroupFields(w http.ResponseWriter, r *http.Request) { + ext, ok := h.awsLogs(w, "GetLogGroupFields") + if !ok { + return + } + + var req struct { + LogGroupName string `json:"logGroupName"` + LogGroupIdentifier string `json:"logGroupIdentifier"` + Time int64 `json:"time"` + } + + if !wire.DecodeJSON(w, r, &req) { + return + } + + if (req.LogGroupName == "") == (req.LogGroupIdentifier == "") { + wire.WriteJSONError(w, http.StatusBadRequest, "InvalidParameterException", + "exactly one of logGroupName or logGroupIdentifier must be specified") + + return + } + + var at time.Time + if req.Time > 0 { + at = time.Unix(req.Time, 0).UTC() + } + + fields, err := ext.GetLogGroupFields(r.Context(), logGroupName(req.LogGroupIdentifier, req.LogGroupName), at) + if err != nil { + writeErr(w, err) + return + } + + out := make([]logGroupFieldJSON, 0, len(fields)) + for _, f := range fields { + out = append(out, logGroupFieldJSON{Name: f.Name, Percent: f.Percent}) + } + + wire.WriteJSON(w, map[string]any{"logGroupFields": out}) +} diff --git a/server/aws/cloudwatchlogs/insights_sdk_test.go b/server/aws/cloudwatchlogs/insights_sdk_test.go new file mode 100644 index 000000000..b90a6f52e --- /dev/null +++ b/server/aws/cloudwatchlogs/insights_sdk_test.go @@ -0,0 +1,137 @@ +package cloudwatchlogs_test + +import ( + "context" + "errors" + "testing" + "time" + + "github.com/aws/aws-sdk-go-v2/aws" + cwl "github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs" + cwltypes "github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs/types" +) + +func resultValue(row []cwltypes.ResultField, name string) string { + for _, f := range row { + if aws.ToString(f.Field) == name { + return aws.ToString(f.Value) + } + } + + return "" +} + +func TestSDKLogsInsights(t *testing.T) { + c := newLogsClient(t) + ctx := context.Background() + g, s := aws.String("/insights/g"), aws.String("s") + + if _, err := c.CreateLogGroup(ctx, &cwl.CreateLogGroupInput{LogGroupName: g}); err != nil { + t.Fatalf("CreateLogGroup: %v", err) + } + + if _, err := c.CreateLogStream(ctx, &cwl.CreateLogStreamInput{LogGroupName: g, LogStreamName: s}); err != nil { + t.Fatalf("CreateLogStream: %v", err) + } + + now := time.Now() + msgs := []string{`{"level":"ERROR","latency":120}`, `{"level":"INFO","latency":15}`, "plain ERROR Exception"} + + events := make([]cwltypes.InputLogEvent, 0, len(msgs)) + for i, m := range msgs { + events = append(events, cwltypes.InputLogEvent{ + Timestamp: aws.Int64(now.Add(time.Duration(i) * time.Millisecond).UnixMilli()), Message: aws.String(m), + }) + } + + if _, err := c.PutLogEvents(ctx, &cwl.PutLogEventsInput{LogGroupName: g, LogStreamName: s, LogEvents: events}); err != nil { + t.Fatalf("PutLogEvents: %v", err) + } + + start, err := c.StartQuery(ctx, &cwl.StartQueryInput{ + LogGroupName: g, StartTime: aws.Int64(now.Add(-time.Hour).Unix()), EndTime: aws.Int64(now.Unix() + 1), + QueryString: aws.String("fields @timestamp, level, latency | filter latency > 10 | sort latency desc"), + }) + if err != nil { + t.Fatalf("StartQuery: %v", err) + } + + // Poll the way real clients do: until the query leaves Running. + var res *cwl.GetQueryResultsOutput + for range 5 { + res, err = c.GetQueryResults(ctx, &cwl.GetQueryResultsInput{QueryId: start.QueryId}) + if err != nil { + t.Fatalf("GetQueryResults: %v", err) + } + + if res.Status != cwltypes.QueryStatusRunning && res.Status != cwltypes.QueryStatusScheduled { + break + } + } + + if res.Status != cwltypes.QueryStatusComplete || len(res.Results) != 2 { + t.Fatalf("results = %s, %d rows", res.Status, len(res.Results)) + } + + if resultValue(res.Results[0], "latency") != "120" || resultValue(res.Results[0], "@ptr") == "" { + t.Fatalf("first row = %+v", res.Results[0]) + } + + if res.Statistics.RecordsMatched != 2 || res.Statistics.RecordsScanned != 3 || res.Statistics.BytesScanned <= 0 { + t.Fatalf("statistics = %+v", res.Statistics) + } + + rec, err := c.GetLogRecord(ctx, &cwl.GetLogRecordInput{LogRecordPointer: aws.String(resultValue(res.Results[0], "@ptr"))}) + if err != nil || rec.LogRecord["@message"] != msgs[0] || rec.LogRecord["level"] != "ERROR" { + t.Fatalf("GetLogRecord = %+v, %v", rec, err) + } + + desc, err := c.DescribeQueries(ctx, &cwl.DescribeQueriesInput{Status: cwltypes.QueryStatusComplete, LogGroupName: g}) + if err != nil || len(desc.Queries) != 1 || aws.ToString(desc.Queries[0].QueryId) != aws.ToString(start.QueryId) { + t.Fatalf("DescribeQueries = %+v, %v", desc, err) + } + + var invalid *cwltypes.InvalidParameterException + if _, err := c.StopQuery(ctx, &cwl.StopQueryInput{QueryId: start.QueryId}); !errors.As(err, &invalid) { + t.Fatalf("StopQuery on a finished query: got %v, want InvalidParameterException", err) + } + + fresh, err := c.StartQuery(ctx, &cwl.StartQueryInput{ + LogGroupName: g, StartTime: aws.Int64(now.Add(-time.Hour).Unix()), EndTime: aws.Int64(now.Unix() + 1), + QueryString: aws.String("fields @message"), + }) + if err != nil { + t.Fatalf("StartQuery: %v", err) + } + + stop, err := c.StopQuery(ctx, &cwl.StopQueryInput{QueryId: fresh.QueryId}) + if err != nil || !stop.Success { + t.Fatalf("StopQuery = %+v, %v", stop, err) + } + + cancelled, err := c.GetQueryResults(ctx, &cwl.GetQueryResultsInput{QueryId: fresh.QueryId}) + if err != nil || cancelled.Status != cwltypes.QueryStatusCancelled { + t.Fatalf("after StopQuery = %+v, %v", cancelled, err) + } + + fields, err := c.GetLogGroupFields(ctx, &cwl.GetLogGroupFieldsInput{LogGroupName: g, Time: aws.Int64(now.Unix())}) + if err != nil || len(fields.LogGroupFields) == 0 || aws.ToString(fields.LogGroupFields[0].Name) == "" { + t.Fatalf("GetLogGroupFields = %+v, %v", fields, err) + } + + var mq *cwltypes.MalformedQueryException + if _, err := c.StartQuery(ctx, &cwl.StartQueryInput{ + LogGroupName: g, StartTime: aws.Int64(now.Add(-time.Hour).Unix()), EndTime: aws.Int64(now.Unix()), + QueryString: aws.String("fields @message | filter latency >"), + }); !errors.As(err, &mq) || mq.QueryCompileError == nil || mq.QueryCompileError.Location == nil { + t.Fatalf("malformed query: got %v, want MalformedQueryException with a location", err) + } + + var nf *cwltypes.ResourceNotFoundException + if _, err := c.StartQuery(ctx, &cwl.StartQueryInput{ + LogGroupName: aws.String("/insights/none"), StartTime: aws.Int64(0), EndTime: aws.Int64(now.Unix()), + QueryString: aws.String("fields @message"), + }); !errors.As(err, &nf) { + t.Fatalf("missing group: got %v, want ResourceNotFoundException", err) + } +} diff --git a/services/logging/driver/aws.go b/services/logging/driver/aws.go index f279e304c..31b93c984 100644 --- a/services/logging/driver/aws.go +++ b/services/logging/driver/aws.go @@ -1,6 +1,9 @@ package driver -import "context" +import ( + "context" + "time" +) // Log group classes CloudWatch Logs accepts on CreateLogGroup. A group's class // is fixed at creation; no API changes it afterwards. @@ -74,6 +77,81 @@ type DescribeMetricFiltersInput struct { MetricNamespace string } +// Logs Insights query statuses. +const ( + QueryStatusRunning = "Running" + QueryStatusComplete = "Complete" + QueryStatusCancelled = "Cancelled" //nolint:misspell // the API's status value is spelled this way. + QueryStatusTimeout = "Timeout" +) + +// StartQueryInput is a Logs Insights StartQuery request. Exactly one of +// LogGroupName, LogGroupNames or LogGroupIdentifiers (names or ARNs) is set. +type StartQueryInput struct { + LogGroupName string + LogGroupNames []string + LogGroupIdentifiers []string + StartTime, EndTime time.Time + QueryString string + QueryLanguage string + // Limit caps the rows returned; zero means the default. + Limit int +} + +// QueryResultField is one field/value pair of a query result row. +type QueryResultField struct{ Field, Value string } + +// QueryStatistics are a query's scan and match counts. +type QueryStatistics struct { + RecordsMatched float64 + RecordsScanned float64 + BytesScanned float64 + LogGroupsScanned float64 +} + +// QueryResults is a GetQueryResults response. +type QueryResults struct { + Status string + QueryLanguage string + EncryptionKey string + Results [][]QueryResultField + Statistics QueryStatistics +} + +// QueryInfo describes a query for DescribeQueries. +type QueryInfo struct { + QueryID string + QueryString string + QueryLanguage string + Status string + LogGroupName string + CreateTime time.Time + // QueryDuration is how long the query ran, in milliseconds. + QueryDuration int64 + BytesScanned float64 +} + +// DescribeQueriesInput narrows DescribeQueries; empty fields match all. +type DescribeQueriesInput struct { + LogGroupName, Status, QueryLanguage string +} + +// LogGroupField is a field GetLogGroupFields found and the share of events +// (0-100) that carry it. +type LogGroupField struct { + Name string + Percent int +} + +// MalformedQueryError is a query string that does not compile. Start and End +// are the character offsets of the offending text. +type MalformedQueryError struct { + Message string + Start, End int +} + +func (e *MalformedQueryError) Error() string { return e.Message } + // AWSLogging is an optional interface implemented by the AWS CloudWatch Logs // backend for operations with no cross-provider equivalent. The CloudWatch // Logs wire handler type-asserts for it; Azure, GCP and OCI backends do not @@ -96,6 +174,18 @@ type AWSLogging interface { // DescribeAWSMetricFilters lists metric filters, sorted by filter name. DescribeAWSMetricFilters(ctx context.Context, input DescribeMetricFiltersInput) ([]MetricFilterInfo, error) + // Logs Insights. A query is Running until its first GetQueryResults, + // which runs it. StartQuery returns a *MalformedQueryError for a query + // string that does not compile. + StartQuery(ctx context.Context, input *StartQueryInput) (string, error) + GetQueryResults(ctx context.Context, queryID string) (*QueryResults, error) + StopQuery(ctx context.Context, queryID string) error + DescribeQueries(ctx context.Context, input DescribeQueriesInput) ([]QueryInfo, error) + GetLogRecord(ctx context.Context, logRecordPointer string) (map[string]string, error) + // GetLogGroupFields samples the 8 minutes either side of at, or the last + // 15 minutes when at is zero. + GetLogGroupFields(ctx context.Context, logGroup string, at time.Time) ([]LogGroupField, error) + // TestMetricFilter runs a filter pattern over sample log messages. TestMetricFilter(ctx context.Context, filterPattern string, logEventMessages []string) ([]MetricFilterMatch, error) } From c93c3f2ef165637bacf0189c0deed93e4dd8f92d Mon Sep 17 00:00:00 2001 From: Nitin Kumar Patel Date: Thu, 22 Oct 2026 07:09:43 +0530 Subject: [PATCH 09/31] fix(aws-eventbridge): connections and API destinations backed by Secrets Manager (#1662) * feat(eventbridge): connections and API destinations Add CreateConnection, DescribeConnection, UpdateConnection, DeauthorizeConnection, DeleteConnection and ListConnections for BASIC, API_KEY and OAUTH_CLIENT_CREDENTIALS, each backed by a real Secrets Manager secret that is updated with the credentials and deleted with the connection. Add CreateApiDestination, DescribeApiDestination, UpdateApiDestination, DeleteApiDestination and ListApiDestinations, whose state follows the connection's authorization. Both are included in snapshots. * fix(eventbridge): keep connection credentials out of validation errors --- providers/aws/aws.go | 3 + providers/aws/eventbridge/api_destinations.go | 262 +++++++++++ .../aws/eventbridge/api_destinations_test.go | 123 ++++++ providers/aws/eventbridge/archives.go | 2 +- providers/aws/eventbridge/connection_auth.go | 409 ++++++++++++++++++ .../connection_secrets_redaction_test.go | 150 +++++++ providers/aws/eventbridge/connections.go | 362 ++++++++++++++++ providers/aws/eventbridge/connections_test.go | 336 ++++++++++++++ providers/aws/eventbridge/eventbridge.go | 11 +- providers/aws/eventbridge/snapshot.go | 64 ++- providers/aws/eventbridge/snapshot_test.go | 37 ++ providers/aws/eventbridge/validation.go | 61 +++ server/aws/eventbridge/api_destinations.go | 170 ++++++++ server/aws/eventbridge/archives.go | 23 +- server/aws/eventbridge/connections.go | 392 +++++++++++++++++ .../aws/eventbridge/connections_sdk_test.go | 174 ++++++++ server/aws/eventbridge/handler.go | 7 +- services/eventbus/driver/connection.go | 145 +++++++ 18 files changed, 2706 insertions(+), 25 deletions(-) create mode 100644 providers/aws/eventbridge/api_destinations.go create mode 100644 providers/aws/eventbridge/api_destinations_test.go create mode 100644 providers/aws/eventbridge/connection_auth.go create mode 100644 providers/aws/eventbridge/connection_secrets_redaction_test.go create mode 100644 providers/aws/eventbridge/connections.go create mode 100644 providers/aws/eventbridge/connections_test.go create mode 100644 server/aws/eventbridge/api_destinations.go create mode 100644 server/aws/eventbridge/connections.go create mode 100644 server/aws/eventbridge/connections_sdk_test.go create mode 100644 services/eventbus/driver/connection.go diff --git a/providers/aws/aws.go b/providers/aws/aws.go index c8e0336f1..2faf11dd6 100644 --- a/providers/aws/aws.go +++ b/providers/aws/aws.go @@ -546,6 +546,9 @@ func newProvider(o *config.Options, shared *GlobalServices) *Provider { // mocks. It is split out of New so the factory stays within the function-length // budget; the wiring-parity test scans this helper as if it were New(). func wirePostBuildServices(o *config.Options, p *Provider) { + // EventBridge connections keep their credentials in a real Secrets Manager + // secret (events!connection//), as AWS does. + p.EventBridge.SetConnectionSecretStore(p.SecretsManager) // S3 event notifications deliver to their configured targets: SQS queues, // SNS topics, and Lambda functions. p.S3.SetSQSDeliverer(p.SQS) diff --git a/providers/aws/eventbridge/api_destinations.go b/providers/aws/eventbridge/api_destinations.go new file mode 100644 index 000000000..24513ea7e --- /dev/null +++ b/providers/aws/eventbridge/api_destinations.go @@ -0,0 +1,262 @@ +package eventbridge + +import ( + "context" + "fmt" + "regexp" + "sort" + "strings" + + "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/internal/idgen" + "github.com/stackshy/cloudemu/v2/services/eventbus/driver" +) + +const ( + destStateActive = "ACTIVE" + destStateInactive = "INACTIVE" + + // defaultInvocationRate is the InvocationRateLimitPerSecond an API + // destination gets when the request omits it. + defaultInvocationRate = 300 + + connectionARNPattern = `^arn:aws([a-z]|\-)*:events:([a-z]|\d|\-)*:([0-9]{12})?:connection\/[\.\-_A-Za-z0-9]+\/[\-A-Za-z0-9]+$` + httpsEndpointPattern = `^((%[0-9A-Fa-f]{2}|[-()_.!~*';/?:@\x26=+$,A-Za-z0-9])+)([).!';/?:,])?$` +) + +var ( + connectionARNRE = regexp.MustCompile(connectionARNPattern) + httpsEndpointRE = regexp.MustCompile(httpsEndpointPattern) +) + +func apiDestinationMethods() []string { + return []string{httpPOST, httpGET, "HEAD", "OPTIONS", httpPUT, "PATCH", "DELETE"} +} + +func errDestinationNotExist(name string) error { + return errors.Newf(errors.NotFound, "An api-destination '%s' does not exist.", name) +} + +// destinationFields checks the fields an API destination request sets, in the +// order of the API's members. +func destinationFields(v *violations, connARN, endpoint, method *string, rate *int) { + if connARN != nil { + v.pattern(*connARN, "connectionArn", connectionARNRE, connectionARNPattern) + } + + if endpoint != nil { + v.required(*endpoint, "invocationEndpoint", maxEndpointLen) + v.pattern(*endpoint, "invocationEndpoint", httpsEndpointRE, httpsEndpointPattern) + } + + if method != nil { + v.enum(*method, "httpMethod", apiDestinationMethods()) + } + + if rate != nil && *rate < 1 { + v.add(fmt.Sprint(*rate), "invocationRateLimitPerSecond", "Member must have value greater than or equal to 1") + } +} + +// connectionNameFromARN returns the connection name an ARN of the form +// arn:...:connection// names. +func connectionNameFromARN(arn string) string { + const infix = ":connection/" + + idx := strings.Index(arn, infix) + if idx == -1 { + return "" + } + + rest := arn[idx+len(infix):] + if slash := strings.Index(rest, "/"); slash != -1 { + return rest[:slash] + } + + return rest +} + +// requireConnection checks that a connection with exactly this ARN exists. +func (m *Mock) requireConnection(arn string) error { + name := connectionNameFromARN(arn) + + cd, ok := m.connections.Get(name) + if !ok || cd.Info.ARN != arn { + return errConnectionNotExist(name) + } + + return nil +} + +// destinationState reports ACTIVE when the destination's connection exists and +// is authorized, INACTIVE otherwise. settle lets a read move the connection out +// of a transient state first. +func (m *Mock) destinationState(connARN string, settle bool) string { + name := connectionNameFromARN(connARN) + + var ( + cd connectionData + ok bool + ) + + if settle { + cd, ok = m.settleConnection(name) + } else { + cd, ok = m.connections.Get(name) + } + + if ok && cd.Info.ARN == connARN && cd.Info.State == connStateAuthorized { + return destStateActive + } + + return destStateInactive +} + +// CreateAPIDestination creates an API destination that calls an HTTP endpoint +// through an existing connection. +func (m *Mock) CreateAPIDestination(_ context.Context, cfg *driver.APIDestinationConfig) (*driver.APIDestination, error) { + var v violations + + v.name(cfg.Name, "name", maxConnectionNameLen) + v.maxLen(cfg.Description, "description", maxDescriptionLen) + destinationFields(&v, &cfg.ConnectionARN, &cfg.InvocationEndpoint, &cfg.HTTPMethod, cfg.InvocationRateLimitPerSecond) + + if err := v.err(); err != nil { + return nil, err + } + + if err := m.requireConnection(cfg.ConnectionARN); err != nil { + return nil, err + } + + rate := defaultInvocationRate + if cfg.InvocationRateLimitPerSecond != nil { + rate = *cfg.InvocationRateLimitPerSecond + } + + now := m.opts.Clock.Now().UTC() + d := driver.APIDestination{ + Name: cfg.Name, + ARN: idgen.AWSARN("events", m.opts.Region, m.opts.AccountID, "api-destination/"+cfg.Name+"/"+idgen.UUID()), + Description: cfg.Description, + ConnectionARN: cfg.ConnectionARN, + InvocationEndpoint: cfg.InvocationEndpoint, + HTTPMethod: cfg.HTTPMethod, + InvocationRateLimitPerSecond: rate, + CreatedAt: now, + LastModifiedAt: now, + } + + if !m.apiDestinations.SetIfAbsent(cfg.Name, d) { + return nil, errors.Newf(errors.AlreadyExists, "An api-destination '%s' already exists.", cfg.Name) + } + + d.State = m.destinationState(d.ConnectionARN, false) + + return &d, nil +} + +// DescribeAPIDestination returns an API destination and its current state. +func (m *Mock) DescribeAPIDestination(_ context.Context, name string) (*driver.APIDestination, error) { + d, ok := m.apiDestinations.Get(name) + if !ok { + return nil, errors.Newf(errors.NotFound, + "Failed to describe the api-destination(s). An api-destination '%s' does not exist.", name) + } + + d.State = m.destinationState(d.ConnectionARN, true) + + return &d, nil +} + +// UpdateAPIDestination changes the fields of an API destination that the +// request sets. +func (m *Mock) UpdateAPIDestination(_ context.Context, upd *driver.APIDestinationUpdate) (*driver.APIDestination, error) { + var v violations + + if upd.Description != nil { + v.maxLen(*upd.Description, "description", maxDescriptionLen) + } + + destinationFields(&v, upd.ConnectionARN, upd.InvocationEndpoint, upd.HTTPMethod, upd.InvocationRateLimitPerSecond) + + if err := v.err(); err != nil { + return nil, err + } + + if upd.ConnectionARN != nil { + if err := m.requireConnection(*upd.ConnectionARN); err != nil { + return nil, err + } + } + + var out driver.APIDestination + + now := m.opts.Clock.Now().UTC() + + if !m.apiDestinations.Update(upd.Name, func(d driver.APIDestination) driver.APIDestination { + applyDestinationUpdate(&d, upd) + d.LastModifiedAt = now + out = d + + return d + }) { + return nil, errDestinationNotExist(upd.Name) + } + + out.State = m.destinationState(out.ConnectionARN, true) + + return &out, nil +} + +func applyDestinationUpdate(d *driver.APIDestination, upd *driver.APIDestinationUpdate) { + if upd.Description != nil { + d.Description = *upd.Description + } + + if upd.ConnectionARN != nil { + d.ConnectionARN = *upd.ConnectionARN + } + + if upd.InvocationEndpoint != nil { + d.InvocationEndpoint = *upd.InvocationEndpoint + } + + if upd.HTTPMethod != nil { + d.HTTPMethod = *upd.HTTPMethod + } + + if upd.InvocationRateLimitPerSecond != nil { + d.InvocationRateLimitPerSecond = *upd.InvocationRateLimitPerSecond + } +} + +// DeleteAPIDestination deletes an API destination. +func (m *Mock) DeleteAPIDestination(_ context.Context, name string) error { + if !m.apiDestinations.Delete(name) { + return errDestinationNotExist(name) + } + + return nil +} + +// ListAPIDestinations lists API destinations sorted by name, narrowed by name +// prefix and connection. +func (m *Mock) ListAPIDestinations(_ context.Context, filter driver.APIDestinationFilter) ([]driver.APIDestination, error) { + all := m.apiDestinations.All() + out := make([]driver.APIDestination, 0, len(all)) + + for name := range all { + d := all[name] + if !strings.HasPrefix(d.Name, filter.NamePrefix) || (filter.ConnectionARN != "" && d.ConnectionARN != filter.ConnectionARN) { + continue + } + + d.State = m.destinationState(d.ConnectionARN, true) + out = append(out, d) + } + + sort.Slice(out, func(i, j int) bool { return out[i].Name < out[j].Name }) + + return out, nil +} diff --git a/providers/aws/eventbridge/api_destinations_test.go b/providers/aws/eventbridge/api_destinations_test.go new file mode 100644 index 000000000..61b9894c1 --- /dev/null +++ b/providers/aws/eventbridge/api_destinations_test.go @@ -0,0 +1,123 @@ +package eventbridge + +import ( + "context" + "testing" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/services/eventbus/driver" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func destinationConfig(name, connARN string) *driver.APIDestinationConfig { + return &driver.APIDestinationConfig{ + Name: name, ConnectionARN: connARN, InvocationEndpoint: "https://example.com/hook", HTTPMethod: "POST", + } +} + +func TestAPIDestinationLifecycle(t *testing.T) { + m, _ := newConnectionMock(t) + ctx := context.Background() + + c, err := m.CreateConnection(ctx, basicConnection("c1")) + require.NoError(t, err) + + d, err := m.CreateAPIDestination(ctx, destinationConfig("d1", c.ARN)) + require.NoError(t, err) + assert.Regexp(t, `^arn:aws:events:us-east-1:123456789012:api-destination/d1/[-0-9a-f]{36}$`, d.ARN) + assert.Equal(t, "ACTIVE", d.State) + assert.Equal(t, 300, d.InvocationRateLimitPerSecond, "the rate limit defaults to 300") + + _, err = m.CreateAPIDestination(ctx, destinationConfig("d1", c.ARN)) + require.True(t, cerrors.IsAlreadyExists(err)) + assert.Equal(t, "An api-destination 'd1' already exists.", cerrors.Message(err)) + + method, rate, desc := "PUT", 5, "v2" + out, err := m.UpdateAPIDestination(ctx, &driver.APIDestinationUpdate{ + Name: "d1", HTTPMethod: &method, InvocationRateLimitPerSecond: &rate, Description: &desc, + }) + require.NoError(t, err) + assert.Equal(t, "ACTIVE", out.State) + + got, err := m.DescribeAPIDestination(ctx, "d1") + require.NoError(t, err) + assert.Equal(t, "PUT", got.HTTPMethod) + assert.Equal(t, 5, got.InvocationRateLimitPerSecond) + assert.Equal(t, "v2", got.Description) + + list, err := m.ListAPIDestinations(ctx, driver.APIDestinationFilter{ConnectionARN: c.ARN}) + require.NoError(t, err) + assert.Len(t, list, 1) + + require.NoError(t, m.DeleteAPIDestination(ctx, "d1")) + + _, err = m.DescribeAPIDestination(ctx, "d1") + require.True(t, cerrors.IsNotFound(err)) + assert.Equal(t, "Failed to describe the api-destination(s). An api-destination 'd1' does not exist.", cerrors.Message(err)) + + err = m.DeleteAPIDestination(ctx, "d1") + require.True(t, cerrors.IsNotFound(err)) + assert.Equal(t, "An api-destination 'd1' does not exist.", cerrors.Message(err)) +} + +func TestAPIDestinationStateFollowsConnection(t *testing.T) { + m, _ := newConnectionMock(t) + ctx := context.Background() + + o, err := m.CreateConnection(ctx, &driver.ConnectionConfig{ + Name: "oauth", AuthorizationType: "OAUTH_CLIENT_CREDENTIALS", + AuthParameters: &driver.ConnectionAuthParameters{OAuth: &driver.ConnectionOAuth{ + AuthorizationEndpoint: "https://auth.example.com/token", HTTPMethod: "POST", + ClientParameters: &driver.ConnectionOAuthClient{ClientID: "id", ClientSecret: "s"}, + }}, + }) + require.NoError(t, err) + + d, err := m.CreateAPIDestination(ctx, destinationConfig("d", o.ARN)) + require.NoError(t, err) + assert.Equal(t, "INACTIVE", d.State, "a destination on an AUTHORIZING connection starts INACTIVE") + + got, err := m.DescribeAPIDestination(ctx, "d") + require.NoError(t, err) + assert.Equal(t, "ACTIVE", got.State) + + _, err = m.DeauthorizeConnection(ctx, "oauth") + require.NoError(t, err) + + got, err = m.DescribeAPIDestination(ctx, "d") + require.NoError(t, err) + assert.Equal(t, "INACTIVE", got.State, "a deauthorized connection makes the destination INACTIVE") + + _, err = m.DeleteConnection(ctx, "oauth") + require.NoError(t, err) + + got, err = m.DescribeAPIDestination(ctx, "d") + require.NoError(t, err) + assert.Equal(t, "INACTIVE", got.State) +} + +func TestCreateAPIDestinationValidation(t *testing.T) { + m, _ := newConnectionMock(t) + ctx := context.Background() + + _, err := m.CreateAPIDestination(ctx, &driver.APIDestinationConfig{ + Name: "d", ConnectionARN: "invalid-connection-arn", InvocationEndpoint: "https://example.com", HTTPMethod: "INVALID_METHOD", + }) + require.True(t, cerrors.IsInvalidArgument(err)) + assert.Equal(t, "2 validation errors detected: Value 'invalid-connection-arn' at 'connectionArn' failed to satisfy constraint: "+ + "Member must satisfy regular expression pattern: "+connectionARNPattern+"; Value 'INVALID_METHOD' at 'httpMethod' failed to "+ + "satisfy constraint: Member must satisfy enum value set: [POST, GET, HEAD, OPTIONS, PUT, PATCH, DELETE]", cerrors.Message(err)) + + missing := "arn:aws:events:us-east-1:123456789012:connection/ghost/0b8c0e5c-5c3f-4c0e-9d1e-3f6a2b1c9d8e" + _, err = m.CreateAPIDestination(ctx, destinationConfig("d", missing)) + require.True(t, cerrors.IsNotFound(err)) + assert.Equal(t, "Connection 'ghost' does not exist.", cerrors.Message(err)) + + zero := 0 + cfg := destinationConfig("d", missing) + cfg.InvocationRateLimitPerSecond = &zero + + _, err = m.CreateAPIDestination(ctx, cfg) + require.True(t, cerrors.IsInvalidArgument(err)) +} diff --git a/providers/aws/eventbridge/archives.go b/providers/aws/eventbridge/archives.go index 6b1d99a69..8e077e10f 100644 --- a/providers/aws/eventbridge/archives.go +++ b/providers/aws/eventbridge/archives.go @@ -26,7 +26,7 @@ const ( // archiveStates is the ArchiveState enum, in the order EventBridge lists it. func archiveStates() []string { - return []string{"ENABLED", "DISABLED", "CREATING", "UPDATING", "CREATE_FAILED", "UPDATE_FAILED"} + return []string{archiveStateEnabled, "DISABLED", stateCreating, stateUpdating, "CREATE_FAILED", "UPDATE_FAILED"} } // archivedEvent is one event held by an archive, with the time it was archived diff --git a/providers/aws/eventbridge/connection_auth.go b/providers/aws/eventbridge/connection_auth.go new file mode 100644 index 000000000..ceaac022b --- /dev/null +++ b/providers/aws/eventbridge/connection_auth.go @@ -0,0 +1,409 @@ +package eventbridge + +import ( + "encoding/json" + "fmt" + + "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/services/eventbus/driver" +) + +const ( + authTypeBasic = "BASIC" + authTypeOAuth = "OAUTH_CLIENT_CREDENTIALS" + authTypeAPIKey = "API_KEY" + + maxConnectionNameLen = 64 + maxAuthValueLen = 512 + maxHTTPParameters = 100 + maxEndpointLen = 2048 +) + +func authorizationTypes() []string { + return []string{authTypeBasic, authTypeOAuth, authTypeAPIKey} +} + +func oauthHTTPMethods() []string { + return []string{httpGET, httpPOST, httpPUT} +} + +// msgAuthParametersNull is AWS's reply to a CreateConnection without +// AuthParameters. +const msgAuthParametersNull = "1 validation error detected: Value null at 'authParameters' failed to satisfy constraint: " + + "Member must not be null" + +func validateConnectionConfig(cfg *driver.ConnectionConfig) error { + var v violations + + v.name(cfg.Name, "name", maxConnectionNameLen) + v.maxLen(cfg.Description, "description", maxDescriptionLen) + v.enum(cfg.AuthorizationType, "authorizationType", authorizationTypes()) + + if err := v.err(); err != nil { + return err + } + + if cfg.AuthParameters == nil { + return errors.New(errors.InvalidArgument, msgAuthParametersNull) + } + + return validateAuthParameters(cfg.AuthorizationType, cfg.AuthParameters) +} + +// authBlockName is the AuthParameters member that carries the credentials of an +// authorization type. +func authBlockName(authType string) string { + switch authType { + case authTypeBasic: + return "BasicAuthParameters" + case authTypeAPIKey: + return "ApiKeyAuthParameters" + default: + return "OAuthParameters" + } +} + +// validateAuthParameters checks that ap carries the credentials block for +// authType and no other, and that every field in it is within AWS's limits. +func validateAuthParameters(authType string, ap *driver.ConnectionAuthParameters) error { + present := map[string]bool{ + authTypeBasic: ap.Basic != nil, authTypeAPIKey: ap.APIKey != nil, authTypeOAuth: ap.OAuth != nil, + } + + if !present[authType] { + return errors.Newf(errors.InvalidArgument, + "%s must be specified for %s authorization type.", authBlockName(authType), authType) + } + + for t, set := range present { + if set && t != authType { + return errors.Newf(errors.InvalidArgument, + "Only the authorization parameters for the %s authorization type can be specified.", authType) + } + } + + var v violations + + authFieldViolations(&v, ap) + httpParamViolations(&v, ap.InvocationHTTPParameters, "authParameters.invocationHttpParameters") + + return v.err() +} + +func authFieldViolations(v *violations, ap *driver.ConnectionAuthParameters) { + if b := ap.Basic; b != nil { + v.required(b.Username, "authParameters.basicAuthParameters.username", maxAuthValueLen) + v.requiredSensitive(b.Password, "authParameters.basicAuthParameters.password", maxAuthValueLen) + } + + if k := ap.APIKey; k != nil { + v.required(k.APIKeyName, "authParameters.apiKeyAuthParameters.apiKeyName", maxAuthValueLen) + v.requiredSensitive(k.APIKeyValue, "authParameters.apiKeyAuthParameters.apiKeyValue", maxAuthValueLen) + } + + if o := ap.OAuth; o != nil { + v.required(o.AuthorizationEndpoint, "authParameters.oAuthParameters.authorizationEndpoint", maxEndpointLen) + v.enum(o.HTTPMethod, "authParameters.oAuthParameters.httpMethod", oauthHTTPMethods()) + + if o.ClientParameters == nil { + v.add("null", "authParameters.oAuthParameters.clientParameters", "Member must not be null") + } else { + v.required(o.ClientParameters.ClientID, "authParameters.oAuthParameters.clientParameters.clientID", maxAuthValueLen) + v.requiredSensitive(o.ClientParameters.ClientSecret, "authParameters.oAuthParameters.clientParameters.clientSecret", + maxAuthValueLen) + } + + httpParamViolations(v, o.OAuthHTTPParameters, "authParameters.oAuthParameters.oAuthHttpParameters") + } +} + +func httpParamViolations(v *violations, p *driver.ConnectionHTTPParameters, field string) { + if p == nil { + return + } + + lists := []struct { + name string + params []driver.HTTPParameter + limit bool // header and query string keys and values are capped at 512 bytes + }{ + {"headerParameters", p.HeaderParameters, true}, + {"queryStringParameters", p.QueryStringParameters, true}, + {"bodyParameters", p.BodyParameters, false}, + } + + for _, l := range lists { + if n := len(l.params); n > maxHTTPParameters { + v.add(fmt.Sprint(n), field+"."+l.name, fmt.Sprintf("Member must have length less than or equal to %d", maxHTTPParameters)) + } + + if l.limit { + httpParamValueViolations(v, l.params, field+"."+l.name) + } + } +} + +// httpParamValueViolations checks each parameter's key and value length. The +// API model marks every parameter value sensitive, so a value is never echoed, +// whether or not IsValueSecret is set. +func httpParamValueViolations(v *violations, params []driver.HTTPParameter, field string) { + for i := range params { + member := fmt.Sprintf("%s.%d", field, i+1) + + if len(params[i].Key) > maxAuthValueLen { + v.add(params[i].Key, member+".member.key", fmt.Sprintf("Member must have length less than or equal to %d", maxAuthValueLen)) + } + + v.maxLenSensitive(params[i].Value, member+".member.value", maxAuthValueLen) + } +} + +// mergeAuthParameters applies an UpdateConnection AuthParameters block onto the +// stored one: each credentials block given replaces the stored fields it sets, +// a block for another authorization type replaces the stored blocks, and +// InvocationHttpParameters replaces the stored set when given. +func mergeAuthParameters(old driver.ConnectionAuthParameters, upd *driver.ConnectionAuthParameters) driver.ConnectionAuthParameters { + out := cloneAuth(old) + if upd == nil { + return out + } + + if upd.Basic != nil { + out.Basic = mergeBasic(out.Basic, upd.Basic) + } + + if upd.APIKey != nil { + out.APIKey = mergeAPIKey(out.APIKey, upd.APIKey) + } + + if upd.OAuth != nil { + out.OAuth = mergeOAuth(out.OAuth, upd.OAuth) + } + + if upd.InvocationHTTPParameters != nil { + out.InvocationHTTPParameters = cloneHTTPParams(upd.InvocationHTTPParameters) + } + + return out +} + +func mergeBasic(old, upd *driver.ConnectionBasicAuth) *driver.ConnectionBasicAuth { + out := driver.ConnectionBasicAuth{} + if old != nil { + out = *old + } + + out.Username = orKeep(upd.Username, out.Username) + out.Password = orKeep(upd.Password, out.Password) + + return &out +} + +func mergeAPIKey(old, upd *driver.ConnectionAPIKeyAuth) *driver.ConnectionAPIKeyAuth { + out := driver.ConnectionAPIKeyAuth{} + if old != nil { + out = *old + } + + out.APIKeyName = orKeep(upd.APIKeyName, out.APIKeyName) + out.APIKeyValue = orKeep(upd.APIKeyValue, out.APIKeyValue) + + return &out +} + +func mergeOAuth(old, upd *driver.ConnectionOAuth) *driver.ConnectionOAuth { + out := driver.ConnectionOAuth{} + if old != nil { + out = *cloneOAuth(old) + } + + out.AuthorizationEndpoint = orKeep(upd.AuthorizationEndpoint, out.AuthorizationEndpoint) + out.HTTPMethod = orKeep(upd.HTTPMethod, out.HTTPMethod) + + if c := upd.ClientParameters; c != nil { + client := driver.ConnectionOAuthClient{} + if out.ClientParameters != nil { + client = *out.ClientParameters + } + + client.ClientID = orKeep(c.ClientID, client.ClientID) + client.ClientSecret = orKeep(c.ClientSecret, client.ClientSecret) + out.ClientParameters = &client + } + + if upd.OAuthHTTPParameters != nil { + out.OAuthHTTPParameters = cloneHTTPParams(upd.OAuthHTTPParameters) + } + + return &out +} + +func orKeep(v, keep string) string { + if v == "" { + return keep + } + + return v +} + +// onlyBlockFor keeps the credentials block of authType and drops the others, so +// a connection switched to another authorization type stops carrying the old +// credentials. +func onlyBlockFor(authType string, ap driver.ConnectionAuthParameters) driver.ConnectionAuthParameters { + out := driver.ConnectionAuthParameters{InvocationHTTPParameters: ap.InvocationHTTPParameters} + + switch authType { + case authTypeBasic: + out.Basic = ap.Basic + case authTypeAPIKey: + out.APIKey = ap.APIKey + default: + out.OAuth = ap.OAuth + } + + return out +} + +func cloneAuth(ap driver.ConnectionAuthParameters) driver.ConnectionAuthParameters { + out := driver.ConnectionAuthParameters{InvocationHTTPParameters: cloneHTTPParams(ap.InvocationHTTPParameters)} + + if ap.Basic != nil { + b := *ap.Basic + out.Basic = &b + } + + if ap.APIKey != nil { + k := *ap.APIKey + out.APIKey = &k + } + + if ap.OAuth != nil { + out.OAuth = cloneOAuth(ap.OAuth) + } + + return out +} + +func cloneOAuth(o *driver.ConnectionOAuth) *driver.ConnectionOAuth { + out := *o + out.OAuthHTTPParameters = cloneHTTPParams(o.OAuthHTTPParameters) + + if o.ClientParameters != nil { + c := *o.ClientParameters + out.ClientParameters = &c + } + + return &out +} + +func cloneHTTPParams(p *driver.ConnectionHTTPParameters) *driver.ConnectionHTTPParameters { + if p == nil { + return nil + } + + return &driver.ConnectionHTTPParameters{ + HeaderParameters: append([]driver.HTTPParameter(nil), p.HeaderParameters...), + QueryStringParameters: append([]driver.HTTPParameter(nil), p.QueryStringParameters...), + BodyParameters: append([]driver.HTTPParameter(nil), p.BodyParameters...), + } +} + +// redactAuth returns a copy of ap without any secret value: passwords, API key +// values, OAuth client secrets and HTTP parameters marked IsValueSecret. That is +// what DescribeConnection shows. +func redactAuth(ap driver.ConnectionAuthParameters) driver.ConnectionAuthParameters { + out := cloneAuth(ap) + + if out.Basic != nil { + out.Basic.Password = "" + } + + if out.APIKey != nil { + out.APIKey.APIKeyValue = "" + } + + if out.OAuth != nil { + if out.OAuth.ClientParameters != nil { + out.OAuth.ClientParameters.ClientSecret = "" + } + + redactHTTPParams(out.OAuth.OAuthHTTPParameters) + } + + redactHTTPParams(out.InvocationHTTPParameters) + + return out +} + +func redactHTTPParams(p *driver.ConnectionHTTPParameters) { + if p == nil { + return + } + + for _, list := range [][]driver.HTTPParameter{p.HeaderParameters, p.QueryStringParameters, p.BodyParameters} { + for i := range list { + if list[i].IsValueSecret { + list[i].Value = "" + } + } + } +} + +type secretHTTPParam struct { + Key string `json:"key"` + Value string `json:"value"` + IsValueSecret bool `json:"is_value_secret"` +} + +type secretHTTPParams struct { + HeaderParameters []secretHTTPParam `json:"header_parameters,omitempty"` + QueryStringParameters []secretHTTPParam `json:"query_string_parameters,omitempty"` + BodyParameters []secretHTTPParam `json:"body_parameters,omitempty"` +} + +func toSecretParams(list []driver.HTTPParameter) []secretHTTPParam { + out := make([]secretHTTPParam, 0, len(list)) + for _, p := range list { + out = append(out, secretHTTPParam{Key: p.Key, Value: p.Value, IsValueSecret: p.IsValueSecret}) + } + + return out +} + +// connectionSecretString renders the SecretString EventBridge stores in a +// connection's Secrets Manager secret: the credentials of the authorization +// type, plus the invocation HTTP parameters when the connection has any. +func connectionSecretString(ap *driver.ConnectionAuthParameters) []byte { + fields := map[string]any{} + + if b := ap.Basic; b != nil { + fields["username"], fields["password"] = b.Username, b.Password + } + + if k := ap.APIKey; k != nil { + fields["api_key_name"], fields["api_key_value"] = k.APIKeyName, k.APIKeyValue + } + + if o := ap.OAuth; o != nil { + fields["authorization_endpoint"], fields["http_method"] = o.AuthorizationEndpoint, o.HTTPMethod + + if c := o.ClientParameters; c != nil { + fields["client_id"], fields["client_secret"] = c.ClientID, c.ClientSecret + } + } + + if p := ap.InvocationHTTPParameters; p != nil { + fields["invocation_http_parameters"] = secretHTTPParams{ + HeaderParameters: toSecretParams(p.HeaderParameters), + QueryStringParameters: toSecretParams(p.QueryStringParameters), + BodyParameters: toSecretParams(p.BodyParameters), + } + } + + out, err := json.Marshal(fields) + if err != nil { + return []byte("{}") + } + + return out +} diff --git a/providers/aws/eventbridge/connection_secrets_redaction_test.go b/providers/aws/eventbridge/connection_secrets_redaction_test.go new file mode 100644 index 000000000..e482ea3ef --- /dev/null +++ b/providers/aws/eventbridge/connection_secrets_redaction_test.go @@ -0,0 +1,150 @@ +package eventbridge + +import ( + "context" + "strings" + "testing" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/services/eventbus/driver" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// leakyValue is an over-long credential: long enough to fail validation, and +// built from a marker that must never appear in an error message. +func leakyValue() string { + return strings.Repeat("TOPSECRET", 60) +} + +func oauthAuth(secret string) *driver.ConnectionAuthParameters { + return &driver.ConnectionAuthParameters{OAuth: &driver.ConnectionOAuth{ + AuthorizationEndpoint: "https://auth.example.com/token", HTTPMethod: "POST", + ClientParameters: &driver.ConnectionOAuthClient{ClientID: "id", ClientSecret: secret}, + }} +} + +func withHeader(ap *driver.ConnectionAuthParameters, param driver.HTTPParameter) *driver.ConnectionAuthParameters { + ap.InvocationHTTPParameters = &driver.ConnectionHTTPParameters{HeaderParameters: []driver.HTTPParameter{param}} + + return ap +} + +// TestConnectionErrorsNeverEchoSecrets checks every sensitive member: a value +// that fails validation is reported without its content, on create and update. +func TestConnectionErrorsNeverEchoSecrets(t *testing.T) { + secret := leakyValue() + + tests := []struct { + name string + authType string + auth *driver.ConnectionAuthParameters + field string + }{ + { + name: "basic password", authType: "BASIC", + auth: &driver.ConnectionAuthParameters{Basic: &driver.ConnectionBasicAuth{Username: "u", Password: secret}}, + field: "authParameters.basicAuthParameters.password", + }, + { + name: "api key value", authType: "API_KEY", + auth: &driver.ConnectionAuthParameters{APIKey: &driver.ConnectionAPIKeyAuth{APIKeyName: "k", APIKeyValue: secret}}, + field: "authParameters.apiKeyAuthParameters.apiKeyValue", + }, + { + name: "oauth client secret", authType: "OAUTH_CLIENT_CREDENTIALS", + auth: oauthAuth(secret), + field: "authParameters.oAuthParameters.clientParameters.clientSecret", + }, + { + name: "secret header value", authType: "BASIC", + auth: withHeader(&driver.ConnectionAuthParameters{Basic: &driver.ConnectionBasicAuth{Username: "u", Password: "p"}}, + driver.HTTPParameter{Key: "x", Value: secret, IsValueSecret: true}), + field: "authParameters.invocationHttpParameters.headerParameters.1.member.value", + }, + { + name: "plain header value", authType: "BASIC", + auth: withHeader(&driver.ConnectionAuthParameters{Basic: &driver.ConnectionBasicAuth{Username: "u", Password: "p"}}, + driver.HTTPParameter{Key: "x", Value: secret}), + field: "authParameters.invocationHttpParameters.headerParameters.1.member.value", + }, + { + name: "oauth query parameter value", authType: "OAUTH_CLIENT_CREDENTIALS", + auth: func() *driver.ConnectionAuthParameters { + ap := oauthAuth("s") + ap.OAuth.OAuthHTTPParameters = &driver.ConnectionHTTPParameters{ + QueryStringParameters: []driver.HTTPParameter{{Key: "q", Value: secret, IsValueSecret: true}}, + } + + return ap + }(), + field: "authParameters.oAuthParameters.oAuthHttpParameters.queryStringParameters.1.member.value", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + m, _ := newConnectionMock(t) + ctx := context.Background() + + _, err := m.CreateConnection(ctx, &driver.ConnectionConfig{Name: "c", AuthorizationType: tc.authType, AuthParameters: tc.auth}) + require.True(t, cerrors.IsInvalidArgument(err), "got %v", err) + assert.NotContains(t, err.Error(), "TOPSECRET", "create error leaks the secret") + assert.Contains(t, cerrors.Message(err), "Value at '"+tc.field+"' failed to satisfy constraint") + + // The same value sent on an update of a valid connection. + valid := &driver.ConnectionConfig{Name: "ok", AuthorizationType: tc.authType, AuthParameters: validAuthFor(tc.authType)} + _, err = m.CreateConnection(ctx, valid) + require.NoError(t, err) + + _, err = m.UpdateConnection(ctx, &driver.ConnectionUpdate{Name: "ok", AuthParameters: tc.auth}) + require.True(t, cerrors.IsInvalidArgument(err), "got %v", err) + assert.NotContains(t, err.Error(), "TOPSECRET", "update error leaks the secret") + }) + } +} + +func validAuthFor(authType string) *driver.ConnectionAuthParameters { + switch authType { + case "API_KEY": + return &driver.ConnectionAuthParameters{APIKey: &driver.ConnectionAPIKeyAuth{APIKeyName: "k", APIKeyValue: "v"}} + case "OAUTH_CLIENT_CREDENTIALS": + return oauthAuth("s") + default: + return &driver.ConnectionAuthParameters{Basic: &driver.ConnectionBasicAuth{Username: "u", Password: "p"}} + } +} + +// TestConnectionNonSecretFieldsStillEchoed keeps AWS's usual form for members +// that are not sensitive: the offending value is quoted. +func TestConnectionNonSecretFieldsStillEchoed(t *testing.T) { + m, _ := newConnectionMock(t) + long := strings.Repeat("u", 513) + + _, err := m.CreateConnection(context.Background(), &driver.ConnectionConfig{ + Name: "c", AuthorizationType: "BASIC", + AuthParameters: &driver.ConnectionAuthParameters{Basic: &driver.ConnectionBasicAuth{Username: long, Password: "p"}}, + }) + require.True(t, cerrors.IsInvalidArgument(err)) + assert.Contains(t, cerrors.Message(err), "Value '"+long+"' at 'authParameters.basicAuthParameters.username'") +} + +// TestBodyParameterValuesAreNotValidatedOrEchoed covers body parameters, which +// have no length limit: a long secret body value is accepted, never echoed, and +// redacted on Describe. +func TestBodyParameterValuesAreNotValidatedOrEchoed(t *testing.T) { + m, _ := newConnectionMock(t) + ctx := context.Background() + + ap := validAuthFor("BASIC") + ap.InvocationHTTPParameters = &driver.ConnectionHTTPParameters{ + BodyParameters: []driver.HTTPParameter{{Key: "b", Value: leakyValue(), IsValueSecret: true}}, + } + + _, err := m.CreateConnection(ctx, &driver.ConnectionConfig{Name: "c", AuthorizationType: "BASIC", AuthParameters: ap}) + require.NoError(t, err) + + got, err := m.DescribeConnection(ctx, "c") + require.NoError(t, err) + assert.Empty(t, got.AuthParameters.InvocationHTTPParameters.BodyParameters[0].Value) +} diff --git a/providers/aws/eventbridge/connections.go b/providers/aws/eventbridge/connections.go new file mode 100644 index 000000000..83ce8842b --- /dev/null +++ b/providers/aws/eventbridge/connections.go @@ -0,0 +1,362 @@ +package eventbridge + +import ( + "context" + "sort" + "strings" + "time" + + "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/internal/idgen" + "github.com/stackshy/cloudemu/v2/services/eventbus/driver" + secretsdriver "github.com/stackshy/cloudemu/v2/services/secrets/driver" +) + +const ( + connStateAuthorized = "AUTHORIZED" + connStateAuthorizing = "AUTHORIZING" + connStateDeauthorized = "DEAUTHORIZED" + connStateDeauthorizing = "DEAUTHORIZING" + connStateDeleting = "DELETING" + + connectionSecretPrefix = "events!connection/" +) + +// connectionStates is the ConnectionState enum, in the order EventBridge lists it. +func connectionStates() []string { + return []string{ + stateCreating, stateUpdating, connStateDeleting, connStateAuthorized, connStateDeauthorized, + connStateAuthorizing, connStateDeauthorizing, "ACTIVE", "FAILED_CONNECTIVITY", + } +} + +// ConnectionSecretStore is the Secrets Manager surface a connection keeps its +// credentials in. The Secrets Manager mock satisfies it. +type ConnectionSecretStore interface { + CreateSecret(ctx context.Context, cfg secretsdriver.SecretConfig, value []byte) (*secretsdriver.SecretInfo, error) + PutSecretValue(ctx context.Context, name string, value []byte) (*secretsdriver.SecretVersion, error) + DeleteSecretWithOptions(ctx context.Context, name string, recoveryWindow *int64, force bool) (*secretsdriver.SecretInfo, string, error) +} + +// SetConnectionSecretStore wires the Secrets Manager backend that holds each +// connection's credentials, so the secret behind a connection's SecretArn is a +// real secret. Without it the SecretArn is only a well-formed identifier. +func (m *Mock) SetConnectionSecretStore(s ConnectionSecretStore) { + m.secrets = s +} + +// connectionData is a stored connection. Info keeps the secret values (they are +// needed to call an API destination); every read path returns a redacted copy. +type connectionData struct { + Info driver.Connection `json:"info"` + SecretName string `json:"secretName"` +} + +func errConnectionNotExist(name string) error { + return errors.Newf(errors.NotFound, "Connection '%s' does not exist.", name) +} + +// initialAuthState is the state a connection enters when it is created or its +// authorization changes: BASIC and API_KEY are authorized at once, OAuth first +// fetches a token. +func initialAuthState(authType string) string { + if authType == authTypeOAuth { + return connStateAuthorizing + } + + return connStateAuthorized +} + +// authorize records authType's initial state on c at now. +func authorize(c *driver.Connection, now time.Time) { + c.State = initialAuthState(c.AuthorizationType) + c.StateReason = "" + c.LastModifiedAt = now + + if c.State == connStateAuthorized { + c.LastAuthorizedAt = now + } +} + +func publicConnection(c *driver.Connection) *driver.Connection { + out := *c + out.AuthParameters = redactAuth(c.AuthParameters) + + return &out +} + +// CreateConnection creates a connection and the Secrets Manager secret that +// holds its credentials. +func (m *Mock) CreateConnection(ctx context.Context, cfg *driver.ConnectionConfig) (*driver.Connection, error) { + if err := validateConnectionConfig(cfg); err != nil { + return nil, err + } + + now := m.opts.Clock.Now().UTC() + id := idgen.UUID() + + cd := connectionData{ + SecretName: connectionSecretPrefix + cfg.Name + "/" + id, + Info: driver.Connection{ + Name: cfg.Name, + ARN: idgen.AWSARN("events", m.opts.Region, m.opts.AccountID, "connection/"+cfg.Name+"/"+id), + Description: cfg.Description, + AuthorizationType: cfg.AuthorizationType, + KMSKeyIdentifier: cfg.KMSKeyIdentifier, + AuthParameters: onlyBlockFor(cfg.AuthorizationType, cloneAuth(*cfg.AuthParameters)), + CreatedAt: now, + }, + } + authorize(&cd.Info, now) + + if !m.connections.SetIfAbsent(cfg.Name, cd) { + return nil, errors.Newf(errors.AlreadyExists, "Connection %s already exists.", cfg.Name) + } + + secretARN, err := m.createConnectionSecret(ctx, &cd) + if err != nil { + m.connections.Delete(cfg.Name) + + return nil, err + } + + cd.Info.SecretARN = secretARN + + if !m.connections.Update(cfg.Name, func(c connectionData) connectionData { + c.Info.SecretARN = secretARN + return c + }) { + // Deleted while its secret was being created: drop the secret too. + m.deleteConnectionSecret(ctx, cd.SecretName) + + return nil, errConnectionNotExist(cfg.Name) + } + + return publicConnection(&cd.Info), nil +} + +func (m *Mock) createConnectionSecret(ctx context.Context, cd *connectionData) (string, error) { + if m.secrets == nil { + return idgen.AWSARN("secretsmanager", m.opts.Region, m.opts.AccountID, + "secret:"+cd.SecretName+"-"+idgen.SecretARNSuffix()), nil + } + + info, err := m.secrets.CreateSecret(ctx, secretsdriver.SecretConfig{ + Name: cd.SecretName, KMSKeyID: cd.Info.KMSKeyIdentifier, + }, connectionSecretString(&cd.Info.AuthParameters)) + if err != nil { + return "", err + } + + return info.ResourceID, nil +} + +func (m *Mock) putConnectionSecret(ctx context.Context, cd *connectionData) { + if m.secrets != nil { + _, _ = m.secrets.PutSecretValue(ctx, cd.SecretName, connectionSecretString(&cd.Info.AuthParameters)) + } +} + +func (m *Mock) deleteConnectionSecret(ctx context.Context, name string) { + if m.secrets != nil { + _, _, _ = m.secrets.DeleteSecretWithOptions(ctx, name, nil, true) + } +} + +// settleConnection moves a connection out of a transient authorization state on +// read: an OAuth connection that is AUTHORIZING has its token, and one that is +// DEAUTHORIZING has finished. It returns the stored connection. +func (m *Mock) settleConnection(name string) (connectionData, bool) { + var out connectionData + + ok := m.connections.Update(name, func(c connectionData) connectionData { + switch c.Info.State { + case connStateAuthorizing: + c.Info.State = connStateAuthorized + c.Info.LastAuthorizedAt = m.opts.Clock.Now().UTC() + case connStateDeauthorizing: + c.Info.State = connStateDeauthorized + } + + out = c + + return c + }) + + return out, ok +} + +// DescribeConnection returns a connection without its secret values. +func (m *Mock) DescribeConnection(_ context.Context, name string) (*driver.Connection, error) { + cd, ok := m.settleConnection(name) + if !ok { + return nil, errors.Newf(errors.NotFound, + "Failed to describe the connection(s). Connection '%s' does not exist.", name) + } + + return publicConnection(&cd.Info), nil +} + +func validateConnectionUpdate(upd *driver.ConnectionUpdate) error { + var v violations + + if upd.Description != nil { + v.maxLen(*upd.Description, "description", maxDescriptionLen) + } + + if upd.AuthorizationType != nil { + v.enum(*upd.AuthorizationType, "authorizationType", authorizationTypes()) + } + + return v.err() +} + +// applyConnectionUpdate merges upd into c and re-authorizes it, failing when the +// merged authorization parameters do not suit the authorization type. +func applyConnectionUpdate(c *driver.Connection, upd *driver.ConnectionUpdate, now time.Time) error { + authType := c.AuthorizationType + if upd.AuthorizationType != nil { + authType = *upd.AuthorizationType + } + + merged := mergeAuthParameters(c.AuthParameters, upd.AuthParameters) + if err := validateAuthParameters(authType, &driver.ConnectionAuthParameters{ + Basic: pick(authType == authTypeBasic, merged.Basic), APIKey: pick(authType == authTypeAPIKey, merged.APIKey), + OAuth: pick(authType == authTypeOAuth, merged.OAuth), InvocationHTTPParameters: merged.InvocationHTTPParameters, + }); err != nil { + return err + } + + if upd.Description != nil { + c.Description = *upd.Description + } + + if upd.KMSKeyIdentifier != nil { + c.KMSKeyIdentifier = *upd.KMSKeyIdentifier + } + + c.AuthorizationType = authType + c.AuthParameters = onlyBlockFor(authType, merged) + authorize(c, now) + + return nil +} + +func pick[T any](keep bool, v *T) *T { + if keep { + return v + } + + return nil +} + +// UpdateConnection changes a connection's description, KMS key, authorization +// type or credentials and re-authorizes it, then writes the new credentials to +// its secret. +func (m *Mock) UpdateConnection(ctx context.Context, upd *driver.ConnectionUpdate) (*driver.Connection, error) { + if err := validateConnectionUpdate(upd); err != nil { + return nil, err + } + + var ( + out connectionData + applyErr error + ) + + now := m.opts.Clock.Now().UTC() + + found := m.connections.Update(upd.Name, func(c connectionData) connectionData { + next := c + next.Info.AuthParameters = cloneAuth(c.Info.AuthParameters) + + if applyErr = applyConnectionUpdate(&next.Info, upd, now); applyErr != nil { + return c + } + + out = next + + return next + }) + + if !found { + return nil, errConnectionNotExist(upd.Name) + } + + if applyErr != nil { + return nil, applyErr + } + + m.putConnectionSecret(ctx, &out) + + return publicConnection(&out.Info), nil +} + +// DeauthorizeConnection removes a connection's credential values, keeping the +// connection so it can be re-authorized later. +func (m *Mock) DeauthorizeConnection(ctx context.Context, name string) (*driver.Connection, error) { + var out connectionData + + now := m.opts.Clock.Now().UTC() + + found := m.connections.Update(name, func(c connectionData) connectionData { + c.Info.AuthParameters = redactAuth(c.Info.AuthParameters) + c.Info.State = connStateDeauthorizing + c.Info.LastModifiedAt = now + out = c + + return c + }) + + if !found { + return nil, errConnectionNotExist(name) + } + + m.putConnectionSecret(ctx, &out) + + return publicConnection(&out.Info), nil +} + +// DeleteConnection deletes a connection and its secret. The reply carries the +// DELETING state. +func (m *Mock) DeleteConnection(ctx context.Context, name string) (*driver.Connection, error) { + cd, ok := m.connections.Get(name) + if !ok || !m.connections.Delete(name) { + return nil, errConnectionNotExist(name) + } + + m.deleteConnectionSecret(ctx, cd.SecretName) + + out := publicConnection(&cd.Info) + out.State = connStateDeleting + + return out, nil +} + +// ListConnections lists connections sorted by name, without secret values. +func (m *Mock) ListConnections(_ context.Context, filter driver.ConnectionFilter) ([]driver.Connection, error) { + if filter.State != "" { + var v violations + + v.enum(filter.State, "connectionState", connectionStates()) + + if err := v.err(); err != nil { + return nil, err + } + } + + names := m.connections.Keys() + sort.Strings(names) + + out := make([]driver.Connection, 0, len(names)) + + for _, name := range names { + cd, ok := m.settleConnection(name) + if !ok || !strings.HasPrefix(name, filter.NamePrefix) || (filter.State != "" && cd.Info.State != filter.State) { + continue + } + + out = append(out, *publicConnection(&cd.Info)) + } + + return out, nil +} diff --git a/providers/aws/eventbridge/connections_test.go b/providers/aws/eventbridge/connections_test.go new file mode 100644 index 000000000..02df9b50f --- /dev/null +++ b/providers/aws/eventbridge/connections_test.go @@ -0,0 +1,336 @@ +package eventbridge + +import ( + "context" + "encoding/json" + "strings" + "sync" + "testing" + + "github.com/stackshy/cloudemu/v2/config" + cerrors "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/providers/aws/secretsmanager" + "github.com/stackshy/cloudemu/v2/services/eventbus/driver" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// newConnectionMock returns a mock wired to a real Secrets Manager mock. +func newConnectionMock(t *testing.T) (*Mock, *secretsmanager.Mock) { + t.Helper() + + m, fc := newTestMock() + sm := secretsmanager.New(config.NewOptions(config.WithClock(fc), config.WithRegion("us-east-1"))) + m.SetConnectionSecretStore(sm) + + return m, sm +} + +func basicConnection(name string) *driver.ConnectionConfig { + return &driver.ConnectionConfig{ + Name: name, + AuthorizationType: "BASIC", + AuthParameters: &driver.ConnectionAuthParameters{ + Basic: &driver.ConnectionBasicAuth{Username: "user", Password: "pass"}, + InvocationHTTPParameters: &driver.ConnectionHTTPParameters{ + HeaderParameters: []driver.HTTPParameter{ + {Key: "x-plain", Value: "shown"}, + {Key: "x-secret", Value: "hidden", IsValueSecret: true}, + }, + }, + }, + } +} + +func secretJSON(t *testing.T, sm *secretsmanager.Mock, arn string) map[string]any { + t.Helper() + + name := strings.TrimSuffix(arn[strings.Index(arn, "secret:")+len("secret:"):], arn[len(arn)-7:]) + + v, err := sm.GetSecretValue(context.Background(), name, "") + require.NoError(t, err) + + var out map[string]any + require.NoError(t, json.Unmarshal(v.Value, &out)) + + return out +} + +func TestCreateConnectionBasic(t *testing.T) { + m, sm := newConnectionMock(t) + ctx := context.Background() + + c, err := m.CreateConnection(ctx, basicConnection("c1")) + require.NoError(t, err) + assert.Regexp(t, `^arn:aws:events:us-east-1:123456789012:connection/c1/[-0-9a-f]{36}$`, c.ARN) + assert.Equal(t, "AUTHORIZED", c.State) + assert.False(t, c.LastAuthorizedAt.IsZero()) + assert.Regexp(t, `^arn:aws:secretsmanager:us-east-1:123456789012:secret:events!connection/c1/[-0-9a-f]{36}-.{6}$`, c.SecretARN) + + secret := secretJSON(t, sm, c.SecretARN) + assert.Equal(t, "user", secret["username"]) + assert.Equal(t, "pass", secret["password"]) + assert.Contains(t, secret, "invocation_http_parameters") + + got, err := m.DescribeConnection(ctx, "c1") + require.NoError(t, err) + assert.Equal(t, "user", got.AuthParameters.Basic.Username) + assert.Empty(t, got.AuthParameters.Basic.Password, "Describe must not return the password") + + headers := got.AuthParameters.InvocationHTTPParameters.HeaderParameters + assert.Equal(t, "shown", headers[0].Value) + assert.Empty(t, headers[1].Value, "a secret header value must be redacted") + + _, err = m.CreateConnection(ctx, basicConnection("c1")) + require.True(t, cerrors.IsAlreadyExists(err)) + assert.Equal(t, "Connection c1 already exists.", cerrors.Message(err)) +} + +func TestCreateConnectionAPIKeyAndOAuth(t *testing.T) { + m, sm := newConnectionMock(t) + ctx := context.Background() + + k, err := m.CreateConnection(ctx, &driver.ConnectionConfig{ + Name: "key", AuthorizationType: "API_KEY", + AuthParameters: &driver.ConnectionAuthParameters{APIKey: &driver.ConnectionAPIKeyAuth{APIKeyName: "x-api-key", APIKeyValue: "s3cr3t"}}, + }) + require.NoError(t, err) + assert.Equal(t, "AUTHORIZED", k.State) + assert.Equal(t, "s3cr3t", secretJSON(t, sm, k.SecretARN)["api_key_value"]) + + o, err := m.CreateConnection(ctx, &driver.ConnectionConfig{ + Name: "oauth", AuthorizationType: "OAUTH_CLIENT_CREDENTIALS", + AuthParameters: &driver.ConnectionAuthParameters{OAuth: &driver.ConnectionOAuth{ + AuthorizationEndpoint: "https://auth.example.com/token", HTTPMethod: "POST", + ClientParameters: &driver.ConnectionOAuthClient{ClientID: "id", ClientSecret: "csecret"}, + }}, + }) + require.NoError(t, err) + assert.Equal(t, "AUTHORIZING", o.State, "OAuth starts AUTHORIZING") + assert.Equal(t, "csecret", secretJSON(t, sm, o.SecretARN)["client_secret"]) + + got, err := m.DescribeConnection(ctx, "oauth") + require.NoError(t, err) + assert.Equal(t, "AUTHORIZED", got.State, "OAuth settles to AUTHORIZED on the next read") + assert.Empty(t, got.AuthParameters.OAuth.ClientParameters.ClientSecret) + assert.Equal(t, "id", got.AuthParameters.OAuth.ClientParameters.ClientID) +} + +func TestCreateConnectionValidation(t *testing.T) { + m, _ := newConnectionMock(t) + ctx := context.Background() + + tests := []struct { + name string + cfg *driver.ConnectionConfig + msg string + }{ + { + name: "bad name", + cfg: &driver.ConnectionConfig{Name: "bad name", AuthorizationType: "BASIC"}, + msg: "1 validation error detected: Value 'bad name' at 'name' failed to satisfy constraint: " + + `Member must satisfy regular expression pattern: [\.\-_A-Za-z0-9]+`, + }, + { + name: "bad authorization type", + cfg: &driver.ConnectionConfig{Name: "c", AuthorizationType: "INVALID_AUTH_TYPE"}, + msg: "1 validation error detected: Value 'INVALID_AUTH_TYPE' at 'authorizationType' failed to satisfy " + + "constraint: Member must satisfy enum value set: [BASIC, OAUTH_CLIENT_CREDENTIALS, API_KEY]", + }, + { + name: "no auth parameters", + cfg: &driver.ConnectionConfig{Name: "c", AuthorizationType: "BASIC"}, + msg: "1 validation error detected: Value null at 'authParameters' failed to satisfy constraint: Member must not be null", + }, + { + name: "block for another type", + cfg: &driver.ConnectionConfig{Name: "c", AuthorizationType: "BASIC", AuthParameters: &driver.ConnectionAuthParameters{ + APIKey: &driver.ConnectionAPIKeyAuth{APIKeyName: "k", APIKeyValue: "v"}, + }}, + msg: "BasicAuthParameters must be specified for BASIC authorization type.", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + _, err := m.CreateConnection(ctx, tc.cfg) + require.True(t, cerrors.IsInvalidArgument(err), "got %v", err) + assert.Equal(t, tc.msg, cerrors.Message(err)) + }) + } + + _, err := m.DescribeConnection(ctx, "c") + assert.True(t, cerrors.IsNotFound(err), "a rejected create must not store the connection") +} + +func TestUpdateConnection(t *testing.T) { + m, sm := newConnectionMock(t) + ctx := context.Background() + + c, err := m.CreateConnection(ctx, basicConnection("c1")) + require.NoError(t, err) + + desc := "new" + out, err := m.UpdateConnection(ctx, &driver.ConnectionUpdate{ + Name: "c1", Description: &desc, + AuthParameters: &driver.ConnectionAuthParameters{Basic: &driver.ConnectionBasicAuth{Username: "new_user"}}, + }) + require.NoError(t, err) + assert.Equal(t, "AUTHORIZED", out.State) + + secret := secretJSON(t, sm, c.SecretARN) + assert.Equal(t, "new_user", secret["username"]) + assert.Equal(t, "pass", secret["password"], "an omitted password keeps its value") + + got, err := m.DescribeConnection(ctx, "c1") + require.NoError(t, err) + assert.Equal(t, "new", got.Description) + + t.Run("switching type needs the new block", func(t *testing.T) { + typ := "API_KEY" + _, err := m.UpdateConnection(ctx, &driver.ConnectionUpdate{Name: "c1", AuthorizationType: &typ}) + require.True(t, cerrors.IsInvalidArgument(err)) + + got, err := m.DescribeConnection(ctx, "c1") + require.NoError(t, err) + assert.Equal(t, "BASIC", got.AuthorizationType, "a rejected update changes nothing") + }) + + t.Run("switching type with the new block", func(t *testing.T) { + typ := "API_KEY" + _, err := m.UpdateConnection(ctx, &driver.ConnectionUpdate{ + Name: "c1", AuthorizationType: &typ, + AuthParameters: &driver.ConnectionAuthParameters{APIKey: &driver.ConnectionAPIKeyAuth{APIKeyName: "k", APIKeyValue: "v"}}, + }) + require.NoError(t, err) + + got, err := m.DescribeConnection(ctx, "c1") + require.NoError(t, err) + assert.Nil(t, got.AuthParameters.Basic, "the old BASIC block is dropped") + assert.Equal(t, "k", got.AuthParameters.APIKey.APIKeyName) + }) + + _, err = m.UpdateConnection(ctx, &driver.ConnectionUpdate{Name: "ghost", Description: &desc}) + require.True(t, cerrors.IsNotFound(err)) + assert.Equal(t, "Connection 'ghost' does not exist.", cerrors.Message(err)) +} + +func TestDeauthorizeConnection(t *testing.T) { + m, sm := newConnectionMock(t) + ctx := context.Background() + + c, err := m.CreateConnection(ctx, basicConnection("c1")) + require.NoError(t, err) + + out, err := m.DeauthorizeConnection(ctx, "c1") + require.NoError(t, err) + assert.Equal(t, "DEAUTHORIZING", out.State) + + got, err := m.DescribeConnection(ctx, "c1") + require.NoError(t, err) + assert.Equal(t, "DEAUTHORIZED", got.State) + assert.Empty(t, secretJSON(t, sm, c.SecretARN)["password"], "deauthorizing removes the credential from the secret") + + _, err = m.DeauthorizeConnection(ctx, "ghost") + assert.True(t, cerrors.IsNotFound(err)) +} + +func TestDeleteConnectionDeletesSecret(t *testing.T) { + m, sm := newConnectionMock(t) + ctx := context.Background() + + c, err := m.CreateConnection(ctx, basicConnection("c1")) + require.NoError(t, err) + + out, err := m.DeleteConnection(ctx, "c1") + require.NoError(t, err) + assert.Equal(t, "DELETING", out.State) + + _, err = m.DescribeConnection(ctx, "c1") + require.True(t, cerrors.IsNotFound(err)) + assert.Equal(t, "Failed to describe the connection(s). Connection 'c1' does not exist.", cerrors.Message(err)) + + secrets, err := sm.ListSecrets(ctx) + require.NoError(t, err) + + for _, s := range secrets { + assert.NotEqual(t, c.SecretARN, s.ResourceID, "the connection's secret must be deleted with it") + } + + _, err = m.DeleteConnection(ctx, "c1") + assert.True(t, cerrors.IsNotFound(err)) +} + +func TestListConnections(t *testing.T) { + m, _ := newConnectionMock(t) + ctx := context.Background() + + for _, name := range []string{"beta", "alpha"} { + _, err := m.CreateConnection(ctx, basicConnection(name)) + require.NoError(t, err) + } + + all, err := m.ListConnections(ctx, driver.ConnectionFilter{}) + require.NoError(t, err) + require.Len(t, all, 2) + assert.Equal(t, "alpha", all[0].Name) + assert.Empty(t, all[0].AuthParameters.Basic.Password) + + byPrefix, err := m.ListConnections(ctx, driver.ConnectionFilter{NamePrefix: "be"}) + require.NoError(t, err) + assert.Len(t, byPrefix, 1) + + byState, err := m.ListConnections(ctx, driver.ConnectionFilter{State: "DEAUTHORIZED"}) + require.NoError(t, err) + assert.Empty(t, byState) + + _, err = m.ListConnections(ctx, driver.ConnectionFilter{State: "BOGUS"}) + assert.True(t, cerrors.IsInvalidArgument(err)) +} + +// TestConnectionsConcurrent runs creates, updates, deauthorizations, reads and +// deletes at once so the race detector can check that no store lock is held +// across a Secrets Manager call. +func TestConnectionsConcurrent(t *testing.T) { + m, sm := newConnectionMock(t) + ctx := context.Background() + + var wg sync.WaitGroup + + for _, name := range []string{"a", "b", "c", "d"} { + wg.Add(1) + + go func() { + defer wg.Done() + + _, _ = m.CreateConnection(ctx, basicConnection(name)) + desc := "x" + _, _ = m.UpdateConnection(ctx, &driver.ConnectionUpdate{Name: name, Description: &desc}) + _, _ = m.DeauthorizeConnection(ctx, name) + _, _ = m.DescribeConnection(ctx, name) + _, _ = m.ListConnections(ctx, driver.ConnectionFilter{}) + _, _ = m.DeleteConnection(ctx, name) + }() + } + + wg.Wait() + + left, err := m.ListConnections(ctx, driver.ConnectionFilter{}) + require.NoError(t, err) + assert.Empty(t, left) + + secrets, err := sm.ListSecrets(ctx) + require.NoError(t, err) + + for _, s := range secrets { + assert.NotContains(t, s.Name, "events!connection/", "every connection's secret is deleted with it") + } +} + +func TestConnectionWithoutSecretStore(t *testing.T) { + m, _ := newTestMock() + + c, err := m.CreateConnection(context.Background(), basicConnection("c1")) + require.NoError(t, err) + assert.Regexp(t, `^arn:aws:secretsmanager:us-east-1:123456789012:secret:events!connection/c1/`, c.SecretARN) +} diff --git a/providers/aws/eventbridge/eventbridge.go b/providers/aws/eventbridge/eventbridge.go index c98a78a2c..0228c51e9 100644 --- a/providers/aws/eventbridge/eventbridge.go +++ b/providers/aws/eventbridge/eventbridge.go @@ -98,7 +98,13 @@ type Mock struct { tagsByARN tagStore archives *memstore.Store[*archiveData] - replays *memstore.Store[driver.Replay] + + connections *memstore.Store[connectionData] + apiDestinations *memstore.Store[driver.APIDestination] + // secrets holds each connection's credentials; nil leaves SecretArn a bare + // identifier. + secrets ConnectionSecretStore + replays *memstore.Store[driver.Replay] // activeReplays counts replays currently delivering, bounded by // maxActiveReplays. activeReplays atomic.Int32 @@ -150,6 +156,9 @@ func New(opts *config.Options) *Mock { opts: opts, archives: memstore.New[*archiveData](), replays: memstore.New[driver.Replay](), + + connections: memstore.New[connectionData](), + apiDestinations: memstore.New[driver.APIDestination](), } // Create the default event bus automatically. diff --git a/providers/aws/eventbridge/snapshot.go b/providers/aws/eventbridge/snapshot.go index 626a5bac9..f252927ea 100644 --- a/providers/aws/eventbridge/snapshot.go +++ b/providers/aws/eventbridge/snapshot.go @@ -25,6 +25,9 @@ type ebSnapshot struct { Tags map[string]map[string]string `json:"tags,omitempty"` Archives map[string]*archiveSnapshot `json:"archives,omitempty"` Replays json.RawMessage `json:"replays,omitempty"` + + Connections json.RawMessage `json:"connections,omitempty"` + APIDestinations json.RawMessage `json:"apiDestinations,omitempty"` } // archiveSnapshot mirrors archiveData, promoting its unexported fields. @@ -70,13 +73,8 @@ func (m *Mock) Snapshot(_ context.Context, _ bool) (json.RawMessage, error) { snap.Tags = m.snapshotTags() snap.Archives = m.snapshotArchives() - if m.replays.Len() > 0 { - replays, err := m.replays.Snapshot() - if err != nil { - return nil, fmt.Errorf("eventbridge: snapshot replays: %w", err) - } - - snap.Replays = replays + if err := m.snapshotStores(&snap); err != nil { + return nil, err } return json.Marshal(snap) @@ -163,9 +161,55 @@ func (m *Mock) Restore(_ context.Context, data json.RawMessage) error { }) } - if len(snap.Replays) > 0 { - if err := m.replays.LoadSnapshot(snap.Replays); err != nil { - return fmt.Errorf("eventbridge: restore replays: %w", err) + return m.restoreStores(&snap) +} + +// snapshottedStore pairs a value-typed store with the snapshot field it is +// saved in. +type snapshottedStore struct { + name string + len func() int + save func() ([]byte, error) + load func([]byte) error + field *json.RawMessage +} + +func (m *Mock) valueStores(snap *ebSnapshot) []snapshottedStore { + return []snapshottedStore{ + {"replays", m.replays.Len, m.replays.Snapshot, m.replays.LoadSnapshot, &snap.Replays}, + {"connections", m.connections.Len, m.connections.Snapshot, m.connections.LoadSnapshot, &snap.Connections}, + { + "api destinations", m.apiDestinations.Len, m.apiDestinations.Snapshot, + m.apiDestinations.LoadSnapshot, &snap.APIDestinations, + }, + } +} + +func (m *Mock) snapshotStores(snap *ebSnapshot) error { + for _, s := range m.valueStores(snap) { + if s.len() == 0 { + continue + } + + data, err := s.save() + if err != nil { + return fmt.Errorf("eventbridge: snapshot %s: %w", s.name, err) + } + + *s.field = data + } + + return nil +} + +func (m *Mock) restoreStores(snap *ebSnapshot) error { + for _, s := range m.valueStores(snap) { + if len(*s.field) == 0 { + continue + } + + if err := s.load(*s.field); err != nil { + return fmt.Errorf("eventbridge: restore %s: %w", s.name, err) } } diff --git a/providers/aws/eventbridge/snapshot_test.go b/providers/aws/eventbridge/snapshot_test.go index 66bd8e716..1877adf01 100644 --- a/providers/aws/eventbridge/snapshot_test.go +++ b/providers/aws/eventbridge/snapshot_test.go @@ -70,6 +70,43 @@ func TestSnapshotEmpty(t *testing.T) { assert.True(t, bytes.Equal(data, data2)) } +// TestSnapshotConnectionsAndAPIDestinations proves connections (with their +// credentials and secret ARN) and API destinations survive a snapshot and +// restore. +func TestSnapshotConnectionsAndAPIDestinations(t *testing.T) { + ctx := context.Background() + + src, _ := newConnectionMock(t) + + c, err := src.CreateConnection(ctx, basicConnection("c1")) + require.NoError(t, err) + + _, err = src.CreateAPIDestination(ctx, destinationConfig("d1", c.ARN)) + require.NoError(t, err) + + data, err := src.Snapshot(ctx, true) + require.NoError(t, err) + + dst, _ := newTestMock() + require.NoError(t, dst.Restore(ctx, data)) + + data2, err := dst.Snapshot(ctx, true) + require.NoError(t, err) + assert.JSONEq(t, string(data), string(data2)) + + got, err := dst.DescribeConnection(ctx, "c1") + require.NoError(t, err) + assert.Equal(t, c.SecretARN, got.SecretARN) + + stored, ok := dst.connections.Get("c1") + require.True(t, ok) + assert.Equal(t, "pass", stored.Info.AuthParameters.Basic.Password, "credentials survive a restore") + + d, err := dst.DescribeAPIDestination(ctx, "d1") + require.NoError(t, err) + assert.Equal(t, "ACTIVE", d.State) +} + // TestSnapshotArchivesAndReplays proves archives (with their archived events) // and replays survive a snapshot and restore. func TestSnapshotArchivesAndReplays(t *testing.T) { diff --git a/providers/aws/eventbridge/validation.go b/providers/aws/eventbridge/validation.go index 356f6b634..26530ae58 100644 --- a/providers/aws/eventbridge/validation.go +++ b/providers/aws/eventbridge/validation.go @@ -62,6 +62,58 @@ func (v *violations) enum(value, field string, allowed []string) { v.add(value, field, "Member must satisfy enum value set: ["+strings.Join(allowed, ", ")+"]") } +// addSensitive records a failure on a member the API model marks sensitive +// (passwords, API key values, client secrets, HTTP parameter values). The value +// is left out of the message, as AWS does, so a credential never reaches an +// error response or log. +func (v *violations) addSensitive(field, constraint string) { + *v = append(*v, fmt.Sprintf("Value at '%s' failed to satisfy constraint: %s", field, constraint)) +} + +// required checks that value is set and at most maxLen bytes long. +func (v *violations) required(value, field string, maxLen int) { + if c := lengthConstraint(value, maxLen); c != "" { + v.add(value, field, c) + } +} + +// requiredSensitive is required for a sensitive member: the value is never +// echoed in the message. +func (v *violations) requiredSensitive(value, field string, maxLen int) { + if c := lengthConstraint(value, maxLen); c != "" { + v.addSensitive(field, c) + } +} + +// maxLenSensitive checks that a sensitive, optional value is at most maxLen +// bytes long without echoing it. +func (v *violations) maxLenSensitive(value, field string, maxLen int) { + if len(value) > maxLen { + v.addSensitive(field, fmt.Sprintf("Member must have length less than or equal to %d", maxLen)) + } +} + +// lengthConstraint returns the constraint a required value of at most maxLen +// bytes breaks, or "" when it is within bounds. +func lengthConstraint(value string, maxLen int) string { + switch { + case value == "": + return "Member must have length greater than or equal to 1" + case len(value) > maxLen: + return fmt.Sprintf("Member must have length less than or equal to %d", maxLen) + default: + return "" + } +} + +// pattern checks value against re, quoting patternText (the pattern as the AWS +// API model spells it) in the message. +func (v *violations) pattern(value, field string, re *regexp.Regexp, patternText string) { + if !re.MatchString(value) { + v.add(value, field, "Member must satisfy regular expression pattern: "+patternText) + } +} + func (v *violations) eventsARN(value, field string) { if !eventsARNRE.MatchString(value) { v.add(value, field, "Member must satisfy regular expression pattern: "+eventsARNPattern) @@ -93,6 +145,15 @@ func busNameFromARN(arn string) string { return arn[idx+len(infix):] } +// Values shared by more than one EventBridge enum. +const ( + stateCreating = "CREATING" + stateUpdating = "UPDATING" + httpGET = "GET" + httpPOST = "POST" + httpPUT = "PUT" +) + // countSet reports how many of the given strings are non-empty. func countSet(values ...string) int { n := 0 diff --git a/server/aws/eventbridge/api_destinations.go b/server/aws/eventbridge/api_destinations.go new file mode 100644 index 000000000..efdc8b40b --- /dev/null +++ b/server/aws/eventbridge/api_destinations.go @@ -0,0 +1,170 @@ +package eventbridge + +import ( + "context" + "net/http" + + "github.com/stackshy/cloudemu/v2/server/wire" + ebdriver "github.com/stackshy/cloudemu/v2/services/eventbus/driver" +) + +type createAPIDestinationRequest struct { + Name string `json:"Name"` + Description string `json:"Description"` + ConnectionArn string `json:"ConnectionArn"` + InvocationEndpoint string `json:"InvocationEndpoint"` + HTTPMethod string `json:"HttpMethod"` + InvocationRateLimitPerSecond *int `json:"InvocationRateLimitPerSecond"` +} + +type updateAPIDestinationRequest struct { + Name string `json:"Name"` + Description *string `json:"Description"` + ConnectionArn *string `json:"ConnectionArn"` + InvocationEndpoint *string `json:"InvocationEndpoint"` + HTTPMethod *string `json:"HttpMethod"` + InvocationRateLimitPerSecond *int `json:"InvocationRateLimitPerSecond"` +} + +// apiDestinationStateResponse is the reply of Create and UpdateApiDestination. +type apiDestinationStateResponse struct { + APIDestinationArn string `json:"ApiDestinationArn"` + APIDestinationState string `json:"ApiDestinationState"` + CreationTime *float64 `json:"CreationTime,omitempty"` + LastModifiedTime *float64 `json:"LastModifiedTime,omitempty"` +} + +type apiDestinationJSON struct { + APIDestinationArn string `json:"ApiDestinationArn"` + Name string `json:"Name"` + Description string `json:"Description,omitempty"` + APIDestinationState string `json:"ApiDestinationState"` + ConnectionArn string `json:"ConnectionArn"` + InvocationEndpoint string `json:"InvocationEndpoint"` + HTTPMethod string `json:"HttpMethod"` + InvocationRateLimitPerSecond int `json:"InvocationRateLimitPerSecond"` + CreationTime *float64 `json:"CreationTime,omitempty"` + LastModifiedTime *float64 `json:"LastModifiedTime,omitempty"` +} + +type listAPIDestinationsResponse struct { + APIDestinations []apiDestinationJSON `json:"ApiDestinations"` + NextToken string `json:"NextToken,omitempty"` +} + +func toAPIDestinationState(d *ebdriver.APIDestination) apiDestinationStateResponse { + return apiDestinationStateResponse{ + APIDestinationArn: d.ARN, + APIDestinationState: d.State, + CreationTime: timeToEpoch(d.CreatedAt), + LastModifiedTime: timeToEpoch(d.LastModifiedAt), + } +} + +func toAPIDestinationJSON(d *ebdriver.APIDestination, full bool) apiDestinationJSON { + out := apiDestinationJSON{ + APIDestinationArn: d.ARN, + Name: d.Name, + APIDestinationState: d.State, + ConnectionArn: d.ConnectionARN, + InvocationEndpoint: d.InvocationEndpoint, + HTTPMethod: d.HTTPMethod, + InvocationRateLimitPerSecond: d.InvocationRateLimitPerSecond, + CreationTime: timeToEpoch(d.CreatedAt), + LastModifiedTime: timeToEpoch(d.LastModifiedAt), + } + + if full { + out.Description = d.Description + } + + return out +} + +// writeDestinationState writes the state reply of a create or update, or the +// error that call returned. +func writeDestinationState(w http.ResponseWriter, d *ebdriver.APIDestination, err error) { + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, toAPIDestinationState(d)) +} + +func createAPIDestination(mgr connectionManager, w http.ResponseWriter, r *http.Request) { + var req createAPIDestinationRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + d, err := mgr.CreateAPIDestination(r.Context(), &ebdriver.APIDestinationConfig{ + Name: req.Name, + Description: req.Description, + ConnectionARN: req.ConnectionArn, + InvocationEndpoint: req.InvocationEndpoint, + HTTPMethod: req.HTTPMethod, + InvocationRateLimitPerSecond: req.InvocationRateLimitPerSecond, + }) + + writeDestinationState(w, d, err) +} + +func describeAPIDestination(mgr connectionManager, w http.ResponseWriter, r *http.Request) { + var req connectionNameRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + d, err := mgr.DescribeAPIDestination(r.Context(), req.Name) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, toAPIDestinationJSON(d, true)) +} + +func updateAPIDestination(mgr connectionManager, w http.ResponseWriter, r *http.Request) { + var req updateAPIDestinationRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + d, err := mgr.UpdateAPIDestination(r.Context(), &ebdriver.APIDestinationUpdate{ + Name: req.Name, + Description: req.Description, + ConnectionARN: req.ConnectionArn, + InvocationEndpoint: req.InvocationEndpoint, + HTTPMethod: req.HTTPMethod, + InvocationRateLimitPerSecond: req.InvocationRateLimitPerSecond, + }) + + writeDestinationState(w, d, err) +} + +func deleteAPIDestination(mgr connectionManager, w http.ResponseWriter, r *http.Request) { + var req connectionNameRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + if err := mgr.DeleteAPIDestination(r.Context(), req.Name); err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, struct{}{}) +} + +func fetchDestinations(ctx context.Context, mgr connectionManager, req *listFilterRequest) ([]ebdriver.APIDestination, error) { + return mgr.ListAPIDestinations(ctx, ebdriver.APIDestinationFilter{NamePrefix: req.NamePrefix, ConnectionARN: req.ConnectionArn}) +} + +func destinationListItem(d *ebdriver.APIDestination) apiDestinationJSON { + return toAPIDestinationJSON(d, false) +} + +func destinationPage(page []apiDestinationJSON, next string) any { + return listAPIDestinationsResponse{APIDestinations: page, NextToken: next} +} diff --git a/server/aws/eventbridge/archives.go b/server/aws/eventbridge/archives.go index ffcd0debc..6d68d73b6 100644 --- a/server/aws/eventbridge/archives.go +++ b/server/aws/eventbridge/archives.go @@ -139,25 +139,28 @@ func toArchiveState(a *ebdriver.Archive) archiveStateResponse { } } -// listFilterRequest is the request shape ListArchives and ListReplays share. +// listFilterRequest is the request shape the archive, replay, connection and +// API destination list operations share; each reads the filters it accepts. type listFilterRequest struct { - NamePrefix string `json:"NamePrefix"` - EventSourceArn string `json:"EventSourceArn"` - State string `json:"State"` - Limit int `json:"Limit"` - NextToken string `json:"NextToken"` + NamePrefix string `json:"NamePrefix"` + EventSourceArn string `json:"EventSourceArn"` + State string `json:"State"` + ConnectionState string `json:"ConnectionState"` + ConnectionArn string `json:"ConnectionArn"` + Limit int `json:"Limit"` + NextToken string `json:"NextToken"` } // listOp builds the handler of a list operation: decode the shared filter // request, fetch the items, convert them to their wire form, and write one page // using EventBridge's cursor pagination. -func listOp[T, J any]( - fetch func(ctx context.Context, mgr archiveManager, req *listFilterRequest) ([]T, error), +func listOp[M, T, J any]( + fetch func(ctx context.Context, mgr M, req *listFilterRequest) ([]T, error), conv func(*T) J, key func(J) string, wrap func(page []J, next string) any, -) archiveOp { - return func(mgr archiveManager, w http.ResponseWriter, r *http.Request) { +) func(mgr M, w http.ResponseWriter, r *http.Request) { + return func(mgr M, w http.ResponseWriter, r *http.Request) { var req listFilterRequest if !wire.DecodeJSON(w, r, &req) { return diff --git a/server/aws/eventbridge/connections.go b/server/aws/eventbridge/connections.go new file mode 100644 index 000000000..e8a6a2399 --- /dev/null +++ b/server/aws/eventbridge/connections.go @@ -0,0 +1,392 @@ +package eventbridge + +import ( + "context" + "net/http" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/server/wire" + ebdriver "github.com/stackshy/cloudemu/v2/services/eventbus/driver" +) + +// connectionManager is the AWS-specific EventBridge connection and API +// destination surface, asserted against the provider (not part of the portable +// EventBus driver). +type connectionManager interface { + CreateConnection(ctx context.Context, cfg *ebdriver.ConnectionConfig) (*ebdriver.Connection, error) + DescribeConnection(ctx context.Context, name string) (*ebdriver.Connection, error) + UpdateConnection(ctx context.Context, upd *ebdriver.ConnectionUpdate) (*ebdriver.Connection, error) + DeauthorizeConnection(ctx context.Context, name string) (*ebdriver.Connection, error) + DeleteConnection(ctx context.Context, name string) (*ebdriver.Connection, error) + ListConnections(ctx context.Context, filter ebdriver.ConnectionFilter) ([]ebdriver.Connection, error) + CreateAPIDestination(ctx context.Context, cfg *ebdriver.APIDestinationConfig) (*ebdriver.APIDestination, error) + DescribeAPIDestination(ctx context.Context, name string) (*ebdriver.APIDestination, error) + UpdateAPIDestination(ctx context.Context, upd *ebdriver.APIDestinationUpdate) (*ebdriver.APIDestination, error) + DeleteAPIDestination(ctx context.Context, name string) error + ListAPIDestinations(ctx context.Context, filter ebdriver.APIDestinationFilter) ([]ebdriver.APIDestination, error) +} + +type connectionOp func(mgr connectionManager, w http.ResponseWriter, r *http.Request) + +// Pagination keys of the connection and API destination lists. +var ( + connectionKey = func(c connectionJSON) string { return c.Name } //nolint:gochecknoglobals // pure function value + destinationKey = func(d apiDestinationJSON) string { return d.Name } //nolint:gochecknoglobals // pure function value +) + +// connectionOps maps each connection and API destination operation to its handler. +func connectionOps() map[string]connectionOp { + return map[string]connectionOp{ + "CreateConnection": createConnection, + "DescribeConnection": describeConnection, + "UpdateConnection": updateConnection, + "DeauthorizeConnection": connectionStateOp(connectionManager.DeauthorizeConnection), + "DeleteConnection": connectionStateOp(connectionManager.DeleteConnection), + "ListConnections": listOp(fetchConnections, connectionListItem, connectionKey, connectionPage), + "CreateApiDestination": createAPIDestination, + "DescribeApiDestination": describeAPIDestination, + "UpdateApiDestination": updateAPIDestination, + "DeleteApiDestination": deleteAPIDestination, + "ListApiDestinations": listOp(fetchDestinations, destinationListItem, destinationKey, destinationPage), + } +} + +// serveConnectionOp serves op when it is a connection or API destination +// operation, reporting whether it did. +func (h *Handler) serveConnectionOp(w http.ResponseWriter, r *http.Request, op string) bool { + fn, ok := connectionOps()[op] + if !ok { + return false + } + + mgr, ok := h.bus.(connectionManager) + if !ok { + writeErr(w, cerrors.New(cerrors.Unimplemented, "connections not supported")) + return true + } + + fn(mgr, w, r) + + return true +} + +type httpParamJSON struct { + Key string `json:"Key"` + Value string `json:"Value,omitempty"` + IsValueSecret bool `json:"IsValueSecret,omitempty"` +} + +type httpParamsJSON struct { + HeaderParameters []httpParamJSON `json:"HeaderParameters,omitempty"` + QueryStringParameters []httpParamJSON `json:"QueryStringParameters,omitempty"` + BodyParameters []httpParamJSON `json:"BodyParameters,omitempty"` +} + +type basicAuthJSON struct { + Username string `json:"Username,omitempty"` + Password string `json:"Password,omitempty"` +} + +type apiKeyAuthJSON struct { + APIKeyName string `json:"ApiKeyName,omitempty"` + APIKeyValue string `json:"ApiKeyValue,omitempty"` +} + +type oauthClientJSON struct { + ClientID string `json:"ClientID,omitempty"` + ClientSecret string `json:"ClientSecret,omitempty"` +} + +type oauthJSON struct { + ClientParameters *oauthClientJSON `json:"ClientParameters,omitempty"` + AuthorizationEndpoint string `json:"AuthorizationEndpoint,omitempty"` + HTTPMethod string `json:"HttpMethod,omitempty"` + OAuthHTTPParameters *httpParamsJSON `json:"OAuthHttpParameters,omitempty"` +} + +type authParamsJSON struct { + BasicAuthParameters *basicAuthJSON `json:"BasicAuthParameters,omitempty"` + OAuthParameters *oauthJSON `json:"OAuthParameters,omitempty"` + APIKeyAuthParameters *apiKeyAuthJSON `json:"ApiKeyAuthParameters,omitempty"` + InvocationHTTPParameters *httpParamsJSON `json:"InvocationHttpParameters,omitempty"` +} + +type connectionNameRequest struct { + Name string `json:"Name"` +} + +type createConnectionRequest struct { + Name string `json:"Name"` + Description string `json:"Description"` + AuthorizationType string `json:"AuthorizationType"` + AuthParameters *authParamsJSON `json:"AuthParameters"` + KmsKeyIdentifier string `json:"KmsKeyIdentifier"` +} + +type updateConnectionRequest struct { + Name string `json:"Name"` + Description *string `json:"Description"` + AuthorizationType *string `json:"AuthorizationType"` + AuthParameters *authParamsJSON `json:"AuthParameters"` + KmsKeyIdentifier *string `json:"KmsKeyIdentifier"` +} + +// connectionStateResponse is the reply of Create, Update, Deauthorize and +// DeleteConnection. +type connectionStateResponse struct { + ConnectionArn string `json:"ConnectionArn"` + ConnectionState string `json:"ConnectionState"` + CreationTime *float64 `json:"CreationTime,omitempty"` + LastModifiedTime *float64 `json:"LastModifiedTime,omitempty"` + LastAuthorizedTime *float64 `json:"LastAuthorizedTime,omitempty"` +} + +type connectionJSON struct { + ConnectionArn string `json:"ConnectionArn"` + Name string `json:"Name"` + Description string `json:"Description,omitempty"` + ConnectionState string `json:"ConnectionState"` + StateReason string `json:"StateReason,omitempty"` + AuthorizationType string `json:"AuthorizationType"` + SecretArn string `json:"SecretArn,omitempty"` + KmsKeyIdentifier string `json:"KmsKeyIdentifier,omitempty"` + AuthParameters *authParamsJSON `json:"AuthParameters,omitempty"` + CreationTime *float64 `json:"CreationTime,omitempty"` + LastModifiedTime *float64 `json:"LastModifiedTime,omitempty"` + LastAuthorizedTime *float64 `json:"LastAuthorizedTime,omitempty"` +} + +type listConnectionsResponse struct { + Connections []connectionJSON `json:"Connections"` + NextToken string `json:"NextToken,omitempty"` +} + +func fromHTTPParams(p *httpParamsJSON) *ebdriver.ConnectionHTTPParameters { + if p == nil { + return nil + } + + conv := func(list []httpParamJSON) []ebdriver.HTTPParameter { + out := make([]ebdriver.HTTPParameter, 0, len(list)) + for _, e := range list { + out = append(out, ebdriver.HTTPParameter{Key: e.Key, Value: e.Value, IsValueSecret: e.IsValueSecret}) + } + + return out + } + + return &ebdriver.ConnectionHTTPParameters{ + HeaderParameters: conv(p.HeaderParameters), + QueryStringParameters: conv(p.QueryStringParameters), + BodyParameters: conv(p.BodyParameters), + } +} + +func toHTTPParams(p *ebdriver.ConnectionHTTPParameters) *httpParamsJSON { + if p == nil { + return nil + } + + conv := func(list []ebdriver.HTTPParameter) []httpParamJSON { + out := make([]httpParamJSON, 0, len(list)) + + for _, e := range list { + item := httpParamJSON{Key: e.Key, Value: e.Value, IsValueSecret: e.IsValueSecret} + if e.IsValueSecret { + item.Value = "" + } + + out = append(out, item) + } + + return out + } + + return &httpParamsJSON{ + HeaderParameters: conv(p.HeaderParameters), + QueryStringParameters: conv(p.QueryStringParameters), + BodyParameters: conv(p.BodyParameters), + } +} + +func fromAuthParams(a *authParamsJSON) *ebdriver.ConnectionAuthParameters { + if a == nil { + return nil + } + + out := &ebdriver.ConnectionAuthParameters{InvocationHTTPParameters: fromHTTPParams(a.InvocationHTTPParameters)} + + if b := a.BasicAuthParameters; b != nil { + out.Basic = &ebdriver.ConnectionBasicAuth{Username: b.Username, Password: b.Password} + } + + if k := a.APIKeyAuthParameters; k != nil { + out.APIKey = &ebdriver.ConnectionAPIKeyAuth{APIKeyName: k.APIKeyName, APIKeyValue: k.APIKeyValue} + } + + if o := a.OAuthParameters; o != nil { + out.OAuth = &ebdriver.ConnectionOAuth{ + AuthorizationEndpoint: o.AuthorizationEndpoint, + HTTPMethod: o.HTTPMethod, + OAuthHTTPParameters: fromHTTPParams(o.OAuthHTTPParameters), + } + + if c := o.ClientParameters; c != nil { + out.OAuth.ClientParameters = &ebdriver.ConnectionOAuthClient{ClientID: c.ClientID, ClientSecret: c.ClientSecret} + } + } + + return out +} + +// toAuthParams renders a connection's AuthParameters for a response. It never +// copies a password, API key value or OAuth client secret, whatever the +// provider hands it, so no response can carry a credential. +func toAuthParams(a *ebdriver.ConnectionAuthParameters) *authParamsJSON { + out := &authParamsJSON{InvocationHTTPParameters: toHTTPParams(a.InvocationHTTPParameters)} + + if b := a.Basic; b != nil { + out.BasicAuthParameters = &basicAuthJSON{Username: b.Username} + } + + if k := a.APIKey; k != nil { + out.APIKeyAuthParameters = &apiKeyAuthJSON{APIKeyName: k.APIKeyName} + } + + if o := a.OAuth; o != nil { + out.OAuthParameters = &oauthJSON{ + AuthorizationEndpoint: o.AuthorizationEndpoint, + HTTPMethod: o.HTTPMethod, + OAuthHTTPParameters: toHTTPParams(o.OAuthHTTPParameters), + } + + if c := o.ClientParameters; c != nil { + out.OAuthParameters.ClientParameters = &oauthClientJSON{ClientID: c.ClientID} + } + } + + return out +} + +func toConnectionState(c *ebdriver.Connection) connectionStateResponse { + return connectionStateResponse{ + ConnectionArn: c.ARN, + ConnectionState: c.State, + CreationTime: timeToEpoch(c.CreatedAt), + LastModifiedTime: timeToEpoch(c.LastModifiedAt), + LastAuthorizedTime: timeToEpoch(c.LastAuthorizedAt), + } +} + +func toConnectionJSON(c *ebdriver.Connection, full bool) connectionJSON { + out := connectionJSON{ + ConnectionArn: c.ARN, + Name: c.Name, + ConnectionState: c.State, + StateReason: c.StateReason, + AuthorizationType: c.AuthorizationType, + CreationTime: timeToEpoch(c.CreatedAt), + LastModifiedTime: timeToEpoch(c.LastModifiedAt), + LastAuthorizedTime: timeToEpoch(c.LastAuthorizedAt), + } + + if full { + out.Description = c.Description + out.SecretArn = c.SecretARN + out.KmsKeyIdentifier = c.KMSKeyIdentifier + out.AuthParameters = toAuthParams(&c.AuthParameters) + } + + return out +} + +func createConnection(mgr connectionManager, w http.ResponseWriter, r *http.Request) { + var req createConnectionRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + c, err := mgr.CreateConnection(r.Context(), &ebdriver.ConnectionConfig{ + Name: req.Name, + Description: req.Description, + AuthorizationType: req.AuthorizationType, + KMSKeyIdentifier: req.KmsKeyIdentifier, + AuthParameters: fromAuthParams(req.AuthParameters), + }) + if err != nil { + writeErr(w, err) + return + } + + resp := toConnectionState(c) + resp.LastAuthorizedTime = nil + + wire.WriteJSON(w, resp) +} + +func describeConnection(mgr connectionManager, w http.ResponseWriter, r *http.Request) { + var req connectionNameRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + c, err := mgr.DescribeConnection(r.Context(), req.Name) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, toConnectionJSON(c, true)) +} + +func updateConnection(mgr connectionManager, w http.ResponseWriter, r *http.Request) { + var req updateConnectionRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + c, err := mgr.UpdateConnection(r.Context(), &ebdriver.ConnectionUpdate{ + Name: req.Name, + Description: req.Description, + AuthorizationType: req.AuthorizationType, + KMSKeyIdentifier: req.KmsKeyIdentifier, + AuthParameters: fromAuthParams(req.AuthParameters), + }) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, toConnectionState(c)) +} + +// connectionStateOp builds the handler of an operation that takes a connection +// name and answers with the connection's state (Deauthorize, Delete). +func connectionStateOp( + call func(mgr connectionManager, ctx context.Context, name string) (*ebdriver.Connection, error), +) connectionOp { + return func(mgr connectionManager, w http.ResponseWriter, r *http.Request) { + var req connectionNameRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + c, err := call(mgr, r.Context(), req.Name) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, toConnectionState(c)) + } +} + +func fetchConnections(ctx context.Context, mgr connectionManager, req *listFilterRequest) ([]ebdriver.Connection, error) { + return mgr.ListConnections(ctx, ebdriver.ConnectionFilter{NamePrefix: req.NamePrefix, State: req.ConnectionState}) +} + +func connectionListItem(c *ebdriver.Connection) connectionJSON { return toConnectionJSON(c, false) } + +func connectionPage(page []connectionJSON, next string) any { + return listConnectionsResponse{Connections: page, NextToken: next} +} diff --git a/server/aws/eventbridge/connections_sdk_test.go b/server/aws/eventbridge/connections_sdk_test.go new file mode 100644 index 000000000..5e4495249 --- /dev/null +++ b/server/aws/eventbridge/connections_sdk_test.go @@ -0,0 +1,174 @@ +package eventbridge_test + +import ( + "context" + "errors" + "net/http/httptest" + "strings" + "testing" + + "github.com/aws/aws-sdk-go-v2/aws" + awsconfig "github.com/aws/aws-sdk-go-v2/config" + "github.com/aws/aws-sdk-go-v2/credentials" + awseb "github.com/aws/aws-sdk-go-v2/service/eventbridge" + ebtypes "github.com/aws/aws-sdk-go-v2/service/eventbridge/types" + awssm "github.com/aws/aws-sdk-go-v2/service/secretsmanager" + + "github.com/stackshy/cloudemu/v2" + awsserver "github.com/stackshy/cloudemu/v2/server/aws" +) + +// newEBAndSecrets serves EventBridge and Secrets Manager from one cloud, so a +// connection's secret can be read back through the Secrets Manager SDK. +func newEBAndSecrets(t *testing.T) (*awseb.Client, *awssm.Client) { + t.Helper() + + cloud := cloudemu.NewAWS() + srv := awsserver.New(awsserver.Drivers{ + EventBridge: cloud.EventBridge, SecretsManager: cloud.SecretsManager, + AccountID: "123456789012", Region: "us-east-1", + }) + + ts := httptest.NewServer(srv) + t.Cleanup(ts.Close) + + cfg, err := awsconfig.LoadDefaultConfig(context.Background(), + awsconfig.WithRegion("us-east-1"), + awsconfig.WithCredentialsProvider(credentials.NewStaticCredentialsProvider("test", "test", "")), + ) + if err != nil { + t.Fatalf("aws config: %v", err) + } + + eb := awseb.NewFromConfig(cfg, func(o *awseb.Options) { o.BaseEndpoint = aws.String(ts.URL) }) + sm := awssm.NewFromConfig(cfg, func(o *awssm.Options) { o.BaseEndpoint = aws.String(ts.URL) }) + + return eb, sm +} + +func TestSDKConnectionAndAPIDestinationLifecycle(t *testing.T) { + eb, sm := newEBAndSecrets(t) + ctx := context.Background() + + created, err := eb.CreateConnection(ctx, &awseb.CreateConnectionInput{ + Name: aws.String("c1"), + AuthorizationType: ebtypes.ConnectionAuthorizationTypeApiKey, + AuthParameters: &ebtypes.CreateConnectionAuthRequestParameters{ + ApiKeyAuthParameters: &ebtypes.CreateConnectionApiKeyAuthRequestParameters{ + ApiKeyName: aws.String("x-api-key"), ApiKeyValue: aws.String("s3cr3t"), + }, + }, + }) + if err != nil { + t.Fatalf("CreateConnection: %v", err) + } + + if created.ConnectionState != ebtypes.ConnectionStateAuthorized { + t.Fatalf("CreateConnection state = %s, want AUTHORIZED", created.ConnectionState) + } + + desc, err := eb.DescribeConnection(ctx, &awseb.DescribeConnectionInput{Name: aws.String("c1")}) + if err != nil { + t.Fatalf("DescribeConnection: %v", err) + } + + key := desc.AuthParameters.ApiKeyAuthParameters + if aws.ToString(key.ApiKeyName) != "x-api-key" || aws.ToString(desc.SecretArn) == "" { + t.Fatalf("DescribeConnection = %+v", desc) + } + + secret, err := sm.GetSecretValue(ctx, &awssm.GetSecretValueInput{SecretId: desc.SecretArn}) + if err != nil { + t.Fatalf("GetSecretValue(%s): %v", aws.ToString(desc.SecretArn), err) + } + + if aws.ToString(secret.SecretString) != `{"api_key_name":"x-api-key","api_key_value":"s3cr3t"}` { + t.Fatalf("connection secret = %s", aws.ToString(secret.SecretString)) + } + + dest, err := eb.CreateApiDestination(ctx, &awseb.CreateApiDestinationInput{ + Name: aws.String("d1"), ConnectionArn: created.ConnectionArn, + InvocationEndpoint: aws.String("https://example.com/hook"), HttpMethod: ebtypes.ApiDestinationHttpMethodPost, + }) + if err != nil { + t.Fatalf("CreateApiDestination: %v", err) + } + + if dest.ApiDestinationState != ebtypes.ApiDestinationStateActive { + t.Fatalf("CreateApiDestination state = %s", dest.ApiDestinationState) + } + + got, err := eb.DescribeApiDestination(ctx, &awseb.DescribeApiDestinationInput{Name: aws.String("d1")}) + if err != nil || aws.ToInt32(got.InvocationRateLimitPerSecond) != 300 { + t.Fatalf("DescribeApiDestination = %+v, %v", got, err) + } + + if _, err := eb.DeauthorizeConnection(ctx, &awseb.DeauthorizeConnectionInput{Name: aws.String("c1")}); err != nil { + t.Fatalf("DeauthorizeConnection: %v", err) + } + + got, err = eb.DescribeApiDestination(ctx, &awseb.DescribeApiDestinationInput{Name: aws.String("d1")}) + if err != nil || got.ApiDestinationState != ebtypes.ApiDestinationStateInactive { + t.Fatalf("DescribeApiDestination after deauthorize = %+v, %v", got, err) + } + + list, err := eb.ListConnections(ctx, &awseb.ListConnectionsInput{}) + if err != nil || len(list.Connections) != 1 { + t.Fatalf("ListConnections = %+v, %v", list, err) + } + + if _, err := eb.DeleteApiDestination(ctx, &awseb.DeleteApiDestinationInput{Name: aws.String("d1")}); err != nil { + t.Fatalf("DeleteApiDestination: %v", err) + } + + deleted, err := eb.DeleteConnection(ctx, &awseb.DeleteConnectionInput{Name: aws.String("c1")}) + if err != nil || deleted.ConnectionState != ebtypes.ConnectionStateDeleting { + t.Fatalf("DeleteConnection = %+v, %v", deleted, err) + } + + _, err = sm.DescribeSecret(ctx, &awssm.DescribeSecretInput{SecretId: desc.SecretArn}) + if err == nil { + t.Fatal("the connection's secret still exists after DeleteConnection") + } + + _, err = eb.DescribeConnection(ctx, &awseb.DescribeConnectionInput{Name: aws.String("c1")}) + + var notFound *ebtypes.ResourceNotFoundException + if !errors.As(err, ¬Found) { + t.Fatalf("DescribeConnection after delete = %v, want ResourceNotFoundException", err) + } +} + +// TestSDKConnectionErrorDoesNotLeakSecret sends an over-long API key value and +// checks the ValidationException names the member without echoing the value. +func TestSDKConnectionErrorDoesNotLeakSecret(t *testing.T) { + eb, _ := newEBAndSecrets(t) + secret := strings.Repeat("TOPSECRET", 60) + + _, err := eb.CreateConnection(context.Background(), &awseb.CreateConnectionInput{ + Name: aws.String("c1"), + AuthorizationType: ebtypes.ConnectionAuthorizationTypeApiKey, + AuthParameters: &ebtypes.CreateConnectionAuthRequestParameters{ + ApiKeyAuthParameters: &ebtypes.CreateConnectionApiKeyAuthRequestParameters{ + ApiKeyName: aws.String("x-api-key"), ApiKeyValue: aws.String(secret), + }, + }, + }) + requireAPIError(t, err, "ValidationException", + "1 validation error detected: Value at 'authParameters.apiKeyAuthParameters.apiKeyValue' failed to satisfy constraint: "+ + "Member must have length less than or equal to 512") + + if strings.Contains(err.Error(), "TOPSECRET") { + t.Fatalf("error leaks the API key value: %v", err) + } +} + +func TestSDKCreateAPIDestinationValidation(t *testing.T) { + eb, _ := newEBAndSecrets(t) + + _, err := eb.CreateApiDestination(context.Background(), &awseb.CreateApiDestinationInput{ + Name: aws.String("d"), ConnectionArn: aws.String("invalid-connection-arn"), + InvocationEndpoint: aws.String("https://example.com"), HttpMethod: "INVALID_METHOD", + }) + requireAPIError(t, err, "ValidationException", "") +} diff --git a/server/aws/eventbridge/handler.go b/server/aws/eventbridge/handler.go index 69b895ae4..3c8527bd6 100644 --- a/server/aws/eventbridge/handler.go +++ b/server/aws/eventbridge/handler.go @@ -92,10 +92,11 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } -// serveExtendedOp serves the operations outside the main switch (archives and -// replays) and answers UnknownOperationException for anything else. +// serveExtendedOp serves the operations outside the main switch (archives, +// replays, connections and API destinations) and answers +// UnknownOperationException for anything else. func (h *Handler) serveExtendedOp(w http.ResponseWriter, r *http.Request, op string) { - if h.serveArchiveOp(w, r, op) { + if h.serveArchiveOp(w, r, op) || h.serveConnectionOp(w, r, op) { return } diff --git a/services/eventbus/driver/connection.go b/services/eventbus/driver/connection.go new file mode 100644 index 000000000..1bc56ee3a --- /dev/null +++ b/services/eventbus/driver/connection.go @@ -0,0 +1,145 @@ +package driver + +import "time" + +// HTTPParameter is one extra header, query string or body parameter an +// EventBridge connection sends with each request. A secret value is stored in +// the connection's secret and never returned by DescribeConnection. +type HTTPParameter struct { + Key string + Value string + IsValueSecret bool +} + +// ConnectionHTTPParameters are the extra parameters a connection sends with +// every invocation or OAuth token request. +type ConnectionHTTPParameters struct { + HeaderParameters []HTTPParameter + QueryStringParameters []HTTPParameter + BodyParameters []HTTPParameter +} + +// ConnectionBasicAuth holds BASIC authorization credentials. +type ConnectionBasicAuth struct { + Username string + Password string +} + +// ConnectionAPIKeyAuth holds API_KEY authorization credentials: the header name +// and the key sent in it. +type ConnectionAPIKeyAuth struct { + APIKeyName string + APIKeyValue string +} + +// ConnectionOAuthClient holds the OAuth client credentials. +type ConnectionOAuthClient struct { + ClientID string + ClientSecret string +} + +// ConnectionOAuth holds OAUTH_CLIENT_CREDENTIALS authorization parameters. +type ConnectionOAuth struct { + AuthorizationEndpoint string + HTTPMethod string + ClientParameters *ConnectionOAuthClient + OAuthHTTPParameters *ConnectionHTTPParameters +} + +// ConnectionAuthParameters holds the authorization parameters of a connection. +// Exactly one of Basic, APIKey and OAuth is set, matching the connection's +// authorization type. +type ConnectionAuthParameters struct { + Basic *ConnectionBasicAuth + APIKey *ConnectionAPIKeyAuth + OAuth *ConnectionOAuth + InvocationHTTPParameters *ConnectionHTTPParameters +} + +// Connection describes an EventBridge connection: the authorization an API +// destination uses to call an HTTP endpoint. Connections are an AWS-only +// concept, consumed through an optional interface, not the portable driver. +type Connection struct { + Name string + ARN string + Description string + State string + StateReason string + AuthorizationType string + SecretARN string + KMSKeyIdentifier string + AuthParameters ConnectionAuthParameters + CreatedAt time.Time + LastModifiedAt time.Time + LastAuthorizedAt time.Time +} + +// ConnectionConfig carries the parameters of an EventBridge CreateConnection call. +type ConnectionConfig struct { + Name string + Description string + AuthorizationType string + KMSKeyIdentifier string + AuthParameters *ConnectionAuthParameters +} + +// ConnectionUpdate carries the parameters of an EventBridge UpdateConnection +// call. A nil field was omitted from the request and leaves the connection +// unchanged; a set auth block replaces the stored one. +type ConnectionUpdate struct { + Name string + Description *string + AuthorizationType *string + KMSKeyIdentifier *string + AuthParameters *ConnectionAuthParameters +} + +// ConnectionFilter narrows ListConnections. +type ConnectionFilter struct { + NamePrefix string + State string +} + +// APIDestination describes an EventBridge API destination: an HTTP endpoint a +// rule target can invoke through a connection. +type APIDestination struct { + Name string + ARN string + Description string + State string + ConnectionARN string + InvocationEndpoint string + HTTPMethod string + InvocationRateLimitPerSecond int + CreatedAt time.Time + LastModifiedAt time.Time +} + +// APIDestinationConfig carries the parameters of an EventBridge +// CreateApiDestination call. A nil InvocationRateLimitPerSecond takes the +// default. +type APIDestinationConfig struct { + Name string + Description string + ConnectionARN string + InvocationEndpoint string + HTTPMethod string + InvocationRateLimitPerSecond *int +} + +// APIDestinationUpdate carries the parameters of an EventBridge +// UpdateApiDestination call. A nil field leaves the destination unchanged. +type APIDestinationUpdate struct { + Name string + Description *string + ConnectionARN *string + InvocationEndpoint *string + HTTPMethod *string + InvocationRateLimitPerSecond *int +} + +// APIDestinationFilter narrows ListApiDestinations. +type APIDestinationFilter struct { + NamePrefix string + ConnectionARN string +} From b9bef54e9d787fa0ec5b7138c6b3f63a1a3a92c0 Mon Sep 17 00:00:00 2001 From: Gajendra Malviya Date: Thu, 22 Oct 2026 11:32:25 +0530 Subject: [PATCH 10/31] fix(aws-networkfirewall): address review on #1652 (rule group ARN clash, per-subnet zones and IP type, subnet and tag validation) --- docs/coverage/aws/networkfirewall.md | 4 +- docs/coverage/nongoals/networkfirewall.md | 4 +- .../aws/networkfirewall/families_test.go | 22 +-- providers/aws/networkfirewall/firewall.go | 65 ++++++-- .../aws/networkfirewall/flow_operation.go | 2 +- providers/aws/networkfirewall/metadata.go | 2 +- .../networkfirewall/networkfirewall_test.go | 146 +++++++++++++++++- .../aws/networkfirewall/reference_test.go | 4 +- providers/aws/networkfirewall/rule_group.go | 9 ++ providers/aws/networkfirewall/snapshot.go | 11 ++ .../aws/networkfirewall/snapshot_test.go | 4 +- providers/aws/networkfirewall/subnets.go | 113 ++++++++++++++ providers/aws/networkfirewall/tags.go | 16 +- providers/aws/networkfirewall/token_test.go | 6 +- .../vpc_endpoint_association.go | 19 ++- server/aws/networkfirewall/depth.go | 4 +- server/aws/networkfirewall/operations.go | 59 ++++--- server/aws/networkfirewall/sdk_parity_test.go | 120 +++++++++++++- .../driver/availability_zones.go | 43 ------ services/networkfirewall/driver/driver.go | 24 ++- 20 files changed, 559 insertions(+), 118 deletions(-) create mode 100644 providers/aws/networkfirewall/subnets.go delete mode 100644 services/networkfirewall/driver/availability_zones.go diff --git a/docs/coverage/aws/networkfirewall.md b/docs/coverage/aws/networkfirewall.md index 5a1d27706..bcc8b3580 100644 --- a/docs/coverage/aws/networkfirewall.md +++ b/docs/coverage/aws/networkfirewall.md @@ -126,9 +126,9 @@ VPCEndpointAssociations covers the VPC endpoint association family. - Not implemented (the operations answer `InvalidRequestException: unknown operation`): the proxy family (`CreateProxy`, `CreateProxyConfiguration`, `CreateProxyRuleGroup`, `CreateProxyRules`, the matching describe, update, delete and list operations, `AttachRuleGroupsToProxyConfiguration`, `DetachRuleGroupsFromProxyConfiguration`, `UpdateProxyRulePriorities`, `UpdateProxyRuleGroupPriorities`), the container association family (`CreateContainerAssociation` and its describe, update, delete and list operations), the transit gateway attachment operations (`AcceptNetworkFirewallTransitGatewayAttachment`, `RejectNetworkFirewallTransitGatewayAttachment`, `DeleteNetworkFirewallTransitGatewayAttachment`) and `UpdateFirewallEncryptionConfiguration`. `EncryptionConfiguration` on create and update requests is accepted and ignored. - There is no packet data plane. Flow capture and flush operations are accepted and complete (immediately, or after the settle window under `--async-settle`) but their results list no flows. Analysis reports complete with no results. Nothing is published to the `AWS/NetworkFirewall` CloudWatch namespace, and the logging configuration is validated and stored but no alert, flow or TLS log is delivered to S3, CloudWatch Logs or Kinesis Data Firehose. - Checks that need another service are not made: the VPC, subnet and Availability Zone ids a firewall or VPC endpoint association names are syntax-checked only, and the certificates, certificate authorities, KMS keys, log destinations and IAM principals named in TLS inspection configurations, logging configurations and resource policies are not looked up. Resource policies are validated (JSON, allowed actions per resource kind) and stored but never evaluated; sharing through Resource Access Manager is not modeled. -- Availability Zones are synthesized. A VPC firewall gets one zone per subnet in the order the subnets were added (`a`, `b`, ...) and a VPC endpoint association gets a zone derived from its subnet id; the real zone of a subnet is not known. +- Availability Zones are synthesized, not read from EC2. When a subnet is attached to a firewall it is given a zone derived from its id (the next free zone if that one is taken, since a firewall has one subnet per zone) and its own endpoint id; both stay fixed while the subnet stays attached, and a VPC endpoint association for the same subnet reports the same zone. The zone is therefore stable and consistent across `DescribeFirewall`, `DescribeFirewallMetadata`, flow operations and associations, but it is not the subnet's real zone. - A transit gateway-attached firewall can be created, described and have Availability Zones associated and disassociated, but no transit gateway attachment is created, so its status never reports a transit gateway attachment sync state. -- Firewalls, policies, rule groups and TLS inspection configurations are `READY` or `ACTIVE` as soon as they are created; there is no `PROVISIONING` period. `DryRun` and `AnalyzeRuleGroup` on create and update requests are accepted and ignored, so a dry run really makes the change and no rule group analysis results are produced. +- Firewalls, policies, rule groups and TLS inspection configurations are `READY` or `ACTIVE` as soon as they are created; there is no `PROVISIONING` period, and `DeleteFirewall` removes the record at once while answering `DELETING`, so `--async-settle` never shows those intermediate states for these resources (only flow operations settle). `DryRun` and `AnalyzeRuleGroup` on create and update requests are accepted and ignored, so a dry run really makes the change and no rule group analysis results are produced. - Rule content is stored and returned byte for byte. Only the parts needed for capacity and summaries are read: stateful rules (one unit per rule in `RulesString`, `StatefulRules` and `RulesSourceList` targets) and stateless rules (the product of the element counts of the match settings, as the CreateRuleGroup reference describes). Suricata syntax, rule variables and reference sets are not validated, and a rule group whose rules need more than its `Capacity` is rejected with `InvalidRequestException` (the exception real AWS uses for this is unconfirmed). - Behaviours the AWS reference does not state were chosen conservatively and are unconfirmed against the live service: a duplicate name answers `InvalidRequestException` (the one exception every Create operation models); deleting a firewall that still has VPC endpoint associations is refused with `InvalidOperationException`; describing or deleting a resource policy that does not exist answers `ResourceNotFoundException`; `DescribeRuleGroupSummary` on a stateless rule group answers `InvalidRequestException` and reports only the options named in the rule group's `SummaryConfiguration`; tagging a resource does not change its update token; a request that gives both a name and an ARN uses the name. - List operations take `MaxResults` (1 to 100, default 100) and `NextToken`, an opaque offset over results sorted by name, so it is only stable while the list does not change between pages. `ListRuleGroups` filters on `Type`, `Scope` and `ManagedType`; the emulator holds no AWS-managed rule groups, so `Scope=MANAGED` or any `ManagedType` returns an empty list. diff --git a/docs/coverage/nongoals/networkfirewall.md b/docs/coverage/nongoals/networkfirewall.md index c01c4f30b..9eaf41357 100644 --- a/docs/coverage/nongoals/networkfirewall.md +++ b/docs/coverage/nongoals/networkfirewall.md @@ -1,9 +1,9 @@ - Not implemented (the operations answer `InvalidRequestException: unknown operation`): the proxy family (`CreateProxy`, `CreateProxyConfiguration`, `CreateProxyRuleGroup`, `CreateProxyRules`, the matching describe, update, delete and list operations, `AttachRuleGroupsToProxyConfiguration`, `DetachRuleGroupsFromProxyConfiguration`, `UpdateProxyRulePriorities`, `UpdateProxyRuleGroupPriorities`), the container association family (`CreateContainerAssociation` and its describe, update, delete and list operations), the transit gateway attachment operations (`AcceptNetworkFirewallTransitGatewayAttachment`, `RejectNetworkFirewallTransitGatewayAttachment`, `DeleteNetworkFirewallTransitGatewayAttachment`) and `UpdateFirewallEncryptionConfiguration`. `EncryptionConfiguration` on create and update requests is accepted and ignored. - There is no packet data plane. Flow capture and flush operations are accepted and complete (immediately, or after the settle window under `--async-settle`) but their results list no flows. Analysis reports complete with no results. Nothing is published to the `AWS/NetworkFirewall` CloudWatch namespace, and the logging configuration is validated and stored but no alert, flow or TLS log is delivered to S3, CloudWatch Logs or Kinesis Data Firehose. - Checks that need another service are not made: the VPC, subnet and Availability Zone ids a firewall or VPC endpoint association names are syntax-checked only, and the certificates, certificate authorities, KMS keys, log destinations and IAM principals named in TLS inspection configurations, logging configurations and resource policies are not looked up. Resource policies are validated (JSON, allowed actions per resource kind) and stored but never evaluated; sharing through Resource Access Manager is not modeled. -- Availability Zones are synthesized. A VPC firewall gets one zone per subnet in the order the subnets were added (`a`, `b`, ...) and a VPC endpoint association gets a zone derived from its subnet id; the real zone of a subnet is not known. +- Availability Zones are synthesized, not read from EC2. When a subnet is attached to a firewall it is given a zone derived from its id (the next free zone if that one is taken, since a firewall has one subnet per zone) and its own endpoint id; both stay fixed while the subnet stays attached, and a VPC endpoint association for the same subnet reports the same zone. The zone is therefore stable and consistent across `DescribeFirewall`, `DescribeFirewallMetadata`, flow operations and associations, but it is not the subnet's real zone. - A transit gateway-attached firewall can be created, described and have Availability Zones associated and disassociated, but no transit gateway attachment is created, so its status never reports a transit gateway attachment sync state. -- Firewalls, policies, rule groups and TLS inspection configurations are `READY` or `ACTIVE` as soon as they are created; there is no `PROVISIONING` period. `DryRun` and `AnalyzeRuleGroup` on create and update requests are accepted and ignored, so a dry run really makes the change and no rule group analysis results are produced. +- Firewalls, policies, rule groups and TLS inspection configurations are `READY` or `ACTIVE` as soon as they are created; there is no `PROVISIONING` period, and `DeleteFirewall` removes the record at once while answering `DELETING`, so `--async-settle` never shows those intermediate states for these resources (only flow operations settle). `DryRun` and `AnalyzeRuleGroup` on create and update requests are accepted and ignored, so a dry run really makes the change and no rule group analysis results are produced. - Rule content is stored and returned byte for byte. Only the parts needed for capacity and summaries are read: stateful rules (one unit per rule in `RulesString`, `StatefulRules` and `RulesSourceList` targets) and stateless rules (the product of the element counts of the match settings, as the CreateRuleGroup reference describes). Suricata syntax, rule variables and reference sets are not validated, and a rule group whose rules need more than its `Capacity` is rejected with `InvalidRequestException` (the exception real AWS uses for this is unconfirmed). - Behaviours the AWS reference does not state were chosen conservatively and are unconfirmed against the live service: a duplicate name answers `InvalidRequestException` (the one exception every Create operation models); deleting a firewall that still has VPC endpoint associations is refused with `InvalidOperationException`; describing or deleting a resource policy that does not exist answers `ResourceNotFoundException`; `DescribeRuleGroupSummary` on a stateless rule group answers `InvalidRequestException` and reports only the options named in the rule group's `SummaryConfiguration`; tagging a resource does not change its update token; a request that gives both a name and an ARN uses the name. - List operations take `MaxResults` (1 to 100, default 100) and `NextToken`, an opaque offset over results sorted by name, so it is only stable while the list does not change between pages. `ListRuleGroups` filters on `Type`, `Scope` and `ManagedType`; the emulator holds no AWS-managed rule groups, so `Scope=MANAGED` or any `ManagedType` returns an empty list. diff --git a/providers/aws/networkfirewall/families_test.go b/providers/aws/networkfirewall/families_test.go index 4b24a7669..ea7660df2 100644 --- a/providers/aws/networkfirewall/families_test.go +++ b/providers/aws/networkfirewall/families_test.go @@ -333,7 +333,7 @@ func TestMetadata_Firewall_RuleGroup(t *testing.T) { assert.Equal(t, fw.ARN, meta.FirewallARN) assert.Equal(t, pol.ARN, meta.PolicyARN) assert.Equal(t, "READY", meta.Status) - assert.Equal(t, []string{"us-east-1a"}, meta.AvailabilityZones) + assert.Equal(t, []string{seedZone}, meta.AvailabilityZones) _, err = m.DescribeFirewallMetadata(bg, "arn:aws:network-firewall:us-east-1:000000000000:firewall/ghost") assert.True(t, cerrors.IsNotFound(err)) @@ -384,9 +384,9 @@ func TestRuleGroupSummary_Stateless_InvalidRequest(t *testing.T) { func TestFlowOperations_StartDescribeListResults(t *testing.T) { m, clock := newClockMock(t) _, fw := seedStack(t, m) - scope := nfdriver.FlowOperationScope{FirewallARN: fw.ARN, AvailabilityZone: "us-east-1a"} + scope := nfdriver.FlowOperationScope{FirewallARN: fw.ARN, AvailabilityZone: seedZone} - capture, err := m.StartFlowCapture(bg, nfdriver.FlowOperationRequest{FirewallARN: fw.ARN, AvailabilityZone: "us-east-1a"}) + capture, err := m.StartFlowCapture(bg, nfdriver.FlowOperationRequest{FirewallARN: fw.ARN, AvailabilityZone: seedZone}) require.NoError(t, err) assert.Regexp(t, uuidPattern, capture.ID) assert.Equal(t, "COMPLETED", capture.Status) @@ -394,7 +394,7 @@ func TestFlowOperations_StartDescribeListResults(t *testing.T) { clock.Advance(time.Second) - flush, err := m.StartFlowFlush(bg, nfdriver.FlowOperationRequest{FirewallARN: fw.ARN, AvailabilityZone: "us-east-1a"}) + flush, err := m.StartFlowFlush(bg, nfdriver.FlowOperationRequest{FirewallARN: fw.ARN, AvailabilityZone: seedZone}) require.NoError(t, err) assert.Equal(t, "FLOW_FLUSH", flush.Type) @@ -424,7 +424,7 @@ func TestFlowOperations_Validation(t *testing.T) { assoc := vpcAssociation(t, m, fw.ARN, "subnet-a") other := seedFirewall(t, m, "fw-2", pol.ARN) - _, err := m.StartFlowCapture(bg, nfdriver.FlowOperationRequest{FirewallARN: "arn:aws:network-firewall:us-east-1:000000000000:firewall/ghost", AvailabilityZone: "us-east-1a"}) + _, err := m.StartFlowCapture(bg, nfdriver.FlowOperationRequest{FirewallARN: "arn:aws:network-firewall:us-east-1:000000000000:firewall/ghost", AvailabilityZone: seedZone}) assert.True(t, cerrors.IsNotFound(err), "%v", err) _, err = m.StartFlowCapture(bg, nfdriver.FlowOperationRequest{FirewallARN: fw.ARN}) @@ -437,19 +437,19 @@ func TestFlowOperations_Validation(t *testing.T) { assert.True(t, cerrors.IsInvalidArgument(err), "association of another firewall: %v", err) neg := int32(-1) - _, err = m.StartFlowFlush(bg, nfdriver.FlowOperationRequest{FirewallARN: fw.ARN, AvailabilityZone: "us-east-1a", MinimumFlowAgeSeconds: &neg}) + _, err = m.StartFlowFlush(bg, nfdriver.FlowOperationRequest{FirewallARN: fw.ARN, AvailabilityZone: seedZone, MinimumFlowAgeSeconds: &neg}) assert.True(t, cerrors.IsInvalidArgument(err), "%v", err) - _, _, err = m.ListFlowOperations(bg, nfdriver.FlowOperationScope{FirewallARN: fw.ARN, AvailabilityZone: "us-east-1a"}, "BOGUS", nfdriver.ListOptions{}) + _, _, err = m.ListFlowOperations(bg, nfdriver.FlowOperationScope{FirewallARN: fw.ARN, AvailabilityZone: seedZone}, "BOGUS", nfdriver.ListOptions{}) assert.True(t, cerrors.IsInvalidArgument(err), "%v", err) - _, err = m.DescribeFlowOperation(bg, nfdriver.FlowOperationScope{FirewallARN: fw.ARN, AvailabilityZone: "us-east-1a"}, "no-such-op") + _, err = m.DescribeFlowOperation(bg, nfdriver.FlowOperationScope{FirewallARN: fw.ARN, AvailabilityZone: seedZone}, "no-such-op") assert.True(t, cerrors.IsNotFound(err), "%v", err) ok, err := m.StartFlowCapture(bg, nfdriver.FlowOperationRequest{FirewallARN: fw.ARN, VPCEndpointAssociationARN: assoc.ARN}) require.NoError(t, err) - _, err = m.DescribeFlowOperation(bg, nfdriver.FlowOperationScope{FirewallARN: fw.ARN, AvailabilityZone: "us-east-1a"}, ok.ID) + _, err = m.DescribeFlowOperation(bg, nfdriver.FlowOperationScope{FirewallARN: fw.ARN, AvailabilityZone: seedZone}, ok.ID) assert.True(t, cerrors.IsNotFound(err), "an operation of another endpoint is not visible: %v", err) } @@ -458,13 +458,13 @@ func TestFlowOperations_AsyncSettle(t *testing.T) { m := New(config.NewOptions(config.WithClock(clock), config.WithAsyncSettle())) _, fw := seedStack(t, m) - op, err := m.StartFlowFlush(bg, nfdriver.FlowOperationRequest{FirewallARN: fw.ARN, AvailabilityZone: "us-east-1a"}) + op, err := m.StartFlowFlush(bg, nfdriver.FlowOperationRequest{FirewallARN: fw.ARN, AvailabilityZone: seedZone}) require.NoError(t, err) assert.Equal(t, "IN_PROGRESS", op.Status) clock.Advance(10 * time.Second) - got, err := m.DescribeFlowOperation(bg, nfdriver.FlowOperationScope{FirewallARN: fw.ARN, AvailabilityZone: "us-east-1a"}, op.ID) + got, err := m.DescribeFlowOperation(bg, nfdriver.FlowOperationScope{FirewallARN: fw.ARN, AvailabilityZone: seedZone}, op.ID) require.NoError(t, err) assert.Equal(t, "COMPLETED", got.Status) } diff --git a/providers/aws/networkfirewall/firewall.go b/providers/aws/networkfirewall/firewall.go index 1e847f867..08237cbb0 100644 --- a/providers/aws/networkfirewall/firewall.go +++ b/providers/aws/networkfirewall/firewall.go @@ -31,7 +31,6 @@ func (m *Mock) CreateFirewall(_ context.Context, cfg nfdriver.CreateFirewallConf ID: idgen.GenerateID(""), PolicyARN: cfg.PolicyARN, VPCID: cfg.VPCID, - SubnetIDs: cloneStrings(cfg.SubnetIDs), AvailabilityZones: cloneStrings(cfg.AvailabilityZones), TransitGatewayID: cfg.TransitGatewayID, Description: cfg.Description, @@ -44,6 +43,18 @@ func (m *Mock) CreateFirewall(_ context.Context, cfg nfdriver.CreateFirewallConf UpdateToken: newToken(), Tags: copyTags(cfg.Tags), } + for _, s := range cfg.SubnetIDs { + if oneOf(s, fw.SubnetIDs...) { + continue + } + + if err := m.assignSubnet(fw, s, cfg.SubnetIPTypes[s]); err != nil { + return nil, err + } + + fw.SubnetIDs = append(fw.SubnetIDs, s) + } + if !m.firewalls.SetIfAbsent(cfg.Name, fw) { return nil, cerrors.Newf(cerrors.AlreadyExists, "firewall %q already exists", cfg.Name) } @@ -112,6 +123,16 @@ func validateFirewallPlacement(cfg *nfdriver.CreateFirewallConfig) error { return invalid("VpcId is required") } + for _, s := range cfg.SubnetIDs { + if err := validateSubnetID(s); err != nil { + return err + } + + if err := validateIPType(cfg.SubnetIPTypes[s]); err != nil { + return err + } + } + return validateVPCID(cfg.VPCID) } @@ -230,6 +251,7 @@ func (m *Mock) lookupFirewall(name, arn string) (*nfdriver.Firewall, bool) { func (m *Mock) outFirewall(f *nfdriver.Firewall) *nfdriver.Firewall { out := *f out.SubnetIDs = cloneStrings(f.SubnetIDs) + out.SubnetDetails = cloneSubnetDetails(f.SubnetDetails) out.AvailabilityZones = cloneStrings(f.AvailabilityZones) out.EnabledAnalysisTypes = cloneStrings(f.EnabledAnalysisTypes) out.Tags = copyTags(f.Tags) @@ -301,7 +323,7 @@ func (m *Mock) AssociateFirewallPolicy(_ context.Context, firewallName, policyAR func (m *Mock) AssociateSubnets( _ context.Context, firewallName string, - subnetIDs []string, + subnets []nfdriver.SubnetMapping, updateToken string, ) (*nfdriver.Firewall, error) { return m.mutateFirewall(firewallName, updateToken, func(fw *nfdriver.Firewall) error { @@ -309,20 +331,30 @@ func (m *Mock) AssociateSubnets( return err } - if len(subnetIDs) == 0 { + if len(subnets) == 0 { return invalid("SubnetMappings is required") } - seen := make(map[string]bool, len(fw.SubnetIDs)) - for _, s := range fw.SubnetIDs { - seen[s] = true + for _, s := range subnets { + if err := validateSubnetID(s.SubnetID); err != nil { + return err + } + + if err := validateIPType(s.IPAddressType); err != nil { + return err + } } - for _, s := range subnetIDs { - if !seen[s] { - fw.SubnetIDs = append(fw.SubnetIDs, s) - seen[s] = true + for _, s := range subnets { + if oneOf(s.SubnetID, fw.SubnetIDs...) { + continue + } + + if err := m.assignSubnet(fw, s.SubnetID, s.IPAddressType); err != nil { + return err } + + fw.SubnetIDs = append(fw.SubnetIDs, s.SubnetID) } return nil @@ -346,7 +378,16 @@ func (m *Mock) DisassociateSubnets( } remove := make(map[string]bool, len(subnetIDs)) + for _, s := range subnetIDs { + if err := validateSubnetID(s); err != nil { + return err + } + + if !oneOf(s, fw.SubnetIDs...) { + return invalid("subnet %q is not associated with firewall %q", s, fw.Name) + } + remove[s] = true } @@ -360,6 +401,10 @@ func (m *Mock) DisassociateSubnets( fw.SubnetIDs = kept + for s := range remove { + delete(fw.SubnetDetails, s) + } + return nil }) } diff --git a/providers/aws/networkfirewall/flow_operation.go b/providers/aws/networkfirewall/flow_operation.go index 8a66b559d..f93e32ba7 100644 --- a/providers/aws/networkfirewall/flow_operation.go +++ b/providers/aws/networkfirewall/flow_operation.go @@ -95,7 +95,7 @@ func (m *Mock) validateFlowScope(s nfdriver.FlowOperationScope) error { return invalid("either AvailabilityZone or VpcEndpointAssociationArn is required") } - if !oneOf(s.AvailabilityZone, nfdriver.SyncZones(fw, m.opts.Region)...) { + if !oneOf(s.AvailabilityZone, syncZones(fw)...) { return invalid("firewall %q has no endpoint in Availability Zone %q", fw.Name, s.AvailabilityZone) } diff --git a/providers/aws/networkfirewall/metadata.go b/providers/aws/networkfirewall/metadata.go index 499062af9..4699a04ff 100644 --- a/providers/aws/networkfirewall/metadata.go +++ b/providers/aws/networkfirewall/metadata.go @@ -37,7 +37,7 @@ func (m *Mock) DescribeFirewallMetadata(_ context.Context, firewallARN string) ( PolicyARN: fw.PolicyARN, Description: fw.Description, Status: fw.Status, - AvailabilityZones: nfdriver.SyncZones(fw, m.opts.Region), + AvailabilityZones: syncZones(fw), }, nil } diff --git a/providers/aws/networkfirewall/networkfirewall_test.go b/providers/aws/networkfirewall/networkfirewall_test.go index 821d9d542..d1f5c3798 100644 --- a/providers/aws/networkfirewall/networkfirewall_test.go +++ b/providers/aws/networkfirewall/networkfirewall_test.go @@ -3,6 +3,7 @@ package networkfirewall import ( "context" "encoding/json" + "fmt" "sync" "testing" "time" @@ -178,7 +179,7 @@ func TestFirewallDepth(t *testing.T) { _ = pol - assoc, err := m.AssociateSubnets(bg, "fw-1", []string{"subnet-1", "subnet-2"}, "") + assoc, err := m.AssociateSubnets(bg, "fw-1", []nfdriver.SubnetMapping{{SubnetID: "subnet-1"}, {SubnetID: "subnet-2"}}, "") require.NoError(t, err) assert.Len(t, assoc.SubnetIDs, 2) @@ -245,3 +246,146 @@ func TestFirewallConcurrentAccess(t *testing.T) { wg.Wait() } + +func TestRuleGroup_StatefulAndDomainShareNamespace(t *testing.T) { + m := newMock() + + stateful := seedRuleGroup(t, m, "dup", "STATEFUL", 10) + + _, err := m.CreateRuleGroup(bg, nfdriver.CreateRuleGroupConfig{Name: "dup", Type: "STATEFUL_DOMAIN", Capacity: 10}) + require.Error(t, err) + assert.True(t, cerrors.IsAlreadyExists(err)) + + // The ARN of the first group still resolves to it, and it is the only one. + got, err := m.DescribeRuleGroup(bg, "", stateful.ARN, "") + require.NoError(t, err) + assert.Equal(t, "STATEFUL", got.Type) + + list, _, err := m.ListRuleGroups(bg, nfdriver.RuleGroupFilter{}, nfdriver.ListOptions{}) + require.NoError(t, err) + assert.Len(t, list, 1) +} + +func TestRuleGroup_NameWithoutTypeIsRejected(t *testing.T) { + m := newMock() + seedRuleGroup(t, m, "y", "STATELESS", 10) + seedRuleGroup(t, m, "y", "STATEFUL", 10) + + _, err := m.DescribeRuleGroup(bg, "y", "", "") + require.True(t, cerrors.IsInvalidArgument(err), "got %v", err) + + _, err = m.DescribeRuleGroupMetadata(bg, "y", "", "") + require.True(t, cerrors.IsInvalidArgument(err), "got %v", err) + + got, err := m.DescribeRuleGroup(bg, "y", "", "STATEFUL") + require.NoError(t, err) + assert.Equal(t, "STATEFUL", got.Type) +} + +func TestTags_RetagAtLimitAllowed_NewKeyRefused(t *testing.T) { + m := newMock() + pol := seedPolicy(t, m, "pol-1") + + full := make(map[string]string, maxTags) + for i := 0; i < maxTags; i++ { + full[fmt.Sprintf("k%d", i)] = "v" + } + + require.NoError(t, m.TagResource(bg, pol.ARN, full)) + + // Same keys, new values: not new tags, so allowed. + require.NoError(t, m.TagResource(bg, pol.ARN, map[string]string{"k0": "changed", "k1": "changed"})) + + err := m.TagResource(bg, pol.ARN, map[string]string{"extra": "v"}) + require.True(t, cerrors.IsInvalidArgument(err), "got %v", err) +} + +func TestSubnets_EmptyAndUnknownIDsRejected(t *testing.T) { + m := newMock() + _, fw := seedStack(t, m) + + _, err := m.AssociateSubnets(bg, "fw-1", []nfdriver.SubnetMapping{{SubnetID: ""}}, "") + require.True(t, cerrors.IsInvalidArgument(err), "got %v", err) + + _, err = m.DisassociateSubnets(bg, "fw-1", []string{"subnet-unknown"}, "") + require.True(t, cerrors.IsInvalidArgument(err), "got %v", err) + + got, err := m.DescribeFirewall(bg, "fw-1", "") + require.NoError(t, err) + assert.Equal(t, fw.UpdateToken, got.UpdateToken, "a rejected call must not rotate the token") + assert.Equal(t, []string{"subnet-1"}, got.SubnetIDs) + + _, err = m.CreateFirewall(bg, nfdriver.CreateFirewallConfig{ + Name: "fw-empty", PolicyARN: fw.PolicyARN, VPCID: "vpc-1a2b", SubnetIDs: []string{""}, + }) + require.True(t, cerrors.IsInvalidArgument(err), "got %v", err) +} + +func TestSubnets_ZonePerSubnetIsStableAndUnique(t *testing.T) { + m := newMock() + pol := seedPolicy(t, m, "pol-1") + + fw, err := m.CreateFirewall(bg, nfdriver.CreateFirewallConfig{ + Name: "fw-az", PolicyARN: pol.ARN, VPCID: "vpc-1a2b", SubnetIDs: []string{"subnet-a", "subnet-b"}, + SubnetIPTypes: map[string]string{"subnet-a": "IPV4"}, + }) + require.NoError(t, err) + + za, zb := fw.SubnetDetails["subnet-a"], fw.SubnetDetails["subnet-b"] + assert.NotEmpty(t, za.AvailabilityZone) + assert.NotEqual(t, za.AvailabilityZone, zb.AvailabilityZone, "one subnet per zone") + assert.NotEqual(t, za.EndpointID, zb.EndpointID, "each zone has its own endpoint") + assert.Equal(t, "IPV4", za.IPAddressType) + + // Removing the first subnet must not move the second one. + after, err := m.DisassociateSubnets(bg, "fw-az", []string{"subnet-a"}, "") + require.NoError(t, err) + assert.Equal(t, zb, after.SubnetDetails["subnet-b"]) + assert.NotContains(t, after.SubnetDetails, "subnet-a") + + // The zone a flow operation accepts is the zone the subnet reports. + _, err = m.StartFlowCapture(bg, nfdriver.FlowOperationRequest{FirewallARN: fw.ARN, AvailabilityZone: zb.AvailabilityZone}) + require.NoError(t, err) + + meta, err := m.DescribeFirewallMetadata(bg, fw.ARN) + require.NoError(t, err) + assert.Equal(t, []string{zb.AvailabilityZone}, meta.AvailabilityZones) + + // A VPC endpoint association for the same subnet lands in the same zone. + assoc, err := m.CreateVpcEndpointAssociation(bg, nfdriver.CreateVPCEndpointAssociationConfig{ + FirewallARN: fw.ARN, VPCID: "vpc-1a2b", SubnetID: "subnet-b", + }) + require.NoError(t, err) + assert.Equal(t, zb.AvailabilityZone, assoc.AvailabilityZone) +} + +func TestSubnets_AssociateKeepsIPAddressType(t *testing.T) { + m := newMock() + seedStack(t, m) + + fw, err := m.AssociateSubnets(bg, "fw-1", []nfdriver.SubnetMapping{{SubnetID: "subnet-2", IPAddressType: "DUALSTACK"}}, "") + require.NoError(t, err) + assert.Equal(t, "DUALSTACK", fw.SubnetDetails["subnet-2"].IPAddressType) + + _, err = m.AssociateSubnets(bg, "fw-1", []nfdriver.SubnetMapping{{SubnetID: "subnet-3", IPAddressType: "BOGUS"}}, "") + require.True(t, cerrors.IsInvalidArgument(err), "got %v", err) +} + +func TestSnapshot_FirewallWithoutSubnetDetailsGetsZones(t *testing.T) { + m := newMock() + _, fw := seedStack(t, m) + + m.mu.Lock() + stored, _ := m.firewalls.Get(fw.Name) + stored.SubnetDetails = nil + m.backfillRestoredState() + m.mu.Unlock() + + got, err := m.DescribeFirewall(bg, "fw-1", "") + require.NoError(t, err) + assert.NotEmpty(t, got.SubnetDetails["subnet-1"].AvailabilityZone) +} + +// seedZone is the Availability Zone the emulator assigns "subnet-1", the subnet +// every seeded firewall is attached to. +var seedZone = zoneForKey("us-east-1", "subnet-1") diff --git a/providers/aws/networkfirewall/reference_test.go b/providers/aws/networkfirewall/reference_test.go index cd62a108d..3ff38a87c 100644 --- a/providers/aws/networkfirewall/reference_test.go +++ b/providers/aws/networkfirewall/reference_test.go @@ -299,9 +299,9 @@ func TestPagination_AllLists_PageSize1_Walk(t *testing.T) { require.NoError(t, err) } - scope := nfdriver.FlowOperationScope{FirewallARN: fw.ARN, AvailabilityZone: "us-east-1a"} + scope := nfdriver.FlowOperationScope{FirewallARN: fw.ARN, AvailabilityZone: seedZone} for i := 0; i < 3; i++ { - _, err := m.StartFlowFlush(bg, nfdriver.FlowOperationRequest{FirewallARN: fw.ARN, AvailabilityZone: "us-east-1a"}) + _, err := m.StartFlowFlush(bg, nfdriver.FlowOperationRequest{FirewallARN: fw.ARN, AvailabilityZone: seedZone}) require.NoError(t, err) } diff --git a/providers/aws/networkfirewall/rule_group.go b/providers/aws/networkfirewall/rule_group.go index f44d9f119..1f81ea05c 100644 --- a/providers/aws/networkfirewall/rule_group.go +++ b/providers/aws/networkfirewall/rule_group.go @@ -66,6 +66,11 @@ func (m *Mock) CreateRuleGroup(_ context.Context, cfg nfdriver.CreateRuleGroupCo UpdateToken: newToken(), Tags: copyTags(cfg.Tags), } + // STATEFUL and STATEFUL_DOMAIN groups share the stateful ARN namespace. + if _, taken := m.ruleGroupByARN(rg.ARN); taken { + return nil, cerrors.Newf(cerrors.AlreadyExists, "rule group %q already exists", cfg.Name) + } + if !m.ruleGroups.SetIfAbsent(ruleGroupKey(cfg.Name, cfg.Type), rg) { return nil, cerrors.Newf(cerrors.AlreadyExists, "rule group %q already exists", cfg.Name) } @@ -241,6 +246,10 @@ func (m *Mock) findRuleGroup(name, arn, ruleType string) (*nfdriver.RuleGroup, e return nil, err } + if name != "" && ruleType == "" && arn == "" { + return nil, invalid("Type is required when RuleGroupArn is not set") + } + if name != "" && ruleType != "" && !oneOf(ruleType, ruleTypeStateful, ruleTypeStateless, ruleTypeStatefulDomain) { return nil, invalid("Type %q is not valid", ruleType) } diff --git a/providers/aws/networkfirewall/snapshot.go b/providers/aws/networkfirewall/snapshot.go index 0a355e5b1..72eb874d1 100644 --- a/providers/aws/networkfirewall/snapshot.go +++ b/providers/aws/networkfirewall/snapshot.go @@ -188,6 +188,7 @@ func (m *Mock) restoreStores(snap *networkfirewallSnapshot) error { func (m *Mock) backfillRestoredState() { for _, f := range m.firewalls.SortedValues() { ensureToken(&f.UpdateToken) + m.backfillSubnets(f) } for _, p := range m.policies.SortedValues() { @@ -217,3 +218,13 @@ func ensureActive(status *string) { *status = statusActive } } + +// backfillSubnets gives a restored firewall the per-subnet zones and endpoints +// state saved before this release does not carry. +func (m *Mock) backfillSubnets(f *nfdriver.Firewall) { + for _, s := range f.SubnetIDs { + if _, ok := f.SubnetDetails[s]; !ok { + _ = m.assignSubnet(f, s, "") + } + } +} diff --git a/providers/aws/networkfirewall/snapshot_test.go b/providers/aws/networkfirewall/snapshot_test.go index fcefe29de..be04a7660 100644 --- a/providers/aws/networkfirewall/snapshot_test.go +++ b/providers/aws/networkfirewall/snapshot_test.go @@ -35,7 +35,7 @@ func TestSnapshotRoundTripNetworkFirewall(t *testing.T) { })) require.NoError(t, src.PutResourcePolicy(bg, pol.ARN, policyDoc("network-firewall:AssociateFirewallPolicy"))) - flow, err := src.StartFlowFlush(bg, nfdriver.FlowOperationRequest{FirewallARN: fw.ARN, AvailabilityZone: "us-east-1a"}) + flow, err := src.StartFlowFlush(bg, nfdriver.FlowOperationRequest{FirewallARN: fw.ARN, AvailabilityZone: seedZone}) require.NoError(t, err) _, err = src.UpdateFirewallAnalysisSettings(bg, "fw-1", []string{"TLS_SNI"}, "") @@ -77,7 +77,7 @@ func TestSnapshotRoundTripNetworkFirewall(t *testing.T) { require.NoError(t, err) assert.Contains(t, policy, "AssociateFirewallPolicy") - _, err = dst.DescribeFlowOperation(bg, nfdriver.FlowOperationScope{FirewallARN: fw.ARN, AvailabilityZone: "us-east-1a"}, flow.ID) + _, err = dst.DescribeFlowOperation(bg, nfdriver.FlowOperationScope{FirewallARN: fw.ARN, AvailabilityZone: seedZone}, flow.ID) require.NoError(t, err) _, err = dst.GetAnalysisReport(bg, "fw-1", "", rep.ID) diff --git a/providers/aws/networkfirewall/subnets.go b/providers/aws/networkfirewall/subnets.go new file mode 100644 index 000000000..c56662db8 --- /dev/null +++ b/providers/aws/networkfirewall/subnets.go @@ -0,0 +1,113 @@ +package networkfirewall + +import ( + "hash/fnv" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" +) + +// azLetters are the zone suffixes the emulator hands out. It has no real +// subnet-to-zone mapping, so a subnet gets a zone derived from its id. +const azLetters = "abcdef" + +// zoneForKey returns the Availability Zone a key (a subnet id) prefers: the same +// key always maps to the same zone. +func zoneForKey(region, key string) string { + h := fnv.New32a() + _, _ = h.Write([]byte(key)) + + return region + string(azLetters[int(h.Sum32()%uint32(len(azLetters)))]) +} + +// assignSubnet records a subnet's zone and endpoint on the firewall. A firewall +// has at most one subnet per zone, so when the preferred zone is taken the next +// free one is used. The assignment is stored and never recomputed. +func (m *Mock) assignSubnet(fw *nfdriver.Firewall, subnetID, ipType string) error { + if fw.SubnetDetails == nil { + fw.SubnetDetails = map[string]nfdriver.SubnetDetail{} + } + + if len(fw.SubnetDetails) >= len(azLetters) { + return invalid("a firewall can have at most %d subnets, one per Availability Zone", len(azLetters)) + } + + used := make(map[string]bool, len(fw.SubnetDetails)) + for _, d := range fw.SubnetDetails { + used[d.AvailabilityZone] = true + } + + zone := zoneForKey(m.opts.Region, subnetID) + + for i := 0; used[zone] && i < len(azLetters); i++ { + zone = m.opts.Region + string(azLetters[(indexOfLetter(zone)+1)%len(azLetters)]) + } + + fw.SubnetDetails[subnetID] = nfdriver.SubnetDetail{ + IPAddressType: ipType, + AvailabilityZone: zone, + EndpointID: endpointID(fw.ID, zone), + } + + return nil +} + +func indexOfLetter(zone string) int { + for i := range azLetters { + if zone[len(zone)-1] == azLetters[i] { + return i + } + } + + return 0 +} + +// endpointID names the firewall endpoint in one zone. +func endpointID(firewallID, zone string) string { + return "vpce-" + firewallID + "-" + zone[len(zone)-1:] +} + +// syncZones returns the zones a firewall runs in: its explicit zones for a +// transit gateway-attached firewall, else the zone of each attached subnet. +func syncZones(fw *nfdriver.Firewall) []string { + if len(fw.AvailabilityZones) > 0 { + return cloneStrings(fw.AvailabilityZones) + } + + zones := make([]string, 0, len(fw.SubnetIDs)) + for _, s := range fw.SubnetIDs { + zones = append(zones, fw.SubnetDetails[s].AvailabilityZone) + } + + return zones +} + +// validateSubnetID rejects an empty subnet id. +func validateSubnetID(id string) error { + if id == "" { + return invalid("SubnetId must not be empty") + } + + return nil +} + +func validateIPType(ipType string) error { + if ipType != "" && !oneOf(ipType, ipAddressType...) { + return cerrors.Newf(cerrors.InvalidArgument, "SubnetMapping.IPAddressType %q is not valid", ipType) + } + + return nil +} + +func cloneSubnetDetails(in map[string]nfdriver.SubnetDetail) map[string]nfdriver.SubnetDetail { + if in == nil { + return nil + } + + out := make(map[string]nfdriver.SubnetDetail, len(in)) + for k, v := range in { + out[k] = v + } + + return out +} diff --git a/providers/aws/networkfirewall/tags.go b/providers/aws/networkfirewall/tags.go index 1ddf8d1e5..e2f4f125c 100644 --- a/providers/aws/networkfirewall/tags.go +++ b/providers/aws/networkfirewall/tags.go @@ -22,7 +22,7 @@ func (m *Mock) TagResource(_ context.Context, arn string, tags map[string]string return cerrors.Newf(cerrors.NotFound, "resource %q not found", arn) } - if len(t)+len(tags) > maxTags { + if len(t)+newKeys(t, tags) > maxTags { return invalid("at most %d tags are allowed", maxTags) } @@ -129,3 +129,17 @@ func (m *Mock) taggableExtra(arn string) map[string]string { return nil } + +// newKeys counts the keys of add that the resource does not have yet: re-tagging +// an existing key is an overwrite, not a new tag. +func newKeys(have, add map[string]string) int { + n := 0 + + for k := range add { + if _, ok := have[k]; !ok { + n++ + } + } + + return n +} diff --git a/providers/aws/networkfirewall/token_test.go b/providers/aws/networkfirewall/token_test.go index afd49a156..ad7a3f120 100644 --- a/providers/aws/networkfirewall/token_test.go +++ b/providers/aws/networkfirewall/token_test.go @@ -198,7 +198,7 @@ func TestChangeProtection_BlocksSubnetAssociateDisassociate(t *testing.T) { _, err := m.UpdateSubnetChangeProtection(bg, "fw-1", true, "") require.NoError(t, err) - _, err = m.AssociateSubnets(bg, "fw-1", []string{"subnet-2"}, "") + _, err = m.AssociateSubnets(bg, "fw-1", []nfdriver.SubnetMapping{{SubnetID: "subnet-2"}}, "") requireException(t, err, nfdriver.ExInvalidOperation) _, err = m.DisassociateSubnets(bg, "fw-1", []string{"subnet-1"}, "") @@ -207,7 +207,7 @@ func TestChangeProtection_BlocksSubnetAssociateDisassociate(t *testing.T) { _, err = m.UpdateSubnetChangeProtection(bg, "fw-1", false, "") require.NoError(t, err) - _, err = m.AssociateSubnets(bg, "fw-1", []string{"subnet-2"}, "") + _, err = m.AssociateSubnets(bg, "fw-1", []nfdriver.SubnetMapping{{SubnetID: "subnet-2"}}, "") require.NoError(t, err) } @@ -285,7 +285,7 @@ func TestAvailabilityZones_OnVPCFirewall_Unsupported(t *testing.T) { _, err := m.AssociateAvailabilityZones(bg, vpc.Name, []string{"us-east-1b"}, "") requireException(t, err, nfdriver.ExInvalidOperation) - _, err = m.AssociateSubnets(bg, tgw.Name, []string{"subnet-1"}, "") + _, err = m.AssociateSubnets(bg, tgw.Name, []nfdriver.SubnetMapping{{SubnetID: "subnet-1"}}, "") requireException(t, err, nfdriver.ExInvalidOperation) } diff --git a/providers/aws/networkfirewall/vpc_endpoint_association.go b/providers/aws/networkfirewall/vpc_endpoint_association.go index f7e2582b5..41d534dd4 100644 --- a/providers/aws/networkfirewall/vpc_endpoint_association.go +++ b/providers/aws/networkfirewall/vpc_endpoint_association.go @@ -27,8 +27,8 @@ func (m *Mock) CreateVpcEndpointAssociation( return nil, invalid("SubnetMapping.SubnetId is required") } - if cfg.IPAddressType != "" && !oneOf(cfg.IPAddressType, ipAddressType...) { - return nil, invalid("SubnetMapping.IPAddressType %q is not valid", cfg.IPAddressType) + if err := validateIPType(cfg.IPAddressType); err != nil { + return nil, err } if err := validateDescription(cfg.Description); err != nil { @@ -39,7 +39,8 @@ func (m *Mock) CreateVpcEndpointAssociation( return nil, err } - if _, ok := m.firewallByARN(cfg.FirewallARN); !ok { + fw, ok := m.firewallByARN(cfg.FirewallARN) + if !ok { return nil, cerrors.Newf(cerrors.NotFound, "firewall %q not found", cfg.FirewallARN) } @@ -51,7 +52,7 @@ func (m *Mock) CreateVpcEndpointAssociation( VPCID: cfg.VPCID, SubnetID: cfg.SubnetID, IPAddressType: cfg.IPAddressType, - AvailabilityZone: nfdriver.ZoneForKey(m.opts.Region, cfg.SubnetID), + AvailabilityZone: m.associationZone(fw, cfg.SubnetID), Description: cfg.Description, Status: statusReady, Tags: copyTags(cfg.Tags), @@ -155,3 +156,13 @@ func cloneAssociation(a *nfdriver.VPCEndpointAssociation) *nfdriver.VPCEndpointA return &out } + +// associationZone is the Availability Zone of an association's subnet: the zone +// the firewall already assigned that subnet, else the zone its id maps to. +func (m *Mock) associationZone(fw *nfdriver.Firewall, subnetID string) string { + if d, ok := fw.SubnetDetails[subnetID]; ok { + return d.AvailabilityZone + } + + return zoneForKey(m.opts.Region, subnetID) +} diff --git a/server/aws/networkfirewall/depth.go b/server/aws/networkfirewall/depth.go index 6ae06d546..6f9ab4bed 100644 --- a/server/aws/networkfirewall/depth.go +++ b/server/aws/networkfirewall/depth.go @@ -106,7 +106,7 @@ type subnetsRequest struct { func (h *Handler) associateSubnets(w http.ResponseWriter, r *http.Request) { serveFirewallOp[subnetsRequest](h, w, r, func(c nfdriver.NetworkFirewall, name string, req *subnetsRequest) (*nfdriver.Firewall, map[string]any, error) { - fw, err := c.AssociateSubnets(r.Context(), name, subnetIDs(req.SubnetMappings), req.UpdateToken) + fw, err := c.AssociateSubnets(r.Context(), name, driverSubnets(req.SubnetMappings), req.UpdateToken) if err != nil { return nil, nil, err } @@ -130,7 +130,7 @@ func (h *Handler) disassociateSubnets(w http.ResponseWriter, r *http.Request) { func subnetMappingsOf(fw *nfdriver.Firewall) []subnetMapping { mappings := make([]subnetMapping, 0, len(fw.SubnetIDs)) for _, s := range fw.SubnetIDs { - mappings = append(mappings, subnetMapping{SubnetID: s}) + mappings = append(mappings, subnetMapping{SubnetID: s, IPAddressType: fw.SubnetDetails[s].IPAddressType}) } return mappings diff --git a/server/aws/networkfirewall/operations.go b/server/aws/networkfirewall/operations.go index 8b4791f49..33dacbacc 100644 --- a/server/aws/networkfirewall/operations.go +++ b/server/aws/networkfirewall/operations.go @@ -3,7 +3,6 @@ package networkfirewall import ( "encoding/json" "net/http" - "strings" "time" "github.com/stackshy/cloudemu/v2/server/wire" @@ -71,21 +70,22 @@ type firewallStatusJSON struct { // toFirewallStatusJSON builds the FirewallStatus block. A firewall in the // emulator is always fully provisioned, so it reports READY / IN_SYNC with one -// SyncState per attached subnet keyed by a synthetic Availability Zone. +// SyncState per attached subnet, keyed by the Availability Zone and endpoint +// the firewall assigned that subnet. func toFirewallStatusJSON(f *nfdriver.Firewall) firewallStatusJSON { - region := arnRegion(f.ARN) - states := make(map[string]syncStateJSON, len(f.SubnetIDs)) - for i, subnet := range f.SubnetIDs { - states[nfdriver.AvailabilityZoneFor(region, i)] = syncStateJSON{Attachment: attachmentJSON{ + + for _, subnet := range f.SubnetIDs { + d := f.SubnetDetails[subnet] + states[d.AvailabilityZone] = syncStateJSON{Attachment: attachmentJSON{ SubnetID: subnet, - EndpointID: "vpce-" + f.ID, + EndpointID: d.EndpointID, Status: "READY", }} } for _, az := range f.AvailabilityZones { - states[az] = syncStateJSON{Attachment: attachmentJSON{EndpointID: "vpce-" + f.ID, Status: "READY"}} + states[az] = syncStateJSON{Attachment: attachmentJSON{EndpointID: "vpce-" + f.ID + "-" + az, Status: "READY"}} } if len(states) == 0 { @@ -99,17 +99,6 @@ func toFirewallStatusJSON(f *nfdriver.Firewall) firewallStatusJSON { } } -// arnRegion extracts the region field (index 3) from an AWS ARN, defaulting to -// "us-east-1" when the ARN is malformed or region-less. -func arnRegion(arn string) string { - parts := strings.Split(arn, ":") - if len(parts) > 3 && parts[3] != "" { - return parts[3] - } - - return "us-east-1" -} - type createFirewallRequest struct { FirewallName string `json:"FirewallName"` FirewallPolicyArn string `json:"FirewallPolicyArn"` @@ -142,6 +131,7 @@ func (h *Handler) createFirewall(w http.ResponseWriter, r *http.Request) { PolicyARN: req.FirewallPolicyArn, VPCID: req.VpcID, SubnetIDs: subnetIDs(req.SubnetMappings), + SubnetIPTypes: subnetIPTypes(req.SubnetMappings), AvailabilityZones: zoneNames(req.AvailabilityZoneMappings), TransitGatewayID: req.TransitGatewayID, Description: req.Description, @@ -236,11 +226,6 @@ func withNextToken(resp map[string]any, next string) map[string]any { } func toFirewallJSON(f *nfdriver.Firewall) firewallJSON { - mappings := make([]subnetMapping, 0, len(f.SubnetIDs)) - for _, s := range f.SubnetIDs { - mappings = append(mappings, subnetMapping{SubnetID: s}) - } - zones := make([]zoneMapping, 0, len(f.AvailabilityZones)) for _, z := range f.AvailabilityZones { zones = append(zones, zoneMapping{AvailabilityZone: z}) @@ -248,7 +233,7 @@ func toFirewallJSON(f *nfdriver.Firewall) firewallJSON { return firewallJSON{ FirewallName: f.Name, FirewallArn: f.ARN, FirewallID: f.ID, FirewallPolicyArn: f.PolicyARN, - VpcID: f.VPCID, SubnetMappings: mappings, AvailabilityZoneMappings: zones, + VpcID: f.VPCID, SubnetMappings: subnetMappingsOf(f), AvailabilityZoneMappings: zones, TransitGatewayID: f.TransitGatewayID, Description: f.Description, DeleteProtection: f.DeleteProtection, SubnetChangeProtection: f.SubnetChangeProtection, FirewallPolicyChangeProtection: f.FirewallPolicyChangeProtection, @@ -273,6 +258,30 @@ func subnetIDs(mappings []subnetMapping) []string { return out } +// subnetIPTypes maps each mapping's subnet id to its IPAddressType, leaving out +// subnets that did not set one. +func subnetIPTypes(mappings []subnetMapping) map[string]string { + out := map[string]string{} + + for _, m := range mappings { + if m.IPAddressType != "" { + out[m.SubnetID] = m.IPAddressType + } + } + + return out +} + +// driverSubnets converts wire subnet mappings to driver ones. +func driverSubnets(mappings []subnetMapping) []nfdriver.SubnetMapping { + out := make([]nfdriver.SubnetMapping, 0, len(mappings)) + for _, m := range mappings { + out = append(out, nfdriver.SubnetMapping{SubnetID: m.SubnetID, IPAddressType: m.IPAddressType}) + } + + return out +} + func zoneNames(mappings []zoneMapping) []string { if len(mappings) == 0 { return nil diff --git a/server/aws/networkfirewall/sdk_parity_test.go b/server/aws/networkfirewall/sdk_parity_test.go index fbaf0e579..76ad94c40 100644 --- a/server/aws/networkfirewall/sdk_parity_test.go +++ b/server/aws/networkfirewall/sdk_parity_test.go @@ -461,7 +461,7 @@ func TestSDK_Metadata_Ops(t *testing.T) { require.NoError(t, err) assert.Equal(t, s.policyARN, aws.ToString(fm.FirewallPolicyArn)) assert.Equal(t, nftypes.FirewallStatusValueReady, fm.Status) - assert.Contains(t, fm.SupportedAvailabilityZones, "us-east-1a") + assert.Contains(t, fm.SupportedAvailabilityZones, "us-east-1c") rm, err := s.c.DescribeRuleGroupMetadata(bg, &networkfirewall.DescribeRuleGroupMetadataInput{RuleGroupArn: rg.RuleGroupResponse.RuleGroupArn}) require.NoError(t, err) @@ -483,29 +483,29 @@ func TestSDK_FlowAndAnalysis_Ops(t *testing.T) { }) require.NoError(t, err) - capture, err := s.c.StartFlowCapture(bg, &networkfirewall.StartFlowCaptureInput{FirewallArn: aws.String(s.fwARN), AvailabilityZone: aws.String("us-east-1a"), FlowFilters: []nftypes.FlowFilter{{SourcePort: aws.String("80")}}}) + capture, err := s.c.StartFlowCapture(bg, &networkfirewall.StartFlowCaptureInput{FirewallArn: aws.String(s.fwARN), AvailabilityZone: aws.String("us-east-1c"), FlowFilters: []nftypes.FlowFilter{{SourcePort: aws.String("80")}}}) require.NoError(t, err) assert.Equal(t, nftypes.FlowOperationStatusCompleted, capture.FlowOperationStatus) - flush, err := s.c.StartFlowFlush(bg, &networkfirewall.StartFlowFlushInput{FirewallArn: aws.String(s.fwARN), AvailabilityZone: aws.String("us-east-1a"), FlowFilters: []nftypes.FlowFilter{{SourcePort: aws.String("80")}}}) + flush, err := s.c.StartFlowFlush(bg, &networkfirewall.StartFlowFlushInput{FirewallArn: aws.String(s.fwARN), AvailabilityZone: aws.String("us-east-1c"), FlowFilters: []nftypes.FlowFilter{{SourcePort: aws.String("80")}}}) require.NoError(t, err) d, err := s.c.DescribeFlowOperation(bg, &networkfirewall.DescribeFlowOperationInput{ - FirewallArn: aws.String(s.fwARN), AvailabilityZone: aws.String("us-east-1a"), FlowOperationId: capture.FlowOperationId, + FirewallArn: aws.String(s.fwARN), AvailabilityZone: aws.String("us-east-1c"), FlowOperationId: capture.FlowOperationId, }) require.NoError(t, err) assert.Equal(t, nftypes.FlowOperationTypeFlowCapture, d.FlowOperationType) assert.NotNil(t, d.FlowRequestTimestamp) ops, err := s.c.ListFlowOperations(bg, &networkfirewall.ListFlowOperationsInput{ - FirewallArn: aws.String(s.fwARN), AvailabilityZone: aws.String("us-east-1a"), FlowOperationType: nftypes.FlowOperationTypeFlowFlush, + FirewallArn: aws.String(s.fwARN), AvailabilityZone: aws.String("us-east-1c"), FlowOperationType: nftypes.FlowOperationTypeFlowFlush, }) require.NoError(t, err) require.Len(t, ops.FlowOperations, 1) assert.Equal(t, aws.ToString(flush.FlowOperationId), aws.ToString(ops.FlowOperations[0].FlowOperationId)) res, err := s.c.ListFlowOperationResults(bg, &networkfirewall.ListFlowOperationResultsInput{ - FirewallArn: aws.String(s.fwARN), AvailabilityZone: aws.String("us-east-1a"), FlowOperationId: capture.FlowOperationId, + FirewallArn: aws.String(s.fwARN), AvailabilityZone: aws.String("us-east-1c"), FlowOperationId: capture.FlowOperationId, }) require.NoError(t, err) assert.Empty(t, res.Flows, "no data plane, no flows") @@ -706,3 +706,111 @@ func TestSDK_DescribeByARN_And_Deletes(t *testing.T) { _, err = s.c.CreateRuleGroup(bg, &networkfirewall.CreateRuleGroupInput{RuleGroupName: aws.String("dup"), Type: nftypes.RuleGroupTypeStateful, Capacity: aws.Int32(1)}) require.ErrorAs(t, err, &invalid, "a duplicate name is InvalidRequestException") } + +func TestSDK_SubnetMapping_IPAddressType_Echoed(t *testing.T) { + s := newStack(t) + + created, err := s.c.CreateFirewall(bg, &networkfirewall.CreateFirewallInput{ + FirewallName: aws.String("fw-ip"), FirewallPolicyArn: aws.String(s.policyARN), VpcId: aws.String("vpc-1a"), + SubnetMappings: []nftypes.SubnetMapping{{SubnetId: aws.String("subnet-9"), IPAddressType: nftypes.IPAddressTypeIpv4}}, + }) + require.NoError(t, err) + require.Len(t, created.Firewall.SubnetMappings, 1) + assert.Equal(t, nftypes.IPAddressTypeIpv4, created.Firewall.SubnetMappings[0].IPAddressType) + + assoc, err := s.c.AssociateSubnets(bg, &networkfirewall.AssociateSubnetsInput{ + FirewallName: aws.String("fw-ip"), + SubnetMappings: []nftypes.SubnetMapping{{SubnetId: aws.String("subnet-8"), IPAddressType: nftypes.IPAddressTypeDualstack}}, + }) + require.NoError(t, err) + + got := map[string]nftypes.IPAddressType{} + for _, m := range assoc.SubnetMappings { + got[aws.ToString(m.SubnetId)] = m.IPAddressType + } + + assert.Equal(t, nftypes.IPAddressTypeIpv4, got["subnet-9"]) + assert.Equal(t, nftypes.IPAddressTypeDualstack, got["subnet-8"]) + + d, err := s.c.DescribeFirewall(bg, &networkfirewall.DescribeFirewallInput{FirewallName: aws.String("fw-ip")}) + require.NoError(t, err) + assert.Len(t, d.Firewall.SubnetMappings, 2) +} + +func TestSDK_SyncStates_ZoneIsStable_FlowCaptureAcceptsIt(t *testing.T) { + s := newStack(t) + + _, err := s.c.CreateFirewall(bg, &networkfirewall.CreateFirewallInput{ + FirewallName: aws.String("fw-z"), FirewallPolicyArn: aws.String(s.policyARN), VpcId: aws.String("vpc-1a"), + SubnetMappings: []nftypes.SubnetMapping{{SubnetId: aws.String("subnet-a")}, {SubnetId: aws.String("subnet-b")}}, + }) + require.NoError(t, err) + + zoneOf := func(subnet string) (string, string) { + d, derr := s.c.DescribeFirewall(bg, &networkfirewall.DescribeFirewallInput{FirewallName: aws.String("fw-z")}) + require.NoError(t, derr) + + for az, st := range d.FirewallStatus.SyncStates { + if aws.ToString(st.Attachment.SubnetId) == subnet { + return az, aws.ToString(st.Attachment.EndpointId) + } + } + + return "", "" + } + + azA, epA := zoneOf("subnet-a") + azB, epB := zoneOf("subnet-b") + require.NotEmpty(t, azA) + require.NotEmpty(t, azB) + assert.NotEqual(t, azA, azB) + assert.NotEqual(t, epA, epB, "each zone has its own endpoint") + + _, err = s.c.DisassociateSubnets(bg, &networkfirewall.DisassociateSubnetsInput{ + FirewallName: aws.String("fw-z"), SubnetIds: []string{"subnet-a"}, + }) + require.NoError(t, err) + + azB2, epB2 := zoneOf("subnet-b") + assert.Equal(t, azB, azB2, "the remaining subnet keeps its zone") + assert.Equal(t, epB, epB2) + + _, err = s.c.StartFlowCapture(bg, &networkfirewall.StartFlowCaptureInput{ + FirewallArn: aws.String(arnOfFirewall(t, s, "fw-z")), AvailabilityZone: aws.String(azB), + FlowFilters: []nftypes.FlowFilter{{SourcePort: aws.String("80")}}, + }) + require.NoError(t, err) +} + +func arnOfFirewall(t *testing.T, s *stack, name string) string { + t.Helper() + + d, err := s.c.DescribeFirewall(bg, &networkfirewall.DescribeFirewallInput{FirewallName: aws.String(name)}) + require.NoError(t, err) + + return aws.ToString(d.Firewall.FirewallArn) +} + +func TestSDK_RuleGroup_DomainNameClash_And_MissingType(t *testing.T) { + s := newStack(t) + + _, err := s.c.CreateRuleGroup(bg, &networkfirewall.CreateRuleGroupInput{ + RuleGroupName: aws.String("dup"), Type: nftypes.RuleGroupTypeStateful, Capacity: aws.Int32(10), + }) + require.NoError(t, err) + + _, err = s.c.CreateRuleGroup(bg, &networkfirewall.CreateRuleGroupInput{ + RuleGroupName: aws.String("dup"), Type: nftypes.RuleGroupTypeStatefulDomain, Capacity: aws.Int32(10), + }) + + var invalid *nftypes.InvalidRequestException + require.ErrorAs(t, err, &invalid) + + _, err = s.c.CreateRuleGroup(bg, &networkfirewall.CreateRuleGroupInput{ + RuleGroupName: aws.String("dup"), Type: nftypes.RuleGroupTypeStateless, Capacity: aws.Int32(10), + }) + require.NoError(t, err) + + _, err = s.c.DescribeRuleGroup(bg, &networkfirewall.DescribeRuleGroupInput{RuleGroupName: aws.String("dup")}) + require.ErrorAs(t, err, &invalid) +} diff --git a/services/networkfirewall/driver/availability_zones.go b/services/networkfirewall/driver/availability_zones.go deleted file mode 100644 index 71870263e..000000000 --- a/services/networkfirewall/driver/availability_zones.go +++ /dev/null @@ -1,43 +0,0 @@ -package driver - -import "hash/fnv" - -// azLetters are the zone suffixes used to give a firewall's subnets stable, -// synthetic Availability Zone names (the emulator has no real subnet to AZ -// mapping). -const azLetters = "abcdef" - -// AvailabilityZoneFor returns the synthetic Availability Zone name for the -// index-th subnet of a firewall in region (index 0 -> "a"). -func AvailabilityZoneFor(region string, index int) string { - if index < 0 { - index = -index - } - - return region + string(azLetters[index%len(azLetters)]) -} - -// SyncZones returns the Availability Zones a firewall runs in: its explicit -// zones for a transit gateway-attached firewall, else one synthetic zone per -// subnet. -func SyncZones(f *Firewall, region string) []string { - if len(f.AvailabilityZones) > 0 { - return append([]string(nil), f.AvailabilityZones...) - } - - zones := make([]string, 0, len(f.SubnetIDs)) - for i := range f.SubnetIDs { - zones = append(zones, AvailabilityZoneFor(region, i)) - } - - return zones -} - -// ZoneForKey returns a stable synthetic Availability Zone name for an opaque key -// (a subnet id): the same key always maps to the same zone. -func ZoneForKey(region, key string) string { - h := fnv.New32a() - _, _ = h.Write([]byte(key)) - - return AvailabilityZoneFor(region, int(h.Sum32()%uint32(len(azLetters)))) -} diff --git a/services/networkfirewall/driver/driver.go b/services/networkfirewall/driver/driver.go index d95f9bf4e..373b21f21 100644 --- a/services/networkfirewall/driver/driver.go +++ b/services/networkfirewall/driver/driver.go @@ -22,6 +22,7 @@ type Firewall struct { PolicyARN string VPCID string SubnetIDs []string + SubnetDetails map[string]SubnetDetail AvailabilityZones []string TransitGatewayID string Description string @@ -36,12 +37,31 @@ type Firewall struct { Tags map[string]string } -// CreateFirewallConfig is the input to CreateFirewall. +// SubnetMapping names a subnet to attach a firewall to, with the optional IP +// address type AWS lets a caller pick per subnet. +type SubnetMapping struct { + SubnetID string + IPAddressType string +} + +// SubnetDetail is what the firewall records for each attached subnet: its IP +// address type, the Availability Zone it sits in and that zone's endpoint id. +// The zone and endpoint are assigned when the subnet is attached and never move +// while it stays attached. +type SubnetDetail struct { + IPAddressType string + AvailabilityZone string + EndpointID string +} + +// CreateFirewallConfig is the input to CreateFirewall. SubnetIPTypes optionally +// maps a subnet id in SubnetIDs to its IP address type. type CreateFirewallConfig struct { Name string PolicyARN string VPCID string SubnetIDs []string + SubnetIPTypes map[string]string AvailabilityZones []string TransitGatewayID string Description string @@ -233,7 +253,7 @@ type NetworkFirewall interface { ListRuleGroups(ctx context.Context, filter RuleGroupFilter, opts ListOptions) ([]RuleGroup, string, error) AssociateFirewallPolicy(ctx context.Context, firewallName, policyARN, updateToken string) (*Firewall, error) - AssociateSubnets(ctx context.Context, firewallName string, subnetIDs []string, updateToken string) (*Firewall, error) + AssociateSubnets(ctx context.Context, firewallName string, subnets []SubnetMapping, updateToken string) (*Firewall, error) DisassociateSubnets(ctx context.Context, firewallName string, subnetIDs []string, updateToken string) (*Firewall, error) UpdateFirewallDeleteProtection(ctx context.Context, firewallName string, enabled bool, updateToken string) (*Firewall, error) UpdateLoggingConfiguration(ctx context.Context, firewallName string, cfg *LoggingConfiguration) error From 0291885988abce5ec2bf688227262aec533e84cb Mon Sep 17 00:00:00 2001 From: Nitin Kumar Patel Date: Fri, 23 Oct 2026 02:49:13 +0530 Subject: [PATCH 11/31] fix(aws-eventbridge): API destination delivery over HTTP (#1663) * feat(eventbridge): deliver events to API destination targets Rule targets that are API destinations now call the endpoint over HTTP: path parameters, query and header parameters (connection values win), Basic, API key or OAuth client credentials auth with a cached token refreshed on 401/407, the destination's invocation rate limit, a 5-second timeout, retries per the target RetryPolicy on the codes EventBridge retries, and the dead-letter queue on failure. Target HttpParameters now round-trip. * fix(eventbridge): deliver to API destinations in the background * fix(aws-eventbridge): detach API destination delivery from the request context * test(providers): record EventBridge SetHTTPClient as a deliberate non-wiring --- docs/coverage/aws/eventbridge.md | 3 +- docs/coverage/azure/eventgrid.md | 3 +- docs/coverage/gcp/eventarc.md | 3 +- docs/coverage/nongoals/eventbus.md | 2 + .../eventbridge/api_destination_delivery.go | 501 ++++++++++++++++++ .../api_destination_delivery_test.go | 455 ++++++++++++++++ providers/aws/eventbridge/eventbridge.go | 56 +- providers/aws/eventbridge/oauth_token.go | 212 ++++++++ providers/wiring_parity_test.go | 7 + .../api_destination_delivery_sdk_test.go | 115 ++++ server/aws/eventbridge/types.go | 6 + services/eventbus/driver/driver.go | 4 + 12 files changed, 1355 insertions(+), 12 deletions(-) create mode 100644 docs/coverage/nongoals/eventbus.md create mode 100644 providers/aws/eventbridge/api_destination_delivery.go create mode 100644 providers/aws/eventbridge/api_destination_delivery_test.go create mode 100644 providers/aws/eventbridge/oauth_token.go create mode 100644 server/aws/eventbridge/api_destination_delivery_sdk_test.go diff --git a/docs/coverage/aws/eventbridge.md b/docs/coverage/aws/eventbridge.md index 335803a3b..319ebef7b 100644 --- a/docs/coverage/aws/eventbridge.md +++ b/docs/coverage/aws/eventbridge.md @@ -26,4 +26,5 @@ AWS's `eventbus` service ยท portable interface `driver.EventBus` ยท [AWS index]( ## Not in scope -_Not documented yet. See the [emulator boundary](../../../README.md) for cloudemu-wide non-goals._ +- AWS: API destination targets are called in the background after `PutEvents` returns, at most 64 deliveries at a time per emulator. A failed call is retried 2 times by default (AWS retries up to 185 times over 24 hours); an explicit `RetryPolicy.MaximumRetryAttempts` is honored. A `Retry-After` from the endpoint delays the next attempt by at most 5 seconds, and one delivery stops retrying after 30 seconds, whatever `MaximumEventAgeInSeconds` says. Then the event goes to the target's dead-letter queue. +- AWS: in-flight API destination deliveries, their retry state and cached OAuth tokens are not persisted in snapshots, and a delivery still running when the emulator stops is dropped. diff --git a/docs/coverage/azure/eventgrid.md b/docs/coverage/azure/eventgrid.md index 305cf5c0d..04024e5d2 100644 --- a/docs/coverage/azure/eventgrid.md +++ b/docs/coverage/azure/eventgrid.md @@ -26,4 +26,5 @@ Azure's `eventbus` service ยท portable interface `driver.EventBus` ยท [Azure ind ## Not in scope -_Not documented yet. See the [emulator boundary](../../../README.md) for cloudemu-wide non-goals._ +- AWS: API destination targets are called in the background after `PutEvents` returns, at most 64 deliveries at a time per emulator. A failed call is retried 2 times by default (AWS retries up to 185 times over 24 hours); an explicit `RetryPolicy.MaximumRetryAttempts` is honored. A `Retry-After` from the endpoint delays the next attempt by at most 5 seconds, and one delivery stops retrying after 30 seconds, whatever `MaximumEventAgeInSeconds` says. Then the event goes to the target's dead-letter queue. +- AWS: in-flight API destination deliveries, their retry state and cached OAuth tokens are not persisted in snapshots, and a delivery still running when the emulator stops is dropped. diff --git a/docs/coverage/gcp/eventarc.md b/docs/coverage/gcp/eventarc.md index 4c372e9cc..fd2edf3a9 100644 --- a/docs/coverage/gcp/eventarc.md +++ b/docs/coverage/gcp/eventarc.md @@ -26,4 +26,5 @@ GCP's `eventbus` service ยท portable interface `driver.EventBus` ยท [GCP index]( ## Not in scope -_Not documented yet. See the [emulator boundary](../../../README.md) for cloudemu-wide non-goals._ +- AWS: API destination targets are called in the background after `PutEvents` returns, at most 64 deliveries at a time per emulator. A failed call is retried 2 times by default (AWS retries up to 185 times over 24 hours); an explicit `RetryPolicy.MaximumRetryAttempts` is honored. A `Retry-After` from the endpoint delays the next attempt by at most 5 seconds, and one delivery stops retrying after 30 seconds, whatever `MaximumEventAgeInSeconds` says. Then the event goes to the target's dead-letter queue. +- AWS: in-flight API destination deliveries, their retry state and cached OAuth tokens are not persisted in snapshots, and a delivery still running when the emulator stops is dropped. diff --git a/docs/coverage/nongoals/eventbus.md b/docs/coverage/nongoals/eventbus.md new file mode 100644 index 000000000..27bb3df86 --- /dev/null +++ b/docs/coverage/nongoals/eventbus.md @@ -0,0 +1,2 @@ +- AWS: API destination targets are called in the background after `PutEvents` returns, at most 64 deliveries at a time per emulator. A failed call is retried 2 times by default (AWS retries up to 185 times over 24 hours); an explicit `RetryPolicy.MaximumRetryAttempts` is honored. A `Retry-After` from the endpoint delays the next attempt by at most 5 seconds, and one delivery stops retrying after 30 seconds, whatever `MaximumEventAgeInSeconds` says. Then the event goes to the target's dead-letter queue. +- AWS: in-flight API destination deliveries, their retry state and cached OAuth tokens are not persisted in snapshots, and a delivery still running when the emulator stops is dropped. diff --git a/providers/aws/eventbridge/api_destination_delivery.go b/providers/aws/eventbridge/api_destination_delivery.go new file mode 100644 index 000000000..e6d4fbe21 --- /dev/null +++ b/providers/aws/eventbridge/api_destination_delivery.go @@ -0,0 +1,501 @@ +package eventbridge + +import ( + "context" + "encoding/json" + "errors" + "io" + "net/http" + "net/url" + "strconv" + "strings" + "sync" + "time" + + "github.com/stackshy/cloudemu/v2/services/eventbus/driver" +) + +const ( + // apiDestinationTimeout is how long EventBridge waits for an API + // destination to answer before it times the request out. + apiDestinationTimeout = 5 * time.Second + + // defaultAPIDestinationRetries is how many times a failed API destination + // call is retried when the target's RetryPolicy does not say. Real + // EventBridge defaults to 185 attempts spread over 24 hours; the emulator + // uses a small default and honors an explicit MaximumRetryAttempts as given. + defaultAPIDestinationRetries = 2 + + // maxRetryAfter caps how long an endpoint's Retry-After can delay the next + // attempt, and maxRetryWindow caps how long one delivery keeps retrying, so + // a background delivery always ends in bounded time. + maxRetryAfter = 5 * time.Second + maxRetryWindow = 30 * time.Second + + // maxAPIDestinationDeliveries is how many API destination deliveries may + // be in flight at once per provider. Deliveries past it wait for a slot. + maxAPIDestinationDeliveries = 64 + + // defaultMaxEventAge is how long EventBridge keeps retrying an event when + // the RetryPolicy does not set MaximumEventAgeInSeconds. + defaultMaxEventAge = 24 * time.Hour + + retryBaseDelay = 50 * time.Millisecond + retryMaxDelay = time.Second + + // maxResponseBody bounds how much of a response is read before it is + // discarded. + maxResponseBody = 1 << 20 + + apiDestinationUserAgent = "Amazon/EventBridge/ApiDestinations" + apiDestinationRange = "bytes=0-1048575" + apiDestinationEncoding = "gzip,deflate" + apiDestinationConn = "close" + defaultContentType = "application/json; charset=utf-8" +) + +// HTTPDoer is the outbound HTTP seam API destination targets are called +// through. The default is a client with EventBridge's 5-second timeout that does +// not follow redirects; callers can wire their own with SetHTTPClient. +type HTTPDoer interface { + Do(req *http.Request) (*http.Response, error) +} + +// SetHTTPClient wires the client API destination targets call out with. +func (m *Mock) SetHTTPClient(c HTTPDoer) { m.httpClient = c } + +func defaultHTTPClient() HTTPDoer { + return &http.Client{ + Timeout: apiDestinationTimeout, + CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }, + } +} + +// removedHeaders are the headers EventBridge strips from every API destination +// request, whatever the connection or target sets. User-Agent and Range are +// among them because EventBridge sets its own values. +func removedHeaders() map[string]bool { + names := []string{ + "A-IM", "Accept-Charset", "Accept-Datetime", "Accept-Encoding", "Cache-Control", "Connection", + "Content-Encoding", "Content-Length", "Content-MD5", "Date", "Expect", "Forwarded", "From", "Host", + "HTTP2-Settings", "If-Match", "If-Modified-Since", "If-None-Match", "If-Range", "If-Unmodified-Since", + "Max-Forwards", "Origin", "Pragma", "Proxy-Authorization", "Range", "Referer", "TE", "Trailer", + "Transfer-Encoding", "User-Agent", "Upgrade", "Via", "Warning", + } + + out := make(map[string]bool, len(names)) + for _, n := range names { + out[http.CanonicalHeaderKey(n)] = true + } + + return out +} + +// targetHTTPParameters is a target's HttpParameters block. +type targetHTTPParameters struct { + PathParameterValues []string `json:"PathParameterValues"` + HeaderParameters map[string]string `json:"HeaderParameters"` + QueryStringParameters map[string]string `json:"QueryStringParameters"` +} + +// retryPolicy is a target's RetryPolicy block. +type retryPolicy struct { + MaximumRetryAttempts *int `json:"MaximumRetryAttempts"` + MaximumEventAgeInSeconds *int `json:"MaximumEventAgeInSeconds"` +} + +func parseHTTPParameters(raw string) targetHTTPParameters { + var p targetHTTPParameters + if raw != "" { + _ = json.Unmarshal([]byte(raw), &p) + } + + return p +} + +// retryLimits returns how many retries a failed call gets and the wall-clock +// time after which no further retry is made: the RetryPolicy's event age, but +// never more than maxRetryWindow from start. +func retryLimits(raw string, start time.Time) (retries int, deadline time.Time) { + var p retryPolicy + if raw != "" { + _ = json.Unmarshal([]byte(raw), &p) + } + + retries = defaultAPIDestinationRetries + if p.MaximumRetryAttempts != nil { + retries = *p.MaximumRetryAttempts + } + + age := defaultMaxEventAge + if p.MaximumEventAgeInSeconds != nil { + age = time.Duration(*p.MaximumEventAgeInSeconds) * time.Second + } + + return retries, start.Add(min(age, maxRetryWindow)) +} + +// destinationCall is everything one API destination request is built from: a +// copy of the destination and its connection, taken so no store lock is held +// while the request is in flight. +type destinationCall struct { + dest driver.APIDestination + conn driver.Connection + params targetHTTPParameters + body string +} + +// resolveDestinationCall looks up the API destination a target ARN names and +// its connection. It fails when either is missing or the connection is not +// authorized, which leaves the destination INACTIVE. +func (m *Mock) resolveDestinationCall(t *driver.Target, body string) (*destinationCall, bool) { + name := t.ARN[strings.Index(t.ARN, ":api-destination/")+len(":api-destination/"):] + if slash := strings.Index(name, "/"); slash != -1 { + name = name[:slash] + } + + dest, ok := m.apiDestinations.Get(name) + if !ok || !strings.HasPrefix(t.ARN, dest.ARN) { + return nil, false + } + + cd, ok := m.settleConnection(connectionNameFromARN(dest.ConnectionARN)) + if !ok || cd.Info.ARN != dest.ConnectionARN || cd.Info.State != connStateAuthorized { + return nil, false + } + + return &destinationCall{dest: dest, conn: cd.Info, params: parseHTTPParameters(t.HTTPParameters), body: body}, true +} + +func isAPIDestination(arn string) bool { + return strings.Contains(arn, ":api-destination/") +} + +// goDeliverAPIDestination runs one API destination delivery in the background, +// as EventBridge delivers after PutEvents has returned. It takes a delivery +// slot when one is free; otherwise a goroutine waits for one, so the caller +// never blocks. Rate-limit waits, retries and the dead-letter send all happen +// in that goroutine, with no lock held. The provider has no shutdown hook, so a +// delivery still running when the process exits is dropped; each one ends +// within maxRetryWindow plus one request timeout. +func (m *Mock) goDeliverAPIDestination(deliver func()) { + run := func() { + defer func() { <-m.apiDestSlots }() + + deliver() + } + + select { + case m.apiDestSlots <- struct{}{}: + go run() + default: + go func() { + m.apiDestSlots <- struct{}{} + + run() + }() + } +} + +// dispatchAPIDestination calls the HTTP endpoint of an API destination target, +// retrying per the target's RetryPolicy on the status codes EventBridge retries +// (401, 407, 409, 429 and 5xx) and on timeouts. It reports whether the call +// succeeded; a failure goes to the target's dead-letter queue. +func (m *Mock) dispatchAPIDestination(ctx context.Context, t *driver.Target, body string) bool { + call, ok := m.resolveDestinationCall(t, body) + if !ok { + return false + } + + retries, deadline := retryLimits(t.RetryPolicy, time.Now()) + + for attempt := 0; ; attempt++ { + delivered, retry, retryAfter := m.callOnce(ctx, call) + if delivered { + return true + } + + if !retry || attempt >= retries { + return false + } + + delay := retryDelay(attempt, retryAfter) + if time.Now().Add(delay).After(deadline) { + return false + } + + time.Sleep(delay) + } +} + +// callOnce makes one rate-limited call and reports whether it delivered the +// event, whether a failure may be retried, and the endpoint's Retry-After. A +// 401 or 407 drops the cached OAuth token so a retry fetches a fresh one. +func (m *Mock) callOnce(ctx context.Context, call *destinationCall) (delivered, retry bool, retryAfter time.Duration) { + time.Sleep(m.limiter.reserve(call.dest.ARN, call.dest.InvocationRateLimitPerSecond, time.Now())) + + status, retryAfter, err := m.sendToDestination(ctx, call) + if err == nil && status >= http.StatusOK && status < http.StatusMultipleChoices { + return true, false, 0 + } + + if status == http.StatusUnauthorized || status == http.StatusProxyAuthRequired { + m.tokens.forget(call.conn.ARN) + } + + return false, shouldRetry(status, retryAfter, err), retryAfter +} + +// shouldRetry reports whether a failed call may be retried: not when no OAuth +// token could be had, not when the endpoint sent a negative Retry-After, and +// otherwise only for the outcomes EventBridge retries. +func shouldRetry(status int, retryAfter time.Duration, err error) bool { + if errors.Is(err, errOAuthFailed) || retryAfter < 0 { + return false + } + + return retryable(status, err) +} + +// retryable reports whether EventBridge retries a call that ended with status +// (or with a transport error such as a timeout). +func retryable(status int, err error) bool { + if err != nil { + return true + } + + switch status { + case http.StatusUnauthorized, http.StatusProxyAuthRequired, http.StatusConflict, http.StatusTooManyRequests: + return true + default: + return status >= http.StatusInternalServerError + } +} + +// retryDelay is the wait before retry number attempt+1: an exponential backoff +// capped at retryMaxDelay, or the endpoint's Retry-After (capped at +// maxRetryAfter) when that is longer, as EventBridge picks the more +// conservative of the two. +func retryDelay(attempt int, retryAfter time.Duration) time.Duration { + const maxShift = 5 + + backoff := min(retryBaseDelay<= time.Second { + w = &rateWindow{start: now} + l.windows[key] = w + } + + if w.count >= perSecond { + w.start = w.start.Add(time.Second) + w.count = 0 + } + + w.count++ + + return max(w.start.Sub(now), 0) +} diff --git a/providers/aws/eventbridge/api_destination_delivery_test.go b/providers/aws/eventbridge/api_destination_delivery_test.go new file mode 100644 index 000000000..a69acc5cd --- /dev/null +++ b/providers/aws/eventbridge/api_destination_delivery_test.go @@ -0,0 +1,455 @@ +package eventbridge + +import ( + "context" + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "sync" + "sync/atomic" + "testing" + "time" + + "github.com/stackshy/cloudemu/v2/services/eventbus/driver" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// receivedRequest is what a test endpoint saw. +type receivedRequest struct { + method string + path string + query map[string][]string + header http.Header + body string +} + +// recordingEndpoint is an httptest server that answers each call with the next +// status in statuses (repeating the last) and records every request. +type recordingEndpoint struct { + *httptest.Server + + mu sync.Mutex + got []receivedRequest + statuses []int + headers map[string]string +} + +func newRecordingEndpoint(t *testing.T, statuses ...int) *recordingEndpoint { + t.Helper() + + e := &recordingEndpoint{statuses: statuses} + e.Server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + body, _ := io.ReadAll(r.Body) + + e.mu.Lock() + e.got = append(e.got, receivedRequest{ + method: r.Method, path: r.URL.Path, query: r.URL.Query(), header: r.Header.Clone(), body: string(body), + }) + status := http.StatusOK + if n := len(e.got); len(e.statuses) > 0 { + status = e.statuses[min(n, len(e.statuses))-1] + } + + for k, v := range e.headers { + w.Header().Set(k, v) + } + e.mu.Unlock() + + w.WriteHeader(status) + })) + t.Cleanup(e.Close) + + return e +} + +func (e *recordingEndpoint) requests() []receivedRequest { + e.mu.Lock() + defer e.mu.Unlock() + + return append([]receivedRequest(nil), e.got...) +} + +// deliveryFixture wires a mock with a connection, an API destination to +// endpoint, and a rule on the default bus whose single target is that +// destination. +type deliveryFixture struct { + m *Mock + sqs *recordingSQS +} + +func newDeliveryFixture(t *testing.T, auth *driver.ConnectionConfig, endpoint, method string, target driver.Target) *deliveryFixture { + t.Helper() + + m, _ := newConnectionMock(t) + sqs := &recordingSQS{} + m.SetSQSDeliverer(sqs) + ctx := context.Background() + + c, err := m.CreateConnection(ctx, auth) + require.NoError(t, err) + + d, err := m.CreateAPIDestination(ctx, &driver.APIDestinationConfig{ + Name: "dest", ConnectionARN: c.ARN, InvocationEndpoint: endpoint, HTTPMethod: method, + }) + require.NoError(t, err) + + _, err = m.PutRule(ctx, &driver.RuleConfig{Name: "r", EventPattern: `{"source":["app"]}`}) + require.NoError(t, err) + + target.ID, target.ARN = "dest", d.ARN + require.NoError(t, m.PutTargets(ctx, "", "r", []driver.Target{target})) + + return &deliveryFixture{m: m, sqs: sqs} +} + +// put puts one event and waits for its background delivery to finish. +func (f *deliveryFixture) put(t *testing.T, detail string) { + t.Helper() + + _, err := f.m.PutEvents(context.Background(), []driver.Event{{Source: "app", DetailType: "OrderCreated", Detail: detail}}) + require.NoError(t, err) + + f.settle(t) +} + +// settle waits until no API destination delivery holds a slot. +func (f *deliveryFixture) settle(t *testing.T) { + t.Helper() + + require.Eventually(t, func() bool { return len(f.m.apiDestSlots) == 0 }, 10*time.Second, 5*time.Millisecond) +} + +func apiKeyConnection() *driver.ConnectionConfig { + return &driver.ConnectionConfig{ + Name: "conn", AuthorizationType: "API_KEY", + AuthParameters: &driver.ConnectionAuthParameters{ + APIKey: &driver.ConnectionAPIKeyAuth{APIKeyName: "x-api-key", APIKeyValue: "s3cr3t"}, + InvocationHTTPParameters: &driver.ConnectionHTTPParameters{ + HeaderParameters: []driver.HTTPParameter{{Key: "x-shared", Value: "from-connection"}}, + QueryStringParameters: []driver.HTTPParameter{{Key: "shared", Value: "from-connection"}}, + BodyParameters: []driver.HTTPParameter{{Key: "tenant", Value: "acme"}}, + }, + }, + } +} + +func TestAPIDestinationDeliversWithAPIKey(t *testing.T) { + e := newRecordingEndpoint(t) + f := newDeliveryFixture(t, apiKeyConnection(), e.URL+"/orders/*/items/*", "POST", driver.Target{ + HTTPParameters: `{"PathParameterValues":["o-1","i 2"],` + + `"HeaderParameters":{"x-shared":"from-target","x-target":"t","User-Agent":"spoofed"},` + + `"QueryStringParameters":{"shared":"from-target","only":"target"}}`, + }) + + f.put(t, `{"id":"42"}`) + + got := e.requests() + require.Len(t, got, 1) + + r := got[0] + assert.Equal(t, "POST", r.method) + assert.Equal(t, "/orders/o-1/items/i 2", r.path) + assert.Equal(t, "s3cr3t", r.header.Get("x-api-key")) + assert.Equal(t, "from-connection", r.header.Get("x-shared"), "connection headers win over target headers") + assert.Equal(t, "t", r.header.Get("x-target")) + assert.Equal(t, "Amazon/EventBridge/ApiDestinations", r.header.Get("User-Agent"), "User-Agent cannot be overridden") + assert.Equal(t, "application/json; charset=utf-8", r.header.Get("Content-Type")) + assert.Equal(t, "gzip,deflate", r.header.Get("Accept-Encoding")) + assert.Equal(t, "close", r.header.Get("Connection")) + assert.Equal(t, []string{"from-connection"}, r.query["shared"], "connection query parameters win") + assert.Equal(t, []string{"target"}, r.query["only"]) + + var body map[string]any + require.NoError(t, json.Unmarshal([]byte(r.body), &body)) + assert.Equal(t, "OrderCreated", body["detail-type"]) + assert.Equal(t, "acme", body["tenant"], "connection body parameters are merged into the event") +} + +func TestAPIDestinationBasicAuthAndInputTransformer(t *testing.T) { + e := newRecordingEndpoint(t) + f := newDeliveryFixture(t, &driver.ConnectionConfig{ + Name: "conn", AuthorizationType: "BASIC", + AuthParameters: &driver.ConnectionAuthParameters{Basic: &driver.ConnectionBasicAuth{Username: "user", Password: "pass"}}, + }, e.URL+"/hook", "PUT", driver.Target{ + InputTransformer: `{"InputPathsMap":{"id":"$.detail.id"},"InputTemplate":"{\"order\": }"}`, + }) + + f.put(t, `{"id":"42"}`) + + got := e.requests() + require.Len(t, got, 1) + + user, pass, ok := (&http.Request{Header: got[0].header}).BasicAuth() + assert.True(t, ok) + assert.Equal(t, "user", user) + assert.Equal(t, "pass", pass) + assert.Equal(t, "PUT", got[0].method) + assert.JSONEq(t, `{"order":"42"}`, got[0].body) +} + +// oauthServer is a fake token endpoint that issues token-1, token-2, ... and +// counts how often it is called. +func oauthServer(t *testing.T, status int) (*httptest.Server, *atomic.Int32) { + t.Helper() + + var calls atomic.Int32 + + s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + n := calls.Add(1) + + id, secret, ok := r.BasicAuth() + _ = r.ParseForm() + + if !ok || id != "client" || secret != "csecret" || r.PostForm.Get("grant_type") != "client_credentials" || + r.PostForm.Get("scope") != "events" { + w.WriteHeader(http.StatusBadRequest) + return + } + + w.WriteHeader(status) + _, _ = io.WriteString(w, `{"access_token":"token-`+string(rune('0'+n))+`","expires_in":3600}`) + })) + t.Cleanup(s.Close) + + return s, &calls +} + +func oauthConnection(tokenURL string) *driver.ConnectionConfig { + return &driver.ConnectionConfig{ + Name: "conn", AuthorizationType: "OAUTH_CLIENT_CREDENTIALS", + AuthParameters: &driver.ConnectionAuthParameters{OAuth: &driver.ConnectionOAuth{ + AuthorizationEndpoint: tokenURL, HTTPMethod: "POST", + ClientParameters: &driver.ConnectionOAuthClient{ClientID: "client", ClientSecret: "csecret"}, + OAuthHTTPParameters: &driver.ConnectionHTTPParameters{ + BodyParameters: []driver.HTTPParameter{{Key: "scope", Value: "events"}}, + }, + }}, + } +} + +func TestAPIDestinationOAuthTokenCachedAndRefreshedOn401(t *testing.T) { + tokens, calls := oauthServer(t, http.StatusOK) + e := newRecordingEndpoint(t, http.StatusOK, http.StatusOK, http.StatusUnauthorized, http.StatusOK) + f := newDeliveryFixture(t, oauthConnection(tokens.URL), e.URL+"/hook", "POST", driver.Target{}) + + f.put(t, `{}`) + f.put(t, `{}`) + assert.Equal(t, int32(1), calls.Load(), "the token is cached between calls") + + // The third call is rejected with 401: the token is dropped, a new one is + // fetched, and the retry succeeds. + f.put(t, `{}`) + + got := e.requests() + require.Len(t, got, 4) + assert.Equal(t, "Bearer token-1", got[0].header.Get("Authorization")) + assert.Equal(t, "Bearer token-1", got[2].header.Get("Authorization")) + assert.Equal(t, "Bearer token-2", got[3].header.Get("Authorization")) + assert.Equal(t, int32(2), calls.Load()) + assert.Empty(t, f.sqs.got(queueA)) +} + +func TestAPIDestinationOAuthFailureDeauthorizes(t *testing.T) { + tokens, _ := oauthServer(t, http.StatusInternalServerError) + e := newRecordingEndpoint(t) + f := newDeliveryFixture(t, oauthConnection(tokens.URL), e.URL+"/hook", "POST", driver.Target{ + DeadLetterConfig: `{"Arn":"` + queueA + `"}`, + }) + + f.put(t, `{}`) + + assert.Empty(t, e.requests(), "no call is made without a token") + assert.Len(t, f.sqs.got(queueA), 1, "the event goes to the dead-letter queue") + + c, err := f.m.DescribeConnection(context.Background(), "conn") + require.NoError(t, err) + assert.Equal(t, "DEAUTHORIZED", c.State) + assert.NotEmpty(t, c.StateReason) + + d, err := f.m.DescribeAPIDestination(context.Background(), "dest") + require.NoError(t, err) + assert.Equal(t, "INACTIVE", d.State) +} + +func TestAPIDestinationRetriesThenDeadLetters(t *testing.T) { + tests := []struct { + name string + statuses []int + policy string + calls int + dlq bool + }{ + {name: "5xx retried up to MaximumRetryAttempts", statuses: []int{500}, policy: `{"MaximumRetryAttempts":2}`, calls: 3, dlq: true}, + {name: "429 then success", statuses: []int{429, 200}, policy: `{"MaximumRetryAttempts":2}`, calls: 2}, + {name: "4xx not retried", statuses: []int{400}, policy: `{"MaximumRetryAttempts":5}`, calls: 1, dlq: true}, + {name: "3xx not retried", statuses: []int{302}, policy: `{"MaximumRetryAttempts":5}`, calls: 1, dlq: true}, + {name: "no retries", statuses: []int{503}, policy: `{"MaximumRetryAttempts":0}`, calls: 1, dlq: true}, + {name: "default policy", statuses: []int{500}, calls: 1 + defaultAPIDestinationRetries, dlq: true}, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + e := newRecordingEndpoint(t, tc.statuses...) + f := newDeliveryFixture(t, apiKeyConnection(), e.URL, "POST", driver.Target{ + RetryPolicy: tc.policy, DeadLetterConfig: `{"Arn":"` + queueA + `"}`, + }) + + f.put(t, `{"n":1}`) + + assert.Len(t, e.requests(), tc.calls) + + if tc.dlq { + dlq := f.sqs.got(queueA) + require.Len(t, dlq, 1) + assert.Equal(t, "OrderCreated", dlq[0]["detail-type"], "the DLQ gets the original event") + } else { + assert.Empty(t, f.sqs.got(queueA)) + } + }) + } +} + +func TestAPIDestinationNegativeRetryAfterStops(t *testing.T) { + e := newRecordingEndpoint(t, http.StatusServiceUnavailable) + e.headers = map[string]string{"Retry-After": "-1"} + + f := newDeliveryFixture(t, apiKeyConnection(), e.URL, "POST", driver.Target{RetryPolicy: `{"MaximumRetryAttempts":5}`}) + + f.put(t, `{}`) + assert.Len(t, e.requests(), 1) +} + +func TestAPIDestinationTimeoutIsRetried(t *testing.T) { + var calls atomic.Int32 + + slow := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + calls.Add(1) + time.Sleep(200 * time.Millisecond) + w.WriteHeader(http.StatusOK) + })) + t.Cleanup(slow.Close) + + f := newDeliveryFixture(t, apiKeyConnection(), slow.URL, "POST", driver.Target{ + RetryPolicy: `{"MaximumRetryAttempts":1}`, DeadLetterConfig: `{"Arn":"` + queueA + `"}`, + }) + f.m.SetHTTPClient(&http.Client{Timeout: 50 * time.Millisecond}) + + f.put(t, `{}`) + + assert.Equal(t, int32(2), calls.Load(), "a timed-out call is retried") + assert.Len(t, f.sqs.got(queueA), 1) +} + +func TestAPIDestinationInactiveIsNotCalled(t *testing.T) { + e := newRecordingEndpoint(t) + f := newDeliveryFixture(t, apiKeyConnection(), e.URL, "POST", driver.Target{DeadLetterConfig: `{"Arn":"` + queueA + `"}`}) + + _, err := f.m.DeauthorizeConnection(context.Background(), "conn") + require.NoError(t, err) + + f.put(t, `{}`) + + assert.Empty(t, e.requests()) + assert.Len(t, f.sqs.got(queueA), 1) +} + +// TestAPIDestinationConcurrentDelivery puts events from several goroutines to an +// OAuth destination so the race detector checks the token cache and limiter. +func TestAPIDestinationConcurrentDelivery(t *testing.T) { + tokens, _ := oauthServer(t, http.StatusOK) + e := newRecordingEndpoint(t) + f := newDeliveryFixture(t, oauthConnection(tokens.URL), e.URL, "POST", driver.Target{}) + + var wg sync.WaitGroup + + for range 6 { + wg.Add(1) + + go func() { + defer wg.Done() + + _, err := f.m.PutEvents(context.Background(), []driver.Event{{Source: "app", DetailType: "OrderCreated", Detail: `{}`}}) + assert.NoError(t, err) + }() + } + + wg.Wait() + f.settle(t) + + assert.Len(t, e.requests(), 6) +} + +func TestRateLimiterSpacesCalls(t *testing.T) { + var l rateLimiter + + now := time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC) + + assert.Zero(t, l.reserve("d", 2, now)) + assert.Zero(t, l.reserve("d", 2, now.Add(100*time.Millisecond))) + assert.Equal(t, 800*time.Millisecond, l.reserve("d", 2, now.Add(200*time.Millisecond)), "the third call waits for the next window") + assert.Equal(t, 800*time.Millisecond, l.reserve("d", 2, now.Add(200*time.Millisecond)), "and so does the fourth") + assert.Equal(t, 1800*time.Millisecond, l.reserve("d", 2, now.Add(200*time.Millisecond)), "the fifth waits a window more") + assert.Zero(t, l.reserve("other", 1, now), "destinations are limited separately") + assert.Zero(t, l.reserve("d", 0, now), "no limit") +} + +func TestAPIDestinationRateLimitDelaysCalls(t *testing.T) { + e := newRecordingEndpoint(t) + f := newDeliveryFixture(t, apiKeyConnection(), e.URL, "POST", driver.Target{}) + + rate := 1 + _, err := f.m.UpdateAPIDestination(context.Background(), &driver.APIDestinationUpdate{Name: "dest", InvocationRateLimitPerSecond: &rate}) + require.NoError(t, err) + + start := time.Now() + + f.put(t, `{}`) + f.put(t, `{}`) + + assert.Len(t, e.requests(), 2) + assert.GreaterOrEqual(t, time.Since(start), 900*time.Millisecond, "the second call waits for the next one-second window") +} + +// TestAPIDestinationDeliveryDoesNotBlockPutEvents checks PutEvents returns at +// once while a slow endpoint is still answering, and the endpoint still gets +// the event. +func TestAPIDestinationDeliveryDoesNotBlockPutEvents(t *testing.T) { + var calls atomic.Int32 + + slow := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + time.Sleep(2 * time.Second) + calls.Add(1) + w.WriteHeader(http.StatusOK) + })) + t.Cleanup(slow.Close) + + f := newDeliveryFixture(t, apiKeyConnection(), slow.URL, "POST", driver.Target{}) + + start := time.Now() + _, err := f.m.PutEvents(context.Background(), []driver.Event{{Source: "app", DetailType: "OrderCreated", Detail: `{}`}}) + require.NoError(t, err) + assert.Less(t, time.Since(start), 200*time.Millisecond, "PutEvents does not wait for the endpoint") + + require.Eventually(t, func() bool { return calls.Load() == 1 }, 10*time.Second, 10*time.Millisecond) + f.settle(t) +} + +func TestRetryDelayCapsRetryAfter(t *testing.T) { + assert.Equal(t, retryBaseDelay, retryDelay(0, 0)) + assert.Equal(t, retryMaxDelay, retryDelay(10, 0), "the backoff is capped") + assert.Equal(t, 3*time.Second, retryDelay(0, 3*time.Second), "a longer Retry-After wins") + assert.Equal(t, maxRetryAfter, retryDelay(0, 30*time.Second), "Retry-After is capped") +} + +func TestRetryLimitsCapTheRetryWindow(t *testing.T) { + start := time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC) + + retries, deadline := retryLimits("", start) + assert.Equal(t, defaultAPIDestinationRetries, retries) + assert.Equal(t, start.Add(maxRetryWindow), deadline, "the 24-hour default is capped") + + _, deadline = retryLimits(`{"MaximumEventAgeInSeconds":10}`, start) + assert.Equal(t, start.Add(10*time.Second), deadline, "a shorter event age is kept") +} diff --git a/providers/aws/eventbridge/eventbridge.go b/providers/aws/eventbridge/eventbridge.go index 0228c51e9..d6094d720 100644 --- a/providers/aws/eventbridge/eventbridge.go +++ b/providers/aws/eventbridge/eventbridge.go @@ -104,7 +104,16 @@ type Mock struct { // secrets holds each connection's credentials; nil leaves SecretArn a bare // identifier. secrets ConnectionSecretStore - replays *memstore.Store[driver.Replay] + + // httpClient calls API destination targets; tokens caches each OAuth + // connection's access token and limiter enforces each destination's + // invocation rate. None of them is held locked during a call. + httpClient HTTPDoer + tokens tokenCache + limiter rateLimiter + // apiDestSlots bounds how many API destination deliveries run at once. + apiDestSlots chan struct{} + replays *memstore.Store[driver.Replay] // activeReplays counts replays currently delivering, bounded by // maxActiveReplays. activeReplays atomic.Int32 @@ -159,6 +168,8 @@ func New(opts *config.Options) *Mock { connections: memstore.New[connectionData](), apiDestinations: memstore.New[driver.APIDestination](), + httpClient: defaultHTTPClient(), + apiDestSlots: make(chan struct{}, maxAPIDestinationDeliveries), } // Create the default event bus automatically. @@ -330,8 +341,8 @@ func (m *Mock) PutRule(_ context.Context, cfg *driver.RuleConfig) (*driver.Rule, targets: memstore.New[driver.Target](), } - for _, t := range rule.Targets { - rd.targets.Set(t.ID, t) + for i := range rule.Targets { + rd.targets.Set(rule.Targets[i].ID, rule.Targets[i]) } bd.rules.Set(cfg.Name, rd) @@ -468,8 +479,8 @@ func (m *Mock) PutTargets(_ context.Context, eventBus, ruleName string, targets return errors.Newf(errors.NotFound, "rule %q not found on event bus %q", ruleName, busName) } - for _, t := range targets { - rd.targets.Set(t.ID, t) + for i := range targets { + rd.targets.Set(targets[i].ID, targets[i]) } rd.rule.Targets = targetsFromStore(rd.targets) @@ -602,14 +613,41 @@ func (m *Mock) deliverEnvelope(ctx context.Context, matched []driver.Rule, event // deliverToTarget dispatches one target and, on failure, routes the original // event to the target's dead-letter queue when one is configured. +// +// An API destination target is called over HTTP in the background (see +// goDeliverAPIDestination), so a slow or rate-limited endpoint never holds up +// the PutEvents caller. Every other target is dispatched in line. func (m *Mock) deliverToTarget( ctx context.Context, rule *driver.Rule, t *driver.Target, busName string, envelope []byte, reserved map[string]json.RawMessage, ) { - if m.dispatchTarget(ctx, t.ARN, targetBody(t, envelope, reserved)) { + body := targetBody(t, envelope, reserved) + + if isAPIDestination(t.ARN) { + target, ruleName := *t, rule.Name + // The delivery outlives the PutEvents request, whose context the HTTP + // server cancels once the handler returns. + bgCtx := context.WithoutCancel(ctx) + + m.goDeliverAPIDestination(func() { + if !m.dispatchAPIDestination(bgCtx, &target, body) { + m.deadLetter(bgCtx, ruleName, &target, busName, envelope) + } + }) + return } - dims := map[string]string{"RuleName": rule.Name, busDimension: busName} + if m.dispatchTarget(ctx, t.ARN, body) { + return + } + + m.deadLetter(ctx, rule.Name, t, busName, envelope) +} + +// deadLetter records a failed delivery and sends the original event to the +// target's dead-letter queue when one is configured. +func (m *Mock) deadLetter(ctx context.Context, ruleName string, t *driver.Target, busName string, envelope []byte) { + dims := map[string]string{"RuleName": ruleName, busDimension: busName} m.emitMetric(ctx, "FailedInvocations", 1, dims) dlqARN := deadLetterARN(t.DeadLetterConfig) @@ -851,8 +889,8 @@ func targetsFromStore(store *memstore.Store[driver.Target]) []driver.Target { all := store.All() targets := make([]driver.Target, 0, len(all)) - for _, t := range all { - targets = append(targets, t) + for id := range all { + targets = append(targets, all[id]) } return targets diff --git a/providers/aws/eventbridge/oauth_token.go b/providers/aws/eventbridge/oauth_token.go new file mode 100644 index 000000000..1f56a9a3d --- /dev/null +++ b/providers/aws/eventbridge/oauth_token.go @@ -0,0 +1,212 @@ +package eventbridge + +import ( + "context" + "encoding/json" + stderrors "errors" + "fmt" + "io" + "net/http" + "net/url" + "strings" + "sync" + "time" + + "github.com/stackshy/cloudemu/v2/services/eventbus/driver" +) + +// errOAuthFailed marks a call that could not get an OAuth token. EventBridge +// deauthorizes the connection, so the call is not retried. +var errOAuthFailed = stderrors.New("oauth token request failed") + +var ( + errTokenStatus = stderrors.New("authorization endpoint returned an error status") + errNoAccessToken = stderrors.New("authorization endpoint returned no access_token") +) + +// tokenCache holds the OAuth access token of each connection, keyed by the +// connection ARN, until it expires or the endpoint rejects it. It is not +// persisted: a restarted emulator fetches a new token on first use. +type tokenCache struct { + mu sync.Mutex + tokens map[string]cachedToken +} + +type cachedToken struct { + value string + expires time.Time // zero means it does not expire +} + +func (c *tokenCache) get(key string, now time.Time) (string, bool) { + c.mu.Lock() + defer c.mu.Unlock() + + t, ok := c.tokens[key] + if !ok || (!t.expires.IsZero() && !now.Before(t.expires)) { + return "", false + } + + return t.value, true +} + +func (c *tokenCache) put(key string, t cachedToken) { + c.mu.Lock() + defer c.mu.Unlock() + + if c.tokens == nil { + c.tokens = map[string]cachedToken{} + } + + c.tokens[key] = t +} + +// forget drops a connection's token so the next call fetches a new one, which +// is what EventBridge does when an endpoint answers 401 or 407. +func (c *tokenCache) forget(key string) { + c.mu.Lock() + defer c.mu.Unlock() + + delete(c.tokens, key) +} + +type tokenResponse struct { + AccessToken string `json:"access_token"` + ExpiresIn int `json:"expires_in"` +} + +// oauthToken returns the connection's cached access token, fetching one with +// the client credentials grant when there is none. A failed fetch deauthorizes +// the connection. No lock is held while the token endpoint is called. +func (m *Mock) oauthToken(ctx context.Context, conn *driver.Connection) (string, error) { + if token, ok := m.tokens.get(conn.ARN, time.Now()); ok { + return token, nil + } + + tok, err := m.fetchOAuthToken(ctx, conn.AuthParameters.OAuth) + if err != nil { + m.deauthorizeAfterFailure(conn.Name, err) + + return "", fmt.Errorf("%w: %w", errOAuthFailed, err) + } + + m.tokens.put(conn.ARN, tok) + + return tok.value, nil +} + +// fetchOAuthToken calls the authorization endpoint with the client credentials +// (as HTTP Basic auth) and grant_type=client_credentials, plus the connection's +// OAuth HTTP parameters, and reads the access token from the JSON reply. +func (m *Mock) fetchOAuthToken(ctx context.Context, o *driver.ConnectionOAuth) (cachedToken, error) { + req, err := buildTokenRequest(ctx, o) + if err != nil { + return cachedToken{}, err + } + + resp, err := m.httpClient.Do(req) + if err != nil { + return cachedToken{}, err + } + + defer resp.Body.Close() + + data, err := io.ReadAll(io.LimitReader(resp.Body, maxResponseBody)) + if err != nil { + return cachedToken{}, err + } + + if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices { + return cachedToken{}, fmt.Errorf("%w: %d", errTokenStatus, resp.StatusCode) + } + + var tr tokenResponse + if err := json.Unmarshal(data, &tr); err != nil || tr.AccessToken == "" { + return cachedToken{}, errNoAccessToken + } + + tok := cachedToken{value: tr.AccessToken} + if tr.ExpiresIn > 0 { + tok.expires = time.Now().Add(time.Duration(tr.ExpiresIn) * time.Second) + } + + return tok, nil +} + +// tokenParams returns the form and query of a token request: the client +// credentials grant plus the connection's OAuth body and query parameters. +func tokenParams(o *driver.ConnectionOAuth, query url.Values) url.Values { + form := url.Values{"grant_type": {"client_credentials"}} + + if p := o.OAuthHTTPParameters; p != nil { + for _, b := range p.BodyParameters { + form.Set(b.Key, b.Value) + } + + for _, q := range p.QueryStringParameters { + query.Set(q.Key, q.Value) + } + } + + return form +} + +func buildTokenRequest(ctx context.Context, o *driver.ConnectionOAuth) (*http.Request, error) { + endpoint, err := url.Parse(o.AuthorizationEndpoint) + if err != nil { + return nil, err + } + + query := endpoint.Query() + form := tokenParams(o, query) + + var body io.Reader + + if o.HTTPMethod == http.MethodGet { + for k, v := range form { + query[k] = v + } + } else { + body = strings.NewReader(form.Encode()) + } + + endpoint.RawQuery = query.Encode() + + req, err := http.NewRequestWithContext(ctx, o.HTTPMethod, endpoint.String(), body) + if err != nil { + return nil, err + } + + if body != nil { + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + } + + setTokenHeaders(req, o) + + return req, nil +} + +// setTokenHeaders adds the connection's OAuth headers and the client +// credentials, sent as HTTP Basic auth. +func setTokenHeaders(req *http.Request, o *driver.ConnectionOAuth) { + if p := o.OAuthHTTPParameters; p != nil { + for _, h := range p.HeaderParameters { + req.Header.Set(h.Key, h.Value) + } + } + + if c := o.ClientParameters; c != nil { + req.SetBasicAuth(c.ClientID, c.ClientSecret) + } +} + +// deauthorizeAfterFailure moves a connection whose token request failed to +// DEAUTHORIZED, recording why, as EventBridge does. Its API destinations then +// read INACTIVE. +func (m *Mock) deauthorizeAfterFailure(name string, cause error) { + m.connections.Update(name, func(c connectionData) connectionData { + c.Info.State = connStateDeauthorized + c.Info.StateReason = "Failed to authorize with the authorization endpoint: " + cause.Error() + + return c + }) +} diff --git a/providers/wiring_parity_test.go b/providers/wiring_parity_test.go index 6b2b4b654..c0982610c 100644 --- a/providers/wiring_parity_test.go +++ b/providers/wiring_parity_test.go @@ -103,6 +103,13 @@ var awsExemptions = []exemption{ "in production without any aws.go wiring. SetHTTPClient is only an override that " + "swaps in a fake or instrumented client, so New deliberately does not call it.", }, + { + field: "EventBridge", + method: "SetHTTPClient", + reason: "eventbridge.New defaults API destination delivery to a real net/http client " + + "that does not follow redirects, so targets reach their endpoints without any " + + "aws.go wiring. SetHTTPClient only swaps in a fake or instrumented client.", + }, } // azureExemptions lists Azure's one deliberate non-wiring. diff --git a/server/aws/eventbridge/api_destination_delivery_sdk_test.go b/server/aws/eventbridge/api_destination_delivery_sdk_test.go new file mode 100644 index 000000000..8a80e2e60 --- /dev/null +++ b/server/aws/eventbridge/api_destination_delivery_sdk_test.go @@ -0,0 +1,115 @@ +package eventbridge_test + +import ( + "context" + "io" + "net/http" + "net/http/httptest" + "sync" + "testing" + "time" + + "github.com/aws/aws-sdk-go-v2/aws" + awseb "github.com/aws/aws-sdk-go-v2/service/eventbridge" + ebtypes "github.com/aws/aws-sdk-go-v2/service/eventbridge/types" +) + +// TestSDKAPIDestinationTargetDelivers puts an event through the SDK and checks +// the API destination's endpoint receives it with the API key header and the +// target's path and query parameters, and that HttpParameters round-trips on +// ListTargetsByRule. +func TestSDKAPIDestinationTargetDelivers(t *testing.T) { + var ( + mu sync.Mutex + got *http.Request + body string + ) + + hook := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + b, _ := io.ReadAll(r.Body) + + mu.Lock() + got, body = r.Clone(context.Background()), string(b) + mu.Unlock() + + w.WriteHeader(http.StatusOK) + })) + t.Cleanup(hook.Close) + + eb, _ := newEBAndSecrets(t) + ctx := context.Background() + + conn, err := eb.CreateConnection(ctx, &awseb.CreateConnectionInput{ + Name: aws.String("c1"), AuthorizationType: ebtypes.ConnectionAuthorizationTypeApiKey, + AuthParameters: &ebtypes.CreateConnectionAuthRequestParameters{ + ApiKeyAuthParameters: &ebtypes.CreateConnectionApiKeyAuthRequestParameters{ + ApiKeyName: aws.String("x-api-key"), ApiKeyValue: aws.String("s3cr3t"), + }, + }, + }) + if err != nil { + t.Fatalf("CreateConnection: %v", err) + } + + dest, err := eb.CreateApiDestination(ctx, &awseb.CreateApiDestinationInput{ + Name: aws.String("d1"), ConnectionArn: conn.ConnectionArn, + InvocationEndpoint: aws.String(hook.URL + "/orders/*"), HttpMethod: ebtypes.ApiDestinationHttpMethodPost, + }) + if err != nil { + t.Fatalf("CreateApiDestination: %v", err) + } + + if _, err := eb.PutRule(ctx, &awseb.PutRuleInput{Name: aws.String("r"), EventPattern: aws.String(`{"source":["app"]}`)}); err != nil { + t.Fatalf("PutRule: %v", err) + } + + params := &ebtypes.HttpParameters{ + PathParameterValues: []string{"o-1"}, + QueryStringParameters: map[string]string{"q": "1"}, + } + + if _, err := eb.PutTargets(ctx, &awseb.PutTargetsInput{ + Rule: aws.String("r"), + Targets: []ebtypes.Target{{Id: aws.String("d"), Arn: dest.ApiDestinationArn, HttpParameters: params}}, + }); err != nil { + t.Fatalf("PutTargets: %v", err) + } + + list, err := eb.ListTargetsByRule(ctx, &awseb.ListTargetsByRuleInput{Rule: aws.String("r")}) + if err != nil || len(list.Targets) != 1 || list.Targets[0].HttpParameters == nil || + list.Targets[0].HttpParameters.PathParameterValues[0] != "o-1" { + t.Fatalf("ListTargetsByRule = %+v, %v", list, err) + } + + if _, err := eb.PutEvents(ctx, &awseb.PutEventsInput{Entries: []ebtypes.PutEventsRequestEntry{ + {Source: aws.String("app"), DetailType: aws.String("t"), Detail: aws.String(`{"id":"42"}`)}, + }}); err != nil { + t.Fatalf("PutEvents: %v", err) + } + + // Delivery runs in the background after PutEvents returns. + for deadline := time.Now().Add(10 * time.Second); ; time.Sleep(10 * time.Millisecond) { + mu.Lock() + called := got != nil + mu.Unlock() + + if called { + break + } + + if time.Now().After(deadline) { + t.Fatal("the API destination endpoint was not called") + } + } + + mu.Lock() + defer mu.Unlock() + + if got.URL.Path != "/orders/o-1" || got.URL.Query().Get("q") != "1" || got.Header.Get("x-api-key") != "s3cr3t" { + t.Fatalf("request = %s %s headers %v", got.Method, got.URL, got.Header) + } + + if body == "" { + t.Fatal("the endpoint got an empty body") + } +} diff --git a/server/aws/eventbridge/types.go b/server/aws/eventbridge/types.go index d3cf5b19d..cb9fa1c79 100644 --- a/server/aws/eventbridge/types.go +++ b/server/aws/eventbridge/types.go @@ -67,6 +67,7 @@ type targetJSON struct { InputTransformer json.RawMessage `json:"InputTransformer,omitempty"` DeadLetterConfig json.RawMessage `json:"DeadLetterConfig,omitempty"` RetryPolicy json.RawMessage `json:"RetryPolicy,omitempty"` + HTTPParameters json.RawMessage `json:"HttpParameters,omitempty"` } type putTargetsRequest struct { @@ -328,6 +329,10 @@ func toTargetJSON(t *ebdriver.Target) targetJSON { out.RetryPolicy = json.RawMessage(t.RetryPolicy) } + if t.HTTPParameters != "" { + out.HTTPParameters = json.RawMessage(t.HTTPParameters) + } + return out } @@ -341,5 +346,6 @@ func fromTargetJSON(t *targetJSON) ebdriver.Target { InputTransformer: string(t.InputTransformer), DeadLetterConfig: string(t.DeadLetterConfig), RetryPolicy: string(t.RetryPolicy), + HTTPParameters: string(t.HTTPParameters), } } diff --git a/services/eventbus/driver/driver.go b/services/eventbus/driver/driver.go index 98f5677f1..2c5b660d5 100644 --- a/services/eventbus/driver/driver.go +++ b/services/eventbus/driver/driver.go @@ -120,6 +120,10 @@ type Target struct { InputTransformer string // raw JSON InputTransformer block DeadLetterConfig string // raw JSON DeadLetterConfig block RetryPolicy string // raw JSON RetryPolicy block + // HTTPParameters is the raw JSON HttpParameters block (path parameter + // values, headers and query string) for API destination and API Gateway + // targets. Empty for other targets. + HTTPParameters string } // Event represents an event to publish. From 7c26ecb34c77b663368ac16e5ed6b63faabcb304 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Patel Date: Fri, 23 Oct 2026 02:56:10 +0530 Subject: [PATCH 12/31] fix(aws-cloudwatchlogs): Logs Insights stats, parse and dedup (#1664) * fix(aws-cloudwatchlogs): Logs Insights stats, parse and dedup commands * fix(aws-cloudwatchlogs): show parsed fields in default Insights columns --- .../aws/cloudwatchlogs/insights/aggregate.go | 257 +++++++++++++++++ .../aws/cloudwatchlogs/insights/datetime.go | 259 ++++++++++++++++++ .../cloudwatchlogs/insights/datetime_test.go | 87 ++++++ .../aws/cloudwatchlogs/insights/dedup.go | 59 ++++ .../aws/cloudwatchlogs/insights/dedup_test.go | 63 +++++ .../aws/cloudwatchlogs/insights/lexer.go | 28 +- .../aws/cloudwatchlogs/insights/parse_cmd.go | 215 +++++++++++++++ .../cloudwatchlogs/insights/parse_cmd_test.go | 155 +++++++++++ .../aws/cloudwatchlogs/insights/parse_expr.go | 39 ++- .../insights/parse_fuzz_test.go | 43 +++ .../aws/cloudwatchlogs/insights/query.go | 196 +++++++++---- .../aws/cloudwatchlogs/insights/query_test.go | 1 - .../aws/cloudwatchlogs/insights/stats.go | 185 +++++++++++++ .../aws/cloudwatchlogs/insights/stats_test.go | 210 ++++++++++++++ .../cloudwatchlogs/insights_queries_test.go | 2 +- .../cloudwatchlogs/insights_stats_sdk_test.go | 113 ++++++++ 16 files changed, 1851 insertions(+), 61 deletions(-) create mode 100644 providers/aws/cloudwatchlogs/insights/aggregate.go create mode 100644 providers/aws/cloudwatchlogs/insights/datetime.go create mode 100644 providers/aws/cloudwatchlogs/insights/datetime_test.go create mode 100644 providers/aws/cloudwatchlogs/insights/dedup.go create mode 100644 providers/aws/cloudwatchlogs/insights/dedup_test.go create mode 100644 providers/aws/cloudwatchlogs/insights/parse_cmd.go create mode 100644 providers/aws/cloudwatchlogs/insights/parse_cmd_test.go create mode 100644 providers/aws/cloudwatchlogs/insights/parse_fuzz_test.go create mode 100644 providers/aws/cloudwatchlogs/insights/stats.go create mode 100644 providers/aws/cloudwatchlogs/insights/stats_test.go create mode 100644 server/aws/cloudwatchlogs/insights_stats_sdk_test.go diff --git a/providers/aws/cloudwatchlogs/insights/aggregate.go b/providers/aws/cloudwatchlogs/insights/aggregate.go new file mode 100644 index 000000000..96b603e7a --- /dev/null +++ b/providers/aws/cloudwatchlogs/insights/aggregate.go @@ -0,0 +1,257 @@ +package insights + +import ( + "math" + "sort" + "strconv" + "strings" +) + +// aggCall is one aggregation function call inside a stats command. Its value +// is computed once per group and read back from the group's slot record. +type aggCall struct { + fn aggFunc + arg expr // nil for count(*) and count() + pct float64 + key string +} + +func (a *aggCall) eval(rec Record) any { return rec[a.key] } + +type aggFunc func(a *aggCall, recs []Record) any + +// Aggregation and stats-only functions. +const ( + aggCount = "count" + aggPct = "pct" +) + +// maxPercent is the largest percentile pct accepts. +const maxPercent = 100 + +// aggregations are the stats functions, keyed by lower-case name. +// count_distinct is the general-functions spelling of countDistinct. +// +//nolint:gochecknoglobals // fixed function table. +var aggregations = map[string]aggFunc{ + aggCount: aggCountFn, + "count_distinct": aggCountDistinct, + "countdistinct": aggCountDistinct, + "sum": numericAgg(sum), + "avg": numericAgg(func(v []float64) float64 { return sum(v) / float64(len(v)) }), + "stddev": numericAgg(stddev), + unitMin: pickAgg(func(a, b any) bool { return compare(a, b) < 0 }), + "max": pickAgg(func(a, b any) bool { return compare(a, b) > 0 }), + "sortsfirst": pickAgg(func(a, b any) bool { return sortCompare(a, b) < 0 }), + "sortslast": pickAgg(func(a, b any) bool { return sortCompare(a, b) > 0 }), + "earliest": timeAgg(-1), + "latest": timeAgg(1), + aggPct: aggPercentile, +} + +func aggCountFn(a *aggCall, recs []Record) any { + if a.arg == nil { + return float64(len(recs)) + } + + n := 0 + + for _, r := range recs { + if a.arg.eval(r) != nil { + n++ + } + } + + return float64(n) +} + +func aggCountDistinct(a *aggCall, recs []Record) any { + seen := map[string]bool{} + + for _, r := range recs { + if v := a.arg.eval(r); v != nil { + seen[toStr(v)] = true + } + } + + return float64(len(seen)) +} + +// numbers returns the argument's numeric values across recs. +func numbers(a *aggCall, recs []Record) []float64 { + var out []float64 + + for _, r := range recs { + if f, ok := toNum(a.arg.eval(r)); ok { + out = append(out, f) + } + } + + return out +} + +// numericAgg folds the numeric values; with none, the result is absent. +func numericAgg(f func([]float64) float64) aggFunc { + return func(a *aggCall, recs []Record) any { + v := numbers(a, recs) + if len(v) == 0 { + return nil + } + + return f(v) + } +} + +func sum(v []float64) float64 { + var s float64 + for _, x := range v { + s += x + } + + return s +} + +// stddev is the population standard deviation, the square root of what +// variance() reports. +func stddev(v []float64) float64 { + mean := sum(v) / float64(len(v)) + + var sq float64 + for _, x := range v { + sq += (x - mean) * (x - mean) + } + + return math.Sqrt(sq / float64(len(v))) +} + +// pickAgg keeps the present value that better ranks first. +func pickAgg(better func(a, b any) bool) aggFunc { + return func(a *aggCall, recs []Record) any { + var best any + + for _, r := range recs { + if v := a.arg.eval(r); v != nil && (best == nil || better(v, best)) { + best = v + } + } + + return best + } +} + +// timeAgg returns the argument from the event with the earliest (sign -1) +// or latest (sign 1) @timestamp among the events that carry it. +func timeAgg(sign int) aggFunc { + return func(a *aggCall, recs []Record) any { + var best, bestTS any + + for _, r := range recs { + v, ts := a.arg.eval(r), r[FieldTimestamp] + if v == nil || ts == nil { + continue + } + + if bestTS == nil || compare(ts, bestTS)*sign > 0 { + best, bestTS = v, ts + } + } + + return best + } +} + +// aggPercentile is the nearest-rank percentile of the numeric values. +func aggPercentile(a *aggCall, recs []Record) any { + v := numbers(a, recs) + if len(v) == 0 { + return nil + } + + sort.Float64s(v) + + rank := math.Ceil(a.pct/maxPercent*float64(len(v))) - 1 + + i, ok := clampIndex(rank, len(v)-1) + if !ok { + return nil + } + + return v[i] +} + +// parseAgg parses an aggregation call's arguments; the opening parenthesis +// is already consumed. Aggregations do not nest. +func (p *exprParser) parseAgg(fn *token, f aggFunc) (expr, error) { + name := strings.ToLower(fn.text) + call := &aggCall{fn: f} + + switch { + case name == aggCount && p.atStarOrClose(): + if t := p.peek(); t.kind == tOp { + p.pos++ + } + default: + arg, err := p.withoutAggs(p.parseOr) + if err != nil { + return nil, err + } + + call.arg = arg + } + + if name == aggPct { + if err := p.parsePercent(fn, call); err != nil { + return nil, err + } + } + + if err := p.closeParen(); err != nil { + return nil, err + } + + call.key = "\x00agg" + strconv.Itoa(len(*p.aggs)) + *p.aggs = append(*p.aggs, call) + + return call, nil +} + +// atStarOrClose reports whether count's argument is * or empty. +func (p *exprParser) atStarOrClose() bool { + t := p.peek() + + return t != nil && (t.kind == tRParen || (t.kind == tOp && t.text == "*")) +} + +func (p *exprParser) parsePercent(fn *token, call *aggCall) error { + if !p.comma() { + return compileErr(fn.start, fn.end, "pct takes a field and a percentile") + } + + t := p.peek() + if t == nil || t.kind != tNumber { + return p.errHere("percentile number expected in pct") + } + + f, err := strconv.ParseFloat(t.text, 64) + if err != nil || f < 0 || f > maxPercent { + return compileErr(t.start, t.end, "percentile must be a number from 0 to 100") + } + + p.pos++ + call.pct = f + + return nil +} + +// withoutAggs runs a sub-parse with aggregation functions disallowed, for +// the arguments of an aggregation. +func (p *exprParser) withoutAggs(parse func() (expr, error)) (expr, error) { + saved := p.aggs + p.aggs = nil + + x, err := parse() + + p.aggs = saved + + return x, err +} diff --git a/providers/aws/cloudwatchlogs/insights/datetime.go b/providers/aws/cloudwatchlogs/insights/datetime.go new file mode 100644 index 000000000..153e57131 --- /dev/null +++ b/providers/aws/cloudwatchlogs/insights/datetime.go @@ -0,0 +1,259 @@ +package insights + +import ( + "math" + "strconv" + "strings" + "time" +) + +// period is a bin/datefloor/dateceil period: a fixed duration, or a whole +// number of calendar months for the month, quarter and year units. +type period struct { + d time.Duration + months int +} + +// Calendar units, in months. +const ( + monthsPerQuarter = 3 + monthsPerYear = 12 +) + +// unitMin is the minute unit, spelled like the min aggregation. +const unitMin = "min" + +// maxPeriod bounds a period so the float-to-integer conversion is safe. +const maxPeriod = 100 * 365 * 24 * time.Hour + +//nolint:gochecknoglobals // fixed unit table. +var durationUnits = map[string]time.Duration{ + "ms": time.Millisecond, "msec": time.Millisecond, "millisecond": time.Millisecond, + "s": time.Second, "sec": time.Second, "second": time.Second, + "m": time.Minute, unitMin: time.Minute, "minute": time.Minute, + "h": time.Hour, "hr": time.Hour, "hour": time.Hour, + "d": 24 * time.Hour, "day": 24 * time.Hour, + "w": 7 * 24 * time.Hour, "week": 7 * 24 * time.Hour, +} + +//nolint:gochecknoglobals // fixed unit table. +var monthUnits = map[string]int{ + "mo": 1, "mon": 1, "month": 1, + "q": monthsPerQuarter, "qtr": monthsPerQuarter, "quarter": monthsPerQuarter, + "y": monthsPerYear, "yr": monthsPerYear, "year": monthsPerYear, +} + +// parsePeriod reads a period literal such as 5m, 1h or 2qtrs. Units longer +// than one character may take a plural s. +func parsePeriod(t *token) (period, error) { + if n, unit, ok := splitPeriod(t); ok { + if p, ok := durationPeriod(n, unit); ok { + return p, nil + } + + if p, ok := monthPeriod(n, unit); ok { + return p, nil + } + } + + return period{}, compileErr(t.start, t.end, "invalid time period '"+t.text+"'") +} + +// splitPeriod splits a period literal into its positive, finite amount +// and its lower-case unit. +func splitPeriod(t *token) (n float64, unit string, ok bool) { + split := strings.IndexFunc(t.text, func(r rune) bool { return (r < '0' || r > '9') && r != '.' }) + if t.kind != tNumber || split <= 0 { + return 0, "", false + } + + n, err := strconv.ParseFloat(t.text[:split], 64) + if err != nil || n <= 0 || math.IsInf(n, 0) || math.IsNaN(n) { + return 0, "", false + } + + return n, strings.ToLower(t.text[split:]), true +} + +func lookupUnit[V any](table map[string]V, unit string) (V, bool) { + if v, ok := table[unit]; ok { + return v, true + } + + if len(unit) > twoChars && strings.HasSuffix(unit, "s") { + v, ok := table[unit[:len(unit)-1]] + return v, ok + } + + var zero V + + return zero, false +} + +func durationPeriod(n float64, unit string) (period, bool) { + u, ok := lookupUnit(durationUnits, unit) + if !ok { + return period{}, false + } + + ns := n * float64(u) + if ns < float64(time.Millisecond) || ns > float64(maxPeriod) { + return period{}, false + } + + return period{d: time.Duration(ns)}, true +} + +func monthPeriod(n float64, unit string) (period, bool) { + u, ok := lookupUnit(monthUnits, unit) + if !ok { + return period{}, false + } + + months := n * float64(u) + if months != math.Trunc(months) || months > float64(maxPeriod/(30*24*time.Hour)) { + return period{}, false + } + + return period{months: int(months)}, true +} + +// floor truncates t to the start of its period. Fixed periods align to the +// Unix epoch; calendar periods to the start of the month, quarter or year. +func (p period) floor(t time.Time) time.Time { + t = t.UTC() + + if p.months > 0 { + total := t.Year()*monthsPerYear + int(t.Month()) - 1 + total -= mod(total, p.months) + + return time.Date(total/monthsPerYear, time.Month(total%monthsPerYear+1), 1, 0, 0, 0, 0, time.UTC) + } + + ms := t.UnixMilli() + step := p.d.Milliseconds() + + return time.UnixMilli(ms - mod64(ms, step)).UTC() +} + +func (p period) ceil(t time.Time) time.Time { + f := p.floor(t) + if f.Equal(t) { + return f + } + + if p.months > 0 { + return f.AddDate(0, p.months, 0) + } + + return f.Add(p.d) +} + +func mod(a, b int) int { return ((a % b) + b) % b } + +func mod64(a, b int64) int64 { return ((a % b) + b) % b } + +// maxEpochMillis bounds a numeric timestamp so its int64 conversion is safe. +const maxEpochMillis = 1e15 + +// toTime reads a timestamp value: the rendered form of @timestamp, or a +// number of epoch milliseconds. +func toTime(v any) (time.Time, bool) { + if s, ok := v.(string); ok { + if t, err := time.Parse(timestampLayout, s); err == nil { + return t, true + } + } + + f, ok := toNum(v) + if !ok || math.IsNaN(f) || math.Abs(f) > maxEpochMillis { + return time.Time{}, false + } + + return time.UnixMilli(int64(f)).UTC(), true +} + +// roundExpr is bin(period), which rounds @timestamp down to the period, and +// datefloor(x, period) / dateceil(x, period). +type roundExpr struct { + x expr + p period + ceil bool +} + +func (e roundExpr) eval(rec Record) any { + t, ok := toTime(e.x.eval(rec)) + if !ok { + return nil + } + + if e.ceil { + t = e.p.ceil(t) + } else { + t = e.p.floor(t) + } + + return t.Format(timestampLayout) +} + +// Date functions that take a period literal. +const ( + fnBin = "bin" + fnDateFloor = "datefloor" + fnDateCeil = "dateceil" +) + +func isDateFunc(name string) bool { + return name == fnBin || name == fnDateFloor || name == fnDateCeil +} + +// parseDateFunc parses the arguments of bin(period), datefloor(x, period) +// or dateceil(x, period); the opening parenthesis is already consumed. +func (p *exprParser) parseDateFunc(fn *token) (expr, error) { + name := strings.ToLower(fn.text) + + var x expr = fieldRef{name: FieldTimestamp} + + if name == fnBin { + p.refs = append(p.refs, token{kind: tIdent, text: FieldTimestamp, start: fn.start, end: fn.end}) + } else { + arg, err := p.parseOr() + if err != nil { + return nil, err + } + + if !p.comma() { + return nil, p.errHere("expected ',' and a time period in " + fn.text) + } + + x = arg + } + + t := p.peek() + if t == nil { + return nil, p.errHere("time period expected in " + fn.text) + } + + per, err := parsePeriod(t) + if err != nil { + return nil, err + } + + p.pos++ + + if err := p.closeParen(); err != nil { + return nil, err + } + + return roundExpr{x: x, p: per, ceil: name == fnDateCeil}, nil +} + +func (p *exprParser) closeParen() error { + if t := p.peek(); t == nil || t.kind != tRParen { + return p.errHere("expected ')'") + } + + p.pos++ + + return nil +} diff --git a/providers/aws/cloudwatchlogs/insights/datetime_test.go b/providers/aws/cloudwatchlogs/insights/datetime_test.go new file mode 100644 index 000000000..218be43e6 --- /dev/null +++ b/providers/aws/cloudwatchlogs/insights/datetime_test.go @@ -0,0 +1,87 @@ +package insights + +import ( + "math" + "strings" + "testing" + "time" +) + +func TestPeriodFloorCeil(t *testing.T) { + at := time.Date(2025, 5, 17, 13, 47, 31, 250e6, time.UTC) + + tests := []struct { + period, floor, ceil string + }{ + {"500ms", "2025-05-17 13:47:31.000", "2025-05-17 13:47:31.500"}, + {"1msec", "2025-05-17 13:47:31.250", "2025-05-17 13:47:31.250"}, + {"30s", "2025-05-17 13:47:30.000", "2025-05-17 13:48:00.000"}, + {"2secs", "2025-05-17 13:47:30.000", "2025-05-17 13:47:32.000"}, + {"5m", "2025-05-17 13:45:00.000", "2025-05-17 13:50:00.000"}, + {"15mins", "2025-05-17 13:45:00.000", "2025-05-17 14:00:00.000"}, + {"1hr", "2025-05-17 13:00:00.000", "2025-05-17 14:00:00.000"}, + {"2hrs", "2025-05-17 12:00:00.000", "2025-05-17 14:00:00.000"}, + {"1.5h", "2025-05-17 13:30:00.000", "2025-05-17 15:00:00.000"}, + {"1d", "2025-05-17 00:00:00.000", "2025-05-18 00:00:00.000"}, + {"1days", "2025-05-17 00:00:00.000", "2025-05-18 00:00:00.000"}, + {"1w", "2025-05-15 00:00:00.000", "2025-05-22 00:00:00.000"}, + {"1mo", "2025-05-01 00:00:00.000", "2025-06-01 00:00:00.000"}, + {"2months", "2025-05-01 00:00:00.000", "2025-07-01 00:00:00.000"}, + {"1q", "2025-04-01 00:00:00.000", "2025-07-01 00:00:00.000"}, + {"1qtr", "2025-04-01 00:00:00.000", "2025-07-01 00:00:00.000"}, + {"1y", "2025-01-01 00:00:00.000", "2026-01-01 00:00:00.000"}, + {"1yrs", "2025-01-01 00:00:00.000", "2026-01-01 00:00:00.000"}, + } + + for _, tc := range tests { + t.Run(tc.period, func(t *testing.T) { + p, err := parsePeriod(&token{kind: tNumber, text: tc.period}) + if err != nil { + t.Fatal(err) + } + + if got := p.floor(at).Format(timestampLayout); got != tc.floor { + t.Errorf("floor = %s, want %s", got, tc.floor) + } + + if got := p.ceil(at).Format(timestampLayout); got != tc.ceil { + t.Errorf("ceil = %s, want %s", got, tc.ceil) + } + }) + } +} + +func TestParsePeriodRejects(t *testing.T) { + for _, s := range []string{"5", "m", "0s", "5x", "5ss", "1.5mo", "1.2.3m", "100000000000000000000h", "2000y", "0.0001ms"} { + if _, err := parsePeriod(&token{kind: tNumber, text: s}); err == nil { + t.Errorf("parsePeriod(%q) accepted", s) + } + } + + if _, err := parsePeriod(&token{kind: tIdent, text: "5m"}); err == nil { + t.Error("an identifier is not a period") + } +} + +func TestToTimeGuards(t *testing.T) { + for _, v := range []any{math.NaN(), math.Inf(1), math.Inf(-1), 1e300, -1e300, "nan", "inf", "not a time", nil} { + if _, ok := toTime(v); ok { + t.Errorf("toTime(%v) accepted", v) + } + } + + got, ok := toTime(float64(1735689600000)) + if !ok || got.Format(timestampLayout) != "2025-01-01 00:00:00.000" { + t.Fatalf("toTime(epoch ms) = %v, %v", got, ok) + } +} + +func TestDateFunctionsInFields(t *testing.T) { + res := run(t, "fields bin(1m) as b, datefloor(@timestamp, 1h) as f, dateceil(@timestamp, 1h) as c, dateceil(latency, 1m) as x"+ + " | sort @timestamp asc | limit 1 | display b, f, c, x") + + got := strings.Join([]string{col(res.Rows, "b")[0], col(res.Rows, "f")[0], col(res.Rows, "c")[0], col(res.Rows, "x")[0]}, "|") + if got != "2025-01-01 00:00:00.000|2025-01-01 00:00:00.000|2025-01-01 00:00:00.000|1970-01-01 00:01:00.000" { + t.Fatalf("date functions = %s", got) + } +} diff --git a/providers/aws/cloudwatchlogs/insights/dedup.go b/providers/aws/cloudwatchlogs/insights/dedup.go new file mode 100644 index 000000000..a0de8b7d0 --- /dev/null +++ b/providers/aws/cloudwatchlogs/insights/dedup.go @@ -0,0 +1,59 @@ +package insights + +// dedupCmd keeps the first record for each combination of its fields; +// records missing any of them are all kept. +type dedupCmd struct{ fields []string } + +func (c dedupCmd) apply(recs []Record) []Record { + seen := map[string]bool{} + out := recs[:0] + + for _, r := range recs { + vals := make([]any, len(c.fields)) + complete := true + + for i, f := range c.fields { + vals[i] = r[f] + complete = complete && vals[i] != nil + } + + if complete { + key := groupKey(vals) + if seen[key] { + continue + } + + seen[key] = true + } + + out = append(out, r) + } + + return out +} + +func (dedupCmd) marksMatched() {} + +func (q *Query) addDedup(p *exprParser) error { + var names []string + + for { + t := p.peek() + if t == nil || t.kind != tIdent { + return p.errHere("field name expected in dedup") + } + + names = append(names, t.text) + p.refs = append(p.refs, *t) + p.pos++ + + if !p.comma() { + break + } + } + + q.cmds = append(q.cmds, dedupCmd{fields: names}) + q.dedup = true + + return p.done() +} diff --git a/providers/aws/cloudwatchlogs/insights/dedup_test.go b/providers/aws/cloudwatchlogs/insights/dedup_test.go new file mode 100644 index 000000000..b8ec6750d --- /dev/null +++ b/providers/aws/cloudwatchlogs/insights/dedup_test.go @@ -0,0 +1,63 @@ +package insights + +import ( + "errors" + "strings" + "testing" + "time" +) + +func serverRecords() []Record { + return eventsAt("s", []string{ + `{"server":"s1","n":1}`, + `{"server":"s2","n":2}`, + `{"server":"s1","n":3}`, + `{"n":4}`, + `{"n":5}`, + }, []time.Duration{0, time.Second, 2 * time.Second, 3 * time.Second, 4 * time.Second}) +} + +func TestDedup(t *testing.T) { + tests := []struct { + q, want string + }{ + // Default newest-first order keeps the latest s1; events without a + // server are all kept. + {"fields n | dedup server", "5|4|3|2"}, + {"fields n | sort @timestamp asc | dedup server", "1|2|4|5"}, + {"fields n | sort n desc | dedup server | limit 2", "5|4"}, + {"fields n | filter ispresent(server) | dedup server, n", "3|2|1"}, + {"fields n | dedup @logStream", "5"}, + } + + for _, tc := range tests { + t.Run(tc.q, func(t *testing.T) { + if got := strings.Join(col(runOn(t, tc.q, serverRecords()).Rows, "n"), "|"); got != tc.want { + t.Fatalf("n = %q, want %q", got, tc.want) + } + }) + } +} + +func TestDedupMatchedCountsBeforeDedup(t *testing.T) { + res := runOn(t, "fields n | dedup server", serverRecords()) + if res.Matched != 5 || len(res.Rows) != 4 { + t.Fatalf("matched %d rows %d, want 5 and 4", res.Matched, len(res.Rows)) + } +} + +func TestDedupCompileErrors(t *testing.T) { + for _, q := range []string{ + "dedup", + "dedup server,", + "dedup server | sort n", + "dedup server | fields n", + "dedup server | stats count(*)", + "stats count(*) as c | dedup server", + } { + var ce *CompileError + if _, err := Parse(q); !errors.As(err, &ce) { + t.Errorf("Parse(%q) = %v, want a CompileError", q, err) + } + } +} diff --git a/providers/aws/cloudwatchlogs/insights/lexer.go b/providers/aws/cloudwatchlogs/insights/lexer.go index ef7b4cbc9..d98a0773e 100644 --- a/providers/aws/cloudwatchlogs/insights/lexer.go +++ b/providers/aws/cloudwatchlogs/insights/lexer.go @@ -1,7 +1,7 @@ // Package insights implements the core of the CloudWatch Logs Insights query -// language: the fields, filter (where), sort, limit and display commands, the -// expression grammar they share, and the system and auto-discovered JSON -// fields of a log event. See +// language: the fields, filter (where), stats, parse, sort, limit, dedup and +// display commands, the expression grammar they share, and the system and +// auto-discovered JSON fields of a log event. See // https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_QuerySyntax.html. package insights @@ -42,6 +42,8 @@ const ( kwAs = "as" cmdFilter = "filter" cmdWhere = "where" + cmdSort = "sort" + cmdLimit = "limit" opEq = "=" opNe = "!=" @@ -163,7 +165,7 @@ func regexAllowed(prev []token) bool { switch p.kind { case tIdent: - return regexKeywords[strings.ToLower(p.text)] + return regexKeywords[strings.ToLower(p.text)] || parseOperand(prev) case tOp, tComma, tLParen, tLBracket, tPipe: return true case tNumber, tString, tRegex, tRParen, tRBracket: @@ -173,6 +175,24 @@ func regexAllowed(prev []token) bool { return false } +// parseOperand reports whether the next token is parse's pattern: it comes +// right after the parse command name or after the field that follows it. +func parseOperand(prev []token) bool { + last := len(prev) - 1 + + return commandHead(prev, last, cmdParse) || commandHead(prev, last-1, cmdParse) +} + +// commandHead reports whether prev[i] is the named command at the start of +// a command. +func commandHead(prev []token, i int, name string) bool { + if i < 0 || prev[i].kind != tIdent || !strings.EqualFold(prev[i].text, name) { + return false + } + + return i == 0 || prev[i-1].kind == tPipe +} + func lexString(q string, i int) (token, int, error) { quote := q[i] diff --git a/providers/aws/cloudwatchlogs/insights/parse_cmd.go b/providers/aws/cloudwatchlogs/insights/parse_cmd.go new file mode 100644 index 000000000..9073a7c92 --- /dev/null +++ b/providers/aws/cloudwatchlogs/insights/parse_cmd.go @@ -0,0 +1,215 @@ +package insights + +import ( + "regexp" + "strconv" + "strings" +) + +const cmdParse = "parse" + +// parseCmd extracts fields from a field's text with a regular expression; +// a glob pattern is compiled to one. names[i] names capture group i+1. +type parseCmd struct { + field string + re *regexp.Regexp + names []string +} + +// apply sets the extracted fields on records whose field matches; the rest +// pass through without them. +func (c parseCmd) apply(recs []Record) []Record { + for _, r := range recs { + v := r[c.field] + if v == nil { + continue + } + + s := toStr(v) + + m := c.re.FindStringSubmatchIndex(s) + if m == nil { + continue + } + + for i, name := range c.names { + if lo, hi := m[2*i+2], m[2*i+3]; name != "" && lo >= 0 { + r[name] = s[lo:hi] + } + } + } + + return recs +} + +// addParse parses `parse [field] "glob" as a, b` and `parse [field] /regex/` +// with named groups (or `as` names for its groups). The field defaults to +// @message. +func (q *Query) addParse(p *exprParser) error { + field, err := p.parseSourceField() + if err != nil { + return err + } + + t := p.peek() + if t == nil || (t.kind != tString && t.kind != tRegex) { + if t != nil && t.kind == tIdent { + if modeErr := unsupportedParseMode(t); modeErr != nil { + return modeErr + } + } + + return p.errHere("glob pattern in quotes or /regex/ expected in parse") + } + + p.pos++ + + cmd, err := p.parsePattern(t) + if err != nil { + return err + } + + cmd.field = field + q.cmds = append(q.cmds, cmd) + p.defines = append(p.defines, cmd.names...) + q.addParsedColumns(cmd.names) + + return p.done() +} + +// addParsedColumns shows the extracted fields by default. After a stats +// command they join its output columns. +func (q *Query) addParsedColumns(names []string) { + cols := &q.parsed + if q.available != nil { + cols = &q.shown + } + + for _, name := range names { + if name != "" && !contains(*cols, name) { + *cols = append(*cols, name) + } + } +} + +// parseSourceField reads parse's optional field, @message by default. +func (p *exprParser) parseSourceField() (string, error) { + t := p.peek() + if t == nil || t.kind != tIdent { + return FieldMessage, nil + } + + if err := unsupportedParseMode(t); err != nil { + return "", err + } + + p.refs = append(p.refs, *t) + p.pos++ + + return t.text, nil +} + +// unsupportedParseMode rejects the parse modes the emulator does not run. +func unsupportedParseMode(t *token) error { + switch strings.ToLower(t.text) { + case "logfmt", "csv": + return compileErr(t.start, t.end, "parse "+t.text+" is not supported") + } + + return nil +} + +func (p *exprParser) parsePattern(t *token) (parseCmd, error) { + glob := t.kind == tString + + src := t.text + if glob { + src = globRegex(t.text) + } + + re, err := regexp.Compile(src) + if err != nil { + return parseCmd{}, compileErr(t.start, t.end, "invalid regular expression /"+t.text+"/") + } + + names := re.SubexpNames()[1:] + + aliases, err := p.parseAliases() + if err != nil { + return parseCmd{}, err + } + + if p.atKeyword("multi") { + return parseCmd{}, p.errHere("parse multi is not supported") + } + + switch { + case aliases != nil && len(aliases) != len(names): + return parseCmd{}, compileErr(t.start, t.end, + "parse pattern has "+strconv.Itoa(len(names))+" captures but "+strconv.Itoa(len(aliases))+" names") + case aliases != nil: + names = aliases + case glob || !anyNamed(names): + return parseCmd{}, compileErr(t.start, t.end, "parse needs named capture groups or as names") + } + + return parseCmd{re: re, names: names}, nil +} + +func (p *exprParser) parseAliases() ([]string, error) { + if !p.atKeyword(kwAs) { + return nil, nil + } + + p.pos++ + + var out []string + + for { + t := p.peek() + if t == nil || t.kind != tIdent { + return nil, p.errHere("field name expected after as") + } + + out = append(out, t.text) + p.pos++ + + if !p.comma() { + return out, nil + } + } +} + +func anyNamed(names []string) bool { + for _, n := range names { + if n != "" { + return true + } + } + + return false +} + +// globRegex compiles a parse glob: each * captures as little text as it can, +// except a trailing * which takes the rest of the value. +func globRegex(glob string) string { + parts := strings.Split(glob, "*") + + var b strings.Builder + + b.WriteString("(?s)") + + for i, part := range parts { + b.WriteString(regexp.QuoteMeta(part)) + + switch { + case i == len(parts)-1: + case i == len(parts)-2 && parts[i+1] == "": + b.WriteString("(.*)") + default: + b.WriteString("(.*?)") + } + } + + return b.String() +} diff --git a/providers/aws/cloudwatchlogs/insights/parse_cmd_test.go b/providers/aws/cloudwatchlogs/insights/parse_cmd_test.go new file mode 100644 index 000000000..717bb3f75 --- /dev/null +++ b/providers/aws/cloudwatchlogs/insights/parse_cmd_test.go @@ -0,0 +1,155 @@ +package insights + +import ( + "errors" + "strings" + "testing" + "time" +) + +func accessRecords() []Record { + return eventsAt("prod/api", []string{ + "user=alice action=login status=200", + "user=bob action=logout status=500", + "no match here", + }, []time.Duration{0, time.Second, 2 * time.Second}) +} + +func TestParseCommand(t *testing.T) { + tests := []struct { + q, field, want string + }{ + {`parse @message "user=* action=* status=*" as u, a, s | sort u asc | display u, a, s`, "a", "login|logout"}, + {`parse @message "user=* action=* status=*" as u, a, s | sort u asc | display s`, "s", "200|500"}, + {`parse "user=* action=*" as u, a | sort u asc | display a`, "a", "login status=200|logout status=500"}, + {`parse @message "user=* " as u | fields u`, "u", "bob|alice"}, + {`parse "user=* action=*" as u, a | fields @message`, "@message", "no match here|user=bob action=logout status=500|user=alice action=login status=200"}, + {`parse @message /user=(?\w+) action=(?\w+)/ | filter ispresent(who) | sort who asc | display who, what`, + "what", "login|logout"}, + {`parse /status=(\d+)/ as code | stats sum(code) as total`, "total", "700"}, + {`parse @message /(?\d{3})$/ | filter code >= 500 | display code`, "code", "500"}, + {`parse @logStream "*/*" as env, svc | stats count(*) as n by env, svc | display svc`, "svc", "api"}, + {`parse @message "user=* " as u | parse u "*l*" as head, tail | filter ispresent(head) | display head`, "head", "a"}, + {`parse @message /a\/b|user=(?[a-z]+)/ | sort u asc | display u`, "u", "alice|bob"}, + {`fields @message | parse @message "action=* " as a | stats count(*) as n by a | sort a asc | display a`, "a", "login|logout"}, + } + + for _, tc := range tests { + t.Run(tc.q, func(t *testing.T) { + got := strings.Join(col(runOn(t, tc.q, accessRecords()).Rows, tc.field), "|") + if got != tc.want { + t.Fatalf("%s = %q, want %q", tc.field, got, tc.want) + } + }) + } +} + +func TestParseGlobRegex(t *testing.T) { + if got := globRegex("[*] *"); got != `(?s)\[(.*?)\] (.*)` { + t.Fatalf("globRegex = %s", got) + } + + if got := globRegex("a=*;"); got != `(?s)a=(.*?);` { + t.Fatalf("globRegex = %s", got) + } +} + +func TestParseCompileErrors(t *testing.T) { + for _, q := range []string{ + `parse`, + `parse @message`, + `parse @message "user=*"`, + `parse @message "user=* action=*" as u`, + `parse @message "no wildcards" as u`, + `parse @message /x(y)/`, + `parse @message /(?x)/ as a, b`, + `parse @message /(?x/`, + `parse @message logfmt as lf`, + `parse csv as a, b`, + `parse @message /(?x)/ multi`, + `parse @message "a=*" as`, + `parse @message "a=*" as x y`, + `stats count(*) as n | parse @message "a=*" as x`, + } { + _, err := Parse(q) + + var ce *CompileError + if !errors.As(err, &ce) { + t.Errorf("Parse(%q) = %v, want a CompileError", q, err) + continue + } + + if ce.Start < 0 || ce.End < ce.Start || ce.End > len(q) { + t.Errorf("Parse(%q) offsets %d..%d out of range", q, ce.Start, ce.End) + } + } +} + +func TestParseKeepsDivision(t *testing.T) { + res := run(t, "fields latency / 2 as half | filter ispresent(half) | sort half asc | display half") + if got := strings.Join(col(res.Rows, "half"), "|"); got != "7.5|40|60" { + t.Fatalf("half = %s", got) + } +} + +func fieldNames(row []Field) string { + names := make([]string, 0, len(row)) + for _, f := range row { + names = append(names, f.Name) + } + + return strings.Join(names, ",") +} + +func TestParseDefaultColumns(t *testing.T) { + tests := []struct { + q string + want []string + }{ + {`parse "user=* action=* " as u, a | sort @timestamp asc`, []string{ + "@timestamp,@message,u,a,@ptr", "@timestamp,@message,u,a,@ptr", "@timestamp,@message,@ptr", + }}, + {`parse @message /user=(?\w+) action=(?\w+)/ | sort @timestamp asc`, []string{ + "@timestamp,@message,who,what,@ptr", "@timestamp,@message,who,what,@ptr", "@timestamp,@message,@ptr", + }}, + {`parse "user=* " as u | parse u "*l*" as head, tail | filter u = "alice"`, []string{"@timestamp,@message,u,head,tail,@ptr"}}, + {`parse "user=* action=* " as u, a | filter ispresent(u) | fields a`, []string{"a,@ptr", "a,@ptr"}}, + {`parse "user=* action=* " as u, a | filter ispresent(u) | display u`, []string{"u,@ptr", "u,@ptr"}}, + {`parse "user=* " as u | filter ispresent(u) | stats count(*) as n by u | sort u asc`, []string{"u,n", "u,n"}}, + {`stats count(*) as n by @logStream | parse @logStream "*/*" as env, svc`, []string{"@logStream,n,env,svc"}}, + } + + for _, tc := range tests { + t.Run(tc.q, func(t *testing.T) { + res := runOn(t, tc.q, accessRecords()) + if len(res.Rows) != len(tc.want) { + t.Fatalf("got %d rows, want %d", len(res.Rows), len(tc.want)) + } + + for i, row := range res.Rows { + if got := fieldNames(row); got != tc.want[i] { + t.Fatalf("row %d columns = %s, want %s", i, got, tc.want[i]) + } + } + }) + } +} + +func TestDedupAfterParseShowsParsedFields(t *testing.T) { + recs := eventsAt("s", []string{ + "user=alice action=login x=1", + "user=bob action=logout x=2", + "user=alice action=login x=3", + }, []time.Duration{0, time.Second, 2 * time.Second}) + + res := runOn(t, `parse @message "user=* action=* x=*" as u, a, x | dedup u, a`, recs) + if got := strings.Join(col(res.Rows, "x"), "|"); got != "3|2" { + t.Fatalf("x = %q, want 3|2", got) + } + + for _, row := range res.Rows { + if got := fieldNames(row); got != "@timestamp,@message,u,a,x,@ptr" { + t.Fatalf("columns = %s", got) + } + } +} diff --git a/providers/aws/cloudwatchlogs/insights/parse_expr.go b/providers/aws/cloudwatchlogs/insights/parse_expr.go index ed8e76ceb..9555a83b2 100644 --- a/providers/aws/cloudwatchlogs/insights/parse_expr.go +++ b/providers/aws/cloudwatchlogs/insights/parse_expr.go @@ -12,6 +12,15 @@ type exprParser struct { pos int // end is the query offset just past the command, for errors at its end. end int + // src is the whole query, for naming expressions by their text. + src string + // aggs collects a stats command's aggregation calls; nil elsewhere, + // where aggregation functions are not allowed. + aggs *[]*aggCall + // refs are the field names the command references, and defines the + // fields it creates. + refs []token + defines []string } func (p *exprParser) peek() *token { @@ -300,12 +309,16 @@ func (p *exprParser) parseIdent(t *token) (expr, error) { } if next := p.peek(); next == nil || next.kind != tLParen { + p.refs = append(p.refs, *t) + return fieldRef{name: t.text}, nil } - spec, ok := functions[strings.ToLower(t.text)] + name := strings.ToLower(t.text) + + spec, ok := functions[name] if !ok { - return nil, compileErr(t.start, t.end, "unknown function '"+t.text+"'") + return p.parseSpecialCall(t, name) } p.pos++ @@ -322,6 +335,28 @@ func (p *exprParser) parseIdent(t *token) (expr, error) { return callExpr{spec: spec, args: args}, nil } +// parseSpecialCall parses the calls that are not plain functions: the +// date functions, which take a period literal, and inside stats the +// aggregations. +func (p *exprParser) parseSpecialCall(t *token, name string) (expr, error) { + agg, isAgg := aggregations[name] + + switch { + case isDateFunc(name): + p.pos++ + + return p.parseDateFunc(t) + case isAgg && p.aggs != nil: + p.pos++ + + return p.parseAgg(t, agg) + case isAgg: + return nil, compileErr(t.start, t.end, "aggregation function '"+t.text+"' is only allowed in stats") + } + + return nil, compileErr(t.start, t.end, "unknown function '"+t.text+"'") +} + func (p *exprParser) parseArgs() ([]expr, error) { var args []expr diff --git a/providers/aws/cloudwatchlogs/insights/parse_fuzz_test.go b/providers/aws/cloudwatchlogs/insights/parse_fuzz_test.go new file mode 100644 index 000000000..2ee40d631 --- /dev/null +++ b/providers/aws/cloudwatchlogs/insights/parse_fuzz_test.go @@ -0,0 +1,43 @@ +package insights + +import ( + "errors" + "testing" +) + +// FuzzParse checks that any query either compiles to a CompileError with +// in-range offsets or runs without panicking. +func FuzzParse(f *testing.F) { + for _, seed := range []string{ + "fields @timestamp, @message | sort @timestamp desc | limit 20", + "filter latency > 50 and level in ['WARN', 'INFO'] | fields substr(@message, 0, 5) as s", + "stats count(*), avg(latency), pct(latency, 95) by level, bin(5m)", + "stats sum(latency) as s by bin(5m) as @t | stats max(s) by dateceil(@t, 10m)", + "stats count_distinct(user.id) as u, stddev(latency), earliest(level), sortsLast(level) | sort u desc", + `parse @message "user=* action=*" as u, a | stats count(*) by a`, + `parse @message /(?\d+)/ | dedup code | limit 3`, + "fields bin(1mo) as m, datefloor(latency, 1q) as q | dedup m", + "stats count(*) by bin(1e)", + } { + f.Add(seed) + } + + f.Fuzz(func(t *testing.T, q string) { + parsed, err := Parse(q) + if err != nil { + var ce *CompileError + if !errors.As(err, &ce) { + t.Fatalf("Parse(%q) = %v, not a CompileError", q, err) + } + + if ce.Start < 0 || ce.End < ce.Start || ce.End > len(q) { + t.Fatalf("Parse(%q) offsets %d..%d out of range", q, ce.Start, ce.End) + } + + return + } + + parsed.Run(sampleRecords(), 0) + parsed.Run(accessRecords(), 1) + }) +} diff --git a/providers/aws/cloudwatchlogs/insights/query.go b/providers/aws/cloudwatchlogs/insights/query.go index b2f8b01c3..f55f14ce4 100644 --- a/providers/aws/cloudwatchlogs/insights/query.go +++ b/providers/aws/cloudwatchlogs/insights/query.go @@ -123,8 +123,26 @@ type Query struct { cmds []command shown []string display []string + // parsed is the fields parse extracted before any stats command, shown + // after the defaults when no fields command sets the columns. + parsed []string + + // Parse-time state for the command ordering rules. + statsCount int + sortOrLimit bool + dedup bool + // available is the fields a command may reference after a stats + // command: that stats command's output and fields defined since. It is + // nil before the first stats command, when any field may be referenced. + available map[string]bool } +// matchPoint is a command that ends the matching part of a query: the +// records reaching it are what recordsMatched counts. +type matchPoint interface{ marksMatched() } + +func (limitCmd) marksMatched() {} + // Result is the output of running a query. type Result struct { Rows [][]Field @@ -154,22 +172,28 @@ func (q *Query) Run(recs []Record, limit int) Result { } } - rows = append(rows, append(row, Field{Name: FieldPtr, Value: toStr(r[FieldPtr])})) + // Aggregated stats rows have no @ptr. + if ptr, ok := r[FieldPtr]; ok { + row = append(row, Field{Name: FieldPtr, Value: toStr(ptr)}) + } + + rows = append(rows, row) } return Result{Rows: rows, Matched: matched} } // pipeline applies the commands in order, newest record first unless a sort -// says otherwise. matched counts the records left when the first limit -// command applies (or at the end), which is what recordsMatched reports. +// says otherwise. matched counts the records left when the first limit, +// stats or dedup command applies (or at the end), which is what +// recordsMatched reports. func (q *Query) pipeline(recs []Record) (out []Record, matched int) { sortCmd{keys: []sortKey{{field: FieldTimestamp, desc: true}}}.apply(recs) matched = -1 for _, c := range q.cmds { - if _, isLimit := c.(limitCmd); isLimit && matched < 0 { + if _, ends := c.(matchPoint); ends && matched < 0 { matched = len(recs) } @@ -184,7 +208,8 @@ func (q *Query) pipeline(recs []Record) (out []Record, matched int) { } // columns are the displayed fields: the last display command, else every -// fields command's fields, else @timestamp and @message. +// fields command's fields (or the stats output), else @timestamp and +// @message followed by the fields parse extracted. func (q *Query) columns() []string { switch { case len(q.display) > 0: @@ -193,7 +218,7 @@ func (q *Query) columns() []string { return q.shown } - return []string{FieldTimestamp, FieldMessage} + return append([]string{FieldTimestamp, FieldMessage}, q.parsed...) } // Parse parses a Logs Insights query. A query that does not parse returns a @@ -211,7 +236,7 @@ func Parse(query string) (*Query, error) { return nil, compileErr(len(query), len(query), "empty command") } - if err := q.addCommand(part, len(query)); err != nil { + if err := q.addCommand(part, query); err != nil { return nil, err } } @@ -238,31 +263,85 @@ func splitPipes(toks []token) [][]token { return parts } -func (q *Query) addCommand(part []token, end int) error { - head := part[0] - p := &exprParser{toks: part[1:], end: end} +// commandParsers parse each command's arguments after its name. +// +//nolint:gochecknoglobals // fixed command table. +var commandParsers = map[string]func(q *Query, p *exprParser, head *token) error{ + "fields": func(q *Query, p *exprParser, _ *token) error { return q.addFields(p) }, + cmdFilter: func(q *Query, p *exprParser, _ *token) error { return q.addFilter(p) }, + cmdWhere: func(q *Query, p *exprParser, _ *token) error { return q.addFilter(p) }, + cmdSort: func(q *Query, p *exprParser, _ *token) error { return q.addSort(p) }, + cmdLimit: func(q *Query, p *exprParser, _ *token) error { return q.addLimit(p) }, + "display": func(q *Query, p *exprParser, _ *token) error { return q.addDisplay(p) }, + "stats": (*Query).addStats, + cmdParse: func(q *Query, p *exprParser, _ *token) error { return q.addParse(p) }, + "dedup": func(q *Query, p *exprParser, _ *token) error { return q.addDedup(p) }, +} - switch strings.ToLower(head.text) { - case "fields": - return q.addFields(p) - case cmdFilter, cmdWhere: - x, err := p.parseOr() - if err != nil { - return err +func (q *Query) addCommand(part []token, src string) error { + head := &part[0] + name := strings.ToLower(head.text) + p := &exprParser{toks: part[1:], end: len(src), src: src} + + add, ok := commandParsers[name] + if !ok { + return compileErr(head.start, head.end, "unsupported or unknown command '"+head.text+"'") + } + + if q.dedup && name != cmdLimit { + return compileErr(head.start, head.end, "only limit can follow dedup") + } + + if name == cmdSort || name == cmdLimit { + q.sortOrLimit = true + } + + // References are checked against the fields available before this + // command, which a stats command replaces. + available := q.available + + if err := add(q, p, head); err != nil { + return err + } + + if err := checkRefs(p.refs, available); err != nil { + return err + } + + if q.available != nil { + for _, name := range p.defines { + q.available[name] = true } + } - q.cmds = append(q.cmds, filterCmd{x: x}) + return nil +} - return p.done() - case "sort": - return q.addSort(p) - case "limit": - return q.addLimit(p) - case "display": - return q.addDisplay(p) +// checkRefs rejects references to fields the preceding stats command did +// not define. +func checkRefs(refs []token, available map[string]bool) error { + if available == nil { + return nil } - return compileErr(head.start, head.end, "unsupported or unknown command '"+head.text+"'") + for _, t := range refs { + if !available[t.text] { + return compileErr(t.start, t.end, "field '"+t.text+"' is not defined by the preceding stats command") + } + } + + return nil +} + +func (q *Query) addFilter(p *exprParser) error { + x, err := p.parseOr() + if err != nil { + return err + } + + q.cmds = append(q.cmds, filterCmd{x: x}) + + return p.done() } func (p *exprParser) done() error { @@ -277,28 +356,12 @@ func (q *Query) addFields(p *exprParser) error { var items []fieldItem for { - startTok := p.peek() - - x, err := p.parseOr() + it, err := p.parseNamedItem() if err != nil { return err } - name := exprName(x, startTok, p) - - if p.atKeyword(kwAs) { - p.pos++ - - t := p.peek() - if t == nil || t.kind != tIdent { - return p.errHere("field name expected after as") - } - - name = t.text - p.pos++ - } - - items = append(items, fieldItem{name: name, x: x}) + items = append(items, it) if !p.comma() { break @@ -311,27 +374,52 @@ func (q *Query) addFields(p *exprParser) error { if !contains(q.shown, it.name) { q.shown = append(q.shown, it.name) } + + p.defines = append(p.defines, it.name) } return p.done() } -// exprName names an unaliased fields expression: a field keeps its own name, -// anything else is named by its source tokens. +// parseNamedItem parses `expr [as name]`. Without an alias a field keeps +// its own name and any other expression is named by its source text. +func (p *exprParser) parseNamedItem() (fieldItem, error) { + startTok := p.peek() + + x, err := p.parseOr() + if err != nil { + return fieldItem{}, err + } + + name := exprName(x, startTok, p) + + if p.atKeyword(kwAs) { + p.pos++ + + t := p.peek() + if t == nil || t.kind != tIdent { + return fieldItem{}, p.errHere("field name expected after as") + } + + name = t.text + p.pos++ + } + + return fieldItem{name: name, x: x}, nil +} + +// exprName names an unaliased expression: a field keeps its own name, +// anything else is named by its source text. func exprName(x expr, start *token, p *exprParser) string { if f, ok := x.(fieldRef); ok { return f.name } - var parts []string - - for _, t := range p.toks { - if start != nil && t.start >= start.start && (p.pos >= len(p.toks) || t.start < p.toks[p.pos].start) { - parts = append(parts, t.text) - } + if start == nil || p.pos == 0 { + return "" } - return strings.Join(parts, "") + return p.src[start.start:p.toks[p.pos-1].end] } func (p *exprParser) comma() bool { @@ -353,6 +441,7 @@ func (q *Query) addSort(p *exprParser) error { } p.pos++ + p.refs = append(p.refs, *t) k := sortKey{field: t.text, desc: true} @@ -401,6 +490,7 @@ func (q *Query) addDisplay(p *exprParser) error { } names = append(names, t.text) + p.refs = append(p.refs, *t) p.pos++ if !p.comma() { diff --git a/providers/aws/cloudwatchlogs/insights/query_test.go b/providers/aws/cloudwatchlogs/insights/query_test.go index 4f8f60a43..7c2c91468 100644 --- a/providers/aws/cloudwatchlogs/insights/query_test.go +++ b/providers/aws/cloudwatchlogs/insights/query_test.go @@ -126,7 +126,6 @@ func TestCompileErrors(t *testing.T) { "fields", "filter latency >", "filter level = 'x", - "stats count(*) by level", "pattern @message", "fields level |", "filter nosuch(level)", diff --git a/providers/aws/cloudwatchlogs/insights/stats.go b/providers/aws/cloudwatchlogs/insights/stats.go new file mode 100644 index 000000000..3e4ce3255 --- /dev/null +++ b/providers/aws/cloudwatchlogs/insights/stats.go @@ -0,0 +1,185 @@ +package insights + +import "strings" + +// maxStatsCommands is how many stats commands one query may have. +const maxStatsCommands = 10 + +// statsCmd groups records by its by expressions and replaces them with one +// record per group holding the group values and the aggregated items. +type statsCmd struct { + aggs []*aggCall + items []fieldItem + by []fieldItem +} + +type statsGroup struct { + vals []any + recs []Record +} + +// apply groups in order of first appearance, so with the default newest +// first order the newest group comes first. No records give no rows. +func (c statsCmd) apply(recs []Record) []Record { + var order []string + + groups := map[string]*statsGroup{} + + for _, r := range recs { + vals := make([]any, len(c.by)) + for i, b := range c.by { + vals[i] = b.x.eval(r) + } + + key := groupKey(vals) + + g, ok := groups[key] + if !ok { + order = append(order, key) + g = &statsGroup{vals: vals} + groups[key] = g + } + + g.recs = append(g.recs, r) + } + + out := make([]Record, 0, len(order)) + for _, key := range order { + out = append(out, c.row(groups[key])) + } + + return out +} + +func (c statsCmd) row(g *statsGroup) Record { + slots := Record{} + for _, a := range c.aggs { + slots[a.key] = a.fn(a, g.recs) + } + + row := Record{} + + for i, b := range c.by { + if g.vals[i] != nil { + row[b.name] = g.vals[i] + } + } + + for _, it := range c.items { + if v := it.x.eval(slots); v != nil { + row[it.name] = v + } + } + + return row +} + +// groupKey joins group values, telling a missing value from an empty one. +func groupKey(vals []any) string { + var b strings.Builder + + for _, v := range vals { + if v == nil { + b.WriteString("\x01") + } else { + b.WriteString("\x02" + toStr(v)) + } + + b.WriteByte(0) + } + + return b.String() +} + +func (statsCmd) marksMatched() {} + +// addStats parses `stats agg [as name], ... [by expr [as name], ...]`. Every +// item must use an aggregation; later commands see only its output fields. +func (q *Query) addStats(p *exprParser, head *token) error { + if q.statsCount == maxStatsCommands { + return compileErr(head.start, head.end, "a query can have at most 10 stats commands") + } + + if q.sortOrLimit { + return compileErr(head.start, head.end, "sort and limit must come after the last stats command") + } + + var aggs []*aggCall + + p.aggs = &aggs + + var items []fieldItem + + for { + before := len(aggs) + start := p.peek() + + it, err := p.parseNamedItem() + if err != nil { + return err + } + + if len(aggs) == before { + return compileErr(start.start, start.end, "stats needs an aggregation function such as count() or sum()") + } + + items = append(items, it) + + if !p.comma() { + break + } + } + + p.aggs = nil + + by, err := p.parseBy() + if err != nil { + return err + } + + q.cmds = append(q.cmds, statsCmd{aggs: aggs, items: items, by: by}) + q.statsCount++ + q.setStatsOutput(by, items) + + return p.done() +} + +func (p *exprParser) parseBy() ([]fieldItem, error) { + if !p.atKeyword("by") { + return nil, nil + } + + p.pos++ + + var by []fieldItem + + for { + it, err := p.parseNamedItem() + if err != nil { + return nil, err + } + + by = append(by, it) + + if !p.comma() { + return by, nil + } + } +} + +// setStatsOutput makes the stats output the only fields later commands can +// reference, and the fields shown by default. +func (q *Query) setStatsOutput(by, items []fieldItem) { + q.available = map[string]bool{} + q.shown = nil + + for _, list := range [][]fieldItem{by, items} { + for _, it := range list { + q.available[it.name] = true + + if !contains(q.shown, it.name) { + q.shown = append(q.shown, it.name) + } + } + } +} diff --git a/providers/aws/cloudwatchlogs/insights/stats_test.go b/providers/aws/cloudwatchlogs/insights/stats_test.go new file mode 100644 index 000000000..1de30986e --- /dev/null +++ b/providers/aws/cloudwatchlogs/insights/stats_test.go @@ -0,0 +1,210 @@ +package insights + +import ( + "errors" + "math" + "strconv" + "strings" + "testing" + "time" +) + +// eventsAt builds records for messages logged at the given offsets from +// midnight 2025-01-01 UTC. +func eventsAt(stream string, msgs []string, offsets []time.Duration) []Record { + base := time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC) + + recs := make([]Record, 0, len(msgs)) + for i, m := range msgs { + recs = append(recs, NewRecord(&Event{ + Account: "123456789012", LogGroup: "/app", LogStream: stream, + Timestamp: base.Add(offsets[i]), Ingestion: base, Message: m, Ptr: "p" + strconv.Itoa(i), + })) + } + + return recs +} + +func runOn(t *testing.T, q string, recs []Record) Result { + t.Helper() + + parsed, err := Parse(q) + if err != nil { + t.Fatalf("Parse(%q): %v", q, err) + } + + return parsed.Run(recs, 0) +} + +func TestStatsFunctions(t *testing.T) { + // The sample latencies are 120, 15 and 80; levels ERROR, INFO, WARN + // and one event without a level. + mean := (120.0 + 15 + 80) / 3 + sd := math.Sqrt((math.Pow(120-mean, 2) + math.Pow(15-mean, 2) + math.Pow(80-mean, 2)) / 3) + + tests := []struct { + q, field, want string + }{ + {"stats count(*)", "count(*)", "4"}, + {"stats count()", "count()", "4"}, + {"stats count(level) as n", "n", "3"}, + {"stats count_distinct(level) as n", "n", "3"}, + {"stats countDistinct(user.id) as n", "n", "2"}, + {"stats sum(latency) as s", "s", "215"}, + {"stats avg(latency) as a", "a", strconv.FormatFloat(mean, 'f', -1, 64)}, + {"stats min(latency) as m", "m", "15"}, + {"stats max(latency) as m", "m", "120"}, + {"stats min(level) as m", "m", "ERROR"}, + {"stats pct(latency, 50) as p", "p", "80"}, + {"stats pct(latency, 0) as p", "p", "15"}, + {"stats pct(latency, 100) as p", "p", "120"}, + {"stats pct(latency, 99.9) as p", "p", "120"}, + {"stats stddev(latency) as sd", "sd", strconv.FormatFloat(sd, 'f', -1, 64)}, + {"stats earliest(level) as e", "e", "ERROR"}, + {"stats latest(level) as l", "l", "WARN"}, + {"stats sortsFirst(level) as f", "f", "ERROR"}, + {"stats sortsLast(level) as l", "l", "WARN"}, + {"stats sum(latency) / count(*) as x", "x", "53.75"}, + {"stats round(avg(latency)) as r", "r", "72"}, + {"stats pct(latency, 50)", "pct(latency, 50)", "80"}, + {"stats count(*) as n, max(latency) as top", "top", "120"}, + {"stats sum(nosuch) as s", "s", ""}, + {"filter ispresent(latency) | stats count(*) as n by level | sort level asc", "level", "ERROR|INFO|WARN"}, + {"stats count(*) as n by level | sort level asc", "level", "ERROR|INFO|WARN"}, + {"stats count(*) as n by level | sort level asc", "n", "1|1|1|1"}, + {"stats count(*) as n by toupper(level) as lv | filter lv = 'INFO'", "lv", "INFO"}, + {"stats count(*) as n by level | stats sum(n) as total", "total", "4"}, + {"stats sum(latency) as s by level | filter s > 50 | stats count(*) as big", "big", "2"}, + {"stats max(latency) as m by level | filter ispresent(m) | sort m desc | limit 2", "m", "120|80"}, + {"stats max(latency) as m by level | fields m * 2 as d | sort d asc | display d", "d", "30|160|240"}, + {"STATS count(*) AS n BY level | SORT n DESC | LIMIT 1", "n", "1"}, + } + + for _, tc := range tests { + t.Run(tc.q, func(t *testing.T) { + got := strings.Join(col(run(t, tc.q).Rows, tc.field), "|") + if got != tc.want { + t.Fatalf("%s = %q, want %q", tc.field, got, tc.want) + } + }) + } +} + +func TestStatsRowShape(t *testing.T) { + res := run(t, "filter ispresent(latency) | stats count(*), avg(latency) by level | sort level asc") + + if res.Matched != 3 { + t.Fatalf("Matched = %d, want the 3 records that reached stats", res.Matched) + } + + if len(res.Rows) != 3 { + t.Fatalf("rows = %d, want 3", len(res.Rows)) + } + + var names []string + for _, f := range res.Rows[0] { + names = append(names, f.Name) + } + + if got := strings.Join(names, ","); got != "level,count(*),avg(latency)" { + t.Fatalf("columns = %s, want the by field then the aggregations and no @ptr", got) + } +} + +func TestStatsNoRecords(t *testing.T) { + if res := run(t, "filter level = 'nope' | stats count(*)"); len(res.Rows) != 0 { + t.Fatalf("rows = %v, want none", res.Rows) + } +} + +func TestStatsBin(t *testing.T) { + msgs := []string{`{"v":1}`, `{"v":2}`, `{"v":4}`, `{"v":8}`} + offsets := []time.Duration{time.Minute, 4 * time.Minute, 6 * time.Minute, 11*time.Minute + 59*time.Second} + recs := func() []Record { return eventsAt("s", msgs, offsets) } + + res := runOn(t, "stats count(*) as n, sum(v) as s by bin(5m)", recs()) + if got := strings.Join(col(res.Rows, "bin(5m)"), "|"); got != "2025-01-01 00:10:00.000|2025-01-01 00:05:00.000|2025-01-01 00:00:00.000" { + t.Fatalf("bins = %s, want newest first", got) + } + + if got := strings.Join(col(res.Rows, "s"), "|"); got != "8|4|3" { + t.Fatalf("sums = %s", got) + } + + res = runOn(t, "stats sum(v) as s by bin(1h) as hour", recs()) + if got := strings.Join(col(res.Rows, "hour"), "|") + "=" + strings.Join(col(res.Rows, "s"), "|"); got != "2025-01-01 00:00:00.000=15" { + t.Fatalf("hourly = %s", got) + } + + res = runOn(t, "stats sum(v) as s by bin(5m) as @t | stats max(s) as peak, avg(s) as mean by dateceil(@t, 10m) as w | sort w asc", + recs()) + if got := strings.Join(col(res.Rows, "w"), "|") + "=" + strings.Join(col(res.Rows, "peak"), "|"); got != + "2025-01-01 00:00:00.000|2025-01-01 00:10:00.000=3|8" { + t.Fatalf("chained bins = %s", got) + } +} + +func TestStatsCompileErrors(t *testing.T) { + tooMany := strings.TrimSuffix(strings.Repeat("stats count(*) as n | ", maxStatsCommands+1), " | ") + tenOK := strings.TrimSuffix(strings.Repeat("stats count(*) as n | ", maxStatsCommands), " | ") + + if _, err := Parse(tenOK); err != nil { + t.Fatalf("10 stats commands: %v", err) + } + + for _, q := range []string{ + "stats", + "stats level", + "stats count(*) by", + "stats count(*) as", + "stats count(*) | sort n | stats count(*)", + "limit 5 | stats count(*)", + "stats count(*) by level | stats max(latency)", + "stats sum(latency) as s by @logStream | stats avg(s) by bin(5m)", + "stats count(*) as n | filter level = 'x'", + "stats count(*) as n | fields @message", + "stats count(*) as n | sort level", + "filter count(*) > 1", + "fields sum(latency)", + "stats sum(count(*))", + "stats count(**)", + "stats sum(*)", + "stats pct(latency)", + "stats pct(latency, 101)", + "stats pct(latency, x)", + "stats count(*) by bin(5x)", + "stats count(*) by bin(0m)", + "stats count(*) by bin(m)", + "stats count(*) by bin(1.5mo)", + "stats count(*) by bin(99999999999999999999h)", + "stats count(*) by bin()", + "stats count(*) by bin(5m", + "stats count(*) by datefloor(@timestamp)", + tooMany, + } { + _, err := Parse(q) + + var ce *CompileError + if !errors.As(err, &ce) { + t.Errorf("Parse(%q) = %v, want a CompileError", q, err) + continue + } + + if ce.Start < 0 || ce.End < ce.Start || ce.End > len(q) { + t.Errorf("Parse(%q) offsets %d..%d out of range", q, ce.Start, ce.End) + } + } +} + +func TestStatsChainingAllowed(t *testing.T) { + for _, q := range []string{ + "stats sum(latency) as s, max(@timestamp) as @t by @logStream | stats avg(s) by dateceil(@t, 5m)", + "stats count(*) as n by level | fields n + 1 as m | stats sum(m) as total", + "stats count(*) as n by level | parse level /(?.)/ | stats count(*) by first", + "stats count(*) as n by level | sort n desc | limit 2", + } { + if _, err := Parse(q); err != nil { + t.Errorf("Parse(%q): %v", q, err) + } + } +} diff --git a/providers/aws/cloudwatchlogs/insights_queries_test.go b/providers/aws/cloudwatchlogs/insights_queries_test.go index c825509c2..ab5622c8b 100644 --- a/providers/aws/cloudwatchlogs/insights_queries_test.go +++ b/providers/aws/cloudwatchlogs/insights_queries_test.go @@ -156,7 +156,7 @@ func TestInsightsStartQueryValidation(t *testing.T) { require.NoError(t, err, "a log group ARN identifies the group") in = base() - in.QueryString = "fields @message | stats count(*)" + in.QueryString = "fields @message | pattern @message" _, err = m.StartQuery(ctx, in) var mq *driver.MalformedQueryError diff --git a/server/aws/cloudwatchlogs/insights_stats_sdk_test.go b/server/aws/cloudwatchlogs/insights_stats_sdk_test.go new file mode 100644 index 000000000..4dd871050 --- /dev/null +++ b/server/aws/cloudwatchlogs/insights_stats_sdk_test.go @@ -0,0 +1,113 @@ +package cloudwatchlogs_test + +import ( + "context" + "errors" + "testing" + "time" + + "github.com/aws/aws-sdk-go-v2/aws" + cwl "github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs" + cwltypes "github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs/types" +) + +// runInsights starts a query over the last hour and polls until it leaves +// Running, the way real clients do. +func runInsights(t *testing.T, c *cwl.Client, group, query string) *cwl.GetQueryResultsOutput { + t.Helper() + + ctx := context.Background() + now := time.Now() + + start, err := c.StartQuery(ctx, &cwl.StartQueryInput{ + LogGroupName: aws.String(group), StartTime: aws.Int64(now.Add(-time.Hour).Unix()), EndTime: aws.Int64(now.Unix() + 1), + QueryString: aws.String(query), + }) + if err != nil { + t.Fatalf("StartQuery(%q): %v", query, err) + } + + for range 5 { + res, rerr := c.GetQueryResults(ctx, &cwl.GetQueryResultsInput{QueryId: start.QueryId}) + if rerr != nil { + t.Fatalf("GetQueryResults: %v", rerr) + } + + if res.Status != cwltypes.QueryStatusRunning && res.Status != cwltypes.QueryStatusScheduled { + return res + } + } + + t.Fatalf("query %q never completed", query) + + return nil +} + +func TestSDKLogsInsightsStatsParseDedup(t *testing.T) { + c := newLogsClient(t) + ctx := context.Background() + g, s := aws.String("/insights/stats"), aws.String("s") + + if _, err := c.CreateLogGroup(ctx, &cwl.CreateLogGroupInput{LogGroupName: g}); err != nil { + t.Fatalf("CreateLogGroup: %v", err) + } + + if _, err := c.CreateLogStream(ctx, &cwl.CreateLogStreamInput{LogGroupName: g, LogStreamName: s}); err != nil { + t.Fatalf("CreateLogStream: %v", err) + } + + // Events sit at the start of the current hour so they share one hourly bin. + now := time.Now() + hour := now.UTC().Truncate(time.Hour) + msgs := []string{ + `{"level":"ERROR","latency":120}`, `{"level":"INFO","latency":15}`, `{"level":"INFO","latency":45}`, + "user=alice action=login", "user=bob action=logout", + } + + events := make([]cwltypes.InputLogEvent, 0, len(msgs)) + for i, m := range msgs { + events = append(events, cwltypes.InputLogEvent{ + Timestamp: aws.Int64(hour.Add(time.Duration(i) * time.Millisecond).UnixMilli()), Message: aws.String(m), + }) + } + + if _, err := c.PutLogEvents(ctx, &cwl.PutLogEventsInput{LogGroupName: g, LogStreamName: s, LogEvents: events}); err != nil { + t.Fatalf("PutLogEvents: %v", err) + } + + res := runInsights(t, c, *g, "filter ispresent(level) | stats count(*) as n, avg(latency) as a by level | sort level asc") + if len(res.Results) != 2 || resultValue(res.Results[1], "n") != "2" || resultValue(res.Results[1], "a") != "30" || + resultValue(res.Results[1], "@ptr") != "" { + t.Fatalf("stats by level = %+v", res.Results) + } + + if res.Statistics.RecordsMatched != 3 || res.Statistics.RecordsScanned != 5 { + t.Fatalf("statistics = %+v, want 3 matched of 5 scanned", res.Statistics) + } + + res = runInsights(t, c, *g, "stats count(*) as n by bin(1h)") + if len(res.Results) != 1 || resultValue(res.Results[0], "n") != "5" || + resultValue(res.Results[0], "bin(1h)") != hour.Format("2006-01-02 15:04:05.000") { + t.Fatalf("stats by bin = %+v", res.Results) + } + + res = runInsights(t, c, *g, `parse @message "user=* action=*" as user, action | filter ispresent(user) | sort user asc | display user, action`) + if len(res.Results) != 2 || resultValue(res.Results[0], "user") != "alice" || resultValue(res.Results[1], "action") != "logout" { + t.Fatalf("parse = %+v", res.Results) + } + + res = runInsights(t, c, *g, "fields level, latency | filter ispresent(level) | dedup level") + if len(res.Results) != 2 || resultValue(res.Results[0], "latency") != "45" || res.Statistics.RecordsMatched != 3 { + t.Fatalf("dedup = %+v, %+v", res.Results, res.Statistics) + } + + _, err := c.StartQuery(ctx, &cwl.StartQueryInput{ + LogGroupName: g, StartTime: aws.Int64(now.Add(-time.Hour).Unix()), EndTime: aws.Int64(now.Unix() + 1), + QueryString: aws.String("sort @timestamp | stats count(*)"), + }) + + var mq *cwltypes.MalformedQueryException + if !errors.As(err, &mq) { + t.Fatalf("sort before stats: got %v, want MalformedQueryException", err) + } +} From 437ba7986854ecc57856fa8686bf3d2fa4193c20 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Patel Date: Fri, 23 Oct 2026 03:48:40 +0530 Subject: [PATCH 13/31] fix(aws-eventbridge): global endpoints and PutEvents EndpointId routing (#1685) --- docs/coverage/aws/eventbridge.md | 3 + docs/coverage/azure/eventgrid.md | 3 + docs/coverage/gcp/eventarc.md | 3 + docs/coverage/nongoals/eventbus.md | 3 + internal/settle/settle.go | 2 + providers/aws/eventbridge/endpoints.go | 496 +++++++++++++++++++ providers/aws/eventbridge/endpoints_test.go | 321 ++++++++++++ providers/aws/eventbridge/eventbridge.go | 6 + providers/aws/eventbridge/snapshot.go | 2 + providers/aws/eventbridge/validation.go | 22 + server/aws/eventbridge/endpoints.go | 294 +++++++++++ server/aws/eventbridge/endpoints_sdk_test.go | 242 +++++++++ server/aws/eventbridge/handler.go | 4 +- server/aws/eventbridge/operations.go | 57 ++- server/aws/eventbridge/types.go | 2 + services/eventbus/driver/endpoint.go | 66 +++ 16 files changed, 1509 insertions(+), 17 deletions(-) create mode 100644 providers/aws/eventbridge/endpoints.go create mode 100644 providers/aws/eventbridge/endpoints_test.go create mode 100644 server/aws/eventbridge/endpoints.go create mode 100644 server/aws/eventbridge/endpoints_sdk_test.go create mode 100644 services/eventbus/driver/endpoint.go diff --git a/docs/coverage/aws/eventbridge.md b/docs/coverage/aws/eventbridge.md index 319ebef7b..2bd0cda36 100644 --- a/docs/coverage/aws/eventbridge.md +++ b/docs/coverage/aws/eventbridge.md @@ -28,3 +28,6 @@ AWS's `eventbus` service ยท portable interface `driver.EventBus` ยท [AWS index]( - AWS: API destination targets are called in the background after `PutEvents` returns, at most 64 deliveries at a time per emulator. A failed call is retried 2 times by default (AWS retries up to 185 times over 24 hours); an explicit `RetryPolicy.MaximumRetryAttempts` is honored. A `Retry-After` from the endpoint delays the next attempt by at most 5 seconds, and one delivery stops retrying after 30 seconds, whatever `MaximumEventAgeInSeconds` says. Then the event goes to the target's dead-letter queue. - AWS: in-flight API destination deliveries, their retry state and cached OAuth tokens are not persisted in snapshots, and a delivery still running when the emulator stops is dropped. +- AWS: `PutEvents` with `EndpointId` works against a custom endpoint URL. aws-sdk-go-v2 and the AWS CLI (which needs the bundled `awscrt`) keep the configured endpoint instead of `.endpoint.events.amazonaws.com` and sign the request with SigV4a. cloudemu does not verify SigV4a, so with `--enforce-auth` such a request is refused. A SigV4a signature carries no Region, so `cloudemu serve` handles the request in its default Region (`--region`), and the endpoint must be created there. +- AWS: a global endpoint has no second Region behind it. Route 53 health checks are not evaluated, so events never fail over, and event replication to the secondary Region is not performed. `PutEvents` through an endpoint goes to its event bus in the endpoint's home Region: an entry with no `EventBusName`, or one naming either endpoint bus by name or ARN, lands on the bus of that name, and an entry naming another bus goes to that bus. +- AWS: `CreateEndpoint` and `UpdateEndpoint` answer `CREATING` and `UPDATING`. Reads then show `ACTIVE`, at once or after a short window with `--async-settle`. `DeleteEndpoint` removes the endpoint at once, without a `DELETING` phase, so `ConcurrentModificationException` and the `*_FAILED` states never occur. diff --git a/docs/coverage/azure/eventgrid.md b/docs/coverage/azure/eventgrid.md index 04024e5d2..801992be8 100644 --- a/docs/coverage/azure/eventgrid.md +++ b/docs/coverage/azure/eventgrid.md @@ -28,3 +28,6 @@ Azure's `eventbus` service ยท portable interface `driver.EventBus` ยท [Azure ind - AWS: API destination targets are called in the background after `PutEvents` returns, at most 64 deliveries at a time per emulator. A failed call is retried 2 times by default (AWS retries up to 185 times over 24 hours); an explicit `RetryPolicy.MaximumRetryAttempts` is honored. A `Retry-After` from the endpoint delays the next attempt by at most 5 seconds, and one delivery stops retrying after 30 seconds, whatever `MaximumEventAgeInSeconds` says. Then the event goes to the target's dead-letter queue. - AWS: in-flight API destination deliveries, their retry state and cached OAuth tokens are not persisted in snapshots, and a delivery still running when the emulator stops is dropped. +- AWS: `PutEvents` with `EndpointId` works against a custom endpoint URL. aws-sdk-go-v2 and the AWS CLI (which needs the bundled `awscrt`) keep the configured endpoint instead of `.endpoint.events.amazonaws.com` and sign the request with SigV4a. cloudemu does not verify SigV4a, so with `--enforce-auth` such a request is refused. A SigV4a signature carries no Region, so `cloudemu serve` handles the request in its default Region (`--region`), and the endpoint must be created there. +- AWS: a global endpoint has no second Region behind it. Route 53 health checks are not evaluated, so events never fail over, and event replication to the secondary Region is not performed. `PutEvents` through an endpoint goes to its event bus in the endpoint's home Region: an entry with no `EventBusName`, or one naming either endpoint bus by name or ARN, lands on the bus of that name, and an entry naming another bus goes to that bus. +- AWS: `CreateEndpoint` and `UpdateEndpoint` answer `CREATING` and `UPDATING`. Reads then show `ACTIVE`, at once or after a short window with `--async-settle`. `DeleteEndpoint` removes the endpoint at once, without a `DELETING` phase, so `ConcurrentModificationException` and the `*_FAILED` states never occur. diff --git a/docs/coverage/gcp/eventarc.md b/docs/coverage/gcp/eventarc.md index fd2edf3a9..6d8aebdac 100644 --- a/docs/coverage/gcp/eventarc.md +++ b/docs/coverage/gcp/eventarc.md @@ -28,3 +28,6 @@ GCP's `eventbus` service ยท portable interface `driver.EventBus` ยท [GCP index]( - AWS: API destination targets are called in the background after `PutEvents` returns, at most 64 deliveries at a time per emulator. A failed call is retried 2 times by default (AWS retries up to 185 times over 24 hours); an explicit `RetryPolicy.MaximumRetryAttempts` is honored. A `Retry-After` from the endpoint delays the next attempt by at most 5 seconds, and one delivery stops retrying after 30 seconds, whatever `MaximumEventAgeInSeconds` says. Then the event goes to the target's dead-letter queue. - AWS: in-flight API destination deliveries, their retry state and cached OAuth tokens are not persisted in snapshots, and a delivery still running when the emulator stops is dropped. +- AWS: `PutEvents` with `EndpointId` works against a custom endpoint URL. aws-sdk-go-v2 and the AWS CLI (which needs the bundled `awscrt`) keep the configured endpoint instead of `.endpoint.events.amazonaws.com` and sign the request with SigV4a. cloudemu does not verify SigV4a, so with `--enforce-auth` such a request is refused. A SigV4a signature carries no Region, so `cloudemu serve` handles the request in its default Region (`--region`), and the endpoint must be created there. +- AWS: a global endpoint has no second Region behind it. Route 53 health checks are not evaluated, so events never fail over, and event replication to the secondary Region is not performed. `PutEvents` through an endpoint goes to its event bus in the endpoint's home Region: an entry with no `EventBusName`, or one naming either endpoint bus by name or ARN, lands on the bus of that name, and an entry naming another bus goes to that bus. +- AWS: `CreateEndpoint` and `UpdateEndpoint` answer `CREATING` and `UPDATING`. Reads then show `ACTIVE`, at once or after a short window with `--async-settle`. `DeleteEndpoint` removes the endpoint at once, without a `DELETING` phase, so `ConcurrentModificationException` and the `*_FAILED` states never occur. diff --git a/docs/coverage/nongoals/eventbus.md b/docs/coverage/nongoals/eventbus.md index 27bb3df86..caa1dd4cb 100644 --- a/docs/coverage/nongoals/eventbus.md +++ b/docs/coverage/nongoals/eventbus.md @@ -1,2 +1,5 @@ - AWS: API destination targets are called in the background after `PutEvents` returns, at most 64 deliveries at a time per emulator. A failed call is retried 2 times by default (AWS retries up to 185 times over 24 hours); an explicit `RetryPolicy.MaximumRetryAttempts` is honored. A `Retry-After` from the endpoint delays the next attempt by at most 5 seconds, and one delivery stops retrying after 30 seconds, whatever `MaximumEventAgeInSeconds` says. Then the event goes to the target's dead-letter queue. - AWS: in-flight API destination deliveries, their retry state and cached OAuth tokens are not persisted in snapshots, and a delivery still running when the emulator stops is dropped. +- AWS: `PutEvents` with `EndpointId` works against a custom endpoint URL. aws-sdk-go-v2 and the AWS CLI (which needs the bundled `awscrt`) keep the configured endpoint instead of `.endpoint.events.amazonaws.com` and sign the request with SigV4a. cloudemu does not verify SigV4a, so with `--enforce-auth` such a request is refused. A SigV4a signature carries no Region, so `cloudemu serve` handles the request in its default Region (`--region`), and the endpoint must be created there. +- AWS: a global endpoint has no second Region behind it. Route 53 health checks are not evaluated, so events never fail over, and event replication to the secondary Region is not performed. `PutEvents` through an endpoint goes to its event bus in the endpoint's home Region: an entry with no `EventBusName`, or one naming either endpoint bus by name or ARN, lands on the bus of that name, and an entry naming another bus goes to that bus. +- AWS: `CreateEndpoint` and `UpdateEndpoint` answer `CREATING` and `UPDATING`. Reads then show `ACTIVE`, at once or after a short window with `--async-settle`. `DeleteEndpoint` removes the endpoint at once, without a `DELETING` phase, so `ConcurrentModificationException` and the `*_FAILED` states never occur. diff --git a/internal/settle/settle.go b/internal/settle/settle.go index dcbffda5a..acb4867da 100644 --- a/internal/settle/settle.go +++ b/internal/settle/settle.go @@ -70,6 +70,8 @@ const ( DefaultLatticeSettle = 2 * time.Second // VPC Lattice resource CREATE_IN_PROGRESS->ACTIVE + DefaultEventBridgeEndpointSettle = 2 * time.Second // EventBridge global endpoint CREATING/UPDATING->ACTIVE + DefaultBedrockFlowExecutionSettle = 2 * time.Second // Bedrock flow execution Running->Succeeded DefaultBedrockIngestionSettle = 2 * time.Second // Bedrock KB ingestion job IN_PROGRESS->COMPLETE diff --git a/providers/aws/eventbridge/endpoints.go b/providers/aws/eventbridge/endpoints.go new file mode 100644 index 000000000..749bed7bf --- /dev/null +++ b/providers/aws/eventbridge/endpoints.go @@ -0,0 +1,496 @@ +package eventbridge + +import ( + "context" + "crypto/sha256" + "fmt" + "math/big" + "regexp" + "strings" + + "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/internal/idgen" + "github.com/stackshy/cloudemu/v2/internal/pagination" + "github.com/stackshy/cloudemu/v2/internal/settle" + "github.com/stackshy/cloudemu/v2/services/eventbus/driver" +) + +const ( + endpointStateActive = "ACTIVE" + + replicationEnabled = "ENABLED" + replicationDisabled = "DISABLED" + + maxEndpointNameLen = 64 + maxEndpoints = 100 + maxEndpointPageSize = 100 + endpointBusCount = 2 + maxHealthCheckLen = 1600 + maxEndpointBusARNLen = 512 + maxRoleARNLen = 256 + minRegionLen = 9 + maxRegionLen = 20 + maxEndpointIDLen = 50 + + // endpointIDHostLen and endpointIDZoneLen size the two labels of a + // generated EndpointId, as in "abcdefghij.xyz". + endpointIDHostLen = 10 + endpointIDZoneLen = 3 + base36 = 36 + + // endpointURLSuffix is the host suffix the AWS SDKs resolve a global + // endpoint to: https://.endpoint.events.amazonaws.com. + endpointURLSuffix = ".endpoint.events.amazonaws.com" + + msgEndpointLimit = "The requested resource exceeds the maximum number allowed." + msgBadNextPage = "The NextToken provided is not valid." +) + +// Patterns of the global endpoint members, as the EventBridge API model spells them. +const ( + healthCheckPattern = `^arn:aws([a-z]|\-)*:route53:::healthcheck/[\-a-z0-9]+$` + regionPattern = `^[\-a-z0-9]+$` + endpointBusPattern = `^arn:aws[a-z-]*:events:[a-z]+-[a-z-]+-\d+:\d{12}:event-bus/[\w.-]+$` + roleARNPattern = `^arn:aws[a-z-]*:iam::\d{12}:role\/[\w+=,.@/-]+$` + endpointIDPattern = `^[A-Za-z0-9\-]+[\.][A-Za-z0-9\-]+$` +) + +var ( + healthCheckRE = regexp.MustCompile(healthCheckPattern) + regionRE = regexp.MustCompile(regionPattern) + endpointBusRE = regexp.MustCompile(endpointBusPattern) + // roleARNRE and endpointIDRE are roleARNPattern and endpointIDPattern + // without their redundant escapes. + roleARNRE = regexp.MustCompile(`^arn:aws[a-z-]*:iam::\d{12}:role/[\w+=,.@/-]+$`) + endpointIDRE = regexp.MustCompile(`^[A-Za-z0-9\-]+\.[A-Za-z0-9\-]+$`) +) + +func errEndpointNotExist(name string) error { + return errors.Newf(errors.NotFound, "Endpoint %s does not exist.", name) +} + +func errInvalidParam(param, reason string) error { + return errors.Newf(errors.InvalidArgument, "Parameter %s is not valid. Reason: %s", param, reason) +} + +// endpointIDFor derives a stable EndpointId from the endpoint's account, Region +// and name: ten and three lowercase alphanumerics joined by a dot. +func endpointIDFor(account, region, name string) string { + sum := sha256.Sum256([]byte(account + "/" + region + "/" + name)) + s := new(big.Int).SetBytes(sum[:]).Text(base36) + + if n := endpointIDHostLen + endpointIDZoneLen; len(s) < n { + s = strings.Repeat("0", n-len(s)) + s + } + + return s[:endpointIDHostLen] + "." + s[endpointIDHostLen:endpointIDHostLen+endpointIDZoneLen] +} + +// regionOfBusARN returns the Region field of an event bus ARN. +func regionOfBusARN(arn string) string { + const regionField = 3 + + parts := strings.Split(arn, ":") + if len(parts) <= regionField { + return "" + } + + return parts[regionField] +} + +// endpointViolations checks every member of an endpoint configuration against +// the API model's required flags, lengths, patterns and enums. +func endpointViolations(cfg *driver.EndpointConfig) violations { + var v violations + + v.name(cfg.Name, "name", maxEndpointNameLen) + v.maxLen(cfg.Description, "description", maxDescriptionLen) + + if v.present(cfg.Routing.HealthCheck, "routingConfig.failoverConfig.primary.healthCheck") { + v.lengthBetween(cfg.Routing.HealthCheck, "routingConfig.failoverConfig.primary.healthCheck", 1, maxHealthCheckLen) + v.pattern(cfg.Routing.HealthCheck, "routingConfig.failoverConfig.primary.healthCheck", healthCheckRE, healthCheckPattern) + } + + if v.present(cfg.Routing.SecondaryRoute, "routingConfig.failoverConfig.secondary.route") { + v.region(cfg.Routing.SecondaryRoute, "routingConfig.failoverConfig.secondary.route") + } + + if cfg.ReplicationState != "" { + v.enum(cfg.ReplicationState, "replicationConfig.state", []string{replicationEnabled, replicationDisabled}) + } + + v.endpointBuses(cfg.EventBusARNs) + + if cfg.RoleARN != "" { + v.lengthBetween(cfg.RoleARN, "roleArn", 1, maxRoleARNLen) + v.pattern(cfg.RoleARN, "roleArn", roleARNRE, roleARNPattern) + } + + return v +} + +func (v *violations) region(value, field string) { + v.lengthBetween(value, field, minRegionLen, maxRegionLen) + v.pattern(value, field, regionRE, regionPattern) +} + +func (v *violations) endpointBuses(arns []string) { + if len(arns) != endpointBusCount { + v.add("["+strings.Join(arns, ", ")+"]", "eventBuses", + fmt.Sprintf("Member must have length less than or equal to %d and greater than or equal to %d", + endpointBusCount, endpointBusCount)) + } + + for i, arn := range arns { + field := fmt.Sprintf("eventBuses.%d.member.eventBusArn", i+1) + if v.present(arn, field) { + v.lengthBetween(arn, field, 1, maxEndpointBusARNLen) + v.pattern(arn, field, endpointBusRE, endpointBusPattern) + } + } +} + +// endpointRuleError checks the rules that tie the members together: the two +// event buses share a name, sit in different Regions, one of them in the home +// Region and the other in the secondary Region, and replication needs a role. +func endpointRuleError(cfg *driver.EndpointConfig, home string) error { + first, second := cfg.EventBusARNs[0], cfg.EventBusARNs[1] + + switch r1, r2 := regionOfBusARN(first), regionOfBusARN(second); { + case busNameFromARN(first) != busNameFromARN(second): + return errInvalidParam("EventBuses", "The event bus names must be identical in each Region.") + case r1 == r2: + return errInvalidParam("EventBuses", "The event buses must be in different Regions.") + case r1 != home && r2 != home: + return errInvalidParam("EventBuses", "One event bus must be in the endpoint's Region "+home+".") + case cfg.Routing.SecondaryRoute == home || (cfg.Routing.SecondaryRoute != r1 && cfg.Routing.SecondaryRoute != r2): + return errInvalidParam("RoutingConfig.FailoverConfig.Secondary.Route", + "The secondary Region must be the Region of the other event bus.") + } + + return nil +} + +func (m *Mock) validateEndpoint(cfg *driver.EndpointConfig) error { + if err := endpointViolations(cfg).err(); err != nil { + return err + } + + if err := endpointRuleError(cfg, m.opts.Region); err != nil { + return err + } + + if cfg.ReplicationState != replicationDisabled && cfg.RoleARN == "" { + return errInvalidParam("RoleArn", "RoleArn is required when event replication is ENABLED.") + } + + return nil +} + +// cloneEndpoint copies e so callers never share its event bus slice. +func cloneEndpoint(e *driver.Endpoint) *driver.Endpoint { + out := *e + out.EventBusARNs = append([]string(nil), e.EventBusARNs...) + + return &out +} + +// CreateEndpoint creates a global endpoint homed in the emulator's Region. It +// answers CREATING, as EventBridge does. Reads then show ACTIVE, after a short +// CREATING window when async settling is on. +func (m *Mock) CreateEndpoint(_ context.Context, cfg *driver.EndpointConfig) (*driver.Endpoint, error) { + if err := m.validateEndpoint(cfg); err != nil { + return nil, err + } + + if m.endpoints.Len() >= maxEndpoints { + return nil, errors.New(errors.ResourceExhausted, msgEndpointLimit) + } + + now := m.opts.Clock.Now().UTC() + id := endpointIDFor(m.opts.AccountID, m.opts.Region, cfg.Name) + + replication := cfg.ReplicationState + if replication == "" { + replication = replicationEnabled + } + + ep := driver.Endpoint{ + Name: cfg.Name, + Description: cfg.Description, + ARN: idgen.AWSARN("events", m.opts.Region, m.opts.AccountID, "endpoint/"+cfg.Name), + Routing: cfg.Routing, + ReplicationState: replication, + EventBusARNs: append([]string(nil), cfg.EventBusARNs...), + RoleARN: cfg.RoleARN, + EndpointID: id, + EndpointURL: "https://" + id + endpointURLSuffix, + HomeRegion: m.opts.Region, + State: endpointStateActive, + CreatedAt: now, + LastModifiedAt: now, + } + + if !m.endpoints.SetIfAbsent(cfg.Name, ep) { + return nil, errors.Newf(errors.AlreadyExists, "Endpoint %s already exists.", cfg.Name) + } + + m.endpointSettle.Begin(cfg.Name, stateCreating, now, m.opts.SettleDuration(settle.DefaultEventBridgeEndpointSettle)) + + out := cloneEndpoint(&ep) + out.State = stateCreating + + return out, nil +} + +// observedEndpoint looks up an endpoint and overlays its settle window: with +// async settling on, a fresh create or update reads as CREATING or UPDATING +// for a moment before ACTIVE; otherwise it is ACTIVE at once. +func (m *Mock) observedEndpoint(name string) (driver.Endpoint, bool) { + ep, ok := m.endpoints.Get(name) + if !ok { + return driver.Endpoint{}, false + } + + ep.EventBusARNs = append([]string(nil), ep.EventBusARNs...) + ep.State = m.endpointSettle.State(name, m.opts.Clock.Now(), ep.State) + + return ep, true +} + +func validateHomeRegion(home string) error { + if home == "" { + return nil + } + + var v violations + + v.region(home, "homeRegion") + + return v.err() +} + +// DescribeEndpoint returns a global endpoint. A non-empty homeRegion must be +// the endpoint's home Region, or the endpoint is not found. +func (m *Mock) DescribeEndpoint(_ context.Context, name, homeRegion string) (*driver.Endpoint, error) { + var v violations + + v.name(name, "name", maxEndpointNameLen) + + if err := v.err(); err != nil { + return nil, err + } + + if err := validateHomeRegion(homeRegion); err != nil { + return nil, err + } + + ep, ok := m.observedEndpoint(name) + if !ok || (homeRegion != "" && homeRegion != ep.HomeRegion) { + return nil, errEndpointNotExist(name) + } + + return &ep, nil +} + +// mergedEndpointConfig is the endpoint configuration e would have after upd. +func mergedEndpointConfig(e *driver.Endpoint, upd *driver.EndpointUpdate) driver.EndpointConfig { + cfg := driver.EndpointConfig{ + Name: e.Name, Description: e.Description, Routing: e.Routing, + ReplicationState: e.ReplicationState, EventBusARNs: e.EventBusARNs, RoleARN: e.RoleARN, + } + + if upd.Description != nil { + cfg.Description = *upd.Description + } + + if upd.Routing != nil { + cfg.Routing = *upd.Routing + } + + if upd.ReplicationState != nil { + cfg.ReplicationState = *upd.ReplicationState + } + + if upd.EventBusARNs != nil { + cfg.EventBusARNs = append([]string(nil), upd.EventBusARNs...) + } + + if upd.RoleARN != nil { + cfg.RoleARN = *upd.RoleARN + } + + return cfg +} + +// UpdateEndpoint changes a global endpoint's description, routing, replication, +// event buses or role. Omitted fields keep their values. It answers UPDATING; +// reads then show ACTIVE, after a short UPDATING window when async settling is +// on. +func (m *Mock) UpdateEndpoint(_ context.Context, upd *driver.EndpointUpdate) (*driver.Endpoint, error) { + var v violations + + v.name(upd.Name, "name", maxEndpointNameLen) + + if err := v.err(); err != nil { + return nil, err + } + + var ( + out driver.Endpoint + applyErr error + ) + + now := m.opts.Clock.Now().UTC() + + found := m.endpoints.Update(upd.Name, func(e driver.Endpoint) driver.Endpoint { + cfg := mergedEndpointConfig(&e, upd) + if applyErr = m.validateEndpoint(&cfg); applyErr != nil { + return e + } + + e.Description, e.Routing, e.RoleARN = cfg.Description, cfg.Routing, cfg.RoleARN + e.ReplicationState, e.EventBusARNs = cfg.ReplicationState, cfg.EventBusARNs + e.StateReason, e.LastModifiedAt = "", now + out = e + + return e + }) + + if !found { + return nil, errEndpointNotExist(upd.Name) + } + + if applyErr != nil { + return nil, applyErr + } + + m.endpointSettle.Begin(upd.Name, stateUpdating, now, m.opts.SettleDuration(settle.DefaultEventBridgeEndpointSettle)) + + res := cloneEndpoint(&out) + res.State = stateUpdating + + return res, nil +} + +// DeleteEndpoint deletes a global endpoint. Real EventBridge passes through +// DELETING first; here the endpoint is gone as soon as the call returns. +func (m *Mock) DeleteEndpoint(_ context.Context, name string) error { + var v violations + + v.name(name, "name", maxEndpointNameLen) + + if err := v.err(); err != nil { + return err + } + + if !m.endpoints.Delete(name) { + return errEndpointNotExist(name) + } + + m.endpointSettle.Clear(name) + + return nil +} + +func validateEndpointFilter(f *driver.EndpointFilter) error { + var v violations + + if f.NamePrefix != "" { + v.name(f.NamePrefix, "namePrefix", maxEndpointNameLen) + } + + if f.HomeRegion != "" { + v.region(f.HomeRegion, "homeRegion") + } + + if f.MaxResults < 0 || f.MaxResults > maxEndpointPageSize { + v.add(fmt.Sprint(f.MaxResults), "maxResults", + fmt.Sprintf("Member must have value less than or equal to %d and greater than or equal to 1", maxEndpointPageSize)) + } + + return v.err() +} + +// ListEndpoints lists global endpoints sorted by name, narrowed by name prefix +// and home Region, one page at a time. It returns the page and the token of +// the next one, or "" on the last page. +func (m *Mock) ListEndpoints(_ context.Context, f driver.EndpointFilter) ([]driver.Endpoint, string, error) { + if err := validateEndpointFilter(&f); err != nil { + return nil, "", err + } + + names := m.endpoints.Keys() + matched := make([]driver.Endpoint, 0, len(names)) + + for _, name := range names { + ep, ok := m.observedEndpoint(name) + if !ok || !strings.HasPrefix(name, f.NamePrefix) || (f.HomeRegion != "" && ep.HomeRegion != f.HomeRegion) { + continue + } + + matched = append(matched, ep) + } + + page, err := pagination.PaginateSorted(matched, func(a, b driver.Endpoint) bool { return a.Name < b.Name }, + f.NextToken, f.MaxResults) + if err != nil { + return nil, "", errors.New(errors.InvalidArgument, msgBadNextPage) + } + + return page.Items, page.NextPageToken, nil +} + +// endpointByID finds the global endpoint whose EndpointId is id. +func (m *Mock) endpointByID(id string) (driver.Endpoint, bool) { + all := m.endpoints.All() + + for name := range all { + if all[name].EndpointID == id { + return all[name], true + } + } + + return driver.Endpoint{}, false +} + +// PutEndpointEventEntries publishes a PutEvents batch sent through the global +// endpoint endpointID. The emulator runs a single Region, so the endpoint always +// routes to its primary event bus here: an entry with no EventBusName, or one +// naming either of the endpoint's buses by name or ARN, goes to the bus of that +// name in this Region. Failover and replication to the secondary Region are not +// modeled. An entry naming some other bus is published to it as usual. +func (m *Mock) PutEndpointEventEntries( + ctx context.Context, endpointID string, events []driver.Event, +) ([]driver.PutEventsEntryResult, error) { + var v violations + + v.lengthBetween(endpointID, "endpointId", 1, maxEndpointIDLen) + v.pattern(endpointID, "endpointId", endpointIDRE, endpointIDPattern) + + if err := v.err(); err != nil { + return nil, err + } + + ep, ok := m.endpointByID(endpointID) + if !ok { + return nil, errInvalidParam("EndpointId", "Endpoint "+endpointID+" does not exist.") + } + + primary := busNameFromARN(ep.EventBusARNs[0]) + + for i := range events { + name := events[i].EventBus + if n := busNameFromARN(name); n != "" { + name = n + } + + if name == "" { + name = primary + } + + events[i].EventBus = name + } + + return m.PutEventEntries(ctx, events) +} diff --git a/providers/aws/eventbridge/endpoints_test.go b/providers/aws/eventbridge/endpoints_test.go new file mode 100644 index 000000000..9170561d8 --- /dev/null +++ b/providers/aws/eventbridge/endpoints_test.go @@ -0,0 +1,321 @@ +package eventbridge + +import ( + "context" + "regexp" + "testing" + "time" + + "github.com/stackshy/cloudemu/v2/config" + "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/internal/settle" + "github.com/stackshy/cloudemu/v2/services/eventbus/driver" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const ( + testHealthCheck = "arn:aws:route53:::healthcheck/abc-123" + testRole = "arn:aws:iam::123456789012:role/replication" +) + +func endpointBusARN(region, name string) string { + return "arn:aws:events:" + region + ":123456789012:event-bus/" + name +} + +func endpointConfig(name string) *driver.EndpointConfig { + return &driver.EndpointConfig{ + Name: name, + Routing: driver.EndpointRouting{HealthCheck: testHealthCheck, SecondaryRoute: "us-west-2"}, + ReplicationState: replicationDisabled, + EventBusARNs: []string{endpointBusARN("us-east-1", "b1"), endpointBusARN("us-west-2", "b1")}, + } +} + +func TestEndpointLifecycle(t *testing.T) { + ctx := context.Background() + m, fc := newTestMock() + + created, err := m.CreateEndpoint(ctx, endpointConfig("ep1")) + require.NoError(t, err) + assert.Equal(t, "CREATING", created.State) + assert.Equal(t, "arn:aws:events:us-east-1:"+m.opts.AccountID+":endpoint/ep1", created.ARN) + assert.Regexp(t, regexp.MustCompile(`^[a-z0-9]{10}\.[a-z0-9]{3}$`), created.EndpointID) + assert.Equal(t, "https://"+created.EndpointID+".endpoint.events.amazonaws.com", created.EndpointURL) + assert.Equal(t, "us-east-1", created.HomeRegion) + + got, err := m.DescribeEndpoint(ctx, "ep1", "") + require.NoError(t, err) + assert.Equal(t, "ACTIVE", got.State, "with async settling off a create reads as ACTIVE at once") + assert.Equal(t, created.EndpointID, got.EndpointID) + + _, err = m.DescribeEndpoint(ctx, "ep1", "eu-west-1") + assert.True(t, errors.IsNotFound(err), "another home Region does not find it") + + fc.Advance(time.Minute) + + desc := "updated" + upd, err := m.UpdateEndpoint(ctx, &driver.EndpointUpdate{ + Name: "ep1", Description: &desc, + ReplicationState: strPtr(replicationEnabled), RoleARN: strPtr(testRole), + }) + require.NoError(t, err) + assert.Equal(t, "UPDATING", upd.State) + assert.Equal(t, replicationEnabled, upd.ReplicationState) + assert.True(t, upd.LastModifiedAt.After(upd.CreatedAt)) + + got, err = m.DescribeEndpoint(ctx, "ep1", "us-east-1") + require.NoError(t, err) + assert.Equal(t, "ACTIVE", got.State) + assert.Equal(t, "updated", got.Description) + assert.Equal(t, testHealthCheck, got.Routing.HealthCheck, "omitted fields keep their values") + + require.NoError(t, m.DeleteEndpoint(ctx, "ep1")) + + _, err = m.DescribeEndpoint(ctx, "ep1", "") + assert.True(t, errors.IsNotFound(err)) + assert.True(t, errors.IsNotFound(m.DeleteEndpoint(ctx, "ep1"))) + + _, err = m.UpdateEndpoint(ctx, &driver.EndpointUpdate{Name: "ep1", Description: &desc}) + assert.True(t, errors.IsNotFound(err)) +} + +func strPtr(s string) *string { return &s } + +func TestEndpointAsyncSettle(t *testing.T) { + ctx := context.Background() + fc := config.NewFakeClock(time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)) + m := New(config.NewOptions(config.WithClock(fc), config.WithRegion("us-east-1"), config.WithAsyncSettle())) + + _, err := m.CreateEndpoint(ctx, endpointConfig("ep")) + require.NoError(t, err) + + got, err := m.DescribeEndpoint(ctx, "ep", "") + require.NoError(t, err) + assert.Equal(t, "CREATING", got.State) + + fc.Advance(settle.DefaultEventBridgeEndpointSettle) + + got, err = m.DescribeEndpoint(ctx, "ep", "") + require.NoError(t, err) + assert.Equal(t, "ACTIVE", got.State) + + _, err = m.UpdateEndpoint(ctx, &driver.EndpointUpdate{Name: "ep", Description: strPtr("x")}) + require.NoError(t, err) + + list, _, err := m.ListEndpoints(ctx, driver.EndpointFilter{}) + require.NoError(t, err) + assert.Equal(t, "UPDATING", list[0].State) + + fc.Advance(settle.DefaultEventBridgeEndpointSettle) + + got, err = m.DescribeEndpoint(ctx, "ep", "") + require.NoError(t, err) + assert.Equal(t, "ACTIVE", got.State) +} + +func TestEndpointCreateDefaultsAndDuplicates(t *testing.T) { + ctx := context.Background() + m, _ := newTestMock() + + cfg := endpointConfig("ep") + cfg.ReplicationState = "" + cfg.RoleARN = testRole + + ep, err := m.CreateEndpoint(ctx, cfg) + require.NoError(t, err) + assert.Equal(t, replicationEnabled, ep.ReplicationState, "replication defaults to ENABLED") + + _, err = m.CreateEndpoint(ctx, cfg) + assert.True(t, errors.IsAlreadyExists(err)) + assert.Equal(t, "Endpoint ep already exists.", errors.Message(err)) +} + +func TestEndpointValidation(t *testing.T) { + cases := map[string]func(c *driver.EndpointConfig){ + "bad name": func(c *driver.EndpointConfig) { c.Name = "bad name" }, + "no health check": func(c *driver.EndpointConfig) { c.Routing.HealthCheck = "" }, + "bad health check": func(c *driver.EndpointConfig) { c.Routing.HealthCheck = "arn:aws:route53:::nope" }, + "no route": func(c *driver.EndpointConfig) { c.Routing.SecondaryRoute = "" }, + "bad replication": func(c *driver.EndpointConfig) { c.ReplicationState = "ON" }, + "one bus": func(c *driver.EndpointConfig) { c.EventBusARNs = c.EventBusARNs[:1] }, + "three buses": func(c *driver.EndpointConfig) { c.EventBusARNs = append(c.EventBusARNs, c.EventBusARNs[0]) }, + "bad bus arn": func(c *driver.EndpointConfig) { c.EventBusARNs[1] = "arn:aws:sqs:us-west-2:123456789012:q" }, + "bus names differ": func(c *driver.EndpointConfig) { c.EventBusARNs[1] = endpointBusARN("us-west-2", "b2") }, + "same region": func(c *driver.EndpointConfig) { c.EventBusARNs[1] = endpointBusARN("us-east-1", "b1") }, + "no home region bus": func(c *driver.EndpointConfig) { c.EventBusARNs[0] = endpointBusARN("eu-west-1", "b1") }, + "route not other bus": func(c *driver.EndpointConfig) { c.Routing.SecondaryRoute = "eu-west-1" }, + "route is home": func(c *driver.EndpointConfig) { c.Routing.SecondaryRoute = "us-east-1" }, + "enabled without role": func(c *driver.EndpointConfig) { + c.ReplicationState = replicationEnabled + }, + "bad role": func(c *driver.EndpointConfig) { c.RoleARN = "arn:aws:iam::1:user/x" }, + } + + for name, mutate := range cases { + t.Run(name, func(t *testing.T) { + m, _ := newTestMock() + cfg := endpointConfig("ep") + mutate(cfg) + + _, err := m.CreateEndpoint(context.Background(), cfg) + assert.True(t, errors.IsInvalidArgument(err), "err = %v", err) + assert.Equal(t, 0, m.endpoints.Len()) + }) + } +} + +func TestEndpointValidationMessages(t *testing.T) { + m, _ := newTestMock() + cfg := endpointConfig("ep") + cfg.Routing.HealthCheck = "" + cfg.EventBusARNs = cfg.EventBusARNs[:1] + + _, err := m.CreateEndpoint(context.Background(), cfg) + assert.Equal(t, "2 validation errors detected: "+ + "Value null at 'routingConfig.failoverConfig.primary.healthCheck' failed to satisfy constraint: Member must not be null; "+ + "Value '["+endpointBusARN("us-east-1", "b1")+"]' at 'eventBuses' failed to satisfy constraint: "+ + "Member must have length less than or equal to 2 and greater than or equal to 2", errors.Message(err)) +} + +func TestEndpointUpdateValidation(t *testing.T) { + ctx := context.Background() + m, _ := newTestMock() + + _, err := m.CreateEndpoint(ctx, endpointConfig("ep")) + require.NoError(t, err) + + _, err = m.UpdateEndpoint(ctx, &driver.EndpointUpdate{Name: "ep", ReplicationState: strPtr(replicationEnabled)}) + assert.True(t, errors.IsInvalidArgument(err), "ENABLED replication needs a role") + + _, err = m.UpdateEndpoint(ctx, &driver.EndpointUpdate{ + Name: "ep", EventBusARNs: []string{endpointBusARN("us-east-1", "b1")}, + }) + assert.True(t, errors.IsInvalidArgument(err)) + + got, err := m.DescribeEndpoint(ctx, "ep", "") + require.NoError(t, err) + assert.Equal(t, replicationDisabled, got.ReplicationState, "a rejected update changes nothing") + assert.Len(t, got.EventBusARNs, 2) + + _, err = m.UpdateEndpoint(ctx, &driver.EndpointUpdate{ + Name: "ep", + Routing: &driver.EndpointRouting{HealthCheck: "arn:aws:route53:::healthcheck/new", SecondaryRoute: "us-west-2"}, + }) + require.NoError(t, err) + + got, err = m.DescribeEndpoint(ctx, "ep", "") + require.NoError(t, err) + assert.Equal(t, "arn:aws:route53:::healthcheck/new", got.Routing.HealthCheck) +} + +func TestEndpointLimit(t *testing.T) { + ctx := context.Background() + m, _ := newTestMock() + + for i := range maxEndpoints { + _, err := m.CreateEndpoint(ctx, endpointConfig("ep"+string(rune('a'+i/26))+string(rune('a'+i%26)))) + require.NoError(t, err) + } + + _, err := m.CreateEndpoint(ctx, endpointConfig("over")) + assert.Equal(t, errors.ResourceExhausted, errors.GetCode(err)) +} + +func TestListEndpointsFiltersAndPaging(t *testing.T) { + ctx := context.Background() + m, _ := newTestMock() + + for _, n := range []string{"b-2", "a-1", "a-2", "a-3"} { + _, err := m.CreateEndpoint(ctx, endpointConfig(n)) + require.NoError(t, err) + } + + page, next, err := m.ListEndpoints(ctx, driver.EndpointFilter{NamePrefix: "a-", MaxResults: 2}) + require.NoError(t, err) + require.Len(t, page, 2) + assert.Equal(t, "a-1", page[0].Name) + assert.Equal(t, "ACTIVE", page[0].State) + require.NotEmpty(t, next) + + page, next, err = m.ListEndpoints(ctx, driver.EndpointFilter{NamePrefix: "a-", MaxResults: 2, NextToken: next}) + require.NoError(t, err) + require.Len(t, page, 1) + assert.Equal(t, "a-3", page[0].Name) + assert.Empty(t, next) + + all, _, err := m.ListEndpoints(ctx, driver.EndpointFilter{HomeRegion: "us-east-1"}) + require.NoError(t, err) + assert.Len(t, all, 4) + + none, _, err := m.ListEndpoints(ctx, driver.EndpointFilter{HomeRegion: "eu-west-1"}) + require.NoError(t, err) + assert.Empty(t, none) + + _, _, err = m.ListEndpoints(ctx, driver.EndpointFilter{MaxResults: 101}) + assert.True(t, errors.IsInvalidArgument(err)) + + _, _, err = m.ListEndpoints(ctx, driver.EndpointFilter{NextToken: "garbage!"}) + assert.True(t, errors.IsInvalidArgument(err)) +} + +func TestPutEventsThroughEndpoint(t *testing.T) { + ctx := context.Background() + m, _ := newTestMock() + createTestBus(t, m, "b1") + + ep, err := m.CreateEndpoint(ctx, endpointConfig("ep")) + require.NoError(t, err) + + res, err := m.PutEndpointEventEntries(ctx, ep.EndpointID, []driver.Event{ + {Source: "s", DetailType: "t", Detail: "{}"}, + {Source: "s", DetailType: "t", Detail: "{}", EventBus: endpointBusARN("us-west-2", "b1")}, + {Source: "s", DetailType: "t", Detail: "{}", EventBus: "b1"}, + {Source: "s", DetailType: "t", Detail: "{}", EventBus: "default"}, + }) + require.NoError(t, err) + require.Len(t, res, 4) + + for _, r := range res { + assert.NotEmpty(t, r.EventID) + } + + b1, _ := m.buses.Get("b1") + assert.Len(t, b1.events, 3, "unnamed and endpoint-bus entries land on the local primary bus") + + def, _ := m.buses.Get(defaultBusName) + assert.Len(t, def.events, 1, "an entry naming another bus keeps it") + + _, err = m.PutEndpointEventEntries(ctx, "nope00000a.abc", []driver.Event{{Source: "s", DetailType: "t", Detail: "{}"}}) + assert.True(t, errors.IsInvalidArgument(err)) + assert.Equal(t, "Parameter EndpointId is not valid. Reason: Endpoint nope00000a.abc does not exist.", errors.Message(err)) + + _, err = m.PutEndpointEventEntries(ctx, "no-dot", []driver.Event{{Source: "s", DetailType: "t", Detail: "{}"}}) + assert.True(t, errors.IsInvalidArgument(err)) +} + +func TestSnapshotEndpoints(t *testing.T) { + ctx := context.Background() + src, _ := newTestMock() + + ep, err := src.CreateEndpoint(ctx, endpointConfig("ep")) + require.NoError(t, err) + + data, err := src.Snapshot(ctx, true) + require.NoError(t, err) + + dst, _ := newTestMock() + require.NoError(t, dst.Restore(ctx, data)) + + data2, err := dst.Snapshot(ctx, true) + require.NoError(t, err) + assert.JSONEq(t, string(data), string(data2)) + + got, err := dst.DescribeEndpoint(ctx, "ep", "") + require.NoError(t, err) + assert.Equal(t, ep.EndpointID, got.EndpointID) + assert.Equal(t, ep.EventBusARNs, got.EventBusARNs) + + _, err = dst.PutEndpointEventEntries(ctx, ep.EndpointID, []driver.Event{{Source: "s", DetailType: "t", Detail: "{}"}}) + require.NoError(t, err, "a restored endpoint still routes PutEvents") +} diff --git a/providers/aws/eventbridge/eventbridge.go b/providers/aws/eventbridge/eventbridge.go index d6094d720..80eef6e1c 100644 --- a/providers/aws/eventbridge/eventbridge.go +++ b/providers/aws/eventbridge/eventbridge.go @@ -19,6 +19,7 @@ import ( "github.com/stackshy/cloudemu/v2/internal/idgen" "github.com/stackshy/cloudemu/v2/internal/memstore" "github.com/stackshy/cloudemu/v2/internal/recursionguard" + "github.com/stackshy/cloudemu/v2/internal/settle" "github.com/stackshy/cloudemu/v2/services/eventbus/driver" mondriver "github.com/stackshy/cloudemu/v2/services/monitoring/driver" "github.com/stackshy/cloudemu/v2/services/scope" @@ -101,6 +102,9 @@ type Mock struct { connections *memstore.Store[connectionData] apiDestinations *memstore.Store[driver.APIDestination] + endpoints *memstore.Store[driver.Endpoint] + // endpointSettle holds each global endpoint's CREATING/UPDATING window. + endpointSettle *settle.Set // secrets holds each connection's credentials; nil leaves SecretArn a bare // identifier. secrets ConnectionSecretStore @@ -168,6 +172,8 @@ func New(opts *config.Options) *Mock { connections: memstore.New[connectionData](), apiDestinations: memstore.New[driver.APIDestination](), + endpoints: memstore.New[driver.Endpoint](), + endpointSettle: settle.NewSet(), httpClient: defaultHTTPClient(), apiDestSlots: make(chan struct{}, maxAPIDestinationDeliveries), } diff --git a/providers/aws/eventbridge/snapshot.go b/providers/aws/eventbridge/snapshot.go index f252927ea..ac05141f5 100644 --- a/providers/aws/eventbridge/snapshot.go +++ b/providers/aws/eventbridge/snapshot.go @@ -28,6 +28,7 @@ type ebSnapshot struct { Connections json.RawMessage `json:"connections,omitempty"` APIDestinations json.RawMessage `json:"apiDestinations,omitempty"` + Endpoints json.RawMessage `json:"endpoints,omitempty"` } // archiveSnapshot mirrors archiveData, promoting its unexported fields. @@ -182,6 +183,7 @@ func (m *Mock) valueStores(snap *ebSnapshot) []snapshottedStore { "api destinations", m.apiDestinations.Len, m.apiDestinations.Snapshot, m.apiDestinations.LoadSnapshot, &snap.APIDestinations, }, + {"endpoints", m.endpoints.Len, m.endpoints.Snapshot, m.endpoints.LoadSnapshot, &snap.Endpoints}, } } diff --git a/providers/aws/eventbridge/validation.go b/providers/aws/eventbridge/validation.go index 26530ae58..ed3069554 100644 --- a/providers/aws/eventbridge/validation.go +++ b/providers/aws/eventbridge/validation.go @@ -114,6 +114,28 @@ func (v *violations) pattern(value, field string, re *regexp.Regexp, patternText } } +// present records a required member the request left out, reporting whether +// value is set. +func (v *violations) present(value, field string) bool { + if value != "" { + return true + } + + *v = append(*v, fmt.Sprintf("Value null at '%s' failed to satisfy constraint: Member must not be null", field)) + + return false +} + +// lengthBetween checks that a set value is minLen to maxLen bytes long. +func (v *violations) lengthBetween(value, field string, minLen, maxLen int) { + switch { + case len(value) < minLen: + v.add(value, field, fmt.Sprintf("Member must have length greater than or equal to %d", minLen)) + case len(value) > maxLen: + v.add(value, field, fmt.Sprintf("Member must have length less than or equal to %d", maxLen)) + } +} + func (v *violations) eventsARN(value, field string) { if !eventsARNRE.MatchString(value) { v.add(value, field, "Member must satisfy regular expression pattern: "+eventsARNPattern) diff --git a/server/aws/eventbridge/endpoints.go b/server/aws/eventbridge/endpoints.go new file mode 100644 index 000000000..8b74f9846 --- /dev/null +++ b/server/aws/eventbridge/endpoints.go @@ -0,0 +1,294 @@ +package eventbridge + +import ( + "context" + "net/http" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/server/wire" + ebdriver "github.com/stackshy/cloudemu/v2/services/eventbus/driver" +) + +// endpointManager is the AWS-specific EventBridge global endpoint surface, +// asserted against the provider (not part of the portable EventBus driver). +type endpointManager interface { + CreateEndpoint(ctx context.Context, cfg *ebdriver.EndpointConfig) (*ebdriver.Endpoint, error) + DescribeEndpoint(ctx context.Context, name, homeRegion string) (*ebdriver.Endpoint, error) + UpdateEndpoint(ctx context.Context, upd *ebdriver.EndpointUpdate) (*ebdriver.Endpoint, error) + DeleteEndpoint(ctx context.Context, name string) error + ListEndpoints(ctx context.Context, filter ebdriver.EndpointFilter) ([]ebdriver.Endpoint, string, error) +} + +type endpointOp func(mgr endpointManager, w http.ResponseWriter, r *http.Request) + +func endpointOps() map[string]endpointOp { + return map[string]endpointOp{ + "CreateEndpoint": createEndpoint, + "DescribeEndpoint": describeEndpoint, + "UpdateEndpoint": updateEndpoint, + "DeleteEndpoint": deleteEndpoint, + "ListEndpoints": listEndpoints, + } +} + +// serveEndpointOp serves op when it is a global endpoint operation, reporting +// whether it did. +func (h *Handler) serveEndpointOp(w http.ResponseWriter, r *http.Request, op string) bool { + fn, ok := endpointOps()[op] + if !ok { + return false + } + + mgr, ok := h.bus.(endpointManager) + if !ok { + writeErr(w, cerrors.New(cerrors.Unimplemented, "global endpoints not supported")) + return true + } + + fn(mgr, w, r) + + return true +} + +type primaryJSON struct { + HealthCheck string `json:"HealthCheck,omitempty"` +} + +type secondaryJSON struct { + Route string `json:"Route,omitempty"` +} + +type failoverJSON struct { + Primary *primaryJSON `json:"Primary,omitempty"` + Secondary *secondaryJSON `json:"Secondary,omitempty"` +} + +type routingConfigJSON struct { + FailoverConfig *failoverJSON `json:"FailoverConfig,omitempty"` +} + +type replicationConfigJSON struct { + State string `json:"State,omitempty"` +} + +type endpointBusJSON struct { + EventBusArn string `json:"EventBusArn"` +} + +type endpointRequest struct { + Name string `json:"Name"` + Description *string `json:"Description"` + RoutingConfig *routingConfigJSON `json:"RoutingConfig"` + ReplicationConfig *replicationConfigJSON `json:"ReplicationConfig"` + EventBuses []endpointBusJSON `json:"EventBuses"` + RoleArn *string `json:"RoleArn"` + HomeRegion string `json:"HomeRegion"` +} + +type listEndpointsRequest struct { + NamePrefix string `json:"NamePrefix"` + HomeRegion string `json:"HomeRegion"` + MaxResults int `json:"MaxResults"` + NextToken string `json:"NextToken"` +} + +type endpointJSON struct { + Name string `json:"Name"` + Description string `json:"Description,omitempty"` + Arn string `json:"Arn"` + RoutingConfig *routingConfigJSON `json:"RoutingConfig"` + ReplicationConfig *replicationConfigJSON `json:"ReplicationConfig"` + EventBuses []endpointBusJSON `json:"EventBuses"` + RoleArn string `json:"RoleArn,omitempty"` + EndpointID string `json:"EndpointId,omitempty"` + EndpointURL string `json:"EndpointUrl,omitempty"` + State string `json:"State"` + StateReason string `json:"StateReason,omitempty"` + CreationTime *float64 `json:"CreationTime,omitempty"` + LastModifiedTime *float64 `json:"LastModifiedTime,omitempty"` +} + +type listEndpointsResponse struct { + Endpoints []endpointJSON `json:"Endpoints"` + NextToken string `json:"NextToken,omitempty"` +} + +func deref(s *string) string { + if s == nil { + return "" + } + + return *s +} + +// toRouting converts a wire RoutingConfig; nil stays nil (omitted). +func toRouting(rc *routingConfigJSON) *ebdriver.EndpointRouting { + if rc == nil { + return nil + } + + out := &ebdriver.EndpointRouting{} + + if fc := rc.FailoverConfig; fc != nil { + if fc.Primary != nil { + out.HealthCheck = fc.Primary.HealthCheck + } + + if fc.Secondary != nil { + out.SecondaryRoute = fc.Secondary.Route + } + } + + return out +} + +func toBusARNs(buses []endpointBusJSON) []string { + if buses == nil { + return nil + } + + out := make([]string, len(buses)) + for i, b := range buses { + out[i] = b.EventBusArn + } + + return out +} + +func (req *endpointRequest) replicationState() *string { + if req.ReplicationConfig == nil { + return nil + } + + return &req.ReplicationConfig.State +} + +// toEndpointJSON renders an endpoint in full, as DescribeEndpoint and +// ListEndpoints return it. +func toEndpointJSON(e *ebdriver.Endpoint) endpointJSON { + buses := make([]endpointBusJSON, len(e.EventBusARNs)) + for i, arn := range e.EventBusARNs { + buses[i] = endpointBusJSON{EventBusArn: arn} + } + + return endpointJSON{ + Name: e.Name, + Description: e.Description, + Arn: e.ARN, + RoutingConfig: &routingConfigJSON{FailoverConfig: &failoverJSON{ + Primary: &primaryJSON{HealthCheck: e.Routing.HealthCheck}, + Secondary: &secondaryJSON{Route: e.Routing.SecondaryRoute}, + }}, + ReplicationConfig: &replicationConfigJSON{State: e.ReplicationState}, + EventBuses: buses, + RoleArn: e.RoleARN, + EndpointID: e.EndpointID, + EndpointURL: e.EndpointURL, + State: e.State, + StateReason: e.StateReason, + CreationTime: timeToEpoch(e.CreatedAt), + LastModifiedTime: timeToEpoch(e.LastModifiedAt), + } +} + +func createEndpoint(mgr endpointManager, w http.ResponseWriter, r *http.Request) { + var req endpointRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + cfg := &ebdriver.EndpointConfig{ + Name: req.Name, Description: deref(req.Description), ReplicationState: deref(req.replicationState()), + EventBusARNs: toBusARNs(req.EventBuses), RoleARN: deref(req.RoleArn), + } + + if rc := toRouting(req.RoutingConfig); rc != nil { + cfg.Routing = *rc + } + + e, err := mgr.CreateEndpoint(r.Context(), cfg) + if err != nil { + writeErr(w, err) + return + } + + // CreateEndpoint answers without the description, id, URL and times. + out := toEndpointJSON(e) + out.Description, out.EndpointID, out.EndpointURL = "", "", "" + out.StateReason, out.CreationTime, out.LastModifiedTime = "", nil, nil + + wire.WriteJSON(w, out) +} + +func describeEndpoint(mgr endpointManager, w http.ResponseWriter, r *http.Request) { + var req endpointRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + e, err := mgr.DescribeEndpoint(r.Context(), req.Name, req.HomeRegion) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, toEndpointJSON(e)) +} + +func updateEndpoint(mgr endpointManager, w http.ResponseWriter, r *http.Request) { + var req endpointRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + e, err := mgr.UpdateEndpoint(r.Context(), &ebdriver.EndpointUpdate{ + Name: req.Name, Description: req.Description, Routing: toRouting(req.RoutingConfig), + ReplicationState: req.replicationState(), EventBusARNs: toBusARNs(req.EventBuses), RoleARN: req.RoleArn, + }) + if err != nil { + writeErr(w, err) + return + } + + // UpdateEndpoint answers without the description, state reason and times. + out := toEndpointJSON(e) + out.Description, out.StateReason, out.CreationTime, out.LastModifiedTime = "", "", nil, nil + + wire.WriteJSON(w, out) +} + +func deleteEndpoint(mgr endpointManager, w http.ResponseWriter, r *http.Request) { + var req endpointRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + if err := mgr.DeleteEndpoint(r.Context(), req.Name); err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, struct{}{}) +} + +func listEndpoints(mgr endpointManager, w http.ResponseWriter, r *http.Request) { + var req listEndpointsRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + items, next, err := mgr.ListEndpoints(r.Context(), ebdriver.EndpointFilter{ + NamePrefix: req.NamePrefix, HomeRegion: req.HomeRegion, MaxResults: req.MaxResults, NextToken: req.NextToken, + }) + if err != nil { + writeErr(w, err) + return + } + + out := listEndpointsResponse{Endpoints: make([]endpointJSON, 0, len(items)), NextToken: next} + for i := range items { + out.Endpoints = append(out.Endpoints, toEndpointJSON(&items[i])) + } + + wire.WriteJSON(w, out) +} diff --git a/server/aws/eventbridge/endpoints_sdk_test.go b/server/aws/eventbridge/endpoints_sdk_test.go new file mode 100644 index 000000000..12128afa4 --- /dev/null +++ b/server/aws/eventbridge/endpoints_sdk_test.go @@ -0,0 +1,242 @@ +package eventbridge_test + +import ( + "context" + "net/http" + "net/http/httptest" + "strings" + "sync" + "testing" + + "github.com/aws/aws-sdk-go-v2/aws" + awsconfig "github.com/aws/aws-sdk-go-v2/config" + "github.com/aws/aws-sdk-go-v2/credentials" + awseb "github.com/aws/aws-sdk-go-v2/service/eventbridge" + ebtypes "github.com/aws/aws-sdk-go-v2/service/eventbridge/types" + + "github.com/stackshy/cloudemu/v2" + awsserver "github.com/stackshy/cloudemu/v2/server/aws" +) + +const ( + sdkHealthCheck = "arn:aws:route53:::healthcheck/hc-1" + sdkWestBusARN = "arn:aws:events:us-west-2:123456789012:event-bus/" +) + +// authRecorder keeps the Authorization header of every PutEvents request. +type authRecorder struct { + mu sync.Mutex + auths []string + next http.Handler +} + +func (a *authRecorder) ServeHTTP(w http.ResponseWriter, r *http.Request) { + if r.Header.Get("X-Amz-Target") == "AWSEvents.PutEvents" { + a.mu.Lock() + a.auths = append(a.auths, r.Header.Get("Authorization")) + a.mu.Unlock() + } + + a.next.ServeHTTP(w, r) +} + +func newEndpointClient(t *testing.T) (*awseb.Client, *authRecorder) { + t.Helper() + + cloud := cloudemu.NewAWS() + rec := &authRecorder{next: awsserver.New(awsserver.Drivers{EventBridge: cloud.EventBridge})} + + ts := httptest.NewServer(rec) + t.Cleanup(ts.Close) + + cfg, err := awsconfig.LoadDefaultConfig(context.Background(), + awsconfig.WithRegion("us-east-1"), + awsconfig.WithCredentialsProvider(credentials.NewStaticCredentialsProvider("test", "test", "")), + ) + if err != nil { + t.Fatalf("aws config: %v", err) + } + + return awseb.NewFromConfig(cfg, func(o *awseb.Options) { o.BaseEndpoint = aws.String(ts.URL) }), rec +} + +func endpointInput(name string) *awseb.CreateEndpointInput { + return &awseb.CreateEndpointInput{ + Name: aws.String(name), + RoutingConfig: &ebtypes.RoutingConfig{FailoverConfig: &ebtypes.FailoverConfig{ + Primary: &ebtypes.Primary{HealthCheck: aws.String(sdkHealthCheck)}, + Secondary: &ebtypes.Secondary{Route: aws.String("us-west-2")}, + }}, + ReplicationConfig: &ebtypes.ReplicationConfig{State: ebtypes.ReplicationStateDisabled}, + EventBuses: []ebtypes.EndpointEventBus{ + {EventBusArn: aws.String(sdkBusARN + "b1")}, {EventBusArn: aws.String(sdkWestBusARN + "b1")}, + }, + } +} + +// TestSDKEndpointLifecycle drives global endpoint CRUD through the real SDK. +func TestSDKEndpointLifecycle(t *testing.T) { + eb, _ := newEndpointClient(t) + ctx := context.Background() + + created, err := eb.CreateEndpoint(ctx, endpointInput("ep1")) + if err != nil { + t.Fatalf("CreateEndpoint: %v", err) + } + + if created.State != ebtypes.EndpointStateCreating || aws.ToString(created.Arn) == "" || + len(created.EventBuses) != 2 || created.ReplicationConfig.State != ebtypes.ReplicationStateDisabled { + t.Fatalf("CreateEndpoint = %+v", created) + } + + desc, err := eb.DescribeEndpoint(ctx, &awseb.DescribeEndpointInput{Name: aws.String("ep1")}) + if err != nil { + t.Fatalf("DescribeEndpoint: %v", err) + } + + id := aws.ToString(desc.EndpointId) + if desc.State != ebtypes.EndpointStateActive || aws.ToString(desc.EndpointUrl) != "https://"+id+".endpoint.events.amazonaws.com" || + aws.ToString(desc.RoutingConfig.FailoverConfig.Primary.HealthCheck) != sdkHealthCheck || desc.CreationTime == nil { + t.Fatalf("DescribeEndpoint = %+v", desc) + } + + upd, err := eb.UpdateEndpoint(ctx, &awseb.UpdateEndpointInput{ + Name: aws.String("ep1"), Description: aws.String("d"), + ReplicationConfig: &ebtypes.ReplicationConfig{State: ebtypes.ReplicationStateEnabled}, + RoleArn: aws.String("arn:aws:iam::123456789012:role/r"), + }) + if err != nil { + t.Fatalf("UpdateEndpoint: %v", err) + } + + if upd.State != ebtypes.EndpointStateUpdating || aws.ToString(upd.EndpointId) != id { + t.Fatalf("UpdateEndpoint = %+v", upd) + } + + list, err := eb.ListEndpoints(ctx, &awseb.ListEndpointsInput{NamePrefix: aws.String("ep"), HomeRegion: aws.String("us-east-1")}) + if err != nil { + t.Fatalf("ListEndpoints: %v", err) + } + + if len(list.Endpoints) != 1 || aws.ToString(list.Endpoints[0].Description) != "d" || + list.Endpoints[0].State != ebtypes.EndpointStateActive { + t.Fatalf("ListEndpoints = %+v", list.Endpoints) + } + + if _, err := eb.DeleteEndpoint(ctx, &awseb.DeleteEndpointInput{Name: aws.String("ep1")}); err != nil { + t.Fatalf("DeleteEndpoint: %v", err) + } + + _, err = eb.DescribeEndpoint(ctx, &awseb.DescribeEndpointInput{Name: aws.String("ep1")}) + requireAPIError(t, err, "ResourceNotFoundException", "Endpoint ep1 does not exist.") +} + +func TestSDKEndpointErrors(t *testing.T) { + eb, _ := newEndpointClient(t) + ctx := context.Background() + + if _, err := eb.CreateEndpoint(ctx, endpointInput("ep")); err != nil { + t.Fatalf("CreateEndpoint: %v", err) + } + + _, err := eb.CreateEndpoint(ctx, endpointInput("ep")) + requireAPIError(t, err, "ResourceAlreadyExistsException", "Endpoint ep already exists.") + + in := endpointInput("ep2") + in.ReplicationConfig = nil + _, err = eb.CreateEndpoint(ctx, in) + requireAPIError(t, err, "ValidationException", "") + + in = endpointInput("ep3") + in.EventBuses[1].EventBusArn = aws.String(sdkWestBusARN + "other") + _, err = eb.CreateEndpoint(ctx, in) + requireAPIError(t, err, "ValidationException", "") + + _, err = eb.UpdateEndpoint(ctx, &awseb.UpdateEndpointInput{Name: aws.String("missing")}) + requireAPIError(t, err, "ResourceNotFoundException", "") + + _, err = eb.DeleteEndpoint(ctx, &awseb.DeleteEndpointInput{Name: aws.String("missing")}) + requireAPIError(t, err, "ResourceNotFoundException", "") +} + +func TestSDKListEndpointsPaging(t *testing.T) { + eb, _ := newEndpointClient(t) + ctx := context.Background() + + for _, n := range []string{"e1", "e2", "e3"} { + if _, err := eb.CreateEndpoint(ctx, endpointInput(n)); err != nil { + t.Fatalf("CreateEndpoint: %v", err) + } + } + + var names []string + + in := &awseb.ListEndpointsInput{MaxResults: aws.Int32(2)} + + for pages := 0; ; pages++ { + page, err := eb.ListEndpoints(ctx, in) + if err != nil { + t.Fatalf("ListEndpoints: %v", err) + } + + for _, e := range page.Endpoints { + names = append(names, aws.ToString(e.Name)) + } + + if page.NextToken == nil || pages > 3 { + break + } + + in.NextToken = page.NextToken + } + + if strings.Join(names, ",") != "e1,e2,e3" { + t.Fatalf("paged names = %v", names) + } +} + +// TestSDKPutEventsWithEndpointID proves the SDK still reaches a custom +// BaseEndpoint when EndpointId is set (it signs with SigV4a), and that the +// events land on the local primary bus. +func TestSDKPutEventsWithEndpointID(t *testing.T) { + eb, rec := newEndpointClient(t) + ctx := context.Background() + + if _, err := eb.CreateEventBus(ctx, &awseb.CreateEventBusInput{Name: aws.String("b1")}); err != nil { + t.Fatalf("CreateEventBus: %v", err) + } + + if _, err := eb.CreateEndpoint(ctx, endpointInput("ep")); err != nil { + t.Fatalf("CreateEndpoint: %v", err) + } + + desc, err := eb.DescribeEndpoint(ctx, &awseb.DescribeEndpointInput{Name: aws.String("ep")}) + if err != nil { + t.Fatalf("DescribeEndpoint: %v", err) + } + + out, err := eb.PutEvents(ctx, &awseb.PutEventsInput{ + EndpointId: desc.EndpointId, + Entries: []ebtypes.PutEventsRequestEntry{{ + Source: aws.String("app"), DetailType: aws.String("t"), Detail: aws.String("{}"), + EventBusName: aws.String(sdkWestBusARN + "b1"), + }}, + }) + if err != nil { + t.Fatalf("PutEvents: %v", err) + } + + if out.FailedEntryCount != 0 || aws.ToString(out.Entries[0].EventId) == "" { + t.Fatalf("PutEvents = %+v", out.Entries) + } + + if len(rec.auths) != 1 || !strings.HasPrefix(rec.auths[0], "AWS4-ECDSA-P256-SHA256") { + t.Fatalf("PutEvents Authorization = %v, want a SigV4a signature", rec.auths) + } + + _, err = eb.PutEvents(ctx, &awseb.PutEventsInput{ + EndpointId: aws.String("missing.abc"), + Entries: []ebtypes.PutEventsRequestEntry{{Source: aws.String("app"), DetailType: aws.String("t"), Detail: aws.String("{}")}}, + }) + requireAPIError(t, err, "ValidationException", "Parameter EndpointId is not valid. Reason: Endpoint missing.abc does not exist.") +} diff --git a/server/aws/eventbridge/handler.go b/server/aws/eventbridge/handler.go index 3c8527bd6..b77b6b93a 100644 --- a/server/aws/eventbridge/handler.go +++ b/server/aws/eventbridge/handler.go @@ -93,10 +93,10 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } // serveExtendedOp serves the operations outside the main switch (archives, -// replays, connections and API destinations) and answers +// replays, connections, API destinations and global endpoints) and answers // UnknownOperationException for anything else. func (h *Handler) serveExtendedOp(w http.ResponseWriter, r *http.Request, op string) { - if h.serveArchiveOp(w, r, op) || h.serveConnectionOp(w, r, op) { + if h.serveArchiveOp(w, r, op) || h.serveConnectionOp(w, r, op) || h.serveEndpointOp(w, r, op) { return } diff --git a/server/aws/eventbridge/operations.go b/server/aws/eventbridge/operations.go index 213104b0e..4f65c69de 100644 --- a/server/aws/eventbridge/operations.go +++ b/server/aws/eventbridge/operations.go @@ -20,6 +20,12 @@ type entryPublisher interface { PutEventEntries(ctx context.Context, events []ebdriver.Event) ([]ebdriver.PutEventsEntryResult, error) } +// endpointPublisher is the AWS-specific PutEvents surface for a request sent +// through a global endpoint (EndpointId set). +type endpointPublisher interface { + PutEndpointEventEntries(ctx context.Context, endpointID string, events []ebdriver.Event) ([]ebdriver.PutEventsEntryResult, error) +} + // --- event buses --- func (h *Handler) createEventBus(w http.ResponseWriter, r *http.Request) { @@ -327,21 +333,7 @@ func (h *Handler) putEvents(w http.ResponseWriter, r *http.Request) { return } - events := make([]ebdriver.Event, len(req.Entries)) - - for i := range req.Entries { - e := &req.Entries[i] - events[i] = ebdriver.Event{ - Source: e.Source, - DetailType: e.DetailType, - Detail: e.Detail, - EventBus: busNameOrDefault(e.EventBusName), - Resources: e.Resources, - Time: entryTime(e.Time), - } - } - - results, err := publisher.PutEventEntries(r.Context(), events) + results, err := h.publishEntries(r.Context(), publisher, &req) if err != nil { writeErr(w, err) return @@ -358,6 +350,41 @@ func (h *Handler) putEvents(w http.ResponseWriter, r *http.Request) { wire.WriteJSON(w, resp) } +// publishEntries hands the request's entries to the provider. Through a global +// endpoint the entries keep their EventBusName as sent, since the endpoint +// decides where an entry without one goes. +func (h *Handler) publishEntries( + ctx context.Context, publisher entryPublisher, req *putEventsRequest, +) ([]ebdriver.PutEventsEntryResult, error) { + viaEndpoint := req.EndpointID != "" + events := make([]ebdriver.Event, len(req.Entries)) + + for i := range req.Entries { + e := &req.Entries[i] + + bus := e.EventBusName + if !viaEndpoint { + bus = busNameOrDefault(bus) + } + + events[i] = ebdriver.Event{ + Source: e.Source, DetailType: e.DetailType, Detail: e.Detail, + EventBus: bus, Resources: e.Resources, Time: entryTime(e.Time), + } + } + + if !viaEndpoint { + return publisher.PutEventEntries(ctx, events) + } + + ep, ok := h.bus.(endpointPublisher) + if !ok { + return nil, cerrors.New(cerrors.Unimplemented, "global endpoints not supported") + } + + return ep.PutEndpointEventEntries(ctx, req.EndpointID, events) +} + // testEventPattern evaluates a sample event against an event pattern using the // same matcher PutEvents delivery uses, so "would this rule fire?" answered here // is consistent with what actually gets delivered. Real users / the console call diff --git a/server/aws/eventbridge/types.go b/server/aws/eventbridge/types.go index cb9fa1c79..f23b0309d 100644 --- a/server/aws/eventbridge/types.go +++ b/server/aws/eventbridge/types.go @@ -103,6 +103,8 @@ type putEventsEntry struct { type putEventsRequest struct { Entries []putEventsEntry `json:"Entries"` + // EndpointID names the global endpoint the request was sent through. + EndpointID string `json:"EndpointId"` } type testEventPatternRequest struct { diff --git a/services/eventbus/driver/endpoint.go b/services/eventbus/driver/endpoint.go new file mode 100644 index 000000000..997cad905 --- /dev/null +++ b/services/eventbus/driver/endpoint.go @@ -0,0 +1,66 @@ +package driver + +import "time" + +// EndpointRouting is an EventBridge global endpoint's failover configuration: +// the Route 53 health check that decides when to fail over, and the secondary +// Region events go to when it does. +type EndpointRouting struct { + HealthCheck string + SecondaryRoute string +} + +// Endpoint describes an EventBridge global endpoint: one event bus name present +// in two Regions, with failover between them. Global endpoints are an AWS-only +// concept, consumed through an optional interface, not the portable driver. +type Endpoint struct { + Name string + Description string + ARN string + Routing EndpointRouting + // ReplicationState is ENABLED or DISABLED. + ReplicationState string + // EventBusARNs holds the two event bus ARNs, one per Region. + EventBusARNs []string + RoleARN string + // EndpointID is the URL subdomain, such as "abcde.veo". + EndpointID string + EndpointURL string + // HomeRegion is the primary Region: the one the endpoint was created in. + HomeRegion string + State string + StateReason string + CreatedAt time.Time + LastModifiedAt time.Time +} + +// EndpointConfig carries the parameters of an EventBridge CreateEndpoint call. +// An empty ReplicationState means the request left it out (ENABLED). +type EndpointConfig struct { + Name string + Description string + Routing EndpointRouting + ReplicationState string + EventBusARNs []string + RoleARN string +} + +// EndpointUpdate carries the parameters of an EventBridge UpdateEndpoint call. A +// nil field was omitted from the request and leaves the endpoint unchanged. +type EndpointUpdate struct { + Name string + Description *string + Routing *EndpointRouting + ReplicationState *string + EventBusARNs []string + RoleARN *string +} + +// EndpointFilter narrows and pages ListEndpoints. MaxResults 0 means the default +// page size. +type EndpointFilter struct { + NamePrefix string + HomeRegion string + MaxResults int + NextToken string +} From 1867b0927eef7bdb62499ba9a38fa074d734adce Mon Sep 17 00:00:00 2001 From: Nitin Kumar Patel Date: Fri, 23 Oct 2026 07:10:03 +0530 Subject: [PATCH 14/31] fix(aws-cloudwatchlogs): export tasks to S3 (#1686) --- docs/coverage/aws/cloudwatchlogs.md | 7 +- docs/coverage/azure/loganalytics.md | 4 +- docs/coverage/coverage.json | 10 + docs/coverage/gcp/cloudlogging.md | 4 +- docs/coverage/nongoals/logging.md | 3 + docs/coverage/oci/logging.md | 4 +- internal/settle/settle.go | 3 + providers/aws/aws.go | 2 + .../aws/cloudwatchlogs/cloudwatchlogs.go | 8 + .../aws/cloudwatchlogs/export_destination.go | 252 +++++++++++ providers/aws/cloudwatchlogs/export_tasks.go | 410 ++++++++++++++++++ .../aws/cloudwatchlogs/export_tasks_test.go | 391 +++++++++++++++++ providers/aws/cloudwatchlogs/snapshot.go | 16 +- server/aws/cloudwatchlogs/export_tasks.go | 177 ++++++++ .../cloudwatchlogs/export_tasks_sdk_test.go | 211 +++++++++ server/aws/cloudwatchlogs/handler.go | 7 +- services/logging/driver/aws.go | 47 ++ 17 files changed, 1548 insertions(+), 8 deletions(-) create mode 100644 docs/coverage/nongoals/logging.md create mode 100644 providers/aws/cloudwatchlogs/export_destination.go create mode 100644 providers/aws/cloudwatchlogs/export_tasks.go create mode 100644 providers/aws/cloudwatchlogs/export_tasks_test.go create mode 100644 server/aws/cloudwatchlogs/export_tasks.go create mode 100644 server/aws/cloudwatchlogs/export_tasks_sdk_test.go diff --git a/docs/coverage/aws/cloudwatchlogs.md b/docs/coverage/aws/cloudwatchlogs.md index 0516d82ca..0920fafe4 100644 --- a/docs/coverage/aws/cloudwatchlogs.md +++ b/docs/coverage/aws/cloudwatchlogs.md @@ -36,9 +36,12 @@ AWSLogging is an optional interface implemented by the AWS CloudWatch Logs | Operation | Description | | --- | --- | | `AssociateKmsKey` | AssociateKmsKey and DisassociateKmsKey target exactly one of a log group | +| `CancelExportTask` | | | `CreateAWSLogGroup` | | +| `CreateExportTask` | Export tasks write a log group's events to an S3 bucket. A task moves | | `DeleteRetentionPolicy` | | | `DescribeAWSMetricFilters` | DescribeAWSMetricFilters lists metric filters, sorted by filter name. | +| `DescribeExportTasks` | | | `DescribeQueries` | | | `DisassociateKmsKey` | | | `GetLogGroupFields` | GetLogGroupFields samples the 8 minutes either side of at, or the last | @@ -53,4 +56,6 @@ AWSLogging is an optional interface implemented by the AWS CloudWatch Logs ## Not in scope -_Not documented yet. See the [emulator boundary](../../../README.md) for cloudemu-wide non-goals._ +- AWS: `CreateExportTask` checks the destination bucket's policy the way CloudWatch Logs does before it accepts a task. The policy must allow the `logs.amazonaws.com` or `logs..amazonaws.com` service principal (or `*`) to call `s3:GetBucketAcl` on the bucket and `s3:PutObject` under the prefix, and a matching `Deny` wins. Policy conditions (`aws:SourceAccount`, `aws:SourceArn`, `s3:x-amz-acl`), `NotPrincipal`/`NotAction`/`NotResource`, the Region named in the principal, SSE-KMS key policies and cross-account buckets are not evaluated. +- AWS: the destination bucket's Region is not compared with the log group's Region, so an export to a bucket in another Region is accepted. +- AWS: an export task writes each log stream to a single `000000.gz` object, where CloudWatch Logs splits large streams across `000000.gz`, `000001.gz`, and so on. The events are written when the task settles to `COMPLETED` and an export-task API call observes it: at once without `--async-settle`, or on the first `DescribeExportTasks` after the settle window with it. `PENDING_CANCEL` is never reported; `CancelExportTask` moves a task straight to `CANCELLED`. diff --git a/docs/coverage/azure/loganalytics.md b/docs/coverage/azure/loganalytics.md index b20419e89..b575a8046 100644 --- a/docs/coverage/azure/loganalytics.md +++ b/docs/coverage/azure/loganalytics.md @@ -27,4 +27,6 @@ Azure's `logging` service ยท portable interface `driver.Logging` ยท [Azure index ## Not in scope -_Not documented yet. See the [emulator boundary](../../../README.md) for cloudemu-wide non-goals._ +- AWS: `CreateExportTask` checks the destination bucket's policy the way CloudWatch Logs does before it accepts a task. The policy must allow the `logs.amazonaws.com` or `logs..amazonaws.com` service principal (or `*`) to call `s3:GetBucketAcl` on the bucket and `s3:PutObject` under the prefix, and a matching `Deny` wins. Policy conditions (`aws:SourceAccount`, `aws:SourceArn`, `s3:x-amz-acl`), `NotPrincipal`/`NotAction`/`NotResource`, the Region named in the principal, SSE-KMS key policies and cross-account buckets are not evaluated. +- AWS: the destination bucket's Region is not compared with the log group's Region, so an export to a bucket in another Region is accepted. +- AWS: an export task writes each log stream to a single `000000.gz` object, where CloudWatch Logs splits large streams across `000000.gz`, `000001.gz`, and so on. The events are written when the task settles to `COMPLETED` and an export-task API call observes it: at once without `--async-settle`, or on the first `DescribeExportTasks` after the settle window with it. `PENDING_CANCEL` is never reported; `CancelExportTask` moves a task straight to `CANCELLED`. diff --git a/docs/coverage/coverage.json b/docs/coverage/coverage.json index c4b9f15f7..f49217dc0 100644 --- a/docs/coverage/coverage.json +++ b/docs/coverage/coverage.json @@ -11566,9 +11566,16 @@ "name": "AssociateKmsKey", "doc": "AssociateKmsKey and DisassociateKmsKey target exactly one of a log group" }, + { + "name": "CancelExportTask" + }, { "name": "CreateAWSLogGroup" }, + { + "name": "CreateExportTask", + "doc": "Export tasks write a log group's events to an S3 bucket. A task moves" + }, { "name": "DeleteRetentionPolicy" }, @@ -11576,6 +11583,9 @@ "name": "DescribeAWSMetricFilters", "doc": "DescribeAWSMetricFilters lists metric filters, sorted by filter name." }, + { + "name": "DescribeExportTasks" + }, { "name": "DescribeQueries" }, diff --git a/docs/coverage/gcp/cloudlogging.md b/docs/coverage/gcp/cloudlogging.md index 60b91f1f5..cb3cffdf5 100644 --- a/docs/coverage/gcp/cloudlogging.md +++ b/docs/coverage/gcp/cloudlogging.md @@ -53,4 +53,6 @@ GCPLogging is an optional interface implemented by the GCP logging backend for ## Not in scope -_Not documented yet. See the [emulator boundary](../../../README.md) for cloudemu-wide non-goals._ +- AWS: `CreateExportTask` checks the destination bucket's policy the way CloudWatch Logs does before it accepts a task. The policy must allow the `logs.amazonaws.com` or `logs..amazonaws.com` service principal (or `*`) to call `s3:GetBucketAcl` on the bucket and `s3:PutObject` under the prefix, and a matching `Deny` wins. Policy conditions (`aws:SourceAccount`, `aws:SourceArn`, `s3:x-amz-acl`), `NotPrincipal`/`NotAction`/`NotResource`, the Region named in the principal, SSE-KMS key policies and cross-account buckets are not evaluated. +- AWS: the destination bucket's Region is not compared with the log group's Region, so an export to a bucket in another Region is accepted. +- AWS: an export task writes each log stream to a single `000000.gz` object, where CloudWatch Logs splits large streams across `000000.gz`, `000001.gz`, and so on. The events are written when the task settles to `COMPLETED` and an export-task API call observes it: at once without `--async-settle`, or on the first `DescribeExportTasks` after the settle window with it. `PENDING_CANCEL` is never reported; `CancelExportTask` moves a task straight to `CANCELLED`. diff --git a/docs/coverage/nongoals/logging.md b/docs/coverage/nongoals/logging.md new file mode 100644 index 000000000..de52ebf7b --- /dev/null +++ b/docs/coverage/nongoals/logging.md @@ -0,0 +1,3 @@ +- AWS: `CreateExportTask` checks the destination bucket's policy the way CloudWatch Logs does before it accepts a task. The policy must allow the `logs.amazonaws.com` or `logs..amazonaws.com` service principal (or `*`) to call `s3:GetBucketAcl` on the bucket and `s3:PutObject` under the prefix, and a matching `Deny` wins. Policy conditions (`aws:SourceAccount`, `aws:SourceArn`, `s3:x-amz-acl`), `NotPrincipal`/`NotAction`/`NotResource`, the Region named in the principal, SSE-KMS key policies and cross-account buckets are not evaluated. +- AWS: the destination bucket's Region is not compared with the log group's Region, so an export to a bucket in another Region is accepted. +- AWS: an export task writes each log stream to a single `000000.gz` object, where CloudWatch Logs splits large streams across `000000.gz`, `000001.gz`, and so on. The events are written when the task settles to `COMPLETED` and an export-task API call observes it: at once without `--async-settle`, or on the first `DescribeExportTasks` after the settle window with it. `PENDING_CANCEL` is never reported; `CancelExportTask` moves a task straight to `CANCELLED`. diff --git a/docs/coverage/oci/logging.md b/docs/coverage/oci/logging.md index 96b27dc04..fb903044b 100644 --- a/docs/coverage/oci/logging.md +++ b/docs/coverage/oci/logging.md @@ -27,4 +27,6 @@ OCI's `logging` service ยท portable interface `driver.Logging` ยท [OCI index](./ ## Not in scope -_Not documented yet. See the [emulator boundary](../../../README.md) for cloudemu-wide non-goals._ +- AWS: `CreateExportTask` checks the destination bucket's policy the way CloudWatch Logs does before it accepts a task. The policy must allow the `logs.amazonaws.com` or `logs..amazonaws.com` service principal (or `*`) to call `s3:GetBucketAcl` on the bucket and `s3:PutObject` under the prefix, and a matching `Deny` wins. Policy conditions (`aws:SourceAccount`, `aws:SourceArn`, `s3:x-amz-acl`), `NotPrincipal`/`NotAction`/`NotResource`, the Region named in the principal, SSE-KMS key policies and cross-account buckets are not evaluated. +- AWS: the destination bucket's Region is not compared with the log group's Region, so an export to a bucket in another Region is accepted. +- AWS: an export task writes each log stream to a single `000000.gz` object, where CloudWatch Logs splits large streams across `000000.gz`, `000001.gz`, and so on. The events are written when the task settles to `COMPLETED` and an export-task API call observes it: at once without `--async-settle`, or on the first `DescribeExportTasks` after the settle window with it. `PENDING_CANCEL` is never reported; `CancelExportTask` moves a task straight to `CANCELLED`. diff --git a/internal/settle/settle.go b/internal/settle/settle.go index acb4867da..6a1c6c9d2 100644 --- a/internal/settle/settle.go +++ b/internal/settle/settle.go @@ -76,6 +76,9 @@ const ( DefaultBedrockIngestionSettle = 2 * time.Second // Bedrock KB ingestion job IN_PROGRESS->COMPLETE + DefaultExportTaskPendingSettle = 1 * time.Second // CloudWatch Logs export task PENDING->RUNNING + DefaultExportTaskRunSettle = 2 * time.Second // CloudWatch Logs export task RUNNING->COMPLETED + // S3 RestoreObject ongoing-request="true"->"false", by retrieval tier. DefaultRestoreExpeditedSettle = 1 * time.Second DefaultRestoreStandardSettle = 3 * time.Second diff --git a/providers/aws/aws.go b/providers/aws/aws.go index 2faf11dd6..48a66f5a3 100644 --- a/providers/aws/aws.go +++ b/providers/aws/aws.go @@ -569,6 +569,8 @@ func wirePostBuildServices(o *config.Options, p *Provider) { // subscription filter's pattern are delivered (gzipped awslogs payload) to // the filter's Lambda destination on PutLogEvents. p.CloudWatchLogs.SetLambdaInvoker(p.Lambda) + // CloudWatch Logs export tasks write log events to an S3 bucket. + p.CloudWatchLogs.SetExportStore(p.S3) // CloudFormation is an orchestrator: it provisions each stack resource by // calling the matching service driver, so its registry is built from the // live mocks rather than a store of its own. diff --git a/providers/aws/cloudwatchlogs/cloudwatchlogs.go b/providers/aws/cloudwatchlogs/cloudwatchlogs.go index 8027ef3e7..016674947 100644 --- a/providers/aws/cloudwatchlogs/cloudwatchlogs.go +++ b/providers/aws/cloudwatchlogs/cloudwatchlogs.go @@ -60,6 +60,9 @@ type Mock struct { lambda LambdaInvoker keys KeyResolver queries *memstore.Store[queryRecord] + // exports holds export tasks; exportStore is the S3 they write to. + exports *memstore.Store[exportTask] + exportStore ExportStore // queryResultKmsKey is the account-wide key associated with Logs Insights // query results (AssociateKmsKey on the query-result resource). @@ -71,6 +74,10 @@ type Mock struct { // admitMu makes StartQuery's concurrent-query count and its insert one // step. Settling only moves queries out of Running, so it needs no lock. admitMu sync.Mutex + // exportMu makes the one-active-export-task check and the insert one + // step, and serializes claiming tasks for writing. It is never held + // across an S3 call. + exportMu sync.Mutex } // SetMonitoring sets the monitoring backend for auto-metric generation. @@ -100,6 +107,7 @@ func New(opts *config.Options) *Mock { return &Mock{ groups: memstore.New[*logGroup](), queries: memstore.New[queryRecord](), + exports: memstore.New[exportTask](), opts: opts, periods: map[string]filterPeriod{}, } diff --git a/providers/aws/cloudwatchlogs/export_destination.go b/providers/aws/cloudwatchlogs/export_destination.go new file mode 100644 index 000000000..c1646faa1 --- /dev/null +++ b/providers/aws/cloudwatchlogs/export_destination.go @@ -0,0 +1,252 @@ +package cloudwatchlogs + +import ( + "context" + "encoding/json" + "path" + "regexp" + "strings" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + storagedriver "github.com/stackshy/cloudemu/v2/services/storage/driver" +) + +// The messages CreateExportTask returns when the destination bucket can't be +// used. All are InvalidParameterException. +const ( + bucketMissingMsg = "The given bucket does not exist. Please make sure the bucket is valid." + grantCheckMsg = " call on the given bucket failed. " + + "Please check if CloudWatch Logs has been granted permission to perform this operation." + getBucketACLMsg = "GetBucketAcl" + grantCheckMsg + putObjectFailedMsg = "PutObject" + grantCheckMsg + " This may be a result of a KMS key or bucket encryption misconfiguration." + + // exportWriteTestKey and its body are the permission-check object + // CloudWatch Logs writes under the prefix when a task is created. + exportWriteTestKey = "aws-logs-write-test" + exportWriteTestBody = "Permission Check Successful" + + bucketPolicyConfig = "policy" + logsGlobalPrincipal = "logs.amazonaws.com" + logsServicePrefix = "logs." + amazonawsSuffix = ".amazonaws.com" +) + +// ExportStore is the part of S3 export tasks use: bucket existence, the +// bucket policy (the raw document the wire API stores, or the typed one the +// Go API stores) and object writes. The S3 mock satisfies it. +type ExportStore interface { + ListBuckets(ctx context.Context) ([]storagedriver.BucketInfo, error) + GetBucketConfig(ctx context.Context, bucket, name string) ([]byte, error) + GetBucketPolicy(ctx context.Context, bucket string) (*storagedriver.BucketPolicy, error) + PutObject(ctx context.Context, bucket, key string, data []byte, contentType string, metadata map[string]string) error +} + +// SetExportStore wires S3 in as the destination of export tasks. Without it +// every CreateExportTask fails as if the bucket did not exist. +func (m *Mock) SetExportStore(s ExportStore) { + m.exportStore = s +} + +// checkDestination does what CloudWatch Logs does before it accepts a task: +// the bucket must exist, its policy must let the logs service principal call +// GetBucketAcl on it and PutObject under the prefix, and the permission-check +// object must be written. Policy conditions are not evaluated. +func (m *Mock) checkDestination(ctx context.Context, bucket, prefix string) error { + if m.exportStore == nil || !m.bucketExists(ctx, bucket) { + return cerrors.New(cerrors.InvalidArgument, bucketMissingMsg) + } + + stmts := m.bucketPolicy(ctx, bucket) + bucketARN := "arn:aws:s3:::" + bucket + testKey := path.Join(prefix, exportWriteTestKey) + + if !logsAllowed(stmts, "s3:GetBucketAcl", bucketARN) { + return cerrors.New(cerrors.InvalidArgument, getBucketACLMsg) + } + + if !logsAllowed(stmts, "s3:PutObject", bucketARN+"/"+testKey) { + return cerrors.New(cerrors.InvalidArgument, putObjectFailedMsg) + } + + if err := m.exportStore.PutObject(ctx, bucket, testKey, []byte(exportWriteTestBody), "text/plain", nil); err != nil { + return cerrors.New(cerrors.InvalidArgument, putObjectFailedMsg) + } + + return nil +} + +func (m *Mock) bucketExists(ctx context.Context, bucket string) bool { + buckets, err := m.exportStore.ListBuckets(ctx) + if err != nil { + return false + } + + for i := range buckets { + if buckets[i].Name == bucket { + return true + } + } + + return false +} + +// policyStatement is the part of a bucket policy statement the check reads. +// Principal, Action and Resource are a string or a list in the document. +type policyStatement struct { + Effect string `json:"Effect"` + Principal json.RawMessage `json:"Principal"` + Action json.RawMessage `json:"Action"` + Resource json.RawMessage `json:"Resource"` + + principals, actions, resources []string +} + +// bucketPolicy returns the bucket's policy statements: the document stored by +// PutBucketPolicy over the wire, else the typed policy, else none. +func (m *Mock) bucketPolicy(ctx context.Context, bucket string) []policyStatement { + if raw, err := m.exportStore.GetBucketConfig(ctx, bucket, bucketPolicyConfig); err == nil { + var doc struct { + Statement json.RawMessage `json:"Statement"` + } + + if json.Unmarshal(raw, &doc) != nil { + return nil + } + + return decodeStatements(doc.Statement) + } + + p, err := m.exportStore.GetBucketPolicy(ctx, bucket) + if err != nil { + return nil + } + + out := make([]policyStatement, 0, len(p.Statements)) + for _, s := range p.Statements { + out = append(out, policyStatement{ + Effect: s.Effect, principals: []string{s.Principal}, actions: s.Actions, resources: s.Resources, + }) + } + + return out +} + +// decodeStatements accepts a single statement object or a list of them. +func decodeStatements(raw json.RawMessage) []policyStatement { + var list []policyStatement + if json.Unmarshal(raw, &list) != nil { + var one policyStatement + if json.Unmarshal(raw, &one) != nil { + return nil + } + + list = []policyStatement{one} + } + + for i := range list { + s := &list[i] + s.principals = principalValues(s.Principal) + s.actions = stringOrList(s.Action) + s.resources = stringOrList(s.Resource) + } + + return list +} + +// principalValues flattens "*" or {"Service": ..., "AWS": ...} into values. +func principalValues(raw json.RawMessage) []string { + if vals := stringOrList(raw); vals != nil { + return vals + } + + var byType map[string]json.RawMessage + if json.Unmarshal(raw, &byType) != nil { + return nil + } + + var out []string + for _, v := range byType { + out = append(out, stringOrList(v)...) + } + + return out +} + +func stringOrList(raw json.RawMessage) []string { + var one string + if json.Unmarshal(raw, &one) == nil { + return []string{one} + } + + var list []string + if json.Unmarshal(raw, &list) == nil { + return list + } + + return nil +} + +// logsAllowed reports whether the statements allow the CloudWatch Logs +// service principal action on resource, with an explicit Deny winning. +func logsAllowed(stmts []policyStatement, action, resource string) bool { + allowed := false + + for i := range stmts { + s := &stmts[i] + if !anyMatch(s.principals, isLogsPrincipal) || + !anyMatch(s.actions, func(a string) bool { return wildcardMatch(a, action, true) }) || + !anyMatch(s.resources, func(r string) bool { return wildcardMatch(r, resource, false) }) { + continue + } + + if strings.EqualFold(s.Effect, "Deny") { + return false + } + + if strings.EqualFold(s.Effect, "Allow") { + allowed = true + } + } + + return allowed +} + +func anyMatch(vals []string, match func(string) bool) bool { + for _, v := range vals { + if match(v) { + return true + } + } + + return false +} + +// isLogsPrincipal matches "*", logs.amazonaws.com and logs..amazonaws.com. +// The region is not checked against the log group's region. +func isLogsPrincipal(p string) bool { + if p == "*" || p == logsGlobalPrincipal { + return true + } + + rest, ok := strings.CutPrefix(p, logsServicePrefix) + if !ok { + return false + } + + region, ok := strings.CutSuffix(rest, amazonawsSuffix) + + return ok && region != "" && !strings.ContainsAny(region, ".*") +} + +// wildcardMatch matches an IAM pattern, where * is any run and ? any one +// character, across the whole value. +func wildcardMatch(pattern, value string, foldCase bool) bool { + expr := "^" + strings.ReplaceAll(strings.ReplaceAll(regexp.QuoteMeta(pattern), `\*`, ".*"), `\?`, ".") + "$" + if foldCase { + expr = "(?i)" + expr + } + + re, err := regexp.Compile(expr) + + return err == nil && re.MatchString(value) +} diff --git a/providers/aws/cloudwatchlogs/export_tasks.go b/providers/aws/cloudwatchlogs/export_tasks.go new file mode 100644 index 000000000..bb91285b8 --- /dev/null +++ b/providers/aws/cloudwatchlogs/export_tasks.go @@ -0,0 +1,410 @@ +package cloudwatchlogs + +import ( + "bytes" + "compress/gzip" + "context" + "path" + "sort" + "strings" + "time" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/internal/idgen" + "github.com/stackshy/cloudemu/v2/internal/settle" + "github.com/stackshy/cloudemu/v2/services/logging/driver" +) + +// Export task defaults and messages, per the CreateExportTask API reference +// and what DescribeExportTasks reports. +const ( + defaultExportPrefix = "exportedlogs" + // exportChunkName is the one object each exported stream gets. Real + // exports split large streams into 000000.gz, 000001.gz, ...; the + // emulator writes every event into the first chunk. + exportChunkName = "000000.gz" + exportLineTimeLayout = "2006-01-02T15:04:05.000Z" + exportContentType = "application/octet-stream" + + exportMsgPending = "Pending" + exportMsgRunning = "Started successfully" + exportMsgCompleted = "Completed successfully" + exportMsgCancelled = "Cancelled by user" //nolint:misspell // matches the service's wording. + exportStatusEnum = "CANCELLED, COMPLETED, FAILED, PENDING, PENDING_CANCEL, RUNNING" //nolint:misspell // the API enum. +) + +// exportTask is one export task. Status holds the stored outcome: COMPLETED +// until the task is canceled or fails. While its settle windows are open a +// COMPLETED task is observed as PENDING, then RUNNING. Written records that +// the task's objects were written (or that it never will write any). +type exportTask struct { + ID string `json:"id"` + Name string `json:"name,omitempty"` + LogGroup string `json:"logGroup"` + StreamPrefix string `json:"streamPrefix,omitempty"` + From time.Time `json:"from"` + To time.Time `json:"to"` + Destination string `json:"destination"` + Prefix string `json:"prefix"` + CreatedAt time.Time `json:"createdAt"` + PendingFor time.Duration `json:"pendingFor,omitempty"` + RunFor time.Duration `json:"runFor,omitempty"` + Status string `json:"status"` + Message string `json:"message,omitempty"` + CompletedAt time.Time `json:"completedAt,omitempty"` + Written bool `json:"written,omitempty"` +} + +// observedStatus is the status a reader sees at now. +func (t *exportTask) observedStatus(now time.Time) string { + if t.Status != driver.ExportTaskCompleted { + return t.Status + } + + running := settle.Pending(driver.ExportTaskRunning, t.CreatedAt, t.PendingFor+t.RunFor). + Observe(now, driver.ExportTaskCompleted) + + return settle.Pending(driver.ExportTaskPending, t.CreatedAt, t.PendingFor).Observe(now, running) +} + +func (t *exportTask) active(now time.Time) bool { + s := t.observedStatus(now) + + return s == driver.ExportTaskPending || s == driver.ExportTaskRunning +} + +func (t *exportTask) describe(now time.Time) driver.ExportTask { + out := driver.ExportTask{ + TaskID: t.ID, TaskName: t.Name, LogGroupName: t.LogGroup, From: t.From, To: t.To, + Destination: t.Destination, DestinationPrefix: t.Prefix, + StatusCode: t.observedStatus(now), CreationTime: t.CreatedAt, + } + + switch out.StatusCode { + case driver.ExportTaskPending: + out.StatusMessage = exportMsgPending + case driver.ExportTaskRunning: + out.StatusMessage = exportMsgRunning + case driver.ExportTaskCompleted: + out.StatusMessage = exportMsgCompleted + out.CompletionTime = t.CreatedAt.Add(t.PendingFor + t.RunFor) + default: + out.StatusMessage = t.Message + out.CompletionTime = t.CompletedAt + } + + return out +} + +// CreateExportTask validates the request and the destination bucket, then +// records the task. Like CloudWatch Logs it writes a permission-check object +// under the prefix before accepting the task. The events are written once the +// task settles to COMPLETED: straight away when async settling is off, +// otherwise on the first export-task call after it settles. +func (m *Mock) CreateExportTask(ctx context.Context, in *driver.CreateExportTaskInput) (string, error) { + if err := validateExportRequest(in); err != nil { + return "", err + } + + g, ok := m.groups.Get(in.LogGroupName) + if !ok { + return "", cerrors.New(cerrors.NotFound, "The specified log group does not exist.") + } + + if created, err := time.Parse(time.RFC3339, g.info.CreatedAt); err == nil && in.To.Before(created) { + return "", cerrors.New(cerrors.InvalidArgument, + "Specified time range of '"+in.To.Format(time.RFC3339)+"' is before the log group creation time.") + } + + prefix := strings.TrimSuffix(in.DestinationPrefix, "/") + if prefix == "" { + prefix = defaultExportPrefix + } + + if err := m.checkExportLimit(); err != nil { + return "", err + } + + if err := m.checkDestination(ctx, in.Destination, prefix); err != nil { + return "", err + } + + now := m.opts.Clock.Now() + task := exportTask{ + ID: idgen.UUID(), Name: in.TaskName, LogGroup: in.LogGroupName, StreamPrefix: in.LogStreamNamePrefix, + From: in.From, To: in.To, Destination: in.Destination, Prefix: prefix, CreatedAt: now, + PendingFor: m.opts.SettleDuration(settle.DefaultExportTaskPendingSettle), + RunFor: m.opts.SettleDuration(settle.DefaultExportTaskRunSettle), + Status: driver.ExportTaskCompleted, + } + + m.exportMu.Lock() + + if m.activeExport(now) { + m.exportMu.Unlock() + + return "", errExportLimit() + } + + m.exports.Set(task.ID, task) + m.exportMu.Unlock() + + m.flushExports(ctx) + + return task.ID, nil +} + +func errExportLimit() error { + return cerrors.New(cerrors.ResourceExhausted, "Resource limit exceeded.") +} + +// checkExportLimit rejects a new task while one is still active. It is +// checked again when the task is recorded, since the destination checks in +// between run without the lock. +func (m *Mock) checkExportLimit() error { + m.exportMu.Lock() + defer m.exportMu.Unlock() + + if m.activeExport(m.opts.Clock.Now()) { + return errExportLimit() + } + + return nil +} + +// activeExport reports whether a PENDING or RUNNING task exists. Callers +// hold exportMu. +func (m *Mock) activeExport(now time.Time) bool { + all := m.exports.All() + for id := range all { + if t := all[id]; t.active(now) { + return true + } + } + + return false +} + +func validateExportRequest(in *driver.CreateExportTaskInput) error { + if err := validateLogGroupName(in.LogGroupName); err != nil { + return err + } + + switch { + case in.Destination == "" || len(in.Destination) > maxLogNameLength: + return cerrors.Newf(cerrors.InvalidArgument, + "1 validation error detected: Value at 'destination' failed to satisfy constraint: "+ + "Member must have length between 1 and %d", maxLogNameLength) + case len(in.TaskName) > maxLogNameLength: + return cerrors.Newf(cerrors.InvalidArgument, + "1 validation error detected: Value at 'taskName' failed to satisfy constraint: "+ + "Member must have length less than or equal to %d", maxLogNameLength) + case len(in.LogStreamNamePrefix) > maxLogNameLength || strings.ContainsAny(in.LogStreamNamePrefix, ":*"): + return cerrors.New(cerrors.InvalidArgument, + "1 validation error detected: Value at 'logStreamNamePrefix' failed to satisfy constraint: "+ + "Member must satisfy regular expression pattern: [^:*]*") + case in.From.Before(time.UnixMilli(0)) || in.To.Before(time.UnixMilli(0)): + return cerrors.New(cerrors.InvalidArgument, + "1 validation error detected: Value at 'from' or 'to' failed to satisfy constraint: "+ + "Member must have value greater than or equal to 0") + case in.To.Before(in.From): + return cerrors.New(cerrors.InvalidArgument, "The value of 'to' must be greater than or equal to the value of 'from'.") + } + + return nil +} + +// DescribeExportTasks lists export tasks, newest first, narrowed by task ID +// and status code. +func (m *Mock) DescribeExportTasks(ctx context.Context, in driver.DescribeExportTasksInput) ([]driver.ExportTask, error) { + if in.StatusCode != "" && !validExportStatus(in.StatusCode) { + return nil, cerrors.Newf(cerrors.InvalidArgument, + "1 validation error detected: Value '%s' at 'statusCode' failed to satisfy constraint: "+ + "Member must satisfy enum value set: [%s]", in.StatusCode, exportStatusEnum) + } + + m.flushExports(ctx) + + now := m.opts.Clock.Now() + + var out []driver.ExportTask + + all := m.exports.All() + for id := range all { + t := all[id] + if in.TaskID != "" && t.ID != in.TaskID { + continue + } + + d := t.describe(now) + if in.StatusCode != "" && d.StatusCode != in.StatusCode { + continue + } + + out = append(out, d) + } + + sort.Slice(out, func(i, j int) bool { + if !out[i].CreationTime.Equal(out[j].CreationTime) { + return out[i].CreationTime.After(out[j].CreationTime) + } + + return out[i].TaskID < out[j].TaskID + }) + + return out, nil +} + +func validExportStatus(s string) bool { + switch s { + case driver.ExportTaskPending, driver.ExportTaskRunning, driver.ExportTaskCompleted, + driver.ExportTaskCancelled, driver.ExportTaskFailed, "PENDING_CANCEL": + return true + } + + return false +} + +// CancelExportTask cancels a PENDING or RUNNING task. A canceled task writes +// no log objects. +func (m *Mock) CancelExportTask(_ context.Context, taskID string) error { + m.exportMu.Lock() + defer m.exportMu.Unlock() + + t, ok := m.exports.Get(taskID) + if !ok { + return cerrors.New(cerrors.NotFound, "The specified export task does not exist.") + } + + now := m.opts.Clock.Now() + if !t.active(now) { + return cerrors.Newf(cerrors.FailedPrecondition, + "The specified export task has already finished. Its status is %s.", t.observedStatus(now)) + } + + t.Status, t.Message, t.CompletedAt, t.Written = driver.ExportTaskCancelled, exportMsgCancelled, now, true + m.exports.Set(taskID, t) + + return nil +} + +// flushExports writes the objects of every task that has settled to +// COMPLETED and has not written them yet. Tasks are claimed under exportMu +// and written after it is released, so no lock is held across an S3 call. A +// write that fails marks the task FAILED. +func (m *Mock) flushExports(ctx context.Context) { + if m.exportStore == nil { + return + } + + now := m.opts.Clock.Now() + + var due []exportTask + + m.exportMu.Lock() + + all := m.exports.All() + for id := range all { + t := all[id] + if t.Written || t.observedStatus(now) != driver.ExportTaskCompleted { + continue + } + + t.Written = true + m.exports.Set(id, t) + due = append(due, t) + } + + m.exportMu.Unlock() + + for i := range due { + if err := m.writeExport(ctx, &due[i]); err != nil { + m.failExport(due[i].ID, err) + } + } +} + +func (m *Mock) failExport(id string, err error) { + m.exportMu.Lock() + defer m.exportMu.Unlock() + + m.exports.Update(id, func(t exportTask) exportTask { + t.Status, t.Message, t.CompletedAt = driver.ExportTaskFailed, cerrors.Message(err), m.opts.Clock.Now() + return t + }) +} + +// writeExport writes one gzip object per log stream that has events in the +// task's range, at ///000000.gz. Each line is +// the event's timestamp and its message. +func (m *Mock) writeExport(ctx context.Context, t *exportTask) error { + g, ok := m.groups.Get(t.LogGroup) + if !ok { + return nil + } + + m.purgeExpired(t.LogGroup, g) + + for _, s := range g.streams.SortedValues() { + if !strings.HasPrefix(s.info.Name, t.StreamPrefix) { + continue + } + + body, n, err := exportStreamBody(s, t.From, t.To) + if err != nil { + return err + } + + if n == 0 { + continue + } + + key := path.Join(t.Prefix, t.ID, s.info.Name, exportChunkName) + if err := m.exportStore.PutObject(ctx, t.Destination, key, body, exportContentType, nil); err != nil { + return cerrors.New(cerrors.InvalidArgument, putObjectFailedMsg) + } + } + + return nil +} + +// exportStreamBody gzips a stream's events with a timestamp in [from, to] and +// returns the body and how many events it holds. +func exportStreamBody(s *logStream, from, to time.Time) (body []byte, n int, err error) { + var lines strings.Builder + + s.mu.RLock() + + for i := range s.events { + e := &s.events[i] + if e.Timestamp.Before(from) || e.Timestamp.After(to) { + continue + } + + lines.WriteString(e.Timestamp.UTC().Format(exportLineTimeLayout)) + lines.WriteByte(' ') + lines.WriteString(e.Message) + lines.WriteByte('\n') + + n++ + } + + s.mu.RUnlock() + + if n == 0 { + return nil, 0, nil + } + + var buf bytes.Buffer + + zw := gzip.NewWriter(&buf) + if _, werr := zw.Write([]byte(lines.String())); werr != nil { + return nil, 0, werr + } + + if cerr := zw.Close(); cerr != nil { + return nil, 0, cerr + } + + return buf.Bytes(), n, nil +} diff --git a/providers/aws/cloudwatchlogs/export_tasks_test.go b/providers/aws/cloudwatchlogs/export_tasks_test.go new file mode 100644 index 000000000..51f30595d --- /dev/null +++ b/providers/aws/cloudwatchlogs/export_tasks_test.go @@ -0,0 +1,391 @@ +package cloudwatchlogs + +import ( + "bytes" + "compress/gzip" + "context" + "encoding/json" + "io" + "testing" + "time" + + "github.com/stackshy/cloudemu/v2/config" + "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/providers/aws/s3" + "github.com/stackshy/cloudemu/v2/services/logging/driver" + storagedriver "github.com/stackshy/cloudemu/v2/services/storage/driver" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const exportBucket = "exp-bucket" + +// exportPolicy is the bucket policy the CloudWatch Logs guide gives for +// exports. +const exportPolicy = `{"Version":"2012-10-17","Statement":[ + {"Effect":"Allow","Principal":{"Service":"logs.us-east-1.amazonaws.com"},"Action":"s3:GetBucketAcl", + "Resource":"arn:aws:s3:::exp-bucket","Condition":{"StringEquals":{"aws:SourceAccount":["123456789012"]}}}, + {"Effect":"Allow","Principal":{"Service":"logs.us-east-1.amazonaws.com"},"Action":"s3:PutObject", + "Resource":"arn:aws:s3:::exp-bucket/*"}]}` + +type exportFixture struct { + logs *Mock + store *s3.Mock + clock *config.FakeClock +} + +func newExportFixture(t *testing.T, async bool, policy string) *exportFixture { + t.Helper() + + fc := config.NewFakeClock(testClockStart()) + opts := []config.Option{config.WithClock(fc), config.WithRegion("us-east-1")} + + if async { + opts = append(opts, config.WithAsyncSettle()) + } + + o := config.NewOptions(opts...) + f := &exportFixture{logs: New(o), store: s3.New(o), clock: fc} + f.logs.SetExportStore(f.store) + + ctx := context.Background() + require.NoError(t, f.store.CreateBucket(ctx, exportBucket)) + + if policy != "" { + require.NoError(t, f.store.PutBucketConfig(ctx, exportBucket, "policy", []byte(policy))) + } + + _, err := f.logs.CreateLogGroup(ctx, driver.LogGroupConfig{Name: "g"}) + require.NoError(t, err) + + base := testEventTime() + + for _, s := range []string{"app-1", "app-2", "other"} { + _, err = f.logs.CreateLogStream(ctx, "g", s) + require.NoError(t, err) + + _, err = f.logs.PutLogEventsWithResult(ctx, "g", s, []driver.LogEvent{ + {Timestamp: base, Message: s + " first"}, + {Timestamp: base.Add(time.Minute), Message: s + " second"}, + {Timestamp: base.Add(2 * time.Hour), Message: s + " late"}, + }) + require.NoError(t, err) + } + + return f +} + +func (f *exportFixture) input() *driver.CreateExportTaskInput { + base := testEventTime() + + return &driver.CreateExportTaskInput{ + TaskName: "t1", LogGroupName: "g", From: base, To: base.Add(3 * time.Hour), Destination: exportBucket, + } +} + +func (f *exportFixture) object(t *testing.T, key string) []byte { + t.Helper() + + obj, err := f.store.GetObject(context.Background(), exportBucket, key) + require.NoError(t, err, key) + + return obj.Data +} + +func gunzip(t *testing.T, data []byte) string { + t.Helper() + + zr, err := gzip.NewReader(bytes.NewReader(data)) + require.NoError(t, err) + + out, err := io.ReadAll(zr) + require.NoError(t, err) + + return string(out) +} + +func (f *exportFixture) keys(t *testing.T) []string { + t.Helper() + + res, err := f.store.ListObjects(context.Background(), exportBucket, storagedriver.ListOptions{}) + require.NoError(t, err) + + out := make([]string, 0, len(res.Objects)) + for _, o := range res.Objects { + out = append(out, o.Key) + } + + return out +} + +func TestExportTaskWritesObjects(t *testing.T) { + ctx := context.Background() + f := newExportFixture(t, false, exportPolicy) + + _, err := f.logs.PutLogEventsWithResult(ctx, "g", "app-1", []driver.LogEvent{ + {Timestamp: testClockStart().Add(30 * time.Minute), Message: "app-1 after to"}, + }) + require.NoError(t, err) + + in := f.input() + in.LogStreamNamePrefix = "app-" + in.From = testEventTime().Add(time.Minute) + in.To = testClockStart() + + id, err := f.logs.CreateExportTask(ctx, in) + require.NoError(t, err) + + tasks, err := f.logs.DescribeExportTasks(ctx, driver.DescribeExportTasksInput{TaskID: id}) + require.NoError(t, err) + require.Len(t, tasks, 1) + assert.Equal(t, driver.ExportTaskCompleted, tasks[0].StatusCode) + assert.Equal(t, "Completed successfully", tasks[0].StatusMessage) + assert.Equal(t, "exportedlogs", tasks[0].DestinationPrefix) + assert.Equal(t, "t1", tasks[0].TaskName) + assert.False(t, tasks[0].CompletionTime.IsZero()) + + assert.ElementsMatch(t, []string{ + "exportedlogs/aws-logs-write-test", + "exportedlogs/" + id + "/app-1/000000.gz", + "exportedlogs/" + id + "/app-2/000000.gz", + }, f.keys(t), "only streams matching the prefix are exported") + + assert.Equal(t, "Permission Check Successful", string(f.object(t, "exportedlogs/aws-logs-write-test"))) + + // from and to bound the event timestamps, both inclusive. + got := gunzip(t, f.object(t, "exportedlogs/"+id+"/app-1/000000.gz")) + assert.Equal(t, "2024-12-31T21:01:00.000Z app-1 second\n2024-12-31T23:00:00.000Z app-1 late\n", got) +} + +func TestExportTaskCustomPrefix(t *testing.T) { + f := newExportFixture(t, false, exportPolicy) + + in := f.input() + in.DestinationPrefix = "my/prefix/" + + id, err := f.logs.CreateExportTask(context.Background(), in) + require.NoError(t, err) + + got := gunzip(t, f.object(t, "my/prefix/"+id+"/other/000000.gz")) + assert.Contains(t, got, "other late") +} + +func TestExportTaskLifecycleSettles(t *testing.T) { + ctx := context.Background() + f := newExportFixture(t, true, exportPolicy) + + id, err := f.logs.CreateExportTask(ctx, f.input()) + require.NoError(t, err) + + status := func() string { + tasks, err := f.logs.DescribeExportTasks(ctx, driver.DescribeExportTasksInput{TaskID: id}) + require.NoError(t, err) + require.Len(t, tasks, 1) + + return tasks[0].StatusCode + } + + assert.Equal(t, driver.ExportTaskPending, status()) + assert.Equal(t, []string{"exportedlogs/aws-logs-write-test"}, f.keys(t), "no events are written before the task runs") + + _, err = f.logs.CreateExportTask(ctx, f.input()) + assert.Equal(t, errors.ResourceExhausted, errors.GetCode(err), "one active task per account") + + f.clock.Advance(time.Second) + assert.Equal(t, driver.ExportTaskRunning, status()) + + f.clock.Advance(2 * time.Second) + assert.Equal(t, driver.ExportTaskCompleted, status()) + assert.Len(t, f.keys(t), 4) + + _, err = f.logs.CreateExportTask(ctx, f.input()) + require.NoError(t, err, "a finished task frees the slot") +} + +func TestCancelExportTask(t *testing.T) { + ctx := context.Background() + f := newExportFixture(t, true, exportPolicy) + + id, err := f.logs.CreateExportTask(ctx, f.input()) + require.NoError(t, err) + + f.clock.Advance(time.Second) + require.NoError(t, f.logs.CancelExportTask(ctx, id)) + + f.clock.Advance(time.Minute) + + tasks, err := f.logs.DescribeExportTasks(ctx, driver.DescribeExportTasksInput{StatusCode: driver.ExportTaskCancelled}) + require.NoError(t, err) + require.Len(t, tasks, 1) + assert.False(t, tasks[0].CompletionTime.IsZero()) + assert.Equal(t, []string{"exportedlogs/aws-logs-write-test"}, f.keys(t), "a cancelled task writes no events") + + err = f.logs.CancelExportTask(ctx, id) + assert.True(t, errors.IsFailedPrecondition(err)) + + err = f.logs.CancelExportTask(ctx, "nope") + assert.True(t, errors.IsNotFound(err)) +} + +func TestCancelCompletedExportTask(t *testing.T) { + ctx := context.Background() + f := newExportFixture(t, false, exportPolicy) + + id, err := f.logs.CreateExportTask(ctx, f.input()) + require.NoError(t, err) + + assert.True(t, errors.IsFailedPrecondition(f.logs.CancelExportTask(ctx, id))) +} + +func TestDescribeExportTasksFilters(t *testing.T) { + ctx := context.Background() + f := newExportFixture(t, false, exportPolicy) + + first, err := f.logs.CreateExportTask(ctx, f.input()) + require.NoError(t, err) + + f.clock.Advance(time.Second) + + second, err := f.logs.CreateExportTask(ctx, f.input()) + require.NoError(t, err) + + all, err := f.logs.DescribeExportTasks(ctx, driver.DescribeExportTasksInput{}) + require.NoError(t, err) + require.Len(t, all, 2) + assert.Equal(t, second, all[0].TaskID, "newest first") + assert.Equal(t, first, all[1].TaskID) + + none, err := f.logs.DescribeExportTasks(ctx, driver.DescribeExportTasksInput{StatusCode: driver.ExportTaskRunning}) + require.NoError(t, err) + assert.Empty(t, none) + + _, err = f.logs.DescribeExportTasks(ctx, driver.DescribeExportTasksInput{StatusCode: "DONE"}) + assert.True(t, errors.IsInvalidArgument(err)) +} + +func TestCreateExportTaskErrors(t *testing.T) { + denyPut := `{"Statement":[ + {"Effect":"Allow","Principal":{"Service":"logs.amazonaws.com"},"Action":"s3:*","Resource":"arn:aws:s3:::exp-bucket*"}, + {"Effect":"Deny","Principal":"*","Action":"s3:PutObject","Resource":"arn:aws:s3:::exp-bucket/*"}]}` + aclOnly := `{"Statement":{"Effect":"Allow","Principal":{"Service":"logs.us-east-1.amazonaws.com"}, + "Action":"s3:GetBucketAcl","Resource":"arn:aws:s3:::exp-bucket"}}` + otherPrincipal := `{"Statement":[{"Effect":"Allow","Principal":{"Service":"delivery.logs.amazonaws.com"}, + "Action":"s3:*","Resource":"*"}]}` + + tests := []struct { + name string + policy string + mutate func(*driver.CreateExportTaskInput) + check func(error) bool + msg string + }{ + {"missing group", exportPolicy, func(in *driver.CreateExportTaskInput) { in.LogGroupName = "nope" }, errors.IsNotFound, ""}, + {"missing bucket", exportPolicy, func(in *driver.CreateExportTaskInput) { in.Destination = "nope" }, + errors.IsInvalidArgument, bucketMissingMsg}, + {"no policy", "", nil, errors.IsInvalidArgument, getBucketACLMsg}, + {"other principal", otherPrincipal, nil, errors.IsInvalidArgument, getBucketACLMsg}, + {"no put grant", aclOnly, nil, errors.IsInvalidArgument, putObjectFailedMsg}, + {"put denied", denyPut, nil, errors.IsInvalidArgument, putObjectFailedMsg}, + {"to before from", exportPolicy, func(in *driver.CreateExportTaskInput) { in.To = in.From.Add(-time.Second) }, + errors.IsInvalidArgument, ""}, + {"to before group creation", exportPolicy, func(in *driver.CreateExportTaskInput) { in.To = in.From }, + errors.IsInvalidArgument, ""}, + {"bad stream prefix", exportPolicy, func(in *driver.CreateExportTaskInput) { in.LogStreamNamePrefix = "a*" }, + errors.IsInvalidArgument, ""}, + {"no destination", exportPolicy, func(in *driver.CreateExportTaskInput) { in.Destination = "" }, errors.IsInvalidArgument, ""}, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + f := newExportFixture(t, false, tc.policy) + + in := f.input() + if tc.mutate != nil { + tc.mutate(in) + } + + _, err := f.logs.CreateExportTask(context.Background(), in) + require.Error(t, err) + assert.True(t, tc.check(err), err.Error()) + + if tc.msg != "" { + assert.Equal(t, tc.msg, errors.Message(err)) + } + }) + } +} + +func TestCreateExportTaskWithoutStore(t *testing.T) { + m := newWindowMock(t, 0) + + _, err := m.CreateExportTask(context.Background(), &driver.CreateExportTaskInput{ + LogGroupName: "g", From: testClockStart(), To: testClockStart(), Destination: exportBucket, + }) + assert.Equal(t, bucketMissingMsg, errors.Message(err)) +} + +func TestExportTaskTypedBucketPolicy(t *testing.T) { + ctx := context.Background() + f := newExportFixture(t, false, "") + + require.NoError(t, f.store.PutBucketPolicy(ctx, exportBucket, storagedriver.BucketPolicy{ + Statements: []storagedriver.PolicyStatement{{ + Effect: "Allow", Principal: "logs.amazonaws.com", Actions: []string{"s3:GetBucketAcl", "s3:PutObject"}, + Resources: []string{"arn:aws:s3:::exp-bucket", "arn:aws:s3:::exp-bucket/*"}, + }}, + })) + + _, err := f.logs.CreateExportTask(ctx, f.input()) + require.NoError(t, err) +} + +func TestExportTaskFailsWhenBucketGoes(t *testing.T) { + ctx := context.Background() + f := newExportFixture(t, true, exportPolicy) + + id, err := f.logs.CreateExportTask(ctx, f.input()) + require.NoError(t, err) + + require.NoError(t, f.store.DeleteObject(ctx, exportBucket, "exportedlogs/aws-logs-write-test")) + require.NoError(t, f.store.DeleteBucket(ctx, exportBucket)) + + f.clock.Advance(time.Minute) + + tasks, err := f.logs.DescribeExportTasks(ctx, driver.DescribeExportTasksInput{TaskID: id}) + require.NoError(t, err) + require.Len(t, tasks, 1) + assert.Equal(t, driver.ExportTaskFailed, tasks[0].StatusCode) + assert.Equal(t, putObjectFailedMsg, tasks[0].StatusMessage) +} + +func TestExportTasksSnapshotRoundTrip(t *testing.T) { + ctx := context.Background() + f := newExportFixture(t, true, exportPolicy) + + id, err := f.logs.CreateExportTask(ctx, f.input()) + require.NoError(t, err) + + data, err := f.logs.Snapshot(ctx, false) + require.NoError(t, err) + + var raw map[string]json.RawMessage + require.NoError(t, json.Unmarshal(data, &raw)) + assert.Contains(t, raw, "exportTasks") + + restored := New(f.logs.opts) + restored.SetExportStore(f.store) + require.NoError(t, restored.Restore(ctx, data)) + + tasks, err := restored.DescribeExportTasks(ctx, driver.DescribeExportTasksInput{}) + require.NoError(t, err) + require.Len(t, tasks, 1) + assert.Equal(t, id, tasks[0].TaskID) + assert.Equal(t, driver.ExportTaskPending, tasks[0].StatusCode) + + f.clock.Advance(time.Minute) + + tasks, err = restored.DescribeExportTasks(ctx, driver.DescribeExportTasksInput{}) + require.NoError(t, err) + assert.Equal(t, driver.ExportTaskCompleted, tasks[0].StatusCode) + assert.Len(t, f.keys(t), 4, "the restored task writes its objects") +} diff --git a/providers/aws/cloudwatchlogs/snapshot.go b/providers/aws/cloudwatchlogs/snapshot.go index 4a03d7780..5fded4639 100644 --- a/providers/aws/cloudwatchlogs/snapshot.go +++ b/providers/aws/cloudwatchlogs/snapshot.go @@ -17,12 +17,13 @@ var _ snapshot.Snapshottable = (*Mock)(nil) // groups store holds an unexported *logGroup whose nested stores (streams, and // the metric/subscription filter stores) must be captured, so it is promoted to // an exported snapshot form keyed by group name. The wired deps (opts, -// monitoring, lambda) are not serialized. +// monitoring, lambda, exportStore) are not serialized. type logsSnapshot struct { Groups map[string]*logGroupSnapshot `json:"groups,omitempty"` QueryResultKmsKey string `json:"queryResultKmsKey,omitempty"` FilterPeriods map[string]filterPeriod `json:"filterPeriods,omitempty"` Queries json.RawMessage `json:"queries,omitempty"` + ExportTasks json.RawMessage `json:"exportTasks,omitempty"` } // logGroupSnapshot mirrors logGroup. Its metric- and subscription-filter stores @@ -60,6 +61,13 @@ func (m *Mock) Snapshot(_ context.Context, _ bool) (json.RawMessage, error) { snap.Queries = queries + exports, err := m.exports.Snapshot() + if err != nil { + return nil, fmt.Errorf("cloudwatchlogs: snapshot export tasks: %w", err) + } + + snap.ExportTasks = exports + if m.groups.Len() == 0 { return json.Marshal(snap) } @@ -130,6 +138,12 @@ func (m *Mock) Restore(_ context.Context, data json.RawMessage) error { } } + if len(snap.ExportTasks) > 0 { + if err := m.exports.LoadSnapshot(snap.ExportTasks); err != nil { + return fmt.Errorf("cloudwatchlogs: restore export tasks: %w", err) + } + } + for name, gs := range snap.Groups { g, err := restoreGroup(gs) if err != nil { diff --git a/server/aws/cloudwatchlogs/export_tasks.go b/server/aws/cloudwatchlogs/export_tasks.go new file mode 100644 index 000000000..7c78d6a57 --- /dev/null +++ b/server/aws/cloudwatchlogs/export_tasks.go @@ -0,0 +1,177 @@ +package cloudwatchlogs + +import ( + "net/http" + "time" + + "github.com/stackshy/cloudemu/v2/server/wire" + logdriver "github.com/stackshy/cloudemu/v2/services/logging/driver" +) + +// maxDescribeExportTasks is the DescribeExportTasks limit maximum and default. +const maxDescribeExportTasks = 50 + +// dispatchExport handles the export task operations, delegating tagging to +// dispatchTags. +func (h *Handler) dispatchExport(w http.ResponseWriter, r *http.Request, op string) { + switch op { + case "CreateExportTask": + h.createExportTask(w, r) + case "DescribeExportTasks": + h.describeExportTasks(w, r) + case "CancelExportTask": + h.cancelExportTask(w, r) + default: + h.dispatchTags(w, r, op) + } +} + +type createExportTaskRequest struct { + TaskName string `json:"taskName"` + LogGroupName string `json:"logGroupName"` + LogStreamNamePrefix string `json:"logStreamNamePrefix"` + From *int64 `json:"from"` + To *int64 `json:"to"` + Destination string `json:"destination"` + DestinationPrefix string `json:"destinationPrefix"` +} + +func (h *Handler) createExportTask(w http.ResponseWriter, r *http.Request) { + ext, ok := h.awsLogs(w, "CreateExportTask") + if !ok { + return + } + + var req createExportTaskRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + for _, f := range []struct { + name string + v *int64 + }{{"from", req.From}, {"to", req.To}} { + if f.v == nil { + wire.WriteJSONError(w, http.StatusBadRequest, "InvalidParameterException", + "1 validation error detected: Value null at '"+f.name+"' failed to satisfy constraint: Member must not be null") + + return + } + } + + id, err := ext.CreateExportTask(r.Context(), &logdriver.CreateExportTaskInput{ + TaskName: req.TaskName, LogGroupName: req.LogGroupName, LogStreamNamePrefix: req.LogStreamNamePrefix, + From: time.UnixMilli(*req.From).UTC(), To: time.UnixMilli(*req.To).UTC(), + Destination: req.Destination, DestinationPrefix: req.DestinationPrefix, + }) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, map[string]string{"taskId": id}) +} + +type exportTaskStatusJSON struct { + Code string `json:"code"` + Message string `json:"message,omitempty"` +} + +type exportExecutionInfoJSON struct { + CreationTime int64 `json:"creationTime,omitempty"` + CompletionTime int64 `json:"completionTime,omitempty"` +} + +type exportTaskJSON struct { + TaskID string `json:"taskId"` + TaskName string `json:"taskName,omitempty"` + LogGroupName string `json:"logGroupName"` + From int64 `json:"from"` + To int64 `json:"to"` + Destination string `json:"destination"` + DestinationPrefix string `json:"destinationPrefix"` + Status exportTaskStatusJSON `json:"status"` + ExecutionInfo exportExecutionInfoJSON `json:"executionInfo"` +} + +func (h *Handler) describeExportTasks(w http.ResponseWriter, r *http.Request) { + ext, ok := h.awsLogs(w, "DescribeExportTasks") + if !ok { + return + } + + var req struct { + TaskID string `json:"taskId"` + StatusCode string `json:"statusCode"` + Limit int32 `json:"limit"` + NextToken string `json:"nextToken"` + } + + if !wire.DecodeJSON(w, r, &req) { + return + } + + if req.Limit < 0 || req.Limit > maxDescribeExportTasks { + wire.WriteJSONError(w, http.StatusBadRequest, "InvalidParameterException", + "1 validation error detected: Value at 'limit' failed to satisfy constraint: "+ + "Member must have value between 1 and 50") + + return + } + + tasks, err := ext.DescribeExportTasks(r.Context(), logdriver.DescribeExportTasksInput{ + TaskID: req.TaskID, StatusCode: req.StatusCode, + }) + if err != nil { + writeErr(w, err) + return + } + + from, to, next := pageBounds(len(tasks), decodeOffsetToken(req.NextToken), req.Limit) + + out := make([]exportTaskJSON, 0, to-from) + for i := range tasks[from:to] { + out = append(out, exportTaskToJSON(&tasks[from+i])) + } + + resp := map[string]any{"exportTasks": out} + if next > 0 { + resp["nextToken"] = encodeOffsetToken(next) + } + + wire.WriteJSON(w, resp) +} + +func exportTaskToJSON(t *logdriver.ExportTask) exportTaskJSON { + return exportTaskJSON{ + TaskID: t.TaskID, TaskName: t.TaskName, LogGroupName: t.LogGroupName, + From: t.From.UnixMilli(), To: t.To.UnixMilli(), + Destination: t.Destination, DestinationPrefix: t.DestinationPrefix, + Status: exportTaskStatusJSON{Code: t.StatusCode, Message: t.StatusMessage}, + ExecutionInfo: exportExecutionInfoJSON{ + CreationTime: epochMillis(t.CreationTime), CompletionTime: epochMillis(t.CompletionTime), + }, + } +} + +func (h *Handler) cancelExportTask(w http.ResponseWriter, r *http.Request) { + ext, ok := h.awsLogs(w, "CancelExportTask") + if !ok { + return + } + + var req struct { + TaskID string `json:"taskId"` + } + + if !wire.DecodeJSON(w, r, &req) { + return + } + + if err := ext.CancelExportTask(r.Context(), req.TaskID); err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, struct{}{}) +} diff --git a/server/aws/cloudwatchlogs/export_tasks_sdk_test.go b/server/aws/cloudwatchlogs/export_tasks_sdk_test.go new file mode 100644 index 000000000..f9f4d60d6 --- /dev/null +++ b/server/aws/cloudwatchlogs/export_tasks_sdk_test.go @@ -0,0 +1,211 @@ +package cloudwatchlogs_test + +import ( + "bytes" + "compress/gzip" + "context" + "errors" + "io" + "net/http/httptest" + "strings" + "testing" + "time" + + "github.com/aws/aws-sdk-go-v2/aws" + awsconfig "github.com/aws/aws-sdk-go-v2/config" + "github.com/aws/aws-sdk-go-v2/credentials" + cwl "github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs" + cwltypes "github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs/types" + awss3 "github.com/aws/aws-sdk-go-v2/service/s3" + + "github.com/stackshy/cloudemu/v2" + awsserver "github.com/stackshy/cloudemu/v2/server/aws" +) + +const sdkExportPolicy = `{"Version":"2012-10-17","Statement":[ + {"Effect":"Allow","Principal":{"Service":"logs.us-east-1.amazonaws.com"},"Action":"s3:GetBucketAcl","Resource":"arn:aws:s3:::sdk-exp"}, + {"Effect":"Allow","Principal":{"Service":"logs.us-east-1.amazonaws.com"},"Action":"s3:PutObject","Resource":"arn:aws:s3:::sdk-exp/*"}]}` + +func newLogsS3Clients(t *testing.T) (*cwl.Client, *awss3.Client) { + t.Helper() + + cloud := cloudemu.NewAWS() + ts := httptest.NewServer(awsserver.New(awsserver.Drivers{CloudWatchLogs: cloud.CloudWatchLogs, S3: cloud.S3})) + t.Cleanup(ts.Close) + + cfg, err := awsconfig.LoadDefaultConfig(context.Background(), + awsconfig.WithRegion("us-east-1"), + awsconfig.WithCredentialsProvider(credentials.NewStaticCredentialsProvider("test", "test", "")), + ) + if err != nil { + t.Fatalf("aws config: %v", err) + } + + cfg.BaseEndpoint = aws.String(ts.URL) + + return cwl.NewFromConfig(cfg), awss3.NewFromConfig(cfg, func(o *awss3.Options) { o.UsePathStyle = true }) +} + +func TestSDKExportTaskToS3(t *testing.T) { + logs, s3c := newLogsS3Clients(t) + ctx := context.Background() + g := aws.String("/export/g") + + mustSDK(t, "CreateBucket", func() error { + _, err := s3c.CreateBucket(ctx, &awss3.CreateBucketInput{Bucket: aws.String("sdk-exp")}) + return err + }) + + mustSDK(t, "CreateLogGroup", func() error { + _, err := logs.CreateLogGroup(ctx, &cwl.CreateLogGroupInput{LogGroupName: g}) + return err + }) + + mustSDK(t, "CreateLogStream", func() error { + _, err := logs.CreateLogStream(ctx, &cwl.CreateLogStreamInput{LogGroupName: g, LogStreamName: aws.String("web/1")}) + return err + }) + + now := time.Now() + mustSDK(t, "PutLogEvents", func() error { + _, err := logs.PutLogEvents(ctx, &cwl.PutLogEventsInput{ + LogGroupName: g, LogStreamName: aws.String("web/1"), + LogEvents: []cwltypes.InputLogEvent{{Timestamp: aws.Int64(now.UnixMilli()), Message: aws.String("hello export")}}, + }) + + return err + }) + + in := &cwl.CreateExportTaskInput{ + LogGroupName: g, Destination: aws.String("sdk-exp"), DestinationPrefix: aws.String("pfx"), + From: aws.Int64(now.Add(-time.Hour).UnixMilli()), To: aws.Int64(now.Add(time.Hour).UnixMilli()), + TaskName: aws.String("sdk-task"), + } + + var ipe *cwltypes.InvalidParameterException + if _, err := logs.CreateExportTask(ctx, in); !errors.As(err, &ipe) || !strings.Contains(err.Error(), "GetBucketAcl") { + t.Fatalf("CreateExportTask without a bucket policy = %v, want InvalidParameterException", err) + } + + mustSDK(t, "PutBucketPolicy", func() error { + _, err := s3c.PutBucketPolicy(ctx, &awss3.PutBucketPolicyInput{Bucket: aws.String("sdk-exp"), Policy: aws.String(sdkExportPolicy)}) + return err + }) + + created, err := logs.CreateExportTask(ctx, in) + if err != nil { + t.Fatalf("CreateExportTask: %v", err) + } + + out, err := logs.DescribeExportTasks(ctx, &cwl.DescribeExportTasksInput{TaskId: created.TaskId}) + if err != nil || len(out.ExportTasks) != 1 { + t.Fatalf("DescribeExportTasks = %+v, %v", out, err) + } + + task := out.ExportTasks[0] + if task.Status.Code != cwltypes.ExportTaskStatusCodeCompleted || aws.ToString(task.TaskName) != "sdk-task" || + aws.ToString(task.DestinationPrefix) != "pfx" || task.ExecutionInfo.CompletionTime == nil { + t.Fatalf("task = %+v status %+v", task, task.Status) + } + + obj, err := s3c.GetObject(ctx, &awss3.GetObjectInput{ + Bucket: aws.String("sdk-exp"), Key: aws.String("pfx/" + aws.ToString(created.TaskId) + "/web/1/000000.gz"), + }) + if err != nil { + t.Fatalf("GetObject: %v", err) + } + + body, _ := io.ReadAll(obj.Body) + _ = obj.Body.Close() + + zr, err := gzip.NewReader(bytes.NewReader(body)) + if err != nil { + t.Fatalf("gzip: %v", err) + } + + text, _ := io.ReadAll(zr) + want := now.UTC().Truncate(time.Millisecond).Format("2006-01-02T15:04:05.000Z") + " hello export\n" + + if string(text) != want { + t.Fatalf("export body = %q, want %q", text, want) + } + + var ioe *cwltypes.InvalidOperationException + if _, err := logs.CancelExportTask(ctx, &cwl.CancelExportTaskInput{TaskId: created.TaskId}); !errors.As(err, &ioe) { + t.Fatalf("CancelExportTask on a completed task = %v, want InvalidOperationException", err) + } + + var rnf *cwltypes.ResourceNotFoundException + if _, err := logs.CancelExportTask(ctx, &cwl.CancelExportTaskInput{TaskId: aws.String("nope")}); !errors.As(err, &rnf) { + t.Fatalf("CancelExportTask unknown = %v, want ResourceNotFoundException", err) + } +} + +func TestSDKDescribeExportTasksPaging(t *testing.T) { + logs, s3c := newLogsS3Clients(t) + ctx := context.Background() + g := aws.String("g") + + mustSDK(t, "CreateBucket", func() error { + _, err := s3c.CreateBucket(ctx, &awss3.CreateBucketInput{Bucket: aws.String("sdk-exp")}) + return err + }) + mustSDK(t, "PutBucketPolicy", func() error { + _, err := s3c.PutBucketPolicy(ctx, &awss3.PutBucketPolicyInput{Bucket: aws.String("sdk-exp"), Policy: aws.String(sdkExportPolicy)}) + return err + }) + mustSDK(t, "CreateLogGroup", func() error { + _, err := logs.CreateLogGroup(ctx, &cwl.CreateLogGroupInput{LogGroupName: g}) + return err + }) + + now := time.Now() + + for range 3 { + mustSDK(t, "CreateExportTask", func() error { + _, err := logs.CreateExportTask(ctx, &cwl.CreateExportTaskInput{ + LogGroupName: g, Destination: aws.String("sdk-exp"), + From: aws.Int64(now.Add(-time.Hour).UnixMilli()), To: aws.Int64(now.Add(time.Hour).UnixMilli()), + }) + + return err + }) + } + + seen, pages := 0, 0 + + var token *string + + for { + page, err := logs.DescribeExportTasks(ctx, &cwl.DescribeExportTasksInput{Limit: aws.Int32(2), NextToken: token}) + if err != nil { + t.Fatalf("DescribeExportTasks page: %v", err) + } + + seen += len(page.ExportTasks) + pages++ + + if page.NextToken == nil { + break + } + + token = page.NextToken + } + + if seen != 3 || pages != 2 { + t.Fatalf("paged %d tasks over %d pages, want 3 over 2", seen, pages) + } + + var ipe *cwltypes.InvalidParameterException + if _, err := logs.DescribeExportTasks(ctx, &cwl.DescribeExportTasksInput{Limit: aws.Int32(51)}); !errors.As(err, &ipe) { + t.Fatalf("limit 51 = %v, want InvalidParameterException", err) + } +} + +func mustSDK(t *testing.T, op string, fn func() error) { + t.Helper() + + if err := fn(); err != nil { + t.Fatalf("%s: %v", op, err) + } +} diff --git a/server/aws/cloudwatchlogs/handler.go b/server/aws/cloudwatchlogs/handler.go index b9a240329..8d7edff4b 100644 --- a/server/aws/cloudwatchlogs/handler.go +++ b/server/aws/cloudwatchlogs/handler.go @@ -19,6 +19,7 @@ // PutRetentionPolicy DeleteRetentionPolicy ListLogGroups // AssociateKmsKey DisassociateKmsKey TestMetricFilter // StartQuery GetQueryResults StopQuery DescribeQueries GetLogRecord GetLogGroupFields +// CreateExportTask DescribeExportTasks CancelExportTask // TagResource / UntagResource / ListTagsForResource (and the legacy *LogGroup forms) // // CloudWatch Logs-only operations go through the optional driver.AWSLogging @@ -130,8 +131,8 @@ func (h *Handler) dispatchGroupSettings(w http.ResponseWriter, r *http.Request, } } -// dispatchInsights handles the Logs Insights operations, delegating tagging -// to dispatchTags. +// dispatchInsights handles the Logs Insights operations, delegating export +// tasks and tagging to dispatchExport. func (h *Handler) dispatchInsights(w http.ResponseWriter, r *http.Request, op string) { switch op { case "StartQuery": @@ -147,7 +148,7 @@ func (h *Handler) dispatchInsights(w http.ResponseWriter, r *http.Request, op st case "GetLogGroupFields": h.getLogGroupFields(w, r) default: - h.dispatchTags(w, r, op) + h.dispatchExport(w, r, op) } } diff --git a/services/logging/driver/aws.go b/services/logging/driver/aws.go index 31b93c984..e86b7ad13 100644 --- a/services/logging/driver/aws.go +++ b/services/logging/driver/aws.go @@ -152,6 +152,46 @@ type MalformedQueryError struct { func (e *MalformedQueryError) Error() string { return e.Message } +// Export task status codes. +const ( + ExportTaskPending = "PENDING" + ExportTaskRunning = "RUNNING" + ExportTaskCompleted = "COMPLETED" + ExportTaskCancelled = "CANCELLED" //nolint:misspell // the API's status value is spelled this way. + ExportTaskFailed = "FAILED" +) + +// CreateExportTaskInput is a CreateExportTask request. From and To bound the +// event timestamps exported, both inclusive. +type CreateExportTaskInput struct { + TaskName string + LogGroupName string + LogStreamNamePrefix string + From, To time.Time + Destination string + DestinationPrefix string +} + +// ExportTask describes an export task for DescribeExportTasks. +// CompletionTime is zero until the task reaches a terminal status. +type ExportTask struct { + TaskID string + TaskName string + LogGroupName string + From, To time.Time + Destination string + DestinationPrefix string + StatusCode string + StatusMessage string + CreationTime time.Time + CompletionTime time.Time +} + +// DescribeExportTasksInput narrows DescribeExportTasks; empty fields match all. +type DescribeExportTasksInput struct { + TaskID, StatusCode string +} + // AWSLogging is an optional interface implemented by the AWS CloudWatch Logs // backend for operations with no cross-provider equivalent. The CloudWatch // Logs wire handler type-asserts for it; Azure, GCP and OCI backends do not @@ -188,4 +228,11 @@ type AWSLogging interface { // TestMetricFilter runs a filter pattern over sample log messages. TestMetricFilter(ctx context.Context, filterPattern string, logEventMessages []string) ([]MetricFilterMatch, error) + + // Export tasks write a log group's events to an S3 bucket. A task moves + // PENDING -> RUNNING -> COMPLETED, and the account runs one active task + // at a time. DescribeExportTasks lists the newest task first. + CreateExportTask(ctx context.Context, input *CreateExportTaskInput) (string, error) + DescribeExportTasks(ctx context.Context, input DescribeExportTasksInput) ([]ExportTask, error) + CancelExportTask(ctx context.Context, taskID string) error } From 785367e2a018ed277d07115e546f8864f61577be Mon Sep 17 00:00:00 2001 From: Nitin Kumar Patel Date: Fri, 23 Oct 2026 07:57:33 +0530 Subject: [PATCH 15/31] fix(aws-eventbridge): partner event sources (#1688) --- docs/coverage/aws/eventbridge.md | 3 + docs/coverage/azure/eventgrid.md | 3 + docs/coverage/gcp/eventarc.md | 3 + docs/coverage/nongoals/eventbus.md | 3 + providers/aws/eventbridge/eventbridge.go | 30 +- providers/aws/eventbridge/partner_sources.go | 532 ++++++++++++++++++ .../aws/eventbridge/partner_sources_test.go | 327 +++++++++++ providers/aws/eventbridge/put_events.go | 15 +- providers/aws/eventbridge/snapshot.go | 5 + server/aws/eventbridge/handler.go | 6 +- server/aws/eventbridge/operations.go | 13 +- server/aws/eventbridge/partner_sources.go | 295 ++++++++++ .../eventbridge/partner_sources_sdk_test.go | 195 +++++++ server/aws/eventbridge/types.go | 7 +- services/eventbus/driver/driver.go | 4 + services/eventbus/driver/partner.go | 44 ++ 16 files changed, 1464 insertions(+), 21 deletions(-) create mode 100644 providers/aws/eventbridge/partner_sources.go create mode 100644 providers/aws/eventbridge/partner_sources_test.go create mode 100644 server/aws/eventbridge/partner_sources.go create mode 100644 server/aws/eventbridge/partner_sources_sdk_test.go create mode 100644 services/eventbus/driver/partner.go diff --git a/docs/coverage/aws/eventbridge.md b/docs/coverage/aws/eventbridge.md index 2bd0cda36..a37c8dad7 100644 --- a/docs/coverage/aws/eventbridge.md +++ b/docs/coverage/aws/eventbridge.md @@ -31,3 +31,6 @@ AWS's `eventbus` service ยท portable interface `driver.EventBus` ยท [AWS index]( - AWS: `PutEvents` with `EndpointId` works against a custom endpoint URL. aws-sdk-go-v2 and the AWS CLI (which needs the bundled `awscrt`) keep the configured endpoint instead of `.endpoint.events.amazonaws.com` and sign the request with SigV4a. cloudemu does not verify SigV4a, so with `--enforce-auth` such a request is refused. A SigV4a signature carries no Region, so `cloudemu serve` handles the request in its default Region (`--region`), and the endpoint must be created there. - AWS: a global endpoint has no second Region behind it. Route 53 health checks are not evaluated, so events never fail over, and event replication to the secondary Region is not performed. `PutEvents` through an endpoint goes to its event bus in the endpoint's home Region: an entry with no `EventBusName`, or one naming either endpoint bus by name or ARN, lands on the bus of that name, and an entry naming another bus goes to that bus. - AWS: `CreateEndpoint` and `UpdateEndpoint` answer `CREATING` and `UPDATING`. Reads then show `ACTIVE`, at once or after a short window with `--async-settle`. `DeleteEndpoint` removes the endpoint at once, without a `DELETING` phase, so `ConcurrentModificationException` and the `*_FAILED` states never occur. +- AWS: partner event sources run in one account. The partner-side operations (`CreatePartnerEventSource`, `PutPartnerEvents` and the rest) are served by the same emulator and account as the customer side, and any 12-digit `Account` is accepted. Only a source created for the emulator's own account shows in `ListEventSources` and `DescribeEventSource`, gets a partner event bus, and has its events delivered; sources for other accounts are visible only to the partner-side operations. +- AWS: a partner event source that stays `PENDING` for two weeks is not deleted. `ExpirationTime` is reported but not enforced. +- AWS: `PutPartnerEvents` for a source that is not `ACTIVE` in this account, or whose partner event bus was deleted, answers with an `EventId` and drops the event. Deleting a partner event bus moves its source back to `PENDING`, and `DeletePartnerEventSource` leaves the partner event bus in place. diff --git a/docs/coverage/azure/eventgrid.md b/docs/coverage/azure/eventgrid.md index 801992be8..d9b6f1541 100644 --- a/docs/coverage/azure/eventgrid.md +++ b/docs/coverage/azure/eventgrid.md @@ -31,3 +31,6 @@ Azure's `eventbus` service ยท portable interface `driver.EventBus` ยท [Azure ind - AWS: `PutEvents` with `EndpointId` works against a custom endpoint URL. aws-sdk-go-v2 and the AWS CLI (which needs the bundled `awscrt`) keep the configured endpoint instead of `.endpoint.events.amazonaws.com` and sign the request with SigV4a. cloudemu does not verify SigV4a, so with `--enforce-auth` such a request is refused. A SigV4a signature carries no Region, so `cloudemu serve` handles the request in its default Region (`--region`), and the endpoint must be created there. - AWS: a global endpoint has no second Region behind it. Route 53 health checks are not evaluated, so events never fail over, and event replication to the secondary Region is not performed. `PutEvents` through an endpoint goes to its event bus in the endpoint's home Region: an entry with no `EventBusName`, or one naming either endpoint bus by name or ARN, lands on the bus of that name, and an entry naming another bus goes to that bus. - AWS: `CreateEndpoint` and `UpdateEndpoint` answer `CREATING` and `UPDATING`. Reads then show `ACTIVE`, at once or after a short window with `--async-settle`. `DeleteEndpoint` removes the endpoint at once, without a `DELETING` phase, so `ConcurrentModificationException` and the `*_FAILED` states never occur. +- AWS: partner event sources run in one account. The partner-side operations (`CreatePartnerEventSource`, `PutPartnerEvents` and the rest) are served by the same emulator and account as the customer side, and any 12-digit `Account` is accepted. Only a source created for the emulator's own account shows in `ListEventSources` and `DescribeEventSource`, gets a partner event bus, and has its events delivered; sources for other accounts are visible only to the partner-side operations. +- AWS: a partner event source that stays `PENDING` for two weeks is not deleted. `ExpirationTime` is reported but not enforced. +- AWS: `PutPartnerEvents` for a source that is not `ACTIVE` in this account, or whose partner event bus was deleted, answers with an `EventId` and drops the event. Deleting a partner event bus moves its source back to `PENDING`, and `DeletePartnerEventSource` leaves the partner event bus in place. diff --git a/docs/coverage/gcp/eventarc.md b/docs/coverage/gcp/eventarc.md index 6d8aebdac..80f07848c 100644 --- a/docs/coverage/gcp/eventarc.md +++ b/docs/coverage/gcp/eventarc.md @@ -31,3 +31,6 @@ GCP's `eventbus` service ยท portable interface `driver.EventBus` ยท [GCP index]( - AWS: `PutEvents` with `EndpointId` works against a custom endpoint URL. aws-sdk-go-v2 and the AWS CLI (which needs the bundled `awscrt`) keep the configured endpoint instead of `.endpoint.events.amazonaws.com` and sign the request with SigV4a. cloudemu does not verify SigV4a, so with `--enforce-auth` such a request is refused. A SigV4a signature carries no Region, so `cloudemu serve` handles the request in its default Region (`--region`), and the endpoint must be created there. - AWS: a global endpoint has no second Region behind it. Route 53 health checks are not evaluated, so events never fail over, and event replication to the secondary Region is not performed. `PutEvents` through an endpoint goes to its event bus in the endpoint's home Region: an entry with no `EventBusName`, or one naming either endpoint bus by name or ARN, lands on the bus of that name, and an entry naming another bus goes to that bus. - AWS: `CreateEndpoint` and `UpdateEndpoint` answer `CREATING` and `UPDATING`. Reads then show `ACTIVE`, at once or after a short window with `--async-settle`. `DeleteEndpoint` removes the endpoint at once, without a `DELETING` phase, so `ConcurrentModificationException` and the `*_FAILED` states never occur. +- AWS: partner event sources run in one account. The partner-side operations (`CreatePartnerEventSource`, `PutPartnerEvents` and the rest) are served by the same emulator and account as the customer side, and any 12-digit `Account` is accepted. Only a source created for the emulator's own account shows in `ListEventSources` and `DescribeEventSource`, gets a partner event bus, and has its events delivered; sources for other accounts are visible only to the partner-side operations. +- AWS: a partner event source that stays `PENDING` for two weeks is not deleted. `ExpirationTime` is reported but not enforced. +- AWS: `PutPartnerEvents` for a source that is not `ACTIVE` in this account, or whose partner event bus was deleted, answers with an `EventId` and drops the event. Deleting a partner event bus moves its source back to `PENDING`, and `DeletePartnerEventSource` leaves the partner event bus in place. diff --git a/docs/coverage/nongoals/eventbus.md b/docs/coverage/nongoals/eventbus.md index caa1dd4cb..2f6be7446 100644 --- a/docs/coverage/nongoals/eventbus.md +++ b/docs/coverage/nongoals/eventbus.md @@ -3,3 +3,6 @@ - AWS: `PutEvents` with `EndpointId` works against a custom endpoint URL. aws-sdk-go-v2 and the AWS CLI (which needs the bundled `awscrt`) keep the configured endpoint instead of `.endpoint.events.amazonaws.com` and sign the request with SigV4a. cloudemu does not verify SigV4a, so with `--enforce-auth` such a request is refused. A SigV4a signature carries no Region, so `cloudemu serve` handles the request in its default Region (`--region`), and the endpoint must be created there. - AWS: a global endpoint has no second Region behind it. Route 53 health checks are not evaluated, so events never fail over, and event replication to the secondary Region is not performed. `PutEvents` through an endpoint goes to its event bus in the endpoint's home Region: an entry with no `EventBusName`, or one naming either endpoint bus by name or ARN, lands on the bus of that name, and an entry naming another bus goes to that bus. - AWS: `CreateEndpoint` and `UpdateEndpoint` answer `CREATING` and `UPDATING`. Reads then show `ACTIVE`, at once or after a short window with `--async-settle`. `DeleteEndpoint` removes the endpoint at once, without a `DELETING` phase, so `ConcurrentModificationException` and the `*_FAILED` states never occur. +- AWS: partner event sources run in one account. The partner-side operations (`CreatePartnerEventSource`, `PutPartnerEvents` and the rest) are served by the same emulator and account as the customer side, and any 12-digit `Account` is accepted. Only a source created for the emulator's own account shows in `ListEventSources` and `DescribeEventSource`, gets a partner event bus, and has its events delivered; sources for other accounts are visible only to the partner-side operations. +- AWS: a partner event source that stays `PENDING` for two weeks is not deleted. `ExpirationTime` is reported but not enforced. +- AWS: `PutPartnerEvents` for a source that is not `ACTIVE` in this account, or whose partner event bus was deleted, answers with an `EventId` and drops the event. Deleting a partner event bus moves its source back to `PENDING`, and `DeletePartnerEventSource` leaves the partner event bus in place. diff --git a/providers/aws/eventbridge/eventbridge.go b/providers/aws/eventbridge/eventbridge.go index 80eef6e1c..015c91969 100644 --- a/providers/aws/eventbridge/eventbridge.go +++ b/providers/aws/eventbridge/eventbridge.go @@ -103,6 +103,7 @@ type Mock struct { connections *memstore.Store[connectionData] apiDestinations *memstore.Store[driver.APIDestination] endpoints *memstore.Store[driver.Endpoint] + partnerSources *memstore.Store[partnerSource] // endpointSettle holds each global endpoint's CREATING/UPDATING window. endpointSettle *settle.Set // secrets holds each connection's credentials; nil leaves SecretArn a bare @@ -173,6 +174,7 @@ func New(opts *config.Options) *Mock { connections: memstore.New[connectionData](), apiDestinations: memstore.New[driver.APIDestination](), endpoints: memstore.New[driver.Endpoint](), + partnerSources: memstore.New[partnerSource](), endpointSettle: settle.NewSet(), httpClient: defaultHTTPClient(), apiDestSlots: make(chan struct{}, maxAPIDestinationDeliveries), @@ -196,14 +198,18 @@ func New(opts *config.Options) *Mock { return m } -// CreateEventBus creates a new EventBridge event bus. +// CreateEventBus creates a new EventBridge event bus. With EventSourceName set +// it creates the partner event bus matched to that partner event source, which +// becomes ACTIVE. func (m *Mock) CreateEventBus(_ context.Context, cfg driver.EventBusConfig) (*driver.EventBusInfo, error) { if cfg.Name == "" { return nil, errors.New(errors.InvalidArgument, "event bus name is required") } - if m.buses.Has(cfg.Name) { - return nil, errors.Newf(errors.AlreadyExists, "event bus %q already exists", cfg.Name) + if cfg.EventSourceName != "" { + if err := m.checkPartnerBus(&cfg); err != nil { + return nil, err + } } busARN := idgen.AWSARN("events", m.opts.Region, m.opts.AccountID, "event-bus/"+cfg.Name) @@ -229,7 +235,18 @@ func (m *Mock) CreateEventBus(_ context.Context, cfg driver.EventBusConfig) (*dr events: []driver.Event{}, } - m.buses.Set(cfg.Name, bd) + if !m.buses.SetIfAbsent(cfg.Name, bd) { + return nil, errors.Newf(errors.AlreadyExists, "event bus %q already exists", cfg.Name) + } + + if cfg.EventSourceName != "" { + // The source may have been deleted since checkPartnerBus looked. + if err := m.transitionOwnSource(cfg.EventSourceName, activate(cfg.EventSourceName)); err != nil { + m.buses.Delete(cfg.Name) + + return nil, err + } + } // Seed the ARN-keyed tag store so create-time tags are visible to // ListTagsForResource, matching real EventBridge. @@ -242,7 +259,8 @@ func (m *Mock) CreateEventBus(_ context.Context, cfg driver.EventBusConfig) (*dr return &result, nil } -// DeleteEventBus deletes an EventBridge event bus. +// DeleteEventBus deletes an EventBridge event bus. Deleting a partner event bus +// moves its event source back to PENDING. func (m *Mock) DeleteEventBus(_ context.Context, name string) error { if name == defaultBusName { return errors.New(errors.InvalidArgument, "cannot delete the default event bus") @@ -252,6 +270,8 @@ func (m *Mock) DeleteEventBus(_ context.Context, name string) error { return errors.Newf(errors.NotFound, "event bus %q not found", name) } + m.releasePartnerSource(name) + return nil } diff --git a/providers/aws/eventbridge/partner_sources.go b/providers/aws/eventbridge/partner_sources.go new file mode 100644 index 000000000..897059b3b --- /dev/null +++ b/providers/aws/eventbridge/partner_sources.go @@ -0,0 +1,532 @@ +package eventbridge + +import ( + "context" + "fmt" + "maps" + "regexp" + "strings" + "time" + + "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/internal/pagination" + "github.com/stackshy/cloudemu/v2/services/eventbus/driver" +) + +// Partner event source states. +const ( + sourceStatePending = "PENDING" + sourceStateActive = "ACTIVE" + sourceStateDeleted = "DELETED" +) + +const ( + maxEventSourceNameLen = 256 + maxSourcePageSize = 100 + maxPartnerEntries = 20 + maxEventResourceLen = 2048 + + // sourcePendingTTL is how long a source may stay PENDING before AWS deletes + // it. It sets ExpirationTime; the expiry itself is not enforced. + sourcePendingTTL = 14 * 24 * time.Hour + + partnerNamePrefix = "aws.partner/" +) + +// Patterns of the partner event source members, as the EventBridge API model +// spells them. +const ( + eventSourceNamePattern = `aws\.partner(/[\.\-_A-Za-z0-9]+){2,}` + accountIDPattern = `\d{12}` + partnerSourcePrefixPatt = `aws\.partner/[\.\-_A-Za-z0-9]+/[/\.\-_A-Za-z0-9]*` + eventSourcePrefixPattern = `[/\.\-_A-Za-z0-9]+` + accountIDLen = 12 + partnerSourceField = "name" + partnerSourceAccountField = "account" +) + +var ( + eventSourceNameRE = regexp.MustCompile(`^aws\.partner(/[.\-_A-Za-z0-9]+){2,}$`) + accountIDRE = regexp.MustCompile(`^\d{12}$`) + partnerSourcePrefixRE = regexp.MustCompile(`^aws\.partner/[.\-_A-Za-z0-9]+/[/.\-_A-Za-z0-9]*$`) + eventSourcePrefixRE = regexp.MustCompile(`^[/.\-_A-Za-z0-9]+$`) +) + +// partnerAccount is a partner event source's state in one customer account. +type partnerAccount struct { + State string `json:"state"` + CreatedAt time.Time `json:"createdAt"` + // PendingSince is when the source last entered PENDING; zero otherwise. + PendingSince time.Time `json:"pendingSince,omitzero"` +} + +// partnerSource is a partner event source and the customer accounts it was +// created for. Accounts is never mutated in place: writers store a copy, so a +// value read from the store stays safe to use. +type partnerSource struct { + Name string `json:"name"` + Accounts map[string]partnerAccount `json:"accounts"` +} + +func errEventSourceNotExist(name string) error { + return errors.Newf(errors.NotFound, "Event source %s does not exist.", name) +} + +func errEventSourceState(name, state string) error { + return errors.Newf(errors.FailedPrecondition, "Event source %s is in state %s.", name, state) +} + +// eventSourceARN is a partner event source's ARN. It has no account segment. +func (m *Mock) eventSourceARN(name string) string { + return "arn:aws:events:" + m.opts.Region + "::event-source/" + name +} + +// partnerOf returns the partner name segment of a source name, as in "acme.com" +// for "aws.partner/acme.com/app/events". +func partnerOf(name string) string { + rest := strings.TrimPrefix(name, partnerNamePrefix) + if i := strings.Index(rest, "/"); i != -1 { + return rest[:i] + } + + return rest +} + +func (v *violations) eventSourceName(value, field string) { + if v.present(value, field) { + v.lengthBetween(value, field, 1, maxEventSourceNameLen) + v.pattern(value, field, eventSourceNameRE, eventSourceNamePattern) + } +} + +func (v *violations) accountID(value, field string) { + if v.present(value, field) { + v.lengthBetween(value, field, accountIDLen, accountIDLen) + v.pattern(value, field, accountIDRE, accountIDPattern) + } +} + +func (v *violations) pageLimit(limit int) { + if limit < 0 || limit > maxSourcePageSize { + v.add(fmt.Sprint(limit), "limit", + fmt.Sprintf("Member must have value less than or equal to %d and greater than or equal to 1", maxSourcePageSize)) + } +} + +func validateSourceName(name string) error { + var v violations + + v.eventSourceName(name, partnerSourceField) + + return v.err() +} + +func validateSourceAndAccount(name, account string) error { + var v violations + + v.eventSourceName(name, partnerSourceField) + v.accountID(account, partnerSourceAccountField) + + return v.err() +} + +// expiresAt is the ExpirationTime of a source in state a: two weeks after it +// entered PENDING, and unset in any other state. +func expiresAt(a *partnerAccount) time.Time { + if a.State != sourceStatePending || a.PendingSince.IsZero() { + return time.Time{} + } + + return a.PendingSince.Add(sourcePendingTTL) +} + +// CreatePartnerEventSource creates a partner event source for a customer +// account, in PENDING. The account may be this emulator's own. A source deleted +// for that account can be created again. +func (m *Mock) CreatePartnerEventSource(_ context.Context, name, account string) (string, error) { + if err := validateSourceAndAccount(name, account); err != nil { + return "", err + } + + now := m.opts.Clock.Now().UTC() + entry := partnerAccount{State: sourceStatePending, CreatedAt: now, PendingSince: now} + + if m.partnerSources.SetIfAbsent(name, partnerSource{Name: name, Accounts: map[string]partnerAccount{account: entry}}) { + return m.eventSourceARN(name), nil + } + + var exists bool + + m.partnerSources.Update(name, func(s partnerSource) partnerSource { + if a, ok := s.Accounts[account]; ok && a.State != sourceStateDeleted { + exists = true + + return s + } + + s.Accounts = maps.Clone(s.Accounts) + s.Accounts[account] = entry + + return s + }) + + if exists { + return "", errors.Newf(errors.AlreadyExists, "Event source %s already exists.", name) + } + + return m.eventSourceARN(name), nil +} + +// DeletePartnerEventSource moves a partner event source to DELETED for one +// customer account. The partner event bus stays, but receives no more events. +// Deleting a source that does not exist for that account succeeds. +func (m *Mock) DeletePartnerEventSource(_ context.Context, name, account string) error { + if err := validateSourceAndAccount(name, account); err != nil { + return err + } + + m.partnerSources.Update(name, func(s partnerSource) partnerSource { + a, ok := s.Accounts[account] + if !ok { + return s + } + + a.State, a.PendingSince = sourceStateDeleted, time.Time{} + s.Accounts = maps.Clone(s.Accounts) + s.Accounts[account] = a + + return s + }) + + return nil +} + +// DescribePartnerEventSource returns a partner event source as its partner sees +// it. +func (m *Mock) DescribePartnerEventSource(_ context.Context, name string) (*driver.PartnerEventSource, error) { + if err := validateSourceName(name); err != nil { + return nil, err + } + + if !m.partnerSources.Has(name) { + return nil, errEventSourceNotExist(name) + } + + return &driver.PartnerEventSource{Name: name, ARN: m.eventSourceARN(name)}, nil +} + +// ListPartnerEventSources lists the partner event sources whose names start +// with the required NamePrefix, sorted by name, one page at a time. +func (m *Mock) ListPartnerEventSources( + _ context.Context, f driver.EventSourceFilter, +) ([]driver.PartnerEventSource, string, error) { + var v violations + + if v.present(f.NamePrefix, "namePrefix") { + v.lengthBetween(f.NamePrefix, "namePrefix", 1, maxEventSourceNameLen) + v.pattern(f.NamePrefix, "namePrefix", partnerSourcePrefixRE, partnerSourcePrefixPatt) + } + + v.pageLimit(f.Limit) + + if err := v.err(); err != nil { + return nil, "", err + } + + var out []driver.PartnerEventSource + + for _, name := range m.partnerSources.Keys() { + if strings.HasPrefix(name, f.NamePrefix) { + out = append(out, driver.PartnerEventSource{Name: name, ARN: m.eventSourceARN(name)}) + } + } + + return pageOf(out, func(a, b driver.PartnerEventSource) bool { return a.Name < b.Name }, f) +} + +// ListPartnerEventSourceAccounts lists the customer accounts a partner event +// source was created for, sorted by account, one page at a time. +func (m *Mock) ListPartnerEventSourceAccounts( + _ context.Context, name string, limit int, nextToken string, +) ([]driver.PartnerEventSourceAccount, string, error) { + var v violations + + v.eventSourceName(name, "eventSourceName") + v.pageLimit(limit) + + if err := v.err(); err != nil { + return nil, "", err + } + + src, ok := m.partnerSources.Get(name) + if !ok { + return nil, "", errEventSourceNotExist(name) + } + + out := make([]driver.PartnerEventSourceAccount, 0, len(src.Accounts)) + + for account, a := range src.Accounts { + out = append(out, driver.PartnerEventSourceAccount{ + Account: account, State: a.State, CreatedAt: a.CreatedAt, ExpiresAt: expiresAt(&a), + }) + } + + return pageOf(out, func(a, b driver.PartnerEventSourceAccount) bool { return a.Account < b.Account }, + driver.EventSourceFilter{Limit: limit, NextToken: nextToken}) +} + +// pageOf sorts items with less and returns the page f asks for. +func pageOf[T any](items []T, less func(a, b T) bool, f driver.EventSourceFilter) (page []T, next string, err error) { + p, perr := pagination.PaginateSorted(items, less, f.NextToken, f.Limit) + if perr != nil { + return nil, "", errors.New(errors.InvalidArgument, msgBadNextPage) + } + + return p.Items, p.NextPageToken, nil +} + +// ownSource returns this account's view of a partner event source. +func (m *Mock) ownSource(name string) (driver.EventSource, bool) { + src, ok := m.partnerSources.Get(name) + if !ok { + return driver.EventSource{}, false + } + + a, ok := src.Accounts[m.opts.AccountID] + if !ok { + return driver.EventSource{}, false + } + + return driver.EventSource{ + Name: name, ARN: m.eventSourceARN(name), CreatedBy: partnerOf(name), + State: a.State, CreatedAt: a.CreatedAt, ExpiresAt: expiresAt(&a), + }, true +} + +// DescribeEventSource returns a partner event source shared with this account. +func (m *Mock) DescribeEventSource(_ context.Context, name string) (*driver.EventSource, error) { + if err := validateSourceName(name); err != nil { + return nil, err + } + + src, ok := m.ownSource(name) + if !ok { + return nil, errEventSourceNotExist(name) + } + + return &src, nil +} + +// ListEventSources lists the partner event sources shared with this account, +// sorted by name, one page at a time. +func (m *Mock) ListEventSources(_ context.Context, f driver.EventSourceFilter) ([]driver.EventSource, string, error) { + var v violations + + if f.NamePrefix != "" { + v.lengthBetween(f.NamePrefix, "namePrefix", 1, maxEventSourceNameLen) + v.pattern(f.NamePrefix, "namePrefix", eventSourcePrefixRE, eventSourcePrefixPattern) + } + + v.pageLimit(f.Limit) + + if err := v.err(); err != nil { + return nil, "", err + } + + var out []driver.EventSource + + for _, name := range m.partnerSources.Keys() { + if !strings.HasPrefix(name, f.NamePrefix) { + continue + } + + if src, ok := m.ownSource(name); ok { + out = append(out, src) + } + } + + return pageOf(out, func(a, b driver.EventSource) bool { return a.Name < b.Name }, f) +} + +// transitionOwnSource applies fn to this account's entry of a partner event +// source in one atomic step. fn reports an error to leave the entry unchanged. +func (m *Mock) transitionOwnSource(name string, fn func(a *partnerAccount) error) error { + notFound := true + + var applyErr error + + m.partnerSources.Update(name, func(s partnerSource) partnerSource { + a, ok := s.Accounts[m.opts.AccountID] + if !ok { + return s + } + + notFound = false + + if applyErr = fn(&a); applyErr != nil { + return s + } + + s.Accounts = maps.Clone(s.Accounts) + s.Accounts[m.opts.AccountID] = a + + return s + }) + + if notFound { + return errEventSourceNotExist(name) + } + + return applyErr +} + +// activate moves a PENDING or ACTIVE source to ACTIVE. +func activate(name string) func(a *partnerAccount) error { + return func(a *partnerAccount) error { + if a.State == sourceStateDeleted { + return errEventSourceState(name, a.State) + } + + a.State, a.PendingSince = sourceStateActive, time.Time{} + + return nil + } +} + +// ActivateEventSource moves a partner event source shared with this account +// from PENDING to ACTIVE, so its partner event bus receives events again. +func (m *Mock) ActivateEventSource(_ context.Context, name string) error { + if err := validateSourceName(name); err != nil { + return err + } + + return m.transitionOwnSource(name, activate(name)) +} + +// DeactivateEventSource moves a partner event source shared with this account +// from ACTIVE to PENDING. Its partner event bus stays but receives no events. +func (m *Mock) DeactivateEventSource(_ context.Context, name string) error { + if err := validateSourceName(name); err != nil { + return err + } + + now := m.opts.Clock.Now().UTC() + + return m.transitionOwnSource(name, func(a *partnerAccount) error { + switch a.State { + case sourceStateDeleted: + return errEventSourceState(name, a.State) + case sourceStateActive: + a.State, a.PendingSince = sourceStatePending, now + } + + return nil + }) +} + +// checkPartnerBus validates a CreateEventBus for a partner event bus: the bus +// is named after its event source, which must be shared with this account and +// not deleted. +func (m *Mock) checkPartnerBus(cfg *driver.EventBusConfig) error { + var v violations + + v.eventSourceName(cfg.EventSourceName, "eventSourceName") + + if err := v.err(); err != nil { + return err + } + + if cfg.Name != cfg.EventSourceName { + return errInvalidParam("Name", "Event bus name must match the event source name "+cfg.EventSourceName+".") + } + + src, ok := m.ownSource(cfg.EventSourceName) + if !ok { + return errEventSourceNotExist(cfg.EventSourceName) + } + + if src.State == sourceStateDeleted { + return errEventSourceState(src.Name, src.State) + } + + return nil +} + +// releasePartnerSource moves the event source a deleted partner event bus was +// matched to back to PENDING. +func (m *Mock) releasePartnerSource(busName string) { + if !strings.HasPrefix(busName, partnerNamePrefix) { + return + } + + now := m.opts.Clock.Now().UTC() + + _ = m.transitionOwnSource(busName, func(a *partnerAccount) error { + if a.State == sourceStateActive { + a.State, a.PendingSince = sourceStatePending, now + } + + return nil + }) +} + +// partnerEntriesError checks the request-wide limits of a PutPartnerEvents +// call: 1 to 20 entries, a total size under the Region's limit, and each Source +// and Resource within the API model's constraints. +func partnerEntriesError(events []driver.Event, region string) error { + if err := validateEntriesRequest(events, region, maxPartnerEntries); err != nil { + return err + } + + var v violations + + for i := range events { + field := fmt.Sprintf("entries.%d.member.", i+1) + + if s := events[i].Source; s != "" { + v.lengthBetween(s, field+"source", 1, maxEventSourceNameLen) + v.pattern(s, field+"source", eventSourceNameRE, eventSourceNamePattern) + } + + for j, r := range events[i].Resources { + v.maxLen(r, fmt.Sprintf("%sresources.%d.member", field, j+1), maxEventResourceLen) + } + } + + return v.err() +} + +// PutPartnerEventEntries publishes events sent by a SaaS partner, reporting each +// entry's outcome by index. An entry goes to the partner event bus named after +// its Source when that source is ACTIVE for this account. Otherwise it still +// gets an EventId but is dropped, as PutEvents does for a bus that does not +// exist. +func (m *Mock) PutPartnerEventEntries(ctx context.Context, events []driver.Event) ([]driver.PutEventsEntryResult, error) { + if err := partnerEntriesError(events, m.opts.Region); err != nil { + return nil, err + } + + results := make([]driver.PutEventsEntryResult, len(events)) + + for i := range events { + if code, msg := validateEntry(&events[i]); code != "" { + results[i] = driver.PutEventsEntryResult{ErrorCode: code, ErrorMessage: msg} + + continue + } + + results[i] = driver.PutEventsEntryResult{EventID: m.publishPartnerEvent(ctx, &events[i], i)} + } + + return results, nil +} + +func (m *Mock) publishPartnerEvent(ctx context.Context, event *driver.Event, index int) string { + if src, ok := m.ownSource(event.Source); ok && src.State == sourceStateActive { + event.EventBus = event.Source + + return m.publishEvent(ctx, event, index) + } + + return generateEventID(event, m.opts.Clock.Now(), index) +} diff --git a/providers/aws/eventbridge/partner_sources_test.go b/providers/aws/eventbridge/partner_sources_test.go new file mode 100644 index 000000000..98e9463e8 --- /dev/null +++ b/providers/aws/eventbridge/partner_sources_test.go @@ -0,0 +1,327 @@ +package eventbridge + +import ( + "context" + "strings" + "testing" + "time" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/services/eventbus/driver" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const ( + testSource = "aws.partner/acme.com/app/events" + testAccount = "123456789012" + otherAccount = "210987654321" + testSourceARN = "arn:aws:events:us-east-1::event-source/" + testSource + testPartnerQ = "arn:aws:sqs:us-east-1:123456789012:partner" + partnerPattern = `{"source":[{"prefix":"aws.partner/"}]}` +) + +func partnerEvent(source string) driver.Event { + return driver.Event{Source: source, DetailType: "t", Detail: `{"k":1}`} +} + +func requireSourceState(t *testing.T, m *Mock, want string) *driver.EventSource { + t.Helper() + + got, err := m.DescribeEventSource(context.Background(), testSource) + require.NoError(t, err) + assert.Equal(t, want, got.State) + + return got +} + +// TestPartnerEventSourceLifecycle drives a partner source through PENDING, +// ACTIVE (partner event bus), PENDING (deactivate), ACTIVE and DELETED. +func TestPartnerEventSourceLifecycle(t *testing.T) { + ctx := context.Background() + m, fc := newTestMock() + sqs := &recordingSQS{} + m.SetSQSDeliverer(sqs) + + arn, err := m.CreatePartnerEventSource(ctx, testSource, testAccount) + require.NoError(t, err) + assert.Equal(t, testSourceARN, arn) + + src := requireSourceState(t, m, sourceStatePending) + assert.Equal(t, "acme.com", src.CreatedBy) + assert.Equal(t, testSourceARN, src.ARN) + assert.Equal(t, src.CreatedAt.Add(14*24*time.Hour), src.ExpiresAt) + + fc.Advance(time.Minute) + + _, err = m.CreateEventBus(ctx, driver.EventBusConfig{Name: testSource, EventSourceName: testSource}) + require.NoError(t, err) + + src = requireSourceState(t, m, sourceStateActive) + assert.True(t, src.ExpiresAt.IsZero(), "an ACTIVE source has no expiration time") + + _, err = m.PutRule(ctx, &driver.RuleConfig{Name: "r", EventBus: testSource, EventPattern: partnerPattern}) + require.NoError(t, err) + require.NoError(t, m.PutTargets(ctx, testSource, "r", []driver.Target{{ID: "q", ARN: testPartnerQ}})) + + res, err := m.PutPartnerEventEntries(ctx, []driver.Event{partnerEvent(testSource)}) + require.NoError(t, err) + require.Len(t, res, 1) + assert.NotEmpty(t, res[0].EventID) + + got := sqs.got(testPartnerQ) + require.Len(t, got, 1) + assert.Equal(t, testSource, got[0]["source"]) + + require.NoError(t, m.DeactivateEventSource(ctx, testSource)) + src = requireSourceState(t, m, sourceStatePending) + assert.Equal(t, fc.Now().UTC().Add(14*24*time.Hour), src.ExpiresAt, "expiry counts from deactivation") + + res, err = m.PutPartnerEventEntries(ctx, []driver.Event{partnerEvent(testSource)}) + require.NoError(t, err) + assert.NotEmpty(t, res[0].EventID, "an event for a PENDING source is accepted") + assert.Len(t, sqs.got(testPartnerQ), 1, "and dropped") + + require.NoError(t, m.ActivateEventSource(ctx, testSource)) + requireSourceState(t, m, sourceStateActive) + + require.NoError(t, m.DeletePartnerEventSource(ctx, testSource, testAccount)) + requireSourceState(t, m, sourceStateDeleted) + + _, err = m.PutPartnerEventEntries(ctx, []driver.Event{partnerEvent(testSource)}) + require.NoError(t, err) + assert.Len(t, sqs.got(testPartnerQ), 1, "a DELETED source delivers nothing") + + for _, op := range []func(context.Context, string) error{m.ActivateEventSource, m.DeactivateEventSource} { + err = op(ctx, testSource) + assert.True(t, cerrors.IsFailedPrecondition(err), "got %v", err) + assert.Equal(t, "Event source "+testSource+" is in state DELETED.", cerrors.Message(err)) + } + + _, err = m.GetEventBus(ctx, testSource) + require.NoError(t, err, "deleting the source leaves the partner event bus") + + _, err = m.CreatePartnerEventSource(ctx, testSource, testAccount) + require.NoError(t, err, "a deleted source can be created again") + requireSourceState(t, m, sourceStatePending) +} + +func TestPartnerEventSourcePartnerSide(t *testing.T) { + ctx := context.Background() + m, _ := newTestMock() + + _, err := m.CreatePartnerEventSource(ctx, testSource, testAccount) + require.NoError(t, err) + + _, err = m.CreatePartnerEventSource(ctx, testSource, testAccount) + assert.True(t, cerrors.IsAlreadyExists(err)) + assert.Equal(t, "Event source "+testSource+" already exists.", cerrors.Message(err)) + + _, err = m.CreatePartnerEventSource(ctx, testSource, otherAccount) + require.NoError(t, err) + + _, err = m.CreatePartnerEventSource(ctx, "aws.partner/acme.com/other", otherAccount) + require.NoError(t, err) + + got, err := m.DescribePartnerEventSource(ctx, testSource) + require.NoError(t, err) + assert.Equal(t, driver.PartnerEventSource{Name: testSource, ARN: testSourceARN}, *got) + + _, err = m.DescribePartnerEventSource(ctx, "aws.partner/acme.com/nope") + assert.True(t, cerrors.IsNotFound(err)) + + list, next, err := m.ListPartnerEventSources(ctx, driver.EventSourceFilter{NamePrefix: "aws.partner/acme.com/", Limit: 1}) + require.NoError(t, err) + require.Len(t, list, 1) + assert.Equal(t, "aws.partner/acme.com/app/events", list[0].Name) + require.NotEmpty(t, next) + + list, next, err = m.ListPartnerEventSources(ctx, driver.EventSourceFilter{NamePrefix: "aws.partner/acme.com/", NextToken: next}) + require.NoError(t, err) + require.Len(t, list, 1) + assert.Equal(t, "aws.partner/acme.com/other", list[0].Name) + assert.Empty(t, next) + + accounts, _, err := m.ListPartnerEventSourceAccounts(ctx, testSource, 0, "") + require.NoError(t, err) + require.Len(t, accounts, 2) + assert.Equal(t, testAccount, accounts[0].Account) + assert.Equal(t, otherAccount, accounts[1].Account) + assert.Equal(t, sourceStatePending, accounts[1].State) + + _, _, err = m.ListPartnerEventSourceAccounts(ctx, "aws.partner/acme.com/nope", 0, "") + assert.True(t, cerrors.IsNotFound(err)) + + require.NoError(t, m.DeletePartnerEventSource(ctx, testSource, otherAccount)) + require.NoError(t, m.DeletePartnerEventSource(ctx, "aws.partner/acme.com/nope", otherAccount), "delete is idempotent") + + accounts, _, err = m.ListPartnerEventSourceAccounts(ctx, testSource, 0, "") + require.NoError(t, err) + assert.Equal(t, sourceStateDeleted, accounts[1].State) + assert.True(t, accounts[1].ExpiresAt.IsZero()) + assert.Equal(t, sourceStatePending, accounts[0].State, "other accounts keep their state") +} + +// TestEventSourceCustomerView proves the customer side sees only sources +// created for this account. +func TestEventSourceCustomerView(t *testing.T) { + ctx := context.Background() + m, _ := newTestMock() + + _, err := m.CreatePartnerEventSource(ctx, testSource, testAccount) + require.NoError(t, err) + + _, err = m.CreatePartnerEventSource(ctx, "aws.partner/acme.com/elsewhere", otherAccount) + require.NoError(t, err) + + list, _, err := m.ListEventSources(ctx, driver.EventSourceFilter{}) + require.NoError(t, err) + require.Len(t, list, 1) + assert.Equal(t, testSource, list[0].Name) + + list, _, err = m.ListEventSources(ctx, driver.EventSourceFilter{NamePrefix: "aws.partner/other"}) + require.NoError(t, err) + assert.Empty(t, list) + + _, err = m.DescribeEventSource(ctx, "aws.partner/acme.com/elsewhere") + assert.True(t, cerrors.IsNotFound(err), "a source for another account is invisible") + assert.Equal(t, "Event source aws.partner/acme.com/elsewhere does not exist.", cerrors.Message(err)) + + for _, op := range []func(context.Context, string) error{m.ActivateEventSource, m.DeactivateEventSource} { + assert.True(t, cerrors.IsNotFound(op(ctx, "aws.partner/acme.com/elsewhere"))) + } +} + +func TestPartnerEventBusCreateErrors(t *testing.T) { + ctx := context.Background() + m, _ := newTestMock() + + _, err := m.CreatePartnerEventSource(ctx, testSource, testAccount) + require.NoError(t, err) + + _, err = m.CreateEventBus(ctx, driver.EventBusConfig{Name: "custom", EventSourceName: testSource}) + assert.True(t, cerrors.IsInvalidArgument(err), "the bus must be named after its source: %v", err) + + _, err = m.CreateEventBus(ctx, driver.EventBusConfig{Name: "aws.partner/acme.com/x", EventSourceName: "aws.partner/acme.com/x"}) + assert.True(t, cerrors.IsNotFound(err)) + + _, err = m.CreateEventBus(ctx, driver.EventBusConfig{Name: "bad", EventSourceName: "bad"}) + assert.True(t, cerrors.IsInvalidArgument(err)) + assert.Contains(t, cerrors.Message(err), "at 'eventSourceName' failed to satisfy constraint") + + _, err = m.CreateEventBus(ctx, driver.EventBusConfig{Name: testSource, EventSourceName: testSource}) + require.NoError(t, err) + + _, err = m.CreateEventBus(ctx, driver.EventBusConfig{Name: testSource, EventSourceName: testSource}) + assert.True(t, cerrors.IsAlreadyExists(err)) + + require.NoError(t, m.DeleteEventBus(ctx, testSource)) + requireSourceState(t, m, sourceStatePending) + + require.NoError(t, m.DeletePartnerEventSource(ctx, testSource, testAccount)) + + _, err = m.CreateEventBus(ctx, driver.EventBusConfig{Name: testSource, EventSourceName: testSource}) + assert.True(t, cerrors.IsFailedPrecondition(err), "a DELETED source cannot get a bus: %v", err) + + _, err = m.GetEventBus(ctx, testSource) + assert.True(t, cerrors.IsNotFound(err)) +} + +func TestPartnerSourceValidation(t *testing.T) { + ctx := context.Background() + m, _ := newTestMock() + + _, err := m.CreatePartnerEventSource(ctx, "aws.partner/acme.com", "12") + require.Error(t, err) + assert.Equal(t, "3 validation errors detected: "+ + "Value 'aws.partner/acme.com' at 'name' failed to satisfy constraint: Member must satisfy regular expression pattern: "+ + eventSourceNamePattern+"; "+ + "Value '12' at 'account' failed to satisfy constraint: Member must have length greater than or equal to 12; "+ + "Value '12' at 'account' failed to satisfy constraint: Member must satisfy regular expression pattern: \\d{12}", + cerrors.Message(err)) + + _, err = m.CreatePartnerEventSource(ctx, "", "") + assert.Equal(t, "2 validation errors detected: Value null at 'name' failed to satisfy constraint: Member must not be null; "+ + "Value null at 'account' failed to satisfy constraint: Member must not be null", cerrors.Message(err)) + + _, _, err = m.ListPartnerEventSources(ctx, driver.EventSourceFilter{}) + assert.True(t, cerrors.IsInvalidArgument(err), "NamePrefix is required") + + _, _, err = m.ListPartnerEventSources(ctx, driver.EventSourceFilter{NamePrefix: "acme"}) + assert.True(t, cerrors.IsInvalidArgument(err)) + + _, _, err = m.ListEventSources(ctx, driver.EventSourceFilter{Limit: 101}) + assert.Contains(t, cerrors.Message(err), "at 'limit' failed to satisfy constraint") + + _, _, err = m.ListEventSources(ctx, driver.EventSourceFilter{NextToken: "garbage"}) + assert.Equal(t, msgBadNextPage, cerrors.Message(err)) + + _, err = m.DescribeEventSource(ctx, "aws.partner/x") + assert.True(t, cerrors.IsInvalidArgument(err)) +} + +func TestPutPartnerEventsValidation(t *testing.T) { + ctx := context.Background() + m, _ := newTestMock() + + _, err := m.PutPartnerEventEntries(ctx, nil) + assert.Equal(t, "1 validation error detected: Value at 'entries' failed to satisfy constraint: "+ + "Member must have length less than or equal to 20 and greater than or equal to 1.", cerrors.Message(err)) + + _, err = m.PutPartnerEventEntries(ctx, make([]driver.Event, 21)) + assert.True(t, cerrors.IsInvalidArgument(err)) + + _, err = m.PutPartnerEventEntries(ctx, []driver.Event{partnerEvent("my.app")}) + assert.Equal(t, "1 validation error detected: Value 'my.app' at 'entries.1.member.source' failed to satisfy "+ + "constraint: Member must satisfy regular expression pattern: "+eventSourceNamePattern, cerrors.Message(err)) + + big := partnerEvent(testSource) + big.Detail = `{"k":"` + strings.Repeat("x", 1<<20) + `"}` + _, err = m.PutPartnerEventEntries(ctx, []driver.Event{big}) + assert.Equal(t, putEventsSizeMessage, cerrors.Message(err)) + + res, err := m.PutPartnerEventEntries(ctx, []driver.Event{ + {DetailType: "t", Detail: "{}"}, + {Source: testSource, Detail: "{}"}, + {Source: testSource, DetailType: "t", Detail: "[]"}, + partnerEvent(testSource), + }) + require.NoError(t, err) + assert.Equal(t, errCodeInvalidArgument, res[0].ErrorCode) + assert.Equal(t, msgDetailTypeRequired, res[1].ErrorMessage) + assert.Equal(t, errCodeMalformedDetail, res[2].ErrorCode) + assert.NotEmpty(t, res[3].EventID, "an unknown source is accepted and dropped") +} + +func TestSnapshotPartnerSources(t *testing.T) { + ctx := context.Background() + src, _ := newTestMock() + + _, err := src.CreatePartnerEventSource(ctx, testSource, testAccount) + require.NoError(t, err) + + _, err = src.CreateEventBus(ctx, driver.EventBusConfig{Name: testSource, EventSourceName: testSource}) + require.NoError(t, err) + + _, err = src.CreatePartnerEventSource(ctx, "aws.partner/acme.com/b", otherAccount) + require.NoError(t, err) + + data, err := src.Snapshot(ctx, true) + require.NoError(t, err) + + dst, _ := newTestMock() + require.NoError(t, dst.Restore(ctx, data)) + + data2, err := dst.Snapshot(ctx, true) + require.NoError(t, err) + assert.JSONEq(t, string(data), string(data2)) + + got, err := dst.DescribeEventSource(ctx, testSource) + require.NoError(t, err) + assert.Equal(t, sourceStateActive, got.State) + + accounts, _, err := dst.ListPartnerEventSourceAccounts(ctx, "aws.partner/acme.com/b", 0, "") + require.NoError(t, err) + require.Len(t, accounts, 1) + assert.Equal(t, otherAccount, accounts[0].Account) +} diff --git a/providers/aws/eventbridge/put_events.go b/providers/aws/eventbridge/put_events.go index d7049fb52..b32dfecdb 100644 --- a/providers/aws/eventbridge/put_events.go +++ b/providers/aws/eventbridge/put_events.go @@ -29,8 +29,6 @@ const ( ) const ( - putEventsEntryCountMessage = "1 validation error detected: Value at 'entries' failed to satisfy " + - "constraint: Member must have length less than or equal to 10 and greater than or equal to 1." putEventsSizeMessage = "Total size of the entries in the request is over the limit." msgSourceRequired = "Parameter Source is not valid. Reason: Source is a required argument." @@ -75,11 +73,12 @@ func entrySize(e *driver.Event) int { return size } -// validatePutEventsRequest applies the request-wide limits that fail the whole -// call: 1 to 10 entries and a total size under the region's limit. -func validatePutEventsRequest(events []driver.Event, region string) error { - if n := len(events); n < 1 || n > maxPutEventsEntries { - return errors.New(errors.InvalidArgument, putEventsEntryCountMessage) +// validateEntriesRequest applies the request-wide limits that fail the whole +// call: 1 to maxEntries entries and a total size under the region's limit. +func validateEntriesRequest(events []driver.Event, region string, maxEntries int) error { + if n := len(events); n < 1 || n > maxEntries { + return errors.Newf(errors.InvalidArgument, "1 validation error detected: Value at 'entries' failed to satisfy "+ + "constraint: Member must have length less than or equal to %d and greater than or equal to 1.", maxEntries) } total := 0 @@ -125,7 +124,7 @@ func isJSONObject(s string) bool { // addressed to a bus that does not exist still gets an EventId and counts as a // success, but is dropped: that is what real EventBridge does. func (m *Mock) PutEventEntries(ctx context.Context, events []driver.Event) ([]driver.PutEventsEntryResult, error) { - if err := validatePutEventsRequest(events, m.opts.Region); err != nil { + if err := validateEntriesRequest(events, m.opts.Region, maxPutEventsEntries); err != nil { return nil, err } diff --git a/providers/aws/eventbridge/snapshot.go b/providers/aws/eventbridge/snapshot.go index ac05141f5..036ed6512 100644 --- a/providers/aws/eventbridge/snapshot.go +++ b/providers/aws/eventbridge/snapshot.go @@ -29,6 +29,7 @@ type ebSnapshot struct { Connections json.RawMessage `json:"connections,omitempty"` APIDestinations json.RawMessage `json:"apiDestinations,omitempty"` Endpoints json.RawMessage `json:"endpoints,omitempty"` + PartnerSources json.RawMessage `json:"partnerSources,omitempty"` } // archiveSnapshot mirrors archiveData, promoting its unexported fields. @@ -184,6 +185,10 @@ func (m *Mock) valueStores(snap *ebSnapshot) []snapshottedStore { m.apiDestinations.LoadSnapshot, &snap.APIDestinations, }, {"endpoints", m.endpoints.Len, m.endpoints.Snapshot, m.endpoints.LoadSnapshot, &snap.Endpoints}, + { + "partner event sources", m.partnerSources.Len, m.partnerSources.Snapshot, + m.partnerSources.LoadSnapshot, &snap.PartnerSources, + }, } } diff --git a/server/aws/eventbridge/handler.go b/server/aws/eventbridge/handler.go index b77b6b93a..f5a7641de 100644 --- a/server/aws/eventbridge/handler.go +++ b/server/aws/eventbridge/handler.go @@ -93,10 +93,12 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } // serveExtendedOp serves the operations outside the main switch (archives, -// replays, connections, API destinations and global endpoints) and answers +// replays, connections, API destinations, global endpoints and partner event +// sources) and answers // UnknownOperationException for anything else. func (h *Handler) serveExtendedOp(w http.ResponseWriter, r *http.Request, op string) { - if h.serveArchiveOp(w, r, op) || h.serveConnectionOp(w, r, op) || h.serveEndpointOp(w, r, op) { + if h.serveArchiveOp(w, r, op) || h.serveConnectionOp(w, r, op) || h.serveEndpointOp(w, r, op) || + h.servePartnerSourceOp(w, r, op) { return } diff --git a/server/aws/eventbridge/operations.go b/server/aws/eventbridge/operations.go index 4f65c69de..47e742453 100644 --- a/server/aws/eventbridge/operations.go +++ b/server/aws/eventbridge/operations.go @@ -35,9 +35,10 @@ func (h *Handler) createEventBus(w http.ResponseWriter, r *http.Request) { } info, err := h.bus.CreateEventBus(r.Context(), ebdriver.EventBusConfig{ - Name: req.Name, - Tags: tagsToMap(req.Tags), - Description: req.Description, + Name: req.Name, + Tags: tagsToMap(req.Tags), + Description: req.Description, + EventSourceName: req.EventSourceName, }) if err != nil { writeErr(w, err) @@ -339,6 +340,12 @@ func (h *Handler) putEvents(w http.ResponseWriter, r *http.Request) { return } + writeEntryResults(w, results) +} + +// writeEntryResults answers a PutEvents or PutPartnerEvents call with each +// entry's outcome and the count of failed entries. +func writeEntryResults(w http.ResponseWriter, results []ebdriver.PutEventsEntryResult) { resp := putEventsResponse{Entries: make([]putEventsResultEntry, len(results))} for i, res := range results { resp.Entries[i] = putEventsResultEntry{EventID: res.EventID, ErrorCode: res.ErrorCode, ErrorMessage: res.ErrorMessage} diff --git a/server/aws/eventbridge/partner_sources.go b/server/aws/eventbridge/partner_sources.go new file mode 100644 index 000000000..ac5c88fb2 --- /dev/null +++ b/server/aws/eventbridge/partner_sources.go @@ -0,0 +1,295 @@ +package eventbridge + +import ( + "context" + "net/http" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/server/wire" + ebdriver "github.com/stackshy/cloudemu/v2/services/eventbus/driver" +) + +// partnerSourceManager is the AWS-specific EventBridge partner event source +// surface, both the SaaS partner side and the customer side, asserted against +// the provider (not part of the portable EventBus driver). +type partnerSourceManager interface { + CreatePartnerEventSource(ctx context.Context, name, account string) (string, error) + DeletePartnerEventSource(ctx context.Context, name, account string) error + DescribePartnerEventSource(ctx context.Context, name string) (*ebdriver.PartnerEventSource, error) + ListPartnerEventSources(ctx context.Context, f ebdriver.EventSourceFilter) ([]ebdriver.PartnerEventSource, string, error) + ListPartnerEventSourceAccounts( + ctx context.Context, name string, limit int, nextToken string, + ) ([]ebdriver.PartnerEventSourceAccount, string, error) + PutPartnerEventEntries(ctx context.Context, events []ebdriver.Event) ([]ebdriver.PutEventsEntryResult, error) + DescribeEventSource(ctx context.Context, name string) (*ebdriver.EventSource, error) + ListEventSources(ctx context.Context, f ebdriver.EventSourceFilter) ([]ebdriver.EventSource, string, error) + ActivateEventSource(ctx context.Context, name string) error + DeactivateEventSource(ctx context.Context, name string) error +} + +type partnerSourceOp func(mgr partnerSourceManager, w http.ResponseWriter, r *http.Request) + +func partnerSourceOps() map[string]partnerSourceOp { + return map[string]partnerSourceOp{ + "CreatePartnerEventSource": createPartnerEventSource, + "DeletePartnerEventSource": deletePartnerEventSource, + "DescribePartnerEventSource": describePartnerEventSource, + "ListPartnerEventSources": listPartnerEventSources, + "ListPartnerEventSourceAccounts": listPartnerEventSourceAccounts, + "PutPartnerEvents": putPartnerEvents, + "DescribeEventSource": describeEventSource, + "ListEventSources": listEventSources, + "ActivateEventSource": sourceAction(partnerSourceManager.ActivateEventSource), + "DeactivateEventSource": sourceAction(partnerSourceManager.DeactivateEventSource), + } +} + +// servePartnerSourceOp serves op when it is a partner event source operation, +// reporting whether it did. +func (h *Handler) servePartnerSourceOp(w http.ResponseWriter, r *http.Request, op string) bool { + fn, ok := partnerSourceOps()[op] + if !ok { + return false + } + + mgr, ok := h.bus.(partnerSourceManager) + if !ok { + writeErr(w, cerrors.New(cerrors.Unimplemented, "partner event sources not supported")) + return true + } + + fn(mgr, w, r) + + return true +} + +type partnerSourceRequest struct { + Name string `json:"Name"` + Account string `json:"Account"` +} + +type listSourcesRequest struct { + NamePrefix string `json:"NamePrefix"` + EventSourceName string `json:"EventSourceName"` + Limit int `json:"Limit"` + NextToken string `json:"NextToken"` +} + +type partnerSourceJSON struct { + Arn string `json:"Arn"` + Name string `json:"Name"` +} + +type partnerAccountJSON struct { + Account string `json:"Account"` + CreationTime *float64 `json:"CreationTime,omitempty"` + ExpirationTime *float64 `json:"ExpirationTime,omitempty"` + State string `json:"State"` +} + +type eventSourceJSON struct { + Arn string `json:"Arn"` + CreatedBy string `json:"CreatedBy"` + CreationTime *float64 `json:"CreationTime,omitempty"` + ExpirationTime *float64 `json:"ExpirationTime,omitempty"` + Name string `json:"Name"` + State string `json:"State"` +} + +type putPartnerEventsEntry struct { + Source string `json:"Source"` + DetailType string `json:"DetailType"` + Detail string `json:"Detail"` + Resources []string `json:"Resources"` + Time *float64 `json:"Time"` +} + +type putPartnerEventsRequest struct { + Entries []putPartnerEventsEntry `json:"Entries"` +} + +func toEventSourceJSON(s *ebdriver.EventSource) eventSourceJSON { + return eventSourceJSON{ + Arn: s.ARN, CreatedBy: s.CreatedBy, Name: s.Name, State: s.State, + CreationTime: timeToEpoch(s.CreatedAt), ExpirationTime: timeToEpoch(s.ExpiresAt), + } +} + +func createPartnerEventSource(mgr partnerSourceManager, w http.ResponseWriter, r *http.Request) { + var req partnerSourceRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + arn, err := mgr.CreatePartnerEventSource(r.Context(), req.Name, req.Account) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, struct { + EventSourceArn string `json:"EventSourceArn"` + }{arn}) +} + +func deletePartnerEventSource(mgr partnerSourceManager, w http.ResponseWriter, r *http.Request) { + var req partnerSourceRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + if err := mgr.DeletePartnerEventSource(r.Context(), req.Name, req.Account); err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, struct{}{}) +} + +func describePartnerEventSource(mgr partnerSourceManager, w http.ResponseWriter, r *http.Request) { + var req partnerSourceRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + src, err := mgr.DescribePartnerEventSource(r.Context(), req.Name) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, partnerSourceJSON{Arn: src.ARN, Name: src.Name}) +} + +func listPartnerEventSources(mgr partnerSourceManager, w http.ResponseWriter, r *http.Request) { + var req listSourcesRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + items, next, err := mgr.ListPartnerEventSources(r.Context(), ebdriver.EventSourceFilter{ + NamePrefix: req.NamePrefix, Limit: req.Limit, NextToken: req.NextToken, + }) + if err != nil { + writeErr(w, err) + return + } + + out := make([]partnerSourceJSON, len(items)) + for i := range items { + out[i] = partnerSourceJSON{Arn: items[i].ARN, Name: items[i].Name} + } + + wire.WriteJSON(w, struct { + PartnerEventSources []partnerSourceJSON `json:"PartnerEventSources"` + NextToken string `json:"NextToken,omitempty"` + }{out, next}) +} + +func listPartnerEventSourceAccounts(mgr partnerSourceManager, w http.ResponseWriter, r *http.Request) { + var req listSourcesRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + items, next, err := mgr.ListPartnerEventSourceAccounts(r.Context(), req.EventSourceName, req.Limit, req.NextToken) + if err != nil { + writeErr(w, err) + return + } + + out := make([]partnerAccountJSON, len(items)) + for i := range items { + out[i] = partnerAccountJSON{ + Account: items[i].Account, State: items[i].State, + CreationTime: timeToEpoch(items[i].CreatedAt), ExpirationTime: timeToEpoch(items[i].ExpiresAt), + } + } + + wire.WriteJSON(w, struct { + PartnerEventSourceAccounts []partnerAccountJSON `json:"PartnerEventSourceAccounts"` + NextToken string `json:"NextToken,omitempty"` + }{out, next}) +} + +func putPartnerEvents(mgr partnerSourceManager, w http.ResponseWriter, r *http.Request) { + var req putPartnerEventsRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + events := make([]ebdriver.Event, len(req.Entries)) + + for i := range req.Entries { + e := &req.Entries[i] + events[i] = ebdriver.Event{ + Source: e.Source, DetailType: e.DetailType, Detail: e.Detail, Resources: e.Resources, Time: entryTime(e.Time), + } + } + + results, err := mgr.PutPartnerEventEntries(r.Context(), events) + if err != nil { + writeErr(w, err) + return + } + + writeEntryResults(w, results) +} + +func describeEventSource(mgr partnerSourceManager, w http.ResponseWriter, r *http.Request) { + var req partnerSourceRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + src, err := mgr.DescribeEventSource(r.Context(), req.Name) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, toEventSourceJSON(src)) +} + +func listEventSources(mgr partnerSourceManager, w http.ResponseWriter, r *http.Request) { + var req listSourcesRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + items, next, err := mgr.ListEventSources(r.Context(), ebdriver.EventSourceFilter{ + NamePrefix: req.NamePrefix, Limit: req.Limit, NextToken: req.NextToken, + }) + if err != nil { + writeErr(w, err) + return + } + + out := make([]eventSourceJSON, len(items)) + for i := range items { + out[i] = toEventSourceJSON(&items[i]) + } + + wire.WriteJSON(w, struct { + EventSources []eventSourceJSON `json:"EventSources"` + NextToken string `json:"NextToken,omitempty"` + }{out, next}) +} + +// sourceAction serves an operation that takes only the event source name and +// answers an empty body. +func sourceAction(act func(partnerSourceManager, context.Context, string) error) partnerSourceOp { + return func(mgr partnerSourceManager, w http.ResponseWriter, r *http.Request) { + var req partnerSourceRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + if err := act(mgr, r.Context(), req.Name); err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, struct{}{}) + } +} diff --git a/server/aws/eventbridge/partner_sources_sdk_test.go b/server/aws/eventbridge/partner_sources_sdk_test.go new file mode 100644 index 000000000..4a86de5b8 --- /dev/null +++ b/server/aws/eventbridge/partner_sources_sdk_test.go @@ -0,0 +1,195 @@ +package eventbridge_test + +import ( + "context" + "encoding/json" + "testing" + + "github.com/aws/aws-sdk-go-v2/aws" + awseb "github.com/aws/aws-sdk-go-v2/service/eventbridge" + ebtypes "github.com/aws/aws-sdk-go-v2/service/eventbridge/types" +) + +const sdkSource = "aws.partner/acme.com/app/events" + +func sourceState(t *testing.T, eb *awseb.Client) ebtypes.EventSourceState { + t.Helper() + + out, err := eb.DescribeEventSource(context.Background(), &awseb.DescribeEventSourceInput{Name: aws.String(sdkSource)}) + if err != nil { + t.Fatalf("DescribeEventSource: %v", err) + } + + if aws.ToString(out.CreatedBy) != "acme.com" || aws.ToString(out.Arn) != "arn:aws:events:us-east-1::event-source/"+sdkSource || + out.CreationTime == nil { + t.Fatalf("DescribeEventSource = %+v", out) + } + + return out.State +} + +// TestSDKPartnerEventSourceFlow drives the partner and customer sides through +// the real SDK: create the source, match a partner event bus to it, deliver a +// partner event to SQS, then deactivate, activate and delete it. +func TestSDKPartnerEventSourceFlow(t *testing.T) { + eb, sqs := newEBAndSQS(t) + ctx := context.Background() + + created, err := eb.CreatePartnerEventSource(ctx, &awseb.CreatePartnerEventSourceInput{ + Name: aws.String(sdkSource), Account: aws.String("123456789012"), + }) + if err != nil { + t.Fatalf("CreatePartnerEventSource: %v", err) + } + + if aws.ToString(created.EventSourceArn) != "arn:aws:events:us-east-1::event-source/"+sdkSource { + t.Fatalf("EventSourceArn = %q", aws.ToString(created.EventSourceArn)) + } + + _, err = eb.CreatePartnerEventSource(ctx, &awseb.CreatePartnerEventSourceInput{ + Name: aws.String(sdkSource), Account: aws.String("123456789012"), + }) + requireAPIError(t, err, "ResourceAlreadyExistsException", "Event source "+sdkSource+" already exists.") + + listed, err := eb.ListEventSources(ctx, &awseb.ListEventSourcesInput{NamePrefix: aws.String("aws.partner/acme")}) + if err != nil || len(listed.EventSources) != 1 || listed.EventSources[0].State != ebtypes.EventSourceStatePending || + listed.EventSources[0].ExpirationTime == nil { + t.Fatalf("ListEventSources = %+v, %v", listed, err) + } + + _, err = eb.CreateEventBus(ctx, &awseb.CreateEventBusInput{Name: aws.String("custom"), EventSourceName: aws.String(sdkSource)}) + requireAPIError(t, err, "ValidationException", "") + + if _, err = eb.CreateEventBus(ctx, &awseb.CreateEventBusInput{ + Name: aws.String(sdkSource), EventSourceName: aws.String(sdkSource), + }); err != nil { + t.Fatalf("CreateEventBus: %v", err) + } + + if s := sourceState(t, eb); s != ebtypes.EventSourceStateActive { + t.Fatalf("state after CreateEventBus = %s", s) + } + + url, queueARN := makeQueue(t, sqs, "partner-q") + + if _, err = eb.PutRule(ctx, &awseb.PutRuleInput{ + Name: aws.String("r"), EventBusName: aws.String(sdkSource), + EventPattern: aws.String(`{"source":[{"prefix":"aws.partner/"}]}`), + }); err != nil { + t.Fatalf("PutRule: %v", err) + } + + if _, err = eb.PutTargets(ctx, &awseb.PutTargetsInput{ + Rule: aws.String("r"), EventBusName: aws.String(sdkSource), + Targets: []ebtypes.Target{{Id: aws.String("q"), Arn: aws.String(queueARN)}}, + }); err != nil { + t.Fatalf("PutTargets: %v", err) + } + + putPartner(t, eb) + + body, n := receiveOne(t, sqs, url) + if n != 1 { + t.Fatalf("got %d messages", n) + } + + var env map[string]any + if err = json.Unmarshal([]byte(body), &env); err != nil || env["source"] != sdkSource || env["detail-type"] != "t" { + t.Fatalf("delivered %s (%v)", body, err) + } + + if _, err = eb.DeactivateEventSource(ctx, &awseb.DeactivateEventSourceInput{Name: aws.String(sdkSource)}); err != nil { + t.Fatalf("DeactivateEventSource: %v", err) + } + + if s := sourceState(t, eb); s != ebtypes.EventSourceStatePending { + t.Fatalf("state after Deactivate = %s", s) + } + + putPartner(t, eb) + + if _, n = receiveOne(t, sqs, url); n != 0 { + t.Fatalf("a PENDING source delivered %d messages", n) + } + + if _, err = eb.ActivateEventSource(ctx, &awseb.ActivateEventSourceInput{Name: aws.String(sdkSource)}); err != nil { + t.Fatalf("ActivateEventSource: %v", err) + } + + if _, err = eb.DeletePartnerEventSource(ctx, &awseb.DeletePartnerEventSourceInput{ + Name: aws.String(sdkSource), Account: aws.String("123456789012"), + }); err != nil { + t.Fatalf("DeletePartnerEventSource: %v", err) + } + + if s := sourceState(t, eb); s != ebtypes.EventSourceStateDeleted { + t.Fatalf("state after delete = %s", s) + } + + _, err = eb.ActivateEventSource(ctx, &awseb.ActivateEventSourceInput{Name: aws.String(sdkSource)}) + requireAPIError(t, err, "InvalidStateException", "Event source "+sdkSource+" is in state DELETED.") +} + +func putPartner(t *testing.T, eb *awseb.Client) { + t.Helper() + + out, err := eb.PutPartnerEvents(context.Background(), &awseb.PutPartnerEventsInput{ + Entries: []ebtypes.PutPartnerEventsRequestEntry{{ + Source: aws.String(sdkSource), DetailType: aws.String("t"), Detail: aws.String(`{"a":1}`), + }}, + }) + if err != nil || out.FailedEntryCount != 0 || len(out.Entries) != 1 || aws.ToString(out.Entries[0].EventId) == "" { + t.Fatalf("PutPartnerEvents = %+v, %v", out, err) + } +} + +func TestSDKPartnerEventSourcePartnerSide(t *testing.T) { + eb, _ := newEBAndSQS(t) + ctx := context.Background() + + for _, acct := range []string{"123456789012", "210987654321"} { + if _, err := eb.CreatePartnerEventSource(ctx, &awseb.CreatePartnerEventSourceInput{ + Name: aws.String(sdkSource), Account: aws.String(acct), + }); err != nil { + t.Fatalf("CreatePartnerEventSource %s: %v", acct, err) + } + } + + desc, err := eb.DescribePartnerEventSource(ctx, &awseb.DescribePartnerEventSourceInput{Name: aws.String(sdkSource)}) + if err != nil || aws.ToString(desc.Name) != sdkSource { + t.Fatalf("DescribePartnerEventSource = %+v, %v", desc, err) + } + + list, err := eb.ListPartnerEventSources(ctx, &awseb.ListPartnerEventSourcesInput{NamePrefix: aws.String("aws.partner/acme.com/")}) + if err != nil || len(list.PartnerEventSources) != 1 { + t.Fatalf("ListPartnerEventSources = %+v, %v", list, err) + } + + accts, err := eb.ListPartnerEventSourceAccounts(ctx, &awseb.ListPartnerEventSourceAccountsInput{ + EventSourceName: aws.String(sdkSource), Limit: aws.Int32(1), + }) + if err != nil || len(accts.PartnerEventSourceAccounts) != 1 || accts.NextToken == nil || + aws.ToString(accts.PartnerEventSourceAccounts[0].Account) != "123456789012" || + accts.PartnerEventSourceAccounts[0].State != ebtypes.EventSourceStatePending { + t.Fatalf("ListPartnerEventSourceAccounts = %+v, %v", accts, err) + } + + _, err = eb.DescribePartnerEventSource(ctx, &awseb.DescribePartnerEventSourceInput{Name: aws.String("aws.partner/acme.com/none")}) + requireAPIError(t, err, "ResourceNotFoundException", "") + + _, err = eb.DescribeEventSource(ctx, &awseb.DescribeEventSourceInput{Name: aws.String("aws.partner/acme.com/none")}) + requireAPIError(t, err, "ResourceNotFoundException", "") + + _, err = eb.CreatePartnerEventSource(ctx, &awseb.CreatePartnerEventSourceInput{ + Name: aws.String("acme"), Account: aws.String("1"), + }) + requireAPIError(t, err, "ValidationException", "") + + _, err = eb.CreateEventBus(ctx, &awseb.CreateEventBusInput{ + Name: aws.String("aws.partner/acme.com/none"), EventSourceName: aws.String("aws.partner/acme.com/none"), + }) + requireAPIError(t, err, "ResourceNotFoundException", "") + + _, err = eb.PutPartnerEvents(ctx, &awseb.PutPartnerEventsInput{Entries: make([]ebtypes.PutPartnerEventsRequestEntry, 21)}) + requireAPIError(t, err, "ValidationException", "") +} diff --git a/server/aws/eventbridge/types.go b/server/aws/eventbridge/types.go index f23b0309d..6fed5643e 100644 --- a/server/aws/eventbridge/types.go +++ b/server/aws/eventbridge/types.go @@ -21,9 +21,10 @@ type tagJSON struct { // --- request envelopes --- type createEventBusRequest struct { - Name string `json:"Name"` - Description string `json:"Description"` - Tags []tagJSON `json:"Tags"` + Name string `json:"Name"` + EventSourceName string `json:"EventSourceName"` + Description string `json:"Description"` + Tags []tagJSON `json:"Tags"` } type nameRequest struct { diff --git a/services/eventbus/driver/driver.go b/services/eventbus/driver/driver.go index 2c5b660d5..cc57b6685 100644 --- a/services/eventbus/driver/driver.go +++ b/services/eventbus/driver/driver.go @@ -82,6 +82,10 @@ type EventBusConfig struct { // Description is the caller-supplied free-text description of the event // bus (EventBridge CreateEventBus's Description). Empty for Azure and GCP. Description string + // EventSourceName is the partner event source a partner event bus is + // matched to (EventBridge CreateEventBus's EventSourceName). Empty for a + // custom event bus, and for Azure and GCP. + EventSourceName string } // Rule defines an event routing rule with filtering. diff --git a/services/eventbus/driver/partner.go b/services/eventbus/driver/partner.go new file mode 100644 index 000000000..96399df8b --- /dev/null +++ b/services/eventbus/driver/partner.go @@ -0,0 +1,44 @@ +package driver + +import "time" + +// PartnerEventSource is a partner event source as the SaaS partner that created +// it sees it. Partner event sources are an AWS-only concept, consumed through an +// optional interface, not the portable driver. +type PartnerEventSource struct { + Name string + ARN string +} + +// PartnerEventSourceAccount is one customer account a partner event source was +// created for, with the source's state in that account. +type PartnerEventSourceAccount struct { + Account string + // State is PENDING, ACTIVE or DELETED. + State string + CreatedAt time.Time + // ExpiresAt is set only while the source is PENDING. + ExpiresAt time.Time +} + +// EventSource is a partner event source as the customer account it was shared +// with sees it. +type EventSource struct { + Name string + ARN string + // CreatedBy is the name of the partner that created the source. + CreatedBy string + // State is PENDING, ACTIVE or DELETED. + State string + CreatedAt time.Time + // ExpiresAt is set only while the source is PENDING. + ExpiresAt time.Time +} + +// EventSourceFilter narrows and pages ListEventSources and +// ListPartnerEventSources. Limit 0 means the default page size. +type EventSourceFilter struct { + NamePrefix string + Limit int + NextToken string +} From 020ead93efbc1b99140fd0097116198cbf671732 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Patel Date: Fri, 23 Oct 2026 08:27:18 +0530 Subject: [PATCH 16/31] fix(aws-sfn): StartSyncExecution rejects STANDARD state machines (#1689) --- docs/coverage/aws/sfn.md | 2 +- docs/coverage/nongoals/sfn.md | 1 + providers/aws/sfn/errors.go | 6 ++ providers/aws/sfn/executions.go | 20 ++++ providers/aws/sfn/interpreter_test.go | 23 ++++- providers/aws/sfn/parallel_map_test.go | 2 +- providers/aws/sfn/sfn_test.go | 3 +- .../aws/sfn/start_sync_execution_test.go | 93 +++++++++++++++++++ providers/aws/sfn/task_test.go | 2 +- server/aws/sfn/start_sync_execution_test.go | 63 +++++++++++++ services/sfn/driver/errors.go | 3 + 11 files changed, 210 insertions(+), 8 deletions(-) create mode 100644 docs/coverage/nongoals/sfn.md create mode 100644 providers/aws/sfn/start_sync_execution_test.go create mode 100644 server/aws/sfn/start_sync_execution_test.go diff --git a/docs/coverage/aws/sfn.md b/docs/coverage/aws/sfn.md index fdca184dc..2cd249989 100644 --- a/docs/coverage/aws/sfn.md +++ b/docs/coverage/aws/sfn.md @@ -47,4 +47,4 @@ AWS's `sfn` service ยท portable interface `driver.SFN` ยท [AWS index](./README.m ## Not in scope -_Not documented yet. See the [emulator boundary](../../../README.md) for cloudemu-wide non-goals._ +- `StopExecution` on an execution that has already closed returns 200 with its original `stopDate` and changes nothing. The API reference documents no error for this case, and the behavior is not verified against live AWS. diff --git a/docs/coverage/nongoals/sfn.md b/docs/coverage/nongoals/sfn.md new file mode 100644 index 000000000..339ac04fb --- /dev/null +++ b/docs/coverage/nongoals/sfn.md @@ -0,0 +1 @@ +- `StopExecution` on an execution that has already closed returns 200 with its original `stopDate` and changes nothing. The API reference documents no error for this case, and the behavior is not verified against live AWS. diff --git a/providers/aws/sfn/errors.go b/providers/aws/sfn/errors.go index 45eb1c1d9..b2246ff77 100644 --- a/providers/aws/sfn/errors.go +++ b/providers/aws/sfn/errors.go @@ -99,6 +99,12 @@ func invalidExecutionInput(msg string) error { return &driver.APIError{Exception: driver.ExInvalidExecutionInput, Err: errors.New(errors.InvalidArgument, msg)} } +// smTypeNotSupported builds the StateMachineTypeNotSupported error an +// operation returns for a state machine type it does not support. +func smTypeNotSupported(msg string) error { + return &driver.APIError{Exception: driver.ExStateMachineTypeNotSupported, Err: errors.New(errors.FailedPrecondition, msg)} +} + // invalidToken builds an InvalidToken-tagged error. func invalidToken(format string, args ...any) error { return &driver.APIError{Exception: driver.ExInvalidToken, Err: errors.Newf(errors.InvalidArgument, format, args...)} diff --git a/providers/aws/sfn/executions.go b/providers/aws/sfn/executions.go index 1023e2dc6..6ed5f10f8 100644 --- a/providers/aws/sfn/executions.go +++ b/providers/aws/sfn/executions.go @@ -218,7 +218,23 @@ func (m *Mock) StartExternal(ctx context.Context, stateMachineARN, input string) return err } +// StartSyncExecution runs an EXPRESS workflow synchronously. Real Step +// Functions does not offer it for STANDARD workflows and rejects them with +// StateMachineTypeNotSupported. func (m *Mock) StartSyncExecution(ctx context.Context, in driver.StartExecutionInput) (*driver.Execution, error) { + sd, err := m.getSM(in.StateMachineArn) + if err != nil { + return nil, err + } + + sd.mu.RLock() + smType := sd.sm.Type + sd.mu.RUnlock() + + if smType != driver.TypeExpress { + return nil, smTypeNotSupported("This operation is not supported by this type of state machine") + } + return m.runExecution(ctx, in, false) } @@ -310,6 +326,10 @@ func (m *Mock) abortExecution(ed *execData, errCode, cause string) (time.Time, * now := m.now() + // Pinned: StopExecution on an already-closed execution returns 200 with the + // stored stop date and changes nothing. The API reference documents no + // error for this case and the behavior is not verified against live AWS. + // // While an execution is still settling (observably RUNNING under AsyncSettle), // Stop aborts it: ABORTED with a stop date, no output, the caller's error/cause // persisted, and the history trimmed to the events already visible plus a diff --git a/providers/aws/sfn/interpreter_test.go b/providers/aws/sfn/interpreter_test.go index f20bf0a6d..769c8d367 100644 --- a/providers/aws/sfn/interpreter_test.go +++ b/providers/aws/sfn/interpreter_test.go @@ -11,12 +11,27 @@ import ( "github.com/stackshy/cloudemu/v2/services/sfn/driver" ) -// createDef registers a state machine with the given verbatim ASL definition. +// createDef registers a STANDARD state machine with the given verbatim ASL +// definition. func createDef(t *testing.T, m *sfn.Mock, name, def string) string { t.Helper() + return createTypedDef(t, m, name, def, driver.TypeStandard) +} + +// createExpressDef registers an EXPRESS state machine, the only type +// StartSyncExecution accepts. +func createExpressDef(t *testing.T, m *sfn.Mock, name, def string) string { + t.Helper() + + return createTypedDef(t, m, name, def, driver.TypeExpress) +} + +func createTypedDef(t *testing.T, m *sfn.Mock, name, def, smType string) string { + t.Helper() + arn, _, _, err := m.CreateStateMachine(context.Background(), driver.CreateStateMachineInput{ - Name: name, Definition: def, RoleArn: "arn:aws:iam::000000000000:role/r", + Name: name, Definition: def, RoleArn: "arn:aws:iam::000000000000:role/r", Type: smType, }) if err != nil { t.Fatalf("CreateStateMachine(%s): %v", name, err) @@ -25,11 +40,11 @@ func createDef(t *testing.T, m *sfn.Mock, name, def string) string { return arn } -// runSync creates a machine and runs one synchronous (instant) execution. +// runSync creates an EXPRESS machine and runs one synchronous execution. func runSync(t *testing.T, m *sfn.Mock, name, def, input string) *driver.Execution { t.Helper() - arn := createDef(t, m, name, def) + arn := createExpressDef(t, m, name, def) exec, err := m.StartSyncExecution(context.Background(), driver.StartExecutionInput{ StateMachineArn: arn, Name: "run", Input: input, diff --git a/providers/aws/sfn/parallel_map_test.go b/providers/aws/sfn/parallel_map_test.go index d42147f57..0a0537821 100644 --- a/providers/aws/sfn/parallel_map_test.go +++ b/providers/aws/sfn/parallel_map_test.go @@ -171,7 +171,7 @@ func TestParallelTaskLambdaRecursionBounded(t *testing.T) { def := `{"StartAt":"P","States":{"P":{"Type":"Parallel","End":true, "Branches":[{"StartAt":"T","States":{"T":{"Type":"Task","Resource":"` + lambdaFuncARN + `","End":true}}}]}}}` - arn := createDef(t, m, "par-recurse", def) + arn := createExpressDef(t, m, "par-recurse", def) maxDepth := 0 m.SetLambdaSyncInvoker(fakeLambda{fn: func(ctx context.Context, _ string, payload []byte) ([]byte, string, error) { diff --git a/providers/aws/sfn/sfn_test.go b/providers/aws/sfn/sfn_test.go index 058a386c2..a4bb031b9 100644 --- a/providers/aws/sfn/sfn_test.go +++ b/providers/aws/sfn/sfn_test.go @@ -641,9 +641,10 @@ func TestAsyncSettleSyncExecutionAndHistory(t *testing.T) { config.WithAccountID("000000000000"), config.WithAsyncSettle())) ctx := context.Background() arn := createSM(t, m, "sm") + expressArn := createExpressDef(t, m, "sm-express", definition) // Synchronous execution returns terminal SUCCEEDED with output, not RUNNING. - sync, err := m.StartSyncExecution(ctx, driver.StartExecutionInput{StateMachineArn: arn, Name: "sync1", Input: `{"k":1}`}) + sync, err := m.StartSyncExecution(ctx, driver.StartExecutionInput{StateMachineArn: expressArn, Name: "sync1", Input: `{"k":1}`}) if err != nil { t.Fatalf("StartSyncExecution: %v", err) } diff --git a/providers/aws/sfn/start_sync_execution_test.go b/providers/aws/sfn/start_sync_execution_test.go new file mode 100644 index 000000000..b70c3bd15 --- /dev/null +++ b/providers/aws/sfn/start_sync_execution_test.go @@ -0,0 +1,93 @@ +package sfn_test + +import ( + "context" + stderrors "errors" + "testing" + + "github.com/stackshy/cloudemu/v2/services/sfn/driver" +) + +// TestStartSyncExecutionRejectsStandard pins that StartSyncExecution is only +// offered for EXPRESS workflows: a STANDARD state machine is rejected with +// StateMachineTypeNotSupported and no execution is recorded. +func TestStartSyncExecutionRejectsStandard(t *testing.T) { + m := newMock(t) + ctx := context.Background() + arn := createSM(t, m, "std") + + _, err := m.StartSyncExecution(ctx, driver.StartExecutionInput{StateMachineArn: arn, Name: "s1"}) + + var apiErr *driver.APIError + if !stderrors.As(err, &apiErr) || apiErr.Exception != driver.ExStateMachineTypeNotSupported { + t.Fatalf("StartSyncExecution(STANDARD) = %v, want StateMachineTypeNotSupported", err) + } + + execs, err := m.ListExecutions(ctx, arn, "") + if err != nil || len(execs) != 0 { + t.Fatalf("ListExecutions = %d, %v; want no execution recorded", len(execs), err) + } +} + +// TestStartSyncExecutionExpress runs an EXPRESS workflow to completion. +func TestStartSyncExecutionExpress(t *testing.T) { + m := newMock(t) + arn := createExpressDef(t, m, "exp", definition) + + exec, err := m.StartSyncExecution(context.Background(), + driver.StartExecutionInput{StateMachineArn: arn, Name: "s1", Input: `{"a":1}`}) + if err != nil { + t.Fatalf("StartSyncExecution(EXPRESS): %v", err) + } + + if exec.Status != driver.ExecStatusSucceeded || exec.Output != `{"a":1}` { + t.Fatalf("execution = %s %q, want SUCCEEDED {\"a\":1}", exec.Status, exec.Output) + } +} + +// TestStartSyncExecutionMissingStateMachine checks existence before type. +func TestStartSyncExecutionMissingStateMachine(t *testing.T) { + m := newMock(t) + + _, err := m.StartSyncExecution(context.Background(), driver.StartExecutionInput{ + StateMachineArn: "arn:aws:states:us-east-1:000000000000:stateMachine:missing", + }) + + var apiErr *driver.APIError + if !stderrors.As(err, &apiErr) || apiErr.Exception != driver.ExStateMachineDoesNotExist { + t.Fatalf("StartSyncExecution(missing) = %v, want StateMachineDoesNotExist", err) + } +} + +// TestStopExecutionTerminalKeepsStopDate pins StopExecution on a closed +// execution: 200 with the original stop date and no state change. The API +// reference documents no error for this case, and the behavior is unverified +// against live AWS. +func TestStopExecutionTerminalKeepsStopDate(t *testing.T) { + m := newMock(t) + ctx := context.Background() + arn := createSM(t, m, "stop-closed") + + exec, err := m.StartExecution(ctx, driver.StartExecutionInput{StateMachineArn: arn, Name: "done"}) + if err != nil || exec.Status != driver.ExecStatusSucceeded { + t.Fatalf("StartExecution = %v, %v; want SUCCEEDED", exec, err) + } + + stopDate, err := m.StopExecution(ctx, exec.ARN, "Halt", "late") + if err != nil { + t.Fatalf("StopExecution(closed): %v", err) + } + + if !stopDate.Equal(exec.StopDate) { + t.Fatalf("stopDate = %v, want the original %v", stopDate, exec.StopDate) + } + + got, err := m.DescribeExecution(ctx, exec.ARN) + if err != nil { + t.Fatalf("DescribeExecution: %v", err) + } + + if got.Status != driver.ExecStatusSucceeded || got.Error != "" || got.Cause != "" { + t.Fatalf("after stop = %s %q/%q, want unchanged SUCCEEDED", got.Status, got.Error, got.Cause) + } +} diff --git a/providers/aws/sfn/task_test.go b/providers/aws/sfn/task_test.go index 6e23596d1..e0eb8bede 100644 --- a/providers/aws/sfn/task_test.go +++ b/providers/aws/sfn/task_test.go @@ -281,7 +281,7 @@ func TestTaskRecursionGuardTerminates(t *testing.T) { m := newMock(t) def := `{"StartAt":"T","States":{"T":{"Type":"Task","Resource":"` + lambdaFuncARN + `","End":true}}}` - arn := createDef(t, m, "recurse", def) + arn := createExpressDef(t, m, "recurse", def) maxDepth := 0 m.SetLambdaSyncInvoker(fakeLambda{fn: func(ctx context.Context, _ string, payload []byte) ([]byte, string, error) { diff --git a/server/aws/sfn/start_sync_execution_test.go b/server/aws/sfn/start_sync_execution_test.go new file mode 100644 index 000000000..c7bb3f832 --- /dev/null +++ b/server/aws/sfn/start_sync_execution_test.go @@ -0,0 +1,63 @@ +package sfn_test + +import ( + "context" + "errors" + "testing" + + "github.com/aws/aws-sdk-go-v2/aws" + awssfn "github.com/aws/aws-sdk-go-v2/service/sfn" + sfntypes "github.com/aws/aws-sdk-go-v2/service/sfn/types" + "github.com/aws/smithy-go" +) + +// syncOpts lets StartSyncExecution reach the local server (it uses the +// "sync-" endpoint host prefix). +func syncOpts(o *awssfn.Options) { o.APIOptions = append(o.APIOptions, disableHostPrefix) } + +// TestSDKStartSyncExecutionStandardTypeNotSupported pins the wire error a real +// SDK sees for StartSyncExecution on a STANDARD state machine. +func TestSDKStartSyncExecutionStandardTypeNotSupported(t *testing.T) { + ctx := context.Background() + c := newSFNClient(t) + arn := createSM(t, c, "std-sync") + + _, err := c.StartSyncExecution(ctx, &awssfn.StartSyncExecutionInput{StateMachineArn: aws.String(arn)}, syncOpts) + + var notSupported *sfntypes.StateMachineTypeNotSupported + if !errors.As(err, ¬Supported) { + var apiErr smithy.APIError + if errors.As(err, &apiErr) { + t.Fatalf("want StateMachineTypeNotSupported, got %q", apiErr.ErrorCode()) + } + + t.Fatalf("want StateMachineTypeNotSupported, got %v", err) + } +} + +// TestSDKStartSyncExecutionExpress runs an EXPRESS workflow over the wire. +func TestSDKStartSyncExecutionExpress(t *testing.T) { + ctx := context.Background() + c := newSFNClient(t) + + created, err := c.CreateStateMachine(ctx, &awssfn.CreateStateMachineInput{ + Name: aws.String("exp-sync"), + Definition: aws.String(definition), + RoleArn: aws.String("arn:aws:iam::123456789012:role/svc"), + Type: sfntypes.StateMachineTypeExpress, + }) + if err != nil { + t.Fatalf("CreateStateMachine: %v", err) + } + + out, err := c.StartSyncExecution(ctx, &awssfn.StartSyncExecutionInput{ + StateMachineArn: created.StateMachineArn, Input: aws.String(`{"k":1}`), + }, syncOpts) + if err != nil { + t.Fatalf("StartSyncExecution(EXPRESS): %v", err) + } + + if out.Status != sfntypes.SyncExecutionStatusSucceeded || aws.ToString(out.Output) != `{"k":1}` { + t.Fatalf("got %s %q, want SUCCEEDED {\"k\":1}", out.Status, aws.ToString(out.Output)) + } +} diff --git a/services/sfn/driver/errors.go b/services/sfn/driver/errors.go index 99f309e8b..1d93169bb 100644 --- a/services/sfn/driver/errors.go +++ b/services/sfn/driver/errors.go @@ -22,6 +22,9 @@ const ( ExConflict = "ConflictException" ExValidation = "ValidationException" ExMissingRequiredParameter = "MissingRequiredParameter" + // ExStateMachineTypeNotSupported is returned when an operation does not + // support the state machine's type, e.g. StartSyncExecution on STANDARD. + ExStateMachineTypeNotSupported = "StateMachineTypeNotSupported" ) // APIError tags a canonical cloudemu error with the SFN exception name it From 7f0d74173dbc686bbad58eef921ae52858fbe8f2 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Patel Date: Fri, 23 Oct 2026 08:37:02 +0530 Subject: [PATCH 17/31] fix(aws-cloudwatchlogs): data protection and account policies (#1700) --- docs/coverage/aws/cloudwatchlogs.md | 16 +- docs/coverage/azure/loganalytics.md | 6 + docs/coverage/coverage.json | 30 +- docs/coverage/gcp/cloudlogging.md | 6 + docs/coverage/nongoals/logging.md | 6 + docs/coverage/oci/logging.md | 6 + .../aws/cloudwatchlogs/account_policies.go | 531 ++++++++++++++++++ .../cloudwatchlogs/account_policies_test.go | 212 +++++++ .../aws/cloudwatchlogs/cloudwatchlogs.go | 53 +- .../aws/cloudwatchlogs/data_protection.go | 162 ++++++ .../cloudwatchlogs/data_protection_test.go | 274 +++++++++ .../aws/cloudwatchlogs/dataprotection/mask.go | 168 ++++++ .../cloudwatchlogs/dataprotection/policy.go | 298 ++++++++++ .../dataprotection/policy_test.go | 183 ++++++ providers/aws/cloudwatchlogs/ingest.go | 3 + .../aws/cloudwatchlogs/insights_queries.go | 4 +- .../cloudwatchlogs/insights_queries_test.go | 4 +- providers/aws/cloudwatchlogs/log_groups.go | 7 +- providers/aws/cloudwatchlogs/snapshot.go | 59 ++ .../cloudwatchlogs/subscription_filters.go | 11 +- .../cloudwatchlogs/account_policy_sdk_test.go | 108 ++++ .../data_protection_sdk_test.go | 135 +++++ server/aws/cloudwatchlogs/export_tasks.go | 6 +- server/aws/cloudwatchlogs/handler.go | 2 + server/aws/cloudwatchlogs/insights.go | 3 +- server/aws/cloudwatchlogs/operations.go | 29 +- server/aws/cloudwatchlogs/policies.go | 216 +++++++ server/aws/cloudwatchlogs/types.go | 7 + services/logging/driver/aws.go | 78 ++- services/logging/driver/driver.go | 4 + 30 files changed, 2595 insertions(+), 32 deletions(-) create mode 100644 providers/aws/cloudwatchlogs/account_policies.go create mode 100644 providers/aws/cloudwatchlogs/account_policies_test.go create mode 100644 providers/aws/cloudwatchlogs/data_protection.go create mode 100644 providers/aws/cloudwatchlogs/data_protection_test.go create mode 100644 providers/aws/cloudwatchlogs/dataprotection/mask.go create mode 100644 providers/aws/cloudwatchlogs/dataprotection/policy.go create mode 100644 providers/aws/cloudwatchlogs/dataprotection/policy_test.go create mode 100644 server/aws/cloudwatchlogs/account_policy_sdk_test.go create mode 100644 server/aws/cloudwatchlogs/data_protection_sdk_test.go create mode 100644 server/aws/cloudwatchlogs/policies.go diff --git a/docs/coverage/aws/cloudwatchlogs.md b/docs/coverage/aws/cloudwatchlogs.md index 0920fafe4..0947e2d66 100644 --- a/docs/coverage/aws/cloudwatchlogs.md +++ b/docs/coverage/aws/cloudwatchlogs.md @@ -39,15 +39,23 @@ AWSLogging is an optional interface implemented by the AWS CloudWatch Logs | `CancelExportTask` | | | `CreateAWSLogGroup` | | | `CreateExportTask` | Export tasks write a log group's events to an S3 bucket. A task moves | +| `DeleteAccountPolicy` | | +| `DeleteDataProtectionPolicy` | | | `DeleteRetentionPolicy` | | | `DescribeAWSMetricFilters` | DescribeAWSMetricFilters lists metric filters, sorted by filter name. | +| `DescribeAccountPolicies` | | | `DescribeExportTasks` | | | `DescribeQueries` | | | `DisassociateKmsKey` | | +| `FilterAWSLogEvents` | | +| `GetAWSLogEvents` | GetAWSLogEvents and FilterAWSLogEvents are GetLogEvents and | +| `GetDataProtectionPolicy` | | | `GetLogGroupFields` | GetLogGroupFields samples the 8 minutes either side of at, or the last | -| `GetLogRecord` | | +| `GetLogRecord` | GetLogRecord returns the masked record unless unmask is set. | | `GetQueryResults` | | | `ListAWSLogGroups` | | +| `PutAccountPolicy` | Account policies apply to every log group in the account, or to the | +| `PutDataProtectionPolicy` | Data protection policies take a log group name or ARN. Masking applies | | `PutLogEventsWithResult` | | | `PutRetentionPolicy` | | | `StartQuery` | Logs Insights. A query is Running until its first GetQueryResults, | @@ -59,3 +67,9 @@ AWSLogging is an optional interface implemented by the AWS CloudWatch Logs - AWS: `CreateExportTask` checks the destination bucket's policy the way CloudWatch Logs does before it accepts a task. The policy must allow the `logs.amazonaws.com` or `logs..amazonaws.com` service principal (or `*`) to call `s3:GetBucketAcl` on the bucket and `s3:PutObject` under the prefix, and a matching `Deny` wins. Policy conditions (`aws:SourceAccount`, `aws:SourceArn`, `s3:x-amz-acl`), `NotPrincipal`/`NotAction`/`NotResource`, the Region named in the principal, SSE-KMS key policies and cross-account buckets are not evaluated. - AWS: the destination bucket's Region is not compared with the log group's Region, so an export to a bucket in another Region is accepted. - AWS: an export task writes each log stream to a single `000000.gz` object, where CloudWatch Logs splits large streams across `000000.gz`, `000001.gz`, and so on. The events are written when the task settles to `COMPLETED` and an export-task API call observes it: at once without `--async-settle`, or on the first `DescribeExportTasks` after the settle window with it. `PENDING_CANCEL` is never reported; `CancelExportTask` moves a task straight to `CANCELLED`. +- AWS: data protection detects four managed data identifiers: `EmailAddress`, `IpAddress` (IPv4 and IPv6), `AwsSecretKey` (a 40-character key next to a keyword such as `aws_secret_access_key`) and `CreditCardNumber` (13-19 digits that pass the Luhn check), plus custom data identifiers. Any other well-formed managed identifier ARN is accepted and stored but masks nothing, and the machine-learning and keyword-proximity detection AWS uses for the other identifiers is not modelled. Each masked character becomes one `*`. +- AWS: data protection audit findings are not produced. No findings report goes to the `FindingsDestination`, and no `LogEventsWithFindings` metric is emitted. A CloudWatch Logs findings destination must exist, but S3 and Firehose destinations are not checked. +- AWS: `logs:Unmask` is not enforced, so any caller can pass `unmask` to `GetLogEvents`, `FilterLogEvents` and `GetLogRecord`. A `FilterLogEvents` pattern always matches the masked text. The Logs Insights `unmask` command is not supported, and Live Tail is not modelled. +- AWS: an account data protection policy applies to existing groups at once, without AWS's eventual-consistency delay of up to 5 minutes. `DescribeLogGroups` reports `dataProtectionStatus` `ACTIVATED` only for a group-level policy, and the `DELETED`, `ARCHIVED` and `DISABLED` statuses are never reported. +- AWS: `FIELD_INDEX_POLICY`, `TRANSFORMER_POLICY` and `METRIC_EXTRACTION_POLICY` account policies are validated, stored and returned, but they have no data-plane effect: no field indexes, no transformation at ingest, and no change to EMF metric extraction. Transformer and metric extraction documents only have to be valid JSON. A `SUBSCRIPTION_FILTER_POLICY` delivers only to Lambda destinations, the same as group subscription filters. +- AWS: account policy quotas are one data protection policy and one subscription filter policy per account, and, for each of the other types, one unscoped policy plus up to 20 scoped policies (5 for metric extraction). Scoped policies of one type can't share overlapping `LogGroupNamePrefix` values or an identical data source, and a type can have only one `NOT IN` policy. AWS's finer `IN`/`NOT IN` containment rules for metric extraction policies are not modelled. `DescribeAccountPolicies` returns every match on one page and never returns a `nextToken`. diff --git a/docs/coverage/azure/loganalytics.md b/docs/coverage/azure/loganalytics.md index b575a8046..532cef441 100644 --- a/docs/coverage/azure/loganalytics.md +++ b/docs/coverage/azure/loganalytics.md @@ -30,3 +30,9 @@ Azure's `logging` service ยท portable interface `driver.Logging` ยท [Azure index - AWS: `CreateExportTask` checks the destination bucket's policy the way CloudWatch Logs does before it accepts a task. The policy must allow the `logs.amazonaws.com` or `logs..amazonaws.com` service principal (or `*`) to call `s3:GetBucketAcl` on the bucket and `s3:PutObject` under the prefix, and a matching `Deny` wins. Policy conditions (`aws:SourceAccount`, `aws:SourceArn`, `s3:x-amz-acl`), `NotPrincipal`/`NotAction`/`NotResource`, the Region named in the principal, SSE-KMS key policies and cross-account buckets are not evaluated. - AWS: the destination bucket's Region is not compared with the log group's Region, so an export to a bucket in another Region is accepted. - AWS: an export task writes each log stream to a single `000000.gz` object, where CloudWatch Logs splits large streams across `000000.gz`, `000001.gz`, and so on. The events are written when the task settles to `COMPLETED` and an export-task API call observes it: at once without `--async-settle`, or on the first `DescribeExportTasks` after the settle window with it. `PENDING_CANCEL` is never reported; `CancelExportTask` moves a task straight to `CANCELLED`. +- AWS: data protection detects four managed data identifiers: `EmailAddress`, `IpAddress` (IPv4 and IPv6), `AwsSecretKey` (a 40-character key next to a keyword such as `aws_secret_access_key`) and `CreditCardNumber` (13-19 digits that pass the Luhn check), plus custom data identifiers. Any other well-formed managed identifier ARN is accepted and stored but masks nothing, and the machine-learning and keyword-proximity detection AWS uses for the other identifiers is not modelled. Each masked character becomes one `*`. +- AWS: data protection audit findings are not produced. No findings report goes to the `FindingsDestination`, and no `LogEventsWithFindings` metric is emitted. A CloudWatch Logs findings destination must exist, but S3 and Firehose destinations are not checked. +- AWS: `logs:Unmask` is not enforced, so any caller can pass `unmask` to `GetLogEvents`, `FilterLogEvents` and `GetLogRecord`. A `FilterLogEvents` pattern always matches the masked text. The Logs Insights `unmask` command is not supported, and Live Tail is not modelled. +- AWS: an account data protection policy applies to existing groups at once, without AWS's eventual-consistency delay of up to 5 minutes. `DescribeLogGroups` reports `dataProtectionStatus` `ACTIVATED` only for a group-level policy, and the `DELETED`, `ARCHIVED` and `DISABLED` statuses are never reported. +- AWS: `FIELD_INDEX_POLICY`, `TRANSFORMER_POLICY` and `METRIC_EXTRACTION_POLICY` account policies are validated, stored and returned, but they have no data-plane effect: no field indexes, no transformation at ingest, and no change to EMF metric extraction. Transformer and metric extraction documents only have to be valid JSON. A `SUBSCRIPTION_FILTER_POLICY` delivers only to Lambda destinations, the same as group subscription filters. +- AWS: account policy quotas are one data protection policy and one subscription filter policy per account, and, for each of the other types, one unscoped policy plus up to 20 scoped policies (5 for metric extraction). Scoped policies of one type can't share overlapping `LogGroupNamePrefix` values or an identical data source, and a type can have only one `NOT IN` policy. AWS's finer `IN`/`NOT IN` containment rules for metric extraction policies are not modelled. `DescribeAccountPolicies` returns every match on one page and never returns a `nextToken`. diff --git a/docs/coverage/coverage.json b/docs/coverage/coverage.json index f49217dc0..804792a90 100644 --- a/docs/coverage/coverage.json +++ b/docs/coverage/coverage.json @@ -11576,6 +11576,12 @@ "name": "CreateExportTask", "doc": "Export tasks write a log group's events to an S3 bucket. A task moves" }, + { + "name": "DeleteAccountPolicy" + }, + { + "name": "DeleteDataProtectionPolicy" + }, { "name": "DeleteRetentionPolicy" }, @@ -11583,6 +11589,9 @@ "name": "DescribeAWSMetricFilters", "doc": "DescribeAWSMetricFilters lists metric filters, sorted by filter name." }, + { + "name": "DescribeAccountPolicies" + }, { "name": "DescribeExportTasks" }, @@ -11592,12 +11601,23 @@ { "name": "DisassociateKmsKey" }, + { + "name": "FilterAWSLogEvents" + }, + { + "name": "GetAWSLogEvents", + "doc": "GetAWSLogEvents and FilterAWSLogEvents are GetLogEvents and" + }, + { + "name": "GetDataProtectionPolicy" + }, { "name": "GetLogGroupFields", "doc": "GetLogGroupFields samples the 8 minutes either side of at, or the last" }, { - "name": "GetLogRecord" + "name": "GetLogRecord", + "doc": "GetLogRecord returns the masked record unless unmask is set." }, { "name": "GetQueryResults" @@ -11605,6 +11625,14 @@ { "name": "ListAWSLogGroups" }, + { + "name": "PutAccountPolicy", + "doc": "Account policies apply to every log group in the account, or to the" + }, + { + "name": "PutDataProtectionPolicy", + "doc": "Data protection policies take a log group name or ARN. Masking applies" + }, { "name": "PutLogEventsWithResult" }, diff --git a/docs/coverage/gcp/cloudlogging.md b/docs/coverage/gcp/cloudlogging.md index cb3cffdf5..befa4d3bf 100644 --- a/docs/coverage/gcp/cloudlogging.md +++ b/docs/coverage/gcp/cloudlogging.md @@ -56,3 +56,9 @@ GCPLogging is an optional interface implemented by the GCP logging backend for - AWS: `CreateExportTask` checks the destination bucket's policy the way CloudWatch Logs does before it accepts a task. The policy must allow the `logs.amazonaws.com` or `logs..amazonaws.com` service principal (or `*`) to call `s3:GetBucketAcl` on the bucket and `s3:PutObject` under the prefix, and a matching `Deny` wins. Policy conditions (`aws:SourceAccount`, `aws:SourceArn`, `s3:x-amz-acl`), `NotPrincipal`/`NotAction`/`NotResource`, the Region named in the principal, SSE-KMS key policies and cross-account buckets are not evaluated. - AWS: the destination bucket's Region is not compared with the log group's Region, so an export to a bucket in another Region is accepted. - AWS: an export task writes each log stream to a single `000000.gz` object, where CloudWatch Logs splits large streams across `000000.gz`, `000001.gz`, and so on. The events are written when the task settles to `COMPLETED` and an export-task API call observes it: at once without `--async-settle`, or on the first `DescribeExportTasks` after the settle window with it. `PENDING_CANCEL` is never reported; `CancelExportTask` moves a task straight to `CANCELLED`. +- AWS: data protection detects four managed data identifiers: `EmailAddress`, `IpAddress` (IPv4 and IPv6), `AwsSecretKey` (a 40-character key next to a keyword such as `aws_secret_access_key`) and `CreditCardNumber` (13-19 digits that pass the Luhn check), plus custom data identifiers. Any other well-formed managed identifier ARN is accepted and stored but masks nothing, and the machine-learning and keyword-proximity detection AWS uses for the other identifiers is not modelled. Each masked character becomes one `*`. +- AWS: data protection audit findings are not produced. No findings report goes to the `FindingsDestination`, and no `LogEventsWithFindings` metric is emitted. A CloudWatch Logs findings destination must exist, but S3 and Firehose destinations are not checked. +- AWS: `logs:Unmask` is not enforced, so any caller can pass `unmask` to `GetLogEvents`, `FilterLogEvents` and `GetLogRecord`. A `FilterLogEvents` pattern always matches the masked text. The Logs Insights `unmask` command is not supported, and Live Tail is not modelled. +- AWS: an account data protection policy applies to existing groups at once, without AWS's eventual-consistency delay of up to 5 minutes. `DescribeLogGroups` reports `dataProtectionStatus` `ACTIVATED` only for a group-level policy, and the `DELETED`, `ARCHIVED` and `DISABLED` statuses are never reported. +- AWS: `FIELD_INDEX_POLICY`, `TRANSFORMER_POLICY` and `METRIC_EXTRACTION_POLICY` account policies are validated, stored and returned, but they have no data-plane effect: no field indexes, no transformation at ingest, and no change to EMF metric extraction. Transformer and metric extraction documents only have to be valid JSON. A `SUBSCRIPTION_FILTER_POLICY` delivers only to Lambda destinations, the same as group subscription filters. +- AWS: account policy quotas are one data protection policy and one subscription filter policy per account, and, for each of the other types, one unscoped policy plus up to 20 scoped policies (5 for metric extraction). Scoped policies of one type can't share overlapping `LogGroupNamePrefix` values or an identical data source, and a type can have only one `NOT IN` policy. AWS's finer `IN`/`NOT IN` containment rules for metric extraction policies are not modelled. `DescribeAccountPolicies` returns every match on one page and never returns a `nextToken`. diff --git a/docs/coverage/nongoals/logging.md b/docs/coverage/nongoals/logging.md index de52ebf7b..6ca70512b 100644 --- a/docs/coverage/nongoals/logging.md +++ b/docs/coverage/nongoals/logging.md @@ -1,3 +1,9 @@ - AWS: `CreateExportTask` checks the destination bucket's policy the way CloudWatch Logs does before it accepts a task. The policy must allow the `logs.amazonaws.com` or `logs..amazonaws.com` service principal (or `*`) to call `s3:GetBucketAcl` on the bucket and `s3:PutObject` under the prefix, and a matching `Deny` wins. Policy conditions (`aws:SourceAccount`, `aws:SourceArn`, `s3:x-amz-acl`), `NotPrincipal`/`NotAction`/`NotResource`, the Region named in the principal, SSE-KMS key policies and cross-account buckets are not evaluated. - AWS: the destination bucket's Region is not compared with the log group's Region, so an export to a bucket in another Region is accepted. - AWS: an export task writes each log stream to a single `000000.gz` object, where CloudWatch Logs splits large streams across `000000.gz`, `000001.gz`, and so on. The events are written when the task settles to `COMPLETED` and an export-task API call observes it: at once without `--async-settle`, or on the first `DescribeExportTasks` after the settle window with it. `PENDING_CANCEL` is never reported; `CancelExportTask` moves a task straight to `CANCELLED`. +- AWS: data protection detects four managed data identifiers: `EmailAddress`, `IpAddress` (IPv4 and IPv6), `AwsSecretKey` (a 40-character key next to a keyword such as `aws_secret_access_key`) and `CreditCardNumber` (13-19 digits that pass the Luhn check), plus custom data identifiers. Any other well-formed managed identifier ARN is accepted and stored but masks nothing, and the machine-learning and keyword-proximity detection AWS uses for the other identifiers is not modelled. Each masked character becomes one `*`. +- AWS: data protection audit findings are not produced. No findings report goes to the `FindingsDestination`, and no `LogEventsWithFindings` metric is emitted. A CloudWatch Logs findings destination must exist, but S3 and Firehose destinations are not checked. +- AWS: `logs:Unmask` is not enforced, so any caller can pass `unmask` to `GetLogEvents`, `FilterLogEvents` and `GetLogRecord`. A `FilterLogEvents` pattern always matches the masked text. The Logs Insights `unmask` command is not supported, and Live Tail is not modelled. +- AWS: an account data protection policy applies to existing groups at once, without AWS's eventual-consistency delay of up to 5 minutes. `DescribeLogGroups` reports `dataProtectionStatus` `ACTIVATED` only for a group-level policy, and the `DELETED`, `ARCHIVED` and `DISABLED` statuses are never reported. +- AWS: `FIELD_INDEX_POLICY`, `TRANSFORMER_POLICY` and `METRIC_EXTRACTION_POLICY` account policies are validated, stored and returned, but they have no data-plane effect: no field indexes, no transformation at ingest, and no change to EMF metric extraction. Transformer and metric extraction documents only have to be valid JSON. A `SUBSCRIPTION_FILTER_POLICY` delivers only to Lambda destinations, the same as group subscription filters. +- AWS: account policy quotas are one data protection policy and one subscription filter policy per account, and, for each of the other types, one unscoped policy plus up to 20 scoped policies (5 for metric extraction). Scoped policies of one type can't share overlapping `LogGroupNamePrefix` values or an identical data source, and a type can have only one `NOT IN` policy. AWS's finer `IN`/`NOT IN` containment rules for metric extraction policies are not modelled. `DescribeAccountPolicies` returns every match on one page and never returns a `nextToken`. diff --git a/docs/coverage/oci/logging.md b/docs/coverage/oci/logging.md index fb903044b..d938f6db4 100644 --- a/docs/coverage/oci/logging.md +++ b/docs/coverage/oci/logging.md @@ -30,3 +30,9 @@ OCI's `logging` service ยท portable interface `driver.Logging` ยท [OCI index](./ - AWS: `CreateExportTask` checks the destination bucket's policy the way CloudWatch Logs does before it accepts a task. The policy must allow the `logs.amazonaws.com` or `logs..amazonaws.com` service principal (or `*`) to call `s3:GetBucketAcl` on the bucket and `s3:PutObject` under the prefix, and a matching `Deny` wins. Policy conditions (`aws:SourceAccount`, `aws:SourceArn`, `s3:x-amz-acl`), `NotPrincipal`/`NotAction`/`NotResource`, the Region named in the principal, SSE-KMS key policies and cross-account buckets are not evaluated. - AWS: the destination bucket's Region is not compared with the log group's Region, so an export to a bucket in another Region is accepted. - AWS: an export task writes each log stream to a single `000000.gz` object, where CloudWatch Logs splits large streams across `000000.gz`, `000001.gz`, and so on. The events are written when the task settles to `COMPLETED` and an export-task API call observes it: at once without `--async-settle`, or on the first `DescribeExportTasks` after the settle window with it. `PENDING_CANCEL` is never reported; `CancelExportTask` moves a task straight to `CANCELLED`. +- AWS: data protection detects four managed data identifiers: `EmailAddress`, `IpAddress` (IPv4 and IPv6), `AwsSecretKey` (a 40-character key next to a keyword such as `aws_secret_access_key`) and `CreditCardNumber` (13-19 digits that pass the Luhn check), plus custom data identifiers. Any other well-formed managed identifier ARN is accepted and stored but masks nothing, and the machine-learning and keyword-proximity detection AWS uses for the other identifiers is not modelled. Each masked character becomes one `*`. +- AWS: data protection audit findings are not produced. No findings report goes to the `FindingsDestination`, and no `LogEventsWithFindings` metric is emitted. A CloudWatch Logs findings destination must exist, but S3 and Firehose destinations are not checked. +- AWS: `logs:Unmask` is not enforced, so any caller can pass `unmask` to `GetLogEvents`, `FilterLogEvents` and `GetLogRecord`. A `FilterLogEvents` pattern always matches the masked text. The Logs Insights `unmask` command is not supported, and Live Tail is not modelled. +- AWS: an account data protection policy applies to existing groups at once, without AWS's eventual-consistency delay of up to 5 minutes. `DescribeLogGroups` reports `dataProtectionStatus` `ACTIVATED` only for a group-level policy, and the `DELETED`, `ARCHIVED` and `DISABLED` statuses are never reported. +- AWS: `FIELD_INDEX_POLICY`, `TRANSFORMER_POLICY` and `METRIC_EXTRACTION_POLICY` account policies are validated, stored and returned, but they have no data-plane effect: no field indexes, no transformation at ingest, and no change to EMF metric extraction. Transformer and metric extraction documents only have to be valid JSON. A `SUBSCRIPTION_FILTER_POLICY` delivers only to Lambda destinations, the same as group subscription filters. +- AWS: account policy quotas are one data protection policy and one subscription filter policy per account, and, for each of the other types, one unscoped policy plus up to 20 scoped policies (5 for metric extraction). Scoped policies of one type can't share overlapping `LogGroupNamePrefix` values or an identical data source, and a type can have only one `NOT IN` policy. AWS's finer `IN`/`NOT IN` containment rules for metric extraction policies are not modelled. `DescribeAccountPolicies` returns every match on one page and never returns a `nextToken`. diff --git a/providers/aws/cloudwatchlogs/account_policies.go b/providers/aws/cloudwatchlogs/account_policies.go new file mode 100644 index 000000000..7e6516c44 --- /dev/null +++ b/providers/aws/cloudwatchlogs/account_policies.go @@ -0,0 +1,531 @@ +package cloudwatchlogs + +import ( + "context" + "encoding/json" + "regexp" + "sort" + "strings" + + "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/providers/aws/cloudwatchlogs/dataprotection" + "github.com/stackshy/cloudemu/v2/providers/aws/cloudwatchlogs/filterpattern" + "github.com/stackshy/cloudemu/v2/services/logging/driver" +) + +// Account policy limits, per the PutAccountPolicy reference. +const ( + maxSelectionCriteriaBytes = 25 * 1024 + maxCriteriaValues = 50 + maxScopedTransformer = 20 + maxScopedFieldIndex = 20 + maxScopedMetricExtraction = 5 + reservedPolicyPrefix = "aws/" +) + +// Selection criteria keys and operators. +const ( + critLogGroupName = "LogGroupName" + critLogGroupPrefix = "LogGroupNamePrefix" + critDataSourceName = "DataSourceName" + critDataSourceType = "DataSourceType" + opIn = "IN" + opNotIn = "NOT IN" + opEquals = "=" +) + +var ( + criterionRE = regexp.MustCompile(`^\s*(LogGroupNamePrefix|LogGroupName|DataSourceName|DataSourceType)\s*(NOT\s+IN|IN|=)\s*(.+?)\s*$`) + criteriaAndRE = regexp.MustCompile(`\s+AND\s+`) + accountIDRE = regexp.MustCompile(`^\d{12}$`) + spaceRunRE = regexp.MustCompile(`\s+`) +) + +// accountPolicy is a stored account policy with the parsed forms ingest uses. +type accountPolicy struct { + info driver.AccountPolicy + criteria []criterion + dp *dataprotection.Policy + sub *driver.SubscriptionFilterInfo +} + +type criterion struct { + key, op string + values []string +} + +func accountPolicyKey(policyType, name string) string { return policyType + "\x00" + name } + +func validPolicyType(t string) bool { + switch t { + case driver.AccountPolicyDataProtection, driver.AccountPolicySubscriptionFilter, driver.AccountPolicyFieldIndex, + driver.AccountPolicyTransformer, driver.AccountPolicyMetricExtraction: + return true + default: + return false + } +} + +func policyTypeError(t string) error { + return errors.Newf(errors.InvalidArgument, + "1 validation error detected: Value '%s' at 'policyType' failed to satisfy constraint: Member must satisfy enum value set: "+ + "[DATA_PROTECTION_POLICY, SUBSCRIPTION_FILTER_POLICY, FIELD_INDEX_POLICY, TRANSFORMER_POLICY, METRIC_EXTRACTION_POLICY]", t) +} + +// PutAccountPolicy creates or replaces an account policy, identified by its +// type and name. +func (m *Mock) PutAccountPolicy(_ context.Context, in *driver.PutAccountPolicyInput) (*driver.AccountPolicy, error) { + ap, err := m.buildAccountPolicy(in, true) + if err != nil { + return nil, err + } + + ap.info.LastUpdatedTime = m.opts.Clock.Now().UTC() + + m.mu.Lock() + defer m.mu.Unlock() + + if err := m.checkAccountPolicyQuota(ap); err != nil { + return nil, err + } + + if m.accountPolicies == nil { + m.accountPolicies = map[string]*accountPolicy{} + } + + m.accountPolicies[accountPolicyKey(ap.info.PolicyType, ap.info.PolicyName)] = ap + + out := ap.info + + return &out, nil +} + +// buildAccountPolicy validates a request and parses its document. checkRefs +// also checks that referenced resources exist; restore skips it. +func (m *Mock) buildAccountPolicy(in *driver.PutAccountPolicyInput, checkRefs bool) (*accountPolicy, error) { + if err := validateAccountPolicyInput(in); err != nil { + return nil, err + } + + crits, err := parseSelectionCriteria(in.PolicyType, in.SelectionCriteria) + if err != nil { + return nil, err + } + + ap := &accountPolicy{ + info: driver.AccountPolicy{ + AccountID: m.opts.AccountID, PolicyName: in.PolicyName, PolicyType: in.PolicyType, + PolicyDocument: in.PolicyDocument, Scope: driver.AccountPolicyScopeAll, SelectionCriteria: in.SelectionCriteria, + }, + criteria: crits, + } + + if err := m.parseAccountDocument(ap, checkRefs); err != nil { + return nil, err + } + + return ap, nil +} + +func validateAccountPolicyInput(in *driver.PutAccountPolicyInput) error { + switch { + case in.PolicyName == "": + return errors.New(errors.InvalidArgument, "policyName is required") + case strings.HasPrefix(in.PolicyName, reservedPolicyPrefix): + return errors.New(errors.InvalidArgument, "Policy names can't start with aws/") + case !validPolicyType(in.PolicyType): + return policyTypeError(in.PolicyType) + case in.Scope != "" && in.Scope != driver.AccountPolicyScopeAll: + return errors.Newf(errors.InvalidArgument, + "1 validation error detected: Value '%s' at 'scope' failed to satisfy constraint: Member must satisfy enum value set: [ALL]", + in.Scope) + case in.PolicyDocument == "": + return errors.New(errors.InvalidArgument, "policyDocument is required") + case len(in.SelectionCriteria) > maxSelectionCriteriaBytes: + return errors.New(errors.InvalidArgument, "selectionCriteria must be at most 25KB") + } + + return nil +} + +func (m *Mock) parseAccountDocument(ap *accountPolicy, checkRefs bool) error { + doc := ap.info.PolicyDocument + + switch ap.info.PolicyType { + case driver.AccountPolicyDataProtection: + var err error + if checkRefs { + ap.dp, err = m.parseDataProtection(doc) + } else { + ap.dp, err = dataprotection.Parse(doc) + } + + return err + case driver.AccountPolicySubscriptionFilter: + sub, err := parseSubscriptionPolicy(doc) + ap.sub = sub + + return err + case driver.AccountPolicyFieldIndex: + return validateFieldIndexPolicy(doc) + default: + if !json.Valid([]byte(doc)) { + return errors.New(errors.InvalidArgument, "policyDocument is not valid JSON") + } + + return nil + } +} + +func parseSubscriptionPolicy(doc string) (*driver.SubscriptionFilterInfo, error) { + var d struct { + DestinationArn string `json:"DestinationArn"` + RoleArn string `json:"RoleArn"` + FilterPattern string `json:"FilterPattern"` + Distribution string `json:"Distribution"` + } + + if err := json.Unmarshal([]byte(doc), &d); err != nil { + return nil, errors.New(errors.InvalidArgument, "policyDocument is not a valid subscription filter policy") + } + + if !strings.HasPrefix(d.DestinationArn, "arn:") { + return nil, errors.New(errors.InvalidArgument, "The subscription filter policy must have a DestinationArn") + } + + if d.Distribution != "" && d.Distribution != "Random" && d.Distribution != "ByLogStream" { + return nil, errors.Newf(errors.InvalidArgument, "Distribution %s must be Random or ByLogStream", d.Distribution) + } + + if _, err := filterpattern.Parse(d.FilterPattern); err != nil { + return nil, err + } + + return &driver.SubscriptionFilterInfo{ + FilterPattern: d.FilterPattern, DestinationARN: d.DestinationArn, RoleARN: d.RoleArn, Distribution: d.Distribution, + }, nil +} + +func validateFieldIndexPolicy(doc string) error { + var d struct { + Fields []string `json:"Fields"` + FieldsV2 map[string]struct { + Type string `json:"type"` + } `json:"FieldsV2"` + } + + if err := json.Unmarshal([]byte(doc), &d); err != nil { + return errors.New(errors.InvalidArgument, "policyDocument is not a valid field index policy") + } + + if len(d.Fields)+len(d.FieldsV2) == 0 { + return errors.New(errors.InvalidArgument, "A field index policy must contain at least one field index") + } + + for _, f := range d.Fields { + if _, dup := d.FieldsV2[f]; dup || f == "" { + return errors.Newf(errors.InvalidArgument, "Field %q must appear once, in Fields or FieldsV2", f) + } + } + + for name, f := range d.FieldsV2 { + if f.Type != "FIELD_INDEX" && f.Type != "FACET" { + return errors.Newf(errors.InvalidArgument, "FieldsV2 %q type must be FIELD_INDEX or FACET", name) + } + } + + return nil +} + +// parseSelectionCriteria parses clauses such as `LogGroupName NOT IN ["a"]` +// or `LogGroupNamePrefix = "test"`, joined with AND, and checks them against +// what the policy type supports. +func parseSelectionCriteria(policyType, s string) ([]criterion, error) { + if strings.TrimSpace(s) == "" { + return nil, nil + } + + if policyType == driver.AccountPolicyDataProtection { + return nil, errors.New(errors.InvalidArgument, "selectionCriteria is not supported for DATA_PROTECTION_POLICY") + } + + var out []criterion + + for _, clause := range criteriaAndRE.Split(s, -1) { + c, err := parseCriterion(clause) + if err != nil { + return nil, err + } + + out = append(out, c) + } + + if !criteriaAllowed(policyType, out) { + return nil, errors.Newf(errors.InvalidArgument, "selectionCriteria %q is not supported for %s", s, policyType) + } + + return out, nil +} + +func parseCriterion(clause string) (criterion, error) { + bad := errors.Newf(errors.InvalidArgument, "Invalid selectionCriteria: %s", clause) + + m := criterionRE.FindStringSubmatch(clause) + if m == nil { + return criterion{}, bad + } + + c := criterion{key: m[1], op: spaceRunRE.ReplaceAllString(m[2], " ")} + + if strings.HasPrefix(m[3], "[") { + if err := json.Unmarshal([]byte(m[3]), &c.values); err != nil || len(c.values) == 0 { + return criterion{}, bad + } + } else { + var v string + if err := json.Unmarshal([]byte(m[3]), &v); err != nil { + return criterion{}, bad + } + + c.values = []string{v} + } + + if len(c.values) > maxCriteriaValues { + return criterion{}, errors.Newf(errors.InvalidArgument, "selectionCriteria lists can have at most %d values", maxCriteriaValues) + } + + return c, nil +} + +func criteriaAllowed(policyType string, cs []criterion) bool { + switch policyType { + case driver.AccountPolicySubscriptionFilter: + return isSingle(cs, critLogGroupName) && cs[0].op == opNotIn + case driver.AccountPolicyTransformer: + return isSingle(cs, critLogGroupPrefix) && cs[0].positive() + case driver.AccountPolicyFieldIndex: + return fieldIndexCriteriaAllowed(cs) + default: // METRIC_EXTRACTION_POLICY + return (isSingle(cs, critLogGroupName) || isSingle(cs, critLogGroupPrefix)) && cs[0].op != opEquals + } +} + +// positive reports an IN or = criterion. +func (c *criterion) positive() bool { return c.op == opIn || c.op == opEquals } + +// isSingle reports criteria made of one clause on key. +func isSingle(cs []criterion, key string) bool { return len(cs) == 1 && cs[0].key == key } + +// fieldIndexCriteriaAllowed accepts a LogGroupNamePrefix clause, or a +// DataSourceName clause AND a DataSourceType clause. +func fieldIndexCriteriaAllowed(cs []criterion) bool { + if isSingle(cs, critLogGroupPrefix) { + return cs[0].positive() + } + + const dataSourceClauses = 2 + + return len(cs) == dataSourceClauses && cs[0].key == critDataSourceName && cs[1].key == critDataSourceType && + cs[0].positive() && cs[1].positive() +} + +// criteriaKind groups policies that share a quota: "" for no criteria, or +// the first criterion's key. +func criteriaKind(cs []criterion) string { + if len(cs) == 0 { + return "" + } + + return cs[0].key +} + +func quotaFor(policyType, kind string) int { + switch { + case policyType == driver.AccountPolicyDataProtection, policyType == driver.AccountPolicySubscriptionFilter, kind == "": + return 1 + case policyType == driver.AccountPolicyTransformer: + return maxScopedTransformer + case policyType == driver.AccountPolicyFieldIndex: + return maxScopedFieldIndex + default: + return maxScopedMetricExtraction + } +} + +// checkAccountPolicyQuota applies the per-type limits and the no-overlap +// rule. Caller holds m.mu. +func (m *Mock) checkAccountPolicyQuota(ap *accountPolicy) error { + kind := criteriaKind(ap.criteria) + limit := quotaFor(ap.info.PolicyType, kind) + sameTypeOnly := ap.info.PolicyType == driver.AccountPolicyDataProtection || + ap.info.PolicyType == driver.AccountPolicySubscriptionFilter + count := 0 + + for _, other := range m.accountPolicies { + if other.info.PolicyType != ap.info.PolicyType || other.info.PolicyName == ap.info.PolicyName { + continue + } + + if sameTypeOnly || criteriaKind(other.criteria) == kind { + count++ + } + + if overlaps(ap.criteria, other.criteria) { + return errors.Newf(errors.InvalidArgument, + "The selectionCriteria of policy %s overlaps with the existing policy %s", ap.info.PolicyName, other.info.PolicyName) + } + } + + if count >= limit { + return errors.Newf(errors.ResourceExhausted, + "The account already has the maximum number of %s account policies", ap.info.PolicyType) + } + + return nil +} + +// overlaps reports whether two scoped policies of one type select the same +// log groups: prefix lists where one prefix starts with another, identical +// data sources, or two NOT IN policies. +func overlaps(a, b []criterion) bool { + if len(a) == 0 || len(b) == 0 { + return false + } + + // Only one NOT IN policy of a type is allowed, whatever its key. + if a[0].op == opNotIn && b[0].op == opNotIn { + return true + } + + if a[0].key != b[0].key || a[0].op == opNotIn || b[0].op == opNotIn { + return false + } + + if a[0].key == critDataSourceName { + return criteriaString(a) == criteriaString(b) + } + + return prefixesOverlap(a[0].values, b[0].values) +} + +// criteriaString renders criteria values for an equality check. +func criteriaString(cs []criterion) string { + parts := make([]string, 0, len(cs)) + for i := range cs { + parts = append(parts, strings.Join(cs[i].values, ",")) + } + + return strings.Join(parts, "/") +} + +// prefixesOverlap reports whether one value of a starts with one of b, or the +// other way round. +func prefixesOverlap(a, b []string) bool { + for _, x := range a { + for _, y := range b { + if strings.HasPrefix(x, y) || strings.HasPrefix(y, x) { + return true + } + } + } + + return false +} + +// DescribeAccountPolicies lists the account's policies of one type, by name. +func (m *Mock) DescribeAccountPolicies(_ context.Context, in driver.DescribeAccountPoliciesInput) ([]driver.AccountPolicy, error) { + if !validPolicyType(in.PolicyType) { + return nil, policyTypeError(in.PolicyType) + } + + if len(in.AccountIdentifiers) > 1 { + return nil, errors.New(errors.InvalidArgument, "accountIdentifiers can have at most 1 item") + } + + for _, id := range in.AccountIdentifiers { + if !accountIDRE.MatchString(id) { + return nil, errors.Newf(errors.InvalidArgument, "accountIdentifiers value %q must be a 12-digit account ID", id) + } + + if id != m.opts.AccountID { + return []driver.AccountPolicy{}, nil + } + } + + m.mu.RLock() + defer m.mu.RUnlock() + + out := []driver.AccountPolicy{} + + for _, ap := range m.accountPolicies { + if ap.info.PolicyType == in.PolicyType && (in.PolicyName == "" || ap.info.PolicyName == in.PolicyName) { + out = append(out, ap.info) + } + } + + sort.Slice(out, func(i, j int) bool { return out[i].PolicyName < out[j].PolicyName }) + + return out, nil +} + +// DeleteAccountPolicy removes an account policy. Events already masked by a +// data protection policy stay masked. +func (m *Mock) DeleteAccountPolicy(_ context.Context, name, policyType string) error { + if !validPolicyType(policyType) { + return policyTypeError(policyType) + } + + m.mu.Lock() + defer m.mu.Unlock() + + key := accountPolicyKey(policyType, name) + if _, ok := m.accountPolicies[key]; !ok { + return errors.Newf(errors.NotFound, "Policy with name [%s] does not exist", name) + } + + delete(m.accountPolicies, key) + + return nil +} + +// accountDataProtection is the account data protection policy, or nil. +func (m *Mock) accountDataProtection() *dataprotection.Policy { + m.mu.RLock() + defer m.mu.RUnlock() + + for _, ap := range m.accountPolicies { + if ap.dp != nil { + return ap.dp + } + } + + return nil +} + +// accountSubscription is the account subscription filter policy as a filter +// on group, or nil when there is none or the criteria exclude the group. +func (m *Mock) accountSubscription(group string) *driver.SubscriptionFilterInfo { + m.mu.RLock() + defer m.mu.RUnlock() + + for _, ap := range m.accountPolicies { + if ap.sub == nil { + continue + } + + for _, c := range ap.criteria { + for _, excluded := range c.values { + if excluded == group { + return nil + } + } + } + + sf := *ap.sub + sf.Name, sf.LogGroup, sf.CreatedAt = ap.info.PolicyName, group, ap.info.LastUpdatedTime + + return &sf + } + + return nil +} diff --git a/providers/aws/cloudwatchlogs/account_policies_test.go b/providers/aws/cloudwatchlogs/account_policies_test.go new file mode 100644 index 000000000..79db304ca --- /dev/null +++ b/providers/aws/cloudwatchlogs/account_policies_test.go @@ -0,0 +1,212 @@ +package cloudwatchlogs + +import ( + "context" + "strconv" + "testing" + "time" + + "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/services/logging/driver" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const subPolicyDoc = `{"DestinationArn":"` + lambdaDestARN + `","FilterPattern":"ERROR"}` + +func TestAccountPolicyLifecycle(t *testing.T) { + ctx := context.Background() + m := newTestMock() + + in := &driver.PutAccountPolicyInput{ + PolicyName: "idx", PolicyType: driver.AccountPolicyFieldIndex, + PolicyDocument: `{"Fields":["RequestId"]}`, SelectionCriteria: `LogGroupNamePrefix IN ["/app"]`, + } + + p, err := m.PutAccountPolicy(ctx, in) + require.NoError(t, err) + assert.Equal(t, driver.AccountPolicyScopeAll, p.Scope, "scope defaults to ALL") + assert.Equal(t, m.opts.AccountID, p.AccountID) + assert.Equal(t, testClockStart(), p.LastUpdatedTime) + + in.PolicyDocument = `{"Fields":["RequestId","TraceId"]}` + _, err = m.PutAccountPolicy(ctx, in) + require.NoError(t, err, "update in place") + + got, err := m.DescribeAccountPolicies(ctx, driver.DescribeAccountPoliciesInput{PolicyType: driver.AccountPolicyFieldIndex}) + require.NoError(t, err) + require.Len(t, got, 1) + assert.Equal(t, in.PolicyDocument, got[0].PolicyDocument) + assert.Equal(t, in.SelectionCriteria, got[0].SelectionCriteria) + + got, err = m.DescribeAccountPolicies(ctx, driver.DescribeAccountPoliciesInput{ + PolicyType: driver.AccountPolicyFieldIndex, PolicyName: "other", + }) + require.NoError(t, err) + assert.Empty(t, got) + + got, err = m.DescribeAccountPolicies(ctx, driver.DescribeAccountPoliciesInput{ + PolicyType: driver.AccountPolicyFieldIndex, AccountIdentifiers: []string{"111122223333"}, + }) + require.NoError(t, err) + assert.Empty(t, got, "another account's policies") + + err = m.DeleteAccountPolicy(ctx, "idx", driver.AccountPolicyTransformer) + assert.True(t, errors.IsNotFound(err), "wrong type: %v", err) + + require.NoError(t, m.DeleteAccountPolicy(ctx, "idx", driver.AccountPolicyFieldIndex)) + + err = m.DeleteAccountPolicy(ctx, "idx", driver.AccountPolicyFieldIndex) + assert.True(t, errors.IsNotFound(err), "second delete: %v", err) +} + +func TestPutAccountPolicyValidation(t *testing.T) { + ctx := context.Background() + m := newTestMock() + + tests := []struct { + name string + in driver.PutAccountPolicyInput + }{ + {"no name", driver.PutAccountPolicyInput{PolicyType: driver.AccountPolicyTransformer, PolicyDocument: "[]"}}, + {"aws/ name", driver.PutAccountPolicyInput{PolicyName: "aws/x", PolicyType: driver.AccountPolicyTransformer, PolicyDocument: "[]"}}, + {"bad type", driver.PutAccountPolicyInput{PolicyName: "p", PolicyType: "GLOBAL", PolicyDocument: "[]"}}, + {"bad scope", driver.PutAccountPolicyInput{ + PolicyName: "p", PolicyType: driver.AccountPolicyTransformer, PolicyDocument: "[]", Scope: "SOME", + }}, + {"no document", driver.PutAccountPolicyInput{PolicyName: "p", PolicyType: driver.AccountPolicyTransformer}}, + {"bad JSON", driver.PutAccountPolicyInput{PolicyName: "p", PolicyType: driver.AccountPolicyTransformer, PolicyDocument: "[{"}}, + {"bad data protection", driver.PutAccountPolicyInput{ + PolicyName: "p", PolicyType: driver.AccountPolicyDataProtection, PolicyDocument: `{"Statement":[]}`, + }}, + {"criteria on data protection", driver.PutAccountPolicyInput{ + PolicyName: "p", PolicyType: driver.AccountPolicyDataProtection, PolicyDocument: dpPolicy("EmailAddress"), + SelectionCriteria: `LogGroupName NOT IN ["a"]`, + }}, + {"subscription without destination", driver.PutAccountPolicyInput{ + PolicyName: "p", PolicyType: driver.AccountPolicySubscriptionFilter, PolicyDocument: `{"FilterPattern":"x"}`, + }}, + {"subscription bad pattern", driver.PutAccountPolicyInput{ + PolicyName: "p", PolicyType: driver.AccountPolicySubscriptionFilter, + PolicyDocument: `{"DestinationArn":"` + lambdaDestARN + `","FilterPattern":"{ $.a = "}`, + }}, + {"subscription bad distribution", driver.PutAccountPolicyInput{ + PolicyName: "p", PolicyType: driver.AccountPolicySubscriptionFilter, + PolicyDocument: `{"DestinationArn":"` + lambdaDestARN + `","Distribution":"Even"}`, + }}, + {"subscription IN criteria", driver.PutAccountPolicyInput{ + PolicyName: "p", PolicyType: driver.AccountPolicySubscriptionFilter, PolicyDocument: subPolicyDoc, + SelectionCriteria: `LogGroupName IN ["a"]`, + }}, + {"unparseable criteria", driver.PutAccountPolicyInput{ + PolicyName: "p", PolicyType: driver.AccountPolicyTransformer, PolicyDocument: "[]", + SelectionCriteria: `LogGroupNamePrefix LIKE "a"`, + }}, + {"field index without fields", driver.PutAccountPolicyInput{ + PolicyName: "p", PolicyType: driver.AccountPolicyFieldIndex, PolicyDocument: `{"Fields":[]}`, + }}, + {"field index duplicate field", driver.PutAccountPolicyInput{ + PolicyName: "p", PolicyType: driver.AccountPolicyFieldIndex, + PolicyDocument: `{"Fields":["a"],"FieldsV2":{"a":{"type":"FACET"}}}`, + }}, + {"field index bad type", driver.PutAccountPolicyInput{ + PolicyName: "p", PolicyType: driver.AccountPolicyFieldIndex, PolicyDocument: `{"FieldsV2":{"a":{"type":"X"}}}`, + }}, + {"metric extraction equals", driver.PutAccountPolicyInput{ + PolicyName: "p", PolicyType: driver.AccountPolicyMetricExtraction, PolicyDocument: `{}`, + SelectionCriteria: `LogGroupName = "a"`, + }}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + _, err := m.PutAccountPolicy(ctx, &tt.in) + assert.True(t, errors.IsInvalidArgument(err), "got %v", err) + }) + } + + _, err := m.DescribeAccountPolicies(ctx, driver.DescribeAccountPoliciesInput{}) + assert.True(t, errors.IsInvalidArgument(err), "describe without type: %v", err) + + _, err = m.DescribeAccountPolicies(ctx, driver.DescribeAccountPoliciesInput{ + PolicyType: driver.AccountPolicyTransformer, AccountIdentifiers: []string{"12"}, + }) + assert.True(t, errors.IsInvalidArgument(err), "bad account id: %v", err) +} + +func TestAccountPolicyQuotas(t *testing.T) { + ctx := context.Background() + m := newTestMock() + + put := func(name, typ, doc, crit string) error { + _, err := m.PutAccountPolicy(ctx, &driver.PutAccountPolicyInput{ + PolicyName: name, PolicyType: typ, PolicyDocument: doc, SelectionCriteria: crit, + }) + + return err + } + + require.NoError(t, put("dp1", driver.AccountPolicyDataProtection, dpPolicy("EmailAddress"), "")) + assert.Equal(t, errors.ResourceExhausted, errors.GetCode(put("dp2", driver.AccountPolicyDataProtection, dpPolicy("EmailAddress"), ""))) + + require.NoError(t, put("s1", driver.AccountPolicySubscriptionFilter, subPolicyDoc, "")) + assert.Equal(t, errors.ResourceExhausted, errors.GetCode(put("s2", driver.AccountPolicySubscriptionFilter, subPolicyDoc, ""))) + + require.NoError(t, put("t-all", driver.AccountPolicyTransformer, "[]", "")) + assert.Equal(t, errors.ResourceExhausted, errors.GetCode(put("t-all2", driver.AccountPolicyTransformer, "[]", ""))) + + for i := range maxScopedTransformer { + require.NoError(t, put("t"+strconv.Itoa(i), driver.AccountPolicyTransformer, "[]", + `LogGroupNamePrefix IN ["/g`+strconv.Itoa(i)+`x"]`)) + } + + assert.Equal(t, errors.ResourceExhausted, + errors.GetCode(put("t-over", driver.AccountPolicyTransformer, "[]", `LogGroupNamePrefix IN ["/new"]`))) + + err := put("t-overlap", driver.AccountPolicyTransformer, "[]", `LogGroupNamePrefix = "/g1xyz"`) + assert.True(t, errors.IsInvalidArgument(err), "overlapping prefix: %v", err) + + require.NoError(t, put("ds", driver.AccountPolicyFieldIndex, `{"Fields":["a"]}`, + `DataSourceName = "amazon_vpc" AND DataSourceType = "flow"`)) + err = put("ds2", driver.AccountPolicyFieldIndex, `{"Fields":["a"]}`, + `DataSourceName = "amazon_vpc" AND DataSourceType = "flow"`) + assert.True(t, errors.IsInvalidArgument(err), "duplicate data source: %v", err) + + require.NoError(t, put("me1", driver.AccountPolicyMetricExtraction, `{}`, `LogGroupNamePrefix NOT IN ["/aws"]`)) + err = put("me2", driver.AccountPolicyMetricExtraction, `{}`, `LogGroupName NOT IN ["x"]`) + assert.Error(t, err, "a second NOT IN policy") +} + +func TestAccountSubscriptionPolicyDelivers(t *testing.T) { + ctx := context.Background() + m := newTestMock() + inv := &recordingInvoker{} + m.SetLambdaInvoker(inv) + setupGroupAndStream(t, m) + + _, err := m.CreateLogGroup(ctx, driver.LogGroupConfig{Name: "excluded"}) + require.NoError(t, err) + _, err = m.CreateLogStream(ctx, "excluded", "s") + require.NoError(t, err) + + _, err = m.PutAccountPolicy(ctx, &driver.PutAccountPolicyInput{ + PolicyName: "acct-sub", PolicyType: driver.AccountPolicySubscriptionFilter, PolicyDocument: subPolicyDoc, + SelectionCriteria: `LogGroupName NOT IN ["excluded", "other"]`, + }) + require.NoError(t, err) + + ev := []driver.LogEvent{ + {Timestamp: testEventTime(), Message: "ERROR boom"}, + {Timestamp: testEventTime().Add(time.Second), Message: "INFO ok"}, + } + require.NoError(t, m.PutLogEvents(ctx, "test-group", "test-stream", ev)) + require.NoError(t, m.PutLogEvents(ctx, "excluded", "s", ev)) + + require.Len(t, inv.payloads, 1, "only the included group delivers") + + got := decodeAWSLogs(t, inv.payloads[0]) + assert.Equal(t, "test-group", got.LogGroup) + assert.Equal(t, []string{"acct-sub"}, got.SubscriptionFilters) + require.Len(t, got.LogEvents, 1) + assert.Equal(t, "ERROR boom", got.LogEvents[0].Message) +} diff --git a/providers/aws/cloudwatchlogs/cloudwatchlogs.go b/providers/aws/cloudwatchlogs/cloudwatchlogs.go index 016674947..3697b5faa 100644 --- a/providers/aws/cloudwatchlogs/cloudwatchlogs.go +++ b/providers/aws/cloudwatchlogs/cloudwatchlogs.go @@ -7,6 +7,7 @@ import ( "sort" "strings" "sync" + "sync/atomic" "time" "github.com/stackshy/cloudemu/v2/config" @@ -43,6 +44,8 @@ type logGroup struct { streams *memstore.Store[*logStream] metricFilters *memstore.Store[*driver.MetricFilterInfo] subFilters *memstore.Store[*driver.SubscriptionFilterInfo] + // dataProtection is the group's data protection policy, nil when unset. + dataProtection atomic.Pointer[groupPolicy] } // LambdaInvoker asynchronously invokes a Lambda function by ARN with a @@ -67,6 +70,8 @@ type Mock struct { // queryResultKmsKey is the account-wide key associated with Logs Insights // query results (AssociateKmsKey on the query-result resource). queryResultKmsKey string + // accountPolicies holds account policies by type and name, under mu. + accountPolicies map[string]*accountPolicy // periods holds each metric filter's per-period default-value state, // keyed by filterPeriodKey. periods map[string]filterPeriod @@ -110,6 +115,8 @@ func New(opts *config.Options) *Mock { exports: memstore.New[exportTask](), opts: opts, periods: map[string]filterPeriod{}, + + accountPolicies: map[string]*accountPolicy{}, } } @@ -235,8 +242,14 @@ func (m *Mock) PutLogEvents(ctx context.Context, groupName, streamName string, e return err } -// GetLogEvents retrieves log events matching the query. -func (m *Mock) GetLogEvents(_ context.Context, input *driver.LogQueryInput) ([]driver.LogEvent, error) { +// GetLogEvents retrieves log events matching the query. Events masked by a +// data protection policy come back masked. +func (m *Mock) GetLogEvents(ctx context.Context, input *driver.LogQueryInput) ([]driver.LogEvent, error) { + return m.GetAWSLogEvents(ctx, input, false) +} + +// GetAWSLogEvents is GetLogEvents with the unmask flag. +func (m *Mock) GetAWSLogEvents(_ context.Context, input *driver.LogQueryInput, unmask bool) ([]driver.LogEvent, error) { g, ok := m.groups.Get(input.LogGroup) if !ok { return nil, errors.Newf(errors.NotFound, "log group %q not found", input.LogGroup) @@ -257,10 +270,10 @@ func (m *Mock) GetLogEvents(_ context.Context, input *driver.LogQueryInput) ([]d return nil, errors.Newf(errors.NotFound, "log stream %q not found in group %q", input.LogStream, input.LogGroup) } - results = append(results, m.filterEvents(s, input)...) + results = append(results, m.filterEvents(s, input, unmask)...) } else { for _, s := range g.streams.SortedValues() { - events := m.filterEvents(s, input) + events := m.filterEvents(s, input, unmask) results = append(results, events...) } @@ -292,13 +305,15 @@ func sortLogEventsAscending(events []driver.LogEvent) { func (*Mock) filterEvents( s *logStream, input *driver.LogQueryInput, + unmask bool, ) []driver.LogEvent { s.mu.RLock() defer s.mu.RUnlock() var results []driver.LogEvent - for _, e := range s.events { + for i := range s.events { + e := &s.events[i] if !input.StartTime.IsZero() && e.Timestamp.Before(input.StartTime) { continue } @@ -311,16 +326,27 @@ func (*Mock) filterEvents( continue } - results = append(results, e) + results = append(results, eventView(e, unmask)) } return results } -// FilterLogEvents filters log events across streams using a pattern. +// FilterLogEvents filters log events across streams using a pattern. Events +// masked by a data protection policy come back masked. func (m *Mock) FilterLogEvents( + ctx context.Context, + input *driver.FilterLogEventsInput, +) ([]driver.FilteredLogEvent, error) { + return m.FilterAWSLogEvents(ctx, input, false) +} + +// FilterAWSLogEvents is FilterLogEvents with the unmask flag. The pattern +// matches the stored (masked) text either way. +func (m *Mock) FilterAWSLogEvents( _ context.Context, input *driver.FilterLogEventsInput, + unmask bool, ) ([]driver.FilteredLogEvent, error) { g, ok := m.groups.Get(input.LogGroup) if !ok { @@ -344,10 +370,10 @@ func (m *Mock) FilterLogEvents( var results []driver.FilteredLogEvent if input.LogStream != "" { - results = m.filterStreamEvents(g, input.LogStream, input, pattern) + results = m.filterStreamEvents(g, input.LogStream, input, pattern, unmask) } else { for name, s := range g.streams.All() { - results = append(results, m.matchEvents(s, name, input, pattern)...) + results = append(results, m.matchEvents(s, name, input, pattern, unmask)...) } // CloudWatch Logs interleaves matches across streams in ascending @@ -381,13 +407,14 @@ func (m *Mock) filterStreamEvents( streamName string, input *driver.FilterLogEventsInput, pattern *filterpattern.Pattern, + unmask bool, ) []driver.FilteredLogEvent { s, ok := g.streams.Get(streamName) if !ok { return nil } - return m.matchEvents(s, streamName, input, pattern) + return m.matchEvents(s, streamName, input, pattern, unmask) } func (*Mock) matchEvents( @@ -395,13 +422,15 @@ func (*Mock) matchEvents( streamName string, input *driver.FilterLogEventsInput, pattern *filterpattern.Pattern, + unmask bool, ) []driver.FilteredLogEvent { s.mu.RLock() defer s.mu.RUnlock() var results []driver.FilteredLogEvent - for _, e := range s.events { + for i := range s.events { + e := &s.events[i] if !input.StartTime.IsZero() && e.Timestamp.Before(input.StartTime) { continue } @@ -418,7 +447,7 @@ func (*Mock) matchEvents( LogStream: streamName, Timestamp: e.Timestamp, IngestionTime: e.IngestionTime, - Message: e.Message, + Message: eventView(e, unmask).Message, }) } diff --git a/providers/aws/cloudwatchlogs/data_protection.go b/providers/aws/cloudwatchlogs/data_protection.go new file mode 100644 index 000000000..a7af967e0 --- /dev/null +++ b/providers/aws/cloudwatchlogs/data_protection.go @@ -0,0 +1,162 @@ +package cloudwatchlogs + +import ( + "context" + "regexp" + "time" + + "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/providers/aws/cloudwatchlogs/dataprotection" + "github.com/stackshy/cloudemu/v2/services/logging/driver" +) + +// maxLogGroupIdentifierLength is the logGroupIdentifier length limit of the +// data protection operations. +const maxLogGroupIdentifierLength = 2048 + +var logGroupIdentifierRE = regexp.MustCompile(`^[\w#+=/:,.@-]+$`) + +// groupPolicy is a log group's data protection policy: the document as the +// caller sent it, and its parsed form. +type groupPolicy struct { + document string + updated time.Time + policy *dataprotection.Policy +} + +// groupByIdentifier resolves a log group name or ARN to its group. +func (m *Mock) groupByIdentifier(id string) (*logGroup, error) { + if id == "" || len(id) > maxLogGroupIdentifierLength || !logGroupIdentifierRE.MatchString(id) { + return nil, errors.New(errors.InvalidArgument, + "1 validation error detected: Value at 'logGroupIdentifier' failed to satisfy constraint: "+ + `Member must satisfy regular expression pattern: [\w#+=/:,.@-]*`) + } + + name := logGroupFromIdentifier(id) + + g, ok := m.groups.Get(name) + if !ok { + return nil, errors.Newf(errors.NotFound, "The specified log group does not exist: %s", name) + } + + return g, nil +} + +// parseDataProtection validates a policy document and checks that a findings +// destination log group exists. S3 and Firehose destinations are not checked. +func (m *Mock) parseDataProtection(doc string) (*dataprotection.Policy, error) { + p, err := dataprotection.Parse(doc) + if err != nil { + return nil, err + } + + if fg := p.FindingsLogGroup(); fg != "" && !m.groups.Has(fg) { + return nil, errors.Newf(errors.InvalidArgument, + "Invalid data protection policy: the findings destination log group %s does not exist", fg) + } + + return p, nil +} + +// PutDataProtectionPolicy sets a log group's data protection policy. Events +// ingested from now on are masked; earlier events are left as they are. +func (m *Mock) PutDataProtectionPolicy(_ context.Context, id, doc string) (*driver.DataProtectionPolicy, error) { + g, err := m.groupByIdentifier(id) + if err != nil { + return nil, err + } + + p, err := m.parseDataProtection(doc) + if err != nil { + return nil, err + } + + now := m.opts.Clock.Now().UTC() + g.dataProtection.Store(&groupPolicy{document: doc, updated: now, policy: p}) + + return &driver.DataProtectionPolicy{LogGroupIdentifier: id, Document: doc, LastUpdatedTime: now}, nil +} + +// GetDataProtectionPolicy returns a log group's policy. A group without one +// returns a policy with an empty document, as AWS does. +func (m *Mock) GetDataProtectionPolicy(_ context.Context, id string) (*driver.DataProtectionPolicy, error) { + g, err := m.groupByIdentifier(id) + if err != nil { + return nil, err + } + + out := &driver.DataProtectionPolicy{LogGroupIdentifier: id} + if gp := g.dataProtection.Load(); gp != nil { + out.Document, out.LastUpdatedTime = gp.document, gp.updated + } + + return out, nil +} + +// DeleteDataProtectionPolicy removes a log group's policy. Events masked while +// it was in force stay masked. +func (m *Mock) DeleteDataProtectionPolicy(_ context.Context, id string) error { + g, err := m.groupByIdentifier(id) + if err != nil { + return err + } + + if g.dataProtection.Swap(nil) == nil { + return errors.Newf(errors.NotFound, "No data protection policy found for log group %s", g.info.Name) + } + + return nil +} + +// maskingPolicies are the policies that apply to a group: its own and the +// account's. They are cumulative. +func (m *Mock) maskingPolicies(g *logGroup) []*dataprotection.Policy { + var out []*dataprotection.Policy + + if gp := g.dataProtection.Load(); gp != nil { + out = append(out, gp.policy) + } + + if p := m.accountDataProtection(); p != nil { + out = append(out, p) + } + + return out +} + +// maskEvents masks accepted events at ingest. A masked event keeps its +// original text in Unmasked for unmask reads. It returns new events; the +// caller's slice is not changed. +func (m *Mock) maskEvents(g *logGroup, events []driver.LogEvent) []driver.LogEvent { + policies := m.maskingPolicies(g) + out := make([]driver.LogEvent, len(events)) + + for i := range events { + out[i] = events[i] + out[i].Unmasked = "" + + if len(policies) == 0 { + continue + } + + if masked, ok := dataprotection.Mask(events[i].Message, policies...); ok { + out[i].Unmasked = events[i].Message + out[i].Message = masked + } + } + + return out +} + +// eventView is what a read returns for a stored event: the masked text, or +// with unmask the original text. Unmasked never leaves the provider. +func eventView(e *driver.LogEvent, unmask bool) driver.LogEvent { + out := *e + if unmask && out.Unmasked != "" { + out.Message = out.Unmasked + } + + out.Unmasked = "" + + return out +} diff --git a/providers/aws/cloudwatchlogs/data_protection_test.go b/providers/aws/cloudwatchlogs/data_protection_test.go new file mode 100644 index 000000000..cdff1dee8 --- /dev/null +++ b/providers/aws/cloudwatchlogs/data_protection_test.go @@ -0,0 +1,274 @@ +package cloudwatchlogs + +import ( + "context" + "strings" + "testing" + "time" + + "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/providers/aws/cloudwatchlogs/insights" + "github.com/stackshy/cloudemu/v2/services/logging/driver" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const dpARN = "arn:aws:dataprotection::aws:data-identifier/" + +// dpPolicy builds a valid data protection policy over the managed names. +func dpPolicy(names ...string) string { + ids := make([]string, 0, len(names)) + for _, n := range names { + ids = append(ids, `"`+dpARN+n+`"`) + } + + list := strings.Join(ids, ",") + + return `{"Name":"p","Version":"2021-06-01","Statement":[ +{"Sid":"audit","DataIdentifier":[` + list + `],"Operation":{"Audit":{"FindingsDestination":{}}}}, +{"Sid":"redact","DataIdentifier":[` + list + `],"Operation":{"Deidentify":{"MaskConfig":{}}}}]}` +} + +func putOne(t *testing.T, m *Mock, msg string) { + t.Helper() + + require.NoError(t, m.PutLogEvents(context.Background(), "test-group", "test-stream", + []driver.LogEvent{{Timestamp: testEventTime(), Message: msg}})) +} + +func messages(events []driver.LogEvent) []string { + out := make([]string, 0, len(events)) + for i := range events { + out = append(out, events[i].Message) + } + + return out +} + +func TestDataProtectionMasksOnlyAfterPut(t *testing.T) { + ctx := context.Background() + m := newTestMock() + setupGroupAndStream(t, m) + + putOne(t, m, "before a@b.io") + + p, err := m.PutDataProtectionPolicy(ctx, "test-group", dpPolicy("EmailAddress")) + require.NoError(t, err) + assert.Equal(t, "test-group", p.LogGroupIdentifier) + assert.Equal(t, testClockStart(), p.LastUpdatedTime) + + putOne(t, m, "after c@d.io") + + q := &driver.LogQueryInput{LogGroup: "test-group", LogStream: "test-stream"} + + masked, err := m.GetLogEvents(ctx, q) + require.NoError(t, err) + assert.Equal(t, []string{"before a@b.io", "after ******"}, messages(masked)) + + for i := range masked { + assert.Empty(t, masked[i].Unmasked, "Unmasked must not leave the provider") + } + + clear, err := m.GetAWSLogEvents(ctx, q, true) + require.NoError(t, err) + assert.Equal(t, []string{"before a@b.io", "after c@d.io"}, messages(clear)) + + fin := &driver.FilterLogEventsInput{LogGroup: "test-group", FilterPattern: "after"} + + filtered, err := m.FilterLogEvents(ctx, fin) + require.NoError(t, err) + require.Len(t, filtered, 1) + assert.Equal(t, "after ******", filtered[0].Message) + + filtered, err = m.FilterAWSLogEvents(ctx, fin, true) + require.NoError(t, err) + require.Len(t, filtered, 1) + assert.Equal(t, "after c@d.io", filtered[0].Message) +} + +func TestDataProtectionGetLogRecordUnmask(t *testing.T) { + ctx := context.Background() + m := newTestMock() + setupGroupAndStream(t, m) + + _, err := m.PutDataProtectionPolicy(ctx, "test-group", dpPolicy("IpAddress")) + require.NoError(t, err) + putOne(t, m, "from 10.1.2.3") + + id, err := m.StartQuery(ctx, &driver.StartQueryInput{ + LogGroupName: "test-group", StartTime: testEventTime().Add(-time.Hour), EndTime: testClockStart(), + QueryString: "fields @message", + }) + require.NoError(t, err) + + res, err := m.GetQueryResults(ctx, id) + require.NoError(t, err) + require.Len(t, res.Results, 1) + assert.Equal(t, "from ********", rowField(res.Results[0], insights.FieldMessage)) + + ptr := rowField(res.Results[0], insights.FieldPtr) + + rec, err := m.GetLogRecord(ctx, ptr, false) + require.NoError(t, err) + assert.Equal(t, "from ********", rec[insights.FieldMessage]) + + rec, err = m.GetLogRecord(ctx, ptr, true) + require.NoError(t, err) + assert.Equal(t, "from 10.1.2.3", rec[insights.FieldMessage]) +} + +func TestDataProtectionPolicyLifecycle(t *testing.T) { + ctx := context.Background() + m := newTestMock() + setupGroupAndStream(t, m) + + arn := "arn:aws:logs:us-east-1:" + m.opts.AccountID + ":log-group:test-group" + + got, err := m.GetDataProtectionPolicy(ctx, arn) + require.NoError(t, err) + assert.Empty(t, got.Document, "no policy yet") + + _, err = m.PutDataProtectionPolicy(ctx, arn, dpPolicy("EmailAddress")) + require.NoError(t, err) + + got, err = m.GetDataProtectionPolicy(ctx, "test-group") + require.NoError(t, err) + assert.Equal(t, dpPolicy("EmailAddress"), got.Document) + + groups, err := m.ListAWSLogGroups(ctx, driver.ListLogGroupsFilter{}) + require.NoError(t, err) + assert.Equal(t, driver.DataProtectionStatusActivated, groups[0].DataProtectionStatus) + + require.NoError(t, m.DeleteDataProtectionPolicy(ctx, "test-group")) + + err = m.DeleteDataProtectionPolicy(ctx, "test-group") + assert.True(t, errors.IsNotFound(err), "second delete: %v", err) + + groups, err = m.ListAWSLogGroups(ctx, driver.ListLogGroupsFilter{}) + require.NoError(t, err) + assert.Empty(t, groups[0].DataProtectionStatus) + + putOne(t, m, "now a@b.io") + + events, err := m.GetLogEvents(ctx, &driver.LogQueryInput{LogGroup: "test-group"}) + require.NoError(t, err) + assert.Equal(t, []string{"now a@b.io"}, messages(events), "no masking after delete") +} + +func TestDataProtectionErrors(t *testing.T) { + ctx := context.Background() + m := newTestMock() + setupGroupAndStream(t, m) + + _, err := m.PutDataProtectionPolicy(ctx, "missing", dpPolicy("EmailAddress")) + assert.True(t, errors.IsNotFound(err), "missing group: %v", err) + + _, err = m.GetDataProtectionPolicy(ctx, "missing") + assert.True(t, errors.IsNotFound(err), "get missing group: %v", err) + + _, err = m.PutDataProtectionPolicy(ctx, "bad id!", dpPolicy("EmailAddress")) + assert.True(t, errors.IsInvalidArgument(err), "bad identifier: %v", err) + + _, err = m.PutDataProtectionPolicy(ctx, "test-group", `{"Statement":[]}`) + assert.True(t, errors.IsInvalidArgument(err), "bad document: %v", err) + + findings := strings.Replace(dpPolicy("EmailAddress"), `"FindingsDestination":{}`, + `"FindingsDestination":{"CloudWatchLogs":{"LogGroup":"no-such-group"}}`, 1) + _, err = m.PutDataProtectionPolicy(ctx, "test-group", findings) + assert.True(t, errors.IsInvalidArgument(err), "missing findings group: %v", err) + + _, err = m.PutDataProtectionPolicy(ctx, "test-group", strings.Replace(findings, "no-such-group", "test-group", 1)) + assert.NoError(t, err, "existing findings group") +} + +func TestDataProtectionAccountPolicyIsCumulative(t *testing.T) { + ctx := context.Background() + m := newTestMock() + setupGroupAndStream(t, m) + + _, err := m.PutDataProtectionPolicy(ctx, "test-group", dpPolicy("EmailAddress")) + require.NoError(t, err) + + _, err = m.PutAccountPolicy(ctx, &driver.PutAccountPolicyInput{ + PolicyName: "acct", PolicyType: driver.AccountPolicyDataProtection, PolicyDocument: dpPolicy("IpAddress"), + }) + require.NoError(t, err) + + putOne(t, m, "a@b.io from 10.0.0.1") + + // A group created after the account policy is covered too. + _, err = m.CreateLogGroup(ctx, driver.LogGroupConfig{Name: "later"}) + require.NoError(t, err) + _, err = m.CreateLogStream(ctx, "later", "s") + require.NoError(t, err) + require.NoError(t, m.PutLogEvents(ctx, "later", "s", + []driver.LogEvent{{Timestamp: testEventTime(), Message: "a@b.io from 10.0.0.1"}})) + + events, err := m.GetLogEvents(ctx, &driver.LogQueryInput{LogGroup: "test-group"}) + require.NoError(t, err) + assert.Equal(t, []string{"****** from ********"}, messages(events)) + + events, err = m.GetLogEvents(ctx, &driver.LogQueryInput{LogGroup: "later"}) + require.NoError(t, err) + assert.Equal(t, []string{"a@b.io from ********"}, messages(events)) +} + +func TestDataProtectionMasksMetricFiltersAndSubscriptions(t *testing.T) { + ctx := context.Background() + m := newTestMock() + inv := &recordingInvoker{} + m.SetLambdaInvoker(inv) + setupGroupAndStream(t, m) + + require.NoError(t, m.PutSubscriptionFilter(ctx, &driver.SubscriptionFilterConfig{ + Name: "all", LogGroup: "test-group", DestinationARN: lambdaDestARN, + })) + + _, err := m.PutDataProtectionPolicy(ctx, "test-group", dpPolicy("EmailAddress")) + require.NoError(t, err) + putOne(t, m, "mail a@b.io") + + require.Len(t, inv.payloads, 1) + ev := decodeAWSLogs(t, inv.payloads[0]) + assert.Equal(t, "mail ******", ev.LogEvents[0].Message) +} + +func TestDataProtectionSnapshotRoundTrip(t *testing.T) { + ctx := context.Background() + src := newTestMock() + setupGroupAndStream(t, src) + + _, err := src.PutDataProtectionPolicy(ctx, "test-group", dpPolicy("EmailAddress")) + require.NoError(t, err) + _, err = src.PutAccountPolicy(ctx, &driver.PutAccountPolicyInput{ + PolicyName: "acct", PolicyType: driver.AccountPolicyDataProtection, PolicyDocument: dpPolicy("IpAddress"), + }) + require.NoError(t, err) + putOne(t, src, "a@b.io 10.0.0.1") + + raw, err := src.Snapshot(ctx, true) + require.NoError(t, err) + + dst := newTestMock() + require.NoError(t, dst.Restore(ctx, raw)) + + clear, err := dst.GetAWSLogEvents(ctx, &driver.LogQueryInput{LogGroup: "test-group"}, true) + require.NoError(t, err) + assert.Equal(t, []string{"a@b.io 10.0.0.1"}, messages(clear)) + + got, err := dst.GetDataProtectionPolicy(ctx, "test-group") + require.NoError(t, err) + assert.Equal(t, dpPolicy("EmailAddress"), got.Document) + assert.Equal(t, testClockStart(), got.LastUpdatedTime) + + accts, err := dst.DescribeAccountPolicies(ctx, driver.DescribeAccountPoliciesInput{PolicyType: driver.AccountPolicyDataProtection}) + require.NoError(t, err) + require.Len(t, accts, 1) + + // The restored policies still mask new events. + putOne(t, dst, "c@d.io 10.0.0.2") + + events, err := dst.GetLogEvents(ctx, &driver.LogQueryInput{LogGroup: "test-group"}) + require.NoError(t, err) + assert.Equal(t, []string{"****** ********", "****** ********"}, messages(events)) +} diff --git a/providers/aws/cloudwatchlogs/dataprotection/mask.go b/providers/aws/cloudwatchlogs/dataprotection/mask.go new file mode 100644 index 000000000..e0980f37f --- /dev/null +++ b/providers/aws/cloudwatchlogs/dataprotection/mask.go @@ -0,0 +1,168 @@ +package dataprotection + +import ( + "net" + "regexp" + "sort" + "strings" + "unicode/utf8" +) + +// The managed identifiers this emulator detects. Every regex runs on Go's RE2 +// engine, which matches in time linear in the input. +const ( + managedEmail = "EmailAddress" + managedIP = "IpAddress" + managedSecretKey = "AwsSecretKey" + managedCreditCard = "CreditCardNumber" +) + +var ( + emailRE = regexp.MustCompile(`[A-Za-z0-9._%+\-]+@[A-Za-z0-9\-]+(?:\.[A-Za-z0-9\-]+)*\.[A-Za-z]{2,}`) + ipv4RE = regexp.MustCompile( + `\b(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\b`) + ipv6RE = regexp.MustCompile(`[0-9A-Fa-f]{0,4}(?::[0-9A-Fa-f]{0,4}){2,7}`) + // AwsSecretKey needs a keyword next to the 40-character key, as on AWS. + secretKeyRE = regexp.MustCompile( + `(?i)(?:aws_?)?secret_?(?:access_?)?key["']?\s*[:=]\s*["']?([A-Za-z0-9/+]{40})(?:[^A-Za-z0-9/+=]|$)`) + creditCardRE = regexp.MustCompile(`\b\d(?:[ -]?\d){12,18}\b`) +) + +// detector finds one kind of sensitive data. group selects the submatch to +// mask (0 is the whole match); valid, when set, confirms a candidate. +type detector struct { + re *regexp.Regexp + group int + valid func(string) bool +} + +func isManagedName(name string) bool { + _, ok := managedDetector(name) + return ok +} + +func managedDetector(name string) (detector, bool) { + switch name { + case managedEmail: + return detector{re: emailRE}, true + case managedSecretKey: + return detector{re: secretKeyRE, group: 1}, true + case managedCreditCard: + return detector{re: creditCardRE, valid: luhn}, true + case managedIP: + return detector{re: ipv4RE}, true + default: + return detector{}, false + } +} + +// ipv6Detector backs IpAddress alongside ipv4RE: the candidate must parse as +// an IPv6 address. +func ipv6Detector() detector { + return detector{re: ipv6RE, valid: func(s string) bool { + ip := net.ParseIP(s) + return ip != nil && ip.To4() == nil && strings.ContainsAny(s, "0123456789abcdefABCDEF") + }} +} + +func (p *Policy) appendIPv6() { + for _, id := range p.identifiers { + if id == managedARNPrefix+managedIP { + p.detectors = append(p.detectors, ipv6Detector()) + return + } + } +} + +// Luhn check constants: the base, and the most a doubled digit may be before +// its digits are summed. +const ( + luhnBase = 10 + luhnMaxDoubled = 9 + minCardDigits = 13 + luhnDoubleEvery = 2 +) + +func luhn(s string) bool { + sum, n := 0, 0 + + for i := len(s) - 1; i >= 0; i-- { + c := s[i] + if c < '0' || c > '9' { + continue + } + + d := int(c - '0') + if n%luhnDoubleEvery == 1 { + d *= luhnDoubleEvery + if d > luhnMaxDoubled { + d -= luhnMaxDoubled + } + } + + sum += d + n++ + } + + return n >= minCardDigits && sum%luhnBase == 0 +} + +type span struct{ start, end int } + +// Mask replaces every match of the policies' identifiers in msg with one +// asterisk per character. It reports whether anything was masked. +func Mask(msg string, policies ...*Policy) (string, bool) { + var spans []span + + for _, p := range policies { + if p == nil { + continue + } + + for _, d := range p.detectors { + spans = d.find(msg, spans) + } + } + + if len(spans) == 0 { + return msg, false + } + + sort.Slice(spans, func(i, j int) bool { return spans[i].start < spans[j].start }) + + var b strings.Builder + + last := 0 + + for _, s := range spans { + if s.end <= last { + continue + } + + start := max(s.start, last) + b.WriteString(msg[last:start]) + b.WriteString(strings.Repeat("*", utf8.RuneCountInString(msg[start:s.end]))) + last = s.end + } + + b.WriteString(msg[last:]) + + return b.String(), true +} + +func (d *detector) find(msg string, spans []span) []span { + for _, m := range d.re.FindAllStringSubmatchIndex(msg, -1) { + start, end := m[2*d.group], m[2*d.group+1] + if start < 0 || end <= start { + continue + } + + if d.valid != nil && !d.valid(msg[start:end]) { + continue + } + + spans = append(spans, span{start, end}) + } + + return spans +} diff --git a/providers/aws/cloudwatchlogs/dataprotection/policy.go b/providers/aws/cloudwatchlogs/dataprotection/policy.go new file mode 100644 index 000000000..e661f25ce --- /dev/null +++ b/providers/aws/cloudwatchlogs/dataprotection/policy.go @@ -0,0 +1,298 @@ +// Package dataprotection parses CloudWatch Logs data protection policy +// documents and masks the sensitive data they name in log messages. +package dataprotection + +import ( + "bytes" + "encoding/json" + "regexp" + "slices" + "strings" + "unicode/utf8" + + "github.com/stackshy/cloudemu/v2/errors" +) + +// Policy document limits and fixed values, per the CloudWatch Logs docs. +const ( + MaxDocumentLength = 30720 + policyVersion = "2021-06-01" + managedARNPrefix = "arn:aws:dataprotection::aws:data-identifier/" + maxCustomIDs = 10 + maxCustomNameLength = 128 + maxCustomRegexLen = 200 + statementCount = 2 +) + +var ( + customNameRE = regexp.MustCompile(`^[A-Za-z0-9_-]+$`) + managedNameRE = regexp.MustCompile(`^[A-Za-z0-9-]+$`) + customRegexChar = regexp.MustCompile(`^[A-Za-z0-9_#=@/;,\- ^$?\[\]{}|\\*+.()]+$`) +) + +// Policy is a parsed, validated data protection policy. +type Policy struct { + identifiers []string + findingsLogGroup string + detectors []detector +} + +// FindingsLogGroup is the audit findings destination log group, or "". +func (p *Policy) FindingsLogGroup() string { return p.findingsLogGroup } + +// Identifiers returns the policy's data identifiers in document order. +func (p *Policy) Identifiers() []string { return slices.Clone(p.identifiers) } + +type document struct { + Name *string `json:"Name"` + Description *string `json:"Description"` + Version *string `json:"Version"` + Configuration *configuration `json:"Configuration"` + Statement []statement `json:"Statement"` +} + +type configuration struct { + CustomDataIdentifier []customIdentifier `json:"CustomDataIdentifier"` +} + +type customIdentifier struct { + Name string `json:"Name"` + Regex string `json:"Regex"` +} + +type statement struct { + Sid string `json:"Sid"` + DataIdentifier []string `json:"DataIdentifier"` + Operation map[string]json.RawMessage `json:"Operation"` +} + +type auditOperation struct { + FindingsDestination *struct { + CloudWatchLogs *struct { + LogGroup string `json:"LogGroup"` + } `json:"CloudWatchLogs"` + } `json:"FindingsDestination"` +} + +type deidentifyOperation struct { + MaskConfig json.RawMessage `json:"MaskConfig"` +} + +func invalid(format string, args ...any) error { + return errors.Newf(errors.InvalidArgument, "Invalid data protection policy: "+format, args...) +} + +// Parse validates a policy document: at most 30720 characters, Version +// 2021-06-01 when present, one Audit statement with a FindingsDestination and +// one Deidentify statement with an empty MaskConfig, both naming the same data +// identifiers. Identifiers are managed identifier ARNs or the names of custom +// identifiers declared under Configuration. +func Parse(doc string) (*Policy, error) { + if utf8.RuneCountInString(doc) > MaxDocumentLength { + return nil, errors.Newf(errors.InvalidArgument, + "1 validation error detected: Value at 'policyDocument' failed to satisfy constraint: "+ + "Member must have length less than or equal to %d", MaxDocumentLength) + } + + var d document + + dec := json.NewDecoder(strings.NewReader(doc)) + if err := dec.Decode(&d); err != nil || dec.More() { + return nil, invalid("the policy document is not a valid JSON object") + } + + if d.Version != nil && *d.Version != policyVersion { + return nil, invalid("Version must be %s", policyVersion) + } + + custom, err := parseCustom(d.Configuration) + if err != nil { + return nil, err + } + + p := &Policy{} + if err := p.parseStatements(d.Statement); err != nil { + return nil, err + } + + if err := p.resolveDetectors(custom); err != nil { + return nil, err + } + + return p, nil +} + +// resolveDetectors builds the detectors for the policy's identifiers. +func (p *Policy) resolveDetectors(custom map[string]*regexp.Regexp) error { + for _, id := range p.identifiers { + det, err := resolveIdentifier(id, custom) + if err != nil { + return err + } + + if det != nil { + p.detectors = append(p.detectors, *det) + } + } + + p.appendIPv6() + + return nil +} + +func (p *Policy) parseStatements(stmts []statement) error { + if len(stmts) != statementCount { + return invalid("the policy must have exactly two statements, one Audit and one Deidentify") + } + + var audit, deidentify []string + + for i := range stmts { + s := &stmts[i] + + op, err := parseStatement(s, i) + if err != nil { + return err + } + + switch { + case op.audit && audit == nil: + audit = s.DataIdentifier + p.findingsLogGroup = op.findingsLogGroup + case !op.audit && deidentify == nil: + deidentify = s.DataIdentifier + default: + return invalid("the policy must have exactly one Audit and one Deidentify statement") + } + } + + if !slices.Equal(audit, deidentify) { + return invalid("the DataIdentifier arrays of the Audit and Deidentify statements must match exactly") + } + + p.identifiers = audit + + return nil +} + +// parseStatement checks one statement's shape and parses its operation. +func parseStatement(s *statement, i int) (parsedOperation, error) { + if len(s.DataIdentifier) == 0 { + return parsedOperation{}, invalid("statement %d must have a non-empty DataIdentifier array", i+1) + } + + if len(s.Operation) != 1 { + return parsedOperation{}, invalid("statement %d must have exactly one Operation, Audit or Deidentify", i+1) + } + + return parseOperation(s.Operation) +} + +type parsedOperation struct { + audit bool + findingsLogGroup string +} + +func parseOperation(ops map[string]json.RawMessage) (parsedOperation, error) { + if raw, ok := ops["Audit"]; ok { + var a auditOperation + if err := json.Unmarshal(raw, &a); err != nil || a.FindingsDestination == nil { + return parsedOperation{}, invalid("the Audit operation must contain a FindingsDestination object") + } + + out := parsedOperation{audit: true} + if cwl := a.FindingsDestination.CloudWatchLogs; cwl != nil { + out.findingsLogGroup = cwl.LogGroup + } + + return out, nil + } + + raw, ok := ops["Deidentify"] + if !ok { + return parsedOperation{}, invalid("Operation must be Audit or Deidentify") + } + + var d deidentifyOperation + if err := json.Unmarshal(raw, &d); err != nil || d.MaskConfig == nil { + return parsedOperation{}, invalid("the Deidentify operation must contain a MaskConfig object") + } + + if !bytes.Equal(bytes.Join(bytes.Fields(d.MaskConfig), nil), []byte("{}")) { + return parsedOperation{}, invalid("MaskConfig must be an empty object") + } + + return parsedOperation{}, nil +} + +func parseCustom(cfg *configuration) (map[string]*regexp.Regexp, error) { + out := map[string]*regexp.Regexp{} + if cfg == nil { + return out, nil + } + + if len(cfg.CustomDataIdentifier) > maxCustomIDs { + return nil, invalid("a policy can have at most %d custom data identifiers", maxCustomIDs) + } + + for _, c := range cfg.CustomDataIdentifier { + re, err := compileCustom(c) + if err != nil { + return nil, err + } + + if _, dup := out[c.Name]; dup { + return nil, invalid("custom data identifier %q is defined more than once", c.Name) + } + + out[c.Name] = re + } + + return out, nil +} + +func compileCustom(c customIdentifier) (*regexp.Regexp, error) { + switch { + case c.Name == "" || len(c.Name) > maxCustomNameLength || !customNameRE.MatchString(c.Name): + return nil, invalid("custom data identifier name %q must be 1-%d characters of a-z, A-Z, 0-9, _ or -", + c.Name, maxCustomNameLength) + case isManagedName(c.Name): + return nil, invalid("custom data identifier %q has the name of a managed data identifier", c.Name) + case c.Regex == "" || len(c.Regex) > maxCustomRegexLen: + return nil, invalid("custom data identifier %q: Regex must be 1-%d characters", c.Name, maxCustomRegexLen) + case !customRegexChar.MatchString(c.Regex): + return nil, invalid("custom data identifier %q: Regex contains an unsupported character", c.Name) + } + + // Go's RE2 engine matches in linear time, so a hostile pattern cannot + // backtrack catastrophically; compile rejects oversized repeats. + re, err := regexp.Compile(c.Regex) + if err != nil { + return nil, invalid("custom data identifier %q: Regex is not valid", c.Name) + } + + return re, nil +} + +// resolveIdentifier maps a DataIdentifier entry to its detector. A managed +// identifier this emulator does not detect resolves to nil. +func resolveIdentifier(id string, custom map[string]*regexp.Regexp) (*detector, error) { + if name, ok := strings.CutPrefix(id, managedARNPrefix); ok { + if !managedNameRE.MatchString(name) { + return nil, invalid("%q is not a valid managed data identifier ARN", id) + } + + if det, known := managedDetector(name); known { + return &det, nil + } + + return nil, nil + } + + re, ok := custom[id] + if !ok { + return nil, invalid("data identifier %q is neither a managed data identifier ARN nor a custom data identifier", id) + } + + return &detector{re: re}, nil +} diff --git a/providers/aws/cloudwatchlogs/dataprotection/policy_test.go b/providers/aws/cloudwatchlogs/dataprotection/policy_test.go new file mode 100644 index 000000000..460f9177e --- /dev/null +++ b/providers/aws/cloudwatchlogs/dataprotection/policy_test.go @@ -0,0 +1,183 @@ +package dataprotection + +import ( + "strings" + "testing" + + "github.com/stackshy/cloudemu/v2/errors" +) + +const arn = "arn:aws:dataprotection::aws:data-identifier/" + +// doc builds a two-statement policy over ids, with an optional Configuration. +func doc(ids, config string) string { + cfg := "" + if config != "" { + cfg = `"Configuration":` + config + `,` + } + + return `{"Name":"p","Version":"2021-06-01",` + cfg + `"Statement":[ +{"Sid":"audit","DataIdentifier":[` + ids + `],"Operation":{"Audit":{"FindingsDestination":{}}}}, +{"Sid":"redact","DataIdentifier":[` + ids + `],"Operation":{"Deidentify":{"MaskConfig":{}}}}]}` +} + +func TestParseRejectsInvalidPolicies(t *testing.T) { + email := `"` + arn + `EmailAddress"` + ip := `"` + arn + `IpAddress"` + + tests := []struct { + name string + doc string + }{ + {"not JSON", `{"Statement":`}, + {"trailing data", doc(email, "") + `{}`}, + {"wrong version", strings.Replace(doc(email, ""), "2021-06-01", "2012-10-17", 1)}, + {"one statement", `{"Statement":[{"DataIdentifier":[` + email + `],"Operation":{"Audit":{"FindingsDestination":{}}}}]}`}, + {"two audits", strings.Replace(doc(email, ""), `{"Deidentify":{"MaskConfig":{}}}`, `{"Audit":{"FindingsDestination":{}}}`, 1)}, + {"audit without destination", strings.Replace(doc(email, ""), `"FindingsDestination":{}`, `"X":{}`, 1)}, + {"non-empty MaskConfig", strings.Replace(doc(email, ""), `"MaskConfig":{}`, `"MaskConfig":{"a":1}`, 1)}, + {"missing MaskConfig", strings.Replace(doc(email, ""), `"MaskConfig":{}`, `"Other":{}`, 1)}, + {"unknown operation", strings.Replace(doc(email, ""), `"Deidentify"`, `"Redact"`, 1)}, + {"two operations", strings.Replace(doc(email, ""), `{"Deidentify":{"MaskConfig":{}}}`, + `{"Deidentify":{"MaskConfig":{}},"Audit":{"FindingsDestination":{}}}`, 1)}, + {"mismatched identifiers", strings.Replace(doc(email, ""), `"Sid":"redact","DataIdentifier":[`+email, + `"Sid":"redact","DataIdentifier":[`+ip, 1)}, + {"empty identifiers", doc("", "")}, + {"undefined custom identifier", doc(`"EmployeeId"`, "")}, + {"bad managed ARN", doc(`"`+arn+`Email Address"`, "")}, + {"custom named like a managed identifier", doc(`"EmailAddress"`, + `{"CustomDataIdentifier":[{"Name":"EmailAddress","Regex":"x"}]}`)}, + {"custom regex too long", doc(`"Emp"`, + `{"CustomDataIdentifier":[{"Name":"Emp","Regex":"`+strings.Repeat("a", 201)+`"}]}`)}, + {"custom regex bad character", doc(`"Emp"`, `{"CustomDataIdentifier":[{"Name":"Emp","Regex":"a&b"}]}`)}, + {"custom regex does not compile", doc(`"Emp"`, `{"CustomDataIdentifier":[{"Name":"Emp","Regex":"a{2000}"}]}`)}, + {"custom name invalid", doc(`"E.mp"`, `{"CustomDataIdentifier":[{"Name":"E.mp","Regex":"x"}]}`)}, + {"duplicate custom", doc(`"Emp"`, + `{"CustomDataIdentifier":[{"Name":"Emp","Regex":"x"},{"Name":"Emp","Regex":"y"}]}`)}, + {"too long", doc(email, "") + strings.Repeat(" ", MaxDocumentLength)}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if _, err := Parse(tt.doc); !errors.IsInvalidArgument(err) { + t.Fatalf("Parse = %v, want InvalidArgument", err) + } + }) + } +} + +func TestParseCustomIdentifierLimit(t *testing.T) { + var defs, ids []string + + for i := range 11 { + n := "C" + strings.Repeat("x", i) + defs = append(defs, `{"Name":"`+n+`","Regex":"x"}`) + ids = append(ids, `"`+n+`"`) + } + + cfg := `{"CustomDataIdentifier":[` + strings.Join(defs, ",") + `]}` + if _, err := Parse(doc(strings.Join(ids, ","), cfg)); !errors.IsInvalidArgument(err) { + t.Fatalf("11 custom identifiers = %v, want InvalidArgument", err) + } + + cfg = `{"CustomDataIdentifier":[` + strings.Join(defs[:10], ",") + `]}` + if _, err := Parse(doc(strings.Join(ids[:10], ","), cfg)); err != nil { + t.Fatalf("10 custom identifiers: %v", err) + } +} + +func TestParseAcceptsAWSExample(t *testing.T) { + p, err := Parse(`{"Name":"data-protection-policy","Description":"test description","Version":"2021-06-01", +"Statement":[{"Sid":"audit-policy","DataIdentifier":["` + arn + `EmailAddress","` + arn + `DriversLicense-US"], +"Operation":{"Audit":{"FindingsDestination":{"CloudWatchLogs":{"LogGroup":"findings"},"S3":{"Bucket":"b"}}}}}, +{"Sid":"redact-policy","DataIdentifier":["` + arn + `EmailAddress","` + arn + `DriversLicense-US"], +"Operation":{"Deidentify":{"MaskConfig":{ }}}}]}`) + if err != nil { + t.Fatalf("Parse: %v", err) + } + + if p.FindingsLogGroup() != "findings" { + t.Fatalf("FindingsLogGroup = %q", p.FindingsLogGroup()) + } + + if got := p.Identifiers(); len(got) != 2 { + t.Fatalf("Identifiers = %v", got) + } + + // DriversLicense-US is accepted but not detected; the email still is. + if got, _ := Mask("mail a@b.io lic D1234567", p); got != "mail ****** lic D1234567" { + t.Fatalf("Mask = %q", got) + } +} + +func TestMask(t *testing.T) { + all := doc(`"`+arn+`EmailAddress","`+arn+`IpAddress","`+arn+`AwsSecretKey","`+arn+`CreditCardNumber","Emp"`, + `{"CustomDataIdentifier":[{"Name":"Emp","Regex":"EmployeeID-\\d{9}"}]}`) + + p, err := Parse(all) + if err != nil { + t.Fatalf("Parse: %v", err) + } + + tests := []struct { + name, in, want string + masked bool + }{ + {"email", "user alice@example.com logged in", "user ***************** logged in", true}, + {"ipv4", "from 10.0.0.12 ok", "from ********* ok", true}, + {"not an ipv4", "version 1.2.3 and 999.1.1.1", "version 1.2.3 and 999.1.1.1", false}, + {"ipv6", "from 2001:db8::1 ok", "from *********** ok", true}, + {"clock time is not ipv6", "at 12:30:45 ok", "at 12:30:45 ok", false}, + {"secret key", "aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY end", + "aws_secret_access_key=**************************************** end", true}, + {"40 chars without keyword", "token wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", "token wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", false}, + {"credit card", "card 4111 1111 1111 1111 ok", "card ******************* ok", true}, + {"luhn failure", "card 4111 1111 1111 1112 ok", "card 4111 1111 1111 1112 ok", false}, + {"custom", "id EmployeeID-123456789.", "id ********************.", true}, + {"nothing", "plain text", "plain text", false}, + {"non-ASCII prefix kept", "รฉ a@x.io", "รฉ ******", true}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, masked := Mask(tt.in, p) + if got != tt.want || masked != tt.masked { + t.Fatalf("Mask(%q) = %q, %v; want %q, %v", tt.in, got, masked, tt.want, tt.masked) + } + }) + } +} + +func TestMaskCumulativeAndNil(t *testing.T) { + email, err := Parse(doc(`"`+arn+`EmailAddress"`, "")) + if err != nil { + t.Fatal(err) + } + + ip, err := Parse(doc(`"`+arn+`IpAddress"`, "")) + if err != nil { + t.Fatal(err) + } + + if got, _ := Mask("a@b.io 1.2.3.4", email, nil, ip); got != "****** *******" { + t.Fatalf("cumulative Mask = %q", got) + } + + if got, masked := Mask("a@b.io"); got != "a@b.io" || masked { + t.Fatalf("no policies = %q, %v", got, masked) + } +} + +func TestMaskLinearOnHostileInput(t *testing.T) { + p, err := Parse(doc(`"Evil"`, `{"CustomDataIdentifier":[{"Name":"Evil","Regex":"(a+)+b"}]}`)) + if err != nil { + t.Fatalf("Parse: %v", err) + } + + // A backtracking engine takes exponential time to fail this match; RE2 + // fails it in linear time. + in := strings.Repeat("a", 100000) + "!" + if _, masked := Mask(in, p); masked { + t.Fatal("(a+)+b must not match a run of a's") + } +} diff --git a/providers/aws/cloudwatchlogs/ingest.go b/providers/aws/cloudwatchlogs/ingest.go index 942a4381e..1bf80b82d 100644 --- a/providers/aws/cloudwatchlogs/ingest.go +++ b/providers/aws/cloudwatchlogs/ingest.go @@ -53,6 +53,9 @@ func (m *Mock) PutLogEventsWithResult( m.purgeExpired(groupName, g) accepted, rejected := m.ingestWindow(events, g.info.RetentionDays) + // Data protection masks at ingest, so every egress (reads, Insights, + // metric filters, subscriptions, exports) sees the masked text. + accepted = m.maskEvents(g, accepted) if len(accepted) > 0 && accepted[len(accepted)-1].Timestamp.Sub(accepted[0].Timestamp) > putLogEventsMaxBatchSpan { diff --git a/providers/aws/cloudwatchlogs/insights_queries.go b/providers/aws/cloudwatchlogs/insights_queries.go index 120a79273..400241d03 100644 --- a/providers/aws/cloudwatchlogs/insights_queries.go +++ b/providers/aws/cloudwatchlogs/insights_queries.go @@ -356,7 +356,7 @@ const ptrParts = 5 // and ingestionTime. The API reference's sample shows a 10-digit @timestamp, // but that sample is hand-written (pointer "123456789", invalid JSON); the // SDKs carry no fixture and LocalStack does not implement GetLogRecord. -func (m *Mock) GetLogRecord(_ context.Context, ptr string) (map[string]string, error) { +func (m *Mock) GetLogRecord(_ context.Context, ptr string, unmask bool) (map[string]string, error) { raw, err := base64.RawURLEncoding.DecodeString(ptr) parts := strings.Split(string(raw), ptrSep) @@ -379,7 +379,7 @@ func (m *Mock) GetLogRecord(_ context.Context, ptr string) (map[string]string, e rec := insights.NewRecord(&insights.Event{ Account: m.opts.AccountID, LogGroup: parts[0], LogStream: parts[1], - Timestamp: e.Timestamp, Ingestion: e.IngestionTime, Message: e.Message, + Timestamp: e.Timestamp, Ingestion: e.IngestionTime, Message: eventView(&e, unmask).Message, }).Strings() delete(rec, insights.FieldPtr) diff --git a/providers/aws/cloudwatchlogs/insights_queries_test.go b/providers/aws/cloudwatchlogs/insights_queries_test.go index ab5622c8b..3055126d3 100644 --- a/providers/aws/cloudwatchlogs/insights_queries_test.go +++ b/providers/aws/cloudwatchlogs/insights_queries_test.go @@ -76,7 +76,7 @@ func TestInsightsQueryLifecycle(t *testing.T) { require.Error(t, err) assert.True(t, errors.IsInvalidArgument(err)) - rec, err := m.GetLogRecord(ctx, rowField(res.Results[0], "@ptr")) + rec, err := m.GetLogRecord(ctx, rowField(res.Results[0], "@ptr"), false) require.NoError(t, err) assert.Equal(t, `{"level":"ERROR","latency":120,"user":{"id":"u1"}}`, rec["@message"]) assert.Equal(t, "u1", rec["user.id"]) @@ -276,6 +276,6 @@ func TestGetLogGroupFields(t *testing.T) { _, err = m.GetLogGroupFields(ctx, "nope", time.Time{}) assert.True(t, errors.IsNotFound(err)) - _, err = m.GetLogRecord(ctx, "not-a-pointer") + _, err = m.GetLogRecord(ctx, "not-a-pointer", false) assert.True(t, errors.IsInvalidArgument(err)) } diff --git a/providers/aws/cloudwatchlogs/log_groups.go b/providers/aws/cloudwatchlogs/log_groups.go index 8a570cff1..7b6c9cf02 100644 --- a/providers/aws/cloudwatchlogs/log_groups.go +++ b/providers/aws/cloudwatchlogs/log_groups.go @@ -105,7 +105,12 @@ func (g *logGroup) awsView() driver.AWSLogGroup { class = driver.LogGroupClassStandard } - return driver.AWSLogGroup{LogGroupInfo: info, KmsKeyID: g.kmsKeyID, LogGroupClass: class} + out := driver.AWSLogGroup{LogGroupInfo: info, KmsKeyID: g.kmsKeyID, LogGroupClass: class} + if g.dataProtection.Load() != nil { + out.DataProtectionStatus = driver.DataProtectionStatusActivated + } + + return out } // ListAWSLogGroups returns every log group matching filter, ordered by name. diff --git a/providers/aws/cloudwatchlogs/snapshot.go b/providers/aws/cloudwatchlogs/snapshot.go index 5fded4639..8f18fd1a7 100644 --- a/providers/aws/cloudwatchlogs/snapshot.go +++ b/providers/aws/cloudwatchlogs/snapshot.go @@ -5,9 +5,11 @@ import ( "encoding/json" "fmt" "maps" + "time" "github.com/stackshy/cloudemu/v2/internal/memstore" "github.com/stackshy/cloudemu/v2/internal/snapshot" + "github.com/stackshy/cloudemu/v2/providers/aws/cloudwatchlogs/dataprotection" "github.com/stackshy/cloudemu/v2/services/logging/driver" ) @@ -24,6 +26,7 @@ type logsSnapshot struct { FilterPeriods map[string]filterPeriod `json:"filterPeriods,omitempty"` Queries json.RawMessage `json:"queries,omitempty"` ExportTasks json.RawMessage `json:"exportTasks,omitempty"` + AccountPolicies []driver.AccountPolicy `json:"accountPolicies,omitempty"` } // logGroupSnapshot mirrors logGroup. Its metric- and subscription-filter stores @@ -37,6 +40,14 @@ type logGroupSnapshot struct { Streams map[string]*logStreamSnapshot `json:"streams,omitempty"` MetricFilters json.RawMessage `json:"metricFilters,omitempty"` SubFilters json.RawMessage `json:"subFilters,omitempty"` + // DataProtection is the group's data protection policy; events keep + // their original text in driver.LogEvent.Unmasked. + DataProtection *dataProtectionSnapshot `json:"dataProtection,omitempty"` +} + +type dataProtectionSnapshot struct { + Document string `json:"document"` + LastUpdatedTime time.Time `json:"lastUpdatedTime"` } // logStreamSnapshot mirrors logStream, promoting its fields past the unexported @@ -52,6 +63,10 @@ type logStreamSnapshot struct { func (m *Mock) Snapshot(_ context.Context, _ bool) (json.RawMessage, error) { m.mu.RLock() snap := logsSnapshot{QueryResultKmsKey: m.queryResultKmsKey, FilterPeriods: maps.Clone(m.periods)} + + for _, ap := range m.accountPolicies { + snap.AccountPolicies = append(snap.AccountPolicies, ap.info) + } m.mu.RUnlock() queries, err := m.queries.Snapshot() @@ -89,6 +104,10 @@ func (m *Mock) Snapshot(_ context.Context, _ bool) (json.RawMessage, error) { func snapshotGroup(g *logGroup) (*logGroupSnapshot, error) { gs := &logGroupSnapshot{Info: g.info, KmsKeyID: g.kmsKeyID, LogGroupClass: g.class} + if gp := g.dataProtection.Load(); gp != nil { + gs.DataProtection = &dataProtectionSnapshot{Document: gp.document, LastUpdatedTime: gp.updated} + } + mf, err := g.metricFilters.Snapshot() if err != nil { return nil, fmt.Errorf("cloudwatchlogs: snapshot metric filters: %w", err) @@ -132,6 +151,10 @@ func (m *Mock) Restore(_ context.Context, data json.RawMessage) error { maps.Copy(m.periods, snap.FilterPeriods) m.mu.Unlock() + if err := m.restoreAccountPolicies(snap.AccountPolicies); err != nil { + return err + } + if len(snap.Queries) > 0 { if err := m.queries.LoadSnapshot(snap.Queries); err != nil { return fmt.Errorf("cloudwatchlogs: restore queries: %w", err) @@ -182,5 +205,41 @@ func restoreGroup(gs *logGroupSnapshot) (*logGroup, error) { g.streams.Set(sName, &logStream{info: ss.Info, events: ss.Events, uploads: ss.Uploads}) } + if dp := gs.DataProtection; dp != nil { + p, err := dataprotection.Parse(dp.Document) + if err != nil { + return nil, fmt.Errorf("cloudwatchlogs: restore data protection policy: %w", err) + } + + g.dataProtection.Store(&groupPolicy{document: dp.Document, updated: dp.LastUpdatedTime, policy: p}) + } + return g, nil } + +// restoreAccountPolicies rebuilds the account policies, re-parsing each +// document into the form ingest uses. +func (m *Mock) restoreAccountPolicies(infos []driver.AccountPolicy) error { + policies := make(map[string]*accountPolicy, len(infos)) + + for i := range infos { + in := &driver.PutAccountPolicyInput{ + PolicyName: infos[i].PolicyName, PolicyType: infos[i].PolicyType, PolicyDocument: infos[i].PolicyDocument, + Scope: infos[i].Scope, SelectionCriteria: infos[i].SelectionCriteria, + } + + ap, err := m.buildAccountPolicy(in, false) + if err != nil { + return fmt.Errorf("cloudwatchlogs: restore account policy %s: %w", infos[i].PolicyName, err) + } + + ap.info = infos[i] + policies[accountPolicyKey(ap.info.PolicyType, ap.info.PolicyName)] = ap + } + + m.mu.Lock() + m.accountPolicies = policies + m.mu.Unlock() + + return nil +} diff --git a/providers/aws/cloudwatchlogs/subscription_filters.go b/providers/aws/cloudwatchlogs/subscription_filters.go index cb4fcdd9f..c738c4d6d 100644 --- a/providers/aws/cloudwatchlogs/subscription_filters.go +++ b/providers/aws/cloudwatchlogs/subscription_filters.go @@ -103,13 +103,20 @@ func (m *Mock) DescribeSubscriptionFilters(_ context.Context, logGroup string) ( // carried forward so a subscriber Lambda that writes back into CloudWatch Logs // stays bounded (the guard lives in lambda.InvokeExternal). func (m *Mock) deliverToSubscriptions(callerCtx context.Context, g *logGroup, streamName string, events []driver.LogEvent) { - if m.lambda == nil || g.subFilters.Len() == 0 || len(events) == 0 { + if m.lambda == nil || len(events) == 0 { return } + // An account subscription filter policy delivers in addition to the + // group's own filters. + filters := g.subFilters.SortedValues() + if sf := m.accountSubscription(g.info.Name); sf != nil { + filters = append(filters, sf) + } + ctx := recursionguard.WithDepth(context.Background(), recursionguard.Depth(callerCtx)) - for _, sf := range g.subFilters.SortedValues() { + for _, sf := range filters { matched := matchSubscriptionEvents(sf.FilterPattern, events) if len(matched) == 0 { continue diff --git a/server/aws/cloudwatchlogs/account_policy_sdk_test.go b/server/aws/cloudwatchlogs/account_policy_sdk_test.go new file mode 100644 index 000000000..753dd83a2 --- /dev/null +++ b/server/aws/cloudwatchlogs/account_policy_sdk_test.go @@ -0,0 +1,108 @@ +package cloudwatchlogs_test + +import ( + "context" + "errors" + "testing" + "time" + + "github.com/aws/aws-sdk-go-v2/aws" + cwl "github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs" + cwltypes "github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs/types" +) + +func TestSDKAccountPolicyLifecycle(t *testing.T) { + c := newLogsClient(t) + ctx := context.Background() + + put, err := c.PutAccountPolicy(ctx, &cwl.PutAccountPolicyInput{ + PolicyName: aws.String("acct-dp"), PolicyType: cwltypes.PolicyTypeDataProtectionPolicy, + PolicyDocument: aws.String(sdkDPPolicy), + }) + if err != nil { + t.Fatalf("PutAccountPolicy: %v", err) + } + + ap := put.AccountPolicy + if ap.Scope != cwltypes.ScopeAll || aws.ToString(ap.AccountId) == "" || aws.ToInt64(ap.LastUpdatedTime) == 0 { + t.Fatalf("accountPolicy = %+v", ap) + } + + // The account policy masks every group, including one created later. + mustLogGroupStream(t, c, "/acct/g", "s") + mustSDK(t, "PutLogEvents", func() error { + _, err := c.PutLogEvents(ctx, &cwl.PutLogEventsInput{ + LogGroupName: aws.String("/acct/g"), LogStreamName: aws.String("s"), + LogEvents: []cwltypes.InputLogEvent{{Timestamp: aws.Int64(time.Now().UnixMilli()), Message: aws.String("to a@b.io")}}, + }) + + return err + }) + + ev, err := c.GetLogEvents(ctx, &cwl.GetLogEventsInput{LogGroupName: aws.String("/acct/g"), LogStreamName: aws.String("s")}) + if err != nil || aws.ToString(ev.Events[0].Message) != "to ******" { + t.Fatalf("GetLogEvents = %v %+v", err, ev) + } + + desc, err := c.DescribeAccountPolicies(ctx, &cwl.DescribeAccountPoliciesInput{ + PolicyType: cwltypes.PolicyTypeDataProtectionPolicy, PolicyName: aws.String("acct-dp"), + }) + if err != nil || len(desc.AccountPolicies) != 1 || aws.ToString(desc.AccountPolicies[0].PolicyDocument) != sdkDPPolicy { + t.Fatalf("DescribeAccountPolicies = %v %+v", err, desc) + } + + var lee *cwltypes.LimitExceededException + + _, err = c.PutAccountPolicy(ctx, &cwl.PutAccountPolicyInput{ + PolicyName: aws.String("acct-dp2"), PolicyType: cwltypes.PolicyTypeDataProtectionPolicy, + PolicyDocument: aws.String(sdkDPPolicy), + }) + if !errors.As(err, &lee) { + t.Fatalf("second data protection policy = %v, want LimitExceededException", err) + } + + mustSDK(t, "DeleteAccountPolicy", func() error { + _, err := c.DeleteAccountPolicy(ctx, &cwl.DeleteAccountPolicyInput{ + PolicyName: aws.String("acct-dp"), PolicyType: cwltypes.PolicyTypeDataProtectionPolicy, + }) + + return err + }) + + var rnf *cwltypes.ResourceNotFoundException + + _, err = c.DeleteAccountPolicy(ctx, &cwl.DeleteAccountPolicyInput{ + PolicyName: aws.String("acct-dp"), PolicyType: cwltypes.PolicyTypeDataProtectionPolicy, + }) + if !errors.As(err, &rnf) { + t.Fatalf("second delete = %v, want ResourceNotFoundException", err) + } +} + +func TestSDKAccountPolicyValidation(t *testing.T) { + c := newLogsClient(t) + ctx := context.Background() + + var ipe *cwltypes.InvalidParameterException + + _, err := c.PutAccountPolicy(ctx, &cwl.PutAccountPolicyInput{ + PolicyName: aws.String("aws/x"), PolicyType: cwltypes.PolicyTypeTransformerPolicy, PolicyDocument: aws.String("[]"), + }) + if !errors.As(err, &ipe) { + t.Fatalf("aws/ name = %v, want InvalidParameterException", err) + } + + _, err = c.PutAccountPolicy(ctx, &cwl.PutAccountPolicyInput{ + PolicyName: aws.String("sub"), PolicyType: cwltypes.PolicyTypeSubscriptionFilterPolicy, + PolicyDocument: aws.String(`{"DestinationArn":"arn:aws:lambda:us-east-1:000000000000:function:f"}`), + SelectionCriteria: aws.String(`LogGroupName IN ["a"]`), + }) + if !errors.As(err, &ipe) { + t.Fatalf("IN criteria on a subscription policy = %v, want InvalidParameterException", err) + } + + _, err = c.DescribeAccountPolicies(ctx, &cwl.DescribeAccountPoliciesInput{PolicyType: "GLOBAL"}) + if !errors.As(err, &ipe) { + t.Fatalf("describe with an unknown policyType = %v, want InvalidParameterException", err) + } +} diff --git a/server/aws/cloudwatchlogs/data_protection_sdk_test.go b/server/aws/cloudwatchlogs/data_protection_sdk_test.go new file mode 100644 index 000000000..c2f533177 --- /dev/null +++ b/server/aws/cloudwatchlogs/data_protection_sdk_test.go @@ -0,0 +1,135 @@ +package cloudwatchlogs_test + +import ( + "context" + "errors" + "strings" + "testing" + "time" + + "github.com/aws/aws-sdk-go-v2/aws" + cwl "github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs" + cwltypes "github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs/types" +) + +const sdkDPPolicy = `{"Name":"p","Version":"2021-06-01","Statement":[ +{"Sid":"audit","DataIdentifier":["arn:aws:dataprotection::aws:data-identifier/EmailAddress", +"arn:aws:dataprotection::aws:data-identifier/AwsSecretKey"],"Operation":{"Audit":{"FindingsDestination":{}}}}, +{"Sid":"redact","DataIdentifier":["arn:aws:dataprotection::aws:data-identifier/EmailAddress", +"arn:aws:dataprotection::aws:data-identifier/AwsSecretKey"],"Operation":{"Deidentify":{"MaskConfig":{}}}}]}` + +func TestSDKDataProtectionMaskAndUnmask(t *testing.T) { + c := newLogsClient(t) + ctx := context.Background() + g, s := "/dp/g", "s" + + mustLogGroupStream(t, c, g, s) + + put, err := c.PutDataProtectionPolicy(ctx, &cwl.PutDataProtectionPolicyInput{ + LogGroupIdentifier: aws.String(g), PolicyDocument: aws.String(sdkDPPolicy), + }) + if err != nil { + t.Fatalf("PutDataProtectionPolicy: %v", err) + } + + if aws.ToString(put.LogGroupIdentifier) != g || aws.ToInt64(put.LastUpdatedTime) == 0 { + t.Fatalf("put response = %+v", put) + } + + msg := "user a@b.io key aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY" + mustSDK(t, "PutLogEvents", func() error { + _, err := c.PutLogEvents(ctx, &cwl.PutLogEventsInput{ + LogGroupName: aws.String(g), LogStreamName: aws.String(s), + LogEvents: []cwltypes.InputLogEvent{{Timestamp: aws.Int64(time.Now().UnixMilli()), Message: aws.String(msg)}}, + }) + + return err + }) + + want := "user ****** key aws_secret_access_key=" + strings.Repeat("*", 40) + + for _, unmask := range []bool{false, true} { + ev, err := c.GetLogEvents(ctx, &cwl.GetLogEventsInput{ + LogGroupName: aws.String(g), LogStreamName: aws.String(s), Unmask: unmask, + }) + if err != nil || len(ev.Events) != 1 { + t.Fatalf("GetLogEvents unmask=%v: %v %+v", unmask, err, ev) + } + + fl, err := c.FilterLogEvents(ctx, &cwl.FilterLogEventsInput{LogGroupName: aws.String(g), Unmask: unmask}) + if err != nil || len(fl.Events) != 1 { + t.Fatalf("FilterLogEvents unmask=%v: %v %+v", unmask, err, fl) + } + + exp := want + if unmask { + exp = msg + } + + if got := aws.ToString(ev.Events[0].Message); got != exp { + t.Fatalf("GetLogEvents unmask=%v = %q, want %q", unmask, got, exp) + } + + if got := aws.ToString(fl.Events[0].Message); got != exp { + t.Fatalf("FilterLogEvents unmask=%v = %q, want %q", unmask, got, exp) + } + } + + groups, err := c.DescribeLogGroups(ctx, &cwl.DescribeLogGroupsInput{LogGroupNamePrefix: aws.String(g)}) + if err != nil || groups.LogGroups[0].DataProtectionStatus != cwltypes.DataProtectionStatusActivated { + t.Fatalf("DescribeLogGroups dataProtectionStatus: %v %+v", err, groups) + } +} + +func TestSDKDataProtectionPolicyLifecycle(t *testing.T) { + c := newLogsClient(t) + ctx := context.Background() + g := "/dp/life" + + mustLogGroupStream(t, c, g, "s") + + got, err := c.GetDataProtectionPolicy(ctx, &cwl.GetDataProtectionPolicyInput{LogGroupIdentifier: aws.String(g)}) + if err != nil || got.PolicyDocument != nil { + t.Fatalf("Get before put = %v, %+v; want no document", err, got) + } + + arn := "arn:aws:logs:us-east-1:000000000000:log-group:" + g + mustSDK(t, "PutDataProtectionPolicy", func() error { + _, err := c.PutDataProtectionPolicy(ctx, &cwl.PutDataProtectionPolicyInput{ + LogGroupIdentifier: aws.String(arn), PolicyDocument: aws.String(sdkDPPolicy), + }) + + return err + }) + + got, err = c.GetDataProtectionPolicy(ctx, &cwl.GetDataProtectionPolicyInput{LogGroupIdentifier: aws.String(g)}) + if err != nil || aws.ToString(got.PolicyDocument) != sdkDPPolicy { + t.Fatalf("Get after put = %v, %+v", err, got) + } + + mustSDK(t, "DeleteDataProtectionPolicy", func() error { + _, err := c.DeleteDataProtectionPolicy(ctx, &cwl.DeleteDataProtectionPolicyInput{LogGroupIdentifier: aws.String(g)}) + return err + }) + + var rnf *cwltypes.ResourceNotFoundException + + _, err = c.DeleteDataProtectionPolicy(ctx, &cwl.DeleteDataProtectionPolicyInput{LogGroupIdentifier: aws.String(g)}) + if !errors.As(err, &rnf) { + t.Fatalf("second delete = %v, want ResourceNotFoundException", err) + } + + _, err = c.GetDataProtectionPolicy(ctx, &cwl.GetDataProtectionPolicyInput{LogGroupIdentifier: aws.String("/dp/none")}) + if !errors.As(err, &rnf) { + t.Fatalf("missing group = %v, want ResourceNotFoundException", err) + } + + var ipe *cwltypes.InvalidParameterException + + _, err = c.PutDataProtectionPolicy(ctx, &cwl.PutDataProtectionPolicyInput{ + LogGroupIdentifier: aws.String(g), PolicyDocument: aws.String(`{"Statement":[]}`), + }) + if !errors.As(err, &ipe) { + t.Fatalf("bad document = %v, want InvalidParameterException", err) + } +} diff --git a/server/aws/cloudwatchlogs/export_tasks.go b/server/aws/cloudwatchlogs/export_tasks.go index 7c78d6a57..76b152eae 100644 --- a/server/aws/cloudwatchlogs/export_tasks.go +++ b/server/aws/cloudwatchlogs/export_tasks.go @@ -11,8 +11,8 @@ import ( // maxDescribeExportTasks is the DescribeExportTasks limit maximum and default. const maxDescribeExportTasks = 50 -// dispatchExport handles the export task operations, delegating tagging to -// dispatchTags. +// dispatchExport handles the export task operations, delegating the policy +// operations to dispatchPolicies. func (h *Handler) dispatchExport(w http.ResponseWriter, r *http.Request, op string) { switch op { case "CreateExportTask": @@ -22,7 +22,7 @@ func (h *Handler) dispatchExport(w http.ResponseWriter, r *http.Request, op stri case "CancelExportTask": h.cancelExportTask(w, r) default: - h.dispatchTags(w, r, op) + h.dispatchPolicies(w, r, op) } } diff --git a/server/aws/cloudwatchlogs/handler.go b/server/aws/cloudwatchlogs/handler.go index 8d7edff4b..7c695e290 100644 --- a/server/aws/cloudwatchlogs/handler.go +++ b/server/aws/cloudwatchlogs/handler.go @@ -20,6 +20,8 @@ // AssociateKmsKey DisassociateKmsKey TestMetricFilter // StartQuery GetQueryResults StopQuery DescribeQueries GetLogRecord GetLogGroupFields // CreateExportTask DescribeExportTasks CancelExportTask +// PutDataProtectionPolicy GetDataProtectionPolicy DeleteDataProtectionPolicy +// PutAccountPolicy DescribeAccountPolicies DeleteAccountPolicy // TagResource / UntagResource / ListTagsForResource (and the legacy *LogGroup forms) // // CloudWatch Logs-only operations go through the optional driver.AWSLogging diff --git a/server/aws/cloudwatchlogs/insights.go b/server/aws/cloudwatchlogs/insights.go index 7328a4179..df6b3f4df 100644 --- a/server/aws/cloudwatchlogs/insights.go +++ b/server/aws/cloudwatchlogs/insights.go @@ -253,13 +253,14 @@ func (h *Handler) getLogRecord(w http.ResponseWriter, r *http.Request) { var req struct { LogRecordPointer string `json:"logRecordPointer"` + Unmask bool `json:"unmask"` } if !wire.DecodeJSON(w, r, &req) { return } - rec, err := ext.GetLogRecord(r.Context(), req.LogRecordPointer) + rec, err := ext.GetLogRecord(r.Context(), req.LogRecordPointer, req.Unmask) if err != nil { writeErr(w, err) return diff --git a/server/aws/cloudwatchlogs/operations.go b/server/aws/cloudwatchlogs/operations.go index 80e64f44a..ab39254dc 100644 --- a/server/aws/cloudwatchlogs/operations.go +++ b/server/aws/cloudwatchlogs/operations.go @@ -381,13 +381,13 @@ func (h *Handler) getLogEvents(w http.ResponseWriter, r *http.Request) { // Fetch the full ordered slice (Limit -1 = no cap) and page it here so the // forward / backward tokens can carry a real position across all events. - events, err := h.logs.GetLogEvents(r.Context(), &logdriver.LogQueryInput{ + events, err := h.readLogEvents(r, &logdriver.LogQueryInput{ LogGroup: req.LogGroupName, LogStream: req.LogStreamName, StartTime: millisToTime(req.StartTime), EndTime: millisToTime(req.EndTime), Limit: -1, - }) + }, req.Unmask) if err != nil { writeErr(w, err) return @@ -442,14 +442,14 @@ func (h *Handler) filterLogEvents(w http.ResponseWriter, r *http.Request) { // Fetch every match (Limit -1 = no cap) and page here so a NextToken can be // handed back across all matches. - events, err := h.logs.FilterLogEvents(r.Context(), &logdriver.FilterLogEventsInput{ + events, err := h.filterEvents(r, &logdriver.FilterLogEventsInput{ LogGroup: req.LogGroupName, LogStream: stream, FilterPattern: req.FilterPattern, StartTime: millisToTime(req.StartTime), EndTime: millisToTime(req.EndTime), Limit: -1, - }) + }, req.Unmask) if err != nil { writeErr(w, err) return @@ -486,6 +486,27 @@ func (h *Handler) filterLogEvents(w http.ResponseWriter, r *http.Request) { wire.WriteJSON(w, resp) } +// readLogEvents reads events with the unmask flag when the backend supports +// it, and through the portable driver otherwise. +func (h *Handler) readLogEvents(r *http.Request, in *logdriver.LogQueryInput, unmask bool) ([]logdriver.LogEvent, error) { + if ext, ok := h.logs.(logdriver.AWSLogging); ok { + return ext.GetAWSLogEvents(r.Context(), in, unmask) + } + + return h.logs.GetLogEvents(r.Context(), in) +} + +// filterEvents is readLogEvents for FilterLogEvents. +func (h *Handler) filterEvents( + r *http.Request, in *logdriver.FilterLogEventsInput, unmask bool, +) ([]logdriver.FilteredLogEvent, error) { + if ext, ok := h.logs.(logdriver.AWSLogging); ok { + return ext.FilterAWSLogEvents(r.Context(), in, unmask) + } + + return h.logs.FilterLogEvents(r.Context(), in) +} + // scopeFilteredStreams restricts filtered events to the caller's requested // streams. names limits results to that exact set; prefix limits them to streams // whose name starts with it. With neither set (or an empty names list), events diff --git a/server/aws/cloudwatchlogs/policies.go b/server/aws/cloudwatchlogs/policies.go new file mode 100644 index 000000000..ad4fee9d6 --- /dev/null +++ b/server/aws/cloudwatchlogs/policies.go @@ -0,0 +1,216 @@ +package cloudwatchlogs + +import ( + "net/http" + + "github.com/stackshy/cloudemu/v2/server/wire" + logdriver "github.com/stackshy/cloudemu/v2/services/logging/driver" +) + +// dispatchPolicies handles the data protection and account policy +// operations, delegating tagging to dispatchTags. +func (h *Handler) dispatchPolicies(w http.ResponseWriter, r *http.Request, op string) { + switch op { + case "PutDataProtectionPolicy": + h.putDataProtectionPolicy(w, r) + case "GetDataProtectionPolicy": + h.getDataProtectionPolicy(w, r) + case "DeleteDataProtectionPolicy": + h.deleteDataProtectionPolicy(w, r) + case "PutAccountPolicy": + h.putAccountPolicy(w, r) + case "DescribeAccountPolicies": + h.describeAccountPolicies(w, r) + case "DeleteAccountPolicy": + h.deleteAccountPolicy(w, r) + default: + h.dispatchTags(w, r, op) + } +} + +type dataProtectionPolicyRequest struct { + LogGroupIdentifier string `json:"logGroupIdentifier"` + PolicyDocument string `json:"policyDocument"` +} + +type dataProtectionPolicyResponse struct { + LogGroupIdentifier string `json:"logGroupIdentifier,omitempty"` + PolicyDocument string `json:"policyDocument,omitempty"` + LastUpdatedTime int64 `json:"lastUpdatedTime,omitempty"` +} + +func toDataProtectionResponse(p *logdriver.DataProtectionPolicy) dataProtectionPolicyResponse { + return dataProtectionPolicyResponse{ + LogGroupIdentifier: p.LogGroupIdentifier, + PolicyDocument: p.Document, + LastUpdatedTime: epochMillis(p.LastUpdatedTime), + } +} + +func (h *Handler) putDataProtectionPolicy(w http.ResponseWriter, r *http.Request) { + ext, ok := h.awsLogs(w, "PutDataProtectionPolicy") + if !ok { + return + } + + var req dataProtectionPolicyRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + p, err := ext.PutDataProtectionPolicy(r.Context(), req.LogGroupIdentifier, req.PolicyDocument) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, toDataProtectionResponse(p)) +} + +func (h *Handler) getDataProtectionPolicy(w http.ResponseWriter, r *http.Request) { + ext, ok := h.awsLogs(w, "GetDataProtectionPolicy") + if !ok { + return + } + + var req dataProtectionPolicyRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + p, err := ext.GetDataProtectionPolicy(r.Context(), req.LogGroupIdentifier) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, toDataProtectionResponse(p)) +} + +func (h *Handler) deleteDataProtectionPolicy(w http.ResponseWriter, r *http.Request) { + ext, ok := h.awsLogs(w, "DeleteDataProtectionPolicy") + if !ok { + return + } + + var req dataProtectionPolicyRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + if err := ext.DeleteDataProtectionPolicy(r.Context(), req.LogGroupIdentifier); err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, struct{}{}) +} + +type accountPolicyJSON struct { + AccountID string `json:"accountId"` + LastUpdatedTime int64 `json:"lastUpdatedTime"` + PolicyDocument string `json:"policyDocument"` + PolicyName string `json:"policyName"` + PolicyType string `json:"policyType"` + Scope string `json:"scope"` + SelectionCriteria string `json:"selectionCriteria,omitempty"` +} + +func toAccountPolicyJSON(p *logdriver.AccountPolicy) accountPolicyJSON { + return accountPolicyJSON{ + AccountID: p.AccountID, + LastUpdatedTime: epochMillis(p.LastUpdatedTime), + PolicyDocument: p.PolicyDocument, + PolicyName: p.PolicyName, + PolicyType: p.PolicyType, + Scope: p.Scope, + SelectionCriteria: p.SelectionCriteria, + } +} + +func (h *Handler) putAccountPolicy(w http.ResponseWriter, r *http.Request) { + ext, ok := h.awsLogs(w, "PutAccountPolicy") + if !ok { + return + } + + var req struct { + PolicyName string `json:"policyName"` + PolicyType string `json:"policyType"` + PolicyDocument string `json:"policyDocument"` + Scope string `json:"scope"` + SelectionCriteria string `json:"selectionCriteria"` + } + + if !wire.DecodeJSON(w, r, &req) { + return + } + + p, err := ext.PutAccountPolicy(r.Context(), &logdriver.PutAccountPolicyInput{ + PolicyName: req.PolicyName, PolicyType: req.PolicyType, PolicyDocument: req.PolicyDocument, + Scope: req.Scope, SelectionCriteria: req.SelectionCriteria, + }) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, map[string]accountPolicyJSON{"accountPolicy": toAccountPolicyJSON(p)}) +} + +// describeAccountPolicies returns every match on one page; an account holds +// a few dozen policies at most. +func (h *Handler) describeAccountPolicies(w http.ResponseWriter, r *http.Request) { + ext, ok := h.awsLogs(w, "DescribeAccountPolicies") + if !ok { + return + } + + var req struct { + PolicyType string `json:"policyType"` + PolicyName string `json:"policyName"` + AccountIdentifiers []string `json:"accountIdentifiers"` + } + + if !wire.DecodeJSON(w, r, &req) { + return + } + + policies, err := ext.DescribeAccountPolicies(r.Context(), logdriver.DescribeAccountPoliciesInput{ + PolicyType: req.PolicyType, PolicyName: req.PolicyName, AccountIdentifiers: req.AccountIdentifiers, + }) + if err != nil { + writeErr(w, err) + return + } + + out := make([]accountPolicyJSON, 0, len(policies)) + for i := range policies { + out = append(out, toAccountPolicyJSON(&policies[i])) + } + + wire.WriteJSON(w, map[string][]accountPolicyJSON{"accountPolicies": out}) +} + +func (h *Handler) deleteAccountPolicy(w http.ResponseWriter, r *http.Request) { + ext, ok := h.awsLogs(w, "DeleteAccountPolicy") + if !ok { + return + } + + var req struct { + PolicyName string `json:"policyName"` + PolicyType string `json:"policyType"` + } + + if !wire.DecodeJSON(w, r, &req) { + return + } + + if err := ext.DeleteAccountPolicy(r.Context(), req.PolicyName, req.PolicyType); err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, struct{}{}) +} diff --git a/server/aws/cloudwatchlogs/types.go b/server/aws/cloudwatchlogs/types.go index e37a61bf3..da7decaac 100644 --- a/server/aws/cloudwatchlogs/types.go +++ b/server/aws/cloudwatchlogs/types.go @@ -63,6 +63,7 @@ type getLogEventsRequest struct { Limit int32 `json:"limit"` NextToken string `json:"nextToken"` StartFromHead *bool `json:"startFromHead"` + Unmask bool `json:"unmask"` } type filterLogEventsRequest struct { @@ -74,6 +75,7 @@ type filterLogEventsRequest struct { EndTime int64 `json:"endTime"` Limit int32 `json:"limit"` NextToken string `json:"nextToken"` + Unmask bool `json:"unmask"` } // --- response envelopes --- @@ -91,6 +93,9 @@ type logGroupJSON struct { StoredBytes int64 `json:"storedBytes"` KmsKeyID string `json:"kmsKeyId,omitempty"` LogGroupClass string `json:"logGroupClass,omitempty"` + // DataProtectionStatus is ACTIVATED while the group has a data + // protection policy. + DataProtectionStatus string `json:"dataProtectionStatus,omitempty"` } type describeLogGroupsResponse struct { @@ -214,6 +219,8 @@ func toLogGroupJSON(g *logdriver.AWSLogGroup) logGroupJSON { StoredBytes: g.StoredBytes, KmsKeyID: g.KmsKeyID, LogGroupClass: g.LogGroupClass, + + DataProtectionStatus: g.DataProtectionStatus, } } diff --git a/services/logging/driver/aws.go b/services/logging/driver/aws.go index e86b7ad13..0be28e016 100644 --- a/services/logging/driver/aws.go +++ b/services/logging/driver/aws.go @@ -20,11 +20,17 @@ type AWSLogGroupOptions struct { LogGroupClass string } +// DataProtectionStatusActivated is the DescribeLogGroups dataProtectionStatus +// of a log group that has a data protection policy. +const DataProtectionStatusActivated = "ACTIVATED" + // AWSLogGroup is a log group with its CloudWatch Logs-only attributes. +// DataProtectionStatus is empty when the group has no data protection policy. type AWSLogGroup struct { LogGroupInfo - KmsKeyID string - LogGroupClass string + KmsKeyID string + LogGroupClass string + DataProtectionStatus string } // LogGroupTagFilter matches log groups that carry Key with any of Values (or @@ -192,6 +198,53 @@ type DescribeExportTasksInput struct { TaskID, StatusCode string } +// DataProtectionPolicy is a log group's data protection policy. Document is +// empty, and LastUpdatedTime zero, when the group has no policy. +type DataProtectionPolicy struct { + LogGroupIdentifier string + Document string + LastUpdatedTime time.Time +} + +// Account policy types and the only account policy scope. +const ( + AccountPolicyDataProtection = "DATA_PROTECTION_POLICY" + AccountPolicySubscriptionFilter = "SUBSCRIPTION_FILTER_POLICY" + AccountPolicyFieldIndex = "FIELD_INDEX_POLICY" + AccountPolicyTransformer = "TRANSFORMER_POLICY" + AccountPolicyMetricExtraction = "METRIC_EXTRACTION_POLICY" + AccountPolicyScopeAll = "ALL" +) + +// PutAccountPolicyInput is a PutAccountPolicy request. An empty Scope means ALL. +type PutAccountPolicyInput struct { + PolicyName string + PolicyType string + PolicyDocument string + Scope string + SelectionCriteria string +} + +// AccountPolicy is an account-level policy. A policy is identified by its +// type and name. +type AccountPolicy struct { + AccountID string + PolicyName string + PolicyType string + PolicyDocument string + Scope string + SelectionCriteria string + LastUpdatedTime time.Time +} + +// DescribeAccountPoliciesInput narrows DescribeAccountPolicies. PolicyType is +// required; PolicyName and AccountIdentifiers are optional filters. +type DescribeAccountPoliciesInput struct { + PolicyType string + PolicyName string + AccountIdentifiers []string +} + // AWSLogging is an optional interface implemented by the AWS CloudWatch Logs // backend for operations with no cross-provider equivalent. The CloudWatch // Logs wire handler type-asserts for it; Azure, GCP and OCI backends do not @@ -221,7 +274,8 @@ type AWSLogging interface { GetQueryResults(ctx context.Context, queryID string) (*QueryResults, error) StopQuery(ctx context.Context, queryID string) error DescribeQueries(ctx context.Context, input DescribeQueriesInput) ([]QueryInfo, error) - GetLogRecord(ctx context.Context, logRecordPointer string) (map[string]string, error) + // GetLogRecord returns the masked record unless unmask is set. + GetLogRecord(ctx context.Context, logRecordPointer string, unmask bool) (map[string]string, error) // GetLogGroupFields samples the 8 minutes either side of at, or the last // 15 minutes when at is zero. GetLogGroupFields(ctx context.Context, logGroup string, at time.Time) ([]LogGroupField, error) @@ -235,4 +289,22 @@ type AWSLogging interface { CreateExportTask(ctx context.Context, input *CreateExportTaskInput) (string, error) DescribeExportTasks(ctx context.Context, input DescribeExportTasksInput) ([]ExportTask, error) CancelExportTask(ctx context.Context, taskID string) error + + // GetAWSLogEvents and FilterAWSLogEvents are GetLogEvents and + // FilterLogEvents with the unmask flag: data protection masks events at + // ingest, and only an unmask read returns the original text. + GetAWSLogEvents(ctx context.Context, input *LogQueryInput, unmask bool) ([]LogEvent, error) + FilterAWSLogEvents(ctx context.Context, input *FilterLogEventsInput, unmask bool) ([]FilteredLogEvent, error) + + // Data protection policies take a log group name or ARN. Masking applies + // to events ingested after the policy is put. + PutDataProtectionPolicy(ctx context.Context, logGroupIdentifier, document string) (*DataProtectionPolicy, error) + GetDataProtectionPolicy(ctx context.Context, logGroupIdentifier string) (*DataProtectionPolicy, error) + DeleteDataProtectionPolicy(ctx context.Context, logGroupIdentifier string) error + + // Account policies apply to every log group in the account, or to the + // groups the selection criteria pick. + PutAccountPolicy(ctx context.Context, input *PutAccountPolicyInput) (*AccountPolicy, error) + DescribeAccountPolicies(ctx context.Context, input DescribeAccountPoliciesInput) ([]AccountPolicy, error) + DeleteAccountPolicy(ctx context.Context, policyName, policyType string) error } diff --git a/services/logging/driver/driver.go b/services/logging/driver/driver.go index ab39ef61c..28f95a03b 100644 --- a/services/logging/driver/driver.go +++ b/services/logging/driver/driver.go @@ -46,6 +46,10 @@ type LogEvent struct { // Timestamp. Zero when unknown. IngestionTime time.Time Message string + // Unmasked is the original text of an event that a CloudWatch Logs data + // protection policy masked at ingest. Message then holds the masked text. + // Only the AWS backend sets it, and it never returns it from a read. + Unmasked string `json:"Unmasked,omitempty"` } // LogQueryInput configures a log query operation. From 2eccc87aa65f10fe81b26c711a19297a8fb215bf Mon Sep 17 00:00:00 2001 From: Nitin Kumar Patel Date: Fri, 23 Oct 2026 08:57:39 +0530 Subject: [PATCH 18/31] fix(aws-cloudwatch): validate metric Period on GetMetricData and GetMetricStatistics (#1703) An invalid or huge Period overflowed the bucket step and crashed the server with a divide by zero. Period now follows the AWS rule and returns InvalidParameterValue, buckets are counted in int64 seconds, and the math alarm lookback check no longer overflows. --- providers/aws/cloudwatch/cloudwatch.go | 24 ++- providers/aws/cloudwatch/period.go | 52 +++++ providers/aws/cloudwatch/period_test.go | 84 ++++++++ .../aws/cloudwatch/core_alarm_statistics.go | 27 ++- server/aws/cloudwatch/core_metric_data.go | 51 ++++- server/aws/cloudwatch/core_metrics.go | 9 +- .../metric_period_validation_test.go | 193 ++++++++++++++++++ 7 files changed, 428 insertions(+), 12 deletions(-) create mode 100644 providers/aws/cloudwatch/period.go create mode 100644 providers/aws/cloudwatch/period_test.go create mode 100644 server/aws/cloudwatch/metric_period_validation_test.go diff --git a/providers/aws/cloudwatch/cloudwatch.go b/providers/aws/cloudwatch/cloudwatch.go index 79ac744a4..db2b671fd 100644 --- a/providers/aws/cloudwatch/cloudwatch.go +++ b/providers/aws/cloudwatch/cloudwatch.go @@ -178,6 +178,14 @@ func (m *Mock) PutMetricData(ctx context.Context, data []driver.MetricDatum) err // //nolint:gocritic // hugeParam: interface method signature cannot be changed. func (m *Mock) GetMetricData(_ context.Context, input driver.GetMetricInput) (*driver.MetricDataResult, error) { + // Zero keeps meaning the one-minute default. Any other value follows the + // AWS Period rule, so it cannot overflow the bucket step. + if input.Period != 0 { + if err := m.ValidatePeriod("Period", input.Period); err != nil { + return nil, err + } + } + return m.readMetric(&input), nil } @@ -200,7 +208,7 @@ func (m *Mock) readMetric(input *driver.GetMetricInput) *driver.MetricDataResult period := input.Period if period <= 0 { - period = 60 + period = defaultPeriodSeconds } return buildMetricResult(filtered, input.StartTime, period, input.Stat) @@ -272,20 +280,26 @@ func buildMetricResult(filtered []driver.MetricDatum, startTime time.Time, perio // unit reads back as None, like on AWS. result.Unit = alarmeval.EffectiveUnit(unitOf(filtered)) - periodDur := time.Duration(period) * time.Second + // Count in whole seconds, so a long period cannot overflow a Duration. + step := int64(period) + if step <= 0 { + return result + } + + bucket := func(ts time.Time) int64 { return int64(ts.Sub(startTime)/time.Second) / step } for from := 0; from < len(filtered); { - idx := filtered[from].Timestamp.Sub(startTime) / periodDur + idx := bucket(filtered[from].Timestamp) to := from + 1 - for to < len(filtered) && filtered[to].Timestamp.Sub(startTime)/periodDur == idx { + for to < len(filtered) && bucket(filtered[to].Timestamp) == idx { to++ } // A period where an extended statistic is not available, such as a // percentile over negative values, is left out as on AWS. if v, ok := alarmeval.StatValue(filtered[from:to], stat); ok { - result.Timestamps = append(result.Timestamps, startTime.Add(idx*periodDur)) + result.Timestamps = append(result.Timestamps, startTime.Add(time.Duration(idx*step)*time.Second)) result.Values = append(result.Values, v) } diff --git a/providers/aws/cloudwatch/period.go b/providers/aws/cloudwatch/period.go new file mode 100644 index 000000000..93558517f --- /dev/null +++ b/providers/aws/cloudwatch/period.go @@ -0,0 +1,52 @@ +package cloudwatch + +import ( + "math" + "slices" + + "github.com/stackshy/cloudemu/v2/errors" +) + +// Period rules from the MetricStat, MetricDataQuery and GetMetricStatistics +// API references. Period is an Integer of at least 1. A regular resolution +// period is a multiple of 60, and a high-resolution metric also allows 1, 5, +// 10, 20 and 30. +const ( + defaultPeriodSeconds = 60 + maxPeriodSeconds = math.MaxInt32 + periodSecondsInMinute = 60 +) + +// validPeriod reports whether p is 1, 5, 10, 20, 30 or a multiple of 60 that +// fits the API's Integer type. +func validPeriod(p int) bool { + switch { + case p < 1 || p > maxPeriodSeconds: + return false + case p%periodSecondsInMinute == 0: + return true + default: + periods := highResolutionPeriods() + + return slices.Contains(periods[:], p) + } +} + +// highResolutionPeriods are the periods under a minute that a high-resolution +// metric allows. +func highResolutionPeriods() [5]int { + return [5]int{1, 5, 10, 20, 30} +} + +// ValidatePeriod returns an InvalidArgument error when period is not a valid +// metric period. param names the request field in the message, such as +// "MetricDataQueries.member.1.MetricStat.Period". +func (*Mock) ValidatePeriod(param string, period int) error { + if validPeriod(period) { + return nil + } + + return errors.Newf(errors.InvalidArgument, + "The value %d for parameter %s is not a valid period. Valid periods are 1, 5, 10, 20, 30, 60 and any multiple of 60.", + period, param) +} diff --git a/providers/aws/cloudwatch/period_test.go b/providers/aws/cloudwatch/period_test.go new file mode 100644 index 000000000..345c050ac --- /dev/null +++ b/providers/aws/cloudwatch/period_test.go @@ -0,0 +1,84 @@ +package cloudwatch + +import ( + "context" + "strings" + "testing" + "time" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/services/monitoring/driver" +) + +func TestValidatePeriod(t *testing.T) { + m := newTestMock() + + for _, p := range []int{1, 5, 10, 20, 30, 60, 120, 300, 3600, 86400, 2147483640} { + if err := m.ValidatePeriod("Period", p); err != nil { + t.Errorf("period %d: unexpected error %v", p, err) + } + } + + for _, p := range []int{0, -1, -60, 2, 15, 45, 61, 90, 2147483700, 540431955284459520} { + err := m.ValidatePeriod("MetricDataQueries.member.1.MetricStat.Period", p) + if !cerrors.IsInvalidArgument(err) { + t.Fatalf("period %d: want InvalidArgument, got %v", p, err) + } + + if !strings.Contains(cerrors.Message(err), "parameter MetricDataQueries.member.1.MetricStat.Period is not a valid period") { + t.Errorf("period %d: message should name the parameter: %s", p, cerrors.Message(err)) + } + } +} + +// TestGetMetricDataRejectsBadPeriod covers a Period that used to overflow the +// bucket step to zero (divide by zero) or to a negative value. +func TestGetMetricDataRejectsBadPeriod(t *testing.T) { + m := newTestMock() + ctx := context.Background() + now := m.opts.Clock.Now() + + requireNoError(t, m.PutMetricData(ctx, []driver.MetricDatum{ + {Namespace: "App", MetricName: "Latency", Value: 5, Timestamp: now.Add(-time.Minute)}, + })) + + in := driver.GetMetricInput{ + Namespace: "App", MetricName: "Latency", StartTime: now.Add(-time.Hour), EndTime: now, Stat: "Sum", + } + + for _, p := range []int{-60, 61, 540431955284459520, 9223372036854775800} { + in.Period = p + + if _, err := m.GetMetricData(ctx, in); !cerrors.IsInvalidArgument(err) { + t.Fatalf("period %d: want InvalidArgument, got %v", p, err) + } + } + + in.Period = 0 + + res, err := m.GetMetricData(ctx, in) + requireNoError(t, err) + + if len(res.Values) != 1 || res.Values[0] != 5 { + t.Fatalf("zero period should default to a minute: %+v", res) + } +} + +// TestBuildMetricResultLongPeriod checks that a period longer than a +// Duration can hold still buckets every point into one period. +func TestBuildMetricResultLongPeriod(t *testing.T) { + start := time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC) + data := []driver.MetricDatum{ + {Value: 1, Timestamp: start.Add(time.Hour)}, + {Value: 2, Timestamp: start.Add(48 * time.Hour)}, + } + + res := buildMetricResult(data, start, 540431955284459520, "Sum") + if len(res.Values) != 1 || res.Values[0] != 3 || !res.Timestamps[0].Equal(start) { + t.Fatalf("want one bucket of 3 at start, got %+v", res) + } + + if res := buildMetricResult(data, start, 0, "Sum"); len(res.Values) != 0 { + t.Fatalf("a zero step should return no points, got %+v", res) + } +} diff --git a/server/aws/cloudwatch/core_alarm_statistics.go b/server/aws/cloudwatch/core_alarm_statistics.go index 59ff4dae0..007cc2275 100644 --- a/server/aws/cloudwatch/core_alarm_statistics.go +++ b/server/aws/cloudwatch/core_alarm_statistics.go @@ -67,7 +67,19 @@ func validateAlarmPeriods(cfg *mondriver.AlarmConfig) error { return newWireError(errValidation, "DatapointsToAlarm must be less than or equal to EvaluationPeriods.") } - if len(cfg.Metrics) > 0 || cfg.Period == 0 { + // validateQueryShape checks the MetricStat periods of a metric math + // alarm. Each one is held to the same lookback cap. + if len(cfg.Metrics) > 0 { + for _, p := range alarmPeriods(cfg) { + if err := validateAlarmLookback(p, cfg.EvaluationPeriods); err != nil { + return err + } + } + + return nil + } + + if cfg.Period == 0 { return nil } @@ -79,15 +91,20 @@ func validateAlarmPeriods(cfg *mondriver.AlarmConfig) error { } // validateAlarmLookback checks EvaluationPeriods * Period against the day or -// week cap that applies to period p. +// week cap that applies to period p. It divides instead of multiplying, so a +// huge Period or EvaluationPeriods cannot overflow past the cap. func validateAlarmLookback(p, evaluationPeriods int) error { - lookback := p * max(evaluationPeriods, 1) + if p <= 0 { + return nil + } + + n := max(evaluationPeriods, 1) switch { - case p < hourPeriod && lookback > maxAlarmLookbackDay: + case p < hourPeriod && n > maxAlarmLookbackDay/p: return newWireError(errValidation, "Metrics cannot be checked across more than a day "+ "(EvaluationPeriods * Period must be <= 86400)") - case p >= hourPeriod && lookback > maxAlarmLookbackWeek: + case p >= hourPeriod && n > maxAlarmLookbackWeek/p: return newWireError(errValidation, "Metrics cannot be checked across more than a week "+ "(EvaluationPeriods * Period must be <= 604800)") } diff --git a/server/aws/cloudwatch/core_metric_data.go b/server/aws/cloudwatch/core_metric_data.go index a1d8ed7f7..ae0cdc0f1 100644 --- a/server/aws/cloudwatch/core_metric_data.go +++ b/server/aws/cloudwatch/core_metric_data.go @@ -3,8 +3,10 @@ package cloudwatch import ( "context" "sort" + "strconv" "time" + cerrors "github.com/stackshy/cloudemu/v2/errors" mondriver "github.com/stackshy/cloudemu/v2/services/monitoring/driver" "github.com/stackshy/cloudemu/v2/services/monitoring/metricmath" ) @@ -57,6 +59,10 @@ func (h *Handler) getMetricDataCore(ctx context.Context, in *getMetricDataInput) return getMetricDataResult{}, err } + if err := h.validateQueryPeriods(in.MetricDataQueries); err != nil { + return getMetricDataResult{}, err + } + queries := toDriverQueries(in.MetricDataQueries) eval := h.metricDataEvaluator(ctx, queries, timeOrZero(in.StartTime), timeOrZero(in.EndTime)) rows := make([]metricDataRow, 0, len(in.MetricDataQueries)) @@ -71,7 +77,7 @@ func (h *Handler) getMetricDataCore(ctx context.Context, in *getMetricDataInput) qrows, err := metricDataRows(eval, q, descending) if err != nil { - return getMetricDataResult{}, err + return getMetricDataResult{}, invalidParameterError(err) } rows = append(rows, qrows...) @@ -82,6 +88,49 @@ func (h *Handler) getMetricDataCore(ctx context.Context, in *getMetricDataInput) return getMetricDataResult{Rows: page, NextToken: next}, nil } +// periodValidator is the AWS-local capability that checks a metric Period. +// param names the request field in the error message. +type periodValidator interface { + ValidatePeriod(param string, period int) error +} + +// validateQueryPeriods checks the MetricStat Period and the optional Period +// of every query, including ones that return no data. +func (h *Handler) validateQueryPeriods(queries []metricDataQueryCBR) error { + v, ok := h.monitoring.(periodValidator) + if !ok { + return nil + } + + for i := range queries { + member := "MetricDataQueries.member." + strconv.Itoa(i+1) + + if ms := queries[i].MetricStat; ms != nil { + if err := v.ValidatePeriod(member+".MetricStat.Period", ms.Period); err != nil { + return invalidParameterError(err) + } + } + + if p := queries[i].Period; p != 0 { + if err := v.ValidatePeriod(member+".Period", p); err != nil { + return invalidParameterError(err) + } + } + } + + return nil +} + +// invalidParameterError maps a driver InvalidArgument error to the +// InvalidParameterValue wire error and passes any other error through. +func invalidParameterError(err error) error { + if cerrors.IsInvalidArgument(err) { + return newWireError(errInvalidParameterValue, cerrors.Message(err)) + } + + return err +} + // bandExclusionSource is the AWS-local capability that returns the excluded // training ranges of the detector behind a band. type bandExclusionSource interface { diff --git a/server/aws/cloudwatch/core_metrics.go b/server/aws/cloudwatch/core_metrics.go index 5c3d69227..6ac0ed6d2 100644 --- a/server/aws/cloudwatch/core_metrics.go +++ b/server/aws/cloudwatch/core_metrics.go @@ -306,6 +306,13 @@ func (h *Handler) getMetricStatisticsCore( return getMetricStatisticsResult{}, err } + // Period is required, so zero is out of range here. + if v, ok := h.monitoring.(periodValidator); ok { + if err := v.ValidatePeriod("Period", in.Period); err != nil { + return getMetricStatisticsResult{}, invalidParameterError(err) + } + } + // Callers often ask for several statistics at once and expect all of them // on each datapoint. stats := requestedStats(in) @@ -335,7 +342,7 @@ func (h *Handler) getMetricStatisticsCore( res, err := h.monitoring.GetMetricData(ctx, q) if err != nil { - return getMetricStatisticsResult{}, err + return getMetricStatisticsResult{}, invalidParameterError(err) } acc.add(res, stat, unit) diff --git a/server/aws/cloudwatch/metric_period_validation_test.go b/server/aws/cloudwatch/metric_period_validation_test.go new file mode 100644 index 000000000..412ef9f11 --- /dev/null +++ b/server/aws/cloudwatch/metric_period_validation_test.go @@ -0,0 +1,193 @@ +package cloudwatch_test + +import ( + "io" + "net/http" + "net/http/httptest" + "net/url" + "strings" + "testing" + "time" + + "github.com/stackshy/cloudemu/v2/config" + cwprovider "github.com/stackshy/cloudemu/v2/providers/aws/cloudwatch" + cwserver "github.com/stackshy/cloudemu/v2/server/aws/cloudwatch" +) + +// periodRequestTimeout bounds each request, so a Period that sends the server +// into an endless loop fails the test instead of hanging it. +const periodRequestTimeout = 10 * time.Second + +// newPeriodPoster is newQueryPoster with a client timeout. It stores one +// datum of App/Latency so GetMetricData has points to bucket. +func newPeriodPoster(t *testing.T) func(url.Values) (int, string) { + t.Helper() + + ts := httptest.NewServer(cwserver.New(cwprovider.New(config.NewOptions()))) + t.Cleanup(ts.Close) + + client := &http.Client{Timeout: periodRequestTimeout} + + post := func(form url.Values) (int, string) { + req, _ := http.NewRequest(http.MethodPost, ts.URL+"/", strings.NewReader(form.Encode())) + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + req.Header.Set("Authorization", monitoringAuth) + + resp, err := client.Do(req) + if err != nil { + t.Fatalf("post %s: %v", form.Get("Action"), err) + } + defer resp.Body.Close() + + b, _ := io.ReadAll(resp.Body) + + return resp.StatusCode, string(b) + } + + if code, body := post(url.Values{ + "Action": {"PutMetricData"}, "Namespace": {"App"}, + "MetricData.member.1.MetricName": {"Latency"}, "MetricData.member.1.Value": {"5"}, + "MetricData.member.1.Timestamp": {time.Now().Add(-10 * time.Minute).UTC().Format(time.RFC3339)}, + }); code != http.StatusOK { + t.Fatalf("PutMetricData: code=%d body=%s", code, body) + } + + return post +} + +func periodMetricDataForm(statPeriod, exprPeriod string) url.Values { + form := url.Values{ + "Action": {"GetMetricData"}, + "StartTime": {time.Now().Add(-time.Hour).UTC().Format(time.RFC3339)}, + "EndTime": {time.Now().UTC().Format(time.RFC3339)}, + "MetricDataQueries.member.1.Id": {"m1"}, + "MetricDataQueries.member.1.MetricStat.Metric.Namespace": {"App"}, + "MetricDataQueries.member.1.MetricStat.Metric.MetricName": {"Latency"}, + "MetricDataQueries.member.1.MetricStat.Period": {statPeriod}, + "MetricDataQueries.member.1.MetricStat.Stat": {"Sum"}, + } + + if exprPeriod != "" { + form.Set("MetricDataQueries.member.2.Id", "e1") + form.Set("MetricDataQueries.member.2.Expression", "m1 * 2") + form.Set("MetricDataQueries.member.2.Period", exprPeriod) + } + + return form +} + +// TestGetMetricDataPeriodValidation checks the Period rule of MetricStat and +// MetricDataQuery: at least 1, and 1, 5, 10, 20, 30, 60 or a multiple of 60. +// A huge Period used to overflow the bucket step and crash or hang the server. +func TestGetMetricDataPeriodValidation(t *testing.T) { + post := newPeriodPoster(t) + + bad := []struct { + name, statPeriod, exprPeriod, param string + }{ + {"huge multiple of 60 overflowing to a zero step", "540431955284459520", "", "MetricDataQueries.member.1.MetricStat.Period"}, + {"huge near int64 max", "9223372036854775800", "", "MetricDataQueries.member.1.MetricStat.Period"}, + {"above int32", "2147483700", "", "MetricDataQueries.member.1.MetricStat.Period"}, + {"zero", "0", "", "MetricDataQueries.member.1.MetricStat.Period"}, + {"negative", "-60", "", "MetricDataQueries.member.1.MetricStat.Period"}, + {"not a multiple of 60", "61", "", "MetricDataQueries.member.1.MetricStat.Period"}, + {"high resolution value not allowed", "15", "", "MetricDataQueries.member.1.MetricStat.Period"}, + {"negative expression period", "60", "-60", "MetricDataQueries.member.2.Period"}, + {"huge expression period", "60", "540431955284459520", "MetricDataQueries.member.2.Period"}, + } + + for _, tc := range bad { + t.Run(tc.name, func(t *testing.T) { + code, body := post(periodMetricDataForm(tc.statPeriod, tc.exprPeriod)) + if code != http.StatusBadRequest || !strings.Contains(body, "InvalidParameterValue") { + t.Fatalf("want 400 InvalidParameterValue, got code=%d body=%s", code, body) + } + + if !strings.Contains(body, "parameter "+tc.param+" is not a valid period") { + t.Fatalf("message should name %s: %s", tc.param, body) + } + }) + } + + for _, p := range []string{"1", "5", "10", "20", "30", "60", "300", "86400"} { + t.Run("valid "+p, func(t *testing.T) { + code, body := post(periodMetricDataForm(p, p)) + if code != http.StatusOK { + t.Fatalf("period %s: code=%d body=%s", p, code, body) + } + + if !strings.Contains(body, "m1") || !strings.Contains(body, "5") { + t.Fatalf("period %s: want the stored datum back: %s", p, body) + } + }) + } +} + +// TestGetMetricStatisticsPeriodValidation applies the same Period rule to +// GetMetricStatistics, where Period is required. +func TestGetMetricStatisticsPeriodValidation(t *testing.T) { + post := newPeriodPoster(t) + + form := func(period string) url.Values { + return url.Values{ + "Action": {"GetMetricStatistics"}, "Namespace": {"App"}, "MetricName": {"Latency"}, + "StartTime": {time.Now().Add(-time.Hour).UTC().Format(time.RFC3339)}, + "EndTime": {time.Now().UTC().Format(time.RFC3339)}, + "Period": {period}, + "Statistics.member.1": {"Sum"}, + } + } + + for _, p := range []string{"0", "-60", "61", "540431955284459520"} { + t.Run("invalid "+p, func(t *testing.T) { + code, body := post(form(p)) + if code != http.StatusBadRequest || !strings.Contains(body, "InvalidParameterValue") || + !strings.Contains(body, "parameter Period is not a valid period") { + t.Fatalf("want 400 InvalidParameterValue, got code=%d body=%s", code, body) + } + }) + } + + code, body := post(form("300")) + if code != http.StatusOK || !strings.Contains(body, "5") { + t.Fatalf("valid period: code=%d body=%s", code, body) + } +} + +// TestPutMetricAlarmMathPeriodLookback checks that a metric math alarm's +// MetricStat Period obeys the EvaluationPeriods * Period cap, so a huge +// Period cannot reach the evaluator. +func TestPutMetricAlarmMathPeriodLookback(t *testing.T) { + post := newPeriodPoster(t) + + form := func(period, evalPeriods string) url.Values { + return url.Values{ + "Action": {"PutMetricAlarm"}, "AlarmName": {"math"}, "ComparisonOperator": {"GreaterThanThreshold"}, + "EvaluationPeriods": {evalPeriods}, "Threshold": {"1"}, + "Metrics.member.1.Id": {"m1"}, + "Metrics.member.1.ReturnData": {"false"}, + "Metrics.member.1.MetricStat.Metric.Namespace": {"App"}, + "Metrics.member.1.MetricStat.Metric.MetricName": {"Latency"}, + "Metrics.member.1.MetricStat.Period": {period}, + "Metrics.member.1.MetricStat.Stat": {"Sum"}, + "Metrics.member.2.Id": {"e1"}, + "Metrics.member.2.Expression": {"m1 * 2"}, + "Metrics.member.2.ReturnData": {"true"}, + } + } + + for _, tc := range []struct{ period, evalPeriods string }{ + {"540431955284459520", "1"}, + {"60", "1537228672809129302"}, + {"3600", "169"}, + } { + code, body := post(form(tc.period, tc.evalPeriods)) + if code != http.StatusBadRequest || !strings.Contains(body, "ValidationError") { + t.Fatalf("period %s x %s: want 400 ValidationError, got code=%d body=%s", tc.period, tc.evalPeriods, code, body) + } + } + + if code, body := post(form("3600", "168")); code != http.StatusOK { + t.Fatalf("a week of hourly periods: code=%d body=%s", code, body) + } +} From ec39f286b2957cda89e81f80d12b1b765cb2baad Mon Sep 17 00:00:00 2001 From: Nitin Kumar Patel Date: Fri, 23 Oct 2026 09:38:39 +0530 Subject: [PATCH 19/31] fix(aws-elasticache): typed faults, control-plane lock and id reservation (#1704) * fix(aws-elasticache): typed faults, control-plane lock, atomic creates * fix(aws-elasticache): reserved group member ids, drop tagMu, race tests for snapshot and parameter-group ops --- docs/coverage/aws/elasticache.md | 6 +- docs/coverage/azure/cache.md | 6 +- docs/coverage/gcp/memorystore.md | 6 +- docs/coverage/nongoals/cache.md | 5 + providers/aws/elasticache/concurrency_test.go | 756 ++++++++++++++++++ providers/aws/elasticache/elasticache.go | 386 ++++++--- providers/aws/elasticache/faults.go | 95 +++ providers/aws/elasticache/parameters.go | 34 +- providers/aws/elasticache/persist_snapshot.go | 17 +- .../aws/elasticache/replication_group.go | 283 +++++-- providers/aws/elasticache/snapshot.go | 65 +- providers/aws/elasticache/subnet_group.go | 96 ++- providers/aws/elasticache/tags.go | 123 ++- providers/aws/elasticache/validation.go | 2 +- server/aws/elasticache/faults.go | 60 ++ .../elasticache/faults_sdk_roundtrip_test.go | 88 ++ server/aws/elasticache/faults_test.go | 107 +++ server/aws/elasticache/handler.go | 81 -- 18 files changed, 1844 insertions(+), 372 deletions(-) create mode 100644 docs/coverage/nongoals/cache.md create mode 100644 providers/aws/elasticache/concurrency_test.go create mode 100644 providers/aws/elasticache/faults.go create mode 100644 server/aws/elasticache/faults.go create mode 100644 server/aws/elasticache/faults_sdk_roundtrip_test.go create mode 100644 server/aws/elasticache/faults_test.go diff --git a/docs/coverage/aws/elasticache.md b/docs/coverage/aws/elasticache.md index 8e609d706..5ce9a6320 100644 --- a/docs/coverage/aws/elasticache.md +++ b/docs/coverage/aws/elasticache.md @@ -63,4 +63,8 @@ SubnetGroups is an OPTIONAL capability, discovered by type assertion. Cache ## Not in scope -_Not documented yet. See the [emulator boundary](../../../README.md) for cloudemu-wide non-goals._ +- AWS: ElastiCache clusters, replication groups, and subnet and parameter groups share one id namespace across regions, so the same id cannot exist in two regions at once (#1693). +- AWS: ElastiCache snapshot ARNs cannot be tagged yet; AddTagsToResource and ListTagsForResource on a `:snapshot:` ARN answer not-found (#1695). +- AWS: with a real cache engine, a cluster or replication group reports `creating` only while the engine is being started and `deleting` only while it is being stopped. Without one, both are immediate unless `--async-settle` is set. +- AWS: a persisted snapshot (`--persist`, `/_cloudemu/snapshot`) leaves out ElastiCache clusters and replication groups that are still `creating` or `deleting` with a real engine, so they are not restored. +- AWS: while a replication group is `deleting` with a real engine, its member clusters (`-001`, โ€ฆ) are already gone from DescribeCacheClusters, though the group itself still shows `deleting`. diff --git a/docs/coverage/azure/cache.md b/docs/coverage/azure/cache.md index c42085ec8..9e1f69d8f 100644 --- a/docs/coverage/azure/cache.md +++ b/docs/coverage/azure/cache.md @@ -54,4 +54,8 @@ RedisChildren is an OPTIONAL capability, discovered by type assertion. Azure ## Not in scope -_Not documented yet. See the [emulator boundary](../../../README.md) for cloudemu-wide non-goals._ +- AWS: ElastiCache clusters, replication groups, and subnet and parameter groups share one id namespace across regions, so the same id cannot exist in two regions at once (#1693). +- AWS: ElastiCache snapshot ARNs cannot be tagged yet; AddTagsToResource and ListTagsForResource on a `:snapshot:` ARN answer not-found (#1695). +- AWS: with a real cache engine, a cluster or replication group reports `creating` only while the engine is being started and `deleting` only while it is being stopped. Without one, both are immediate unless `--async-settle` is set. +- AWS: a persisted snapshot (`--persist`, `/_cloudemu/snapshot`) leaves out ElastiCache clusters and replication groups that are still `creating` or `deleting` with a real engine, so they are not restored. +- AWS: while a replication group is `deleting` with a real engine, its member clusters (`-001`, โ€ฆ) are already gone from DescribeCacheClusters, though the group itself still shows `deleting`. diff --git a/docs/coverage/gcp/memorystore.md b/docs/coverage/gcp/memorystore.md index cb31efc52..7dba3ff7a 100644 --- a/docs/coverage/gcp/memorystore.md +++ b/docs/coverage/gcp/memorystore.md @@ -27,4 +27,8 @@ GCP's `cache` service ยท portable interface `driver.Cache` ยท [GCP index](./READ ## Not in scope -_Not documented yet. See the [emulator boundary](../../../README.md) for cloudemu-wide non-goals._ +- AWS: ElastiCache clusters, replication groups, and subnet and parameter groups share one id namespace across regions, so the same id cannot exist in two regions at once (#1693). +- AWS: ElastiCache snapshot ARNs cannot be tagged yet; AddTagsToResource and ListTagsForResource on a `:snapshot:` ARN answer not-found (#1695). +- AWS: with a real cache engine, a cluster or replication group reports `creating` only while the engine is being started and `deleting` only while it is being stopped. Without one, both are immediate unless `--async-settle` is set. +- AWS: a persisted snapshot (`--persist`, `/_cloudemu/snapshot`) leaves out ElastiCache clusters and replication groups that are still `creating` or `deleting` with a real engine, so they are not restored. +- AWS: while a replication group is `deleting` with a real engine, its member clusters (`-001`, โ€ฆ) are already gone from DescribeCacheClusters, though the group itself still shows `deleting`. diff --git a/docs/coverage/nongoals/cache.md b/docs/coverage/nongoals/cache.md new file mode 100644 index 000000000..a26745609 --- /dev/null +++ b/docs/coverage/nongoals/cache.md @@ -0,0 +1,5 @@ +- AWS: ElastiCache clusters, replication groups, and subnet and parameter groups share one id namespace across regions, so the same id cannot exist in two regions at once (#1693). +- AWS: ElastiCache snapshot ARNs cannot be tagged yet; AddTagsToResource and ListTagsForResource on a `:snapshot:` ARN answer not-found (#1695). +- AWS: with a real cache engine, a cluster or replication group reports `creating` only while the engine is being started and `deleting` only while it is being stopped. Without one, both are immediate unless `--async-settle` is set. +- AWS: a persisted snapshot (`--persist`, `/_cloudemu/snapshot`) leaves out ElastiCache clusters and replication groups that are still `creating` or `deleting` with a real engine, so they are not restored. +- AWS: while a replication group is `deleting` with a real engine, its member clusters (`-001`, โ€ฆ) are already gone from DescribeCacheClusters, though the group itself still shows `deleting`. diff --git a/providers/aws/elasticache/concurrency_test.go b/providers/aws/elasticache/concurrency_test.go new file mode 100644 index 000000000..40f849f46 --- /dev/null +++ b/providers/aws/elasticache/concurrency_test.go @@ -0,0 +1,756 @@ +package elasticache + +import ( + "context" + "errors" + "fmt" + "strconv" + "sync" + "testing" + "time" + + "github.com/stackshy/cloudemu/v2/config" + cerrors "github.com/stackshy/cloudemu/v2/errors" + driver "github.com/stackshy/cloudemu/v2/services/cache/driver" + "github.com/stackshy/cloudemu/v2/services/scope" +) + +const raceGoroutines = 50 + +// gatedCacheEngine is a real-engine stand-in whose Provision and Deprovision +// block until released, so a test can act while a create or delete is mid-way +// through its engine call. +type gatedCacheEngine struct { + provisionEntered chan struct{} + provisionRelease chan struct{} + deprovisionEntered chan struct{} + deprovisionRelease chan struct{} +} + +func newGatedCacheEngine() *gatedCacheEngine { + return &gatedCacheEngine{ + provisionEntered: make(chan struct{}, 1), + provisionRelease: make(chan struct{}), + deprovisionEntered: make(chan struct{}, 1), + deprovisionRelease: make(chan struct{}), + } +} + +func (e *gatedCacheEngine) Provision(_ context.Context, _ config.CacheProvisionRequest) (config.ProvisionResult, error) { + e.provisionEntered <- struct{}{} + <-e.provisionRelease + + return config.ProvisionResult{Host: "127.0.0.1", Port: 6379}, nil +} + +func (e *gatedCacheEngine) Deprovision(_ context.Context, _ string) error { + e.deprovisionEntered <- struct{}{} + <-e.deprovisionRelease + + return nil +} + +func newGatedMock() (*Mock, *gatedCacheEngine) { + eng := newGatedCacheEngine() + fc := config.NewFakeClock(time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)) + + return New(config.NewOptions(config.WithClock(fc), config.WithRegion("us-east-1"), config.WithCacheEngine(eng))), eng +} + +// faultCode returns the wire fault code carried by err, or "". +func faultCode(err error) string { + var f *Fault + if errors.As(err, &f) { + return f.AWSFault() + } + + return "" +} + +// runConcurrently runs fn on n goroutines started together and returns how +// many calls succeeded and the fault codes of the ones that failed. +func runConcurrently(n int, fn func() error) (successes int, codes []string) { + var ( + wg sync.WaitGroup + mu sync.Mutex + start = make(chan struct{}) + ) + + for range n { + wg.Add(1) + + go func() { + defer wg.Done() + + <-start + + err := fn() + + mu.Lock() + defer mu.Unlock() + + if err == nil { + successes++ + } else { + codes = append(codes, faultCode(err)) + } + }() + } + + close(start) + wg.Wait() + + return successes, codes +} + +func assertOneWinner(t *testing.T, successes int, codes []string, wantCode string) { + t.Helper() + + if successes != 1 { + t.Fatalf("successes = %d, want exactly 1", successes) + } + + for _, c := range codes { + if c != wantCode { + t.Fatalf("losing create got code %q, want %q", c, wantCode) + } + } +} + +func TestCreateCacheConcurrentDuplicate(t *testing.T) { + m, _ := newTestMock() + + successes, codes := runConcurrently(raceGoroutines, func() error { + _, err := m.CreateCache(context.Background(), driver.CacheConfig{Name: "dup"}) + return err + }) + + assertOneWinner(t, successes, codes, FaultCacheClusterAlreadyExists) +} + +func TestCreateReplicationGroupConcurrentDuplicate(t *testing.T) { + m, _ := newTestMock() + + successes, codes := runConcurrently(raceGoroutines, func() error { + _, err := m.CreateReplicationGroup(context.Background(), driver.ReplicationGroupConfig{ID: "dup"}) + return err + }) + + assertOneWinner(t, successes, codes, FaultReplicationGroupAlreadyExists) +} + +func TestCreateCacheSubnetGroupConcurrentDuplicate(t *testing.T) { + m, _ := newTestMock() + + successes, codes := runConcurrently(raceGoroutines, func() error { + _, err := m.CreateCacheSubnetGroup(context.Background(), driver.SubnetGroupConfig{ + Name: "dup", SubnetIDs: []string{"subnet-1"}, + }) + + return err + }) + + assertOneWinner(t, successes, codes, FaultCacheSubnetGroupAlreadyExists) +} + +// TestDeleteSubnetGroupVsCreateCacheRace races cluster creates against deletes +// of the subnet group they are placed in. Either the delete loses with +// CacheSubnetGroupInUse or the create loses with CacheSubnetGroupNotFoundFault; +// a cluster must never end up pointing at a deleted group. +func TestDeleteSubnetGroupVsCreateCacheRace(t *testing.T) { + ctx := context.Background() + m, _ := newTestMock() + + for i := range raceGoroutines { + sg := fmt.Sprintf("sg-%d", i) + if _, err := m.CreateCacheSubnetGroup(ctx, driver.SubnetGroupConfig{Name: sg, SubnetIDs: []string{"subnet-1"}}); err != nil { + t.Fatalf("CreateCacheSubnetGroup: %v", err) + } + + var wg sync.WaitGroup + + wg.Add(2) + + go func() { + defer wg.Done() + + _, _ = m.CreateCache(ctx, driver.CacheConfig{Name: "c-" + sg, SubnetGroupName: sg}) + }() + + go func() { + defer wg.Done() + + _ = m.DeleteCacheSubnetGroup(ctx, sg) + }() + + wg.Wait() + + _, cacheErr := m.GetCache(ctx, "c-"+sg) + _, sgErr := m.DescribeCacheSubnetGroups(ctx, []string{sg}) + + if cacheErr == nil && sgErr != nil { + t.Fatalf("cluster c-%s exists but its subnet group was deleted", sg) + } + } +} + +// TestDeleteSubnetGroupWhileCacheCreating proves a cluster still being +// provisioned counts as a user of its subnet group. +func TestDeleteSubnetGroupWhileCacheCreating(t *testing.T) { + ctx := context.Background() + m, eng := newGatedMock() + + if _, err := m.CreateCacheSubnetGroup(ctx, driver.SubnetGroupConfig{Name: "sg", SubnetIDs: []string{"subnet-1"}}); err != nil { + t.Fatalf("CreateCacheSubnetGroup: %v", err) + } + + done := make(chan error, 1) + + go func() { + _, err := m.CreateCache(ctx, driver.CacheConfig{Name: "c1", SubnetGroupName: "sg"}) + done <- err + }() + + <-eng.provisionEntered + + if code := faultCode(m.DeleteCacheSubnetGroup(ctx, "sg")); code != FaultCacheSubnetGroupInUse { + t.Fatalf("DeleteCacheSubnetGroup during create: code %q, want %q", code, FaultCacheSubnetGroupInUse) + } + + close(eng.provisionRelease) + + if err := <-done; err != nil { + t.Fatalf("CreateCache: %v", err) + } +} + +// TestDeleteParameterGroupWhileCacheCreating proves a create that has +// validated a parameter group keeps it from being deleted until it commits. +func TestDeleteParameterGroupWhileCacheCreating(t *testing.T) { + ctx := context.Background() + m, eng := newGatedMock() + + if _, err := m.CreateCacheParameterGroup(ctx, "pg", "redis7", "d"); err != nil { + t.Fatalf("CreateCacheParameterGroup: %v", err) + } + + done := make(chan error, 1) + + go func() { + _, err := m.CreateCache(ctx, driver.CacheConfig{Name: "c1", ParameterGroupName: "pg"}) + done <- err + }() + + <-eng.provisionEntered + + if code := faultCode(m.DeleteCacheParameterGroup(ctx, "pg")); code != FaultInvalidCacheParameterGroupState { + t.Fatalf("DeleteCacheParameterGroup during create: code %q, want %q", code, FaultInvalidCacheParameterGroupState) + } + + close(eng.provisionRelease) + + if err := <-done; err != nil { + t.Fatalf("CreateCache: %v", err) + } +} + +// TestDeleteParameterGroupVsCreateRace races creates that name a parameter +// group against deletes of that group: a committed cluster must never name a +// deleted group. +func TestDeleteParameterGroupVsCreateRace(t *testing.T) { + ctx := context.Background() + m, _ := newTestMock() + + for i := range raceGoroutines { + pg := fmt.Sprintf("pg-%d", i) + if _, err := m.CreateCacheParameterGroup(ctx, pg, "redis7", "d"); err != nil { + t.Fatalf("CreateCacheParameterGroup: %v", err) + } + + var wg sync.WaitGroup + + wg.Add(2) + + go func() { + defer wg.Done() + + _, _ = m.CreateCache(ctx, driver.CacheConfig{Name: "c-" + pg, ParameterGroupName: pg}) + }() + + go func() { + defer wg.Done() + + _ = m.DeleteCacheParameterGroup(ctx, pg) + }() + + wg.Wait() + + _, cacheErr := m.GetCache(ctx, "c-"+pg) + _, pgErr := m.DescribeCacheParameterGroups(ctx, []string{pg}) + + if cacheErr == nil && pgErr != nil { + t.Fatalf("cluster c-%s exists but its parameter group was deleted", pg) + } + } +} + +// TestDescribeReservedCreatingReturnsCreating proves a cluster whose engine is +// still being provisioned is describable with status creating. +func TestDescribeReservedCreatingReturnsCreating(t *testing.T) { + ctx := context.Background() + m, eng := newGatedMock() + + done := make(chan error, 1) + + go func() { + _, err := m.CreateCache(ctx, driver.CacheConfig{Name: "c1", Engine: "redis"}) + done <- err + }() + + <-eng.provisionEntered + + info, err := m.GetCache(ctx, "c1") + if err != nil || info.Status != statusCreating { + t.Fatalf("GetCache during create = %+v, %v; want status %q", info, err, statusCreating) + } + + list, err := m.ListCaches(ctx, info.Scope) + if err != nil || len(list) != 1 || list[0].Status != statusCreating { + t.Fatalf("ListCaches during create = %+v, %v; want one creating cluster", list, err) + } + + close(eng.provisionRelease) + + if err := <-done; err != nil { + t.Fatalf("CreateCache: %v", err) + } + + if info, _ = m.GetCache(ctx, "c1"); info.Status != statusAvailable { + t.Fatalf("status after create = %q, want %q", info.Status, statusAvailable) + } +} + +// TestModifyReservedCreatingIsInvalidState proves mutating calls on a cluster +// or replication group that is still being created report the invalid-state +// fault, not not-found. +func TestModifyReservedCreatingIsInvalidState(t *testing.T) { + ctx := context.Background() + m, eng := newGatedMock() + + cacheDone := make(chan error, 1) + + go func() { + _, err := m.CreateCache(ctx, driver.CacheConfig{Name: "c1", Engine: "redis"}) + cacheDone <- err + }() + + <-eng.provisionEntered + + _, err := m.ModifyCache(ctx, driver.ModifyCacheConfig{Name: "c1", NodeType: "cache.m5.large"}) + if code := faultCode(err); code != FaultInvalidCacheClusterState { + t.Fatalf("ModifyCache during create: code %q, want %q", code, FaultInvalidCacheClusterState) + } + + if code := faultCode(m.DeleteCache(ctx, "c1")); code != FaultInvalidCacheClusterState { + t.Fatalf("DeleteCache during create: code %q, want %q", code, FaultInvalidCacheClusterState) + } + + close(eng.provisionRelease) + + if err := <-cacheDone; err != nil { + t.Fatalf("CreateCache: %v", err) + } + + eng.provisionRelease = make(chan struct{}) + rgDone := make(chan error, 1) + + go func() { + _, err := m.CreateReplicationGroup(ctx, driver.ReplicationGroupConfig{ID: "rg1", Engine: "redis"}) + rgDone <- err + }() + + <-eng.provisionEntered + + _, err = m.ModifyReplicationGroup(ctx, "rg1", 2) + if code := faultCode(err); code != FaultInvalidReplicationGroupState { + t.Fatalf("ModifyReplicationGroup during create: code %q, want %q", code, FaultInvalidReplicationGroupState) + } + + groups, err := m.DescribeReplicationGroups(ctx, []string{"rg1"}) + if err != nil || groups[0].Status != statusCreating { + t.Fatalf("DescribeReplicationGroups during create = %+v, %v; want creating", groups, err) + } + + close(eng.provisionRelease) + + if err := <-rgDone; err != nil { + t.Fatalf("CreateReplicationGroup: %v", err) + } +} + +// TestRecreateWhileDeprovisioning proves an id stays taken until the real +// engine has been torn down, so the same engine slot is never provisioned +// twice, and that Describe reports the cluster as deleting meanwhile. +func TestRecreateWhileDeprovisioning(t *testing.T) { + ctx := context.Background() + m, eng := newGatedMock() + + close(eng.provisionRelease) + + if _, err := m.CreateCache(ctx, driver.CacheConfig{Name: "c1", Engine: "redis"}); err != nil { + t.Fatalf("CreateCache: %v", err) + } + + <-eng.provisionEntered + + done := make(chan error, 1) + + go func() { done <- m.DeleteCache(ctx, "c1") }() + + <-eng.deprovisionEntered + + info, err := m.GetCache(ctx, "c1") + if err != nil || info.Status != statusDeleting { + t.Fatalf("GetCache during delete = %+v, %v; want status %q", info, err, statusDeleting) + } + + _, err = m.CreateCache(ctx, driver.CacheConfig{Name: "c1", Engine: "redis"}) + if code := faultCode(err); code != FaultCacheClusterAlreadyExists { + t.Fatalf("CreateCache during delete: code %q, want %q", code, FaultCacheClusterAlreadyExists) + } + + if code := faultCode(m.DeleteCache(ctx, "c1")); code != FaultInvalidCacheClusterState { + t.Fatalf("DeleteCache during delete: code %q, want %q", code, FaultInvalidCacheClusterState) + } + + close(eng.deprovisionRelease) + + if err := <-done; err != nil { + t.Fatalf("DeleteCache: %v", err) + } + + if _, err := m.GetCache(ctx, "c1"); !cerrors.IsNotFound(err) { + t.Fatalf("GetCache after delete: %v, want not found", err) + } + + if _, err := m.CreateCache(ctx, driver.CacheConfig{Name: "c1", Engine: "redis"}); err != nil { + t.Fatalf("CreateCache after delete: %v", err) + } +} + +// TestDescribeDuringDeprovisionReturnsDeleting covers the replication-group +// side of the deleting reservation. +func TestDescribeDuringDeprovisionReturnsDeleting(t *testing.T) { + ctx := context.Background() + m, eng := newGatedMock() + + close(eng.provisionRelease) + + if _, err := m.CreateReplicationGroup(ctx, driver.ReplicationGroupConfig{ID: "rg1", Engine: "redis"}); err != nil { + t.Fatalf("CreateReplicationGroup: %v", err) + } + + <-eng.provisionEntered + + done := make(chan error, 1) + + go func() { done <- m.DeleteReplicationGroup(ctx, "rg1", driver.DeleteReplicationGroupOptions{}) }() + + <-eng.deprovisionEntered + + groups, err := m.DescribeReplicationGroups(ctx, nil) + if err != nil || len(groups) != 1 || groups[0].Status != statusDeleting { + t.Fatalf("DescribeReplicationGroups during delete = %+v, %v; want one deleting group", groups, err) + } + + _, err = m.CreateReplicationGroup(ctx, driver.ReplicationGroupConfig{ID: "rg1"}) + if code := faultCode(err); code != FaultReplicationGroupAlreadyExists { + t.Fatalf("CreateReplicationGroup during delete: code %q, want %q", code, FaultReplicationGroupAlreadyExists) + } + + close(eng.deprovisionRelease) + + if err := <-done; err != nil { + t.Fatalf("DeleteReplicationGroup: %v", err) + } + + if _, err := m.DescribeReplicationGroups(ctx, []string{"rg1"}); faultCode(err) != FaultReplicationGroupNotFound { + t.Fatalf("DescribeReplicationGroups after delete: %v, want ReplicationGroupNotFoundFault", err) + } +} + +// TestSnapshotRestoreSerialisedWithCreate runs persist Snapshot and Restore +// alongside creates and tag writes; under -race it proves they share mu. +func TestSnapshotRestoreSerialisedWithCreate(t *testing.T) { + ctx := context.Background() + src, _ := newTestMock() + dst, _ := newTestMock() + + var wg sync.WaitGroup + + for i := range raceGoroutines { + wg.Add(1) + + go func() { + defer wg.Done() + + name := fmt.Sprintf("c%d", i) + if _, err := src.CreateCache(ctx, driver.CacheConfig{Name: name, Tags: map[string]string{"k": "v"}}); err != nil { + t.Errorf("CreateCache: %v", err) + return + } + + _ = src.AddTags(ctx, src.cacheARN("us-east-1", name), map[string]string{"i": name}) + + raw, err := src.Snapshot(ctx, true) + if err != nil { + t.Errorf("Snapshot: %v", err) + return + } + + if err := dst.Restore(ctx, raw); err != nil { + t.Errorf("Restore: %v", err) + } + }() + } + + wg.Wait() + + raw, err := src.Snapshot(ctx, true) + if err != nil { + t.Fatalf("Snapshot: %v", err) + } + + if err := dst.Restore(ctx, raw); err != nil { + t.Fatalf("Restore: %v", err) + } + + list, err := dst.ListCaches(ctx, scope.Scope{}) + if err != nil || len(list) != raceGoroutines { + t.Fatalf("restored %d clusters (%v), want %d", len(list), err, raceGoroutines) + } +} + +func TestCreateCacheParameterGroupConcurrentDuplicate(t *testing.T) { + m, _ := newTestMock() + + successes, codes := runConcurrently(raceGoroutines, func() error { + _, err := m.CreateCacheParameterGroup(context.Background(), "dup", "redis7", "d") + return err + }) + + assertOneWinner(t, successes, codes, FaultCacheParameterGroupAlreadyExists) +} + +// TestModifyVsResetParameterGroupRace interleaves modifies, resets and reads of +// one parameter group. Under -race it proves the overrides map is never +// written while a reader holds it. +func TestModifyVsResetParameterGroupRace(t *testing.T) { + ctx := context.Background() + m, _ := newTestMock() + + if _, err := m.CreateCacheParameterGroup(ctx, "pg", "redis7", "d"); err != nil { + t.Fatalf("CreateCacheParameterGroup: %v", err) + } + + var wg sync.WaitGroup + + for i := range raceGoroutines { + wg.Add(3) + + go func() { + defer wg.Done() + + if err := m.ModifyCacheParameterGroup(ctx, "pg", []ParameterUpdate{ + {Name: "maxmemory-policy", Value: "allkeys-lru"}, {Name: "timeout", Value: strconv.Itoa(i + 20)}, + }); err != nil { + t.Errorf("ModifyCacheParameterGroup: %v", err) + } + }() + + go func() { + defer wg.Done() + + if err := m.ResetCacheParameterGroup(ctx, "pg", i%2 == 0, []string{"timeout"}); err != nil { + t.Errorf("ResetCacheParameterGroup: %v", err) + } + }() + + go func() { + defer wg.Done() + + readParameterGroup(t, m, "pg") + }() + } + + wg.Wait() +} + +// readParameterGroup reads a group's overrides and parameters the way a +// Describe caller would. +func readParameterGroup(t *testing.T, m *Mock, name string) { + t.Helper() + + groups, err := m.DescribeCacheParameterGroups(context.Background(), []string{name}) + if err != nil { + t.Errorf("DescribeCacheParameterGroups: %v", err) + return + } + + seen := 0 + for range groups[0].Overrides { + seen++ + } + + if seen > 2 { + t.Errorf("overrides = %d entries, want at most 2", seen) + } + + if _, err := m.DescribeCacheParameters(context.Background(), name, ""); err != nil { + t.Errorf("DescribeCacheParameters: %v", err) + } +} + +// snapshotRaceResult holds the outcome of one round of +// TestSnapshotVsDeleteReplicationGroupRace. +type snapshotRaceResult struct { + manual, final, copied, deleted error +} + +// TestSnapshotVsDeleteReplicationGroupRace races snapshot create, copy and +// delete against deleting the replication group they back up. The manual +// snapshot and the group's final snapshot share a name, so exactly one of them +// wins; a snapshot of the group never outlives a failed lookup; and copy and +// delete of one snapshot are serialized. +func TestSnapshotVsDeleteReplicationGroupRace(t *testing.T) { + ctx := context.Background() + m, _ := newTestMock() + + for i := range raceGoroutines { + rg := fmt.Sprintf("rg%d", i) + if _, err := m.CreateReplicationGroup(ctx, driver.ReplicationGroupConfig{ID: rg, Engine: "redis"}); err != nil { + t.Fatalf("CreateReplicationGroup: %v", err) + } + + if _, err := m.CreateSnapshot(ctx, driver.SnapshotConfig{SnapshotName: "base-" + rg, ReplicationGroupID: rg}); err != nil { + t.Fatalf("CreateSnapshot: %v", err) + } + + var ( + wg sync.WaitGroup + res snapshotRaceResult + ) + + wg.Add(4) + + go func() { + defer wg.Done() + + _, res.manual = m.CreateSnapshot(ctx, driver.SnapshotConfig{SnapshotName: "snap-" + rg, ReplicationGroupID: rg}) + }() + + go func() { + defer wg.Done() + + res.final = m.DeleteReplicationGroup(ctx, rg, driver.DeleteReplicationGroupOptions{FinalSnapshotIdentifier: "snap-" + rg}) + }() + + go func() { + defer wg.Done() + + _, res.copied = m.CopySnapshot(ctx, driver.CopySnapshotConfig{SourceSnapshotName: "base-" + rg, TargetSnapshotName: "copy-" + rg}) + }() + + go func() { + defer wg.Done() + + _, res.deleted = m.DeleteSnapshot(ctx, "base-"+rg) + }() + + wg.Wait() + + checkSnapshotRaceOutcome(t, m, rg, &res) + } +} + +// checkSnapshotRaceOutcome asserts the invariants of one round. +func checkSnapshotRaceOutcome(t *testing.T, m *Mock, rg string, res *snapshotRaceResult) { + t.Helper() + + ctx := context.Background() + + // Manual first: it wins and the final snapshot then collides, so the group + // survives. Delete first: the final snapshot wins and the manual one finds + // no group. Never both, never neither. + if (res.manual == nil) == (res.final == nil) { + t.Fatalf("%s: manual snapshot err %v, delete err %v; want exactly one to succeed", rg, res.manual, res.final) + } + + // Losing to the delete, the manual snapshot finds the final snapshot's name + // already taken (the name is checked first) or, failing that, no group. + if code := faultCode(res.manual); res.manual != nil && + code != FaultSnapshotAlreadyExists && code != FaultReplicationGroupNotFound { + t.Fatalf("%s: manual snapshot error %v, want SnapshotAlreadyExistsFault or ReplicationGroupNotFoundFault", rg, res.manual) + } + + if res.final != nil && faultCode(res.final) != FaultSnapshotAlreadyExists { + t.Fatalf("%s: delete error %v, want SnapshotAlreadyExistsFault", rg, res.final) + } + + _, describeErr := m.DescribeReplicationGroups(ctx, []string{rg}) + if (res.final == nil) != (faultCode(describeErr) == FaultReplicationGroupNotFound) { + t.Fatalf("%s: delete err %v but describe err %v", rg, res.final, describeErr) + } + + if res.deleted != nil { + t.Fatalf("%s: DeleteSnapshot of base: %v", rg, res.deleted) + } + + if res.copied != nil && faultCode(res.copied) != FaultSnapshotNotFound { + t.Fatalf("%s: CopySnapshot error %v, want nil or SnapshotNotFoundFault", rg, res.copied) + } + + if _, err := m.DescribeSnapshots(ctx, driver.SnapshotFilter{SnapshotName: "base-" + rg}); faultCode(err) != FaultSnapshotNotFound { + t.Fatalf("%s: base snapshot survived its delete: %v", rg, err) + } +} + +// TestCreateCacheCollidingWithReservedGroupMember proves a replication group +// still being created owns its member ids, so a cluster named "-002" is +// refused during the window, and that a group whose member id is already a +// cluster is refused too. +func TestCreateCacheCollidingWithReservedGroupMember(t *testing.T) { + ctx := context.Background() + m, eng := newGatedMock() + + done := make(chan error, 1) + + go func() { + _, err := m.CreateReplicationGroup(ctx, driver.ReplicationGroupConfig{ID: "rg", Engine: "redis", NumCacheNodes: 2}) + done <- err + }() + + <-eng.provisionEntered + + _, err := m.CreateCache(ctx, driver.CacheConfig{Name: "rg-002", Engine: "memcached"}) + if code := faultCode(err); code != FaultCacheClusterAlreadyExists { + t.Fatalf("CreateCache(rg-002) during group create: code %q, want %q", code, FaultCacheClusterAlreadyExists) + } + + close(eng.provisionRelease) + + if err := <-done; err != nil { + t.Fatalf("CreateReplicationGroup: %v", err) + } + + plain, _ := newTestMock() + + if _, err := plain.CreateCache(ctx, driver.CacheConfig{Name: "g2-001", Engine: "memcached"}); err != nil { + t.Fatalf("CreateCache: %v", err) + } + + _, err = plain.CreateReplicationGroup(ctx, driver.ReplicationGroupConfig{ID: "g2"}) + if code := faultCode(err); code != FaultCacheClusterAlreadyExists { + t.Fatalf("CreateReplicationGroup over a cluster's id: code %q, want %q", code, FaultCacheClusterAlreadyExists) + } +} diff --git a/providers/aws/elasticache/elasticache.go b/providers/aws/elasticache/elasticache.go index 263d823f2..02bfa98dc 100644 --- a/providers/aws/elasticache/elasticache.go +++ b/providers/aws/elasticache/elasticache.go @@ -79,6 +79,9 @@ const ( // modify before settling to available. statusCreating = "creating" statusModifying = "modifying" + // statusDeleting is what a cluster or replication group reports from its + // delete call until it is gone. + statusDeleting = "deleting" defaultRedisVersion = "7.1" defaultMemcachedVersion = "1.6.22" @@ -135,8 +138,7 @@ func normalizeNodeCount(engine string, requested int) (int, error) { // check). An empty name places the cluster in the default subnet group. func (m *Mock) requireSubnetGroup(name string) error { if name != "" && !m.subnetGroups.Has(name) { - return errors.Newf(errors.NotFound, - "CacheSubnetGroupNotFoundFault: cache subnet group %q not found", name) + return subnetGroupNotFound(name) } return nil @@ -194,8 +196,31 @@ func defaultEngineVersion(engine string) string { return defaultRedisVersion } +// reservation holds an id whose resource is mid-way through a real-engine +// provision (state creating) or deprovision (state deleting). The id cannot be +// reused until the engine call returns, and Describe reports record with that +// status, the way a real resource in those states is still visible. +type reservation[T any] struct { + state string + record T +} + // Mock is an in-memory mock implementation of the AWS ElastiCache service. +// +// Lock order: mu, then a settle set's lock. A settle lock is only taken while +// holding mu or no lock at all, never the reverse. The settle sets compute +// state from the clock on read and never call back, so nothing re-enters mu. +// No lock is held across a call to another service (monitoring, the VPC subnet +// resolver) or to the real cache engine. type Mock struct { + // mu guards the control-plane stores and the reservations below, so every + // check-then-act (duplicate checks, parent validation, in-use scans, + // cascades, Snapshot/Restore) is atomic. The per-cache key/value stores + // (cacheData.items) are data plane and rely on memstore's own locking. + mu sync.Mutex + cacheRes map[string]*reservation[driver.CacheInfo] + rgRes map[string]*reservation[driver.ReplicationGroup] + caches *memstore.Store[*cacheData] subnetGroups *memstore.Store[driver.SubnetGroup] replicationGroups *memstore.Store[driver.ReplicationGroup] @@ -214,7 +239,7 @@ type Mock struct { cacheSettle *settle.Set rgSettle *settle.Set - tagMu sync.Mutex + // tagsByARN is guarded by mu like the stores above. tagsByARN map[string]map[string]string } @@ -259,6 +284,8 @@ func New(opts *config.Options) *Mock { snapshots: memstore.New[driver.Snapshot](), cacheSettle: settle.NewSet(), rgSettle: settle.NewSet(), + cacheRes: make(map[string]*reservation[driver.CacheInfo]), + rgRes: make(map[string]*reservation[driver.ReplicationGroup]), tagsByARN: make(map[string]map[string]string), opts: opts, } @@ -277,28 +304,119 @@ func (m *Mock) settleRGStatus(id, final string) string { } // CreateCache creates a new ElastiCache cluster. +// +// The id is reserved under mu while the request is validated, the optional +// real engine is provisioned with no lock held, and the cluster is committed +// under mu again. A concurrent create of the same id sees the reservation and +// reports CacheClusterAlreadyExists. +// +//nolint:gocritic // cfg is passed by value to match the driver.Cache signature. func (m *Mock) CreateCache(ctx context.Context, cfg driver.CacheConfig) (*driver.CacheInfo, error) { - if cfg.Name == "" { - return nil, errors.New(errors.InvalidArgument, "cache name is required") + info, err := m.reserveCache(ctx, &cfg) + if err != nil { + return nil, err + } + + // Opt-in: back the cache with a real server, replacing the synthetic + // endpoint with the real host:port a client connects to. + if err = cacheengine.Provision(ctx, m.opts.CacheEngine, &info); err != nil { + m.mu.Lock() + delete(m.cacheRes, info.Name) + m.mu.Unlock() + + return nil, err } - if m.caches.Has(cfg.Name) { - return nil, errors.Newf(errors.AlreadyExists, "cache %q already exists", cfg.Name) + return m.commitCache(&info), nil +} + +// cacheIDTaken reports whether name is already used by a cluster, a pending +// reservation, or a replication-group member node. The caller holds mu. +func (m *Mock) cacheIDTaken(name string) bool { + if m.caches.Has(name) { + return true + } + + if _, reserved := m.cacheRes[name]; reserved { + return true } // A replication group's member nodes ("-001", โ€ฆ) are describable as // single-node cache clusters without being backed by m.caches, so guard the // same id space here. Real ElastiCache rejects a create colliding with a // member id with CacheClusterAlreadyExists. - if _, member := m.lookupMember(cfg.Name); member { - return nil, errors.Newf(errors.AlreadyExists, "cache %q already exists", cfg.Name) + if _, member := m.lookupMember(name); member { + return true } + // A replication group still being created or deleted owns its member ids + // too, or a cluster named "-001" could slip in and collide with the + // member once the group commits. + for _, r := range m.rgRes { + if slices.Contains(r.record.MemberClusters, name) { + return true + } + } + + return false +} + +// reserveCache validates a create under mu, builds the cluster record, and +// reserves its id in the creating state. +func (m *Mock) reserveCache(ctx context.Context, cfg *driver.CacheConfig) (driver.CacheInfo, error) { + if cfg.Name == "" { + return driver.CacheInfo{}, errors.New(errors.InvalidArgument, "cache name is required") + } + + m.mu.Lock() + defer m.mu.Unlock() + + if m.cacheIDTaken(cfg.Name) { + return driver.CacheInfo{}, cacheAlreadyExists(cfg.Name) + } + + info, err := m.buildCacheInfo(ctx, cfg) + if err != nil { + return driver.CacheInfo{}, err + } + + m.cacheRes[cfg.Name] = &reservation[driver.CacheInfo]{state: statusCreating, record: info} + + return info, nil +} + +// commitCache stores a provisioned cluster and drops its reservation. +func (m *Mock) commitCache(info *driver.CacheInfo) *driver.CacheInfo { + m.mu.Lock() + defer m.mu.Unlock() + + delete(m.cacheRes, info.Name) + + m.caches.Set(info.Name, &cacheData{info: *info, items: memstore.New[cacheItem]()}) + m.seedTags(info.ARN, info.Tags) + + // Under AsyncSettle a fresh cluster reports creating until the window elapses, + // matching real ElastiCache (CreateCacheCluster โ†’ creating โ†’ available). With + // the default (AsyncSettle off) SettleDuration is 0 โ†’ inactive window โ†’ + // available immediately, so nothing changes for existing callers. + m.cacheSettle.Begin(info.Name, statusCreating, m.opts.Clock.Now(), + m.opts.SettleDuration(settle.DefaultCacheSettle)) + + result := *info + result.Tags = cloneTags(info.Tags) + result.Status = m.settleCacheStatus(info.Name, result.Status) + + return &result +} + +// buildCacheInfo applies the restore seed and defaults to a create request and +// validates it against its parents. The caller holds mu. +func (m *Mock) buildCacheInfo(ctx context.Context, cfg *driver.CacheConfig) (driver.CacheInfo, error) { // A restore (SnapshotName set) seeds the unset config fields from the // snapshot before defaults are applied, so the new cluster reproduces the // source's engine/version/node-type/count/port. - if err := m.seedCacheRestore(&cfg); err != nil { - return nil, err + if err := m.seedCacheRestore(cfg); err != nil { + return driver.CacheInfo{}, err } engine := cfg.Engine @@ -307,79 +425,66 @@ func (m *Mock) CreateCache(ctx context.Context, cfg driver.CacheConfig) (*driver } if err := validateEngine(engine, false); err != nil { - return nil, err - } - - nodeType := cfg.NodeType - if nodeType == "" { - nodeType = defaultNodeType - } - - engineVersion := cfg.EngineVersion - if engineVersion == "" { - engineVersion = defaultEngineVersion(engine) + return driver.CacheInfo{}, err } numNodes, err := normalizeNodeCount(engine, cfg.NumCacheNodes) if err != nil { - return nil, err + return driver.CacheInfo{}, err } if err := m.requireSubnetGroup(cfg.SubnetGroupName); err != nil { - return nil, err + return driver.CacheInfo{}, err } if err := m.requireParameterGroup(cfg.ParameterGroupName); err != nil { - return nil, err + return driver.CacheInfo{}, err } - region := regionctx.RegionOr(ctx, m.opts.Region) - endpoint := clusterEndpoint(cfg.Name, region, engine, resolvePort(engine, cfg.Port)) + nodeType := cfg.NodeType + if nodeType == "" { + nodeType = defaultNodeType + } - tags := cloneTags(cfg.Tags) + engineVersion := cfg.EngineVersion + if engineVersion == "" { + engineVersion = defaultEngineVersion(engine) + } - info := driver.CacheInfo{ + region := regionctx.RegionOr(ctx, m.opts.Region) + + return driver.CacheInfo{ Name: cfg.Name, Scope: cfg.Scope, NodeType: nodeType, Engine: engine, EngineVersion: engineVersion, Status: statusAvailable, - Endpoint: endpoint, + Endpoint: clusterEndpoint(cfg.Name, region, engine, resolvePort(engine, cfg.Port)), ARN: m.cacheARN(region, cfg.Name), CreatedAt: m.opts.Clock.Now().UTC().Format(time.RFC3339), - Tags: tags, + Tags: cloneTags(cfg.Tags), NumCacheNodes: numNodes, SubnetGroupName: cfg.SubnetGroupName, ParameterGroupName: cfg.ParameterGroupName, AutoMinorVersionUpgrade: boolOrDefault(cfg.AutoMinorVersionUpgrade, true), - } + }, nil +} - // Opt-in: back the cache with a real server, replacing the synthetic - // endpoint with the real host:port a client connects to. - if err := cacheengine.Provision(ctx, m.opts.CacheEngine, &info); err != nil { - return nil, err +// mutableCache returns the stored cluster a mutating call targets. A cluster +// that is still being created or is being deleted reports +// InvalidCacheClusterState, not CacheClusterNotFound: in AWS it exists in that +// state. The caller holds mu. +func (m *Mock) mutableCache(name string) (*cacheData, error) { + if cd, ok := m.caches.Get(name); ok { + return cd, nil } - cd := &cacheData{ - info: info, - items: memstore.New[cacheItem](), + if r, reserved := m.cacheRes[name]; reserved { + return nil, cacheInvalidState(name, r.state) } - m.caches.Set(cfg.Name, cd) - m.seedTags(info.ARN, tags) - - // Under AsyncSettle a fresh cluster reports creating until the window elapses, - // matching real ElastiCache (CreateCacheCluster โ†’ creating โ†’ available). With - // the default (AsyncSettle off) SettleDuration is 0 โ†’ inactive window โ†’ - // available immediately, so nothing changes for existing callers. - m.cacheSettle.Begin(cfg.Name, statusCreating, m.opts.Clock.Now(), - m.opts.SettleDuration(settle.DefaultCacheSettle)) - - result := info - result.Status = m.settleCacheStatus(cfg.Name, result.Status) - - return &result, nil + return nil, cacheNotFound(name) } // ModifyCache updates the mutable fields (node type, engine version, node @@ -388,9 +493,12 @@ func (m *Mock) CreateCache(ctx context.Context, cfg driver.CacheConfig) (*driver // change is re-validated against the cluster's engine (Memcached scales 1-40; // Redis/Valkey stays at 1). func (m *Mock) ModifyCache(_ context.Context, cfg driver.ModifyCacheConfig) (*driver.CacheInfo, error) { - cd, ok := m.caches.Get(cfg.Name) - if !ok { - return nil, errors.Newf(errors.NotFound, "cache %q not found", cfg.Name) + m.mu.Lock() + defer m.mu.Unlock() + + cd, err := m.mutableCache(cfg.Name) + if err != nil { + return nil, err } if cfg.NumCacheNodes != 0 { @@ -399,23 +507,7 @@ func (m *Mock) ModifyCache(_ context.Context, cfg driver.ModifyCacheConfig) (*dr } } - if cfg.NodeType != "" { - cd.info.NodeType = cfg.NodeType - } - - if cfg.EngineVersion != "" { - cd.info.EngineVersion = cfg.EngineVersion - } - - if cfg.NumCacheNodes > 0 { - cd.info.NumCacheNodes = cfg.NumCacheNodes - } - - if cfg.AutoMinorVersionUpgrade != nil { - cd.info.AutoMinorVersionUpgrade = *cfg.AutoMinorVersionUpgrade - } - - m.caches.Set(cfg.Name, cd) + applyCacheModify(&cd.info, &cfg) // Under AsyncSettle a modified cluster briefly reports modifying before // settling back to available (ModifyCacheCluster โ†’ modifying โ†’ available); a @@ -424,67 +516,128 @@ func (m *Mock) ModifyCache(_ context.Context, cfg driver.ModifyCacheConfig) (*dr m.opts.SettleDuration(settle.DefaultCacheModifySettle)) result := cd.info + result.Tags = cloneTags(cd.info.Tags) result.Status = m.settleCacheStatus(cfg.Name, result.Status) return &result, nil } +// applyCacheModify copies the non-empty fields of a modify request onto info. +func applyCacheModify(info *driver.CacheInfo, cfg *driver.ModifyCacheConfig) { + if cfg.NodeType != "" { + info.NodeType = cfg.NodeType + } + + if cfg.EngineVersion != "" { + info.EngineVersion = cfg.EngineVersion + } + + if cfg.NumCacheNodes > 0 { + info.NumCacheNodes = cfg.NumCacheNodes + } + + if cfg.AutoMinorVersionUpgrade != nil { + info.AutoMinorVersionUpgrade = *cfg.AutoMinorVersionUpgrade + } +} + // RebootCache reboots a cache cluster (ElastiCache RebootCacheCluster), the // path real deployments use to apply pending parameter-group changes. The // cluster stays available in the emulator; a reboot metric is emitted to mirror -// the lifecycle event. +// the lifecycle event, after mu is released since monitoring is another service. func (m *Mock) RebootCache(_ context.Context, name string) (*driver.CacheInfo, error) { - cd, ok := m.caches.Get(name) - if !ok { - return nil, errors.Newf(errors.NotFound, "cache %q not found", name) - } + m.mu.Lock() - m.emitMetric("Reboots", 1, map[string]string{"CacheClusterId": name}) + cd, err := m.mutableCache(name) + if err != nil { + m.mu.Unlock() + + return nil, err + } result := cd.info + result.Tags = cloneTags(cd.info.Tags) + m.mu.Unlock() + + m.emitMetric("Reboots", 1, map[string]string{"CacheClusterId": name}) return &result, nil } // DeleteCache deletes an ElastiCache cluster by name. +// +// The cluster is removed under mu and its id is held in the deleting state +// until the real engine (if any) has been torn down, so a create of the same id +// cannot provision the engine slot while the old one is still stopping. If the +// teardown fails the cluster is put back, as it was never deleted. func (m *Mock) DeleteCache(ctx context.Context, name string) error { - cd, ok := m.caches.Get(name) - if !ok { - return errors.Newf(errors.NotFound, "cache %q not found", name) - } + m.mu.Lock() + + cd, err := m.mutableCache(name) + if err != nil { + m.mu.Unlock() - // Tear down the real cache server backing the instance, if any. - if err := cacheengine.Deprovision(ctx, m.opts.CacheEngine, &cd.info); err != nil { return err } m.caches.Delete(name) m.cacheSettle.Clear(name) + last := cd.info + last.Status = statusDeleting + m.cacheRes[name] = &reservation[driver.CacheInfo]{state: statusDeleting, record: last} + m.mu.Unlock() + + // Tear down the real cache server backing the instance, if any. + derr := cacheengine.Deprovision(ctx, m.opts.CacheEngine, &cd.info) + + m.mu.Lock() + defer m.mu.Unlock() + + delete(m.cacheRes, name) + + if derr != nil { + m.caches.Set(name, cd) + + return derr + } + return nil } // GetCache retrieves information about an ElastiCache cluster. // -// A name that is not a standalone cluster may still be a member node of a -// replication group ("-001", โ€ฆ); real ElastiCache describes those as -// single-node cache clusters, so fall back to synthesizing the member from its -// group before reporting not-found. IaC that manages a replication group reads -// each member back this way. +// A cluster whose real engine is still being provisioned or torn down is +// reported with status creating or deleting. A name that is not a standalone +// cluster may still be a member node of a replication group ("-001", +// โ€ฆ); real ElastiCache describes those as single-node cache clusters, so fall +// back to synthesizing the member from its group before reporting not-found. +// IaC that manages a replication group reads each member back this way. func (m *Mock) GetCache(_ context.Context, name string) (*driver.CacheInfo, error) { - cd, ok := m.caches.Get(name) - if !ok { - if info, found := m.lookupMember(name); found { - return &info, nil - } + m.mu.Lock() + defer m.mu.Unlock() - return nil, errors.Newf(errors.NotFound, "cache %q not found", name) + if cd, ok := m.caches.Get(name); ok { + result := cd.info + result.Tags = cloneTags(cd.info.Tags) + result.Status = m.settleCacheStatus(name, result.Status) + + return &result, nil } - result := cd.info - result.Status = m.settleCacheStatus(name, result.Status) + if r, reserved := m.cacheRes[name]; reserved { + result := r.record + result.Tags = cloneTags(r.record.Tags) + result.Status = r.state - return &result, nil + return &result, nil + } + + if info, found := m.lookupMember(name); found { + return &info, nil + } + + return nil, cacheNotFound(name) } // lookupMember returns the synthesized cache-cluster view of a replication-group @@ -544,11 +697,16 @@ func (m *Mock) memberCacheInfo(rg *driver.ReplicationGroup, memberID string) dri // included as individual single-node cache clusters, matching real ElastiCache // (DescribeCacheClusters returns them alongside standalone clusters). The result // is sorted by cluster id so the wire order is deterministic (no map-iteration -// randomness) and matches the id-ordered listing AWS returns. +// randomness) and matches the id-ordered listing AWS returns. A cluster whose +// real engine is still being provisioned or torn down is listed with status +// creating or deleting, as AWS lists a cluster in those states. func (m *Mock) ListCaches(_ context.Context, filter scope.Scope) ([]driver.CacheInfo, error) { + m.mu.Lock() + defer m.mu.Unlock() + all := m.caches.SortedValues() - caches := make([]driver.CacheInfo, 0, len(all)) + caches := make([]driver.CacheInfo, 0, len(all)+len(m.cacheRes)) for _, cd := range all { if !cd.info.Scope.Matches(filter) { @@ -556,10 +714,22 @@ func (m *Mock) ListCaches(_ context.Context, filter scope.Scope) ([]driver.Cache } info := cd.info + info.Tags = cloneTags(cd.info.Tags) info.Status = m.settleCacheStatus(info.Name, info.Status) caches = append(caches, info) } + for _, r := range m.cacheRes { + if !r.record.Scope.Matches(filter) { + continue + } + + info := r.record + info.Tags = cloneTags(r.record.Tags) + info.Status = r.state + caches = append(caches, info) + } + groups := m.replicationGroups.SortedValues() for i := range groups { for _, memberID := range groups[i].MemberClusters { @@ -583,27 +753,31 @@ func (m *Mock) ListCaches(_ context.Context, filter scope.Scope) ([]driver.Cache // CreateOrUpdate-on-existing semantics (node type and tags come from the // request; identity, endpoint, and CreatedAt are preserved). func (m *Mock) UpdateCache(_ context.Context, cfg driver.CacheConfig) (*driver.CacheInfo, error) { - cd, ok := m.caches.Get(cfg.Name) - if !ok { - return nil, errors.Newf(errors.NotFound, "cache %q not found", cfg.Name) + m.mu.Lock() + defer m.mu.Unlock() + + cd, err := m.mutableCache(cfg.Name) + if err != nil { + return nil, err } if cfg.NodeType != "" { cd.info.NodeType = cfg.NodeType } + if cfg.Tags != nil { cd.info.Tags = maps.Clone(cfg.Tags) } + if !cfg.Scope.IsZero() { cd.info.Scope = cfg.Scope } - m.caches.Set(cfg.Name, cd) - m.cacheSettle.Begin(cfg.Name, statusModifying, m.opts.Clock.Now(), m.opts.SettleDuration(settle.DefaultCacheModifySettle)) result := cd.info + result.Tags = cloneTags(cd.info.Tags) result.Status = m.settleCacheStatus(cfg.Name, result.Status) return &result, nil diff --git a/providers/aws/elasticache/faults.go b/providers/aws/elasticache/faults.go new file mode 100644 index 000000000..c56d44f7f --- /dev/null +++ b/providers/aws/elasticache/faults.go @@ -0,0 +1,95 @@ +package elasticache + +import ( + cerrors "github.com/stackshy/cloudemu/v2/errors" +) + +// ElastiCache wire fault codes, as the AWS API (botocore error.code) spells +// them. A caller matching the SDK's typed errors sees this code, so it travels +// on the error as a typed field rather than inside the message text. +const ( + FaultCacheClusterNotFound = "CacheClusterNotFound" + FaultCacheClusterAlreadyExists = "CacheClusterAlreadyExists" + FaultInvalidCacheClusterState = "InvalidCacheClusterState" + FaultReplicationGroupNotFound = "ReplicationGroupNotFoundFault" + FaultReplicationGroupAlreadyExists = "ReplicationGroupAlreadyExists" + FaultInvalidReplicationGroupState = "InvalidReplicationGroupState" + FaultCacheSubnetGroupNotFound = "CacheSubnetGroupNotFoundFault" + FaultCacheSubnetGroupAlreadyExists = "CacheSubnetGroupAlreadyExists" + FaultCacheSubnetGroupInUse = "CacheSubnetGroupInUse" + FaultCacheParameterGroupNotFound = "CacheParameterGroupNotFound" + FaultCacheParameterGroupAlreadyExists = "CacheParameterGroupAlreadyExists" + FaultInvalidCacheParameterGroupState = "InvalidCacheParameterGroupState" + FaultSnapshotNotFound = "SnapshotNotFoundFault" + FaultSnapshotAlreadyExists = "SnapshotAlreadyExistsFault" + FaultSnapshotFeatureNotSupported = "SnapshotFeatureNotSupportedFault" +) + +// Fault is an ElastiCache error that carries its AWS wire fault code apart from +// the human message. Error() keeps the canonical cloudemu form; it unwraps to +// the canonical error, so cerrors.IsNotFound and friends keep working, and +// cerrors.Message returns the message without any code token, which is what +// real ElastiCache puts in . +type Fault struct { + err *cerrors.Error + code string +} + +// Error implements error. +func (f *Fault) Error() string { return f.err.Error() } + +// Unwrap returns the canonical cloudemu error. +func (f *Fault) Unwrap() error { return f.err } + +// AWSFault returns the ElastiCache wire fault code. +func (f *Fault) AWSFault() string { return f.code } + +// newFault builds a Fault of the given canonical kind and wire code. +func newFault(kind cerrors.Code, code, format string, args ...any) *Fault { + return &Fault{err: cerrors.Newf(kind, format, args...), code: code} +} + +func cacheNotFound(name string) *Fault { + return newFault(cerrors.NotFound, FaultCacheClusterNotFound, "cache cluster %q not found", name) +} + +func cacheAlreadyExists(name string) *Fault { + return newFault(cerrors.AlreadyExists, FaultCacheClusterAlreadyExists, "cache cluster %q already exists", name) +} + +// cacheInvalidState reports a mutating call on a cache cluster whose status +// does not allow it (it is still being created or is being deleted). +func cacheInvalidState(name, status string) *Fault { + return newFault(cerrors.FailedPrecondition, FaultInvalidCacheClusterState, + "cache cluster %q is not available (status %s)", name, status) +} + +func rgNotFound(id string) *Fault { + return newFault(cerrors.NotFound, FaultReplicationGroupNotFound, "replication group %q not found", id) +} + +func rgAlreadyExists(id string) *Fault { + return newFault(cerrors.AlreadyExists, FaultReplicationGroupAlreadyExists, + "replication group %q already exists", id) +} + +func rgInvalidState(id, status string) *Fault { + return newFault(cerrors.FailedPrecondition, FaultInvalidReplicationGroupState, + "replication group %q is not available (status %s)", id, status) +} + +func subnetGroupNotFound(name string) *Fault { + return newFault(cerrors.NotFound, FaultCacheSubnetGroupNotFound, "cache subnet group %q not found", name) +} + +func parameterGroupNotFound(name string) *Fault { + return newFault(cerrors.NotFound, FaultCacheParameterGroupNotFound, "cache parameter group %q not found", name) +} + +func snapshotNotFound(name string) *Fault { + return newFault(cerrors.NotFound, FaultSnapshotNotFound, "snapshot %q not found", name) +} + +func snapshotAlreadyExists(name string) *Fault { + return newFault(cerrors.AlreadyExists, FaultSnapshotAlreadyExists, "snapshot %q already exists", name) +} diff --git a/providers/aws/elasticache/parameters.go b/providers/aws/elasticache/parameters.go index 35cf33800..7c285cdbe 100644 --- a/providers/aws/elasticache/parameters.go +++ b/providers/aws/elasticache/parameters.go @@ -134,9 +134,12 @@ func defaultParameterIndex(family string) map[string]Parameter { // "engine-default" (only unmodified defaults). A missing group reports // CacheParameterGroupNotFound. func (m *Mock) DescribeCacheParameters(_ context.Context, name, source string) ([]Parameter, error) { + m.mu.Lock() pg, ok := m.lookupParameterGroup(name) + m.mu.Unlock() + if !ok { - return nil, cerrors.Newf(cerrors.NotFound, "cache parameter group %q not found", name) + return nil, parameterGroupNotFound(name) } defaults := defaultCacheParameters(pg.Family) @@ -186,9 +189,12 @@ func (m *Mock) ModifyCacheParameterGroup(_ context.Context, name string, updates return err } + m.mu.Lock() + defer m.mu.Unlock() + pg, ok := m.parameterGroups.Get(name) if !ok { - return cerrors.Newf(cerrors.NotFound, "cache parameter group %q not found", name) + return parameterGroupNotFound(name) } idx := defaultParameterIndex(pg.Family) @@ -204,9 +210,9 @@ func (m *Mock) ModifyCacheParameterGroup(_ context.Context, name string, updates } } - if pg.Overrides == nil { - pg.Overrides = make(map[string]string, len(updates)) - } + // Copy on write: a Describe result handed out earlier may still hold the old + // map, so it is never mutated in place. + pg.Overrides = cloneOverrides(pg.Overrides, len(updates)) for _, u := range updates { pg.Overrides[u.Name] = u.Value @@ -226,9 +232,12 @@ func (m *Mock) ResetCacheParameterGroup(_ context.Context, name string, resetAll return err } + m.mu.Lock() + defer m.mu.Unlock() + pg, ok := m.parameterGroups.Get(name) if !ok { - return cerrors.Newf(cerrors.NotFound, "cache parameter group %q not found", name) + return parameterGroupNotFound(name) } if resetAll { @@ -245,6 +254,8 @@ func (m *Mock) ResetCacheParameterGroup(_ context.Context, name string, resetAll } } + pg.Overrides = cloneOverrides(pg.Overrides, 0) + for _, n := range names { delete(pg.Overrides, n) } @@ -253,3 +264,14 @@ func (m *Mock) ResetCacheParameterGroup(_ context.Context, name string, resetAll return nil } + +// cloneOverrides returns a writable copy of a parameter group's overrides, +// sized for extra more entries. +func cloneOverrides(in map[string]string, extra int) map[string]string { + out := make(map[string]string, len(in)+extra) + for k, v := range in { + out[k] = v + } + + return out +} diff --git a/providers/aws/elasticache/persist_snapshot.go b/providers/aws/elasticache/persist_snapshot.go index c89f8e6e4..49af55f58 100644 --- a/providers/aws/elasticache/persist_snapshot.go +++ b/providers/aws/elasticache/persist_snapshot.go @@ -39,7 +39,15 @@ type cacheDataSnapshot struct { // Snapshot captures the mock's entire state as JSON. includeAssets is unused. Cache entry values // are the payload and are always captured. +// +// It holds mu for the whole capture (including the marshal, since the tag map +// is captured by reference), so it never sees a half-applied create or delete. +// A cluster or replication group whose real engine is still being provisioned +// or torn down is not part of the captured state. func (m *Mock) Snapshot(_ context.Context, _ bool) (json.RawMessage, error) { + m.mu.Lock() + defer m.mu.Unlock() + var snap persistSnapshot if err := m.snapshotStores(&snap); err != nil { return nil, err @@ -52,9 +60,7 @@ func (m *Mock) Snapshot(_ context.Context, _ bool) (json.RawMessage, error) { snap.Caches = caches - m.tagMu.Lock() snap.TagsByARN = m.tagsByARN - m.tagMu.Unlock() return json.Marshal(snap) } @@ -109,6 +115,11 @@ func (m *Mock) Restore(_ context.Context, data json.RawMessage) error { return fmt.Errorf("elasticache: parse snapshot: %w", err) } + // Restore swaps every store in one critical section, so it is serialized + // against in-flight creates and deletes. + m.mu.Lock() + defer m.mu.Unlock() + if err := m.restoreStores(&snap); err != nil { return err } @@ -118,9 +129,7 @@ func (m *Mock) Restore(_ context.Context, data json.RawMessage) error { } if snap.TagsByARN != nil { - m.tagMu.Lock() m.tagsByARN = snap.TagsByARN - m.tagMu.Unlock() } return nil diff --git a/providers/aws/elasticache/replication_group.go b/providers/aws/elasticache/replication_group.go index d5166af56..968457c11 100644 --- a/providers/aws/elasticache/replication_group.go +++ b/providers/aws/elasticache/replication_group.go @@ -4,7 +4,9 @@ import ( "context" "fmt" "net" + "slices" "strconv" + "strings" "time" cerrors "github.com/stackshy/cloudemu/v2/errors" @@ -20,22 +22,102 @@ import ( // A duplicate id reports ReplicationGroupAlreadyExists: callers re-running a // provision treat that specific code as "already there, carry on", so folding // it into a generic error would turn a safe re-run into a hard failure. +// +// The id is reserved under mu while the request is validated, the optional real +// engine is provisioned with no lock held, and the group is committed under mu. func (m *Mock) CreateReplicationGroup( ctx context.Context, cfg cachedriver.ReplicationGroupConfig, ) (*cachedriver.ReplicationGroup, error) { + rg, err := m.reserveReplicationGroup(ctx, &cfg) + if err != nil { + return nil, err + } + + // Opt-in: back the primary with a real Redis server, replacing the synthetic + // hostname with the host:port a client connects to. Routes through the same + // cacheengine helper the cache-cluster path uses so the two cannot drift. + if err = m.provisionPrimaryEndpoint(ctx, &rg); err != nil { + m.mu.Lock() + delete(m.rgRes, rg.ID) + m.mu.Unlock() + + return nil, err + } + + return m.commitReplicationGroup(&rg), nil +} + +// reserveReplicationGroup validates a create under mu, builds the group, and +// reserves its id in the creating state. +func (m *Mock) reserveReplicationGroup( + ctx context.Context, cfg *cachedriver.ReplicationGroupConfig, +) (cachedriver.ReplicationGroup, error) { if cfg.ID == "" { - return nil, cerrors.New(cerrors.InvalidArgument, "ReplicationGroupId is required") + return cachedriver.ReplicationGroup{}, cerrors.New(cerrors.InvalidArgument, "ReplicationGroupId is required") + } + + m.mu.Lock() + defer m.mu.Unlock() + + if _, reserved := m.rgRes[cfg.ID]; reserved || m.replicationGroups.Has(cfg.ID) { + return cachedriver.ReplicationGroup{}, rgAlreadyExists(cfg.ID) + } + + rg, err := m.buildReplicationGroup(ctx, cfg) + if err != nil { + return cachedriver.ReplicationGroup{}, err } - if m.replicationGroups.Has(cfg.ID) { - return nil, cerrors.Newf(cerrors.AlreadyExists, - "ReplicationGroupAlreadyExists: replication group %q already exists", cfg.ID) + // Each member is a describable cache cluster, so a member id already used by + // a cluster (stored or reserved) is a CacheClusterAlreadyExists, as in AWS. + for _, member := range rg.MemberClusters { + if _, reserved := m.cacheRes[member]; reserved || m.caches.Has(member) { + return cachedriver.ReplicationGroup{}, cacheAlreadyExists(member) + } } + m.rgRes[cfg.ID] = &reservation[cachedriver.ReplicationGroup]{state: statusCreating, record: rg} + + return rg, nil +} + +// commitReplicationGroup stores a provisioned group and drops its reservation. +func (m *Mock) commitReplicationGroup(rg *cachedriver.ReplicationGroup) *cachedriver.ReplicationGroup { + m.mu.Lock() + defer m.mu.Unlock() + + delete(m.rgRes, rg.ID) + m.replicationGroups.Set(rg.ID, *rg) + + // Under AsyncSettle a fresh group reports creating until the window elapses + // (CreateReplicationGroup โ†’ creating โ†’ available); a no-op when settle is off. + m.rgSettle.Begin(rg.ID, statusCreating, m.opts.Clock.Now(), + m.opts.SettleDuration(settle.DefaultCacheSettle)) + + result := cloneReplicationGroup(rg) + result.Status = m.settleRGStatus(rg.ID, result.Status) + + return &result +} + +// cloneReplicationGroup copies a group so callers cannot alias its stored +// member list. +func cloneReplicationGroup(rg *cachedriver.ReplicationGroup) cachedriver.ReplicationGroup { + out := *rg + out.MemberClusters = append([]string(nil), rg.MemberClusters...) + + return out +} + +// buildReplicationGroup applies the restore seed and defaults to a create +// request and validates it against its parents. The caller holds mu. +func (m *Mock) buildReplicationGroup( + ctx context.Context, cfg *cachedriver.ReplicationGroupConfig, +) (cachedriver.ReplicationGroup, error) { // A restore (SnapshotName set) seeds the unset config fields from the // snapshot before defaults are applied. - if err := m.seedReplicationGroupRestore(&cfg); err != nil { - return nil, err + if err := m.seedReplicationGroupRestore(cfg); err != nil { + return cachedriver.ReplicationGroup{}, err } engine := cfg.Engine @@ -44,15 +126,15 @@ func (m *Mock) CreateReplicationGroup( } if err := validateEngine(engine, true); err != nil { - return nil, err + return cachedriver.ReplicationGroup{}, err } if err := validateReplicationNodeCount(cfg.NumCacheNodes); err != nil { - return nil, err + return cachedriver.ReplicationGroup{}, err } if err := m.requireParameterGroup(cfg.ParameterGroupName); err != nil { - return nil, err + return cachedriver.ReplicationGroup{}, err } nodeType := cfg.NodeType @@ -60,10 +142,7 @@ func (m *Mock) CreateReplicationGroup( nodeType = defaultNodeType } - nodes := cfg.NumCacheNodes - if nodes < 1 { - nodes = 1 - } + nodes := max(cfg.NumCacheNodes, 1) engineVersion := cfg.EngineVersion if engineVersion == "" { @@ -71,7 +150,8 @@ func (m *Mock) CreateReplicationGroup( } region := regionctx.RegionOr(ctx, m.opts.Region) - rg := cachedriver.ReplicationGroup{ + + return cachedriver.ReplicationGroup{ ID: cfg.ID, Description: cfg.Description, Status: statusAvailable, @@ -81,39 +161,17 @@ func (m *Mock) CreateReplicationGroup( NumCacheNodes: nodes, // Callers read the primary endpoint to build a connection string; a // group without one is indistinguishable from a broken provision. - PrimaryAddress: fmt.Sprintf("%s.%s.cache.amazonaws.com", - cfg.ID, region), - PrimaryPort: defaultRedisPort, + PrimaryAddress: fmt.Sprintf("%s.%s.cache.amazonaws.com", cfg.ID, region), + PrimaryPort: defaultRedisPort, // The reader endpoint lets clients scale reads across the replicas. - ReaderAddress: fmt.Sprintf("%s-ro.%s.cache.amazonaws.com", - cfg.ID, region), + ReaderAddress: fmt.Sprintf("%s-ro.%s.cache.amazonaws.com", cfg.ID, region), ReaderPort: defaultRedisPort, MemberClusters: memberClusters(cfg.ID, nodes), AutomaticFailover: failoverStatus(cfg.AutomaticFailoverEnabled), SubnetGroupName: cfg.SubnetGroupName, ParameterGroupName: cfg.ParameterGroupName, - ARN: "arn:aws:elasticache:" + region + ":" + m.opts.AccountID + - ":replicationgroup:" + cfg.ID, - } - - // Opt-in: back the primary with a real Redis server, replacing the synthetic - // hostname with the host:port a client connects to. Routes through the same - // cacheengine helper the cache-cluster path uses so the two cannot drift. - if err := m.provisionPrimaryEndpoint(ctx, &rg); err != nil { - return nil, err - } - - m.replicationGroups.Set(cfg.ID, rg) - - // Under AsyncSettle a fresh group reports creating until the window elapses - // (CreateReplicationGroup โ†’ creating โ†’ available); a no-op when settle is off. - m.rgSettle.Begin(cfg.ID, statusCreating, m.opts.Clock.Now(), - m.opts.SettleDuration(settle.DefaultCacheSettle)) - - result := rg - result.Status = m.settleRGStatus(cfg.ID, result.Status) - - return &result, nil + ARN: "arn:aws:elasticache:" + region + ":" + m.opts.AccountID + ":replicationgroup:" + cfg.ID, + }, nil } // maxReplicationGroupNodes bounds the member-cluster count a replication group @@ -198,42 +256,95 @@ func splitEndpoint(endpoint string) (host string, port int, err error) { } // DescribeReplicationGroups returns the named groups, or all when none given. +// A group whose real engine is still being provisioned or torn down is reported +// with status creating or deleting. func (m *Mock) DescribeReplicationGroups( _ context.Context, ids []string, ) ([]cachedriver.ReplicationGroup, error) { + m.mu.Lock() + defer m.mu.Unlock() + if len(ids) == 0 { - all := m.replicationGroups.SortedValues() - for i := range all { - all[i].Status = m.settleRGStatus(all[i].ID, all[i].Status) + stored := m.replicationGroups.SortedValues() + out := make([]cachedriver.ReplicationGroup, 0, len(stored)+len(m.rgRes)) + + for i := range stored { + rg := cloneReplicationGroup(&stored[i]) + rg.Status = m.settleRGStatus(rg.ID, rg.Status) + out = append(out, rg) + } + + for _, r := range m.rgRes { + rg := cloneReplicationGroup(&r.record) + rg.Status = r.state + out = append(out, rg) } - return all, nil + slices.SortFunc(out, func(a, b cachedriver.ReplicationGroup) int { return strings.Compare(a.ID, b.ID) }) + + return out, nil } out := make([]cachedriver.ReplicationGroup, 0, len(ids)) for _, id := range ids { - rg, ok := m.replicationGroups.Get(id) - if !ok { - return nil, cerrors.Newf(cerrors.NotFound, - "ReplicationGroupNotFoundFault: replication group %q not found", id) + rg, err := m.describeReplicationGroup(id) + if err != nil { + return nil, err } - rg.Status = m.settleRGStatus(id, rg.Status) out = append(out, rg) } return out, nil } +// describeReplicationGroup reads one group, falling back to a pending +// reservation. The caller holds mu. +func (m *Mock) describeReplicationGroup(id string) (cachedriver.ReplicationGroup, error) { + if rg, ok := m.replicationGroups.Get(id); ok { + out := cloneReplicationGroup(&rg) + out.Status = m.settleRGStatus(id, out.Status) + + return out, nil + } + + if r, reserved := m.rgRes[id]; reserved { + out := cloneReplicationGroup(&r.record) + out.Status = r.state + + return out, nil + } + + return cachedriver.ReplicationGroup{}, rgNotFound(id) +} + +// mutableReplicationGroup returns the stored group a mutating call targets. A +// group that is still being created or is being deleted reports +// InvalidReplicationGroupState, not ReplicationGroupNotFoundFault. The caller +// holds mu. +func (m *Mock) mutableReplicationGroup(id string) (cachedriver.ReplicationGroup, error) { + if rg, ok := m.replicationGroups.Get(id); ok { + return rg, nil + } + + if r, reserved := m.rgRes[id]; reserved { + return cachedriver.ReplicationGroup{}, rgInvalidState(id, r.state) + } + + return cachedriver.ReplicationGroup{}, rgNotFound(id) +} + // ModifyReplicationGroup changes the node count of an existing group. func (m *Mock) ModifyReplicationGroup( _ context.Context, id string, numCacheNodes int, ) (*cachedriver.ReplicationGroup, error) { - rg, ok := m.replicationGroups.Get(id) - if !ok { - return nil, cerrors.Newf(cerrors.NotFound, - "ReplicationGroupNotFoundFault: replication group %q not found", id) + m.mu.Lock() + defer m.mu.Unlock() + + rg, err := m.mutableReplicationGroup(id) + if err != nil { + return nil, err } if err := validateReplicationNodeCount(numCacheNodes); err != nil { @@ -252,7 +363,7 @@ func (m *Mock) ModifyReplicationGroup( m.rgSettle.Begin(id, statusModifying, m.opts.Clock.Now(), m.opts.SettleDuration(settle.DefaultCacheModifySettle)) - result := rg + result := cloneReplicationGroup(&rg) result.Status = m.settleRGStatus(id, result.Status) return &result, nil @@ -264,10 +375,12 @@ func (m *Mock) ModifyReplicationGroup( func (m *Mock) ModifyReplicationGroupParameterGroup( _ context.Context, id, name string, ) (*cachedriver.ReplicationGroup, error) { - rg, ok := m.replicationGroups.Get(id) - if !ok { - return nil, cerrors.Newf(cerrors.NotFound, - "ReplicationGroupNotFoundFault: replication group %q not found", id) + m.mu.Lock() + defer m.mu.Unlock() + + rg, err := m.mutableReplicationGroup(id) + if err != nil { + return nil, err } if err := m.requireParameterGroup(name); err != nil { @@ -277,7 +390,7 @@ func (m *Mock) ModifyReplicationGroupParameterGroup( rg.ParameterGroupName = name m.replicationGroups.Set(id, rg) - result := rg + result := cloneReplicationGroup(&rg) result.Status = m.settleRGStatus(id, result.Status) return &result, nil @@ -288,48 +401,70 @@ func (m *Mock) ModifyReplicationGroupParameterGroup( // group (which then shows up in DescribeSnapshots, as real ElastiCache does). // When opts.RetainPrimaryCluster is set the primary node group is kept as a // standalone cache cluster instead of being torn down. +// +// Otherwise the group is removed under mu and its id is held in the deleting +// state until the real engine (if any) has been torn down. If the teardown +// fails the group is put back, as it was never deleted. func (m *Mock) DeleteReplicationGroup( ctx context.Context, id string, opts cachedriver.DeleteReplicationGroupOptions, ) error { - rg, ok := m.replicationGroups.Get(id) - if !ok { - return cerrors.Newf(cerrors.NotFound, - "ReplicationGroupNotFoundFault: replication group %q not found", id) + m.mu.Lock() + + rg, err := m.mutableReplicationGroup(id) + if err != nil { + m.mu.Unlock() + + return err } if opts.FinalSnapshotIdentifier != "" { - if _, err := m.CreateSnapshot(ctx, cachedriver.SnapshotConfig{ + if _, err := m.createSnapshotLocked(ctx, cachedriver.SnapshotConfig{ SnapshotName: opts.FinalSnapshotIdentifier, ReplicationGroupID: id, }); err != nil { + m.mu.Unlock() + return err } } + m.replicationGroups.Delete(id) + m.rgSettle.Clear(id) + if opts.RetainPrimaryCluster { m.retainPrimaryCluster(&rg) - m.replicationGroups.Delete(id) - m.rgSettle.Clear(id) + m.mu.Unlock() return nil } + last := cloneReplicationGroup(&rg) + last.Status = statusDeleting + m.rgRes[id] = &reservation[cachedriver.ReplicationGroup]{state: statusDeleting, record: last} + m.mu.Unlock() + info := cachedriver.CacheInfo{Name: rg.ID, Engine: rg.Engine} - if err := cacheengine.Deprovision(ctx, m.opts.CacheEngine, &info); err != nil { - return err - } + derr := cacheengine.Deprovision(ctx, m.opts.CacheEngine, &info) - m.replicationGroups.Delete(id) - m.rgSettle.Clear(id) + m.mu.Lock() + defer m.mu.Unlock() + + delete(m.rgRes, id) + + if derr != nil { + m.replicationGroups.Set(id, rg) + + return derr + } return nil } // retainPrimaryCluster keeps the primary node group of a deleted replication // group as a standalone cache cluster, so DescribeCacheClusters still returns -// it. The backing engine (if any) is handed over untouched. +// it. The backing engine (if any) is handed over untouched. The caller holds mu. func (m *Mock) retainPrimaryCluster(rg *cachedriver.ReplicationGroup) { - if m.caches.Has(rg.ID) { + if m.caches.Has(rg.ID) || m.cacheRes[rg.ID] != nil { return } diff --git a/providers/aws/elasticache/snapshot.go b/providers/aws/elasticache/snapshot.go index f48743ede..66a9f083a 100644 --- a/providers/aws/elasticache/snapshot.go +++ b/providers/aws/elasticache/snapshot.go @@ -20,14 +20,22 @@ func (m *Mock) snapshotARN(region, name string) string { // type, version, port, node count) is copied into the snapshot so a restore can // recreate a like-for-like cluster. func (m *Mock) CreateSnapshot(ctx context.Context, cfg cachedriver.SnapshotConfig) (*cachedriver.Snapshot, error) { + m.mu.Lock() + defer m.mu.Unlock() + + return m.createSnapshotLocked(ctx, cfg) +} + +// createSnapshotLocked is CreateSnapshot for a caller that already holds mu, +// such as DeleteReplicationGroup taking a final snapshot. +func (m *Mock) createSnapshotLocked(ctx context.Context, cfg cachedriver.SnapshotConfig) (*cachedriver.Snapshot, error) { name := strings.ToLower(cfg.SnapshotName) if name == "" { return nil, cerrors.New(cerrors.InvalidArgument, "SnapshotName is required") } if m.snapshots.Has(name) { - return nil, cerrors.Newf(cerrors.AlreadyExists, - "SnapshotAlreadyExistsFault: snapshot %q already exists", name) + return nil, snapshotAlreadyExists(name) } // A snapshot is a child of the cluster or replication group it backs up, so @@ -61,10 +69,9 @@ func (m *Mock) CreateSnapshot(ctx context.Context, cfg cachedriver.SnapshotConfi func (m *Mock) fillSnapshotSource(cfg cachedriver.SnapshotConfig, snap *cachedriver.Snapshot, fallbackRegion string) error { switch { case cfg.CacheClusterID != "": - cd, ok := m.caches.Get(cfg.CacheClusterID) - if !ok { - return cerrors.Newf(cerrors.NotFound, - "CacheClusterNotFound: cache cluster %q not found", cfg.CacheClusterID) + cd, lookupErr := m.mutableCache(cfg.CacheClusterID) + if lookupErr != nil { + return lookupErr } if err := checkSnapshotSupported(cd.info.Engine, cd.info.NodeType); err != nil { @@ -82,10 +89,9 @@ func (m *Mock) fillSnapshotSource(cfg cachedriver.SnapshotConfig, snap *cachedri snap.NumCacheNodes = cd.info.NumCacheNodes } case cfg.ReplicationGroupID != "": - rg, ok := m.replicationGroups.Get(cfg.ReplicationGroupID) - if !ok { - return cerrors.Newf(cerrors.NotFound, - "ReplicationGroupNotFoundFault: replication group %q not found", cfg.ReplicationGroupID) + rg, err := m.mutableReplicationGroup(cfg.ReplicationGroupID) + if err != nil { + return err } snap.ReplicationGroupID = cfg.ReplicationGroupID @@ -112,13 +118,13 @@ func (m *Mock) fillSnapshotSource(cfg cachedriver.SnapshotConfig, snap *cachedri // running on a cache.t1.micro node with SnapshotFeatureNotSupportedFault. func checkSnapshotSupported(engine, nodeType string) error { if engine == engineMemcached { - return cerrors.New(cerrors.FailedPrecondition, - "SnapshotFeatureNotSupportedFault: snapshots are not supported for Memcached clusters") + return newFault(cerrors.FailedPrecondition, FaultSnapshotFeatureNotSupported, + "snapshots are not supported for Memcached clusters") } if nodeType == "cache.t1.micro" { - return cerrors.New(cerrors.FailedPrecondition, - "SnapshotFeatureNotSupportedFault: snapshots are not supported for cache.t1.micro nodes") + return newFault(cerrors.FailedPrecondition, FaultSnapshotFeatureNotSupported, + "snapshots are not supported for cache.t1.micro nodes") } return nil @@ -148,10 +154,12 @@ func (m *Mock) CopySnapshot(ctx context.Context, cfg cachedriver.CopySnapshotCon return nil, cerrors.New(cerrors.InvalidArgument, "TargetSnapshotName is required") } + m.mu.Lock() + defer m.mu.Unlock() + src, ok := m.snapshots.Get(source) if !ok { - return nil, cerrors.Newf(cerrors.NotFound, - "SnapshotNotFoundFault: snapshot %q not found", source) + return nil, snapshotNotFound(source) } region := regionctx.RegionOr(ctx, m.opts.Region) @@ -166,8 +174,7 @@ func (m *Mock) CopySnapshot(ctx context.Context, cfg cachedriver.CopySnapshotCon // SetIfAbsent is the atomic create-if-new guard: a concurrent copy to the same // target loses the race and reports the duplicate, never silently overwrites. if !m.snapshots.SetIfAbsent(target, dst) { - return nil, cerrors.Newf(cerrors.AlreadyExists, - "SnapshotAlreadyExistsFault: snapshot %q already exists", target) + return nil, snapshotAlreadyExists(target) } return &dst, nil @@ -179,18 +186,15 @@ func (m *Mock) CopySnapshot(ctx context.Context, cfg cachedriver.CopySnapshotCon func (m *Mock) DeleteSnapshot(_ context.Context, name string) (*cachedriver.Snapshot, error) { key := strings.ToLower(name) + m.mu.Lock() + defer m.mu.Unlock() + snap, ok := m.snapshots.Get(key) if !ok { - return nil, cerrors.Newf(cerrors.NotFound, - "SnapshotNotFoundFault: snapshot %q not found", key) + return nil, snapshotNotFound(key) } - // Gate the response on the delete winning: if a concurrent caller removed the - // snapshot between the Get and here, report the not-found it now is. - if !m.snapshots.Delete(key) { - return nil, cerrors.Newf(cerrors.NotFound, - "SnapshotNotFoundFault: snapshot %q not found", key) - } + m.snapshots.Delete(key) snap.Status = "deleting" @@ -209,8 +213,7 @@ func (m *Mock) restoreSeed(name string) (*cachedriver.Snapshot, error) { snap, ok := m.snapshots.Get(key) if !ok { - return nil, cerrors.Newf(cerrors.NotFound, - "SnapshotNotFoundFault: snapshot %q not found", key) + return nil, snapshotNotFound(key) } return &snap, nil @@ -281,11 +284,13 @@ func (m *Mock) seedReplicationGroupRestore(cfg *cachedriver.ReplicationGroupConf func (m *Mock) DescribeSnapshots( _ context.Context, filter cachedriver.SnapshotFilter, ) ([]cachedriver.Snapshot, error) { + m.mu.Lock() + defer m.mu.Unlock() + if name := strings.ToLower(filter.SnapshotName); name != "" { snap, ok := m.snapshots.Get(name) if !ok { - return nil, cerrors.Newf(cerrors.NotFound, - "SnapshotNotFoundFault: snapshot %q not found", name) + return nil, snapshotNotFound(name) } return []cachedriver.Snapshot{snap}, nil diff --git a/providers/aws/elasticache/subnet_group.go b/providers/aws/elasticache/subnet_group.go index c8368f4c6..15c96b5a0 100644 --- a/providers/aws/elasticache/subnet_group.go +++ b/providers/aws/elasticache/subnet_group.go @@ -34,11 +34,8 @@ func (m *Mock) CreateCacheSubnetGroup( return nil, cerrors.New(cerrors.InvalidArgument, "at least one subnet is required") } - if m.subnetGroups.Has(cfg.Name) { - return nil, cerrors.Newf(cerrors.AlreadyExists, - "CacheSubnetGroupAlreadyExists: cache subnet group %q already exists", cfg.Name) - } - + // The VPC lookup is a call into the networking mock, so it runs before mu + // is taken. sg := cachedriver.SubnetGroup{ Name: cfg.Name, Description: cfg.Description, @@ -48,15 +45,28 @@ func (m *Mock) CreateCacheSubnetGroup( ARN: "arn:aws:elasticache:" + regionctx.RegionOr(ctx, m.opts.Region) + ":" + m.opts.AccountID + ":subnetgroup:" + cfg.Name, } - m.subnetGroups.Set(cfg.Name, sg) - return &sg, nil + m.mu.Lock() + defer m.mu.Unlock() + + if !m.subnetGroups.SetIfAbsent(cfg.Name, sg) { + return nil, newFault(cerrors.AlreadyExists, FaultCacheSubnetGroupAlreadyExists, + "cache subnet group %q already exists", cfg.Name) + } + + out := sg + out.SubnetIDs = append([]string(nil), sg.SubnetIDs...) + + return &out, nil } // DescribeCacheSubnetGroups returns the named groups, or all when none given. func (m *Mock) DescribeCacheSubnetGroups( _ context.Context, names []string, ) ([]cachedriver.SubnetGroup, error) { + m.mu.Lock() + defer m.mu.Unlock() + if len(names) == 0 { return m.subnetGroups.SortedValues(), nil } @@ -66,8 +76,7 @@ func (m *Mock) DescribeCacheSubnetGroups( for _, n := range names { sg, ok := m.subnetGroups.Get(n) if !ok { - return nil, cerrors.Newf(cerrors.NotFound, - "CacheSubnetGroupNotFoundFault: cache subnet group %q not found", n) + return nil, subnetGroupNotFound(n) } out = append(out, sg) @@ -77,31 +86,70 @@ func (m *Mock) DescribeCacheSubnetGroups( } // DeleteCacheSubnetGroup deletes a cache subnet group. +// +// Real ElastiCache refuses to delete a subnet group associated with any +// clusters: standalone cache clusters count, not only replication groups. A +// teardown that skipped this would leave the group deleted and a live cluster +// pointing at nothing. The scan and the delete run under mu, and clusters still +// being created count as users, so a concurrent create cannot slip in between. func (m *Mock) DeleteCacheSubnetGroup(_ context.Context, name string) error { - // Real ElastiCache refuses to delete a subnet group associated with any - // clusters: standalone cache clusters count, not only replication groups. - // A teardown that skipped this would leave the group deleted and a live - // cluster pointing at nothing. + m.mu.Lock() + defer m.mu.Unlock() + + if user, inUse := m.subnetGroupUser(name); inUse { + return newFault(cerrors.FailedPrecondition, FaultCacheSubnetGroupInUse, + "cache subnet group %q is in use by %q", name, user) + } + + if !m.subnetGroups.Delete(name) { + return subnetGroupNotFound(name) + } + + return nil +} + +// subnetGroupUser returns a cluster or replication group (stored or reserved) +// placed in the named subnet group. The caller holds mu. +func (m *Mock) subnetGroupUser(name string) (string, bool) { + return m.firstUser(func(info *cachedriver.CacheInfo) bool { + return info.SubnetGroupName == name + }, func(rg *cachedriver.ReplicationGroup) bool { + return rg.SubnetGroupName == name + }) +} + +// firstUser returns the id of the first cluster or replication group, stored or +// reserved, matching the predicates. The caller holds mu. +func (m *Mock) firstUser( + cacheUses func(*cachedriver.CacheInfo) bool, rgUses func(*cachedriver.ReplicationGroup) bool, +) (string, bool) { for _, cd := range m.caches.All() { - if cd.info.SubnetGroupName == name { - return cerrors.Newf(cerrors.FailedPrecondition, - "CacheSubnetGroupInUse: cache subnet group %q is in use by %q", name, cd.info.Name) + if cacheUses(&cd.info) { + return cd.info.Name, true } } - for _, rg := range m.replicationGroups.All() { - if rg.SubnetGroupName == name { - return cerrors.Newf(cerrors.FailedPrecondition, - "CacheSubnetGroupInUse: cache subnet group %q is in use by %q", name, rg.ID) + for _, r := range m.cacheRes { + if cacheUses(&r.record) { + return r.record.Name, true } } - if !m.subnetGroups.Delete(name) { - return cerrors.Newf(cerrors.NotFound, - "CacheSubnetGroupNotFoundFault: cache subnet group %q not found", name) + groups := m.replicationGroups.All() + for id := range groups { + rg := groups[id] + if rgUses(&rg) { + return id, true + } } - return nil + for _, r := range m.rgRes { + if rgUses(&r.record) { + return r.record.ID, true + } + } + + return "", false } // resolveVPCID reports the VPC the member subnets belong to. diff --git a/providers/aws/elasticache/tags.go b/providers/aws/elasticache/tags.go index 69ad2af14..ffabe2bf2 100644 --- a/providers/aws/elasticache/tags.go +++ b/providers/aws/elasticache/tags.go @@ -6,12 +6,16 @@ import ( "strings" cerrors "github.com/stackshy/cloudemu/v2/errors" + cachedriver "github.com/stackshy/cloudemu/v2/services/cache/driver" ) -// resourceExists reports whether an ElastiCache ARN names a resource this mock -// knows about (a cache cluster, replication group, or parameter group). Tag -// operations on an unknown ARN return NotFound, matching AWS. -func (m *Mock) resourceExists(arn string) bool { +// taggableResource reports whether an ElastiCache ARN names a resource this +// mock knows about (a cache cluster, replication group, parameter group, or +// subnet group), returning the resource's not-found fault when it does not. +// A cluster or replication group whose real engine is still being provisioned +// or torn down exists in AWS, so a tag write on it reports the invalid-state +// fault, while a tag read is allowed. The caller holds mu. +func (m *Mock) taggableResource(arn string, write bool) error { name := arn if i := strings.LastIndex(arn, ":"); i >= 0 { name = arn[i+1:] @@ -19,15 +23,43 @@ func (m *Mock) resourceExists(arn string) bool { switch { case strings.Contains(arn, ":cluster:"): - return m.caches.Has(name) + return reservedOr(m.caches.Has(name), m.cacheRes[name], write, + func(state string) error { return cacheInvalidState(name, state) }, cacheNotFound(name)) case strings.Contains(arn, ":replicationgroup:"): - return m.replicationGroups.Has(name) + return reservedOr(m.replicationGroups.Has(name), m.rgRes[name], write, + func(state string) error { return rgInvalidState(name, state) }, rgNotFound(name)) case strings.Contains(arn, ":parametergroup:"): - return m.parameterGroups.Has(name) + if m.parameterGroups.Has(name) { + return nil + } + + return parameterGroupNotFound(name) case strings.Contains(arn, ":subnetgroup:"): - return m.subnetGroups.Has(name) + if m.subnetGroups.Has(name) { + return nil + } + + return subnetGroupNotFound(name) + default: + // Snapshot ARNs are not taggable yet (#1695); an unknown type keeps + // answering not-found as before. + return newFault(cerrors.NotFound, FaultCacheClusterNotFound, "cache resource %q not found", arn) + } +} + +// reservedOr resolves a tag target that may be stored or reserved: stored is +// taggable, reserved is readable but not writable, and anything else is +// notFound. +func reservedOr[T any](stored bool, r *reservation[T], write bool, invalid func(string) error, notFound error) error { + switch { + case stored: + return nil + case r != nil && write: + return invalid(r.state) + case r != nil: + return nil default: - return false + return notFound } } @@ -38,9 +70,6 @@ func (m *Mock) seedTags(arn string, tags map[string]string) { return } - m.tagMu.Lock() - defer m.tagMu.Unlock() - stored := make(map[string]string, len(tags)) for k, v := range tags { stored[k] = v @@ -51,12 +80,12 @@ func (m *Mock) seedTags(arn string, tags map[string]string) { // AddTags adds or overwrites tags on an ElastiCache resource by ARN. func (m *Mock) AddTags(_ context.Context, arn string, tags map[string]string) error { - if !m.resourceExists(arn) { - return cerrors.Newf(cerrors.NotFound, "cache resource %q not found", arn) - } + m.mu.Lock() + defer m.mu.Unlock() - m.tagMu.Lock() - defer m.tagMu.Unlock() + if err := m.taggableResource(arn, true); err != nil { + return err + } stored := m.tagsByARN[arn] if stored == nil { @@ -73,12 +102,12 @@ func (m *Mock) AddTags(_ context.Context, arn string, tags map[string]string) er // ListTags returns the tags on an ElastiCache resource by ARN. func (m *Mock) ListTags(_ context.Context, arn string) (map[string]string, error) { - if !m.resourceExists(arn) { - return nil, cerrors.Newf(cerrors.NotFound, "cache resource %q not found", arn) - } + m.mu.Lock() + defer m.mu.Unlock() - m.tagMu.Lock() - defer m.tagMu.Unlock() + if err := m.taggableResource(arn, false); err != nil { + return nil, err + } out := make(map[string]string, len(m.tagsByARN[arn])) for k, v := range m.tagsByARN[arn] { @@ -90,12 +119,12 @@ func (m *Mock) ListTags(_ context.Context, arn string) (map[string]string, error // RemoveTags deletes the named tag keys from an ElastiCache resource by ARN. func (m *Mock) RemoveTags(_ context.Context, arn string, keys []string) error { - if !m.resourceExists(arn) { - return cerrors.Newf(cerrors.NotFound, "cache resource %q not found", arn) - } + m.mu.Lock() + defer m.mu.Unlock() - m.tagMu.Lock() - defer m.tagMu.Unlock() + if err := m.taggableResource(arn, true); err != nil { + return err + } stored := m.tagsByARN[arn] for _, k := range keys { @@ -111,8 +140,12 @@ func (m *Mock) CreateCacheParameterGroup(_ context.Context, name, family, descri return nil, cerrors.New(cerrors.InvalidArgument, "CacheParameterGroupName is required") } + m.mu.Lock() + defer m.mu.Unlock() + if _, exists := m.lookupParameterGroup(name); exists { - return nil, cerrors.Newf(cerrors.AlreadyExists, "cache parameter group %q already exists", name) + return nil, newFault(cerrors.AlreadyExists, FaultCacheParameterGroupAlreadyExists, + "cache parameter group %q already exists", name) } pg := ParameterGroup{Name: name, Family: family, Description: description, Overrides: map[string]string{}} @@ -124,6 +157,9 @@ func (m *Mock) CreateCacheParameterGroup(_ context.Context, name, family, descri // DescribeCacheParameterGroups returns the named groups, or all when none given. // With no names it lists the built-in default groups too, as ElastiCache does. func (m *Mock) DescribeCacheParameterGroups(_ context.Context, names []string) ([]ParameterGroup, error) { + m.mu.Lock() + defer m.mu.Unlock() + if len(names) == 0 { out := m.parameterGroups.SortedValues() @@ -142,7 +178,7 @@ func (m *Mock) DescribeCacheParameterGroups(_ context.Context, names []string) ( for _, name := range names { pg, ok := m.lookupParameterGroup(name) if !ok { - return nil, cerrors.Newf(cerrors.NotFound, "cache parameter group %q not found", name) + return nil, parameterGroupNotFound(name) } out = append(out, pg) @@ -152,29 +188,30 @@ func (m *Mock) DescribeCacheParameterGroups(_ context.Context, names []string) ( } // DeleteCacheParameterGroup removes an ElastiCache cache parameter group. -// Real ElastiCache refuses to delete a default group or one a cluster still uses. +// Real ElastiCache refuses to delete a default group or one a cluster still +// uses. The in-use scan and the delete run under mu, and clusters or groups +// still being created count as users, so a create that has validated the +// parameter group cannot commit after the group is gone. func (m *Mock) DeleteCacheParameterGroup(_ context.Context, name string) error { if err := rejectDefaultGroupChange(name); err != nil { return err } - for _, cd := range m.caches.All() { - if cd.info.ParameterGroupName == name { - return cerrors.Newf(cerrors.FailedPrecondition, - "InvalidCacheParameterGroupState: cache parameter group %q is in use by %q", name, cd.info.Name) - } - } + m.mu.Lock() + defer m.mu.Unlock() - groups := m.replicationGroups.All() - for id := range groups { - if groups[id].ParameterGroupName == name { - return cerrors.Newf(cerrors.FailedPrecondition, - "InvalidCacheParameterGroupState: cache parameter group %q is in use by %q", name, id) - } + user, inUse := m.firstUser(func(info *cachedriver.CacheInfo) bool { + return info.ParameterGroupName == name + }, func(rg *cachedriver.ReplicationGroup) bool { + return rg.ParameterGroupName == name + }) + if inUse { + return newFault(cerrors.FailedPrecondition, FaultInvalidCacheParameterGroupState, + "cache parameter group %q is in use by %q", name, user) } if !m.parameterGroups.Delete(name) { - return cerrors.Newf(cerrors.NotFound, "cache parameter group %q not found", name) + return parameterGroupNotFound(name) } return nil diff --git a/providers/aws/elasticache/validation.go b/providers/aws/elasticache/validation.go index 41b0f2f79..26996f800 100644 --- a/providers/aws/elasticache/validation.go +++ b/providers/aws/elasticache/validation.go @@ -142,7 +142,7 @@ func (m *Mock) requireParameterGroup(name string) error { } if _, ok := m.lookupParameterGroup(name); !ok { - return cerrors.Newf(cerrors.NotFound, "cache parameter group %q not found", name) + return parameterGroupNotFound(name) } return nil diff --git a/server/aws/elasticache/faults.go b/server/aws/elasticache/faults.go new file mode 100644 index 000000000..df5995262 --- /dev/null +++ b/server/aws/elasticache/faults.go @@ -0,0 +1,60 @@ +package elasticache + +import ( + "errors" + "net/http" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + ecprovider "github.com/stackshy/cloudemu/v2/providers/aws/elasticache" + "github.com/stackshy/cloudemu/v2/server/wire/awsquery" +) + +// awsFaulter is an error that names its ElastiCache wire fault code. The +// provider's typed Fault implements it. +type awsFaulter interface { + AWSFault() string +} + +// faultStatus is the HTTP status ElastiCache answers each fault code with, from +// the botocore model (error.httpStatusCode). Note that CacheSubnetGroupNotFound +// is a 400 in the real API, unlike most not-found faults. +func faultStatus(code string) int { + switch code { + case ecprovider.FaultCacheClusterNotFound, + ecprovider.FaultReplicationGroupNotFound, + ecprovider.FaultCacheParameterGroupNotFound, + ecprovider.FaultSnapshotNotFound: + return http.StatusNotFound + default: + return http.StatusBadRequest + } +} + +// writeErr maps cloudemu errors to ElastiCache XML error responses. +// +// A typed fault carries its wire code; the message is written without any code +// token, since a real ElastiCache never begins with one. Errors with +// no fault code (request parsing, unsupported capabilities) fall back to a +// code chosen by the canonical error kind. +func writeErr(w http.ResponseWriter, err error) { + msg := cerrors.Message(err) + + var f awsFaulter + if errors.As(err, &f) { + awsquery.WriteXMLError(w, faultStatus(f.AWSFault()), f.AWSFault(), msg) + return + } + + switch { + case cerrors.IsNotFound(err): + awsquery.WriteXMLError(w, http.StatusNotFound, ecprovider.FaultCacheClusterNotFound, msg) + case cerrors.IsAlreadyExists(err): + awsquery.WriteXMLError(w, http.StatusBadRequest, ecprovider.FaultCacheClusterAlreadyExists, msg) + case cerrors.IsInvalidArgument(err): + awsquery.WriteXMLError(w, http.StatusBadRequest, "InvalidParameterValue", msg) + case cerrors.IsFailedPrecondition(err): + awsquery.WriteXMLError(w, http.StatusBadRequest, ecprovider.FaultInvalidCacheClusterState, msg) + default: + awsquery.WriteXMLError(w, http.StatusInternalServerError, "InternalFailure", msg) + } +} diff --git a/server/aws/elasticache/faults_sdk_roundtrip_test.go b/server/aws/elasticache/faults_sdk_roundtrip_test.go new file mode 100644 index 000000000..ed05e5654 --- /dev/null +++ b/server/aws/elasticache/faults_sdk_roundtrip_test.go @@ -0,0 +1,88 @@ +package elasticache_test + +import ( + "context" + "errors" + "net/http" + "strings" + "testing" + + "github.com/aws/aws-sdk-go-v2/aws" + awshttp "github.com/aws/aws-sdk-go-v2/aws/transport/http" + awselasticache "github.com/aws/aws-sdk-go-v2/service/elasticache" + ectypes "github.com/aws/aws-sdk-go-v2/service/elasticache/types" + "github.com/aws/smithy-go" +) + +// assertFault checks the SDK error's code, HTTP status, and that its message +// carries no code token. +func assertFault(t *testing.T, op string, err error, wantCode string, wantStatus int) { + t.Helper() + + var apiErr smithy.APIError + if !errors.As(err, &apiErr) || apiErr.ErrorCode() != wantCode { + t.Fatalf("%s: got %v, want %s", op, err, wantCode) + } + + msg := apiErr.ErrorMessage() + if msg == "" || strings.HasPrefix(msg, wantCode) || strings.Contains(msg, "NotFound: ") || + strings.Contains(msg, "AlreadyExists: ") || strings.Contains(msg, "FailedPrecondition: ") { + t.Fatalf("%s: message %q is empty or leaks a code prefix", op, msg) + } + + var respErr *awshttp.ResponseError + if !errors.As(err, &respErr) || respErr.HTTPStatusCode() != wantStatus { + t.Fatalf("%s: HTTP status = %v, want %d", op, err, wantStatus) + } +} + +// TestTypedFaultsSurviveSDK proves the provider's typed faults reach the SDK as +// its modelled error types, with the botocore status and a prefix-free message. +func TestTypedFaultsSurviveSDK(t *testing.T) { + client := newSDKClient(t) + ctx := context.Background() + + in := &awselasticache.CreateCacheClusterInput{ + CacheClusterId: aws.String("dup"), Engine: aws.String("redis"), + CacheNodeType: aws.String("cache.t3.micro"), NumCacheNodes: aws.Int32(1), + } + if _, err := client.CreateCacheCluster(ctx, in); err != nil { + t.Fatalf("CreateCacheCluster: %v", err) + } + + _, err := client.CreateCacheCluster(ctx, in) + assertFault(t, "CreateCacheCluster(dup)", err, "CacheClusterAlreadyExists", http.StatusBadRequest) + + var exists *ectypes.CacheClusterAlreadyExistsFault + if !errors.As(err, &exists) { + t.Fatalf("duplicate create: %T is not *types.CacheClusterAlreadyExistsFault", err) + } + + _, err = client.DescribeReplicationGroups(ctx, &awselasticache.DescribeReplicationGroupsInput{ + ReplicationGroupId: aws.String("nope"), + }) + assertFault(t, "DescribeReplicationGroups(nope)", err, "ReplicationGroupNotFoundFault", http.StatusNotFound) + + var rgMissing *ectypes.ReplicationGroupNotFoundFault + if !errors.As(err, &rgMissing) { + t.Fatalf("missing group: %T is not *types.ReplicationGroupNotFoundFault", err) + } + + _, err = client.DeleteCacheSubnetGroup(ctx, &awselasticache.DeleteCacheSubnetGroupInput{ + CacheSubnetGroupName: aws.String("nope"), + }) + assertFault(t, "DeleteCacheSubnetGroup(nope)", err, "CacheSubnetGroupNotFoundFault", http.StatusBadRequest) + + if _, err = client.CreateCacheParameterGroup(ctx, &awselasticache.CreateCacheParameterGroupInput{ + CacheParameterGroupName: aws.String("pg"), CacheParameterGroupFamily: aws.String("redis7"), + Description: aws.String("d"), + }); err != nil { + t.Fatalf("CreateCacheParameterGroup: %v", err) + } + + _, err = client.CreateCacheParameterGroup(ctx, &awselasticache.CreateCacheParameterGroupInput{ + CacheParameterGroupName: aws.String("pg"), CacheParameterGroupFamily: aws.String("redis7"), + Description: aws.String("d"), + }) + assertFault(t, "CreateCacheParameterGroup(dup)", err, "CacheParameterGroupAlreadyExists", http.StatusBadRequest) +} diff --git a/server/aws/elasticache/faults_test.go b/server/aws/elasticache/faults_test.go new file mode 100644 index 000000000..c8d495129 --- /dev/null +++ b/server/aws/elasticache/faults_test.go @@ -0,0 +1,107 @@ +package elasticache + +import ( + "encoding/xml" + "net/http" + "net/http/httptest" + "strings" + "testing" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + ecprovider "github.com/stackshy/cloudemu/v2/providers/aws/elasticache" +) + +// testFault is a stand-in for the provider's typed Fault. +type testFault struct { + err *cerrors.Error + code string +} + +func (f testFault) Error() string { return f.err.Error() } +func (f testFault) Unwrap() error { return f.err } +func (f testFault) AWSFault() string { return f.code } + +type xmlErrorBody struct { + Code string `xml:"Error>Code"` + Message string `xml:"Error>Message"` +} + +func recordWriteErr(t *testing.T, err error) (status int, body xmlErrorBody) { + t.Helper() + + rec := httptest.NewRecorder() + writeErr(rec, err) + + if uerr := xml.Unmarshal(rec.Body.Bytes(), &body); uerr != nil { + t.Fatalf("unmarshal error body %q: %v", rec.Body.String(), uerr) + } + + return rec.Code, body +} + +// allFaultCodes lists every fault code the provider emits. +func allFaultCodes() []string { + return []string{ + ecprovider.FaultCacheClusterNotFound, ecprovider.FaultCacheClusterAlreadyExists, + ecprovider.FaultInvalidCacheClusterState, ecprovider.FaultReplicationGroupNotFound, + ecprovider.FaultReplicationGroupAlreadyExists, ecprovider.FaultInvalidReplicationGroupState, + ecprovider.FaultCacheSubnetGroupNotFound, ecprovider.FaultCacheSubnetGroupAlreadyExists, + ecprovider.FaultCacheSubnetGroupInUse, ecprovider.FaultCacheParameterGroupNotFound, + ecprovider.FaultCacheParameterGroupAlreadyExists, ecprovider.FaultInvalidCacheParameterGroupState, + ecprovider.FaultSnapshotNotFound, ecprovider.FaultSnapshotAlreadyExists, + ecprovider.FaultSnapshotFeatureNotSupported, + } +} + +// TestWriteErrStripsCodeFromMessage proves the wire carries only the +// human text: neither the canonical taxonomy token ("NotFound: ") nor the +// fault code itself, as a real ElastiCache message never starts with one. +func TestWriteErrStripsCodeFromMessage(t *testing.T) { + for _, code := range allFaultCodes() { + t.Run(code, func(t *testing.T) { + err := testFault{err: cerrors.New(cerrors.NotFound, "resource \"x\" not found"), code: code} + + _, body := recordWriteErr(t, err) + + if body.Code != code { + t.Fatalf("Code = %q, want %q", body.Code, code) + } + + if body.Message != `resource "x" not found` { + t.Fatalf("Message = %q, want the bare human message", body.Message) + } + + if strings.HasPrefix(body.Message, code) || strings.HasPrefix(body.Message, "NotFound") { + t.Fatalf("Message %q leaks a code prefix", body.Message) + } + }) + } +} + +// TestWriteErrStatusFromTable checks the HTTP status per fault code against the +// botocore model, including CacheSubnetGroupNotFoundFault answering 400. +func TestWriteErrStatusFromTable(t *testing.T) { + notFound := map[string]bool{ + ecprovider.FaultCacheClusterNotFound: true, + ecprovider.FaultReplicationGroupNotFound: true, + ecprovider.FaultCacheParameterGroupNotFound: true, + ecprovider.FaultSnapshotNotFound: true, + } + + for _, code := range allFaultCodes() { + want := http.StatusBadRequest + if notFound[code] { + want = http.StatusNotFound + } + + status, _ := recordWriteErr(t, testFault{err: cerrors.New(cerrors.NotFound, "m"), code: code}) + if status != want { + t.Errorf("%s: status %d, want %d", code, status, want) + } + } + + status, body := recordWriteErr(t, cerrors.New(cerrors.InvalidArgument, "bad input")) + if status != http.StatusBadRequest || body.Code != "InvalidParameterValue" || body.Message != "bad input" { + t.Fatalf("plain InvalidArgument = %d %+v, want 400 InvalidParameterValue", status, body) + } +} diff --git a/server/aws/elasticache/handler.go b/server/aws/elasticache/handler.go index 0e97812f9..542d0bda8 100644 --- a/server/aws/elasticache/handler.go +++ b/server/aws/elasticache/handler.go @@ -23,7 +23,6 @@ import ( "net/http" "strings" - cerrors "github.com/stackshy/cloudemu/v2/errors" "github.com/stackshy/cloudemu/v2/server/wire/awsauthz" "github.com/stackshy/cloudemu/v2/server/wire/awsquery" cachedriver "github.com/stackshy/cloudemu/v2/services/cache/driver" @@ -240,86 +239,6 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } -// writeErr maps cloudemu errors to ElastiCache XML error responses. -func writeErr(w http.ResponseWriter, err error) { - msg := cerrors.Message(err) - - switch { - case cerrors.IsNotFound(err): - awsquery.WriteXMLError(w, http.StatusNotFound, notFoundCode(err), msg) - case cerrors.IsAlreadyExists(err): - awsquery.WriteXMLError(w, http.StatusBadRequest, alreadyExistsCode(err), msg) - case cerrors.IsInvalidArgument(err): - awsquery.WriteXMLError(w, http.StatusBadRequest, "InvalidParameterValue", msg) - case cerrors.IsFailedPrecondition(err): - awsquery.WriteXMLError(w, http.StatusBadRequest, failedPreconditionCode(err), msg) - default: - awsquery.WriteXMLError(w, http.StatusInternalServerError, "InternalFailure", msg) - } -} - -// failedPreconditionCode picks the AWS-shaped error code for a failed -// precondition. The driver encodes the intended fault as a prefix on the -// message (a subnet group still in use, or an unsupported snapshot); a caller -// matching the SDK's typed errors sees the code, not the message, so it has to -// be surfaced as the wire Code. Anything else is a cache-cluster state error. -func failedPreconditionCode(err error) string { - msg := err.Error() - - switch { - case strings.Contains(msg, "CacheSubnetGroupInUse"): - return "CacheSubnetGroupInUse" - case strings.Contains(msg, "SnapshotFeatureNotSupportedFault"): - return "SnapshotFeatureNotSupportedFault" - case strings.Contains(msg, "InvalidCacheParameterGroupState"): - return "InvalidCacheParameterGroupState" - default: - return "InvalidCacheClusterState" - } -} - -// notFoundCode picks the AWS-shaped error code for the resource the message -// names. A caller matching the SDK's typed errors sees the code, not the -// message, so a generic one leaves it unable to tell a missing replication -// group from a missing cache cluster. -// -// Order matters: "cache subnet group" also contains "cache", so the more -// specific resources are checked first. -func notFoundCode(err error) string { - msg := err.Error() - - switch { - case strings.Contains(msg, "snapshot"): - return "SnapshotNotFoundFault" - case strings.Contains(msg, "replication group"): - return "ReplicationGroupNotFoundFault" - case strings.Contains(msg, "cache subnet group"): - return "CacheSubnetGroupNotFoundFault" - case strings.Contains(msg, "parameter group"): - return "CacheParameterGroupNotFound" - default: - return "CacheClusterNotFound" - } -} - -// alreadyExistsCode is the create-side counterpart of notFoundCode. Callers -// treat the specific already-exists code as "already provisioned, carry on", -// so collapsing it turns an idempotent re-run into a hard failure. -func alreadyExistsCode(err error) string { - msg := err.Error() - - switch { - case strings.Contains(msg, "snapshot"): - return "SnapshotAlreadyExistsFault" - case strings.Contains(msg, "replication group"): - return "ReplicationGroupAlreadyExists" - case strings.Contains(msg, "cache subnet group"): - return "CacheSubnetGroupAlreadyExists" - default: - return "CacheClusterAlreadyExists" - } -} - // IAMService returns the IAM service prefix of the operations this handler // serves. func (*Handler) IAMService() string { return "elasticache" } From 8d3d70cb69120ad35c486d4bf0895aaf4413e131 Mon Sep 17 00:00:00 2001 From: Satyam-Trivedi-ZS Date: Fri, 23 Oct 2026 09:49:39 +0530 Subject: [PATCH 20/31] fix(aws-kinesis): live SubscribeToShard push, throttling metrics, input limits, stream lifecycle states (#1650) * fix(aws-kinesis): live SubscribeToShard push, throttling metrics, input limits, stream lifecycle states - SubscribeToShard keeps the event stream open and pushes a new SubscribeToShardEvent whenever records land, for up to 5 minutes, until the client disconnects, or until the shard ends (final event carries ChildShards and no ContinuationSequenceNumber). The loop runs on the request goroutine; lifetime and poll interval are injectable for tests. - ContinuationSequenceNumber now captures the read position even when no records were delivered, and the shard's starting sequence number is accepted as AT/AFTER_SEQUENCE_NUMBER, so resuming does not replay. - Publish WriteProvisionedThroughputExceeded (PutRecord/PutRecords), ReadProvisionedThroughputExceeded (GetRecords) and PutRecords.ThrottledRecords per operation with value 0: the emulator enforces no shard throughput limits, so nothing is ever throttled. - PartitionKey longer than 256 characters is rejected with ValidationException on PutRecord and on PutRecords (whole request). - GetRecords Limit outside 1..10000 returns InvalidArgumentException instead of being clamped. - With --async-settle, CreateStream reports CREATING and DeleteStream reports DELETING for a settle window; only ACTIVE streams can be deleted. Default behaviour is unchanged. Part of #1513 * fix(aws-kinesis): race-free stream reaping, ACTIVE gating, snapshot ARN filter - removeStream now does compare-and-delete through memstore UpdateOrDelete for both the stream entry and its ARN index entry, so a reader purging an expired DELETING stream can no longer remove a stream that a concurrent CreateStream just installed under the same name (only reachable with --async-settle). - UpdateShardCount, Increase/DecreaseStreamRetentionPeriod, UpdateStreamMode, Start/StopStreamEncryption, SplitShard and MergeShards return ResourceInUseException unless the stream is ACTIVE, sharing DeleteStream's check. Without --async-settle every stream is ACTIVE, so nothing changes. - Snapshot serializes only the ARN index entries of streams it captures, so a DELETING stream left out of the snapshot no longer leaves a dangling entry. - Fold the PutRecords count and PartitionKey checks into validatePutRecordsRequest, bringing PutRecords back under the gocyclo limit. - Export driver.MaxGetRecordsLimit and use it in both the provider and the wire handler's Limit 0 message, so the bound lives in one place. * fix(aws-kinesis): SubscribeToShard idle heartbeats and prompt end on shutdown - An idle subscription now pushes an empty SubscribeToShardEvent (unchanged continuation) every 5s, as real enhanced fan-out does, so KCL 2.x no longer cancels and resubscribes on a quiet shard. - cloudemu serve stamps a shutdown signal (wire.ShutdownSignal) on every request's base context and closes it when http.Server.Shutdown begins. Open subscriptions watch it and end promptly instead of holding a graceful shutdown open until its timeout. Request contexts are not canceled, so ordinary in-flight requests are unaffected. - pushSubscription's wait and idle checks move into small helpers to keep it under the gocyclo limit. --- docs/services.md | 5 +- providers/aws/kinesis/consumers.go | 33 +- providers/aws/kinesis/errors.go | 6 + providers/aws/kinesis/kinesis.go | 10 +- providers/aws/kinesis/metrics.go | 15 + providers/aws/kinesis/metrics_test.go | 28 + providers/aws/kinesis/parity_test.go | 541 ++++++++++++++++++ providers/aws/kinesis/policy.go | 2 +- providers/aws/kinesis/records.go | 107 +++- providers/aws/kinesis/resharding.go | 8 + providers/aws/kinesis/snapshot.go | 26 +- providers/aws/kinesis/streams.go | 164 +++++- server/aws/kinesis/export_test.go | 16 + server/aws/kinesis/handler.go | 17 +- server/aws/kinesis/records_ops.go | 23 +- .../kinesis/sdk_roundtrip_streaming_test.go | 6 + .../kinesis/sdk_roundtrip_validation_test.go | 80 +++ server/aws/kinesis/sdk_subscribe_live_test.go | 351 ++++++++++++ server/aws/kinesis/streaming_ops.go | 198 ++++++- server/serverkit/serverkit.go | 18 +- server/serverkit/shutdown_signal_test.go | 70 +++ server/wire/shutdown.go | 49 ++ server/wire/shutdown_test.go | 32 ++ services/kinesis/driver/driver.go | 10 +- services/kinesis/driver/errors.go | 4 + 25 files changed, 1750 insertions(+), 69 deletions(-) create mode 100644 providers/aws/kinesis/parity_test.go create mode 100644 server/aws/kinesis/export_test.go create mode 100644 server/aws/kinesis/sdk_roundtrip_validation_test.go create mode 100644 server/aws/kinesis/sdk_subscribe_live_test.go create mode 100644 server/serverkit/shutdown_signal_test.go create mode 100644 server/wire/shutdown.go create mode 100644 server/wire/shutdown_test.go diff --git a/docs/services.md b/docs/services.md index a52a5315b..7d57f535e 100644 --- a/docs/services.md +++ b/docs/services.md @@ -3364,7 +3364,10 @@ streams records to a registered consumer as an `application/vnd.amazon.eventstream` response: it resolves the consumer's shard from the requested `StartingPosition`, emits the `initial-response` frame the SDK awaits, then a `SubscribeToShardEvent` frame carrying the records, continuation -sequence number, and `MillisBehindLatest`. Polling via +sequence number, and `MillisBehindLatest`. The connection then stays open and +pushes a new event whenever records land on the shard, for up to 5 minutes (as +real Kinesis does), until the client disconnects, or until the shard is closed +(the final event carries `ChildShards` and no continuation). Polling via `GetShardIterator`/`GetRecords` covers the same read path. **Total: 39 operations.** diff --git a/providers/aws/kinesis/consumers.go b/providers/aws/kinesis/consumers.go index db049af10..ade2d8664 100644 --- a/providers/aws/kinesis/consumers.go +++ b/providers/aws/kinesis/consumers.go @@ -172,21 +172,38 @@ func (m *Mock) SubscribeToShard( recs := make([]driver.Record, len(shard.records)-start) copy(recs, shard.records[start:]) - return &driver.SubscribeToShardResult{ + res := &driver.SubscribeToShardResult{ Records: recs, - ContinuationSequenceNumber: continuationSeq(shard, recs), + ContinuationSequenceNumber: continuationSeq(shard, start, recs), MillisBehindLatest: 0, - }, nil + } + + // A closed shard that has been read to its end signals shard end the way + // real Kinesis does: no continuation sequence number, plus the child shards + // to subscribe to next (KCL treats exactly this combination as shard end). + if shard.closed { + res.ContinuationSequenceNumber = "" + res.ChildShards = childShardsOf(sd.shards, shard.shard.ShardID) + } + + return res, nil } -// continuationSeq is the cursor a client uses to resume a subscription: the last -// delivered record's sequence number, or the shard's starting sequence number -// when no records were delivered. -func continuationSeq(shard *shardState, recs []driver.Record) string { +// continuationSeq is the cursor a client passes back as AFTER_SEQUENCE_NUMBER to +// resume a subscription. Real Kinesis notes it "captures your shard progress +// even when no data is written to the shard", so with nothing delivered it is +// the record just before the read position (not the shard start, which would +// replay a LATEST/AT_TIMESTAMP subscription from the beginning), or the shard's +// starting sequence number when the position is the very start of the shard. +func continuationSeq(shard *shardState, start int, recs []driver.Record) string { if n := len(recs); n > 0 { return recs[n-1].SequenceNumber } + if start > 0 { + return shard.records[start-1].SequenceNumber + } + return shard.shard.SequenceNumberRange.StartingSequenceNumber } @@ -206,7 +223,7 @@ func (m *Mock) findConsumerStream(streamARN, consumerName, consumerARN string) ( // Fall back to scanning by consumer ARN. if consumerARN != "" { - for _, sd := range m.streams.All() { + for _, sd := range m.liveStreams() { sd.mu.RLock() for name, c := range sd.consumers { if c.ConsumerARN == consumerARN { diff --git a/providers/aws/kinesis/errors.go b/providers/aws/kinesis/errors.go index 9331e342f..7320cc3ac 100644 --- a/providers/aws/kinesis/errors.go +++ b/providers/aws/kinesis/errors.go @@ -26,3 +26,9 @@ func invalidArg(format string, args ...any) error { func expiredIterator(format string, args ...any) error { return &driver.APIError{Exception: driver.ExExpiredIterator, Err: errors.Newf(errors.InvalidArgument, format, args...)} } + +// validationErr builds a ValidationException-tagged error: the request-shape +// (model constraint) failure real Kinesis reports before the operation runs. +func validationErr(format string, args ...any) error { + return &driver.APIError{Exception: driver.ExValidation, Err: errors.Newf(errors.InvalidArgument, format, args...)} +} diff --git a/providers/aws/kinesis/kinesis.go b/providers/aws/kinesis/kinesis.go index ca757fdcf..5adf530d7 100644 --- a/providers/aws/kinesis/kinesis.go +++ b/providers/aws/kinesis/kinesis.go @@ -18,6 +18,7 @@ import ( "github.com/stackshy/cloudemu/v2/internal/memstore" "github.com/stackshy/cloudemu/v2/internal/recursionguard" "github.com/stackshy/cloudemu/v2/internal/regionctx" + "github.com/stackshy/cloudemu/v2/internal/settle" "github.com/stackshy/cloudemu/v2/services/kinesis/driver" mondriver "github.com/stackshy/cloudemu/v2/services/monitoring/driver" ) @@ -66,6 +67,13 @@ type streamData struct { maxRecKiB int32 warmMiBps int32 seq int64 // per-stream monotonic sequence counter + // createWin overlays CREATING on the stored ACTIVE status for the settle + // window after CreateStream; deleteWin (with deleting set) reports DELETING + // until the stream is purged. Both are inactive unless config.AsyncSettle + // is on, and are transient (not snapshotted), like every settle window. + createWin settle.Window + deleteWin settle.Window + deleting bool mu sync.RWMutex } @@ -189,7 +197,7 @@ func (m *Mock) resolve(name, arn string) (*streamData, error) { name = n } - sd, ok := m.streams.Get(name) + sd, ok := m.lookup(name) if !ok { return nil, errNotFoundName(name) } diff --git a/providers/aws/kinesis/metrics.go b/providers/aws/kinesis/metrics.go index 09173a643..fa8b1214c 100644 --- a/providers/aws/kinesis/metrics.go +++ b/providers/aws/kinesis/metrics.go @@ -16,6 +16,17 @@ const ( unitBytes = "Bytes" unitCount = "Count" unitMilliseconds = "Milliseconds" + + // Throttling metrics. The emulator enforces no shard throughput limits + // (it never returns ProvisionedThroughputExceededException), so the + // throttled count of every put/get is truly 0. Like IncomingRecords and the + // other per-operation stream metrics, they get one sample per PutRecord, + // PutRecords or GetRecords call, carrying that call's throttled count, so + // Average/Maximum/Minimum read as the Kinesis metrics page describes ("When + // the Maximum statistic has a value of 0 (zero), no records were throttled") + // and nothing is published for an idle stream. + metricWriteThrottled = "WriteProvisionedThroughputExceeded" + metricReadThrottled = "ReadProvisionedThroughputExceeded" ) // SetMonitoring wires the CloudWatch backend that receives the stream-level @@ -59,6 +70,7 @@ func (m *Mock) emitPutRecordMetrics(ctx context.Context, stream string, size int {MetricName: "PutRecord.Bytes", Value: float64(size), Unit: unitBytes}, {MetricName: "PutRecord.Success", Value: 1, Unit: unitCount}, {MetricName: "PutRecord.Latency", Value: m.sinceMillis(start), Unit: unitMilliseconds}, + {MetricName: metricWriteThrottled, Value: 0, Unit: unitCount}, }) } @@ -80,7 +92,9 @@ func (m *Mock) emitPutRecordsMetrics(ctx context.Context, stream string, o putRe {MetricName: "PutRecords.TotalRecords", Value: float64(o.total), Unit: unitCount}, {MetricName: "PutRecords.SuccessfulRecords", Value: float64(succeeded), Unit: unitCount}, {MetricName: "PutRecords.FailedRecords", Value: float64(o.failed), Unit: unitCount}, + {MetricName: "PutRecords.ThrottledRecords", Value: 0, Unit: unitCount}, {MetricName: "PutRecords.Latency", Value: m.sinceMillis(start), Unit: unitMilliseconds}, + {MetricName: metricWriteThrottled, Value: 0, Unit: unitCount}, }) } @@ -104,5 +118,6 @@ func (m *Mock) emitGetRecordsMetrics(ctx context.Context, stream string, recs [] {MetricName: "GetRecords.Success", Value: 1, Unit: unitCount}, {MetricName: "GetRecords.IteratorAgeMilliseconds", Value: age, Unit: unitMilliseconds}, {MetricName: "GetRecords.Latency", Value: m.sinceMillis(start), Unit: unitMilliseconds}, + {MetricName: metricReadThrottled, Value: 0, Unit: unitCount}, }) } diff --git a/providers/aws/kinesis/metrics_test.go b/providers/aws/kinesis/metrics_test.go index 4521598df..956522fe8 100644 --- a/providers/aws/kinesis/metrics_test.go +++ b/providers/aws/kinesis/metrics_test.go @@ -73,6 +73,11 @@ func TestStreamLevelMetrics(t *testing.T) { {"GetRecords.Bytes", 9, "Bytes"}, {"GetRecords.Success", 1, "Count"}, {"GetRecords.IteratorAgeMilliseconds", 0, "Milliseconds"}, + // Never throttled: a 0-valued datapoint is still published so + // Average/Maximum alarms on these always-available metrics resolve. + {"WriteProvisionedThroughputExceeded", 0, "Count"}, + {"ReadProvisionedThroughputExceeded", 0, "Count"}, + {"PutRecords.ThrottledRecords", 0, "Count"}, } for _, w := range want { @@ -90,4 +95,27 @@ func TestStreamLevelMetrics(t *testing.T) { t.Errorf("%s = %v unit %q, want [%v] unit %q", w.name, res.Values, res.Unit, w.sum, w.unit) } } + + // Throttling metrics are per operation, like IncomingRecords: one sample + // per put call (PutRecord + PutRecords) and one per GetRecords call, not + // one per record. + for name, wantSamples := range map[string]float64{ + "WriteProvisionedThroughputExceeded": 2, + "ReadProvisionedThroughputExceeded": 1, + "IncomingRecords": 2, + } { + res, gerr := cw.GetMetricData(ctx, mondriver.GetMetricInput{ + Namespace: "AWS/Kinesis", MetricName: name, + Dimensions: map[string]string{"StreamName": "s"}, + StartTime: clk.Now().Add(-time.Minute), EndTime: clk.Now().Add(time.Minute), + Period: 60, Stat: "SampleCount", + }) + if gerr != nil { + t.Fatalf("GetMetricData %s SampleCount: %v", name, gerr) + } + + if len(res.Values) != 1 || res.Values[0] != wantSamples { + t.Errorf("%s SampleCount = %v, want [%v]", name, res.Values, wantSamples) + } + } } diff --git a/providers/aws/kinesis/parity_test.go b/providers/aws/kinesis/parity_test.go new file mode 100644 index 000000000..c627bc103 --- /dev/null +++ b/providers/aws/kinesis/parity_test.go @@ -0,0 +1,541 @@ +package kinesis_test + +import ( + "context" + "errors" + "strings" + "sync" + "testing" + "time" + + "github.com/stackshy/cloudemu/v2/config" + cerrors "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/providers/aws/kinesis" + "github.com/stackshy/cloudemu/v2/services/kinesis/driver" +) + +func wantException(t *testing.T, err error, exception string) { + t.Helper() + + var apiErr *driver.APIError + if !errors.As(err, &apiErr) || apiErr.Exception != exception { + t.Fatalf("err = %v, want %s", err, exception) + } +} + +func TestPutRecordRejectsPartitionKeyOver256Chars(t *testing.T) { + ctx := context.Background() + m := newMock(t) + + if err := m.CreateStream(ctx, driver.CreateStreamInput{StreamName: "s", ShardCount: 1}); err != nil { + t.Fatalf("CreateStream: %v", err) + } + + // 256 characters is the documented maximum and is accepted, counting + // Unicode characters (each "รฉ" is 2 bytes) rather than bytes. + for _, ok := range []string{strings.Repeat("k", 256), strings.Repeat("รฉ", 256)} { + if _, err := m.PutRecord(ctx, driver.PutRecordInput{StreamName: "s", PartitionKey: ok, Data: []byte("d")}); err != nil { + t.Fatalf("PutRecord with %d-char key: %v", len([]rune(ok)), err) + } + } + + _, err := m.PutRecord(ctx, driver.PutRecordInput{ + StreamName: "s", PartitionKey: strings.Repeat("k", 257), Data: []byte("d"), + }) + wantException(t, err, driver.ExValidation) + + if !strings.Contains(err.Error(), "at 'partitionKey' failed to satisfy constraint: "+ + "Member must have length less than or equal to 256") { + t.Fatalf("message = %q", err.Error()) + } +} + +func TestPutRecordsRejectsPartitionKeyOver256Chars(t *testing.T) { + ctx := context.Background() + m := newMock(t) + + shardID := seedShard(t, ctx, m, 0) + + _, _, err := m.PutRecords(ctx, "s", "", []driver.PutRecordsRequestEntry{ + {PartitionKey: "ok", Data: []byte("a")}, + {PartitionKey: strings.Repeat("k", 257), Data: []byte("b")}, + }) + wantException(t, err, driver.ExValidation) + + if !strings.Contains(err.Error(), "at 'records.2.member.partitionKey'") { + t.Fatalf("message = %q, want the offending entry's member path", err.Error()) + } + + // The whole request is rejected: not even the valid entry is written. + out, err := m.GetRecords(ctx, trimHorizonIterator(t, ctx, m, shardID), 0) + if err != nil { + t.Fatalf("GetRecords: %v", err) + } + + if len(out.Records) != 0 { + t.Fatalf("records written = %d, want 0", len(out.Records)) + } +} + +func TestGetRecordsRejectsOutOfRangeLimit(t *testing.T) { + ctx := context.Background() + m := newMock(t) + + shardID := seedShard(t, ctx, m, 3) + it := trimHorizonIterator(t, ctx, m, shardID) + + for _, bad := range []int32{-1, 10001} { + _, err := m.GetRecords(ctx, it, bad) + wantException(t, err, driver.ExInvalidArgument) + } + + // The bounds themselves are valid; 0 is the Go zero value for "omitted". + for _, ok := range []int32{0, 1, 10000} { + if _, err := m.GetRecords(ctx, it, ok); err != nil { + t.Fatalf("GetRecords(limit=%d): %v", ok, err) + } + } +} + +func newSettleMock(t *testing.T) (*kinesis.Mock, *config.FakeClock) { + t.Helper() + + clk := config.NewFakeClock(time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)) + + return kinesis.New(config.NewOptions(config.WithClock(clk), config.WithAsyncSettle())), clk +} + +func streamStatus(t *testing.T, m *kinesis.Mock, name string) string { + t.Helper() + + sum, err := m.DescribeStreamSummary(context.Background(), name, "") + if err != nil { + t.Fatalf("DescribeStreamSummary: %v", err) + } + + desc, err := m.DescribeStream(context.Background(), name, "", 0, "") + if err != nil { + t.Fatalf("DescribeStream: %v", err) + } + + if desc.StreamStatus != sum.StreamStatus { + t.Fatalf("DescribeStream status %s != DescribeStreamSummary status %s", desc.StreamStatus, sum.StreamStatus) + } + + return sum.StreamStatus +} + +func TestCreateStreamReportsCreatingUnderAsyncSettle(t *testing.T) { + ctx := context.Background() + m, clk := newSettleMock(t) + + if err := m.CreateStream(ctx, driver.CreateStreamInput{StreamName: "s", ShardCount: 1}); err != nil { + t.Fatalf("CreateStream: %v", err) + } + + if got := streamStatus(t, m, "s"); got != driver.StatusCreating { + t.Fatalf("status right after create = %s, want CREATING", got) + } + + list, err := m.ListStreams(ctx, "", "", 0) + if err != nil || len(list.StreamSummaries) != 1 || list.StreamSummaries[0].StreamStatus != driver.StatusCreating { + t.Fatalf("ListStreams = %+v, %v; want one CREATING summary", list, err) + } + + // Only an ACTIVE stream can be deleted. + err = m.DeleteStream(ctx, "s", "", false) + if !cerrors.IsFailedPrecondition(err) { + t.Fatalf("DeleteStream while CREATING = %v, want ResourceInUse", err) + } + + clk.Advance(2 * time.Second) + + if got := streamStatus(t, m, "s"); got != driver.StatusActive { + t.Fatalf("status after settle = %s, want ACTIVE", got) + } +} + +func TestDeleteStreamReportsDeletingUnderAsyncSettle(t *testing.T) { + ctx := context.Background() + m, clk := newSettleMock(t) + + if err := m.CreateStream(ctx, driver.CreateStreamInput{StreamName: "s", ShardCount: 1}); err != nil { + t.Fatalf("CreateStream: %v", err) + } + + clk.Advance(2 * time.Second) + + if err := m.DeleteStream(ctx, "s", "", false); err != nil { + t.Fatalf("DeleteStream: %v", err) + } + + if got := streamStatus(t, m, "s"); got != driver.StatusDeleting { + t.Fatalf("status right after delete = %s, want DELETING", got) + } + + // Kinesis may keep serving data operations on a DELETING stream. + if _, err := m.PutRecord(ctx, driver.PutRecordInput{StreamName: "s", PartitionKey: "k", Data: []byte("d")}); err != nil { + t.Fatalf("PutRecord while DELETING: %v", err) + } + + if err := m.DeleteStream(ctx, "s", "", false); !cerrors.IsFailedPrecondition(err) { + t.Fatalf("second DeleteStream = %v, want ResourceInUse", err) + } + + if err := m.CreateStream(ctx, driver.CreateStreamInput{StreamName: "s", ShardCount: 1}); !cerrors.IsFailedPrecondition(err) { + t.Fatalf("re-create while DELETING = %v, want ResourceInUse", err) + } + + // A snapshot taken mid-deletion must not resurrect the stream. + raw, err := m.Snapshot(ctx, false) + if err != nil { + t.Fatalf("Snapshot: %v", err) + } + + if strings.Contains(string(raw), `"StreamName":"s"`) { + t.Fatalf("snapshot still carries the DELETING stream: %s", raw) + } + + clk.Advance(2 * time.Second) + + if _, err := m.DescribeStreamSummary(ctx, "s", ""); !cerrors.IsNotFound(err) { + t.Fatalf("DescribeStreamSummary after delete settles = %v, want ResourceNotFound", err) + } + + list, err := m.ListStreams(ctx, "", "", 0) + if err != nil || len(list.StreamNames) != 0 { + t.Fatalf("ListStreams after delete = %+v, %v; want empty", list, err) + } + + // The name is free again once deletion has completed. + if err := m.CreateStream(ctx, driver.CreateStreamInput{StreamName: "s", ShardCount: 1}); err != nil { + t.Fatalf("re-create after delete: %v", err) + } +} + +func TestStreamLifecycleImmediateWithoutAsyncSettle(t *testing.T) { + ctx := context.Background() + m, _ := newMockClock(t) + + if err := m.CreateStream(ctx, driver.CreateStreamInput{StreamName: "s", ShardCount: 1}); err != nil { + t.Fatalf("CreateStream: %v", err) + } + + if got := streamStatus(t, m, "s"); got != driver.StatusActive { + t.Fatalf("status = %s, want ACTIVE immediately (AsyncSettle off)", got) + } + + if err := m.DeleteStream(ctx, "s", "", false); err != nil { + t.Fatalf("DeleteStream: %v", err) + } + + if _, err := m.DescribeStreamSummary(ctx, "s", ""); !cerrors.IsNotFound(err) { + t.Fatalf("DescribeStreamSummary after delete = %v, want ResourceNotFound immediately", err) + } +} + +// subscribeFixture registers a consumer on stream "s" and returns its ARN. +func subscribeFixture(t *testing.T, ctx context.Context, m *kinesis.Mock) string { + t.Helper() + + sum, err := m.DescribeStreamSummary(ctx, "s", "") + if err != nil { + t.Fatalf("DescribeStreamSummary: %v", err) + } + + c, err := m.RegisterStreamConsumer(ctx, sum.StreamARN, "efo") + if err != nil { + t.Fatalf("RegisterStreamConsumer: %v", err) + } + + return c.ConsumerARN +} + +// TestSubscribeToShardContinuationResumesWithoutReplay: the continuation of an +// event that delivered nothing must still capture the read position, so a +// LATEST subscription resumed with AFTER_SEQUENCE_NUMBER sees only new records. +func TestSubscribeToShardContinuationResumesWithoutReplay(t *testing.T) { + ctx := context.Background() + m := newMock(t) + + shardID := seedShard(t, ctx, m, 2) + consumer := subscribeFixture(t, ctx, m) + + first, err := m.SubscribeToShard(ctx, driver.SubscribeToShardInput{ + ConsumerARN: consumer, ShardID: shardID, StartingPositionType: driver.IteratorLatest, + }) + if err != nil { + t.Fatalf("SubscribeToShard LATEST: %v", err) + } + + if len(first.Records) != 0 || first.ContinuationSequenceNumber == "" { + t.Fatalf("LATEST event = %+v, want no records and a continuation", first) + } + + if _, err := m.PutRecord(ctx, driver.PutRecordInput{StreamName: "s", PartitionKey: "k", Data: []byte("new")}); err != nil { + t.Fatalf("PutRecord: %v", err) + } + + next, err := m.SubscribeToShard(ctx, driver.SubscribeToShardInput{ + ConsumerARN: consumer, ShardID: shardID, + StartingPositionType: driver.IteratorAfterSequenceNumber, + StartingSequenceNumber: first.ContinuationSequenceNumber, + }) + if err != nil { + t.Fatalf("SubscribeToShard AFTER continuation: %v", err) + } + + if len(next.Records) != 1 || string(next.Records[0].Data) != "new" { + t.Fatalf("resumed records = %+v, want only the new record", next.Records) + } +} + +// TestSubscribeToShardContinuationOnEmptyShard: on a shard with no records the +// continuation is the shard's starting sequence number, which must be accepted +// as AFTER_SEQUENCE_NUMBER (it precedes every record). +func TestSubscribeToShardContinuationOnEmptyShard(t *testing.T) { + ctx := context.Background() + m := newMock(t) + + shardID := seedShard(t, ctx, m, 0) + consumer := subscribeFixture(t, ctx, m) + + first, err := m.SubscribeToShard(ctx, driver.SubscribeToShardInput{ + ConsumerARN: consumer, ShardID: shardID, StartingPositionType: driver.IteratorTrimHorizon, + }) + if err != nil { + t.Fatalf("SubscribeToShard: %v", err) + } + + if _, err := m.PutRecord(ctx, driver.PutRecordInput{StreamName: "s", PartitionKey: "k", Data: []byte("x")}); err != nil { + t.Fatalf("PutRecord: %v", err) + } + + next, err := m.SubscribeToShard(ctx, driver.SubscribeToShardInput{ + ConsumerARN: consumer, ShardID: shardID, + StartingPositionType: driver.IteratorAfterSequenceNumber, + StartingSequenceNumber: first.ContinuationSequenceNumber, + }) + if err != nil { + t.Fatalf("SubscribeToShard AFTER shard start: %v", err) + } + + if len(next.Records) != 1 { + t.Fatalf("records = %d, want 1", len(next.Records)) + } +} + +// TestSubscribeToShardSignalsShardEnd: reading a closed shard to its end +// returns ChildShards and no continuation, the shard-end signal. +func TestSubscribeToShardSignalsShardEnd(t *testing.T) { + ctx := context.Background() + m := newMock(t) + + shardID := seedShard(t, ctx, m, 1) + consumer := subscribeFixture(t, ctx, m) + + if _, _, err := m.UpdateShardCount(ctx, "s", "", 2, "UNIFORM_SCALING"); err != nil { + t.Fatalf("UpdateShardCount: %v", err) + } + + res, err := m.SubscribeToShard(ctx, driver.SubscribeToShardInput{ + ConsumerARN: consumer, ShardID: shardID, StartingPositionType: driver.IteratorTrimHorizon, + }) + if err != nil { + t.Fatalf("SubscribeToShard: %v", err) + } + + if len(res.Records) != 1 || res.ContinuationSequenceNumber != "" || len(res.ChildShards) != 2 { + t.Fatalf("closed-shard event = %+v, want 1 record, empty continuation, 2 child shards", res) + } +} + +// TestRecreateDuringConcurrentReapKeepsNewStream races readers that reap an +// expired DELETING stream against a CreateStream that reaps the same entry and +// installs a new stream under the name. The reader's purge must never remove +// the new stream (or its ARN index entry) once CreateStream has returned nil. +func TestRecreateDuringConcurrentReapKeepsNewStream(t *testing.T) { + ctx := context.Background() + m, clk := newSettleMock(t) + + const ( + rounds = 10000 + readers = 4 + ) + + for round := range rounds { + if err := m.CreateStream(ctx, driver.CreateStreamInput{StreamName: "s", ShardCount: 1}); err != nil { + t.Fatalf("round %d: CreateStream: %v", round, err) + } + + clk.Advance(3 * time.Second) + + if err := m.DeleteStream(ctx, "s", "", false); err != nil { + t.Fatalf("round %d: DeleteStream: %v", round, err) + } + + // The deletion has settled but nothing has purged the entry yet. + clk.Advance(3 * time.Second) + + var ( + wg sync.WaitGroup + createErr error + start = make(chan struct{}) + ) + + for range readers { + wg.Go(func() { + <-start + + _, _ = m.DescribeStreamSummary(ctx, "s", "") + }) + } + + wg.Go(func() { + <-start + + createErr = m.CreateStream(ctx, driver.CreateStreamInput{StreamName: "s", ShardCount: 1}) + }) + + close(start) + wg.Wait() + + if createErr != nil { + t.Fatalf("round %d: re-create: %v", round, createErr) + } + + sum, err := m.DescribeStreamSummary(ctx, "s", "") + if err != nil { + t.Fatalf("round %d: re-created stream lost by name: %v", round, err) + } + + if _, err := m.DescribeStreamSummary(ctx, "", sum.StreamARN); err != nil { + t.Fatalf("round %d: re-created stream lost by ARN: %v", round, err) + } + + // Let the re-created stream settle to ACTIVE, then clear it for the next round. + clk.Advance(3 * time.Second) + + if err := m.DeleteStream(ctx, "s", "", false); err != nil { + t.Fatalf("round %d: cleanup DeleteStream: %v", round, err) + } + + clk.Advance(3 * time.Second) + + if _, err := m.DescribeStreamSummary(ctx, "s", ""); !cerrors.IsNotFound(err) { + t.Fatalf("round %d: cleanup: stream still present: %v", round, err) + } + } +} + +// TestControlPlaneOpsRequireActiveUnderAsyncSettle checks that the stream-level +// control-plane operations whose API reference lists ResourceInUseException +// ("the resource must be in the ACTIVE state") reject a CREATING or DELETING +// stream, and succeed once it is ACTIVE. +func TestControlPlaneOpsRequireActiveUnderAsyncSettle(t *testing.T) { + ctx := context.Background() + + ops := map[string]func(m *kinesis.Mock, arn string) error{ + "UpdateShardCount": func(m *kinesis.Mock, _ string) error { + _, _, err := m.UpdateShardCount(ctx, "s", "", 2, "UNIFORM_SCALING") + return err + }, + "IncreaseStreamRetentionPeriod": func(m *kinesis.Mock, _ string) error { + return m.IncreaseStreamRetentionPeriod(ctx, "s", "", 48) + }, + "DecreaseStreamRetentionPeriod": func(m *kinesis.Mock, _ string) error { + return m.DecreaseStreamRetentionPeriod(ctx, "s", "", 24) + }, + "UpdateStreamMode": func(m *kinesis.Mock, arn string) error { + return m.UpdateStreamMode(ctx, arn, driver.ModeProvisioned) + }, + "StartStreamEncryption": func(m *kinesis.Mock, _ string) error { + return m.StartStreamEncryption(ctx, "s", "", driver.EncryptionKMS, "alias/aws/kinesis") + }, + "StopStreamEncryption": func(m *kinesis.Mock, _ string) error { + return m.StopStreamEncryption(ctx, "s", "", driver.EncryptionKMS, "alias/aws/kinesis") + }, + "SplitShard": func(m *kinesis.Mock, _ string) error { + return m.SplitShard(ctx, "s", "", "shardId-000000000000", "1000") + }, + "MergeShards": func(m *kinesis.Mock, _ string) error { + return m.MergeShards(ctx, "s", "", "shardId-000000000000", "shardId-000000000001") + }, + } + + for name, op := range ops { + t.Run(name, func(t *testing.T) { + m, clk := newSettleMock(t) + + if err := m.CreateStream(ctx, driver.CreateStreamInput{StreamName: "s", ShardCount: 2}); err != nil { + t.Fatalf("CreateStream: %v", err) + } + + sum, err := m.DescribeStreamSummary(ctx, "s", "") + if err != nil { + t.Fatalf("DescribeStreamSummary: %v", err) + } + + if err := op(m, sum.StreamARN); !cerrors.IsFailedPrecondition(err) { + t.Fatalf("%s while CREATING = %v, want ResourceInUse", name, err) + } + + clk.Advance(2 * time.Second) + + if err := op(m, sum.StreamARN); err != nil { + t.Fatalf("%s while ACTIVE: %v", name, err) + } + + if err := m.DeleteStream(ctx, "s", "", false); err != nil { + t.Fatalf("DeleteStream: %v", err) + } + + if err := op(m, sum.StreamARN); !cerrors.IsFailedPrecondition(err) { + t.Fatalf("%s while DELETING = %v, want ResourceInUse", name, err) + } + }) + } +} + +// TestSnapshotOmitsArnIndexOfDeletingStream checks that a stream left out of a +// snapshot because it is DELETING takes its ARN index entry with it, while a +// live stream's entry still round-trips and resolves after a restore. +func TestSnapshotOmitsArnIndexOfDeletingStream(t *testing.T) { + ctx := context.Background() + m, clk := newSettleMock(t) + + for _, name := range []string{"gone", "kept"} { + if err := m.CreateStream(ctx, driver.CreateStreamInput{StreamName: name, ShardCount: 1}); err != nil { + t.Fatalf("CreateStream %s: %v", name, err) + } + } + + clk.Advance(2 * time.Second) + + if err := m.DeleteStream(ctx, "gone", "", false); err != nil { + t.Fatalf("DeleteStream: %v", err) + } + + raw, err := m.Snapshot(ctx, false) + if err != nil { + t.Fatalf("Snapshot: %v", err) + } + + if strings.Contains(string(raw), "stream/gone") { + t.Fatalf("snapshot still carries the DELETING stream's ARN index entry: %s", raw) + } + + restored := kinesis.New(config.NewOptions()) + if err := restored.Restore(ctx, raw); err != nil { + t.Fatalf("Restore: %v", err) + } + + sum, err := m.DescribeStreamSummary(ctx, "kept", "") + if err != nil { + t.Fatalf("DescribeStreamSummary: %v", err) + } + + if _, err := restored.DescribeStreamSummary(ctx, "", sum.StreamARN); err != nil { + t.Fatalf("restored stream not resolvable by ARN: %v", err) + } +} diff --git a/providers/aws/kinesis/policy.go b/providers/aws/kinesis/policy.go index 456d89611..49fb70613 100644 --- a/providers/aws/kinesis/policy.go +++ b/providers/aws/kinesis/policy.go @@ -66,7 +66,7 @@ func (m *Mock) DeleteResourcePolicy(_ context.Context, resourceARN string) error func (m *Mock) DescribeLimits(_ context.Context) (*driver.Limits, error) { var open, onDemand int32 - for _, sd := range m.streams.All() { + for _, sd := range m.liveStreams() { sd.mu.RLock() open += openShardCount(sd.shards) diff --git a/providers/aws/kinesis/records.go b/providers/aws/kinesis/records.go index 7538e103c..ea91336bd 100644 --- a/providers/aws/kinesis/records.go +++ b/providers/aws/kinesis/records.go @@ -4,14 +4,23 @@ import ( "context" "encoding/base64" "encoding/json" + "fmt" "math/big" + "strings" "time" + "unicode/utf8" "github.com/stackshy/cloudemu/v2/services/kinesis/driver" ) const ( - defaultGetRecordsLimit = 10000 + // defaultGetRecordsLimit is both the GetRecords default and its maximum + // Limit: real Kinesis documents Limit's valid range as 1..10000 and throws + // InvalidArgumentException for a value above 10,000. + defaultGetRecordsLimit = driver.MaxGetRecordsLimit + // maxPartitionKeyLen is the PartitionKey length cap (Unicode characters) + // on PutRecord and every PutRecords entry. + maxPartitionKeyLen = 256 // maxRecordBytes is Kinesis's per-record data cap (1 MiB) when the stream has // no explicit MaxRecordSize configured. maxRecordBytes = 1 << 20 @@ -57,6 +66,64 @@ func validateBatchSize(sd *streamData, entries []driver.PutRecordsRequestEntry) return nil } +// partitionKeyTooLong reports whether a partition key exceeds the 256-character +// model constraint. Length is counted in Unicode characters, not bytes. +func partitionKeyTooLong(key string) bool { + return utf8.RuneCountInString(key) > maxPartitionKeyLen +} + +// partitionKeyLengthError renders the model-constraint failure real Kinesis +// returns for over-long partition keys, at the given member path(s). +func partitionKeyLengthError(violations []string) error { + noun := "error" + if len(violations) > 1 { + noun = "errors" + } + + return validationErr("%d validation %s detected: %s", len(violations), noun, strings.Join(violations, "; ")) +} + +func partitionKeyViolation(key, path string) string { + return fmt.Sprintf("Value '%s' at '%s' failed to satisfy constraint: "+ + "Member must have length less than or equal to %d", key, path, maxPartitionKeyLen) +} + +// validatePutRecordsRequest applies the request-level PutRecords checks that +// need no stream state: the 1..500 record count and the per-entry PartitionKey +// length. Any failure rejects the whole request before a record is written. +func validatePutRecordsRequest(entries []driver.PutRecordsRequestEntry) error { + if len(entries) == 0 { + return invalidArg("Records must contain at least one entry") + } + + if len(entries) > maxBatchRecords { + return invalidArg("a PutRecords request may contain at most %d records, got %d", + maxBatchRecords, len(entries)) + } + + return validateBatchPartitionKeys(entries) +} + +// validateBatchPartitionKeys rejects the whole PutRecords request when any +// entry's PartitionKey exceeds 256 characters, as request validation does in +// real Kinesis (no records are written). +func validateBatchPartitionKeys(entries []driver.PutRecordsRequestEntry) error { + var violations []string + + for i := range entries { + if partitionKeyTooLong(entries[i].PartitionKey) { + violations = append(violations, + partitionKeyViolation(entries[i].PartitionKey, fmt.Sprintf("records.%d.member.partitionKey", i+1))) + } + } + + if len(violations) > 0 { + return partitionKeyLengthError(violations) + } + + return nil +} + // iteratorToken is the opaque state a shard iterator carries. Records are never // trimmed in the emulator, so a stable slice index is a sufficient cursor. // CreatedAtMillis stamps when the iterator was minted (Unix milliseconds) so @@ -118,6 +185,10 @@ func (m *Mock) PutRecord(ctx context.Context, in driver.PutRecordInput) (*driver return nil, invalidArg("PartitionKey is required") } + if partitionKeyTooLong(in.PartitionKey) { + return nil, partitionKeyLengthError([]string{partitionKeyViolation(in.PartitionKey, "partitionKey")}) + } + sd, err := m.resolve(in.StreamName, in.StreamARN) if err != nil { return nil, err @@ -162,13 +233,8 @@ func (m *Mock) PutRecords( ) ([]driver.PutRecordsResultEntry, int32, error) { start := m.now() - if len(entries) == 0 { - return nil, 0, invalidArg("Records must contain at least one entry") - } - - if len(entries) > maxBatchRecords { - return nil, 0, invalidArg("a PutRecords request may contain at most %d records, got %d", - maxBatchRecords, len(entries)) + if err := validatePutRecordsRequest(entries); err != nil { + return nil, 0, err } sd, err := m.resolve(name, arn) @@ -335,10 +401,16 @@ func positionFor(shard *shardState, in *driver.GetShardIteratorInput) (int, erro } return indexByTimestamp(shard.records, in.Timestamp), nil - case driver.IteratorAtSequenceNumber: - return indexBySeq(shard.records, in.StartingSequenceNumber, false) - case driver.IteratorAfterSequenceNumber: - return indexBySeq(shard.records, in.StartingSequenceNumber, true) + case driver.IteratorAtSequenceNumber, driver.IteratorAfterSequenceNumber: + // The shard's own starting sequence number precedes every record; it is + // what a SubscribeToShard continuation carries before any data arrives. + if in.StartingSequenceNumber != "" && + in.StartingSequenceNumber == shard.shard.SequenceNumberRange.StartingSequenceNumber { + return 0, nil + } + + return indexBySeq(shard.records, in.StartingSequenceNumber, + in.ShardIteratorType == driver.IteratorAfterSequenceNumber) default: return 0, invalidArg("unsupported ShardIteratorType %q", in.ShardIteratorType) } @@ -370,9 +442,16 @@ func indexBySeq(records []driver.Record, seq string, after bool) (int, error) { // GetRecords returns records from the iterator position and advances it, then // publishes the stream-level GetRecords.* metrics for the successful read. +// A zero limit means Limit was omitted (the 10,000 default); a negative limit +// or one above 10,000 is rejected with InvalidArgumentException rather than +// silently clamped, as real Kinesis does. func (m *Mock) GetRecords(ctx context.Context, shardIterator string, limit int32) (*driver.GetRecordsOutput, error) { start := m.now() + if limit < 0 || limit > defaultGetRecordsLimit { + return nil, invalidArg("Limit %d is invalid: it must be between 1 and %d", limit, defaultGetRecordsLimit) + } + tok, err := decodeIterator(shardIterator) if err != nil { return nil, err @@ -397,7 +476,7 @@ func (m *Mock) readRecords(tok iteratorToken, limit int32) (*driver.GetRecordsOu createdAt.Format(time.RFC1123)) } - sd, ok := m.streams.Get(tok.StreamName) + sd, ok := m.lookup(tok.StreamName) if !ok { return nil, errNotFoundName(tok.StreamName) } @@ -410,7 +489,7 @@ func (m *Mock) readRecords(tok iteratorToken, limit int32) (*driver.GetRecordsOu return nil, expiredIterator("shard %q no longer exists", tok.ShardID) } - if limit <= 0 || limit > defaultGetRecordsLimit { + if limit == 0 { limit = defaultGetRecordsLimit } diff --git a/providers/aws/kinesis/resharding.go b/providers/aws/kinesis/resharding.go index fb9f99006..645b0cca3 100644 --- a/providers/aws/kinesis/resharding.go +++ b/providers/aws/kinesis/resharding.go @@ -30,6 +30,10 @@ func (m *Mock) SplitShard(_ context.Context, name, arn, shardToSplit, newStartin sd.mu.Lock() defer sd.mu.Unlock() + if err := m.requireActive(sd); err != nil { + return err + } + parent := findOpenShard(sd.shards, shardToSplit) if parent == nil { return errNotFound("open shard %q not found", shardToSplit) @@ -72,6 +76,10 @@ func (m *Mock) MergeShards(_ context.Context, name, arn, shardToMerge, adjacentS sd.mu.Lock() defer sd.mu.Unlock() + if err := m.requireActive(sd); err != nil { + return err + } + a := findOpenShard(sd.shards, shardToMerge) b := findOpenShard(sd.shards, adjacentShardToMerge) diff --git a/providers/aws/kinesis/snapshot.go b/providers/aws/kinesis/snapshot.go index a71e2871b..b53a6abf9 100644 --- a/providers/aws/kinesis/snapshot.go +++ b/providers/aws/kinesis/snapshot.go @@ -52,7 +52,14 @@ type shardStateSnapshot struct { func (m *Mock) Snapshot(_ context.Context, _ bool) (json.RawMessage, error) { snap := kinesisSnapshot{Streams: m.snapshotStreams()} - arns, err := m.arnToName.Snapshot() + // Serialize only the index entries of streams that are being captured, so a + // stream left out (one already DELETING) doesn't leave a dangling ARN entry + // pointing at a name the restored state doesn't have. The shape is the same + // map[arn]name memstore.Snapshot emits, so Restore's LoadSnapshot reads it. + arns, err := json.Marshal(m.arnToName.Filter(func(_, name string) bool { + _, kept := snap.Streams[name] + return kept + })) if err != nil { return nil, fmt.Errorf("kinesis: snapshot arn index: %w", err) } @@ -68,14 +75,25 @@ func (m *Mock) Snapshot(_ context.Context, _ bool) (json.RawMessage, error) { // snapshotStreams promotes each stored streamData to its exported form. func (m *Mock) snapshotStreams() map[string]*streamDataSnapshot { - if m.streams.Len() == 0 { + live := m.liveStreams() + if len(live) == 0 { return nil } - out := make(map[string]*streamDataSnapshot, m.streams.Len()) + out := make(map[string]*streamDataSnapshot, len(live)) - for name, sd := range m.streams.All() { + for name, sd := range live { sd.mu.RLock() + + // A stream already in DELETING is committed to removal: the transient + // settle window isn't serialized, so restoring it would resurrect the + // stream as ACTIVE forever. Leave it out instead. + if sd.deleting { + sd.mu.RUnlock() + + continue + } + out[name] = &streamDataSnapshot{ Desc: sd.desc, Shards: snapshotShards(sd.shards), Consumers: sd.consumers, Tags: sd.tags, Policy: sd.policy, MaxRecKiB: sd.maxRecKiB, diff --git a/providers/aws/kinesis/streams.go b/providers/aws/kinesis/streams.go index 650717c1d..fd3aeccba 100644 --- a/providers/aws/kinesis/streams.go +++ b/providers/aws/kinesis/streams.go @@ -7,6 +7,7 @@ import ( "sort" "time" + "github.com/stackshy/cloudemu/v2/internal/settle" "github.com/stackshy/cloudemu/v2/services/kinesis/driver" ) @@ -41,8 +42,107 @@ func (*Mock) buildShards(count int32, startIndex int, startSeq string, createdAt return shards } +// Stream lifecycle settle windows. Real Kinesis CreateStream returns while the +// stream is CREATING and DeleteStream leaves it DELETING until removal. With +// config.AsyncSettle off (the default) SettleDuration returns 0, so streams are +// ACTIVE immediately and deleted immediately, the historical behavior. +const ( + settleStreamCreate = 2 * time.Second // CREATING -> ACTIVE + settleStreamDelete = 2 * time.Second // DELETING -> gone +) + +// lookup returns the named stream, first purging it if its DELETING window has +// elapsed so a finished deletion reads as ResourceNotFoundException. +func (m *Mock) lookup(name string) (*streamData, bool) { + sd, ok := m.streams.Get(name) + if !ok { + return nil, false + } + + if m.reapIfDeleted(name, sd) { + return nil, false + } + + return sd, true +} + +// liveStreams returns every stream that still exists, purging those whose +// DELETING window has elapsed. +func (m *Mock) liveStreams() map[string]*streamData { + all := m.streams.All() + + for name, sd := range all { + if m.reapIfDeleted(name, sd) { + delete(all, name) + } + } + + return all +} + +// reapIfDeleted removes sd when its asynchronous deletion has completed and +// reports whether it did. +func (m *Mock) reapIfDeleted(name string, sd *streamData) bool { + sd.mu.RLock() + done := sd.deleting && sd.deleteWin.Settled(m.now()) + arn := sd.desc.StreamARN + sd.mu.RUnlock() + + if !done { + return false + } + + m.removeStream(name, arn, sd) + + return true +} + +// removeStream drops the stream and its ARN index entry, unless the name has +// since been taken by a different stream. Each removal is a compare-and-delete +// under the store's lock, so a concurrent CreateStream that reaps the same entry +// and installs a new stream under the name can never have that new stream (or +// its ARN entry) removed by this purge. +func (m *Mock) removeStream(name, arn string, sd *streamData) { + m.streams.UpdateOrDelete(name, func(cur *streamData) (*streamData, bool) { + return cur, cur != sd + }) + + // Checking the stream store while holding the ARN index lock closes the gap + // with CreateStream, which installs the stream before (re)writing the ARN + // entry: either the new stream is already visible here and the entry is + // kept, or CreateStream's Set lands after this delete and restores it. + m.arnToName.UpdateOrDelete(arn, func(cur string) (string, bool) { + return cur, cur != name || m.streams.Has(name) + }) +} + +// statusOf is the stream's observed status: DELETING while a deletion is +// settling, CREATING inside the post-create window, else the stored status. +// Caller holds sd.mu. +func (m *Mock) statusOf(sd *streamData) string { + if sd.deleting { + return driver.StatusDeleting + } + + return sd.createWin.Observe(m.now(), sd.desc.StreamStatus) +} + +// requireActive returns the ResourceInUseException real Kinesis raises when a +// stream-level control-plane operation targets a stream that is not ACTIVE +// (CREATING or DELETING under AsyncSettle). Without AsyncSettle every stream is +// ACTIVE, so this never fires. Caller holds sd.mu. +func (m *Mock) requireActive(sd *streamData) error { + if status := m.statusOf(sd); status != driver.StatusActive { + return errInUse("Stream %s under account %s is not in ACTIVE state (current state: %s)", + sd.desc.StreamName, m.opts.AccountID, status) + } + + return nil +} + // CreateStream creates a new stream with the requested shard count (or on-demand -// default) and moves it straight to ACTIVE. +// default). It reports CREATING for a short window when AsyncSettle is on and +// ACTIVE immediately otherwise. func (m *Mock) CreateStream(ctx context.Context, in driver.CreateStreamInput) error { if in.StreamName == "" { return invalidArg("StreamName is required") @@ -84,9 +184,15 @@ func (m *Mock) CreateStream(ctx context.Context, in driver.CreateStreamInput) er }, consumers: map[string]*driver.Consumer{}, tags: copyTags(in.Tags), + createWin: settle.Pending(driver.StatusCreating, now, m.opts.SettleDuration(settleStreamCreate)), } sd.shards = m.buildShards(shardCount, 0, formatSeq(0), now) + // A stream whose deletion already completed must not block re-creation. + if old, ok := m.streams.Get(in.StreamName); ok { + m.reapIfDeleted(in.StreamName, old) + } + if !m.streams.SetIfAbsent(in.StreamName, sd) { return errInUse("stream %q already exists", in.StreamName) } @@ -96,25 +202,44 @@ func (m *Mock) CreateStream(ctx context.Context, in driver.CreateStreamInput) er return nil } -// DeleteStream removes a stream (and its consumers when enforced). +// DeleteStream removes a stream (and its consumers when enforced). Only an +// ACTIVE stream can be deleted; with AsyncSettle on the stream then reports +// DELETING (still serving reads and writes, as real Kinesis may) until the +// settle window elapses, after which it is gone. func (m *Mock) DeleteStream(_ context.Context, name, arn string, enforceConsumerDeletion bool) error { sd, err := m.resolve(name, arn) if err != nil { return err } - sd.mu.RLock() - hasConsumers := len(sd.consumers) > 0 + sd.mu.Lock() + streamName := sd.desc.StreamName streamARN := sd.desc.StreamARN - sd.mu.RUnlock() - if hasConsumers && !enforceConsumerDeletion { + if err := m.requireActive(sd); err != nil { + sd.mu.Unlock() + + return err + } + + if len(sd.consumers) > 0 && !enforceConsumerDeletion { + sd.mu.Unlock() + return errInUse("stream %q has registered consumers; set EnforceConsumerDeletion", streamName) } - m.streams.Delete(streamName) - m.arnToName.Delete(streamARN) + if d := m.opts.SettleDuration(settleStreamDelete); d > 0 { + sd.deleting = true + sd.deleteWin = settle.Pending(driver.StatusDeleting, m.now(), d) + sd.mu.Unlock() + + return nil + } + + sd.mu.Unlock() + + m.removeStream(streamName, streamARN, sd) return nil } @@ -132,6 +257,7 @@ func (m *Mock) DescribeStream( defer sd.mu.RUnlock() out := sd.desc + out.StreamStatus = m.statusOf(sd) shards, more := pageShards(sd.shards, limit, exclusiveStartShardID) out.Shards = shards out.HasMoreShards = more @@ -182,7 +308,7 @@ func (m *Mock) DescribeStreamSummary(_ context.Context, name, arn string) (*driv return &driver.StreamSummary{ StreamName: sd.desc.StreamName, StreamARN: sd.desc.StreamARN, - StreamStatus: sd.desc.StreamStatus, + StreamStatus: m.statusOf(sd), StreamModeDetails: sd.desc.StreamModeDetails, RetentionPeriodHours: sd.desc.RetentionPeriodHours, StreamCreationTimestamp: sd.desc.StreamCreationTimestamp, @@ -212,7 +338,7 @@ func openShardCount(shards []*shardState) int32 { func (m *Mock) ListStreams( _ context.Context, nextToken, exclusiveStartStreamName string, limit int32, ) (*driver.ListStreamsOutput, error) { - all := m.streams.All() + all := m.liveStreams() names := make([]string, 0, len(all)) for name := range all { @@ -257,7 +383,7 @@ func (m *Mock) ListStreams( out.StreamSummaries = append(out.StreamSummaries, driver.StreamSummary{ StreamName: sd.desc.StreamName, StreamARN: sd.desc.StreamARN, - StreamStatus: sd.desc.StreamStatus, + StreamStatus: m.statusOf(sd), StreamModeDetails: sd.desc.StreamModeDetails, RetentionPeriodHours: sd.desc.RetentionPeriodHours, StreamCreationTimestamp: sd.desc.StreamCreationTimestamp, @@ -294,6 +420,10 @@ func (m *Mock) setRetention(name, arn string, hours int32, increase bool) error sd.mu.Lock() defer sd.mu.Unlock() + if err := m.requireActive(sd); err != nil { + return err + } + if increase && hours < sd.desc.RetentionPeriodHours { return invalidArg("new retention %d is below current %d", hours, sd.desc.RetentionPeriodHours) } @@ -325,6 +455,10 @@ func (m *Mock) UpdateShardCount( sd.mu.Lock() defer sd.mu.Unlock() + if err := m.requireActive(sd); err != nil { + return 0, 0, err + } + before := openShardCount(sd.shards) // AWS allows scaling by at most a factor of 2 (up or down) in a single call. @@ -415,6 +549,10 @@ func (m *Mock) UpdateStreamMode(_ context.Context, arn, mode string) error { sd.mu.Lock() defer sd.mu.Unlock() + if err := m.requireActive(sd); err != nil { + return err + } + sd.desc.StreamModeDetails = mode return nil @@ -447,6 +585,10 @@ func (m *Mock) setEncryption(name, arn, encryptionType, keyID string, on bool) e sd.mu.Lock() defer sd.mu.Unlock() + if err := m.requireActive(sd); err != nil { + return err + } + if on { sd.desc.EncryptionType = driver.EncryptionKMS sd.desc.KeyID = keyID diff --git a/server/aws/kinesis/export_test.go b/server/aws/kinesis/export_test.go new file mode 100644 index 000000000..a03aa49de --- /dev/null +++ b/server/aws/kinesis/export_test.go @@ -0,0 +1,16 @@ +package kinesis + +import "time" + +// SetSubscriptionTimings shortens the SubscribeToShard lifetime and poll +// interval so tests exercise the live push without waiting real minutes. +func (h *Handler) SetSubscriptionTimings(lifetime, poll time.Duration) { + h.subscriptionLifetime = lifetime + h.subscriptionPoll = poll +} + +// SetSubscriptionHeartbeat shortens how long an idle subscription waits before +// pushing an empty event. +func (h *Handler) SetSubscriptionHeartbeat(d time.Duration) { + h.subscriptionHeartbeat = d +} diff --git a/server/aws/kinesis/handler.go b/server/aws/kinesis/handler.go index 16ab488b6..dbb6cb7d7 100644 --- a/server/aws/kinesis/handler.go +++ b/server/aws/kinesis/handler.go @@ -12,6 +12,7 @@ import ( "errors" "net/http" "strings" + "time" cerrors "github.com/stackshy/cloudemu/v2/errors" "github.com/stackshy/cloudemu/v2/server/wire" @@ -24,11 +25,25 @@ const targetPrefix = "Kinesis_20131202." type Handler struct { kinesis kinesisdriver.Kinesis routes map[string]http.HandlerFunc + + // subscriptionLifetime bounds one SubscribeToShard connection and + // subscriptionPoll is how often it pushes newly arrived records; see + // streaming_ops.go. Tests shorten them so they never wait real minutes. + // subscriptionHeartbeat is how long an idle subscription waits before + // pushing an empty event. + subscriptionLifetime time.Duration + subscriptionPoll time.Duration + subscriptionHeartbeat time.Duration } // New returns a Kinesis handler backed by d. func New(d kinesisdriver.Kinesis) *Handler { - h := &Handler{kinesis: d} + h := &Handler{ + kinesis: d, + subscriptionLifetime: defaultSubscriptionLifetime, + subscriptionPoll: defaultSubscriptionPoll, + subscriptionHeartbeat: defaultSubscriptionHeartbeat, + } h.routes = map[string]http.HandlerFunc{ "CreateStream": h.createStream, "DeleteStream": h.deleteStream, diff --git a/server/aws/kinesis/records_ops.go b/server/aws/kinesis/records_ops.go index bf8cb676a..1bfb49ffd 100644 --- a/server/aws/kinesis/records_ops.go +++ b/server/aws/kinesis/records_ops.go @@ -5,6 +5,7 @@ import ( "net/http" "time" + cerrors "github.com/stackshy/cloudemu/v2/errors" kinesisdriver "github.com/stackshy/cloudemu/v2/services/kinesis/driver" ) @@ -125,13 +126,31 @@ func (h *Handler) getShardIterator(w http.ResponseWriter, r *http.Request) { type getRecordsRequest struct { ShardIterator string `json:"ShardIterator"` - Limit int32 `json:"Limit"` + Limit *int32 `json:"Limit"` StreamARN string `json:"StreamARN"` } func (h *Handler) getRecords(w http.ResponseWriter, r *http.Request) { dispatch(h, w, r, func(h *Handler, ctx context.Context, req *getRecordsRequest) (any, error) { - out, err := h.kinesis.GetRecords(ctx, req.ShardIterator, req.Limit) + // The driver reads a zero limit as "Limit omitted" (the default) and + // enforces the 1..MaxGetRecordsLimit range for every other value, so an + // explicitly sent Limit of 0 is the one wire shape it cannot see; reject + // just that here with the same InvalidArgumentException and bound. + var limit int32 + + if req.Limit != nil { + if *req.Limit == 0 { + return nil, &kinesisdriver.APIError{ + Exception: kinesisdriver.ExInvalidArgument, + Err: cerrors.Newf(cerrors.InvalidArgument, + "Limit 0 is invalid: it must be between 1 and %d", kinesisdriver.MaxGetRecordsLimit), + } + } + + limit = *req.Limit + } + + out, err := h.kinesis.GetRecords(ctx, req.ShardIterator, limit) if err != nil { return nil, err } diff --git a/server/aws/kinesis/sdk_roundtrip_streaming_test.go b/server/aws/kinesis/sdk_roundtrip_streaming_test.go index 8e2c74927..e7091538d 100644 --- a/server/aws/kinesis/sdk_roundtrip_streaming_test.go +++ b/server/aws/kinesis/sdk_roundtrip_streaming_test.go @@ -120,6 +120,8 @@ func TestSDKSubscribeToShard(t *testing.T) { gotData string ) + // The subscription stays open (live push), so stop reading once the + // already-written record has been delivered. for ev := range stream.Events() { if e, ok := ev.(*kinesistypes.SubscribeToShardEventStreamMemberSubscribeToShardEvent); ok { sawEvent = true @@ -131,6 +133,10 @@ func TestSDKSubscribeToShard(t *testing.T) { if aws.ToString(e.Value.ContinuationSequenceNumber) == "" { t.Fatal("expected a continuation sequence number") } + + if gotData != "" { + break + } } } diff --git a/server/aws/kinesis/sdk_roundtrip_validation_test.go b/server/aws/kinesis/sdk_roundtrip_validation_test.go new file mode 100644 index 000000000..b6a8a4fda --- /dev/null +++ b/server/aws/kinesis/sdk_roundtrip_validation_test.go @@ -0,0 +1,80 @@ +package kinesis_test + +import ( + "context" + "errors" + "strings" + "testing" + + "github.com/aws/aws-sdk-go-v2/aws" + awskinesis "github.com/aws/aws-sdk-go-v2/service/kinesis" + kinesistypes "github.com/aws/aws-sdk-go-v2/service/kinesis/types" + "github.com/aws/smithy-go" +) + +func wantAPIErrorCode(t *testing.T, err error, code string) { + t.Helper() + + var apiErr smithy.APIError + if !errors.As(err, &apiErr) || apiErr.ErrorCode() != code { + t.Fatalf("err = %v, want %s", err, code) + } +} + +func TestSDKPartitionKeyOver256CharsIsValidationException(t *testing.T) { + ctx := context.Background() + c := newKinesisClient(t) + mustCreate(t, c, "pk", 1) + + long := strings.Repeat("k", 257) + + _, err := c.PutRecord(ctx, &awskinesis.PutRecordInput{ + StreamName: aws.String("pk"), PartitionKey: aws.String(long), Data: []byte("d"), + }) + wantAPIErrorCode(t, err, "ValidationException") + + _, err = c.PutRecords(ctx, &awskinesis.PutRecordsInput{ + StreamName: aws.String("pk"), + Records: []kinesistypes.PutRecordsRequestEntry{ + {PartitionKey: aws.String("ok"), Data: []byte("a")}, + {PartitionKey: aws.String(long), Data: []byte("b")}, + }, + }) + wantAPIErrorCode(t, err, "ValidationException") + + if _, err = c.PutRecord(ctx, &awskinesis.PutRecordInput{ + StreamName: aws.String("pk"), PartitionKey: aws.String(strings.Repeat("k", 256)), Data: []byte("d"), + }); err != nil { + t.Fatalf("256-char key: %v", err) + } +} + +func TestSDKGetRecordsLimitOutOfRange(t *testing.T) { + ctx := context.Background() + c := newKinesisClient(t) + mustCreate(t, c, "lim", 1) + + ls, err := c.ListShards(ctx, &awskinesis.ListShardsInput{StreamName: aws.String("lim")}) + if err != nil { + t.Fatalf("ListShards: %v", err) + } + + it, err := c.GetShardIterator(ctx, &awskinesis.GetShardIteratorInput{ + StreamName: aws.String("lim"), ShardId: ls.Shards[0].ShardId, + ShardIteratorType: kinesistypes.ShardIteratorTypeTrimHorizon, + }) + if err != nil { + t.Fatalf("GetShardIterator: %v", err) + } + + for _, bad := range []int32{0, 10001} { + _, err = c.GetRecords(ctx, &awskinesis.GetRecordsInput{ShardIterator: it.ShardIterator, Limit: aws.Int32(bad)}) + wantAPIErrorCode(t, err, "InvalidArgumentException") + } + + for _, ok := range []*int32{nil, aws.Int32(1), aws.Int32(10000)} { + if _, err = c.GetRecords(ctx, &awskinesis.GetRecordsInput{ShardIterator: it.ShardIterator, Limit: ok}); err != nil { + t.Fatalf("GetRecords(limit=%v): %v", aws.ToInt32(ok), err) + } + } +} diff --git a/server/aws/kinesis/sdk_subscribe_live_test.go b/server/aws/kinesis/sdk_subscribe_live_test.go new file mode 100644 index 000000000..5bc42cc12 --- /dev/null +++ b/server/aws/kinesis/sdk_subscribe_live_test.go @@ -0,0 +1,351 @@ +package kinesis_test + +import ( + "context" + "net" + "net/http" + "net/http/httptest" + "strings" + "sync/atomic" + "testing" + "time" + + "github.com/aws/aws-sdk-go-v2/aws" + awsconfig "github.com/aws/aws-sdk-go-v2/config" + "github.com/aws/aws-sdk-go-v2/credentials" + awskinesis "github.com/aws/aws-sdk-go-v2/service/kinesis" + kinesistypes "github.com/aws/aws-sdk-go-v2/service/kinesis/types" + + "github.com/stackshy/cloudemu/v2" + kinesisserver "github.com/stackshy/cloudemu/v2/server/aws/kinesis" + "github.com/stackshy/cloudemu/v2/server/wire" +) + +// liveFixture is a Kinesis wire server whose SubscribeToShard lifetime and poll +// interval are shortened, plus a count of SubscribeToShard handlers still +// running so tests can prove a subscription's handler returns (no leak). +type liveFixture struct { + c *awskinesis.Client + active atomic.Int32 +} + +func newLiveFixture(t *testing.T, lifetime time.Duration) *liveFixture { + t.Helper() + + return newLiveFixtureWith(t, lifetime, nil, nil) +} + +// newLiveFixtureWith is newLiveFixture plus an optional handler tweak (e.g. a +// shorter heartbeat) and an optional shutdown notifier stamped onto every +// request's base context, the way cloudemu serve does. +func newLiveFixtureWith( + t *testing.T, lifetime time.Duration, tweak func(*kinesisserver.Handler), shutdown *wire.ShutdownNotifier, +) *liveFixture { + t.Helper() + + cloud := cloudemu.NewAWS() + h := kinesisserver.New(cloud.Kinesis) + h.SetSubscriptionTimings(lifetime, 20*time.Millisecond) + + if tweak != nil { + tweak(h) + } + + f := &liveFixture{} + + ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if strings.HasSuffix(r.Header.Get("X-Amz-Target"), ".SubscribeToShard") { + f.active.Add(1) + defer f.active.Add(-1) + } + + h.ServeHTTP(w, r) + })) + + if shutdown != nil { + ts.Config.BaseContext = func(net.Listener) context.Context { return shutdown.Context(context.Background()) } + } + + ts.Start() + t.Cleanup(ts.Close) + + cfg, err := awsconfig.LoadDefaultConfig(context.Background(), + awsconfig.WithRegion("us-east-1"), + awsconfig.WithCredentialsProvider(credentials.NewStaticCredentialsProvider("test", "test", "")), + ) + if err != nil { + t.Fatalf("aws config: %v", err) + } + + f.c = awskinesis.NewFromConfig(cfg, func(o *awskinesis.Options) { + o.BaseEndpoint = aws.String(ts.URL) + o.HTTPClient = &http.Client{Transport: &http.Transport{DisableKeepAlives: true}} + }) + + return f +} + +// waitNoActive fails unless every SubscribeToShard handler has returned. +func (f *liveFixture) waitNoActive(t *testing.T) { + t.Helper() + + deadline := time.Now().Add(5 * time.Second) + for f.active.Load() != 0 { + if time.Now().After(deadline) { + t.Fatalf("%d SubscribeToShard handler(s) still running", f.active.Load()) + } + + time.Sleep(10 * time.Millisecond) + } +} + +// subscribe creates a 1-shard stream with a registered consumer and opens a +// subscription at the given starting position. +func (f *liveFixture) subscribe( + t *testing.T, stream string, pos kinesistypes.ShardIteratorType, +) (*awskinesis.SubscribeToShardEventStream, string) { + t.Helper() + + ctx := context.Background() + mustCreate(t, f.c, stream, 1) + + sum, err := f.c.DescribeStreamSummary(ctx, &awskinesis.DescribeStreamSummaryInput{StreamName: aws.String(stream)}) + if err != nil { + t.Fatalf("DescribeStreamSummary: %v", err) + } + + reg, err := f.c.RegisterStreamConsumer(ctx, &awskinesis.RegisterStreamConsumerInput{ + StreamARN: sum.StreamDescriptionSummary.StreamARN, ConsumerName: aws.String("efo"), + }) + if err != nil { + t.Fatalf("RegisterStreamConsumer: %v", err) + } + + ls, err := f.c.ListShards(ctx, &awskinesis.ListShardsInput{StreamName: aws.String(stream)}) + if err != nil { + t.Fatalf("ListShards: %v", err) + } + + shardID := aws.ToString(ls.Shards[0].ShardId) + + out, err := f.c.SubscribeToShard(ctx, &awskinesis.SubscribeToShardInput{ + ConsumerARN: reg.Consumer.ConsumerARN, + ShardId: aws.String(shardID), + StartingPosition: &kinesistypes.StartingPosition{Type: pos}, + }) + if err != nil { + t.Fatalf("SubscribeToShard: %v", err) + } + + return out.GetStream(), shardID +} + +// nextEvent returns the next SubscribeToShardEvent, or nil when the stream ends. +func nextEvent(t *testing.T, s *awskinesis.SubscribeToShardEventStream) *kinesistypes.SubscribeToShardEvent { + t.Helper() + + select { + case ev, ok := <-s.Events(): + if !ok { + return nil + } + + e, isEvent := ev.(*kinesistypes.SubscribeToShardEventStreamMemberSubscribeToShardEvent) + if !isEvent { + t.Fatalf("unexpected event %T", ev) + } + + return &e.Value + case <-time.After(5 * time.Second): + t.Fatal("timed out waiting for a SubscribeToShardEvent") + + return nil + } +} + +func putData(t *testing.T, c *awskinesis.Client, stream, data string) { + t.Helper() + + if _, err := c.PutRecord(context.Background(), &awskinesis.PutRecordInput{ + StreamName: aws.String(stream), PartitionKey: aws.String("pk"), Data: []byte(data), + }); err != nil { + t.Fatalf("PutRecord: %v", err) + } +} + +// TestSDKSubscribeToShardPushesLiveRecords: real Kinesis keeps the HTTP/2 +// subscription open and pushes records as they are written, not one event and +// close. Records written after subscribing (from LATEST) must arrive on the +// same stream, in order, and closing the stream must end the server handler. +func TestSDKSubscribeToShardPushesLiveRecords(t *testing.T) { + f := newLiveFixture(t, time.Minute) + + stream, _ := f.subscribe(t, "live", kinesistypes.ShardIteratorTypeLatest) + + first := nextEvent(t, stream) + if first == nil || len(first.Records) != 0 || aws.ToString(first.ContinuationSequenceNumber) == "" { + t.Fatalf("initial LATEST event = %+v, want empty records with a continuation", first) + } + + for _, want := range []string{"one", "two"} { + putData(t, f.c, "live", want) + + ev := nextEvent(t, stream) + if ev == nil { + t.Fatalf("stream closed before %q was pushed", want) + } + + if len(ev.Records) != 1 || string(ev.Records[0].Data) != want { + t.Fatalf("pushed records = %+v, want exactly %q", ev.Records, want) + } + + if aws.ToString(ev.ContinuationSequenceNumber) != aws.ToString(ev.Records[0].SequenceNumber) { + t.Fatalf("continuation %q, want last record seq %q", + aws.ToString(ev.ContinuationSequenceNumber), aws.ToString(ev.Records[0].SequenceNumber)) + } + } + + if f.active.Load() != 1 { + t.Fatalf("active subscriptions = %d, want 1 while the client reads", f.active.Load()) + } + + // Client disconnect ends the server-side subscription. + if err := stream.Close(); err != nil && !benignStreamTeardown(err) { + t.Fatalf("Close: %v", err) + } + + f.waitNoActive(t) +} + +// TestSDKSubscribeToShardEndsAfterLifetime: a subscription lasts a bounded time +// (5 minutes in real Kinesis, shortened here) and then the server closes it. +func TestSDKSubscribeToShardEndsAfterLifetime(t *testing.T) { + f := newLiveFixture(t, 300*time.Millisecond) + + stream, _ := f.subscribe(t, "expire", kinesistypes.ShardIteratorTypeTrimHorizon) + defer stream.Close() + + if nextEvent(t, stream) == nil { + t.Fatal("want the initial event before expiry") + } + + if ev := nextEvent(t, stream); ev != nil { + t.Fatalf("want the stream to close at the end of its lifetime, got event %+v", ev) + } + + if err := stream.Err(); err != nil && !benignStreamTeardown(err) { + t.Fatalf("stream error: %v", err) + } + + f.waitNoActive(t) +} + +// TestSDKSubscribeToShardSignalsShardEnd: when the subscribed shard is closed by +// a reshard, the final event carries ChildShards and no continuation sequence +// number (the shard-end contract KCL validates), then the stream ends. +func TestSDKSubscribeToShardSignalsShardEnd(t *testing.T) { + f := newLiveFixture(t, time.Minute) + + stream, _ := f.subscribe(t, "reshard", kinesistypes.ShardIteratorTypeTrimHorizon) + defer stream.Close() + + if nextEvent(t, stream) == nil { + t.Fatal("want the initial event") + } + + putData(t, f.c, "reshard", "before-close") + + if _, err := f.c.UpdateShardCount(context.Background(), &awskinesis.UpdateShardCountInput{ + StreamName: aws.String("reshard"), TargetShardCount: aws.Int32(2), + ScalingType: kinesistypes.ScalingTypeUniformScaling, + }); err != nil { + t.Fatalf("UpdateShardCount: %v", err) + } + + var end *kinesistypes.SubscribeToShardEvent + + for { + ev := nextEvent(t, stream) + if ev == nil { + break + } + + end = ev + } + + if end == nil || len(end.ChildShards) != 2 { + t.Fatalf("final event = %+v, want 2 child shards", end) + } + + if end.ContinuationSequenceNumber != nil { + t.Fatalf("shard-end continuation = %q, want nil", aws.ToString(end.ContinuationSequenceNumber)) + } + + f.waitNoActive(t) +} + +// TestSDKSubscribeToShardHeartbeatsWhenIdle: real enhanced fan-out keeps +// sending empty SubscribeToShardEvents on a quiet shard (KCL restarts a +// subscription that delivers nothing for ~35s), each carrying the unchanged +// continuation so the consumer's checkpoint still advances. +func TestSDKSubscribeToShardHeartbeatsWhenIdle(t *testing.T) { + f := newLiveFixtureWith(t, time.Minute, func(h *kinesisserver.Handler) { + h.SetSubscriptionHeartbeat(100 * time.Millisecond) + }, nil) + + stream, _ := f.subscribe(t, "idle", kinesistypes.ShardIteratorTypeLatest) + defer stream.Close() + + first := nextEvent(t, stream) + if first == nil { + t.Fatal("want the initial event") + } + + cont := aws.ToString(first.ContinuationSequenceNumber) + + for i := range 2 { + ev := nextEvent(t, stream) + if ev == nil { + t.Fatalf("heartbeat %d: stream closed on an idle shard", i) + } + + if len(ev.Records) != 0 || aws.ToString(ev.ContinuationSequenceNumber) != cont { + t.Fatalf("heartbeat %d = %+v, want empty records with continuation %q", i, ev, cont) + } + } + + // A record written after heartbeats still arrives with its own continuation. + putData(t, f.c, "idle", "late") + + ev := nextEvent(t, stream) + for ev != nil && len(ev.Records) == 0 { + ev = nextEvent(t, stream) + } + + if ev == nil || string(ev.Records[0].Data) != "late" { + t.Fatalf("event after heartbeats = %+v, want the %q record", ev, "late") + } +} + +// TestSDKSubscribeToShardEndsOnServerShutdown: when the serving process begins +// a graceful shutdown, open subscriptions end promptly instead of holding the +// shutdown open until its timeout (the subscription lifetime here is a minute). +func TestSDKSubscribeToShardEndsOnServerShutdown(t *testing.T) { + shutdown := wire.NewShutdownNotifier() + f := newLiveFixtureWith(t, time.Minute, nil, shutdown) + + stream, _ := f.subscribe(t, "shutdown", kinesistypes.ShardIteratorTypeLatest) + defer stream.Close() + + if nextEvent(t, stream) == nil { + t.Fatal("want the initial event") + } + + shutdown.Notify() + + if ev := nextEvent(t, stream); ev != nil { + t.Fatalf("want the stream to end on shutdown, got event %+v", ev) + } + + f.waitNoActive(t) +} diff --git a/server/aws/kinesis/streaming_ops.go b/server/aws/kinesis/streaming_ops.go index 477a27497..b8ff3c114 100644 --- a/server/aws/kinesis/streaming_ops.go +++ b/server/aws/kinesis/streaming_ops.go @@ -1,13 +1,16 @@ package kinesis import ( + "context" "encoding/json" + "errors" "net/http" "time" "github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream" "github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream/eventstreamapi" + cerrors "github.com/stackshy/cloudemu/v2/errors" "github.com/stackshy/cloudemu/v2/server/wire" kinesisdriver "github.com/stackshy/cloudemu/v2/services/kinesis/driver" ) @@ -20,6 +23,20 @@ const ( // connection. The SDK blocks its SubscribeToShard call until it arrives, so it // must precede any data event. eventTypeInitial = "initial-response" + + // defaultSubscriptionLifetime is how long one SubscribeToShard connection + // stays open: real Kinesis pushes events "for up to 5 minutes, after which + // time you need to call SubscribeToShard again to renew the subscription". + defaultSubscriptionLifetime = 5 * time.Minute + // defaultSubscriptionPoll is how often an open subscription checks the shard + // for newly written records to push. + defaultSubscriptionPoll = 200 * time.Millisecond + // defaultSubscriptionHeartbeat is how long an idle subscription goes without + // an event before it pushes an empty one. Real enhanced fan-out keeps sending + // empty SubscribeToShardEvents (roughly every 5s) that carry the + // continuation and MillisBehindLatest, and KCL 2.x restarts a subscription + // that delivers nothing for ~35s. + defaultSubscriptionHeartbeat = 5 * time.Second ) // startingPositionJSON is the {Type,SequenceNumber,Timestamp} wire shape of a @@ -40,15 +57,18 @@ type subscribeToShardRequest struct { // frame. Data on each record is a base64 blob (encoding/json renders []byte as // base64), matching the SDK's blob decoding. type subscribeToShardEventJSON struct { - Records []recordJSON `json:"Records"` - ContinuationSequenceNumber string `json:"ContinuationSequenceNumber"` - MillisBehindLatest int64 `json:"MillisBehindLatest"` + Records []recordJSON `json:"Records"` + ContinuationSequenceNumber *string `json:"ContinuationSequenceNumber,omitempty"` + MillisBehindLatest int64 `json:"MillisBehindLatest"` + ChildShards []childShardJSON `json:"ChildShards,omitempty"` } // subscribeToShard handles Kinesis_20131202.SubscribeToShard. It resolves the -// subscription against the driver, then writes an HTTP/2-style -// application/vnd.amazon.eventstream response carrying a single -// SubscribeToShardEvent frame before returning. +// subscription against the driver, then holds an +// application/vnd.amazon.eventstream response open, pushing a +// SubscribeToShardEvent for the records available now and another whenever new +// records land, until the shard ends, the subscription lifetime (5 minutes) +// elapses, or the client disconnects. func (h *Handler) subscribeToShard(w http.ResponseWriter, r *http.Request) { var req subscribeToShardRequest @@ -76,12 +96,93 @@ func (h *Handler) subscribeToShard(w http.ResponseWriter, r *http.Request) { return } - writeSubscribeEvent(w, res) + es := startEventStream(w) + if es.writeSubscribeEvent(res) != nil || res.ContinuationSequenceNumber == "" { + return + } + + h.pushSubscription(r.Context(), es, req.ConsumerARN, req.ShardID, res.ContinuationSequenceNumber) } -// writeSubscribeEvent frames res as a single SubscribeToShardEvent over the -// eventstream protocol. -func writeSubscribeEvent(w http.ResponseWriter, res *kinesisdriver.SubscribeToShardResult) { +// pushSubscription keeps a subscription live: every poll interval it reads the +// records written after the continuation cursor and pushes them as one event, +// and an idle shard still gets an empty event every heartbeat interval. It runs +// on the request goroutine (no goroutines to leak) and returns when the client +// goes away (ctx), the server begins shutting down, the lifetime elapses, the +// shard ends, the write fails, or the shard/consumer disappears (pushed as an +// in-stream exception). +func (h *Handler) pushSubscription(ctx context.Context, es *eventStream, consumerARN, shardID, cont string) { + lifetime := time.NewTimer(h.subscriptionLifetime) + defer lifetime.Stop() + + tick := time.NewTicker(h.subscriptionPoll) + defer tick.Stop() + + shutdown := wire.ShutdownSignal(ctx) + lastEvent := time.Now() + + for awaitTick(ctx, shutdown, lifetime.C, tick.C) { + res, err := h.kinesis.SubscribeToShard(ctx, kinesisdriver.SubscribeToShardInput{ + ConsumerARN: consumerARN, + ShardID: shardID, + StartingPositionType: kinesisdriver.IteratorAfterSequenceNumber, + StartingSequenceNumber: cont, + }) + if err != nil { + es.writeException(err) + + return + } + + // Nothing new on an open shard: keep waiting rather than pushing an + // empty event every tick, but send one once the heartbeat interval has + // passed so an idle subscription still shows progress. + if idleWithinHeartbeat(res, lastEvent, h.subscriptionHeartbeat) { + continue + } + + if es.writeSubscribeEvent(res) != nil || res.ContinuationSequenceNumber == "" { + return + } + + lastEvent = time.Now() + cont = res.ContinuationSequenceNumber + } +} + +// awaitTick blocks until the next poll tick and reports true, or reports false +// once the client goes away, the server begins shutting down, or the +// subscription lifetime elapses. +func awaitTick(ctx context.Context, shutdown <-chan struct{}, lifetime, tick <-chan time.Time) bool { + select { + case <-ctx.Done(): + return false + case <-shutdown: + return false + case <-lifetime: + return false + case <-tick: + return true + } +} + +// idleWithinHeartbeat reports whether a poll found nothing new on an open shard +// and the last event went out less than one heartbeat ago, so nothing needs +// pushing yet. +func idleWithinHeartbeat(res *kinesisdriver.SubscribeToShardResult, lastEvent time.Time, heartbeat time.Duration) bool { + return len(res.Records) == 0 && res.ContinuationSequenceNumber != "" && time.Since(lastEvent) < heartbeat +} + +// eventStream is an open application/vnd.amazon.eventstream response. +type eventStream struct { + w http.ResponseWriter + enc *eventstream.Encoder + flusher http.Flusher +} + +// startEventStream writes the eventstream response headers and the +// initial-response frame the SDK's SubscribeToShard call blocks on. +func startEventStream(w http.ResponseWriter) *eventStream { w.Header().Set("Content-Type", contentTypeEventStream) // Deliberately do NOT force "Connection: close" here. Closing the connection // as the handler returns races the client's in-flight read of the final @@ -91,36 +192,87 @@ func writeSubscribeEvent(w http.ResponseWriter, res *kinesisdriver.SubscribeToSh // stream tests tolerate, never as lost data. w.WriteHeader(http.StatusOK) - enc := eventstream.NewEncoder() flusher, _ := w.(http.Flusher) + es := &eventStream{w: w, enc: eventstream.NewEncoder(), flusher: flusher} // The SDK's SubscribeToShard call blocks until it reads the initial-response // event, so emit it (empty SubscribeToShardOutput body) before any data. - writeEvent(w, enc, flusher, eventTypeInitial, []byte("{}")) + _ = es.writeEvent(eventTypeInitial, []byte("{}")) - payload, err := json.Marshal(subscribeToShardEventJSON{ - Records: recordsToWire(res.Records), - ContinuationSequenceNumber: res.ContinuationSequenceNumber, - MillisBehindLatest: res.MillisBehindLatest, - }) + return es +} + +// writeSubscribeEvent frames res as one SubscribeToShardEvent. An empty +// continuation (shard end) is omitted on the wire, i.e. null, with ChildShards. +func (es *eventStream) writeSubscribeEvent(res *kinesisdriver.SubscribeToShardResult) error { + ev := subscribeToShardEventJSON{ + Records: recordsToWire(res.Records), + MillisBehindLatest: res.MillisBehindLatest, + ChildShards: childShardsToWire(res.ChildShards), + } + + if res.ContinuationSequenceNumber != "" { + ev.ContinuationSequenceNumber = &res.ContinuationSequenceNumber + } + + payload, err := json.Marshal(ev) if err != nil { - return + return err } - writeEvent(w, enc, flusher, eventTypeSubscribe, payload) + return es.writeEvent(eventTypeSubscribe, payload) } // writeEvent frames one JSON event of the given type and flushes it. -func writeEvent(w http.ResponseWriter, enc *eventstream.Encoder, flusher http.Flusher, eventType string, payload []byte) { +func (es *eventStream) writeEvent(eventType string, payload []byte) error { var hdr eventstream.Headers hdr.Set(eventstreamapi.MessageTypeHeader, eventstream.StringValue(eventstreamapi.EventMessageType)) hdr.Set(eventstreamapi.EventTypeHeader, eventstream.StringValue(eventType)) hdr.Set(eventstreamapi.ContentTypeHeader, eventstream.StringValue(contentTypeJSON)) - _ = enc.Encode(w, eventstream.Message{Headers: hdr, Payload: payload}) + return es.encode(eventstream.Message{Headers: hdr, Payload: payload}) +} + +// writeException pushes err as an in-stream exception event (the +// SubscribeToShardEventStream carries ResourceNotFoundException, +// ResourceInUseException, ... members), which the SDK surfaces from Err(). +func (es *eventStream) writeException(err error) { + exType := "InternalFailureException" + + var apiErr *kinesisdriver.APIError - if flusher != nil { - flusher.Flush() + switch { + case errors.As(err, &apiErr): + exType = apiErr.Exception + case cerrors.IsNotFound(err): + exType = "ResourceNotFoundException" + case cerrors.IsAlreadyExists(err), cerrors.IsFailedPrecondition(err): + exType = "ResourceInUseException" + } + + payload, mErr := json.Marshal(map[string]string{"message": cerrors.Message(err)}) + if mErr != nil { + return } + + var hdr eventstream.Headers + + hdr.Set(eventstreamapi.MessageTypeHeader, eventstream.StringValue(eventstreamapi.ExceptionMessageType)) + hdr.Set(eventstreamapi.ExceptionTypeHeader, eventstream.StringValue(exType)) + hdr.Set(eventstreamapi.ContentTypeHeader, eventstream.StringValue(contentTypeJSON)) + + _ = es.encode(eventstream.Message{Headers: hdr, Payload: payload}) +} + +func (es *eventStream) encode(msg eventstream.Message) error { + if err := es.enc.Encode(es.w, msg); err != nil { + return err + } + + if es.flusher != nil { + es.flusher.Flush() + } + + return nil } diff --git a/server/serverkit/serverkit.go b/server/serverkit/serverkit.go index 6bf0f794b..ec8d05dde 100644 --- a/server/serverkit/serverkit.go +++ b/server/serverkit/serverkit.go @@ -49,6 +49,7 @@ import ( cgrpc "github.com/stackshy/cloudemu/v2/server/grpc" bigtableadmingrpc "github.com/stackshy/cloudemu/v2/server/grpc/bigtableadmin" ociserver "github.com/stackshy/cloudemu/v2/server/oci" + "github.com/stackshy/cloudemu/v2/server/wire" "github.com/stackshy/cloudemu/v2/server/wire/awsidentity" btdriver "github.com/stackshy/cloudemu/v2/services/bigtable/driver" "github.com/stackshy/cloudemu/v2/services/kubernetes" @@ -1112,12 +1113,12 @@ func (a *App) buildServers() ([]listenerServer, endpointSet, error) { servers = append(servers, &httpServer{ label: p, - srv: &http.Server{ + srv: withShutdownSignal(&http.Server{ Addr: addr, Handler: a.handlerFor(a.backends[p], a.seedFor(p)), TLSConfig: tlsCfg, ReadHeaderTimeout: serverReadHeaderTimeout, - }, + }), tls: isTLS, }) } @@ -1325,6 +1326,19 @@ func (h *httpServer) serve(ln net.Listener) error { func (h *httpServer) shutdown(ctx context.Context) error { return h.srv.Shutdown(ctx) } +// withShutdownSignal gives every request srv serves a wire.ShutdownSignal that +// closes the moment srv.Shutdown begins, so long-lived responses (Kinesis +// SubscribeToShard event streams) end promptly rather than holding a graceful +// shutdown open until its timeout. Ordinary requests are unaffected: the +// request context itself is not canceled. +func withShutdownSignal(srv *http.Server) *http.Server { + n := wire.NewShutdownNotifier() + srv.BaseContext = func(net.Listener) context.Context { return n.Context(context.Background()) } + srv.RegisterOnShutdown(n.Notify) + + return srv +} + // grpcServer adapts the server/grpc transport wrapper to listenerServer, so the // optional gRPC endpoint binds and drains through the same loop as the HTTP // ones. Its serve() already maps a clean stop to nil (see server/grpc). diff --git a/server/serverkit/shutdown_signal_test.go b/server/serverkit/shutdown_signal_test.go new file mode 100644 index 000000000..ef6e1c751 --- /dev/null +++ b/server/serverkit/shutdown_signal_test.go @@ -0,0 +1,70 @@ +package serverkit + +import ( + "context" + "net" + "net/http" + "testing" + "time" + + "github.com/stackshy/cloudemu/v2/server/wire" +) + +// TestWithShutdownSignalEndsLongLivedHandlers: a handler holding a response +// open until the server shuts down (as a SubscribeToShard event stream does) +// sees wire.ShutdownSignal fire as soon as Shutdown begins, so a graceful +// shutdown completes promptly rather than waiting out its timeout. +func TestWithShutdownSignalEndsLongLivedHandlers(t *testing.T) { + entered := make(chan struct{}) + + srv := withShutdownSignal(&http.Server{ + ReadHeaderTimeout: time.Second, + Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + w.(http.Flusher).Flush() + close(entered) + + select { + case <-wire.ShutdownSignal(r.Context()): + case <-time.After(30 * time.Second): + } + }), + }) + + var lc net.ListenConfig + + ln, err := lc.Listen(context.Background(), "tcp", "127.0.0.1:0") + if err != nil { + t.Fatalf("listen: %v", err) + } + + go func() { _ = srv.Serve(ln) }() + + go func() { + req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "http://"+ln.Addr().String(), http.NoBody) + + resp, err := http.DefaultClient.Do(req) + if err == nil { + _ = resp.Body.Close() + } + }() + + select { + case <-entered: + case <-time.After(5 * time.Second): + t.Fatal("handler never started") + } + + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + + start := time.Now() + + if err := srv.Shutdown(ctx); err != nil { + t.Fatalf("Shutdown: %v", err) + } + + if took := time.Since(start); took > 3*time.Second { + t.Fatalf("Shutdown took %v; the long-lived handler did not see the shutdown signal", took) + } +} diff --git a/server/wire/shutdown.go b/server/wire/shutdown.go new file mode 100644 index 000000000..2390c656a --- /dev/null +++ b/server/wire/shutdown.go @@ -0,0 +1,49 @@ +package wire + +import ( + "context" + "sync" +) + +type shutdownSignalKey struct{} + +// WithShutdownSignal returns a copy of ctx that carries ch as the server's +// shutdown signal. A server installs it as the base context of every request +// and closes ch when a graceful shutdown begins, so long-lived handlers (event +// streams, watches) can end promptly instead of holding the shutdown open +// until its timeout. Unlike canceling the request context, it leaves ordinary +// in-flight requests untouched. +func WithShutdownSignal(ctx context.Context, ch <-chan struct{}) context.Context { + return context.WithValue(ctx, shutdownSignalKey{}, ch) +} + +// ShutdownSignal returns the channel that closes when the serving process +// begins shutting down, or nil (which never fires in a select) when ctx carries +// none, e.g. under httptest or an embedding application's own server. +func ShutdownSignal(ctx context.Context) <-chan struct{} { + ch, _ := ctx.Value(shutdownSignalKey{}).(<-chan struct{}) + + return ch +} + +// ShutdownNotifier owns one shutdown signal: Context stamps it onto a base +// context and Notify closes it (safe to call more than once). +type ShutdownNotifier struct { + once sync.Once + ch chan struct{} +} + +// NewShutdownNotifier returns an open shutdown signal. +func NewShutdownNotifier() *ShutdownNotifier { + return &ShutdownNotifier{ch: make(chan struct{})} +} + +// Context returns ctx carrying this notifier's signal. +func (n *ShutdownNotifier) Context(ctx context.Context) context.Context { + return WithShutdownSignal(ctx, n.ch) +} + +// Notify closes the signal, ending every handler waiting on it. +func (n *ShutdownNotifier) Notify() { + n.once.Do(func() { close(n.ch) }) +} diff --git a/server/wire/shutdown_test.go b/server/wire/shutdown_test.go new file mode 100644 index 000000000..0b05a4a5f --- /dev/null +++ b/server/wire/shutdown_test.go @@ -0,0 +1,32 @@ +package wire_test + +import ( + "context" + "testing" + + "github.com/stackshy/cloudemu/v2/server/wire" +) + +func TestShutdownSignal(t *testing.T) { + if ch := wire.ShutdownSignal(context.Background()); ch != nil { + t.Fatalf("ShutdownSignal without a notifier = %v, want nil", ch) + } + + n := wire.NewShutdownNotifier() + ch := wire.ShutdownSignal(n.Context(context.Background())) + + select { + case <-ch: + t.Fatal("signal fired before Notify") + default: + } + + n.Notify() + n.Notify() // idempotent + + select { + case <-ch: + default: + t.Fatal("signal did not fire after Notify") + } +} diff --git a/services/kinesis/driver/driver.go b/services/kinesis/driver/driver.go index 6d1c1aa55..82442410a 100644 --- a/services/kinesis/driver/driver.go +++ b/services/kinesis/driver/driver.go @@ -29,6 +29,11 @@ const ( EncryptionKMS = "KMS" ) +// MaxGetRecordsLimit is the upper bound of GetRecords' Limit (valid range +// 1..10000) and the number of records returned when Limit is omitted. It is +// shared by implementations and wire layers so the bound can't drift. +const MaxGetRecordsLimit = 10000 + // Shard iterator types. const ( IteratorAtSequenceNumber = "AT_SEQUENCE_NUMBER" @@ -202,11 +207,14 @@ type SubscribeToShardInput struct { } // SubscribeToShardResult carries one enhanced fan-out event's worth of records -// plus the continuation cursor used to resume the subscription. +// plus the continuation cursor used to resume the subscription. At the end of a +// closed shard ContinuationSequenceNumber is empty and ChildShards lists the +// shard's children, matching the SubscribeToShardEvent shard-end contract. type SubscribeToShardResult struct { Records []Record ContinuationSequenceNumber string MillisBehindLatest int64 + ChildShards []ChildShard } // ShardFilter narrows ListShards to a subset of shards. Type is one of the diff --git a/services/kinesis/driver/errors.go b/services/kinesis/driver/errors.go index 4b2353d9f..19566e623 100644 --- a/services/kinesis/driver/errors.go +++ b/services/kinesis/driver/errors.go @@ -5,6 +5,10 @@ package driver const ( ExExpiredIterator = "ExpiredIteratorException" ExInvalidArgument = "InvalidArgumentException" + // ExValidation is the request-shape (model constraint) failure real Kinesis + // returns as "1 validation error detected: Value ... failed to satisfy + // constraint ...", e.g. a PartitionKey longer than 256 characters. + ExValidation = "ValidationException" ) // APIError tags a canonical cloudemu error with the Kinesis exception name it From f3dbe31c6d01f21a5efd4836fea072d0e6556b66 Mon Sep 17 00:00:00 2001 From: Satyam-Trivedi-ZS Date: Fri, 23 Oct 2026 09:50:09 +0530 Subject: [PATCH 21/31] fix(aws-secretsmanager): replication ops, ListSecrets planned-deletion/sort/filters, DescribeSecret region and access fields (#1651) * fix(aws-secretsmanager): replication ops, ListSecrets planned-deletion/sort/filters, DescribeSecret region and access fields - ListSecrets honors IncludePlannedDeletion (with DeletedDate), SortBy and SortOrder, real primary-region/owning-service prefix filters and '!' negation; unknown filter keys, SortBy/SortOrder values and out-of-range MaxResults return ValidationException, and NextToken is a cursor (InvalidNextTokenException when malformed). - Add ReplicateSecretToRegions, RemoveRegionsFromReplication and StopReplicationToReplica, and honor CreateSecret AddReplicaRegions, as replica metadata on the primary; a replicated primary can't be deleted. - DescribeSecret reports PrimaryRegion, ReplicationStatus and LastAccessedDate (day resolution, stamped by GetSecretValue). - Use the real AWS messages for operations on a secret scheduled for deletion and for CreateSecret over such a name. Part of #1516 * docs(coverage): Secrets Manager replication and ListSecrets non-goals Part of #1516 * refactor(aws-secretsmanager): bring ListSecrets and BatchGetSecretValue handlers under the gocyclo limit Move BatchGetSecretValue's request checks into validateBatchRequest and ListSecrets' per-entry enrichment (rotation info, DeletedDate) into listEntry. Both handlers were at cyclomatic complexity 11. * fix(aws-secretsmanager): validate replica Regions, close the delete/replicate race, stamp LastChangedDate - Reject a replica Region that doesn't match the API model's RegionType pattern (^([a-z]+-)+\d+$) with InvalidParameterException, on both CreateSecret AddReplicaRegions and ReplicateSecretToRegions, so a typo can't leave a phantom replica that blocks DeleteSecret. - DeleteSecret now checks for replicas under the same write lock that schedules (or force-marks) the deletion, so a concurrent ReplicateSecretToRegions can't leave a secret both deleted and replicated. - ReplicateSecretToRegions and RemoveRegionsFromReplication update LastChangedDate, which AWS defines as the last time the secret was modified in any way. * docs(coverage): document ListSecrets negated-value semantics and replica Region checks Values within one filter are OR'd, negated ones included; excluding several prefixes takes one filter per prefix. Pin that in a test. --- docs/coverage/aws/secretsmanager.md | 4 +- docs/coverage/azure/keyvault.md | 4 +- docs/coverage/gcp/secretmanager.md | 4 +- docs/coverage/nongoals/secrets.md | 3 + docs/coverage/oci/vault.md | 4 +- providers/aws/secretsmanager/replication.go | 270 ++++++++++++ .../aws/secretsmanager/replication_test.go | 335 ++++++++++++++ .../aws/secretsmanager/resource_policy.go | 6 +- .../aws/secretsmanager/secretsmanager.go | 102 +++-- providers/aws/secretsmanager/snapshot.go | 6 + providers/aws/secretsmanager/staging.go | 20 +- providers/aws/secretsmanager/update.go | 3 +- server/aws/secretsmanager/batch.go | 25 +- server/aws/secretsmanager/handler.go | 55 ++- server/aws/secretsmanager/helpers.go | 29 +- server/aws/secretsmanager/list.go | 240 ++++++++++ server/aws/secretsmanager/list_test.go | 156 +++++++ server/aws/secretsmanager/operations.go | 105 ++++- server/aws/secretsmanager/replication.go | 78 ++++ server/aws/secretsmanager/replication_test.go | 415 ++++++++++++++++++ server/aws/secretsmanager/types.go | 132 +++++- services/secrets/driver/driver.go | 34 ++ services/secrets/secrets.go | 2 + 23 files changed, 1917 insertions(+), 115 deletions(-) create mode 100644 docs/coverage/nongoals/secrets.md create mode 100644 providers/aws/secretsmanager/replication.go create mode 100644 providers/aws/secretsmanager/replication_test.go create mode 100644 server/aws/secretsmanager/list.go create mode 100644 server/aws/secretsmanager/list_test.go create mode 100644 server/aws/secretsmanager/replication.go create mode 100644 server/aws/secretsmanager/replication_test.go diff --git a/docs/coverage/aws/secretsmanager.md b/docs/coverage/aws/secretsmanager.md index 83f3fc83f..095d0ec4e 100644 --- a/docs/coverage/aws/secretsmanager.md +++ b/docs/coverage/aws/secretsmanager.md @@ -17,4 +17,6 @@ AWS's `secrets` service ยท portable interface `driver.Secrets` ยท [AWS index](./ ## Not in scope -_Not documented yet. See the [emulator boundary](../../../README.md) for cloudemu-wide non-goals._ +- AWS Secrets Manager multi-Region replication (`CreateSecret` `AddReplicaRegions`, `ReplicateSecretToRegions`, `RemoveRegionsFromReplication`) is control-plane metadata on the primary secret: `DescribeSecret` reports each replica's Region, KMS key (`alias/aws/secretsmanager` when none was given), `InSync` status and `Replication succeeded` message, but no replica secret exists in the replica Region. Each Region's mock is independent (and `cloudemu serve` builds a Region's state only when that Region is first used), so the primary cannot write into it. Replication completes at once instead of passing through `InProgress`. Because no secret here is ever a replica, `StopReplicationToReplica` answers `InvalidRequestException` for an existing secret and `ForceOverwriteReplicaSecret` has nothing to overwrite. A replicated primary can't be deleted until its replicas are removed, as in AWS. A replica Region must have the shape of a Region code (`^([a-z]+-)+\d+$`, the API model's `RegionType` pattern); cloudemu has no list of real Regions, so a well-formed code that isn't a real Region is still accepted. The messages for a replica Region equal to the primary, a duplicate replica Region and removing a Region that isn't replicated are descriptive; the exception (`InvalidParameterException`) is unconfirmed against AWS. +- AWS Secrets Manager `LastAccessedDate` is stamped at day resolution (midnight UTC) by `GetSecretValue` and `BatchGetSecretValue`, for the secret only; per-version `LastAccessedDate` on `ListSecretVersionIds` and replica `LastAccessedDate` are not tracked. No secret has an `OwningService`, because no emulated service creates managed secrets. +- AWS Secrets Manager `ListSecrets` with neither `SortBy` nor `SortOrder` lists by name ascending. AWS documents `CreatedDate` as the default sort key but not its direction, so the default order is left unchanged; once either field is given, `SortBy` defaults to `created-date` and `SortOrder` to `asc`. Values prefixed with `!` are negation filters. Values within one filter are OR'd, negated ones included, so `Key=name,Values=!a,!b` matches every secret whose name lacks either prefix (that is, nearly all of them); to exclude both prefixes, pass two `name` filters, which are AND'd. AWS doesn't document how several negated values combine, and moto ORs them the same way. diff --git a/docs/coverage/azure/keyvault.md b/docs/coverage/azure/keyvault.md index 02c4044fc..da44d6f8a 100644 --- a/docs/coverage/azure/keyvault.md +++ b/docs/coverage/azure/keyvault.md @@ -96,4 +96,6 @@ KeyVaultVaults is the Azure Key Vault control-plane (ARM) surface ## Not in scope -_Not documented yet. See the [emulator boundary](../../../README.md) for cloudemu-wide non-goals._ +- AWS Secrets Manager multi-Region replication (`CreateSecret` `AddReplicaRegions`, `ReplicateSecretToRegions`, `RemoveRegionsFromReplication`) is control-plane metadata on the primary secret: `DescribeSecret` reports each replica's Region, KMS key (`alias/aws/secretsmanager` when none was given), `InSync` status and `Replication succeeded` message, but no replica secret exists in the replica Region. Each Region's mock is independent (and `cloudemu serve` builds a Region's state only when that Region is first used), so the primary cannot write into it. Replication completes at once instead of passing through `InProgress`. Because no secret here is ever a replica, `StopReplicationToReplica` answers `InvalidRequestException` for an existing secret and `ForceOverwriteReplicaSecret` has nothing to overwrite. A replicated primary can't be deleted until its replicas are removed, as in AWS. A replica Region must have the shape of a Region code (`^([a-z]+-)+\d+$`, the API model's `RegionType` pattern); cloudemu has no list of real Regions, so a well-formed code that isn't a real Region is still accepted. The messages for a replica Region equal to the primary, a duplicate replica Region and removing a Region that isn't replicated are descriptive; the exception (`InvalidParameterException`) is unconfirmed against AWS. +- AWS Secrets Manager `LastAccessedDate` is stamped at day resolution (midnight UTC) by `GetSecretValue` and `BatchGetSecretValue`, for the secret only; per-version `LastAccessedDate` on `ListSecretVersionIds` and replica `LastAccessedDate` are not tracked. No secret has an `OwningService`, because no emulated service creates managed secrets. +- AWS Secrets Manager `ListSecrets` with neither `SortBy` nor `SortOrder` lists by name ascending. AWS documents `CreatedDate` as the default sort key but not its direction, so the default order is left unchanged; once either field is given, `SortBy` defaults to `created-date` and `SortOrder` to `asc`. Values prefixed with `!` are negation filters. Values within one filter are OR'd, negated ones included, so `Key=name,Values=!a,!b` matches every secret whose name lacks either prefix (that is, nearly all of them); to exclude both prefixes, pass two `name` filters, which are AND'd. AWS doesn't document how several negated values combine, and moto ORs them the same way. diff --git a/docs/coverage/gcp/secretmanager.md b/docs/coverage/gcp/secretmanager.md index 3c0f603e2..17cdc12b0 100644 --- a/docs/coverage/gcp/secretmanager.md +++ b/docs/coverage/gcp/secretmanager.md @@ -35,4 +35,6 @@ GCPSecrets is the GCP Secret Manager-specific surface kept off the shared ## Not in scope -_Not documented yet. See the [emulator boundary](../../../README.md) for cloudemu-wide non-goals._ +- AWS Secrets Manager multi-Region replication (`CreateSecret` `AddReplicaRegions`, `ReplicateSecretToRegions`, `RemoveRegionsFromReplication`) is control-plane metadata on the primary secret: `DescribeSecret` reports each replica's Region, KMS key (`alias/aws/secretsmanager` when none was given), `InSync` status and `Replication succeeded` message, but no replica secret exists in the replica Region. Each Region's mock is independent (and `cloudemu serve` builds a Region's state only when that Region is first used), so the primary cannot write into it. Replication completes at once instead of passing through `InProgress`. Because no secret here is ever a replica, `StopReplicationToReplica` answers `InvalidRequestException` for an existing secret and `ForceOverwriteReplicaSecret` has nothing to overwrite. A replicated primary can't be deleted until its replicas are removed, as in AWS. A replica Region must have the shape of a Region code (`^([a-z]+-)+\d+$`, the API model's `RegionType` pattern); cloudemu has no list of real Regions, so a well-formed code that isn't a real Region is still accepted. The messages for a replica Region equal to the primary, a duplicate replica Region and removing a Region that isn't replicated are descriptive; the exception (`InvalidParameterException`) is unconfirmed against AWS. +- AWS Secrets Manager `LastAccessedDate` is stamped at day resolution (midnight UTC) by `GetSecretValue` and `BatchGetSecretValue`, for the secret only; per-version `LastAccessedDate` on `ListSecretVersionIds` and replica `LastAccessedDate` are not tracked. No secret has an `OwningService`, because no emulated service creates managed secrets. +- AWS Secrets Manager `ListSecrets` with neither `SortBy` nor `SortOrder` lists by name ascending. AWS documents `CreatedDate` as the default sort key but not its direction, so the default order is left unchanged; once either field is given, `SortBy` defaults to `created-date` and `SortOrder` to `asc`. Values prefixed with `!` are negation filters. Values within one filter are OR'd, negated ones included, so `Key=name,Values=!a,!b` matches every secret whose name lacks either prefix (that is, nearly all of them); to exclude both prefixes, pass two `name` filters, which are AND'd. AWS doesn't document how several negated values combine, and moto ORs them the same way. diff --git a/docs/coverage/nongoals/secrets.md b/docs/coverage/nongoals/secrets.md new file mode 100644 index 000000000..f7cff08bb --- /dev/null +++ b/docs/coverage/nongoals/secrets.md @@ -0,0 +1,3 @@ +- AWS Secrets Manager multi-Region replication (`CreateSecret` `AddReplicaRegions`, `ReplicateSecretToRegions`, `RemoveRegionsFromReplication`) is control-plane metadata on the primary secret: `DescribeSecret` reports each replica's Region, KMS key (`alias/aws/secretsmanager` when none was given), `InSync` status and `Replication succeeded` message, but no replica secret exists in the replica Region. Each Region's mock is independent (and `cloudemu serve` builds a Region's state only when that Region is first used), so the primary cannot write into it. Replication completes at once instead of passing through `InProgress`. Because no secret here is ever a replica, `StopReplicationToReplica` answers `InvalidRequestException` for an existing secret and `ForceOverwriteReplicaSecret` has nothing to overwrite. A replicated primary can't be deleted until its replicas are removed, as in AWS. A replica Region must have the shape of a Region code (`^([a-z]+-)+\d+$`, the API model's `RegionType` pattern); cloudemu has no list of real Regions, so a well-formed code that isn't a real Region is still accepted. The messages for a replica Region equal to the primary, a duplicate replica Region and removing a Region that isn't replicated are descriptive; the exception (`InvalidParameterException`) is unconfirmed against AWS. +- AWS Secrets Manager `LastAccessedDate` is stamped at day resolution (midnight UTC) by `GetSecretValue` and `BatchGetSecretValue`, for the secret only; per-version `LastAccessedDate` on `ListSecretVersionIds` and replica `LastAccessedDate` are not tracked. No secret has an `OwningService`, because no emulated service creates managed secrets. +- AWS Secrets Manager `ListSecrets` with neither `SortBy` nor `SortOrder` lists by name ascending. AWS documents `CreatedDate` as the default sort key but not its direction, so the default order is left unchanged; once either field is given, `SortBy` defaults to `created-date` and `SortOrder` to `asc`. Values prefixed with `!` are negation filters. Values within one filter are OR'd, negated ones included, so `Key=name,Values=!a,!b` matches every secret whose name lacks either prefix (that is, nearly all of them); to exclude both prefixes, pass two `name` filters, which are AND'd. AWS doesn't document how several negated values combine, and moto ORs them the same way. diff --git a/docs/coverage/oci/vault.md b/docs/coverage/oci/vault.md index 46940abeb..99448eec9 100644 --- a/docs/coverage/oci/vault.md +++ b/docs/coverage/oci/vault.md @@ -17,4 +17,6 @@ OCI's `secrets` service ยท portable interface `driver.Secrets` ยท [OCI index](./ ## Not in scope -_Not documented yet. See the [emulator boundary](../../../README.md) for cloudemu-wide non-goals._ +- AWS Secrets Manager multi-Region replication (`CreateSecret` `AddReplicaRegions`, `ReplicateSecretToRegions`, `RemoveRegionsFromReplication`) is control-plane metadata on the primary secret: `DescribeSecret` reports each replica's Region, KMS key (`alias/aws/secretsmanager` when none was given), `InSync` status and `Replication succeeded` message, but no replica secret exists in the replica Region. Each Region's mock is independent (and `cloudemu serve` builds a Region's state only when that Region is first used), so the primary cannot write into it. Replication completes at once instead of passing through `InProgress`. Because no secret here is ever a replica, `StopReplicationToReplica` answers `InvalidRequestException` for an existing secret and `ForceOverwriteReplicaSecret` has nothing to overwrite. A replicated primary can't be deleted until its replicas are removed, as in AWS. A replica Region must have the shape of a Region code (`^([a-z]+-)+\d+$`, the API model's `RegionType` pattern); cloudemu has no list of real Regions, so a well-formed code that isn't a real Region is still accepted. The messages for a replica Region equal to the primary, a duplicate replica Region and removing a Region that isn't replicated are descriptive; the exception (`InvalidParameterException`) is unconfirmed against AWS. +- AWS Secrets Manager `LastAccessedDate` is stamped at day resolution (midnight UTC) by `GetSecretValue` and `BatchGetSecretValue`, for the secret only; per-version `LastAccessedDate` on `ListSecretVersionIds` and replica `LastAccessedDate` are not tracked. No secret has an `OwningService`, because no emulated service creates managed secrets. +- AWS Secrets Manager `ListSecrets` with neither `SortBy` nor `SortOrder` lists by name ascending. AWS documents `CreatedDate` as the default sort key but not its direction, so the default order is left unchanged; once either field is given, `SortBy` defaults to `created-date` and `SortOrder` to `asc`. Values prefixed with `!` are negation filters. Values within one filter are OR'd, negated ones included, so `Key=name,Values=!a,!b` matches every secret whose name lacks either prefix (that is, nearly all of them); to exclude both prefixes, pass two `name` filters, which are AND'd. AWS doesn't document how several negated values combine, and moto ORs them the same way. diff --git a/providers/aws/secretsmanager/replication.go b/providers/aws/secretsmanager/replication.go new file mode 100644 index 000000000..806736987 --- /dev/null +++ b/providers/aws/secretsmanager/replication.go @@ -0,0 +1,270 @@ +package secretsmanager + +import ( + "context" + "regexp" + "strings" + "time" + + "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/services/secrets/driver" +) + +// Multi-Region secret replication (ReplicateSecretToRegions, +// RemoveRegionsFromReplication, StopReplicationToReplica, CreateSecret +// AddReplicaRegions). +// +// cloudemu runs one Secrets Manager mock per Region and those mocks don't +// reach one another, so replication is modeled as control-plane metadata on +// the primary secret: the replica list and per-replica status that +// DescribeSecret/CreateSecret/ReplicateSecretToRegions report. No replica +// secret materializes in the target Region's mock. This mirrors how the KMS +// mock models ReplicateKey and the DynamoDB mock models global-table replicas. +// Replication completes synchronously, so a replica is reported InSync +// immediately rather than passing through InProgress. + +// Replica status values (StatusType) and the message real Secrets Manager +// reports for a replica that finished replicating. +const ( + replicaStatusInSync = "InSync" + replicaStatusMessageInSync = "Replication succeeded" +) + +// regionNamePattern and maxRegionNameLen are the RegionType constraints from +// the Secrets Manager API model. cloudemu has no authoritative list of AWS +// Regions, so a replica Region is checked against the Region-code shape: +// that catches typos such as "not-a-region" or "eu-west", but a well-formed +// code that isn't a real Region is still accepted. +var regionNamePattern = regexp.MustCompile(`^([a-z]+-)+\d+$`) + +const maxRegionNameLen = 128 + +// validateReplicaRegion rejects a replica Region that is empty or isn't shaped +// like a Region code, as InvalidParameterException. +func validateReplicaRegion(region string) error { + if region == "" { + return errors.New(errors.InvalidArgument, "a replica Region is required") + } + + if len(region) > maxRegionNameLen || !regionNamePattern.MatchString(region) { + return errors.Newf(errors.InvalidArgument, "Invalid Region: %s is not a valid Region code", region) + } + + return nil +} + +// addReplicas validates add against the existing replica list and the +// primary Region and returns a NEW slice (existing is never mutated, so +// SecretInfo copies that share it stay consistent) with the added replicas +// appended. A replica without a KmsKeyId is encrypted with +// alias/aws/secretsmanager, which is what real Secrets Manager echoes. +func addReplicas( + existing []driver.SecretReplicationStatus, add []driver.SecretReplicaRegion, primaryRegion string, +) ([]driver.SecretReplicationStatus, error) { + if len(add) == 0 { + return nil, errors.New(errors.InvalidArgument, "AddReplicaRegions must contain at least one Region") + } + + out := make([]driver.SecretReplicationStatus, len(existing), len(existing)+len(add)) + copy(out, existing) + + for _, r := range add { + region := r.Region + if err := validateReplicaRegion(region); err != nil { + return nil, err + } + + if region == primaryRegion { + return nil, errors.Newf(errors.InvalidArgument, + "Invalid replica Region %s: it is the primary Region of the secret", region) + } + + if replicaIndex(out, region) >= 0 { + return nil, errors.Newf(errors.InvalidArgument, + "The secret is already replicated to Region %s", region) + } + + kmsKey := r.KMSKeyID + if kmsKey == "" { + kmsKey = defaultKMSKey + } + + out = append(out, driver.SecretReplicationStatus{ + Region: region, + KMSKeyID: kmsKey, + Status: replicaStatusInSync, + StatusMessage: replicaStatusMessageInSync, + }) + } + + return out, nil +} + +// initialReplicas builds CreateSecret's AddReplicaRegions replica list (nil +// when none were requested). +func (m *Mock) initialReplicas(add []driver.SecretReplicaRegion) ([]driver.SecretReplicationStatus, error) { + if len(add) == 0 { + return nil, nil + } + + return addReplicas(nil, add, m.opts.Region) +} + +// replicaIndex returns the index of region in replicas, or -1. +func replicaIndex(replicas []driver.SecretReplicationStatus, region string) int { + for i := range replicas { + if replicas[i].Region == region { + return i + } + } + + return -1 +} + +// primaryRegion is the secret's home Region: the stored PrimaryRegion, else +// the Region in its ARN, else this mock's Region. Callers must hold sd.mu. +func (m *Mock) primaryRegion(sd *secretData) string { + if sd.info.PrimaryRegion != "" { + return sd.info.PrimaryRegion + } + + return arnRegion(sd.info.ResourceID, m.opts.Region) +} + +// arnRegion returns the region field of a Secrets Manager ARN +// (arn:aws:secretsmanager:::secret:), or fallback when +// the ARN is malformed. +func arnRegion(arn, fallback string) string { + const regionField, minFields = 3, 6 + + parts := strings.Split(arn, ":") + if len(parts) < minFields || parts[regionField] == "" { + return fallback + } + + return parts[regionField] +} + +// activeSecret returns the named secret locked for writing, or the NotFound / +// marked-for-deletion error real Secrets Manager reports. On success the +// caller must unlock sd.mu. +func (m *Mock) activeSecret(name string) (*secretData, error) { + sd, ok := m.secrets.Get(name) + if !ok { + return nil, errors.Newf(errors.NotFound, "secret %q not found", name) + } + + sd.mu.Lock() + + if !sd.deletedAt.IsZero() { + sd.mu.Unlock() + + return nil, errors.New(errors.FailedPrecondition, msgMarkedForDeletion) + } + + return sd, nil +} + +// ReplicateSecretToRegions adds replica Regions to a primary secret and returns +// the secret's metadata, whose ReplicationStatus lists every replica. +// forceOverwrite (ForceOverwriteReplicaSecret) is accepted for API parity; with +// no replica secret materialized in the target Region there is never a +// same-named secret there to overwrite. +func (m *Mock) ReplicateSecretToRegions( + _ context.Context, name string, regions []driver.SecretReplicaRegion, _ bool, +) (*driver.SecretInfo, error) { + sd, err := m.activeSecret(name) + if err != nil { + return nil, err + } + defer sd.mu.Unlock() + + replicas, err := addReplicas(sd.info.ReplicationStatus, regions, m.primaryRegion(sd)) + if err != nil { + return nil, err + } + + sd.info.ReplicationStatus = replicas + sd.info.PrimaryRegion = m.primaryRegion(sd) + sd.info.UpdatedAt = m.opts.Clock.Now().UTC().Format(time.RFC3339) + + info := sd.info + + return &info, nil +} + +// RemoveRegionsFromReplication deletes the named replicas from a primary +// secret and returns the secret's metadata with the remaining replicas. +func (m *Mock) RemoveRegionsFromReplication( + _ context.Context, name string, regions []string, +) (*driver.SecretInfo, error) { + if len(regions) == 0 { + return nil, errors.New(errors.InvalidArgument, "RemoveReplicaRegions must contain at least one Region") + } + + sd, err := m.activeSecret(name) + if err != nil { + return nil, err + } + defer sd.mu.Unlock() + + remaining := make([]driver.SecretReplicationStatus, len(sd.info.ReplicationStatus)) + copy(remaining, sd.info.ReplicationStatus) + + for _, region := range regions { + i := replicaIndex(remaining, region) + if i < 0 { + return nil, errors.Newf(errors.InvalidArgument, + "The secret is not replicated to Region %s", region) + } + + remaining = append(remaining[:i], remaining[i+1:]...) + } + + if len(remaining) == 0 { + remaining = nil + } + + sd.info.ReplicationStatus = remaining + sd.info.UpdatedAt = m.opts.Clock.Now().UTC().Format(time.RFC3339) + + info := sd.info + + return &info, nil +} + +// StopReplicationToReplica promotes a replica secret to a standalone primary. +// It must be called in the replica's Region on the replica secret. Every +// secret this mock holds is a primary in this Region (replicas are not +// materialized; see the package note above), so the call is rejected with +// InvalidRequestException for an existing secret and ResourceNotFoundException +// for a missing one. +func (m *Mock) StopReplicationToReplica(_ context.Context, name string) (*driver.SecretInfo, error) { + sd, err := m.activeSecret(name) + if err != nil { + return nil, err + } + defer sd.mu.Unlock() + + return nil, errors.Newf(errors.FailedPrecondition, + "Secret %s is not a replica secret in Region %s, so replication can't be stopped. "+ + "Call StopReplicationToReplica in the Region of the replica.", sd.info.Name, m.primaryRegion(sd)) +} + +// retrieveVersion decrypts a copy of v for a GetSecretValue-style read and, on +// success, stamps the secret's LastAccessedDate. AWS reports LastAccessedDate +// at date resolution (truncated to midnight UTC). Callers must hold sd.mu for +// writing. +func (m *Mock) retrieveVersion( + ctx context.Context, sd *secretData, v *driver.SecretVersion, +) (*driver.SecretVersion, error) { + out, err := m.decryptVersion(ctx, copyVersion(v)) + if err != nil { + return nil, err + } + + day := m.opts.Clock.Now().UTC().Truncate(24 * time.Hour) //nolint:mnd // one day + sd.info.LastAccessedDate = day.Format(time.RFC3339) + + return out, nil +} diff --git a/providers/aws/secretsmanager/replication_test.go b/providers/aws/secretsmanager/replication_test.go new file mode 100644 index 000000000..1d2b5e4da --- /dev/null +++ b/providers/aws/secretsmanager/replication_test.go @@ -0,0 +1,335 @@ +package secretsmanager + +import ( + "context" + "encoding/json" + "sync" + "testing" + "time" + + "github.com/stackshy/cloudemu/v2/config" + "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/services/secrets/driver" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func newClockedMock() (*Mock, *config.FakeClock) { + fc := config.NewFakeClock(time.Date(2025, 1, 1, 15, 4, 5, 0, time.UTC)) + + return New(config.NewOptions(config.WithClock(fc), config.WithRegion("us-east-1"))), fc +} + +func TestCreateSecretRecordsPrimaryRegionAndReplicas(t *testing.T) { + m := newTestMock() + ctx := context.Background() + + info, err := m.CreateSecret(ctx, driver.SecretConfig{ + Name: "rep", + AddReplicaRegions: []driver.SecretReplicaRegion{ + {Region: "eu-west-3"}, + {Region: "us-west-2", KMSKeyID: "alias/custom"}, + }, + }, []byte("v")) + require.NoError(t, err) + + assert.Equal(t, "us-east-1", info.PrimaryRegion) + require.Len(t, info.ReplicationStatus, 2) + assert.Equal(t, driver.SecretReplicationStatus{ + Region: "eu-west-3", KMSKeyID: "alias/aws/secretsmanager", Status: "InSync", StatusMessage: "Replication succeeded", + }, info.ReplicationStatus[0]) + assert.Equal(t, "alias/custom", info.ReplicationStatus[1].KMSKeyID) + + meta, err := m.SecretMetadata(ctx, "rep") + require.NoError(t, err) + assert.Len(t, meta.ReplicationStatus, 2) +} + +func TestCreateSecretRejectsInvalidReplicaRegions(t *testing.T) { + tests := []struct { + name string + regions []driver.SecretReplicaRegion + }{ + {"primary region", []driver.SecretReplicaRegion{{Region: "us-east-1"}}}, + {"duplicate", []driver.SecretReplicaRegion{{Region: "eu-west-1"}, {Region: "eu-west-1"}}}, + {"empty region", []driver.SecretReplicaRegion{{Region: ""}}}, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + m := newTestMock() + + _, err := m.CreateSecret(context.Background(), + driver.SecretConfig{Name: "s", AddReplicaRegions: tc.regions}, []byte("v")) + require.Error(t, err) + assert.Equal(t, errors.InvalidArgument, errors.GetCode(err)) + + _, getErr := m.GetSecret(context.Background(), "s") + assert.True(t, errors.IsNotFound(getErr), "rejected create must not store the secret") + }) + } +} + +func TestReplicateAndRemoveRegions(t *testing.T) { + m := newTestMock() + ctx := context.Background() + createTestSecret(t, m, "s", "v") + + info, err := m.ReplicateSecretToRegions(ctx, "s", []driver.SecretReplicaRegion{{Region: "eu-west-3"}}, false) + require.NoError(t, err) + require.Len(t, info.ReplicationStatus, 1) + assert.Equal(t, "eu-west-3", info.ReplicationStatus[0].Region) + + _, err = m.ReplicateSecretToRegions(ctx, "s", []driver.SecretReplicaRegion{{Region: "eu-west-3"}}, false) + assert.Equal(t, errors.InvalidArgument, errors.GetCode(err), "re-adding a replica Region is rejected") + + info, err = m.ReplicateSecretToRegions(ctx, "s", []driver.SecretReplicaRegion{{Region: "ap-south-1"}}, true) + require.NoError(t, err) + assert.Len(t, info.ReplicationStatus, 2) + + // A primary with replicas can't be deleted (with or without recovery). + _, _, err = m.DeleteSecretWithOptions(ctx, "s", nil, false) + assert.Equal(t, errors.FailedPrecondition, errors.GetCode(err)) + _, _, err = m.DeleteSecretWithOptions(ctx, "s", nil, true) + assert.Equal(t, errors.FailedPrecondition, errors.GetCode(err)) + + _, err = m.RemoveRegionsFromReplication(ctx, "s", []string{"us-west-1"}) + assert.Equal(t, errors.InvalidArgument, errors.GetCode(err), "removing a non-replica Region is rejected") + + info, err = m.RemoveRegionsFromReplication(ctx, "s", []string{"eu-west-3", "ap-south-1"}) + require.NoError(t, err) + assert.Empty(t, info.ReplicationStatus) + + _, _, err = m.DeleteSecretWithOptions(ctx, "s", nil, false) + require.NoError(t, err) + + _, err = m.ReplicateSecretToRegions(ctx, "s", []driver.SecretReplicaRegion{{Region: "eu-west-3"}}, false) + assert.Equal(t, errors.FailedPrecondition, errors.GetCode(err)) + assert.Equal(t, msgMarkedForDeletion, errors.Message(err)) +} + +func TestReplicationNotFound(t *testing.T) { + m := newTestMock() + ctx := context.Background() + + _, err := m.ReplicateSecretToRegions(ctx, "nope", []driver.SecretReplicaRegion{{Region: "eu-west-3"}}, false) + assert.True(t, errors.IsNotFound(err)) + _, err = m.RemoveRegionsFromReplication(ctx, "nope", []string{"eu-west-3"}) + assert.True(t, errors.IsNotFound(err)) + _, err = m.StopReplicationToReplica(ctx, "nope") + assert.True(t, errors.IsNotFound(err)) +} + +func TestStopReplicationToReplicaRejectsPrimary(t *testing.T) { + m := newTestMock() + createTestSecret(t, m, "s", "v") + + _, err := m.StopReplicationToReplica(context.Background(), "s") + assert.Equal(t, errors.FailedPrecondition, errors.GetCode(err)) +} + +func TestGetSecretValueStampsLastAccessedDateAtDayResolution(t *testing.T) { + m, fc := newClockedMock() + ctx := context.Background() + createTestSecret(t, m, "s", "v") + + info, err := m.SecretMetadata(ctx, "s") + require.NoError(t, err) + assert.Empty(t, info.LastAccessedDate, "never retrieved: no LastAccessedDate") + + _, err = m.GetSecretValueStage(ctx, "s", "", "") + require.NoError(t, err) + + info, err = m.SecretMetadata(ctx, "s") + require.NoError(t, err) + assert.Equal(t, "2025-01-01T00:00:00Z", info.LastAccessedDate) + + fc.Advance(36 * time.Hour) + + _, err = m.GetSecretValue(ctx, "s", "") + require.NoError(t, err) + + info, err = m.SecretMetadata(ctx, "s") + require.NoError(t, err) + assert.Equal(t, "2025-01-03T00:00:00Z", info.LastAccessedDate) +} + +func TestListSecretsWithOptionsIncludePlannedDeletion(t *testing.T) { + m := newTestMock() + ctx := context.Background() + createTestSecret(t, m, "live", "v") + createTestSecret(t, m, "gone", "v") + require.NoError(t, m.DeleteSecret(ctx, "gone")) + + active, err := m.ListSecretsWithOptions(ctx, false) + require.NoError(t, err) + assert.Len(t, active, 1) + + all, err := m.ListSecretsWithOptions(ctx, true) + require.NoError(t, err) + assert.Len(t, all, 2) + + portable, err := m.ListSecrets(ctx) + require.NoError(t, err) + assert.Len(t, portable, 1, "portable ListSecrets still hides scheduled-for-deletion secrets") +} + +func TestScheduledForDeletionMessages(t *testing.T) { + m := newTestMock() + ctx := context.Background() + createTestSecret(t, m, "s", "v") + require.NoError(t, m.DeleteSecret(ctx, "s")) + + _, err := m.GetSecretValue(ctx, "s", "") + assert.Equal(t, "You can't perform this operation on the secret because it was marked for deletion.", + errors.Message(err)) + + _, err = m.CreateSecret(ctx, driver.SecretConfig{Name: "s"}, []byte("v")) + assert.Equal(t, + "You can't create this secret because a secret with this name is already scheduled for deletion.", + errors.Message(err)) +} + +func TestReplicationSurvivesSnapshot(t *testing.T) { + m := newTestMock() + ctx := context.Background() + + _, err := m.CreateSecret(ctx, driver.SecretConfig{ + Name: "s", AddReplicaRegions: []driver.SecretReplicaRegion{{Region: "eu-west-3"}}, + }, []byte("v")) + require.NoError(t, err) + _, err = m.GetSecretValue(ctx, "s", "") + require.NoError(t, err) + + snap, err := m.Snapshot(ctx, false) + require.NoError(t, err) + + restored := newTestMock() + require.NoError(t, restored.Restore(ctx, snap)) + + info, err := restored.SecretMetadata(ctx, "s") + require.NoError(t, err) + assert.Equal(t, "us-east-1", info.PrimaryRegion) + assert.Equal(t, "2025-01-01T00:00:00Z", info.LastAccessedDate) + require.Len(t, info.ReplicationStatus, 1) + assert.Equal(t, "eu-west-3", info.ReplicationStatus[0].Region) +} + +func TestRestoreBackfillsPrimaryRegionFromARN(t *testing.T) { + m := newTestMock() + ctx := context.Background() + + legacy, err := json.Marshal(secretsSnapshot{Secrets: map[string]*secretSnapshot{ + "old": {Info: driver.SecretInfo{ + Name: "old", ResourceID: "arn:aws:secretsmanager:eu-central-1:123456789012:secret:old-AbCdEf", + }}, + }}) + require.NoError(t, err) + require.NoError(t, m.Restore(ctx, legacy)) + + info, err := m.SecretMetadata(ctx, "old") + require.NoError(t, err) + assert.Equal(t, "eu-central-1", info.PrimaryRegion) +} + +func TestReplicaRegionNameValidated(t *testing.T) { + for _, region := range []string{"not-a-region", "eu-west", "EU-WEST-1", "euwest1", "eu-west-1 "} { + t.Run(region, func(t *testing.T) { + m := newTestMock() + ctx := context.Background() + + _, err := m.CreateSecret(ctx, driver.SecretConfig{ + Name: "c", AddReplicaRegions: []driver.SecretReplicaRegion{{Region: region}}, + }, []byte("v")) + require.Error(t, err) + assert.Equal(t, errors.InvalidArgument, errors.GetCode(err)) + + _, getErr := m.GetSecret(ctx, "c") + assert.True(t, errors.IsNotFound(getErr), "rejected create must not store the secret") + + createTestSecret(t, m, "s", "v") + + _, err = m.ReplicateSecretToRegions(ctx, "s", []driver.SecretReplicaRegion{{Region: region}}, false) + assert.Equal(t, errors.InvalidArgument, errors.GetCode(err)) + + meta, err := m.SecretMetadata(ctx, "s") + require.NoError(t, err) + assert.Empty(t, meta.ReplicationStatus) + }) + } +} + +func TestReplicationStampsLastChangedDate(t *testing.T) { + m, fc := newClockedMock() + ctx := context.Background() + createTestSecret(t, m, "s", "v") + + created, err := m.GetSecret(ctx, "s") + require.NoError(t, err) + + fc.Advance(time.Hour) + + info, err := m.ReplicateSecretToRegions(ctx, "s", []driver.SecretReplicaRegion{{Region: "eu-west-3"}}, false) + require.NoError(t, err) + assert.Equal(t, fc.Now().UTC().Format(time.RFC3339), info.UpdatedAt) + assert.NotEqual(t, created.UpdatedAt, info.UpdatedAt) + + fc.Advance(time.Hour) + + info, err = m.RemoveRegionsFromReplication(ctx, "s", []string{"eu-west-3"}) + require.NoError(t, err) + assert.Equal(t, fc.Now().UTC().Format(time.RFC3339), info.UpdatedAt) + + got, err := m.GetSecret(ctx, "s") + require.NoError(t, err) + assert.Equal(t, info.UpdatedAt, got.UpdatedAt) +} + +// TestDeleteAndReplicateAreAtomic races DeleteSecret against +// ReplicateSecretToRegions on the same secret. Whatever the interleaving, a +// secret must never end up both scheduled for deletion (or force-deleted) and +// replicated: exactly one of the two calls may win. +func TestDeleteAndReplicateAreAtomic(t *testing.T) { + ctx := context.Background() + + for _, force := range []bool{false, true} { + for i := 0; i < 500; i++ { + m := newTestMock() + createTestSecret(t, m, "s", "v") + + var ( + wg sync.WaitGroup + delErr, repErr error + start = make(chan struct{}) + ) + + wg.Add(2) + + go func() { + defer wg.Done() + <-start + _, _, delErr = m.DeleteSecretWithOptions(ctx, "s", nil, force) + }() + + go func() { + defer wg.Done() + <-start + _, repErr = m.ReplicateSecretToRegions(ctx, "s", + []driver.SecretReplicaRegion{{Region: "eu-west-3"}}, false) + }() + + close(start) + wg.Wait() + + require.False(t, delErr == nil && repErr == nil, + "force=%v: both DeleteSecret and ReplicateSecretToRegions succeeded", force) + + if sd, ok := m.secrets.Get("s"); ok { + sd.mu.RLock() + bad := !sd.deletedAt.IsZero() && len(sd.info.ReplicationStatus) > 0 + sd.mu.RUnlock() + require.False(t, bad, "secret is scheduled for deletion and replicated") + } + } + } +} diff --git a/providers/aws/secretsmanager/resource_policy.go b/providers/aws/secretsmanager/resource_policy.go index 51824d38c..207399ff2 100644 --- a/providers/aws/secretsmanager/resource_policy.go +++ b/providers/aws/secretsmanager/resource_policy.go @@ -21,8 +21,7 @@ func (m *Mock) PutResourcePolicy(_ context.Context, name, policy string) (*drive defer sd.mu.Unlock() if !sd.deletedAt.IsZero() { - return nil, errors.New(errors.FailedPrecondition, - "secret is scheduled for deletion, so this operation is not allowed") + return nil, errors.New(errors.FailedPrecondition, msgMarkedForDeletion) } sd.resourcePolicy = policy @@ -60,8 +59,7 @@ func (m *Mock) DeleteResourcePolicy(_ context.Context, name string) (*driver.Sec defer sd.mu.Unlock() if !sd.deletedAt.IsZero() { - return nil, errors.New(errors.FailedPrecondition, - "secret is scheduled for deletion, so this operation is not allowed") + return nil, errors.New(errors.FailedPrecondition, msgMarkedForDeletion) } sd.resourcePolicy = "" diff --git a/providers/aws/secretsmanager/secretsmanager.go b/providers/aws/secretsmanager/secretsmanager.go index 851b0a303..f6d639e1a 100644 --- a/providers/aws/secretsmanager/secretsmanager.go +++ b/providers/aws/secretsmanager/secretsmanager.go @@ -65,6 +65,19 @@ const ( defaultRecoveryWindowDays = 30 ) +// msgMarkedForDeletion is real Secrets Manager's InvalidRequestException message +// for an operation on a secret that is scheduled for deletion. AWS documents it +// as the error GetSecretValue writes to CloudTrail for such a secret +// (https://docs.aws.amazon.com/secretsmanager/latest/userguide/monitoring_cloudwatch_deleted-secrets.html +// filters on "*secret because it was marked for deletion*"). +const msgMarkedForDeletion = "You can't perform this operation on the secret because it was marked for deletion." + +// msgCreateScheduledForDeletion is real Secrets Manager's InvalidRequestException +// message for CreateSecret reusing the name of a secret scheduled for deletion +// (quoted in https://repost.aws/knowledge-center/delete-secrets-manager-secret). +const msgCreateScheduledForDeletion = "You can't create this secret because a secret with this name is already " + + "scheduled for deletion." + // defaultKMSKey is the AWS-managed key Secrets Manager uses to encrypt a secret // whose CreateSecret request omitted KmsKeyId. const defaultKMSKey = "alias/aws/secretsmanager" @@ -178,17 +191,21 @@ func (m *Mock) CreateSecret(ctx context.Context, cfg driver.SecretConfig, value // Manager rejects an unknown key with InvalidParameterException rather than // storing a dangling reference. AWS-managed aliases such as // alias/aws/secretsmanager always exist, so they skip the check. - if _, managed := kmscrypto.ReservedAlias(cfg.KMSKeyID); cfg.KMSKeyID != "" && !managed && m.kmsCrypto != nil { - if _, err := m.kmsCrypto.DescribeKey(ctx, cfg.KMSKeyID); err != nil { - return nil, errors.Newf(errors.InvalidArgument, - "KMS key %q does not exist or is not accessible", cfg.KMSKeyID) - } + if err := m.validateKMSKey(ctx, cfg.KMSKeyID); err != nil { + return nil, err } if existing, ok := m.secrets.Get(cfg.Name); ok { return m.createSecretConflict(ctx, existing, cfg, value) } + // AddReplicaRegions is validated before anything is stored, so a bad replica + // list leaves no half-created secret behind. + replicas, err := m.initialReplicas(cfg.AddReplicaRegions) + if err != nil { + return nil, err + } + now := m.opts.Clock.Now().UTC().Format(time.RFC3339) // The ARN resource segment ends with a fresh random 6-char suffix // (:secret:-), matching real Secrets Manager: a secret deleted @@ -212,6 +229,9 @@ func (m *Mock) CreateSecret(ctx context.Context, cfg driver.SecretConfig, value UpdatedAt: now, Tags: tags, KMSKeyID: cfg.KMSKeyID, + // The secret's home Region; replicas (if any) are listed separately. + PrimaryRegion: m.opts.Region, + ReplicationStatus: replicas, } sd := &secretData{info: info, stages: map[string]string{}} @@ -237,6 +257,20 @@ func (m *Mock) CreateSecret(ctx context.Context, cfg driver.SecretConfig, value return &result, nil } +// validateKMSKey checks that a customer-supplied KmsKeyId names a key that +// exists. Empty ids, AWS-managed aliases and a mock without KMS wired pass. +func (m *Mock) validateKMSKey(ctx context.Context, keyID string) error { + if _, managed := kmscrypto.ReservedAlias(keyID); keyID == "" || managed || m.kmsCrypto == nil { + return nil + } + + if _, err := m.kmsCrypto.DescribeKey(ctx, keyID); err != nil { + return errors.Newf(errors.InvalidArgument, "KMS key %q does not exist or is not accessible", keyID) + } + + return nil +} + // seedInitialVersion appends the CreateSecret initial version to a freshly built // secret and gives it the AWSCURRENT label. The version id is the caller's // ClientRequestToken when set (AWS reuses it as the version id), else a fresh @@ -297,8 +331,7 @@ func (m *Mock) createSecretConflict( existing.mu.Unlock() if scheduledForDeletion { - return nil, errors.New(errors.FailedPrecondition, - "a secret with this name is already scheduled for deletion") + return nil, errors.New(errors.FailedPrecondition, msgCreateScheduledForDeletion) } return nil, errors.Newf(errors.AlreadyExists, "secret %q already exists", cfg.Name) @@ -368,22 +401,38 @@ func (m *Mock) DeleteSecretWithOptions( now := m.opts.Clock.Now().UTC() + // The replica check and the delete happen under one write lock, so a + // concurrent ReplicateSecretToRegions (which also takes sd.mu and refuses a + // secret marked for deletion) can't slip in between them. + sd.mu.Lock() + + // Real Secrets Manager refuses to delete a primary secret that still has + // replicas: they must be removed with RemoveRegionsFromReplication first + // (https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_DeleteSecret.html). + if len(sd.info.ReplicationStatus) > 0 { + sd.mu.Unlock() + + return nil, "", errors.New(errors.FailedPrecondition, + "You can't delete a primary secret that is replicated to other Regions. "+ + "Remove the replicas with RemoveRegionsFromReplication first.") + } + if force { - sd.mu.RLock() + // Mark the secret deleted before releasing the lock so any call that + // grabs it before the store removal below sees it as gone. + sd.deletedAt = now info := sd.info - sd.mu.RUnlock() + sd.mu.Unlock() m.secrets.Delete(name) return &info, now.Format(time.RFC3339), nil } - sd.mu.Lock() defer sd.mu.Unlock() if !sd.deletedAt.IsZero() { - return nil, "", errors.Newf(errors.FailedPrecondition, - "You can't perform this operation on the secret because it was marked for deletion.") + return nil, "", errors.New(errors.FailedPrecondition, msgMarkedForDeletion) } sd.deletedAt = now @@ -404,8 +453,7 @@ func (m *Mock) GetSecret(_ context.Context, name string) (*driver.SecretInfo, er defer sd.mu.RUnlock() if !sd.deletedAt.IsZero() { - return nil, errors.New(errors.FailedPrecondition, - "secret is scheduled for deletion, so this operation is not allowed") + return nil, errors.New(errors.FailedPrecondition, msgMarkedForDeletion) } result := sd.info @@ -414,14 +462,21 @@ func (m *Mock) GetSecret(_ context.Context, name string) (*driver.SecretInfo, er } // ListSecrets lists all secrets, excluding soft-deleted ones. -func (m *Mock) ListSecrets(_ context.Context) ([]driver.SecretInfo, error) { +func (m *Mock) ListSecrets(ctx context.Context) ([]driver.SecretInfo, error) { + return m.ListSecretsWithOptions(ctx, false) +} + +// ListSecretsWithOptions is the AWS ListSecrets surface: secrets scheduled for +// deletion are excluded unless includePlannedDeletion is set, matching the +// IncludePlannedDeletion request flag. +func (m *Mock) ListSecretsWithOptions(_ context.Context, includePlannedDeletion bool) ([]driver.SecretInfo, error) { all := m.secrets.All() secrets := make([]driver.SecretInfo, 0, len(all)) for _, sd := range all { sd.mu.RLock() - if sd.deletedAt.IsZero() { + if includePlannedDeletion || sd.deletedAt.IsZero() { secrets = append(secrets, sd.info) } sd.mu.RUnlock() @@ -458,8 +513,7 @@ func (m *Mock) PutSecretValueStaged( defer sd.mu.Unlock() if !sd.deletedAt.IsZero() { - return nil, errors.New(errors.FailedPrecondition, - "secret is scheduled for deletion, so this operation is not allowed") + return nil, errors.New(errors.FailedPrecondition, msgMarkedForDeletion) } if clientRequestToken != "" { @@ -521,18 +575,17 @@ func (m *Mock) GetSecretValue(ctx context.Context, name, versionID string) (*dri return nil, errors.Newf(errors.NotFound, "secret %q not found", name) } - sd.mu.RLock() - defer sd.mu.RUnlock() + sd.mu.Lock() + defer sd.mu.Unlock() if !sd.deletedAt.IsZero() { - return nil, errors.New(errors.FailedPrecondition, - "secret is scheduled for deletion, so this operation is not allowed") + return nil, errors.New(errors.FailedPrecondition, msgMarkedForDeletion) } for i := range sd.versions { v := &sd.versions[i] if (versionID == "" && v.Current) || v.VersionID == versionID { - return m.decryptVersion(ctx, copyVersion(v)) + return m.retrieveVersion(ctx, sd, v) } } @@ -550,8 +603,7 @@ func (m *Mock) ListSecretVersions(_ context.Context, name string) ([]driver.Secr defer sd.mu.RUnlock() if !sd.deletedAt.IsZero() { - return nil, errors.New(errors.FailedPrecondition, - "secret is scheduled for deletion, so this operation is not allowed") + return nil, errors.New(errors.FailedPrecondition, msgMarkedForDeletion) } versions := make([]driver.SecretVersion, len(sd.versions)) diff --git a/providers/aws/secretsmanager/snapshot.go b/providers/aws/secretsmanager/snapshot.go index 99bdb1248..300d8df9e 100644 --- a/providers/aws/secretsmanager/snapshot.go +++ b/providers/aws/secretsmanager/snapshot.go @@ -75,6 +75,12 @@ func (m *Mock) Restore(_ context.Context, data json.RawMessage) error { } for name, ss := range snap.Secrets { + // Snapshots taken before PrimaryRegion was tracked recover it from the + // secret's ARN. LastAccessedDate and ReplicationStatus ride in Info. + if ss.Info.PrimaryRegion == "" { + ss.Info.PrimaryRegion = arnRegion(ss.Info.ResourceID, m.opts.Region) + } + m.secrets.Set(name, &secretData{ info: ss.Info, versions: ss.Versions, diff --git a/providers/aws/secretsmanager/staging.go b/providers/aws/secretsmanager/staging.go index 4471c9a5e..cd3cf0d00 100644 --- a/providers/aws/secretsmanager/staging.go +++ b/providers/aws/secretsmanager/staging.go @@ -144,17 +144,16 @@ func (m *Mock) GetSecretValueStage(ctx context.Context, name, versionID, stage s return nil, errors.Newf(errors.NotFound, "secret %q not found", name) } - sd.mu.RLock() - defer sd.mu.RUnlock() + sd.mu.Lock() + defer sd.mu.Unlock() if !sd.deletedAt.IsZero() { - return nil, errors.New(errors.FailedPrecondition, - "secret is scheduled for deletion, so this operation is not allowed") + return nil, errors.New(errors.FailedPrecondition, msgMarkedForDeletion) } if versionID != "" { if v, found := sd.versionByID(versionID); found { - return m.decryptVersion(ctx, copyVersion(v)) + return m.retrieveVersion(ctx, sd, v) } return nil, errors.Newf(errors.NotFound, "version %q not found for secret %q", versionID, name) @@ -175,7 +174,7 @@ func (m *Mock) GetSecretValueStage(ctx context.Context, name, versionID, stage s return nil, errors.Newf(errors.NotFound, "stage %q not found for secret %q", label, name) } - return m.decryptVersion(ctx, copyVersion(v)) + return m.retrieveVersion(ctx, sd, v) } // SecretVersionStages returns the stage labels for each version ID, so @@ -227,8 +226,7 @@ func (m *Mock) UpdateSecretVersionStage( defer sd.mu.Unlock() if !sd.deletedAt.IsZero() { - return nil, errors.New(errors.FailedPrecondition, - "secret is scheduled for deletion, so this operation is not allowed") + return nil, errors.New(errors.FailedPrecondition, msgMarkedForDeletion) } if err := sd.validateStageMove(versionStage, removeFrom, moveTo); err != nil { @@ -361,8 +359,7 @@ func (m *Mock) RotateSecret( defer sd.mu.Unlock() if !sd.deletedAt.IsZero() { - return nil, errors.New(errors.FailedPrecondition, - "secret is scheduled for deletion, so this operation is not allowed") + return nil, errors.New(errors.FailedPrecondition, msgMarkedForDeletion) } // A secret that has never had a rotation Lambda configured, and whose @@ -442,8 +439,7 @@ func (m *Mock) CancelRotateSecret(_ context.Context, name string) (*driver.Secre defer sd.mu.Unlock() if !sd.deletedAt.IsZero() { - return nil, "", errors.New(errors.FailedPrecondition, - "secret is scheduled for deletion, so this operation is not allowed") + return nil, "", errors.New(errors.FailedPrecondition, msgMarkedForDeletion) } sd.rotationEnabled = false diff --git a/providers/aws/secretsmanager/update.go b/providers/aws/secretsmanager/update.go index a1aed3856..e8c09cb1e 100644 --- a/providers/aws/secretsmanager/update.go +++ b/providers/aws/secretsmanager/update.go @@ -24,8 +24,7 @@ func (m *Mock) UpdateSecret(ctx context.Context, name, description string, value defer sd.mu.Unlock() if !sd.deletedAt.IsZero() { - return nil, "", errors.New(errors.FailedPrecondition, - "secret is scheduled for deletion, so this operation is not allowed") + return nil, "", errors.New(errors.FailedPrecondition, msgMarkedForDeletion) } now := m.opts.Clock.Now().UTC().Format(time.RFC3339) diff --git a/server/aws/secretsmanager/batch.go b/server/aws/secretsmanager/batch.go index 3b58289c8..0bb732f67 100644 --- a/server/aws/secretsmanager/batch.go +++ b/server/aws/secretsmanager/batch.go @@ -19,10 +19,7 @@ func (h *Handler) batchGetSecretValue(w http.ResponseWriter, r *http.Request) { return } - if len(req.SecretIDList) == 0 && len(req.Filters) == 0 { - wire.WriteJSONError(w, http.StatusBadRequest, - "InvalidParameterException", "either SecretIdList or Filters must be provided") - + if !validateBatchRequest(w, &req) { return } @@ -59,6 +56,26 @@ func (h *Handler) batchGetSecretValue(w http.ResponseWriter, r *http.Request) { wire.WriteJSON(w, batchGetSecretValueResponse{SecretValues: values, Errors: errs, NextToken: next}) } +// validateBatchRequest rejects a BatchGetSecretValue request with neither +// SecretIdList nor Filters (InvalidParameterException) or with a malformed +// filter (ValidationException), writing the error. It reports whether the +// request is valid. +func validateBatchRequest(w http.ResponseWriter, req *batchGetSecretValueRequest) bool { + if len(req.SecretIDList) == 0 && len(req.Filters) == 0 { + wire.WriteJSONError(w, http.StatusBadRequest, + "InvalidParameterException", "either SecretIdList or Filters must be provided") + + return false + } + + if err := validateFilters(req.Filters); err != nil { + writeValidation(w, err) + return false + } + + return true +} + // filteredSecretIDs returns the names of secrets matching the BatchGetSecretValue // filters, in the stable (by-name) order the offset NextToken relies on. func (h *Handler) filteredSecretIDs(r *http.Request, filters []secretFilter) ([]string, error) { diff --git a/server/aws/secretsmanager/handler.go b/server/aws/secretsmanager/handler.go index 64ad37402..4fcf72f76 100644 --- a/server/aws/secretsmanager/handler.go +++ b/server/aws/secretsmanager/handler.go @@ -65,6 +65,18 @@ type secretPolicyManager interface { DeleteResourcePolicy(ctx context.Context, name string) (*secretsdriver.SecretInfo, error) } +// secretReplicator is the AWS-specific multi-Region replication and +// ListSecrets-options surface the handler type-asserts for (implemented by the +// AWS provider), keeping the portable Secrets driver minimal. +type secretReplicator interface { + ListSecretsWithOptions(ctx context.Context, includePlannedDeletion bool) ([]secretsdriver.SecretInfo, error) + ReplicateSecretToRegions( + ctx context.Context, name string, regions []secretsdriver.SecretReplicaRegion, forceOverwrite bool, + ) (*secretsdriver.SecretInfo, error) + RemoveRegionsFromReplication(ctx context.Context, name string, regions []string) (*secretsdriver.SecretInfo, error) + StopReplicationToReplica(ctx context.Context, name string) (*secretsdriver.SecretInfo, error) +} + // Handler serves Secrets Manager JSON-RPC requests against a Secrets driver. type Handler struct { secrets secretsdriver.Secrets @@ -75,26 +87,29 @@ type Handler struct { func New(s secretsdriver.Secrets) *Handler { h := &Handler{secrets: s} h.routes = map[string]http.HandlerFunc{ - "CreateSecret": h.createSecret, - "DeleteSecret": h.deleteSecret, - "DescribeSecret": h.describeSecret, - "GetResourcePolicy": h.getResourcePolicy, - "PutResourcePolicy": h.putResourcePolicy, - "DeleteResourcePolicy": h.deleteResourcePolicy, - "ValidateResourcePolicy": h.validateResourcePolicy, - "ListSecrets": h.listSecrets, - "GetSecretValue": h.getSecretValue, - "BatchGetSecretValue": h.batchGetSecretValue, - "PutSecretValue": h.putSecretValue, - "ListSecretVersionIds": h.listSecretVersionIDs, - "UpdateSecret": h.updateSecret, - "UpdateSecretVersionStage": h.updateSecretVersionStage, - "RestoreSecret": h.restoreSecret, - "RotateSecret": h.rotateSecret, - "CancelRotateSecret": h.cancelRotateSecret, - "GetRandomPassword": h.getRandomPassword, - "TagResource": h.tagResource, - "UntagResource": h.untagResource, + "CreateSecret": h.createSecret, + "DeleteSecret": h.deleteSecret, + "DescribeSecret": h.describeSecret, + "GetResourcePolicy": h.getResourcePolicy, + "PutResourcePolicy": h.putResourcePolicy, + "DeleteResourcePolicy": h.deleteResourcePolicy, + "ValidateResourcePolicy": h.validateResourcePolicy, + "ListSecrets": h.listSecrets, + "GetSecretValue": h.getSecretValue, + "BatchGetSecretValue": h.batchGetSecretValue, + "PutSecretValue": h.putSecretValue, + "ListSecretVersionIds": h.listSecretVersionIDs, + "UpdateSecret": h.updateSecret, + "UpdateSecretVersionStage": h.updateSecretVersionStage, + "RestoreSecret": h.restoreSecret, + "ReplicateSecretToRegions": h.replicateSecretToRegions, + "RemoveRegionsFromReplication": h.removeRegionsFromReplication, + "StopReplicationToReplica": h.stopReplicationToReplica, + "RotateSecret": h.rotateSecret, + "CancelRotateSecret": h.cancelRotateSecret, + "GetRandomPassword": h.getRandomPassword, + "TagResource": h.tagResource, + "UntagResource": h.untagResource, } return h diff --git a/server/aws/secretsmanager/helpers.go b/server/aws/secretsmanager/helpers.go index f928b0f82..7ee6c92b1 100644 --- a/server/aws/secretsmanager/helpers.go +++ b/server/aws/secretsmanager/helpers.go @@ -76,9 +76,10 @@ func decodePageToken(token string) (int, error) { // matchesSecretFilters reports whether a secret satisfies every ListSecrets // filter (filters are AND'd, values within a filter OR'd). Supported keys are -// name, description, tag-key, tag-value, and all. Matching mirrors the real -// service: name/tag-key/tag-value are case-sensitive prefix matches, description -// is a case-insensitive prefix match, and all tokenizes the value into words and +// name, description, tag-key, tag-value, primary-region, owning-service, and +// all. Matching mirrors the real service: name/tag-key/tag-value/primary-region/ +// owning-service are case-sensitive prefix matches, description is a +// case-insensitive prefix match, and all tokenizes the value into words and // searches every attribute case-insensitively. func matchesSecretFilters(info *secretsdriver.SecretInfo, filters []secretFilter) bool { for i := range filters { @@ -96,6 +97,16 @@ func matchesSecretFilter(info *secretsdriver.SecretInfo, f *secretFilter) bool { } for _, v := range f.Values { + // A value prefixed with "!" is a negation filter: it matches the secrets + // the rest of the value does not match. + if rest, negated := strings.CutPrefix(v, "!"); negated { + if !secretFieldMatches(info, f.Key, rest) { + return true + } + + continue + } + if secretFieldMatches(info, f.Key, v) { return true } @@ -114,11 +125,19 @@ func secretFieldMatches(info *secretsdriver.SecretInfo, key, value string) bool return anyTagHasPrefix(info.Tags, value, true) case "tag-value": return anyTagHasPrefix(info.Tags, value, false) + case "primary-region": + return strings.HasPrefix(info.PrimaryRegion, value) + case "owning-service": + // OwningService is set only on secrets another AWS service creates and + // manages; cloudemu never creates such secrets, so every secret's owning + // service is empty and only an empty prefix matches. + return value == "" case "all": return matchesAllWords(info, value) default: - // Unsupported key: do not exclude on it. - return true + // Unreachable for a validated request: unknown keys are rejected with + // ValidationException before matching. + return false } } diff --git a/server/aws/secretsmanager/list.go b/server/aws/secretsmanager/list.go new file mode 100644 index 000000000..e3cb36b03 --- /dev/null +++ b/server/aws/secretsmanager/list.go @@ -0,0 +1,240 @@ +package secretsmanager + +import ( + "cmp" + "encoding/base64" + "encoding/json" + "errors" + "fmt" + "net/http" + "slices" + "sort" + "strings" + + "github.com/stackshy/cloudemu/v2/server/wire" + secretsdriver "github.com/stackshy/cloudemu/v2/services/secrets/driver" +) + +// ListSecrets / BatchGetSecretValue input enums, from the Secrets Manager API +// model (FilterNameStringType, SortByType, SortOrderType). +const ( + filterDescription = "description" + filterName = "name" + filterTagKey = "tag-key" + filterTagValue = "tag-value" + filterPrimaryRegion = "primary-region" + filterOwningService = "owning-service" + filterAll = "all" + + sortByCreatedDate = "created-date" + sortByLastAccessedDate = "last-accessed-date" + sortByLastChangedDate = "last-changed-date" + sortByName = "name" + + sortOrderAsc = "asc" + sortOrderDesc = "desc" + + // listSecretsMaxResults is ListSecrets' documented MaxResults upper bound + // (valid range 1-100). + listSecretsMaxResults = 100 +) + +//nolint:gochecknoglobals // fixed enum value sets from the API model. +var ( + filterKeys = []string{ + filterDescription, filterName, filterTagKey, filterTagValue, + filterPrimaryRegion, filterOwningService, filterAll, + } + sortByValues = []string{sortByCreatedDate, sortByLastAccessedDate, sortByLastChangedDate, sortByName} + sortOrderValues = []string{sortOrderAsc, sortOrderDesc} +) + +// errValidation is the wire-level ValidationException ("The input fails to +// satisfy the constraints specified by the service", HTTP 400) Secrets Manager +// lists among its common errors, raised here for model constraint violations +// such as a value outside an enum. +type errValidation string + +func (e errValidation) Error() string { return string(e) } + +// enumViolation formats the model-constraint message for a value outside an +// enum, in the "1 validation error detected" shape AWS JSON services use. +func enumViolation(value, field string, allowed []string) errValidation { + return errValidation(fmt.Sprintf( + "1 validation error detected: Value '%s' at '%s' failed to satisfy constraint: "+ + "Member must satisfy enum value set: [%s]", value, field, strings.Join(allowed, ", "))) +} + +// validateFilters rejects a filter Key outside FilterNameStringType, so a typo +// such as "Name" or "primary_region" fails instead of silently matching every +// secret. +func validateFilters(filters []secretFilter) error { + for i := range filters { + if !slices.Contains(filterKeys, filters[i].Key) { + return enumViolation(filters[i].Key, fmt.Sprintf("filters.%d.member.key", i+1), filterKeys) + } + } + + return nil +} + +// validateListSecrets checks the ListSecrets inputs that are constrained by the +// model: the filter keys, SortBy, SortOrder and the MaxResults range. +func validateListSecrets(req *listSecretsRequest) error { + if err := validateFilters(req.Filters); err != nil { + return err + } + + if req.SortBy != "" && !slices.Contains(sortByValues, req.SortBy) { + return enumViolation(req.SortBy, "sortBy", sortByValues) + } + + if req.SortOrder != "" && !slices.Contains(sortOrderValues, req.SortOrder) { + return enumViolation(req.SortOrder, "sortOrder", sortOrderValues) + } + + if req.MaxResults != nil && (*req.MaxResults < 1 || *req.MaxResults > listSecretsMaxResults) { + return errValidation(fmt.Sprintf( + "1 validation error detected: Value '%d' at 'maxResults' failed to satisfy constraint: "+ + "Member must have value between 1 and %d", *req.MaxResults, listSecretsMaxResults)) + } + + return nil +} + +// writeValidation writes a ValidationException. +func writeValidation(w http.ResponseWriter, err error) { + wire.WriteJSONError(w, http.StatusBadRequest, "ValidationException", err.Error()) +} + +// secretOrder returns the strict total order a ListSecrets result is listed in. +// With neither SortBy nor SortOrder supplied it is name ascending (the order +// cloudemu has always used; AWS documents CreatedDate as the default key but +// not its direction). Otherwise SortBy picks the key (CreatedDate when omitted, +// as AWS documents) and SortOrder the direction (ascending unless "desc"). +// Ties on the key fall back to name ascending, and names are unique, so the +// order is total and a cursor NextToken can resume after any entry. +func secretOrder(sortBy, sortOrder string) func(a, b *secretsdriver.SecretInfo) int { + if sortBy == "" && sortOrder == "" { + sortBy = sortByName + } + + key := sortKey(sortBy) + sign := 1 + + if sortOrder == sortOrderDesc { + sign = -1 + } + + return func(a, b *secretsdriver.SecretInfo) int { + if c := key(a, b); c != 0 { + return sign * c + } + + return cmp.Compare(a.Name, b.Name) + } +} + +// sortKey returns a three-way comparator for a ListSecrets SortBy value. +func sortKey(sortBy string) func(a, b *secretsdriver.SecretInfo) int { + switch sortBy { + case sortByName: + return func(a, b *secretsdriver.SecretInfo) int { return cmp.Compare(a.Name, b.Name) } + case sortByLastChangedDate: + return func(a, b *secretsdriver.SecretInfo) int { + return cmp.Compare(epochSeconds(a.UpdatedAt), epochSeconds(b.UpdatedAt)) + } + case sortByLastAccessedDate: + return func(a, b *secretsdriver.SecretInfo) int { + return cmp.Compare(epochSeconds(a.LastAccessedDate), epochSeconds(b.LastAccessedDate)) + } + default: // sortByCreatedDate + return func(a, b *secretsdriver.SecretInfo) int { + return cmp.Compare(epochSeconds(a.CreatedAt), epochSeconds(b.CreatedAt)) + } + } +} + +// listCursor is the ListSecrets NextToken payload: the sort-relevant fields of +// the last entry returned, so the next page resumes strictly after it. Unlike +// an offset, a cursor doesn't skip or repeat entries when secrets are created +// or deleted between page calls. +type listCursor struct { + Name string `json:"n"` + CreatedAt string `json:"c,omitempty"` + UpdatedAt string `json:"u,omitempty"` + LastAccessed string `json:"a,omitempty"` +} + +func encodeListCursor(info *secretsdriver.SecretInfo) string { + b, _ := json.Marshal(listCursor{ + Name: info.Name, CreatedAt: info.CreatedAt, UpdatedAt: info.UpdatedAt, LastAccessed: info.LastAccessedDate, + }) + + return base64.RawURLEncoding.EncodeToString(b) +} + +// errInvalidNextToken marks a NextToken that doesn't decode to a cursor; the +// handler answers it with ListSecrets' documented InvalidNextTokenException. +var errInvalidNextToken = errors.New("invalid NextToken") + +// msgInvalidNextToken is the InvalidNextTokenException description from the +// Secrets Manager common errors. +const msgInvalidNextToken = "The NextToken value is invalid." + +func decodeListCursor(token string) (*secretsdriver.SecretInfo, error) { + b, err := base64.RawURLEncoding.DecodeString(token) + if err != nil { + return nil, errInvalidNextToken + } + + var c listCursor + if err := json.Unmarshal(b, &c); err != nil || c.Name == "" { + return nil, errInvalidNextToken + } + + return &secretsdriver.SecretInfo{ + Name: c.Name, CreatedAt: c.CreatedAt, UpdatedAt: c.UpdatedAt, LastAccessedDate: c.LastAccessed, + }, nil +} + +// listSecretsPage orders infos, then returns the page that starts after the +// NextToken cursor (the first page when token is empty) and the token for the +// following page ("" on the last page). maxResults nil returns everything left. +func listSecretsPage( + infos []secretsdriver.SecretInfo, req *listSecretsRequest, +) ([]secretsdriver.SecretInfo, string, error) { + order := secretOrder(req.SortBy, req.SortOrder) + + sort.Slice(infos, func(i, j int) bool { return order(&infos[i], &infos[j]) < 0 }) + + start := 0 + + if req.NextToken != "" { + after, err := decodeListCursor(req.NextToken) + if err != nil { + return nil, "", err + } + + start = len(infos) + + for i := range infos { + if order(after, &infos[i]) < 0 { + start = i + break + } + } + } + + end := len(infos) + if req.MaxResults != nil && start+int(*req.MaxResults) < end { + end = start + int(*req.MaxResults) + } + + page := infos[start:end] + if end == len(infos) || len(page) == 0 { + return page, "", nil + } + + return page, encodeListCursor(&page[len(page)-1]), nil +} diff --git a/server/aws/secretsmanager/list_test.go b/server/aws/secretsmanager/list_test.go new file mode 100644 index 000000000..0786b6848 --- /dev/null +++ b/server/aws/secretsmanager/list_test.go @@ -0,0 +1,156 @@ +package secretsmanager_test + +import ( + "context" + "errors" + "testing" + + "github.com/aws/aws-sdk-go-v2/aws" + awssm "github.com/aws/aws-sdk-go-v2/service/secretsmanager" + smtypes "github.com/aws/aws-sdk-go-v2/service/secretsmanager/types" +) + +func TestSDKListSecretsRejectsInvalidInput(t *testing.T) { + client, _ := newClockedSecretsClient(t) + ctx := context.Background() + + mustCreate(t, client, &awssm.CreateSecretInput{Name: aws.String("s"), SecretString: aws.String("x")}) + + invalid := []struct { + name string + in awssm.ListSecretsInput + }{ + {"unknown filter key", awssm.ListSecretsInput{ + Filters: []smtypes.Filter{{Key: "primary_region", Values: []string{"us"}}}, + }}, + {"unknown SortBy", awssm.ListSecretsInput{SortBy: "size"}}, + {"unknown SortOrder", awssm.ListSecretsInput{SortOrder: "up"}}, + {"MaxResults 0", awssm.ListSecretsInput{MaxResults: aws.Int32(0)}}, + {"MaxResults 101", awssm.ListSecretsInput{MaxResults: aws.Int32(101)}}, + } + + for _, tc := range invalid { + in := tc.in + + _, err := client.ListSecrets(ctx, &in) + + var apiErr interface{ ErrorCode() string } + if !errors.As(err, &apiErr) || apiErr.ErrorCode() != "ValidationException" { + t.Fatalf("%s: err = %v, want ValidationException", tc.name, err) + } + } + + var bad *smtypes.InvalidNextTokenException + if _, err := client.ListSecrets(ctx, &awssm.ListSecretsInput{NextToken: aws.String("not-a-token!")}); !errors.As(err, &bad) { + t.Fatalf("bad NextToken err = %v, want InvalidNextTokenException", err) + } + + _, err := client.BatchGetSecretValue(ctx, &awssm.BatchGetSecretValueInput{ + Filters: []smtypes.Filter{{Key: "bogus", Values: []string{"s"}}}, + }) + + var apiErr interface{ ErrorCode() string } + if !errors.As(err, &apiErr) || apiErr.ErrorCode() != "ValidationException" { + t.Fatalf("BatchGetSecretValue unknown filter key err = %v, want ValidationException", err) + } +} + +func TestSDKListSecretsNegationFilter(t *testing.T) { + client, _ := newClockedSecretsClient(t) + ctx := context.Background() + + for _, n := range []string{"prod-db", "dev-db"} { + mustCreate(t, client, &awssm.CreateSecretInput{Name: aws.String(n), SecretString: aws.String("x")}) + } + + out, err := client.ListSecrets(ctx, &awssm.ListSecretsInput{ + Filters: []smtypes.Filter{{Key: smtypes.FilterNameStringTypeName, Values: []string{"!prod"}}}, + }) + if err != nil { + t.Fatalf("ListSecrets: %v", err) + } + + if got := secretNames(out.SecretList); len(got) != 1 || got[0] != "dev-db" { + t.Fatalf("name=!prod returned %v, want [dev-db]", got) + } + + // Values in one filter are OR'd, negated ones included (AWS documents no + // other combination, and moto behaves the same): every name fails at least + // one of the two prefixes, so both secrets match. Excluding both prefixes + // takes two filters, which are AND'd. + out, err = client.ListSecrets(ctx, &awssm.ListSecretsInput{ + Filters: []smtypes.Filter{{Key: smtypes.FilterNameStringTypeName, Values: []string{"!prod", "!dev"}}}, + }) + if err != nil { + t.Fatalf("ListSecrets: %v", err) + } + + if len(out.SecretList) != 2 { + t.Fatalf("name=[!prod,!dev] returned %v, want both secrets", secretNames(out.SecretList)) + } + + out, err = client.ListSecrets(ctx, &awssm.ListSecretsInput{Filters: []smtypes.Filter{ + {Key: smtypes.FilterNameStringTypeName, Values: []string{"!prod"}}, + {Key: smtypes.FilterNameStringTypeName, Values: []string{"!dev"}}, + }}) + if err != nil { + t.Fatalf("ListSecrets: %v", err) + } + + if len(out.SecretList) != 0 { + t.Fatalf("name=!prod AND name=!dev returned %v, want none", secretNames(out.SecretList)) + } + + // No secret here is owned by another service, so "!rds" keeps them all. + out, err = client.ListSecrets(ctx, &awssm.ListSecretsInput{ + Filters: []smtypes.Filter{{Key: smtypes.FilterNameStringTypeOwningService, Values: []string{"!rds"}}}, + }) + if err != nil { + t.Fatalf("ListSecrets: %v", err) + } + + if len(out.SecretList) != 2 { + t.Fatalf("owning-service=!rds returned %v, want both secrets", secretNames(out.SecretList)) + } +} + +// TestSDKListSecretsCursorStableAcrossInserts checks the NextToken is a cursor: +// a secret created between page calls that sorts before the cursor neither +// shifts the next page nor makes an entry repeat. +func TestSDKListSecretsCursorStableAcrossInserts(t *testing.T) { + client, _ := newClockedSecretsClient(t) + ctx := context.Background() + + for _, n := range []string{"b", "d", "f", "h"} { + mustCreate(t, client, &awssm.CreateSecretInput{Name: aws.String(n), SecretString: aws.String("x")}) + } + + page1, err := client.ListSecrets(ctx, &awssm.ListSecretsInput{MaxResults: aws.Int32(2)}) + if err != nil { + t.Fatalf("page1: %v", err) + } + + mustCreate(t, client, &awssm.CreateSecretInput{Name: aws.String("a"), SecretString: aws.String("x")}) + + page2, err := client.ListSecrets(ctx, &awssm.ListSecretsInput{MaxResults: aws.Int32(2), NextToken: page1.NextToken}) + if err != nil { + t.Fatalf("page2: %v", err) + } + + got := append(secretNames(page1.SecretList), secretNames(page2.SecretList)...) + want := []string{"b", "d", "f", "h"} + + if len(got) != len(want) { + t.Fatalf("pages = %v, want %v", got, want) + } + + for i := range want { + if got[i] != want[i] { + t.Fatalf("pages = %v, want %v", got, want) + } + } + + if page2.NextToken != nil { + t.Fatalf("last page NextToken = %q, want none", aws.ToString(page2.NextToken)) + } +} diff --git a/server/aws/secretsmanager/operations.go b/server/aws/secretsmanager/operations.go index 5da1625cd..9157c51db 100644 --- a/server/aws/secretsmanager/operations.go +++ b/server/aws/secretsmanager/operations.go @@ -21,21 +21,26 @@ func (h *Handler) createSecret(w http.ResponseWriter, r *http.Request) { Tags: tagsToMap(req.Tags), KMSKeyID: req.KmsKeyID, ClientRequestToken: req.ClientRequestToken, + AddReplicaRegions: replicaRegionsToDriver(req.AddReplicaRegions), }, secretValue(req.SecretString, req.SecretBinary)) if err != nil { writeErr(w, err) return } - // The driver seeds the initial version internally; fetch it so the + // The driver seeds the initial version internally; look it up so the // response carries the VersionId real Secrets Manager returns. out := createSecretResponse{ARN: info.ResourceID, Name: info.Name} - if ver, verr := h.secrets.GetSecretValue(r.Context(), info.Name, ""); verr == nil { - out.VersionID = ver.VersionID + if len(info.ReplicationStatus) > 0 { + out.ReplicationStatus = toReplicationStatusJSON(info.ReplicationStatus) + } + + if versionID := h.currentVersionID(r, info.Name); versionID != "" { + out.VersionID = versionID if isBinary(req.SecretString, req.SecretBinary) { if st, ok := h.secrets.(secretStager); ok { - _ = st.MarkVersionBinary(r.Context(), info.Name, ver.VersionID) + _ = st.MarkVersionBinary(r.Context(), info.Name, versionID) } } } @@ -43,6 +48,35 @@ func (h *Handler) createSecret(w http.ResponseWriter, r *http.Request) { wire.WriteJSON(w, out) } +// currentVersionID returns the id of the version carrying AWSCURRENT ("" when +// there is none). With the AWS staging surface it reads the staging index, so +// CreateSecret's own lookup is not a retrieval of the value and does not stamp +// LastAccessedDate; other drivers fall back to a portable value read. +func (h *Handler) currentVersionID(r *http.Request, name string) string { + if st, ok := h.secrets.(secretStager); ok { + stages, err := st.SecretVersionStages(r.Context(), name) + if err != nil { + return "" + } + + for id, labels := range stages { + for _, l := range labels { + if l == stageCurrent { + return id + } + } + } + + return "" + } + + if ver, err := h.secrets.GetSecretValue(r.Context(), name, ""); err == nil { + return ver.VersionID + } + + return "" +} + func (h *Handler) deleteSecret(w http.ResponseWriter, r *http.Request) { var req deleteSecretRequest if !wire.DecodeJSON(w, r, &req) { @@ -116,6 +150,9 @@ func (h *Handler) describeSecret(w http.ResponseWriter, r *http.Request) { } out := toSecretListEntry(info) + if len(info.ReplicationStatus) > 0 { + out.ReplicationStatus = toReplicationStatusJSON(info.ReplicationStatus) + } if isStager { if stages, serr := st.SecretVersionStages(r.Context(), name); serr == nil { @@ -246,15 +283,17 @@ func (h *Handler) listSecrets(w http.ResponseWriter, r *http.Request) { return } - infos, err := h.secrets.ListSecrets(r.Context()) + if err := validateListSecrets(&req); err != nil { + writeValidation(w, err) + return + } + + infos, err := h.listSecretInfos(r, req.IncludePlannedDeletion) if err != nil { writeErr(w, err) return } - // A stable order (by name) keeps the offset-based NextToken valid across pages. - sort.Slice(infos, func(i, j int) bool { return infos[i].Name < infos[j].Name }) - filtered := infos[:0:0] for i := range infos { @@ -263,29 +302,53 @@ func (h *Handler) listSecrets(w http.ResponseWriter, r *http.Request) { } } - start, end, next, err := pageWindow(req.NextToken, req.MaxResults, len(filtered)) + page, next, err := listSecretsPage(filtered, &req) if err != nil { - writeErr(w, err) + wire.WriteJSONError(w, http.StatusBadRequest, "InvalidNextTokenException", msgInvalidNextToken) return } - st, isStager := h.secrets.(secretStager) + entries := make([]secretListEntryJSON, 0, len(page)) - entries := make([]secretListEntryJSON, 0, end-start) + for i := range page { + entries = append(entries, h.listEntry(r, &page[i])) + } - for i := start; i < end; i++ { - entry := toSecretListEntry(&filtered[i]) + wire.WriteJSON(w, listSecretsResponse{SecretList: entries, NextToken: next}) +} - if isStager { - if rot, rerr := st.SecretRotationDetails(r.Context(), filtered[i].Name); rerr == nil { - applyRotationInfo(&entry, rot) - } - } +// listEntry renders one ListSecrets entry: the secret's metadata plus, when +// the AWS staging surface is available, its rotation details and (for a +// secret scheduled for deletion, only listed with IncludePlannedDeletion) its +// DeletedDate, as DescribeSecret reports them. +func (h *Handler) listEntry(r *http.Request, info *secretsdriver.SecretInfo) secretListEntryJSON { + entry := toSecretListEntry(info) + + st, ok := h.secrets.(secretStager) + if !ok { + return entry + } - entries = append(entries, entry) + if rot, err := st.SecretRotationDetails(r.Context(), info.Name); err == nil { + applyRotationInfo(&entry, rot) } - wire.WriteJSON(w, listSecretsResponse{SecretList: entries, NextToken: next}) + if date, deleted := st.SecretDeletionDate(r.Context(), info.Name); deleted { + entry.DeletedDate = epochSeconds(date) + } + + return entry +} + +// listSecretInfos returns the secrets ListSecrets considers: active secrets, +// plus those scheduled for deletion when includePlannedDeletion is set and the +// driver supports it (the AWS provider does). +func (h *Handler) listSecretInfos(r *http.Request, includePlannedDeletion bool) ([]secretsdriver.SecretInfo, error) { + if rep, ok := h.secrets.(secretReplicator); ok { + return rep.ListSecretsWithOptions(r.Context(), includePlannedDeletion) + } + + return h.secrets.ListSecrets(r.Context()) } func (h *Handler) getSecretValue(w http.ResponseWriter, r *http.Request) { diff --git a/server/aws/secretsmanager/replication.go b/server/aws/secretsmanager/replication.go new file mode 100644 index 000000000..a6839981b --- /dev/null +++ b/server/aws/secretsmanager/replication.go @@ -0,0 +1,78 @@ +package secretsmanager + +import ( + "net/http" + + "github.com/stackshy/cloudemu/v2/server/wire" +) + +// replicateSecretToRegions adds replica Regions to a primary secret. +func (h *Handler) replicateSecretToRegions(w http.ResponseWriter, r *http.Request) { + rep, ok := h.secrets.(secretReplicator) + if !ok { + writeErr(w, errNotSupported) + return + } + + var req replicateSecretToRegionsRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + info, err := rep.ReplicateSecretToRegions(r.Context(), resolveSecretID(req.SecretID), + replicaRegionsToDriver(req.AddReplicaRegions), req.ForceOverwriteReplicaSecret) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, replicationResponse{ + ARN: info.ResourceID, ReplicationStatus: toReplicationStatusJSON(info.ReplicationStatus), + }) +} + +// removeRegionsFromReplication deletes replicas of a primary secret. +func (h *Handler) removeRegionsFromReplication(w http.ResponseWriter, r *http.Request) { + rep, ok := h.secrets.(secretReplicator) + if !ok { + writeErr(w, errNotSupported) + return + } + + var req removeRegionsFromReplicationRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + info, err := rep.RemoveRegionsFromReplication(r.Context(), resolveSecretID(req.SecretID), req.RemoveReplicaRegions) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, replicationResponse{ + ARN: info.ResourceID, ReplicationStatus: toReplicationStatusJSON(info.ReplicationStatus), + }) +} + +// stopReplicationToReplica promotes a replica to a standalone primary. +func (h *Handler) stopReplicationToReplica(w http.ResponseWriter, r *http.Request) { + rep, ok := h.secrets.(secretReplicator) + if !ok { + writeErr(w, errNotSupported) + return + } + + var req secretIDRequest + if !wire.DecodeJSON(w, r, &req) { + return + } + + info, err := rep.StopReplicationToReplica(r.Context(), resolveSecretID(req.SecretID)) + if err != nil { + writeErr(w, err) + return + } + + wire.WriteJSON(w, stopReplicationToReplicaResponse{ARN: info.ResourceID}) +} diff --git a/server/aws/secretsmanager/replication_test.go b/server/aws/secretsmanager/replication_test.go new file mode 100644 index 000000000..750062dcf --- /dev/null +++ b/server/aws/secretsmanager/replication_test.go @@ -0,0 +1,415 @@ +package secretsmanager_test + +import ( + "context" + "errors" + "net/http/httptest" + "testing" + "time" + + "github.com/aws/aws-sdk-go-v2/aws" + awsconfig "github.com/aws/aws-sdk-go-v2/config" + "github.com/aws/aws-sdk-go-v2/credentials" + awssm "github.com/aws/aws-sdk-go-v2/service/secretsmanager" + smtypes "github.com/aws/aws-sdk-go-v2/service/secretsmanager/types" + + "github.com/stackshy/cloudemu/v2" + "github.com/stackshy/cloudemu/v2/config" + awsserver "github.com/stackshy/cloudemu/v2/server/aws" +) + +// newClockedSecretsClient returns an SDK client bound to a wire server whose +// AWS provider runs on a fake clock, so tests can space out CreatedDate / +// LastAccessedDate deterministically. +func newClockedSecretsClient(t *testing.T) (*awssm.Client, *config.FakeClock) { + t.Helper() + + fc := config.NewFakeClock(time.Date(2025, 3, 10, 9, 30, 0, 0, time.UTC)) + cloud := cloudemu.NewAWS(config.WithClock(fc)) + ts := httptest.NewServer(awsserver.New(awsserver.Drivers{SecretsManager: cloud.SecretsManager})) + t.Cleanup(ts.Close) + + cfg, err := awsconfig.LoadDefaultConfig(context.Background(), + awsconfig.WithRegion("us-east-1"), + awsconfig.WithCredentialsProvider(credentials.NewStaticCredentialsProvider("test", "test", "")), + ) + if err != nil { + t.Fatalf("aws config: %v", err) + } + + return awssm.NewFromConfig(cfg, func(o *awssm.Options) { o.BaseEndpoint = aws.String(ts.URL) }), fc +} + +func mustCreate(t *testing.T, c *awssm.Client, in *awssm.CreateSecretInput) *awssm.CreateSecretOutput { + t.Helper() + + out, err := c.CreateSecret(context.Background(), in) + if err != nil { + t.Fatalf("CreateSecret %s: %v", aws.ToString(in.Name), err) + } + + return out +} + +func TestSDKListSecretsIncludePlannedDeletion(t *testing.T) { + client, _ := newClockedSecretsClient(t) + ctx := context.Background() + + mustCreate(t, client, &awssm.CreateSecretInput{Name: aws.String("live"), SecretString: aws.String("x")}) + mustCreate(t, client, &awssm.CreateSecretInput{Name: aws.String("gone"), SecretString: aws.String("x")}) + + if _, err := client.DeleteSecret(ctx, &awssm.DeleteSecretInput{SecretId: aws.String("gone")}); err != nil { + t.Fatalf("DeleteSecret: %v", err) + } + + def, err := client.ListSecrets(ctx, &awssm.ListSecretsInput{}) + if err != nil { + t.Fatalf("ListSecrets: %v", err) + } + + if got := secretNames(def.SecretList); len(got) != 1 || got[0] != "live" { + t.Fatalf("default ListSecrets = %v, want [live]", got) + } + + all, err := client.ListSecrets(ctx, &awssm.ListSecretsInput{IncludePlannedDeletion: aws.Bool(true)}) + if err != nil { + t.Fatalf("ListSecrets IncludePlannedDeletion: %v", err) + } + + if len(all.SecretList) != 2 { + t.Fatalf("IncludePlannedDeletion listed %v, want both secrets", secretNames(all.SecretList)) + } + + for _, e := range all.SecretList { + deleted := e.DeletedDate != nil + if wantDeleted := aws.ToString(e.Name) == "gone"; deleted != wantDeleted { + t.Fatalf("%s DeletedDate set=%v, want %v", aws.ToString(e.Name), deleted, wantDeleted) + } + } +} + +func TestSDKListSecretsSortOrder(t *testing.T) { + client, fc := newClockedSecretsClient(t) + ctx := context.Background() + + // Created b, then c, then a: CreatedDate order differs from name order. + for _, n := range []string{"b", "c", "a"} { + mustCreate(t, client, &awssm.CreateSecretInput{Name: aws.String(n), SecretString: aws.String("x")}) + fc.Advance(time.Minute) + } + + // Change "b" last so last-changed-date order differs again. + if _, err := client.PutSecretValue(ctx, &awssm.PutSecretValueInput{ + SecretId: aws.String("b"), SecretString: aws.String("y"), + }); err != nil { + t.Fatalf("PutSecretValue: %v", err) + } + + cases := []struct { + name string + in awssm.ListSecretsInput + order []string + }{ + {"created asc (SortOrder only)", awssm.ListSecretsInput{SortOrder: smtypes.SortOrderTypeAsc}, []string{"b", "c", "a"}}, + {"created desc (SortOrder only)", awssm.ListSecretsInput{SortOrder: smtypes.SortOrderTypeDesc}, []string{"a", "c", "b"}}, + {"name desc", awssm.ListSecretsInput{SortBy: smtypes.SortByTypeName, SortOrder: smtypes.SortOrderTypeDesc}, []string{"c", "b", "a"}}, + {"last-changed desc", awssm.ListSecretsInput{ + SortBy: smtypes.SortByTypeLastChangedDate, SortOrder: smtypes.SortOrderTypeDesc, + }, []string{"b", "a", "c"}}, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + in := tc.in + + out, err := client.ListSecrets(ctx, &in) + if err != nil { + t.Fatalf("ListSecrets: %v", err) + } + + got := secretNames(out.SecretList) + if len(got) != len(tc.order) { + t.Fatalf("got %v, want %v", got, tc.order) + } + + for i := range got { + if got[i] != tc.order[i] { + t.Fatalf("got %v, want %v", got, tc.order) + } + } + }) + } + + // Pagination stays consistent with the requested order. + page1, err := client.ListSecrets(ctx, &awssm.ListSecretsInput{SortOrder: smtypes.SortOrderTypeDesc, MaxResults: aws.Int32(2)}) + if err != nil { + t.Fatalf("ListSecrets page1: %v", err) + } + + page2, err := client.ListSecrets(ctx, &awssm.ListSecretsInput{ + SortOrder: smtypes.SortOrderTypeDesc, MaxResults: aws.Int32(2), NextToken: page1.NextToken, + }) + if err != nil { + t.Fatalf("ListSecrets page2: %v", err) + } + + if got := append(secretNames(page1.SecretList), secretNames(page2.SecretList)...); len(got) != 3 || got[2] != "b" { + t.Fatalf("paged desc order = %v, want [a c b]", got) + } +} + +func TestSDKDescribeSecretAccessAndRegionFields(t *testing.T) { + client, _ := newClockedSecretsClient(t) + ctx := context.Background() + + mustCreate(t, client, &awssm.CreateSecretInput{Name: aws.String("s"), SecretString: aws.String("x")}) + + before, err := client.DescribeSecret(ctx, &awssm.DescribeSecretInput{SecretId: aws.String("s")}) + if err != nil { + t.Fatalf("DescribeSecret: %v", err) + } + + if before.LastAccessedDate != nil { + t.Fatalf("LastAccessedDate = %v before any GetSecretValue, want omitted", before.LastAccessedDate) + } + + if aws.ToString(before.PrimaryRegion) != "us-east-1" { + t.Fatalf("PrimaryRegion = %q, want us-east-1", aws.ToString(before.PrimaryRegion)) + } + + if before.OwningService != nil { + t.Fatalf("OwningService = %q, want omitted for a user-created secret", aws.ToString(before.OwningService)) + } + + if _, err := client.GetSecretValue(ctx, &awssm.GetSecretValueInput{SecretId: aws.String("s")}); err != nil { + t.Fatalf("GetSecretValue: %v", err) + } + + after, err := client.DescribeSecret(ctx, &awssm.DescribeSecretInput{SecretId: aws.String("s")}) + if err != nil { + t.Fatalf("DescribeSecret: %v", err) + } + + want := time.Date(2025, 3, 10, 0, 0, 0, 0, time.UTC) + if after.LastAccessedDate == nil || !after.LastAccessedDate.Equal(want) { + t.Fatalf("LastAccessedDate = %v, want %v (day resolution)", after.LastAccessedDate, want) + } + + list, err := client.ListSecrets(ctx, &awssm.ListSecretsInput{}) + if err != nil { + t.Fatalf("ListSecrets: %v", err) + } + + if e := list.SecretList[0]; e.LastAccessedDate == nil || aws.ToString(e.PrimaryRegion) != "us-east-1" { + t.Fatalf("ListSecrets entry LastAccessedDate=%v PrimaryRegion=%q", e.LastAccessedDate, aws.ToString(e.PrimaryRegion)) + } +} + +func TestSDKListSecretsRegionAndOwningServiceFilters(t *testing.T) { + client, _ := newClockedSecretsClient(t) + ctx := context.Background() + + mustCreate(t, client, &awssm.CreateSecretInput{Name: aws.String("s"), SecretString: aws.String("x")}) + + cases := []struct { + key smtypes.FilterNameStringType + value string + want int + }{ + {smtypes.FilterNameStringTypePrimaryRegion, "us-east", 1}, + {smtypes.FilterNameStringTypePrimaryRegion, "eu-", 0}, + {smtypes.FilterNameStringTypePrimaryRegion, "US-EAST", 0}, // case-sensitive + {smtypes.FilterNameStringTypeOwningService, "rds", 0}, + } + + for _, tc := range cases { + out, err := client.ListSecrets(ctx, &awssm.ListSecretsInput{ + Filters: []smtypes.Filter{{Key: tc.key, Values: []string{tc.value}}}, + }) + if err != nil { + t.Fatalf("ListSecrets %s=%s: %v", tc.key, tc.value, err) + } + + if len(out.SecretList) != tc.want { + t.Fatalf("%s=%s matched %v, want %d", tc.key, tc.value, secretNames(out.SecretList), tc.want) + } + } +} + +func TestSDKReplicationLifecycle(t *testing.T) { + client, _ := newClockedSecretsClient(t) + ctx := context.Background() + + created := mustCreate(t, client, &awssm.CreateSecretInput{ + Name: aws.String("rep"), SecretString: aws.String("x"), + AddReplicaRegions: []smtypes.ReplicaRegionType{{Region: aws.String("eu-west-3")}}, + }) + + if len(created.ReplicationStatus) != 1 || created.ReplicationStatus[0].Status != smtypes.StatusTypeInSync || + aws.ToString(created.ReplicationStatus[0].KmsKeyId) != "alias/aws/secretsmanager" { + t.Fatalf("CreateSecret ReplicationStatus = %+v", created.ReplicationStatus) + } + + rep, err := client.ReplicateSecretToRegions(ctx, &awssm.ReplicateSecretToRegionsInput{ + SecretId: created.ARN, + AddReplicaRegions: []smtypes.ReplicaRegionType{{Region: aws.String("ap-south-1"), KmsKeyId: aws.String("alias/k")}}, + }) + if err != nil { + t.Fatalf("ReplicateSecretToRegions: %v", err) + } + + if aws.ToString(rep.ARN) != aws.ToString(created.ARN) || len(rep.ReplicationStatus) != 2 { + t.Fatalf("ReplicateSecretToRegions = ARN %q, %+v", aws.ToString(rep.ARN), rep.ReplicationStatus) + } + + desc, err := client.DescribeSecret(ctx, &awssm.DescribeSecretInput{SecretId: aws.String("rep")}) + if err != nil { + t.Fatalf("DescribeSecret: %v", err) + } + + if len(desc.ReplicationStatus) != 2 || aws.ToString(desc.ReplicationStatus[1].KmsKeyId) != "alias/k" { + t.Fatalf("DescribeSecret ReplicationStatus = %+v", desc.ReplicationStatus) + } + + // Duplicate replica Region โ†’ InvalidParameterException. + var ipe *smtypes.InvalidParameterException + if _, err := client.ReplicateSecretToRegions(ctx, &awssm.ReplicateSecretToRegionsInput{ + SecretId: aws.String("rep"), AddReplicaRegions: []smtypes.ReplicaRegionType{{Region: aws.String("eu-west-3")}}, + }); !errors.As(err, &ipe) { + t.Fatalf("duplicate replica err = %v, want InvalidParameterException", err) + } + + // A replicated primary can't be deleted โ†’ InvalidRequestException. + var ire *smtypes.InvalidRequestException + if _, err := client.DeleteSecret(ctx, &awssm.DeleteSecretInput{SecretId: aws.String("rep")}); !errors.As(err, &ire) { + t.Fatalf("DeleteSecret replicated primary err = %v, want InvalidRequestException", err) + } + + // The secret here is the primary, not a replica โ†’ InvalidRequestException. + if _, err := client.StopReplicationToReplica(ctx, &awssm.StopReplicationToReplicaInput{ + SecretId: aws.String("rep"), + }); !errors.As(err, &ire) { + t.Fatalf("StopReplicationToReplica on primary err = %v, want InvalidRequestException", err) + } + + var rnf *smtypes.ResourceNotFoundException + if _, err := client.StopReplicationToReplica(ctx, &awssm.StopReplicationToReplicaInput{ + SecretId: aws.String("missing"), + }); !errors.As(err, &rnf) { + t.Fatalf("StopReplicationToReplica missing err = %v, want ResourceNotFoundException", err) + } + + removed, err := client.RemoveRegionsFromReplication(ctx, &awssm.RemoveRegionsFromReplicationInput{ + SecretId: aws.String("rep"), RemoveReplicaRegions: []string{"eu-west-3", "ap-south-1"}, + }) + if err != nil { + t.Fatalf("RemoveRegionsFromReplication: %v", err) + } + + if len(removed.ReplicationStatus) != 0 { + t.Fatalf("after removal ReplicationStatus = %+v, want empty", removed.ReplicationStatus) + } + + if _, err := client.DeleteSecret(ctx, &awssm.DeleteSecretInput{SecretId: aws.String("rep")}); err != nil { + t.Fatalf("DeleteSecret after removing replicas: %v", err) + } +} + +func TestSDKScheduledForDeletionMessage(t *testing.T) { + client, _ := newClockedSecretsClient(t) + ctx := context.Background() + + mustCreate(t, client, &awssm.CreateSecretInput{Name: aws.String("s"), SecretString: aws.String("x")}) + + if _, err := client.DeleteSecret(ctx, &awssm.DeleteSecretInput{SecretId: aws.String("s")}); err != nil { + t.Fatalf("DeleteSecret: %v", err) + } + + var ire *smtypes.InvalidRequestException + + _, err := client.GetSecretValue(ctx, &awssm.GetSecretValueInput{SecretId: aws.String("s")}) + if !errors.As(err, &ire) || + ire.ErrorMessage() != "You can't perform this operation on the secret because it was marked for deletion." { + t.Fatalf("GetSecretValue on deleted secret err = %v", err) + } + + _, err = client.CreateSecret(ctx, &awssm.CreateSecretInput{Name: aws.String("s"), SecretString: aws.String("x")}) + if !errors.As(err, &ire) || ire.ErrorMessage() != + "You can't create this secret because a secret with this name is already scheduled for deletion." { + t.Fatalf("CreateSecret over deleted name err = %v", err) + } +} + +// TestSDKReplicaRegionMustBeRegionCode checks a replica Region that isn't +// shaped like a Region code is rejected on CreateSecret and +// ReplicateSecretToRegions, so a typo can't leave a phantom replica that then +// blocks DeleteSecret. +func TestSDKReplicaRegionMustBeRegionCode(t *testing.T) { + client, _ := newClockedSecretsClient(t) + ctx := context.Background() + + var ipe *smtypes.InvalidParameterException + if _, err := client.CreateSecret(ctx, &awssm.CreateSecretInput{ + Name: aws.String("bad"), SecretString: aws.String("x"), + AddReplicaRegions: []smtypes.ReplicaRegionType{{Region: aws.String("not-a-region")}}, + }); !errors.As(err, &ipe) { + t.Fatalf("CreateSecret bad replica Region err = %v, want InvalidParameterException", err) + } + + mustCreate(t, client, &awssm.CreateSecretInput{Name: aws.String("ok"), SecretString: aws.String("x")}) + + if _, err := client.ReplicateSecretToRegions(ctx, &awssm.ReplicateSecretToRegionsInput{ + SecretId: aws.String("ok"), AddReplicaRegions: []smtypes.ReplicaRegionType{{Region: aws.String("eu-west")}}, + }); !errors.As(err, &ipe) { + t.Fatalf("ReplicateSecretToRegions bad Region err = %v, want InvalidParameterException", err) + } + + if _, err := client.DeleteSecret(ctx, &awssm.DeleteSecretInput{SecretId: aws.String("ok")}); err != nil { + t.Fatalf("DeleteSecret after rejected replication: %v", err) + } +} + +// TestSDKReplicationUpdatesLastChangedDate checks adding and removing replicas +// moves DescribeSecret's LastChangedDate, which AWS defines as the last time +// the secret was modified in any way. +func TestSDKReplicationUpdatesLastChangedDate(t *testing.T) { + client, fc := newClockedSecretsClient(t) + ctx := context.Background() + + mustCreate(t, client, &awssm.CreateSecretInput{Name: aws.String("rep"), SecretString: aws.String("x")}) + + lastChanged := func() time.Time { + t.Helper() + + desc, err := client.DescribeSecret(ctx, &awssm.DescribeSecretInput{SecretId: aws.String("rep")}) + if err != nil { + t.Fatalf("DescribeSecret: %v", err) + } + + return aws.ToTime(desc.LastChangedDate) + } + + fc.Advance(time.Hour) + + if _, err := client.ReplicateSecretToRegions(ctx, &awssm.ReplicateSecretToRegionsInput{ + SecretId: aws.String("rep"), AddReplicaRegions: []smtypes.ReplicaRegionType{{Region: aws.String("eu-west-3")}}, + }); err != nil { + t.Fatalf("ReplicateSecretToRegions: %v", err) + } + + if got := lastChanged(); !got.Equal(fc.Now().UTC().Truncate(time.Second)) { + t.Fatalf("LastChangedDate after replicate = %v, want %v", got, fc.Now().UTC()) + } + + fc.Advance(time.Hour) + + if _, err := client.RemoveRegionsFromReplication(ctx, &awssm.RemoveRegionsFromReplicationInput{ + SecretId: aws.String("rep"), RemoveReplicaRegions: []string{"eu-west-3"}, + }); err != nil { + t.Fatalf("RemoveRegionsFromReplication: %v", err) + } + + if got := lastChanged(); !got.Equal(fc.Now().UTC().Truncate(time.Second)) { + t.Fatalf("LastChangedDate after remove = %v, want %v", got, fc.Now().UTC()) + } +} diff --git a/server/aws/secretsmanager/types.go b/server/aws/secretsmanager/types.go index b6a15390a..9e3227f4b 100644 --- a/server/aws/secretsmanager/types.go +++ b/server/aws/secretsmanager/types.go @@ -32,6 +32,15 @@ type secretListEntryJSON struct { // KmsKeyId is the customer KMS key the secret is encrypted with, echoed on // DescribeSecret. Omitted when the default aws/secretsmanager key is used. KmsKeyID string `json:"KmsKeyId,omitempty"` + // LastAccessedDate is the day (midnight UTC) the value was last retrieved; + // omitted when the secret has never been retrieved. + LastAccessedDate float64 `json:"LastAccessedDate,omitempty"` + // PrimaryRegion is the Region the secret was created in. + PrimaryRegion string `json:"PrimaryRegion,omitempty"` + // ReplicationStatus lists the secret's replicas. DescribeSecret only: the + // ListSecrets SecretListEntry shape has no such member, so listSecrets + // leaves it nil. + ReplicationStatus []replicationStatusJSON `json:"ReplicationStatus,omitempty"` VersionIDsToStages map[string][]string `json:"VersionIdsToStages,omitempty"` @@ -90,13 +99,89 @@ type versionJSON struct { // --- request envelopes --- type createSecretRequest struct { - Name string `json:"Name"` - Description string `json:"Description"` - SecretString string `json:"SecretString"` - SecretBinary []byte `json:"SecretBinary"` - Tags []tagJSON `json:"Tags"` - KmsKeyID string `json:"KmsKeyId"` - ClientRequestToken string `json:"ClientRequestToken"` + Name string `json:"Name"` + Description string `json:"Description"` + SecretString string `json:"SecretString"` + SecretBinary []byte `json:"SecretBinary"` + Tags []tagJSON `json:"Tags"` + KmsKeyID string `json:"KmsKeyId"` + ClientRequestToken string `json:"ClientRequestToken"` + AddReplicaRegions []replicaRegionJSON `json:"AddReplicaRegions"` + // ForceOverwriteReplicaSecret is accepted for parity; replicas are + // metadata-only, so there is never a destination secret to overwrite. + ForceOverwriteReplicaSecret bool `json:"ForceOverwriteReplicaSecret"` +} + +// replicaRegionJSON is the ReplicaRegionType wire shape. +type replicaRegionJSON struct { + Region string `json:"Region"` + KmsKeyID string `json:"KmsKeyId"` +} + +// replicationStatusJSON is the ReplicationStatusType wire shape. +type replicationStatusJSON struct { + Region string `json:"Region"` + KmsKeyID string `json:"KmsKeyId,omitempty"` + Status string `json:"Status,omitempty"` + StatusMessage string `json:"StatusMessage,omitempty"` + LastAccessedDate float64 `json:"LastAccessedDate,omitempty"` +} + +type replicateSecretToRegionsRequest struct { + SecretID string `json:"SecretId"` + AddReplicaRegions []replicaRegionJSON `json:"AddReplicaRegions"` + ForceOverwriteReplicaSecret bool `json:"ForceOverwriteReplicaSecret"` +} + +type removeRegionsFromReplicationRequest struct { + SecretID string `json:"SecretId"` + RemoveReplicaRegions []string `json:"RemoveReplicaRegions"` +} + +// replicationResponse is the {ARN, ReplicationStatus} envelope of +// ReplicateSecretToRegions and RemoveRegionsFromReplication. ReplicationStatus +// is always emitted (an empty list once every replica is removed). +type replicationResponse struct { + ARN string `json:"ARN"` + ReplicationStatus []replicationStatusJSON `json:"ReplicationStatus"` +} + +type stopReplicationToReplicaResponse struct { + ARN string `json:"ARN"` +} + +func (r replicaRegionJSON) toDriver() secretsdriver.SecretReplicaRegion { + return secretsdriver.SecretReplicaRegion{Region: r.Region, KMSKeyID: r.KmsKeyID} +} + +func replicaRegionsToDriver(in []replicaRegionJSON) []secretsdriver.SecretReplicaRegion { + if len(in) == 0 { + return nil + } + + out := make([]secretsdriver.SecretReplicaRegion, 0, len(in)) + for _, r := range in { + out = append(out, r.toDriver()) + } + + return out +} + +// toReplicationStatusJSON renders a secret's replica list; nil in, empty out +// (never nil), so callers that always emit the member get "[]". +func toReplicationStatusJSON(in []secretsdriver.SecretReplicationStatus) []replicationStatusJSON { + out := make([]replicationStatusJSON, 0, len(in)) + for _, r := range in { + out = append(out, replicationStatusJSON{ + Region: r.Region, + KmsKeyID: r.KMSKeyID, + Status: r.Status, + StatusMessage: r.StatusMessage, + LastAccessedDate: epochSeconds(r.LastAccessedDate), + }) + } + + return out } type secretIDRequest struct { @@ -144,9 +229,17 @@ type secretFilter struct { } type listSecretsRequest struct { - MaxResults int32 `json:"MaxResults"` + // MaxResults is a pointer so an explicit out-of-range 0 is distinguishable + // from an absent field (which lists everything). + MaxResults *int32 `json:"MaxResults"` NextToken string `json:"NextToken"` Filters []secretFilter `json:"Filters"` + // IncludePlannedDeletion also lists secrets scheduled for deletion. + IncludePlannedDeletion bool `json:"IncludePlannedDeletion"` + // SortBy is created-date | last-accessed-date | last-changed-date | name. + SortBy string `json:"SortBy"` + // SortOrder is asc | desc. + SortOrder string `json:"SortOrder"` } type putSecretValueRequest struct { @@ -206,9 +299,10 @@ type updateSecretVersionStageResponse struct { // --- response envelopes --- type createSecretResponse struct { - ARN string `json:"ARN"` - Name string `json:"Name"` - VersionID string `json:"VersionId,omitempty"` + ARN string `json:"ARN"` + Name string `json:"Name"` + VersionID string `json:"VersionId,omitempty"` + ReplicationStatus []replicationStatusJSON `json:"ReplicationStatus,omitempty"` } type deleteSecretResponse struct { @@ -431,12 +525,14 @@ func tagsToMap(tags []tagJSON) map[string]string { func toSecretListEntry(info *secretsdriver.SecretInfo) secretListEntryJSON { return secretListEntryJSON{ - ARN: info.ResourceID, - Name: info.Name, - Description: info.Description, - Tags: mapToTags(info.Tags), - CreatedDate: epochSeconds(info.CreatedAt), - LastChangedDate: epochSeconds(info.UpdatedAt), - KmsKeyID: info.KMSKeyID, + ARN: info.ResourceID, + Name: info.Name, + Description: info.Description, + Tags: mapToTags(info.Tags), + CreatedDate: epochSeconds(info.CreatedAt), + LastChangedDate: epochSeconds(info.UpdatedAt), + KmsKeyID: info.KMSKeyID, + LastAccessedDate: epochSeconds(info.LastAccessedDate), + PrimaryRegion: info.PrimaryRegion, } } diff --git a/services/secrets/driver/driver.go b/services/secrets/driver/driver.go index f43acf5e7..41b3d074c 100644 --- a/services/secrets/driver/driver.go +++ b/services/secrets/driver/driver.go @@ -50,6 +50,10 @@ type SecretConfig struct { // VersionAliases maps GCP Secret Manager version aliases to version ids. // Ignored by AWS/Azure. VersionAliases map[string]string + // AddReplicaRegions are the AWS Secrets Manager Regions (and optional + // per-replica KMS keys) to replicate the new secret to (CreateSecret + // AddReplicaRegions). Ignored by Azure/GCP. + AddReplicaRegions []SecretReplicaRegion } // SecretInfo describes a secret. @@ -86,6 +90,36 @@ type SecretInfo struct { // VersionAliases maps GCP Secret Manager version aliases to version ids, // echoed on the secret and honored by GetSecretValue. Nil when unset. VersionAliases map[string]string + // PrimaryRegion is the AWS Secrets Manager Region the secret was created in + // (DescribeSecret/ListSecrets PrimaryRegion). Empty for Azure/GCP. + PrimaryRegion string + // LastAccessedDate is the RFC3339 date (truncated to midnight UTC, as AWS + // reports it) the secret value was last retrieved. Empty when the secret + // has never been retrieved. AWS Secrets Manager only. + LastAccessedDate string + // ReplicationStatus lists the AWS Secrets Manager replica Regions of this + // primary secret and their status. Nil when the secret is not replicated. + // The slice is replaced, never mutated in place, so copies of SecretInfo + // may share it safely. + ReplicationStatus []SecretReplicationStatus +} + +// SecretReplicaRegion is one AWS Secrets Manager replica target +// (ReplicaRegionType): a Region and the optional KMS key that encrypts the +// replica (empty selects aws/secretsmanager). +type SecretReplicaRegion struct { + Region string + KMSKeyID string +} + +// SecretReplicationStatus is one AWS Secrets Manager replica's status +// (ReplicationStatusType). Status is one of InSync, InProgress or Failed. +type SecretReplicationStatus struct { + Region string + KMSKeyID string + Status string + StatusMessage string + LastAccessedDate string } // GCPReplica is one location a user-managed GCP secret replicates to, with an diff --git a/services/secrets/secrets.go b/services/secrets/secrets.go index 246437e5b..f2bf55e9d 100644 --- a/services/secrets/secrets.go +++ b/services/secrets/secrets.go @@ -96,6 +96,8 @@ func (s *Secrets) rec(op string, input, output any, err error, dur time.Duration } // CreateSecret creates a new secret with an initial value. +// +//nolint:gocritic // hugeParam: mirrors the driver.Secrets interface signature. func (s *Secrets) CreateSecret(ctx context.Context, config driver.SecretConfig, value []byte) (*driver.SecretInfo, error) { out, err := s.do(ctx, "CreateSecret", config, func() (any, error) { return s.driver.CreateSecret(ctx, config, value) }) if err != nil { From 057abf7a23eb88f3ecbc1cd0ecd5c738102ed1f7 Mon Sep 17 00:00:00 2001 From: Satyam-Trivedi-ZS Date: Fri, 23 Oct 2026 09:50:23 +0530 Subject: [PATCH 22/31] fix(aws-keyspaces): tag existence and restrictions, name validation, missing-keyspace error, GetTable omissions (#1645) * fix(aws-keyspaces): tag existence and restrictions, name validation, missing-keyspace error, GetTable omissions - TagResource/UntagResource/ListTagsForResource validate the ARN shape (ValidationException) and require an existing keyspace or table (ResourceNotFoundException); the trailing "/" is optional. - Tags follow the documented restrictions on every path that accepts them: key 1-128 and value 1-256 characters, allowed character set, reserved "aws:" prefix, and at most 60 tags per request. - Keyspace/table names enforce the documented 1-48 char pattern [a-zA-Z0-9][a-zA-Z0-9_]{0,47} (a leading digit is allowed); UDT names get the documented 48-char limit. - CreateTable/CreateType/RestoreTable on a missing parent keyspace return ResourceNotFoundException instead of ValidationException. - GetTable omits readCapacityUnits/writeCapacityUnits for PAY_PER_REQUEST and an unset cdcSpecification.viewType. - Seed the documented fourth system keyspace, system_schema_mcs, which likewise cannot be deleted. Part of #1512 * fix(aws): skip resources deleted mid-walk in tagged RGT discovery A resource deleted between a service's list call and its tag read makes the tag read return NotFound. project returned that error, which aborted DiscoverResources for every service. Keyspaces hit it once ListTagsForResource started rejecting unknown ARNs. Skip the vanished resource instead, as the resourcediscovery walkers already do; other errors still propagate. * fix(aws-keyspaces): copy tags under the lock in Snapshot, canonicalise tag ARNs on restore Snapshot handed json.Marshal the live tag maps after releasing the lock, so a concurrent TagResource raced it. Deep-copy them under the lock. Restore now keys tags by the canonical ARN of an existing keyspace or table. Tags an older build stored under the slash-less ARN form are merged in (the canonical entry wins on a clash), and entries for ARNs that name no resource are dropped, so they can't leak onto a resource created later under that ARN. * fix(aws-keyspaces): enforce the 60-item TagList bound in the provider TagResource, UntagResource, CreateKeyspace, CreateTable and RestoreTable now reject more than 60 tags on the library path too, matching the wire handler. Scope the omitEmptyEnum comment to cdcSpecification.viewType, and rename the parity test files to feature names (names_test.go, tags_test.go, tables_test.go). --- providers/aws/keyspaces/keyspaces.go | 61 +++++- providers/aws/keyspaces/keyspaces_test.go | 11 +- providers/aws/keyspaces/names_test.go | 115 +++++++++++ providers/aws/keyspaces/snapshot.go | 55 ++++- providers/aws/keyspaces/snapshot_test.go | 112 +++++++++++ providers/aws/keyspaces/tables.go | 12 +- providers/aws/keyspaces/tags.go | 162 ++++++++++++++- providers/aws/keyspaces/tags_test.go | 223 +++++++++++++++++++++ providers/aws/keyspaces/udt.go | 4 +- providers/aws/rgt_discovery.go | 11 + providers/aws/rgt_discovery_test.go | 105 ++++++++++ server/aws/keyspaces/keyspaces.go | 4 + server/aws/keyspaces/sdk_roundtrip_test.go | 4 +- server/aws/keyspaces/tables.go | 8 + server/aws/keyspaces/tables_test.go | 120 +++++++++++ server/aws/keyspaces/tags.go | 29 +++ server/aws/keyspaces/tags_test.go | 100 +++++++++ server/aws/keyspaces/types.go | 14 +- server/aws/keyspaces/wirejson.go | 44 ++-- 19 files changed, 1148 insertions(+), 46 deletions(-) create mode 100644 providers/aws/keyspaces/names_test.go create mode 100644 providers/aws/keyspaces/tags_test.go create mode 100644 providers/aws/rgt_discovery_test.go create mode 100644 server/aws/keyspaces/tables_test.go create mode 100644 server/aws/keyspaces/tags_test.go diff --git a/providers/aws/keyspaces/keyspaces.go b/providers/aws/keyspaces/keyspaces.go index 9d4adf587..bfff6e452 100644 --- a/providers/aws/keyspaces/keyspaces.go +++ b/providers/aws/keyspaces/keyspaces.go @@ -7,6 +7,7 @@ package keyspaces import ( "context" "fmt" + "regexp" "strings" "sync" @@ -57,7 +58,7 @@ func New(opts *config.Options) *Mock { opts: opts, } - for _, sys := range []string{"system", "system_schema", "system_multiregion_info"} { + for _, sys := range systemKeyspaceNames() { m.keyspaces.Set(sys, ksdriver.Keyspace{ Name: sys, ARN: m.keyspaceARN(sys), ReplicationStrategy: ksdriver.SingleRegion, ReplicationRegions: []string{opts.Region}, @@ -79,13 +80,51 @@ func tableKey(keyspace, table string) string { return keyspace + "/" + table } func typeKey(keyspace, name string) string { return keyspace + "/" + name } +// maxNameLen is the documented maximum length of keyspace, table, and +// user-defined type names (API reference KeyspaceName/TableName/TypeName: +// "Maximum length of 48"; devguide quotas: "Max UDT name length 48"). +const maxNameLen = 48 + +// keyspaceTableNamePattern is the documented KeyspaceName/TableName pattern +// ([a-zA-Z0-9][a-zA-Z0-9_]{0,47}), anchored: alphanumerics and underscores +// only, not starting with an underscore. A leading digit is allowed. +var keyspaceTableNamePattern = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9_]{0,47}$`) + +// validName validates a keyspace or table name against the documented +// KeyspaceName/TableName constraints. func validName(kind, name string) error { if name == "" { return cerrors.Newf(cerrors.InvalidArgument, "%s name is required", kind) } + if len(name) > maxNameLen { + return cerrors.Newf(cerrors.InvalidArgument, + "%s name %q must be at most %d characters", kind, name, maxNameLen) + } + + if !keyspaceTableNamePattern.MatchString(name) { + return cerrors.Newf(cerrors.InvalidArgument, + "%s name %q must match pattern [a-zA-Z0-9][a-zA-Z0-9_]{0,47}", kind, name) + } + + return nil +} + +// validTypeName validates a user-defined type name. The API model gives +// TypeName only a 1-48 length constraint (no pattern), so only emptiness, +// length, and the '/' used in internal store keys are checked. +func validTypeName(name string) error { + if name == "" { + return cerrors.New(cerrors.InvalidArgument, "type name is required") + } + + if len(name) > maxNameLen { + return cerrors.Newf(cerrors.InvalidArgument, + "type name %q must be at most %d characters", name, maxNameLen) + } + if strings.Contains(name, "/") { - return cerrors.Newf(cerrors.InvalidArgument, "%s name %q must not contain '/'", kind, name) + return cerrors.Newf(cerrors.InvalidArgument, "type name %q must not contain '/'", name) } return nil @@ -120,6 +159,10 @@ func (m *Mock) CreateKeyspace(_ context.Context, cfg ksdriver.CreateKeyspaceConf return nil, err } + if err := validateTags(cfg.Tags); err != nil { + return nil, err + } + strategy := cfg.ReplicationStrategy if strategy == "" { strategy = ksdriver.SingleRegion @@ -244,13 +287,15 @@ func (m *Mock) DeleteKeyspace(_ context.Context, name string) error { return nil } +// systemKeyspaceNames lists the four read-only system keyspaces every account +// has ("Amazon Keyspaces uses four system keyspaces", devguide +// working-with-keyspaces.html). +func systemKeyspaceNames() []string { + return []string{"system", "system_schema", "system_schema_mcs", "system_multiregion_info"} +} + func isSystemKeyspace(name string) bool { - switch name { - case "system", "system_schema", "system_multiregion_info": - return true - default: - return false - } + return containsStr(systemKeyspaceNames(), name) } func (m *Mock) keyspaceHasTables(keyspace string) bool { diff --git a/providers/aws/keyspaces/keyspaces_test.go b/providers/aws/keyspaces/keyspaces_test.go index 402d6c0e9..aa1135c89 100644 --- a/providers/aws/keyspaces/keyspaces_test.go +++ b/providers/aws/keyspaces/keyspaces_test.go @@ -69,8 +69,8 @@ func TestKeyspaceLifecycle(t *testing.T) { t.Fatalf("ListKeyspaces: %v", err) } - if len(list) != 4 { - t.Fatalf("expected 4 keyspaces, got %d", len(list)) + if len(list) != 5 { // 4 system + app + t.Fatalf("expected 5 keyspaces, got %d", len(list)) } } @@ -114,11 +114,12 @@ func TestTableLifecycleAndValidation(t *testing.T) { ctx := context.Background() mustKeyspace(t, m, "app") - // Missing keyspace rejected. + // Missing keyspace is ResourceNotFound (the documented "tried to access a + // keyspace ... that doesn't exist" error), not a validation failure. if _, err := m.CreateTable(ctx, ksdriver.CreateTableConfig{ KeyspaceName: "ghost", Name: "t", SchemaDefinition: sampleSchema(), - }); !cerrors.IsInvalidArgument(err) { - t.Fatalf("table in missing keyspace: got %v, want InvalidArgument", err) + }); !cerrors.IsNotFound(err) { + t.Fatalf("table in missing keyspace: got %v, want NotFound", err) } // No partition key rejected. diff --git a/providers/aws/keyspaces/names_test.go b/providers/aws/keyspaces/names_test.go new file mode 100644 index 000000000..03604fb1c --- /dev/null +++ b/providers/aws/keyspaces/names_test.go @@ -0,0 +1,115 @@ +package keyspaces + +import ( + "context" + "strings" + "testing" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + ksdriver "github.com/stackshy/cloudemu/v2/services/keyspaces/driver" +) + +// Keyspace/table names follow the documented KeyspaceName/TableName shape: +// length 1-48, pattern [a-zA-Z0-9][a-zA-Z0-9_]{0,47}. +func TestNameValidation(t *testing.T) { + m := newTestMock() + ctx := context.Background() + + maxName := strings.Repeat("a", maxNameLen) + tooLong := strings.Repeat("a", maxNameLen+1) + + valid := []string{"a", "A1", "1abc", "9", "my_keyspace", maxName} + for _, n := range valid { + if _, err := m.CreateKeyspace(ctx, ksdriver.CreateKeyspaceConfig{Name: n}); err != nil { + t.Fatalf("CreateKeyspace(%q): unexpected error %v", n, err) + } + } + + invalid := []string{"", tooLong, "_lead", "has-dash", "has.dot", "has space", "a/b", "รผnicode"} + for _, n := range invalid { + if _, err := m.CreateKeyspace(ctx, ksdriver.CreateKeyspaceConfig{Name: n}); !cerrors.IsInvalidArgument(err) { + t.Fatalf("CreateKeyspace(%q): got %v, want InvalidArgument", n, err) + } + + if _, err := m.CreateTable(ctx, ksdriver.CreateTableConfig{ + KeyspaceName: "a", Name: n, SchemaDefinition: sampleSchema(), + }); !cerrors.IsInvalidArgument(err) { + t.Fatalf("CreateTable(%q): got %v, want InvalidArgument", n, err) + } + } + + if _, err := m.CreateTable(ctx, ksdriver.CreateTableConfig{ + KeyspaceName: "a", Name: "1t", SchemaDefinition: sampleSchema(), + }); err != nil { + t.Fatalf("CreateTable leading digit: %v", err) + } + + if _, err := m.CreateTable(ctx, ksdriver.CreateTableConfig{ + KeyspaceName: "a", Name: maxName, SchemaDefinition: sampleSchema(), + }); err != nil { + t.Fatalf("CreateTable 48-char name: %v", err) + } + + if _, err := m.RestoreTable(ctx, ksdriver.RestoreTableConfig{ + SourceKeyspace: "a", SourceTable: "1t", TargetKeyspace: "a", TargetTable: tooLong, + RestoreTimestamp: m.opts.Clock.Now().Add(-1), + }); !cerrors.IsInvalidArgument(err) { + t.Fatalf("RestoreTable too-long target: got %v, want InvalidArgument", err) + } + + fields := []ksdriver.FieldDefinition{{Name: "f", Type: "text"}} + if _, err := m.CreateType(ctx, "a", tooLong, fields); !cerrors.IsInvalidArgument(err) { + t.Fatalf("CreateType too long: got %v, want InvalidArgument", err) + } + + if _, err := m.CreateType(ctx, "a", maxName, fields); err != nil { + t.Fatalf("CreateType 48-char name: %v", err) + } +} + +// A well-formed request naming a parent keyspace that doesn't exist is +// ResourceNotFound for CreateTable, CreateType, and RestoreTable's target. +func TestMissingParentKeyspaceIsNotFound(t *testing.T) { + m := newTestMock() + ctx := context.Background() + mustKeyspace(t, m, "app") + + if _, err := m.CreateTable(ctx, ksdriver.CreateTableConfig{ + KeyspaceName: "ghost", Name: "t", SchemaDefinition: sampleSchema(), + }); !cerrors.IsNotFound(err) { + t.Fatalf("CreateTable: got %v, want NotFound", err) + } + + if _, err := m.CreateType(ctx, "ghost", "addr", []ksdriver.FieldDefinition{{Name: "f", Type: "text"}}); !cerrors.IsNotFound(err) { + t.Fatalf("CreateType: got %v, want NotFound", err) + } + + if _, err := m.CreateTable(ctx, ksdriver.CreateTableConfig{ + KeyspaceName: "app", Name: "src", SchemaDefinition: sampleSchema(), + }); err != nil { + t.Fatalf("CreateTable: %v", err) + } + + if _, err := m.RestoreTable(ctx, ksdriver.RestoreTableConfig{ + SourceKeyspace: "app", SourceTable: "src", TargetKeyspace: "ghost", TargetTable: "dst", + RestoreTimestamp: m.opts.Clock.Now(), + }); !cerrors.IsNotFound(err) { + t.Fatalf("RestoreTable: got %v, want NotFound", err) + } +} + +// All four documented system keyspaces exist and none can be deleted. +func TestSystemKeyspaces(t *testing.T) { + m := newTestMock() + ctx := context.Background() + + for _, name := range []string{"system", "system_schema", "system_schema_mcs", "system_multiregion_info"} { + if _, err := m.GetKeyspace(ctx, name); err != nil { + t.Fatalf("GetKeyspace(%s): %v", name, err) + } + + if err := m.DeleteKeyspace(ctx, name); !cerrors.IsInvalidArgument(err) { + t.Fatalf("DeleteKeyspace(%s): got %v, want InvalidArgument", name, err) + } + } +} diff --git a/providers/aws/keyspaces/snapshot.go b/providers/aws/keyspaces/snapshot.go index f57a670aa..9410446c8 100644 --- a/providers/aws/keyspaces/snapshot.go +++ b/providers/aws/keyspaces/snapshot.go @@ -47,9 +47,11 @@ func (m *Mock) Snapshot(_ context.Context, _ bool) (json.RawMessage, error) { *d.dst = b } + // Copy the tag maps under the lock: json.Marshal runs after the unlock, and + // TagResource mutates the inner maps in place. m.mu.RLock() if len(m.tags) > 0 { - snap.Tags = m.tags + snap.Tags = copyTagMaps(m.tags) } m.mu.RUnlock() @@ -86,9 +88,58 @@ func (m *Mock) Restore(_ context.Context, data json.RawMessage) error { if snap.Tags != nil { m.mu.Lock() - m.tags = snap.Tags + m.tags = m.canonicalTagsLocked(snap.Tags) m.mu.Unlock() } return nil } + +// canonicalTagsLocked keys restored tags by the canonical ARN of the keyspace +// or table they belong to. Tag reads and writes resolve to the canonical ARN, +// but an older build stored tags under whatever ARN the caller passed (for +// example the slash-less table form) and accepted ARNs of resources that don't +// exist. A non-canonical entry for an existing resource is merged into the +// canonical one, with the canonical entry's values winning on a key clash. An +// entry that names no existing resource is dropped: no API can reach it, and +// keeping it would hand its tags to a resource created later under that ARN. +// The caller holds the write lock, and the stores are already restored. +func (m *Mock) canonicalTagsLocked(in map[string]map[string]string) map[string]map[string]string { + out := make(map[string]map[string]string, len(in)) + + for arn, tags := range in { + if m.existingResourceARN(arn) == arn { + out[arn] = copyTags(tags) + } + } + + for arn, tags := range in { + canonical := m.existingResourceARN(arn) + if canonical == "" || canonical == arn || arn+"/" != canonical { + continue + } + + cur := out[canonical] + if cur == nil { + cur = make(map[string]string, len(tags)) + out[canonical] = cur + } + + for k, v := range tags { + if _, ok := cur[k]; !ok { + cur[k] = v + } + } + } + + return out +} + +func copyTagMaps(in map[string]map[string]string) map[string]map[string]string { + out := make(map[string]map[string]string, len(in)) + for arn, tags := range in { + out[arn] = copyTags(tags) + } + + return out +} diff --git a/providers/aws/keyspaces/snapshot_test.go b/providers/aws/keyspaces/snapshot_test.go index d626540f6..0e9eb712a 100644 --- a/providers/aws/keyspaces/snapshot_test.go +++ b/providers/aws/keyspaces/snapshot_test.go @@ -2,6 +2,10 @@ package keyspaces import ( "context" + "encoding/json" + "reflect" + "strconv" + "strings" "testing" ksdriver "github.com/stackshy/cloudemu/v2/services/keyspaces/driver" @@ -62,3 +66,111 @@ func TestSnapshotRoundTripKeyspaces(t *testing.T) { t.Fatalf("restored tags = %+v", tags) } } + +// Snapshot must not hand json.Marshal the live tag maps: TagResource mutates +// them in place, so a concurrent tag write raced the marshal (go test -race). +func TestSnapshotConcurrentWithTagResource(t *testing.T) { + ctx := context.Background() + m := newTestMock() + mustKeyspace(t, m, "app") + + arn := m.keyspaceARN("app") + if err := m.TagResource(ctx, arn, map[string]string{"env": "prod"}); err != nil { + t.Fatalf("TagResource: %v", err) + } + + done := make(chan struct{}) + + go func() { + defer close(done) + + for i := 0; i < 500; i++ { + _ = m.TagResource(ctx, arn, map[string]string{"k" + strconv.Itoa(i%50): "v"}) + } + }() + + for i := 0; i < 200; i++ { + if _, err := m.Snapshot(ctx, false); err != nil { + t.Fatalf("Snapshot: %v", err) + } + } + + <-done +} + +// A snapshot written by an older build can hold tags under a non-canonical ARN +// (the slash-less table form) or under an ARN that names no resource. Restore +// moves the first onto the canonical ARN, so the tags stay reachable and are +// dropped with the table, and discards the second, so a resource created later +// under that ARN doesn't inherit them. +func TestRestoreCanonicalizesLegacyTagARNs(t *testing.T) { + ctx := context.Background() + src := newTestMock() + mustKeyspace(t, src, "app") + + if _, err := src.CreateTable(ctx, ksdriver.CreateTableConfig{ + KeyspaceName: "app", Name: "t1", SchemaDefinition: sampleSchema(), + }); err != nil { + t.Fatalf("CreateTable: %v", err) + } + + tableARN := src.tableARN("app", "t1") + ghostARN := src.keyspaceARN("ghost") + + raw, err := src.Snapshot(ctx, false) + if err != nil { + t.Fatalf("Snapshot: %v", err) + } + + var snap keyspacesSnapshot + if err := json.Unmarshal(raw, &snap); err != nil { + t.Fatalf("decode snapshot: %v", err) + } + + snap.Tags = map[string]map[string]string{ + tableARN: {"env": "prod"}, + strings.TrimSuffix(tableARN, "/"): {"env": "old", "team": "data"}, + ghostARN: {"stale": "yes"}, + src.keyspaceARN("app") + "extra/x/y": {"junk": "1"}, + } + + legacy, err := json.Marshal(snap) + if err != nil { + t.Fatalf("encode snapshot: %v", err) + } + + dst := newTestMock() + if err := dst.Restore(ctx, legacy); err != nil { + t.Fatalf("Restore: %v", err) + } + + got, err := dst.ListTagsForResource(ctx, tableARN) + if err != nil { + t.Fatalf("ListTagsForResource: %v", err) + } + + want := []ksdriver.Tag{{Key: "env", Value: "prod"}, {Key: "team", Value: "data"}} + if !reflect.DeepEqual(got, want) { + t.Fatalf("restored table tags = %+v, want %+v", got, want) + } + + if len(dst.tags) != 1 { + t.Fatalf("restored tag store = %+v, want only the canonical table entry", dst.tags) + } + + if _, err := dst.CreateKeyspace(ctx, ksdriver.CreateKeyspaceConfig{Name: "ghost"}); err != nil { + t.Fatalf("CreateKeyspace ghost: %v", err) + } + + if tags, err := dst.ListTagsForResource(ctx, ghostARN); err != nil || len(tags) != 0 { + t.Fatalf("new keyspace inherited stale tags: %+v, %v", tags, err) + } + + if err := dst.DeleteTable(ctx, "app", "t1"); err != nil { + t.Fatalf("DeleteTable: %v", err) + } + + if len(dst.tags) != 0 { + t.Fatalf("tags left after DeleteTable: %+v", dst.tags) + } +} diff --git a/providers/aws/keyspaces/tables.go b/providers/aws/keyspaces/tables.go index f4797578d..c97de5e36 100644 --- a/providers/aws/keyspaces/tables.go +++ b/providers/aws/keyspaces/tables.go @@ -117,6 +117,10 @@ func (m *Mock) CreateTable(_ context.Context, cfg ksdriver.CreateTableConfig) (* return nil, err } + if err := validateTags(cfg.Tags); err != nil { + return nil, err + } + if err := validateSchema(&cfg.SchemaDefinition); err != nil { return nil, err } @@ -125,7 +129,7 @@ func (m *Mock) CreateTable(_ context.Context, cfg ksdriver.CreateTableConfig) (* defer m.mu.Unlock() if !m.keyspaces.Has(cfg.KeyspaceName) { - return nil, cerrors.Newf(cerrors.InvalidArgument, "keyspace %q not found", cfg.KeyspaceName) + return nil, cerrors.Newf(cerrors.NotFound, "keyspace %q not found", cfg.KeyspaceName) } key := tableKey(cfg.KeyspaceName, cfg.Name) @@ -346,6 +350,10 @@ func (m *Mock) RestoreTable(_ context.Context, cfg ksdriver.RestoreTableConfig) return nil, err } + if err := validateTags(cfg.Tags); err != nil { + return nil, err + } + if cfg.RestoreTimestamp.IsZero() { return nil, cerrors.New(cerrors.InvalidArgument, "restoreTimestamp is required") } @@ -363,7 +371,7 @@ func (m *Mock) RestoreTable(_ context.Context, cfg ksdriver.RestoreTableConfig) } if !m.keyspaces.Has(cfg.TargetKeyspace) { - return nil, cerrors.Newf(cerrors.InvalidArgument, "target keyspace %q not found", cfg.TargetKeyspace) + return nil, cerrors.Newf(cerrors.NotFound, "target keyspace %q not found", cfg.TargetKeyspace) } targetKey := tableKey(cfg.TargetKeyspace, cfg.TargetTable) diff --git a/providers/aws/keyspaces/tags.go b/providers/aws/keyspaces/tags.go index 27ec4869c..531727c20 100644 --- a/providers/aws/keyspaces/tags.go +++ b/providers/aws/keyspaces/tags.go @@ -2,7 +2,10 @@ package keyspaces import ( "context" + "regexp" "sort" + "strings" + "unicode/utf8" cerrors "github.com/stackshy/cloudemu/v2/errors" ksdriver "github.com/stackshy/cloudemu/v2/services/keyspaces/driver" @@ -26,26 +29,164 @@ func (m *Mock) setTags(arn string, tags map[string]string) { m.tags[arn] = cur } -// TagResource adds tags to a resource by ARN. -func (m *Mock) TagResource(_ context.Context, arn string, tags map[string]string) error { +// resourceARNPattern is the documented resourceArn pattern for the tagging +// operations (arn:(aws[a-zA-Z0-9-]*):cassandra:.+.*, length 20-1000). +var resourceARNPattern = regexp.MustCompile(`^arn:aws[a-zA-Z0-9-]*:cassandra:.+`) + +// tagCharsPattern is the documented tag key/value character set: letters, +// white space, numbers, and the characters + - = . _ : and /. +var tagCharsPattern = regexp.MustCompile(`^[\p{L}\p{Z}\p{N}+\-=._:/]+$`) + +const ( + minResourceARNLen = 20 + maxResourceARNLen = 1000 + maxTagKeyLen = 128 + maxTagValueLen = 256 + // maxTagListLen is the documented TagList maximum (Array Members: Maximum + // number of 60 items) on TagResource, UntagResource, CreateKeyspace, + // CreateTable and RestoreTable. + maxTagListLen = 60 + reservedTagPrefix = "aws:" + arnResourceMarker = ":/keyspace/" + arnTableSegment = "table" +) + +// validateTags applies the documented Amazon Keyspaces tag restrictions: key +// 1-128 and value 1-256 Unicode characters (a value can't be empty), only +// letters, white space, numbers and + - = . _ : /, and no user-assigned keys +// with the reserved "aws:" prefix. A request carries at most 60 tags. +func validateTags(tags map[string]string) error { + if len(tags) > maxTagListLen { + return cerrors.Newf(cerrors.InvalidArgument, "tags must contain at most %d items", maxTagListLen) + } + + for k, v := range tags { + if n := utf8.RuneCountInString(k); n == 0 || n > maxTagKeyLen { + return cerrors.Newf(cerrors.InvalidArgument, "tag key %q must be 1-%d characters", k, maxTagKeyLen) + } + + if n := utf8.RuneCountInString(v); n == 0 || n > maxTagValueLen { + return cerrors.Newf(cerrors.InvalidArgument, + "tag value for key %q must be 1-%d characters", k, maxTagValueLen) + } + + if strings.HasPrefix(strings.ToLower(k), reservedTagPrefix) { + return cerrors.Newf(cerrors.InvalidArgument, "tag key %q uses the reserved prefix %q", k, reservedTagPrefix) + } + + if !tagCharsPattern.MatchString(k) || !tagCharsPattern.MatchString(v) { + return cerrors.Newf(cerrors.InvalidArgument, + "tag %q contains characters outside letters, numbers, white space and + - = . _ : /", k) + } + } + + return nil +} + +// resolveTaggableLocked validates a resource ARN and returns the canonical ARN +// of the existing keyspace or table it names. Real Keyspaces rejects a +// malformed ARN with ValidationException and an unknown resource with +// ResourceNotFoundException. The trailing "/" is optional, so both the form +// this emulator returns and the slash-less table form in the AWS docs resolve +// to the same resource. The caller holds the lock. +func (m *Mock) resolveTaggableLocked(arn string) (string, error) { + if err := validateResourceARN(arn); err != nil { + return "", err + } + + canonical := m.existingResourceARN(arn) + + // The region/account (and partition) must match this emulator's too. + if canonical == "" || (arn != canonical && arn+"/" != canonical) { + return "", cerrors.Newf(cerrors.NotFound, "resource %q not found", arn) + } + + return canonical, nil +} + +// validateResourceARN checks the documented resourceArn length and pattern. +func validateResourceARN(arn string) error { if arn == "" { return cerrors.New(cerrors.InvalidArgument, "resourceArn is required") } + if len(arn) < minResourceARNLen || len(arn) > maxResourceARNLen || !resourceARNPattern.MatchString(arn) { + return cerrors.Newf(cerrors.InvalidArgument, "resourceArn %q is not a valid Amazon Keyspaces ARN", arn) + } + + return nil +} + +// existingResourceARN returns the canonical ARN of the keyspace +// (".../keyspace//") or table (".../keyspace//table//") that arn's +// resource path names, or "" when the path names no existing keyspace or +// table. The caller holds the lock. +func (m *Mock) existingResourceARN(arn string) string { + idx := strings.Index(arn, arnResourceMarker) + if idx < 0 { + return "" + } + + parts := strings.Split(strings.TrimSuffix(arn[idx+len(arnResourceMarker):], "/"), "/") + + switch { + case len(parts) == 1 && m.keyspaces.Has(parts[0]): + return m.keyspaceARN(parts[0]) + case len(parts) == 3 && parts[1] == arnTableSegment && m.tables.Has(tableKey(parts[0], parts[2])): + return m.tableARN(parts[0], parts[2]) + default: + return "" + } +} + +// TagResource adds tags to an existing keyspace or table by ARN. +func (m *Mock) TagResource(_ context.Context, arn string, tags map[string]string) error { + if len(tags) == 0 { + return cerrors.New(cerrors.InvalidArgument, "tags must contain at least one tag") + } + + if err := validateTags(tags); err != nil { + return err + } + m.mu.Lock() defer m.mu.Unlock() - m.setTags(arn, tags) + canonical, err := m.resolveTaggableLocked(arn) + if err != nil { + return err + } + + m.setTags(canonical, tags) return nil } -// UntagResource removes tag keys from a resource by ARN. +// UntagResource removes tag keys from an existing keyspace or table by ARN. func (m *Mock) UntagResource(_ context.Context, arn string, keys []string) error { + if len(keys) == 0 { + return cerrors.New(cerrors.InvalidArgument, "tags must contain at least one tag") + } + + if len(keys) > maxTagListLen { + return cerrors.Newf(cerrors.InvalidArgument, "tags must contain at most %d items", maxTagListLen) + } + + for _, k := range keys { + if n := utf8.RuneCountInString(k); n == 0 || n > maxTagKeyLen { + return cerrors.Newf(cerrors.InvalidArgument, "tag key %q must be 1-%d characters", k, maxTagKeyLen) + } + } + m.mu.Lock() defer m.mu.Unlock() - cur := m.tags[arn] + canonical, err := m.resolveTaggableLocked(arn) + if err != nil { + return err + } + + cur := m.tags[canonical] for _, k := range keys { delete(cur, k) } @@ -53,13 +194,18 @@ func (m *Mock) UntagResource(_ context.Context, arn string, keys []string) error return nil } -// ListTagsForResource returns a resource's tags in deterministic (sorted) -// order (AWS leaves tag order unspecified). +// ListTagsForResource returns an existing resource's tags in deterministic +// (sorted) order (AWS leaves tag order unspecified). func (m *Mock) ListTagsForResource(_ context.Context, arn string) ([]ksdriver.Tag, error) { m.mu.RLock() defer m.mu.RUnlock() - tags := m.tags[arn] + canonical, err := m.resolveTaggableLocked(arn) + if err != nil { + return nil, err + } + + tags := m.tags[canonical] names := make([]string, 0, len(tags)) for k := range tags { diff --git a/providers/aws/keyspaces/tags_test.go b/providers/aws/keyspaces/tags_test.go new file mode 100644 index 000000000..7c7dbe9b6 --- /dev/null +++ b/providers/aws/keyspaces/tags_test.go @@ -0,0 +1,223 @@ +package keyspaces + +import ( + "context" + "strconv" + "strings" + "testing" + "time" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + ksdriver "github.com/stackshy/cloudemu/v2/services/keyspaces/driver" +) + +// Tagging operations require an existing keyspace or table and a well-formed +// Keyspaces ARN. +func TestTagsRequireExistingResource(t *testing.T) { + m := newTestMock() + ctx := context.Background() + mustKeyspace(t, m, "app") + + ghostKS := m.keyspaceARN("ghost") + ghostTable := m.tableARN("app", "ghost") + + for _, arn := range []string{ghostKS, ghostTable} { + if err := m.TagResource(ctx, arn, map[string]string{"k": "v"}); !cerrors.IsNotFound(err) { + t.Fatalf("TagResource(%s): got %v, want NotFound", arn, err) + } + + if err := m.UntagResource(ctx, arn, []string{"k"}); !cerrors.IsNotFound(err) { + t.Fatalf("UntagResource(%s): got %v, want NotFound", arn, err) + } + + if _, err := m.ListTagsForResource(ctx, arn); !cerrors.IsNotFound(err) { + t.Fatalf("ListTagsForResource(%s): got %v, want NotFound", arn, err) + } + } + + if len(m.tags) != 0 { + t.Fatalf("tags stored for missing resources: %v", m.tags) + } + + for _, arn := range []string{"", "not-an-arn", "arn:aws:dynamodb:us-east-1:123456789012:table/x"} { + if err := m.TagResource(ctx, arn, map[string]string{"k": "v"}); !cerrors.IsInvalidArgument(err) { + t.Fatalf("TagResource(%q): got %v, want InvalidArgument", arn, err) + } + } + + // Existing keyspace and table ARNs still work, including after deletion + // turning them into NotFound. + if _, err := m.CreateTable(ctx, ksdriver.CreateTableConfig{ + KeyspaceName: "app", Name: "t", SchemaDefinition: sampleSchema(), + }); err != nil { + t.Fatalf("CreateTable: %v", err) + } + + tableARN := m.tableARN("app", "t") + for _, arn := range []string{m.keyspaceARN("app"), tableARN} { + if err := m.TagResource(ctx, arn, map[string]string{"k": "v"}); err != nil { + t.Fatalf("TagResource(%s): %v", arn, err) + } + + if tags, err := m.ListTagsForResource(ctx, arn); err != nil || len(tags) != 1 { + t.Fatalf("ListTagsForResource(%s): %v %v", arn, tags, err) + } + } + + if err := m.DeleteTable(ctx, "app", "t"); err != nil { + t.Fatalf("DeleteTable: %v", err) + } + + if _, err := m.ListTagsForResource(ctx, tableARN); !cerrors.IsNotFound(err) { + t.Fatalf("ListTagsForResource after delete: got %v, want NotFound", err) + } +} + +// Tag keys/values follow the documented Keyspaces tag restrictions on every +// path that accepts tags. +func TestTagRestrictions(t *testing.T) { + m := newTestMock() + ctx := context.Background() + mustKeyspace(t, m, "app") + + arn := m.keyspaceARN("app") + + bad := []map[string]string{ + {"aws:owner": "x"}, + {"AWS:owner": "x"}, + {"": "x"}, + {"k": ""}, + {strings.Repeat("k", maxTagKeyLen+1): "v"}, + {"k": strings.Repeat("v", maxTagValueLen+1)}, + {"k*": "v"}, + {"k": "v@x"}, + } + + for _, tags := range bad { + if err := m.TagResource(ctx, arn, tags); !cerrors.IsInvalidArgument(err) { + t.Fatalf("TagResource(%v): got %v, want InvalidArgument", tags, err) + } + + if _, err := m.CreateKeyspace(ctx, ksdriver.CreateKeyspaceConfig{Name: "k2", Tags: tags}); !cerrors.IsInvalidArgument(err) { + t.Fatalf("CreateKeyspace tags %v: got %v, want InvalidArgument", tags, err) + } + + if _, err := m.CreateTable(ctx, ksdriver.CreateTableConfig{ + KeyspaceName: "app", Name: "t2", SchemaDefinition: sampleSchema(), Tags: tags, + }); !cerrors.IsInvalidArgument(err) { + t.Fatalf("CreateTable tags %v: got %v, want InvalidArgument", tags, err) + } + } + + if err := m.TagResource(ctx, arn, nil); !cerrors.IsInvalidArgument(err) { + t.Fatalf("TagResource no tags: got %v, want InvalidArgument", err) + } + + if err := m.UntagResource(ctx, arn, nil); !cerrors.IsInvalidArgument(err) { + t.Fatalf("UntagResource no tags: got %v, want InvalidArgument", err) + } + + good := map[string]string{ + strings.Repeat("k", maxTagKeyLen): strings.Repeat("v", maxTagValueLen), + "team name": "data + ml = a/b_c:d.e-f", + "ะบะปัŽั‡": "ะทะฝะฐั‡ะตะฝะธะต1", + "awsish": "ok", + } + if err := m.TagResource(ctx, arn, good); err != nil { + t.Fatalf("TagResource valid: %v", err) + } + + if tags, err := m.ListTagsForResource(ctx, arn); err != nil || len(tags) != len(good) { + t.Fatalf("ListTagsForResource: %v %v", tags, err) + } +} + +// The documented TagList bound (at most 60 items per request) applies to the +// library path too, not only to the wire handler, on every operation that +// takes tags. Exactly 60 is accepted. +func TestTagListBound(t *testing.T) { + m := newTestMock() + ctx := context.Background() + mustKeyspace(t, m, "app") + + arn := m.keyspaceARN("app") + + tooMany := make(map[string]string, maxTagListLen+1) + keys := make([]string, 0, maxTagListLen+1) + + for i := 0; i <= maxTagListLen; i++ { + k := "k" + strconv.Itoa(i) + tooMany[k] = "v" + keys = append(keys, k) + } + + if err := m.TagResource(ctx, arn, tooMany); !cerrors.IsInvalidArgument(err) { + t.Fatalf("TagResource 61 tags: got %v, want InvalidArgument", err) + } + + if err := m.UntagResource(ctx, arn, keys); !cerrors.IsInvalidArgument(err) { + t.Fatalf("UntagResource 61 keys: got %v, want InvalidArgument", err) + } + + if _, err := m.CreateKeyspace(ctx, ksdriver.CreateKeyspaceConfig{Name: "k2", Tags: tooMany}); !cerrors.IsInvalidArgument(err) { + t.Fatalf("CreateKeyspace 61 tags: got %v, want InvalidArgument", err) + } + + if _, err := m.CreateTable(ctx, ksdriver.CreateTableConfig{ + KeyspaceName: "app", Name: "t2", SchemaDefinition: sampleSchema(), Tags: tooMany, + }); !cerrors.IsInvalidArgument(err) { + t.Fatalf("CreateTable 61 tags: got %v, want InvalidArgument", err) + } + + if _, err := m.RestoreTable(ctx, ksdriver.RestoreTableConfig{ + SourceKeyspace: "app", SourceTable: "t1", TargetKeyspace: "app", TargetTable: "t3", + RestoreTimestamp: time.Now(), Tags: tooMany, + }); !cerrors.IsInvalidArgument(err) { + t.Fatalf("RestoreTable 61 tags: got %v, want InvalidArgument", err) + } + + delete(tooMany, "k0") + + if err := m.TagResource(ctx, arn, tooMany); err != nil { + t.Fatalf("TagResource 60 tags: %v", err) + } + + if err := m.UntagResource(ctx, arn, keys[1:]); err != nil { + t.Fatalf("UntagResource 60 keys: %v", err) + } +} + +// The trailing "/" on a resource ARN is optional; both forms address the same +// resource, and an ARN for another account or region is not found. +func TestTagARNForms(t *testing.T) { + m := newTestMock() + ctx := context.Background() + mustKeyspace(t, m, "app") + + if _, err := m.CreateTable(ctx, ksdriver.CreateTableConfig{ + KeyspaceName: "app", Name: "t", SchemaDefinition: sampleSchema(), + }); err != nil { + t.Fatalf("CreateTable: %v", err) + } + + noSlash := strings.TrimSuffix(m.tableARN("app", "t"), "/") + if err := m.TagResource(ctx, noSlash, map[string]string{"k": "v"}); err != nil { + t.Fatalf("TagResource without trailing slash: %v", err) + } + + tags, err := m.ListTagsForResource(ctx, m.tableARN("app", "t")) + if err != nil || len(tags) != 1 { + t.Fatalf("ListTagsForResource canonical: %v %v", tags, err) + } + + for _, arn := range []string{ + "arn:aws:cassandra:us-west-2:123456789012:/keyspace/app/", + "arn:aws:cassandra:us-east-1:999999999999:/keyspace/app/", + "arn:aws:cassandra:us-east-1:123456789012:/keyspace/app/table/", + "arn:aws:cassandra:us-east-1:123456789012:/keyspace/app/type/t/", + } { + if _, err := m.ListTagsForResource(ctx, arn); !cerrors.IsNotFound(err) { + t.Fatalf("ListTagsForResource(%s): got %v, want NotFound", arn, err) + } + } +} diff --git a/providers/aws/keyspaces/udt.go b/providers/aws/keyspaces/udt.go index 5f32f0e98..6bd9365a1 100644 --- a/providers/aws/keyspaces/udt.go +++ b/providers/aws/keyspaces/udt.go @@ -101,7 +101,7 @@ func cloneUDT(in *ksdriver.UDT) ksdriver.UDT { // CreateType creates a user-defined type in an existing keyspace. func (m *Mock) CreateType(_ context.Context, keyspace, name string, fields []ksdriver.FieldDefinition) (*ksdriver.UDT, error) { - if err := validName("type", name); err != nil { + if err := validTypeName(name); err != nil { return nil, err } @@ -113,7 +113,7 @@ func (m *Mock) CreateType(_ context.Context, keyspace, name string, fields []ksd defer m.mu.Unlock() if !m.keyspaces.Has(keyspace) { - return nil, cerrors.Newf(cerrors.InvalidArgument, "keyspace %q not found", keyspace) + return nil, cerrors.Newf(cerrors.NotFound, "keyspace %q not found", keyspace) } key := typeKey(keyspace, name) diff --git a/providers/aws/rgt_discovery.go b/providers/aws/rgt_discovery.go index 2c263bba6..cd2d207e4 100644 --- a/providers/aws/rgt_discovery.go +++ b/providers/aws/rgt_discovery.go @@ -3,6 +3,7 @@ package aws import ( "context" + cerrors "github.com/stackshy/cloudemu/v2/errors" "github.com/stackshy/cloudemu/v2/internal/idgen" acmdriver "github.com/stackshy/cloudemu/v2/services/acm/driver" cloudtraildriver "github.com/stackshy/cloudemu/v2/services/cloudtrail/driver" @@ -419,6 +420,12 @@ func (d taggedDiscovery) discoverConfig(ctx context.Context) ([]resourcediscover // collected rows. It factors out the identical enumerate โ†’ read-tags โ†’ row loop // the single-list discoverers share. The projection returns its error last so a // tag-read failure inside fn short-circuits the walk with that error. +// +// A NotFound from fn means the resource was deleted between the list call and +// its tag read. It is skipped, the same as a resource that was never listed, +// so a delete racing GetResources doesn't fail the whole inventory (the +// resourcediscovery walkers handle this race the same way). Any other error +// still propagates. func project[T any](ctx context.Context, items []T, fn func(context.Context, T) (resourcediscovery.DiscoveredResource, error), ) ([]resourcediscovery.DiscoveredResource, error) { @@ -426,6 +433,10 @@ func project[T any](ctx context.Context, items []T, for i := range items { r, err := fn(ctx, items[i]) + if cerrors.IsNotFound(err) { + continue + } + if err != nil { return nil, err } diff --git a/providers/aws/rgt_discovery_test.go b/providers/aws/rgt_discovery_test.go new file mode 100644 index 000000000..3a1255891 --- /dev/null +++ b/providers/aws/rgt_discovery_test.go @@ -0,0 +1,105 @@ +package aws + +import ( + "context" + "errors" + "sync" + "testing" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + ksdriver "github.com/stackshy/cloudemu/v2/services/keyspaces/driver" + "github.com/stackshy/cloudemu/v2/services/resourcediscovery" +) + +// A resource deleted between the list call and its tag read makes the tag read +// return NotFound. project must drop that resource instead of failing, since +// the error would otherwise abort DiscoverResources for every service. Any +// other error must still propagate. +func TestProjectSkipsVanishedResource(t *testing.T) { + ctx := context.Background() + boom := errors.New("boom") + + read := func(_ context.Context, name string) (resourcediscovery.DiscoveredResource, error) { + switch name { + case "gone": + return resourcediscovery.DiscoveredResource{}, cerrors.Newf(cerrors.NotFound, "resource %q not found", name) + case "broken": + return resourcediscovery.DiscoveredResource{}, boom + default: + return row("svc", "thing", name, "arn:"+name, nil), nil + } + } + + rows, err := project(ctx, []string{"a", "gone", "b"}, read) + if err != nil { + t.Fatalf("project with a vanished resource: %v", err) + } + + if len(rows) != 2 || rows[0].ID != "a" || rows[1].ID != "b" { + t.Fatalf("rows = %+v, want a and b only", rows) + } + + if _, err := project(ctx, []string{"a", "broken"}, read); !errors.Is(err, boom) { + t.Fatalf("project with a failing read: err = %v, want boom", err) + } +} + +// A DeleteKeyspace racing the Keyspaces discovery walk must not fail it. +// ListTagsForResource returns NotFound for a keyspace that is gone, so without +// the skip in project nearly every walk that overlaps the delete loop fails. +func TestKeyspacesDiscoverySurvivesConcurrentDelete(t *testing.T) { + ctx := context.Background() + p := New() + d := taggedDiscovery{p} + + if _, err := p.Keyspaces.CreateKeyspace(ctx, ksdriver.CreateKeyspaceConfig{ + Name: "stable", Tags: map[string]string{"env": "prod"}, + }); err != nil { + t.Fatalf("CreateKeyspace stable: %v", err) + } + + stop := make(chan struct{}) + + var wg sync.WaitGroup + + wg.Add(1) + + go func() { + defer wg.Done() + + for { + select { + case <-stop: + return + default: + } + + _, _ = p.Keyspaces.CreateKeyspace(ctx, ksdriver.CreateKeyspaceConfig{Name: "churn"}) + _ = p.Keyspaces.DeleteKeyspace(ctx, "churn") + } + }() + + defer func() { + close(stop) + wg.Wait() + }() + + for i := 0; i < 2000; i++ { + rows, err := d.discoverKeyspaces(ctx) + if err != nil { + t.Fatalf("walk %d: discoverKeyspaces: %v", i, err) + } + + var found bool + + for _, r := range rows { + if r.ID == "stable" && r.Tags["env"] == "prod" { + found = true + } + } + + if !found { + t.Fatalf("walk %d: stable keyspace missing or untagged: %+v", i, rows) + } + } +} diff --git a/server/aws/keyspaces/keyspaces.go b/server/aws/keyspaces/keyspaces.go index 35f4871bf..b1ed93707 100644 --- a/server/aws/keyspaces/keyspaces.go +++ b/server/aws/keyspaces/keyspaces.go @@ -17,6 +17,10 @@ func (h *Handler) createKeyspace(w http.ResponseWriter, r *http.Request) { return } + if rejectTagListTooLong(w, len(in.Tags)) { + return + } + cfg := ksdriver.CreateKeyspaceConfig{Name: aws.ToString(in.KeyspaceName), Tags: tagMap(in.Tags)} if in.ReplicationSpecification != nil { cfg.ReplicationStrategy = string(in.ReplicationSpecification.ReplicationStrategy) diff --git a/server/aws/keyspaces/sdk_roundtrip_test.go b/server/aws/keyspaces/sdk_roundtrip_test.go index eaa054a9b..a257eeb4a 100644 --- a/server/aws/keyspaces/sdk_roundtrip_test.go +++ b/server/aws/keyspaces/sdk_roundtrip_test.go @@ -77,8 +77,8 @@ func TestSDKKeyspaceLifecycle(t *testing.T) { t.Fatalf("ListKeyspaces: %v", err) } - if len(list.Keyspaces) != 4 { // 3 system + app - t.Fatalf("expected 4 keyspaces, got %d", len(list.Keyspaces)) + if len(list.Keyspaces) != 5 { // 4 system + app + t.Fatalf("expected 5 keyspaces, got %d", len(list.Keyspaces)) } if _, err := client.DeleteKeyspace(ctx, &awskeyspaces.DeleteKeyspaceInput{KeyspaceName: aws.String("app")}); err != nil { diff --git a/server/aws/keyspaces/tables.go b/server/aws/keyspaces/tables.go index c981abdc4..7d80b49cb 100644 --- a/server/aws/keyspaces/tables.go +++ b/server/aws/keyspaces/tables.go @@ -60,6 +60,10 @@ func (h *Handler) createTable(w http.ResponseWriter, r *http.Request) { return } + if rejectTagListTooLong(w, len(in.Tags)) { + return + } + cfg := ksdriver.CreateTableConfig{ KeyspaceName: aws.ToString(in.KeyspaceName), Name: aws.ToString(in.TableName), @@ -248,6 +252,10 @@ func (h *Handler) restoreTable(w http.ResponseWriter, r *http.Request) { return } + if rejectTagListTooLong(w, len(in.TagsOverride)) { + return + } + cfg := ksdriver.RestoreTableConfig{ SourceKeyspace: aws.ToString(in.SourceKeyspaceName), SourceTable: aws.ToString(in.SourceTableName), diff --git a/server/aws/keyspaces/tables_test.go b/server/aws/keyspaces/tables_test.go new file mode 100644 index 000000000..4922733c9 --- /dev/null +++ b/server/aws/keyspaces/tables_test.go @@ -0,0 +1,120 @@ +package keyspaces_test + +import ( + "context" + "encoding/json" + "errors" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/aws/aws-sdk-go-v2/aws" + awskeyspaces "github.com/aws/aws-sdk-go-v2/service/keyspaces" + kstypes "github.com/aws/aws-sdk-go-v2/service/keyspaces/types" + + "github.com/stackshy/cloudemu/v2/config" + ksprovider "github.com/stackshy/cloudemu/v2/providers/aws/keyspaces" + ksserver "github.com/stackshy/cloudemu/v2/server/aws/keyspaces" +) + +// Keyspace/table names must be 1-48 chars matching [a-zA-Z0-9][a-zA-Z0-9_]*; +// a missing parent keyspace is ResourceNotFoundException. +func TestSDKNameValidationAndMissingKeyspace(t *testing.T) { + client := newSDKClient(t) + ctx := context.Background() + + var ve *kstypes.ValidationException + + for _, n := range []string{strings.Repeat("a", 49), "bad-name", "_lead"} { + _, err := client.CreateKeyspace(ctx, &awskeyspaces.CreateKeyspaceInput{KeyspaceName: aws.String(n)}) + if !errors.As(err, &ve) { + t.Fatalf("CreateKeyspace(%q): got %v, want ValidationException", n, err) + } + } + + if _, err := client.CreateKeyspace(ctx, &awskeyspaces.CreateKeyspaceInput{KeyspaceName: aws.String("1app")}); err != nil { + t.Fatalf("CreateKeyspace leading digit: %v", err) + } + + _, err := client.CreateTable(ctx, &awskeyspaces.CreateTableInput{ + KeyspaceName: aws.String("1app"), TableName: aws.String(strings.Repeat("t", 49)), SchemaDefinition: sampleSchema(), + }) + if !errors.As(err, &ve) { + t.Fatalf("CreateTable too-long name: got %v, want ValidationException", err) + } + + var rnf *kstypes.ResourceNotFoundException + + _, err = client.CreateTable(ctx, &awskeyspaces.CreateTableInput{ + KeyspaceName: aws.String("ghost"), TableName: aws.String("t"), SchemaDefinition: sampleSchema(), + }) + if !errors.As(err, &rnf) { + t.Fatalf("CreateTable missing keyspace: got %v, want ResourceNotFoundException", err) + } +} + +// GetTable on a PAY_PER_REQUEST table without CDC omits readCapacityUnits, +// writeCapacityUnits, and cdcSpecification.viewType; PROVISIONED keeps them. +func TestGetTableOmitsUnsetFields(t *testing.T) { + opts := config.NewOptions(config.WithRegion("us-east-1"), config.WithAccountID("123456789012")) + h := ksserver.New(ksprovider.New(opts)) + + call := func(op, body string) map[string]any { + t.Helper() + + req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(body)) + req.Header.Set("X-Amz-Target", "KeyspacesService."+op) + req.Header.Set("Content-Type", "application/x-amz-json-1.0") + + rec := httptest.NewRecorder() + h.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("%s: status %d: %s", op, rec.Code, rec.Body.String()) + } + + var out map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil { + t.Fatalf("%s: decode: %v", op, err) + } + + return out + } + + schema := `"schemaDefinition":{"allColumns":[{"name":"id","type":"int"}],"partitionKeys":[{"name":"id"}]}` + + call("CreateKeyspace", `{"keyspaceName":"app"}`) + call("CreateTable", `{"keyspaceName":"app","tableName":"ppr",`+schema+`}`) + call("CreateTable", `{"keyspaceName":"app","tableName":"prov",`+schema+ + `,"capacitySpecification":{"throughputMode":"PROVISIONED","readCapacityUnits":5,"writeCapacityUnits":7}}`) + + ppr := call("GetTable", `{"keyspaceName":"app","tableName":"ppr"}`) + + capSpec, _ := ppr["capacitySpecification"].(map[string]any) + if capSpec["throughputMode"] != "PAY_PER_REQUEST" { + t.Fatalf("ppr throughputMode: %v", capSpec) + } + + for _, k := range []string{"readCapacityUnits", "writeCapacityUnits"} { + if _, ok := capSpec[k]; ok { + t.Fatalf("ppr capacitySpecification has %s: %v", k, capSpec) + } + } + + cdc, _ := ppr["cdcSpecification"].(map[string]any) + if cdc["status"] != "DISABLED" { + t.Fatalf("cdc status: %v", cdc) + } + + if _, ok := cdc["viewType"]; ok { + t.Fatalf("cdcSpecification has empty viewType: %v", cdc) + } + + prov := call("GetTable", `{"keyspaceName":"app","tableName":"prov"}`) + + pcap, _ := prov["capacitySpecification"].(map[string]any) + if pcap["readCapacityUnits"] != float64(5) || pcap["writeCapacityUnits"] != float64(7) { + t.Fatalf("provisioned capacity: %v", pcap) + } +} diff --git a/server/aws/keyspaces/tags.go b/server/aws/keyspaces/tags.go index 069aac061..8196b06bf 100644 --- a/server/aws/keyspaces/tags.go +++ b/server/aws/keyspaces/tags.go @@ -1,6 +1,7 @@ package keyspaces import ( + "fmt" "net/http" "github.com/aws/aws-sdk-go-v2/aws" @@ -15,6 +16,10 @@ func (h *Handler) tagResource(w http.ResponseWriter, r *http.Request) { return } + if rejectTagListTooLong(w, len(in.Tags)) { + return + } + if err := h.db.TagResource(r.Context(), aws.ToString(in.ResourceArn), tagMap(in.Tags)); err != nil { writeErr(w, err) return @@ -29,6 +34,10 @@ func (h *Handler) untagResource(w http.ResponseWriter, r *http.Request) { return } + if rejectTagListTooLong(w, len(in.Tags)) { + return + } + keys := make([]string, 0, len(in.Tags)) for i := range in.Tags { keys = append(keys, aws.ToString(in.Tags[i].Key)) @@ -62,3 +71,23 @@ func (h *Handler) listTagsForResource(w http.ResponseWriter, r *http.Request) { writeJSON(w, keyspaces.ListTagsForResourceOutput{Tags: toWireTags(page), NextToken: next}) } + +// maxTagListLen is the documented TagList maximum (Array Members: Maximum +// number of 60 items) on TagResource, UntagResource, CreateKeyspace, +// CreateTable and RestoreTable. The provider applies the same bound to the +// tags it receives; this check runs on the raw wire list, which can repeat a +// key and so be longer than the map the provider sees. +const maxTagListLen = 60 + +// rejectTagListTooLong writes a ValidationException and returns true when a +// request's tag list exceeds the documented TagList size. +func rejectTagListTooLong(w http.ResponseWriter, n int) bool { + if n <= maxTagListLen { + return false + } + + wire.WriteJSONError(w, http.StatusBadRequest, "ValidationException", + fmt.Sprintf("tags must contain at most %d items", maxTagListLen)) + + return true +} diff --git a/server/aws/keyspaces/tags_test.go b/server/aws/keyspaces/tags_test.go new file mode 100644 index 000000000..759b42d4b --- /dev/null +++ b/server/aws/keyspaces/tags_test.go @@ -0,0 +1,100 @@ +package keyspaces_test + +import ( + "context" + "errors" + "strconv" + "testing" + + "github.com/aws/aws-sdk-go-v2/aws" + awskeyspaces "github.com/aws/aws-sdk-go-v2/service/keyspaces" + kstypes "github.com/aws/aws-sdk-go-v2/service/keyspaces/types" +) + +// Tagging a keyspace/table ARN that doesn't exist is ResourceNotFoundException. +func TestSDKTagsOnMissingResource(t *testing.T) { + client := newSDKClient(t) + ctx := context.Background() + + if _, err := client.CreateKeyspace(ctx, &awskeyspaces.CreateKeyspaceInput{KeyspaceName: aws.String("app")}); err != nil { + t.Fatalf("CreateKeyspace: %v", err) + } + + ghost := aws.String("arn:aws:cassandra:us-east-1:123456789012:/keyspace/ghost/") + tags := []kstypes.Tag{{Key: aws.String("k"), Value: aws.String("v")}} + + var rnf *kstypes.ResourceNotFoundException + + _, err := client.TagResource(ctx, &awskeyspaces.TagResourceInput{ResourceArn: ghost, Tags: tags}) + if !errors.As(err, &rnf) { + t.Fatalf("TagResource: got %v, want ResourceNotFoundException", err) + } + + _, err = client.UntagResource(ctx, &awskeyspaces.UntagResourceInput{ResourceArn: ghost, Tags: tags}) + if !errors.As(err, &rnf) { + t.Fatalf("UntagResource: got %v, want ResourceNotFoundException", err) + } + + _, err = client.ListTagsForResource(ctx, &awskeyspaces.ListTagsForResourceInput{ResourceArn: ghost}) + if !errors.As(err, &rnf) { + t.Fatalf("ListTagsForResource: got %v, want ResourceNotFoundException", err) + } + + var ve *kstypes.ValidationException + + _, err = client.TagResource(ctx, &awskeyspaces.TagResourceInput{ + ResourceArn: aws.String("arn:aws:dynamodb:us-east-1:123456789012:table/x"), Tags: tags, + }) + if !errors.As(err, &ve) { + t.Fatalf("TagResource non-cassandra ARN: got %v, want ValidationException", err) + } +} + +// Tag restrictions over the wire: reserved aws: prefix, empty value, and more +// than 60 tags in one request are ValidationException; the system_schema_mcs +// keyspace exists and can't be deleted. +func TestSDKTagRestrictionsAndSystemKeyspace(t *testing.T) { + client := newSDKClient(t) + ctx := context.Background() + + out, err := client.CreateKeyspace(ctx, &awskeyspaces.CreateKeyspaceInput{KeyspaceName: aws.String("app")}) + if err != nil { + t.Fatalf("CreateKeyspace: %v", err) + } + + var ve *kstypes.ValidationException + + for _, tag := range []kstypes.Tag{ + {Key: aws.String("aws:owner"), Value: aws.String("v")}, + {Key: aws.String("k"), Value: aws.String("")}, + } { + _, err = client.TagResource(ctx, &awskeyspaces.TagResourceInput{ResourceArn: out.ResourceArn, Tags: []kstypes.Tag{tag}}) + if !errors.As(err, &ve) { + t.Fatalf("TagResource %q: got %v, want ValidationException", aws.ToString(tag.Key), err) + } + } + + many := make([]kstypes.Tag, 61) + for i := range many { + many[i] = kstypes.Tag{Key: aws.String("k" + strconv.Itoa(i)), Value: aws.String("v")} + } + + _, err = client.TagResource(ctx, &awskeyspaces.TagResourceInput{ResourceArn: out.ResourceArn, Tags: many}) + if !errors.As(err, &ve) { + t.Fatalf("TagResource 61 tags: got %v, want ValidationException", err) + } + + _, err = client.CreateKeyspace(ctx, &awskeyspaces.CreateKeyspaceInput{KeyspaceName: aws.String("app2"), Tags: many}) + if !errors.As(err, &ve) { + t.Fatalf("CreateKeyspace 61 tags: got %v, want ValidationException", err) + } + + if _, err := client.GetKeyspace(ctx, &awskeyspaces.GetKeyspaceInput{KeyspaceName: aws.String("system_schema_mcs")}); err != nil { + t.Fatalf("GetKeyspace system_schema_mcs: %v", err) + } + + _, err = client.DeleteKeyspace(ctx, &awskeyspaces.DeleteKeyspaceInput{KeyspaceName: aws.String("system_schema_mcs")}) + if !errors.As(err, &ve) { + t.Fatalf("DeleteKeyspace system_schema_mcs: got %v, want ValidationException", err) + } +} diff --git a/server/aws/keyspaces/types.go b/server/aws/keyspaces/types.go index 0dc7879e6..595ff1abb 100644 --- a/server/aws/keyspaces/types.go +++ b/server/aws/keyspaces/types.go @@ -30,12 +30,18 @@ func toWireSchema(s *ksdriver.SchemaDefinition) *types.SchemaDefinition { return out } +// toWireCapacity emits read/write capacity units only for PROVISIONED tables. +// Real Keyspaces omits them for PAY_PER_REQUEST (their documented valid range +// starts at 1, so 0 is never a real value). func toWireCapacity(c ksdriver.CapacitySpecification) *types.CapacitySpecificationSummary { - return &types.CapacitySpecificationSummary{ - ThroughputMode: types.ThroughputMode(c.ThroughputMode), - ReadCapacityUnits: aws.Int64(c.ReadCapacityUnits), - WriteCapacityUnits: aws.Int64(c.WriteCapacityUnits), + out := &types.CapacitySpecificationSummary{ThroughputMode: types.ThroughputMode(c.ThroughputMode)} + + if c.ThroughputMode == ksdriver.ThroughputProvisioned { + out.ReadCapacityUnits = aws.Int64(c.ReadCapacityUnits) + out.WriteCapacityUnits = aws.Int64(c.WriteCapacityUnits) } + + return out } func toWireEncryption(e ksdriver.EncryptionSpecification) *types.EncryptionSpecification { diff --git a/server/aws/keyspaces/wirejson.go b/server/aws/keyspaces/wirejson.go index 62312e986..118a56e61 100644 --- a/server/aws/keyspaces/wirejson.go +++ b/server/aws/keyspaces/wirejson.go @@ -32,23 +32,27 @@ func writeJSON(w http.ResponseWriter, v any) { wire.WriteJSON(w, lowerCamelKeys(decoded)) } +// omitEmptyEnum reports whether an empty value should be dropped because real +// Keyspaces omits the member when it is unset. It covers only +// cdcSpecification.viewType: the SDK models it as a non-pointer string enum, so +// encoding/json renders it as "" on a table without a CDC stream. In the +// Keyspaces SDK, ViewType appears only on CdcSpecification and +// CdcSpecificationSummary, so matching the key name is enough. This is not a +// general rule for empty strings: the decoded map no longer carries Go types, +// so a general rule would also drop non-enum members that are legitimately +// empty. Add another unset enum here by name if one turns up. +func omitEmptyEnum(key string, val any) bool { + return key == "ViewType" && val == "" +} + // lowerCamelKeys returns v with the first letter of every map key lowercased, -// recursively. It also drops null values and the SDK's always-empty -// resultMetadata envelope so the response carries only meaningful fields. +// recursively. It also drops null values, an unset cdcSpecification.viewType +// (see omitEmptyEnum), and the SDK's always-empty resultMetadata envelope so the +// response carries only meaningful fields. func lowerCamelKeys(v any) any { switch t := v.(type) { case map[string]any: - out := make(map[string]any, len(t)) - - for k, val := range t { - if val == nil || k == "ResultMetadata" { - continue - } - - out[lowerFirst(k)] = lowerCamelKeys(val) - } - - return out + return lowerCamelMap(t) case []any: for i := range t { t[i] = lowerCamelKeys(t[i]) @@ -69,6 +73,20 @@ func lowerCamelKeys(v any) any { } } +func lowerCamelMap(t map[string]any) map[string]any { + out := make(map[string]any, len(t)) + + for k, val := range t { + if val == nil || k == "ResultMetadata" || omitEmptyEnum(k, val) { + continue + } + + out[lowerFirst(k)] = lowerCamelKeys(val) + } + + return out +} + func lowerFirst(s string) string { if s == "" { return s From 8a5dfc123bfd4d5f1c249a491e85a4abda10aec6 Mon Sep 17 00:00:00 2001 From: Satyam-Trivedi-ZS Date: Fri, 23 Oct 2026 09:50:43 +0530 Subject: [PATCH 23/31] fix(aws-kms): DeriveSharedSecret, custom key stores, reserved aws: tags, grant principal validation (#1647) * fix(aws-kms): add DeriveSharedSecret and custom key stores, reject aws: tags and malformed grant principals - DeriveSharedSecret (ECDH): CreateKey accepts KeyUsage=KEY_AGREEMENT for ECC_NIST_P256/P384/P521 and advertises KeyAgreementAlgorithms=[ECDH] in KeyMetadata and GetPublicKey. The shared secret is computed with crypto/ecdh from the key's private half and the peer's DER SubjectPublicKeyInfo, so it equals what the peer derives locally. - Custom key stores: Create/Describe/Update/Delete/Connect/Disconnect CustomKeyStore for AWS_CLOUDHSM and EXTERNAL_KEY_STORE stores, with the documented name/cluster/proxy-URI uniqueness errors and connection-state rules. CreateKey takes CustomKeyStoreId/XksKeyId; keys in a disconnected store fail crypto with KMSInvalidStateException, rotation is unsupported for them, and a store holding keys can't be deleted. Stores persist in snapshots. - TagResource, CreateKey and ReplicateKey reject tag keys in the reserved aws: namespace (case-insensitive) with TagException. - CreateGrant rejects a GranteePrincipal/RetiringPrincipal that is not an IAM/STS principal ARN, service principal or account ID with InvalidArnException, and values outside the modeled pattern with ValidationException. Part of #1523 * docs(coverage): regenerate for KMS DeriveSharedSecret and custom key stores, add KMS non-goals Part of #1523 * fix(aws-kms): report Unavailable for keys in a disconnected store, tighten grant principals - Keys in a custom key store that is not CONNECTED now report KeyState Unavailable (Enabled false) on DescribeKey/ListKeys, except PendingDeletion keys. The state is derived on read, so EnableKey/DisableKey on an Unavailable key take effect once the store reconnects and snapshots keep the stored state. Crypto checks it first and fails with KMSInvalidStateException " is unavailable", also for a Disabled key. - CreateGrant principals must be iam root/user/role or sts assumed-role/federated-user ARNs with no Region and a 12-digit account. ValidationException messages now include the value, in the AWS format. - GenerateRandom honours CustomKeyStoreId: CustomKeyStoreNotFoundException, UnsupportedOperationException for an external key store, and CustomKeyStoreInvalidStateException for a store that is not connected. - CreateKey into a store names the single-Region symmetric rule in its UnsupportedOperationException message. - Drop the unused ExInvalidMarker and ExUnsupportedOperationForCustomStore constants; add driver.WithMessage for sentinel errors with a specific message. - Rename the issue-named test files by feature and add tests for the XKS connectivity switch and the custom key store list order. --- docs/coverage/README.md | 2 +- docs/coverage/aws/README.md | 2 +- docs/coverage/aws/kms.md | 29 +- docs/coverage/coverage.json | 58 ++ docs/coverage/nongoals/kms.md | 3 + providers/aws/kms/aliases.go | 22 + providers/aws/kms/crypto.go | 27 +- providers/aws/kms/crypto_asym.go | 98 ++- providers/aws/kms/customkeystore.go | 741 ++++++++++++++++++ providers/aws/kms/customkeystore_test.go | 664 ++++++++++++++++ .../aws/kms/derive_shared_secret_test.go | 238 ++++++ providers/aws/kms/keyimport.go | 4 + providers/aws/kms/kms.go | 16 +- providers/aws/kms/lifecycle.go | 47 +- providers/aws/kms/management.go | 22 +- providers/aws/kms/principal.go | 109 +++ providers/aws/kms/snapshot.go | 16 + .../aws/kms/tag_grant_validation_test.go | 125 +++ server/aws/kms/crypto_ops.go | 32 +- server/aws/kms/crypto_types.go | 18 +- server/aws/kms/customkeystore_ops.go | 206 +++++ server/aws/kms/customkeystore_sdk_test.go | 253 ++++++ .../aws/kms/derive_shared_secret_sdk_test.go | 107 +++ server/aws/kms/handler.go | 20 + server/aws/kms/operations.go | 3 + .../aws/kms/tag_grant_validation_sdk_test.go | 84 ++ server/aws/kms/types.go | 19 + services/kms/driver/crypto.go | 52 +- services/kms/driver/customkeystore.go | 109 +++ services/kms/driver/driver.go | 32 +- services/kms/driver/errors.go | 61 ++ services/kms/driver/management.go | 1 + 32 files changed, 3172 insertions(+), 48 deletions(-) create mode 100644 docs/coverage/nongoals/kms.md create mode 100644 providers/aws/kms/customkeystore.go create mode 100644 providers/aws/kms/customkeystore_test.go create mode 100644 providers/aws/kms/derive_shared_secret_test.go create mode 100644 providers/aws/kms/principal.go create mode 100644 providers/aws/kms/tag_grant_validation_test.go create mode 100644 server/aws/kms/customkeystore_ops.go create mode 100644 server/aws/kms/customkeystore_sdk_test.go create mode 100644 server/aws/kms/derive_shared_secret_sdk_test.go create mode 100644 server/aws/kms/tag_grant_validation_sdk_test.go create mode 100644 services/kms/driver/customkeystore.go create mode 100644 services/kms/driver/errors.go diff --git a/docs/coverage/README.md b/docs/coverage/README.md index 2dd04bcb7..a95cf862b 100644 --- a/docs/coverage/README.md +++ b/docs/coverage/README.md @@ -108,7 +108,7 @@ code does not implement. Machine-readable: [`coverage.json`](./coverage.json). | `keyspaces` | [Keyspaces](./aws/keyspaces.md) | - | - | - | 18 | | `kinesis` | [Kinesis](./aws/kinesis.md) | - | - | - | 39 | | `kinesisvideo` | [KinesisVideo](./aws/kinesisvideo.md) | - | - | - | 17 | -| `kms` | [KMS](./aws/kms.md) | - | - | - | 46 | +| `kms` | [KMS](./aws/kms.md) | - | - | - | 54 | | `kms-gcp` | - | - | [KMS](./gcp/kms.md) | - | 22 | | `kusto` | - | [Kusto](./azure/kusto.md) | - | - | 14 | | `loadbalancer` | [ELB](./aws/elb.md) | [LB](./azure/lb.md) | [LB](./gcp/lb.md) | - | 19 | diff --git a/docs/coverage/aws/README.md b/docs/coverage/aws/README.md index 0f6297d6e..bf42ede04 100644 --- a/docs/coverage/aws/README.md +++ b/docs/coverage/aws/README.md @@ -45,7 +45,7 @@ Services cloudemu emulates for AWS, by native name. Back to the [cross-provider | [GuardDuty](./guardduty.md) | `guardduty` | 87 | | [HealthLake](./healthlake.md) | `healthlake` | 7 | | [IAM](./iam.md) | `iam` | 40 | -| [KMS](./kms.md) | `kms` | 46 | +| [KMS](./kms.md) | `kms` | 54 | | [Kafka](./kafka.md) | `kafka` | 59 | | [Kendra](./kendra.md) | `kendra` | 13 | | [Keyspaces](./keyspaces.md) | `keyspaces` | 18 | diff --git a/docs/coverage/aws/kms.md b/docs/coverage/aws/kms.md index e52655693..1801f4412 100644 --- a/docs/coverage/aws/kms.md +++ b/docs/coverage/aws/kms.md @@ -3,20 +3,26 @@ AWS's `kms` service ยท portable interface `driver.KMS` ยท [AWS index](./README.md) -## Operations (46) +## Operations (54) | Operation | Description | | --- | --- | | `CancelKeyDeletion` | | +| `ConnectCustomKeyStore` | | | `CreateAlias` | Aliases. | +| `CreateCustomKeyStore` | | | `CreateGrant` | Grants. | | `CreateKey` | Key lifecycle. | | `Decrypt` | | | `DeleteAlias` | | +| `DeleteCustomKeyStore` | | | `DeleteImportedKeyMaterial` | | +| `DeriveSharedSecret` | | +| `DescribeCustomKeyStores` | DescribeCustomKeyStores returns every store, or only the one matching id | | `DescribeKey` | | | `DisableKey` | | | `DisableKeyRotation` | | +| `DisconnectCustomKeyStore` | | | `EnableKey` | | | `EnableKeyRotation` | Rotation. | | `Encrypt` | | @@ -26,6 +32,7 @@ AWS's `kms` service ยท portable interface `driver.KMS` ยท [AWS index](./README.m | `GenerateDataKeyWithoutPlaintext` | | | `GenerateMac` | | | `GenerateRandom` | | +| `GenerateRandomInCustomKeyStore` | GenerateRandomInCustomKeyStore serves GenerateRandom with a CustomKeyStoreId. | | `GetKeyPolicy` | Key policies. | | `GetKeyRotationStatus` | | | `GetParametersForImport` | | @@ -49,6 +56,7 @@ AWS's `kms` service ยท portable interface `driver.KMS` ยท [AWS index](./README.m | `TagResource` | Tags. | | `UntagResource` | | | `UpdateAlias` | | +| `UpdateCustomKeyStore` | | | `UpdateKeyDescription` | | | `UpdatePrimaryRegion` | | | `Verify` | | @@ -65,6 +73,7 @@ Crypto is the cryptographic surface of KMS. It is embedded in the KMS | Operation | Description | | --- | --- | | `Decrypt` | | +| `DeriveSharedSecret` | | | `Encrypt` | | | `GenerateDataKey` | | | `GenerateDataKeyPair` | | @@ -78,6 +87,20 @@ Crypto is the cryptographic surface of KMS. It is embedded in the KMS | `Verify` | | | `VerifyMac` | | +### CustomKeyStores + +CustomKeyStores is the custom key store surface of KMS. + +| Operation | Description | +| --- | --- | +| `ConnectCustomKeyStore` | | +| `CreateCustomKeyStore` | | +| `DeleteCustomKeyStore` | | +| `DescribeCustomKeyStores` | DescribeCustomKeyStores returns every store, or only the one matching id | +| `DisconnectCustomKeyStore` | | +| `GenerateRandomInCustomKeyStore` | GenerateRandomInCustomKeyStore serves GenerateRandom with a CustomKeyStoreId. | +| `UpdateCustomKeyStore` | | + ### KeyImport KeyImport is the import-material and multi-region surface of KMS. @@ -112,4 +135,6 @@ Management is the grants/rotation/policy surface of KMS. ## Not in scope -_Not documented yet. See the [emulator boundary](../../../README.md) for cloudemu-wide non-goals._ +- Custom key stores are control-plane only. There is no CloudHSM cluster or external key store proxy to reach, so `ConnectCustomKeyStore` moves a store straight to `CONNECTED` and never reports `CONNECTING`, `FAILED` or a `ConnectionErrorCode`, and `CreateCustomKeyStore` does not contact a `PUBLIC_ENDPOINT` proxy. Cluster existence and activity, cluster relatedness (`CloudHsmClusterNotRelatedException`), the trust anchor, the `kmsuser` password and the proxy credentials are not checked. Keys in a store use locally generated key material, so crypto works the same as for `AWS_KMS` keys while the store is connected. While it is not, the keys report `KeyState` `Unavailable` (a `PendingDeletion` key keeps that state) and crypto fails with `KMSInvalidStateException`. `GenerateRandom` with the `CustomKeyStoreId` of a connected CloudHSM store returns locally generated bytes. +- `DeriveSharedSecret` returns the raw ECDH secret only. The Nitro Enclave / NitroTPM `Recipient` attestation flow (`CiphertextForRecipient`), `DryRun` and grant tokens are not emulated. +- SM2 (China Regions only), `ECC_SECG_P256K1`, ML-DSA and `ECC_NIST_EDWARDS25519` key specs are not supported, so `KEY_AGREEMENT` keys are limited to `ECC_NIST_P256`, `ECC_NIST_P384` and `ECC_NIST_P521`. diff --git a/docs/coverage/coverage.json b/docs/coverage/coverage.json index 804792a90..9174b0a86 100644 --- a/docs/coverage/coverage.json +++ b/docs/coverage/coverage.json @@ -10668,10 +10668,16 @@ { "name": "CancelKeyDeletion" }, + { + "name": "ConnectCustomKeyStore" + }, { "name": "CreateAlias", "doc": "Aliases." }, + { + "name": "CreateCustomKeyStore" + }, { "name": "CreateGrant", "doc": "Grants." @@ -10686,9 +10692,19 @@ { "name": "DeleteAlias" }, + { + "name": "DeleteCustomKeyStore" + }, { "name": "DeleteImportedKeyMaterial" }, + { + "name": "DeriveSharedSecret" + }, + { + "name": "DescribeCustomKeyStores", + "doc": "DescribeCustomKeyStores returns every store, or only the one matching id" + }, { "name": "DescribeKey" }, @@ -10698,6 +10714,9 @@ { "name": "DisableKeyRotation" }, + { + "name": "DisconnectCustomKeyStore" + }, { "name": "EnableKey" }, @@ -10726,6 +10745,10 @@ { "name": "GenerateRandom" }, + { + "name": "GenerateRandomInCustomKeyStore", + "doc": "GenerateRandomInCustomKeyStore serves GenerateRandom with a CustomKeyStoreId." + }, { "name": "GetKeyPolicy", "doc": "Key policies." @@ -10797,6 +10820,9 @@ { "name": "UpdateAlias" }, + { + "name": "UpdateCustomKeyStore" + }, { "name": "UpdateKeyDescription" }, @@ -10818,6 +10844,9 @@ { "name": "Decrypt" }, + { + "name": "DeriveSharedSecret" + }, { "name": "Encrypt" }, @@ -10856,6 +10885,35 @@ } ] }, + { + "name": "CustomKeyStores", + "doc": "CustomKeyStores is the custom key store surface of KMS.", + "operations": [ + { + "name": "ConnectCustomKeyStore" + }, + { + "name": "CreateCustomKeyStore" + }, + { + "name": "DeleteCustomKeyStore" + }, + { + "name": "DescribeCustomKeyStores", + "doc": "DescribeCustomKeyStores returns every store, or only the one matching id" + }, + { + "name": "DisconnectCustomKeyStore" + }, + { + "name": "GenerateRandomInCustomKeyStore", + "doc": "GenerateRandomInCustomKeyStore serves GenerateRandom with a CustomKeyStoreId." + }, + { + "name": "UpdateCustomKeyStore" + } + ] + }, { "name": "KeyImport", "doc": "KeyImport is the import-material and multi-region surface of KMS.", diff --git a/docs/coverage/nongoals/kms.md b/docs/coverage/nongoals/kms.md new file mode 100644 index 000000000..4cff4af0e --- /dev/null +++ b/docs/coverage/nongoals/kms.md @@ -0,0 +1,3 @@ +- Custom key stores are control-plane only. There is no CloudHSM cluster or external key store proxy to reach, so `ConnectCustomKeyStore` moves a store straight to `CONNECTED` and never reports `CONNECTING`, `FAILED` or a `ConnectionErrorCode`, and `CreateCustomKeyStore` does not contact a `PUBLIC_ENDPOINT` proxy. Cluster existence and activity, cluster relatedness (`CloudHsmClusterNotRelatedException`), the trust anchor, the `kmsuser` password and the proxy credentials are not checked. Keys in a store use locally generated key material, so crypto works the same as for `AWS_KMS` keys while the store is connected. While it is not, the keys report `KeyState` `Unavailable` (a `PendingDeletion` key keeps that state) and crypto fails with `KMSInvalidStateException`. `GenerateRandom` with the `CustomKeyStoreId` of a connected CloudHSM store returns locally generated bytes. +- `DeriveSharedSecret` returns the raw ECDH secret only. The Nitro Enclave / NitroTPM `Recipient` attestation flow (`CiphertextForRecipient`), `DryRun` and grant tokens are not emulated. +- SM2 (China Regions only), `ECC_SECG_P256K1`, ML-DSA and `ECC_NIST_EDWARDS25519` key specs are not supported, so `KEY_AGREEMENT` keys are limited to `ECC_NIST_P256`, `ECC_NIST_P384` and `ECC_NIST_P521`. diff --git a/providers/aws/kms/aliases.go b/providers/aws/kms/aliases.go index 97d847264..083901523 100644 --- a/providers/aws/kms/aliases.go +++ b/providers/aws/kms/aliases.go @@ -121,8 +121,30 @@ func (m *Mock) ListAliases(_ context.Context, keyID string) ([]driver.Alias, err return out, nil } +// reservedTagPrefix is the tag-key namespace AWS reserves for system tags. +// User-defined tags can't use it ("You can't edit or delete a tag that begins +// with the aws: prefix"); KMS rejects such a tag with TagException. +const reservedTagPrefix = "aws:" + +// validateTags rejects tags KMS refuses: a key in the reserved "aws:" +// namespace (matched case-insensitively, as AWS tagging treats the prefix). +func validateTags(tags map[string]string) error { + for k := range tags { + if strings.HasPrefix(strings.ToLower(k), reservedTagPrefix) { + return driver.NewAPIError(driver.ExTag, errors.InvalidArgument, + "Tag key %q is invalid: the aws: prefix is reserved for AWS use", k) + } + } + + return nil +} + // TagResource adds or overwrites tags on a key. func (m *Mock) TagResource(_ context.Context, keyID string, tags map[string]string) error { + if err := validateTags(tags); err != nil { + return err + } + return m.mutateKey(keyID, func(kd *keyData) error { for k, v := range tags { kd.tags[k] = v diff --git a/providers/aws/kms/crypto.go b/providers/aws/kms/crypto.go index 4961cf40d..62a2548d3 100644 --- a/providers/aws/kms/crypto.go +++ b/providers/aws/kms/crypto.go @@ -148,7 +148,14 @@ func symmetricGCM(material []byte) (cipher.AEAD, error) { // requireUsable reports whether a key may be used for a cryptographic // operation, returning the sentinel that maps to the precise KMS exception: // Disabled โ†’ DisabledException, any other non-Enabled state โ†’ KMSInvalidState. -func requireUsable(kd *keyData) error { +func (m *Mock) requireUsable(kd *keyData) error { + // A key in a custom key store whose store is not CONNECTED is Unavailable + // whatever its stored state (bar PendingDeletion), and KMS fails crypto on + // it with KMSInvalidStateException " is unavailable". + if m.keyUnavailable(kd) { + return driver.WithMessage(driver.ErrKeyInvalidState, "%s is unavailable", kd.meta.ARN) + } + switch kd.meta.KeyState { case driver.StateEnabled: return nil @@ -170,7 +177,7 @@ func (m *Mock) Encrypt(_ context.Context, in driver.EncryptInput) (*driver.Encry kd.mu.RLock() defer kd.mu.RUnlock() - if uerr := requireUsable(kd); uerr != nil { + if uerr := m.requireUsable(kd); uerr != nil { return nil, uerr } @@ -290,7 +297,7 @@ func (m *Mock) decryptWrapped(in driver.DecryptInput) (out *driver.DecryptOutput kd.mu.RLock() defer kd.mu.RUnlock() - if uerr := requireUsable(kd); uerr != nil { + if uerr := m.requireUsable(kd); uerr != nil { return nil, true, uerr } @@ -318,7 +325,7 @@ func (m *Mock) decryptRawAsymmetric(in driver.DecryptInput) (*driver.DecryptOutp kd.mu.RLock() defer kd.mu.RUnlock() - if uerr := requireUsable(kd); uerr != nil { + if uerr := m.requireUsable(kd); uerr != nil { return nil, uerr } @@ -490,8 +497,8 @@ func (m *Mock) GenerateDataKeyWithoutPlaintext( // GenerateRandom returns cryptographically random bytes. func (*Mock) GenerateRandom(_ context.Context, numberOfBytes int32) ([]byte, error) { - if numberOfBytes <= 0 || numberOfBytes > maxRandomBytes { - return nil, errors.New(errors.InvalidArgument, "NumberOfBytes must be 1-1024") + if err := validateRandomBytes(numberOfBytes); err != nil { + return nil, err } buf := make([]byte, numberOfBytes) @@ -501,3 +508,11 @@ func (*Mock) GenerateRandom(_ context.Context, numberOfBytes int32) ([]byte, err return buf, nil } + +func validateRandomBytes(numberOfBytes int32) error { + if numberOfBytes <= 0 || numberOfBytes > maxRandomBytes { + return errors.New(errors.InvalidArgument, "NumberOfBytes must be 1-1024") + } + + return nil +} diff --git a/providers/aws/kms/crypto_asym.go b/providers/aws/kms/crypto_asym.go index b1c0260b9..4fc22e095 100644 --- a/providers/aws/kms/crypto_asym.go +++ b/providers/aws/kms/crypto_asym.go @@ -98,7 +98,7 @@ func (m *Mock) GetPublicKey(_ context.Context, in driver.GetPublicKeyInput) (*dr kd.mu.RLock() defer kd.mu.RUnlock() - if uerr := requireUsable(kd); uerr != nil { + if uerr := m.requireUsable(kd); uerr != nil { return nil, uerr } @@ -119,9 +119,97 @@ func (m *Mock) GetPublicKey(_ context.Context, in driver.GetPublicKeyInput) (*dr KeyUsage: kd.meta.KeyUsage, EncryptionAlgorithms: driver.EncryptionAlgorithmsFor(kd.meta.KeySpec, kd.meta.KeyUsage), SigningAlgorithms: driver.SigningAlgorithmsFor(kd.meta.KeySpec, kd.meta.KeyUsage), + + KeyAgreementAlgorithms: driver.KeyAgreementAlgorithmsFor(kd.meta.KeySpec, kd.meta.KeyUsage), }, nil } +// DeriveSharedSecret performs ECDH between the private half of a KEY_AGREEMENT +// ECC key and the peer's public key (DER SubjectPublicKeyInfo on the same +// curve), returning the raw shared secret: the x-coordinate of the shared +// point, exactly what the peer computes locally with its own private key and +// this key's public key (GetPublicKey). +func (m *Mock) DeriveSharedSecret( + _ context.Context, in driver.DeriveSharedSecretInput, +) (*driver.DeriveSharedSecretOutput, error) { + if in.KeyAgreementAlgorithm != driver.KeyAgreementECDH { + return nil, errors.Newf(errors.InvalidArgument, + "1 validation error detected: Value '%s' at 'keyAgreementAlgorithm' failed to satisfy constraint: "+ + "Member must satisfy enum value set: [ECDH]", in.KeyAgreementAlgorithm) + } + + if len(in.PublicKey) == 0 { + return nil, errors.New(errors.InvalidArgument, "PublicKey is required") + } + + kd, err := m.getKey(in.KeyID) + if err != nil { + return nil, err + } + + kd.mu.RLock() + defer kd.mu.RUnlock() + + if uerr := m.requireUsable(kd); uerr != nil { + return nil, uerr + } + + if kd.meta.KeyUsage != driver.UsageKeyAgreement { + return nil, driver.ErrInvalidKeyUsage + } + + priv, ok := kd.privKey.(*ecdsa.PrivateKey) + if !ok { + return nil, driver.ErrInvalidKeyUsage + } + + secret, err := ecdhSharedSecret(priv, in.PublicKey) + if err != nil { + return nil, err + } + + return &driver.DeriveSharedSecretOutput{ + KeyID: kd.meta.KeyID, + SharedSecret: secret, + KeyAgreementAlgorithm: driver.KeyAgreementECDH, + KeyOrigin: kd.meta.Origin, + }, nil +} + +// ecdhSharedSecret computes the ECDH shared secret of priv and a peer public +// key given as DER SubjectPublicKeyInfo. The peer key must be an EC key on the +// same curve as priv. +func ecdhSharedSecret(priv *ecdsa.PrivateKey, peerDER []byte) ([]byte, error) { + parsed, err := x509.ParsePKIXPublicKey(peerDER) + if err != nil { + return nil, errors.New(errors.InvalidArgument, + "PublicKey must be a DER-encoded X.509 public key (SubjectPublicKeyInfo, RFC 5280)") + } + + peer, ok := parsed.(*ecdsa.PublicKey) + if !ok || peer.Curve != priv.Curve { + return nil, errors.New(errors.InvalidArgument, + "PublicKey must be an elliptic curve public key on the same curve as the KMS key") + } + + ecdhPriv, err := priv.ECDH() + if err != nil { + return nil, errors.Newf(errors.Internal, "convert private key: %v", err) + } + + ecdhPeer, err := peer.ECDH() + if err != nil { + return nil, errors.New(errors.InvalidArgument, "PublicKey is not a valid point on the key's curve") + } + + secret, err := ecdhPriv.ECDH(ecdhPeer) + if err != nil { + return nil, errors.Newf(errors.InvalidArgument, "derive shared secret: %v", err) + } + + return secret, nil +} + func publicKeyOf(priv crypto.PrivateKey) crypto.PublicKey { switch k := priv.(type) { case *rsa.PrivateKey: @@ -187,7 +275,7 @@ func (m *Mock) Sign(_ context.Context, in driver.SignInput) (*driver.SignOutput, kd.mu.RLock() defer kd.mu.RUnlock() - if uerr := requireUsable(kd); uerr != nil { + if uerr := m.requireUsable(kd); uerr != nil { return nil, uerr } @@ -241,7 +329,7 @@ func (m *Mock) Verify(_ context.Context, in driver.VerifyInput) (*driver.VerifyO kd.mu.RLock() defer kd.mu.RUnlock() - if uerr := requireUsable(kd); uerr != nil { + if uerr := m.requireUsable(kd); uerr != nil { return nil, uerr } @@ -294,7 +382,7 @@ func (m *Mock) GenerateMac(_ context.Context, in driver.GenerateMacInput) (*driv kd.mu.RLock() defer kd.mu.RUnlock() - if uerr := requireUsable(kd); uerr != nil { + if uerr := m.requireUsable(kd); uerr != nil { return nil, uerr } @@ -322,7 +410,7 @@ func (m *Mock) VerifyMac(_ context.Context, in driver.VerifyMacInput) (*driver.V kd.mu.RLock() defer kd.mu.RUnlock() - if uerr := requireUsable(kd); uerr != nil { + if uerr := m.requireUsable(kd); uerr != nil { return nil, uerr } diff --git a/providers/aws/kms/customkeystore.go b/providers/aws/kms/customkeystore.go new file mode 100644 index 000000000..e80abfaa7 --- /dev/null +++ b/providers/aws/kms/customkeystore.go @@ -0,0 +1,741 @@ +package kms + +import ( + "context" + "regexp" + "sort" + + "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/internal/idgen" + "github.com/stackshy/cloudemu/v2/services/kms/driver" +) + +// Field constraints KMS models for custom key store parameters. +var ( + cloudHsmClusterIDPattern = regexp.MustCompile(`^cluster-[2-7a-zA-Z]{11,16}$`) + xksURIEndpointPattern = regexp.MustCompile(`^https://[a-zA-Z0-9.-]+$`) + xksURIPathPattern = regexp.MustCompile(`^(/[a-zA-Z0-9/_-]+/kms/xks/v\d{1,2})$|^(/kms/xks/v\d{1,2})$`) + xksVpcServicePattern = regexp.MustCompile(`^(com|eu)\.amazonaws\.vpce\.([a-z]+-){2,3}\d+\.vpce-svc-[0-9a-z]+$`) + xksAccessKeyIDPattern = regexp.MustCompile(`^[A-Z2-7]+$`) + xksSecretPattern = regexp.MustCompile(`^[a-zA-Z0-9/+=]+$`) + xksKeyIDPattern = regexp.MustCompile(`^[a-zA-Z0-9-_.]+$`) +) + +const ( + maxCustomKeyStoreNameLen = 256 + minKeyStorePasswordLen = 7 + maxKeyStorePasswordLen = 32 + maxTrustAnchorLen = 5000 + minXksURILen = 10 + maxXksURILen = 128 + minXksVpcServiceLen = 20 + maxXksVpcServiceLen = 64 + minXksAccessKeyIDLen = 20 + maxXksAccessKeyIDLen = 30 + minXksSecretLen = 43 + maxXksSecretLen = 64 + maxXksKeyIDLen = 128 +) + +func invalidParam(format string, args ...any) error { + return errors.Newf(errors.InvalidArgument, format, args...) +} + +func cksNotFound(ref string) error { + return driver.NewAPIError(driver.ExCustomKeyStoreNotFound, errors.NotFound, + "Custom key store %s does not exist", ref) +} + +func cksInvalidState(format string, args ...any) error { + return driver.NewAPIError(driver.ExCustomKeyStoreInvalidState, errors.FailedPrecondition, format, args...) +} + +func isCustomKeyStoreOrigin(origin string) bool { + return origin == driver.OriginAWSCloudHSM || origin == driver.OriginExternalKeyStore +} + +// customKeyStoreConnected reports whether the store id exists and is CONNECTED. +func (m *Mock) customKeyStoreConnected(id string) bool { + s, ok := m.customKeyStores.Get(id) + + return ok && s.ConnectionState == driver.ConnectionStateConnected +} + +// keyUnavailable reports whether a key is in the Unavailable state: it lives in +// a custom key store that is not CONNECTED. A PendingDeletion key keeps that +// state while its store is disconnected, so its deletion can still be +// canceled. Caller holds kd.mu. +func (m *Mock) keyUnavailable(kd *keyData) bool { + return kd.meta.CustomKeyStoreID != "" && kd.meta.KeyState != driver.StatePendingDeletion && + !m.customKeyStoreConnected(kd.meta.CustomKeyStoreID) +} + +// keyMetadata returns the key's metadata as KMS reports it. The stored state +// is kept as is (EnableKey/DisableKey on an Unavailable key take effect once +// the store reconnects); only the reported state is Unavailable while the +// key's store is not connected. Caller holds kd.mu. +func (m *Mock) keyMetadata(kd *keyData) driver.KeyMetadata { + out := kd.meta + + if m.keyUnavailable(kd) { + out.KeyState = driver.StateUnavailable + out.Enabled = false + } + + return out +} + +// keyGone reports whether a key no longer counts as living in its store: real +// KMS deletes a PendingDeletion key once its waiting period ends. +func (m *Mock) keyGone(kd *keyData) bool { + return kd.meta.KeyState == driver.StatePendingDeletion && !kd.meta.DeletionDate.After(m.now()) +} + +// customKeyStoreForNewKey validates CreateKey's custom key store placement and +// returns the target store (nil for a key outside any store). The caller holds +// cksMu whenever a store or store origin is involved. +// +//nolint:gocyclo // a flat list of the CreateKey custom-store rules KMS documents +func (m *Mock) customKeyStoreForNewKey(in *driver.CreateKeyInput, origin, spec, usage string) (*driver.CustomKeyStore, error) { + if in.CustomKeyStoreID == "" { + if isCustomKeyStoreOrigin(origin) { + return nil, invalidParam("CustomKeyStoreId is required when Origin is %s", origin) + } + + if in.XksKeyID != "" { + return nil, invalidParam("XksKeyId is valid only for keys with Origin EXTERNAL_KEY_STORE") + } + + return nil, nil + } + + store, ok := m.customKeyStores.Get(in.CustomKeyStoreID) + if !ok { + return nil, cksNotFound(in.CustomKeyStoreID) + } + + wantOrigin := driver.OriginAWSCloudHSM + if store.CustomKeyStoreType == driver.CustomKeyStoreTypeExternal { + wantOrigin = driver.OriginExternalKeyStore + } + + if origin != wantOrigin { + return nil, invalidParam("Origin must be %s to create a key in custom key store %s", + wantOrigin, in.CustomKeyStoreID) + } + + if spec != driver.SpecSymmetricDefault || usage != driver.UsageEncryptDecrypt || in.MultiRegion { + return nil, driver.WithMessage(driver.ErrUnsupportedOperation, + "Custom key stores support only single-Region symmetric encryption keys "+ + "(KeySpec SYMMETRIC_DEFAULT, KeyUsage ENCRYPT_DECRYPT, MultiRegion false)") + } + + if store.ConnectionState != driver.ConnectionStateConnected { + return nil, cksInvalidState("Custom key store %s is not connected (ConnectionState %s)", + in.CustomKeyStoreID, store.ConnectionState) + } + + if store.CustomKeyStoreType != driver.CustomKeyStoreTypeExternal { + if in.XksKeyID != "" { + return nil, invalidParam("XksKeyId is valid only for keys with Origin EXTERNAL_KEY_STORE") + } + + return store, nil + } + + if in.XksKeyID == "" { + return nil, invalidParam("XksKeyId is required when Origin is EXTERNAL_KEY_STORE") + } + + if len(in.XksKeyID) > maxXksKeyIDLen || !xksKeyIDPattern.MatchString(in.XksKeyID) { + return nil, invalidParam("XksKeyId %q is not valid", in.XksKeyID) + } + + for _, kd := range m.keys.All() { + kd.mu.RLock() + inUse := kd.meta.CustomKeyStoreID == store.CustomKeyStoreID && kd.meta.XksKeyID == in.XksKeyID && !m.keyGone(kd) + kd.mu.RUnlock() + + if inUse { + return nil, driver.NewAPIError(driver.ExXksKeyAlreadyInUse, errors.AlreadyExists, + "External key %s is already associated with a KMS key in this external key store", in.XksKeyID) + } + } + + return store, nil +} + +// CreateCustomKeyStore creates a DISCONNECTED CloudHSM or external key store. +// +//nolint:gocritic // in is the public input, taken by value to match the driver API +func (m *Mock) CreateCustomKeyStore(_ context.Context, in driver.CreateCustomKeyStoreInput) (string, error) { + storeType := in.CustomKeyStoreType + if storeType == "" { + storeType = driver.CustomKeyStoreTypeCloudHSM + } + + s := &driver.CustomKeyStore{ + CustomKeyStoreName: in.CustomKeyStoreName, + CustomKeyStoreType: storeType, + CloudHsmClusterID: in.CloudHsmClusterID, + TrustAnchorCertificate: in.TrustAnchorCertificate, + KeyStorePassword: in.KeyStorePassword, + ConnectionState: driver.ConnectionStateDisconnected, + } + + if err := validateNewStore(s, &in); err != nil { + return "", err + } + + m.cksMu.Lock() + defer m.cksMu.Unlock() + + if err := m.checkStoreUniqueness(s); err != nil { + return "", err + } + + id, err := idgen.GenerateLongID("cks-") + if err != nil { + return "", errors.Newf(errors.Internal, "generate custom key store id: %v", err) + } + + s.CustomKeyStoreID = id + s.CreationDate = m.now() + m.customKeyStores.Set(id, s) + + return id, nil +} + +// validateNewStore checks the type-specific required parameters of a new store +// and fills in its XKS proxy configuration. +// +//nolint:gocyclo // a flat list of the per-type required/forbidden parameters KMS documents +func validateNewStore(s *driver.CustomKeyStore, in *driver.CreateCustomKeyStoreInput) error { + if err := validateStoreName(in.CustomKeyStoreName); err != nil { + return err + } + + switch s.CustomKeyStoreType { + case driver.CustomKeyStoreTypeCloudHSM: + if in.XksProxyURIEndpoint != "" || in.XksProxyURIPath != "" || in.XksProxyConnectivity != "" || + in.XksProxyVpcEndpointServiceName != "" || in.XksProxyVpcEndpointServiceOwner != "" || + in.XksProxyAuthenticationCredential != nil { + return invalidParam("XksProxy parameters are valid only for custom key stores of type EXTERNAL_KEY_STORE") + } + + if in.CloudHsmClusterID == "" || in.TrustAnchorCertificate == "" || in.KeyStorePassword == "" { + return invalidParam("CloudHsmClusterId, TrustAnchorCertificate, and KeyStorePassword are required " + + "for a custom key store of type AWS_CLOUDHSM") + } + + return validateCloudHSMFields(in.CloudHsmClusterID, in.TrustAnchorCertificate, in.KeyStorePassword) + case driver.CustomKeyStoreTypeExternal: + if in.CloudHsmClusterID != "" || in.TrustAnchorCertificate != "" || in.KeyStorePassword != "" { + return invalidParam("CloudHsmClusterId, TrustAnchorCertificate, and KeyStorePassword are valid only " + + "for custom key stores of type AWS_CLOUDHSM") + } + + if in.XksProxyURIEndpoint == "" || in.XksProxyURIPath == "" || in.XksProxyConnectivity == "" || + in.XksProxyAuthenticationCredential == nil { + return invalidParam("XksProxyUriEndpoint, XksProxyUriPath, XksProxyConnectivity, and " + + "XksProxyAuthenticationCredential are required for a custom key store of type EXTERNAL_KEY_STORE") + } + + s.XksProxyConfiguration = &driver.XksProxyConfiguration{ + Connectivity: in.XksProxyConnectivity, + AccessKeyID: in.XksProxyAuthenticationCredential.AccessKeyID, + URIEndpoint: in.XksProxyURIEndpoint, + URIPath: in.XksProxyURIPath, + VpcEndpointServiceName: in.XksProxyVpcEndpointServiceName, + VpcEndpointServiceOwner: in.XksProxyVpcEndpointServiceOwner, + } + s.XksRawSecretAccessKey = in.XksProxyAuthenticationCredential.RawSecretAccessKey + + return validateXksConfig(s.XksProxyConfiguration, in.XksProxyAuthenticationCredential) + default: + return invalidParam("CustomKeyStoreType %q is not valid; expected AWS_CLOUDHSM or EXTERNAL_KEY_STORE", + s.CustomKeyStoreType) + } +} + +func validateStoreName(name string) error { + if name == "" || len(name) > maxCustomKeyStoreNameLen { + return invalidParam("CustomKeyStoreName must be 1-%d characters", maxCustomKeyStoreNameLen) + } + + return nil +} + +func validateCloudHSMFields(clusterID, trustAnchor, password string) error { + if clusterID != "" && !cloudHsmClusterIDPattern.MatchString(clusterID) { + return invalidParam("CloudHsmClusterId %q is not valid", clusterID) + } + + if len(trustAnchor) > maxTrustAnchorLen { + return invalidParam("TrustAnchorCertificate must be at most %d characters", maxTrustAnchorLen) + } + + if password != "" && (len(password) < minKeyStorePasswordLen || len(password) > maxKeyStorePasswordLen) { + return invalidParam("KeyStorePassword must be %d-%d characters", minKeyStorePasswordLen, maxKeyStorePasswordLen) + } + + return nil +} + +// validateXksConfig checks an external key store proxy configuration (and a +// credential, when one is being set). +// +//nolint:gocyclo // a flat list of the modeled XKS field constraints +func validateXksConfig(cfg *driver.XksProxyConfiguration, cred *driver.XksProxyAuthenticationCredential) error { + switch cfg.Connectivity { + case driver.XksConnectivityPublicEndpoint: + if cfg.VpcEndpointServiceName != "" || cfg.VpcEndpointServiceOwner != "" { + return invalidParam("XksProxyVpcEndpointServiceName is valid only when XksProxyConnectivity is VPC_ENDPOINT_SERVICE") + } + case driver.XksConnectivityVpcEndpointService: + if cfg.VpcEndpointServiceName == "" { + return invalidParam("XksProxyVpcEndpointServiceName is required when XksProxyConnectivity is VPC_ENDPOINT_SERVICE") + } + default: + return invalidParam("XksProxyConnectivity %q is not valid; expected PUBLIC_ENDPOINT or VPC_ENDPOINT_SERVICE", + cfg.Connectivity) + } + + if !lenIn(cfg.URIEndpoint, minXksURILen, maxXksURILen) || !xksURIEndpointPattern.MatchString(cfg.URIEndpoint) { + return invalidParam("XksProxyUriEndpoint %q is not valid: it must be https:// followed by a host name", cfg.URIEndpoint) + } + + if !lenIn(cfg.URIPath, minXksURILen, maxXksURILen) || !xksURIPathPattern.MatchString(cfg.URIPath) { + return invalidParam("XksProxyUriPath %q is not valid: it must end with /kms/xks/v", cfg.URIPath) + } + + if cfg.VpcEndpointServiceName != "" && (!lenIn(cfg.VpcEndpointServiceName, minXksVpcServiceLen, maxXksVpcServiceLen) || + !xksVpcServicePattern.MatchString(cfg.VpcEndpointServiceName)) { + return invalidParam("XksProxyVpcEndpointServiceName %q is not valid", cfg.VpcEndpointServiceName) + } + + if cfg.VpcEndpointServiceOwner != "" && !accountIDPattern.MatchString(cfg.VpcEndpointServiceOwner) { + return invalidParam("XksProxyVpcEndpointServiceOwner %q is not a 12-digit account ID", cfg.VpcEndpointServiceOwner) + } + + if cred == nil { + return nil + } + + if !lenIn(cred.AccessKeyID, minXksAccessKeyIDLen, maxXksAccessKeyIDLen) || !xksAccessKeyIDPattern.MatchString(cred.AccessKeyID) { + return invalidParam("XksProxyAuthenticationCredential.AccessKeyId must be %d-%d characters of A-Z and 2-7", + minXksAccessKeyIDLen, maxXksAccessKeyIDLen) + } + + if !lenIn(cred.RawSecretAccessKey, minXksSecretLen, maxXksSecretLen) || !xksSecretPattern.MatchString(cred.RawSecretAccessKey) { + return invalidParam("XksProxyAuthenticationCredential.RawSecretAccessKey must be %d-%d characters of "+ + "a-z, A-Z, 0-9, /, +, and =", minXksSecretLen, maxXksSecretLen) + } + + return nil +} + +func lenIn(s string, lo, hi int) bool { + return len(s) >= lo && len(s) <= hi +} + +// checkStoreUniqueness enforces the account/Region uniqueness rules KMS +// documents for custom key stores against every other store. Caller holds +// cksMu. +func (m *Mock) checkStoreUniqueness(s *driver.CustomKeyStore) error { + for id, other := range m.customKeyStores.All() { + if id == s.CustomKeyStoreID { + continue + } + + if other.CustomKeyStoreName == s.CustomKeyStoreName { + return driver.NewAPIError(driver.ExCustomKeyStoreNameInUse, errors.AlreadyExists, + "Custom key store name %s is already in use", s.CustomKeyStoreName) + } + + if s.CloudHsmClusterID != "" && other.CloudHsmClusterID == s.CloudHsmClusterID { + return driver.NewAPIError(driver.ExCloudHsmClusterInUse, errors.AlreadyExists, + "CloudHSM cluster %s is already associated with custom key store %s", s.CloudHsmClusterID, id) + } + + if err := xksConflict(s.XksProxyConfiguration, other.XksProxyConfiguration); err != nil { + return err + } + } + + return nil +} + +// xksConflict reports a proxy-address clash between two external key stores: +// the endpoint+path pair must be unique, a VPC_ENDPOINT_SERVICE store's +// endpoint (private DNS name) and endpoint service must be unique, and a public +// store can't reuse a VPC store's endpoint. +func xksConflict(mine, other *driver.XksProxyConfiguration) error { + if mine == nil || other == nil { + return nil + } + + if mine.URIEndpoint == other.URIEndpoint && mine.URIPath == other.URIPath { + return driver.NewAPIError(driver.ExXksProxyURIInUse, errors.AlreadyExists, + "XksProxyUriEndpoint and XksProxyUriPath %s%s are already associated with another external key store", + mine.URIEndpoint, mine.URIPath) + } + + vpc := mine.Connectivity == driver.XksConnectivityVpcEndpointService || + other.Connectivity == driver.XksConnectivityVpcEndpointService + if vpc && mine.URIEndpoint == other.URIEndpoint { + return driver.NewAPIError(driver.ExXksProxyURIEndpointInUse, errors.AlreadyExists, + "XksProxyUriEndpoint %s is already associated with another external key store", mine.URIEndpoint) + } + + if mine.VpcEndpointServiceName != "" && mine.VpcEndpointServiceName == other.VpcEndpointServiceName { + return driver.NewAPIError(driver.ExXksProxyVpcEndpointServiceInUse, errors.AlreadyExists, + "VPC endpoint service %s is already associated with another external key store", mine.VpcEndpointServiceName) + } + + return nil +} + +// DescribeCustomKeyStores lists every store, or the one named by id or name. +func (m *Mock) DescribeCustomKeyStores(_ context.Context, id, name string) ([]driver.CustomKeyStore, error) { + if id != "" && name != "" { + return nil, invalidParam("specify either CustomKeyStoreId or CustomKeyStoreName, not both") + } + + m.cksMu.Lock() + defer m.cksMu.Unlock() + + if id != "" { + s, ok := m.customKeyStores.Get(id) + if !ok { + return nil, cksNotFound(id) + } + + return []driver.CustomKeyStore{copyStore(s)}, nil + } + + out := make([]driver.CustomKeyStore, 0, m.customKeyStores.Len()) + + for _, s := range m.customKeyStores.All() { + if name == "" || s.CustomKeyStoreName == name { + out = append(out, copyStore(s)) + } + } + + if name != "" && len(out) == 0 { + return nil, cksNotFound(name) + } + + sortStores(out) + + return out, nil +} + +// sortStores orders stores by creation date then ID, so paging is stable. +func sortStores(out []driver.CustomKeyStore) { + sort.Slice(out, func(i, j int) bool { + if !out[i].CreationDate.Equal(out[j].CreationDate) { + return out[i].CreationDate.Before(out[j].CreationDate) + } + + return out[i].CustomKeyStoreID < out[j].CustomKeyStoreID + }) +} + +func copyStore(s *driver.CustomKeyStore) driver.CustomKeyStore { + out := *s + + if s.XksProxyConfiguration != nil { + cfg := *s.XksProxyConfiguration + out.XksProxyConfiguration = &cfg + } + + return out +} + +// UpdateCustomKeyStore changes a store's properties. The name (and, for an +// external key store, the proxy credential and URI path) can change while the +// store is CONNECTED or DISCONNECTED; every other property requires +// DISCONNECTED. +// +//nolint:gocritic // in is the public input, taken by value to match the driver API +func (m *Mock) UpdateCustomKeyStore(_ context.Context, in driver.UpdateCustomKeyStoreInput) error { + m.cksMu.Lock() + defer m.cksMu.Unlock() + + cur, ok := m.customKeyStores.Get(in.CustomKeyStoreID) + if !ok { + return cksNotFound(in.CustomKeyStoreID) + } + + next := copyStore(cur) + + if in.NewCustomKeyStoreName != "" { + if err := validateStoreName(in.NewCustomKeyStoreName); err != nil { + return err + } + + next.CustomKeyStoreName = in.NewCustomKeyStoreName + } + + var err error + if cur.CustomKeyStoreType == driver.CustomKeyStoreTypeExternal { + err = applyXksUpdate(&next, &in) + } else { + err = applyCloudHSMUpdate(&next, &in) + } + + if err != nil { + return err + } + + if err := m.checkStoreUniqueness(&next); err != nil { + return err + } + + m.customKeyStores.Set(next.CustomKeyStoreID, &next) + + return nil +} + +func requireDisconnected(s *driver.CustomKeyStore, what string) error { + if s.ConnectionState != driver.ConnectionStateDisconnected { + return cksInvalidState("Custom key store %s must be DISCONNECTED to update %s (ConnectionState %s)", + s.CustomKeyStoreID, what, s.ConnectionState) + } + + return nil +} + +func requireConnectedOrDisconnected(s *driver.CustomKeyStore, what string) error { + if s.ConnectionState != driver.ConnectionStateDisconnected && s.ConnectionState != driver.ConnectionStateConnected { + return cksInvalidState("Custom key store %s must be CONNECTED or DISCONNECTED to update %s (ConnectionState %s)", + s.CustomKeyStoreID, what, s.ConnectionState) + } + + return nil +} + +//nolint:gocyclo // a flat list of the CloudHSM update rules KMS documents +func applyCloudHSMUpdate(s *driver.CustomKeyStore, in *driver.UpdateCustomKeyStoreInput) error { + if in.XksProxyURIEndpoint != "" || in.XksProxyURIPath != "" || in.XksProxyConnectivity != "" || + in.XksProxyVpcEndpointServiceName != "" || in.XksProxyVpcEndpointServiceOwner != "" || + in.XksProxyAuthenticationCredential != nil { + return invalidParam("XksProxy parameters are valid only for custom key stores of type EXTERNAL_KEY_STORE") + } + + if in.KeyStorePassword == "" && in.CloudHsmClusterID == "" { + return nil + } + + if err := requireDisconnected(s, "KeyStorePassword or CloudHsmClusterId"); err != nil { + return err + } + + if err := validateCloudHSMFields(in.CloudHsmClusterID, "", in.KeyStorePassword); err != nil { + return err + } + + if in.KeyStorePassword != "" { + s.KeyStorePassword = in.KeyStorePassword + } + + if in.CloudHsmClusterID != "" { + s.CloudHsmClusterID = in.CloudHsmClusterID + } + + return nil +} + +//nolint:gocyclo // a flat list of the XKS update rules KMS documents +func applyXksUpdate(s *driver.CustomKeyStore, in *driver.UpdateCustomKeyStoreInput) error { + if in.KeyStorePassword != "" || in.CloudHsmClusterID != "" { + return invalidParam("KeyStorePassword and CloudHsmClusterId are valid only for custom key stores of type AWS_CLOUDHSM") + } + + cfg := s.XksProxyConfiguration + if cfg == nil { + cfg = &driver.XksProxyConfiguration{} + s.XksProxyConfiguration = cfg + } + + if in.XksProxyAuthenticationCredential != nil || in.XksProxyURIPath != "" { + if err := requireConnectedOrDisconnected(s, "XksProxyAuthenticationCredential or XksProxyUriPath"); err != nil { + return err + } + } + + if in.XksProxyURIEndpoint != "" || in.XksProxyConnectivity != "" || + in.XksProxyVpcEndpointServiceName != "" || in.XksProxyVpcEndpointServiceOwner != "" { + if err := requireDisconnected(s, "the external key store proxy connection settings"); err != nil { + return err + } + } + + if err := applyXksConnectivity(cfg, in); err != nil { + return err + } + + if in.XksProxyURIEndpoint != "" { + cfg.URIEndpoint = in.XksProxyURIEndpoint + } + + if in.XksProxyURIPath != "" { + cfg.URIPath = in.XksProxyURIPath + } + + if in.XksProxyVpcEndpointServiceName != "" { + cfg.VpcEndpointServiceName = in.XksProxyVpcEndpointServiceName + } + + if in.XksProxyVpcEndpointServiceOwner != "" { + cfg.VpcEndpointServiceOwner = in.XksProxyVpcEndpointServiceOwner + } + + if c := in.XksProxyAuthenticationCredential; c != nil { + cfg.AccessKeyID = c.AccessKeyID + s.XksRawSecretAccessKey = c.RawSecretAccessKey + } + + return validateXksConfig(cfg, in.XksProxyAuthenticationCredential) +} + +// applyXksConnectivity switches the proxy connectivity. Moving to +// VPC_ENDPOINT_SERVICE needs a new endpoint and an endpoint service name; +// moving to PUBLIC_ENDPOINT needs a new endpoint and drops the service name. +func applyXksConnectivity(cfg *driver.XksProxyConfiguration, in *driver.UpdateCustomKeyStoreInput) error { + if in.XksProxyConnectivity == "" || in.XksProxyConnectivity == cfg.Connectivity { + return nil + } + + switch in.XksProxyConnectivity { + case driver.XksConnectivityVpcEndpointService: + if in.XksProxyURIEndpoint == "" || in.XksProxyVpcEndpointServiceName == "" { + return invalidParam("changing XksProxyConnectivity to VPC_ENDPOINT_SERVICE requires " + + "XksProxyUriEndpoint and XksProxyVpcEndpointServiceName") + } + case driver.XksConnectivityPublicEndpoint: + if in.XksProxyURIEndpoint == "" { + return invalidParam("changing XksProxyConnectivity to PUBLIC_ENDPOINT requires XksProxyUriEndpoint") + } + + cfg.VpcEndpointServiceName = "" + cfg.VpcEndpointServiceOwner = "" + } + + cfg.Connectivity = in.XksProxyConnectivity + + return nil +} + +// DeleteCustomKeyStore deletes a DISCONNECTED store that holds no KMS keys. +func (m *Mock) DeleteCustomKeyStore(_ context.Context, id string) error { + m.cksMu.Lock() + defer m.cksMu.Unlock() + + s, ok := m.customKeyStores.Get(id) + if !ok { + return cksNotFound(id) + } + + if s.ConnectionState != driver.ConnectionStateDisconnected { + return cksInvalidState("Custom key store %s must be DISCONNECTED before it can be deleted (ConnectionState %s)", + id, s.ConnectionState) + } + + for _, kd := range m.keys.All() { + kd.mu.RLock() + holds := kd.meta.CustomKeyStoreID == id && !m.keyGone(kd) + kd.mu.RUnlock() + + if holds { + return driver.NewAPIError(driver.ExCustomKeyStoreHasCMKs, errors.FailedPrecondition, + "Custom key store %s contains KMS keys; delete them before deleting the key store", id) + } + } + + m.customKeyStores.Delete(id) + + return nil +} + +// ConnectCustomKeyStore connects a store. cloudemu has no backing CloudHSM +// cluster or XKS proxy to reach, so the connection completes immediately +// (DescribeCustomKeyStores reports CONNECTED). Connecting a store that is +// DISCONNECTING or FAILED is rejected, as in KMS. +func (m *Mock) ConnectCustomKeyStore(_ context.Context, id string) error { + return m.transitionStore(id, func(s *driver.CustomKeyStore) error { + switch s.ConnectionState { + case driver.ConnectionStateDisconnecting, driver.ConnectionStateFailed: + return cksInvalidState("Custom key store %s cannot be connected from ConnectionState %s", + id, s.ConnectionState) + } + + s.ConnectionState = driver.ConnectionStateConnected + s.ConnectionErrorCode = "" + + return nil + }) +} + +// DisconnectCustomKeyStore disconnects a store; a store already DISCONNECTED or +// DISCONNECTING is rejected, as in KMS. +func (m *Mock) DisconnectCustomKeyStore(_ context.Context, id string) error { + return m.transitionStore(id, func(s *driver.CustomKeyStore) error { + switch s.ConnectionState { + case driver.ConnectionStateDisconnected, driver.ConnectionStateDisconnecting: + return cksInvalidState("Custom key store %s cannot be disconnected from ConnectionState %s", + id, s.ConnectionState) + } + + s.ConnectionState = driver.ConnectionStateDisconnected + s.ConnectionErrorCode = "" + + return nil + }) +} + +func (m *Mock) transitionStore(id string, fn func(*driver.CustomKeyStore) error) error { + m.cksMu.Lock() + defer m.cksMu.Unlock() + + cur, ok := m.customKeyStores.Get(id) + if !ok { + return cksNotFound(id) + } + + next := copyStore(cur) + if err := fn(&next); err != nil { + return err + } + + m.customKeyStores.Set(id, &next) + + return nil +} + +// GenerateRandomInCustomKeyStore generates random bytes in a custom key +// store's CloudHSM cluster. KMS rejects an external key store with +// UnsupportedOperationException and a store that isn't CONNECTED with +// CustomKeyStoreInvalidStateException. +func (m *Mock) GenerateRandomInCustomKeyStore(ctx context.Context, id string, numberOfBytes int32) ([]byte, error) { + if err := validateRandomBytes(numberOfBytes); err != nil { + return nil, err + } + + s, ok := m.customKeyStores.Get(id) + if !ok { + return nil, cksNotFound(id) + } + + if s.CustomKeyStoreType == driver.CustomKeyStoreTypeExternal { + return nil, driver.WithMessage(driver.ErrUnsupportedOperation, + "GenerateRandom is not supported for external key store %s", id) + } + + if s.ConnectionState != driver.ConnectionStateConnected { + return nil, cksInvalidState("Custom key store %s is not connected (ConnectionState %s)", id, s.ConnectionState) + } + + return m.GenerateRandom(ctx, numberOfBytes) +} diff --git a/providers/aws/kms/customkeystore_test.go b/providers/aws/kms/customkeystore_test.go new file mode 100644 index 000000000..360760f71 --- /dev/null +++ b/providers/aws/kms/customkeystore_test.go @@ -0,0 +1,664 @@ +package kms_test + +import ( + "context" + "errors" + "sort" + "strings" + "testing" + "time" + + "github.com/stackshy/cloudemu/v2/config" + cerrors "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/providers/aws/kms" + "github.com/stackshy/cloudemu/v2/services/kms/driver" +) + +// --- custom key stores --- + +const ( + testCluster = "cluster-abcdefghijk" + testTA = "-----BEGIN CERTIFICATE-----\nMIIB\n-----END CERTIFICATE-----" + testXksAKID = "ABCDEFGHIJKLMNOPQRST" + testXksSK = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQ=" +) + +func newHSMStore(t *testing.T, m *kms.Mock, name, cluster string) string { + t.Helper() + + id, err := m.CreateCustomKeyStore(context.Background(), driver.CreateCustomKeyStoreInput{ + CustomKeyStoreName: name, CloudHsmClusterID: cluster, TrustAnchorCertificate: testTA, KeyStorePassword: "kmsPswd1", + }) + if err != nil { + t.Fatalf("CreateCustomKeyStore: %v", err) + } + + return id +} + +func xksInput(name, endpoint, path string) driver.CreateCustomKeyStoreInput { + return driver.CreateCustomKeyStoreInput{ + CustomKeyStoreName: name, + CustomKeyStoreType: driver.CustomKeyStoreTypeExternal, + XksProxyURIEndpoint: endpoint, + XksProxyURIPath: path, + XksProxyConnectivity: driver.XksConnectivityPublicEndpoint, + XksProxyAuthenticationCredential: &driver.XksProxyAuthenticationCredential{ + AccessKeyID: testXksAKID, RawSecretAccessKey: testXksSK, + }, + } +} + +func describeOne(t *testing.T, m *kms.Mock, id string) driver.CustomKeyStore { + t.Helper() + + out, err := m.DescribeCustomKeyStores(context.Background(), id, "") + if err != nil || len(out) != 1 { + t.Fatalf("DescribeCustomKeyStores(%s) = %v, %v", id, out, err) + } + + return out[0] +} + +func TestCustomKeyStoreLifecycle(t *testing.T) { + ctx := context.Background() + m := newMock(t) + + id := newHSMStore(t, m, "hsm-store", testCluster) + + s := describeOne(t, m, id) + if s.ConnectionState != driver.ConnectionStateDisconnected || s.CustomKeyStoreType != driver.CustomKeyStoreTypeCloudHSM || + s.CloudHsmClusterID != testCluster || s.TrustAnchorCertificate != testTA { + t.Fatalf("new store = %+v", s) + } + + // Duplicate name / cluster. + _, err := m.CreateCustomKeyStore(ctx, driver.CreateCustomKeyStoreInput{ + CustomKeyStoreName: "hsm-store", CloudHsmClusterID: "cluster-zzzzzzzzzzz", TrustAnchorCertificate: testTA, KeyStorePassword: "kmsPswd1", + }) + wantException(t, err, driver.ExCustomKeyStoreNameInUse) + + _, err = m.CreateCustomKeyStore(ctx, driver.CreateCustomKeyStoreInput{ + CustomKeyStoreName: "other", CloudHsmClusterID: testCluster, TrustAnchorCertificate: testTA, KeyStorePassword: "kmsPswd1", + }) + wantException(t, err, driver.ExCloudHsmClusterInUse) + + // Missing required CloudHSM params โ†’ validation. + if _, err = m.CreateCustomKeyStore(ctx, driver.CreateCustomKeyStoreInput{CustomKeyStoreName: "x"}); !cerrors.IsInvalidArgument(err) { + t.Fatalf("missing params: %v", err) + } + + // Creating a key in a DISCONNECTED store fails. + _, err = m.CreateKey(ctx, driver.CreateKeyInput{Origin: driver.OriginAWSCloudHSM, CustomKeyStoreID: id}) + wantException(t, err, driver.ExCustomKeyStoreInvalidState) + + // Disconnecting a DISCONNECTED store fails; connecting succeeds. + wantException(t, m.DisconnectCustomKeyStore(ctx, id), driver.ExCustomKeyStoreInvalidState) + + if err := m.ConnectCustomKeyStore(ctx, id); err != nil { + t.Fatalf("Connect: %v", err) + } + + if st := describeOne(t, m, id).ConnectionState; st != driver.ConnectionStateConnected { + t.Fatalf("state after connect = %s", st) + } + + // Rename allowed while CONNECTED; password change is not. + if err := m.UpdateCustomKeyStore(ctx, driver.UpdateCustomKeyStoreInput{CustomKeyStoreID: id, NewCustomKeyStoreName: "renamed"}); err != nil { + t.Fatalf("rename while connected: %v", err) + } + + err = m.UpdateCustomKeyStore(ctx, driver.UpdateCustomKeyStoreInput{CustomKeyStoreID: id, KeyStorePassword: "newPassw0rd"}) + wantException(t, err, driver.ExCustomKeyStoreInvalidState) + + // Delete while CONNECTED โ†’ invalid state. + wantException(t, m.DeleteCustomKeyStore(ctx, id), driver.ExCustomKeyStoreInvalidState) + + // Create a key in the connected store; it works for crypto. + k, err := m.CreateKey(ctx, driver.CreateKeyInput{Origin: driver.OriginAWSCloudHSM, CustomKeyStoreID: id}) + if err != nil { + t.Fatalf("CreateKey in store: %v", err) + } + + if k.CustomKeyStoreID != id || k.CloudHsmClusterID != testCluster || k.Origin != driver.OriginAWSCloudHSM { + t.Fatalf("key metadata = %+v", k) + } + + if _, err := m.Encrypt(ctx, driver.EncryptInput{KeyID: k.KeyID, Plaintext: []byte("hi")}); err != nil { + t.Fatalf("Encrypt in connected store: %v", err) + } + + // Only symmetric keys may live in a store. + _, err = m.CreateKey(ctx, driver.CreateKeyInput{ + Origin: driver.OriginAWSCloudHSM, CustomKeyStoreID: id, KeySpec: driver.SpecRSA2048, KeyUsage: driver.UsageSignVerify, + }) + if !errors.Is(err, driver.ErrUnsupportedOperation) { + t.Fatalf("asymmetric in store: %v", err) + } + + // Rotation is unsupported for custom key store keys. + if err := m.EnableKeyRotation(ctx, k.KeyID, 0); !errors.Is(err, driver.ErrUnsupportedOperation) { + t.Fatalf("EnableKeyRotation on CKS key: %v", err) + } + + // Disconnect: crypto on its keys now fails; update password works. + if err := m.DisconnectCustomKeyStore(ctx, id); err != nil { + t.Fatalf("Disconnect: %v", err) + } + + if _, err := m.Encrypt(ctx, driver.EncryptInput{KeyID: k.KeyID, Plaintext: []byte("hi")}); !errors.Is(err, driver.ErrKeyInvalidState) { + t.Fatalf("Encrypt with disconnected store: %v", err) + } + + if err := m.UpdateCustomKeyStore(ctx, driver.UpdateCustomKeyStoreInput{CustomKeyStoreID: id, KeyStorePassword: "newPassw0rd"}); err != nil { + t.Fatalf("password update while disconnected: %v", err) + } + + // Store still holds a key โ†’ CustomKeyStoreHasCMKsException. + wantException(t, m.DeleteCustomKeyStore(ctx, id), driver.ExCustomKeyStoreHasCMKs) + + // Schedule deletion; once the waiting period passes the store can go. + if _, err := m.ScheduleKeyDeletion(ctx, k.KeyID, 7); err != nil { + t.Fatal(err) + } + + wantException(t, m.DeleteCustomKeyStore(ctx, id), driver.ExCustomKeyStoreHasCMKs) +} + +func TestCustomKeyStoreDeleteAfterKeyDeletion(t *testing.T) { + ctx := context.Background() + clock := config.NewFakeClock(time.Unix(0, 0)) + m := kms.New(config.NewOptions(config.WithClock(clock), config.WithRegion("us-east-1"), config.WithAccountID("000000000000"))) + + id := newHSMStore(t, m, "s", testCluster) + if err := m.ConnectCustomKeyStore(ctx, id); err != nil { + t.Fatal(err) + } + + k, err := m.CreateKey(ctx, driver.CreateKeyInput{Origin: driver.OriginAWSCloudHSM, CustomKeyStoreID: id}) + if err != nil { + t.Fatal(err) + } + + if _, err := m.ScheduleKeyDeletion(ctx, k.KeyID, 7); err != nil { + t.Fatal(err) + } + + if err := m.DisconnectCustomKeyStore(ctx, id); err != nil { + t.Fatal(err) + } + + wantException(t, m.DeleteCustomKeyStore(ctx, id), driver.ExCustomKeyStoreHasCMKs) + + clock.Advance(8 * 24 * time.Hour) + + if err := m.DeleteCustomKeyStore(ctx, id); err != nil { + t.Fatalf("Delete after key deletion window: %v", err) + } + + _, err = m.DescribeCustomKeyStores(ctx, id, "") + wantException(t, err, driver.ExCustomKeyStoreNotFound) + + wantException(t, m.ConnectCustomKeyStore(ctx, id), driver.ExCustomKeyStoreNotFound) +} + +func TestExternalKeyStore(t *testing.T) { + ctx := context.Background() + m := newMock(t) + + id, err := m.CreateCustomKeyStore(ctx, xksInput("xks", "https://xks.example.com", "/kms/xks/v1")) + if err != nil { + t.Fatalf("CreateCustomKeyStore XKS: %v", err) + } + + s := describeOne(t, m, id) + if s.XksProxyConfiguration == nil || s.XksProxyConfiguration.AccessKeyID != testXksAKID || + s.XksProxyConfiguration.URIEndpoint != "https://xks.example.com" { + t.Fatalf("xks store = %+v", s) + } + + // Same endpoint+path โ†’ XksProxyUriInUseException. + _, err = m.CreateCustomKeyStore(ctx, xksInput("xks2", "https://xks.example.com", "/kms/xks/v1")) + wantException(t, err, driver.ExXksProxyURIInUse) + + // Bad path / endpoint โ†’ validation. + if _, err = m.CreateCustomKeyStore(ctx, xksInput("xks3", "http://xks.example.com", "/kms/xks/v1")); !cerrors.IsInvalidArgument(err) { + t.Fatalf("http endpoint: %v", err) + } + + if _, err = m.CreateCustomKeyStore(ctx, xksInput("xks3", "https://xks.example.com", "/bad")); !cerrors.IsInvalidArgument(err) { + t.Fatalf("bad path: %v", err) + } + + // VPC connectivity needs an endpoint service name. + vin := xksInput("vpc", "https://vpc.example.com", "/kms/xks/v1") + vin.XksProxyConnectivity = driver.XksConnectivityVpcEndpointService + + if _, err = m.CreateCustomKeyStore(ctx, vin); !cerrors.IsInvalidArgument(err) { + t.Fatalf("vpc without service name: %v", err) + } + + vin.XksProxyVpcEndpointServiceName = "com.amazonaws.vpce.us-east-1.vpce-svc-0123456789abcdef0" + if _, err = m.CreateCustomKeyStore(ctx, vin); err != nil { + t.Fatalf("vpc store: %v", err) + } + + // A public store can't share the VPC store's endpoint. + _, err = m.CreateCustomKeyStore(ctx, xksInput("pub", "https://vpc.example.com", "/p/kms/xks/v1")) + wantException(t, err, driver.ExXksProxyURIEndpointInUse) + + if err := m.ConnectCustomKeyStore(ctx, id); err != nil { + t.Fatal(err) + } + + // XksKeyId required, unique within the store. + _, err = m.CreateKey(ctx, driver.CreateKeyInput{Origin: driver.OriginExternalKeyStore, CustomKeyStoreID: id}) + if !cerrors.IsInvalidArgument(err) { + t.Fatalf("missing XksKeyId: %v", err) + } + + k, err := m.CreateKey(ctx, driver.CreateKeyInput{Origin: driver.OriginExternalKeyStore, CustomKeyStoreID: id, XksKeyID: "ext-key-1"}) + if err != nil { + t.Fatalf("CreateKey XKS: %v", err) + } + + if k.XksKeyID != "ext-key-1" || k.CustomKeyStoreID != id { + t.Fatalf("xks key = %+v", k) + } + + _, err = m.CreateKey(ctx, driver.CreateKeyInput{Origin: driver.OriginExternalKeyStore, CustomKeyStoreID: id, XksKeyID: "ext-key-1"}) + wantException(t, err, driver.ExXksKeyAlreadyInUse) + + // Wrong origin for the store type. + _, err = m.CreateKey(ctx, driver.CreateKeyInput{Origin: driver.OriginAWSCloudHSM, CustomKeyStoreID: id}) + if !cerrors.IsInvalidArgument(err) { + t.Fatalf("origin mismatch: %v", err) + } + + // While CONNECTED: path + credential updatable, endpoint is not. + if err := m.UpdateCustomKeyStore(ctx, driver.UpdateCustomKeyStoreInput{ + CustomKeyStoreID: id, XksProxyURIPath: "/v2pre/kms/xks/v1", + }); err != nil { + t.Fatalf("update path while connected: %v", err) + } + + err = m.UpdateCustomKeyStore(ctx, driver.UpdateCustomKeyStoreInput{ + CustomKeyStoreID: id, XksProxyURIEndpoint: "https://new.example.com", + }) + wantException(t, err, driver.ExCustomKeyStoreInvalidState) + + // CloudHSM-only params rejected for an XKS store. + if err := m.UpdateCustomKeyStore(ctx, driver.UpdateCustomKeyStoreInput{ + CustomKeyStoreID: id, KeyStorePassword: "abcdefgh", + }); !cerrors.IsInvalidArgument(err) { + t.Fatalf("password on xks: %v", err) + } + + // Describe by name, and both id+name โ†’ validation. + byName, err := m.DescribeCustomKeyStores(ctx, "", "xks") + if err != nil || len(byName) != 1 || byName[0].CustomKeyStoreID != id { + t.Fatalf("describe by name = %v, %v", byName, err) + } + + if _, err := m.DescribeCustomKeyStores(ctx, id, "xks"); !cerrors.IsInvalidArgument(err) { + t.Fatalf("id+name: %v", err) + } + + _, err = m.DescribeCustomKeyStores(ctx, "", "missing") + wantException(t, err, driver.ExCustomKeyStoreNotFound) +} + +func TestCustomKeyStoreSnapshotRoundTrip(t *testing.T) { + ctx := context.Background() + src := newMock(t) + + id := newHSMStore(t, src, "persisted", testCluster) + if err := src.ConnectCustomKeyStore(ctx, id); err != nil { + t.Fatal(err) + } + + k, err := src.CreateKey(ctx, driver.CreateKeyInput{Origin: driver.OriginAWSCloudHSM, CustomKeyStoreID: id}) + if err != nil { + t.Fatal(err) + } + + snap, err := src.Snapshot(ctx, false) + if err != nil { + t.Fatal(err) + } + + dst := newMock(t) + if err := dst.Restore(ctx, snap); err != nil { + t.Fatal(err) + } + + got := describeOne(t, dst, id) + if got.CustomKeyStoreName != "persisted" || got.ConnectionState != driver.ConnectionStateConnected { + t.Fatalf("restored store = %+v", got) + } + + md, err := dst.DescribeKey(ctx, k.KeyID) + if err != nil || md.CustomKeyStoreID != id { + t.Fatalf("restored key = %+v, %v", md, err) + } + + // Name uniqueness still enforced after restore. + _, err = dst.CreateCustomKeyStore(ctx, driver.CreateCustomKeyStoreInput{ + CustomKeyStoreName: "persisted", CloudHsmClusterID: "cluster-zzzzzzzzzzz", TrustAnchorCertificate: testTA, KeyStorePassword: "kmsPswd1", + }) + wantException(t, err, driver.ExCustomKeyStoreNameInUse) +} + +func wantKeyState(t *testing.T, m *kms.Mock, keyID, state string, enabled bool) { + t.Helper() + + md, err := m.DescribeKey(context.Background(), keyID) + if err != nil { + t.Fatalf("DescribeKey: %v", err) + } + + if md.KeyState != state || md.Enabled != enabled { + t.Fatalf("DescribeKey state = %s enabled=%v, want %s enabled=%v", md.KeyState, md.Enabled, state, enabled) + } + + keys, err := m.ListKeys(context.Background()) + if err != nil { + t.Fatalf("ListKeys: %v", err) + } + + for i := range keys { + if keys[i].KeyID == keyID && keys[i].KeyState != state { + t.Fatalf("ListKeys state = %s, want %s", keys[i].KeyState, state) + } + } +} + +func wantUnavailable(t *testing.T, err error) { + t.Helper() + + if !errors.Is(err, driver.ErrKeyInvalidState) || errors.Is(err, driver.ErrKeyDisabled) { + t.Fatalf("err = %v, want ErrKeyInvalidState", err) + } + + if msg := cerrors.Message(err); !strings.HasSuffix(msg, " is unavailable") { + t.Fatalf("message = %q, want ' is unavailable'", msg) + } +} + +// TestCustomKeyStoreKeyUnavailableWhileDisconnected: a key in a store that is +// not CONNECTED reports KeyState Unavailable (except PendingDeletion), crypto +// fails with KMSInvalidStateException even for a Disabled key, and +// EnableKey/DisableKey take effect once the store reconnects. +func TestCustomKeyStoreKeyUnavailableWhileDisconnected(t *testing.T) { + ctx := context.Background() + m := newMock(t) + + id := newHSMStore(t, m, "hsm", testCluster) + if err := m.ConnectCustomKeyStore(ctx, id); err != nil { + t.Fatal(err) + } + + k, err := m.CreateKey(ctx, driver.CreateKeyInput{Origin: driver.OriginAWSCloudHSM, CustomKeyStoreID: id}) + if err != nil { + t.Fatal(err) + } + + wantKeyState(t, m, k.KeyID, driver.StateEnabled, true) + + if err := m.DisconnectCustomKeyStore(ctx, id); err != nil { + t.Fatal(err) + } + + wantKeyState(t, m, k.KeyID, driver.StateUnavailable, false) + + _, err = m.Encrypt(ctx, driver.EncryptInput{KeyID: k.KeyID, Plaintext: []byte("hi")}) + wantUnavailable(t, err) + + // DisableKey succeeds, but the key stays Unavailable and crypto still fails + // with KMSInvalidStateException rather than DisabledException. + if err := m.DisableKey(ctx, k.KeyID); err != nil { + t.Fatalf("DisableKey on unavailable key: %v", err) + } + + wantKeyState(t, m, k.KeyID, driver.StateUnavailable, false) + + _, err = m.GenerateDataKey(ctx, driver.GenerateDataKeyInput{KeyID: k.KeyID, KeySpec: "AES_256"}) + wantUnavailable(t, err) + + // A PendingDeletion key keeps that state while its store is disconnected, + // and canceling the deletion makes it Unavailable again. + if _, err := m.ScheduleKeyDeletion(ctx, k.KeyID, 7); err != nil { + t.Fatalf("ScheduleKeyDeletion on unavailable key: %v", err) + } + + wantKeyState(t, m, k.KeyID, driver.StatePendingDeletion, false) + + canceled, err := m.CancelKeyDeletion(ctx, k.KeyID) + if err != nil || canceled.KeyState != driver.StateUnavailable { + t.Fatalf("CancelKeyDeletion = %+v, %v", canceled, err) + } + + // Reconnecting reveals the stored state: Disabled. + if err := m.ConnectCustomKeyStore(ctx, id); err != nil { + t.Fatal(err) + } + + wantKeyState(t, m, k.KeyID, driver.StateDisabled, false) + + if _, err := m.Encrypt(ctx, driver.EncryptInput{KeyID: k.KeyID, Plaintext: []byte("hi")}); !errors.Is(err, driver.ErrKeyDisabled) { + t.Fatalf("Encrypt on disabled key after reconnect: %v", err) + } + + if err := m.EnableKey(ctx, k.KeyID); err != nil { + t.Fatal(err) + } + + wantKeyState(t, m, k.KeyID, driver.StateEnabled, true) + + // The derived state is never persisted: a snapshot taken while the store + // is disconnected restores the stored Enabled state. + if err := m.DisconnectCustomKeyStore(ctx, id); err != nil { + t.Fatal(err) + } + + snap, err := m.Snapshot(ctx, false) + if err != nil { + t.Fatal(err) + } + + dst := newMock(t) + if err := dst.Restore(ctx, snap); err != nil { + t.Fatal(err) + } + + wantKeyState(t, dst, k.KeyID, driver.StateUnavailable, false) + + if err := dst.ConnectCustomKeyStore(ctx, id); err != nil { + t.Fatal(err) + } + + wantKeyState(t, dst, k.KeyID, driver.StateEnabled, true) +} + +// TestCustomKeyStoreCreateKeyUnsupportedMessage: a key that can't live in a +// custom key store fails with UnsupportedOperationException naming the rule. +func TestCustomKeyStoreCreateKeyUnsupportedMessage(t *testing.T) { + ctx := context.Background() + m := newMock(t) + + id := newHSMStore(t, m, "hsm", testCluster) + if err := m.ConnectCustomKeyStore(ctx, id); err != nil { + t.Fatal(err) + } + + for name, in := range map[string]driver.CreateKeyInput{ + "multi-region": {Origin: driver.OriginAWSCloudHSM, CustomKeyStoreID: id, MultiRegion: true}, + "rsa": {Origin: driver.OriginAWSCloudHSM, CustomKeyStoreID: id, KeySpec: driver.SpecRSA2048, KeyUsage: driver.UsageSignVerify}, + "hmac": {Origin: driver.OriginAWSCloudHSM, CustomKeyStoreID: id, KeySpec: driver.SpecHMAC256, KeyUsage: driver.UsageGenerateVerifyMac}, + } { + _, err := m.CreateKey(ctx, in) + if !errors.Is(err, driver.ErrUnsupportedOperation) { + t.Fatalf("%s: err = %v, want ErrUnsupportedOperation", name, err) + } + + if msg := cerrors.Message(err); !strings.Contains(msg, "only single-Region symmetric encryption keys") { + t.Fatalf("%s: message = %q", name, msg) + } + } +} + +// TestGenerateRandomInCustomKeyStore: GenerateRandom with a CustomKeyStoreId +// needs an existing, CONNECTED CloudHSM store. +func TestGenerateRandomInCustomKeyStore(t *testing.T) { + ctx := context.Background() + m := newMock(t) + + _, err := m.GenerateRandomInCustomKeyStore(ctx, "cks-missing", 16) + wantException(t, err, driver.ExCustomKeyStoreNotFound) + + hsm := newHSMStore(t, m, "hsm", testCluster) + + _, err = m.GenerateRandomInCustomKeyStore(ctx, hsm, 16) + wantException(t, err, driver.ExCustomKeyStoreInvalidState) + + if _, err := m.GenerateRandomInCustomKeyStore(ctx, hsm, 0); !cerrors.IsInvalidArgument(err) { + t.Fatalf("NumberOfBytes 0: %v", err) + } + + if err := m.ConnectCustomKeyStore(ctx, hsm); err != nil { + t.Fatal(err) + } + + out, err := m.GenerateRandomInCustomKeyStore(ctx, hsm, 16) + if err != nil || len(out) != 16 { + t.Fatalf("connected store = %d bytes, %v", len(out), err) + } + + xks, err := m.CreateCustomKeyStore(ctx, xksInput("xks", "https://xks.example.com", "/kms/xks/v1")) + if err != nil { + t.Fatal(err) + } + + if err := m.ConnectCustomKeyStore(ctx, xks); err != nil { + t.Fatal(err) + } + + if _, err := m.GenerateRandomInCustomKeyStore(ctx, xks, 16); !errors.Is(err, driver.ErrUnsupportedOperation) { + t.Fatalf("external key store: %v", err) + } +} + +// TestUpdateExternalKeyStoreConnectivity switches an external key store +// between PUBLIC_ENDPOINT and VPC_ENDPOINT_SERVICE. +func TestUpdateExternalKeyStoreConnectivity(t *testing.T) { + ctx := context.Background() + m := newMock(t) + + const svc = "com.amazonaws.vpce.us-east-1.vpce-svc-0123456789abcdef0" + + id, err := m.CreateCustomKeyStore(ctx, xksInput("xks", "https://xks.example.com", "/kms/xks/v1")) + if err != nil { + t.Fatal(err) + } + + // To VPC_ENDPOINT_SERVICE: needs a new endpoint and a service name. + for _, in := range []driver.UpdateCustomKeyStoreInput{ + {CustomKeyStoreID: id, XksProxyConnectivity: driver.XksConnectivityVpcEndpointService}, + {CustomKeyStoreID: id, XksProxyConnectivity: driver.XksConnectivityVpcEndpointService, XksProxyURIEndpoint: "https://vpc.example.com"}, + {CustomKeyStoreID: id, XksProxyConnectivity: driver.XksConnectivityVpcEndpointService, XksProxyVpcEndpointServiceName: svc}, + } { + if err := m.UpdateCustomKeyStore(ctx, in); !cerrors.IsInvalidArgument(err) { + t.Fatalf("update %+v: err = %v, want validation error", in, err) + } + } + + if err := m.UpdateCustomKeyStore(ctx, driver.UpdateCustomKeyStoreInput{ + CustomKeyStoreID: id, XksProxyConnectivity: driver.XksConnectivityVpcEndpointService, + XksProxyURIEndpoint: "https://vpc.example.com", XksProxyVpcEndpointServiceName: svc, + XksProxyVpcEndpointServiceOwner: "111122223333", + }); err != nil { + t.Fatalf("switch to VPC_ENDPOINT_SERVICE: %v", err) + } + + cfg := describeOne(t, m, id).XksProxyConfiguration + if cfg.Connectivity != driver.XksConnectivityVpcEndpointService || cfg.URIEndpoint != "https://vpc.example.com" || + cfg.VpcEndpointServiceName != svc || cfg.VpcEndpointServiceOwner != "111122223333" { + t.Fatalf("after switch to VPC = %+v", cfg) + } + + // Connectivity can only change while DISCONNECTED. + if err := m.ConnectCustomKeyStore(ctx, id); err != nil { + t.Fatal(err) + } + + err = m.UpdateCustomKeyStore(ctx, driver.UpdateCustomKeyStoreInput{ + CustomKeyStoreID: id, XksProxyConnectivity: driver.XksConnectivityPublicEndpoint, XksProxyURIEndpoint: "https://pub.example.com", + }) + wantException(t, err, driver.ExCustomKeyStoreInvalidState) + + if err := m.DisconnectCustomKeyStore(ctx, id); err != nil { + t.Fatal(err) + } + + // Back to PUBLIC_ENDPOINT: needs a new endpoint and drops the service. + err = m.UpdateCustomKeyStore(ctx, driver.UpdateCustomKeyStoreInput{ + CustomKeyStoreID: id, XksProxyConnectivity: driver.XksConnectivityPublicEndpoint, + }) + if !cerrors.IsInvalidArgument(err) { + t.Fatalf("PUBLIC_ENDPOINT without endpoint: %v", err) + } + + if err := m.UpdateCustomKeyStore(ctx, driver.UpdateCustomKeyStoreInput{ + CustomKeyStoreID: id, XksProxyConnectivity: driver.XksConnectivityPublicEndpoint, XksProxyURIEndpoint: "https://pub.example.com", + }); err != nil { + t.Fatalf("switch to PUBLIC_ENDPOINT: %v", err) + } + + cfg = describeOne(t, m, id).XksProxyConfiguration + if cfg.Connectivity != driver.XksConnectivityPublicEndpoint || cfg.URIEndpoint != "https://pub.example.com" || + cfg.VpcEndpointServiceName != "" || cfg.VpcEndpointServiceOwner != "" { + t.Fatalf("after switch to PUBLIC = %+v", cfg) + } +} + +// TestDescribeCustomKeyStoresOrder: the list is ordered by creation date, then +// by ID for stores created at the same instant, so paging is stable. The +// newest store is created first, so map or insertion order can't produce the +// expected result by accident. +func TestDescribeCustomKeyStoresOrder(t *testing.T) { + ctx := context.Background() + clock := config.NewFakeClock(time.Unix(0, 0).Add(time.Hour)) + m := kms.New(config.NewOptions(config.WithClock(clock), config.WithRegion("us-east-1"), config.WithAccountID("000000000000"))) + + newest := newHSMStore(t, m, "newest", "cluster-cccccccccccc") + + clock.Set(time.Unix(0, 0)) + + sameTime := []string{ + newHSMStore(t, m, "a", "cluster-aaaaaaaaaaaa"), + newHSMStore(t, m, "b", "cluster-bbbbbbbbbbbb"), + } + + xks, err := m.CreateCustomKeyStore(ctx, xksInput("c", "https://xks.example.com", "/kms/xks/v1")) + if err != nil { + t.Fatal(err) + } + + sameTime = append(sameTime, xks) + sort.Strings(sameTime) + + out, err := m.DescribeCustomKeyStores(ctx, "", "") + if err != nil { + t.Fatal(err) + } + + got := make([]string, 0, len(out)) + for i := range out { + got = append(got, out[i].CustomKeyStoreID) + } + + want := append(sameTime, newest) + if strings.Join(got, ",") != strings.Join(want, ",") { + t.Fatalf("order = %v, want %v", got, want) + } +} diff --git a/providers/aws/kms/derive_shared_secret_test.go b/providers/aws/kms/derive_shared_secret_test.go new file mode 100644 index 000000000..310dde38d --- /dev/null +++ b/providers/aws/kms/derive_shared_secret_test.go @@ -0,0 +1,238 @@ +package kms_test + +import ( + "bytes" + "context" + "crypto/ecdsa" + "crypto/elliptic" + "crypto/rand" + "crypto/x509" + "errors" + "testing" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/services/kms/driver" +) + +func wantException(t *testing.T, err error, exception string) { + t.Helper() + + var apiErr *driver.APIError + if !errors.As(err, &apiErr) || apiErr.Exception != exception { + t.Fatalf("err = %v, want %s", err, exception) + } +} + +// --- DeriveSharedSecret (ECDH) --- + +func TestDeriveSharedSecretMatchesLocalECDH(t *testing.T) { + ctx := context.Background() + m := newMock(t) + + for _, tc := range []struct { + spec string + curve elliptic.Curve + }{ + {driver.SpecECCNISTP256, elliptic.P256()}, + {driver.SpecECCNISTP384, elliptic.P384()}, + {driver.SpecECCNISTP521, elliptic.P521()}, + } { + t.Run(tc.spec, func(t *testing.T) { + k, err := m.CreateKey(ctx, driver.CreateKeyInput{KeySpec: tc.spec, KeyUsage: driver.UsageKeyAgreement}) + if err != nil { + t.Fatalf("CreateKey KEY_AGREEMENT: %v", err) + } + + if len(k.KeyAgreementAlgorithms) != 1 || k.KeyAgreementAlgorithms[0] != driver.KeyAgreementECDH { + t.Fatalf("KeyAgreementAlgorithms = %v, want [ECDH]", k.KeyAgreementAlgorithms) + } + + if len(k.SigningAlgorithms) != 0 || len(k.EncryptionAlgorithms) != 0 { + t.Fatalf("unexpected algorithms on KEY_AGREEMENT key: %+v", k) + } + + // Bob's key pair lives outside KMS. + bob, err := ecdsa.GenerateKey(tc.curve, rand.Reader) + if err != nil { + t.Fatal(err) + } + + bobPub, err := x509.MarshalPKIXPublicKey(&bob.PublicKey) + if err != nil { + t.Fatal(err) + } + + out, err := m.DeriveSharedSecret(ctx, driver.DeriveSharedSecretInput{ + KeyID: k.KeyID, KeyAgreementAlgorithm: driver.KeyAgreementECDH, PublicKey: bobPub, + }) + if err != nil { + t.Fatalf("DeriveSharedSecret: %v", err) + } + + if out.KeyAgreementAlgorithm != driver.KeyAgreementECDH || out.KeyOrigin != driver.OriginAWSKMS { + t.Fatalf("output = %+v", out) + } + + // Bob derives the same secret from Alice's (KMS) public key. + pk, err := m.GetPublicKey(ctx, driver.GetPublicKeyInput{KeyID: k.KeyID}) + if err != nil { + t.Fatalf("GetPublicKey: %v", err) + } + + if len(pk.KeyAgreementAlgorithms) != 1 { + t.Fatalf("GetPublicKey KeyAgreementAlgorithms = %v", pk.KeyAgreementAlgorithms) + } + + parsed, err := x509.ParsePKIXPublicKey(pk.PublicKey) + if err != nil { + t.Fatal(err) + } + + alicePub, err := parsed.(*ecdsa.PublicKey).ECDH() + if err != nil { + t.Fatal(err) + } + + bobECDH, err := bob.ECDH() + if err != nil { + t.Fatal(err) + } + + want, err := bobECDH.ECDH(alicePub) + if err != nil { + t.Fatal(err) + } + + if !bytes.Equal(out.SharedSecret, want) { + t.Fatalf("shared secret mismatch:\n kms %x\n local %x", out.SharedSecret, want) + } + }) + } +} + +func TestDeriveSharedSecretErrors(t *testing.T) { + ctx := context.Background() + m := newMock(t) + + agree, err := m.CreateKey(ctx, driver.CreateKeyInput{KeySpec: driver.SpecECCNISTP256, KeyUsage: driver.UsageKeyAgreement}) + if err != nil { + t.Fatal(err) + } + + signKey, err := m.CreateKey(ctx, driver.CreateKeyInput{KeySpec: driver.SpecECCNISTP256, KeyUsage: driver.UsageSignVerify}) + if err != nil { + t.Fatal(err) + } + + peer256, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + peerDER, _ := x509.MarshalPKIXPublicKey(&peer256.PublicKey) + + peer384, _ := ecdsa.GenerateKey(elliptic.P384(), rand.Reader) + peer384DER, _ := x509.MarshalPKIXPublicKey(&peer384.PublicKey) + + // SIGN_VERIFY key โ†’ InvalidKeyUsageException. + _, err = m.DeriveSharedSecret(ctx, driver.DeriveSharedSecretInput{ + KeyID: signKey.KeyID, KeyAgreementAlgorithm: driver.KeyAgreementECDH, PublicKey: peerDER, + }) + if !errors.Is(err, driver.ErrInvalidKeyUsage) { + t.Fatalf("sign key: err = %v, want ErrInvalidKeyUsage", err) + } + + // Peer on a different curve โ†’ validation error. + _, err = m.DeriveSharedSecret(ctx, driver.DeriveSharedSecretInput{ + KeyID: agree.KeyID, KeyAgreementAlgorithm: driver.KeyAgreementECDH, PublicKey: peer384DER, + }) + if !cerrors.IsInvalidArgument(err) { + t.Fatalf("curve mismatch: err = %v, want InvalidArgument", err) + } + + // Garbage public key / bad algorithm โ†’ validation error. + if _, err = m.DeriveSharedSecret(ctx, driver.DeriveSharedSecretInput{ + KeyID: agree.KeyID, KeyAgreementAlgorithm: driver.KeyAgreementECDH, PublicKey: []byte("nope"), + }); !cerrors.IsInvalidArgument(err) { + t.Fatalf("bad DER: err = %v", err) + } + + if _, err = m.DeriveSharedSecret(ctx, driver.DeriveSharedSecretInput{ + KeyID: agree.KeyID, KeyAgreementAlgorithm: "XDH", PublicKey: peerDER, + }); !cerrors.IsInvalidArgument(err) { + t.Fatalf("bad algorithm: err = %v", err) + } + + // Disabled key โ†’ DisabledException. + if err := m.DisableKey(ctx, agree.KeyID); err != nil { + t.Fatal(err) + } + + _, err = m.DeriveSharedSecret(ctx, driver.DeriveSharedSecretInput{ + KeyID: agree.KeyID, KeyAgreementAlgorithm: driver.KeyAgreementECDH, PublicKey: peerDER, + }) + if !errors.Is(err, driver.ErrKeyDisabled) { + t.Fatalf("disabled: err = %v", err) + } + + // A KEY_AGREEMENT key can't sign. + if err := m.EnableKey(ctx, agree.KeyID); err != nil { + t.Fatal(err) + } + + _, err = m.Sign(ctx, driver.SignInput{ + KeyID: agree.KeyID, Message: []byte("x"), MessageType: driver.MessageTypeRaw, + SigningAlgorithm: driver.SignECDSASHA256, + }) + if !errors.Is(err, driver.ErrInvalidKeyUsage) { + t.Fatalf("sign with KEY_AGREEMENT key: err = %v", err) + } +} + +func TestCreateKeyKeyAgreementSpecRules(t *testing.T) { + ctx := context.Background() + m := newMock(t) + + for _, spec := range []string{driver.SpecSymmetricDefault, driver.SpecRSA2048, driver.SpecHMAC256} { + if _, err := m.CreateKey(ctx, driver.CreateKeyInput{KeySpec: spec, KeyUsage: driver.UsageKeyAgreement}); !cerrors.IsInvalidArgument(err) { + t.Fatalf("%s + KEY_AGREEMENT: err = %v, want InvalidArgument", spec, err) + } + } +} + +func TestKeyAgreementKeySurvivesSnapshot(t *testing.T) { + ctx := context.Background() + src := newMock(t) + + k, err := src.CreateKey(ctx, driver.CreateKeyInput{KeySpec: driver.SpecECCNISTP256, KeyUsage: driver.UsageKeyAgreement}) + if err != nil { + t.Fatal(err) + } + + peer, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + peerDER, _ := x509.MarshalPKIXPublicKey(&peer.PublicKey) + + before, err := src.DeriveSharedSecret(ctx, driver.DeriveSharedSecretInput{ + KeyID: k.KeyID, KeyAgreementAlgorithm: driver.KeyAgreementECDH, PublicKey: peerDER, + }) + if err != nil { + t.Fatal(err) + } + + snap, err := src.Snapshot(ctx, false) + if err != nil { + t.Fatal(err) + } + + dst := newMock(t) + if err := dst.Restore(ctx, snap); err != nil { + t.Fatal(err) + } + + after, err := dst.DeriveSharedSecret(ctx, driver.DeriveSharedSecretInput{ + KeyID: k.KeyID, KeyAgreementAlgorithm: driver.KeyAgreementECDH, PublicKey: peerDER, + }) + if err != nil { + t.Fatal(err) + } + + if !bytes.Equal(before.SharedSecret, after.SharedSecret) { + t.Fatal("shared secret changed across snapshot/restore") + } +} diff --git a/providers/aws/kms/keyimport.go b/providers/aws/kms/keyimport.go index 198b5195a..b593c6781 100644 --- a/providers/aws/kms/keyimport.go +++ b/providers/aws/kms/keyimport.go @@ -176,6 +176,10 @@ func (m *Mock) ReplicateKey(_ context.Context, in driver.ReplicateKeyInput) (*dr return nil, errors.New(errors.InvalidArgument, "ReplicaRegion is required") } + if err := validateTags(in.Tags); err != nil { + return nil, err + } + kd, err := m.getKey(in.KeyID) if err != nil { return nil, err diff --git a/providers/aws/kms/kms.go b/providers/aws/kms/kms.go index c86bac51f..080ecab32 100644 --- a/providers/aws/kms/kms.go +++ b/providers/aws/kms/kms.go @@ -80,10 +80,16 @@ type aliasData struct { // Mock is an in-memory implementation of AWS KMS. type Mock struct { - keys *memstore.Store[*keyData] - aliases *memstore.Store[*aliasData] - grants *memstore.Store[*driver.Grant] - opts *config.Options + keys *memstore.Store[*keyData] + aliases *memstore.Store[*aliasData] + grants *memstore.Store[*driver.Grant] + customKeyStores *memstore.Store[*driver.CustomKeyStore] + opts *config.Options + + // cksMu serializes custom key store mutations (name/cluster/proxy + // uniqueness, connection-state transitions) and the placement of new keys + // into a store, so a check-then-act can't race. + cksMu sync.Mutex } // New creates a new KMS mock with the given configuration options. @@ -93,6 +99,8 @@ func New(opts *config.Options) *Mock { aliases: memstore.New[*aliasData](), grants: memstore.New[*driver.Grant](), opts: opts, + + customKeyStores: memstore.New[*driver.CustomKeyStore](), } } diff --git a/providers/aws/kms/lifecycle.go b/providers/aws/kms/lifecycle.go index db150099f..4765c6c4c 100644 --- a/providers/aws/kms/lifecycle.go +++ b/providers/aws/kms/lifecycle.go @@ -35,11 +35,25 @@ func (m *Mock) CreateKey(ctx context.Context, in driver.CreateKeyInput) (*driver return nil, err } + if err := validateTags(in.Tags); err != nil { + return nil, err + } + origin := in.Origin if origin == "" { origin = driver.OriginAWSKMS } + // Keys placed in a custom key store are checked (and inserted) under the + // store lock, so a concurrent DeleteCustomKeyStore can't miss them. + m.cksMu.Lock() + defer m.cksMu.Unlock() + + store, err := m.customKeyStoreForNewKey(&in, origin, spec, usage) + if err != nil { + return nil, err + } + id := newKeyID(in.MultiRegion) now := m.now() @@ -62,9 +76,7 @@ func (m *Mock) CreateKey(ctx context.Context, in driver.CreateKeyInput) (*driver policies: map[string]string{driver.DefaultPolicyName: defaultKeyPolicy(in.Policy, m.opts.AccountID)}, } - kd.meta.EncryptionAlgorithms = driver.EncryptionAlgorithmsFor(spec, usage) - kd.meta.SigningAlgorithms = driver.SigningAlgorithmsFor(spec, usage) - kd.meta.MacAlgorithms = driver.MacAlgorithmsFor(spec, usage) + setDerivedMetadata(&kd.meta, store, in.XksKeyID) if in.MultiRegion { kd.meta.MultiRegionKeyType = "PRIMARY" @@ -82,6 +94,21 @@ func (m *Mock) CreateKey(ctx context.Context, in driver.CreateKeyInput) (*driver return &out, nil } +// setDerivedMetadata fills the algorithm lists KMS advertises for the key's +// spec+usage and, for a key in a custom key store, its store placement. +func setDerivedMetadata(md *driver.KeyMetadata, store *driver.CustomKeyStore, xksKeyID string) { + md.EncryptionAlgorithms = driver.EncryptionAlgorithmsFor(md.KeySpec, md.KeyUsage) + md.SigningAlgorithms = driver.SigningAlgorithmsFor(md.KeySpec, md.KeyUsage) + md.MacAlgorithms = driver.MacAlgorithmsFor(md.KeySpec, md.KeyUsage) + md.KeyAgreementAlgorithms = driver.KeyAgreementAlgorithmsFor(md.KeySpec, md.KeyUsage) + + if store != nil { + md.CustomKeyStoreID = store.CustomKeyStoreID + md.CloudHsmClusterID = store.CloudHsmClusterID + md.XksKeyID = xksKeyID + } +} + // populateKeyMaterial fills in a new key's material based on its origin and // spec. EXTERNAL-origin keys start with no material (PendingImport, disabled); // asymmetric specs get a generated private key; everything else gets raw bytes. @@ -154,6 +181,8 @@ func validateSpec(spec string) error { } // validateSpecUsage rejects spec/usage combinations KMS does not allow. +// +//nolint:gocyclo // one case per key-spec family in the KMS key spec reference func validateSpecUsage(spec, usage string) error { switch { case spec == driver.SpecSymmetricDefault && usage != driver.UsageEncryptDecrypt: @@ -165,9 +194,9 @@ func validateSpecUsage(spec, usage string) error { case strings.HasPrefix(spec, "RSA_") && usage != driver.UsageEncryptDecrypt && usage != driver.UsageSignVerify: return errors.Newf(errors.InvalidArgument, "RSA keys support ENCRYPT_DECRYPT or SIGN_VERIFY, got %q", usage) - case strings.HasPrefix(spec, "ECC_") && usage != driver.UsageSignVerify: + case strings.HasPrefix(spec, "ECC_") && usage != driver.UsageSignVerify && usage != driver.UsageKeyAgreement: return errors.Newf(errors.InvalidArgument, - "ECC keys only support SIGN_VERIFY, got %q", usage) + "ECC NIST keys support SIGN_VERIFY or KEY_AGREEMENT, got %q", usage) default: return nil } @@ -183,7 +212,7 @@ func (m *Mock) DescribeKey(_ context.Context, keyID string) (*driver.KeyMetadata kd.mu.RLock() defer kd.mu.RUnlock() - out := kd.meta + out := m.keyMetadata(kd) return &out, nil } @@ -195,7 +224,7 @@ func (m *Mock) ListKeys(_ context.Context) ([]driver.KeyMetadata, error) { for _, kd := range all { kd.mu.RLock() - out = append(out, kd.meta) + out = append(out, m.keyMetadata(kd)) kd.mu.RUnlock() } @@ -272,7 +301,7 @@ func (m *Mock) ScheduleKeyDeletion( kd.meta.Enabled = false kd.meta.KeyState = driver.StatePendingDeletion kd.meta.DeletionDate = m.now().Add(time.Duration(days) * hoursPerDay * time.Hour) - out = kd.meta + out = m.keyMetadata(kd) return nil }) @@ -294,7 +323,7 @@ func (m *Mock) CancelKeyDeletion(_ context.Context, keyID string) (*driver.KeyMe kd.meta.KeyState = driver.StateDisabled kd.meta.DeletionDate = time.Time{} - out = kd.meta + out = m.keyMetadata(kd) return nil }) diff --git a/providers/aws/kms/management.go b/providers/aws/kms/management.go index cb7f67845..22723c7b8 100644 --- a/providers/aws/kms/management.go +++ b/providers/aws/kms/management.go @@ -42,6 +42,16 @@ func (m *Mock) CreateGrant(_ context.Context, in driver.CreateGrantInput) (strin return "", "", errors.New(errors.InvalidArgument, "GranteePrincipal is required") } + if err := validateGrantPrincipal("GranteePrincipal", in.GranteePrincipal); err != nil { + return "", "", err + } + + if in.RetiringPrincipal != "" { + if err := validateGrantPrincipal("RetiringPrincipal", in.RetiringPrincipal); err != nil { + return "", "", err + } + } + if len(in.Operations) == 0 { return "", "", errors.New(errors.InvalidArgument, "at least one grant operation is required") } @@ -165,7 +175,7 @@ func (m *Mock) EnableKeyRotation(_ context.Context, keyID string, rotationPeriod } return m.mutateKey(keyID, func(kd *keyData) error { - if kd.meta.KeySpec != driver.SpecSymmetricDefault || kd.meta.Origin == driver.OriginExternal { + if kd.meta.KeySpec != driver.SpecSymmetricDefault || kd.meta.Origin != driver.OriginAWSKMS { return driver.ErrUnsupportedOperation } @@ -173,7 +183,7 @@ func (m *Mock) EnableKeyRotation(_ context.Context, keyID string, rotationPeriod // state: a disabled key reports DisabledException and a pending-deletion // key reports KMSInvalidStateException, matching real KMS's key-state // table. Real KMS silently no-ops none of these. It rejects them. - if err := requireUsable(kd); err != nil { + if err := m.requireUsable(kd); err != nil { return err } @@ -187,11 +197,11 @@ func (m *Mock) EnableKeyRotation(_ context.Context, keyID string, rotationPeriod // DisableKeyRotation turns off automatic rotation. func (m *Mock) DisableKeyRotation(_ context.Context, keyID string) error { return m.mutateKey(keyID, func(kd *keyData) error { - if kd.meta.KeySpec != driver.SpecSymmetricDefault || kd.meta.Origin == driver.OriginExternal { + if kd.meta.KeySpec != driver.SpecSymmetricDefault || kd.meta.Origin != driver.OriginAWSKMS { return driver.ErrUnsupportedOperation } - if err := requireUsable(kd); err != nil { + if err := m.requireUsable(kd); err != nil { return err } @@ -246,11 +256,11 @@ func (m *Mock) ListKeyRotations(_ context.Context, keyID string) ([]driver.Rotat // RotateKeyOnDemand rotates the key material now and records the rotation. func (m *Mock) RotateKeyOnDemand(_ context.Context, keyID string) error { return m.mutateKey(keyID, func(kd *keyData) error { - if kd.meta.KeySpec != driver.SpecSymmetricDefault || kd.meta.Origin == driver.OriginExternal { + if kd.meta.KeySpec != driver.SpecSymmetricDefault || kd.meta.Origin != driver.OriginAWSKMS { return driver.ErrUnsupportedOperation } - if err := requireUsable(kd); err != nil { + if err := m.requireUsable(kd); err != nil { return err } diff --git a/providers/aws/kms/principal.go b/providers/aws/kms/principal.go new file mode 100644 index 000000000..31e3b12d4 --- /dev/null +++ b/providers/aws/kms/principal.go @@ -0,0 +1,109 @@ +package kms + +import ( + "regexp" + "strings" + + "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/services/kms/driver" +) + +const maxPrincipalLen = 256 + +var ( + // principalPattern is the PrincipalIdType constraint KMS models for + // GranteePrincipal/RetiringPrincipal (^[\w+=,.@:/-]+$, 1-256 chars). + principalPattern = regexp.MustCompile(`^[\w+=,.@:/-]+$`) + // servicePrincipalPattern is the shape of an AWS service principal + // (e.g. "dynamodb.us-east-1.amazonaws.com"), as KMS models it for + // GranteeServicePrincipal. + servicePrincipalPattern = regexp.MustCompile(`^([A-Za-z0-9\-]+)\.([A-Za-z0-9\-]+)(\.[A-Za-z0-9\-]+)+$`) + // accountIDPattern is a bare 12-digit AWS account ID. + accountIDPattern = regexp.MustCompile(`^\d{12}$`) +) + +// arnFieldCount is the number of ':'-separated fields of an ARN +// (arn:partition:service:region:account:resource). +const arnFieldCount = 6 + +// validateGrantPrincipal checks a grant principal. KMS documents the value as +// "the ARN of an AWS principal": an account (iam ...:root), an IAM user or +// role, a federated user, or an assumed-role user. A value that is not one of +// those ARN forms is rejected with InvalidArnException. Values outside the +// modeled PrincipalIdType constraint fail with ValidationException. Service +// principals and bare account IDs are accepted, since AWS services and older +// clients create grants with those forms. +func validateGrantPrincipal(field, p string) error { + if err := validatePrincipalConstraint(field, p); err != nil { + return err + } + + if !strings.HasPrefix(p, "arn:") { + if servicePrincipalPattern.MatchString(p) || accountIDPattern.MatchString(p) { + return nil + } + + return driver.NewAPIError(driver.ExInvalidArn, errors.InvalidArgument, + "%s %s is not a valid principal ARN", field, p) + } + + if !isPrincipalARN(p) { + return driver.NewAPIError(driver.ExInvalidArn, errors.InvalidArgument, + "%s %s does not refer to a valid principal", field, p) + } + + return nil +} + +// validatePrincipalConstraint enforces the modeled PrincipalIdType length and +// pattern, with the message AWS returns for a failed constraint. +func validatePrincipalConstraint(field, p string) error { + const prefix = "1 validation error detected: Value '%s' at '%s' failed to satisfy constraint: " + + if len(p) > maxPrincipalLen { + return errors.Newf(errors.InvalidArgument, + prefix+"Member must have length less than or equal to %d", p, lowerFirst(field), maxPrincipalLen) + } + + if !principalPattern.MatchString(p) { + return errors.Newf(errors.InvalidArgument, + prefix+"Member must satisfy regular expression pattern: ^[\\w+=,.@:/-]+$", p, lowerFirst(field)) + } + + return nil +} + +// isPrincipalARN reports whether arn names a principal a grant can be given +// to: arn::iam:::root|user/...|role/... or +// arn::sts:::assumed-role/...|federated-user/.... IAM +// and STS principal ARNs are global, so the Region field must be empty. +func isPrincipalARN(arn string) bool { + parts := strings.SplitN(arn, ":", arnFieldCount) + if len(parts) != arnFieldCount || parts[1] == "" || parts[3] != "" || !accountIDPattern.MatchString(parts[4]) { + return false + } + + resource := parts[5] + + switch parts[2] { + case "iam": + return resource == "root" || hasNamedPrefix(resource, "user/") || hasNamedPrefix(resource, "role/") + case "sts": + return hasNamedPrefix(resource, "assumed-role/") || hasNamedPrefix(resource, "federated-user/") + default: + return false + } +} + +// hasNamedPrefix reports whether s is prefix followed by a non-empty name. +func hasNamedPrefix(s, prefix string) bool { + return strings.HasPrefix(s, prefix) && len(s) > len(prefix) +} + +func lowerFirst(s string) string { + if s == "" { + return s + } + + return strings.ToLower(s[:1]) + s[1:] +} diff --git a/providers/aws/kms/snapshot.go b/providers/aws/kms/snapshot.go index a52fed42c..31b9001c1 100644 --- a/providers/aws/kms/snapshot.go +++ b/providers/aws/kms/snapshot.go @@ -25,6 +25,9 @@ type kmsSnapshot struct { Keys map[string]*keySnapshot `json:"keys,omitempty"` Aliases map[string]*aliasSnapshot `json:"aliases,omitempty"` Grants json.RawMessage `json:"grants,omitempty"` + // CustomKeyStores holds fully-exported *driver.CustomKeyStore values and + // round-trips through the generic memstore helper. + CustomKeyStores json.RawMessage `json:"customKeyStores,omitempty"` } // keySnapshot mirrors keyData. The asymmetric private key is serialized as @@ -68,6 +71,13 @@ func (m *Mock) Snapshot(_ context.Context, _ bool) (json.RawMessage, error) { snap.Grants = grants + stores, err := m.customKeyStores.Snapshot() + if err != nil { + return nil, fmt.Errorf("kms: snapshot custom key stores: %w", err) + } + + snap.CustomKeyStores = stores + return json.Marshal(snap) } @@ -141,6 +151,12 @@ func (m *Mock) Restore(_ context.Context, data json.RawMessage) error { } } + if len(snap.CustomKeyStores) > 0 { + if err := m.customKeyStores.LoadSnapshot(snap.CustomKeyStores); err != nil { + return fmt.Errorf("kms: restore custom key stores: %w", err) + } + } + return nil } diff --git a/providers/aws/kms/tag_grant_validation_test.go b/providers/aws/kms/tag_grant_validation_test.go new file mode 100644 index 000000000..deb8926cb --- /dev/null +++ b/providers/aws/kms/tag_grant_validation_test.go @@ -0,0 +1,125 @@ +package kms_test + +import ( + "context" + "errors" + "strings" + "testing" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/services/kms/driver" +) + +// --- reserved aws: tag prefix --- + +func TestReservedAWSTagPrefixRejected(t *testing.T) { + ctx := context.Background() + m := newMock(t) + + _, err := m.CreateKey(ctx, driver.CreateKeyInput{Tags: map[string]string{"aws:cloudformation:stack-name": "x"}}) + wantException(t, err, driver.ExTag) + + k, err := m.CreateKey(ctx, driver.CreateKeyInput{Tags: map[string]string{"team": "a", "awsome": "ok"}}) + if err != nil { + t.Fatalf("CreateKey with normal tags: %v", err) + } + + for _, key := range []string{"aws:foo", "AWS:Foo", "Aws:bar"} { + err := m.TagResource(ctx, k.KeyID, map[string]string{"ok": "1", key: "v"}) + wantException(t, err, driver.ExTag) + } + + tags, err := m.ListResourceTags(ctx, k.KeyID) + if err != nil { + t.Fatal(err) + } + + if len(tags) != 2 || tags["ok"] != "" { + t.Fatalf("rejected TagResource must not partially apply; tags = %v", tags) + } + + if err := m.TagResource(ctx, k.KeyID, map[string]string{"owner": "me"}); err != nil { + t.Fatalf("TagResource normal: %v", err) + } +} + +// --- CreateGrant principal validation --- + +func TestCreateGrantPrincipalValidation(t *testing.T) { + ctx := context.Background() + m := newMock(t) + + k, err := m.CreateKey(ctx, driver.CreateKeyInput{}) + if err != nil { + t.Fatal(err) + } + + ops := []string{driver.GrantOpEncrypt} + + valid := []string{ + "arn:aws:iam::111122223333:root", + "arn:aws:iam::111122223333:user/alice", + "arn:aws:iam::111122223333:role/ExampleRole", + "arn:aws:sts::111122223333:assumed-role/ExampleRole/session", + "arn:aws:sts::111122223333:federated-user/bob", + "arn:aws-us-gov:iam::111122223333:role/r", + "dynamodb.us-east-1.amazonaws.com", + "111122223333", + } + for _, p := range valid { + if _, _, err := m.CreateGrant(ctx, driver.CreateGrantInput{KeyID: k.KeyID, GranteePrincipal: p, Operations: ops}); err != nil { + t.Errorf("GranteePrincipal %q rejected: %v", p, err) + } + } + + for _, p := range []string{ + "not-an-arn", "alice", "arn:aws:s3:::bucket", "arn:aws:iam", "arn:aws:iam:::role/x", + // iam/sts ARNs that aren't grantable principals. + "arn:aws:iam::111122223333:group/G", + "arn:aws:iam::111122223333:policy/P", + "arn:aws:iam::111122223333:instance-profile/I", + "arn:aws:iam::111122223333:user/", + "arn:aws:iam::111122223333:role", + "arn:aws:sts::111122223333:root", + "arn:aws:sts::111122223333:assumed-role/", + // IAM and STS ARNs are global: a Region is not allowed. + "arn:aws:iam:us-east-1:111122223333:role/R", + "arn:aws:sts:us-east-1:111122223333:assumed-role/R/s", + // The account must be a 12-digit ID. + "arn:aws:iam::acct:root", + } { + _, _, err := m.CreateGrant(ctx, driver.CreateGrantInput{KeyID: k.KeyID, GranteePrincipal: p, Operations: ops}) + wantException(t, err, driver.ExInvalidArn) + } + + // Outside the modeled pattern โ†’ ValidationException (no exception tag). + _, _, err = m.CreateGrant(ctx, driver.CreateGrantInput{KeyID: k.KeyID, GranteePrincipal: "has space", Operations: ops}) + if !cerrors.IsInvalidArgument(err) { + t.Fatalf("pattern violation: err = %v", err) + } + + var apiErr *driver.APIError + if errors.As(err, &apiErr) { + t.Fatalf("pattern violation should be a plain validation error, got %s", apiErr.Exception) + } + + const wantMsg = "1 validation error detected: Value 'has space' at 'granteePrincipal' failed to satisfy constraint: " + + "Member must satisfy regular expression pattern: ^[\\w+=,.@:/-]+$" + if msg := cerrors.Message(err); msg != wantMsg { + t.Fatalf("pattern violation message = %q, want %q", msg, wantMsg) + } + + long := "arn:aws:iam::111122223333:role/" + strings.Repeat("r", 256) + + _, _, err = m.CreateGrant(ctx, driver.CreateGrantInput{KeyID: k.KeyID, GranteePrincipal: long, Operations: ops}) + if msg := cerrors.Message(err); !strings.HasSuffix(msg, "at 'granteePrincipal' failed to satisfy constraint: "+ + "Member must have length less than or equal to 256") { + t.Fatalf("length violation message = %q", msg) + } + + // RetiringPrincipal is validated the same way. + _, _, err = m.CreateGrant(ctx, driver.CreateGrantInput{ + KeyID: k.KeyID, GranteePrincipal: valid[0], RetiringPrincipal: "bogus", Operations: ops, + }) + wantException(t, err, driver.ExInvalidArn) +} diff --git a/server/aws/kms/crypto_ops.go b/server/aws/kms/crypto_ops.go index dcdda0f9a..b7d587248 100644 --- a/server/aws/kms/crypto_ops.go +++ b/server/aws/kms/crypto_ops.go @@ -120,6 +120,7 @@ func (h *Handler) generateDataKeyPair(w http.ResponseWriter, r *http.Request) { }) } +//nolint:dupl // templated KMS wire handler; the decode/call/respond shape is intrinsic func (h *Handler) generateDataKeyPairWithoutPlaintext(w http.ResponseWriter, r *http.Request) { dispatch(h, w, r, func(h *Handler, ctx context.Context, req *generateDataKeyPairRequest) (any, error) { out, err := h.kms.GenerateDataKeyPairWithoutPlaintext(ctx, kmsdriver.GenerateDataKeyPairInput{ @@ -140,7 +141,17 @@ func (h *Handler) generateDataKeyPairWithoutPlaintext(w http.ResponseWriter, r * func (h *Handler) generateRandom(w http.ResponseWriter, r *http.Request) { dispatch(h, w, r, func(h *Handler, ctx context.Context, req *generateRandomRequest) (any, error) { - out, err := h.kms.GenerateRandom(ctx, req.NumberOfBytes) + var ( + out []byte + err error + ) + + if req.CustomKeyStoreID != "" { + out, err = h.kms.GenerateRandomInCustomKeyStore(ctx, req.CustomKeyStoreID, req.NumberOfBytes) + } else { + out, err = h.kms.GenerateRandom(ctx, req.NumberOfBytes) + } + if err != nil { return nil, err } @@ -164,6 +175,25 @@ func (h *Handler) getPublicKey(w http.ResponseWriter, r *http.Request) { KeyUsage: out.KeyUsage, EncryptionAlgorithms: out.EncryptionAlgorithms, SigningAlgorithms: out.SigningAlgorithms, + + KeyAgreementAlgorithms: out.KeyAgreementAlgorithms, + }, nil + }) +} + +//nolint:dupl // templated KMS wire handler; the decode/call/respond shape is intrinsic +func (h *Handler) deriveSharedSecret(w http.ResponseWriter, r *http.Request) { + dispatch(h, w, r, func(h *Handler, ctx context.Context, req *deriveSharedSecretRequest) (any, error) { + out, err := h.kms.DeriveSharedSecret(ctx, kmsdriver.DeriveSharedSecretInput{ + KeyID: req.KeyID, KeyAgreementAlgorithm: req.KeyAgreementAlgorithm, PublicKey: req.PublicKey, + }) + if err != nil { + return nil, err + } + + return deriveSharedSecretResponse{ + KeyID: out.KeyID, SharedSecret: out.SharedSecret, + KeyAgreementAlgorithm: out.KeyAgreementAlgorithm, KeyOrigin: out.KeyOrigin, }, nil }) } diff --git a/server/aws/kms/crypto_types.go b/server/aws/kms/crypto_types.go index e48c720b7..42bc9ed56 100644 --- a/server/aws/kms/crypto_types.go +++ b/server/aws/kms/crypto_types.go @@ -75,7 +75,8 @@ type generateDataKeyPairResponse struct { } type generateRandomRequest struct { - NumberOfBytes int32 `json:"NumberOfBytes"` + NumberOfBytes int32 `json:"NumberOfBytes"` + CustomKeyStoreID string `json:"CustomKeyStoreId"` } type generateRandomResponse struct { @@ -94,6 +95,21 @@ type getPublicKeyResponse struct { KeyUsage string `json:"KeyUsage"` EncryptionAlgorithms []string `json:"EncryptionAlgorithms,omitempty"` SigningAlgorithms []string `json:"SigningAlgorithms,omitempty"` + + KeyAgreementAlgorithms []string `json:"KeyAgreementAlgorithms,omitempty"` +} + +type deriveSharedSecretRequest struct { + KeyID string `json:"KeyId"` + KeyAgreementAlgorithm string `json:"KeyAgreementAlgorithm"` + PublicKey []byte `json:"PublicKey"` +} + +type deriveSharedSecretResponse struct { + KeyID string `json:"KeyId"` + SharedSecret []byte `json:"SharedSecret"` + KeyAgreementAlgorithm string `json:"KeyAgreementAlgorithm"` + KeyOrigin string `json:"KeyOrigin"` } type signRequest struct { diff --git a/server/aws/kms/customkeystore_ops.go b/server/aws/kms/customkeystore_ops.go new file mode 100644 index 000000000..2adca2f89 --- /dev/null +++ b/server/aws/kms/customkeystore_ops.go @@ -0,0 +1,206 @@ +package kms + +import ( + "context" + "net/http" + + kmsdriver "github.com/stackshy/cloudemu/v2/services/kms/driver" +) + +// xksCredentialJSON is the XksProxyAuthenticationCredentialType wire shape. +type xksCredentialJSON struct { + AccessKeyID string `json:"AccessKeyId"` + RawSecretAccessKey string `json:"RawSecretAccessKey"` +} + +func (c *xksCredentialJSON) toDriver() *kmsdriver.XksProxyAuthenticationCredential { + if c == nil { + return nil + } + + return &kmsdriver.XksProxyAuthenticationCredential{ + AccessKeyID: c.AccessKeyID, RawSecretAccessKey: c.RawSecretAccessKey, + } +} + +// xksProxyConfigJSON is the XksProxyConfigurationType wire shape (no secret). +type xksProxyConfigJSON struct { + Connectivity string `json:"Connectivity,omitempty"` + AccessKeyID string `json:"AccessKeyId,omitempty"` + URIEndpoint string `json:"UriEndpoint,omitempty"` + URIPath string `json:"UriPath,omitempty"` + VpcEndpointServiceName string `json:"VpcEndpointServiceName,omitempty"` + VpcEndpointServiceOwner string `json:"VpcEndpointServiceOwner,omitempty"` +} + +// customKeyStoreJSON is the CustomKeyStoresListEntry wire shape. The kmsuser +// password and XKS secret are never returned. +type customKeyStoreJSON struct { + CustomKeyStoreID string `json:"CustomKeyStoreId"` + CustomKeyStoreName string `json:"CustomKeyStoreName"` + CustomKeyStoreType string `json:"CustomKeyStoreType"` + CloudHsmClusterID string `json:"CloudHsmClusterId,omitempty"` + TrustAnchorCertificate string `json:"TrustAnchorCertificate,omitempty"` + ConnectionState string `json:"ConnectionState"` + ConnectionErrorCode string `json:"ConnectionErrorCode,omitempty"` + CreationDate *float64 `json:"CreationDate,omitempty"` + XksProxyConfiguration *xksProxyConfigJSON `json:"XksProxyConfiguration,omitempty"` +} + +func customKeyStoreToJSON(s *kmsdriver.CustomKeyStore) customKeyStoreJSON { + out := customKeyStoreJSON{ + CustomKeyStoreID: s.CustomKeyStoreID, + CustomKeyStoreName: s.CustomKeyStoreName, + CustomKeyStoreType: s.CustomKeyStoreType, + CloudHsmClusterID: s.CloudHsmClusterID, + TrustAnchorCertificate: s.TrustAnchorCertificate, + ConnectionState: s.ConnectionState, + ConnectionErrorCode: s.ConnectionErrorCode, + CreationDate: epochOrNil(s.CreationDate), + } + + if c := s.XksProxyConfiguration; c != nil { + out.XksProxyConfiguration = &xksProxyConfigJSON{ + Connectivity: c.Connectivity, + AccessKeyID: c.AccessKeyID, + URIEndpoint: c.URIEndpoint, + URIPath: c.URIPath, + VpcEndpointServiceName: c.VpcEndpointServiceName, + VpcEndpointServiceOwner: c.VpcEndpointServiceOwner, + } + } + + return out +} + +type createCustomKeyStoreRequest struct { + CustomKeyStoreName string `json:"CustomKeyStoreName"` + CloudHsmClusterID string `json:"CloudHsmClusterId"` + TrustAnchorCertificate string `json:"TrustAnchorCertificate"` + KeyStorePassword string `json:"KeyStorePassword"` + CustomKeyStoreType string `json:"CustomKeyStoreType"` + XksProxyURIEndpoint string `json:"XksProxyUriEndpoint"` + XksProxyURIPath string `json:"XksProxyUriPath"` + XksProxyVpcEndpointServiceName string `json:"XksProxyVpcEndpointServiceName"` + XksProxyVpcEndpointServiceOwner string `json:"XksProxyVpcEndpointServiceOwner"` + XksProxyAuthenticationCredential *xksCredentialJSON `json:"XksProxyAuthenticationCredential"` + XksProxyConnectivity string `json:"XksProxyConnectivity"` +} + +type createCustomKeyStoreResponse struct { + CustomKeyStoreID string `json:"CustomKeyStoreId"` +} + +type describeCustomKeyStoresRequest struct { + CustomKeyStoreID string `json:"CustomKeyStoreId"` + CustomKeyStoreName string `json:"CustomKeyStoreName"` + Limit int32 `json:"Limit"` + Marker string `json:"Marker"` +} + +type describeCustomKeyStoresResponse struct { + CustomKeyStores []customKeyStoreJSON `json:"CustomKeyStores"` + NextMarker string `json:"NextMarker,omitempty"` + Truncated bool `json:"Truncated"` +} + +type updateCustomKeyStoreRequest struct { + CustomKeyStoreID string `json:"CustomKeyStoreId"` + NewCustomKeyStoreName string `json:"NewCustomKeyStoreName"` + KeyStorePassword string `json:"KeyStorePassword"` + CloudHsmClusterID string `json:"CloudHsmClusterId"` + XksProxyURIEndpoint string `json:"XksProxyUriEndpoint"` + XksProxyURIPath string `json:"XksProxyUriPath"` + XksProxyVpcEndpointServiceName string `json:"XksProxyVpcEndpointServiceName"` + XksProxyVpcEndpointServiceOwner string `json:"XksProxyVpcEndpointServiceOwner"` + XksProxyAuthenticationCredential *xksCredentialJSON `json:"XksProxyAuthenticationCredential"` + XksProxyConnectivity string `json:"XksProxyConnectivity"` +} + +type customKeyStoreIDRequest struct { + CustomKeyStoreID string `json:"CustomKeyStoreId"` +} + +func (h *Handler) createCustomKeyStore(w http.ResponseWriter, r *http.Request) { + dispatch(h, w, r, func(h *Handler, ctx context.Context, req *createCustomKeyStoreRequest) (any, error) { + id, err := h.kms.CreateCustomKeyStore(ctx, kmsdriver.CreateCustomKeyStoreInput{ + CustomKeyStoreName: req.CustomKeyStoreName, + CustomKeyStoreType: req.CustomKeyStoreType, + CloudHsmClusterID: req.CloudHsmClusterID, + TrustAnchorCertificate: req.TrustAnchorCertificate, + KeyStorePassword: req.KeyStorePassword, + XksProxyURIEndpoint: req.XksProxyURIEndpoint, + XksProxyURIPath: req.XksProxyURIPath, + XksProxyVpcEndpointServiceName: req.XksProxyVpcEndpointServiceName, + XksProxyVpcEndpointServiceOwner: req.XksProxyVpcEndpointServiceOwner, + XksProxyAuthenticationCredential: req.XksProxyAuthenticationCredential.toDriver(), + XksProxyConnectivity: req.XksProxyConnectivity, + }) + if err != nil { + return nil, err + } + + return createCustomKeyStoreResponse{CustomKeyStoreID: id}, nil + }) +} + +func (h *Handler) describeCustomKeyStores(w http.ResponseWriter, r *http.Request) { + dispatch(h, w, r, func(h *Handler, ctx context.Context, req *describeCustomKeyStoresRequest) (any, error) { + stores, err := h.kms.DescribeCustomKeyStores(ctx, req.CustomKeyStoreID, req.CustomKeyStoreName) + if err != nil { + return nil, err + } + + start, end, next, truncated, err := pageWindow(req.Marker, req.Limit, len(stores)) + if err != nil { + return nil, err + } + + entries := make([]customKeyStoreJSON, 0, end-start) + for i := start; i < end; i++ { + entries = append(entries, customKeyStoreToJSON(&stores[i])) + } + + return describeCustomKeyStoresResponse{CustomKeyStores: entries, NextMarker: next, Truncated: truncated}, nil + }) +} + +func (h *Handler) updateCustomKeyStore(w http.ResponseWriter, r *http.Request) { + dispatch(h, w, r, func(h *Handler, ctx context.Context, req *updateCustomKeyStoreRequest) (any, error) { + err := h.kms.UpdateCustomKeyStore(ctx, kmsdriver.UpdateCustomKeyStoreInput{ + CustomKeyStoreID: req.CustomKeyStoreID, + NewCustomKeyStoreName: req.NewCustomKeyStoreName, + KeyStorePassword: req.KeyStorePassword, + CloudHsmClusterID: req.CloudHsmClusterID, + XksProxyURIEndpoint: req.XksProxyURIEndpoint, + XksProxyURIPath: req.XksProxyURIPath, + XksProxyVpcEndpointServiceName: req.XksProxyVpcEndpointServiceName, + XksProxyVpcEndpointServiceOwner: req.XksProxyVpcEndpointServiceOwner, + XksProxyAuthenticationCredential: req.XksProxyAuthenticationCredential.toDriver(), + XksProxyConnectivity: req.XksProxyConnectivity, + }) + if err != nil { + return nil, err + } + + return struct{}{}, nil + }) +} + +func (h *Handler) deleteCustomKeyStore(w http.ResponseWriter, r *http.Request) { + dispatch(h, w, r, func(h *Handler, ctx context.Context, req *customKeyStoreIDRequest) (any, error) { + return struct{}{}, h.kms.DeleteCustomKeyStore(ctx, req.CustomKeyStoreID) + }) +} + +func (h *Handler) connectCustomKeyStore(w http.ResponseWriter, r *http.Request) { + dispatch(h, w, r, func(h *Handler, ctx context.Context, req *customKeyStoreIDRequest) (any, error) { + return struct{}{}, h.kms.ConnectCustomKeyStore(ctx, req.CustomKeyStoreID) + }) +} + +func (h *Handler) disconnectCustomKeyStore(w http.ResponseWriter, r *http.Request) { + dispatch(h, w, r, func(h *Handler, ctx context.Context, req *customKeyStoreIDRequest) (any, error) { + return struct{}{}, h.kms.DisconnectCustomKeyStore(ctx, req.CustomKeyStoreID) + }) +} diff --git a/server/aws/kms/customkeystore_sdk_test.go b/server/aws/kms/customkeystore_sdk_test.go new file mode 100644 index 000000000..3eff9692a --- /dev/null +++ b/server/aws/kms/customkeystore_sdk_test.go @@ -0,0 +1,253 @@ +package kms_test + +import ( + "context" + "errors" + "strings" + "testing" + + "github.com/aws/aws-sdk-go-v2/aws" + awskms "github.com/aws/aws-sdk-go-v2/service/kms" + kmstypes "github.com/aws/aws-sdk-go-v2/service/kms/types" +) + +// TestSDKCustomKeyStoreLifecycle drives the six custom key store operations +// (plus CreateKey in a store) through the real client, including the typed +// state errors. +func TestSDKCustomKeyStoreLifecycle(t *testing.T) { + ctx := context.Background() + c := newKMSClient(t) + + create := func(name, cluster string) (*awskms.CreateCustomKeyStoreOutput, error) { + return c.CreateCustomKeyStore(ctx, &awskms.CreateCustomKeyStoreInput{ + CustomKeyStoreName: aws.String(name), + CloudHsmClusterId: aws.String(cluster), + TrustAnchorCertificate: aws.String("-----BEGIN CERTIFICATE-----\nMIIB\n-----END CERTIFICATE-----"), + KeyStorePassword: aws.String("kmsPswd1"), + }) + } + + created, err := create("hsm", "cluster-abcdefghijk") + if err != nil { + t.Fatalf("CreateCustomKeyStore: %v", err) + } + + id := created.CustomKeyStoreId + + var nameErr *kmstypes.CustomKeyStoreNameInUseException + if _, err := create("hsm", "cluster-zzzzzzzzzzz"); !errors.As(err, &nameErr) { + t.Fatalf("dup name: err = %v", err) + } + + var clusterErr *kmstypes.CloudHsmClusterInUseException + if _, err := create("hsm2", "cluster-abcdefghijk"); !errors.As(err, &clusterErr) { + t.Fatalf("dup cluster: err = %v", err) + } + + desc, err := c.DescribeCustomKeyStores(ctx, &awskms.DescribeCustomKeyStoresInput{CustomKeyStoreId: id}) + if err != nil || len(desc.CustomKeyStores) != 1 { + t.Fatalf("Describe: %v, %v", desc, err) + } + + s := desc.CustomKeyStores[0] + if s.ConnectionState != kmstypes.ConnectionStateTypeDisconnected || + s.CustomKeyStoreType != kmstypes.CustomKeyStoreTypeAwsCloudhsm || aws.ToString(s.CloudHsmClusterId) != "cluster-abcdefghijk" { + t.Fatalf("store = %+v", s) + } + + var stateErr *kmstypes.CustomKeyStoreInvalidStateException + + _, err = c.CreateKey(ctx, &awskms.CreateKeyInput{Origin: kmstypes.OriginTypeAwsCloudhsm, CustomKeyStoreId: id}) + if !errors.As(err, &stateErr) { + t.Fatalf("CreateKey in disconnected store: %v", err) + } + + if _, err := c.DisconnectCustomKeyStore(ctx, &awskms.DisconnectCustomKeyStoreInput{CustomKeyStoreId: id}); !errors.As(err, &stateErr) { + t.Fatalf("disconnect disconnected: %v", err) + } + + if _, err := c.ConnectCustomKeyStore(ctx, &awskms.ConnectCustomKeyStoreInput{CustomKeyStoreId: id}); err != nil { + t.Fatalf("Connect: %v", err) + } + + key, err := c.CreateKey(ctx, &awskms.CreateKeyInput{Origin: kmstypes.OriginTypeAwsCloudhsm, CustomKeyStoreId: id}) + if err != nil { + t.Fatalf("CreateKey in store: %v", err) + } + + if aws.ToString(key.KeyMetadata.CustomKeyStoreId) != aws.ToString(id) || + aws.ToString(key.KeyMetadata.CloudHsmClusterId) != "cluster-abcdefghijk" { + t.Fatalf("key metadata = %+v", key.KeyMetadata) + } + + if _, err := c.UpdateCustomKeyStore(ctx, &awskms.UpdateCustomKeyStoreInput{ + CustomKeyStoreId: id, NewCustomKeyStoreName: aws.String("renamed"), + }); err != nil { + t.Fatalf("rename: %v", err) + } + + if _, err := c.DeleteCustomKeyStore(ctx, &awskms.DeleteCustomKeyStoreInput{CustomKeyStoreId: id}); !errors.As(err, &stateErr) { + t.Fatalf("delete connected: %v", err) + } + + if _, err := c.DisconnectCustomKeyStore(ctx, &awskms.DisconnectCustomKeyStoreInput{CustomKeyStoreId: id}); err != nil { + t.Fatalf("Disconnect: %v", err) + } + + var hasKeys *kmstypes.CustomKeyStoreHasCMKsException + if _, err := c.DeleteCustomKeyStore(ctx, &awskms.DeleteCustomKeyStoreInput{CustomKeyStoreId: id}); !errors.As(err, &hasKeys) { + t.Fatalf("delete with keys: %v", err) + } + + var notFound *kmstypes.CustomKeyStoreNotFoundException + if _, err := c.DescribeCustomKeyStores(ctx, &awskms.DescribeCustomKeyStoresInput{ + CustomKeyStoreId: aws.String("cks-00000000000000000"), + }); !errors.As(err, ¬Found) { + t.Fatalf("describe missing: %v", err) + } + + byName, err := c.DescribeCustomKeyStores(ctx, &awskms.DescribeCustomKeyStoresInput{CustomKeyStoreName: aws.String("renamed")}) + if err != nil || len(byName.CustomKeyStores) != 1 { + t.Fatalf("describe by new name: %v, %v", byName, err) + } +} + +// TestSDKExternalKeyStore checks the XKS proxy configuration round-trips (without +// the secret) and the proxy URI uniqueness error. +func TestSDKExternalKeyStore(t *testing.T) { + ctx := context.Background() + c := newKMSClient(t) + + in := func(name string) *awskms.CreateCustomKeyStoreInput { + return &awskms.CreateCustomKeyStoreInput{ + CustomKeyStoreName: aws.String(name), + CustomKeyStoreType: kmstypes.CustomKeyStoreTypeExternalKeyStore, + XksProxyUriEndpoint: aws.String("https://xks.example.com"), + XksProxyUriPath: aws.String("/kms/xks/v1"), + XksProxyConnectivity: kmstypes.XksProxyConnectivityTypePublicEndpoint, + XksProxyAuthenticationCredential: &kmstypes.XksProxyAuthenticationCredentialType{ + AccessKeyId: aws.String("ABCDEFGHIJKLMNOPQRST"), + RawSecretAccessKey: aws.String("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQ="), + }, + } + } + + created, err := c.CreateCustomKeyStore(ctx, in("xks")) + if err != nil { + t.Fatalf("CreateCustomKeyStore: %v", err) + } + + var uriErr *kmstypes.XksProxyUriInUseException + if _, err := c.CreateCustomKeyStore(ctx, in("xks2")); !errors.As(err, &uriErr) { + t.Fatalf("dup uri: %v", err) + } + + desc, err := c.DescribeCustomKeyStores(ctx, &awskms.DescribeCustomKeyStoresInput{CustomKeyStoreId: created.CustomKeyStoreId}) + if err != nil { + t.Fatal(err) + } + + cfg := desc.CustomKeyStores[0].XksProxyConfiguration + if cfg == nil || aws.ToString(cfg.UriEndpoint) != "https://xks.example.com" || + aws.ToString(cfg.AccessKeyId) != "ABCDEFGHIJKLMNOPQRST" || cfg.Connectivity != kmstypes.XksProxyConnectivityTypePublicEndpoint { + t.Fatalf("XksProxyConfiguration = %+v", cfg) + } + + if _, err := c.ConnectCustomKeyStore(ctx, &awskms.ConnectCustomKeyStoreInput{CustomKeyStoreId: created.CustomKeyStoreId}); err != nil { + t.Fatal(err) + } + + key, err := c.CreateKey(ctx, &awskms.CreateKeyInput{ + Origin: kmstypes.OriginTypeExternalKeyStore, CustomKeyStoreId: created.CustomKeyStoreId, XksKeyId: aws.String("ext-1"), + }) + if err != nil { + t.Fatalf("CreateKey XKS: %v", err) + } + + if key.KeyMetadata.XksKeyConfiguration == nil || aws.ToString(key.KeyMetadata.XksKeyConfiguration.Id) != "ext-1" { + t.Fatalf("XksKeyConfiguration = %+v", key.KeyMetadata.XksKeyConfiguration) + } + + var inUse *kmstypes.XksKeyAlreadyInUseException + if _, err := c.CreateKey(ctx, &awskms.CreateKeyInput{ + Origin: kmstypes.OriginTypeExternalKeyStore, CustomKeyStoreId: created.CustomKeyStoreId, XksKeyId: aws.String("ext-1"), + }); !errors.As(err, &inUse) { + t.Fatalf("dup xks key: %v", err) + } +} + +// TestSDKCustomKeyStoreKeyUnavailable: after DisconnectCustomKeyStore a real +// client sees the store's key as Unavailable, crypto fails with +// KMSInvalidStateException (also for a Disabled key), GenerateRandom with the +// store's ID fails with CustomKeyStoreInvalidStateException, and a +// multi-Region key in a store fails with UnsupportedOperationException. +func TestSDKCustomKeyStoreKeyUnavailable(t *testing.T) { + ctx := context.Background() + c := newKMSClient(t) + + created, err := c.CreateCustomKeyStore(ctx, &awskms.CreateCustomKeyStoreInput{ + CustomKeyStoreName: aws.String("hsm"), + CloudHsmClusterId: aws.String("cluster-abcdefghijk"), + TrustAnchorCertificate: aws.String("-----BEGIN CERTIFICATE-----\nMIIB\n-----END CERTIFICATE-----"), + KeyStorePassword: aws.String("kmsPswd1"), + }) + if err != nil { + t.Fatal(err) + } + + id := created.CustomKeyStoreId + + var stateErr *kmstypes.CustomKeyStoreInvalidStateException + if _, err := c.GenerateRandom(ctx, &awskms.GenerateRandomInput{ + NumberOfBytes: aws.Int32(8), CustomKeyStoreId: id, + }); !errors.As(err, &stateErr) { + t.Fatalf("GenerateRandom on disconnected store: %v", err) + } + + if _, err := c.ConnectCustomKeyStore(ctx, &awskms.ConnectCustomKeyStoreInput{CustomKeyStoreId: id}); err != nil { + t.Fatal(err) + } + + rnd, err := c.GenerateRandom(ctx, &awskms.GenerateRandomInput{NumberOfBytes: aws.Int32(8), CustomKeyStoreId: id}) + if err != nil || len(rnd.Plaintext) != 8 { + t.Fatalf("GenerateRandom on connected store = %v, %v", rnd, err) + } + + var unsupported *kmstypes.UnsupportedOperationException + if _, err := c.CreateKey(ctx, &awskms.CreateKeyInput{ + Origin: kmstypes.OriginTypeAwsCloudhsm, CustomKeyStoreId: id, MultiRegion: aws.Bool(true), + }); !errors.As(err, &unsupported) || !strings.Contains(unsupported.ErrorMessage(), "single-Region symmetric") { + t.Fatalf("multi-Region key in store: %v", err) + } + + key, err := c.CreateKey(ctx, &awskms.CreateKeyInput{Origin: kmstypes.OriginTypeAwsCloudhsm, CustomKeyStoreId: id}) + if err != nil { + t.Fatal(err) + } + + keyID := key.KeyMetadata.KeyId + + if _, err := c.DisableKey(ctx, &awskms.DisableKeyInput{KeyId: keyID}); err != nil { + t.Fatal(err) + } + + if _, err := c.DisconnectCustomKeyStore(ctx, &awskms.DisconnectCustomKeyStoreInput{CustomKeyStoreId: id}); err != nil { + t.Fatal(err) + } + + desc, err := c.DescribeKey(ctx, &awskms.DescribeKeyInput{KeyId: keyID}) + if err != nil { + t.Fatal(err) + } + + if desc.KeyMetadata.KeyState != kmstypes.KeyStateUnavailable || desc.KeyMetadata.Enabled { + t.Fatalf("DescribeKey state = %s enabled=%v, want Unavailable", desc.KeyMetadata.KeyState, desc.KeyMetadata.Enabled) + } + + var invalidState *kmstypes.KMSInvalidStateException + + _, err = c.Encrypt(ctx, &awskms.EncryptInput{KeyId: keyID, Plaintext: []byte("hi")}) + if !errors.As(err, &invalidState) || !strings.HasSuffix(invalidState.ErrorMessage(), "is unavailable") { + t.Fatalf("Encrypt on unavailable key: %v", err) + } +} diff --git a/server/aws/kms/derive_shared_secret_sdk_test.go b/server/aws/kms/derive_shared_secret_sdk_test.go new file mode 100644 index 000000000..d1f53f7e1 --- /dev/null +++ b/server/aws/kms/derive_shared_secret_sdk_test.go @@ -0,0 +1,107 @@ +package kms_test + +import ( + "bytes" + "context" + "crypto/ecdsa" + "crypto/elliptic" + "crypto/rand" + "crypto/x509" + "errors" + "testing" + + awskms "github.com/aws/aws-sdk-go-v2/service/kms" + kmstypes "github.com/aws/aws-sdk-go-v2/service/kms/types" +) + +// TestSDKDeriveSharedSecret: a real client creates a KEY_AGREEMENT key, derives +// a shared secret with a locally generated peer key, and checks it equals the +// secret the peer computes from the key's downloaded public key. +func TestSDKDeriveSharedSecret(t *testing.T) { + ctx := context.Background() + c := newKMSClient(t) + + key, err := c.CreateKey(ctx, &awskms.CreateKeyInput{ + KeySpec: kmstypes.KeySpecEccNistP256, KeyUsage: kmstypes.KeyUsageTypeKeyAgreement, + }) + if err != nil { + t.Fatalf("CreateKey: %v", err) + } + + md := key.KeyMetadata + if md.KeyUsage != kmstypes.KeyUsageTypeKeyAgreement || + len(md.KeyAgreementAlgorithms) != 1 || md.KeyAgreementAlgorithms[0] != kmstypes.KeyAgreementAlgorithmSpecEcdh { + t.Fatalf("metadata = usage %s, KeyAgreementAlgorithms %v", md.KeyUsage, md.KeyAgreementAlgorithms) + } + + peer, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + if err != nil { + t.Fatal(err) + } + + peerDER, err := x509.MarshalPKIXPublicKey(&peer.PublicKey) + if err != nil { + t.Fatal(err) + } + + out, err := c.DeriveSharedSecret(ctx, &awskms.DeriveSharedSecretInput{ + KeyId: md.KeyId, KeyAgreementAlgorithm: kmstypes.KeyAgreementAlgorithmSpecEcdh, PublicKey: peerDER, + }) + if err != nil { + t.Fatalf("DeriveSharedSecret: %v", err) + } + + if out.KeyAgreementAlgorithm != kmstypes.KeyAgreementAlgorithmSpecEcdh || out.KeyOrigin != kmstypes.OriginTypeAwsKms { + t.Fatalf("response = %+v", out) + } + + pub, err := c.GetPublicKey(ctx, &awskms.GetPublicKeyInput{KeyId: md.KeyId}) + if err != nil { + t.Fatalf("GetPublicKey: %v", err) + } + + if len(pub.KeyAgreementAlgorithms) != 1 { + t.Fatalf("GetPublicKey KeyAgreementAlgorithms = %v", pub.KeyAgreementAlgorithms) + } + + parsed, err := x509.ParsePKIXPublicKey(pub.PublicKey) + if err != nil { + t.Fatal(err) + } + + kmsPub, err := parsed.(*ecdsa.PublicKey).ECDH() + if err != nil { + t.Fatal(err) + } + + peerECDH, err := peer.ECDH() + if err != nil { + t.Fatal(err) + } + + want, err := peerECDH.ECDH(kmsPub) + if err != nil { + t.Fatal(err) + } + + if !bytes.Equal(out.SharedSecret, want) { + t.Fatalf("shared secret mismatch:\n kms %x\n local %x", out.SharedSecret, want) + } + + // Wrong key usage โ†’ InvalidKeyUsageException. + signKey, err := c.CreateKey(ctx, &awskms.CreateKeyInput{ + KeySpec: kmstypes.KeySpecEccNistP256, KeyUsage: kmstypes.KeyUsageTypeSignVerify, + }) + if err != nil { + t.Fatal(err) + } + + _, err = c.DeriveSharedSecret(ctx, &awskms.DeriveSharedSecretInput{ + KeyId: signKey.KeyMetadata.KeyId, KeyAgreementAlgorithm: kmstypes.KeyAgreementAlgorithmSpecEcdh, PublicKey: peerDER, + }) + + var usageErr *kmstypes.InvalidKeyUsageException + if !errors.As(err, &usageErr) { + t.Fatalf("sign key: err = %v, want InvalidKeyUsageException", err) + } +} diff --git a/server/aws/kms/handler.go b/server/aws/kms/handler.go index 2ebacf7e7..3a4f538d6 100644 --- a/server/aws/kms/handler.go +++ b/server/aws/kms/handler.go @@ -59,6 +59,7 @@ func New(k kmsdriver.KMS) *Handler { "Verify": h.verify, "GenerateMac": h.generateMac, "VerifyMac": h.verifyMac, + "DeriveSharedSecret": h.deriveSharedSecret, "CreateGrant": h.createGrant, "ListGrants": h.listGrants, @@ -79,6 +80,13 @@ func New(k kmsdriver.KMS) *Handler { "DeleteImportedKeyMaterial": h.deleteImportedKeyMaterial, "ReplicateKey": h.replicateKey, "UpdatePrimaryRegion": h.updatePrimaryRegion, + + "CreateCustomKeyStore": h.createCustomKeyStore, + "DescribeCustomKeyStores": h.describeCustomKeyStores, + "UpdateCustomKeyStore": h.updateCustomKeyStore, + "DeleteCustomKeyStore": h.deleteCustomKeyStore, + "ConnectCustomKeyStore": h.connectCustomKeyStore, + "DisconnectCustomKeyStore": h.disconnectCustomKeyStore, } return h @@ -158,6 +166,18 @@ func sentinelException(err error) string { func writeErr(w http.ResponseWriter, err error) { msg := cerrors.Message(err) + var apiErr *kmsdriver.APIError + if errors.As(err, &apiErr) { + status := http.StatusBadRequest + if cerrors.GetCode(err) == cerrors.Internal { + status = http.StatusInternalServerError + } + + wire.WriteJSONError(w, status, apiErr.Exception, msg) + + return + } + if exc := sentinelException(err); exc != "" { wire.WriteJSONError(w, http.StatusBadRequest, exc, msg) diff --git a/server/aws/kms/operations.go b/server/aws/kms/operations.go index 5d3fc056d..c7ad38656 100644 --- a/server/aws/kms/operations.go +++ b/server/aws/kms/operations.go @@ -27,6 +27,9 @@ func (h *Handler) createKey(w http.ResponseWriter, r *http.Request) { MultiRegion: req.MultiRegion, Policy: req.Policy, Tags: tagsToMap(req.Tags), + + CustomKeyStoreID: req.CustomKeyStoreID, + XksKeyID: req.XksKeyID, }) if err != nil { writeErr(w, err) diff --git a/server/aws/kms/tag_grant_validation_sdk_test.go b/server/aws/kms/tag_grant_validation_sdk_test.go new file mode 100644 index 000000000..4114ad711 --- /dev/null +++ b/server/aws/kms/tag_grant_validation_sdk_test.go @@ -0,0 +1,84 @@ +package kms_test + +import ( + "context" + "errors" + "testing" + + "github.com/aws/aws-sdk-go-v2/aws" + awskms "github.com/aws/aws-sdk-go-v2/service/kms" + kmstypes "github.com/aws/aws-sdk-go-v2/service/kms/types" +) + +// TestSDKReservedTagPrefix: tags in the aws: namespace fail with TagException +// on both CreateKey and TagResource. +func TestSDKReservedTagPrefix(t *testing.T) { + ctx := context.Background() + c := newKMSClient(t) + + _, err := c.CreateKey(ctx, &awskms.CreateKeyInput{ + Tags: []kmstypes.Tag{{TagKey: aws.String("aws:owner"), TagValue: aws.String("x")}}, + }) + + var tagErr *kmstypes.TagException + if !errors.As(err, &tagErr) { + t.Fatalf("CreateKey: err = %v, want TagException", err) + } + + key, err := c.CreateKey(ctx, &awskms.CreateKeyInput{}) + if err != nil { + t.Fatal(err) + } + + _, err = c.TagResource(ctx, &awskms.TagResourceInput{ + KeyId: key.KeyMetadata.KeyId, + Tags: []kmstypes.Tag{{TagKey: aws.String("aws:owner"), TagValue: aws.String("x")}}, + }) + if !errors.As(err, &tagErr) { + t.Fatalf("TagResource: err = %v, want TagException", err) + } +} + +// TestSDKCreateGrantInvalidPrincipal: a non-ARN grantee, or an IAM ARN that is +// not an account, user or role, fails with InvalidArnException, while a role +// ARN succeeds. +func TestSDKCreateGrantInvalidPrincipal(t *testing.T) { + ctx := context.Background() + c := newKMSClient(t) + + key, err := c.CreateKey(ctx, &awskms.CreateKeyInput{}) + if err != nil { + t.Fatal(err) + } + + _, err = c.CreateGrant(ctx, &awskms.CreateGrantInput{ + KeyId: key.KeyMetadata.KeyId, GranteePrincipal: aws.String("not-an-arn"), + Operations: []kmstypes.GrantOperation{kmstypes.GrantOperationEncrypt}, + }) + + var arnErr *kmstypes.InvalidArnException + if !errors.As(err, &arnErr) { + t.Fatalf("err = %v, want InvalidArnException", err) + } + + for _, p := range []string{ + "arn:aws:iam::111122223333:group/G", + "arn:aws:iam::111122223333:policy/P", + "arn:aws:iam:us-east-1:111122223333:role/R", + } { + _, err = c.CreateGrant(ctx, &awskms.CreateGrantInput{ + KeyId: key.KeyMetadata.KeyId, GranteePrincipal: aws.String(p), + Operations: []kmstypes.GrantOperation{kmstypes.GrantOperationEncrypt}, + }) + if !errors.As(err, &arnErr) { + t.Fatalf("%s: err = %v, want InvalidArnException", p, err) + } + } + + if _, err := c.CreateGrant(ctx, &awskms.CreateGrantInput{ + KeyId: key.KeyMetadata.KeyId, GranteePrincipal: aws.String("arn:aws:iam::111122223333:role/app"), + Operations: []kmstypes.GrantOperation{kmstypes.GrantOperationEncrypt}, + }); err != nil { + t.Fatalf("valid role ARN: %v", err) + } +} diff --git a/server/aws/kms/types.go b/server/aws/kms/types.go index b00db7f92..d549b7c91 100644 --- a/server/aws/kms/types.go +++ b/server/aws/kms/types.go @@ -75,7 +75,16 @@ type keyMetadataJSON struct { EncryptionAlgorithms []string `json:"EncryptionAlgorithms,omitempty"` SigningAlgorithms []string `json:"SigningAlgorithms,omitempty"` MacAlgorithms []string `json:"MacAlgorithms,omitempty"` + KeyAgreementAlgorithms []string `json:"KeyAgreementAlgorithms,omitempty"` MultiRegionConfiguration *multiRegionConfigJSON `json:"MultiRegionConfiguration,omitempty"` + CustomKeyStoreID string `json:"CustomKeyStoreId,omitempty"` + CloudHsmClusterID string `json:"CloudHsmClusterId,omitempty"` + XksKeyConfiguration *xksKeyConfigJSON `json:"XksKeyConfiguration,omitempty"` +} + +// xksKeyConfigJSON is the KMS XksKeyConfigurationType wire shape. +type xksKeyConfigJSON struct { + ID string `json:"Id"` } func metadataJSON(md *kmsdriver.KeyMetadata) keyMetadataJSON { @@ -105,6 +114,14 @@ func metadataJSON(md *kmsdriver.KeyMetadata) keyMetadataJSON { out.EncryptionAlgorithms = md.EncryptionAlgorithms out.SigningAlgorithms = md.SigningAlgorithms out.MacAlgorithms = md.MacAlgorithms + out.KeyAgreementAlgorithms = md.KeyAgreementAlgorithms + + out.CustomKeyStoreID = md.CustomKeyStoreID + out.CloudHsmClusterID = md.CloudHsmClusterID + + if md.XksKeyID != "" { + out.XksKeyConfiguration = &xksKeyConfigJSON{ID: md.XksKeyID} + } if md.MultiRegion { cfg := &multiRegionConfigJSON{MultiRegionKeyType: md.MultiRegionKeyType} @@ -133,6 +150,8 @@ type createKeyRequest struct { MultiRegion bool `json:"MultiRegion"` Policy string `json:"Policy"` Tags []tag `json:"Tags"` + CustomKeyStoreID string `json:"CustomKeyStoreId"` + XksKeyID string `json:"XksKeyId"` } type keyIDRequest struct { diff --git a/services/kms/driver/crypto.go b/services/kms/driver/crypto.go index fa097c6f3..ed2e2aba2 100644 --- a/services/kms/driver/crypto.go +++ b/services/kms/driver/crypto.go @@ -123,6 +123,27 @@ func MacAlgorithmsFor(spec, usage string) []string { } } +// Key agreement algorithms. +const ( + KeyAgreementECDH = "ECDH" +) + +// KeyAgreementAlgorithmsFor returns the key agreement algorithms KMS advertises +// for a KEY_AGREEMENT key of the given spec (empty for any other usage/spec). +// Only the NIST-standard ECC curves support key agreement. +func KeyAgreementAlgorithmsFor(spec, usage string) []string { + if usage != UsageKeyAgreement { + return nil + } + + switch spec { + case SpecECCNISTP256, SpecECCNISTP384, SpecECCNISTP521: + return []string{KeyAgreementECDH} + default: + return nil + } +} + // Message types for Sign/Verify. const ( MessageTypeRaw = "RAW" @@ -297,12 +318,30 @@ type GetPublicKeyInput struct { // GetPublicKeyOutput carries the DER-encoded (SPKI, RFC 5280) public key of an // asymmetric key plus the spec/usage and the algorithms the key supports. type GetPublicKeyOutput struct { - KeyID string - PublicKey []byte - KeySpec string - KeyUsage string - EncryptionAlgorithms []string - SigningAlgorithms []string + KeyID string + PublicKey []byte + KeySpec string + KeyUsage string + EncryptionAlgorithms []string + SigningAlgorithms []string + KeyAgreementAlgorithms []string +} + +// DeriveSharedSecretInput describes a DeriveSharedSecret request. PublicKey is +// the peer's DER-encoded SubjectPublicKeyInfo on the same curve as the key. +type DeriveSharedSecretInput struct { + KeyID string + KeyAgreementAlgorithm string + PublicKey []byte +} + +// DeriveSharedSecretOutput carries the raw ECDH shared secret (the +// x-coordinate of the shared point, as NIST SP 800-56A defines it). +type DeriveSharedSecretOutput struct { + KeyID string + SharedSecret []byte + KeyAgreementAlgorithm string + KeyOrigin string } // Crypto is the cryptographic surface of KMS. It is embedded in the KMS @@ -321,4 +360,5 @@ type Crypto interface { Verify(ctx context.Context, in VerifyInput) (*VerifyOutput, error) GenerateMac(ctx context.Context, in GenerateMacInput) (*GenerateMacOutput, error) VerifyMac(ctx context.Context, in VerifyMacInput) (*VerifyMacOutput, error) + DeriveSharedSecret(ctx context.Context, in DeriveSharedSecretInput) (*DeriveSharedSecretOutput, error) } diff --git a/services/kms/driver/customkeystore.go b/services/kms/driver/customkeystore.go new file mode 100644 index 000000000..b1d527ca9 --- /dev/null +++ b/services/kms/driver/customkeystore.go @@ -0,0 +1,109 @@ +package driver + +import ( + "context" + "time" +) + +// Custom key store types. +const ( + CustomKeyStoreTypeCloudHSM = "AWS_CLOUDHSM" + CustomKeyStoreTypeExternal = "EXTERNAL_KEY_STORE" +) + +// Custom key store connection states. +const ( + ConnectionStateConnected = "CONNECTED" + ConnectionStateConnecting = "CONNECTING" + ConnectionStateFailed = "FAILED" + ConnectionStateDisconnected = "DISCONNECTED" + ConnectionStateDisconnecting = "DISCONNECTING" +) + +// External key store proxy connectivity values. +const ( + XksConnectivityPublicEndpoint = "PUBLIC_ENDPOINT" + XksConnectivityVpcEndpointService = "VPC_ENDPOINT_SERVICE" +) + +// XksProxyAuthenticationCredential is the credential KMS uses to sign requests +// to an external key store proxy. RawSecretAccessKey is write-only: it is never +// returned by DescribeCustomKeyStores. +type XksProxyAuthenticationCredential struct { + AccessKeyID string + RawSecretAccessKey string +} + +// XksProxyConfiguration is the external key store proxy configuration of an +// EXTERNAL_KEY_STORE custom key store. +type XksProxyConfiguration struct { + Connectivity string + AccessKeyID string + URIEndpoint string + URIPath string + VpcEndpointServiceName string + VpcEndpointServiceOwner string +} + +// CustomKeyStore describes a custom key store, mirroring +// CustomKeyStoresListEntry. KeyStorePassword and the XKS secret are kept for +// the backend's bookkeeping but are never part of a Describe response. +type CustomKeyStore struct { + CustomKeyStoreID string + CustomKeyStoreName string + CustomKeyStoreType string + CloudHsmClusterID string + TrustAnchorCertificate string + KeyStorePassword string + ConnectionState string + ConnectionErrorCode string + CreationDate time.Time + XksProxyConfiguration *XksProxyConfiguration + XksRawSecretAccessKey string +} + +// CreateCustomKeyStoreInput describes a CreateCustomKeyStore request. +type CreateCustomKeyStoreInput struct { + CustomKeyStoreName string + CustomKeyStoreType string // defaults to AWS_CLOUDHSM + CloudHsmClusterID string + TrustAnchorCertificate string + KeyStorePassword string + XksProxyURIEndpoint string + XksProxyURIPath string + XksProxyVpcEndpointServiceName string + XksProxyVpcEndpointServiceOwner string + XksProxyAuthenticationCredential *XksProxyAuthenticationCredential + XksProxyConnectivity string +} + +// UpdateCustomKeyStoreInput describes an UpdateCustomKeyStore request. Empty +// fields (nil credential) are left unchanged. +type UpdateCustomKeyStoreInput struct { + CustomKeyStoreID string + NewCustomKeyStoreName string + KeyStorePassword string + CloudHsmClusterID string + XksProxyURIEndpoint string + XksProxyURIPath string + XksProxyVpcEndpointServiceName string + XksProxyVpcEndpointServiceOwner string + XksProxyAuthenticationCredential *XksProxyAuthenticationCredential + XksProxyConnectivity string +} + +// CustomKeyStores is the custom key store surface of KMS. +type CustomKeyStores interface { + CreateCustomKeyStore(ctx context.Context, in CreateCustomKeyStoreInput) (string, error) + // DescribeCustomKeyStores returns every store, or only the one matching id + // or name (at most one of which may be set). + DescribeCustomKeyStores(ctx context.Context, id, name string) ([]CustomKeyStore, error) + UpdateCustomKeyStore(ctx context.Context, in UpdateCustomKeyStoreInput) error + DeleteCustomKeyStore(ctx context.Context, id string) error + ConnectCustomKeyStore(ctx context.Context, id string) error + DisconnectCustomKeyStore(ctx context.Context, id string) error + // GenerateRandomInCustomKeyStore serves GenerateRandom with a CustomKeyStoreId. + // The bytes come from the store's CloudHSM cluster, so the store must + // exist, be a CloudHSM store, and be CONNECTED. + GenerateRandomInCustomKeyStore(ctx context.Context, id string, numberOfBytes int32) ([]byte, error) +} diff --git a/services/kms/driver/driver.go b/services/kms/driver/driver.go index 43af12dd7..9d446e383 100644 --- a/services/kms/driver/driver.go +++ b/services/kms/driver/driver.go @@ -15,6 +15,8 @@ const ( UsageEncryptDecrypt = "ENCRYPT_DECRYPT" UsageSignVerify = "SIGN_VERIFY" UsageGenerateVerifyMac = "GENERATE_VERIFY_MAC" + // UsageKeyAgreement marks an NIST ECC key pair used with DeriveSharedSecret. + UsageKeyAgreement = "KEY_AGREEMENT" ) // Key spec values (the algorithm/parameters of the key material). @@ -38,12 +40,17 @@ const ( StateDisabled = "Disabled" StatePendingDeletion = "PendingDeletion" StatePendingImport = "PendingImport" + // StateUnavailable is reported for a key in a custom key store while the + // store is not connected. It is derived when the key is read, never stored. + StateUnavailable = "Unavailable" ) // Key origin values. const ( - OriginAWSKMS = "AWS_KMS" - OriginExternal = "EXTERNAL" + OriginAWSKMS = "AWS_KMS" + OriginExternal = "EXTERNAL" + OriginAWSCloudHSM = "AWS_CLOUDHSM" + OriginExternalKeyStore = "EXTERNAL_KEY_STORE" ) // Key manager values. @@ -74,10 +81,18 @@ type KeyMetadata struct { // Algorithm lists advertised for the key, populated from KeySpec+KeyUsage. // Only the one matching the usage is set: EncryptionAlgorithms for // ENCRYPT_DECRYPT, SigningAlgorithms for SIGN_VERIFY, MacAlgorithms for - // GENERATE_VERIFY_MAC. - EncryptionAlgorithms []string - SigningAlgorithms []string - MacAlgorithms []string + // GENERATE_VERIFY_MAC, KeyAgreementAlgorithms for KEY_AGREEMENT. + EncryptionAlgorithms []string + SigningAlgorithms []string + MacAlgorithms []string + KeyAgreementAlgorithms []string + + // Custom key store placement (set only for keys created in a custom key + // store). CloudHsmClusterID is set for AWS_CLOUDHSM stores and XksKeyID for + // EXTERNAL_KEY_STORE stores. + CustomKeyStoreID string + CloudHsmClusterID string + XksKeyID string // Multi-region configuration (populated only when MultiRegion is true). // MultiRegionKeyType is "PRIMARY" or "REPLICA". @@ -95,6 +110,10 @@ type CreateKeyInput struct { MultiRegion bool Policy string Tags map[string]string + // CustomKeyStoreID places the key in a (CONNECTED) custom key store. + CustomKeyStoreID string + // XksKeyID names the external key backing an EXTERNAL_KEY_STORE key. + XksKeyID string } // Alias is a friendly name pointing at a key. @@ -113,6 +132,7 @@ type KMS interface { Crypto Management KeyImport + CustomKeyStores // Key lifecycle. CreateKey(ctx context.Context, in CreateKeyInput) (*KeyMetadata, error) diff --git a/services/kms/driver/errors.go b/services/kms/driver/errors.go new file mode 100644 index 000000000..867d8c5fa --- /dev/null +++ b/services/kms/driver/errors.go @@ -0,0 +1,61 @@ +package driver + +import ( + stderrors "errors" + + "github.com/stackshy/cloudemu/v2/errors" +) + +// KMS exception names that don't map one-to-one onto a canonical cloudemu +// error code. Errors carrying one of these (via APIError) reach the client as +// that exact exception, so an SDK caller can errors.As the typed exception. +const ( + ExTag = "TagException" + ExInvalidArn = "InvalidArnException" + ExCustomKeyStoreNotFound = "CustomKeyStoreNotFoundException" + ExCustomKeyStoreNameInUse = "CustomKeyStoreNameInUseException" + ExCustomKeyStoreInvalidState = "CustomKeyStoreInvalidStateException" + ExCustomKeyStoreHasCMKs = "CustomKeyStoreHasCMKsException" + ExCloudHsmClusterInUse = "CloudHsmClusterInUseException" + ExXksProxyURIInUse = "XksProxyUriInUseException" + ExXksProxyURIEndpointInUse = "XksProxyUriEndpointInUseException" + ExXksProxyVpcEndpointServiceInUse = "XksProxyVpcEndpointServiceInUseException" + ExXksKeyAlreadyInUse = "XksKeyAlreadyInUseException" + ExUnsupportedOperation = "UnsupportedOperationException" + ExKMSInvalidState = "KMSInvalidStateException" +) + +// APIError tags a canonical cloudemu error with the KMS exception name it +// concerns, so the wire layer can emit the exact __type while GetCode/Message +// still resolve through Unwrap. +type APIError struct { + Exception string + Err error +} + +func (e *APIError) Error() string { return e.Err.Error() } +func (e *APIError) Unwrap() error { return e.Err } + +// NewAPIError builds an APIError for exception with a canonical code and a +// formatted message. +func NewAPIError(exception string, code errors.Code, format string, args ...any) error { + return &APIError{Exception: exception, Err: errors.Newf(code, format, args...)} +} + +// sentinelMessageError is a sentinel error with a request-specific message. +type sentinelMessageError struct { + err *errors.Error + sentinel error +} + +func (e *sentinelMessageError) Error() string { return e.err.Error() } +func (e *sentinelMessageError) Unwrap() error { return e.err } +func (e *sentinelMessageError) Is(target error) bool { return stderrors.Is(target, e.sentinel) } + +// WithMessage returns an error that matches errors.Is(err, sentinel) and keeps +// the sentinel's code, but carries its own message, so the client sees the +// exact reason (for example " is unavailable") under the sentinel's +// exception. +func WithMessage(sentinel *errors.Error, format string, args ...any) error { + return &sentinelMessageError{err: errors.Newf(sentinel.Code, format, args...), sentinel: sentinel} +} diff --git a/services/kms/driver/management.go b/services/kms/driver/management.go index 46a302f48..e205b2c93 100644 --- a/services/kms/driver/management.go +++ b/services/kms/driver/management.go @@ -21,6 +21,7 @@ const ( GrantOpDescribeKey = "DescribeKey" GrantOpGenerateMac = "GenerateMac" GrantOpVerifyMac = "VerifyMac" + GrantOpDeriveSharedSecret = "DeriveSharedSecret" ) // Rotation types. From 4e3fbceecc8c395f64800434d87d964c0ff80b46 Mon Sep 17 00:00:00 2001 From: Satyam-Trivedi-ZS Date: Fri, 23 Oct 2026 09:51:01 +0530 Subject: [PATCH 24/31] fix(aws-efs): protection API, async lifecycle, scheduled metrics, LONG_ID default (#1649) * fix(aws-efs): protection API, async lifecycle, scheduled metrics, LONG_ID default Close the open real-cloud parity gaps in #1519: - Add UpdateFileSystemProtection (PUT /2015-02-01/file-systems/{id}/protection) with the ENABLED | DISABLED | REPLICATING enum, BadRequest for other values, IncorrectFileSystemLifeCycleState when the file system is not available, and ReplicationAlreadyExists for a replication source or destination. DescribeFileSystems reflects the new setting. - Model the creating/deleting lifecycle of file systems and mount targets under AsyncSettle: create returns "creating", delete returns while "deleting", and the resource is gone (FileSystemNotFound / MountTargetNotFound) once the window elapses. Operations the EFS model marks with IncorrectFileSystemLifeCycleState are rejected until the file system is available. The default (AsyncSettle off) stays synchronous. The windows are persisted in snapshots. - Publish AWS/EFS metrics from the shared serve scheduler (EFS is now a config.Tickable, ticked before CloudWatch): StorageBytes on FileSystemId + StorageClass every 15 minutes, and PermittedThroughput once a minute for provisioned file systems. Metrics whose values cloudemu does not model are not published. - DescribeAccountPreferences reports LONG_ID for FILE_SYSTEM and MOUNT_TARGET when no preference was set, and PutAccountPreferences rejects SHORT_ID and unknown values with BadRequest. Part of #1519 * fix(aws-efs): copy state under lock in snapshots, atomic access-point create, report reaps from Tick Address the review on #1649: - Snapshot now copies each file system's mutable state while holding its lock: the mount-target and access-point maps and values, the mtReadyAt / mtDeleteAt lifecycle windows, and the file-system and access-point tag maps. It also copies the IP counters. Before, they were marshaled after the lock was released while requests and Tick kept writing them, which races and can crash with a concurrent map read and write. - CreateAccessPoint checks the lifecycle state under the same lock that stores the access point, so a concurrent DeleteFileSystem either sees the access point (FileSystemInUse) or has already marked the file system deleting. The immediate (default) delete also marks the removed record as deleting, so a create waiting on its lock is rejected instead of storing a child on a file system that is gone. - UpdateFileSystemProtection rejects REPLICATING with BadRequest. EFS sets that state on a replication destination; callers choose ENABLED or DISABLED. - Tick reports reaping a deleted file system or mount target as a state change, so serve persistence saves it, and it reaps even with no CloudWatch backend wired. - Restore turns a missing mount-target or access-point map (dropped by omitempty) into an empty one, so a file system restored with neither can take new ones instead of panicking on a nil map. Part of #1519 --- docs/coverage/README.md | 2 +- docs/coverage/aws/README.md | 2 +- docs/coverage/aws/efs.md | 3 +- docs/coverage/coverage.json | 4 + providers/aws/aws.go | 5 +- providers/aws/efs/config.go | 40 ++- providers/aws/efs/efs.go | 27 +- providers/aws/efs/errors.go | 8 + providers/aws/efs/file_systems.go | 134 +++++++++- providers/aws/efs/lifecycle.go | 191 ++++++++++++++ providers/aws/efs/lifecycle_test.go | 274 ++++++++++++++++++++ providers/aws/efs/metrics.go | 148 +++++++++++ providers/aws/efs/metrics_test.go | 207 +++++++++++++++ providers/aws/efs/mount_targets.go | 59 ++++- providers/aws/efs/settle_test.go | 248 ++++++++++++++++++ providers/aws/efs/snapshot.go | 70 ++++- providers/aws/tickables_test.go | 11 +- server/aws/efs/errors.go | 4 + server/aws/efs/file_systems.go | 26 +- server/aws/efs/lifecycle_sdk_test.go | 173 ++++++++++++ server/aws/efs/prefs_protection_sdk_test.go | 65 +++++ services/efs/driver/driver.go | 12 + services/efs/driver/errors.go | 3 + 23 files changed, 1676 insertions(+), 40 deletions(-) create mode 100644 providers/aws/efs/lifecycle.go create mode 100644 providers/aws/efs/lifecycle_test.go create mode 100644 providers/aws/efs/metrics.go create mode 100644 providers/aws/efs/metrics_test.go create mode 100644 providers/aws/efs/settle_test.go create mode 100644 server/aws/efs/lifecycle_sdk_test.go create mode 100644 server/aws/efs/prefs_protection_sdk_test.go diff --git a/docs/coverage/README.md b/docs/coverage/README.md index a95cf862b..1f226d00d 100644 --- a/docs/coverage/README.md +++ b/docs/coverage/README.md @@ -81,7 +81,7 @@ code does not implement. Machine-readable: [`coverage.json`](./coverage.json). | `disks` | - | [Disks](./azure/disks.md) | - | - | 7 | | `dns` | [Route53](./aws/route53.md) | [DNS](./azure/dns.md) | [CloudDNS](./gcp/clouddns.md) | - | 16 | | `ecs` | [ECS](./aws/ecs.md) | - | - | - | 41 | -| `efs` | [EFS](./aws/efs.md) | - | - | - | 27 | +| `efs` | [EFS](./aws/efs.md) | - | - | - | 28 | | `eks` | [EKS](./aws/eks.md) | - | - | - | 40 | | `elasticsan` | - | [ElasticSan](./azure/elasticsan.md) | - | - | 8 | | `emr` | [EMR](./aws/emr.md) | - | - | - | 22 | diff --git a/docs/coverage/aws/README.md b/docs/coverage/aws/README.md index bf42ede04..daa69bc47 100644 --- a/docs/coverage/aws/README.md +++ b/docs/coverage/aws/README.md @@ -32,7 +32,7 @@ Services cloudemu emulates for AWS, by native name. Back to the [cross-provider | [EC2](./ec2.md) | `compute` | 37 | | [ECR](./ecr.md) | `containerregistry` | 15 | | [ECS](./ecs.md) | `ecs` | 41 | -| [EFS](./efs.md) | `efs` | 27 | +| [EFS](./efs.md) | `efs` | 28 | | [EKS](./eks.md) | (provider-native) | 40 | | [ELB](./elb.md) | `loadbalancer` | 19 | | [EMR](./emr.md) | (provider-native) | 22 | diff --git a/docs/coverage/aws/efs.md b/docs/coverage/aws/efs.md index 413c091f9..e92e048ff 100644 --- a/docs/coverage/aws/efs.md +++ b/docs/coverage/aws/efs.md @@ -3,7 +3,7 @@ AWS's `efs` service ยท portable interface `driver.EFS` ยท [AWS index](./README.md) -## Operations (27) +## Operations (28) | Operation | Description | | --- | --- | @@ -34,6 +34,7 @@ AWS's `efs` service ยท portable interface `driver.EFS` ยท [AWS index](./README.m | `TagResource` | Tags (current + legacy APIs share this backing). | | `UntagResource` | | | `UpdateFileSystem` | | +| `UpdateFileSystemProtection` | UpdateFileSystemProtection sets the replication-overwrite protection. An | ## Optional capabilities diff --git a/docs/coverage/coverage.json b/docs/coverage/coverage.json index 9174b0a86..f8c4de6e9 100644 --- a/docs/coverage/coverage.json +++ b/docs/coverage/coverage.json @@ -7478,6 +7478,10 @@ }, { "name": "UpdateFileSystem" + }, + { + "name": "UpdateFileSystemProtection", + "doc": "UpdateFileSystemProtection sets the replication-overwrite protection. An" } ], "optionalCapabilities": [ diff --git a/providers/aws/aws.go b/providers/aws/aws.go index 48a66f5a3..eb2a1d7b3 100644 --- a/providers/aws/aws.go +++ b/providers/aws/aws.go @@ -464,6 +464,7 @@ func newProvider(o *config.Options, shared *GlobalServices) *Provider { // EFS mount targets derive their VpcId and AZ from the subnet, so all mount // targets of a file system share a VpcId and each reflects its subnet's zone. p.EFS.SetSubnetResolver(p.VPC) + p.EFS.SetMonitoring(p.CloudWatch) // An IamInstanceProfile passed to RunInstances resolves through IAM so the // role->profile->instance chain reads back on DescribeInstances. p.EC2.SetInstanceProfileResolver(p.IAM) @@ -636,7 +637,9 @@ func awsDrivers(p *Provider) *resourcediscovery.Drivers { // background ticker calls each one on its interval. New time-driven services // register here. func (p *Provider) Tickables() []config.Tickable { - return []config.Tickable{p.CloudWatch, p.SSM, p.CloudFormation} + // EFS runs before CloudWatch so the alarms evaluated in the same tick see + // the AWS/EFS metrics it just published. + return []config.Tickable{p.EFS, p.CloudWatch, p.SSM, p.CloudFormation} } // Close tears down any real engines wired into the provider via diff --git a/providers/aws/efs/config.go b/providers/aws/efs/config.go index a2f5b8848..2b095e4da 100644 --- a/providers/aws/efs/config.go +++ b/providers/aws/efs/config.go @@ -21,6 +21,10 @@ func (m *Mock) PutLifecycleConfiguration( fd.mu.Lock() defer fd.mu.Unlock() + if err := m.requireAvailableLocked(fd); err != nil { + return nil, err + } + fd.lifecycle = append([]driver.LifecyclePolicy(nil), policies...) return append([]driver.LifecyclePolicy(nil), fd.lifecycle...), nil @@ -55,6 +59,10 @@ func (m *Mock) PutBackupPolicy(_ context.Context, fileSystemID, status string) ( fd.mu.Lock() defer fd.mu.Unlock() + if err := m.requireAvailableLocked(fd); err != nil { + return "", err + } + fd.backup = status return status, nil @@ -94,6 +102,10 @@ func (m *Mock) CreateReplicationConfiguration( fd.mu.Lock() defer fd.mu.Unlock() + if err := m.requireAvailableLocked(fd); err != nil { + return nil, err + } + if fd.replication != nil { return nil, conflict(driver.KindReplication, "file system %q already has a replication configuration", sourceFileSystemID) @@ -194,10 +206,27 @@ func (m *Mock) DescribeReplicationConfigurations( return out, nil } -// PutAccountPreferences sets the account-level resource-id preference. +// Resource-id preference values (PutAccountPreferences ResourceIdType). +const ( + resourceIDLong = "LONG_ID" + resourceIDShort = "SHORT_ID" +) + +// PutAccountPreferences sets the account-level resource-id preference. Since +// October 2021 EFS rejects SHORT_ID: "you will receive an error when setting the +// account preference to SHORT_ID" +// (https://docs.aws.amazon.com/efs/latest/ug/API_PutAccountPreferences.html). func (m *Mock) PutAccountPreferences(_ context.Context, resourceIDType string) (string, error) { - if resourceIDType == "" { + switch resourceIDType { + case resourceIDLong: + case "": return "", errors.New(errors.InvalidArgument, "ResourceIdType is required") + case resourceIDShort: + return "", errors.New(errors.InvalidArgument, + "setting the resource ID preference to SHORT_ID is no longer supported; use LONG_ID") + default: + return "", errors.Newf(errors.InvalidArgument, + "invalid ResourceIdType %q; valid values are LONG_ID and SHORT_ID", resourceIDType) } m.prefMu.Lock() @@ -209,9 +238,16 @@ func (m *Mock) PutAccountPreferences(_ context.Context, resourceIDType string) ( } // DescribeAccountPreferences returns the account-level resource-id preference. +// An account that never set one reports LONG_ID: every file system and mount +// target created since October 2021 gets a long ID, and SHORT_ID can no longer +// be set. func (m *Mock) DescribeAccountPreferences(_ context.Context) (string, error) { m.prefMu.RLock() defer m.prefMu.RUnlock() + if m.accountPref == "" { + return resourceIDLong, nil + } + return m.accountPref, nil } diff --git a/providers/aws/efs/efs.go b/providers/aws/efs/efs.go index 2a9b513d4..39216a6ee 100644 --- a/providers/aws/efs/efs.go +++ b/providers/aws/efs/efs.go @@ -3,11 +3,13 @@ package efs import ( "sync" + "time" "github.com/stackshy/cloudemu/v2/config" "github.com/stackshy/cloudemu/v2/internal/idgen" "github.com/stackshy/cloudemu/v2/internal/memstore" "github.com/stackshy/cloudemu/v2/services/efs/driver" + mondriver "github.com/stackshy/cloudemu/v2/services/monitoring/driver" ) // Compile-time check that Mock implements driver.EFS. @@ -24,7 +26,22 @@ type fsData struct { accessPts map[string]*driver.AccessPoint lifecycle []driver.LifecyclePolicy replication *driver.ReplicationConfiguration - mu sync.RWMutex + + // readyAt is when a just-created file system leaves "creating" (zero: no + // window); deleteAt, once set by DeleteFileSystem, is when the "deleting" + // file system disappears. mtReadyAt/mtDeleteAt are the same per mount + // target. All are zero/empty unless config.Options.AsyncSettle is set. + readyAt time.Time + deleteAt time.Time + mtReadyAt map[string]time.Time + mtDeleteAt map[string]time.Time + + // storageSampleAt and throughputSampleAt are when Tick last published the + // file system's StorageBytes and PermittedThroughput metrics. + storageSampleAt time.Time + throughputSampleAt time.Time + + mu sync.RWMutex } // Mock is an in-memory implementation of AWS EFS. @@ -60,6 +77,9 @@ type Mock struct { ipCounters map[string]int ipMu sync.Mutex + // monitoring receives the AWS/EFS CloudWatch metrics; nil publishes nothing. + monitoring mondriver.Monitoring + opts *config.Options } @@ -93,11 +113,6 @@ func (m *Mock) defaultKMSKeyARN() string { return idgen.AWSARN("kms", m.opts.Region, m.opts.AccountID, "key/"+defaultEFSKeyID) } -// getFS resolves a file system by id under a read lock check. -func (m *Mock) getFS(id string) (*fsData, bool) { - return m.fileSystems.Get(id) -} - func copyTags(in map[string]string) map[string]string { out := make(map[string]string, len(in)) for k, v := range in { diff --git a/providers/aws/efs/errors.go b/providers/aws/efs/errors.go index 997a5fa49..4e77560a9 100644 --- a/providers/aws/efs/errors.go +++ b/providers/aws/efs/errors.go @@ -27,6 +27,14 @@ func conflictWithID(kind, resourceID, format string, args ...any) error { } } +// incorrectFSState builds the IncorrectFileSystemLifeCycleState condition: a +// kind-tagged FailedPrecondition the wire layer maps to that 409 exception. +func incorrectFSState(fileSystemID, state string) error { + return &driver.ResourceError{Kind: driver.KindFileSystemState, Err: errors.Newf(errors.FailedPrecondition, + "file system %q is in lifecycle state %q; the operation requires it to be %q", + fileSystemID, state, driver.StateAvailable)} +} + // inUse builds a kind-tagged FailedPrecondition error. func inUse(kind, format string, args ...any) error { return &driver.ResourceError{Kind: kind, Err: errors.Newf(errors.FailedPrecondition, format, args...)} diff --git a/providers/aws/efs/file_systems.go b/providers/aws/efs/file_systems.go index 8d684639f..224150898 100644 --- a/providers/aws/efs/file_systems.go +++ b/providers/aws/efs/file_systems.go @@ -77,17 +77,24 @@ func (m *Mock) CreateFileSystem(_ context.Context, in driver.CreateFileSystemInp AvailabilityZoneName: in.AvailabilityZoneName, AvailabilityZoneID: azIDFromName(in.AvailabilityZoneName), Tags: copyTags(in.Tags), - Protection: driver.FileSystemProtection{ReplicationOverwriteProtection: "ENABLED"}, + Protection: driver.FileSystemProtection{ReplicationOverwriteProtection: driver.ProtectionEnabled}, } - m.fileSystems.Set(id, &fsData{ + // Real EFS returns while the new file system is still "creating"; under + // AsyncSettle it reports that until the window elapses. + fd := &fsData{ fs: fs, mountTgts: map[string]*driver.MountTarget{}, accessPts: map[string]*driver.AccessPoint{}, backup: backupOnCreate(in.Backup, in.AvailabilityZoneName), - }) + readyAt: m.settleDeadline(fileSystemCreateSettle), + } + + m.fileSystems.Set(id, fd) - out := fs + fd.mu.RLock() + out := m.fsViewLocked(fd) + fd.mu.RUnlock() return &out, nil } @@ -107,7 +114,9 @@ func backupOnCreate(backup bool, availabilityZoneName string) string { // access points, and no replication configuration. The check and delete run // under the file system's write lock so a concurrent CreateMountTarget can't // attach to a file system mid-delete, and all index entries (token, access -// points) are released. +// points) are released. Under AsyncSettle the call returns while the file +// system is "deleting" (as real EFS does) and it disappears once the window +// elapses; a repeated delete of a "deleting" file system is a no-op. func (m *Mock) DeleteFileSystem(_ context.Context, fileSystemID string) error { fd, ok := m.getFS(fileSystemID) if !ok { @@ -117,6 +126,10 @@ func (m *Mock) DeleteFileSystem(_ context.Context, fileSystemID string) error { fd.mu.Lock() defer fd.mu.Unlock() + if !fd.deleteAt.IsZero() { + return nil + } + if n := len(fd.mountTgts); n > 0 { return inUse(driver.KindFileSystem, "file system %q has %d mount target(s); delete them first", fileSystemID, n) @@ -132,6 +145,18 @@ func (m *Mock) DeleteFileSystem(_ context.Context, fileSystemID string) error { "file system %q has a replication configuration; delete it first", fileSystemID) } + if at := m.settleDeadline(fileSystemDeleteSettle); !at.IsZero() { + fd.deleteAt = at + + return nil + } + + // Immediate delete (the default). Mark the record as deleting as well, so a + // request that resolved fd before this delete and is waiting on fd.mu (for + // example CreateAccessPoint or CreateMountTarget) is rejected instead of + // storing a child on a file system that is gone. + fd.deleteAt = m.now() + m.fileSystems.Delete(fileSystemID) m.tokenIndex.Delete(fd.fs.CreationToken) @@ -152,16 +177,21 @@ func (m *Mock) DescribeFileSystems( fd.mu.RLock() defer fd.mu.RUnlock() - return []driver.FileSystem{fd.fs}, nil + return []driver.FileSystem{m.fsViewLocked(fd)}, nil } all := m.fileSystems.All() out := make([]driver.FileSystem, 0, len(all)) - for _, fd := range all { + for id := range all { + fd, ok := m.getFS(id) + if !ok { + continue + } + fd.mu.RLock() if creationToken == "" || fd.fs.CreationToken == creationToken { - out = append(out, fd.fs) + out = append(out, m.fsViewLocked(fd)) } fd.mu.RUnlock() } @@ -179,6 +209,10 @@ func (m *Mock) UpdateFileSystem(_ context.Context, in driver.UpdateFileSystemInp fd.mu.Lock() defer fd.mu.Unlock() + if err := m.requireAvailableLocked(fd); err != nil { + return nil, err + } + if err := validateUpdate(&fd.fs, in); err != nil { return nil, err } @@ -197,7 +231,54 @@ func (m *Mock) UpdateFileSystem(_ context.Context, in driver.UpdateFileSystemInp fd.fs.ProvisionedThroughputInMibps = 0 } - out := fd.fs + out := m.fsViewLocked(fd) + + return &out, nil +} + +// UpdateFileSystemProtection sets a file system's replication-overwrite +// protection (ENABLED | DISABLED). An empty value leaves the setting unchanged. +// REPLICATING is in the API enum, but it is the state EFS itself sets on a +// replication destination ("The file system is being used as the destination +// file system in a replication configuration"), not one a caller chooses, so it +// is rejected with BadRequest like any other value. The file system must be +// "available", and a file system that is already the source or destination of +// a replication configuration is rejected with ReplicationAlreadyExists. +func (m *Mock) UpdateFileSystemProtection( + _ context.Context, fileSystemID, protection string, +) (*driver.FileSystemProtection, error) { + switch protection { + case "", driver.ProtectionEnabled, driver.ProtectionDisabled: + default: + return nil, errors.Newf(errors.InvalidArgument, + "invalid ReplicationOverwriteProtection %q; valid values are ENABLED, DISABLED", protection) + } + + fd, ok := m.getFS(fileSystemID) + if !ok { + return nil, notFound(driver.KindFileSystem, "file system %q not found", fileSystemID) + } + + // Checked before taking fd.mu: it reads every file system's replication. + inReplication := m.inReplication(fileSystemID) + + fd.mu.Lock() + defer fd.mu.Unlock() + + if err := m.requireAvailableLocked(fd); err != nil { + return nil, err + } + + if inReplication { + return nil, conflict(driver.KindReplication, + "file system %q is already included in a replication configuration", fileSystemID) + } + + if protection != "" { + fd.fs.Protection.ReplicationOverwriteProtection = protection + } + + out := fd.fs.Protection return &out, nil } @@ -218,6 +299,10 @@ func (m *Mock) PutFileSystemPolicy( fd.mu.Lock() defer fd.mu.Unlock() + if err := m.requireAvailableLocked(fd); err != nil { + return "", err + } + fd.policy = policy return policy, nil @@ -250,6 +335,10 @@ func (m *Mock) DeleteFileSystemPolicy(_ context.Context, fileSystemID string) er fd.mu.Lock() defer fd.mu.Unlock() + if err := m.requireAvailableLocked(fd); err != nil { + return err + } + fd.policy = "" return nil @@ -335,3 +424,30 @@ func (m *Mock) ListTagsForResource(_ context.Context, resourceID string) (map[st return copyTags(getTags()), nil } + +// inReplication reports whether a file system is the source or a destination of +// any replication configuration. Callers must not hold any file system's lock. +func (m *Mock) inReplication(fileSystemID string) bool { + for _, fd := range m.fileSystems.All() { + fd.mu.RLock() + rc := fd.replication + member := rc != nil && rc.SourceFileSystemID == fileSystemID + + if rc != nil && !member { + for i := range rc.Destinations { + if rc.Destinations[i].FileSystemID == fileSystemID { + member = true + + break + } + } + } + fd.mu.RUnlock() + + if member { + return true + } + } + + return false +} diff --git a/providers/aws/efs/lifecycle.go b/providers/aws/efs/lifecycle.go new file mode 100644 index 000000000..57f3529cf --- /dev/null +++ b/providers/aws/efs/lifecycle.go @@ -0,0 +1,191 @@ +package efs + +import ( + "time" + + "github.com/stackshy/cloudemu/v2/services/efs/driver" +) + +// Settle windows for EFS's asynchronous lifecycle transitions. Real EFS returns +// from CreateFileSystem / CreateMountTarget while the resource is still +// "creating" and from DeleteFileSystem / DeleteMountTarget while it is still +// "deleting". They only apply when config.Options.AsyncSettle is set +// (SettleDuration returns 0 otherwise), so the default stays synchronous: +// resources are "available" immediately and deletes are instant. They are short +// so an SDK waiter succeeds on its first poll while an immediate Describe still +// observes the transient state. +const ( + fileSystemCreateSettle = 2 * time.Second + fileSystemDeleteSettle = 1 * time.Second + mountTargetCreateSettle = 2 * time.Second + mountTargetDeleteSettle = 1 * time.Second +) + +func (m *Mock) now() time.Time { return m.opts.Clock.Now() } + +// settleDeadline returns now+d when async settling is enabled, else the zero +// time (no window). +func (m *Mock) settleDeadline(d time.Duration) time.Time { + if d = m.opts.SettleDuration(d); d <= 0 { + return time.Time{} + } + + return m.now().Add(d) +} + +// fsStateLocked is the lifecycle state a file system reports now: "deleting" +// once DeleteFileSystem has accepted it, "creating" until its create window +// elapses, otherwise its stored (available) state. Callers hold fd.mu. +func (m *Mock) fsStateLocked(fd *fsData) string { + switch { + case !fd.deleteAt.IsZero(): + return driver.StateDeleting + case m.now().Before(fd.readyAt): + return driver.StateCreating + default: + return fd.fs.LifeCycleState + } +} + +// fsViewLocked returns the file system as callers see it, with the lifecycle +// state overlaid. Callers hold fd.mu. +func (m *Mock) fsViewLocked(fd *fsData) driver.FileSystem { + out := fd.fs + out.LifeCycleState = m.fsStateLocked(fd) + + return out +} + +// requireAvailableLocked rejects an operation on a file system that is not +// "available" with IncorrectFileSystemLifeCycleState, as real EFS does for +// CreateMountTarget, CreateAccessPoint, UpdateFileSystem, the policy/backup/ +// lifecycle/replication writes and UpdateFileSystemProtection. Callers hold fd.mu. +func (m *Mock) requireAvailableLocked(fd *fsData) error { + if st := m.fsStateLocked(fd); st != driver.StateAvailable { + return incorrectFSState(fd.fs.FileSystemID, st) + } + + return nil +} + +// mtStateLocked is the lifecycle state a mount target reports now. Callers hold +// fd.mu. +func (m *Mock) mtStateLocked(fd *fsData, mt *driver.MountTarget) string { + if _, ok := fd.mtDeleteAt[mt.MountTargetID]; ok { + return driver.StateDeleting + } + + if ready, ok := fd.mtReadyAt[mt.MountTargetID]; ok && m.now().Before(ready) { + return driver.StateCreating + } + + return mt.LifeCycleState +} + +// mtViewLocked returns a copy of a mount target with its lifecycle state +// overlaid. Callers hold fd.mu. +func (m *Mock) mtViewLocked(fd *fsData, mt *driver.MountTarget) driver.MountTarget { + out := *mt + out.LifeCycleState = m.mtStateLocked(fd, mt) + + return out +} + +// getFS resolves a file system by id, first finishing any deletion whose +// window has elapsed: a file system past its "deleting" window is gone +// (FileSystemNotFound), and mount targets past theirs are removed. +func (m *Mock) getFS(id string) (*fsData, bool) { + fd, ok, _ := m.settleFS(id) + + return fd, ok +} + +// settleFS is getFS that also reports whether it changed stored state (reaped +// the file system or any of its mount targets, or dropped an elapsed create +// window), so Tick can report that change to the serve scheduler. +func (m *Mock) settleFS(id string) (fd *fsData, ok, changed bool) { + fd, ok = m.fileSystems.Get(id) + if !ok { + return nil, false, false + } + + if m.reapFS(id, fd) { + return nil, false, true + } + + return fd, true, m.reapMountTargets(fd) +} + +// reapFS removes a file system whose deletion window has elapsed, releasing its +// creation token, and reports whether it did. +func (m *Mock) reapFS(id string, fd *fsData) bool { + fd.mu.RLock() + gone := !fd.deleteAt.IsZero() && !m.now().Before(fd.deleteAt) + token := fd.fs.CreationToken + fd.mu.RUnlock() + + if !gone { + return false + } + + m.fileSystems.Delete(id) + + if owner, ok := m.tokenIndex.Get(token); ok && owner == id { + m.tokenIndex.Delete(token) + } + + return true +} + +// reapMountTargets removes mount targets whose deletion window has elapsed and +// drops create windows that have elapsed. It reports whether it changed +// anything. +func (m *Mock) reapMountTargets(fd *fsData) bool { + fd.mu.Lock() + defer fd.mu.Unlock() + + now := m.now() + changed := false + + for id, ready := range fd.mtReadyAt { + if !now.Before(ready) { + delete(fd.mtReadyAt, id) + + changed = true + } + } + + removed := false + + for id, at := range fd.mtDeleteAt { + if now.Before(at) { + continue + } + + delete(fd.mtDeleteAt, id) + delete(fd.mtReadyAt, id) + delete(fd.mountTgts, id) + m.mtIndex.Delete(id) + + removed = true + } + + if removed { + //nolint:gosec // mount-target count is tiny, never near int32 max + fd.fs.NumberOfMountTargets = int32(len(fd.mountTgts)) + } + + return changed || removed +} + +// removeMountTargetLocked deletes a mount target immediately. Callers hold fd.mu +// for writing. +func (m *Mock) removeMountTargetLocked(fd *fsData, mountTargetID string) { + delete(fd.mountTgts, mountTargetID) + delete(fd.mtReadyAt, mountTargetID) + delete(fd.mtDeleteAt, mountTargetID) + //nolint:gosec // mount-target count is tiny, never near int32 max + fd.fs.NumberOfMountTargets = int32(len(fd.mountTgts)) + + m.mtIndex.Delete(mountTargetID) +} diff --git a/providers/aws/efs/lifecycle_test.go b/providers/aws/efs/lifecycle_test.go new file mode 100644 index 000000000..9f90e9426 --- /dev/null +++ b/providers/aws/efs/lifecycle_test.go @@ -0,0 +1,274 @@ +package efs_test + +import ( + "context" + stderrors "errors" + "testing" + "time" + + "github.com/stackshy/cloudemu/v2/config" + "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/providers/aws/efs" + "github.com/stackshy/cloudemu/v2/services/efs/driver" +) + +func newAsyncMock(t *testing.T) (*efs.Mock, *config.FakeClock) { + t.Helper() + + clk := config.NewFakeClock(time.Unix(1_700_000_000, 0)) + + return efs.New(config.NewOptions( + config.WithClock(clk), + config.WithRegion("us-east-1"), + config.WithAccountID("000000000000"), + config.WithAsyncSettle(), + )), clk +} + +// isIncorrectFSState reports whether err is the IncorrectFileSystemLifeCycleState +// condition (FailedPrecondition tagged with the file-system-state kind). +func isIncorrectFSState(err error) bool { + var re *driver.ResourceError + + return errors.IsFailedPrecondition(err) && stderrors.As(err, &re) && re.Kind == driver.KindFileSystemState +} + +func fsState(t *testing.T, m *efs.Mock, id string) string { + t.Helper() + + got, err := m.DescribeFileSystems(context.Background(), id, "") + if err != nil { + t.Fatalf("DescribeFileSystems(%s): %v", id, err) + } + + return got[0].LifeCycleState +} + +func mtState(t *testing.T, m *efs.Mock, id string) string { + t.Helper() + + got, err := m.DescribeMountTargets(context.Background(), "", id, "") + if err != nil { + t.Fatalf("DescribeMountTargets(%s): %v", id, err) + } + + return got[0].LifeCycleState +} + +// TestAsyncFileSystemLifecycle pins the real EFS lifecycle under AsyncSettle: +// CreateFileSystem returns "creating" and only becomes "available" later; +// mount targets can't be created until then (IncorrectFileSystemLifeCycleState); +// DeleteFileSystem returns while the file system is "deleting" and it then +// disappears (FileSystemNotFound). +func TestAsyncFileSystemLifecycle(t *testing.T) { + ctx := context.Background() + m, clk := newAsyncMock(t) + + fs, err := m.CreateFileSystem(ctx, driver.CreateFileSystemInput{CreationToken: "tok"}) + if err != nil { + t.Fatalf("CreateFileSystem: %v", err) + } + + if fs.LifeCycleState != driver.StateCreating { + t.Fatalf("create response state = %q, want creating", fs.LifeCycleState) + } + + if got := fsState(t, m, fs.FileSystemID); got != driver.StateCreating { + t.Fatalf("describe right after create = %q, want creating", got) + } + + _, err = m.CreateMountTarget(ctx, driver.CreateMountTargetInput{FileSystemID: fs.FileSystemID, SubnetID: "subnet-1"}) + if !isIncorrectFSState(err) { + t.Fatalf("CreateMountTarget on creating fs: want IncorrectFileSystemLifeCycleState, got %v", err) + } + + if _, err = m.UpdateFileSystemProtection(ctx, fs.FileSystemID, "DISABLED"); !isIncorrectFSState(err) { + t.Fatalf("UpdateFileSystemProtection on creating fs: want IncorrectFileSystemLifeCycleState, got %v", err) + } + + clk.Advance(time.Minute) + + if got := fsState(t, m, fs.FileSystemID); got != driver.StateAvailable { + t.Fatalf("after settle = %q, want available", got) + } + + mt, err := m.CreateMountTarget(ctx, driver.CreateMountTargetInput{FileSystemID: fs.FileSystemID, SubnetID: "subnet-1"}) + if err != nil { + t.Fatalf("CreateMountTarget: %v", err) + } + + if mt.LifeCycleState != driver.StateCreating { + t.Fatalf("mount target create state = %q, want creating", mt.LifeCycleState) + } + + clk.Advance(time.Minute) + + if got := mtState(t, m, mt.MountTargetID); got != driver.StateAvailable { + t.Fatalf("mount target after settle = %q, want available", got) + } + + if err = m.DeleteMountTarget(ctx, mt.MountTargetID); err != nil { + t.Fatalf("DeleteMountTarget: %v", err) + } + + if got := mtState(t, m, mt.MountTargetID); got != driver.StateDeleting { + t.Fatalf("mount target after delete = %q, want deleting", got) + } + + // Still present while deleting, so the file system is still in use. + if err = m.DeleteFileSystem(ctx, fs.FileSystemID); !errors.IsFailedPrecondition(err) || isIncorrectFSState(err) { + t.Fatalf("DeleteFileSystem with deleting mount target: want FileSystemInUse, got %v", err) + } + + clk.Advance(time.Minute) + + if _, err = m.DescribeMountTargets(ctx, "", mt.MountTargetID, ""); !errors.IsNotFound(err) { + t.Fatalf("mount target after delete settle: want NotFound, got %v", err) + } + + if got := fsState(t, m, fs.FileSystemID); got != driver.StateAvailable { + t.Fatalf("fs state = %q, want available", got) + } + + if err = m.DeleteFileSystem(ctx, fs.FileSystemID); err != nil { + t.Fatalf("DeleteFileSystem: %v", err) + } + + if got := fsState(t, m, fs.FileSystemID); got != driver.StateDeleting { + t.Fatalf("fs after delete = %q, want deleting", got) + } + + if _, err = m.PutBackupPolicy(ctx, fs.FileSystemID, driver.BackupEnabled); !isIncorrectFSState(err) { + t.Fatalf("PutBackupPolicy on deleting fs: want IncorrectFileSystemLifeCycleState, got %v", err) + } + + clk.Advance(time.Minute) + + if _, err = m.DescribeFileSystems(ctx, fs.FileSystemID, ""); !errors.IsNotFound(err) { + t.Fatalf("fs after delete settle: want NotFound, got %v", err) + } + + all, err := m.DescribeFileSystems(ctx, "", "") + if err != nil || len(all) != 0 { + t.Fatalf("describe all after delete = %v, %v; want empty", all, err) + } + + // The creation token is released once the file system is gone. + if _, err = m.CreateFileSystem(ctx, driver.CreateFileSystemInput{CreationToken: "tok"}); err != nil { + t.Fatalf("re-create with released token: %v", err) + } +} + +// TestSyncDefaultIsImmediate pins the historical default (AsyncSettle off): +// everything is available immediately and deletes are instant. +func TestSyncDefaultIsImmediate(t *testing.T) { + ctx := context.Background() + m := newMock(t) + + fs, err := m.CreateFileSystem(ctx, driver.CreateFileSystemInput{CreationToken: "tok"}) + if err != nil || fs.LifeCycleState != driver.StateAvailable { + t.Fatalf("create = %+v, %v; want available", fs, err) + } + + mt, err := m.CreateMountTarget(ctx, driver.CreateMountTargetInput{FileSystemID: fs.FileSystemID, SubnetID: "subnet-1"}) + if err != nil || mt.LifeCycleState != driver.StateAvailable { + t.Fatalf("create mt = %+v, %v; want available", mt, err) + } + + if err = m.DeleteMountTarget(ctx, mt.MountTargetID); err != nil { + t.Fatalf("DeleteMountTarget: %v", err) + } + + if _, err = m.DescribeMountTargets(ctx, "", mt.MountTargetID, ""); !errors.IsNotFound(err) { + t.Fatalf("mount target after sync delete: want NotFound, got %v", err) + } + + if err = m.DeleteFileSystem(ctx, fs.FileSystemID); err != nil { + t.Fatalf("DeleteFileSystem: %v", err) + } + + if _, err = m.DescribeFileSystems(ctx, fs.FileSystemID, ""); !errors.IsNotFound(err) { + t.Fatalf("fs after sync delete: want NotFound, got %v", err) + } +} + +func TestUpdateFileSystemProtection(t *testing.T) { + ctx := context.Background() + m := newMock(t) + + fs, err := m.CreateFileSystem(ctx, driver.CreateFileSystemInput{CreationToken: "tok"}) + if err != nil { + t.Fatalf("CreateFileSystem: %v", err) + } + + if fs.Protection.ReplicationOverwriteProtection != driver.ProtectionEnabled { + t.Fatalf("default protection = %q, want ENABLED", fs.Protection.ReplicationOverwriteProtection) + } + + got, err := m.UpdateFileSystemProtection(ctx, fs.FileSystemID, driver.ProtectionDisabled) + if err != nil || got.ReplicationOverwriteProtection != driver.ProtectionDisabled { + t.Fatalf("UpdateFileSystemProtection = %+v, %v; want DISABLED", got, err) + } + + desc, err := m.DescribeFileSystems(ctx, fs.FileSystemID, "") + if err != nil || desc[0].Protection.ReplicationOverwriteProtection != driver.ProtectionDisabled { + t.Fatalf("describe protection = %+v, %v; want DISABLED", desc, err) + } + + // Omitting the value leaves the current setting unchanged. + if got, err = m.UpdateFileSystemProtection(ctx, fs.FileSystemID, ""); err != nil || + got.ReplicationOverwriteProtection != driver.ProtectionDisabled { + t.Fatalf("empty update = %+v, %v; want unchanged DISABLED", got, err) + } + + if _, err = m.UpdateFileSystemProtection(ctx, fs.FileSystemID, "BOGUS"); !errors.IsInvalidArgument(err) { + t.Fatalf("invalid value: want InvalidArgument, got %v", err) + } + + if _, err = m.UpdateFileSystemProtection(ctx, "fs-0123456789abcdef0", driver.ProtectionEnabled); !errors.IsNotFound(err) { + t.Fatalf("unknown fs: want NotFound, got %v", err) + } +} + +// TestLifecycleSurvivesSnapshot checks a deleting file system and mount target +// keep their transient state (and later vanish) across Snapshot/Restore. +func TestLifecycleSurvivesSnapshot(t *testing.T) { + ctx := context.Background() + m, clk := newAsyncMock(t) + + fs, err := m.CreateFileSystem(ctx, driver.CreateFileSystemInput{CreationToken: "tok"}) + if err != nil { + t.Fatalf("CreateFileSystem: %v", err) + } + + clk.Advance(time.Minute) + + mt, err := m.CreateMountTarget(ctx, driver.CreateMountTargetInput{FileSystemID: fs.FileSystemID, SubnetID: "subnet-1"}) + if err != nil { + t.Fatalf("CreateMountTarget: %v", err) + } + + if err = m.DeleteMountTarget(ctx, mt.MountTargetID); err != nil { + t.Fatalf("DeleteMountTarget: %v", err) + } + + data, err := m.Snapshot(ctx, false) + if err != nil { + t.Fatalf("Snapshot: %v", err) + } + + restored := efs.New(config.NewOptions(config.WithClock(clk), config.WithAsyncSettle())) + if err = restored.Restore(ctx, data); err != nil { + t.Fatalf("Restore: %v", err) + } + + if got := mtState(t, restored, mt.MountTargetID); got != driver.StateDeleting { + t.Fatalf("restored mount target = %q, want deleting", got) + } + + clk.Advance(time.Minute) + + if _, err = restored.DescribeMountTargets(ctx, "", mt.MountTargetID, ""); !errors.IsNotFound(err) { + t.Fatalf("restored mount target after settle: want NotFound, got %v", err) + } +} diff --git a/providers/aws/efs/metrics.go b/providers/aws/efs/metrics.go new file mode 100644 index 000000000..f82f786ed --- /dev/null +++ b/providers/aws/efs/metrics.go @@ -0,0 +1,148 @@ +package efs + +import ( + "context" + "sort" + "time" + + "github.com/stackshy/cloudemu/v2/services/efs/driver" + mondriver "github.com/stackshy/cloudemu/v2/services/monitoring/driver" +) + +// AWS/EFS CloudWatch metrics +// (https://docs.aws.amazon.com/efs/latest/ug/efs-metrics.html). +// +// Real EFS pushes its metrics on a schedule, not per request: "Amazon EFS metric +// data is automatically sent to CloudWatch at 1-minute periods, unless noted" +// (https://docs.aws.amazon.com/efs/latest/ug/monitoring-cloudwatch.html), and +// StorageBytes "is emitted to CloudWatch every 15 minutes". Tick does the same +// on the shared serve scheduler. +// +// Only metrics whose values cloudemu actually knows are published: +// +// - StorageBytes (Bytes) on FileSystemId + StorageClass, for the Total, +// Standard and IA classes, from the file system's stored SizeInBytes. The +// Archive, IASizeOverhead and ArchiveSizeOverhead classes are not modeled. +// - PermittedThroughput (Bytes/Second) on FileSystemId, for provisioned +// file systems only: with no stored data it is exactly the provisioned +// amount. For bursting and elastic file systems it depends on burst credits +// and AWS-side limits cloudemu does not model, so it is not published. +// +// The I/O metrics (TotalIOBytes, DataReadIOBytes, MeteredIOBytes, ...), +// ClientConnections, PercentIOLimit and BurstCreditBalance describe NFS traffic +// and credit accounting that cloudemu does not serve, and TimeSinceLastSync +// needs real replication, so none of them is published. +const ( + metricsNamespace = "AWS/EFS" + + dimFileSystemID = "FileSystemId" + dimStorageClass = "StorageClass" + + metricStorageBytes = "StorageBytes" + metricPermittedThroughput = "PermittedThroughput" + + unitBytes = "Bytes" + unitBytesPerSecond = "Bytes/Second" + + bytesPerMiB = 1 << 20 + + // throughputInterval is how often the 1-minute metrics are published. + throughputInterval = time.Minute + // storageInterval is how often StorageBytes is published. + storageInterval = 15 * time.Minute +) + +// SetMonitoring wires the CloudWatch backend that receives the AWS/EFS metrics. +// Nil-safe: with no backend wired nothing is published. +func (m *Mock) SetMonitoring(mon mondriver.Monitoring) { + m.monitoring = mon +} + +// Tick finishes the lifecycle transitions whose windows have elapsed (a +// "deleting" file system or mount target is removed) and publishes the AWS/EFS +// metrics that are due at now for every available file system. It reports +// whether it changed state: a reap, or published metrics (the datapoints land +// in CloudWatch's persisted state and the sample times in EFS's). It has the +// config.Tickable signature of the shared serve scheduler; library mode never +// calls it, so tests drive it with a FakeClock. Each file system is sampled +// under its own lock and everything is published after all locks are released. +func (m *Mock) Tick(now time.Time) bool { + var ( + data []mondriver.MetricDatum + changed bool + ) + + ids := m.fileSystems.Keys() + sort.Strings(ids) + + for _, id := range ids { + fd, ok, reaped := m.settleFS(id) + changed = changed || reaped + + if !ok || m.monitoring == nil { + continue + } + + data = append(data, m.dueMetrics(fd, now)...) + } + + if len(data) > 0 { + _ = m.monitoring.PutMetricData(context.Background(), data) + + changed = true + } + + return changed +} + +// dueMetrics samples fd's metrics that are due at now and records the sample +// times. A file system that is still creating or already deleting reports +// nothing. Callers must not hold fd.mu. +func (m *Mock) dueMetrics(fd *fsData, now time.Time) []mondriver.MetricDatum { + fd.mu.Lock() + defer fd.mu.Unlock() + + if m.fsStateLocked(fd) != driver.StateAvailable { + return nil + } + + id := fd.fs.FileSystemID + + var data []mondriver.MetricDatum + + if due(fd.storageSampleAt, now, storageInterval) { + fd.storageSampleAt = now + + for _, c := range []struct { + class string + value int64 + }{ + {"Total", fd.fs.SizeInBytes.Value}, + {"Standard", fd.fs.SizeInBytes.ValueInStandard}, + {"IA", fd.fs.SizeInBytes.ValueInIA}, + } { + data = append(data, mondriver.MetricDatum{ + Namespace: metricsNamespace, MetricName: metricStorageBytes, + Value: float64(c.value), Unit: unitBytes, Timestamp: now, + Dimensions: map[string]string{dimFileSystemID: id, dimStorageClass: c.class}, + }) + } + } + + if fd.fs.ThroughputMode == driver.ThroughputProvisioned && due(fd.throughputSampleAt, now, throughputInterval) { + fd.throughputSampleAt = now + + data = append(data, mondriver.MetricDatum{ + Namespace: metricsNamespace, MetricName: metricPermittedThroughput, + Value: fd.fs.ProvisionedThroughputInMibps * bytesPerMiB, Unit: unitBytesPerSecond, Timestamp: now, + Dimensions: map[string]string{dimFileSystemID: id}, + }) + } + + return data +} + +// due reports whether a metric last sampled at last is due again at now. +func due(last, now time.Time, interval time.Duration) bool { + return last.IsZero() || now.Sub(last) >= interval +} diff --git a/providers/aws/efs/metrics_test.go b/providers/aws/efs/metrics_test.go new file mode 100644 index 000000000..74cfe5b0b --- /dev/null +++ b/providers/aws/efs/metrics_test.go @@ -0,0 +1,207 @@ +package efs_test + +import ( + "context" + stderrors "errors" + "testing" + "time" + + "github.com/stackshy/cloudemu/v2/config" + "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/providers/aws/cloudwatch" + "github.com/stackshy/cloudemu/v2/providers/aws/efs" + "github.com/stackshy/cloudemu/v2/services/efs/driver" + mondriver "github.com/stackshy/cloudemu/v2/services/monitoring/driver" +) + +func sampleCount(t *testing.T, cw *cloudwatch.Mock, name string, dims map[string]string, from, to time.Time) []float64 { + t.Helper() + + res, err := cw.GetMetricData(context.Background(), mondriver.GetMetricInput{ + Namespace: "AWS/EFS", MetricName: name, Dimensions: dims, + StartTime: from, EndTime: to, Period: 60, Stat: "SampleCount", + }) + if err != nil { + t.Fatalf("GetMetricData %s: %v", name, err) + } + + return res.Values +} + +// TestMetricsPublishedOnTick checks AWS/EFS metrics are published by the +// scheduler, not by API calls: nothing on create, then StorageBytes every 15 +// minutes and PermittedThroughput (provisioned only) once a minute, whatever +// the request traffic. +func TestMetricsPublishedOnTick(t *testing.T) { + ctx := context.Background() + start := time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC) + clk := config.NewFakeClock(start) + opts := config.NewOptions(config.WithClock(clk)) + m := efs.New(opts) + cw := cloudwatch.New(opts) + m.SetMonitoring(cw) + + fs, err := m.CreateFileSystem(ctx, driver.CreateFileSystemInput{ + CreationToken: "tok", ThroughputMode: driver.ThroughputProvisioned, ProvisionedThroughputInMibps: 10, + }) + if err != nil { + t.Fatalf("CreateFileSystem: %v", err) + } + + burst, err := m.CreateFileSystem(ctx, driver.CreateFileSystemInput{CreationToken: "burst"}) + if err != nil { + t.Fatalf("CreateFileSystem bursting: %v", err) + } + + if names, _ := cw.ListMetrics(ctx, "AWS/EFS"); len(names) != 0 { + t.Fatalf("metrics published on create: %v", names) + } + + // Two ticks per minute for 30 minutes, with request traffic in between. + for i := 0; i < 30; i++ { + if _, err = m.DescribeFileSystems(ctx, fs.FileSystemID, ""); err != nil { + t.Fatalf("DescribeFileSystems: %v", err) + } + + m.Tick(clk.Now()) + m.Tick(clk.Now()) // a second tick in the same minute publishes nothing + clk.Advance(time.Minute) + } + + end := clk.Now() + tput := sampleCount(t, cw, "PermittedThroughput", map[string]string{"FileSystemId": fs.FileSystemID}, start, end) + + if len(tput) != 30 { + t.Fatalf("PermittedThroughput minutes = %d, want 30", len(tput)) + } + + for _, v := range tput { + if v != 1 { + t.Fatalf("PermittedThroughput SampleCount per minute = %v, want 1", tput) + } + } + + res, err := cw.GetMetricData(ctx, mondriver.GetMetricInput{ + Namespace: "AWS/EFS", MetricName: "PermittedThroughput", + Dimensions: map[string]string{"FileSystemId": fs.FileSystemID}, + StartTime: start, EndTime: start.Add(time.Minute), Period: 60, Stat: "Maximum", + }) + if err != nil || len(res.Values) != 1 || res.Values[0] != 10*(1<<20) || res.Unit != "Bytes/Second" { + t.Fatalf("PermittedThroughput = %+v, %v; want 10 MiB/s in Bytes/Second", res, err) + } + + if got := sampleCount(t, cw, "PermittedThroughput", + map[string]string{"FileSystemId": burst.FileSystemID}, start, end); len(got) != 0 { + t.Fatalf("bursting file system published PermittedThroughput: %v", got) + } + + for _, class := range []string{"Total", "Standard", "IA"} { + got := sampleCount(t, cw, "StorageBytes", + map[string]string{"FileSystemId": fs.FileSystemID, "StorageClass": class}, start, end) + if len(got) != 2 { // minutes 0 and 15 + t.Fatalf("StorageBytes %s samples over 30 min = %v, want 2 (every 15 minutes)", class, got) + } + } +} + +// TestMetricsSkipCreatingAndDeleting checks a file system reports metrics only +// while it is available. +func TestMetricsSkipCreatingAndDeleting(t *testing.T) { + ctx := context.Background() + clk := config.NewFakeClock(time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)) + opts := config.NewOptions(config.WithClock(clk), config.WithAsyncSettle()) + m := efs.New(opts) + cw := cloudwatch.New(opts) + m.SetMonitoring(cw) + + fs, err := m.CreateFileSystem(ctx, driver.CreateFileSystemInput{CreationToken: "tok"}) + if err != nil { + t.Fatalf("CreateFileSystem: %v", err) + } + + if m.Tick(clk.Now()) { + t.Fatal("Tick published for a creating file system") + } + + clk.Advance(time.Minute) + + if !m.Tick(clk.Now()) { + t.Fatal("Tick published nothing for an available file system") + } + + if err = m.DeleteFileSystem(ctx, fs.FileSystemID); err != nil { + t.Fatalf("DeleteFileSystem: %v", err) + } + + deletedAt := clk.Now() + + clk.Advance(time.Hour) + + // This Tick reaps the file system (a state change) but publishes nothing. + m.Tick(clk.Now()) + + dims := map[string]string{"FileSystemId": fs.FileSystemID, "StorageClass": "Total"} + if got := sampleCount(t, cw, "StorageBytes", dims, deletedAt.Add(time.Second), clk.Now().Add(time.Minute)); len(got) != 0 { + t.Fatalf("Tick published for a deleted file system: %v", got) + } + + if m.Tick(clk.Now()) { + t.Fatal("Tick reported a change once the file system was gone") + } +} + +func TestAccountPreferencesDefault(t *testing.T) { + ctx := context.Background() + m := newMock(t) + + if got, err := m.DescribeAccountPreferences(ctx); err != nil || got != "LONG_ID" { + t.Fatalf("unset preference = %q, %v; want LONG_ID", got, err) + } + + for _, bad := range []string{"SHORT_ID", "long_id", "BOGUS", ""} { + if _, err := m.PutAccountPreferences(ctx, bad); !errors.IsInvalidArgument(err) { + t.Errorf("PutAccountPreferences(%q): want InvalidArgument, got %v", bad, err) + } + } + + if got, err := m.PutAccountPreferences(ctx, "LONG_ID"); err != nil || got != "LONG_ID" { + t.Fatalf("PutAccountPreferences(LONG_ID) = %q, %v", got, err) + } +} + +// TestProtectionRejectedInReplication checks UpdateFileSystemProtection returns +// ReplicationAlreadyExists for a file system that is the source or the +// destination of a replication configuration. +func TestProtectionRejectedInReplication(t *testing.T) { + ctx := context.Background() + m := newMock(t) + + src, _ := m.CreateFileSystem(ctx, driver.CreateFileSystemInput{CreationToken: "src"}) + dst, _ := m.CreateFileSystem(ctx, driver.CreateFileSystemInput{CreationToken: "dst"}) + + if _, err := m.CreateReplicationConfiguration(ctx, src.FileSystemID, + []driver.DestinationToCreate{{FileSystemID: dst.FileSystemID}}); err != nil { + t.Fatalf("CreateReplicationConfiguration: %v", err) + } + + for _, id := range []string{src.FileSystemID, dst.FileSystemID} { + _, err := m.UpdateFileSystemProtection(ctx, id, driver.ProtectionEnabled) + if !isReplicationConflict(err) { + t.Fatalf("UpdateFileSystemProtection(%s) in replication: want ReplicationAlreadyExists, got %v", id, err) + } + } + + if err := m.DeleteReplicationConfiguration(ctx, src.FileSystemID); err != nil { + t.Fatalf("DeleteReplicationConfiguration: %v", err) + } + + if _, err := m.UpdateFileSystemProtection(ctx, dst.FileSystemID, driver.ProtectionDisabled); err != nil { + t.Fatalf("UpdateFileSystemProtection after replication removed: %v", err) + } +} + +func isReplicationConflict(err error) bool { + var re *driver.ResourceError + + return stderrors.As(err, &re) && re.Kind == driver.KindReplication && errors.IsAlreadyExists(err) +} diff --git a/providers/aws/efs/mount_targets.go b/providers/aws/efs/mount_targets.go index a51288a11..ac3c6d125 100644 --- a/providers/aws/efs/mount_targets.go +++ b/providers/aws/efs/mount_targets.go @@ -6,6 +6,7 @@ import ( "net" "strconv" "strings" + "time" "github.com/stackshy/cloudemu/v2/errors" "github.com/stackshy/cloudemu/v2/internal/idgen" @@ -33,6 +34,11 @@ func (m *Mock) CreateMountTarget(ctx context.Context, in driver.CreateMountTarge fd.mu.Lock() defer fd.mu.Unlock() + // A mount target can only be created once the file system is "available". + if err := m.requireAvailableLocked(fd); err != nil { + return nil, err + } + vpcID, azName, azIdent := m.mountTargetPlacement(&fd.fs, subnet) // A One Zone file system only takes a mount target in its own zone. @@ -70,7 +76,17 @@ func (m *Mock) CreateMountTarget(ctx context.Context, in driver.CreateMountTarge fd.fs.NumberOfMountTargets = int32(len(fd.mountTgts)) m.mtIndex.Set(id, fd.fs.FileSystemID) - out := *mt + // Real EFS returns while the mount target is still "creating"; under + // AsyncSettle it reports that until the window elapses. + if ready := m.settleDeadline(mountTargetCreateSettle); !ready.IsZero() { + if fd.mtReadyAt == nil { + fd.mtReadyAt = map[string]time.Time{} + } + + fd.mtReadyAt[id] = ready + } + + out := m.mtViewLocked(fd, mt) return &out, nil } @@ -251,7 +267,9 @@ func (m *Mock) nextIP() string { return fmt.Sprintf("10.0.%d.%d", n/ipSegmentSize, n%ipSegmentSize) } -// DeleteMountTarget removes a mount target. +// DeleteMountTarget removes a mount target. Under AsyncSettle the call returns +// while the mount target is "deleting" (as real EFS does) and it disappears once +// the window elapses; a repeated delete of a "deleting" mount target is a no-op. func (m *Mock) DeleteMountTarget(_ context.Context, mountTargetID string) error { fsID, ok := m.mtIndex.Get(mountTargetID) if !ok { @@ -268,11 +286,25 @@ func (m *Mock) DeleteMountTarget(_ context.Context, mountTargetID string) error fd.mu.Lock() defer fd.mu.Unlock() - delete(fd.mountTgts, mountTargetID) - //nolint:gosec // mount-target count is tiny, never near int32 max - fd.fs.NumberOfMountTargets = int32(len(fd.mountTgts)) + if _, ok := fd.mountTgts[mountTargetID]; !ok { + return notFound(driver.KindMountTarget, "mount target %q not found", mountTargetID) + } + + if _, deleting := fd.mtDeleteAt[mountTargetID]; deleting { + return nil + } + + if at := m.settleDeadline(mountTargetDeleteSettle); !at.IsZero() { + if fd.mtDeleteAt == nil { + fd.mtDeleteAt = map[string]time.Time{} + } + + fd.mtDeleteAt[mountTargetID] = at + + return nil + } - m.mtIndex.Delete(mountTargetID) + m.removeMountTargetLocked(fd, mountTargetID) return nil } @@ -319,7 +351,7 @@ func (m *Mock) mountTargetByID(mountTargetID string) ([]driver.MountTarget, erro return nil, notFound(driver.KindMountTarget, "mount target %q not found", mountTargetID) } - return []driver.MountTarget{*mt}, nil + return []driver.MountTarget{m.mtViewLocked(fd, mt)}, nil } func (m *Mock) mountTargetsOfFS(fileSystemID string) ([]driver.MountTarget, error) { @@ -333,7 +365,7 @@ func (m *Mock) mountTargetsOfFS(fileSystemID string) ([]driver.MountTarget, erro out := make([]driver.MountTarget, 0, len(fd.mountTgts)) for _, mt := range fd.mountTgts { - out = append(out, *mt) + out = append(out, m.mtViewLocked(fd, mt)) } return out, nil @@ -407,6 +439,17 @@ func (m *Mock) CreateAccessPoint(_ context.Context, in driver.CreateAccessPointI // file-system path is safe the same way: its loser recovers by id, not by // object lookup). fd.mu.Lock() + + // An access point can only be created on an "available" file system. The + // check runs under the same lock as the store, so a concurrent + // DeleteFileSystem either sees this access point (FileSystemInUse) or has + // already marked the file system "deleting" (rejected here). + if err := m.requireAvailableLocked(fd); err != nil { + fd.mu.Unlock() + + return nil, err + } + ap := &driver.AccessPoint{ ClientToken: in.ClientToken, Name: name, diff --git a/providers/aws/efs/settle_test.go b/providers/aws/efs/settle_test.go new file mode 100644 index 000000000..ce782f883 --- /dev/null +++ b/providers/aws/efs/settle_test.go @@ -0,0 +1,248 @@ +package efs_test + +import ( + "context" + "fmt" + "sync" + "testing" + "time" + + "github.com/stackshy/cloudemu/v2/config" + "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/providers/aws/cloudwatch" + "github.com/stackshy/cloudemu/v2/providers/aws/efs" + "github.com/stackshy/cloudemu/v2/services/efs/driver" +) + +// TestSnapshotConcurrentWithLifecycle runs Snapshot while requests and Tick keep +// writing the per-file-system maps (mount targets settling and being reaped, +// tags changing). Snapshot marshals after it releases the file-system lock, so +// it must copy those maps first; under -race this fails if it does not. +func TestSnapshotConcurrentWithLifecycle(t *testing.T) { + ctx := context.Background() + m, clk := newAsyncMock(t) + m.SetMonitoring(cloudwatch.New(config.NewOptions(config.WithClock(clk)))) + + fs, err := m.CreateFileSystem(ctx, driver.CreateFileSystemInput{CreationToken: "tok"}) + if err != nil { + t.Fatalf("CreateFileSystem: %v", err) + } + + clk.Advance(time.Minute) + + ap, err := m.CreateAccessPoint(ctx, driver.CreateAccessPointInput{FileSystemID: fs.FileSystemID}) + if err != nil { + t.Fatalf("CreateAccessPoint: %v", err) + } + + const rounds = 200 + + var wg sync.WaitGroup + + wg.Add(3) + + go func() { + defer wg.Done() + + for i := range rounds { + mt, err := m.CreateMountTarget(ctx, driver.CreateMountTargetInput{ + FileSystemID: fs.FileSystemID, SubnetID: fmt.Sprintf("subnet-%d", i), + }) + if err != nil { + continue + } + + _ = m.DeleteMountTarget(ctx, mt.MountTargetID) + _ = m.TagResource(ctx, fs.FileSystemID, map[string]string{"k": fmt.Sprint(i)}) + _ = m.TagResource(ctx, ap.AccessPointID, map[string]string{"k": fmt.Sprint(i)}) + clk.Advance(time.Second) + } + }() + + go func() { + defer wg.Done() + + for range rounds { + m.Tick(clk.Now()) + _, _ = m.DescribeFileSystems(ctx, fs.FileSystemID, "") + } + }() + + go func() { + defer wg.Done() + + for range rounds { + if _, err := m.Snapshot(ctx, false); err != nil { + t.Errorf("Snapshot: %v", err) + + return + } + } + }() + + wg.Wait() +} + +// TestCreateAccessPointRacesDeleteFileSystem checks the lifecycle check and the +// store in CreateAccessPoint are atomic: when DeleteFileSystem and +// CreateAccessPoint race, either the delete sees the access point +// (FileSystemInUse) or the create sees the file system "deleting", never both +// succeeding with the access point left on a file system that is then reaped. +func TestCreateAccessPointRacesDeleteFileSystem(t *testing.T) { + t.Run("async", func(t *testing.T) { + m, clk := newAsyncMock(t) + raceAccessPointAgainstDelete(t, m, clk) + }) + + t.Run("sync", func(t *testing.T) { + clk := config.NewFakeClock(time.Unix(1_700_000_000, 0)) + raceAccessPointAgainstDelete(t, efs.New(config.NewOptions(config.WithClock(clk))), clk) + }) +} + +func raceAccessPointAgainstDelete(t *testing.T, m *efs.Mock, clk *config.FakeClock) { + t.Helper() + + ctx := context.Background() + + for i := range 500 { + fs, err := m.CreateFileSystem(ctx, driver.CreateFileSystemInput{CreationToken: fmt.Sprintf("tok-%d", i)}) + if err != nil { + t.Fatalf("CreateFileSystem: %v", err) + } + + clk.Advance(time.Minute) + + var ( + wg sync.WaitGroup + apErr, delEr error + ap *driver.AccessPoint + ) + + wg.Add(2) + + go func() { + defer wg.Done() + + ap, apErr = m.CreateAccessPoint(ctx, driver.CreateAccessPointInput{FileSystemID: fs.FileSystemID}) + }() + + go func() { + defer wg.Done() + + delEr = m.DeleteFileSystem(ctx, fs.FileSystemID) + }() + + wg.Wait() + + if apErr == nil && delEr == nil { + t.Fatalf("round %d: CreateAccessPoint (%s) and DeleteFileSystem both succeeded", i, ap.AccessPointID) + } + } +} + +// TestUpdateFileSystemProtectionRejectsReplicating checks REPLICATING, the state +// EFS sets on a replication destination, is not accepted from a caller. +func TestUpdateFileSystemProtectionRejectsReplicating(t *testing.T) { + ctx := context.Background() + m := newMock(t) + + fs, err := m.CreateFileSystem(ctx, driver.CreateFileSystemInput{CreationToken: "tok"}) + if err != nil { + t.Fatalf("CreateFileSystem: %v", err) + } + + if _, err = m.UpdateFileSystemProtection(ctx, fs.FileSystemID, driver.ProtectionReplicating); !errors.IsInvalidArgument(err) { + t.Fatalf("REPLICATING: want InvalidArgument (BadRequest), got %v", err) + } + + desc, err := m.DescribeFileSystems(ctx, fs.FileSystemID, "") + if err != nil || desc[0].Protection.ReplicationOverwriteProtection != driver.ProtectionEnabled { + t.Fatalf("protection after rejected update = %+v, %v; want ENABLED", desc, err) + } +} + +// TestTickReportsReap checks Tick reports a reap as a state change (so serve +// persistence saves it) even when no metric is due or no CloudWatch is wired, +// and reports nothing once there is nothing left to do. +func TestTickReportsReap(t *testing.T) { + ctx := context.Background() + m, clk := newAsyncMock(t) // no monitoring wired: no metric is ever published + + fs, err := m.CreateFileSystem(ctx, driver.CreateFileSystemInput{CreationToken: "tok"}) + if err != nil { + t.Fatalf("CreateFileSystem: %v", err) + } + + clk.Advance(time.Minute) + m.Tick(clk.Now()) + + if m.Tick(clk.Now()) { + t.Fatal("idle Tick reported a change") + } + + mt, err := m.CreateMountTarget(ctx, driver.CreateMountTargetInput{FileSystemID: fs.FileSystemID, SubnetID: "subnet-1"}) + if err != nil { + t.Fatalf("CreateMountTarget: %v", err) + } + + clk.Advance(time.Minute) + m.Tick(clk.Now()) // drops the elapsed create window + + if err = m.DeleteMountTarget(ctx, mt.MountTargetID); err != nil { + t.Fatalf("DeleteMountTarget: %v", err) + } + + clk.Advance(time.Minute) + + if !m.Tick(clk.Now()) { + t.Fatal("Tick that reaped a mount target reported no change") + } + + if err = m.DeleteFileSystem(ctx, fs.FileSystemID); err != nil { + t.Fatalf("DeleteFileSystem: %v", err) + } + + clk.Advance(time.Minute) + + if !m.Tick(clk.Now()) { + t.Fatal("Tick that reaped a file system reported no change") + } + + if m.Tick(clk.Now()) { + t.Fatal("Tick after everything was reaped reported a change") + } +} + +// TestRestoredEmptyFileSystemTakesMountTargets checks a file system snapshotted +// with no mount targets or access points (their maps are dropped by omitempty) +// can take new ones after Restore instead of panicking on a nil map. +func TestRestoredEmptyFileSystemTakesMountTargets(t *testing.T) { + ctx := context.Background() + src := newMock(t) + + fs, err := src.CreateFileSystem(ctx, driver.CreateFileSystemInput{CreationToken: "tok"}) + if err != nil { + t.Fatalf("CreateFileSystem: %v", err) + } + + data, err := src.Snapshot(ctx, false) + if err != nil { + t.Fatalf("Snapshot: %v", err) + } + + dst := efs.New(config.NewOptions(config.WithClock(config.NewFakeClock(time.Unix(0, 0))))) + if err = dst.Restore(ctx, data); err != nil { + t.Fatalf("Restore: %v", err) + } + + if _, err = dst.CreateMountTarget(ctx, driver.CreateMountTargetInput{ + FileSystemID: fs.FileSystemID, SubnetID: "subnet-1", + }); err != nil { + t.Fatalf("CreateMountTarget after restore: %v", err) + } + + if _, err = dst.CreateAccessPoint(ctx, driver.CreateAccessPointInput{FileSystemID: fs.FileSystemID}); err != nil { + t.Fatalf("CreateAccessPoint after restore: %v", err) + } +} diff --git a/providers/aws/efs/snapshot.go b/providers/aws/efs/snapshot.go index 214c0d2d6..04a7d2493 100644 --- a/providers/aws/efs/snapshot.go +++ b/providers/aws/efs/snapshot.go @@ -4,6 +4,8 @@ import ( "context" "encoding/json" "fmt" + "maps" + "time" "github.com/stackshy/cloudemu/v2/internal/snapshot" "github.com/stackshy/cloudemu/v2/services/efs/driver" @@ -37,6 +39,15 @@ type fsDataSnapshot struct { AccessPts map[string]*driver.AccessPoint `json:"accessPts,omitempty"` Lifecycle []driver.LifecyclePolicy `json:"lifecycle,omitempty"` Replication *driver.ReplicationConfiguration `json:"replication,omitempty"` + // Lifecycle windows (AsyncSettle): a restored "creating"/"deleting" file + // system or mount target keeps its transient state until the window ends. + ReadyAt time.Time `json:"readyAt,omitzero"` + DeleteAt time.Time `json:"deleteAt,omitzero"` + MTReadyAt map[string]time.Time `json:"mtReadyAt,omitempty"` + MTDeleteAt map[string]time.Time `json:"mtDeleteAt,omitempty"` + // Metric sample times, so a restore keeps the publishing cadence. + StorageSampleAt time.Time `json:"storageSampleAt,omitzero"` + ThroughputSampleAt time.Time `json:"throughputSampleAt,omitzero"` } // Snapshot captures the mock's entire state as JSON. includeAssets is unused. EFS holds no bulk @@ -54,7 +65,7 @@ func (m *Mock) Snapshot(_ context.Context, _ bool) (json.RawMessage, error) { m.ipMu.Lock() if len(m.ipCounters) > 0 { - snap.IPCounters = m.ipCounters + snap.IPCounters = maps.Clone(m.ipCounters) } m.ipMu.Unlock() @@ -71,16 +82,53 @@ func (m *Mock) snapshotFileSystems() map[string]*fsDataSnapshot { for id, d := range m.fileSystems.All() { d.mu.RLock() - out[id] = &fsDataSnapshot{ - FS: d.fs, Policy: d.policy, Backup: d.backup, MountTgts: d.mountTgts, - AccessPts: d.accessPts, Lifecycle: d.lifecycle, Replication: d.replication, - } + out[id] = d.snapshotLocked() d.mu.RUnlock() } return out } +// snapshotLocked copies d into its exported snapshot form. Everything that is +// mutated in place after creation (the mount-target and access-point maps and +// their values, the lifecycle-window maps, and the tag maps) is copied, because +// the snapshot is marshaled after d.mu is released while requests and Tick keep +// writing those. The lifecycle policies and the replication configuration are +// only ever replaced wholesale, so sharing them is safe. Callers hold d.mu. +func (d *fsData) snapshotLocked() *fsDataSnapshot { + fs := d.fs + fs.Tags = maps.Clone(d.fs.Tags) + + var mts map[string]*driver.MountTarget + if d.mountTgts != nil { + mts = make(map[string]*driver.MountTarget, len(d.mountTgts)) + + for id, mt := range d.mountTgts { + c := *mt + mts[id] = &c + } + } + + var aps map[string]*driver.AccessPoint + if d.accessPts != nil { + aps = make(map[string]*driver.AccessPoint, len(d.accessPts)) + + for id, ap := range d.accessPts { + c := *ap + c.Tags = maps.Clone(ap.Tags) + aps[id] = &c + } + } + + return &fsDataSnapshot{ + FS: fs, Policy: d.policy, Backup: d.backup, MountTgts: mts, + AccessPts: aps, Lifecycle: d.lifecycle, Replication: d.replication, + ReadyAt: d.readyAt, DeleteAt: d.deleteAt, + MTReadyAt: maps.Clone(d.mtReadyAt), MTDeleteAt: maps.Clone(d.mtDeleteAt), + StorageSampleAt: d.storageSampleAt, ThroughputSampleAt: d.throughputSampleAt, + } +} + func (m *Mock) snapshotIndexes(snap *efsSnapshot) error { dumps := []struct { dst *json.RawMessage @@ -113,9 +161,21 @@ func (m *Mock) Restore(_ context.Context, data json.RawMessage) error { } for id, s := range snap.FileSystems { + // omitempty drops an empty map, but the live code writes into these + // maps without a nil check, so restore them as empty, not nil. + if s.MountTgts == nil { + s.MountTgts = map[string]*driver.MountTarget{} + } + + if s.AccessPts == nil { + s.AccessPts = map[string]*driver.AccessPoint{} + } + m.fileSystems.Set(id, &fsData{ fs: s.FS, policy: s.Policy, backup: s.Backup, mountTgts: s.MountTgts, accessPts: s.AccessPts, lifecycle: s.Lifecycle, replication: s.Replication, + readyAt: s.ReadyAt, deleteAt: s.DeleteAt, mtReadyAt: s.MTReadyAt, mtDeleteAt: s.MTDeleteAt, + storageSampleAt: s.StorageSampleAt, throughputSampleAt: s.ThroughputSampleAt, }) } diff --git a/providers/aws/tickables_test.go b/providers/aws/tickables_test.go index 15d0d66df..08d4b72b0 100644 --- a/providers/aws/tickables_test.go +++ b/providers/aws/tickables_test.go @@ -8,14 +8,15 @@ import ( ) // TestTickablesRegistersTimeDrivenServices checks the serve ticker reaches the -// region's own CloudWatch, SSM and CloudFormation, so due alarms, parameter policies and -// settling stack operations run without a read. +// region's own EFS, CloudWatch, SSM and CloudFormation, so EFS metrics, due +// alarms, parameter policies and settling stack operations run without a read. +// EFS comes before CloudWatch so alarms see the metrics published in the same tick. func TestTickablesRegistersTimeDrivenServices(t *testing.T) { p := aws.New() got := p.Tickables() - if len(got) != 3 || got[0] != config.Tickable(p.CloudWatch) || got[1] != config.Tickable(p.SSM) || - got[2] != config.Tickable(p.CloudFormation) { - t.Fatalf("Tickables() = %v, want this provider's CloudWatch, SSM and CloudFormation", got) + if len(got) != 4 || got[0] != config.Tickable(p.EFS) || got[1] != config.Tickable(p.CloudWatch) || + got[2] != config.Tickable(p.SSM) || got[3] != config.Tickable(p.CloudFormation) { + t.Fatalf("Tickables() = %v, want this provider's EFS, CloudWatch, SSM and CloudFormation", got) } } diff --git a/server/aws/efs/errors.go b/server/aws/efs/errors.go index 3f825b969..d45b4051a 100644 --- a/server/aws/efs/errors.go +++ b/server/aws/efs/errors.go @@ -84,6 +84,10 @@ func exceptionFor(kind string, err error) (status int, errType string) { return http.StatusBadRequest, "BadRequest" case cerrors.IsFailedPrecondition(err): + if kind == driver.KindFileSystemState { + return http.StatusConflict, "IncorrectFileSystemLifeCycleState" + } + return http.StatusConflict, "FileSystemInUse" default: return http.StatusInternalServerError, "InternalServerError" diff --git a/server/aws/efs/file_systems.go b/server/aws/efs/file_systems.go index 8344101a9..0ce36fedd 100644 --- a/server/aws/efs/file_systems.go +++ b/server/aws/efs/file_systems.go @@ -45,7 +45,7 @@ func (h *Handler) serveFileSystemByID(w http.ResponseWriter, r *http.Request, id } // serveFileSystemSub routes /file-systems/{id}/{sub}: policy, lifecycle- -// configuration, backup-policy, and replication-configuration. +// configuration, backup-policy, replication-configuration, and protection. func (h *Handler) serveFileSystemSub(w http.ResponseWriter, r *http.Request, id, sub string) { switch sub { case "policy": @@ -56,6 +56,13 @@ func (h *Handler) serveFileSystemSub(w http.ResponseWriter, r *http.Request, id, h.serveBackupPolicy(w, r, id) case "replication-configuration": h.serveReplicationConfig(w, r, id) + case "protection": + if r.Method != http.MethodPut { + methodNotAllowed(w) + return + } + + h.updateFileSystemProtection(w, r, id) default: notFound(w, r.URL.Path) } @@ -211,3 +218,20 @@ func (h *Handler) deleteFileSystemPolicy(w http.ResponseWriter, r *http.Request, writeStatus(w, http.StatusOK) } + +// updateFileSystemProtection serves UpdateFileSystemProtection +// (PUT /file-systems/{id}/protection), returning the resulting protection. +func (h *Handler) updateFileSystemProtection(w http.ResponseWriter, r *http.Request, id string) { + var req fileSystemProtectionJSON + if !decodeJSON(w, r, &req) { + return + } + + p, err := h.efs.UpdateFileSystemProtection(r.Context(), id, req.ReplicationOverwriteProtection) + if err != nil { + writeErr(w, err) + return + } + + writeJSON(w, fileSystemProtectionJSON{ReplicationOverwriteProtection: p.ReplicationOverwriteProtection}) +} diff --git a/server/aws/efs/lifecycle_sdk_test.go b/server/aws/efs/lifecycle_sdk_test.go new file mode 100644 index 000000000..49924e764 --- /dev/null +++ b/server/aws/efs/lifecycle_sdk_test.go @@ -0,0 +1,173 @@ +package efs_test + +import ( + "context" + "errors" + "net/http/httptest" + "testing" + "time" + + "github.com/aws/aws-sdk-go-v2/aws" + awsconfig "github.com/aws/aws-sdk-go-v2/config" + "github.com/aws/aws-sdk-go-v2/credentials" + awsefs "github.com/aws/aws-sdk-go-v2/service/efs" + efstypes "github.com/aws/aws-sdk-go-v2/service/efs/types" + + cloudemu "github.com/stackshy/cloudemu/v2" + "github.com/stackshy/cloudemu/v2/config" + awsserver "github.com/stackshy/cloudemu/v2/server/aws" +) + +// newAsyncEFSClient serves EFS from a provider with AsyncSettle on a FakeClock, +// so the creating/deleting lifecycle is observable and advanceable. +func newAsyncEFSClient(t *testing.T) (*awsefs.Client, *config.FakeClock) { + t.Helper() + + clk := config.NewFakeClock(time.Unix(1_700_000_000, 0)) + cloud := cloudemu.NewAWS(config.WithClock(clk), config.WithAsyncSettle()) + + ts := httptest.NewServer(awsserver.New(awsserver.Drivers{EFS: cloud.EFS})) + t.Cleanup(ts.Close) + + cfg, err := awsconfig.LoadDefaultConfig(context.Background(), + awsconfig.WithRegion("us-east-1"), + awsconfig.WithCredentialsProvider(credentials.NewStaticCredentialsProvider("test", "test", "")), + ) + if err != nil { + t.Fatalf("aws config: %v", err) + } + + return awsefs.NewFromConfig(cfg, func(o *awsefs.Options) { + o.BaseEndpoint = aws.String(ts.URL) + }), clk +} + +func TestSDKUpdateFileSystemProtection(t *testing.T) { + ctx := context.Background() + c := newEFSClient(t) + + fs, err := c.CreateFileSystem(ctx, &awsefs.CreateFileSystemInput{CreationToken: aws.String("prot-1")}) + if err != nil { + t.Fatalf("CreateFileSystem: %v", err) + } + + out, err := c.UpdateFileSystemProtection(ctx, &awsefs.UpdateFileSystemProtectionInput{ + FileSystemId: fs.FileSystemId, + ReplicationOverwriteProtection: efstypes.ReplicationOverwriteProtectionDisabled, + }) + if err != nil { + t.Fatalf("UpdateFileSystemProtection: %v", err) + } + + if out.ReplicationOverwriteProtection != efstypes.ReplicationOverwriteProtectionDisabled { + t.Fatalf("response protection = %s, want DISABLED", out.ReplicationOverwriteProtection) + } + + desc, err := c.DescribeFileSystems(ctx, &awsefs.DescribeFileSystemsInput{FileSystemId: fs.FileSystemId}) + if err != nil { + t.Fatalf("DescribeFileSystems: %v", err) + } + + if p := desc.FileSystems[0].FileSystemProtection; p == nil || + p.ReplicationOverwriteProtection != efstypes.ReplicationOverwriteProtectionDisabled { + t.Fatalf("described protection = %+v, want DISABLED", p) + } + + var nf *efstypes.FileSystemNotFound + + _, err = c.UpdateFileSystemProtection(ctx, &awsefs.UpdateFileSystemProtectionInput{ + FileSystemId: aws.String("fs-0123456789abcdef0"), + ReplicationOverwriteProtection: efstypes.ReplicationOverwriteProtectionEnabled, + }) + if !errors.As(err, &nf) { + t.Fatalf("unknown fs: want FileSystemNotFound, got %v", err) + } + + var bad *efstypes.BadRequest + + _, err = c.UpdateFileSystemProtection(ctx, &awsefs.UpdateFileSystemProtectionInput{ + FileSystemId: fs.FileSystemId, + ReplicationOverwriteProtection: "BOGUS", + }) + if !errors.As(err, &bad) { + t.Fatalf("invalid value: want BadRequest, got %v", err) + } + + // REPLICATING is set by EFS on a replication destination, not by callers. + _, err = c.UpdateFileSystemProtection(ctx, &awsefs.UpdateFileSystemProtectionInput{ + FileSystemId: fs.FileSystemId, + ReplicationOverwriteProtection: efstypes.ReplicationOverwriteProtectionReplicating, + }) + if !errors.As(err, &bad) { + t.Fatalf("REPLICATING: want BadRequest, got %v", err) + } +} + +// TestSDKAsyncLifecycle drives the creating -> available -> deleting -> gone +// lifecycle over the wire, including IncorrectFileSystemLifeCycleState. +func TestSDKAsyncLifecycle(t *testing.T) { + ctx := context.Background() + c, clk := newAsyncEFSClient(t) + + fs, err := c.CreateFileSystem(ctx, &awsefs.CreateFileSystemInput{CreationToken: aws.String("async-1")}) + if err != nil { + t.Fatalf("CreateFileSystem: %v", err) + } + + if fs.LifeCycleState != efstypes.LifeCycleStateCreating { + t.Fatalf("create state = %s, want creating", fs.LifeCycleState) + } + + var incorrect *efstypes.IncorrectFileSystemLifeCycleState + + _, err = c.CreateMountTarget(ctx, &awsefs.CreateMountTargetInput{ + FileSystemId: fs.FileSystemId, SubnetId: aws.String("subnet-0123456789abcdef0"), + }) + if !errors.As(err, &incorrect) { + t.Fatalf("CreateMountTarget on creating fs: want IncorrectFileSystemLifeCycleState, got %v", err) + } + + clk.Advance(time.Minute) + + mt, err := c.CreateMountTarget(ctx, &awsefs.CreateMountTargetInput{ + FileSystemId: fs.FileSystemId, SubnetId: aws.String("subnet-0123456789abcdef0"), + }) + if err != nil { + t.Fatalf("CreateMountTarget: %v", err) + } + + if mt.LifeCycleState != efstypes.LifeCycleStateCreating { + t.Fatalf("mount target create state = %s, want creating", mt.LifeCycleState) + } + + clk.Advance(time.Minute) + + if _, err = c.DeleteMountTarget(ctx, &awsefs.DeleteMountTargetInput{MountTargetId: mt.MountTargetId}); err != nil { + t.Fatalf("DeleteMountTarget: %v", err) + } + + mts, err := c.DescribeMountTargets(ctx, &awsefs.DescribeMountTargetsInput{MountTargetId: mt.MountTargetId}) + if err != nil || mts.MountTargets[0].LifeCycleState != efstypes.LifeCycleStateDeleting { + t.Fatalf("mount target after delete = %+v, %v; want deleting", mts, err) + } + + clk.Advance(time.Minute) + + if _, err = c.DeleteFileSystem(ctx, &awsefs.DeleteFileSystemInput{FileSystemId: fs.FileSystemId}); err != nil { + t.Fatalf("DeleteFileSystem: %v", err) + } + + desc, err := c.DescribeFileSystems(ctx, &awsefs.DescribeFileSystemsInput{FileSystemId: fs.FileSystemId}) + if err != nil || desc.FileSystems[0].LifeCycleState != efstypes.LifeCycleStateDeleting { + t.Fatalf("fs after delete = %+v, %v; want deleting", desc, err) + } + + clk.Advance(time.Minute) + + var nf *efstypes.FileSystemNotFound + + _, err = c.DescribeFileSystems(ctx, &awsefs.DescribeFileSystemsInput{FileSystemId: fs.FileSystemId}) + if !errors.As(err, &nf) { + t.Fatalf("fs after delete settle: want FileSystemNotFound, got %v", err) + } +} diff --git a/server/aws/efs/prefs_protection_sdk_test.go b/server/aws/efs/prefs_protection_sdk_test.go new file mode 100644 index 000000000..0ec779711 --- /dev/null +++ b/server/aws/efs/prefs_protection_sdk_test.go @@ -0,0 +1,65 @@ +package efs_test + +import ( + "context" + "errors" + "testing" + + "github.com/aws/aws-sdk-go-v2/aws" + awsefs "github.com/aws/aws-sdk-go-v2/service/efs" + efstypes "github.com/aws/aws-sdk-go-v2/service/efs/types" +) + +// TestSDKAccountPreferencesDefault checks an account that never set a +// preference reports LONG_ID for file systems and mount targets, and that +// SHORT_ID can no longer be set. +func TestSDKAccountPreferencesDefault(t *testing.T) { + ctx := context.Background() + c := newEFSClient(t) + + got, err := c.DescribeAccountPreferences(ctx, &awsefs.DescribeAccountPreferencesInput{}) + if err != nil { + t.Fatalf("DescribeAccountPreferences: %v", err) + } + + pref := got.ResourceIdPreference + if pref == nil || pref.ResourceIdType != efstypes.ResourceIdTypeLongId || len(pref.Resources) != 2 || + pref.Resources[0] != efstypes.ResourceFileSystem || pref.Resources[1] != efstypes.ResourceMountTarget { + t.Fatalf("default preference = %+v, want LONG_ID for FILE_SYSTEM and MOUNT_TARGET", pref) + } + + var bad *efstypes.BadRequest + + _, err = c.PutAccountPreferences(ctx, &awsefs.PutAccountPreferencesInput{ResourceIdType: efstypes.ResourceIdTypeShortId}) + if !errors.As(err, &bad) { + t.Fatalf("PutAccountPreferences(SHORT_ID): want BadRequest, got %v", err) + } +} + +// TestSDKProtectionReplicationAlreadyExists checks UpdateFileSystemProtection on +// a replication source or destination returns ReplicationAlreadyExists (409). +func TestSDKProtectionReplicationAlreadyExists(t *testing.T) { + ctx := context.Background() + c := newEFSClient(t) + + src, _ := c.CreateFileSystem(ctx, &awsefs.CreateFileSystemInput{CreationToken: aws.String("rep-src")}) + dst, _ := c.CreateFileSystem(ctx, &awsefs.CreateFileSystemInput{CreationToken: aws.String("rep-dst")}) + + if _, err := c.CreateReplicationConfiguration(ctx, &awsefs.CreateReplicationConfigurationInput{ + SourceFileSystemId: src.FileSystemId, + Destinations: []efstypes.DestinationToCreate{{FileSystemId: dst.FileSystemId}}, + }); err != nil { + t.Fatalf("CreateReplicationConfiguration: %v", err) + } + + for _, id := range []*string{src.FileSystemId, dst.FileSystemId} { + var exists *efstypes.ReplicationAlreadyExists + + _, err := c.UpdateFileSystemProtection(ctx, &awsefs.UpdateFileSystemProtectionInput{ + FileSystemId: id, ReplicationOverwriteProtection: efstypes.ReplicationOverwriteProtectionEnabled, + }) + if !errors.As(err, &exists) { + t.Fatalf("UpdateFileSystemProtection(%s): want ReplicationAlreadyExists, got %v", aws.ToString(id), err) + } + } +} diff --git a/services/efs/driver/driver.go b/services/efs/driver/driver.go index 9286b934b..5daa0b6bf 100644 --- a/services/efs/driver/driver.go +++ b/services/efs/driver/driver.go @@ -39,6 +39,13 @@ type FileSystemSize struct { ValueInStandard int64 } +// Replication-overwrite protection values (FileSystemProtection). +const ( + ProtectionEnabled = "ENABLED" + ProtectionDisabled = "DISABLED" + ProtectionReplicating = "REPLICATING" +) + // FileSystemProtection is the replication-overwrite protection setting. type FileSystemProtection struct { ReplicationOverwriteProtection string // ENABLED | DISABLED | REPLICATING @@ -100,6 +107,11 @@ type EFS interface { DeleteFileSystem(ctx context.Context, fileSystemID string) error DescribeFileSystems(ctx context.Context, fileSystemID, creationToken string) ([]FileSystem, error) UpdateFileSystem(ctx context.Context, in UpdateFileSystemInput) (*FileSystem, error) + // UpdateFileSystemProtection sets the replication-overwrite protection. An + // empty value leaves the current setting unchanged. + UpdateFileSystemProtection( + ctx context.Context, fileSystemID, replicationOverwriteProtection string, + ) (*FileSystemProtection, error) // File system policy. PutFileSystemPolicy(ctx context.Context, fileSystemID, policy string, bypassCheck bool) (string, error) diff --git a/services/efs/driver/errors.go b/services/efs/driver/errors.go index f07dc74d0..7c68277f6 100644 --- a/services/efs/driver/errors.go +++ b/services/efs/driver/errors.go @@ -12,6 +12,9 @@ const ( // KindAvailabilityZone marks a mount target placed outside a One Zone // file system's zone. KindAvailabilityZone = "AvailabilityZone" + // KindFileSystemState marks an operation rejected because the file system + // is not in the "available" lifecycle state (IncorrectFileSystemLifeCycleState). + KindFileSystemState = "FileSystemState" ) // ResourceError tags a canonical cloudemu error with the EFS resource kind it From 040ef08843822f68f44db629ff6f01af6635f5b5 Mon Sep 17 00:00:00 2001 From: Satyam-Trivedi-ZS Date: Sat, 24 Oct 2026 01:37:43 +0530 Subject: [PATCH 25/31] fix(aws-sqs): per-minute queue gauges, in-flight NotVisible, caller SenderId (#1539) * fix(aws-sts): report the role's RoleId in AssumedRoleId AssumeRole, AssumeRoleWithWebIdentity and AssumeRoleWithSAML returned the fixed AssumedRoleId prefix AROACLOUDEMU0000000000 for every role. Real STS returns ":", where the role id is the role's unique RoleId. Use the IAM role's RoleId when the role is known and keep the synthetic prefix only when no IAM driver knows it. GetCallerIdentity and the SQS SenderId derive from this id, so two roles with the same session name no longer collapse onto one identity. Part of #1514 * fix(aws-sqs): publish queue gauges every minute and resolve SenderId Publish the AWS/SQS queue-state gauges (ApproximateNumberOfMessagesVisible, ApproximateNumberOfMessagesNotVisible, ApproximateNumberOfMessagesDelayed, ApproximateAgeOfOldestMessage and, for FIFO queues, ApproximateNumberOfGroupsWithInflightMessages) from a Tick on the shared serve scheduler: one sample per active queue per minute of provider-clock time, where a queue is active while it holds messages or was accessed in the last six hours. A queue whose consumers stopped keeps reporting a growing backlog and age, and each minute holds one sample, so Sum/Average/SampleCount match real SQS. The counters stay on the request paths and every metric is published after the queue lock is released. The access and sample times are part of the SQS snapshot. GetQueueAttributes ApproximateNumberOfMessagesNotVisible now counts in-flight messages only, not delayed ones, matching the API reference and the gauge. SenderId is the caller's IAM unique id (user id, or role id:session for an assumed role) resolved by the shared awsidentity.Resolver, matching GetCallerIdentity's UserId; library and service-to-service sends keep the account id. Part of #1514 --- providers/aws/aws.go | 6 +- providers/aws/sqs/metrics.go | 209 ++++++++++ providers/aws/sqs/metrics_test.go | 524 +++++++++++++++++++++++++ providers/aws/sqs/movetask.go | 2 +- providers/aws/sqs/permissions.go | 4 +- providers/aws/sqs/snapshot.go | 12 +- providers/aws/sqs/sqs.go | 108 ++--- providers/aws/sqs/tags.go | 6 +- providers/aws/tickables_test.go | 14 +- server/aws/aws.go | 19 +- server/aws/sqs/handler.go | 22 ++ server/aws/sqs_metrics_sdk_test.go | 260 ++++++++++++ server/aws/sts/assumed_role_id_test.go | 67 ++++ server/aws/sts/operations.go | 26 +- services/messagequeue/driver/driver.go | 9 + 15 files changed, 1205 insertions(+), 83 deletions(-) create mode 100644 providers/aws/sqs/metrics.go create mode 100644 providers/aws/sqs/metrics_test.go create mode 100644 server/aws/sqs_metrics_sdk_test.go create mode 100644 server/aws/sts/assumed_role_id_test.go diff --git a/providers/aws/aws.go b/providers/aws/aws.go index eb2a1d7b3..8b848a975 100644 --- a/providers/aws/aws.go +++ b/providers/aws/aws.go @@ -637,9 +637,9 @@ func awsDrivers(p *Provider) *resourcediscovery.Drivers { // background ticker calls each one on its interval. New time-driven services // register here. func (p *Provider) Tickables() []config.Tickable { - // EFS runs before CloudWatch so the alarms evaluated in the same tick see - // the AWS/EFS metrics it just published. - return []config.Tickable{p.EFS, p.CloudWatch, p.SSM, p.CloudFormation} + // SQS and EFS run before CloudWatch so the alarms evaluated in the same + // tick see the queue gauges and AWS/EFS metrics they just published. + return []config.Tickable{p.SQS, p.EFS, p.CloudWatch, p.SSM, p.CloudFormation} } // Close tears down any real engines wired into the provider via diff --git a/providers/aws/sqs/metrics.go b/providers/aws/sqs/metrics.go new file mode 100644 index 000000000..f8bc603e9 --- /dev/null +++ b/providers/aws/sqs/metrics.go @@ -0,0 +1,209 @@ +package sqs + +import ( + "context" + "math" + "time" + + mondriver "github.com/stackshy/cloudemu/v2/services/monitoring/driver" +) + +// AWS/SQS queue-state gauges, published on the single QueueName dimension +// (https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-available-cloudwatch-metrics.html). +// +// Real SQS pushes these once a minute for every active queue: one that holds +// messages, or that an action accessed in the last six hours +// (https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/monitoring-using-cloudwatch.html). +// Tick does the same on the shared serve scheduler, so a queue whose consumers +// stopped keeps reporting a growing backlog and age. The counters +// (NumberOfMessagesSent and friends) stay on the request paths. +const ( + metricsNamespace = "AWS/SQS" + unitCount = "Count" + unitSeconds = "Seconds" + + metricVisible = "ApproximateNumberOfMessagesVisible" + metricNotVisible = "ApproximateNumberOfMessagesNotVisible" + metricDelayed = "ApproximateNumberOfMessagesDelayed" + metricOldestAge = "ApproximateAgeOfOldestMessage" + metricGroupsInflight = "ApproximateNumberOfGroupsWithInflightMessages" + + // poisonPillReceiveCount is the receive count from which a standard queue + // moves an undeleted message to the back of the queue and leaves it out of + // ApproximateAgeOfOldestMessage until it is processed. + poisonPillReceiveCount = 3 + + // gaugeInterval is how often an active queue publishes its gauges. + gaugeInterval = time.Minute + // activeWindow is how long an action keeps an empty queue active. + activeWindow = 6 * time.Hour +) + +// queueGauges is one sample of a queue's state gauges. +type queueGauges struct { + visible, notVisible, delayed, groupsInflight int + // oldestAge is the age in whole seconds of the oldest message counted by + // ApproximateAgeOfOldestMessage; hasOldest is false when there is none. + oldestAge float64 + hasOldest bool +} + +// sampleGauges reads the queue's state gauges as of now. Caller holds qd.mu. +func sampleGauges(qd *queueData, now time.Time) queueGauges { + var ( + g queueGauges + oldest time.Time + groups map[string]struct{} + ) + + for _, msg := range qd.messages { + if g.countState(msg, now) && qd.info.FIFO && msg.GroupID != "" { + if groups == nil { + groups = make(map[string]struct{}) + } + + groups[msg.GroupID] = struct{}{} + } + + if countsForAge(qd.info.FIFO, msg) && (!g.hasOldest || msg.SentAt.Before(oldest)) { + oldest = msg.SentAt + g.hasOldest = true + } + } + + g.groupsInflight = len(groups) + + if g.hasOldest { + g.oldestAge = math.Max(0, math.Floor(now.Sub(oldest).Seconds())) + } + + return g +} + +// countState adds msg to the visible, delayed or in-flight count and reports +// whether it is in flight. A message that has never been received and whose +// visibility lies in the future is delayed; one that has been received and is +// still hidden is in flight. +func (g *queueGauges) countState(msg *sqsMessage, now time.Time) (inFlight bool) { + switch { + case !msg.VisibleAt.After(now): + g.visible++ + case msg.ReceiveCount == 0: + g.delayed++ + default: + g.notVisible++ + return true + } + + return false +} + +// countsForAge reports whether msg counts toward ApproximateAgeOfOldestMessage. +// A standard queue leaves out poison-pill messages (received three or more +// times without being deleted); FIFO queues keep order, so every message +// counts. +func countsForAge(fifo bool, msg *sqsMessage) bool { + return fifo || msg.ReceiveCount < poisonPillReceiveCount +} + +// Tick publishes one sample of the state gauges for every active queue whose +// last sample is at least gaugeInterval old at now, and reports whether it +// published anything. It has the config.Tickable signature of the shared serve +// scheduler; library mode never calls it, so tests drive it with a FakeClock. +// Each queue is sampled under its own lock and everything is published after +// all locks are released, because a CloudWatch alarm on a gauge may notify an +// SNS topic that delivers straight back into a queue. +func (m *Mock) Tick(now time.Time) bool { + if m.monitoring == nil { + return false + } + + var data []mondriver.MetricDatum + + for _, qd := range m.queues.SortedValues() { + data = append(data, dueGauges(qd, now)...) + } + + if len(data) == 0 { + return false + } + + _ = m.monitoring.PutMetricData(context.Background(), data) + + return true +} + +// dueGauges samples qd when it is active and its gauges are due at now, and +// records the sample time. It returns nil otherwise. Callers must not hold +// qd.mu. +func dueGauges(qd *queueData, now time.Time) []mondriver.MetricDatum { + qd.mu.Lock() + defer qd.mu.Unlock() + + if !qd.lastSampleAt.IsZero() && now.Sub(qd.lastSampleAt) < gaugeInterval { + return nil + } + + if len(qd.messages) == 0 && now.Sub(qd.lastAccessedAt()) >= activeWindow { + return nil + } + + qd.lastSampleAt = now + + return gaugeData(qd.info.Name, qd.info.FIFO, sampleGauges(qd, now), now) +} + +// gaugeData builds the datums of one gauge sample. +func gaugeData(queue string, fifo bool, g queueGauges, now time.Time) []mondriver.MetricDatum { + dims := map[string]string{"QueueName": queue} + datum := func(metric string, value float64, unit string) mondriver.MetricDatum { + return mondriver.MetricDatum{ + Namespace: metricsNamespace, MetricName: metric, Value: value, Unit: unit, + Dimensions: dims, Timestamp: now, + } + } + + data := []mondriver.MetricDatum{ + datum(metricVisible, float64(g.visible), unitCount), + datum(metricNotVisible, float64(g.notVisible), unitCount), + datum(metricDelayed, float64(g.delayed), unitCount), + } + + // The age is reported only while the queue holds a message it counts. + if g.hasOldest { + data = append(data, datum(metricOldestAge, g.oldestAge, unitSeconds)) + } + + if fifo { + data = append(data, datum(metricGroupsInflight, float64(g.groupsInflight), unitCount)) + } + + return data +} + +// touch records an action on the queue at now. +func (qd *queueData) touch(now time.Time) { + qd.lastAccessed.Store(now.UnixNano()) +} + +// lastAccessedAt is the time of the last action on the queue, or the zero +// time when none was recorded. +func (qd *queueData) lastAccessedAt() time.Time { + n := qd.lastAccessed.Load() + if n == 0 { + return time.Time{} + } + + return time.Unix(0, n).UTC() +} + +// accessQueue looks up the queue at url for an API action and records the +// access, which keeps the queue active for its CloudWatch gauges. +func (m *Mock) accessQueue(url string) (*queueData, bool) { + qd, ok := m.queues.Get(url) + if ok { + qd.touch(m.opts.Clock.Now()) + } + + return qd, ok +} diff --git a/providers/aws/sqs/metrics_test.go b/providers/aws/sqs/metrics_test.go new file mode 100644 index 000000000..12bfbd25d --- /dev/null +++ b/providers/aws/sqs/metrics_test.go @@ -0,0 +1,524 @@ +package sqs + +import ( + "context" + "errors" + "fmt" + "sync" + "sync/atomic" + "testing" + "time" + + "github.com/stackshy/cloudemu/v2/config" + "github.com/stackshy/cloudemu/v2/services/messagequeue/driver" + mondriver "github.com/stackshy/cloudemu/v2/services/monitoring/driver" +) + +// gaugeRecorder is a monitoring backend that keeps every datum it is sent. +// onPut, when set, runs inside PutMetricData, so a test can call back into +// the SQS mock the way a CloudWatch alarm -> SNS -> SQS action would. +type gaugeRecorder struct { + mondriver.Monitoring + + mu sync.Mutex + data []mondriver.MetricDatum + onPut func() +} + +func (r *gaugeRecorder) PutMetricData(_ context.Context, data []mondriver.MetricDatum) error { + r.mu.Lock() + r.data = append(r.data, data...) + onPut := r.onPut + r.mu.Unlock() + + if onPut != nil { + onPut() + } + + return nil +} + +// samples returns every datum of metric for queue, oldest first. +func (r *gaugeRecorder) samples(metric, queue string) []mondriver.MetricDatum { + r.mu.Lock() + defer r.mu.Unlock() + + var out []mondriver.MetricDatum + + for _, d := range r.data { + if d.MetricName == metric && d.Dimensions["QueueName"] == queue { + out = append(out, d) + } + } + + return out +} + +// last returns the most recent datum of metric for queue. +func (r *gaugeRecorder) last(metric, queue string) (mondriver.MetricDatum, bool) { + s := r.samples(metric, queue) + if len(s) == 0 { + return mondriver.MetricDatum{}, false + } + + return s[len(s)-1], true +} + +func (r *gaugeRecorder) reset() { + r.mu.Lock() + r.data = nil + r.mu.Unlock() +} + +func newGaugeMock() (*Mock, *gaugeRecorder, *config.FakeClock) { + m, fc := newTestMock() + rec := &gaugeRecorder{} + m.SetMonitoring(rec) + + return m, rec, fc +} + +func requireGauge(t *testing.T, rec *gaugeRecorder, metric, queue string, want float64, unit string) { + t.Helper() + + d, ok := rec.last(metric, queue) + if !ok { + t.Fatalf("%s for %s was never emitted", metric, queue) + } + + if d.Value != want || d.Unit != unit || d.Namespace != "AWS/SQS" { + t.Fatalf("%s for %s = %v %s (%s), want %v %s (AWS/SQS)", metric, queue, d.Value, d.Unit, d.Namespace, want, unit) + } + + if len(d.Dimensions) != 1 { + t.Fatalf("%s dimensions = %v, want only QueueName", metric, d.Dimensions) + } +} + +func requireNoGauge(t *testing.T, rec *gaugeRecorder, metric, queue string) { + t.Helper() + + if d, ok := rec.last(metric, queue); ok { + t.Fatalf("%s for %s emitted %v, want no datum", metric, queue, d.Value) + } +} + +func TestRequestsPublishCountersButNoGauges(t *testing.T) { + m, rec, _ := newGaugeMock() + ctx := context.Background() + q := createStdQueue(m, "quiet-paths") + + sendN(t, m, q.URL, 3) + + msgs, err := m.ReceiveMessages(ctx, driver.ReceiveMessageInput{QueueURL: q.URL}) + requireNoError(t, err) + requireNoError(t, m.ChangeVisibility(ctx, q.URL, msgs[0].ReceiptHandle, 0)) + requireNoError(t, m.DeleteMessage(ctx, q.URL, msgs[0].ReceiptHandle)) + requireNoError(t, m.PurgeQueue(ctx, q.URL)) + + for _, g := range []string{metricVisible, metricNotVisible, metricDelayed, metricOldestAge} { + requireNoGauge(t, rec, g, "quiet-paths") + } + + if got := len(rec.samples("NumberOfMessagesSent", "quiet-paths")); got != 3 { + t.Fatalf("NumberOfMessagesSent datums = %d, want 3", got) + } +} + +func TestTickPublishesOneSamplePerMinute(t *testing.T) { + m, rec, fc := newGaugeMock() + q := createStdQueue(m, "per-minute") + + // Six sends in the same minute: one sample of 6, not one per send. + sendN(t, m, q.URL, 6) + + if !m.Tick(fc.Now()) { + t.Fatal("first Tick published nothing") + } + + requireGauge(t, rec, metricVisible, "per-minute", 6, unitCount) + + for _, step := range []time.Duration{time.Second, 30 * time.Second, 28 * time.Second} { + fc.Advance(step) + + if m.Tick(fc.Now()) { + t.Fatalf("Tick %v after the sample published again inside the minute", fc.Now()) + } + } + + fc.Advance(time.Second) // a full minute since the first sample + + if !m.Tick(fc.Now()) { + t.Fatal("Tick a minute after the last sample published nothing") + } + + if got := len(rec.samples(metricVisible, "per-minute")); got != 2 { + t.Fatalf("Visible samples = %d, want 2 (one per minute)", got) + } +} + +func TestTickTracksVisibleInFlightDelayedAndAge(t *testing.T) { + m, rec, fc := newGaugeMock() + ctx := context.Background() + q := createStdQueue(m, "gauges") + + _, err := m.SendMessage(ctx, driver.SendMessageInput{QueueURL: q.URL, Body: "a"}) + requireNoError(t, err) + + _, err = m.SendMessage(ctx, driver.SendMessageInput{ + QueueURL: q.URL, Body: "delayed", DelaySeconds: 600, DelaySecondsSet: true, + }) + requireNoError(t, err) + + fc.Advance(10 * time.Second) + + _, err = m.SendMessage(ctx, driver.SendMessageInput{QueueURL: q.URL, Body: "c"}) + requireNoError(t, err) + + msgs, err := m.ReceiveMessages(ctx, driver.ReceiveMessageInput{QueueURL: q.URL, VisibilityTimeout: 300}) + requireNoError(t, err) + assertEqual(t, "a", msgs[0].Body) + + m.Tick(fc.Now()) + + // a is in flight, one message is delayed, c is visible, and the oldest + // message was sent 10s ago. + requireGauge(t, rec, metricVisible, "gauges", 1, unitCount) + requireGauge(t, rec, metricNotVisible, "gauges", 1, unitCount) + requireGauge(t, rec, metricDelayed, "gauges", 1, unitCount) + requireGauge(t, rec, metricOldestAge, "gauges", 10, unitSeconds) + requireNoGauge(t, rec, metricGroupsInflight, "gauges") + + // GetQueueAttributes agrees with the gauges: in flight excludes delayed. + attrs, err := m.GetQueueAttributes(ctx, q.URL) + requireNoError(t, err) + assertEqual(t, 1, attrs.ApproximateMessageCount) + assertEqual(t, 1, attrs.ApproximateNotVisibleCount) + assertEqual(t, 1, attrs.ApproximateDelayedCount) +} + +// TestTickStuckQueueKeepsReportingAndAges is the stopped-consumer case: after +// a single delayed send nothing touches the queue, and the gauges still show +// the message becoming visible and getting older every minute. +func TestTickStuckQueueKeepsReportingAndAges(t *testing.T) { + m, rec, fc := newGaugeMock() + ctx := context.Background() + q := createStdQueue(m, "stuck") + + _, err := m.SendMessage(ctx, driver.SendMessageInput{ + QueueURL: q.URL, Body: "late", DelaySeconds: 5, DelaySecondsSet: true, + }) + requireNoError(t, err) + + m.Tick(fc.Now()) + requireGauge(t, rec, metricVisible, "stuck", 0, unitCount) + requireGauge(t, rec, metricDelayed, "stuck", 1, unitCount) + requireGauge(t, rec, metricOldestAge, "stuck", 0, unitSeconds) + + fc.Advance(time.Minute) + m.Tick(fc.Now()) + requireGauge(t, rec, metricVisible, "stuck", 1, unitCount) + requireGauge(t, rec, metricDelayed, "stuck", 0, unitCount) + requireGauge(t, rec, metricOldestAge, "stuck", 60, unitSeconds) + + // A queue holding messages stays active long past the six-hour window. + for i := 0; i < 8*60; i++ { + fc.Advance(time.Minute) + m.Tick(fc.Now()) + } + + requireGauge(t, rec, metricOldestAge, "stuck", (8*60+1)*60, unitSeconds) + + if got := len(rec.samples(metricVisible, "stuck")); got != 8*60+2 { + t.Fatalf("Visible samples = %d, want %d (one per minute)", got, 8*60+2) + } +} + +func TestTickStopsForEmptyQueueIdleSixHours(t *testing.T) { + m, rec, fc := newGaugeMock() + ctx := context.Background() + q := createStdQueue(m, "idle") + + if !m.Tick(fc.Now()) { + t.Fatal("a just-created queue is active and should publish") + } + + fc.Advance(activeWindow - time.Minute) + + if !m.Tick(fc.Now()) { + t.Fatal("an empty queue accessed under six hours ago should still publish") + } + + fc.Advance(time.Minute) + rec.reset() + + if m.Tick(fc.Now()) { + t.Fatal("an empty queue idle for six hours should stop publishing") + } + + // Any action wakes it up again. + _, err := m.GetQueueAttributes(ctx, q.URL) + requireNoError(t, err) + + fc.Advance(time.Minute) + + if !m.Tick(fc.Now()) { + t.Fatal("an accessed queue should publish again") + } + + requireGauge(t, rec, metricVisible, "idle", 0, unitCount) + requireNoGauge(t, rec, metricOldestAge, "idle") +} + +func TestTickAgeSkipsStandardPoisonPills(t *testing.T) { + m, rec, fc := newGaugeMock() + ctx := context.Background() + q := createStdQueue(m, "poison") + + _, err := m.SendMessage(ctx, driver.SendMessageInput{QueueURL: q.URL, Body: "poison"}) + requireNoError(t, err) + + fc.Advance(100 * time.Second) + + receive := func() { + t.Helper() + + msgs, rerr := m.ReceiveMessages(ctx, driver.ReceiveMessageInput{ + QueueURL: q.URL, VisibilityTimeout: 0, VisibilityTimeoutSet: true, + }) + requireNoError(t, rerr) + assertEqual(t, 1, len(msgs)) + } + + receive() + receive() + m.Tick(fc.Now()) + requireGauge(t, rec, metricOldestAge, "poison", 100, unitSeconds) + + // The third receive makes it a poison pill: nothing else is queued, so + // there is no age to report. + receive() + rec.reset() + fc.Advance(time.Minute) + m.Tick(fc.Now()) + requireGauge(t, rec, metricVisible, "poison", 1, unitCount) + requireNoGauge(t, rec, metricOldestAge, "poison") +} + +func TestTickFIFOCountsInflightGroups(t *testing.T) { + m, rec, fc := newGaugeMock() + ctx := context.Background() + + q, err := m.CreateQueue(ctx, driver.QueueConfig{Name: "orders.fifo", FIFO: true, ContentBasedDeduplication: true}) + requireNoError(t, err) + + for i, g := range []string{"g1", "g1", "g2", "g3"} { + _, err = m.SendMessage(ctx, driver.SendMessageInput{QueueURL: q.URL, Body: fmt.Sprintf("%s-%d", g, i), GroupID: g}) + requireNoError(t, err) + } + + fc.Advance(30 * time.Second) + + msgs, err := m.ReceiveMessages(ctx, driver.ReceiveMessageInput{QueueURL: q.URL, MaxMessages: 10, VisibilityTimeout: 300}) + requireNoError(t, err) + assertEqual(t, 4, len(msgs)) + + m.Tick(fc.Now()) + requireGauge(t, rec, metricGroupsInflight, "orders.fifo", 3, unitCount) + requireGauge(t, rec, metricNotVisible, "orders.fifo", 4, unitCount) + requireGauge(t, rec, metricOldestAge, "orders.fifo", 30, unitSeconds) + + for _, msg := range msgs[:2] { + requireNoError(t, m.DeleteMessage(ctx, q.URL, msg.ReceiptHandle)) + } + + fc.Advance(time.Minute) + m.Tick(fc.Now()) + requireGauge(t, rec, metricGroupsInflight, "orders.fifo", 2, unitCount) + requireGauge(t, rec, metricNotVisible, "orders.fifo", 2, unitCount) +} + +func TestTickFollowsDeadLetterRedriveAndMoveTask(t *testing.T) { + m, rec, fc := newGaugeMock() + ctx := context.Background() + + dlq := createStdQueue(m, "dlq") + src, err := m.CreateQueue(ctx, driver.QueueConfig{Name: "src", RedrivePolicy: redrivePolicy(dlq.ARN, 1)}) + requireNoError(t, err) + + sendN(t, m, src.URL, 1) + fc.Advance(50 * time.Second) + + receive := func() { + t.Helper() + + _, rerr := m.ReceiveMessages(ctx, driver.ReceiveMessageInput{QueueURL: src.URL, VisibilityTimeout: 1}) + requireNoError(t, rerr) + } + + receive() + fc.Advance(2 * time.Second) + receive() // exceeds maxReceiveCount=1: the message moves to the DLQ + + m.Tick(fc.Now()) + requireGauge(t, rec, metricVisible, "src", 0, unitCount) + requireGauge(t, rec, metricVisible, "dlq", 1, unitCount) + // The age resets when a message is moved to a DLQ. + requireGauge(t, rec, metricOldestAge, "dlq", 0, unitSeconds) + + fc.Advance(time.Minute) + requireGaugeAfterMove(t, m, rec, fc, dlq.ARN) +} + +func requireGaugeAfterMove(t *testing.T, m *Mock, rec *gaugeRecorder, fc *config.FakeClock, dlqARN string) { + t.Helper() + + _, err := m.StartMessageMoveTask(context.Background(), dlqARN, "", 0) + requireNoError(t, err) + + m.Tick(fc.Now()) + requireGauge(t, rec, metricVisible, "dlq", 0, unitCount) + requireGauge(t, rec, metricVisible, "src", 1, unitCount) +} + +// TestTickPublishesOutsideQueueLocks calls back into the queues from inside +// PutMetricData, as a CloudWatch alarm whose SNS action delivers to a queue +// would. Publishing while holding a queue lock would deadlock here. +func TestTickPublishesOutsideQueueLocks(t *testing.T) { + m, rec, fc := newGaugeMock() + ctx := context.Background() + q := createStdQueue(m, "reentrant") + sendN(t, m, q.URL, 1) + + var called atomic.Bool + + // Only the first publish calls back, or the SendMessage counter it emits + // would call back again forever. + rec.onPut = func() { + if called.CompareAndSwap(false, true) { + _, _ = m.GetQueueAttributes(ctx, q.URL) + _, _ = m.SendMessage(ctx, driver.SendMessageInput{QueueURL: q.URL, Body: "from-alarm"}) + } + } + + done := make(chan struct{}) + + go func() { + defer close(done) + + m.Tick(fc.Now()) + + msgs, err := m.ReceiveMessages(ctx, driver.ReceiveMessageInput{QueueURL: q.URL}) + if err == nil && len(msgs) == 1 { + _ = m.DeleteMessage(ctx, q.URL, msgs[0].ReceiptHandle) + } + + _, _ = m.ReceiveMessagesWithOptions(ctx, q.URL, driver.ReceiveOptions{}) + }() + + select { + case <-done: + case <-time.After(5 * time.Second): + t.Fatal("publishing a metric deadlocked on a queue lock") + } +} + +func TestTickWithoutMonitoringIsNoOp(t *testing.T) { + m, fc := newTestMock() + q := createStdQueue(m, "no-backend") + sendN(t, m, q.URL, 1) + + if m.Tick(fc.Now()) { + t.Fatal("Tick without a monitoring backend reported a change") + } +} + +func TestSnapshotKeepsGaugeSchedule(t *testing.T) { + ctx := context.Background() + src, rec, fc := newGaugeMock() + createStdQueue(src, "sched") + + src.Tick(fc.Now()) + requireGauge(t, rec, metricVisible, "sched", 0, unitCount) + + data, err := src.Snapshot(ctx, false) + requireNoError(t, err) + + dst, dstRec, _ := newGaugeMock() + requireNoError(t, dst.Restore(ctx, data)) + + // The restored queue sampled at fc.Now(), so it is not due again yet. + if dst.Tick(fc.Now().Add(30 * time.Second)) { + t.Fatal("restored queue lost its last sample time") + } + + // It was accessed at fc.Now(), so it is still active a minute later. + if !dst.Tick(fc.Now().Add(time.Minute)) { + t.Fatal("restored queue lost its last access time") + } + + requireGauge(t, dstRec, metricVisible, "sched", 0, unitCount) + + // Six hours after that access the empty queue is idle. + if dst.Tick(fc.Now().Add(activeWindow + time.Minute)) { + t.Fatal("restored empty queue should be idle after six hours") + } +} + +var errHandlerFailed = errors.New("handler failed") + +// failingInvoker is an event-source mapping whose handler always fails. +type failingInvoker struct{} + +func (failingInvoker) DeliverEventSourceBatch(context.Context, string, []byte) (bool, error) { + return true, errHandlerFailed +} + +func TestTickFollowsLambdaESMDeadLetterRedrive(t *testing.T) { + m, rec, fc := newGaugeMock() + ctx := context.Background() + + dlq := createStdQueue(m, "esm-dlq") + src, err := m.CreateQueue(ctx, driver.QueueConfig{Name: "esm-src", RedrivePolicy: redrivePolicy(dlq.ARN, 2)}) + requireNoError(t, err) + + m.SetEventSourceInvoker(failingInvoker{}) + + sendN(t, m, src.URL, 1) + m.Tick(fc.Now()) + + requireGauge(t, rec, metricVisible, "esm-dlq", 1, unitCount) + requireGauge(t, rec, metricVisible, "esm-src", 0, unitCount) + requireGauge(t, rec, metricNotVisible, "esm-src", 0, unitCount) +} + +func TestSenderIDFromCallerElseAccount(t *testing.T) { + m, _ := newTestMock() + ctx := context.Background() + q := createStdQueue(m, "sender") + + _, err := m.SendMessage(ctx, driver.SendMessageInput{QueueURL: q.URL, Body: "user", SenderID: "AIDAEXAMPLE123ABC"}) + requireNoError(t, err) + + res, err := m.SendMessageBatch(ctx, q.URL, []driver.BatchSendEntry{ + {ID: "1", Body: "role", SenderID: "AROAEXAMPLE123ABC:session"}, + }) + requireNoError(t, err) + assertEqual(t, 1, len(res.Successful)) + + _, err = m.SendMessage(ctx, driver.SendMessageInput{QueueURL: q.URL, Body: "library"}) + requireNoError(t, err) + + msgs, err := m.ReceiveMessages(ctx, driver.ReceiveMessageInput{QueueURL: q.URL, MaxMessages: 10}) + requireNoError(t, err) + assertEqual(t, 3, len(msgs)) + + got := map[string]string{} + for _, msg := range msgs { + got[msg.Body] = msg.SystemAttributes["SenderId"] + } + + assertEqual(t, "AIDAEXAMPLE123ABC", got["user"]) + assertEqual(t, "AROAEXAMPLE123ABC:session", got["role"]) + assertEqual(t, m.opts.AccountID, got["library"]) +} diff --git a/providers/aws/sqs/movetask.go b/providers/aws/sqs/movetask.go index 8cdf84a03..59e45daec 100644 --- a/providers/aws/sqs/movetask.go +++ b/providers/aws/sqs/movetask.go @@ -50,7 +50,7 @@ func (m *Mock) StartMessageMoveTask(_ context.Context, sourceARN, destARN string } sourceURL := m.urlForARN(sourceARN) - if sourceURL == "" { + if _, ok := m.accessQueue(sourceURL); !ok { return "", errors.Newf(errors.NotFound, "no queue found for source arn %q", sourceARN) } diff --git a/providers/aws/sqs/permissions.go b/providers/aws/sqs/permissions.go index 92655940b..5d00f2982 100644 --- a/providers/aws/sqs/permissions.go +++ b/providers/aws/sqs/permissions.go @@ -11,7 +11,7 @@ import ( // the given SQS actions on the queue, mutating the stored access policy. It is // AWS-specific and used directly by the SQS wire handler. func (m *Mock) AddPermission(_ context.Context, queueURL, label string, accountIDs, actions []string) error { - qd, ok := m.queues.Get(queueURL) + qd, ok := m.accessQueue(queueURL) if !ok { return errors.Newf(errors.NotFound, "queue %q not found", queueURL) } @@ -50,7 +50,7 @@ func (m *Mock) AddPermission(_ context.Context, queueURL, label string, accountI // RemovePermission removes the statement whose Sid equals label. It is // AWS-specific and used directly by the SQS wire handler. func (m *Mock) RemovePermission(_ context.Context, queueURL, label string) error { - qd, ok := m.queues.Get(queueURL) + qd, ok := m.accessQueue(queueURL) if !ok { return errors.Newf(errors.NotFound, "queue %q not found", queueURL) } diff --git a/providers/aws/sqs/snapshot.go b/providers/aws/sqs/snapshot.go index 8d505759f..6e38a52af 100644 --- a/providers/aws/sqs/snapshot.go +++ b/providers/aws/sqs/snapshot.go @@ -48,6 +48,11 @@ type queueSnapshot struct { DeduplicationIndex map[string]time.Time `json:"deduplicationIndex,omitempty"` DLQConfig *driver.DeadLetterConfig `json:"dlqConfig,omitempty"` SeqCounter uint64 `json:"seqCounter,omitempty"` + // LastAccessedAt and LastSampleAt drive the per-minute CloudWatch gauges + // (metrics.go): whether the queue is still active, and when it last + // published. + LastAccessedAt time.Time `json:"lastAccessedAt"` + LastSampleAt time.Time `json:"lastSampleAt"` } // messageSnapshot mirrors sqsMessage, promoting its one unexported field @@ -132,6 +137,7 @@ func snapshotQueue(qd *queueData) *queueSnapshot { FifoThroughputLim: qd.fifoThroughputLim, CreatedAt: qd.createdAt, LastModifiedAt: qd.lastModifiedAt, DeduplicationIndex: qd.deduplicationIndex, DLQConfig: qd.dlqConfig, SeqCounter: qd.seqCounter.Load(), + LastAccessedAt: qd.lastAccessedAt(), LastSampleAt: qd.lastSampleAt, } if len(qd.messages) > 0 { @@ -190,7 +196,11 @@ func restoreQueue(qs *queueSnapshot) *queueData { sqsManagedSSE: qs.SqsManagedSSE, deduplicationScope: qs.DeduplicationScope, fifoThroughputLim: qs.FifoThroughputLim, createdAt: qs.CreatedAt, lastModifiedAt: qs.LastModifiedAt, deduplicationIndex: qs.DeduplicationIndex, - dlqConfig: qs.DLQConfig, + dlqConfig: qs.DLQConfig, lastSampleAt: qs.LastSampleAt, + } + + if !qs.LastAccessedAt.IsZero() { + qd.touch(qs.LastAccessedAt) } if qd.deduplicationIndex == nil { diff --git a/providers/aws/sqs/sqs.go b/providers/aws/sqs/sqs.go index d2c710159..c8b53223a 100644 --- a/providers/aws/sqs/sqs.go +++ b/providers/aws/sqs/sqs.go @@ -87,6 +87,13 @@ type queueData struct { deduplicationIndex map[string]time.Time dlqConfig *driver.DeadLetterConfig seqCounter atomic.Uint64 + + // lastAccessed is the provider-clock time (Unix nanoseconds) of the last + // action on the queue. With the message count it decides whether the + // queue is active and so publishes its CloudWatch gauges (see metrics.go). + lastAccessed atomic.Int64 + // lastSampleAt is when the gauges were last published. Guarded by mu. + lastSampleAt time.Time } // EventSourceInvoker delivers an SQS event-source-mapping batch to whatever @@ -123,7 +130,7 @@ func (m *Mock) emitMetric(metricName string, value float64, unit string, dims ma } _ = m.monitoring.PutMetricData(context.Background(), []mondriver.MetricDatum{{ - Namespace: "AWS/SQS", MetricName: metricName, Value: value, Unit: unit, + Namespace: metricsNamespace, MetricName: metricName, Value: value, Unit: unit, Dimensions: dims, Timestamp: m.opts.Clock.Now(), }}) } @@ -197,7 +204,7 @@ func (m *Mock) CreateQueue(ctx context.Context, cfg driver.QueueConfig) (*driver // CreateQueue is idempotent: re-creating an existing queue with the exact // same attributes returns the existing URL. QueueNameExists is returned only // when the incoming attributes differ from the stored ones. - if existing, ok := m.queues.Get(url); ok { + if existing, ok := m.accessQueue(url); ok { if sameQueueConfig(existing, &cfg) { result := existing.info return &result, nil @@ -255,6 +262,7 @@ func (m *Mock) CreateQueue(ctx context.Context, cfg driver.QueueConfig) (*driver } } + qd.touch(now) m.queues.Set(url, qd) result := info @@ -443,7 +451,7 @@ func (m *Mock) DeleteQueue(_ context.Context, url string) error { // GetQueueInfo retrieves information about an SQS queue by URL. func (m *Mock) GetQueueInfo(_ context.Context, url string) (*driver.QueueInfo, error) { - qd, ok := m.queues.Get(url) + qd, ok := m.accessQueue(url) if !ok { return nil, errors.Newf(errors.NotFound, "queue %q not found", url) } @@ -487,7 +495,7 @@ func (m *Mock) ListQueues(_ context.Context, prefix string) ([]driver.QueueInfo, // //nolint:gocritic // hugeParam: interface method signature cannot be changed. func (m *Mock) SendMessage(ctx context.Context, input driver.SendMessageInput) (*driver.SendMessageOutput, error) { - qd, ok := m.queues.Get(input.QueueURL) + qd, ok := m.accessQueue(input.QueueURL) if !ok { return nil, errors.Newf(errors.NotFound, "queue %q not found", input.QueueURL) } @@ -769,13 +777,24 @@ func (m *Mock) buildStoredMessage(qd *queueData, input *driver.SendMessageInput, Attributes: attrs, MessageAttributes: copyMessageAttributes(input.MessageAttributes), SystemAttributes: systemAttributeStrings(input.SystemAttributes), - SenderID: m.opts.AccountID, + SenderID: senderID(input.SenderID, m.opts.AccountID), SequenceNumber: seqNum, VisibleAt: now.Add(time.Duration(delaySeconds) * time.Second), SentAt: now, } } +// senderID is the SenderId recorded on a sent message: the caller's IAM unique +// id when the wire handler resolved one, else the account id (library callers +// and service-to-service deliveries carry no caller principal). +func senderID(caller, accountID string) string { + if caller != "" { + return caller + } + + return accountID +} + // systemAttributeStrings flattens caller-supplied SQS message system attributes // (e.g. AWSTraceHeader) into a name->string map for storage. Only the string // value is retained; system attributes are always String-typed in SQS. @@ -868,7 +887,7 @@ func findDuplicate(qd *queueData, input *driver.SendMessageInput, now time.Time) // When WaitTimeSeconds (or the queue's ReceiveMessageWaitTimeSeconds default) is > 0, it long-polls: // it retries on a short interval until a message is available, the deadline elapses, or ctx is canceled. func (m *Mock) ReceiveMessages(ctx context.Context, input driver.ReceiveMessageInput) ([]driver.Message, error) { - qd, ok := m.queues.Get(input.QueueURL) + qd, ok := m.accessQueue(input.QueueURL) if !ok { return nil, errors.Newf(errors.NotFound, "queue %q not found", input.QueueURL) } @@ -1125,7 +1144,7 @@ func (m *Mock) moveToDLQ(dlqURL, sourceURL string, msg *sqsMessage) { // DeleteMessage deletes a message from the specified queue using its receipt handle. func (m *Mock) DeleteMessage(_ context.Context, queueURL, receiptHandle string) error { - qd, ok := m.queues.Get(queueURL) + qd, ok := m.accessQueue(queueURL) if !ok { return errors.Newf(errors.NotFound, "queue %q not found", queueURL) } @@ -1138,19 +1157,28 @@ func (m *Mock) DeleteMessage(_ context.Context, queueURL, receiptHandle string) } qd.mu.Lock() - defer qd.mu.Unlock() + + deleted := false for i, msg := range qd.messages { if msg.ReceiptHandle == receiptHandle { qd.messages = append(qd.messages[:i], qd.messages[i+1:]...) - m.emitMetric("NumberOfMessagesDeleted", 1, "Count", map[string]string{"QueueName": qd.info.Name}) + deleted = true - return nil + break } } + qd.mu.Unlock() + // DeleteMessage is idempotent: an old/stale (but well-formed) receipt handle - // against an existing queue succeeds without deleting anything. + // against an existing queue succeeds without deleting anything. The metric + // is published after the lock is released, since an alarm on it may + // deliver back into this queue. + if deleted { + m.emitMetric("NumberOfMessagesDeleted", 1, "Count", map[string]string{"QueueName": qd.info.Name}) + } + return nil } @@ -1178,7 +1206,7 @@ func isWellFormedReceiptHandle(handle string) bool { // ChangeVisibility changes the visibility timeout of a message in the specified queue. func (m *Mock) ChangeVisibility(_ context.Context, queueURL, receiptHandle string, timeout int) error { - qd, ok := m.queues.Get(queueURL) + qd, ok := m.accessQueue(queueURL) if !ok { return errors.Newf(errors.NotFound, "queue %q not found", queueURL) } @@ -1244,6 +1272,7 @@ func batchEntryToSendInput(queue string, entry *driver.BatchSendEntry) driver.Se Attributes: entry.Attributes, MessageAttributes: entry.MessageAttributes, SystemAttributes: entry.SystemAttributes, + SenderID: entry.SenderID, } } @@ -1279,7 +1308,7 @@ func (m *Mock) DeleteMessageBatch( func (m *Mock) ReceiveMessagesWithOptions( _ context.Context, queue string, opts driver.ReceiveOptions, ) ([]driver.Message, error) { - qd, ok := m.queues.Get(queue) + qd, ok := m.accessQueue(queue) if !ok { return nil, errors.Newf(errors.NotFound, "queue %q not found", queue) } @@ -1289,7 +1318,6 @@ func (m *Mock) ReceiveMessagesWithOptions( } qd.mu.Lock() - defer qd.mu.Unlock() maxMsgs := defaultMaxMessages(opts.MaxMessages) @@ -1302,17 +1330,14 @@ func (m *Mock) ReceiveMessagesWithOptions( results, toRemove := m.collectVisibleMessages(qd, maxMsgs, visTimeout, now) removeByIndices(qd, toRemove) + qd.mu.Unlock() if results == nil { results = []driver.Message{} } - dims := map[string]string{"QueueName": qd.info.Name} - if len(results) > 0 { - m.emitMetric("NumberOfMessagesReceived", float64(len(results)), "Count", dims) - } else { - m.emitMetric("NumberOfEmptyReceives", 1.0, "Count", dims) - } + // Published after the lock is released, like ReceiveMessages. + m.emitReceiveMetrics(qd, len(results)) return results, nil } @@ -1321,7 +1346,7 @@ func (m *Mock) ReceiveMessagesWithOptions( func (m *Mock) GetQueueAttributes( _ context.Context, queue string, ) (*driver.QueueAttributes, error) { - qd, ok := m.queues.Get(queue) + qd, ok := m.accessQueue(queue) if !ok { return nil, errors.Newf(errors.NotFound, "queue %q not found", queue) } @@ -1329,9 +1354,11 @@ func (m *Mock) GetQueueAttributes( qd.mu.Lock() defer qd.mu.Unlock() - now := m.opts.Clock.Now() - visible, notVisible := countMessages(qd, now) - delayed := countDelayedMessages(qd, now) + // ApproximateNumberOfMessagesNotVisible counts in-flight messages only + // (received, not yet deleted or expired); delayed messages are reported + // separately, matching the CloudWatch gauges. + g := sampleGauges(qd, m.opts.Clock.Now()) + visible, notVisible, delayed := g.visible, g.notVisible, g.delayed return &driver.QueueAttributes{ DelaySeconds: qd.delaySeconds, @@ -1357,24 +1384,11 @@ func (m *Mock) GetQueueAttributes( }, nil } -// countDelayedMessages counts messages still within their delay window. -func countDelayedMessages(qd *queueData, now time.Time) int { - delayed := 0 - - for _, msg := range qd.messages { - if msg.ReceiveCount == 0 && msg.VisibleAt.After(now) { - delayed++ - } - } - - return delayed -} - // SetQueueAttributes updates the attributes of the specified queue. func (m *Mock) SetQueueAttributes( _ context.Context, queue string, attrs map[string]int, ) error { - qd, ok := m.queues.Get(queue) + qd, ok := m.accessQueue(queue) if !ok { return errors.Newf(errors.NotFound, "queue %q not found", queue) } @@ -1415,7 +1429,7 @@ func applyQueueAttributes(qd *queueData, attrs map[string]int) { // Policy, KmsMasterKeyId) that the numeric SetQueueAttributes path cannot express. // It is AWS-specific and used directly by the SQS wire handler. func (m *Mock) SetQueueAttributesRaw(_ context.Context, queue string, attrs map[string]string) error { - qd, ok := m.queues.Get(queue) + qd, ok := m.accessQueue(queue) if !ok { return errors.Newf(errors.NotFound, "queue %q not found", queue) } @@ -1512,7 +1526,7 @@ func parseNumericAttrs(attrs map[string]string) map[string]int { // ListDeadLetterSourceQueues returns the URLs of queues whose RedrivePolicy // targets the given DLQ URL. func (m *Mock) ListDeadLetterSourceQueues(_ context.Context, dlqURL string) ([]string, error) { - if _, ok := m.queues.Get(dlqURL); !ok { + if _, ok := m.accessQueue(dlqURL); !ok { return nil, errors.Newf(errors.NotFound, "queue %q not found", dlqURL) } @@ -1533,7 +1547,7 @@ func (m *Mock) ListDeadLetterSourceQueues(_ context.Context, dlqURL string) ([]s // PurgeQueue removes all messages from the specified queue. func (m *Mock) PurgeQueue(_ context.Context, queue string) error { - qd, ok := m.queues.Get(queue) + qd, ok := m.accessQueue(queue) if !ok { return errors.Newf(errors.NotFound, "queue %q not found", queue) } @@ -1547,18 +1561,6 @@ func (m *Mock) PurgeQueue(_ context.Context, queue string) error { return nil } -func countMessages(qd *queueData, now time.Time) (visible, notVisible int) { - for _, msg := range qd.messages { - if msg.VisibleAt.After(now) { - notVisible++ - } else { - visible++ - } - } - - return visible, notVisible -} - func removeByIndices(qd *queueData, indices []int) { for i := len(indices) - 1; i >= 0; i-- { idx := indices[i] diff --git a/providers/aws/sqs/tags.go b/providers/aws/sqs/tags.go index 2d3c0f542..598804b61 100644 --- a/providers/aws/sqs/tags.go +++ b/providers/aws/sqs/tags.go @@ -8,7 +8,7 @@ import ( // TagQueue adds or overwrites tags on a queue (SQS TagQueue). func (m *Mock) TagQueue(_ context.Context, queueURL string, tags map[string]string) error { - qd, ok := m.queues.Get(queueURL) + qd, ok := m.accessQueue(queueURL) if !ok { return errors.Newf(errors.NotFound, "queue %q not found", queueURL) } @@ -29,7 +29,7 @@ func (m *Mock) TagQueue(_ context.Context, queueURL string, tags map[string]stri // UntagQueue removes tags by key from a queue (SQS UntagQueue). func (m *Mock) UntagQueue(_ context.Context, queueURL string, keys []string) error { - qd, ok := m.queues.Get(queueURL) + qd, ok := m.accessQueue(queueURL) if !ok { return errors.Newf(errors.NotFound, "queue %q not found", queueURL) } @@ -46,7 +46,7 @@ func (m *Mock) UntagQueue(_ context.Context, queueURL string, keys []string) err // ListQueueTags returns a queue's tags (SQS ListQueueTags). func (m *Mock) ListQueueTags(_ context.Context, queueURL string) (map[string]string, error) { - qd, ok := m.queues.Get(queueURL) + qd, ok := m.accessQueue(queueURL) if !ok { return nil, errors.Newf(errors.NotFound, "queue %q not found", queueURL) } diff --git a/providers/aws/tickables_test.go b/providers/aws/tickables_test.go index 08d4b72b0..2347f0c1a 100644 --- a/providers/aws/tickables_test.go +++ b/providers/aws/tickables_test.go @@ -8,15 +8,17 @@ import ( ) // TestTickablesRegistersTimeDrivenServices checks the serve ticker reaches the -// region's own EFS, CloudWatch, SSM and CloudFormation, so EFS metrics, due -// alarms, parameter policies and settling stack operations run without a read. -// EFS comes before CloudWatch so alarms see the metrics published in the same tick. +// region's own SQS, EFS, CloudWatch, SSM and CloudFormation, so queue gauges, +// EFS metrics, due alarms, parameter policies and settling stack operations run +// without a read. SQS and EFS come before CloudWatch so alarms see the metrics +// published in the same tick. func TestTickablesRegistersTimeDrivenServices(t *testing.T) { p := aws.New() got := p.Tickables() - if len(got) != 4 || got[0] != config.Tickable(p.EFS) || got[1] != config.Tickable(p.CloudWatch) || - got[2] != config.Tickable(p.SSM) || got[3] != config.Tickable(p.CloudFormation) { - t.Fatalf("Tickables() = %v, want this provider's EFS, CloudWatch, SSM and CloudFormation", got) + if len(got) != 5 || got[0] != config.Tickable(p.SQS) || got[1] != config.Tickable(p.EFS) || + got[2] != config.Tickable(p.CloudWatch) || got[3] != config.Tickable(p.SSM) || + got[4] != config.Tickable(p.CloudFormation) { + t.Fatalf("Tickables() = %v, want this provider's SQS, EFS, CloudWatch, SSM and CloudFormation", got) } } diff --git a/server/aws/aws.go b/server/aws/aws.go index 14ac0e0f9..ffe0533fe 100644 --- a/server/aws/aws.go +++ b/server/aws/aws.go @@ -623,6 +623,14 @@ type authzSets struct { func newServer(d Drivers) (*server.Server, authzSets) { srv := server.New() + // STS records the sessions it mints here; EKS reads the cluster creator and + // SQS the message SenderId from it, so all of them see the caller the same + // way. + identities := d.Identities + if identities == nil { + identities = awsidentity.New(d.AccountID, d.IAM) + } + // The auth gate reads X-Amz-Target only for the handlers registered // through rpc, which route on that header. jsonRPC := map[server.Handler]bool{} @@ -673,7 +681,9 @@ func newServer(d Drivers) (*server.Server, authzSets) { // prefix (AmazonSQS.* vs DynamoDB_20120810.*); their Matches predicates // are mutually exclusive. if d.SQS != nil { - srv.Register(sqs.New(d.SQS)) + sqsHandler := sqs.New(d.SQS) + sqsHandler.SetIdentities(identities) + srv.Register(sqsHandler) } // Resource Groups Tagging API: X-Amz-Target prefix @@ -1139,13 +1149,6 @@ func newServer(d Drivers) (*server.Server, authzSets) { stsSessions = stssrv.NewSessionStore(authClock) } - // STS records the sessions it mints here, and EKS reads the cluster - // creator from it, so both see the caller the same way. - identities := d.Identities - if identities == nil { - identities = awsidentity.New(d.AccountID, d.IAM) - } - if d.STS { // Pass the IAM driver so AssumeRole can enforce the target role's trust // policy and existence. d.IAM may be nil (standalone STS-only), in which diff --git a/server/aws/sqs/handler.go b/server/aws/sqs/handler.go index 81a1b4b14..5335ff580 100644 --- a/server/aws/sqs/handler.go +++ b/server/aws/sqs/handler.go @@ -18,6 +18,7 @@ import ( cerrors "github.com/stackshy/cloudemu/v2/errors" "github.com/stackshy/cloudemu/v2/server/wire" + "github.com/stackshy/cloudemu/v2/server/wire/awsidentity" mqdriver "github.com/stackshy/cloudemu/v2/services/messagequeue/driver" ) @@ -57,6 +58,9 @@ const ( // driver. type Handler struct { mq mqdriver.MessageQueue + // identities, when set, resolves the caller of a send the way STS + // GetCallerIdentity reports it, for the SenderId message attribute. + identities *awsidentity.Resolver } // New returns an SQS handler backed by mq. @@ -64,6 +68,21 @@ func New(mq mqdriver.MessageQueue) *Handler { return &Handler{mq: mq} } +// SetIdentities shares the resolver STS uses, so a message's SenderId is the +// caller's IAM unique id: the user id for an IAM user, ":" +// for an assumed role. Without it the provider records the account id. +func (h *Handler) SetIdentities(r *awsidentity.Resolver) { h.identities = r } + +// senderID resolves the SenderId of a send request, or "" when no resolver +// is wired. +func (h *Handler) senderID(r *http.Request) string { + if h.identities == nil { + return "" + } + + return h.identities.Resolve(r).UserID +} + // Matches identifies SQS requests by their X-Amz-Target header. SQS shares // the same content-type as DynamoDB (application/x-amz-json-1.0) so the // header prefix is the only reliable discriminator. @@ -311,6 +330,7 @@ func (h *Handler) sendMessage(w http.ResponseWriter, r *http.Request) { DeduplicationID: req.DeduplicationID, MessageAttributes: msgAttrs, SystemAttributes: sysAttrs, + SenderID: h.senderID(r), }) if err != nil { writeErr(w, err) @@ -578,6 +598,7 @@ func (h *Handler) sendMessageBatch(w http.ResponseWriter, r *http.Request) { sysAttrsByID := make(map[string]map[string]mqdriver.MessageAttributeValue, len(req.Entries)) entries := make([]mqdriver.BatchSendEntry, 0, len(req.Entries)) + sender := h.senderID(r) for i := range req.Entries { msgAttrs := toDriverMessageAttributes(req.Entries[i].MessageAttributes) @@ -594,6 +615,7 @@ func (h *Handler) sendMessageBatch(w http.ResponseWriter, r *http.Request) { DeduplicationID: req.Entries[i].MessageDeduplicationID, MessageAttributes: msgAttrs, SystemAttributes: sysAttrs, + SenderID: sender, }) } diff --git a/server/aws/sqs_metrics_sdk_test.go b/server/aws/sqs_metrics_sdk_test.go new file mode 100644 index 000000000..233530af0 --- /dev/null +++ b/server/aws/sqs_metrics_sdk_test.go @@ -0,0 +1,260 @@ +package aws_test + +import ( + "context" + "net/http/httptest" + "slices" + "testing" + "time" + + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/credentials" + "github.com/aws/aws-sdk-go-v2/service/cloudwatch" + cwtypes "github.com/aws/aws-sdk-go-v2/service/cloudwatch/types" + "github.com/aws/aws-sdk-go-v2/service/iam" + "github.com/aws/aws-sdk-go-v2/service/sqs" + sqstypes "github.com/aws/aws-sdk-go-v2/service/sqs/types" + "github.com/aws/aws-sdk-go-v2/service/sts" + "github.com/stretchr/testify/require" + + "github.com/stackshy/cloudemu/v2" + "github.com/stackshy/cloudemu/v2/config" + awsserver "github.com/stackshy/cloudemu/v2/server/aws" +) + +// sqsGaugeStat reads one statistic of an AWS/SQS metric for a queue over the +// last few minutes through the real CloudWatch client. +func sqsGaugeStat( + t *testing.T, cw *cloudwatch.Client, metric, queue string, stat cwtypes.Statistic, +) (value float64, unit cwtypes.StandardUnit, found bool) { + t.Helper() + + now := time.Now().UTC() + + out, err := cw.GetMetricStatistics(context.Background(), &cloudwatch.GetMetricStatisticsInput{ + Namespace: aws.String("AWS/SQS"), + MetricName: aws.String(metric), + Dimensions: []cwtypes.Dimension{{Name: aws.String("QueueName"), Value: aws.String(queue)}}, + StartTime: aws.Time(now.Add(-5 * time.Minute)), + EndTime: aws.Time(now.Add(5 * time.Minute)), + Period: aws.Int32(600), + Statistics: []cwtypes.Statistic{stat}, + }) + require.NoError(t, err) + + if len(out.Datapoints) == 0 { + return 0, "", false + } + + dp := out.Datapoints[0] + + switch stat { + case cwtypes.StatisticMaximum: + return aws.ToFloat64(dp.Maximum), dp.Unit, true + case cwtypes.StatisticMinimum: + return aws.ToFloat64(dp.Minimum), dp.Unit, true + case cwtypes.StatisticSum: + return aws.ToFloat64(dp.Sum), dp.Unit, true + default: + return aws.ToFloat64(dp.SampleCount), dp.Unit, true + } +} + +// sqsMetricNames lists the AWS/SQS metric names CloudWatch knows. +func sqsMetricNames(t *testing.T, cw *cloudwatch.Client) []string { + t.Helper() + + metrics, err := cw.ListMetrics(context.Background(), &cloudwatch.ListMetricsInput{Namespace: aws.String("AWS/SQS")}) + require.NoError(t, err) + + names := make([]string, 0, len(metrics.Metrics)) + for _, m := range metrics.Metrics { + names = append(names, aws.ToString(m.MetricName)) + } + + return names +} + +// tickProvider runs one background tick of every time-driven service, the +// way the serve scheduler does. +func tickProvider(p interface{ Tickables() []config.Tickable }) { + now := time.Now() + for _, t := range p.Tickables() { + t.Tick(now) + } +} + +// TestSDKSQSQueueGaugesReachCloudWatch drives SQS with the real SDK and reads +// the queue-state gauges back through CloudWatch, the way a dashboard or an +// alarm on ApproximateNumberOfMessagesVisible would. The gauges come from the +// background tick, not from the requests. +func TestSDKSQSQueueGaugesReachCloudWatch(t *testing.T) { + provider := cloudemu.NewAWS() + ts := httptest.NewServer(awsserver.New(awsserver.Drivers{CloudWatch: provider.CloudWatch, SQS: provider.SQS})) + defer ts.Close() + + cfg := sdkConfig(t, ts.URL) + ctx := context.Background() + sqsClient := sqs.NewFromConfig(cfg) + cw := cloudwatch.NewFromConfig(cfg) + + q, err := sqsClient.CreateQueue(ctx, &sqs.CreateQueueInput{QueueName: aws.String("gauge-q")}) + require.NoError(t, err) + + _, err = sqsClient.SendMessage(ctx, &sqs.SendMessageInput{QueueUrl: q.QueueUrl, MessageBody: aws.String("now")}) + require.NoError(t, err) + + _, err = sqsClient.SendMessage(ctx, &sqs.SendMessageInput{ + QueueUrl: q.QueueUrl, MessageBody: aws.String("later"), DelaySeconds: 600, + }) + require.NoError(t, err) + + recv, err := sqsClient.ReceiveMessage(ctx, &sqs.ReceiveMessageInput{QueueUrl: q.QueueUrl, VisibilityTimeout: 300}) + require.NoError(t, err) + require.Len(t, recv.Messages, 1) + + // Requests publish the counters only. + require.False(t, slices.Contains(sqsMetricNames(t, cw), "ApproximateNumberOfMessagesVisible")) + + tickProvider(provider) + + names := sqsMetricNames(t, cw) + for _, want := range []string{ + "ApproximateNumberOfMessagesVisible", "ApproximateNumberOfMessagesNotVisible", + "ApproximateNumberOfMessagesDelayed", "ApproximateAgeOfOldestMessage", + } { + require.True(t, slices.Contains(names, want), "ListMetrics AWS/SQS missing %s (got %v)", want, names) + } + + // One sample: nothing visible, the received message in flight, the + // delayed one delayed. + visible, unit, ok := sqsGaugeStat(t, cw, "ApproximateNumberOfMessagesVisible", "gauge-q", cwtypes.StatisticMaximum) + require.True(t, ok) + require.InDelta(t, 0, visible, 0) + require.Equal(t, cwtypes.StandardUnitCount, unit) + + samples, _, _ := sqsGaugeStat(t, cw, "ApproximateNumberOfMessagesVisible", "gauge-q", cwtypes.StatisticSampleCount) + require.InDelta(t, 1, samples, 0) + + delayed, _, _ := sqsGaugeStat(t, cw, "ApproximateNumberOfMessagesDelayed", "gauge-q", cwtypes.StatisticMaximum) + require.InDelta(t, 1, delayed, 0) + + inFlight, _, _ := sqsGaugeStat(t, cw, "ApproximateNumberOfMessagesNotVisible", "gauge-q", cwtypes.StatisticMaximum) + require.InDelta(t, 1, inFlight, 0) + + _, ageUnit, ok := sqsGaugeStat(t, cw, "ApproximateAgeOfOldestMessage", "gauge-q", cwtypes.StatisticMaximum) + require.True(t, ok) + require.Equal(t, cwtypes.StandardUnitSeconds, ageUnit) + + // GetQueueAttributes agrees: the delayed message is not counted in flight. + attrs, err := sqsClient.GetQueueAttributes(ctx, &sqs.GetQueueAttributesInput{ + QueueUrl: q.QueueUrl, + AttributeNames: []sqstypes.QueueAttributeName{sqstypes.QueueAttributeNameAll}, + }) + require.NoError(t, err) + require.Equal(t, "1", attrs.Attributes["ApproximateNumberOfMessagesNotVisible"]) + require.Equal(t, "1", attrs.Attributes["ApproximateNumberOfMessagesDelayed"]) + require.Equal(t, "0", attrs.Attributes["ApproximateNumberOfMessages"]) +} + +// TestSDKSQSGaugeSumIsOneSamplePerMinute is the Sum-statistic case: six sends +// in a minute give one Visible datapoint of 6, so Sum is 6, not a running +// total of one sample per send. +func TestSDKSQSGaugeSumIsOneSamplePerMinute(t *testing.T) { + provider := cloudemu.NewAWS() + ts := httptest.NewServer(awsserver.New(awsserver.Drivers{CloudWatch: provider.CloudWatch, SQS: provider.SQS})) + defer ts.Close() + + cfg := sdkConfig(t, ts.URL) + ctx := context.Background() + sqsClient := sqs.NewFromConfig(cfg) + cw := cloudwatch.NewFromConfig(cfg) + + q, err := sqsClient.CreateQueue(ctx, &sqs.CreateQueueInput{QueueName: aws.String("sum-q")}) + require.NoError(t, err) + + for i := 0; i < 6; i++ { + _, err = sqsClient.SendMessage(ctx, &sqs.SendMessageInput{QueueUrl: q.QueueUrl, MessageBody: aws.String("m")}) + require.NoError(t, err) + } + + tickProvider(provider) + tickProvider(provider) // a second tick inside the minute publishes nothing + + sum, _, _ := sqsGaugeStat(t, cw, "ApproximateNumberOfMessagesVisible", "sum-q", cwtypes.StatisticSum) + require.InDelta(t, 6, sum, 0) + + samples, _, _ := sqsGaugeStat(t, cw, "ApproximateNumberOfMessagesVisible", "sum-q", cwtypes.StatisticSampleCount) + require.InDelta(t, 1, samples, 0) +} + +// receiveSenderID sends body with client and returns the SenderId the queue +// reports for it. +func receiveSenderID(t *testing.T, client *sqs.Client, queueURL *string, body string) string { + t.Helper() + + ctx := context.Background() + + _, err := client.SendMessage(ctx, &sqs.SendMessageInput{QueueUrl: queueURL, MessageBody: aws.String(body)}) + require.NoError(t, err) + + out, err := client.ReceiveMessage(ctx, &sqs.ReceiveMessageInput{ + QueueUrl: queueURL, + MessageSystemAttributeNames: []sqstypes.MessageSystemAttributeName{sqstypes.MessageSystemAttributeNameSenderId}, + }) + require.NoError(t, err) + require.Len(t, out.Messages, 1) + require.Equal(t, body, aws.ToString(out.Messages[0].Body)) + + _, err = client.DeleteMessage(ctx, &sqs.DeleteMessageInput{QueueUrl: queueURL, ReceiptHandle: out.Messages[0].ReceiptHandle}) + require.NoError(t, err) + + return out.Messages[0].Attributes[string(sqstypes.MessageSystemAttributeNameSenderId)] +} + +// TestSDKSQSSenderIDMatchesCallerIdentity checks that SenderId is the +// caller's IAM unique id, the same UserId STS GetCallerIdentity reports: the +// user id for long-term keys, ":" for an assumed role. +func TestSDKSQSSenderIDMatchesCallerIdentity(t *testing.T) { + provider := cloudemu.NewAWS() + ts := httptest.NewServer(awsserver.NewFromProvider(provider)) + defer ts.Close() + + cfg := sdkConfig(t, ts.URL) + ctx := context.Background() + sqsClient := sqs.NewFromConfig(cfg) + + q, err := sqsClient.CreateQueue(ctx, &sqs.CreateQueueInput{QueueName: aws.String("sender-q")}) + require.NoError(t, err) + + who, err := sts.NewFromConfig(cfg).GetCallerIdentity(ctx, &sts.GetCallerIdentityInput{}) + require.NoError(t, err) + require.NotEqual(t, aws.ToString(who.Account), aws.ToString(who.UserId)) + require.Equal(t, aws.ToString(who.UserId), receiveSenderID(t, sqsClient, q.QueueUrl, "as-user")) + + // An assumed role's messages carry ":". + role, err := iam.NewFromConfig(cfg).CreateRole(ctx, &iam.CreateRoleInput{ + RoleName: aws.String("sender-role"), + AssumeRolePolicyDocument: aws.String(`{"Version":"2012-10-17","Statement":[{"Effect":"Allow",` + + `"Principal":{"AWS":"*"},"Action":"sts:AssumeRole"}]}`), + }) + require.NoError(t, err) + + assumed, err := sts.NewFromConfig(cfg).AssumeRole(ctx, &sts.AssumeRoleInput{ + RoleArn: aws.String("arn:aws:iam::" + aws.ToString(who.Account) + ":role/sender-role"), + RoleSessionName: aws.String("worker-1"), + }) + require.NoError(t, err) + + roleCfg := cfg.Copy() + roleCfg.Credentials = credentials.NewStaticCredentialsProvider( + aws.ToString(assumed.Credentials.AccessKeyId), + aws.ToString(assumed.Credentials.SecretAccessKey), + aws.ToString(assumed.Credentials.SessionToken), + ) + + // AssumedRoleId is the role's own RoleId plus the session name. + roleID := aws.ToString(assumed.AssumedRoleUser.AssumedRoleId) + require.Equal(t, aws.ToString(role.Role.RoleId)+":worker-1", roleID) + require.Equal(t, roleID, receiveSenderID(t, sqs.NewFromConfig(roleCfg), q.QueueUrl, "as-role")) +} diff --git a/server/aws/sts/assumed_role_id_test.go b/server/aws/sts/assumed_role_id_test.go new file mode 100644 index 000000000..f580b46e6 --- /dev/null +++ b/server/aws/sts/assumed_role_id_test.go @@ -0,0 +1,67 @@ +package sts_test + +import ( + "context" + "strings" + "testing" + + "github.com/aws/aws-sdk-go-v2/aws" + awsiam "github.com/aws/aws-sdk-go-v2/service/iam" + awssts "github.com/aws/aws-sdk-go-v2/service/sts" + + awsserver "github.com/stackshy/cloudemu/v2/server/aws" +) + +// TestSDKAssumedRoleIDUsesRoleID checks that AssumedRoleId is the assumed +// role's own RoleId plus the session name, as real STS returns it, so two +// roles with the same session name get different ids. +func TestSDKAssumedRoleIDUsesRoleID(t *testing.T) { + ts, iamClient := newTrustServer(t) + ctx := context.Background() + client := stsClient(t, ts.URL) + + for _, name := range []string{"role-a", "role-b"} { + role, err := iamClient.CreateRole(ctx, &awsiam.CreateRoleInput{ + RoleName: aws.String(name), + AssumeRolePolicyDocument: aws.String(allowRootTrust), + }) + if err != nil { + t.Fatalf("CreateRole %s: %v", name, err) + } + + out, err := client.AssumeRole(ctx, &awssts.AssumeRoleInput{ + RoleArn: aws.String("arn:aws:iam::" + testAccountID + ":role/" + name), + RoleSessionName: aws.String("shared-session"), + }) + if err != nil { + t.Fatalf("AssumeRole %s: %v", name, err) + } + + want := aws.ToString(role.Role.RoleId) + ":shared-session" + if got := aws.ToString(out.AssumedRoleUser.AssumedRoleId); got != want { + t.Fatalf("AssumedRoleId for %s = %q, want %q", name, got, want) + } + + if !strings.HasPrefix(want, "AROA") { + t.Fatalf("RoleId %q does not have the AROA prefix", want) + } + } +} + +// TestSDKAssumedRoleIDWithoutIAM keeps the synthetic prefix when no IAM driver +// knows the role. +func TestSDKAssumedRoleIDWithoutIAM(t *testing.T) { + ts := newServer(t, awsserver.Drivers{STS: true, AccountID: testAccountID, Region: testRegion}) + + out, err := stsClient(t, ts.URL).AssumeRole(context.Background(), &awssts.AssumeRoleInput{ + RoleArn: aws.String("arn:aws:iam::" + testAccountID + ":role/anything"), + RoleSessionName: aws.String("s"), + }) + if err != nil { + t.Fatalf("AssumeRole: %v", err) + } + + if got := aws.ToString(out.AssumedRoleUser.AssumedRoleId); got != "AROACLOUDEMU0000000000:s" { + t.Fatalf("AssumedRoleId = %q, want the synthetic fallback", got) + } +} diff --git a/server/aws/sts/operations.go b/server/aws/sts/operations.go index adceb5504..7cf9a49f2 100644 --- a/server/aws/sts/operations.go +++ b/server/aws/sts/operations.go @@ -73,7 +73,7 @@ func (h *Handler) assumeRole(w http.ResponseWriter, r *http.Request) { } assumedArn := "arn:aws:sts::" + h.accountID + ":assumed-role/" + roleName + "/" + sessionName - assumedRoleID := assumedRoleIDPrefix + ":" + sessionName + assumedRoleID := h.assumedRoleID(r, roleArn, roleName, sessionName) creds, ok := h.mintCredentials(w, durationFromForm(r), awsidentity.Identity{ARN: assumedArn, UserID: assumedRoleID}, roleOwner(roleName)) @@ -95,11 +95,25 @@ func (h *Handler) assumeRole(w http.ResponseWriter, r *http.Request) { } // assumedRoleIDPrefix is the synthetic AssumedRoleId prefix (real STS uses the -// "AROA" prefix followed by a unique id) reported by every AssumeRole-family -// operation, and used to derive the identity a minted session is recorded -// under for GetCallerIdentity. +// role's unique "AROA..." id) reported by an AssumeRole-family operation when +// the role is not known to the IAM driver, and used to derive the identity a +// minted session is recorded under for GetCallerIdentity. const assumedRoleIDPrefix = "AROACLOUDEMU0000000000" +// assumedRoleID is the AssumedRoleId of a session of the role roleArn names: +// ":", where the role id is the role's unique id as +// IAM reports it (RoleId), as real STS returns it. When no IAM driver is wired +// or the role is unknown it falls back to assumedRoleIDPrefix. +func (h *Handler) assumedRoleID(r *http.Request, roleArn, roleName, sessionName string) string { + prefix := assumedRoleIDPrefix + + if role, ok := h.requestedRole(r, roleArn, roleName); ok && role.ID != "" { + prefix = role.ID + } + + return prefix + ":" + sessionName +} + // trustAllows reports whether the caller may assume the role RoleArn names. // // Under EnforceAuth the gate has authenticated the caller and recorded its @@ -150,7 +164,7 @@ func (h *Handler) assumeRoleWithWebIdentity(w http.ResponseWriter, r *http.Reque provider = "cloudemu.local" } - assumedRoleID := assumedRoleIDPrefix + ":" + sessionName + assumedRoleID := h.assumedRoleID(r, r.Form.Get("RoleArn"), roleName, sessionName) creds, ok := h.mintCredentials(w, durationFromForm(r), awsidentity.Identity{ARN: assumedArn, UserID: assumedRoleID}, roleOwner(roleName)) @@ -185,7 +199,7 @@ func (h *Handler) assumeRoleWithSAML(w http.ResponseWriter, r *http.Request) { roleName := roleNameFromArn(r.Form.Get("RoleArn")) sessionName := "cloudemu-saml-session" assumedArn := "arn:aws:sts::" + h.accountID + ":assumed-role/" + roleName + "/" + sessionName - assumedRoleID := assumedRoleIDPrefix + ":" + sessionName + assumedRoleID := h.assumedRoleID(r, r.Form.Get("RoleArn"), roleName, sessionName) creds, ok := h.mintCredentials(w, durationFromForm(r), awsidentity.Identity{ARN: assumedArn, UserID: assumedRoleID}, roleOwner(roleName)) diff --git a/services/messagequeue/driver/driver.go b/services/messagequeue/driver/driver.go index 9b21f9ca0..81418e046 100644 --- a/services/messagequeue/driver/driver.go +++ b/services/messagequeue/driver/driver.go @@ -157,6 +157,13 @@ type SendMessageInput struct { // message stays invisible until this instant, measured on the provider's // clock. The zero value means no schedule. Ignored by non-Azure providers. ScheduledEnqueueTime time.Time + // SenderID is the IAM unique id of the caller that sent the message (an + // IAM user id, or ":" for an assumed role), which + // AWS SQS returns as the SenderId message system attribute. The SQS wire + // handler resolves it from the request's credentials; empty means no + // caller principal is known and the account id is used. Ignored by + // non-AWS providers. + SenderID string } // SendMessageOutput is the result of sending a message. @@ -234,6 +241,8 @@ type BatchSendEntry struct { // SystemAttributes are SQS message system attributes (AWS SQS; the only // supported key is AWSTraceHeader). SystemAttributes map[string]MessageAttributeValue + // SenderID has the same meaning as on SendMessageInput. + SenderID string } // BatchSendResult is the result of a batch send. From e6b4b9440b4408f1087be9768db97ca947111840 Mon Sep 17 00:00:00 2001 From: Satyam-Trivedi-ZS Date: Sat, 24 Oct 2026 01:50:48 +0530 Subject: [PATCH 26/31] fix(aws-batch): job lifecycle, scheduling policies, job definition type check and job state-change events (#1648) * fix(aws-batch): add job lifecycle, scheduling policies, job definition type check and job state-change events Add SubmitJob, DescribeJobs, ListJobs, CancelJob and TerminateJob. Jobs move SUBMITTED -> PENDING -> RUNNABLE -> STARTING -> RUNNING -> SUCCEEDED or FAILED on the provider clock, honour dependsOn and array jobs, and follow the documented cancel vs terminate rules. SubmitJob validates the queue (exists, ENABLED), the job definition revision, dependencies, share identifier, array size, priority, timeout and tags. ListJobs supports the documented filters and pages with a stable cursor. Add scheduling-policy CRUD and validate schedulingPolicyArn on CreateJobQueue/UpdateJobQueue. Enforce the RegisterJobDefinition type enum (container, multinode) and the properties each type requires. Jobs and scheduling policies are taggable, and tags follow the TagResource limits and the reserved aws: prefix. Publish "Batch Job State Change" events to the EventBridge default bus from the shared serve scheduler (config.Tickable) and on job API calls. New state is included in snapshots. Part of #1496 * docs(coverage): regenerate for the Batch job and scheduling-policy operations Part of #1496 * fix(aws-batch): bound array child index to int32 when parsing job IDs Parse the array child index with strconv.ParseInt(..., 10, 32) so the value converted into ArrayProperties.Index is bounded at parse time (CodeQL go/incorrect-integer-conversion). lookup already rejected any index at or above the array size, so no out-of-range value reached the conversion; this makes the bound explicit. * fix(aws-batch): apply property overrides and address review findings - SubmitJob merges ecsPropertiesOverride, eksPropertiesOverride and nodeOverrides into the job detail instead of dropping them, and rejects an override the job definition can't take (wrong properties kind, unknown container name, numNodes without an open-ended range, targetNodes covering no range). - SubmitJob refuses an explicit INACTIVE revision (name:revision or ARN). - Replayed intermediate "Batch Job State Change" events no longer carry the final container exitCode. - With async settle a job stays RUNNING for 5s (DefaultBatchJobRunDwell) so a real client can terminate a running job; the dwell is stored on the job record and survives snapshots. - ListJobs with arrayJobId applies the JOB_NAME and other filters to the children. - Scheduling-policy binding (CreateJobQueue, UpdateJobQueue) and DeleteSchedulingPolicy are serialized so a queue can't end up bound to a deleted policy. - RegisterJobDefinition stores platformCapabilities as sent instead of echoing a default EC2, which made Terraform multinode definitions drift. --- docs/coverage/README.md | 2 +- docs/coverage/aws/README.md | 2 +- docs/coverage/aws/batch.md | 22 +- docs/coverage/coverage.json | 34 +- docs/coverage/nongoals/batch.md | 7 + internal/settle/settle.go | 10 + providers/aws/aws.go | 3 +- providers/aws/batch/batch.go | 49 +- providers/aws/batch/batch_test.go | 6 +- providers/aws/batch/compute_environment.go | 4 + providers/aws/batch/events.go | 233 +++++ providers/aws/batch/job_definition.go | 41 +- providers/aws/batch/job_lifecycle.go | 568 ++++++++++++ providers/aws/batch/job_queue.go | 64 +- providers/aws/batch/jobs.go | 955 +++++++++++++++++++++ providers/aws/batch/jobs_overrides_test.go | 367 ++++++++ providers/aws/batch/jobs_paging_test.go | 238 +++++ providers/aws/batch/jobs_test.go | 752 ++++++++++++++++ providers/aws/batch/overrides.go | 609 +++++++++++++ providers/aws/batch/scheduling_policy.go | 284 ++++++ providers/aws/batch/snapshot.go | 21 +- providers/aws/batch/tags.go | 128 ++- providers/aws/tickables_test.go | 14 +- server/aws/batch/handler.go | 70 +- server/aws/batch/jobs.go | 119 +++ server/aws/batch/jobs_paging_sdk_test.go | 96 +++ server/aws/batch/jobs_sdk_test.go | 291 +++++++ server/aws/batch/scheduling_policy.go | 104 +++ server/aws/batch/types_jobs.go | 326 +++++++ services/batch/driver/driver.go | 21 +- services/batch/driver/types.go | 152 ++++ 31 files changed, 5525 insertions(+), 67 deletions(-) create mode 100644 docs/coverage/nongoals/batch.md create mode 100644 providers/aws/batch/events.go create mode 100644 providers/aws/batch/job_lifecycle.go create mode 100644 providers/aws/batch/jobs.go create mode 100644 providers/aws/batch/jobs_overrides_test.go create mode 100644 providers/aws/batch/jobs_paging_test.go create mode 100644 providers/aws/batch/jobs_test.go create mode 100644 providers/aws/batch/overrides.go create mode 100644 providers/aws/batch/scheduling_policy.go create mode 100644 server/aws/batch/jobs.go create mode 100644 server/aws/batch/jobs_paging_sdk_test.go create mode 100644 server/aws/batch/jobs_sdk_test.go create mode 100644 server/aws/batch/scheduling_policy.go create mode 100644 server/aws/batch/types_jobs.go diff --git a/docs/coverage/README.md b/docs/coverage/README.md index 1f226d00d..3a6db0aa8 100644 --- a/docs/coverage/README.md +++ b/docs/coverage/README.md @@ -33,7 +33,7 @@ code does not implement. Machine-readable: [`coverage.json`](./coverage.json). | `backup` | [Backup](./aws/backup.md) | - | - | - | 25 | | `backupdr` | - | - | [BackupDR](./gcp/backupdr.md) | - | 6 | | `bastion` | - | [Bastion](./azure/bastion.md) | - | - | 4 | -| `batch` | [Batch](./aws/batch.md) | - | - | - | 14 | +| `batch` | [Batch](./aws/batch.md) | - | - | - | 24 | | `batch-azure` | - | [Batch](./azure/batch.md) | - | - | 17 | | `bedrock` | [Bedrock](./aws/bedrock.md) | - | - | - | 65 | | `bedrockagent` | [BedrockAgent](./aws/bedrockagent.md) | - | - | - | 32 | diff --git a/docs/coverage/aws/README.md b/docs/coverage/aws/README.md index daa69bc47..9583d0ad5 100644 --- a/docs/coverage/aws/README.md +++ b/docs/coverage/aws/README.md @@ -15,7 +15,7 @@ Services cloudemu emulates for AWS, by native name. Back to the [cross-provider | [AppSync](./appsync.md) | `appsync` | 17 | | [Athena](./athena.md) | `athena` | 26 | | [Backup](./backup.md) | `backup` | 25 | -| [Batch](./batch.md) | `batch` | 14 | +| [Batch](./batch.md) | `batch` | 24 | | [Bedrock](./bedrock.md) | `bedrock` | 65 | | [BedrockAgent](./bedrockagent.md) | `bedrockagent` | 32 | | [BedrockAgentRuntime](./bedrockagentruntime.md) | `bedrockagentruntime` | 3 | diff --git a/docs/coverage/aws/batch.md b/docs/coverage/aws/batch.md index 20101f174..2dbae14ca 100644 --- a/docs/coverage/aws/batch.md +++ b/docs/coverage/aws/batch.md @@ -3,25 +3,41 @@ AWS's `batch` service ยท portable interface `driver.Batch` ยท [AWS index](./README.md) -## Operations (14) +## Operations (24) | Operation | Description | | --- | --- | +| `CancelJob` | | | `CreateComputeEnvironment` | Compute environments. | | `CreateJobQueue` | Job queues. | +| `CreateSchedulingPolicy` | Scheduling policies. | | `DeleteComputeEnvironment` | | | `DeleteJobQueue` | | +| `DeleteSchedulingPolicy` | | | `DeregisterJobDefinition` | | | `DescribeComputeEnvironments` | | | `DescribeJobDefinitions` | | | `DescribeJobQueues` | | +| `DescribeJobs` | | +| `DescribeSchedulingPolicies` | | +| `ListJobs` | | +| `ListSchedulingPolicies` | | | `ListTagsForResource` | | | `RegisterJobDefinition` | Job definitions. | -| `TagResource` | Tags (by ARN), shared across all three resource kinds. | +| `SubmitJob` | Jobs. | +| `TagResource` | Tags (by ARN), shared across every taggable resource kind. | +| `TerminateJob` | | | `UntagResource` | | | `UpdateComputeEnvironment` | | | `UpdateJobQueue` | | +| `UpdateSchedulingPolicy` | | ## Not in scope -_Not documented yet. See the [emulator boundary](../../../README.md) for cloudemu-wide non-goals._ +- Jobs run no workload: a job always succeeds (container `exitCode` 0) once its dependencies succeed. By default it is `SUCCEEDED` as soon as it is submitted; with `--async-settle` it spends about one second each in `SUBMITTED`, `RUNNABLE` and `STARTING`, then five seconds in `RUNNING` (and stays `PENDING` while a dependency is unfinished). `attempts` is always empty, and retry strategies and timeouts are stored and echoed but never applied. +- `CancelJob` on a `PENDING` job takes effect at once. Real Batch waits for the job's dependencies to finish first. +- Array jobs: children are derived from the parent, not stored one by one. `SEQUENTIAL` and `N_TO_N` dependencies are honoured. An array parent reports `PENDING` while any child is unfinished. Multi-node parallel jobs run as a single job: `ListJobs` with `multiNodeJobId` returns an empty list. +- Fair-share scheduling policies are stored, validated and bound to queues, but they never change the order in which jobs run. Jobs never stay `RUNNABLE` because no capacity is free, so "Batch Job Queue Blocked" events are never sent. +- "Batch Job State Change" events go to the EventBridge default bus on the serve scheduler's tick (and on any Batch job API call), one per status the job passed through; none is sent for the initial submission. In library mode, nothing ticks, so events are published when a job API call sees the change or when `Tick` is called. A job that fails reports only `FAILED`, without the earlier steps. Array children send no events of their own. Real Batch sends no state-change events for compute environments or job queues, so the emulator sends none either. +- Error messages are written by the emulator. The exception type (`ClientException`, HTTP 400) is the documented one. +- `containerOverrides`, `ecsPropertiesOverride`, `eksPropertiesOverride` and `nodeOverrides` are merged into the job detail `DescribeJobs` returns (environment variables by name, resource requirements by type, other fields replaced). Overrides are not checked against compute-environment platform rules (for example `nodeOverrides` on Fargate), and multi-node jobs report no per-node `nodeDetails`. diff --git a/docs/coverage/coverage.json b/docs/coverage/coverage.json index f8c4de6e9..6e1ad5147 100644 --- a/docs/coverage/coverage.json +++ b/docs/coverage/coverage.json @@ -2655,6 +2655,9 @@ "service": "batch", "interface": "Batch", "operations": [ + { + "name": "CancelJob" + }, { "name": "CreateComputeEnvironment", "doc": "Compute environments." @@ -2663,12 +2666,19 @@ "name": "CreateJobQueue", "doc": "Job queues." }, + { + "name": "CreateSchedulingPolicy", + "doc": "Scheduling policies." + }, { "name": "DeleteComputeEnvironment" }, { "name": "DeleteJobQueue" }, + { + "name": "DeleteSchedulingPolicy" + }, { "name": "DeregisterJobDefinition" }, @@ -2681,6 +2691,18 @@ { "name": "DescribeJobQueues" }, + { + "name": "DescribeJobs" + }, + { + "name": "DescribeSchedulingPolicies" + }, + { + "name": "ListJobs" + }, + { + "name": "ListSchedulingPolicies" + }, { "name": "ListTagsForResource" }, @@ -2688,9 +2710,16 @@ "name": "RegisterJobDefinition", "doc": "Job definitions." }, + { + "name": "SubmitJob", + "doc": "Jobs." + }, { "name": "TagResource", - "doc": "Tags (by ARN), shared across all three resource kinds." + "doc": "Tags (by ARN), shared across every taggable resource kind." + }, + { + "name": "TerminateJob" }, { "name": "UntagResource" @@ -2700,6 +2729,9 @@ }, { "name": "UpdateJobQueue" + }, + { + "name": "UpdateSchedulingPolicy" } ], "providers": { diff --git a/docs/coverage/nongoals/batch.md b/docs/coverage/nongoals/batch.md new file mode 100644 index 000000000..1cee8c5cd --- /dev/null +++ b/docs/coverage/nongoals/batch.md @@ -0,0 +1,7 @@ +- Jobs run no workload: a job always succeeds (container `exitCode` 0) once its dependencies succeed. By default it is `SUCCEEDED` as soon as it is submitted; with `--async-settle` it spends about one second each in `SUBMITTED`, `RUNNABLE` and `STARTING`, then five seconds in `RUNNING` (and stays `PENDING` while a dependency is unfinished). `attempts` is always empty, and retry strategies and timeouts are stored and echoed but never applied. +- `CancelJob` on a `PENDING` job takes effect at once. Real Batch waits for the job's dependencies to finish first. +- Array jobs: children are derived from the parent, not stored one by one. `SEQUENTIAL` and `N_TO_N` dependencies are honoured. An array parent reports `PENDING` while any child is unfinished. Multi-node parallel jobs run as a single job: `ListJobs` with `multiNodeJobId` returns an empty list. +- Fair-share scheduling policies are stored, validated and bound to queues, but they never change the order in which jobs run. Jobs never stay `RUNNABLE` because no capacity is free, so "Batch Job Queue Blocked" events are never sent. +- "Batch Job State Change" events go to the EventBridge default bus on the serve scheduler's tick (and on any Batch job API call), one per status the job passed through; none is sent for the initial submission. In library mode, nothing ticks, so events are published when a job API call sees the change or when `Tick` is called. A job that fails reports only `FAILED`, without the earlier steps. Array children send no events of their own. Real Batch sends no state-change events for compute environments or job queues, so the emulator sends none either. +- Error messages are written by the emulator. The exception type (`ClientException`, HTTP 400) is the documented one. +- `containerOverrides`, `ecsPropertiesOverride`, `eksPropertiesOverride` and `nodeOverrides` are merged into the job detail `DescribeJobs` returns (environment variables by name, resource requirements by type, other fields replaced). Overrides are not checked against compute-environment platform rules (for example `nodeOverrides` on Fargate), and multi-node jobs report no per-node `nodeDetails`. diff --git a/internal/settle/settle.go b/internal/settle/settle.go index 6a1c6c9d2..0d122d90a 100644 --- a/internal/settle/settle.go +++ b/internal/settle/settle.go @@ -83,6 +83,16 @@ const ( DefaultRestoreExpeditedSettle = 1 * time.Second DefaultRestoreStandardSettle = 3 * time.Second DefaultRestoreBulkSettle = 5 * time.Second + + // DefaultBatchJobPhase is how long an AWS Batch job dwells in SUBMITTED, + // RUNNABLE and STARTING before moving on; RUNNING lasts + // DefaultBatchJobRunDwell. + DefaultBatchJobPhase = 1 * time.Second + + // DefaultBatchJobRunDwell is how long an AWS Batch job stays RUNNING. It is + // longer than the other phases so a real CLI or SDK client can observe a + // running job and call TerminateJob on it before it finishes. + DefaultBatchJobRunDwell = 5 * time.Second ) // Window is a read-time overlay describing a resource still settling into its diff --git a/providers/aws/aws.go b/providers/aws/aws.go index 8b848a975..5aafa2483 100644 --- a/providers/aws/aws.go +++ b/providers/aws/aws.go @@ -598,6 +598,7 @@ func wireLifecycleEvents(p *Provider) { p.Glue.SetEventPublisher(p.EventBridge) p.CloudWatch.SetEventPublisher(p.EventBridge) p.SageMaker.SetEventPublisher(p.EventBridge) + p.Batch.SetEventPublisher(p.EventBridge) } // awsDrivers assembles the resource-discovery driver set from the provider's @@ -639,7 +640,7 @@ func awsDrivers(p *Provider) *resourcediscovery.Drivers { func (p *Provider) Tickables() []config.Tickable { // SQS and EFS run before CloudWatch so the alarms evaluated in the same // tick see the queue gauges and AWS/EFS metrics they just published. - return []config.Tickable{p.SQS, p.EFS, p.CloudWatch, p.SSM, p.CloudFormation} + return []config.Tickable{p.SQS, p.EFS, p.CloudWatch, p.SSM, p.CloudFormation, p.Batch} } // Close tears down any real engines wired into the provider via diff --git a/providers/aws/batch/batch.go b/providers/aws/batch/batch.go index 85239d10d..6e6b761d7 100644 --- a/providers/aws/batch/batch.go +++ b/providers/aws/batch/batch.go @@ -1,7 +1,10 @@ // Package batch provides an in-memory mock implementation of AWS Batch: the -// compute-environment, job-queue, and job-definition control plane. Resources -// provision synchronously (VALID/ACTIVE immediately), ARNs and ECS cluster ARNs -// are synthesized, and job-definition revisions increment on re-register. +// compute-environment, job-queue, job-definition and scheduling-policy control +// plane plus the job lifecycle. Resources provision synchronously +// (VALID/ACTIVE immediately), ARNs and ECS cluster ARNs are synthesized, and +// job-definition revisions increment on re-register. Jobs run no workload: a +// submitted job walks SUBMITTED -> RUNNABLE -> STARTING -> RUNNING -> +// SUCCEEDED on the configured clock (instantly unless AsyncSettle is on). package batch import ( @@ -11,6 +14,7 @@ import ( "sync" "github.com/stackshy/cloudemu/v2/config" + "github.com/stackshy/cloudemu/v2/internal/awsevents" "github.com/stackshy/cloudemu/v2/internal/idgen" "github.com/stackshy/cloudemu/v2/internal/memstore" "github.com/stackshy/cloudemu/v2/services/batch/driver" @@ -24,6 +28,8 @@ const ( kindComputeEnvironment = "compute-environment" kindJobQueue = "job-queue" kindJobDefinition = "job-definition" + kindSchedulingPolicy = "scheduling-policy" + kindJob = "job" ) // Mock is an in-memory implementation of AWS Batch. Each resource kind lives in @@ -33,26 +39,45 @@ const ( // reads are copy-on-write (mutations build fresh values, never mutating stored // ones in place). type Mock struct { - computeEnvs *memstore.Store[driver.ComputeEnvironment] - jobQueues *memstore.Store[driver.JobQueue] - jobDefs *memstore.Store[driver.JobDefinition] - jobDefMaxRev *memstore.Store[int32] + computeEnvs *memstore.Store[driver.ComputeEnvironment] + jobQueues *memstore.Store[driver.JobQueue] + jobDefs *memstore.Store[driver.JobDefinition] + jobDefMaxRev *memstore.Store[int32] + schedPolicies *memstore.Store[driver.SchedulingPolicy] + jobs *memstore.Store[jobRecord] + jobSeq *memstore.Store[int64] // registerMu serializes job-definition revision allocation so two concurrent // RegisterJobDefinition calls for the same name can't claim the same revision. registerMu sync.Mutex + // policyBindMu serializes binding a scheduling policy to a job queue + // (CreateJobQueue, UpdateJobQueue) against DeleteSchedulingPolicy, so a + // policy can't be deleted between the in-use check and a queue binding it. + policyBindMu sync.Mutex + + // jobsMu serializes job submission (sequence allocation), cancel/terminate + // decisions and the event-emission bookkeeping on job records. + jobsMu sync.Mutex + + // events publishes "Batch Job State Change" events to the EventBridge + // default bus once wired by the provider factory. + events awsevents.Emitter + opts *config.Options } // New creates a new AWS Batch mock with the given configuration options. func New(opts *config.Options) *Mock { return &Mock{ - computeEnvs: memstore.New[driver.ComputeEnvironment](), - jobQueues: memstore.New[driver.JobQueue](), - jobDefs: memstore.New[driver.JobDefinition](), - jobDefMaxRev: memstore.New[int32](), - opts: opts, + computeEnvs: memstore.New[driver.ComputeEnvironment](), + jobQueues: memstore.New[driver.JobQueue](), + jobDefs: memstore.New[driver.JobDefinition](), + jobDefMaxRev: memstore.New[int32](), + schedPolicies: memstore.New[driver.SchedulingPolicy](), + jobs: memstore.New[jobRecord](), + jobSeq: memstore.New[int64](), + opts: opts, } } diff --git a/providers/aws/batch/batch_test.go b/providers/aws/batch/batch_test.go index 6be9c0ee6..174dd5639 100644 --- a/providers/aws/batch/batch_test.go +++ b/providers/aws/batch/batch_test.go @@ -189,8 +189,10 @@ func TestRegisterJobDefinitionRevisionIncrements(t *testing.T) { t.Fatalf("first revision/status wrong: %+v", first) } - if len(first.PlatformCapabilities) != 1 || first.PlatformCapabilities[0] != "EC2" { - t.Fatalf("platformCapabilities default should be [EC2], got %v", first.PlatformCapabilities) + // No default is echoed when the caller sent none (it would drift in + // Terraform, which reads the list back as-is). + if len(first.PlatformCapabilities) != 0 { + t.Fatalf("platformCapabilities should be empty when none was sent, got %v", first.PlatformCapabilities) } second, err := m.RegisterJobDefinition(ctx, in) diff --git a/providers/aws/batch/compute_environment.go b/providers/aws/batch/compute_environment.go index 8136d760d..b17b8ed9b 100644 --- a/providers/aws/batch/compute_environment.go +++ b/providers/aws/batch/compute_environment.go @@ -33,6 +33,10 @@ func (m *Mock) CreateComputeEnvironment( return nil, cerrors.New(cerrors.InvalidArgument, "computeEnvironmentName is required") } + if err := validateTags(in.Tags); err != nil { + return nil, err + } + if in.Type != driver.CETypeManaged && in.Type != driver.CETypeUnmanaged { return nil, cerrors.Newf(cerrors.InvalidArgument, "type must be MANAGED or UNMANAGED, got %q", in.Type) } diff --git a/providers/aws/batch/events.go b/providers/aws/batch/events.go new file mode 100644 index 000000000..f3947cf03 --- /dev/null +++ b/providers/aws/batch/events.go @@ -0,0 +1,233 @@ +package batch + +import ( + "context" + "encoding/json" + "time" + + "github.com/stackshy/cloudemu/v2/internal/awsevents" + "github.com/stackshy/cloudemu/v2/services/batch/driver" +) + +// EventBridge identity of Batch's native job events. See "Job state change +// events" in the AWS Batch user guide. Batch publishes a "Batch Job State +// Change" event every time an existing job changes status, but none for the +// initial submission; the detail is the job as DescribeJobs reports it. +const ( + eventSource = "aws.batch" + eventJobStateChange = "Batch Job State Change" +) + +// jobEventDependency is a dependsOn entry of the event detail. +type jobEventDependency struct { + JobID string `json:"jobId,omitempty"` + Type string `json:"type,omitempty"` +} + +// jobEventArray is the arrayProperties section of the event detail. +type jobEventArray struct { + Size int32 `json:"size,omitempty"` + Index *int32 `json:"index,omitempty"` + StatusSummary map[string]int32 `json:"statusSummary,omitempty"` +} + +// jobStateChangeDetail is the detail payload of a "Batch Job State Change" +// event: the JobDetail shape DescribeJobs returns. +type jobStateChangeDetail struct { + JobArn string `json:"jobArn"` + JobName string `json:"jobName"` + JobID string `json:"jobId"` + JobQueue string `json:"jobQueue"` + Status string `json:"status"` + StatusReason string `json:"statusReason,omitempty"` + Attempts []any `json:"attempts"` + CreatedAt int64 `json:"createdAt"` + StartedAt int64 `json:"startedAt,omitempty"` + StoppedAt int64 `json:"stoppedAt,omitempty"` + RetryStrategy json.RawMessage `json:"retryStrategy,omitempty"` + DependsOn []jobEventDependency `json:"dependsOn"` + JobDefinition string `json:"jobDefinition"` + Parameters map[string]string `json:"parameters"` + Container json.RawMessage `json:"container,omitempty"` + NodeProperties json.RawMessage `json:"nodeProperties,omitempty"` + EcsProperties json.RawMessage `json:"ecsProperties,omitempty"` + EksProperties json.RawMessage `json:"eksProperties,omitempty"` + ArrayProperties *jobEventArray `json:"arrayProperties,omitempty"` + Timeout json.RawMessage `json:"timeout,omitempty"` + Tags map[string]string `json:"tags,omitempty"` + PropagateTags bool `json:"propagateTags"` + PlatformCapabilities []string `json:"platformCapabilities"` + ShareIdentifier string `json:"shareIdentifier,omitempty"` + SchedulingPriority *int32 `json:"schedulingPriority,omitempty"` + IsCancelled bool `json:"isCancelled,omitempty"` + IsTerminated bool `json:"isTerminated,omitempty"` +} + +// SetEventPublisher wires the EventBridge default bus that job state changes +// are published to. Safe to leave unset. No events are emitted. +func (m *Mock) SetEventPublisher(p awsevents.Publisher) { + m.events.SetPublisher(p) +} + +// pendingJobEvent is one event computed under jobsMu and published after it is +// released. +type pendingJobEvent struct { + arn string + detail jobStateChangeDetail +} + +// jobPath is the ordered status path a job walks on success. Array parents go +// straight from PENDING to their terminal state; a job with dependencies passes +// through PENDING; others skip it. +func jobPath(rec *jobRecord) []string { + switch { + case rec.arraySize() > 0: + return []string{driver.JobStatusSubmitted, driver.JobStatusPending, driver.JobStatusSucceeded} + case len(rec.Job.DependsOn) > 0: + return []string{ + driver.JobStatusSubmitted, driver.JobStatusPending, driver.JobStatusRunnable, + driver.JobStatusStarting, driver.JobStatusRunning, driver.JobStatusSucceeded, + } + default: + return []string{ + driver.JobStatusSubmitted, driver.JobStatusRunnable, driver.JobStatusStarting, + driver.JobStatusRunning, driver.JobStatusSucceeded, + } + } +} + +// transitions returns the statuses a job passed through since last (exclusive) +// up to current (inclusive). A FAILED job reports only FAILED: the emulator +// does not reconstruct how far it got before failing. +func transitions(rec *jobRecord, last, current string) []string { + if last == current { + return nil + } + + if current == driver.JobStatusFailed { + return []string{current} + } + + path := jobPath(rec) + + from, to := -1, -1 + + for i, s := range path { + if s == last { + from = i + } + + if s == current { + to = i + } + } + + if to < 0 || to <= from { + return []string{current} + } + + return path[from+1 : to+1] +} + +// publishJobEvents brings every job's published state up to date: for each job +// whose observed status moved since the last event, it publishes one "Batch +// Job State Change" per status passed through. It runs on every scheduler Tick +// and on every Batch job API call, so each transition is published once, +// shortly after the provider clock passes it. It reports whether anything was +// published. +func (m *Mock) publishJobEvents(ctx context.Context, now time.Time) bool { + m.jobsMu.Lock() + + o := m.newObserver(now) + + var pending []pendingJobEvent + + all := m.jobs.All() + for id := range all { + rec := all[id] + if isTerminal(rec.LastEmitted) { + continue + } + + j := o.render(&rec, -1) + + steps := transitions(&rec, rec.LastEmitted, j.Status) + if len(steps) == 0 { + continue + } + + for _, s := range steps { + pending = append(pending, pendingJobEvent{arn: j.JobARN, detail: eventDetail(&j, s)}) + } + + next := cloneJobRecord(rec) + next.LastEmitted = j.Status + m.jobs.Set(rec.Job.JobID, next) + } + + m.jobsMu.Unlock() + + for i := range pending { + m.events.Emit(ctx, eventSource, eventJobStateChange, pending[i].detail, pending[i].arn) + } + + return len(pending) > 0 +} + +// Tick publishes the state-change events of every job whose status moved by +// now. It has the config.Tickable signature of the shared serve scheduler, so a +// job that nobody polls still reports each transition shortly after it +// happens; Batch job API calls also publish, so nothing has to call it. +func (m *Mock) Tick(now time.Time) bool { + return m.publishJobEvents(context.Background(), now) +} + +// eventDetail renders j as it stood in status: times not yet reached in that +// status are dropped. +func eventDetail(j *driver.Job, status string) jobStateChangeDetail { + d := jobStateChangeDetail{ + JobArn: j.JobARN, JobName: j.JobName, JobID: j.JobID, JobQueue: j.JobQueue, + Status: status, Attempts: []any{}, CreatedAt: j.CreatedAt, + RetryStrategy: j.RetryStrategy, DependsOn: make([]jobEventDependency, 0, len(j.DependsOn)), + JobDefinition: j.JobDefinition, Parameters: j.Parameters, Container: j.Container, + NodeProperties: j.NodeProperties, EcsProperties: j.EcsProperties, EksProperties: j.EksProperties, + Timeout: j.Timeout, Tags: j.Tags, PlatformCapabilities: j.PlatformCapabilities, + ShareIdentifier: j.ShareIdentifier, SchedulingPriority: j.SchedulingPriority, + } + + if d.Parameters == nil { + d.Parameters = map[string]string{} + } + + if d.PlatformCapabilities == nil { + d.PlatformCapabilities = []string{} + } + + if j.PropagateTags != nil { + d.PropagateTags = *j.PropagateTags + } + + for _, dep := range j.DependsOn { + d.DependsOn = append(d.DependsOn, jobEventDependency(dep)) + } + + if ap := j.ArrayProperties; ap != nil { + d.ArrayProperties = &jobEventArray{Size: ap.Size, Index: ap.Index, StatusSummary: ap.StatusSummary} + } + + if status != j.Status { + // An intermediate status replayed after the fact: report only what was + // true then. The container hadn't exited yet, so it has no exitCode. + d.Container = withoutExitCode(j.Container) + + return d + } + + d.StatusReason = j.StatusReason + d.StartedAt = j.StartedAt + d.StoppedAt = j.StoppedAt + d.IsCancelled = j.IsCancelled + d.IsTerminated = j.IsTerminated + + return d +} diff --git a/providers/aws/batch/job_definition.go b/providers/aws/batch/job_definition.go index 032b1fe69..1c8bc54d7 100644 --- a/providers/aws/batch/job_definition.go +++ b/providers/aws/batch/job_definition.go @@ -28,7 +28,10 @@ func cloneJobDefinition(jd driver.JobDefinition) driver.JobDefinition { // RegisterJobDefinition registers a new revision. A repeated name yields the // next revision (N+1) with a new ARN; prior revisions remain ACTIVE until -// deregistered. platformCapabilities defaults to [EC2]. +// deregistered. platformCapabilities is stored as sent: real Batch treats an +// empty list as EC2 but doesn't echo a default, so DescribeJobDefinitions +// returns none (Terraform's acceptance tests assert platform_capabilities.# = 0 +// for container and multinode definitions registered without it). // //nolint:gocritic // in is the driver RegisterJobDefinitionInput, taken by value to match the interface func (m *Mock) RegisterJobDefinition( @@ -38,13 +41,12 @@ func (m *Mock) RegisterJobDefinition( return nil, cerrors.New(cerrors.InvalidArgument, "jobDefinitionName is required") } - if in.Type == "" { - return nil, cerrors.New(cerrors.InvalidArgument, "type is required (container or multinode)") + if err := validateTags(in.Tags); err != nil { + return nil, err } - caps := in.PlatformCapabilities - if len(caps) == 0 { - caps = []string{"EC2"} + if err := validateJobDefinitionType(&in); err != nil { + return nil, err } m.registerMu.Lock() @@ -65,7 +67,7 @@ func (m *Mock) RegisterJobDefinition( RetryStrategy: copyRaw(in.RetryStrategy), Timeout: copyRaw(in.Timeout), Parameters: copyTags(in.Parameters), - PlatformCapabilities: copyStrings(caps), + PlatformCapabilities: copyStrings(in.PlatformCapabilities), PropagateTags: copyBool(in.PropagateTags), SchedulingPriority: copyInt32(in.SchedulingPriority), Tags: copyTags(in.Tags), @@ -180,3 +182,28 @@ func (m *Mock) sortedJobDefs() []driver.JobDefinition { return out } + +// validateJobDefinitionType enforces the RegisterJobDefinition type enum +// (container | multinode) and the properties each type requires: a container +// job definition needs containerProperties, ecsProperties or eksProperties; a +// multinode one needs nodeProperties. +func validateJobDefinitionType(in *driver.RegisterJobDefinitionInput) error { + switch in.Type { + case "": + return cerrors.New(cerrors.InvalidArgument, "type is required (container or multinode)") + case driver.JDTypeContainer: + if !isSet(in.ContainerProperties) && !isSet(in.EcsProperties) && !isSet(in.EksProperties) { + return cerrors.New(cerrors.InvalidArgument, + "a container job definition requires containerProperties, ecsProperties or eksProperties") + } + case driver.JDTypeMultiNode: + if !isSet(in.NodeProperties) { + return cerrors.New(cerrors.InvalidArgument, "a multinode job definition requires nodeProperties") + } + default: + return cerrors.Newf(cerrors.InvalidArgument, + "invalid job definition type %q; valid values are container and multinode", in.Type) + } + + return nil +} diff --git a/providers/aws/batch/job_lifecycle.go b/providers/aws/batch/job_lifecycle.go new file mode 100644 index 000000000..f2c319853 --- /dev/null +++ b/providers/aws/batch/job_lifecycle.go @@ -0,0 +1,568 @@ +package batch + +import ( + "encoding/json" + "strconv" + "strings" + "time" + + "github.com/stackshy/cloudemu/v2/services/batch/driver" +) + +// Status reasons the emulator reports. "Essential container in task exited" is +// what Batch reports once a job's container exits; "Dependent Job failed" is +// the reason a job fails because one of its dependencies failed. +const ( + reasonContainerExited = "Essential container in task exited" + reasonDependentFailed = "Dependent Job failed" +) + +// Phase offsets, in units of a job's phase duration, measured from the instant +// it became ready (dependencies met): RUNNABLE for one phase, STARTING for the +// next, then RUNNING for the job's run dwell before SUCCEEDED. +const ( + phasesToStarting = 1 + phasesToRunning = 2 +) + +// jobStop records a CancelJob/TerminateJob that took effect on a job (or one +// array child) at At. StartedAt is when that unit had started RUNNING (zero if +// it never did), so a terminated running job keeps its startedAt. +type jobStop struct { + At time.Time `json:"at"` + Reason string `json:"reason"` + Terminate bool `json:"terminate"` + StartedAt time.Time `json:"startedAt,omitempty"` +} + +// jobRecord is a stored job. Job holds the static detail captured at +// submission; status, statusReason and the started/stopped times are derived +// on every read from SubmittedAt, Phase and the job's dependencies, the way the +// settle overlay derives other services' transient states: no goroutines or +// timers, advancing the clock is what moves a job through its lifecycle. +// +// Phase is the dwell time in each non-terminal phase except RUNNING, which +// lasts RunPhase; zero (AsyncSettle off) means the job is SUCCEEDED as soon as +// its dependencies are. A record restored from a snapshot taken before +// RunPhase existed has it zero and runs for Phase. Array children +// are not stored individually: child i's state is derived from the parent +// record, with ChildStops (keyed by the decimal index) holding any child that +// was canceled or terminated. +type jobRecord struct { + Job driver.Job `json:"job"` + Seq int64 `json:"seq"` + SubmittedAt time.Time `json:"submittedAt"` + Phase time.Duration `json:"phase"` + RunPhase time.Duration `json:"runPhase,omitempty"` + Sequential bool `json:"sequential,omitempty"` + Stop *jobStop `json:"stop,omitempty"` + ChildStops map[string]jobStop `json:"childStops,omitempty"` + LastEmitted string `json:"lastEmitted,omitempty"` +} + +//nolint:gocritic // clone intentionally takes a value to return an independent copy-on-write copy +func cloneJobRecord(r jobRecord) jobRecord { + r.Job = cloneJob(r.Job) + + if r.Stop != nil { + s := *r.Stop + r.Stop = &s + } + + if r.ChildStops != nil { + cs := make(map[string]jobStop, len(r.ChildStops)) + for k, v := range r.ChildStops { + cs[k] = v + } + + r.ChildStops = cs + } + + return r +} + +//nolint:gocritic // clone intentionally takes a value to return an independent copy-on-write copy +func cloneJob(j driver.Job) driver.Job { + if j.DependsOn != nil { + deps := make([]driver.JobDependency, len(j.DependsOn)) + copy(deps, j.DependsOn) + j.DependsOn = deps + } + + if j.ArrayProperties != nil { + ap := *j.ArrayProperties + ap.Index = copyInt32(ap.Index) + + if ap.StatusSummary != nil { + ss := make(map[string]int32, len(ap.StatusSummary)) + for k, v := range ap.StatusSummary { + ss[k] = v + } + + ap.StatusSummary = ss + } + + j.ArrayProperties = &ap + } + + j.Parameters = copyTags(j.Parameters) + j.Container = copyRaw(j.Container) + j.NodeProperties = copyRaw(j.NodeProperties) + j.EcsProperties = copyRaw(j.EcsProperties) + j.EksProperties = copyRaw(j.EksProperties) + j.RetryStrategy = copyRaw(j.RetryStrategy) + j.Timeout = copyRaw(j.Timeout) + j.PlatformCapabilities = copyStrings(j.PlatformCapabilities) + j.PropagateTags = copyBool(j.PropagateTags) + j.SchedulingPriority = copyInt32(j.SchedulingPriority) + j.Tags = copyTags(j.Tags) + + return j +} + +// runDwell is how long the job stays RUNNING. +func (r *jobRecord) runDwell() time.Duration { + if r.RunPhase > 0 { + return r.RunPhase + } + + return r.Phase +} + +func (r *jobRecord) arraySize() int { + if r.Job.ArrayProperties == nil { + return 0 + } + + return int(r.Job.ArrayProperties.Size) +} + +// jobView is a job's (or array child's) state at one observation instant. +type jobView struct { + Status string + Reason string + StartedAt time.Time + StoppedAt time.Time + Stop *jobStop +} + +func isTerminal(status string) bool { + return status == driver.JobStatusSucceeded || status == driver.JobStatusFailed +} + +// cancellable reports whether CancelJob affects a job in status: only jobs that +// haven't reached STARTING are canceled. +func cancellable(status string) bool { + switch status { + case driver.JobStatusSubmitted, driver.JobStatusPending, driver.JobStatusRunnable: + return true + default: + return false + } +} + +func later(a, b time.Time) time.Time { + if b.After(a) { + return b + } + + return a +} + +func stoppedView(s *jobStop) jobView { + return jobView{Status: driver.JobStatusFailed, Reason: s.Reason, StartedAt: s.StartedAt, StoppedAt: s.At, Stop: s} +} + +// observer derives job states at one instant, memoizing each job (and each +// array's children) so a dependency chain or a large array is evaluated once +// per call. It reads the job store without locks of its own; callers that +// mutate records hold jobsMu. +type observer struct { + m *Mock + now time.Time + views map[string]jobView + children map[string][]jobView +} + +func (m *Mock) newObserver(now time.Time) *observer { + return &observer{m: m, now: now, views: map[string]jobView{}, children: map[string][]jobView{}} +} + +// splitChildID splits "parent:index" into its parts. +func splitChildID(id string) (parent string, idx int, ok bool) { + i := strings.LastIndex(id, ":") + if i <= 0 { + return "", 0, false + } + + // Parse with a 32-bit bound: the index ends up in the int32 + // ArrayProperties.Index field. + n, err := strconv.ParseInt(id[i+1:], 10, 32) + if err != nil || n < 0 { + return "", 0, false + } + + return id[:i], int(n), true +} + +// lookup resolves a job ID to its record and, for an array child, its index +// (-1 for a top-level job). +func (m *Mock) lookup(id string) (rec jobRecord, idx int, ok bool) { + if r, found := m.jobs.Get(id); found { + return r, -1, true + } + + parent, i, isChild := splitChildID(id) + if !isChild { + return jobRecord{}, 0, false + } + + r, found := m.jobs.Get(parent) + if !found || i >= r.arraySize() { + return jobRecord{}, 0, false + } + + return r, i, true +} + +// viewID returns the state of the job (or array child) with the given ID. +func (o *observer) viewID(id string) (jobView, bool) { + rec, idx, ok := o.m.lookup(id) + if !ok { + return jobView{}, false + } + + if idx >= 0 { + return o.childViews(&rec)[idx], true + } + + return o.view(&rec), true +} + +// view returns the state of a top-level job or array parent. +func (o *observer) view(rec *jobRecord) jobView { + if v, ok := o.views[rec.Job.JobID]; ok { + return v + } + + var v jobView + + switch { + case rec.arraySize() > 0: + v = o.parentView(rec) + case rec.Stop != nil: + v = stoppedView(rec.Stop) + default: + v = o.natural(rec, -1, nil) + } + + o.views[rec.Job.JobID] = v + + return v +} + +// childViews returns the state of every child of an array job, in index order. +func (o *observer) childViews(rec *jobRecord) []jobView { + if cv, ok := o.children[rec.Job.JobID]; ok { + return cv + } + + n := rec.arraySize() + cv := make([]jobView, n) + + for i := 0; i < n; i++ { + if s, ok := rec.ChildStops[strconv.Itoa(i)]; ok { + cv[i] = stoppedView(&s) + + continue + } + + var prev *jobView + if rec.Sequential && i > 0 { + prev = &cv[i-1] + } + + cv[i] = o.natural(rec, i, prev) + } + + o.children[rec.Job.JobID] = cv + + return cv +} + +// childAggregate summarizes an array's children. +type childAggregate struct { + started, stopped time.Time + failed, open bool +} + +func aggregateChildren(cv []jobView) childAggregate { + var a childAggregate + + for i := range cv { + c := &cv[i] + if !isTerminal(c.Status) { + a.open = true + + continue + } + + a.failed = a.failed || c.Status == driver.JobStatusFailed + + if !c.StartedAt.IsZero() && (a.started.IsZero() || c.StartedAt.Before(a.started)) { + a.started = c.StartedAt + } + + a.stopped = later(a.stopped, c.StoppedAt) + } + + return a +} + +// parentView aggregates an array parent from its children: the parent stays +// PENDING while any child is unfinished, then SUCCEEDED if every child +// succeeded and FAILED otherwise. +func (o *observer) parentView(rec *jobRecord) jobView { + a := aggregateChildren(o.childViews(rec)) + + if a.open { + if o.now.Before(rec.SubmittedAt.Add(rec.Phase)) { + return jobView{Status: driver.JobStatusSubmitted} + } + + return jobView{Status: driver.JobStatusPending} + } + + v := jobView{Status: driver.JobStatusSucceeded, StartedAt: a.started, StoppedAt: a.stopped, Stop: rec.Stop} + if a.failed { + v.Status = driver.JobStatusFailed + } + + if rec.Stop != nil { + v.Reason = rec.Stop.Reason + } + + return v +} + +// depState folds one dependency into the readiness computation: it returns the +// later ready time, whether the job must keep waiting, and whether the +// dependency failed. +func depState(dv *jobView, ready time.Time) (next time.Time, waiting, failed bool) { + switch dv.Status { + case driver.JobStatusSucceeded: + return later(ready, dv.StoppedAt), false, false + case driver.JobStatusFailed: + return later(ready, dv.StoppedAt), false, true + default: + return ready, true, false + } +} + +// dependencyID returns the job a dependency waits on for array child idx: an +// N_TO_N dependency on an array job pairs child idx with the dependency's child +// idx; anything else waits on the dependency job as a whole. +func (o *observer) dependencyID(dep driver.JobDependency, idx int) string { + if dep.Type != driver.DependencyNToN || idx < 0 { + return dep.JobID + } + + if r, ok := o.m.jobs.Get(dep.JobID); ok && idx < r.arraySize() { + return dep.JobID + ":" + strconv.Itoa(idx) + } + + return dep.JobID +} + +// natural derives the state of a job (idx -1) or array child (idx >= 0) that +// was not canceled or terminated. prev is the previous child of a SEQUENTIAL +// array. Each non-terminal phase lasts rec.Phase: SUBMITTED from submission, +// PENDING while a dependency is unfinished, then RUNNABLE, STARTING and RUNNING +// before SUCCEEDED. +func (o *observer) natural(rec *jobRecord, idx int, prev *jobView) jobView { + ready := rec.SubmittedAt.Add(rec.Phase) + if o.now.Before(ready) { + return jobView{Status: driver.JobStatusSubmitted} + } + + ready, blocked, done := o.readiness(rec, idx, prev, ready) + if done { + return blocked + } + + return o.phaseView(ready, rec.Phase, rec.runDwell()) +} + +// readiness folds the job's dependencies (and a SEQUENTIAL predecessor) into +// the instant it became ready. done reports that the dependencies decide the +// state on their own (PENDING, or FAILED because one failed), returned as view. +func (o *observer) readiness(rec *jobRecord, idx int, prev *jobView, ready time.Time) (time.Time, jobView, bool) { + deps := make([]jobView, 0, len(rec.Job.DependsOn)+1) + + for _, dep := range rec.Job.DependsOn { + if dep.JobID == "" { + continue + } + + if dv, ok := o.viewID(o.dependencyID(dep, idx)); ok { + deps = append(deps, dv) + } + } + + if prev != nil { + deps = append(deps, *prev) + } + + waiting := false + + for i := range deps { + next, wait, failed := depState(&deps[i], ready) + if failed { + return ready, jobView{Status: driver.JobStatusFailed, Reason: reasonDependentFailed, StoppedAt: next}, true + } + + ready = next + waiting = waiting || wait + } + + if waiting { + return ready, jobView{Status: driver.JobStatusPending}, true + } + + return ready, jobView{}, false +} + +// phaseView places a ready job in its RUNNABLE/STARTING/RUNNING/SUCCEEDED +// phase given the phase duration d and the RUNNING dwell run. +func (o *observer) phaseView(ready time.Time, d, run time.Duration) jobView { + started := ready.Add(phasesToRunning * d) + stopped := started.Add(run) + + switch { + case o.now.Before(ready.Add(phasesToStarting * d)): + return jobView{Status: driver.JobStatusRunnable} + case o.now.Before(started): + return jobView{Status: driver.JobStatusStarting} + case o.now.Before(stopped): + return jobView{Status: driver.JobStatusRunning, StartedAt: started} + default: + return jobView{ + Status: driver.JobStatusSucceeded, Reason: reasonContainerExited, + StartedAt: started, StoppedAt: stopped, + } + } +} + +func millis(t time.Time) int64 { + if t.IsZero() { + return 0 + } + + return t.UnixMilli() +} + +// statusSummary counts an array's children per status, listing every status. +func statusSummary(cv []jobView) map[string]int32 { + out := map[string]int32{ + driver.JobStatusSubmitted: 0, driver.JobStatusPending: 0, driver.JobStatusRunnable: 0, + driver.JobStatusStarting: 0, driver.JobStatusRunning: 0, driver.JobStatusSucceeded: 0, + driver.JobStatusFailed: 0, + } + + for i := range cv { + out[cv[i].Status]++ + } + + return out +} + +// withExitCode returns the container detail with exitCode 0 added, as Batch +// reports for a job whose container exited successfully. +func withExitCode(container json.RawMessage) json.RawMessage { + if !isSet(container) { + return container + } + + var obj map[string]json.RawMessage + if err := json.Unmarshal(container, &obj); err != nil { + return container + } + + obj["exitCode"] = json.RawMessage("0") + + out, err := json.Marshal(obj) + if err != nil { + return container + } + + return out +} + +// withoutExitCode returns the container detail with any exitCode removed, for +// an event replaying a status the job held before its container exited. +func withoutExitCode(container json.RawMessage) json.RawMessage { + if !isSet(container) { + return container + } + + var obj map[string]json.RawMessage + if err := json.Unmarshal(container, &obj); err != nil { + return container + } + + if _, ok := obj["exitCode"]; !ok { + return container + } + + delete(obj, "exitCode") + + out, err := json.Marshal(obj) + if err != nil { + return container + } + + return out +} + +// render builds the externally visible job (top-level, idx -1, or array child +// idx) as observed by o. +func (o *observer) render(rec *jobRecord, idx int) driver.Job { + j := cloneJob(rec.Job) + + var v jobView + + if idx >= 0 { + v = o.childViews(rec)[idx] + j.JobID = rec.Job.JobID + ":" + strconv.Itoa(idx) + j.JobARN = rec.Job.JobARN + ":" + strconv.Itoa(idx) + i := int32(idx) + j.ArrayProperties = &driver.ArrayProperties{Index: &i} + } else { + v = o.view(rec) + + if rec.arraySize() > 0 { + j.ArrayProperties.StatusSummary = statusSummary(o.childViews(rec)) + } + } + + j.Status = v.Status + j.StatusReason = v.Reason + j.StartedAt = millis(v.StartedAt) + j.StoppedAt = millis(v.StoppedAt) + + stop := v.Stop + if idx < 0 && rec.Stop != nil { + stop = rec.Stop + } + + if stop != nil { + j.IsCancelled = !stop.Terminate + j.IsTerminated = stop.Terminate + } + + if v.Status == driver.JobStatusSucceeded && (idx >= 0 || rec.arraySize() == 0) { + j.Container = withExitCode(j.Container) + } + + return j +} diff --git a/providers/aws/batch/job_queue.go b/providers/aws/batch/job_queue.go index dd0508cb4..99506a0b8 100644 --- a/providers/aws/batch/job_queue.go +++ b/providers/aws/batch/job_queue.go @@ -38,6 +38,10 @@ func (m *Mock) CreateJobQueue(_ context.Context, in driver.CreateJobQueueInput) return nil, cerrors.New(cerrors.InvalidArgument, "jobQueueName is required") } + if err := validateTags(in.Tags); err != nil { + return nil, err + } + if len(in.ComputeEnvironmentOrder) == 0 { return nil, cerrors.New(cerrors.InvalidArgument, "computeEnvironmentOrder must list at least one compute environment") } @@ -46,6 +50,20 @@ func (m *Mock) CreateJobQueue(_ context.Context, in driver.CreateJobQueueInput) return nil, err } + m.policyBindMu.Lock() + defer m.policyBindMu.Unlock() + + policyARN := "" + + if in.SchedulingPolicyARN != "" { + arn, err := m.resolveSchedulingPolicy(in.SchedulingPolicyARN) + if err != nil { + return nil, err + } + + policyARN = arn + } + state := in.State if state == "" { state = driver.StateEnabled @@ -59,7 +77,7 @@ func (m *Mock) CreateJobQueue(_ context.Context, in driver.CreateJobQueueInput) StatusReason: jqHealthyReason, Priority: in.Priority, ComputeEnvironmentOrder: cloneOrder(in.ComputeEnvironmentOrder), - SchedulingPolicyARN: in.SchedulingPolicyARN, + SchedulingPolicyARN: policyARN, Tags: copyTags(in.Tags), } @@ -88,9 +106,24 @@ func (m *Mock) UpdateJobQueue(_ context.Context, in driver.UpdateJobQueueInput) } } + // Held across the read, the policy resolution and the write, so the policy + // can't be deleted (and no other update can land) in between. + m.policyBindMu.Lock() + defer m.policyBindMu.Unlock() + + cur, ok := m.jobQueues.Get(name) + if !ok { + return nil, cerrors.Newf(cerrors.NotFound, "job queue %q does not exist", name) + } + + policyARN, err := m.nextSchedulingPolicy(&cur, in.SchedulingPolicyARN) + if err != nil { + return nil, err + } + var updated driver.JobQueue - ok := m.jobQueues.Update(name, func(q driver.JobQueue) driver.JobQueue { + ok = m.jobQueues.Update(name, func(q driver.JobQueue) driver.JobQueue { next := cloneJobQueue(q) if in.State != nil { next.State = *in.State @@ -100,9 +133,7 @@ func (m *Mock) UpdateJobQueue(_ context.Context, in driver.UpdateJobQueueInput) next.Priority = *in.Priority } - if in.SchedulingPolicyARN != nil { - next.SchedulingPolicyARN = *in.SchedulingPolicyARN - } + next.SchedulingPolicyARN = policyARN if len(in.ComputeEnvironmentOrder) > 0 { next.ComputeEnvironmentOrder = cloneOrder(in.ComputeEnvironmentOrder) @@ -159,3 +190,26 @@ func (m *Mock) validateOrder(order []driver.ComputeEnvironmentOrder) error { return nil } + +// nextSchedulingPolicy resolves the scheduling-policy ARN an UpdateJobQueue +// leaves on the queue. Per the CreateJobQueue reference, a FIFO queue (no +// policy) can't have one added, and a fair-share queue's policy can be replaced +// but not removed. A nil ref leaves the policy unchanged. +func (m *Mock) nextSchedulingPolicy(q *driver.JobQueue, ref *string) (string, error) { + if ref == nil { + return q.SchedulingPolicyARN, nil + } + + switch { + case *ref == "" && q.SchedulingPolicyARN == "": + return "", nil + case *ref == "": + return "", cerrors.Newf(cerrors.InvalidArgument, + "the scheduling policy of job queue %q can be replaced but not removed", q.Name) + case q.SchedulingPolicyARN == "": + return "", cerrors.Newf(cerrors.InvalidArgument, + "job queue %q has no scheduling policy (FIFO) and can't have one added", q.Name) + } + + return m.resolveSchedulingPolicy(*ref) +} diff --git a/providers/aws/batch/jobs.go b/providers/aws/batch/jobs.go new file mode 100644 index 000000000..1ad6ca11f --- /dev/null +++ b/providers/aws/batch/jobs.go @@ -0,0 +1,955 @@ +package batch + +import ( + "context" + "encoding/base64" + "encoding/json" + "regexp" + "sort" + "strconv" + "strings" + "time" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/internal/idgen" + "github.com/stackshy/cloudemu/v2/internal/settle" + "github.com/stackshy/cloudemu/v2/services/batch/driver" +) + +// SubmitJob / CancelJob / TerminateJob / DescribeJobs bounds from the Batch API +// reference. +const ( + minArraySize = 2 + maxArraySize = 10000 + maxDependencies = 20 + maxSchedulingPriority = 9999 + minAttemptDuration = 60 + maxReasonLen = 1024 + maxDescribeJobs = 100 + + jobSeqKey = "seq" +) + +// jobNameRE is the SubmitJob jobName rule: up to 128 characters, the first +// alphanumeric, then letters, numbers, hyphens and underscores. +var jobNameRE = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9_-]{0,127}$`) + +// SubmitJob validates and records a job. The job then advances through its +// lifecycle on the configured clock (see natural). Jobs run no workload: a job +// whose dependencies succeed always succeeds. +// +//nolint:gocritic // in is the driver SubmitJobInput, taken by value to match the interface +func (m *Mock) SubmitJob(ctx context.Context, in driver.SubmitJobInput) (*driver.Job, error) { + if err := validateSubmitShape(&in); err != nil { + return nil, err + } + + q, err := m.submitQueue(in.JobQueue) + if err != nil { + return nil, err + } + + if shareErr := validateShare(&q, &in); shareErr != nil { + return nil, shareErr + } + + jd, err := m.resolveJobDefinition(in.JobDefinition) + if err != nil { + return nil, err + } + + job, err := m.buildJob(&in, &q, &jd) + if err != nil { + return nil, err + } + + m.jobsMu.Lock() + + if err := m.validateDependencies(in.DependsOn); err != nil { + m.jobsMu.Unlock() + + return nil, err + } + + seq, _ := m.jobSeq.Get(jobSeqKey) + seq++ + m.jobSeq.Set(jobSeqKey, seq) + + now := m.opts.Clock.Now() + job.CreatedAt = now.UnixMilli() + + rec := jobRecord{ + Job: job, + Seq: seq, + SubmittedAt: now, + Phase: m.opts.SettleDuration(settle.DefaultBatchJobPhase), + RunPhase: m.opts.SettleDuration(settle.DefaultBatchJobRunDwell), + Sequential: hasSequential(in.DependsOn), + LastEmitted: driver.JobStatusSubmitted, + } + + m.jobs.Set(job.JobID, rec) + + m.jobsMu.Unlock() + + m.publishJobEvents(ctx, m.opts.Clock.Now()) + + out := cloneJob(job) + + return &out, nil +} + +// validateSubmitShape checks the request fields that need no stored state. +func validateSubmitShape(in *driver.SubmitJobInput) error { + if err := validateSubmitRequired(in); err != nil { + return err + } + + if err := validateSubmitLimits(in); err != nil { + return err + } + + if err := validateTimeout(in.Timeout); err != nil { + return err + } + + return validateDependencyShape(in.DependsOn, in.ArraySize != nil) +} + +// validateSubmitRequired checks the required SubmitJob fields and the job name +// rule. +func validateSubmitRequired(in *driver.SubmitJobInput) error { + switch { + case in.JobName == "": + return cerrors.New(cerrors.InvalidArgument, "jobName is required") + case !jobNameRE.MatchString(in.JobName): + return cerrors.Newf(cerrors.InvalidArgument, + "jobName %q must be up to 128 characters, start with a letter or number, "+ + "and contain only letters, numbers, hyphens and underscores", in.JobName) + case in.JobQueue == "": + return cerrors.New(cerrors.InvalidArgument, "jobQueue is required") + case in.JobDefinition == "": + return cerrors.New(cerrors.InvalidArgument, "jobDefinition is required") + default: + return nil + } +} + +// validateSubmitLimits checks the documented SubmitJob numeric bounds and the +// job's tags. +func validateSubmitLimits(in *driver.SubmitJobInput) error { + if in.ArraySize != nil && (*in.ArraySize < minArraySize || *in.ArraySize > maxArraySize) { + return cerrors.Newf(cerrors.InvalidArgument, + "arrayProperties.size must be between %d and %d, got %d", minArraySize, maxArraySize, *in.ArraySize) + } + + if len(in.DependsOn) > maxDependencies { + return cerrors.Newf(cerrors.InvalidArgument, + "a job can depend on at most %d jobs, got %d", maxDependencies, len(in.DependsOn)) + } + + if p := in.SchedulingPriorityOverride; p != nil && (*p < 0 || *p > maxSchedulingPriority) { + return cerrors.Newf(cerrors.InvalidArgument, + "schedulingPriorityOverride must be between 0 and %d", maxSchedulingPriority) + } + + return validateTags(in.Tags) +} + +// validateTimeout enforces the 60-second minimum attemptDurationSeconds. +func validateTimeout(raw json.RawMessage) error { + if !isSet(raw) { + return nil + } + + var t struct { + AttemptDurationSeconds *int64 `json:"attemptDurationSeconds"` + } + + if err := json.Unmarshal(raw, &t); err != nil { + return cerrors.Newf(cerrors.InvalidArgument, "invalid timeout: %v", err) + } + + if t.AttemptDurationSeconds != nil && *t.AttemptDurationSeconds < minAttemptDuration { + return cerrors.Newf(cerrors.InvalidArgument, + "timeout.attemptDurationSeconds must be at least %d seconds", minAttemptDuration) + } + + return nil +} + +// validateDependencyShape checks dependency types: a dependency without a job +// ID is only meaningful as an array job's SEQUENTIAL dependency, and the +// N_TO_N/SEQUENTIAL array dependency types apply only to array jobs. +func validateDependencyShape(deps []driver.JobDependency, isArray bool) error { + for _, d := range deps { + switch d.Type { + case "": + case driver.DependencyNToN, driver.DependencySequential: + if !isArray { + return cerrors.Newf(cerrors.InvalidArgument, "dependency type %s is only valid for array jobs", d.Type) + } + default: + return cerrors.Newf(cerrors.InvalidArgument, + "invalid dependency type %q; valid values are N_TO_N and SEQUENTIAL", d.Type) + } + + if d.JobID == "" && d.Type != driver.DependencySequential { + return cerrors.New(cerrors.InvalidArgument, "dependsOn.jobId is required unless the type is SEQUENTIAL") + } + } + + return nil +} + +func hasSequential(deps []driver.JobDependency) bool { + for _, d := range deps { + if d.Type == driver.DependencySequential { + return true + } + } + + return false +} + +// validateDependencies requires every referenced job to exist. Callers hold +// jobsMu. +func (m *Mock) validateDependencies(deps []driver.JobDependency) error { + for _, d := range deps { + if d.JobID == "" { + continue + } + + if _, _, ok := m.lookup(d.JobID); !ok { + return cerrors.Newf(cerrors.InvalidArgument, "dependency job %q does not exist", d.JobID) + } + } + + return nil +} + +// submitQueue resolves the target queue, which must exist and be ENABLED (a +// DISABLED queue accepts no new jobs). +func (m *Mock) submitQueue(ref string) (driver.JobQueue, error) { + q, ok := m.jobQueues.Get(nameFromARN(kindJobQueue, ref)) + if !ok { + return driver.JobQueue{}, cerrors.Newf(cerrors.InvalidArgument, "job queue %q does not exist", ref) + } + + if q.State != driver.StateEnabled { + return driver.JobQueue{}, cerrors.Newf(cerrors.InvalidArgument, + "job queue %q is %s and can't accept new jobs", q.Name, q.State) + } + + return q, nil +} + +// validateShare enforces the shareIdentifier rule: required on a queue with a +// fair-share scheduling policy, forbidden on a FIFO queue. +func validateShare(q *driver.JobQueue, in *driver.SubmitJobInput) error { + if q.SchedulingPolicyARN != "" && in.ShareIdentifier == "" { + return cerrors.Newf(cerrors.InvalidArgument, + "job queue %q has a scheduling policy, so shareIdentifier must be specified", q.Name) + } + + if q.SchedulingPolicyARN == "" && in.ShareIdentifier != "" { + return cerrors.Newf(cerrors.InvalidArgument, + "job queue %q has no scheduling policy, so shareIdentifier must not be specified", q.Name) + } + + return nil +} + +// resolveJobDefinition resolves name, name:revision, or an ARN with or without +// a revision. Without a revision the latest ACTIVE revision is used; an +// explicit revision must be ACTIVE. +func (m *Mock) resolveJobDefinition(ref string) (driver.JobDefinition, error) { + key := nameFromARN(kindJobDefinition, ref) + + if strings.Contains(key, ":") { + jd, ok := m.jobDefs.Get(key) + if !ok { + return driver.JobDefinition{}, cerrors.Newf(cerrors.InvalidArgument, "job definition %q does not exist", ref) + } + + // A deregistered revision can't be submitted to, even by explicit + // revision. + if jd.Status != driver.JDStatusActive { + return driver.JobDefinition{}, cerrors.Newf(cerrors.InvalidArgument, + "job definition %q is %s and can't be used to submit jobs", ref, jd.Status) + } + + return jd, nil + } + + var ( + latest driver.JobDefinition + found bool + ) + + defs := m.sortedJobDefs() + for i := range defs { + if defs[i].Name == key && defs[i].Status == driver.JDStatusActive { + latest, found = defs[i], true + } + } + + if !found { + return driver.JobDefinition{}, cerrors.Newf(cerrors.InvalidArgument, + "job definition %q has no ACTIVE revision", ref) + } + + return latest, nil +} + +// buildJob assembles the static detail of a new job from the request, the +// queue and the job definition (submit-time values override the definition's). +func (m *Mock) buildJob(in *driver.SubmitJobInput, q *driver.JobQueue, jd *driver.JobDefinition) (driver.Job, error) { + container, err := mergeContainer(jd.ContainerProperties, in.ContainerOverrides) + if err != nil { + return driver.Job{}, err + } + + props, err := applyPropertyOverrides(in, jd) + if err != nil { + return driver.Job{}, err + } + + id := idgen.UUID() + + job := driver.Job{ + JobID: id, + JobARN: m.arn(kindJob, id), + JobName: in.JobName, + JobQueue: q.ARN, + JobDefinition: jd.ARN, + DependsOn: cloneDeps(in.DependsOn), + Parameters: mergeParams(jd.Parameters, in.Parameters), + Container: container, + NodeProperties: props.node, + EcsProperties: props.ecs, + EksProperties: props.eks, + RetryStrategy: firstSet(in.RetryStrategy, jd.RetryStrategy), + Timeout: firstSet(in.Timeout, jd.Timeout), + PlatformCapabilities: copyStrings(jd.PlatformCapabilities), + PropagateTags: copyBool(jd.PropagateTags), + SchedulingPriority: copyInt32(jd.SchedulingPriority), + ShareIdentifier: in.ShareIdentifier, + Tags: copyTags(in.Tags), + } + + if in.PropagateTags != nil { + job.PropagateTags = copyBool(in.PropagateTags) + } + + if in.SchedulingPriorityOverride != nil { + job.SchedulingPriority = copyInt32(in.SchedulingPriorityOverride) + } + + if in.ArraySize != nil { + job.ArrayProperties = &driver.ArrayProperties{Size: *in.ArraySize} + } + + return job, nil +} + +func cloneDeps(in []driver.JobDependency) []driver.JobDependency { + if in == nil { + return nil + } + + out := make([]driver.JobDependency, len(in)) + copy(out, in) + + return out +} + +func firstSet(a, b json.RawMessage) json.RawMessage { + if isSet(a) { + return copyRaw(a) + } + + return copyRaw(b) +} + +func mergeParams(def, sub map[string]string) map[string]string { + if def == nil && sub == nil { + return nil + } + + out := copyTags(def) + if out == nil { + out = make(map[string]string, len(sub)) + } + + for k, v := range sub { + out[k] = v + } + + return out +} + +// mergeContainer applies SubmitJob containerOverrides to the job definition's +// containerProperties: environment variables and resource requirements are +// merged (by name / type), every other override replaces the property. +func mergeContainer(props, overrides json.RawMessage) (json.RawMessage, error) { + if !isSet(props) { + return nil, nil + } + + if !isSet(overrides) { + return copyRaw(props), nil + } + + var base, over map[string]json.RawMessage + + if err := json.Unmarshal(props, &base); err != nil { + return copyRaw(props), nil //nolint:nilerr // a non-object definition blob is echoed unchanged + } + + if err := json.Unmarshal(overrides, &over); err != nil { + return nil, cerrors.Newf(cerrors.InvalidArgument, "invalid containerOverrides: %v", err) + } + + for k, v := range over { + if !isSet(v) { + continue + } + + switch k { + case "environment": + base[k] = mergeKeyed(base[k], v, "name") + case "resourceRequirements": + base[k] = mergeKeyed(base[k], v, "type") + default: + base[k] = v + } + } + + out, err := json.Marshal(base) + if err != nil { + return nil, cerrors.Newf(cerrors.Internal, "merge container overrides: %v", err) + } + + return out, nil +} + +// mergeKeyed merges two JSON arrays of objects, replacing entries of base whose +// key field matches an entry of over and appending the rest. +func mergeKeyed(base, over json.RawMessage, key string) json.RawMessage { + var b, o []map[string]json.RawMessage + + if err := json.Unmarshal(over, &o); err != nil { + return over + } + + if isSet(base) { + if err := json.Unmarshal(base, &b); err != nil { + return over + } + } + + for _, entry := range o { + replaced := false + + for i := range b { + if string(b[i][key]) == string(entry[key]) { + b[i] = entry + replaced = true + + break + } + } + + if !replaced { + b = append(b, entry) + } + } + + out, err := json.Marshal(b) + if err != nil { + return over + } + + return out +} + +// DescribeJobs returns the requested jobs (top-level or array-child IDs) in +// request order, each observed now. Unknown IDs are omitted. +func (m *Mock) DescribeJobs(ctx context.Context, jobIDs []string) ([]driver.Job, error) { + if len(jobIDs) == 0 { + return nil, cerrors.New(cerrors.InvalidArgument, "jobs is required") + } + + if len(jobIDs) > maxDescribeJobs { + return nil, cerrors.Newf(cerrors.InvalidArgument, "jobs accepts at most %d job IDs, got %d", maxDescribeJobs, len(jobIDs)) + } + + m.publishJobEvents(ctx, m.opts.Clock.Now()) + + o := m.newObserver(m.opts.Clock.Now()) + out := make([]driver.Job, 0, len(jobIDs)) + + for _, id := range jobIDs { + rec, idx, ok := m.lookup(id) + if !ok { + continue + } + + out = append(out, o.render(&rec, idx)) + } + + return out, nil +} + +// ListJobs page sizes: up to 1000 results per page when listing by status and +// up to 100 when a filter is used (ListJobs API reference, maxResults). +const ( + listJobsStatusPage = 1000 + listJobsFilterPage = 100 +) + +// ListJobs lists the jobs of a queue (top-level jobs and array parents) or the +// children of an array job. Queue listings are newest first, as the ListJobs +// reference describes; children are listed by index. Exactly one of jobQueue, +// arrayJobId and multiNodeJobId must be given. Without a filter only jobs in +// jobStatus (default RUNNING) are returned. +// +// The page token is a cursor on the listing order (submission sequence, or +// child index), not an offset, so jobs submitted or changing status between +// pages never make a page skip or repeat an entry. +// +//nolint:gocritic // in is the driver ListJobsInput, taken by value to match the interface +func (m *Mock) ListJobs(ctx context.Context, in driver.ListJobsInput) (*driver.ListJobsOutput, error) { + if err := validateListJobs(&in); err != nil { + return nil, err + } + + limit, err := listJobsLimit(&in) + if err != nil { + return nil, err + } + + after, err := decodeListCursor(in.NextToken) + if err != nil { + return nil, err + } + + now := m.opts.Clock.Now() + m.publishJobEvents(ctx, now) + + l := jobLister{o: m.newObserver(now), in: &in, limit: limit, after: after} + status, useStatus := listStatus(&in) + l.status, l.useStatus = status, useStatus + + switch { + case in.MultiNodeJobID != "": + // Multi-node parallel jobs are not modeled node by node. + return &driver.ListJobsOutput{Jobs: []driver.Job{}}, nil + case in.ArrayJobID != "": + return l.children(m), nil + default: + return l.queue(m), nil + } +} + +// listJobsLimit resolves the page size: maxResults (at least 1) capped at the +// default page size for the kind of listing. +func listJobsLimit(in *driver.ListJobsInput) (int, error) { + limit := listJobsStatusPage + if len(in.Filters) > 0 { + limit = listJobsFilterPage + } + + if in.MaxResults == nil { + return limit, nil + } + + if *in.MaxResults < 1 { + return 0, cerrors.Newf(cerrors.InvalidArgument, "maxResults must be at least 1, got %d", *in.MaxResults) + } + + return min(limit, int(*in.MaxResults)), nil +} + +// decodeListCursor reads a ListJobs nextToken: the base64 of the listing +// position the previous page ended at. An empty token starts from the top. +func decodeListCursor(token string) (int64, error) { + if token == "" { + return -1, nil + } + + b, err := base64.RawURLEncoding.DecodeString(token) + if err != nil { + return 0, cerrors.New(cerrors.InvalidArgument, "invalid nextToken") + } + + n, err := strconv.ParseInt(string(b), 10, 64) + if err != nil || n < 0 { + return 0, cerrors.New(cerrors.InvalidArgument, "invalid nextToken") + } + + return n, nil +} + +func encodeListCursor(n int64) string { + return base64.RawURLEncoding.EncodeToString([]byte(strconv.FormatInt(n, 10))) +} + +// jobLister builds one ListJobs page. +type jobLister struct { + o *observer + in *driver.ListJobsInput + limit int + after int64 + status string + useStatus bool +} + +// queue lists a queue's top-level jobs newest first, resuming below the cursor +// sequence number. +func (l *jobLister) queue(m *Mock) *driver.ListJobsOutput { + queue := nameFromARN(kindJobQueue, l.in.JobQueue) + + recs := m.jobs.SortedValues() + sort.SliceStable(recs, func(i, j int) bool { return recs[i].Seq > recs[j].Seq }) + + out := &driver.ListJobsOutput{Jobs: []driver.Job{}} + + var lastSeq int64 + + for i := range recs { + rec := &recs[i] + if (l.after >= 0 && rec.Seq >= l.after) || nameFromARN(kindJobQueue, rec.Job.JobQueue) != queue { + continue + } + + j := l.o.render(rec, -1) + if (l.useStatus && j.Status != l.status) || !matchesFilters(&j, l.in.Filters) { + continue + } + + if len(out.Jobs) == l.limit { + out.NextToken = encodeListCursor(lastSeq) + + break + } + + out.Jobs = append(out.Jobs, j) + lastSeq = rec.Seq + } + + return out +} + +// children lists an array job's children by index, resuming after the cursor +// index. Filters apply to children the same way they apply to a queue listing. +func (l *jobLister) children(m *Mock) *driver.ListJobsOutput { + out := &driver.ListJobsOutput{Jobs: []driver.Job{}} + + rec, ok := m.jobs.Get(l.in.ArrayJobID) + if !ok { + return out + } + + for i := int(l.after + 1); i < rec.arraySize(); i++ { + j := l.o.render(&rec, i) + if (l.useStatus && j.Status != l.status) || !matchesFilters(&j, l.in.Filters) { + continue + } + + if len(out.Jobs) == l.limit { + out.NextToken = encodeListCursor(int64(*out.Jobs[len(out.Jobs)-1].ArrayProperties.Index)) + + break + } + + out.Jobs = append(out.Jobs, j) + } + + return out +} + +//nolint:gochecknoglobals // immutable enum set, read-only after init +var validJobStatuses = map[string]bool{ + driver.JobStatusSubmitted: true, driver.JobStatusPending: true, driver.JobStatusRunnable: true, + driver.JobStatusStarting: true, driver.JobStatusRunning: true, driver.JobStatusSucceeded: true, + driver.JobStatusFailed: true, +} + +//nolint:gochecknoglobals // immutable enum set, read-only after init +var validJobFilters = map[string]bool{ + driver.FilterJobName: true, driver.FilterJobDefinition: true, driver.FilterBeforeCreatedAt: true, + driver.FilterAfterCreatedAt: true, driver.FilterShareIdentifier: true, +} + +func validateListJobs(in *driver.ListJobsInput) error { + set := 0 + + for _, v := range []string{in.JobQueue, in.ArrayJobID, in.MultiNodeJobID} { + if v != "" { + set++ + } + } + + if set != 1 { + return cerrors.New(cerrors.InvalidArgument, + "specify exactly one of jobQueue, arrayJobId or multiNodeJobId") + } + + if in.JobStatus != "" && !validJobStatuses[in.JobStatus] { + return cerrors.Newf(cerrors.InvalidArgument, "invalid jobStatus %q", in.JobStatus) + } + + return validateJobFilters(in.Filters) +} + +// validateJobFilters checks filter names and values. Only one filter may be +// used at a time, except that SHARE_IDENTIFIER combines with the others. +func validateJobFilters(filters []driver.JobFilter) error { + primary := 0 + + for i := range filters { + f := &filters[i] + if !validJobFilters[f.Name] { + return cerrors.Newf(cerrors.InvalidArgument, "invalid filter name %q", f.Name) + } + + if f.Name != driver.FilterShareIdentifier { + primary++ + } + + if err := validateCreatedAtValues(f); err != nil { + return err + } + } + + if primary > 1 { + return cerrors.New(cerrors.InvalidArgument, "only one filter can be used at a time") + } + + return nil +} + +// validateCreatedAtValues requires BEFORE/AFTER_CREATED_AT values to be epoch +// milliseconds. +func validateCreatedAtValues(f *driver.JobFilter) error { + if f.Name != driver.FilterBeforeCreatedAt && f.Name != driver.FilterAfterCreatedAt { + return nil + } + + for _, v := range f.Values { + if _, err := strconv.ParseInt(v, 10, 64); err != nil { + return cerrors.Newf(cerrors.InvalidArgument, "%s value %q must be epoch milliseconds", f.Name, v) + } + } + + return nil +} + +// listStatus returns the status ListJobs filters on. A filter other than +// SHARE_IDENTIFIER disables status filtering. +func listStatus(in *driver.ListJobsInput) (string, bool) { + for _, f := range in.Filters { + if f.Name != driver.FilterShareIdentifier { + return "", false + } + } + + if in.JobStatus == "" { + return driver.JobStatusRunning, true + } + + return in.JobStatus, true +} + +func matchesFilters(j *driver.Job, filters []driver.JobFilter) bool { + for _, f := range filters { + if !matchesFilter(j, f) { + return false + } + } + + return true +} + +// matchesFilter reports whether a job matches any value of one filter. +func matchesFilter(j *driver.Job, f driver.JobFilter) bool { + for _, v := range f.Values { + if matchesFilterValue(j, f.Name, v) { + return true + } + } + + return false +} + +func matchesFilterValue(j *driver.Job, name, v string) bool { + switch name { + case driver.FilterJobName: + return matchPrefix(strings.ToLower(j.JobName), strings.ToLower(v)) + case driver.FilterJobDefinition: + if strings.HasPrefix(v, "arn:") { + return j.JobDefinition == v + } + + jdName := nameFromARN(kindJobDefinition, j.JobDefinition) + if i := strings.LastIndex(jdName, ":"); i >= 0 { + jdName = jdName[:i] + } + + return matchPrefix(jdName, v) + case driver.FilterBeforeCreatedAt: + n, _ := strconv.ParseInt(v, 10, 64) + + return j.CreatedAt < n + case driver.FilterAfterCreatedAt: + n, _ := strconv.ParseInt(v, 10, 64) + + return j.CreatedAt > n + case driver.FilterShareIdentifier: + return j.ShareIdentifier == v + default: + return false + } +} + +// matchPrefix matches s against pattern, where a trailing '*' makes the +// pattern a prefix. +func matchPrefix(s, pattern string) bool { + if p, ok := strings.CutSuffix(pattern, "*"); ok { + return strings.HasPrefix(s, p) + } + + return s == pattern +} + +// CancelJob cancels a job that hasn't reached STARTING (it moves to FAILED with +// the given reason). A job that is STARTING, RUNNING or finished is left alone +// and the call still succeeds. Canceling an array parent cancels every child +// that hasn't started. +func (m *Mock) CancelJob(ctx context.Context, jobID, reason string) error { + return m.stopJob(ctx, jobID, reason, false) +} + +// TerminateJob moves any unfinished job to FAILED with the given reason (a job +// that hadn't reached STARTING is canceled). Terminating an array parent +// terminates every unfinished child. +func (m *Mock) TerminateJob(ctx context.Context, jobID, reason string) error { + return m.stopJob(ctx, jobID, reason, true) +} + +func (m *Mock) stopJob(ctx context.Context, jobID, reason string, terminate bool) error { + if err := validateStopArgs(jobID, reason); err != nil { + return err + } + + m.jobsMu.Lock() + + rec, idx, ok := m.lookup(jobID) + if !ok { + m.jobsMu.Unlock() + + return cerrors.Newf(cerrors.NotFound, "job %q does not exist", jobID) + } + + sr := stopRequest{now: m.opts.Clock.Now(), reason: reason, terminate: terminate} + o := m.newObserver(sr.now) + + var ( + next jobRecord + changed bool + ) + + if idx >= 0 || rec.arraySize() > 0 { + next, changed = sr.applyToArray(o, &rec, idx) + } else { + next, changed = sr.applyToJob(o, &rec) + } + + if changed { + m.jobs.Set(rec.Job.JobID, next) + } + + m.jobsMu.Unlock() + + m.publishJobEvents(ctx, sr.now) + + return nil +} + +func validateStopArgs(jobID, reason string) error { + switch { + case jobID == "": + return cerrors.New(cerrors.InvalidArgument, "jobId is required") + case reason == "": + return cerrors.New(cerrors.InvalidArgument, "reason is required") + case len(reason) > maxReasonLen: + return cerrors.Newf(cerrors.InvalidArgument, "reason must be at most %d characters", maxReasonLen) + default: + return nil + } +} + +// stopRequest is one CancelJob (terminate false) or TerminateJob call. +type stopRequest struct { + now time.Time + reason string + terminate bool +} + +// affects reports whether the call stops a unit in status: TerminateJob stops +// anything unfinished, CancelJob only what hasn't reached STARTING. +func (sr *stopRequest) affects(status string) bool { + if sr.terminate { + return !isTerminal(status) + } + + return cancellable(status) +} + +// stopFor records the stop of a unit currently in v. A terminate of a unit +// that hadn't reached STARTING counts as a cancel. +func (sr *stopRequest) stopFor(v *jobView) jobStop { + return jobStop{At: sr.now, Reason: sr.reason, Terminate: sr.terminate && !cancellable(v.Status), StartedAt: v.StartedAt} +} + +func (sr *stopRequest) applyToJob(o *observer, rec *jobRecord) (jobRecord, bool) { + v := o.view(rec) + if !sr.affects(v.Status) { + return jobRecord{}, false + } + + next := cloneJobRecord(*rec) + s := sr.stopFor(&v) + next.Stop = &s + + return next, true +} + +// applyToArray stops one child (idx >= 0) or, for the parent (idx -1), every +// affected child, marking the parent canceled/terminated when any child was. +func (sr *stopRequest) applyToArray(o *observer, rec *jobRecord, idx int) (jobRecord, bool) { + next := cloneJobRecord(*rec) + cv := o.childViews(rec) + changed := false + + for i := range cv { + if (idx >= 0 && i != idx) || !sr.affects(cv[i].Status) { + continue + } + + if next.ChildStops == nil { + next.ChildStops = map[string]jobStop{} + } + + next.ChildStops[strconv.Itoa(i)] = sr.stopFor(&cv[i]) + changed = true + } + + if changed && idx < 0 { + next.Stop = &jobStop{At: sr.now, Reason: sr.reason, Terminate: sr.terminate} + } + + return next, changed +} diff --git a/providers/aws/batch/jobs_overrides_test.go b/providers/aws/batch/jobs_overrides_test.go new file mode 100644 index 000000000..af664d4ba --- /dev/null +++ b/providers/aws/batch/jobs_overrides_test.go @@ -0,0 +1,367 @@ +package batch_test + +import ( + "context" + "encoding/json" + "fmt" + "sync" + "testing" + + "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/providers/aws/batch" + "github.com/stackshy/cloudemu/v2/services/batch/driver" +) + +func registerJD(t *testing.T, m *batch.Mock, in driver.RegisterJobDefinitionInput) { + t.Helper() + + if _, err := m.RegisterJobDefinition(context.Background(), in); err != nil { + t.Fatalf("register %s: %v", in.Name, err) + } +} + +func decodeMap(t *testing.T, raw json.RawMessage) map[string]any { + t.Helper() + + var out map[string]any + if err := json.Unmarshal(raw, &out); err != nil { + t.Fatalf("decode %s: %v", raw, err) + } + + return out +} + +func TestSubmitJobAppliesEcsPropertiesOverride(t *testing.T) { + m := newMock(t) + setupQueue(t, m) + + registerJD(t, m, driver.RegisterJobDefinitionInput{ + Name: "ecs", Type: driver.JDTypeContainer, + EcsProperties: json.RawMessage(`{"taskProperties":[{"executionRoleArn":"role","containers":[ + {"name":"app","image":"app:1","command":["run"],"environment":[{"name":"A","value":"1"},{"name":"B","value":"2"}]}, + {"name":"side","image":"side:1","command":["tail"]}]}]}`), + }) + + j := submit(t, m, driver.SubmitJobInput{ + JobDefinition: "ecs", + EcsPropertiesOverride: json.RawMessage(`{"taskProperties":[{"containers":[ + {"name":"app","command":["run","--fast"],"environment":[{"name":"B","value":"20"},{"name":"C","value":"3"}]}]}]}`), + }) + + got := decodeMap(t, describe(t, m, j.JobID).EcsProperties) + task := got["taskProperties"].([]any)[0].(map[string]any) + + if task["executionRoleArn"] != "role" { + t.Fatalf("task-level properties lost: %v", task) + } + + containers := task["containers"].([]any) + app := containers[0].(map[string]any) + side := containers[1].(map[string]any) + + if fmt.Sprint(app["command"]) != "[run --fast]" || app["image"] != "app:1" { + t.Fatalf("app command not overridden or image lost: %v", app) + } + + if fmt.Sprint(app["environment"]) != "[map[name:A value:1] map[name:B value:20] map[name:C value:3]]" { + t.Fatalf("app environment not merged by name: %v", app["environment"]) + } + + if fmt.Sprint(side["command"]) != "[tail]" { + t.Fatalf("container that wasn't overridden changed: %v", side) + } + + // The job definition itself is untouched. + j2 := submit(t, m, driver.SubmitJobInput{JobDefinition: "ecs"}) + if c := decodeMap(t, describe(t, m, j2.JobID).EcsProperties)["taskProperties"].([]any)[0].(map[string]any)["containers"].([]any)[0].(map[string]any); fmt.Sprint(c["command"]) != "[run]" { + t.Fatalf("override leaked into the job definition: %v", c) + } +} + +func TestSubmitJobAppliesEksPropertiesOverride(t *testing.T) { + m := newMock(t) + setupQueue(t, m) + + registerJD(t, m, driver.RegisterJobDefinitionInput{ + Name: "eks", Type: driver.JDTypeContainer, + EksProperties: json.RawMessage(`{"podProperties":{"hostNetwork":true, + "containers":[{"name":"main","image":"img:1","command":["sleep"],"env":[{"name":"X","value":"1"}]}], + "metadata":{"labels":{"team":"a","env":"dev"}}}}`), + }) + + j := submit(t, m, driver.SubmitJobInput{ + JobDefinition: "eks", + EksPropertiesOverride: json.RawMessage(`{"podProperties":{ + "containers":[{"image":"img:2","args":["60"],"env":[{"name":"Y","value":"2"}]}], + "metadata":{"labels":{"env":"prod"}}}}`), + }) + + pod := decodeMap(t, describe(t, m, j.JobID).EksProperties)["podProperties"].(map[string]any) + c := pod["containers"].([]any)[0].(map[string]any) + + if c["image"] != "img:2" || fmt.Sprint(c["args"]) != "[60]" || fmt.Sprint(c["command"]) != "[sleep]" { + t.Fatalf("unnamed override on the only container not applied: %v", c) + } + + if fmt.Sprint(c["env"]) != "[map[name:X value:1] map[name:Y value:2]]" { + t.Fatalf("env not merged: %v", c["env"]) + } + + labels := pod["metadata"].(map[string]any)["labels"].(map[string]any) + if labels["team"] != "a" || labels["env"] != "prod" || pod["hostNetwork"] != true { + t.Fatalf("metadata labels not merged or pod settings lost: %v", pod) + } +} + +func TestSubmitJobAppliesNodeOverrides(t *testing.T) { + m := newMock(t) + setupQueue(t, m) + + registerJD(t, m, driver.RegisterJobDefinitionInput{ + Name: "mnp", Type: driver.JDTypeMultiNode, + NodeProperties: json.RawMessage(`{"numNodes":2,"mainNode":0,"nodeRangeProperties":[ + {"targetNodes":"0","container":{"image":"main","command":["lead"]}}, + {"targetNodes":"1:","container":{"image":"worker","command":["work"]}}]}`), + }) + + j := submit(t, m, driver.SubmitJobInput{ + JobDefinition: "mnp", + NodeOverrides: json.RawMessage(`{"numNodes":4,"nodePropertyOverrides":[ + {"targetNodes":"1:","containerOverrides":{"command":["work","--hard"]},"instanceTypes":["c5.large"]}]}`), + }) + + np := decodeMap(t, describe(t, m, j.JobID).NodeProperties) + if np["numNodes"] != float64(4) { + t.Fatalf("numNodes not overridden: %v", np) + } + + ranges := np["nodeRangeProperties"].([]any) + lead := ranges[0].(map[string]any)["container"].(map[string]any) + worker := ranges[1].(map[string]any) + + if fmt.Sprint(lead["command"]) != "[lead]" { + t.Fatalf("range outside targetNodes changed: %v", lead) + } + + if wc := worker["container"].(map[string]any); fmt.Sprint(wc["command"]) != "[work --hard]" || wc["image"] != "worker" { + t.Fatalf("worker container override not merged: %v", wc) + } + + if fmt.Sprint(worker["instanceTypes"]) != "[c5.large]" { + t.Fatalf("instanceTypes not overridden: %v", worker) + } +} + +func TestSubmitJobRejectsUnusableOverrides(t *testing.T) { + m := newMock(t) + setupQueue(t, m) + ctx := context.Background() + + registerJD(t, m, driver.RegisterJobDefinitionInput{ + Name: "ecs2", Type: driver.JDTypeContainer, + EcsProperties: json.RawMessage(`{"taskProperties":[{"containers":[{"name":"a","image":"a"},{"name":"b","image":"b"}]}]}`), + }) + registerJD(t, m, driver.RegisterJobDefinitionInput{ + Name: "mnp", Type: driver.JDTypeMultiNode, + NodeProperties: json.RawMessage(`{"numNodes":2,"mainNode":1,"nodeRangeProperties":[{"targetNodes":"0:1","container":{"image":"x"}}]}`), + }) + + cases := map[string]driver.SubmitJobInput{ + "ecs override on a containerProperties definition": {JobDefinition: "jd", + EcsPropertiesOverride: json.RawMessage(`{"taskProperties":[{"containers":[{"command":["x"]}]}]}`)}, + "eks override on an ECS definition": {JobDefinition: "ecs2", + EksPropertiesOverride: json.RawMessage(`{"podProperties":{"containers":[{"image":"x"}]}}`)}, + "node override on a container definition": {JobDefinition: "jd", + NodeOverrides: json.RawMessage(`{"numNodes":3}`)}, + "unknown container name": {JobDefinition: "ecs2", + EcsPropertiesOverride: json.RawMessage(`{"taskProperties":[{"containers":[{"name":"zzz","command":["x"]}]}]}`)}, + "unnamed override with two containers": {JobDefinition: "ecs2", + EcsPropertiesOverride: json.RawMessage(`{"taskProperties":[{"containers":[{"command":["x"]}]}]}`)}, + "numNodes without an open-ended range": {JobDefinition: "mnp", + NodeOverrides: json.RawMessage(`{"numNodes":3}`)}, + "targetNodes covering no range": {JobDefinition: "mnp", + NodeOverrides: json.RawMessage(`{"nodePropertyOverrides":[{"targetNodes":"5:6","containerOverrides":{"command":["x"]}}]}`)}, + } + + for name, in := range cases { + in.JobName, in.JobQueue = "j", "q" + if _, err := m.SubmitJob(ctx, in); !errors.IsInvalidArgument(err) { + t.Errorf("%s: want InvalidArgument, got %v", name, err) + } + } +} + +func TestSubmitJobRejectsInactiveRevision(t *testing.T) { + m := newMock(t) + setupQueue(t, m) + ctx := context.Background() + + registerJD(t, m, driver.RegisterJobDefinitionInput{ + Name: "jd", Type: driver.JDTypeContainer, ContainerProperties: json.RawMessage(`{"image":"v2"}`), + }) + + if err := m.DeregisterJobDefinition(ctx, "jd:1"); err != nil { + t.Fatal(err) + } + + for _, ref := range []string{"jd:1", "arn:aws:batch:us-east-1:000000000000:job-definition/jd:1"} { + _, err := m.SubmitJob(ctx, driver.SubmitJobInput{JobName: "j", JobQueue: "q", JobDefinition: ref}) + if !errors.IsInvalidArgument(err) { + t.Fatalf("submit with INACTIVE %s: want InvalidArgument, got %v", ref, err) + } + } + + // The ACTIVE revision still works by explicit revision. + submit(t, m, driver.SubmitJobInput{JobDefinition: "jd:2"}) +} + +func TestReplayedEventsCarryNoExitCode(t *testing.T) { + m := newMock(t) + pub := &recordingPublisher{} + m.SetEventPublisher(pub) + setupQueue(t, m) + + submit(t, m, driver.SubmitJobInput{}) + + pub.mu.Lock() + defer pub.mu.Unlock() + + if len(pub.events) != 4 { + t.Fatalf("want 4 events, got %d", len(pub.events)) + } + + for _, e := range pub.events { + c, _ := e.detail["container"].(map[string]any) + if c == nil || c["image"] != "busybox" { + t.Fatalf("%s event lost the container: %v", e.detail["status"], e.detail) + } + + _, hasExit := c["exitCode"] + if want := e.detail["status"] == driver.JobStatusSucceeded; hasExit != want { + t.Fatalf("%s event exitCode present = %v, want %v", e.detail["status"], hasExit, want) + } + } +} + +func TestListArrayChildrenAppliesFilters(t *testing.T) { + m := newMock(t) + setupQueue(t, m) + + size := int32(3) + parent := submit(t, m, driver.SubmitJobInput{JobName: "arr", ArraySize: &size}) + + nomatch, err := listJobs(m, driver.ListJobsInput{ + ArrayJobID: parent.JobID, + Filters: []driver.JobFilter{{Name: driver.FilterJobName, Values: []string{"nomatch"}}}, + }) + if err != nil || len(nomatch) != 0 { + t.Fatalf("non-matching JOB_NAME filter listed children: %v %v", nomatch, err) + } + + match, err := listJobs(m, driver.ListJobsInput{ + ArrayJobID: parent.JobID, + Filters: []driver.JobFilter{{Name: driver.FilterJobName, Values: []string{"ARR*"}}}, + }) + if err != nil || len(match) != 3 { + t.Fatalf("matching JOB_NAME filter: got %d children, %v", len(match), err) + } +} + +// TestSchedulingPolicyBindingIsAtomic races queue creation/update against +// policy deletion: no queue may end up bound to a deleted policy. +func TestSchedulingPolicyBindingIsAtomic(t *testing.T) { + m := newMock(t) + ctx := context.Background() + mustCreateCE(t, m, "ce") + + order := []driver.ComputeEnvironmentOrder{{Order: 1, ComputeEnvironment: "ce"}} + + for i := 0; i < 50; i++ { + pol := fmt.Sprintf("p%d", i) + spare := fmt.Sprintf("s%d", i) + + for _, n := range []string{pol, spare} { + if _, err := m.CreateSchedulingPolicy(ctx, driver.CreateSchedulingPolicyInput{Name: n}); err != nil { + t.Fatal(err) + } + } + + if _, err := m.CreateJobQueue(ctx, driver.CreateJobQueueInput{ + Name: "u" + pol, Priority: 1, ComputeEnvironmentOrder: order, SchedulingPolicyARN: spare, + }); err != nil { + t.Fatal(err) + } + + var wg sync.WaitGroup + + wg.Add(3) + + go func() { + defer wg.Done() + + _, _ = m.CreateJobQueue(ctx, driver.CreateJobQueueInput{ + Name: "c" + pol, Priority: 1, ComputeEnvironmentOrder: order, SchedulingPolicyARN: pol, + }) + }() + + go func() { + defer wg.Done() + + _, _ = m.UpdateJobQueue(ctx, driver.UpdateJobQueueInput{Name: "u" + pol, SchedulingPolicyARN: &pol}) + }() + + go func() { + defer wg.Done() + + _ = m.DeleteSchedulingPolicy(ctx, pol) + }() + + wg.Wait() + } + + queues, _ := m.DescribeJobQueues(ctx, nil) + for i := range queues { + q := &queues[i] + if q.SchedulingPolicyARN == "" { + continue + } + + if got, _ := m.DescribeSchedulingPolicies(ctx, []string{q.SchedulingPolicyARN}); len(got) != 1 { + t.Fatalf("queue %s is bound to deleted policy %s", q.Name, q.SchedulingPolicyARN) + } + } +} + +func TestMultinodeJobDefinitionEchoesNoDefaultPlatform(t *testing.T) { + m := newMock(t) + ctx := context.Background() + + registerJD(t, m, driver.RegisterJobDefinitionInput{ + Name: "mnp", Type: driver.JDTypeMultiNode, + NodeProperties: json.RawMessage(`{"numNodes":2,"mainNode":0,"nodeRangeProperties":[{"targetNodes":"0:","container":{"image":"x"}}]}`), + }) + registerJD(t, m, driver.RegisterJobDefinitionInput{ + Name: "fg", Type: driver.JDTypeContainer, PlatformCapabilities: []string{"FARGATE"}, + ContainerProperties: json.RawMessage(`{"image":"x"}`), + }) + + defs, err := m.DescribeJobDefinitions(ctx, driver.DescribeJobDefinitionsInput{}) + if err != nil || len(defs) != 2 { + t.Fatalf("describe: %v %v", defs, err) + } + + for i := range defs { + d := &defs[i] + + switch d.Name { + case "mnp": + if len(d.PlatformCapabilities) != 0 { + t.Fatalf("multinode definition echoes a default platform: %v", d.PlatformCapabilities) + } + case "fg": + if fmt.Sprint(d.PlatformCapabilities) != "[FARGATE]" { + t.Fatalf("explicit platform lost: %v", d.PlatformCapabilities) + } + } + } +} diff --git a/providers/aws/batch/jobs_paging_test.go b/providers/aws/batch/jobs_paging_test.go new file mode 100644 index 000000000..7abb6c352 --- /dev/null +++ b/providers/aws/batch/jobs_paging_test.go @@ -0,0 +1,238 @@ +package batch_test + +import ( + "context" + "strings" + "sync" + "testing" + "time" + + "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/providers/aws/batch" + "github.com/stackshy/cloudemu/v2/services/batch/driver" +) + +func listJobs(m *batch.Mock, in driver.ListJobsInput) ([]driver.Job, error) { + out, err := m.ListJobs(context.Background(), in) + if err != nil { + return nil, err + } + + return out.Jobs, nil +} + +func TestListJobsCursorIsStable(t *testing.T) { + m := newMock(t) + setupQueue(t, m) + ctx := context.Background() + + for _, n := range []string{"a", "b", "c", "d"} { + submit(t, m, driver.SubmitJobInput{JobName: n}) + } + + two := int32(2) + in := driver.ListJobsInput{JobQueue: "q", JobStatus: driver.JobStatusSucceeded, MaxResults: &two} + + p1, err := m.ListJobs(ctx, in) + if err != nil || len(p1.Jobs) != 2 || p1.NextToken == "" || p1.Jobs[0].JobName != "d" || p1.Jobs[1].JobName != "c" { + t.Fatalf("page 1 = %+v, %v", p1, err) + } + + // A job submitted between pages must not shift the next page. + submit(t, m, driver.SubmitJobInput{JobName: "e"}) + + in.NextToken = p1.NextToken + + p2, err := m.ListJobs(ctx, in) + if err != nil || len(p2.Jobs) != 2 || p2.NextToken != "" || p2.Jobs[0].JobName != "b" || p2.Jobs[1].JobName != "a" { + t.Fatalf("page 2 = %+v, %v", p2, err) + } + + in.NextToken = "!!" + if _, err := m.ListJobs(ctx, in); !errors.IsInvalidArgument(err) { + t.Fatalf("bad token: want InvalidArgument, got %v", err) + } + + zero := int32(0) + if _, err := m.ListJobs(ctx, driver.ListJobsInput{JobQueue: "q", MaxResults: &zero}); !errors.IsInvalidArgument(err) { + t.Fatalf("maxResults 0: want InvalidArgument, got %v", err) + } + + // Array children page by index. + size := int32(5) + arr := submit(t, m, driver.SubmitJobInput{JobName: "arr", ArraySize: &size}) + cin := driver.ListJobsInput{ArrayJobID: arr.JobID, JobStatus: driver.JobStatusSucceeded, MaxResults: &two} + + var idx []int32 + + for { + page, err := m.ListJobs(ctx, cin) + if err != nil { + t.Fatal(err) + } + + for i := range page.Jobs { + idx = append(idx, *page.Jobs[i].ArrayProperties.Index) + } + + if page.NextToken == "" { + break + } + + cin.NextToken = page.NextToken + } + + if len(idx) != 5 || idx[0] != 0 || idx[4] != 4 { + t.Fatalf("children pages = %v, want 0..4", idx) + } +} + +func TestTickPublishesUnpolledTransitions(t *testing.T) { + m, fc := newAsyncMock(t) + pub := &recordingPublisher{} + m.SetEventPublisher(pub) + setupQueue(t, m) + + submit(t, m, driver.SubmitJobInput{}) + + if m.Tick(fc.Now()) { + t.Fatal("Tick right after submit reported a change") + } + + fc.Advance(2 * time.Second) + + if !m.Tick(fc.Now()) { + t.Fatal("Tick after 2s reported no change") + } + + if got := pub.statuses(); len(got) != 2 || got[0] != "RUNNABLE" || got[1] != "STARTING" { + t.Fatalf("events after Tick = %v, want [RUNNABLE STARTING]", got) + } + + fc.Advance(10 * time.Second) + m.Tick(fc.Now()) + + if got := pub.statuses(); len(got) != 4 || got[3] != "SUCCEEDED" { + t.Fatalf("events = %v, want [... RUNNING SUCCEEDED]", got) + } + + if m.Tick(fc.Now()) { + t.Fatal("Tick on a finished job reported a change") + } +} + +func TestTagValidation(t *testing.T) { + m := newMock(t) + setupQueue(t, m) + ctx := context.Background() + + j := submit(t, m, driver.SubmitJobInput{}) + + bad := []map[string]string{ + {"aws:owner": "x"}, + {"AWS:owner": "x"}, + {"": "x"}, + {strings.Repeat("k", 129): "x"}, + {"k": strings.Repeat("v", 257)}, + } + + for i, tags := range bad { + if err := m.TagResource(ctx, j.JobARN, tags); !errors.IsInvalidArgument(err) { + t.Errorf("case %d: TagResource want InvalidArgument, got %v", i, err) + } + + if _, err := m.SubmitJob(ctx, driver.SubmitJobInput{ + JobName: "t", JobQueue: "q", JobDefinition: "jd", Tags: tags, + }); !errors.IsInvalidArgument(err) { + t.Errorf("case %d: SubmitJob want InvalidArgument, got %v", i, err) + } + + if _, err := m.CreateSchedulingPolicy(ctx, driver.CreateSchedulingPolicyInput{Name: "sp", Tags: tags}); !errors.IsInvalidArgument(err) { + t.Errorf("case %d: CreateSchedulingPolicy want InvalidArgument, got %v", i, err) + } + } + + if err := m.TagResource(ctx, j.JobARN, map[string]string{"team": "a"}); err != nil { + t.Fatalf("TagResource(job): %v", err) + } + + if tags, err := m.ListTagsForResource(ctx, j.JobARN); err != nil || tags["team"] != "a" { + t.Fatalf("ListTagsForResource(job) = %v, %v", tags, err) + } + + // Child jobs of an array job can't be tagged. + size := int32(2) + arr := submit(t, m, driver.SubmitJobInput{JobName: "arr", ArraySize: &size}) + + if err := m.TagResource(ctx, arr.JobARN+":0", map[string]string{"k": "v"}); !errors.IsNotFound(err) { + t.Fatalf("tagging an array child: want NotFound, got %v", err) + } + + // The merged tag set is capped at 50. + many := map[string]string{} + for i := 0; i < 50; i++ { + many["k"+strings.Repeat("x", i)] = "v" + } + + if err := m.TagResource(ctx, j.JobARN, many); !errors.IsInvalidArgument(err) { + t.Fatalf("51 total tags: want InvalidArgument, got %v", err) + } + + if tags, _ := m.ListTagsForResource(ctx, j.JobARN); len(tags) != 1 { + t.Fatalf("rejected TagResource changed tags: %v", tags) + } +} + +// TestConcurrentJobOperations runs submit, describe, list, cancel, terminate, +// tag and Tick at once (run with -race), with an event target that calls back +// into the mock, which must not deadlock. +func TestConcurrentJobOperations(t *testing.T) { + m, fc := newAsyncMock(t) + setupQueue(t, m) + m.SetEventPublisher(callbackPublisher{m: m}) + + ctx := context.Background() + + var wg sync.WaitGroup + + for w := 0; w < 6; w++ { + wg.Add(1) + + go func() { + defer wg.Done() + + for i := 0; i < 20; i++ { + j, err := m.SubmitJob(ctx, driver.SubmitJobInput{JobName: "c", JobQueue: "q", JobDefinition: "jd"}) + if err != nil { + t.Error(err) + + return + } + + _, _ = m.DescribeJobs(ctx, []string{j.JobID}) + _, _ = m.ListJobs(ctx, driver.ListJobsInput{JobQueue: "q"}) + _ = m.TagResource(ctx, j.JobARN, map[string]string{"k": "v"}) + + if i%2 == 0 { + _ = m.CancelJob(ctx, j.JobID, "r") + } else { + _ = m.TerminateJob(ctx, j.JobID, "r") + } + + fc.Advance(100 * time.Millisecond) + m.Tick(fc.Now()) + } + }() + } + + wg.Wait() +} + +// callbackPublisher re-enters the mock from event delivery, like an +// EventBridge rule whose target calls Batch. +type callbackPublisher struct{ m *batch.Mock } + +func (p callbackPublisher) PublishServiceEvent(ctx context.Context, _, _ string, _ any, resources []string) { + _, _ = p.m.DescribeJobs(ctx, resources) + _, _ = p.m.ListJobs(ctx, driver.ListJobsInput{JobQueue: "q", JobStatus: driver.JobStatusFailed}) +} diff --git a/providers/aws/batch/jobs_test.go b/providers/aws/batch/jobs_test.go new file mode 100644 index 000000000..880927ee5 --- /dev/null +++ b/providers/aws/batch/jobs_test.go @@ -0,0 +1,752 @@ +package batch_test + +import ( + "context" + "encoding/json" + "sync" + "testing" + "time" + + "github.com/stackshy/cloudemu/v2/config" + "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/internal/settle" + "github.com/stackshy/cloudemu/v2/providers/aws/batch" + "github.com/stackshy/cloudemu/v2/services/batch/driver" +) + +var jobEpoch = time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC) + +// recordingPublisher captures published EventBridge events. +type recordingPublisher struct { + mu sync.Mutex + events []recordedEvent +} + +type recordedEvent struct { + source, detailType string + detail map[string]any + resources []string +} + +func (p *recordingPublisher) PublishServiceEvent(_ context.Context, source, detailType string, detail any, resources []string) { + b, _ := json.Marshal(detail) + + var d map[string]any + _ = json.Unmarshal(b, &d) + + p.mu.Lock() + defer p.mu.Unlock() + + p.events = append(p.events, recordedEvent{source: source, detailType: detailType, detail: d, resources: resources}) +} + +func (p *recordingPublisher) statuses() []string { + p.mu.Lock() + defer p.mu.Unlock() + + out := make([]string, 0, len(p.events)) + for _, e := range p.events { + out = append(out, e.detail["status"].(string)) + } + + return out +} + +// newAsyncMock returns a mock whose jobs progress on a fake clock. +func newAsyncMock(t *testing.T) (*batch.Mock, *config.FakeClock) { + t.Helper() + + fc := config.NewFakeClock(jobEpoch) + + return batch.New(config.NewOptions( + config.WithRegion("us-east-1"), + config.WithAccountID("000000000000"), + config.WithClock(fc), + config.WithAsyncSettle(), + )), fc +} + +// setupQueue creates a compute environment, a job queue "q" and a container +// job definition "jd". +func setupQueue(t *testing.T, m *batch.Mock) { + t.Helper() + + ctx := context.Background() + mustCreateCE(t, m, "ce") + + if _, err := m.CreateJobQueue(ctx, driver.CreateJobQueueInput{ + Name: "q", + Priority: 1, + ComputeEnvironmentOrder: []driver.ComputeEnvironmentOrder{{Order: 1, ComputeEnvironment: "ce"}}, + }); err != nil { + t.Fatalf("create queue: %v", err) + } + + if _, err := m.RegisterJobDefinition(ctx, driver.RegisterJobDefinitionInput{ + Name: "jd", Type: driver.JDTypeContainer, + ContainerProperties: json.RawMessage(`{"image":"busybox","command":["echo","hi"]}`), + }); err != nil { + t.Fatalf("register: %v", err) + } +} + +func submit(t *testing.T, m *batch.Mock, in driver.SubmitJobInput) *driver.Job { + t.Helper() + + if in.JobName == "" { + in.JobName = "job" + } + + if in.JobQueue == "" { + in.JobQueue = "q" + } + + if in.JobDefinition == "" { + in.JobDefinition = "jd" + } + + j, err := m.SubmitJob(context.Background(), in) + if err != nil { + t.Fatalf("SubmitJob: %v", err) + } + + return j +} + +func describe(t *testing.T, m *batch.Mock, id string) driver.Job { + t.Helper() + + jobs, err := m.DescribeJobs(context.Background(), []string{id}) + if err != nil || len(jobs) != 1 { + t.Fatalf("DescribeJobs(%s) = %v, %v", id, jobs, err) + } + + return jobs[0] +} + +func TestSubmitJobSyncSucceedsImmediately(t *testing.T) { + m := newMock(t) + setupQueue(t, m) + + j := submit(t, m, driver.SubmitJobInput{ + ContainerOverrides: json.RawMessage(`{"command":["echo","bye"]}`), + Parameters: map[string]string{"p": "v"}, + }) + if j.JobID == "" || j.JobARN != "arn:aws:batch:us-east-1:000000000000:job/"+j.JobID { + t.Fatalf("bad identity: %+v", j) + } + + got := describe(t, m, j.JobID) + if got.Status != driver.JobStatusSucceeded { + t.Fatalf("status = %s, want SUCCEEDED", got.Status) + } + + if got.CreatedAt == 0 || got.StartedAt == 0 || got.StoppedAt == 0 { + t.Fatalf("timestamps not set: %+v", got) + } + + if got.JobQueue != "arn:aws:batch:us-east-1:000000000000:job-queue/q" || + got.JobDefinition != "arn:aws:batch:us-east-1:000000000000:job-definition/jd:1" { + t.Fatalf("queue/definition not resolved to ARNs: %s %s", got.JobQueue, got.JobDefinition) + } + + var c map[string]any + if err := json.Unmarshal(got.Container, &c); err != nil { + t.Fatalf("container: %v", err) + } + + if c["image"] != "busybox" || c["command"].([]any)[1] != "bye" || c["exitCode"] != float64(0) { + t.Fatalf("container overrides/exitCode not applied: %v", c) + } +} + +func TestJobLifecycleAdvancesWithClock(t *testing.T) { + m, fc := newAsyncMock(t) + setupQueue(t, m) + + j := submit(t, m, driver.SubmitJobInput{}) + + // One second each in SUBMITTED, RUNNABLE and STARTING; RUNNING lasts the + // longer run dwell, so a real client has time to terminate a running job. + steps := []struct { + status string + dwell time.Duration + }{ + {driver.JobStatusSubmitted, time.Second}, + {driver.JobStatusRunnable, time.Second}, + {driver.JobStatusStarting, time.Second}, + {driver.JobStatusRunning, time.Second}, + {driver.JobStatusRunning, settle.DefaultBatchJobRunDwell - 2*time.Second}, + {driver.JobStatusRunning, time.Second}, + {driver.JobStatusSucceeded, 0}, + } + + for i, st := range steps { + got := describe(t, m, j.JobID) + if got.Status != st.status { + t.Fatalf("step %d: status = %s, want %s", i, got.Status, st.status) + } + + if st.status == driver.JobStatusRunning && got.StartedAt == 0 { + t.Fatal("RUNNING job has no startedAt") + } + + if st.status == driver.JobStatusSucceeded { + if got.StoppedAt-got.StartedAt != settle.DefaultBatchJobRunDwell.Milliseconds() || got.StatusReason == "" { + t.Fatalf("SUCCEEDED job times/reason wrong: %+v", got) + } + } + + fc.Advance(st.dwell) + } +} + +func TestSubmitJobValidation(t *testing.T) { + m := newMock(t) + setupQueue(t, m) + ctx := context.Background() + + one, tooMany := int32(1), int32(10001) + + cases := map[string]driver.SubmitJobInput{ + "missing name": {JobQueue: "q", JobDefinition: "jd"}, + "bad name": {JobName: "-bad", JobQueue: "q", JobDefinition: "jd"}, + "missing queue": {JobName: "j", JobQueue: "nope", JobDefinition: "jd"}, + "missing definition": {JobName: "j", JobQueue: "q", JobDefinition: "nope"}, + "missing revision": {JobName: "j", JobQueue: "q", JobDefinition: "jd:9"}, + "array size 1": {JobName: "j", JobQueue: "q", JobDefinition: "jd", ArraySize: &one}, + "array size 10001": {JobName: "j", JobQueue: "q", JobDefinition: "jd", ArraySize: &tooMany}, + "unknown dependency": {JobName: "j", JobQueue: "q", JobDefinition: "jd", + DependsOn: []driver.JobDependency{{JobID: "missing"}}}, + "bad dependency type": {JobName: "j", JobQueue: "q", JobDefinition: "jd", + DependsOn: []driver.JobDependency{{JobID: "x", Type: "BOGUS"}}}, + "N_TO_N on non-array": {JobName: "j", JobQueue: "q", JobDefinition: "jd", + DependsOn: []driver.JobDependency{{JobID: "x", Type: driver.DependencyNToN}}}, + "share id on FIFO queue": {JobName: "j", JobQueue: "q", JobDefinition: "jd", ShareIdentifier: "A"}, + "short timeout": {JobName: "j", JobQueue: "q", JobDefinition: "jd", + Timeout: json.RawMessage(`{"attemptDurationSeconds":30}`)}, + } + + for name, in := range cases { + if _, err := m.SubmitJob(ctx, in); !errors.IsInvalidArgument(err) { + t.Errorf("%s: want InvalidArgument, got %v", name, err) + } + } + + // A DISABLED queue accepts no new jobs. + disabled := driver.StateDisabled + if _, err := m.UpdateJobQueue(ctx, driver.UpdateJobQueueInput{Name: "q", State: &disabled}); err != nil { + t.Fatalf("disable: %v", err) + } + + if _, err := m.SubmitJob(ctx, driver.SubmitJobInput{JobName: "j", JobQueue: "q", JobDefinition: "jd"}); !errors.IsInvalidArgument(err) { + t.Fatalf("submit to DISABLED queue: want InvalidArgument, got %v", err) + } +} + +func TestSubmitJobUsesLatestActiveRevision(t *testing.T) { + m := newMock(t) + setupQueue(t, m) + ctx := context.Background() + + if _, err := m.RegisterJobDefinition(ctx, driver.RegisterJobDefinitionInput{ + Name: "jd", Type: driver.JDTypeContainer, ContainerProperties: json.RawMessage(`{"image":"v2"}`), + }); err != nil { + t.Fatal(err) + } + + if err := m.DeregisterJobDefinition(ctx, "jd:2"); err != nil { + t.Fatal(err) + } + + j := describe(t, m, submit(t, m, driver.SubmitJobInput{}).JobID) + if j.JobDefinition != "arn:aws:batch:us-east-1:000000000000:job-definition/jd:1" { + t.Fatalf("want latest ACTIVE revision 1, got %s", j.JobDefinition) + } +} + +func TestDependentJobWaitsAndFailsWithDependency(t *testing.T) { + m, fc := newAsyncMock(t) + setupQueue(t, m) + ctx := context.Background() + + parent := submit(t, m, driver.SubmitJobInput{JobName: "parent"}) + child := submit(t, m, driver.SubmitJobInput{ + JobName: "child", DependsOn: []driver.JobDependency{{JobID: parent.JobID}}, + }) + + fc.Advance(time.Second) + + if got := describe(t, m, child.JobID).Status; got != driver.JobStatusPending { + t.Fatalf("child before parent finishes = %s, want PENDING", got) + } + + // Parent finishes at +8s (3 one-second phases plus the 5s RUNNING dwell); + // child then runs RUNNABLE/STARTING/RUNNING and succeeds at +15s. + fc.Advance(7 * time.Second) + + if got := describe(t, m, child.JobID).Status; got != driver.JobStatusRunnable { + t.Fatalf("child after parent succeeded = %s, want RUNNABLE", got) + } + + fc.Advance(7 * time.Second) + + if got := describe(t, m, child.JobID).Status; got != driver.JobStatusSucceeded { + t.Fatalf("child = %s, want SUCCEEDED", got) + } + + // A dependency that fails fails its dependents. + p2 := submit(t, m, driver.SubmitJobInput{JobName: "p2"}) + c2 := submit(t, m, driver.SubmitJobInput{JobName: "c2", DependsOn: []driver.JobDependency{{JobID: p2.JobID}}}) + + if err := m.CancelJob(ctx, p2.JobID, "stop it"); err != nil { + t.Fatal(err) + } + + fc.Advance(time.Second) + + got := describe(t, m, c2.JobID) + if got.Status != driver.JobStatusFailed || got.StatusReason != "Dependent Job failed" { + t.Fatalf("dependent of cancelled job = %s (%q), want FAILED (Dependent Job failed)", got.Status, got.StatusReason) + } +} + +func TestCancelVersusTerminate(t *testing.T) { + m, fc := newAsyncMock(t) + setupQueue(t, m) + ctx := context.Background() + + // Cancel affects a job that hasn't reached STARTING. + runnable := submit(t, m, driver.SubmitJobInput{JobName: "runnable"}) + fc.Advance(time.Second) + + if err := m.CancelJob(ctx, runnable.JobID, "user cancel"); err != nil { + t.Fatal(err) + } + + got := describe(t, m, runnable.JobID) + if got.Status != driver.JobStatusFailed || !got.IsCancelled || got.IsTerminated || got.StatusReason != "user cancel" { + t.Fatalf("cancelled RUNNABLE job = %+v", got) + } + + // Cancel leaves a RUNNING job alone (and still succeeds). + running := submit(t, m, driver.SubmitJobInput{JobName: "running"}) + fc.Advance(3 * time.Second) + + if err := m.CancelJob(ctx, running.JobID, "too late"); err != nil { + t.Fatalf("cancel of RUNNING job should succeed, got %v", err) + } + + if got := describe(t, m, running.JobID); got.Status != driver.JobStatusRunning || got.IsCancelled { + t.Fatalf("cancel must not affect a RUNNING job: %+v", got) + } + + // Terminate stops a RUNNING job, keeping its startedAt. + if err := m.TerminateJob(ctx, running.JobID, "kill"); err != nil { + t.Fatal(err) + } + + got = describe(t, m, running.JobID) + if got.Status != driver.JobStatusFailed || !got.IsTerminated || got.StartedAt == 0 || got.StatusReason != "kill" { + t.Fatalf("terminated RUNNING job = %+v", got) + } + + // Terminate on a SUBMITTED job cancels it. + fresh := submit(t, m, driver.SubmitJobInput{JobName: "fresh"}) + if err := m.TerminateJob(ctx, fresh.JobID, "early"); err != nil { + t.Fatal(err) + } + + if got := describe(t, m, fresh.JobID); got.Status != driver.JobStatusFailed || !got.IsCancelled || got.IsTerminated { + t.Fatalf("terminate before STARTING should cancel: %+v", got) + } + + // Finished jobs are untouched; unknown jobs and missing reasons are errors. + fc.Advance(10 * time.Second) + + if err := m.TerminateJob(ctx, running.JobID, "again"); err != nil { + t.Fatal(err) + } + + if got := describe(t, m, running.JobID); got.StatusReason != "kill" { + t.Fatalf("terminating a finished job changed it: %+v", got) + } + + if err := m.CancelJob(ctx, "missing", "r"); !errors.IsNotFound(err) { + t.Fatalf("unknown job: want NotFound, got %v", err) + } + + if err := m.CancelJob(ctx, fresh.JobID, ""); !errors.IsInvalidArgument(err) { + t.Fatalf("missing reason: want InvalidArgument, got %v", err) + } +} + +func TestArrayJobChildren(t *testing.T) { + m, fc := newAsyncMock(t) + setupQueue(t, m) + ctx := context.Background() + + size := int32(3) + parent := submit(t, m, driver.SubmitJobInput{ + ArraySize: &size, DependsOn: []driver.JobDependency{{Type: driver.DependencySequential}}, + }) + + fc.Advance(2 * time.Second) + + p := describe(t, m, parent.JobID) + if p.Status != driver.JobStatusPending || p.ArrayProperties == nil || p.ArrayProperties.Size != 3 { + t.Fatalf("array parent while children run = %+v", p) + } + + if p.ArrayProperties.StatusSummary[driver.JobStatusStarting] != 1 || + p.ArrayProperties.StatusSummary[driver.JobStatusPending] != 2 { + t.Fatalf("SEQUENTIAL children should run one at a time: %v", p.ArrayProperties.StatusSummary) + } + + child := describe(t, m, parent.JobID+":0") + if child.ArrayProperties == nil || child.ArrayProperties.Index == nil || *child.ArrayProperties.Index != 0 { + t.Fatalf("child index missing: %+v", child) + } + + // An index past int32 must not wrap around to a real child. + for _, bad := range []string{":4294967296", ":2147483648", ":-1"} { + if jobs, err := m.DescribeJobs(ctx, []string{parent.JobID + bad}); err != nil || len(jobs) != 0 { + t.Fatalf("DescribeJobs(%s) = %+v, %v; want no job", bad, jobs, err) + } + } + + all := driver.JobStatusPending + kids, err := listJobs(m, driver.ListJobsInput{ArrayJobID: parent.JobID, JobStatus: all}) + if err != nil || len(kids) != 2 { + t.Fatalf("ListJobs(arrayJobId, PENDING) = %d, %v; want 2", len(kids), err) + } + + fc.Advance(20 * time.Second) + + if got := describe(t, m, parent.JobID); got.Status != driver.JobStatusSucceeded || + got.ArrayProperties.StatusSummary[driver.JobStatusSucceeded] != 3 { + t.Fatalf("array parent after children finish = %+v", got) + } + + // Terminating an array parent fails every unfinished child. + p2 := submit(t, m, driver.SubmitJobInput{JobName: "arr2", ArraySize: &size}) + if err := m.TerminateJob(ctx, p2.JobID, "abort"); err != nil { + t.Fatal(err) + } + + got := describe(t, m, p2.JobID) + if got.Status != driver.JobStatusFailed || got.ArrayProperties.StatusSummary[driver.JobStatusFailed] != 3 { + t.Fatalf("terminated array = %+v", got) + } +} + +func TestListJobs(t *testing.T) { + m, fc := newAsyncMock(t) + setupQueue(t, m) + ctx := context.Background() + + a := submit(t, m, driver.SubmitJobInput{JobName: "alpha"}) + fc.Advance(time.Second) + b := submit(t, m, driver.SubmitJobInput{JobName: "beta"}) + fc.Advance(2 * time.Second) // alpha RUNNING, beta STARTING + + running, err := listJobs(m, driver.ListJobsInput{JobQueue: "q"}) + if err != nil || len(running) != 1 || running[0].JobID != a.JobID { + t.Fatalf("default ListJobs (RUNNING) = %+v, %v", running, err) + } + + byName, err := listJobs(m, driver.ListJobsInput{ + JobQueue: "arn:aws:batch:us-east-1:000000000000:job-queue/q", + Filters: []driver.JobFilter{{Name: driver.FilterJobName, Values: []string{"BET*"}}}, + }) + if err != nil || len(byName) != 1 || byName[0].JobID != b.JobID { + t.Fatalf("JOB_NAME filter = %+v, %v", byName, err) + } + + all, err := listJobs(m, driver.ListJobsInput{ + JobQueue: "q", Filters: []driver.JobFilter{{Name: driver.FilterJobDefinition, Values: []string{"jd"}}}, + }) + if err != nil || len(all) != 2 || all[0].JobID != b.JobID { + t.Fatalf("JOB_DEFINITION filter should list both, newest first: %+v, %v", all, err) + } + + bad := []driver.ListJobsInput{ + {}, + {JobQueue: "q", ArrayJobID: "x"}, + {JobQueue: "q", JobStatus: "DONE"}, + {JobQueue: "q", Filters: []driver.JobFilter{{Name: "NOPE"}}}, + {JobQueue: "q", Filters: []driver.JobFilter{{Name: driver.FilterJobName}, {Name: driver.FilterJobDefinition}}}, + } + + for i, in := range bad { + if _, err := m.ListJobs(ctx, in); !errors.IsInvalidArgument(err) { + t.Errorf("case %d: want InvalidArgument, got %v", i, err) + } + } +} + +func TestJobStateChangeEvents(t *testing.T) { + m := newMock(t) + pub := &recordingPublisher{} + m.SetEventPublisher(pub) + setupQueue(t, m) + + j := submit(t, m, driver.SubmitJobInput{}) + + got := pub.statuses() + want := []string{"RUNNABLE", "STARTING", "RUNNING", "SUCCEEDED"} + + if len(got) != len(want) { + t.Fatalf("events = %v, want %v (no event for the initial submission)", got, want) + } + + for i := range want { + if got[i] != want[i] { + t.Fatalf("events = %v, want %v", got, want) + } + } + + e := pub.events[3] + if e.source != "aws.batch" || e.detailType != "Batch Job State Change" || + len(e.resources) != 1 || e.resources[0] != j.JobARN || e.detail["jobId"] != j.JobID { + t.Fatalf("bad event envelope: %+v", e) + } + + // Describing again publishes nothing new. + describe(t, m, j.JobID) + + if n := len(pub.statuses()); n != 4 { + t.Fatalf("re-observing a finished job published more events: %d", n) + } +} + +func TestJobEventsFollowAsyncLifecycle(t *testing.T) { + m, fc := newAsyncMock(t) + pub := &recordingPublisher{} + m.SetEventPublisher(pub) + setupQueue(t, m) + ctx := context.Background() + + j := submit(t, m, driver.SubmitJobInput{}) + if n := len(pub.statuses()); n != 0 { + t.Fatalf("submission published %d events, want 0", n) + } + + fc.Advance(time.Second) + describe(t, m, j.JobID) + + if got := pub.statuses(); len(got) != 1 || got[0] != "RUNNABLE" { + t.Fatalf("events after 1s = %v, want [RUNNABLE]", got) + } + + if err := m.CancelJob(ctx, j.JobID, "stop"); err != nil { + t.Fatal(err) + } + + if got := pub.statuses(); len(got) != 2 || got[1] != "FAILED" { + t.Fatalf("events after cancel = %v, want [RUNNABLE FAILED]", got) + } +} + +func TestSchedulingPolicyCRUDAndQueueBinding(t *testing.T) { + m := newMock(t) + ctx := context.Background() + mustCreateCE(t, m, "ce") + + p, err := m.CreateSchedulingPolicy(ctx, driver.CreateSchedulingPolicyInput{ + Name: "fair", + FairsharePolicy: json.RawMessage(`{"shareDecaySeconds":3600,"computeReservation":1,"shareDistribution":[{"shareIdentifier":"A*","weightFactor":0.5}]}`), + Tags: map[string]string{"k": "v"}, + }) + if err != nil { + t.Fatalf("create: %v", err) + } + + if p.ARN != "arn:aws:batch:us-east-1:000000000000:scheduling-policy/fair" { + t.Fatalf("arn = %s", p.ARN) + } + + invalid := []driver.CreateSchedulingPolicyInput{ + {Name: "fair"}, + {Name: "bad name"}, + {Name: "x", FairsharePolicy: json.RawMessage(`{"computeReservation":100}`)}, + {Name: "x", FairsharePolicy: json.RawMessage(`{"shareDecaySeconds":604801}`)}, + {Name: "x", FairsharePolicy: json.RawMessage(`{"shareDistribution":[{"shareIdentifier":"A","weightFactor":1000}]}`)}, + {Name: "x", FairsharePolicy: json.RawMessage(`{}`), QuotaSharePolicy: json.RawMessage(`{}`)}, + } + + for i, in := range invalid { + if _, err := m.CreateSchedulingPolicy(ctx, in); err == nil { + t.Errorf("case %d: want error", i) + } + } + + if err := m.UpdateSchedulingPolicy(ctx, driver.UpdateSchedulingPolicyInput{ + ARN: p.ARN, FairsharePolicy: json.RawMessage(`{"shareDecaySeconds":60}`), + }); err != nil { + t.Fatalf("update: %v", err) + } + + got, err := m.DescribeSchedulingPolicies(ctx, []string{p.ARN, "arn:aws:batch:us-east-1:000000000000:scheduling-policy/none"}) + if err != nil || len(got) != 1 || string(got[0].FairsharePolicy) != `{"shareDecaySeconds":60}` || got[0].Tags["k"] != "v" { + t.Fatalf("describe = %+v, %v", got, err) + } + + if _, err := m.DescribeSchedulingPolicies(ctx, nil); !errors.IsInvalidArgument(err) { + t.Fatalf("describe without arns: want InvalidArgument, got %v", err) + } + + // A queue must reference an existing policy. + order := []driver.ComputeEnvironmentOrder{{Order: 1, ComputeEnvironment: "ce"}} + if _, err := m.CreateJobQueue(ctx, driver.CreateJobQueueInput{ + Name: "bad", Priority: 1, ComputeEnvironmentOrder: order, + SchedulingPolicyARN: "arn:aws:batch:us-east-1:000000000000:scheduling-policy/none", + }); !errors.IsInvalidArgument(err) { + t.Fatalf("queue with unknown policy: want InvalidArgument, got %v", err) + } + + if _, err := m.CreateJobQueue(ctx, driver.CreateJobQueueInput{ + Name: "fq", Priority: 1, ComputeEnvironmentOrder: order, SchedulingPolicyARN: p.ARN, + }); err != nil { + t.Fatalf("fair-share queue: %v", err) + } + + // The policy can't be removed from the queue, and a FIFO queue can't gain one. + empty := "" + if _, err := m.UpdateJobQueue(ctx, driver.UpdateJobQueueInput{Name: "fq", SchedulingPolicyARN: &empty}); !errors.IsInvalidArgument(err) { + t.Fatalf("removing policy: want InvalidArgument, got %v", err) + } + + if _, err := m.CreateJobQueue(ctx, driver.CreateJobQueueInput{Name: "fifo", Priority: 1, ComputeEnvironmentOrder: order}); err != nil { + t.Fatal(err) + } + + if _, err := m.UpdateJobQueue(ctx, driver.UpdateJobQueueInput{Name: "fifo", SchedulingPolicyARN: &p.ARN}); !errors.IsInvalidArgument(err) { + t.Fatalf("adding policy to FIFO queue: want InvalidArgument, got %v", err) + } + + // Jobs on a fair-share queue need a share identifier. + if _, err := m.RegisterJobDefinition(ctx, driver.RegisterJobDefinitionInput{ + Name: "jd", Type: driver.JDTypeContainer, ContainerProperties: json.RawMessage(`{"image":"x"}`), + }); err != nil { + t.Fatal(err) + } + + if _, err := m.SubmitJob(ctx, driver.SubmitJobInput{JobName: "j", JobQueue: "fq", JobDefinition: "jd"}); !errors.IsInvalidArgument(err) { + t.Fatalf("fair-share submit without shareIdentifier: want InvalidArgument, got %v", err) + } + + if _, err := m.SubmitJob(ctx, driver.SubmitJobInput{JobName: "j", JobQueue: "fq", JobDefinition: "jd", ShareIdentifier: "A1"}); err != nil { + t.Fatalf("fair-share submit: %v", err) + } + + // An in-use policy can't be deleted. + if err := m.DeleteSchedulingPolicy(ctx, p.ARN); !errors.IsFailedPrecondition(err) { + t.Fatalf("delete in-use policy: want FailedPrecondition, got %v", err) + } + + disabled := driver.StateDisabled + if _, err := m.UpdateJobQueue(ctx, driver.UpdateJobQueueInput{Name: "fq", State: &disabled}); err != nil { + t.Fatal(err) + } + + if err := m.DeleteJobQueue(ctx, "fq"); err != nil { + t.Fatal(err) + } + + if err := m.DeleteSchedulingPolicy(ctx, p.ARN); err != nil { + t.Fatalf("delete: %v", err) + } + + if list, _ := m.ListSchedulingPolicies(ctx); len(list) != 0 { + t.Fatalf("policy not deleted: %+v", list) + } + + if err := m.DeleteSchedulingPolicy(ctx, p.ARN); !errors.IsNotFound(err) { + t.Fatalf("delete missing policy: want NotFound, got %v", err) + } +} + +func TestRegisterJobDefinitionTypeValidation(t *testing.T) { + m := newMock(t) + ctx := context.Background() + + cases := map[string]driver.RegisterJobDefinitionInput{ + "unknown type": {Name: "a", Type: "batch", ContainerProperties: json.RawMessage(`{"image":"x"}`)}, + "wrong case": {Name: "a", Type: "CONTAINER", ContainerProperties: json.RawMessage(`{"image":"x"}`)}, + "container without props": {Name: "a", Type: driver.JDTypeContainer}, + "multinode without nodeprop": {Name: "a", Type: driver.JDTypeMultiNode, ContainerProperties: json.RawMessage(`{"image":"x"}`)}, + } + + for name, in := range cases { + if _, err := m.RegisterJobDefinition(ctx, in); !errors.IsInvalidArgument(err) { + t.Errorf("%s: want InvalidArgument, got %v", name, err) + } + } + + if _, err := m.RegisterJobDefinition(ctx, driver.RegisterJobDefinitionInput{ + Name: "mn", Type: driver.JDTypeMultiNode, NodeProperties: json.RawMessage(`{"numNodes":2,"mainNode":0}`), + }); err != nil { + t.Fatalf("valid multinode: %v", err) + } + + if _, err := m.RegisterJobDefinition(ctx, driver.RegisterJobDefinitionInput{ + Name: "eks", Type: driver.JDTypeContainer, EksProperties: json.RawMessage(`{"podProperties":{}}`), + }); err != nil { + t.Fatalf("valid eks container: %v", err) + } +} + +func TestJobsAndPoliciesSurviveSnapshot(t *testing.T) { + src, fc := newAsyncMock(t) + setupQueue(t, src) + ctx := context.Background() + + if _, err := src.CreateSchedulingPolicy(ctx, driver.CreateSchedulingPolicyInput{Name: "sp"}); err != nil { + t.Fatal(err) + } + + j := submit(t, src, driver.SubmitJobInput{Tags: map[string]string{"t": "1"}}) + fc.Advance(time.Second) + + data, err := src.Snapshot(ctx, false) + if err != nil { + t.Fatal(err) + } + + dst := batch.New(config.NewOptions( + config.WithRegion("us-east-1"), config.WithAccountID("000000000000"), + config.WithClock(fc), config.WithAsyncSettle(), + )) + if err := dst.Restore(ctx, data); err != nil { + t.Fatal(err) + } + + if got := describe(t, dst, j.JobID); got.Status != driver.JobStatusRunnable || got.Tags["t"] != "1" { + t.Fatalf("restored job = %+v", got) + } + + fc.Advance(7 * time.Second) + + got := describe(t, dst, j.JobID) + if got.Status != driver.JobStatusSucceeded { + t.Fatalf("restored job did not resume its lifecycle: %s", got.Status) + } + + // The RUNNING dwell is part of the restored record. + if got.StoppedAt-got.StartedAt != settle.DefaultBatchJobRunDwell.Milliseconds() { + t.Fatalf("restored job ran for %dms, want %v", got.StoppedAt-got.StartedAt, settle.DefaultBatchJobRunDwell) + } + + if list, _ := dst.ListSchedulingPolicies(ctx); len(list) != 1 { + t.Fatalf("scheduling policy not restored: %+v", list) + } + + // Sequence numbering continues after restore. + k := submit(t, dst, driver.SubmitJobInput{JobName: "after"}) + + all, _ := listJobs(dst, driver.ListJobsInput{JobQueue: "q", Filters: []driver.JobFilter{{Name: driver.FilterJobDefinition, Values: []string{"jd"}}}}) + if len(all) != 2 || all[0].JobID != k.JobID { + t.Fatalf("post-restore ordering wrong: %+v", all) + } +} diff --git a/providers/aws/batch/overrides.go b/providers/aws/batch/overrides.go new file mode 100644 index 000000000..98e363404 --- /dev/null +++ b/providers/aws/batch/overrides.go @@ -0,0 +1,609 @@ +package batch + +import ( + "encoding/json" + "strconv" + "strings" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/services/batch/driver" +) + +// jsonObject is a decoded JSON object whose members stay raw, so fields the +// emulator doesn't interpret are echoed byte for byte. +type jsonObject = map[string]json.RawMessage + +// jobProperties is the node/ECS/EKS detail of a new job: the job definition's +// blobs with the SubmitJob overrides applied. +type jobProperties struct { + node, ecs, eks json.RawMessage +} + +// applyPropertyOverrides applies SubmitJob nodeOverrides, ecsPropertiesOverride +// and eksPropertiesOverride to the job definition. Each override is only valid +// for a job definition that has the matching properties (SubmitJob API +// reference): ECS overrides need ecsProperties, EKS overrides eksProperties, +// and node overrides a multinode job definition. +func applyPropertyOverrides(in *driver.SubmitJobInput, jd *driver.JobDefinition) (jobProperties, error) { + p := jobProperties{node: copyRaw(jd.NodeProperties), ecs: copyRaw(jd.EcsProperties), eks: copyRaw(jd.EksProperties)} + + var err error + + if isSet(in.EcsPropertiesOverride) { + if !isSet(jd.EcsProperties) { + return p, cerrors.New(cerrors.InvalidArgument, + "ecsPropertiesOverride can only be specified for a job definition with ecsProperties") + } + + if p.ecs, err = mergeEcsProperties(jd.EcsProperties, in.EcsPropertiesOverride); err != nil { + return p, err + } + } + + if isSet(in.EksPropertiesOverride) { + if !isSet(jd.EksProperties) { + return p, cerrors.New(cerrors.InvalidArgument, + "eksPropertiesOverride can only be specified for a job definition with eksProperties") + } + + if p.eks, err = mergeEksProperties(jd.EksProperties, in.EksPropertiesOverride); err != nil { + return p, err + } + } + + if isSet(in.NodeOverrides) { + if !isSet(jd.NodeProperties) { + return p, cerrors.New(cerrors.InvalidArgument, + "nodeOverrides can only be specified for a multinode job definition") + } + + if p.node, err = mergeNodeProperties(jd.NodeProperties, in.NodeOverrides); err != nil { + return p, err + } + } + + return p, nil +} + +// decodeObject decodes a JSON object; an unset value is an empty object. +func decodeObject(raw json.RawMessage, what string) (jsonObject, error) { + obj := jsonObject{} + if !isSet(raw) { + return obj, nil + } + + if err := json.Unmarshal(raw, &obj); err != nil { + return nil, cerrors.Newf(cerrors.InvalidArgument, "invalid %s: %v", what, err) + } + + return obj, nil +} + +// decodeObjects decodes a JSON array of objects; an unset value is empty. +func decodeObjects(raw json.RawMessage, what string) ([]jsonObject, error) { + if !isSet(raw) { + return nil, nil + } + + var out []jsonObject + if err := json.Unmarshal(raw, &out); err != nil { + return nil, cerrors.Newf(cerrors.InvalidArgument, "invalid %s: %v", what, err) + } + + return out, nil +} + +func encodeJSON(v any, what string) (json.RawMessage, error) { + out, err := json.Marshal(v) + if err != nil { + return nil, cerrors.Newf(cerrors.Internal, "encode %s: %v", what, err) + } + + return out, nil +} + +// mergeFields applies the set members of over onto base: members named in +// keyed are arrays of objects merged by the given key field (mergeKeyed), +// members in maps are string maps merged key by key, and every other member +// replaces the base value. +func mergeFields(base, over jsonObject, keyed map[string]string, maps map[string]bool) { + for k, v := range over { + if !isSet(v) { + continue + } + + switch { + case keyed[k] != "": + base[k] = mergeKeyed(base[k], v, keyed[k]) + case maps[k]: + base[k] = mergeStringMap(base[k], v) + default: + base[k] = v + } + } +} + +// mergeStringMap merges two JSON string maps, over winning on a shared key. +func mergeStringMap(base, over json.RawMessage) json.RawMessage { + var b, o map[string]json.RawMessage + + if err := json.Unmarshal(over, &o); err != nil { + return over + } + + if isSet(base) { + if err := json.Unmarshal(base, &b); err != nil { + return over + } + } + + if b == nil { + b = make(map[string]json.RawMessage, len(o)) + } + + for k, v := range o { + b[k] = v + } + + out, err := json.Marshal(b) + if err != nil { + return over + } + + return out +} + +// containerName returns the "name" member of a container object. +func containerName(c jsonObject) string { + var s string + + if raw, ok := c["name"]; ok { + _ = json.Unmarshal(raw, &s) + } + + return s +} + +// mergeNamedContainers applies container overrides to a container list. Each +// override names the container it replaces settings of; an unnamed override is +// accepted only when the list has exactly one container. +func mergeNamedContainers( + base []jsonObject, overrides []jsonObject, what string, keyed map[string]string, +) ([]jsonObject, error) { + for _, o := range overrides { + name := containerName(o) + + target := -1 + + switch { + case name == "" && len(base) == 1: + target = 0 + case name == "": + return nil, cerrors.Newf(cerrors.InvalidArgument, + "%s: name is required when the job definition has more than one container", what) + default: + for i := range base { + if containerName(base[i]) == name { + target = i + + break + } + } + } + + if target < 0 { + return nil, cerrors.Newf(cerrors.InvalidArgument, + "%s: container %q is not defined in the job definition", what, name) + } + + mergeFields(base[target], o, keyed, nil) + } + + return base, nil +} + +//nolint:gochecknoglobals // immutable merge rules, read-only after init +var ( + // ECS TaskContainerOverrides: environment merges by name, resource + // requirements by type; command (and name) replace. + ecsContainerKeyed = map[string]string{"environment": "name", "resourceRequirements": "type"} + // EksContainerOverride: env merges by name; image, command, args and + // resources replace. + eksContainerKeyed = map[string]string{"env": "name"} + // EksMetadata: labels and annotations merge key by key; namespace replaces. + eksMetadataMaps = map[string]bool{"labels": true, "annotations": true} +) + +// mergeEcsProperties applies an EcsPropertiesOverride: taskProperties[i] +// overrides the definition's taskProperties[i], each container override +// targeting a container by name. +func mergeEcsProperties(props, override json.RawMessage) (json.RawMessage, error) { + const what = "ecsPropertiesOverride" + + base, err := decodeObject(props, "ecsProperties") + if err != nil { + return copyRaw(props), nil //nolint:nilerr // a non-object definition blob is echoed unchanged + } + + over, err := decodeObject(override, what) + if err != nil { + return nil, err + } + + baseTasks, err := decodeObjects(base["taskProperties"], "ecsProperties.taskProperties") + if err != nil { + return copyRaw(props), nil //nolint:nilerr // a non-object definition blob is echoed unchanged + } + + overTasks, err := decodeObjects(over["taskProperties"], what+".taskProperties") + if err != nil { + return nil, err + } + + if len(overTasks) > len(baseTasks) { + return nil, cerrors.Newf(cerrors.InvalidArgument, + "%s lists %d taskProperties but the job definition has %d", what, len(overTasks), len(baseTasks)) + } + + for i, ot := range overTasks { + if mergeErr := mergeTaskContainers(baseTasks[i], ot, "containers", what, ecsContainerKeyed); mergeErr != nil { + return nil, mergeErr + } + } + + if len(overTasks) > 0 { + if base["taskProperties"], err = encodeJSON(baseTasks, "ecsProperties"); err != nil { + return nil, err + } + } + + return encodeJSON(base, "ecsProperties") +} + +// mergeTaskContainers merges the container overrides listed under field of +// over into the same field of base. +func mergeTaskContainers(base, over jsonObject, field, what string, keyed map[string]string) error { + overs, err := decodeObjects(over[field], what+"."+field) + if err != nil { + return err + } + + if len(overs) == 0 { + return nil + } + + containers, err := decodeObjects(base[field], field) + if err != nil { + return cerrors.Newf(cerrors.InvalidArgument, "%s: the job definition's %s can't be overridden", what, field) + } + + merged, err := mergeNamedContainers(containers, overs, what+"."+field, keyed) + if err != nil { + return err + } + + base[field], err = encodeJSON(merged, field) + + return err +} + +// mergeEksProperties applies an EksPropertiesOverride to the definition's +// podProperties: containers and initContainers by name, plus pod metadata. +func mergeEksProperties(props, override json.RawMessage) (json.RawMessage, error) { + const what = "eksPropertiesOverride" + + base, err := decodeObject(props, "eksProperties") + if err != nil { + return copyRaw(props), nil //nolint:nilerr // a non-object definition blob is echoed unchanged + } + + over, err := decodeObject(override, what) + if err != nil { + return nil, err + } + + overPod, err := decodeObject(over["podProperties"], what+".podProperties") + if err != nil { + return nil, err + } + + if len(overPod) == 0 { + return copyRaw(props), nil + } + + pod, err := decodeObject(base["podProperties"], "eksProperties.podProperties") + if err != nil { + return copyRaw(props), nil //nolint:nilerr // a non-object definition blob is echoed unchanged + } + + if podErr := mergeEksPod(pod, overPod); podErr != nil { + return nil, podErr + } + + if base["podProperties"], err = encodeJSON(pod, "podProperties"); err != nil { + return nil, err + } + + return encodeJSON(base, "eksProperties") +} + +// mergeEksPod applies an EksPodPropertiesOverride to podProperties: containers +// and initContainers by name, plus pod metadata. +func mergeEksPod(pod, overPod jsonObject) error { + const what = "eksPropertiesOverride.podProperties" + + for _, field := range []string{"containers", "initContainers"} { + if err := mergeTaskContainers(pod, overPod, field, what, eksContainerKeyed); err != nil { + return err + } + } + + if !isSet(overPod["metadata"]) { + return nil + } + + meta, err := mergeEksMetadata(pod["metadata"], overPod["metadata"]) + if err != nil { + return err + } + + pod["metadata"] = meta + + return nil +} + +// mergeEksMetadata applies an EksMetadata override: labels and annotations +// merge key by key, namespace replaces. +func mergeEksMetadata(base, override json.RawMessage) (json.RawMessage, error) { + meta, err := decodeObject(base, "eksProperties.podProperties.metadata") + if err != nil { + meta = jsonObject{} + } + + overMeta, err := decodeObject(override, "eksPropertiesOverride.podProperties.metadata") + if err != nil { + return nil, err + } + + mergeFields(meta, overMeta, nil, eksMetadataMaps) + + return encodeJSON(meta, "metadata") +} + +// nodeRange is a resolved targetNodes range, both ends inclusive. +type nodeRange struct{ start, end int } + +// parseTargetNodes resolves a targetNodes value ("0:3", ":3", "2:", ":" or a +// single index) against the job's node count: an omitted start is 0 and an +// omitted end the highest node index. +func parseTargetNodes(s string, numNodes int) (nodeRange, bool) { + lo, hi, isRange := strings.Cut(s, ":") + if !isRange { + hi = lo + } + + r := nodeRange{start: 0, end: numNodes - 1} + + if lo != "" { + n, err := strconv.Atoi(lo) + if err != nil || n < 0 { + return nodeRange{}, false + } + + r.start = n + } + + if hi != "" { + n, err := strconv.Atoi(hi) + if err != nil || n < 0 { + return nodeRange{}, false + } + + r.end = n + } + + return r, r.start <= r.end +} + +// openUpper reports whether a targetNodes value has no upper bound ("n:" or +// ":"), and returns its lower bound. +func openUpper(s string) (lower int, open bool) { + lo, hi, isRange := strings.Cut(s, ":") + if !isRange || hi != "" { + return 0, false + } + + if lo == "" { + return 0, true + } + + n, err := strconv.Atoi(lo) + if err != nil { + return 0, false + } + + return n, true +} + +// nodeOverrides is the NodeOverrides shape; each property override stays raw +// apart from its target range. +type nodeOverrides struct { + NumNodes *int `json:"numNodes"` + NodePropertyOverrides []jsonObject `json:"nodePropertyOverrides"` +} + +// nodeDefinition is the part of NodeProperties a node override is checked +// against. +type nodeDefinition struct { + NumNodes int `json:"numNodes"` + MainNode int `json:"mainNode"` + NodeRangeProperties []jsonObject `json:"nodeRangeProperties"` +} + +// mergeNodeProperties applies NodeOverrides: numNodes replaces the definition's +// node count under the documented conditions, and each nodePropertyOverride +// applies to every node range of the definition that lies inside its +// targetNodes. +func mergeNodeProperties(props, override json.RawMessage) (json.RawMessage, error) { + const what = "nodeOverrides" + + var no nodeOverrides + if err := json.Unmarshal(override, &no); err != nil { + return nil, cerrors.Newf(cerrors.InvalidArgument, "invalid %s: %v", what, err) + } + + base, err := decodeObject(props, "nodeProperties") + if err != nil { + return copyRaw(props), nil //nolint:nilerr // a non-object definition blob is echoed unchanged + } + + var def nodeDefinition + if json.Unmarshal(props, &def) != nil { + return copyRaw(props), nil + } + + if no.NumNodes != nil { + if numErr := checkNumNodesOverride(&def, *no.NumNodes); numErr != nil { + return nil, numErr + } + + def.NumNodes = *no.NumNodes + base["numNodes"] = json.RawMessage(strconv.Itoa(*no.NumNodes)) + } + + for i, po := range no.NodePropertyOverrides { + if poErr := applyNodePropertyOverride(&def, po, i); poErr != nil { + return nil, poErr + } + } + + if len(no.NodePropertyOverrides) > 0 { + if base["nodeRangeProperties"], err = encodeJSON(def.NodeRangeProperties, "nodeRangeProperties"); err != nil { + return nil, err + } + } + + return encodeJSON(base, "nodeProperties") +} + +// checkNumNodesOverride enforces the NodeOverrides.numNodes conditions: the +// definition has a node range with an open upper boundary whose lower boundary +// is below the new count, and the main node index is below it too. +func checkNumNodesOverride(def *nodeDefinition, n int) error { + if n < 1 { + return cerrors.Newf(cerrors.InvalidArgument, "nodeOverrides.numNodes must be at least 1, got %d", n) + } + + if def.MainNode >= n { + return cerrors.Newf(cerrors.InvalidArgument, + "nodeOverrides.numNodes (%d) must be greater than the job definition's mainNode (%d)", n, def.MainNode) + } + + for _, r := range def.NodeRangeProperties { + var target string + _ = json.Unmarshal(r["targetNodes"], &target) + + if lower, open := openUpper(target); open && lower < n { + return nil + } + } + + return cerrors.Newf(cerrors.InvalidArgument, + "nodeOverrides.numNodes requires a node range with an open upper boundary (such as \"n:\") "+ + "whose lower boundary is below %d", n) +} + +// nodeRangeOverrideFields says how each NodePropertyOverride member maps onto a +// NodeRangeProperty member. +// +//nolint:gochecknoglobals // immutable mapping, read-only after init +var nodeRangeOverrideFields = map[string]string{ + "containerOverrides": "container", + "ecsPropertiesOverride": "ecsProperties", + "eksPropertiesOverride": "eksProperties", + "instanceTypes": "instanceTypes", + "consumableResourcePropertiesOverride": "consumableResourceProperties", +} + +// applyNodePropertyOverride applies one NodePropertyOverride to every node +// range of def inside its targetNodes. An override that covers no node range of +// the job definition is rejected rather than ignored. +func applyNodePropertyOverride(def *nodeDefinition, po jsonObject, i int) error { + var target string + _ = json.Unmarshal(po["targetNodes"], &target) + + if target == "" { + return cerrors.Newf(cerrors.InvalidArgument, "nodeOverrides.nodePropertyOverrides[%d].targetNodes is required", i) + } + + or, ok := parseTargetNodes(target, def.NumNodes) + if !ok { + return cerrors.Newf(cerrors.InvalidArgument, + "nodeOverrides.nodePropertyOverrides[%d].targetNodes %q is not a valid node range", i, target) + } + + matched := false + + for _, r := range def.NodeRangeProperties { + var rt string + _ = json.Unmarshal(r["targetNodes"], &rt) + + rr, ok := parseTargetNodes(rt, def.NumNodes) + if !ok || rr.start < or.start || rr.end > or.end { + continue + } + + if err := overrideNodeRange(r, po); err != nil { + return err + } + + matched = true + } + + if !matched { + return cerrors.Newf(cerrors.InvalidArgument, + "nodeOverrides.nodePropertyOverrides[%d].targetNodes %q doesn't cover any node range of the job definition", + i, target) + } + + return nil +} + +// overrideNodeRange applies one NodePropertyOverride to one node range. +func overrideNodeRange(r, po jsonObject) error { + for from, to := range nodeRangeOverrideFields { + v := po[from] + if !isSet(v) { + continue + } + + var ( + merged json.RawMessage + err error + ) + + switch from { + case "containerOverrides": + if !isSet(r[to]) { + return cerrors.New(cerrors.InvalidArgument, + "nodeOverrides: containerOverrides target a node range without container properties") + } + + merged, err = mergeContainer(r[to], v) + case "ecsPropertiesOverride": + merged, err = mergeEcsProperties(r[to], v) + case "eksPropertiesOverride": + merged, err = mergeEksProperties(r[to], v) + default: + merged = copyRaw(v) + } + + if err != nil { + return err + } + + r[to] = merged + } + + return nil +} diff --git a/providers/aws/batch/scheduling_policy.go b/providers/aws/batch/scheduling_policy.go new file mode 100644 index 000000000..046b9a8f9 --- /dev/null +++ b/providers/aws/batch/scheduling_policy.go @@ -0,0 +1,284 @@ +package batch + +import ( + "context" + "encoding/json" + "regexp" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/services/batch/driver" +) + +// Fair-share policy bounds from the FairsharePolicy and ShareAttributes API +// reference. +const ( + maxComputeReservation = 99 + maxShareDecaySeconds = 604800 + minWeightFactor = 0.0001 + maxWeightFactor = 999.9999 + maxShareIdentifierLen = 255 + maxDescribeArns = 100 +) + +// resourceNameRE is the CreateSchedulingPolicy / CreateJobQueue name rule: up +// to 128 letters, numbers, hyphens and underscores. +var resourceNameRE = regexp.MustCompile(`^[A-Za-z0-9_-]{1,128}$`) + +// fairsharePolicy is the subset of the FairsharePolicy shape the emulator +// validates; the raw JSON the caller sent is what is stored and echoed. +type fairsharePolicy struct { + ComputeReservation *int64 `json:"computeReservation"` + ShareDecaySeconds *int64 `json:"shareDecaySeconds"` + ShareDistribution []shareAttributes `json:"shareDistribution"` +} + +// shareAttributes is one ShareAttributes entry of a fair-share policy. +type shareAttributes struct { + ShareIdentifier *string `json:"shareIdentifier"` + WeightFactor *float64 `json:"weightFactor"` +} + +//nolint:gocritic // clone intentionally takes a value to return an independent copy-on-write copy +func cloneSchedulingPolicy(p driver.SchedulingPolicy) driver.SchedulingPolicy { + p.FairsharePolicy = copyRaw(p.FairsharePolicy) + p.QuotaSharePolicy = copyRaw(p.QuotaSharePolicy) + p.Tags = copyTags(p.Tags) + + return p +} + +// isSet reports whether a raw JSON member was actually supplied (not absent or +// an explicit null). +func isSet(raw json.RawMessage) bool { + return len(raw) > 0 && string(raw) != "null" +} + +// validateFairshare enforces the documented FairsharePolicy bounds. +func validateFairshare(raw json.RawMessage) error { + if !isSet(raw) { + return nil + } + + var fp fairsharePolicy + if err := json.Unmarshal(raw, &fp); err != nil { + return cerrors.Newf(cerrors.InvalidArgument, "invalid fairsharePolicy: %v", err) + } + + if err := checkRange("computeReservation", fp.ComputeReservation, maxComputeReservation); err != nil { + return err + } + + if err := checkRange("shareDecaySeconds", fp.ShareDecaySeconds, maxShareDecaySeconds); err != nil { + return err + } + + for i := range fp.ShareDistribution { + if err := validateShareAttributes(i, &fp.ShareDistribution[i]); err != nil { + return err + } + } + + return nil +} + +// checkRange rejects a set fair-share integer outside [0, maxValue]. +func checkRange(field string, v *int64, maxValue int64) error { + if v != nil && (*v < 0 || *v > maxValue) { + return cerrors.Newf(cerrors.InvalidArgument, + "fairsharePolicy.%s must be between 0 and %d, got %d", field, maxValue, *v) + } + + return nil +} + +// validateShareAttributes enforces the ShareAttributes bounds of one +// shareDistribution entry: a required identifier of at most 255 characters +// (plus an optional trailing '*') and a weight factor in [0.0001, 999.9999]. +func validateShareAttributes(i int, sd *shareAttributes) error { + if sd.ShareIdentifier == nil || *sd.ShareIdentifier == "" { + return cerrors.Newf(cerrors.InvalidArgument, "fairsharePolicy.shareDistribution[%d].shareIdentifier is required", i) + } + + if len(*sd.ShareIdentifier) > maxShareIdentifierLen+1 { + return cerrors.Newf(cerrors.InvalidArgument, + "fairsharePolicy.shareDistribution[%d].shareIdentifier is longer than %d characters", i, maxShareIdentifierLen) + } + + if w := sd.WeightFactor; w != nil && (*w < minWeightFactor || *w > maxWeightFactor) { + return cerrors.Newf(cerrors.InvalidArgument, + "fairsharePolicy.shareDistribution[%d].weightFactor must be between %v and %v, got %v", + i, minWeightFactor, maxWeightFactor, *w) + } + + return nil +} + +// CreateSchedulingPolicy creates a fair-share (or quota-share) scheduling +// policy. Only one of the two policy kinds may be set. +func (m *Mock) CreateSchedulingPolicy( + _ context.Context, in driver.CreateSchedulingPolicyInput, +) (*driver.SchedulingPolicy, error) { + if in.Name == "" { + return nil, cerrors.New(cerrors.InvalidArgument, "name is required") + } + + if !resourceNameRE.MatchString(in.Name) { + return nil, cerrors.Newf(cerrors.InvalidArgument, + "scheduling policy name %q must be up to 128 letters, numbers, hyphens and underscores", in.Name) + } + + if isSet(in.FairsharePolicy) && isSet(in.QuotaSharePolicy) { + return nil, cerrors.New(cerrors.InvalidArgument, "only one of fairsharePolicy or quotaSharePolicy can be set") + } + + if err := validateTags(in.Tags); err != nil { + return nil, err + } + + if err := validateFairshare(in.FairsharePolicy); err != nil { + return nil, err + } + + p := driver.SchedulingPolicy{ + Name: in.Name, + ARN: m.arn(kindSchedulingPolicy, in.Name), + FairsharePolicy: copyRaw(nonNull(in.FairsharePolicy)), + QuotaSharePolicy: copyRaw(nonNull(in.QuotaSharePolicy)), + Tags: copyTags(in.Tags), + } + + if !m.schedPolicies.SetIfAbsent(in.Name, p) { + return nil, cerrors.Newf(cerrors.AlreadyExists, "scheduling policy %q already exists", in.Name) + } + + out := cloneSchedulingPolicy(p) + + return &out, nil +} + +// nonNull drops an explicit JSON null so it is stored as "absent". +func nonNull(raw json.RawMessage) json.RawMessage { + if !isSet(raw) { + return nil + } + + return raw +} + +// DescribeSchedulingPolicies returns the named policies (ARN or name) in +// request order; unknown entries are omitted. At least one and at most 100 +// ARNs are required. +func (m *Mock) DescribeSchedulingPolicies(_ context.Context, arns []string) ([]driver.SchedulingPolicy, error) { + if len(arns) == 0 { + return nil, cerrors.New(cerrors.InvalidArgument, "arns is required") + } + + if len(arns) > maxDescribeArns { + return nil, cerrors.Newf(cerrors.InvalidArgument, "arns accepts at most %d entries, got %d", maxDescribeArns, len(arns)) + } + + return describeByNames(m.schedPolicies, arns, kindSchedulingPolicy, cloneSchedulingPolicy), nil +} + +// ListSchedulingPolicies returns every policy, ordered by name. +func (m *Mock) ListSchedulingPolicies(_ context.Context) ([]driver.SchedulingPolicy, error) { + return describeByNames(m.schedPolicies, nil, kindSchedulingPolicy, cloneSchedulingPolicy), nil +} + +// UpdateSchedulingPolicy replaces the policy details. A fair-share policy can't +// be turned into a quota-share policy or vice versa. +func (m *Mock) UpdateSchedulingPolicy(_ context.Context, in driver.UpdateSchedulingPolicyInput) error { + if in.ARN == "" { + return cerrors.New(cerrors.InvalidArgument, "arn is required") + } + + if isSet(in.FairsharePolicy) && isSet(in.QuotaSharePolicy) { + return cerrors.New(cerrors.InvalidArgument, "only one of fairsharePolicy or quotaSharePolicy can be set") + } + + if err := validateFairshare(in.FairsharePolicy); err != nil { + return err + } + + name := nameFromARN(kindSchedulingPolicy, in.ARN) + + cur, ok := m.schedPolicies.Get(name) + if !ok { + return cerrors.Newf(cerrors.NotFound, "scheduling policy %q does not exist", in.ARN) + } + + if err := checkPolicyKind(&cur, &in); err != nil { + return err + } + + m.schedPolicies.Update(name, func(p driver.SchedulingPolicy) driver.SchedulingPolicy { + next := cloneSchedulingPolicy(p) + if isSet(in.FairsharePolicy) { + next.FairsharePolicy = copyRaw(in.FairsharePolicy) + } + + if isSet(in.QuotaSharePolicy) { + next.QuotaSharePolicy = copyRaw(in.QuotaSharePolicy) + } + + return next + }) + + return nil +} + +// checkPolicyKind rejects an update that would turn a fair-share policy into a +// quota-share one or vice versa. +func checkPolicyKind(cur *driver.SchedulingPolicy, in *driver.UpdateSchedulingPolicyInput) error { + if isSet(in.FairsharePolicy) && isSet(cur.QuotaSharePolicy) { + return cerrors.New(cerrors.InvalidArgument, "a quotaSharePolicy can't be changed to a fairsharePolicy") + } + + if isSet(in.QuotaSharePolicy) && isSet(cur.FairsharePolicy) { + return cerrors.New(cerrors.InvalidArgument, "a fairsharePolicy can't be changed to a quotaSharePolicy") + } + + return nil +} + +// DeleteSchedulingPolicy deletes a policy. A policy still referenced by a job +// queue can't be deleted. +func (m *Mock) DeleteSchedulingPolicy(_ context.Context, arn string) error { + if arn == "" { + return cerrors.New(cerrors.InvalidArgument, "arn is required") + } + + name := nameFromARN(kindSchedulingPolicy, arn) + + m.policyBindMu.Lock() + defer m.policyBindMu.Unlock() + + p, ok := m.schedPolicies.Get(name) + if !ok { + return cerrors.Newf(cerrors.NotFound, "scheduling policy %q does not exist", arn) + } + + queues := m.jobQueues.SortedValues() + for i := range queues { + q := &queues[i] + if q.SchedulingPolicyARN != "" && nameFromARN(kindSchedulingPolicy, q.SchedulingPolicyARN) == name { + return cerrors.Newf(cerrors.FailedPrecondition, + "scheduling policy %q is in use by job queue %q", p.ARN, q.Name) + } + } + + m.schedPolicies.Delete(name) + + return nil +} + +// resolveSchedulingPolicy returns the canonical ARN of an existing policy, or a +// ClientException-mapped error when it does not exist. +func (m *Mock) resolveSchedulingPolicy(ref string) (string, error) { + p, ok := m.schedPolicies.Get(nameFromARN(kindSchedulingPolicy, ref)) + if !ok { + return "", cerrors.Newf(cerrors.InvalidArgument, "scheduling policy %q does not exist", ref) + } + + return p.ARN, nil +} diff --git a/providers/aws/batch/snapshot.go b/providers/aws/batch/snapshot.go index 1bc5a8265..6fbfa1e86 100644 --- a/providers/aws/batch/snapshot.go +++ b/providers/aws/batch/snapshot.go @@ -13,13 +13,18 @@ var _ snapshot.Snapshottable = (*Mock)(nil) // batchSnapshot is the full serialized state of the Batch mock. Every store // holds a plain JSON-serializable value, so each round-trips through the generic // memstore dump helper under its original keys (compute-environment/job-queue -// name, job-definition "name:revision"). The registration mutex and the wired +// name, job-definition "name:revision", scheduling-policy name, job ID). Job +// records carry their submission instant and phase duration, so a restored job +// resumes its lifecycle where it left off. The registration mutex and the wired // *config.Options are intentionally not serialized. type batchSnapshot struct { - ComputeEnvs json.RawMessage `json:"computeEnvs,omitempty"` - JobQueues json.RawMessage `json:"jobQueues,omitempty"` - JobDefs json.RawMessage `json:"jobDefs,omitempty"` - JobDefMaxRev json.RawMessage `json:"jobDefMaxRev,omitempty"` + ComputeEnvs json.RawMessage `json:"computeEnvs,omitempty"` + JobQueues json.RawMessage `json:"jobQueues,omitempty"` + JobDefs json.RawMessage `json:"jobDefs,omitempty"` + JobDefMaxRev json.RawMessage `json:"jobDefMaxRev,omitempty"` + SchedPolicies json.RawMessage `json:"schedulingPolicies,omitempty"` + Jobs json.RawMessage `json:"jobs,omitempty"` + JobSeq json.RawMessage `json:"jobSeq,omitempty"` } // Snapshot captures the mock's entire state as JSON. includeAssets is unused. Batch holds no bulk @@ -35,6 +40,9 @@ func (m *Mock) Snapshot(_ context.Context, _ bool) (json.RawMessage, error) { {&snap.JobQueues, m.jobQueues.Snapshot}, {&snap.JobDefs, m.jobDefs.Snapshot}, {&snap.JobDefMaxRev, m.jobDefMaxRev.Snapshot}, + {&snap.SchedPolicies, m.schedPolicies.Snapshot}, + {&snap.Jobs, m.jobs.Snapshot}, + {&snap.JobSeq, m.jobSeq.Snapshot}, } for _, d := range dumps { @@ -64,6 +72,9 @@ func (m *Mock) Restore(_ context.Context, data json.RawMessage) error { {snap.JobQueues, m.jobQueues.LoadSnapshot}, {snap.JobDefs, m.jobDefs.LoadSnapshot}, {snap.JobDefMaxRev, m.jobDefMaxRev.LoadSnapshot}, + {snap.SchedPolicies, m.schedPolicies.LoadSnapshot}, + {snap.Jobs, m.jobs.LoadSnapshot}, + {snap.JobSeq, m.jobSeq.LoadSnapshot}, } for _, l := range loads { diff --git a/providers/aws/batch/tags.go b/providers/aws/batch/tags.go index 76b03b983..8ba8ccdef 100644 --- a/providers/aws/batch/tags.go +++ b/providers/aws/batch/tags.go @@ -17,6 +17,10 @@ func kindFromARN(arn string) string { return kindJobQueue case strings.Contains(arn, ":"+kindJobDefinition+"/"): return kindJobDefinition + case strings.Contains(arn, ":"+kindSchedulingPolicy+"/"): + return kindSchedulingPolicy + case strings.Contains(arn, ":"+kindJob+"/"): + return kindJob default: return "" } @@ -44,11 +48,64 @@ func removeTags(existing map[string]string, keys []string) map[string]string { return out } -// TagResource adds or replaces tags on the resource named by arn. +// Tag limits from the Batch TagResource reference: at most 50 tags, keys of +// 1-128 characters, values of at most 256 characters. The "aws:" key prefix is +// reserved for AWS (see "Tagging AWS resources"). +const ( + maxTags = 50 + maxTagKeyLen = 128 + maxTagValueLen = 256 + reservedPrefix = "aws:" +) + +// validateTags checks one tag map against the TagResource limits. +func validateTags(tags map[string]string) error { + if len(tags) > maxTags { + return cerrors.Newf(cerrors.InvalidArgument, "a resource can have at most %d tags, got %d", maxTags, len(tags)) + } + + for k, v := range tags { + switch { + case k == "" || len(k) > maxTagKeyLen: + return cerrors.Newf(cerrors.InvalidArgument, "tag key %q must be 1 to %d characters", k, maxTagKeyLen) + case len(v) > maxTagValueLen: + return cerrors.Newf(cerrors.InvalidArgument, "tag value for key %q must be at most %d characters", k, maxTagValueLen) + case strings.HasPrefix(strings.ToLower(k), reservedPrefix): + return cerrors.Newf(cerrors.InvalidArgument, "tag key %q uses the reserved prefix %q", k, reservedPrefix) + } + } + + return nil +} + +// TagResource adds or replaces tags on the resource named by arn. Child jobs of +// an array job can't be tagged. func (m *Mock) TagResource(_ context.Context, arn string, tags map[string]string) error { - return m.mutateTags(arn, func(existing map[string]string) map[string]string { - return mergeTags(existing, tags) + if len(tags) == 0 { + return cerrors.New(cerrors.InvalidArgument, "tags is required") + } + + if err := validateTags(tags); err != nil { + return err + } + + var limitErr error + + err := m.mutateTags(arn, func(existing map[string]string) map[string]string { + merged := mergeTags(existing, tags) + if len(merged) > maxTags { + limitErr = cerrors.Newf(cerrors.InvalidArgument, "a resource can have at most %d tags", maxTags) + + return existing + } + + return merged }) + if err != nil { + return err + } + + return limitErr } // UntagResource removes the given tag keys from the resource named by arn. @@ -60,24 +117,45 @@ func (m *Mock) UntagResource(_ context.Context, arn string, keys []string) error // ListTagsForResource returns the tags on the resource named by arn. func (m *Mock) ListTagsForResource(_ context.Context, arn string) (map[string]string, error) { - switch kindFromARN(arn) { + kind := kindFromARN(arn) + if kind == "" { + return nil, cerrors.Newf(cerrors.InvalidArgument, "unsupported resource ARN %q", arn) + } + + tags, ok := m.tagsOf(kind, nameFromARN(kind, arn)) + if !ok { + return nil, cerrors.Newf(cerrors.NotFound, "resource %q does not exist", arn) + } + + return copyTags(tags), nil +} + +// tagsOf returns the stored tags of the named resource of the given kind. +func (m *Mock) tagsOf(kind, name string) (map[string]string, bool) { + switch kind { case kindComputeEnvironment: - if ce, ok := m.computeEnvs.Get(nameFromARN(kindComputeEnvironment, arn)); ok { - return copyTags(ce.Tags), nil - } + ce, ok := m.computeEnvs.Get(name) + + return ce.Tags, ok case kindJobQueue: - if q, ok := m.jobQueues.Get(nameFromARN(kindJobQueue, arn)); ok { - return copyTags(q.Tags), nil - } + q, ok := m.jobQueues.Get(name) + + return q.Tags, ok case kindJobDefinition: - if jd, ok := m.jobDefs.Get(nameFromARN(kindJobDefinition, arn)); ok { - return copyTags(jd.Tags), nil - } + jd, ok := m.jobDefs.Get(name) + + return jd.Tags, ok + case kindSchedulingPolicy: + p, ok := m.schedPolicies.Get(name) + + return p.Tags, ok + case kindJob: + r, ok := m.jobs.Get(name) + + return r.Job.Tags, ok default: - return nil, cerrors.Newf(cerrors.InvalidArgument, "unsupported resource ARN %q", arn) + return nil, false } - - return nil, cerrors.Newf(cerrors.NotFound, "resource %q does not exist", arn) } // mutateTags applies fn to the tag map of the resource named by arn. @@ -109,6 +187,24 @@ func (m *Mock) mutateTags(arn string, fn func(map[string]string) map[string]stri return next }) + case kindSchedulingPolicy: + ok = m.schedPolicies.Update(nameFromARN(kindSchedulingPolicy, arn), + func(p driver.SchedulingPolicy) driver.SchedulingPolicy { + next := cloneSchedulingPolicy(p) + next.Tags = fn(next.Tags) + + return next + }) + case kindJob: + // jobsMu: publishJobEvents rewrites job records under it. + m.jobsMu.Lock() + ok = m.jobs.Update(nameFromARN(kindJob, arn), func(r jobRecord) jobRecord { + next := cloneJobRecord(r) + next.Job.Tags = fn(next.Job.Tags) + + return next + }) + m.jobsMu.Unlock() default: return cerrors.Newf(cerrors.InvalidArgument, "unsupported resource ARN %q", arn) } diff --git a/providers/aws/tickables_test.go b/providers/aws/tickables_test.go index 2347f0c1a..ab76f1d5e 100644 --- a/providers/aws/tickables_test.go +++ b/providers/aws/tickables_test.go @@ -8,17 +8,17 @@ import ( ) // TestTickablesRegistersTimeDrivenServices checks the serve ticker reaches the -// region's own SQS, EFS, CloudWatch, SSM and CloudFormation, so queue gauges, -// EFS metrics, due alarms, parameter policies and settling stack operations run -// without a read. SQS and EFS come before CloudWatch so alarms see the metrics -// published in the same tick. +// region's own SQS, EFS, CloudWatch, SSM, CloudFormation and Batch, so queue +// gauges, EFS metrics, due alarms, parameter policies, settling stack operations +// and Batch job state-change events run without a read. SQS and EFS come before +// CloudWatch so alarms see the metrics published in the same tick. func TestTickablesRegistersTimeDrivenServices(t *testing.T) { p := aws.New() got := p.Tickables() - if len(got) != 5 || got[0] != config.Tickable(p.SQS) || got[1] != config.Tickable(p.EFS) || + if len(got) != 6 || got[0] != config.Tickable(p.SQS) || got[1] != config.Tickable(p.EFS) || got[2] != config.Tickable(p.CloudWatch) || got[3] != config.Tickable(p.SSM) || - got[4] != config.Tickable(p.CloudFormation) { - t.Fatalf("Tickables() = %v, want this provider's SQS, EFS, CloudWatch, SSM and CloudFormation", got) + got[4] != config.Tickable(p.CloudFormation) || got[5] != config.Tickable(p.Batch) { + t.Fatalf("Tickables() = %v, want this provider's SQS, EFS, CloudWatch, SSM, CloudFormation and Batch", got) } } diff --git a/server/aws/batch/handler.go b/server/aws/batch/handler.go index fcced33a4..2883e96bb 100644 --- a/server/aws/batch/handler.go +++ b/server/aws/batch/handler.go @@ -2,8 +2,8 @@ // awsRestjson1) as a server.Handler. Point the real // aws-sdk-go-v2/service/batch client (or the `aws batch` CLI, or the // aws_batch_* Terraform resources) at a Server registered with this handler and -// the compute-environment, job-queue, and job-definition operations work -// end-to-end against an in-memory driver. +// the compute-environment, job-queue, job-definition, scheduling-policy and job +// operations work end-to-end against an in-memory driver. // // Batch routes by HTTP verb + a fixed operation path under the /v1/ prefix // (e.g. POST /v1/createcomputeenvironment); there is no X-Amz-Target header. @@ -35,6 +35,16 @@ const ( opRegisterJobDefinition = "registerjobdefinition" opDescribeJobDefinitions = "describejobdefinitions" opDeregisterJobDefinition = "deregisterjobdefinition" + opCreateSchedulingPolicy = "createschedulingpolicy" + opDescribeSchedulingPolicies = "describeschedulingpolicies" + opListSchedulingPolicies = "listschedulingpolicies" + opUpdateSchedulingPolicy = "updateschedulingpolicy" + opDeleteSchedulingPolicy = "deleteschedulingpolicy" + opSubmitJob = "submitjob" + opDescribeJobs = "describejobs" + opListJobs = "listjobs" + opCancelJob = "canceljob" + opTerminateJob = "terminatejob" opTags = "tags" ) @@ -63,6 +73,16 @@ var knownOps = map[string]bool{ opRegisterJobDefinition: true, opDescribeJobDefinitions: true, opDeregisterJobDefinition: true, + opCreateSchedulingPolicy: true, + opDescribeSchedulingPolicies: true, + opListSchedulingPolicies: true, + opUpdateSchedulingPolicy: true, + opDeleteSchedulingPolicy: true, + opSubmitJob: true, + opDescribeJobs: true, + opListJobs: true, + opCancelJob: true, + opTerminateJob: true, opTags: true, } @@ -118,7 +138,9 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *Handler) serveOperation(w http.ResponseWriter, r *http.Request, op string) { if h.serveComputeEnvironmentOp(w, r, op) || h.serveJobQueueOp(w, r, op) || - h.serveJobDefinitionOp(w, r, op) { + h.serveJobDefinitionOp(w, r, op) || + h.serveSchedulingPolicyOp(w, r, op) || + h.serveJobOp(w, r, op) { return } @@ -180,6 +202,48 @@ func (h *Handler) serveJobDefinitionOp(w http.ResponseWriter, r *http.Request, o return true } +// serveSchedulingPolicyOp handles the scheduling-policy operations, or returns +// false if op is not one of them. +func (h *Handler) serveSchedulingPolicyOp(w http.ResponseWriter, r *http.Request, op string) bool { + switch op { + case opCreateSchedulingPolicy: + h.createSchedulingPolicy(w, r) + case opDescribeSchedulingPolicies: + h.describeSchedulingPolicies(w, r) + case opListSchedulingPolicies: + h.listSchedulingPolicies(w, r) + case opUpdateSchedulingPolicy: + h.updateSchedulingPolicy(w, r) + case opDeleteSchedulingPolicy: + h.deleteSchedulingPolicy(w, r) + default: + return false + } + + return true +} + +// serveJobOp handles the job operations, or returns false if op is not one of +// them. +func (h *Handler) serveJobOp(w http.ResponseWriter, r *http.Request, op string) bool { + switch op { + case opSubmitJob: + h.submitJob(w, r) + case opDescribeJobs: + h.describeJobs(w, r) + case opListJobs: + h.listJobs(w, r) + case opCancelJob: + h.cancelJob(w, r) + case opTerminateJob: + h.terminateJob(w, r) + default: + return false + } + + return true +} + // splitPath splits a URL path into its non-empty segments. func splitPath(p string) []string { p = strings.Trim(p, "/") diff --git a/server/aws/batch/jobs.go b/server/aws/batch/jobs.go new file mode 100644 index 000000000..e7b67ccdb --- /dev/null +++ b/server/aws/batch/jobs.go @@ -0,0 +1,119 @@ +package batch + +import ( + "context" + "net/http" + + "github.com/stackshy/cloudemu/v2/services/batch/driver" +) + +func (h *Handler) submitJob(w http.ResponseWriter, r *http.Request) { + var req submitJobRequest + if !decodeJSON(w, r, &req) { + return + } + + in := driver.SubmitJobInput{ + JobName: req.JobName, + JobQueue: req.JobQueue, + JobDefinition: req.JobDefinition, + DependsOn: depsToDriver(req.DependsOn), + Parameters: req.Parameters, + ContainerOverrides: req.ContainerOverrides, + NodeOverrides: req.NodeOverrides, + EcsPropertiesOverride: req.EcsPropertiesOverride, + EksPropertiesOverride: req.EksPropertiesOverride, + RetryStrategy: req.RetryStrategy, + Timeout: req.Timeout, + PropagateTags: req.PropagateTags, + SchedulingPriorityOverride: req.SchedulingPriorityOverride, + ShareIdentifier: req.ShareIdentifier, + Tags: req.Tags, + } + + if req.ArrayProperties != nil && req.ArrayProperties.Size != nil { + in.ArraySize = req.ArrayProperties.Size + } + + job, err := h.batch.SubmitJob(r.Context(), in) + if err != nil { + writeErr(w, err) + + return + } + + writeJSON(w, submitJobResponse{JobArn: job.JobARN, JobID: job.JobID, JobName: job.JobName}) +} + +func (h *Handler) describeJobs(w http.ResponseWriter, r *http.Request) { + var req describeJobsRequest + if !decodeJSON(w, r, &req) { + return + } + + jobs, err := h.batch.DescribeJobs(r.Context(), req.Jobs) + if err != nil { + writeErr(w, err) + + return + } + + details := make([]jobDetail, 0, len(jobs)) + for i := range jobs { + details = append(details, toJobDetail(&jobs[i])) + } + + writeJSON(w, describeJobsResponse{Jobs: details}) +} + +func (h *Handler) listJobs(w http.ResponseWriter, r *http.Request) { + var req listJobsRequest + if !decodeJSON(w, r, &req) { + return + } + + page, err := h.batch.ListJobs(r.Context(), driver.ListJobsInput{ + JobQueue: req.JobQueue, + ArrayJobID: req.ArrayJobID, + MultiNodeJobID: req.MultiNodeJobID, + JobStatus: req.JobStatus, + Filters: filtersToDriver(req.Filters), + MaxResults: req.MaxResults, + NextToken: req.NextToken, + }) + if err != nil { + writeErr(w, err) + + return + } + + out := make([]jobSummary, 0, len(page.Jobs)) + for i := range page.Jobs { + out = append(out, toJobSummary(&page.Jobs[i])) + } + + writeJSON(w, listJobsResponse{JobSummaryList: out, NextToken: page.NextToken}) +} + +func (h *Handler) cancelJob(w http.ResponseWriter, r *http.Request) { + h.stopJob(w, r, h.batch.CancelJob) +} + +func (h *Handler) terminateJob(w http.ResponseWriter, r *http.Request) { + h.stopJob(w, r, h.batch.TerminateJob) +} + +func (*Handler) stopJob(w http.ResponseWriter, r *http.Request, stop func(ctx context.Context, id, reason string) error) { + var req stopJobRequest + if !decodeJSON(w, r, &req) { + return + } + + if err := stop(r.Context(), req.JobID, req.Reason); err != nil { + writeErr(w, err) + + return + } + + writeJSON(w, emptyResponse{}) +} diff --git a/server/aws/batch/jobs_paging_sdk_test.go b/server/aws/batch/jobs_paging_sdk_test.go new file mode 100644 index 000000000..d81c44794 --- /dev/null +++ b/server/aws/batch/jobs_paging_sdk_test.go @@ -0,0 +1,96 @@ +package batch_test + +import ( + "context" + "testing" + + "github.com/aws/aws-sdk-go-v2/aws" + awsbatch "github.com/aws/aws-sdk-go-v2/service/batch" + batchtypes "github.com/aws/aws-sdk-go-v2/service/batch/types" +) + +func TestSDKListJobsPaginatorAndFilters(t *testing.T) { + ctx := context.Background() + c := newBatchClient(t) + setupSDKQueue(t, c, nil) + + for _, n := range []string{"alpha", "beta", "gamma"} { + if _, err := c.SubmitJob(ctx, &awsbatch.SubmitJobInput{ + JobName: aws.String(n), JobQueue: aws.String("q"), JobDefinition: aws.String("jd"), + }); err != nil { + t.Fatalf("SubmitJob: %v", err) + } + } + + p := awsbatch.NewListJobsPaginator(c, &awsbatch.ListJobsInput{ + JobQueue: aws.String("q"), JobStatus: batchtypes.JobStatusSucceeded, MaxResults: aws.Int32(2), + }) + + var names []string + + pages := 0 + + for p.HasMorePages() { + page, err := p.NextPage(ctx) + if err != nil { + t.Fatalf("NextPage: %v", err) + } + + pages++ + + for _, s := range page.JobSummaryList { + names = append(names, aws.ToString(s.JobName)) + } + } + + if pages != 2 || len(names) != 3 || names[0] != "gamma" || names[2] != "alpha" { + t.Fatalf("paginated ListJobs = %v over %d pages, want [gamma beta alpha] over 2", names, pages) + } + + byName, err := c.ListJobs(ctx, &awsbatch.ListJobsInput{ + JobQueue: aws.String("q"), + Filters: []batchtypes.KeyValuesPair{{Name: aws.String("JOB_NAME"), Values: []string{"GAM*"}}}, + }) + if err != nil || len(byName.JobSummaryList) != 1 || aws.ToString(byName.JobSummaryList[0].JobName) != "gamma" { + t.Fatalf("JOB_NAME filter: %+v, %v", byName, err) + } + + _, err = c.ListJobs(ctx, &awsbatch.ListJobsInput{JobQueue: aws.String("q"), NextToken: aws.String("!!")}) + requireClientException(t, err, "ListJobs bad nextToken") + + _, err = c.ListJobs(ctx, &awsbatch.ListJobsInput{}) + requireClientException(t, err, "ListJobs without jobQueue/arrayJobId/multiNodeJobId") +} + +func TestSDKReservedTagPrefixRejected(t *testing.T) { + ctx := context.Background() + c := newBatchClient(t) + setupSDKQueue(t, c, nil) + + _, err := c.SubmitJob(ctx, &awsbatch.SubmitJobInput{ + JobName: aws.String("j"), JobQueue: aws.String("q"), JobDefinition: aws.String("jd"), + Tags: map[string]string{"aws:team": "x"}, + }) + requireClientException(t, err, "SubmitJob with aws: tag") + + sub, err := c.SubmitJob(ctx, &awsbatch.SubmitJobInput{ + JobName: aws.String("j"), JobQueue: aws.String("q"), JobDefinition: aws.String("jd"), + }) + if err != nil { + t.Fatalf("SubmitJob: %v", err) + } + + _, err = c.TagResource(ctx, &awsbatch.TagResourceInput{ResourceArn: sub.JobArn, Tags: map[string]string{"AWS:x": "y"}}) + requireClientException(t, err, "TagResource with aws: tag") + + if _, err := c.TagResource(ctx, &awsbatch.TagResourceInput{ + ResourceArn: sub.JobArn, Tags: map[string]string{"team": "a"}, + }); err != nil { + t.Fatalf("TagResource(job): %v", err) + } + + tags, err := c.ListTagsForResource(ctx, &awsbatch.ListTagsForResourceInput{ResourceArn: sub.JobArn}) + if err != nil || tags.Tags["team"] != "a" { + t.Fatalf("ListTagsForResource(job): %+v, %v", tags, err) + } +} diff --git a/server/aws/batch/jobs_sdk_test.go b/server/aws/batch/jobs_sdk_test.go new file mode 100644 index 000000000..10f29f568 --- /dev/null +++ b/server/aws/batch/jobs_sdk_test.go @@ -0,0 +1,291 @@ +package batch_test + +import ( + "context" + "errors" + "testing" + + "github.com/aws/aws-sdk-go-v2/aws" + awsbatch "github.com/aws/aws-sdk-go-v2/service/batch" + batchtypes "github.com/aws/aws-sdk-go-v2/service/batch/types" +) + +// setupSDKQueue creates a Fargate CE, a queue "q" (optionally fair-share) and a +// container job definition "jd". +func setupSDKQueue(t *testing.T, c *awsbatch.Client, policyArn *string) { + t.Helper() + + ctx := context.Background() + createFargateCE(t, c, "ce") + + if _, err := c.CreateJobQueue(ctx, &awsbatch.CreateJobQueueInput{ + JobQueueName: aws.String("q"), + Priority: aws.Int32(1), + SchedulingPolicyArn: policyArn, + ComputeEnvironmentOrder: []batchtypes.ComputeEnvironmentOrder{ + {Order: aws.Int32(1), ComputeEnvironment: aws.String("ce")}, + }, + }); err != nil { + t.Fatalf("CreateJobQueue: %v", err) + } + + if _, err := c.RegisterJobDefinition(ctx, &awsbatch.RegisterJobDefinitionInput{ + JobDefinitionName: aws.String("jd"), + Type: batchtypes.JobDefinitionTypeContainer, + ContainerProperties: &batchtypes.ContainerProperties{ + Image: aws.String("busybox"), + Command: []string{"echo", "hi"}, + }, + }); err != nil { + t.Fatalf("RegisterJobDefinition: %v", err) + } +} + +func requireClientException(t *testing.T, err error, what string) { + t.Helper() + + var ce *batchtypes.ClientException + if !errors.As(err, &ce) { + t.Fatalf("%s: want ClientException, got %v", what, err) + } +} + +func TestSDKJobLifecycle(t *testing.T) { + ctx := context.Background() + c := newBatchClient(t) + setupSDKQueue(t, c, nil) + + sub, err := c.SubmitJob(ctx, &awsbatch.SubmitJobInput{ + JobName: aws.String("hello"), + JobQueue: aws.String("q"), + JobDefinition: aws.String("jd"), + ContainerOverrides: &batchtypes.ContainerOverrides{Command: []string{"echo", "bye"}}, + Tags: map[string]string{"team": "a"}, + }) + if err != nil { + t.Fatalf("SubmitJob: %v", err) + } + + if aws.ToString(sub.JobId) == "" || aws.ToString(sub.JobArn) == "" || aws.ToString(sub.JobName) != "hello" { + t.Fatalf("SubmitJob response: %+v", sub) + } + + desc, err := c.DescribeJobs(ctx, &awsbatch.DescribeJobsInput{Jobs: []string{aws.ToString(sub.JobId), "unknown"}}) + if err != nil { + t.Fatalf("DescribeJobs: %v", err) + } + + if len(desc.Jobs) != 1 { + t.Fatalf("want 1 job, got %d", len(desc.Jobs)) + } + + j := desc.Jobs[0] + if j.Status != batchtypes.JobStatusSucceeded || j.Container == nil || aws.ToInt32(j.Container.ExitCode) != 0 || + j.Container.Command[1] != "bye" || aws.ToInt64(j.CreatedAt) == 0 || aws.ToInt64(j.StoppedAt) == 0 || + j.Tags["team"] != "a" { + t.Fatalf("DescribeJobs detail: %+v", j) + } + + list, err := c.ListJobs(ctx, &awsbatch.ListJobsInput{JobQueue: aws.String("q"), JobStatus: batchtypes.JobStatusSucceeded}) + if err != nil || len(list.JobSummaryList) != 1 || aws.ToString(list.JobSummaryList[0].JobId) != aws.ToString(sub.JobId) { + t.Fatalf("ListJobs: %+v, %v", list, err) + } + + // Default ListJobs lists RUNNING jobs only. + running, err := c.ListJobs(ctx, &awsbatch.ListJobsInput{JobQueue: aws.String("q")}) + if err != nil || len(running.JobSummaryList) != 0 { + t.Fatalf("default ListJobs: %+v, %v", running, err) + } + + // Cancel/Terminate of a finished job succeed with no effect; unknown jobs fail. + if _, err := c.TerminateJob(ctx, &awsbatch.TerminateJobInput{JobId: sub.JobId, Reason: aws.String("r")}); err != nil { + t.Fatalf("TerminateJob: %v", err) + } + + _, err = c.CancelJob(ctx, &awsbatch.CancelJobInput{JobId: aws.String("missing"), Reason: aws.String("r")}) + requireClientException(t, err, "CancelJob unknown job") + + _, err = c.SubmitJob(ctx, &awsbatch.SubmitJobInput{ + JobName: aws.String("x"), JobQueue: aws.String("nope"), JobDefinition: aws.String("jd"), + }) + requireClientException(t, err, "SubmitJob unknown queue") + + _, err = c.SubmitJob(ctx, &awsbatch.SubmitJobInput{ + JobName: aws.String("x"), JobQueue: aws.String("q"), JobDefinition: aws.String("jd"), + ArrayProperties: &batchtypes.ArrayProperties{Size: aws.Int32(1)}, + }) + requireClientException(t, err, "SubmitJob array size 1") +} + +func TestSDKArrayJob(t *testing.T) { + ctx := context.Background() + c := newBatchClient(t) + setupSDKQueue(t, c, nil) + + sub, err := c.SubmitJob(ctx, &awsbatch.SubmitJobInput{ + JobName: aws.String("arr"), JobQueue: aws.String("q"), JobDefinition: aws.String("jd"), + ArrayProperties: &batchtypes.ArrayProperties{Size: aws.Int32(3)}, + }) + if err != nil { + t.Fatalf("SubmitJob: %v", err) + } + + desc, err := c.DescribeJobs(ctx, &awsbatch.DescribeJobsInput{Jobs: []string{aws.ToString(sub.JobId), aws.ToString(sub.JobId) + ":2"}}) + if err != nil || len(desc.Jobs) != 2 { + t.Fatalf("DescribeJobs: %+v, %v", desc, err) + } + + parent, child := desc.Jobs[0], desc.Jobs[1] + if aws.ToInt32(parent.ArrayProperties.Size) != 3 || parent.ArrayProperties.StatusSummary["SUCCEEDED"] != 3 { + t.Fatalf("parent arrayProperties: %+v", parent.ArrayProperties) + } + + if aws.ToInt32(child.ArrayProperties.Index) != 2 { + t.Fatalf("child arrayProperties: %+v", child.ArrayProperties) + } + + kids, err := c.ListJobs(ctx, &awsbatch.ListJobsInput{ArrayJobId: sub.JobId, JobStatus: batchtypes.JobStatusSucceeded}) + if err != nil || len(kids.JobSummaryList) != 3 { + t.Fatalf("ListJobs children: %+v, %v", kids, err) + } +} + +func TestSDKSchedulingPolicies(t *testing.T) { + ctx := context.Background() + c := newBatchClient(t) + + created, err := c.CreateSchedulingPolicy(ctx, &awsbatch.CreateSchedulingPolicyInput{ + Name: aws.String("fair"), + FairsharePolicy: &batchtypes.FairsharePolicy{ + ShareDecaySeconds: aws.Int32(3600), + ComputeReservation: aws.Int32(1), + ShareDistribution: []batchtypes.ShareAttributes{ + {ShareIdentifier: aws.String("A*"), WeightFactor: aws.Float32(0.5)}, + }, + }, + Tags: map[string]string{"k": "v"}, + }) + if err != nil { + t.Fatalf("CreateSchedulingPolicy: %v", err) + } + + setupSDKQueue(t, c, created.Arn) + + desc, err := c.DescribeSchedulingPolicies(ctx, &awsbatch.DescribeSchedulingPoliciesInput{Arns: []string{aws.ToString(created.Arn)}}) + if err != nil || len(desc.SchedulingPolicies) != 1 || + aws.ToInt32(desc.SchedulingPolicies[0].FairsharePolicy.ShareDecaySeconds) != 3600 || + desc.SchedulingPolicies[0].Tags["k"] != "v" { + t.Fatalf("DescribeSchedulingPolicies: %+v, %v", desc, err) + } + + if _, err := c.UpdateSchedulingPolicy(ctx, &awsbatch.UpdateSchedulingPolicyInput{ + Arn: created.Arn, FairsharePolicy: &batchtypes.FairsharePolicy{ShareDecaySeconds: aws.Int32(60)}, + }); err != nil { + t.Fatalf("UpdateSchedulingPolicy: %v", err) + } + + list, err := c.ListSchedulingPolicies(ctx, &awsbatch.ListSchedulingPoliciesInput{}) + if err != nil || len(list.SchedulingPolicies) != 1 || aws.ToString(list.SchedulingPolicies[0].Arn) != aws.ToString(created.Arn) { + t.Fatalf("ListSchedulingPolicies: %+v, %v", list, err) + } + + // Fair-share queue: shareIdentifier is required. + _, err = c.SubmitJob(ctx, &awsbatch.SubmitJobInput{JobName: aws.String("j"), JobQueue: aws.String("q"), JobDefinition: aws.String("jd")}) + requireClientException(t, err, "SubmitJob without shareIdentifier") + + if _, err := c.SubmitJob(ctx, &awsbatch.SubmitJobInput{ + JobName: aws.String("j"), JobQueue: aws.String("q"), JobDefinition: aws.String("jd"), ShareIdentifier: aws.String("A1"), + }); err != nil { + t.Fatalf("SubmitJob with shareIdentifier: %v", err) + } + + // In-use policy can't be deleted. + _, err = c.DeleteSchedulingPolicy(ctx, &awsbatch.DeleteSchedulingPolicyInput{Arn: created.Arn}) + requireClientException(t, err, "DeleteSchedulingPolicy in use") + + // A queue can't reference a missing policy. + _, err = c.CreateJobQueue(ctx, &awsbatch.CreateJobQueueInput{ + JobQueueName: aws.String("q2"), + Priority: aws.Int32(1), + SchedulingPolicyArn: aws.String("arn:aws:batch:us-east-1:000000000000:scheduling-policy/none"), + ComputeEnvironmentOrder: []batchtypes.ComputeEnvironmentOrder{ + {Order: aws.Int32(1), ComputeEnvironment: aws.String("ce")}, + }, + }) + requireClientException(t, err, "CreateJobQueue unknown policy") + + // Tags on the policy ARN. + tags, err := c.ListTagsForResource(ctx, &awsbatch.ListTagsForResourceInput{ResourceArn: created.Arn}) + if err != nil || tags.Tags["k"] != "v" { + t.Fatalf("ListTagsForResource(policy): %+v, %v", tags, err) + } +} + +func TestSDKRegisterJobDefinitionInvalidType(t *testing.T) { + ctx := context.Background() + c := newBatchClient(t) + + _, err := c.RegisterJobDefinition(ctx, &awsbatch.RegisterJobDefinitionInput{ + JobDefinitionName: aws.String("bad"), + Type: batchtypes.JobDefinitionType("batch"), + ContainerProperties: &batchtypes.ContainerProperties{Image: aws.String("busybox")}, + }) + requireClientException(t, err, "RegisterJobDefinition invalid type") + + _, err = c.RegisterJobDefinition(ctx, &awsbatch.RegisterJobDefinitionInput{ + JobDefinitionName: aws.String("mn"), + Type: batchtypes.JobDefinitionTypeMultinode, + }) + requireClientException(t, err, "RegisterJobDefinition multinode without nodeProperties") +} + +// TestSDKSubmitJobEcsPropertiesOverride checks an ECS override round-trips +// through the wire layer into DescribeJobs instead of being dropped. +func TestSDKSubmitJobEcsPropertiesOverride(t *testing.T) { + ctx := context.Background() + c := newBatchClient(t) + setupSDKQueue(t, c, nil) + + if _, err := c.RegisterJobDefinition(ctx, &awsbatch.RegisterJobDefinitionInput{ + JobDefinitionName: aws.String("ecs"), + Type: batchtypes.JobDefinitionTypeContainer, + EcsProperties: &batchtypes.EcsProperties{TaskProperties: []batchtypes.EcsTaskProperties{{ + Containers: []batchtypes.TaskContainerProperties{{Name: aws.String("app"), Image: aws.String("app:1"), Command: []string{"run"}}}, + }}}, + }); err != nil { + t.Fatalf("RegisterJobDefinition: %v", err) + } + + sub, err := c.SubmitJob(ctx, &awsbatch.SubmitJobInput{ + JobName: aws.String("ecs"), JobQueue: aws.String("q"), JobDefinition: aws.String("ecs"), + EcsPropertiesOverride: &batchtypes.EcsPropertiesOverride{TaskProperties: []batchtypes.TaskPropertiesOverride{{ + Containers: []batchtypes.TaskContainerOverrides{{Name: aws.String("app"), Command: []string{"run", "--fast"}}}, + }}}, + }) + if err != nil { + t.Fatalf("SubmitJob: %v", err) + } + + desc, err := c.DescribeJobs(ctx, &awsbatch.DescribeJobsInput{Jobs: []string{aws.ToString(sub.JobId)}}) + if err != nil || len(desc.Jobs) != 1 { + t.Fatalf("DescribeJobs: %v %v", desc, err) + } + + ep := desc.Jobs[0].EcsProperties + if ep == nil || len(ep.TaskProperties) != 1 || len(ep.TaskProperties[0].Containers) != 1 { + t.Fatalf("ecsProperties missing: %+v", ep) + } + + if got := ep.TaskProperties[0].Containers[0].Command; len(got) != 2 || got[1] != "--fast" { + t.Fatalf("override not applied: %v", got) + } + + // An ECS override on a containerProperties definition is a ClientException. + _, err = c.SubmitJob(ctx, &awsbatch.SubmitJobInput{ + JobName: aws.String("bad"), JobQueue: aws.String("q"), JobDefinition: aws.String("jd"), + EcsPropertiesOverride: &batchtypes.EcsPropertiesOverride{TaskProperties: []batchtypes.TaskPropertiesOverride{{ + Containers: []batchtypes.TaskContainerOverrides{{Command: []string{"x"}}}, + }}}, + }) + requireClientException(t, err, "ecs override on containerProperties definition") +} diff --git a/server/aws/batch/scheduling_policy.go b/server/aws/batch/scheduling_policy.go new file mode 100644 index 000000000..13a1fd7b1 --- /dev/null +++ b/server/aws/batch/scheduling_policy.go @@ -0,0 +1,104 @@ +package batch + +import ( + "net/http" + + "github.com/stackshy/cloudemu/v2/services/batch/driver" +) + +func (h *Handler) createSchedulingPolicy(w http.ResponseWriter, r *http.Request) { + var req createSchedulingPolicyRequest + if !decodeJSON(w, r, &req) { + return + } + + p, err := h.batch.CreateSchedulingPolicy(r.Context(), req.toInput()) + if err != nil { + writeErr(w, err) + + return + } + + writeJSON(w, schedulingPolicyNameArnResponse{Name: p.Name, Arn: p.ARN}) +} + +func (h *Handler) describeSchedulingPolicies(w http.ResponseWriter, r *http.Request) { + var req describeSchedulingPoliciesRequest + if !decodeJSON(w, r, &req) { + return + } + + policies, err := h.batch.DescribeSchedulingPolicies(r.Context(), req.Arns) + if err != nil { + writeErr(w, err) + + return + } + + details := make([]schedulingPolicyDetail, 0, len(policies)) + for i := range policies { + details = append(details, toSPDetail(&policies[i])) + } + + writeJSON(w, describeSchedulingPoliciesResponse{SchedulingPolicies: details}) +} + +func (h *Handler) listSchedulingPolicies(w http.ResponseWriter, r *http.Request) { + var req listSchedulingPoliciesRequest + if !decodeJSON(w, r, &req) { + return + } + + policies, err := h.batch.ListSchedulingPolicies(r.Context()) + if err != nil { + writeErr(w, err) + + return + } + + page, next, ok := paginate(w, policies, req.MaxResults, req.NextToken) + if !ok { + return + } + + out := make([]schedulingPolicyListing, 0, len(page)) + for i := range page { + out = append(out, schedulingPolicyListing{Arn: page[i].ARN}) + } + + writeJSON(w, listSchedulingPoliciesResponse{SchedulingPolicies: out, NextToken: next}) +} + +func (h *Handler) updateSchedulingPolicy(w http.ResponseWriter, r *http.Request) { + var req updateSchedulingPolicyRequest + if !decodeJSON(w, r, &req) { + return + } + + if err := h.batch.UpdateSchedulingPolicy(r.Context(), driver.UpdateSchedulingPolicyInput{ + ARN: req.Arn, + FairsharePolicy: req.FairsharePolicy, + QuotaSharePolicy: req.QuotaSharePolicy, + }); err != nil { + writeErr(w, err) + + return + } + + writeJSON(w, emptyResponse{}) +} + +func (h *Handler) deleteSchedulingPolicy(w http.ResponseWriter, r *http.Request) { + var req deleteSchedulingPolicyRequest + if !decodeJSON(w, r, &req) { + return + } + + if err := h.batch.DeleteSchedulingPolicy(r.Context(), req.Arn); err != nil { + writeErr(w, err) + + return + } + + writeJSON(w, emptyResponse{}) +} diff --git a/server/aws/batch/types_jobs.go b/server/aws/batch/types_jobs.go new file mode 100644 index 000000000..0e5ce93be --- /dev/null +++ b/server/aws/batch/types_jobs.go @@ -0,0 +1,326 @@ +package batch + +import ( + "encoding/json" + + "github.com/stackshy/cloudemu/v2/services/batch/driver" +) + +// --- Scheduling policies --- + +type createSchedulingPolicyRequest struct { + Name string `json:"name"` + FairsharePolicy json.RawMessage `json:"fairsharePolicy"` + QuotaSharePolicy json.RawMessage `json:"quotaSharePolicy"` + Tags map[string]string `json:"tags"` +} + +func (req *createSchedulingPolicyRequest) toInput() driver.CreateSchedulingPolicyInput { + return driver.CreateSchedulingPolicyInput{ + Name: req.Name, + FairsharePolicy: req.FairsharePolicy, + QuotaSharePolicy: req.QuotaSharePolicy, + Tags: req.Tags, + } +} + +type schedulingPolicyNameArnResponse struct { + Name string `json:"name"` + Arn string `json:"arn"` +} + +type describeSchedulingPoliciesRequest struct { + Arns []string `json:"arns"` +} + +type schedulingPolicyDetail struct { + Name string `json:"name"` + Arn string `json:"arn"` + FairsharePolicy json.RawMessage `json:"fairsharePolicy,omitempty"` + QuotaSharePolicy json.RawMessage `json:"quotaSharePolicy,omitempty"` + Tags map[string]string `json:"tags,omitempty"` +} + +type describeSchedulingPoliciesResponse struct { + SchedulingPolicies []schedulingPolicyDetail `json:"schedulingPolicies"` +} + +type listSchedulingPoliciesRequest struct { + MaxResults *int32 `json:"maxResults"` + NextToken string `json:"nextToken"` +} + +type schedulingPolicyListing struct { + Arn string `json:"arn"` +} + +type listSchedulingPoliciesResponse struct { + SchedulingPolicies []schedulingPolicyListing `json:"schedulingPolicies"` + NextToken string `json:"nextToken,omitempty"` +} + +type updateSchedulingPolicyRequest struct { + Arn string `json:"arn"` + FairsharePolicy json.RawMessage `json:"fairsharePolicy"` + QuotaSharePolicy json.RawMessage `json:"quotaSharePolicy"` +} + +type deleteSchedulingPolicyRequest struct { + Arn string `json:"arn"` +} + +func toSPDetail(p *driver.SchedulingPolicy) schedulingPolicyDetail { + return schedulingPolicyDetail{ + Name: p.Name, + Arn: p.ARN, + FairsharePolicy: p.FairsharePolicy, + QuotaSharePolicy: p.QuotaSharePolicy, + Tags: p.Tags, + } +} + +// --- Jobs --- + +type jobDependencyWire struct { + JobID string `json:"jobId,omitempty"` + Type string `json:"type,omitempty"` +} + +type arrayPropertiesRequest struct { + Size *int32 `json:"size"` +} + +type submitJobRequest struct { + JobName string `json:"jobName"` + JobQueue string `json:"jobQueue"` + JobDefinition string `json:"jobDefinition"` + ArrayProperties *arrayPropertiesRequest `json:"arrayProperties"` + DependsOn []jobDependencyWire `json:"dependsOn"` + Parameters map[string]string `json:"parameters"` + ContainerOverrides json.RawMessage `json:"containerOverrides"` + NodeOverrides json.RawMessage `json:"nodeOverrides"` + EcsPropertiesOverride json.RawMessage `json:"ecsPropertiesOverride"` + EksPropertiesOverride json.RawMessage `json:"eksPropertiesOverride"` + RetryStrategy json.RawMessage `json:"retryStrategy"` + Timeout json.RawMessage `json:"timeout"` + PropagateTags *bool `json:"propagateTags"` + SchedulingPriorityOverride *int32 `json:"schedulingPriorityOverride"` + ShareIdentifier string `json:"shareIdentifier"` + Tags map[string]string `json:"tags"` +} + +type submitJobResponse struct { + JobArn string `json:"jobArn"` + JobID string `json:"jobId"` + JobName string `json:"jobName"` +} + +type describeJobsRequest struct { + Jobs []string `json:"jobs"` +} + +type arrayPropertiesWire struct { + Size int32 `json:"size,omitempty"` + Index *int32 `json:"index,omitempty"` + StatusSummary map[string]int32 `json:"statusSummary,omitempty"` +} + +type jobDetail struct { + JobArn string `json:"jobArn"` + JobName string `json:"jobName"` + JobID string `json:"jobId"` + JobQueue string `json:"jobQueue"` + Status string `json:"status"` + StatusReason string `json:"statusReason,omitempty"` + Attempts []json.RawMessage `json:"attempts"` + CreatedAt int64 `json:"createdAt,omitempty"` + StartedAt int64 `json:"startedAt,omitempty"` + StoppedAt int64 `json:"stoppedAt,omitempty"` + DependsOn []jobDependencyWire `json:"dependsOn"` + JobDefinition string `json:"jobDefinition"` + Parameters map[string]string `json:"parameters"` + Container json.RawMessage `json:"container,omitempty"` + NodeProperties json.RawMessage `json:"nodeProperties,omitempty"` + EcsProperties json.RawMessage `json:"ecsProperties,omitempty"` + EksProperties json.RawMessage `json:"eksProperties,omitempty"` + ArrayProperties *arrayPropertiesWire `json:"arrayProperties,omitempty"` + RetryStrategy json.RawMessage `json:"retryStrategy,omitempty"` + Timeout json.RawMessage `json:"timeout,omitempty"` + PlatformCapabilities []string `json:"platformCapabilities"` + PropagateTags *bool `json:"propagateTags,omitempty"` + SchedulingPriority *int32 `json:"schedulingPriority,omitempty"` + ShareIdentifier string `json:"shareIdentifier,omitempty"` + IsCancelled *bool `json:"isCancelled,omitempty"` + IsTerminated *bool `json:"isTerminated,omitempty"` + Tags map[string]string `json:"tags,omitempty"` +} + +type describeJobsResponse struct { + Jobs []jobDetail `json:"jobs"` +} + +type jobFilterWire struct { + Name string `json:"name"` + Values []string `json:"values"` +} + +type listJobsRequest struct { + JobQueue string `json:"jobQueue"` + ArrayJobID string `json:"arrayJobId"` + MultiNodeJobID string `json:"multiNodeJobId"` + JobStatus string `json:"jobStatus"` + Filters []jobFilterWire `json:"filters"` + MaxResults *int32 `json:"maxResults"` + NextToken string `json:"nextToken"` +} + +type containerSummary struct { + ExitCode *int32 `json:"exitCode,omitempty"` +} + +type jobSummary struct { + JobArn string `json:"jobArn"` + JobID string `json:"jobId"` + JobName string `json:"jobName"` + CreatedAt int64 `json:"createdAt,omitempty"` + Status string `json:"status"` + StatusReason string `json:"statusReason,omitempty"` + StartedAt int64 `json:"startedAt,omitempty"` + StoppedAt int64 `json:"stoppedAt,omitempty"` + Container *containerSummary `json:"container,omitempty"` + ArrayProperties *arrayPropertiesWire `json:"arrayProperties,omitempty"` + JobDefinition string `json:"jobDefinition,omitempty"` + ShareIdentifier string `json:"shareIdentifier,omitempty"` + IsCancelled *bool `json:"isCancelled,omitempty"` + IsTerminated *bool `json:"isTerminated,omitempty"` +} + +type listJobsResponse struct { + JobSummaryList []jobSummary `json:"jobSummaryList"` + NextToken string `json:"nextToken,omitempty"` +} + +type stopJobRequest struct { + JobID string `json:"jobId"` + Reason string `json:"reason"` +} + +func depsToDriver(in []jobDependencyWire) []driver.JobDependency { + if in == nil { + return nil + } + + out := make([]driver.JobDependency, len(in)) + for i, d := range in { + out[i] = driver.JobDependency{JobID: d.JobID, Type: d.Type} + } + + return out +} + +func depsToWire(in []driver.JobDependency) []jobDependencyWire { + out := make([]jobDependencyWire, len(in)) + for i, d := range in { + out[i] = jobDependencyWire{JobID: d.JobID, Type: d.Type} + } + + return out +} + +func filtersToDriver(in []jobFilterWire) []driver.JobFilter { + if in == nil { + return nil + } + + out := make([]driver.JobFilter, len(in)) + for i, f := range in { + out[i] = driver.JobFilter{Name: f.Name, Values: f.Values} + } + + return out +} + +func arrayToWire(ap *driver.ArrayProperties) *arrayPropertiesWire { + if ap == nil { + return nil + } + + return &arrayPropertiesWire{Size: ap.Size, Index: ap.Index, StatusSummary: ap.StatusSummary} +} + +func boolPtrIf(b bool) *bool { + if !b { + return nil + } + + return &b +} + +func toJobDetail(j *driver.Job) jobDetail { + params := j.Parameters + if params == nil { + params = map[string]string{} + } + + caps := j.PlatformCapabilities + if caps == nil { + caps = []string{} + } + + return jobDetail{ + JobArn: j.JobARN, + JobName: j.JobName, + JobID: j.JobID, + JobQueue: j.JobQueue, + Status: j.Status, + StatusReason: j.StatusReason, + Attempts: []json.RawMessage{}, + CreatedAt: j.CreatedAt, + StartedAt: j.StartedAt, + StoppedAt: j.StoppedAt, + DependsOn: depsToWire(j.DependsOn), + JobDefinition: j.JobDefinition, + Parameters: params, + Container: j.Container, + NodeProperties: j.NodeProperties, + EcsProperties: j.EcsProperties, + EksProperties: j.EksProperties, + ArrayProperties: arrayToWire(j.ArrayProperties), + RetryStrategy: j.RetryStrategy, + Timeout: j.Timeout, + PlatformCapabilities: caps, + PropagateTags: j.PropagateTags, + SchedulingPriority: j.SchedulingPriority, + ShareIdentifier: j.ShareIdentifier, + IsCancelled: boolPtrIf(j.IsCancelled), + IsTerminated: boolPtrIf(j.IsTerminated), + Tags: j.Tags, + } +} + +// toJobSummary renders the ListJobs JobSummary of a job. container.exitCode is +// reported once the job's container has exited successfully. +func toJobSummary(j *driver.Job) jobSummary { + s := jobSummary{ + JobArn: j.JobARN, + JobID: j.JobID, + JobName: j.JobName, + CreatedAt: j.CreatedAt, + Status: j.Status, + StatusReason: j.StatusReason, + StartedAt: j.StartedAt, + StoppedAt: j.StoppedAt, + ArrayProperties: arrayToWire(j.ArrayProperties), + JobDefinition: j.JobDefinition, + ShareIdentifier: j.ShareIdentifier, + IsCancelled: boolPtrIf(j.IsCancelled), + IsTerminated: boolPtrIf(j.IsTerminated), + } + + if j.Status == driver.JobStatusSucceeded && j.Container != nil && (j.ArrayProperties == nil || j.ArrayProperties.Index != nil) { + zero := int32(0) + s.Container = &containerSummary{ExitCode: &zero} + } + + return s +} diff --git a/services/batch/driver/driver.go b/services/batch/driver/driver.go index 9d4e91df9..445de672f 100644 --- a/services/batch/driver/driver.go +++ b/services/batch/driver/driver.go @@ -1,6 +1,7 @@ // Package driver defines the interface and types for AWS Batch implementations. -// It models the Batch control plane: compute environments, job queues, and -// job-definition revisions, plus resource tagging. +// It models the Batch control plane: compute environments, job queues, +// job-definition revisions, fair-share scheduling policies, and the job +// lifecycle (submit/describe/list/cancel/terminate), plus resource tagging. // // The interface is deliberately narrow: a backend stores what it is given and // echoes it back, generating ARNs, ECS cluster ARNs, and job-definition @@ -31,7 +32,21 @@ type Batch interface { DescribeJobDefinitions(ctx context.Context, in DescribeJobDefinitionsInput) ([]JobDefinition, error) DeregisterJobDefinition(ctx context.Context, nameRevisionOrARN string) error - // Tags (by ARN), shared across all three resource kinds. + // Scheduling policies. + CreateSchedulingPolicy(ctx context.Context, in CreateSchedulingPolicyInput) (*SchedulingPolicy, error) + DescribeSchedulingPolicies(ctx context.Context, arns []string) ([]SchedulingPolicy, error) + ListSchedulingPolicies(ctx context.Context) ([]SchedulingPolicy, error) + UpdateSchedulingPolicy(ctx context.Context, in UpdateSchedulingPolicyInput) error + DeleteSchedulingPolicy(ctx context.Context, arn string) error + + // Jobs. + SubmitJob(ctx context.Context, in SubmitJobInput) (*Job, error) + DescribeJobs(ctx context.Context, jobIDs []string) ([]Job, error) + ListJobs(ctx context.Context, in ListJobsInput) (*ListJobsOutput, error) + CancelJob(ctx context.Context, jobID, reason string) error + TerminateJob(ctx context.Context, jobID, reason string) error + + // Tags (by ARN), shared across every taggable resource kind. TagResource(ctx context.Context, arn string, tags map[string]string) error UntagResource(ctx context.Context, arn string, keys []string) error ListTagsForResource(ctx context.Context, arn string) (map[string]string, error) diff --git a/services/batch/driver/types.go b/services/batch/driver/types.go index ae88c9136..ea5d6000b 100644 --- a/services/batch/driver/types.go +++ b/services/batch/driver/types.go @@ -173,3 +173,155 @@ type DescribeJobDefinitionsInput struct { Name string Status string } + +// SchedulingPolicy is the server-side detail of an AWS Batch scheduling policy +// (DescribeSchedulingPolicies SchedulingPolicyDetail shape). The fair-share and +// quota-share policy blobs are carried verbatim as raw JSON so every nested +// field round-trips exactly. +type SchedulingPolicy struct { + Name string + ARN string + FairsharePolicy json.RawMessage + QuotaSharePolicy json.RawMessage + Tags map[string]string +} + +// CreateSchedulingPolicyInput describes a scheduling policy to create. +type CreateSchedulingPolicyInput struct { + Name string + FairsharePolicy json.RawMessage + QuotaSharePolicy json.RawMessage + Tags map[string]string +} + +// UpdateSchedulingPolicyInput replaces a scheduling policy's fair-share or +// quota-share details. A nil blob leaves that policy unchanged. +type UpdateSchedulingPolicyInput struct { + ARN string + FairsharePolicy json.RawMessage + QuotaSharePolicy json.RawMessage +} + +// Job statuses (the JobStatus enum). A job moves SUBMITTED -> (PENDING while +// it waits on dependencies) -> RUNNABLE -> STARTING -> RUNNING and ends in +// SUCCEEDED or FAILED. +const ( + JobStatusSubmitted = "SUBMITTED" + JobStatusPending = "PENDING" + JobStatusRunnable = "RUNNABLE" + JobStatusStarting = "STARTING" + JobStatusRunning = "RUNNING" + JobStatusSucceeded = "SUCCEEDED" + JobStatusFailed = "FAILED" +) + +// Array-job dependency types (the ArrayJobDependency enum). +const ( + DependencyNToN = "N_TO_N" + DependencySequential = "SEQUENTIAL" +) + +// ListJobs filter names (the KeyValuesPair names ListJobs accepts). +const ( + FilterJobName = "JOB_NAME" + FilterJobDefinition = "JOB_DEFINITION" + FilterBeforeCreatedAt = "BEFORE_CREATED_AT" + FilterAfterCreatedAt = "AFTER_CREATED_AT" + FilterShareIdentifier = "SHARE_IDENTIFIER" +) + +// JobDependency is one entry of a job's dependsOn list. +type JobDependency struct { + JobID string + Type string +} + +// ArrayProperties is the array-job section of a job. A parent carries Size and +// StatusSummary (child count per status); a child carries Index. +type ArrayProperties struct { + Size int32 + Index *int32 + StatusSummary map[string]int32 +} + +// SubmitJobInput describes a job to submit. JobQueue and JobDefinition accept +// a name or ARN (a job definition with or without a revision). A nil ArraySize +// submits a single (non-array) job. +type SubmitJobInput struct { + JobName string + JobQueue string + JobDefinition string + ArraySize *int32 + DependsOn []JobDependency + Parameters map[string]string + ContainerOverrides json.RawMessage + NodeOverrides json.RawMessage + EcsPropertiesOverride json.RawMessage + EksPropertiesOverride json.RawMessage + RetryStrategy json.RawMessage + Timeout json.RawMessage + PropagateTags *bool + SchedulingPriorityOverride *int32 + ShareIdentifier string + Tags map[string]string +} + +// Job is the server-side detail of an AWS Batch job as DescribeJobs reports it +// (JobDetail shape) at the instant it is observed. Times are epoch +// milliseconds; zero means unset. Container, NodeProperties, EcsProperties and +// EksProperties are the job definition's blobs (with container overrides +// applied) carried as raw JSON. +type Job struct { + JobID string + JobARN string + JobName string + JobQueue string + JobDefinition string + Status string + StatusReason string + CreatedAt int64 + StartedAt int64 + StoppedAt int64 + DependsOn []JobDependency + ArrayProperties *ArrayProperties + Parameters map[string]string + Container json.RawMessage + NodeProperties json.RawMessage + EcsProperties json.RawMessage + EksProperties json.RawMessage + RetryStrategy json.RawMessage + Timeout json.RawMessage + PlatformCapabilities []string + PropagateTags *bool + SchedulingPriority *int32 + ShareIdentifier string + IsCancelled bool + IsTerminated bool + Tags map[string]string +} + +// JobFilter is one ListJobs filter (KeyValuesPair). +type JobFilter struct { + Name string + Values []string +} + +// ListJobsInput selects the jobs ListJobs returns. Exactly one of JobQueue, +// ArrayJobID or MultiNodeJobID must be set. JobStatus defaults to RUNNING and +// is ignored when a filter (other than SHARE_IDENTIFIER) is given. A nil +// MaxResults pages at the service default; NextToken continues a listing. +type ListJobsInput struct { + JobQueue string + ArrayJobID string + MultiNodeJobID string + JobStatus string + Filters []JobFilter + MaxResults *int32 + NextToken string +} + +// ListJobsOutput is one page of ListJobs. NextToken is empty on the last page. +type ListJobsOutput struct { + Jobs []Job + NextToken string +} From b50fd7203d2fb30fd0bdf795019c599206397dda Mon Sep 17 00:00:00 2001 From: Satyam-Trivedi-ZS Date: Sat, 24 Oct 2026 02:01:12 +0530 Subject: [PATCH 27/31] fix(aws-batch): multi-node job nodes and Fargate platform checks (#1684) * fix(aws-batch): report multi-node parallel job nodes DescribeJobs now resolves node job IDs ("#") and ListJobs with multiNodeJobId lists the job's nodes instead of an empty list. Each node carries nodeDetails (nodeIndex, isMainNode) and the container, ECS or EKS properties of the node range that covers it; ListJobs summaries carry nodeProperties (isMainNode, numNodes, nodeIndex). Nodes are derived from the job record, the same way array children are, so there is no new stored state. Every node follows the job's lifecycle and, like array children, sends no state-change events of its own. * fix(aws-batch): reject multi-node work on Fargate job queues SubmitJob now returns a ClientException when nodeOverrides is sent to a job queue whose compute environments are FARGATE or FARGATE_SPOT, or when a multi-node job definition is submitted to one. The SubmitJob reference says nodeOverrides "isn't applicable to jobs that are running on Fargate resources; use containerOverrides instead", and the RegisterJobDefinition reference says multinode isn't supported for jobs run on Fargate. RegisterJobDefinition also rejects nodeProperties together with the FARGATE platform capability, per the same reference. The override tests that submit multi-node jobs now use an EC2 queue, so they keep testing the override rules rather than the platform rule. --- docs/coverage/aws/batch.md | 7 +- docs/coverage/nongoals/batch.md | 7 +- providers/aws/batch/job_definition.go | 4 + providers/aws/batch/job_lifecycle.go | 5 + providers/aws/batch/job_nodes.go | 246 ++++++++++++++++ providers/aws/batch/job_nodes_test.go | 326 +++++++++++++++++++++ providers/aws/batch/jobs.go | 30 +- providers/aws/batch/jobs_overrides_test.go | 8 +- server/aws/batch/jobs_sdk_test.go | 120 ++++++++ server/aws/batch/types_jobs.go | 63 +++- services/batch/driver/types.go | 14 +- 11 files changed, 796 insertions(+), 34 deletions(-) create mode 100644 providers/aws/batch/job_nodes.go create mode 100644 providers/aws/batch/job_nodes_test.go diff --git a/docs/coverage/aws/batch.md b/docs/coverage/aws/batch.md index 2dbae14ca..f2e828997 100644 --- a/docs/coverage/aws/batch.md +++ b/docs/coverage/aws/batch.md @@ -36,8 +36,9 @@ AWS's `batch` service ยท portable interface `driver.Batch` ยท [AWS index](./READ - Jobs run no workload: a job always succeeds (container `exitCode` 0) once its dependencies succeed. By default it is `SUCCEEDED` as soon as it is submitted; with `--async-settle` it spends about one second each in `SUBMITTED`, `RUNNABLE` and `STARTING`, then five seconds in `RUNNING` (and stays `PENDING` while a dependency is unfinished). `attempts` is always empty, and retry strategies and timeouts are stored and echoed but never applied. - `CancelJob` on a `PENDING` job takes effect at once. Real Batch waits for the job's dependencies to finish first. -- Array jobs: children are derived from the parent, not stored one by one. `SEQUENTIAL` and `N_TO_N` dependencies are honoured. An array parent reports `PENDING` while any child is unfinished. Multi-node parallel jobs run as a single job: `ListJobs` with `multiNodeJobId` returns an empty list. +- Array jobs: children are derived from the parent, not stored one by one. `SEQUENTIAL` and `N_TO_N` dependencies are honoured. An array parent reports `PENDING` while any child is unfinished. +- Multi-node parallel jobs: nodes are derived from the job, not stored one by one. `DescribeJobs` on `#` and `ListJobs` with `multiNodeJobId` return each node with `nodeDetails` (`nodeIndex`, `isMainNode`) and the container, ECS or EKS properties of its node range. Every node, main or child, has the job's own status, reason and times; real Batch starts the child nodes only once the main node is `RUNNING`, and a child node can finish or fail on its own. `CancelJob` and `TerminateJob` take the job ID, not a node ID. - Fair-share scheduling policies are stored, validated and bound to queues, but they never change the order in which jobs run. Jobs never stay `RUNNABLE` because no capacity is free, so "Batch Job Queue Blocked" events are never sent. -- "Batch Job State Change" events go to the EventBridge default bus on the serve scheduler's tick (and on any Batch job API call), one per status the job passed through; none is sent for the initial submission. In library mode, nothing ticks, so events are published when a job API call sees the change or when `Tick` is called. A job that fails reports only `FAILED`, without the earlier steps. Array children send no events of their own. Real Batch sends no state-change events for compute environments or job queues, so the emulator sends none either. +- "Batch Job State Change" events go to the EventBridge default bus on the serve scheduler's tick (and on any Batch job API call), one per status the job passed through; none is sent for the initial submission. In library mode, nothing ticks, so events are published when a job API call sees the change or when `Tick` is called. A job that fails reports only `FAILED`, without the earlier steps. Array children and multi-node nodes send no events of their own. Real Batch sends no state-change events for compute environments or job queues, so the emulator sends none either. - Error messages are written by the emulator. The exception type (`ClientException`, HTTP 400) is the documented one. -- `containerOverrides`, `ecsPropertiesOverride`, `eksPropertiesOverride` and `nodeOverrides` are merged into the job detail `DescribeJobs` returns (environment variables by name, resource requirements by type, other fields replaced). Overrides are not checked against compute-environment platform rules (for example `nodeOverrides` on Fargate), and multi-node jobs report no per-node `nodeDetails`. +- `containerOverrides`, `ecsPropertiesOverride`, `eksPropertiesOverride` and `nodeOverrides` are merged into the job detail `DescribeJobs` returns (environment variables by name, resource requirements by type, other fields replaced). On a job queue whose compute environments are `FARGATE` or `FARGATE_SPOT`, `SubmitJob` rejects `nodeOverrides` and multi-node job definitions with a `ClientException`, and `RegisterJobDefinition` rejects `nodeProperties` with the `FARGATE` platform capability. Other platform rules are not checked: multi-node jobs on Spot or `UNMANAGED` compute environments, the `instanceType`, `memory` and `vcpus` container overrides on Fargate, and ECS versus EKS properties against the compute environment, since EKS compute environments are not modeled. diff --git a/docs/coverage/nongoals/batch.md b/docs/coverage/nongoals/batch.md index 1cee8c5cd..48c21cf06 100644 --- a/docs/coverage/nongoals/batch.md +++ b/docs/coverage/nongoals/batch.md @@ -1,7 +1,8 @@ - Jobs run no workload: a job always succeeds (container `exitCode` 0) once its dependencies succeed. By default it is `SUCCEEDED` as soon as it is submitted; with `--async-settle` it spends about one second each in `SUBMITTED`, `RUNNABLE` and `STARTING`, then five seconds in `RUNNING` (and stays `PENDING` while a dependency is unfinished). `attempts` is always empty, and retry strategies and timeouts are stored and echoed but never applied. - `CancelJob` on a `PENDING` job takes effect at once. Real Batch waits for the job's dependencies to finish first. -- Array jobs: children are derived from the parent, not stored one by one. `SEQUENTIAL` and `N_TO_N` dependencies are honoured. An array parent reports `PENDING` while any child is unfinished. Multi-node parallel jobs run as a single job: `ListJobs` with `multiNodeJobId` returns an empty list. +- Array jobs: children are derived from the parent, not stored one by one. `SEQUENTIAL` and `N_TO_N` dependencies are honoured. An array parent reports `PENDING` while any child is unfinished. +- Multi-node parallel jobs: nodes are derived from the job, not stored one by one. `DescribeJobs` on `#` and `ListJobs` with `multiNodeJobId` return each node with `nodeDetails` (`nodeIndex`, `isMainNode`) and the container, ECS or EKS properties of its node range. Every node, main or child, has the job's own status, reason and times; real Batch starts the child nodes only once the main node is `RUNNING`, and a child node can finish or fail on its own. `CancelJob` and `TerminateJob` take the job ID, not a node ID. - Fair-share scheduling policies are stored, validated and bound to queues, but they never change the order in which jobs run. Jobs never stay `RUNNABLE` because no capacity is free, so "Batch Job Queue Blocked" events are never sent. -- "Batch Job State Change" events go to the EventBridge default bus on the serve scheduler's tick (and on any Batch job API call), one per status the job passed through; none is sent for the initial submission. In library mode, nothing ticks, so events are published when a job API call sees the change or when `Tick` is called. A job that fails reports only `FAILED`, without the earlier steps. Array children send no events of their own. Real Batch sends no state-change events for compute environments or job queues, so the emulator sends none either. +- "Batch Job State Change" events go to the EventBridge default bus on the serve scheduler's tick (and on any Batch job API call), one per status the job passed through; none is sent for the initial submission. In library mode, nothing ticks, so events are published when a job API call sees the change or when `Tick` is called. A job that fails reports only `FAILED`, without the earlier steps. Array children and multi-node nodes send no events of their own. Real Batch sends no state-change events for compute environments or job queues, so the emulator sends none either. - Error messages are written by the emulator. The exception type (`ClientException`, HTTP 400) is the documented one. -- `containerOverrides`, `ecsPropertiesOverride`, `eksPropertiesOverride` and `nodeOverrides` are merged into the job detail `DescribeJobs` returns (environment variables by name, resource requirements by type, other fields replaced). Overrides are not checked against compute-environment platform rules (for example `nodeOverrides` on Fargate), and multi-node jobs report no per-node `nodeDetails`. +- `containerOverrides`, `ecsPropertiesOverride`, `eksPropertiesOverride` and `nodeOverrides` are merged into the job detail `DescribeJobs` returns (environment variables by name, resource requirements by type, other fields replaced). On a job queue whose compute environments are `FARGATE` or `FARGATE_SPOT`, `SubmitJob` rejects `nodeOverrides` and multi-node job definitions with a `ClientException`, and `RegisterJobDefinition` rejects `nodeProperties` with the `FARGATE` platform capability. Other platform rules are not checked: multi-node jobs on Spot or `UNMANAGED` compute environments, the `instanceType`, `memory` and `vcpus` container overrides on Fargate, and ECS versus EKS properties against the compute environment, since EKS compute environments are not modeled. diff --git a/providers/aws/batch/job_definition.go b/providers/aws/batch/job_definition.go index 1c8bc54d7..ba98dd705 100644 --- a/providers/aws/batch/job_definition.go +++ b/providers/aws/batch/job_definition.go @@ -49,6 +49,10 @@ func (m *Mock) RegisterJobDefinition( return nil, err } + if err := validateJobDefinitionPlatform(&in); err != nil { + return nil, err + } + m.registerMu.Lock() cur, _ := m.jobDefMaxRev.Get(in.Name) diff --git a/providers/aws/batch/job_lifecycle.go b/providers/aws/batch/job_lifecycle.go index f2c319853..533caa319 100644 --- a/providers/aws/batch/job_lifecycle.go +++ b/providers/aws/batch/job_lifecycle.go @@ -105,6 +105,11 @@ func cloneJob(j driver.Job) driver.Job { j.ArrayProperties = &ap } + if j.NodeDetails != nil { + nd := *j.NodeDetails + j.NodeDetails = &nd + } + j.Parameters = copyTags(j.Parameters) j.Container = copyRaw(j.Container) j.NodeProperties = copyRaw(j.NodeProperties) diff --git a/providers/aws/batch/job_nodes.go b/providers/aws/batch/job_nodes.go new file mode 100644 index 000000000..c48dfc25f --- /dev/null +++ b/providers/aws/batch/job_nodes.go @@ -0,0 +1,246 @@ +package batch + +import ( + "encoding/json" + "math" + "strconv" + "strings" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/services/batch/driver" +) + +// nodeSeparator joins a multi-node parallel job's ID and a node index: node i +// of job J is "J#i" (Multi-node parallel jobs, Batch User Guide). +const nodeSeparator = "#" + +// Compute resource types that run on Fargate (ComputeResource.type), and the +// job definition platform capability for Fargate. +const ( + crTypeFargate = "FARGATE" + crTypeFargateSpot = "FARGATE_SPOT" + platformFargate = "FARGATE" +) + +// nodeLayout is the node count, main node and node ranges of a multi-node +// parallel job, read from its nodeProperties (overrides already applied). +type nodeLayout struct { + numNodes int32 + mainNode int32 + ranges []jsonObject +} + +// nodeLayoutWire decodes the part of nodeProperties a node layout needs. The +// int32 fields make an out-of-range numNodes or mainNode a decode error. +type nodeLayoutWire struct { + NumNodes int32 `json:"numNodes"` + MainNode int32 `json:"mainNode"` + NodeRangeProperties []jsonObject `json:"nodeRangeProperties"` +} + +// parseNodeLayout reads a job's node layout. ok is false for a job that isn't +// a multi-node parallel job. +func parseNodeLayout(props json.RawMessage) (nodeLayout, bool) { + if !isSet(props) { + return nodeLayout{}, false + } + + var w nodeLayoutWire + if json.Unmarshal(props, &w) != nil || w.NumNodes < 1 || w.MainNode < 0 || w.MainNode >= w.NumNodes { + return nodeLayout{}, false + } + + return nodeLayout{numNodes: w.NumNodes, mainNode: w.MainNode, ranges: w.NodeRangeProperties}, true +} + +// rangeFor returns the node range whose targetNodes covers node i, or nil. +func (l *nodeLayout) rangeFor(i int32) jsonObject { + for _, r := range l.ranges { + var target string + _ = json.Unmarshal(r["targetNodes"], &target) + + nr, ok := parseTargetNodes(target, int(l.numNodes)) + if ok && int(i) >= nr.start && int(i) <= nr.end { + return r + } + } + + return nil +} + +// splitNodeID splits "parent#index" into its parts. +func splitNodeID(id string) (parent string, idx int32, ok bool) { + i := strings.LastIndex(id, nodeSeparator) + if i <= 0 { + return "", 0, false + } + + n, err := strconv.ParseInt(id[i+1:], 10, 32) + if err != nil || n < 0 { + return "", 0, false + } + + return id[:i], int32(n), true +} + +// lookupNode resolves a node job ID "#" to its multi-node +// job's record, the node index and the job's node layout. +func (m *Mock) lookupNode(id string) (rec jobRecord, idx int32, layout nodeLayout, ok bool) { + parent, i, isNode := splitNodeID(id) + if !isNode { + return jobRecord{}, 0, nodeLayout{}, false + } + + r, found := m.jobs.Get(parent) + if !found { + return jobRecord{}, 0, nodeLayout{}, false + } + + l, isMNP := parseNodeLayout(r.Job.NodeProperties) + if !isMNP || i >= l.numNodes { + return jobRecord{}, 0, nodeLayout{}, false + } + + return r, i, l, true +} + +// renderNode builds node i of a multi-node parallel job. Nodes are not stored: +// each node follows the job's own lifecycle (status, reason, times and +// cancel/terminate flags), carries nodeDetails, and reports the container, +// ECS or EKS properties of the node range that covers it instead of the job's +// nodeProperties. +func (o *observer) renderNode(rec *jobRecord, l *nodeLayout, i int32) driver.Job { + j := o.render(rec, -1) + + suffix := nodeSeparator + strconv.Itoa(int(i)) + j.JobID += suffix + j.JobARN += suffix + j.NodeProperties = nil + j.NodeDetails = &driver.NodeDetails{NodeIndex: i, IsMainNode: i == l.mainNode, NumNodes: l.numNodes} + + r := l.rangeFor(i) + j.Container = copyRaw(r["container"]) + j.EcsProperties = copyRaw(r["ecsProperties"]) + j.EksProperties = copyRaw(r["eksProperties"]) + + if j.Status == driver.JobStatusSucceeded { + j.Container = withExitCode(j.Container) + } + + return j +} + +// nodes lists the nodes of a multi-node parallel job by index, resuming after +// the cursor index. Every node shares the job's name, definition, creation +// time, share identifier and status, so a status or filter that rejects one +// node rejects them all: the first node decides and the loop stays bounded by +// the page size. +func (l *jobLister) nodes(m *Mock) *driver.ListJobsOutput { + out := &driver.ListJobsOutput{Jobs: []driver.Job{}} + + rec, ok := m.jobs.Get(l.in.MultiNodeJobID) + if !ok { + return out + } + + layout, isMNP := parseNodeLayout(rec.Job.NodeProperties) + + next := l.after + 1 + if !isMNP || next < 0 || next > math.MaxInt32 || next >= int64(layout.numNodes) { + return out + } + + start := int32(next) + + first := l.o.renderNode(&rec, &layout, start) + if !l.keeps(&first) { + return out + } + + for i := start; i < layout.numNodes; i++ { + if len(out.Jobs) == l.limit { + out.NextToken = encodeListCursor(int64(i - 1)) + + break + } + + out.Jobs = append(out.Jobs, l.o.renderNode(&rec, &layout, i)) + } + + return out +} + +// keeps reports whether a listed job passes the status and filters. +func (l *jobLister) keeps(j *driver.Job) bool { + return (!l.useStatus || j.Status == l.status) && matchesFilters(j, l.in.Filters) +} + +// fargateQueue reports whether a job queue runs its jobs on Fargate: its +// compute environments have FARGATE or FARGATE_SPOT compute resources. A queue +// can't mix Fargate and EC2 compute environments (Job queues on Fargate, Batch +// User Guide), so one Fargate compute environment decides. +func (m *Mock) fargateQueue(q *driver.JobQueue) bool { + for _, o := range q.ComputeEnvironmentOrder { + ce, ok := m.computeEnvs.Get(nameFromARN(kindComputeEnvironment, o.ComputeEnvironment)) + if !ok || !isSet(ce.ComputeResources) { + continue + } + + var cr struct { + Type string `json:"type"` + } + + if json.Unmarshal(ce.ComputeResources, &cr) == nil && (cr.Type == crTypeFargate || cr.Type == crTypeFargateSpot) { + return true + } + } + + return false +} + +// validateJobPlatform rejects a submission the queue's platform can't run. On +// a Fargate queue, nodeOverrides isn't applicable (SubmitJob API reference: +// "use containerOverrides instead") and multi-node parallel jobs aren't +// supported (RegisterJobDefinition API reference, type and nodeProperties). +func (m *Mock) validateJobPlatform(q *driver.JobQueue, jd *driver.JobDefinition, in *driver.SubmitJobInput) error { + if !isSet(in.NodeOverrides) && !isMultiNode(jd) { + return nil + } + + if !m.fargateQueue(q) { + return nil + } + + if isSet(in.NodeOverrides) { + return cerrors.Newf(cerrors.InvalidArgument, + "nodeOverrides isn't applicable to jobs that run on Fargate resources (job queue %q uses Fargate "+ + "compute environments); use containerOverrides instead", q.Name) + } + + return cerrors.Newf(cerrors.InvalidArgument, + "multi-node parallel job definition %q can't run on Fargate resources (job queue %q uses Fargate "+ + "compute environments)", jd.ARN, q.Name) +} + +func isMultiNode(jd *driver.JobDefinition) bool { + return jd.Type == driver.JDTypeMultiNode || isSet(jd.NodeProperties) +} + +// validateJobDefinitionPlatform enforces the RegisterJobDefinition rule that a +// job run on Fargate resources can't be multi-node: nodeProperties (and so +// type multinode) can't be combined with the FARGATE platform capability. +func validateJobDefinitionPlatform(in *driver.RegisterJobDefinitionInput) error { + if !isSet(in.NodeProperties) && in.Type != driver.JDTypeMultiNode { + return nil + } + + for _, c := range in.PlatformCapabilities { + if c == platformFargate { + return cerrors.New(cerrors.InvalidArgument, + "multinode job definitions and nodeProperties aren't supported for jobs that run on Fargate "+ + "resources (platformCapabilities FARGATE)") + } + } + + return nil +} diff --git a/providers/aws/batch/job_nodes_test.go b/providers/aws/batch/job_nodes_test.go new file mode 100644 index 000000000..dc1ee3882 --- /dev/null +++ b/providers/aws/batch/job_nodes_test.go @@ -0,0 +1,326 @@ +package batch_test + +import ( + "context" + "encoding/json" + "strings" + "testing" + "time" + + "github.com/stackshy/cloudemu/v2/config" + "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/providers/aws/batch" + "github.com/stackshy/cloudemu/v2/services/batch/driver" +) + +// ec2Queue is the EC2-backed job queue setupEC2Queue creates. +const ec2Queue = "q-ec2" + +// setupEC2Queue creates an EC2 compute environment and the job queue ec2Queue +// on it (multi-node parallel jobs need EC2 resources). +func setupEC2Queue(t *testing.T, m *batch.Mock) { + t.Helper() + + ctx := context.Background() + + if _, err := m.CreateComputeEnvironment(ctx, driver.CreateComputeEnvironmentInput{ + Name: "ce-ec2", Type: driver.CETypeManaged, + ComputeResources: json.RawMessage(`{"type":"EC2","minvCpus":0,"maxvCpus":16,` + + `"instanceTypes":["optimal"],"subnets":["subnet-1"],"securityGroupIds":["sg-1"]}`), + }); err != nil { + t.Fatalf("create EC2 compute environment: %v", err) + } + + if _, err := m.CreateJobQueue(ctx, driver.CreateJobQueueInput{ + Name: ec2Queue, Priority: 1, + ComputeEnvironmentOrder: []driver.ComputeEnvironmentOrder{{Order: 1, ComputeEnvironment: "ce-ec2"}}, + }); err != nil { + t.Fatalf("create EC2 queue: %v", err) + } +} + +// registerMNP registers "mnp": three nodes, main node 1, nodes 0-1 on image +// "lead" and node 2 on image "worker". +func registerMNP(t *testing.T, m *batch.Mock) { + t.Helper() + + registerJD(t, m, driver.RegisterJobDefinitionInput{ + Name: "mnp", Type: driver.JDTypeMultiNode, + NodeProperties: json.RawMessage(`{"numNodes":3,"mainNode":1,"nodeRangeProperties":[ + {"targetNodes":"0:1","container":{"image":"lead"}}, + {"targetNodes":"2:","container":{"image":"worker"}}]}`), + }) +} + +func describeAll(t *testing.T, m *batch.Mock, ids ...string) []driver.Job { + t.Helper() + + jobs, err := m.DescribeJobs(context.Background(), ids) + if err != nil { + t.Fatalf("DescribeJobs: %v", err) + } + + return jobs +} + +func containerImage(t *testing.T, j *driver.Job) string { + t.Helper() + + img, _ := decodeMap(t, j.Container)["image"].(string) + + return img +} + +func TestMultiNodeJobDescribesEachNode(t *testing.T) { + m, fc := newAsyncMock(t) + setupEC2Queue(t, m) + registerMNP(t, m) + + j := submit(t, m, driver.SubmitJobInput{JobQueue: ec2Queue, JobDefinition: "mnp"}) + + parent := describe(t, m, j.JobID) + if parent.NodeDetails != nil || !strings.Contains(string(parent.NodeProperties), `"numNodes":3`) { + t.Fatalf("parent should carry nodeProperties and no nodeDetails: %+v", parent) + } + + nodes := describeAll(t, m, j.JobID+"#0", j.JobID+"#1", j.JobID+"#2", j.JobID+"#3", j.JobID+"#-1") + if len(nodes) != 3 { + t.Fatalf("want nodes #0-#2 (and #3, #-1 omitted), got %d", len(nodes)) + } + + wantImage := []string{"lead", "lead", "worker"} + + for i := range nodes { + n := &nodes[i] + if n.NodeDetails == nil || int(n.NodeDetails.NodeIndex) != i || n.NodeDetails.IsMainNode != (i == 1) { + t.Fatalf("node %d nodeDetails = %+v", i, n.NodeDetails) + } + + if n.JobARN != j.JobARN+"#"+string(rune('0'+i)) || n.NodeProperties != nil || n.Status != driver.JobStatusSubmitted { + t.Fatalf("node %d = %+v", i, n) + } + + if got := containerImage(t, n); got != wantImage[i] { + t.Fatalf("node %d container image = %q, want %q", i, got, wantImage[i]) + } + } + + // Nodes follow the job's lifecycle. + fc.Advance(3 * time.Second) + + if n := describe(t, m, j.JobID+"#2"); n.Status != driver.JobStatusRunning || n.StartedAt == 0 { + t.Fatalf("node #2 while the job runs = %s (startedAt %d)", n.Status, n.StartedAt) + } + + fc.Advance(10 * time.Second) + + p := describe(t, m, j.JobID) + main := describe(t, m, j.JobID+"#1") + + if p.Status != driver.JobStatusSucceeded || main.Status != driver.JobStatusSucceeded || + main.StoppedAt != p.StoppedAt || decodeMap(t, main.Container)["exitCode"] != float64(0) { + t.Fatalf("finished job %s / main node %+v", p.Status, main) + } +} + +func TestMultiNodeNodesFollowCancel(t *testing.T) { + m, _ := newAsyncMock(t) + setupEC2Queue(t, m) + registerMNP(t, m) + + j := submit(t, m, driver.SubmitJobInput{JobQueue: ec2Queue, JobDefinition: "mnp"}) + + if err := m.CancelJob(context.Background(), j.JobID, "stop"); err != nil { + t.Fatal(err) + } + + n := describe(t, m, j.JobID+"#0") + if n.Status != driver.JobStatusFailed || !n.IsCancelled || n.StatusReason != "stop" { + t.Fatalf("node of a canceled job = %+v", n) + } +} + +func TestListJobsByMultiNodeJobID(t *testing.T) { + m, _ := newAsyncMock(t) + setupEC2Queue(t, m) + registerMNP(t, m) + registerJD(t, m, driver.RegisterJobDefinitionInput{ + Name: "single", Type: driver.JDTypeContainer, ContainerProperties: json.RawMessage(`{"image":"x"}`), + }) + + j := submit(t, m, driver.SubmitJobInput{JobName: "mnpjob", JobQueue: ec2Queue, JobDefinition: "mnp"}) + single := submit(t, m, driver.SubmitJobInput{JobQueue: ec2Queue, JobDefinition: "single"}) + + all, err := listJobs(m, driver.ListJobsInput{MultiNodeJobID: j.JobID, JobStatus: driver.JobStatusSubmitted}) + if err != nil || len(all) != 3 { + t.Fatalf("ListJobs multiNodeJobId = %d jobs, %v", len(all), err) + } + + for i := range all { + if all[i].JobID != j.JobID+"#"+string(rune('0'+i)) || all[i].NodeDetails == nil || + all[i].NodeDetails.NumNodes != 3 || all[i].NodeDetails.IsMainNode != (i == 1) { + t.Fatalf("node %d = %+v", i, all[i]) + } + } + + // Pages of two. + max2 := int32(2) + + page, err := m.ListJobs(context.Background(), driver.ListJobsInput{ + MultiNodeJobID: j.JobID, JobStatus: driver.JobStatusSubmitted, MaxResults: &max2, + }) + if err != nil || len(page.Jobs) != 2 || page.NextToken == "" { + t.Fatalf("first page = %+v, %v", page, err) + } + + rest, err := m.ListJobs(context.Background(), driver.ListJobsInput{ + MultiNodeJobID: j.JobID, JobStatus: driver.JobStatusSubmitted, MaxResults: &max2, NextToken: page.NextToken, + }) + if err != nil || len(rest.Jobs) != 1 || rest.Jobs[0].JobID != j.JobID+"#2" || rest.NextToken != "" { + t.Fatalf("second page = %+v, %v", rest, err) + } + + empty := map[string]driver.ListJobsInput{ + "default RUNNING status": {MultiNodeJobID: j.JobID}, + "non-matching job name": {MultiNodeJobID: j.JobID, Filters: []driver.JobFilter{{Name: driver.FilterJobName, Values: []string{"nomatch"}}}}, + "unknown job": {MultiNodeJobID: "nope", JobStatus: driver.JobStatusSubmitted}, + "job that isn't multinode": {MultiNodeJobID: single.JobID, JobStatus: driver.JobStatusSubmitted}, + } + + for name, in := range empty { + got, err := listJobs(m, in) + if err != nil || len(got) != 0 { + t.Errorf("%s: got %d jobs, %v", name, len(got), err) + } + } + + if got, _ := listJobs(m, driver.ListJobsInput{ + MultiNodeJobID: j.JobID, Filters: []driver.JobFilter{{Name: driver.FilterJobName, Values: []string{"mnp*"}}}, + }); len(got) != 3 { + t.Fatalf("matching filter returned %d nodes", len(got)) + } +} + +func TestMultiNodeNodesSendNoEvents(t *testing.T) { + m := newMock(t) + pub := &recordingPublisher{} + m.SetEventPublisher(pub) + setupEC2Queue(t, m) + registerMNP(t, m) + + j := submit(t, m, driver.SubmitJobInput{JobQueue: ec2Queue, JobDefinition: "mnp"}) + describeAll(t, m, j.JobID+"#0", j.JobID+"#1") + + pub.mu.Lock() + defer pub.mu.Unlock() + + if len(pub.events) != 4 { + t.Fatalf("want the job's 4 events only, got %d", len(pub.events)) + } + + for _, e := range pub.events { + if len(e.resources) != 1 || e.resources[0] != j.JobARN { + t.Fatalf("event for something other than the job: %v", e.resources) + } + } +} + +func TestMultiNodeNodesSurviveSnapshot(t *testing.T) { + src, fc := newAsyncMock(t) + setupEC2Queue(t, src) + registerMNP(t, src) + ctx := context.Background() + + j := submit(t, src, driver.SubmitJobInput{ + JobQueue: ec2Queue, JobDefinition: "mnp", + NodeOverrides: json.RawMessage(`{"nodePropertyOverrides":[{"targetNodes":"2:","containerOverrides":{"command":["go"]}}]}`), + }) + + data, err := src.Snapshot(ctx, false) + if err != nil { + t.Fatal(err) + } + + dst := batch.New(config.NewOptions( + config.WithRegion("us-east-1"), config.WithAccountID("000000000000"), + config.WithClock(fc), config.WithAsyncSettle(), + )) + if err := dst.Restore(ctx, data); err != nil { + t.Fatal(err) + } + + n := describe(t, dst, j.JobID+"#2") + if n.NodeDetails == nil || n.NodeDetails.NodeIndex != 2 || !strings.Contains(string(n.Container), `"command":["go"]`) { + t.Fatalf("restored node = %+v", n) + } +} + +func TestSubmitJobRejectsMultiNodeOnFargate(t *testing.T) { + m := newMock(t) + setupQueue(t, m) // queue "q" is on a FARGATE compute environment + setupEC2Queue(t, m) + registerMNP(t, m) + ctx := context.Background() + + if _, err := m.CreateComputeEnvironment(ctx, driver.CreateComputeEnvironmentInput{ + Name: "ce-spot", Type: driver.CETypeManaged, + ComputeResources: json.RawMessage(`{"type":"FARGATE_SPOT","maxvCpus":4,"subnets":["s"]}`), + }); err != nil { + t.Fatal(err) + } + + if _, err := m.CreateJobQueue(ctx, driver.CreateJobQueueInput{ + Name: "q-spot", Priority: 1, + ComputeEnvironmentOrder: []driver.ComputeEnvironmentOrder{{Order: 1, ComputeEnvironment: "ce-spot"}}, + }); err != nil { + t.Fatal(err) + } + + nodeOverrides := json.RawMessage(`{"nodePropertyOverrides":[{"targetNodes":"0:","containerOverrides":{"command":["x"]}}]}`) + + cases := []struct { + name, queue, jd, want string + overrides json.RawMessage + }{ + {"nodeOverrides on a Fargate queue", "q", "mnp", "nodeOverrides isn't applicable", nodeOverrides}, + {"nodeOverrides with a container definition on Fargate", "q", "jd", "nodeOverrides isn't applicable", nodeOverrides}, + {"multinode definition on a Fargate queue", "q", "mnp", "can't run on Fargate", nil}, + {"multinode definition on a FARGATE_SPOT queue (by ARN)", + "arn:aws:batch:us-east-1:000000000000:job-queue/q-spot", "mnp", "can't run on Fargate", nil}, + } + + for _, tc := range cases { + _, err := m.SubmitJob(ctx, driver.SubmitJobInput{ + JobName: "j", JobQueue: tc.queue, JobDefinition: tc.jd, NodeOverrides: tc.overrides, + }) + if !errors.IsInvalidArgument(err) || !strings.Contains(err.Error(), tc.want) { + t.Errorf("%s: want InvalidArgument %q, got %v", tc.name, tc.want, err) + } + } + + // The same submissions are accepted on an EC2 queue. + submit(t, m, driver.SubmitJobInput{JobQueue: ec2Queue, JobDefinition: "mnp", NodeOverrides: nodeOverrides}) + + // A container job on a Fargate queue is unaffected. + submit(t, m, driver.SubmitJobInput{JobQueue: "q", JobDefinition: "jd"}) +} + +func TestRegisterMultiNodeRejectsFargatePlatform(t *testing.T) { + m := newMock(t) + ctx := context.Background() + props := json.RawMessage(`{"numNodes":2,"mainNode":0,"nodeRangeProperties":[{"targetNodes":"0:","container":{"image":"x"}}]}`) + + for _, typ := range []string{driver.JDTypeMultiNode, driver.JDTypeContainer} { + _, err := m.RegisterJobDefinition(ctx, driver.RegisterJobDefinitionInput{ + Name: "fg", Type: typ, NodeProperties: props, PlatformCapabilities: []string{"FARGATE"}, + ContainerProperties: json.RawMessage(`{"image":"x"}`), + }) + if !errors.IsInvalidArgument(err) { + t.Errorf("%s nodeProperties with FARGATE: want InvalidArgument, got %v", typ, err) + } + } + + registerJD(t, m, driver.RegisterJobDefinitionInput{ + Name: "ec2", Type: driver.JDTypeMultiNode, NodeProperties: props, PlatformCapabilities: []string{"EC2"}, + }) +} diff --git a/providers/aws/batch/jobs.go b/providers/aws/batch/jobs.go index 1ad6ca11f..ca40b4b71 100644 --- a/providers/aws/batch/jobs.go +++ b/providers/aws/batch/jobs.go @@ -58,6 +58,10 @@ func (m *Mock) SubmitJob(ctx context.Context, in driver.SubmitJobInput) (*driver return nil, err } + if platformErr := m.validateJobPlatform(&q, &jd, &in); platformErr != nil { + return nil, platformErr + } + job, err := m.buildJob(&in, &q, &jd) if err != nil { return nil, err @@ -474,8 +478,9 @@ func mergeKeyed(base, over json.RawMessage, key string) json.RawMessage { return out } -// DescribeJobs returns the requested jobs (top-level or array-child IDs) in -// request order, each observed now. Unknown IDs are omitted. +// DescribeJobs returns the requested jobs (top-level, array-child ":" or +// multi-node "#" IDs) in request order, each observed now. Unknown IDs +// are omitted. func (m *Mock) DescribeJobs(ctx context.Context, jobIDs []string) ([]driver.Job, error) { if len(jobIDs) == 0 { return nil, cerrors.New(cerrors.InvalidArgument, "jobs is required") @@ -491,12 +496,15 @@ func (m *Mock) DescribeJobs(ctx context.Context, jobIDs []string) ([]driver.Job, out := make([]driver.Job, 0, len(jobIDs)) for _, id := range jobIDs { - rec, idx, ok := m.lookup(id) - if !ok { + if rec, idx, ok := m.lookup(id); ok { + out = append(out, o.render(&rec, idx)) + continue } - out = append(out, o.render(&rec, idx)) + if rec, idx, layout, ok := m.lookupNode(id); ok { + out = append(out, o.renderNode(&rec, &layout, idx)) + } } return out, nil @@ -509,14 +517,15 @@ const ( listJobsFilterPage = 100 ) -// ListJobs lists the jobs of a queue (top-level jobs and array parents) or the -// children of an array job. Queue listings are newest first, as the ListJobs -// reference describes; children are listed by index. Exactly one of jobQueue, +// ListJobs lists the jobs of a queue (top-level jobs and array parents), the +// children of an array job or the nodes of a multi-node parallel job. Queue +// listings are newest first, as the ListJobs reference describes; children and +// nodes are listed by index. Exactly one of jobQueue, // arrayJobId and multiNodeJobId must be given. Without a filter only jobs in // jobStatus (default RUNNING) are returned. // // The page token is a cursor on the listing order (submission sequence, or -// child index), not an offset, so jobs submitted or changing status between +// child or node index), not an offset, so jobs submitted or changing status between // pages never make a page skip or repeat an entry. // //nolint:gocritic // in is the driver ListJobsInput, taken by value to match the interface @@ -544,8 +553,7 @@ func (m *Mock) ListJobs(ctx context.Context, in driver.ListJobsInput) (*driver.L switch { case in.MultiNodeJobID != "": - // Multi-node parallel jobs are not modeled node by node. - return &driver.ListJobsOutput{Jobs: []driver.Job{}}, nil + return l.nodes(m), nil case in.ArrayJobID != "": return l.children(m), nil default: diff --git a/providers/aws/batch/jobs_overrides_test.go b/providers/aws/batch/jobs_overrides_test.go index af664d4ba..60e46c2da 100644 --- a/providers/aws/batch/jobs_overrides_test.go +++ b/providers/aws/batch/jobs_overrides_test.go @@ -116,6 +116,7 @@ func TestSubmitJobAppliesEksPropertiesOverride(t *testing.T) { func TestSubmitJobAppliesNodeOverrides(t *testing.T) { m := newMock(t) setupQueue(t, m) + setupEC2Queue(t, m) registerJD(t, m, driver.RegisterJobDefinitionInput{ Name: "mnp", Type: driver.JDTypeMultiNode, @@ -125,7 +126,7 @@ func TestSubmitJobAppliesNodeOverrides(t *testing.T) { }) j := submit(t, m, driver.SubmitJobInput{ - JobDefinition: "mnp", + JobDefinition: "mnp", JobQueue: ec2Queue, NodeOverrides: json.RawMessage(`{"numNodes":4,"nodePropertyOverrides":[ {"targetNodes":"1:","containerOverrides":{"command":["work","--hard"]},"instanceTypes":["c5.large"]}]}`), }) @@ -155,6 +156,7 @@ func TestSubmitJobAppliesNodeOverrides(t *testing.T) { func TestSubmitJobRejectsUnusableOverrides(t *testing.T) { m := newMock(t) setupQueue(t, m) + setupEC2Queue(t, m) ctx := context.Background() registerJD(t, m, driver.RegisterJobDefinitionInput{ @@ -184,7 +186,9 @@ func TestSubmitJobRejectsUnusableOverrides(t *testing.T) { } for name, in := range cases { - in.JobName, in.JobQueue = "j", "q" + // An EC2 queue, so the override rules (not the Fargate platform + // rule) are what rejects each case. + in.JobName, in.JobQueue = "j", ec2Queue if _, err := m.SubmitJob(ctx, in); !errors.IsInvalidArgument(err) { t.Errorf("%s: want InvalidArgument, got %v", name, err) } diff --git a/server/aws/batch/jobs_sdk_test.go b/server/aws/batch/jobs_sdk_test.go index 10f29f568..eda1b2209 100644 --- a/server/aws/batch/jobs_sdk_test.go +++ b/server/aws/batch/jobs_sdk_test.go @@ -289,3 +289,123 @@ func TestSDKSubmitJobEcsPropertiesOverride(t *testing.T) { }) requireClientException(t, err, "ecs override on containerProperties definition") } + +// setupSDKMultiNode creates an EC2 queue "q-ec2" and a multinode job definition +// "mnp" (two nodes, main node 0) next to setupSDKQueue's Fargate queue "q". +func setupSDKMultiNode(t *testing.T, c *awsbatch.Client) { + t.Helper() + + ctx := context.Background() + + if _, err := c.CreateComputeEnvironment(ctx, &awsbatch.CreateComputeEnvironmentInput{ + ComputeEnvironmentName: aws.String("ce-ec2"), + Type: batchtypes.CETypeManaged, + ComputeResources: &batchtypes.ComputeResource{ + Type: batchtypes.CRTypeEc2, MaxvCpus: aws.Int32(16), InstanceTypes: []string{"optimal"}, + Subnets: []string{"subnet-1111"}, SecurityGroupIds: []string{"sg-abcd"}, + }, + }); err != nil { + t.Fatalf("CreateComputeEnvironment EC2: %v", err) + } + + if _, err := c.CreateJobQueue(ctx, &awsbatch.CreateJobQueueInput{ + JobQueueName: aws.String("q-ec2"), Priority: aws.Int32(1), + ComputeEnvironmentOrder: []batchtypes.ComputeEnvironmentOrder{ + {Order: aws.Int32(1), ComputeEnvironment: aws.String("ce-ec2")}, + }, + }); err != nil { + t.Fatalf("CreateJobQueue EC2: %v", err) + } + + if _, err := c.RegisterJobDefinition(ctx, &awsbatch.RegisterJobDefinitionInput{ + JobDefinitionName: aws.String("mnp"), + Type: batchtypes.JobDefinitionTypeMultinode, + NodeProperties: &batchtypes.NodeProperties{ + NumNodes: aws.Int32(2), MainNode: aws.Int32(0), + NodeRangeProperties: []batchtypes.NodeRangeProperty{ + {TargetNodes: aws.String("0:"), Container: &batchtypes.ContainerProperties{Image: aws.String("mpi")}}, + }, + }, + }); err != nil { + t.Fatalf("RegisterJobDefinition multinode: %v", err) + } +} + +func TestSDKMultiNodeJobNodes(t *testing.T) { + ctx := context.Background() + c := newBatchClient(t) + setupSDKQueue(t, c, nil) + setupSDKMultiNode(t, c) + + sub, err := c.SubmitJob(ctx, &awsbatch.SubmitJobInput{ + JobName: aws.String("mpi"), JobQueue: aws.String("q-ec2"), JobDefinition: aws.String("mnp"), + NodeOverrides: &batchtypes.NodeOverrides{NumNodes: aws.Int32(3)}, + }) + if err != nil { + t.Fatalf("SubmitJob multinode: %v", err) + } + + id := aws.ToString(sub.JobId) + + desc, err := c.DescribeJobs(ctx, &awsbatch.DescribeJobsInput{Jobs: []string{id, id + "#0", id + "#2", id + "#3"}}) + if err != nil || len(desc.Jobs) != 3 { + t.Fatalf("DescribeJobs: %+v, %v", desc, err) + } + + parent, main, last := desc.Jobs[0], desc.Jobs[1], desc.Jobs[2] + if parent.NodeDetails != nil || parent.NodeProperties == nil || aws.ToInt32(parent.NodeProperties.NumNodes) != 3 { + t.Fatalf("parent: nodeDetails %+v nodeProperties %+v", parent.NodeDetails, parent.NodeProperties) + } + + if main.NodeDetails == nil || !aws.ToBool(main.NodeDetails.IsMainNode) || aws.ToInt32(main.NodeDetails.NodeIndex) != 0 || + aws.ToString(main.JobId) != id+"#0" || main.Container == nil || aws.ToString(main.Container.Image) != "mpi" || + main.Status != parent.Status { + t.Fatalf("main node: %+v", main) + } + + if last.NodeDetails == nil || aws.ToBool(last.NodeDetails.IsMainNode) || aws.ToInt32(last.NodeDetails.NodeIndex) != 2 { + t.Fatalf("node #2: %+v", last.NodeDetails) + } + + list, err := c.ListJobs(ctx, &awsbatch.ListJobsInput{MultiNodeJobId: aws.String(id), JobStatus: batchtypes.JobStatusSucceeded}) + if err != nil || len(list.JobSummaryList) != 3 { + t.Fatalf("ListJobs multiNodeJobId: %+v, %v", list, err) + } + + for i, s := range list.JobSummaryList { + np := s.NodeProperties + if np == nil || aws.ToInt32(np.NodeIndex) != int32(i) || aws.ToInt32(np.NumNodes) != 3 || aws.ToBool(np.IsMainNode) != (i == 0) { + t.Fatalf("summary %d nodeProperties: %+v", i, np) + } + } +} + +func TestSDKSubmitJobRejectsNodeOverridesOnFargate(t *testing.T) { + ctx := context.Background() + c := newBatchClient(t) + setupSDKQueue(t, c, nil) + setupSDKMultiNode(t, c) + + _, err := c.SubmitJob(ctx, &awsbatch.SubmitJobInput{ + JobName: aws.String("x"), JobQueue: aws.String("q"), JobDefinition: aws.String("mnp"), + NodeOverrides: &batchtypes.NodeOverrides{NumNodes: aws.Int32(3)}, + }) + requireClientException(t, err, "SubmitJob nodeOverrides on a Fargate queue") + + _, err = c.SubmitJob(ctx, &awsbatch.SubmitJobInput{ + JobName: aws.String("x"), JobQueue: aws.String("q"), JobDefinition: aws.String("mnp"), + }) + requireClientException(t, err, "SubmitJob multinode definition on a Fargate queue") + + _, err = c.RegisterJobDefinition(ctx, &awsbatch.RegisterJobDefinitionInput{ + JobDefinitionName: aws.String("mnp-fg"), Type: batchtypes.JobDefinitionTypeMultinode, + PlatformCapabilities: []batchtypes.PlatformCapability{batchtypes.PlatformCapabilityFargate}, + NodeProperties: &batchtypes.NodeProperties{ + NumNodes: aws.Int32(2), MainNode: aws.Int32(0), + NodeRangeProperties: []batchtypes.NodeRangeProperty{ + {TargetNodes: aws.String("0:"), Container: &batchtypes.ContainerProperties{Image: aws.String("mpi")}}, + }, + }, + }) + requireClientException(t, err, "RegisterJobDefinition multinode with FARGATE") +} diff --git a/server/aws/batch/types_jobs.go b/server/aws/batch/types_jobs.go index 0e5ce93be..c9b952315 100644 --- a/server/aws/batch/types_jobs.go +++ b/server/aws/batch/types_jobs.go @@ -125,6 +125,21 @@ type arrayPropertiesWire struct { StatusSummary map[string]int32 `json:"statusSummary,omitempty"` } +// nodeDetailsWire is JobDetail.nodeDetails, set on a node of a multi-node +// parallel job. +type nodeDetailsWire struct { + NodeIndex int32 `json:"nodeIndex"` + IsMainNode bool `json:"isMainNode"` +} + +// nodePropertiesSummaryWire is JobSummary.nodeProperties for a node of a +// multi-node parallel job. +type nodePropertiesSummaryWire struct { + IsMainNode bool `json:"isMainNode"` + NumNodes int32 `json:"numNodes"` + NodeIndex int32 `json:"nodeIndex"` +} + type jobDetail struct { JobArn string `json:"jobArn"` JobName string `json:"jobName"` @@ -140,6 +155,7 @@ type jobDetail struct { JobDefinition string `json:"jobDefinition"` Parameters map[string]string `json:"parameters"` Container json.RawMessage `json:"container,omitempty"` + NodeDetails *nodeDetailsWire `json:"nodeDetails,omitempty"` NodeProperties json.RawMessage `json:"nodeProperties,omitempty"` EcsProperties json.RawMessage `json:"ecsProperties,omitempty"` EksProperties json.RawMessage `json:"eksProperties,omitempty"` @@ -179,20 +195,21 @@ type containerSummary struct { } type jobSummary struct { - JobArn string `json:"jobArn"` - JobID string `json:"jobId"` - JobName string `json:"jobName"` - CreatedAt int64 `json:"createdAt,omitempty"` - Status string `json:"status"` - StatusReason string `json:"statusReason,omitempty"` - StartedAt int64 `json:"startedAt,omitempty"` - StoppedAt int64 `json:"stoppedAt,omitempty"` - Container *containerSummary `json:"container,omitempty"` - ArrayProperties *arrayPropertiesWire `json:"arrayProperties,omitempty"` - JobDefinition string `json:"jobDefinition,omitempty"` - ShareIdentifier string `json:"shareIdentifier,omitempty"` - IsCancelled *bool `json:"isCancelled,omitempty"` - IsTerminated *bool `json:"isTerminated,omitempty"` + JobArn string `json:"jobArn"` + JobID string `json:"jobId"` + JobName string `json:"jobName"` + CreatedAt int64 `json:"createdAt,omitempty"` + Status string `json:"status"` + StatusReason string `json:"statusReason,omitempty"` + StartedAt int64 `json:"startedAt,omitempty"` + StoppedAt int64 `json:"stoppedAt,omitempty"` + Container *containerSummary `json:"container,omitempty"` + ArrayProperties *arrayPropertiesWire `json:"arrayProperties,omitempty"` + NodeProperties *nodePropertiesSummaryWire `json:"nodeProperties,omitempty"` + JobDefinition string `json:"jobDefinition,omitempty"` + ShareIdentifier string `json:"shareIdentifier,omitempty"` + IsCancelled *bool `json:"isCancelled,omitempty"` + IsTerminated *bool `json:"isTerminated,omitempty"` } type listJobsResponse struct { @@ -248,6 +265,22 @@ func arrayToWire(ap *driver.ArrayProperties) *arrayPropertiesWire { return &arrayPropertiesWire{Size: ap.Size, Index: ap.Index, StatusSummary: ap.StatusSummary} } +func nodeDetailsToWire(nd *driver.NodeDetails) *nodeDetailsWire { + if nd == nil { + return nil + } + + return &nodeDetailsWire{NodeIndex: nd.NodeIndex, IsMainNode: nd.IsMainNode} +} + +func nodeSummaryToWire(nd *driver.NodeDetails) *nodePropertiesSummaryWire { + if nd == nil { + return nil + } + + return &nodePropertiesSummaryWire{IsMainNode: nd.IsMainNode, NumNodes: nd.NumNodes, NodeIndex: nd.NodeIndex} +} + func boolPtrIf(b bool) *bool { if !b { return nil @@ -282,6 +315,7 @@ func toJobDetail(j *driver.Job) jobDetail { JobDefinition: j.JobDefinition, Parameters: params, Container: j.Container, + NodeDetails: nodeDetailsToWire(j.NodeDetails), NodeProperties: j.NodeProperties, EcsProperties: j.EcsProperties, EksProperties: j.EksProperties, @@ -311,6 +345,7 @@ func toJobSummary(j *driver.Job) jobSummary { StartedAt: j.StartedAt, StoppedAt: j.StoppedAt, ArrayProperties: arrayToWire(j.ArrayProperties), + NodeProperties: nodeSummaryToWire(j.NodeDetails), JobDefinition: j.JobDefinition, ShareIdentifier: j.ShareIdentifier, IsCancelled: boolPtrIf(j.IsCancelled), diff --git a/services/batch/driver/types.go b/services/batch/driver/types.go index ea5d6000b..ad416080a 100644 --- a/services/batch/driver/types.go +++ b/services/batch/driver/types.go @@ -244,6 +244,16 @@ type ArrayProperties struct { StatusSummary map[string]int32 } +// NodeDetails identifies one node of a multi-node parallel job: the node job +// "#" that DescribeJobs and ListJobs (multiNodeJobId) return. +// NumNodes is the job's node count, which ListJobs reports in the node's +// JobSummary nodeProperties; DescribeJobs reports NodeIndex and IsMainNode. +type NodeDetails struct { + NodeIndex int32 + IsMainNode bool + NumNodes int32 +} + // SubmitJobInput describes a job to submit. JobQueue and JobDefinition accept // a name or ARN (a job definition with or without a revision). A nil ArraySize // submits a single (non-array) job. @@ -270,7 +280,8 @@ type SubmitJobInput struct { // (JobDetail shape) at the instant it is observed. Times are epoch // milliseconds; zero means unset. Container, NodeProperties, EcsProperties and // EksProperties are the job definition's blobs (with container overrides -// applied) carried as raw JSON. +// applied) carried as raw JSON. NodeDetails is set only on a node of a +// multi-node parallel job. type Job struct { JobID string JobARN string @@ -284,6 +295,7 @@ type Job struct { StoppedAt int64 DependsOn []JobDependency ArrayProperties *ArrayProperties + NodeDetails *NodeDetails Parameters map[string]string Container json.RawMessage NodeProperties json.RawMessage From 787a0f6f80b2a5344597c6f73ab3be65ec36ef51 Mon Sep 17 00:00:00 2001 From: Satyam-Trivedi-ZS Date: Sat, 24 Oct 2026 02:12:50 +0530 Subject: [PATCH 28/31] fix(aws-kafka): async lifecycle, scheduled AWS/Kafka metrics, real broker subnets, managed KMS key (#1654) * fix(aws-kafka): async lifecycle, scheduled AWS/Kafka metrics, real broker subnets, managed KMS key Close the open real-cloud parity gaps in #1517: - ListNodes reports each broker's clientSubnet from the cluster's own BrokerNodeGroupInfo.ClientSubnets, spreading brokers evenly across them (round-robin by broker ID), instead of a fabricated subnet-. The field is omitted when the cluster records no subnets. - Under AsyncSettle a new cluster reports CREATING (create response, Describe, List) and settles to ACTIVE; a deleted cluster reports DELETING and is gone (NotFoundException) once its window ends, keeping its name until then. Update operations on a non-ACTIVE cluster are rejected with BadRequestException. A new VPC connection reports CREATING, then AVAILABLE. The default (AsyncSettle off) stays synchronous. The windows are per-record deadlines persisted in snapshots. - Publish DEFAULT-level AWS/Kafka metrics from the shared serve scheduler (Kafka is now a config.Tickable, ticked before CloudWatch), once a minute per ACTIVE provisioned cluster: ActiveControllerCount, GlobalTopicCount, GlobalPartitionCount and OfflinePartitionsCount on "Cluster Name", and UnderReplicatedPartitions and UnderMinIsrPartitionCount on "Cluster Name" + "Broker ID". Host and traffic metrics cloudemu does not model are not published. Kafka.SetMonitoring is wired to CloudWatch. - The default encryption-at-rest key is the account+region's AWS managed aws/kafka key ARN (one stable UUID key id per account and region), completed by the provider at create time and reported by both v1 and v2 describes, instead of a hard-coded key/msk-default in us-east-1. Part of #1517 * fix(aws-kafka): address review on async lifecycle, metrics tick and encryption defaults - Snapshot deep-copies each cluster, configuration, VPC connection and replicator under its own lock (topics, operations, SCRAM secrets, tags, raw options), so encoding no longer races a concurrent CreateTopic. The per-store loops move into helpers, which also brings Snapshot back under the gocyclo limit. - withDefaultEncryption fills every EncryptionInfo field the request leaves out with its documented default (managed at-rest key, ClientBroker TLS, InCluster true) and keeps explicit values, including InCluster false. - Tick judges cluster state at its own now argument instead of the options clock, and removes clusters whose DELETING window has ended, reporting the change so persist saves it even when nothing reads the cluster. - Topic create/update/delete, SCRAM secret associate/disassociate and cluster policy put/delete are rejected with BadRequestException while a cluster is CREATING or DELETING (only reachable under --async-settle). - GetCompatibleKafkaVersions resolves the cluster through getCluster, so a cluster past its DELETING window is NotFound. Part of #1517 --- providers/aws/aws.go | 10 +- providers/aws/kafka/cluster_policy.go | 8 + providers/aws/kafka/clusters.go | 101 +++-- providers/aws/kafka/clusters_v2.go | 29 +- providers/aws/kafka/encryption.go | 101 +++++ providers/aws/kafka/kafka.go | 33 +- providers/aws/kafka/lifecycle.go | 157 ++++++++ providers/aws/kafka/metrics.go | 149 +++++++ providers/aws/kafka/nodes.go | 37 +- providers/aws/kafka/operations.go | 4 + providers/aws/kafka/parity_test.go | 525 +++++++++++++++++++++++++ providers/aws/kafka/review_test.go | 316 +++++++++++++++ providers/aws/kafka/scram_secrets.go | 8 + providers/aws/kafka/snapshot.go | 162 ++++++-- providers/aws/kafka/topics.go | 12 + providers/aws/kafka/versions.go | 4 +- providers/aws/kafka/vpc_connections.go | 19 +- providers/aws/tickables_test.go | 22 +- server/aws/kafka/parity_sdk_test.go | 291 ++++++++++++++ server/aws/kafka/types.go | 28 +- 20 files changed, 1872 insertions(+), 144 deletions(-) create mode 100644 providers/aws/kafka/encryption.go create mode 100644 providers/aws/kafka/lifecycle.go create mode 100644 providers/aws/kafka/metrics.go create mode 100644 providers/aws/kafka/parity_test.go create mode 100644 providers/aws/kafka/review_test.go create mode 100644 server/aws/kafka/parity_sdk_test.go diff --git a/providers/aws/aws.go b/providers/aws/aws.go index 5aafa2483..06427cd47 100644 --- a/providers/aws/aws.go +++ b/providers/aws/aws.go @@ -500,6 +500,9 @@ func newProvider(o *config.Options, shared *GlobalServices) *Provider { // matching real EKS (which auto-creates the cluster SG and infers the VPC). p.EKS.SetSubnetResolver(p.VPC) p.SageMaker.SetMonitoring(p.CloudWatch) + // MSK publishes DEFAULT-level AWS/Kafka metrics for each ACTIVE provisioned + // cluster once a minute, from the serve scheduler (see Tickables). + p.Kafka.SetMonitoring(p.CloudWatch) // CloudWatch alarm -> SNS: an alarm state transition fires its configured // SNS-topic actions, fanning a notification out to the topic's subscribers. p.CloudWatch.SetSNSPublisher(p.SNS) @@ -638,9 +641,10 @@ func awsDrivers(p *Provider) *resourcediscovery.Drivers { // background ticker calls each one on its interval. New time-driven services // register here. func (p *Provider) Tickables() []config.Tickable { - // SQS and EFS run before CloudWatch so the alarms evaluated in the same - // tick see the queue gauges and AWS/EFS metrics they just published. - return []config.Tickable{p.SQS, p.EFS, p.CloudWatch, p.SSM, p.CloudFormation, p.Batch} + // SQS, EFS and Kafka run before CloudWatch so the alarms evaluated in the + // same tick see the queue gauges and AWS/EFS and AWS/Kafka metrics they just + // published. + return []config.Tickable{p.SQS, p.EFS, p.Kafka, p.CloudWatch, p.SSM, p.CloudFormation, p.Batch} } // Close tears down any real engines wired into the provider via diff --git a/providers/aws/kafka/cluster_policy.go b/providers/aws/kafka/cluster_policy.go index 4abf5b3bd..94c47ecf9 100644 --- a/providers/aws/kafka/cluster_policy.go +++ b/providers/aws/kafka/cluster_policy.go @@ -37,6 +37,10 @@ func (m *Mock) PutClusterPolicy(_ context.Context, clusterARN string, body json. cd.mu.Lock() defer cd.mu.Unlock() + if err := m.requireActive(cd); err != nil { + return "", err + } + if req.CurrentVersion != "" && req.CurrentVersion != cd.policyVersion { return "", badRequest( "currentVersion %q does not match policy version %q", req.CurrentVersion, cd.policyVersion) @@ -75,6 +79,10 @@ func (m *Mock) DeleteClusterPolicy(_ context.Context, clusterARN string) error { cd.mu.Lock() defer cd.mu.Unlock() + if err := m.requireActive(cd); err != nil { + return err + } + cd.policy = "" cd.policyVersion = "" diff --git a/providers/aws/kafka/clusters.go b/providers/aws/kafka/clusters.go index 31ca7b830..e580c43fd 100644 --- a/providers/aws/kafka/clusters.go +++ b/providers/aws/kafka/clusters.go @@ -5,8 +5,8 @@ import ( "encoding/json" "sort" "strings" + "time" - "github.com/stackshy/cloudemu/v2/internal/idgen" "github.com/stackshy/cloudemu/v2/services/kafka/driver" ) @@ -150,16 +150,17 @@ func (m *Mock) getClusterBR(arn string) (*clusterData, error) { // with mkErr so each op returns the exception the SDK actually models for it. func (m *Mock) getClusterErr(arn string, mkErr func(string, ...any) error) (*clusterData, error) { cd, ok := m.clusters.Get(arn) - if !ok { + if !ok || m.reapIfGone(arn, cd) { return nil, mkErr("cluster not found: %s", arn) } return cd, nil } -// CreateCluster provisions a provisioned (v1) cluster that is immediately -// Active. The cluster name is claimed atomically under createMu so a duplicate -// name is rejected with ConflictException. +// CreateCluster provisions a provisioned (v1) cluster. It reports ACTIVE, or +// CREATING for a settle window under AsyncSettle. The cluster name is claimed +// atomically under createMu so a duplicate name is rejected with +// ConflictException. // //nolint:gocritic // hugeParam: signature fixed by driver.Kafka (by-value input). func (m *Mock) CreateCluster(_ context.Context, in driver.CreateClusterInput) (*driver.Cluster, error) { @@ -192,37 +193,17 @@ func (m *Mock) CreateCluster(_ context.Context, in driver.CreateClusterInput) (* kafkaVer = defaultKafkaVer } - m.createMu.Lock() - defer m.createMu.Unlock() - - if _, dup := m.clusterNames.Get(in.ClusterName); dup { - return nil, conflict("cluster already exists: %s", in.ClusterName) - } - - arn := m.clusterARN(in.ClusterName) - - cluster := driver.Cluster{ - ClusterARN: arn, + return m.insertCluster(driver.Cluster{ ClusterName: in.ClusterName, ClusterType: driver.ClusterTypeProvisioned, - State: driver.ClusterStateActive, - CurrentVersion: "K" + idgen.GenerateID(""), KafkaVersion: kafkaVer, NumberOfBrokerNodes: in.NumberOfBrokerNodes, BrokerNodeGroupInfo: copyBrokerNodeGroupInfo(in.BrokerNodeGroupInfo), StorageMode: in.StorageMode, EnhancedMonitoring: in.EnhancedMonitoring, Tags: copyTags(in.Tags), - CreationTime: m.now(), RawOptions: copyRaw(in.RawOptions), - } - - m.clusters.Set(arn, &clusterData{cluster: cluster}) - m.clusterNames.Set(in.ClusterName, arn) - - out := snapshotCluster(cluster) - - return &out, nil + }) } // DescribeCluster returns a deep copy of the stored cluster. @@ -233,34 +214,55 @@ func (m *Mock) DescribeCluster(_ context.Context, arn string) (*driver.Cluster, } cd.mu.RLock() - defer cd.mu.RUnlock() - - out := snapshotCluster(cd.cluster) + out := m.observedClusterLocked(cd) + cd.mu.RUnlock() return &out, nil } -// ListClusters lists clusters (optionally filtered by name prefix), sorted by -// name for deterministic paging. -func (m *Mock) ListClusters( - _ context.Context, namePrefix string, page driver.Page, -) (clusters []driver.Cluster, next string, err error) { +// visibleClusters returns observed deep copies of every cluster real MSK would +// still list, lazily removing any whose DELETING window has elapsed. +func (m *Mock) visibleClusters() []driver.Cluster { vals := m.clusters.SortedValues() all := make([]driver.Cluster, 0, len(vals)) for _, cd := range vals { cd.mu.RLock() - snap := snapshotCluster(cd.cluster) + arn := cd.cluster.ClusterARN cd.mu.RUnlock() - if namePrefix != "" && !strings.HasPrefix(snap.ClusterName, namePrefix) { + if m.reapIfGone(arn, cd) { continue } + cd.mu.RLock() + snap := m.observedClusterLocked(cd) + cd.mu.RUnlock() + all = append(all, snap) } + return all +} + +// ListClusters lists clusters (optionally filtered by name prefix), sorted by +// name for deterministic paging. +func (m *Mock) ListClusters( + _ context.Context, namePrefix string, page driver.Page, +) (clusters []driver.Cluster, next string, err error) { + visible := m.visibleClusters() + + all := make([]driver.Cluster, 0, len(visible)) + + for i := range visible { + if namePrefix != "" && !strings.HasPrefix(visible[i].ClusterName, namePrefix) { + continue + } + + all = append(all, visible[i]) + } + sort.Slice(all, func(i, j int) bool { return all[i].ClusterName < all[j].ClusterName }) start, end, nextTok, err := m.paginate(len(all), page) @@ -271,7 +273,11 @@ func (m *Mock) ListClusters( return all[start:end], nextTok, nil } -// DeleteCluster removes a cluster and frees its name for reuse. +// DeleteCluster deletes a cluster. With AsyncSettle off (the default) it is +// removed at once and its name freed. Under AsyncSettle it reports DELETING via +// Describe/List for a settle window and is then removed (lazily, on the next +// read), as real MSK does. Deleting a cluster that is already DELETING returns +// DELETING again. func (m *Mock) DeleteCluster(_ context.Context, arn, currentVersion string) (arnOut, state string, err error) { cd, err := m.getCluster(arn) if err != nil { @@ -280,6 +286,12 @@ func (m *Mock) DeleteCluster(_ context.Context, arn, currentVersion string) (arn cd.mu.Lock() + if cd.cluster.State == driver.ClusterStateDeleting { + cd.mu.Unlock() + + return arn, driver.ClusterStateDeleting, nil + } + // Honor the optimistic-concurrency version: a stale currentVersion is a 400. if currentVersion != "" && cd.cluster.CurrentVersion != currentVersion { cd.mu.Unlock() @@ -290,10 +302,19 @@ func (m *Mock) DeleteCluster(_ context.Context, arn, currentVersion string) (arn name := cd.cluster.ClusterName cd.cluster.State = driver.ClusterStateDeleting + // A delete supersedes any still-open CREATING window. + cd.readyAt = time.Time{} + cd.deleteAt = m.settleUntil(clusterDeleteSettle) + async := !cd.deleteAt.IsZero() cd.mu.Unlock() - m.clusters.Delete(arn) - m.clusterNames.Delete(name) + if async { + return arn, driver.ClusterStateDeleting, nil + } + + m.reapMu.Lock() + m.removeCluster(arn, name) + m.reapMu.Unlock() return arn, driver.ClusterStateDeleting, nil } diff --git a/providers/aws/kafka/clusters_v2.go b/providers/aws/kafka/clusters_v2.go index afaf1acf2..b94d0a3b5 100644 --- a/providers/aws/kafka/clusters_v2.go +++ b/providers/aws/kafka/clusters_v2.go @@ -95,13 +95,18 @@ func (m *Mock) createClusterV2Serverless(in driver.CreateClusterV2Input) (*drive } // insertCluster finishes minting a cluster (ARN, version, state, creation time) -// and stores it atomically under createMu, rejecting a duplicate name. +// and stores it atomically under createMu, rejecting a duplicate name. The +// stored state is ACTIVE; under AsyncSettle a CREATING window is overlaid on +// reads, and the returned cluster reports it. // //nolint:gocritic // hugeParam: takes the assembled cluster by value to finalize. func (m *Mock) insertCluster(cluster driver.Cluster) (*driver.Cluster, error) { m.createMu.Lock() defer m.createMu.Unlock() + // A name held by a cluster that has finished deleting is free again. + m.reapName(cluster.ClusterName) + if _, dup := m.clusterNames.Get(cluster.ClusterName); dup { return nil, conflict("cluster already exists: %s", cluster.ClusterName) } @@ -111,10 +116,18 @@ func (m *Mock) insertCluster(cluster driver.Cluster) (*driver.Cluster, error) { cluster.CurrentVersion = "K" + idgen.GenerateID("") cluster.CreationTime = m.now() - m.clusters.Set(cluster.ClusterARN, &clusterData{cluster: cluster}) + if cluster.ClusterType == driver.ClusterTypeProvisioned { + cluster.RawOptions = m.withDefaultEncryption(cluster.RawOptions) + } + + cd := &clusterData{cluster: cluster, readyAt: m.settleUntil(clusterCreateSettle)} + + m.clusters.Set(cluster.ClusterARN, cd) m.clusterNames.Set(cluster.ClusterName, cluster.ClusterARN) - out := snapshotCluster(cluster) + cd.mu.RLock() + out := m.observedClusterLocked(cd) + cd.mu.RUnlock() return &out, nil } @@ -134,14 +147,12 @@ func (m *Mock) ListClustersV2( return nil, "", vErr } - vals := m.clusters.SortedValues() + visible := m.visibleClusters() - all := make([]driver.Cluster, 0, len(vals)) + all := make([]driver.Cluster, 0, len(visible)) - for _, cd := range vals { - cd.mu.RLock() - snap := snapshotCluster(cd.cluster) - cd.mu.RUnlock() + for i := range visible { + snap := visible[i] if namePrefix != "" && !strings.HasPrefix(snap.ClusterName, namePrefix) { continue diff --git a/providers/aws/kafka/encryption.go b/providers/aws/kafka/encryption.go new file mode 100644 index 000000000..81bfc927e --- /dev/null +++ b/providers/aws/kafka/encryption.go @@ -0,0 +1,101 @@ +package kafka + +import ( + "crypto/sha256" + "encoding/hex" + "encoding/json" + + "github.com/stackshy/cloudemu/v2/internal/idgen" +) + +// encryptionInfoKey is the RawOptions key holding a provisioned cluster's +// EncryptionInfo block (the wire layer stores the request block under it). +const encryptionInfoKey = "encryptionInfo" + +// managedKafkaKeyARN returns the ARN of this account+region's AWS managed key +// for MSK (alias/aws/kafka). Real MSK encrypts data at rest under that key when +// a create names none, and DescribeCluster reports its key ARN in +// encryptionAtRest.dataVolumeKMSKeyId +// (https://docs.aws.amazon.com/msk/latest/developerguide/msk-working-with-encryption.html). +// There is one such key per account and region, so the key id is a UUID derived +// deterministically from both: every cluster in the account+region reports the +// same key, as in AWS. +func (m *Mock) managedKafkaKeyARN() string { + sum := sha256.Sum256([]byte("alias/aws/kafka|" + m.opts.AccountID + "|" + m.opts.Region)) + // Shape the digest as a version-4 UUID, like a real KMS key id. + sum[6] = sum[6]&0x0f | 0x40 + sum[8] = sum[8]&0x3f | 0x80 + h := hex.EncodeToString(sum[:16]) + keyID := h[0:8] + "-" + h[8:12] + "-" + h[12:16] + "-" + h[16:20] + "-" + h[20:32] + + return idgen.AWSARN("kms", m.opts.Region, m.opts.AccountID, "key/"+keyID) +} + +// withDefaultEncryption returns raw with the cluster's EncryptionInfo completed +// the way real MSK completes it. Every field the request leaves out gets its +// documented default: encryptionAtRest.dataVolumeKMSKeyId the AWS managed key, +// encryptionInTransit.clientBroker TLS and encryptionInTransit.inCluster true +// (https://docs.aws.amazon.com/msk/1.0/apireference/clusters.html#clusters-model-encryptionintransit). +// Fields the request sets, including inCluster false, are kept, as is any other +// content of the block. A block that is not a JSON object is left as sent. raw +// is not mutated. +func (m *Mock) withDefaultEncryption(raw map[string]json.RawMessage) map[string]json.RawMessage { + out := copyRaw(raw) + if out == nil { + out = map[string]json.RawMessage{} + } + + var info map[string]json.RawMessage + if blk := out[encryptionInfoKey]; len(blk) > 0 { + if err := json.Unmarshal(blk, &info); err != nil { + return out + } + } + + if info == nil { + info = map[string]json.RawMessage{} + } + + changed := defaultField(info, "encryptionAtRest", "dataVolumeKMSKeyId", m.managedKafkaKeyARN()) + changed = defaultField(info, "encryptionInTransit", "clientBroker", "TLS") || changed + changed = defaultField(info, "encryptionInTransit", "inCluster", true) || changed + + if changed { + out[encryptionInfoKey] = mustJSON(info) + } + + return out +} + +// defaultField sets info[block][field] to val when the field is absent, null or +// an empty string, creating the block if needed, and reports whether it did. +func defaultField(info map[string]json.RawMessage, block, field string, val any) bool { + var blk map[string]json.RawMessage + if r, ok := info[block]; ok { + _ = json.Unmarshal(r, &blk) + } + + if blk == nil { + blk = map[string]json.RawMessage{} + } + + if v, ok := blk[field]; ok && string(v) != "null" && string(v) != `""` { + return false + } + + blk[field] = mustJSON(val) + info[block] = mustJSON(blk) + + return true +} + +// mustJSON marshals a value built from JSON-safe types (maps, strings, bools, +// raw messages), which cannot fail. +func mustJSON(v any) json.RawMessage { + b, err := json.Marshal(v) + if err != nil { + return json.RawMessage("null") + } + + return b +} diff --git a/providers/aws/kafka/kafka.go b/providers/aws/kafka/kafka.go index b4316fbb3..85371dc0e 100644 --- a/providers/aws/kafka/kafka.go +++ b/providers/aws/kafka/kafka.go @@ -10,9 +10,11 @@ // secrets, policy, operations) live on clusterData under its lock so a mutation // is atomic with its reads. // -// Clusters are provisioned immediately Active (deterministic, no wall-clock -// wait), with a server-minted ARN. Cluster names are unique per account -// (ConflictException on a duplicate). All reads deep-copy maps, slices, and +// Clusters report ACTIVE immediately by default (deterministic, no wall-clock +// wait), with a server-minted ARN. Under config.Options.AsyncSettle a new +// cluster reports CREATING and a deleted one DELETING for a settle window (see +// lifecycle.go), and a new VPC connection reports CREATING. Cluster names are +// unique per account (ConflictException on a duplicate). All reads deep-copy maps, slices, and // json.RawMessage so a caller can never mutate stored state through a result. package kafka @@ -26,6 +28,7 @@ import ( "github.com/stackshy/cloudemu/v2/internal/idgen" "github.com/stackshy/cloudemu/v2/internal/memstore" "github.com/stackshy/cloudemu/v2/services/kafka/driver" + mondriver "github.com/stackshy/cloudemu/v2/services/monitoring/driver" ) // Compile-time check that Mock implements driver.Kafka. @@ -52,14 +55,25 @@ type clusterData struct { scramSecrets []string policy string policyVersion string - mu sync.RWMutex + + // readyAt closes the CREATING window of a new cluster and deleteAt the + // DELETING window of a deleted one (both zero unless AsyncSettle is on). + // metricsSampleAt is when Tick last published its AWS/Kafka metrics. + readyAt time.Time + deleteAt time.Time + metricsSampleAt time.Time + + mu sync.RWMutex } // vpcConnData is a stored VPC connection plus its lock. State transitions (e.g. // a client-vpc-connection reject) mutate under mu. type vpcConnData struct { vpc driver.VpcConnection - mu sync.RWMutex + // readyAt closes the CREATING window of a new connection (zero unless + // AsyncSettle is on). + readyAt time.Time + mu sync.RWMutex } // replicatorData is a stored replicator plus its lock. UpdateReplicationInfo @@ -103,6 +117,15 @@ type Mock struct { // serializes replicator creation so the name-scan and ARN insert are atomic. createMu sync.Mutex + // reapMu serializes the lazy removal of a cluster whose DELETING window has + // elapsed, so two readers never both free its name. Lock order: + // createMu -> reapMu -> clusterData.mu. + reapMu sync.Mutex + + // monitoring receives the AWS/Kafka metrics Tick publishes; nil (the + // default) disables publishing. + monitoring mondriver.Monitoring + opts *config.Options } diff --git a/providers/aws/kafka/lifecycle.go b/providers/aws/kafka/lifecycle.go new file mode 100644 index 000000000..dea6151b1 --- /dev/null +++ b/providers/aws/kafka/lifecycle.go @@ -0,0 +1,157 @@ +package kafka + +import ( + "time" + + "github.com/stackshy/cloudemu/v2/internal/settle" + "github.com/stackshy/cloudemu/v2/services/kafka/driver" +) + +// Settle windows for the MSK lifecycle under config.Options.AsyncSettle. Real +// MSK takes many minutes for each; the emulator keeps the transitions short so +// a polling client observes them without slowing tests down. With AsyncSettle +// off, opts.SettleDuration returns 0 and every transition is immediate. +const ( + clusterCreateSettle = settle.DefaultClusterSettle // CREATING -> ACTIVE + clusterDeleteSettle = settle.DefaultClusterSettle // DELETING -> removed + vpcConnCreateSettle = settle.DefaultClusterSettle // CREATING -> AVAILABLE +) + +// vpcConnStateCreating is the transient state a new VPC connection reports +// under AsyncSettle (VpcConnectionState: CREATING | AVAILABLE | INACTIVE | +// UPDATING | DEACTIVATING | DELETING | FAILED | REJECTED | REJECTING). +const vpcConnStateCreating = "CREATING" + +// settleUntil returns the instant a window of the given default length opened +// now closes, or the zero time when AsyncSettle is off (no window). +func (m *Mock) settleUntil(d time.Duration) time.Time { + dur := m.opts.SettleDuration(d) + if dur <= 0 { + return time.Time{} + } + + return m.now().Add(dur) +} + +// clusterStateLocked returns the cluster's externally visible state now. The +// caller holds cd.mu (read or write). +func (m *Mock) clusterStateLocked(cd *clusterData) string { + return clusterStateAt(cd, m.now()) +} + +// clusterStateAt returns the cluster's externally visible state at t: CREATING +// while its create window is open, otherwise the stored state. The caller holds +// cd.mu (read or write). +func clusterStateAt(cd *clusterData, t time.Time) string { + if cd.cluster.State == driver.ClusterStateActive && t.Before(cd.readyAt) { + return driver.ClusterStateCreating + } + + return cd.cluster.State +} + +// observedClusterLocked returns a deep copy of the stored cluster carrying its +// visible state. The caller holds cd.mu (read or write). +func (m *Mock) observedClusterLocked(cd *clusterData) driver.Cluster { + out := snapshotCluster(cd.cluster) + out.State = m.clusterStateLocked(cd) + + return out +} + +// requireActive rejects a write against a cluster that is not ACTIVE: cluster +// updates, and topic, SCRAM secret and cluster policy writes. Real MSK accepts +// no API operations on a cluster that is CREATING or DELETING +// (https://docs.aws.amazon.com/msk/latest/developerguide/msk-cluster-states.html). +// Without AsyncSettle a cluster is always ACTIVE, so this never fires. The +// caller holds cd.mu. +func (m *Mock) requireActive(cd *clusterData) error { + if st := m.clusterStateLocked(cd); st != driver.ClusterStateActive { + return badRequest("cluster %s is in %s state; this operation requires ACTIVE", + cd.cluster.ClusterARN, st) + } + + return nil +} + +// goneAt reports whether a stored cluster is DELETING and its delete window has +// elapsed at t, i.e. real MSK would no longer return it. A DELETING cluster with +// no window (restored from a snapshot that predates windows) counts as elapsed. +func goneAt(cd *clusterData, t time.Time) bool { + cd.mu.RLock() + defer cd.mu.RUnlock() + + return cd.cluster.State == driver.ClusterStateDeleting && !t.Before(cd.deleteAt) +} + +// reapIfGone removes a cluster whose DELETING window has elapsed now and frees +// its name, reporting whether it was removed. Request paths call it lazily on +// read; Tick calls reapIfGoneAt so a finished delete is removed (and persisted) +// even when nothing reads the cluster. +func (m *Mock) reapIfGone(arn string, cd *clusterData) bool { + return m.reapIfGoneAt(arn, cd, m.now()) +} + +// reapIfGoneAt is reapIfGone judged at t. +func (m *Mock) reapIfGoneAt(arn string, cd *clusterData, t time.Time) bool { + if !goneAt(cd, t) { + return false + } + + m.reapMu.Lock() + defer m.reapMu.Unlock() + + // Re-check under reapMu: a concurrent reader may have removed it already. + cur, ok := m.clusters.Get(arn) + if !ok || cur != cd { + return true + } + + cd.mu.RLock() + name := cd.cluster.ClusterName + cd.mu.RUnlock() + + m.removeCluster(arn, name) + + return true +} + +// removeCluster drops a cluster from the store and frees its name, but only +// when the name still points at this ARN (a later cluster may hold it). +func (m *Mock) removeCluster(arn, name string) { + m.clusters.Delete(arn) + + if owner, ok := m.clusterNames.Get(name); ok && owner == arn { + m.clusterNames.Delete(name) + } +} + +// reapName frees name when it is claimed by a cluster that has finished +// deleting, so a create can reuse it. The caller holds createMu. +func (m *Mock) reapName(name string) { + arn, ok := m.clusterNames.Get(name) + if !ok { + return + } + + cd, ok := m.clusters.Get(arn) + if !ok { + m.clusterNames.Delete(name) + + return + } + + m.reapIfGone(arn, cd) +} + +// observedVpcConnectionLocked returns a copy of the stored VPC connection +// carrying its visible state: CREATING while its create window is open. The +// caller holds vd.mu (read or write). +func (m *Mock) observedVpcConnectionLocked(vd *vpcConnData) driver.VpcConnection { + out := snapshotVpcConnection(vd.vpc) + if out.State == vpcConnStateAvailable && m.now().Before(vd.readyAt) { + out.State = vpcConnStateCreating + } + + return out +} diff --git a/providers/aws/kafka/metrics.go b/providers/aws/kafka/metrics.go new file mode 100644 index 000000000..a95622aa5 --- /dev/null +++ b/providers/aws/kafka/metrics.go @@ -0,0 +1,149 @@ +package kafka + +import ( + "context" + "sort" + "strconv" + "time" + + "github.com/stackshy/cloudemu/v2/services/kafka/driver" + mondriver "github.com/stackshy/cloudemu/v2/services/monitoring/driver" +) + +// AWS/Kafka CloudWatch metrics for provisioned clusters +// (https://docs.aws.amazon.com/msk/latest/developerguide/metrics-details.html). +// +// Real MSK pushes its metrics on a schedule, not per request: "The metrics that +// you configure for your MSK Provisioned clusters are automatically collected +// and pushed to CloudWatch at 1 minute intervals." Tick does the same on the +// shared serve scheduler, and only for clusters that are ACTIVE (every DEFAULT +// metric is documented as visible "After the cluster gets to the ACTIVE +// state"). The dimension names contain spaces ("Cluster Name", "Broker ID"). +// +// Only DEFAULT-level metrics whose values cloudemu actually knows are +// published (DEFAULT metrics are published at every monitoring level): +// +// - Cluster Name: ActiveControllerCount (1: exactly one active controller), +// GlobalTopicCount and GlobalPartitionCount (from the cluster's stored +// topics; partitions exclude replicas, as documented) and +// OfflinePartitionsCount (0: no broker is ever unavailable). +// - Cluster Name, Broker ID (brokers 1..NumberOfBrokerNodes, the IDs +// ListNodes reports): UnderReplicatedPartitions and +// UnderMinIsrPartitionCount (0: replicas never fall behind, since no +// broker runs). +// +// The CPU, memory, disk, network, connection and throughput metrics describe +// broker hosts and client traffic that cloudemu does not run, and per-broker +// PartitionCount/LeaderCount need a partition assignment it does not model, +// so none of them is published. Serverless clusters publish a different +// metric set and are skipped. +const ( + metricsNamespace = "AWS/Kafka" + + dimClusterName = "Cluster Name" + dimBrokerID = "Broker ID" + + unitCount = "Count" + + // metricsInterval is how often the AWS/Kafka metrics are published. + metricsInterval = time.Minute +) + +// SetMonitoring wires the CloudWatch backend that receives the AWS/Kafka +// metrics. With no backend wired (the default) nothing is published. +func (m *Mock) SetMonitoring(mon mondriver.Monitoring) { + m.monitoring = mon +} + +// Tick is the MSK work of the shared serve scheduler (config.Tickable). At now +// it removes every cluster whose DELETING window has ended, so a finished +// delete is dropped (and persisted) even when nothing reads the cluster, and +// publishes the AWS/Kafka metrics that are due for every ACTIVE provisioned +// cluster. It reports whether it removed or published anything. Every state +// check uses now, not the options clock. Library mode never calls it, so tests +// drive it with a FakeClock. Each cluster is sampled under its own lock and +// everything is published after all locks are released. +func (m *Mock) Tick(now time.Time) bool { + arns := m.clusters.Keys() + sort.Strings(arns) + + reaped := false + + var data []mondriver.MetricDatum + + for _, arn := range arns { + cd, ok := m.clusters.Get(arn) + if !ok { + continue + } + + if m.reapIfGoneAt(arn, cd, now) { + reaped = true + + continue + } + + if m.monitoring != nil { + data = append(data, m.dueMetrics(cd, now)...) + } + } + + if len(data) > 0 { + _ = m.monitoring.PutMetricData(context.Background(), data) + } + + return reaped || len(data) > 0 +} + +// dueMetrics samples cd's metrics if they are due at now and records the sample +// time. A cluster that is not ACTIVE, or is serverless, reports nothing. +// Callers must not hold cd.mu. +func (*Mock) dueMetrics(cd *clusterData, now time.Time) []mondriver.MetricDatum { + cd.mu.Lock() + defer cd.mu.Unlock() + + if cd.cluster.ClusterType != driver.ClusterTypeProvisioned || + clusterStateAt(cd, now) != driver.ClusterStateActive { + return nil + } + + if !cd.metricsSampleAt.IsZero() && now.Sub(cd.metricsSampleAt) < metricsInterval { + return nil + } + + cd.metricsSampleAt = now + + name := cd.cluster.ClusterName + + var partitions int64 + for _, t := range cd.topics { + partitions += int64(t.NumberOfPartitions) + } + + datum := func(metric string, value float64, dims map[string]string) mondriver.MetricDatum { + return mondriver.MetricDatum{ + Namespace: metricsNamespace, MetricName: metric, Value: value, + Unit: unitCount, Dimensions: dims, Timestamp: now, + } + } + + clusterDims := map[string]string{dimClusterName: name} + + data := []mondriver.MetricDatum{ + datum("ActiveControllerCount", 1, clusterDims), + datum("GlobalTopicCount", float64(len(cd.topics)), clusterDims), + datum("GlobalPartitionCount", float64(partitions), clusterDims), + datum("OfflinePartitionsCount", 0, clusterDims), + } + + for i := 1; i <= int(cd.cluster.NumberOfBrokerNodes); i++ { + dims := map[string]string{dimClusterName: name, dimBrokerID: strconv.Itoa(i)} + + data = append(data, + datum("UnderReplicatedPartitions", 0, dims), + datum("UnderMinIsrPartitionCount", 0, dims), + ) + } + + return data +} diff --git a/providers/aws/kafka/nodes.go b/providers/aws/kafka/nodes.go index 312089942..d6e4baae1 100644 --- a/providers/aws/kafka/nodes.go +++ b/providers/aws/kafka/nodes.go @@ -9,8 +9,9 @@ import ( ) // ListNodes returns one synthetic broker node per broker in the cluster, sized -// to the cluster's NumberOfBrokerNodes. Broker IDs are 1..N; endpoints and the -// client subnet are derived deterministically from the cluster name. +// to the cluster's NumberOfBrokerNodes. Broker IDs are 1..N; endpoints are +// derived deterministically from the cluster name, and each broker's client +// subnet is one of the cluster's own ClientSubnets (see brokerSubnet). func (m *Mock) ListNodes( _ context.Context, arn string, page driver.Page, ) (nodes []driver.Node, next string, err error) { @@ -45,6 +46,11 @@ func (m *Mock) synthNodes(c *driver.Cluster) []driver.Node { instance = c.BrokerNodeGroupInfo.InstanceType } + var subnets []string + if c.BrokerNodeGroupInfo != nil { + subnets = c.BrokerNodeGroupInfo.ClientSubnets + } + nodes := make([]driver.Node, 0, count) for i := 1; i <= count; i++ { @@ -56,7 +62,7 @@ func (m *Mock) synthNodes(c *driver.Cluster) []driver.Node { NodeType: "BROKER", InstanceType: instance, RawOptions: map[string]json.RawMessage{ - "brokerNodeInfo": brokerNodeInfoJSON(i, endpoint, c.KafkaVersion), + "brokerNodeInfo": brokerNodeInfoJSON(i, endpoint, brokerSubnet(subnets, i), c.KafkaVersion), }, }) } @@ -64,13 +70,28 @@ func (m *Mock) synthNodes(c *driver.Cluster) []driver.Node { return nodes } +// brokerSubnet returns the client subnet broker brokerID (1-based) lives in. +// Real MSK distributes brokers evenly across the cluster's client subnets (one +// per AZ), so brokers are assigned round-robin; "" when the cluster has none. +func brokerSubnet(subnets []string, brokerID int) string { + if len(subnets) == 0 { + return "" + } + + return subnets[(brokerID-1)%len(subnets)] +} + // brokerNodeInfoJSON builds the raw brokerNodeInfo block for a synthetic node. -// brokerId is a JSON number (the SDK models it as *float64). -func brokerNodeInfoJSON(brokerID int, endpoint, kafkaVersion string) json.RawMessage { +// brokerId is a JSON number (the SDK models it as *float64). clientSubnet is +// omitted when the cluster records no client subnets. +func brokerNodeInfoJSON(brokerID int, endpoint, subnet, kafkaVersion string) json.RawMessage { info := map[string]any{ - "brokerId": brokerID, - "clientSubnet": "subnet-" + strconv.Itoa(brokerID), - "endpoints": []string{endpoint}, + "brokerId": brokerID, + "endpoints": []string{endpoint}, + } + + if subnet != "" { + info["clientSubnet"] = subnet } if kafkaVersion != "" { diff --git a/providers/aws/kafka/operations.go b/providers/aws/kafka/operations.go index 5ba752736..34e2b9470 100644 --- a/providers/aws/kafka/operations.go +++ b/providers/aws/kafka/operations.go @@ -117,6 +117,10 @@ func (m *Mock) mutateClusterErr( cd.mu.Lock() defer cd.mu.Unlock() + if err := m.requireActive(cd); err != nil { + return nil, err + } + if currentVersion != "" && cd.cluster.CurrentVersion != currentVersion { return nil, badRequest( "currentVersion %q does not match cluster version %q", diff --git a/providers/aws/kafka/parity_test.go b/providers/aws/kafka/parity_test.go new file mode 100644 index 000000000..9b4e5217d --- /dev/null +++ b/providers/aws/kafka/parity_test.go @@ -0,0 +1,525 @@ +package kafka_test + +import ( + "context" + "encoding/json" + "errors" + "strconv" + "strings" + "testing" + "time" + + "github.com/stackshy/cloudemu/v2/config" + "github.com/stackshy/cloudemu/v2/providers/aws/cloudwatch" + "github.com/stackshy/cloudemu/v2/providers/aws/kafka" + "github.com/stackshy/cloudemu/v2/services/kafka/driver" + mondriver "github.com/stackshy/cloudemu/v2/services/monitoring/driver" +) + +// settleWindow comfortably covers the provider's settle windows. +const settleWindow = 10 * time.Second + +func newSettleMock(t *testing.T) (*kafka.Mock, *config.FakeClock) { + t.Helper() + + fc := config.NewFakeClock(time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)) + + return kafka.New(config.NewOptions(config.WithClock(fc), config.WithAsyncSettle())), fc +} + +func createThreeAZCluster(t *testing.T, m *kafka.Mock, name string) *driver.Cluster { + t.Helper() + + c, err := m.CreateCluster(context.Background(), driver.CreateClusterInput{ + ClusterName: name, + NumberOfBrokerNodes: 6, + BrokerNodeGroupInfo: &driver.BrokerNodeGroupInfo{ + ClientSubnets: []string{"subnet-aaa", "subnet-bbb", "subnet-ccc"}, + InstanceType: "kafka.m5.large", + }, + }) + if err != nil { + t.Fatalf("CreateCluster: %v", err) + } + + return c +} + +func wantException(t *testing.T, err error, ex string) { + t.Helper() + + var apiErr *driver.APIError + if !errors.As(err, &apiErr) || apiErr.Exception != ex { + t.Fatalf("want %s, got %v", ex, err) + } +} + +// TestListNodesClientSubnetFromCluster asserts each broker's clientSubnet is +// one of the cluster's ClientSubnets, distributed evenly (round-robin) the way +// MSK spreads brokers across AZs, not a fabricated subnet-. +func TestListNodesClientSubnetFromCluster(t *testing.T) { + m := newMock(t) + c := createThreeAZCluster(t, m, "subnets") + + nodes, _, err := m.ListNodes(context.Background(), c.ClusterARN, driver.Page{}) + if err != nil || len(nodes) != 6 { + t.Fatalf("ListNodes: %v len=%d", err, len(nodes)) + } + + want := []string{"subnet-aaa", "subnet-bbb", "subnet-ccc", "subnet-aaa", "subnet-bbb", "subnet-ccc"} + + for i, n := range nodes { + var info struct { + BrokerID int `json:"brokerId"` + ClientSubnet string `json:"clientSubnet"` + } + + if err := json.Unmarshal(n.RawOptions["brokerNodeInfo"], &info); err != nil { + t.Fatalf("decode brokerNodeInfo: %v", err) + } + + if info.BrokerID != i+1 || info.ClientSubnet != want[i] { + t.Fatalf("node %d: brokerId=%d clientSubnet=%q, want %d/%q", i, info.BrokerID, info.ClientSubnet, i+1, want[i]) + } + } +} + +// TestListNodesNoSubnetsOmitsClientSubnet asserts a cluster with no recorded +// client subnets does not get a fabricated one. +func TestListNodesNoSubnetsOmitsClientSubnet(t *testing.T) { + m := newMock(t) + ctx := context.Background() + + c, _ := m.CreateCluster(ctx, driver.CreateClusterInput{ClusterName: "nosubnet", NumberOfBrokerNodes: 2, BrokerNodeGroupInfo: bng()}) + + nodes, _, err := m.ListNodes(ctx, c.ClusterARN, driver.Page{}) + if err != nil { + t.Fatalf("ListNodes: %v", err) + } + + if strings.Contains(string(nodes[0].RawOptions["brokerNodeInfo"]), "clientSubnet") { + t.Fatalf("clientSubnet fabricated: %s", nodes[0].RawOptions["brokerNodeInfo"]) + } +} + +// TestClusterCreatingThenActive asserts that under AsyncSettle a new cluster is +// CREATING (create response, Describe, List) and rejects updates, then settles +// to ACTIVE. +func TestClusterCreatingThenActive(t *testing.T) { + m, fc := newSettleMock(t) + ctx := context.Background() + + c := createThreeAZCluster(t, m, "settling") + if c.State != driver.ClusterStateCreating { + t.Fatalf("create state = %s, want CREATING", c.State) + } + + d, err := m.DescribeCluster(ctx, c.ClusterARN) + if err != nil || d.State != driver.ClusterStateCreating { + t.Fatalf("describe = %v/%v, want CREATING", d, err) + } + + list, _, _ := m.ListClustersV2(ctx, "", "", driver.Page{}) + if len(list) != 1 || list[0].State != driver.ClusterStateCreating { + t.Fatalf("list = %+v, want one CREATING", list) + } + + _, err = m.UpdateBrokerCount(ctx, c.ClusterARN, c.CurrentVersion, 9) + wantException(t, err, driver.ExBadRequest) + + fc.Advance(settleWindow) + + d, _ = m.DescribeCluster(ctx, c.ClusterARN) + if d.State != driver.ClusterStateActive { + t.Fatalf("after settle state = %s, want ACTIVE", d.State) + } + + if _, err := m.UpdateBrokerCount(ctx, c.ClusterARN, d.CurrentVersion, 9); err != nil { + t.Fatalf("UpdateBrokerCount after ACTIVE: %v", err) + } +} + +// TestClusterActiveImmediatelyWithoutSettle pins the default behavior. +func TestClusterActiveImmediatelyWithoutSettle(t *testing.T) { + m := newMock(t) + + c := createThreeAZCluster(t, m, "instant") + if c.State != driver.ClusterStateActive { + t.Fatalf("state = %s, want ACTIVE", c.State) + } +} + +// TestDeleteClusterDeletingThenGone asserts that under AsyncSettle a deleted +// cluster stays visible as DELETING (Describe and List), keeps its name, and +// is gone once the window elapses. +func TestDeleteClusterDeletingThenGone(t *testing.T) { + m, fc := newSettleMock(t) + ctx := context.Background() + + c := createThreeAZCluster(t, m, "doomed") + fc.Advance(settleWindow) + + _, state, err := m.DeleteCluster(ctx, c.ClusterARN, "") + if err != nil || state != driver.ClusterStateDeleting { + t.Fatalf("DeleteCluster = %s/%v", state, err) + } + + d, err := m.DescribeCluster(ctx, c.ClusterARN) + if err != nil || d.State != driver.ClusterStateDeleting { + t.Fatalf("describe while deleting = %+v/%v, want DELETING", d, err) + } + + list, _, _ := m.ListClusters(ctx, "", driver.Page{}) + if len(list) != 1 || list[0].State != driver.ClusterStateDeleting { + t.Fatalf("list while deleting = %+v", list) + } + + // A second delete is idempotent; updates are rejected. + if _, state, err = m.DeleteCluster(ctx, c.ClusterARN, ""); err != nil || state != driver.ClusterStateDeleting { + t.Fatalf("second DeleteCluster = %s/%v", state, err) + } + + _, err = m.UpdateMonitoring(ctx, c.ClusterARN, "", json.RawMessage(`{"enhancedMonitoring":"PER_BROKER"}`)) + wantException(t, err, driver.ExBadRequest) + + // The name is still held while the cluster is DELETING. + _, err = m.CreateCluster(ctx, driver.CreateClusterInput{ClusterName: "doomed", NumberOfBrokerNodes: 3, BrokerNodeGroupInfo: bng()}) + wantException(t, err, driver.ExConflict) + + fc.Advance(settleWindow) + + _, err = m.DescribeCluster(ctx, c.ClusterARN) + wantException(t, err, driver.ExNotFound) + + if list, _, _ = m.ListClusters(ctx, "", driver.Page{}); len(list) != 0 { + t.Fatalf("list after delete = %+v", list) + } + + if _, err := m.CreateCluster(ctx, driver.CreateClusterInput{ClusterName: "doomed", NumberOfBrokerNodes: 3, BrokerNodeGroupInfo: bng()}); err != nil { + t.Fatalf("name not freed after delete: %v", err) + } +} + +// TestDeleteFreesNameForCreateWithoutRead asserts a create reclaims the name of +// a cluster whose delete window has elapsed even if nothing read it since. +func TestDeleteFreesNameForCreateWithoutRead(t *testing.T) { + m, fc := newSettleMock(t) + ctx := context.Background() + + c := createThreeAZCluster(t, m, "reuse") + fc.Advance(settleWindow) + + if _, _, err := m.DeleteCluster(ctx, c.ClusterARN, ""); err != nil { + t.Fatalf("DeleteCluster: %v", err) + } + + fc.Advance(settleWindow) + + if _, err := m.CreateCluster(ctx, driver.CreateClusterInput{ClusterName: "reuse", NumberOfBrokerNodes: 3, BrokerNodeGroupInfo: bng()}); err != nil { + t.Fatalf("CreateCluster reusing name: %v", err) + } +} + +// TestVpcConnectionCreatingThenAvailable asserts CREATING (the real transient +// VpcConnectionState; there is no PENDING) under AsyncSettle, then AVAILABLE. +func TestVpcConnectionCreatingThenAvailable(t *testing.T) { + m, fc := newSettleMock(t) + ctx := context.Background() + + c := createThreeAZCluster(t, m, "vpc-target") + fc.Advance(settleWindow) + + body := json.RawMessage(`{"targetClusterArn":"` + c.ClusterARN + + `","authentication":"SASL_IAM","vpcId":"vpc-1","clientSubnets":["subnet-x"],"securityGroups":["sg-1"]}`) + + v, err := m.CreateVpcConnection(ctx, body) + if err != nil || v.State != "CREATING" { + t.Fatalf("CreateVpcConnection = %+v/%v, want CREATING", v, err) + } + + d, _ := m.DescribeVpcConnection(ctx, v.VpcConnectionARN) + if d.State != "CREATING" { + t.Fatalf("describe state = %s, want CREATING", d.State) + } + + fc.Advance(settleWindow) + + d, _ = m.DescribeVpcConnection(ctx, v.VpcConnectionARN) + if d.State != "AVAILABLE" { + t.Fatalf("settled state = %s, want AVAILABLE", d.State) + } +} + +// kafkaSamples returns the per-minute SampleCount datapoints of an AWS/Kafka +// metric with exactly dims between from and to. +func kafkaSamples( + t *testing.T, cw *cloudwatch.Mock, metric string, dims map[string]string, from, to time.Time, +) []float64 { + t.Helper() + + res, err := cw.GetMetricData(context.Background(), mondriver.GetMetricInput{ + Namespace: "AWS/Kafka", MetricName: metric, Dimensions: dims, + StartTime: from, EndTime: to, Period: 60, Stat: "SampleCount", + }) + if err != nil { + t.Fatalf("GetMetricData %s: %v", metric, err) + } + + return res.Values +} + +// TestClusterMetricsPublishedByTick asserts the AWS/Kafka metrics come from the +// scheduler, not from requests: nothing is published on create, nothing while +// the cluster is CREATING, then one sample per minute once it is ACTIVE, under +// the documented dimension names ("Cluster Name", "Broker ID"), with the topic +// counts read from the cluster's stored topics. +func TestClusterMetricsPublishedByTick(t *testing.T) { + fc := config.NewFakeClock(time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)) + opts := config.NewOptions(config.WithClock(fc), config.WithAsyncSettle()) + m := kafka.New(opts) + cw := cloudwatch.New(opts) + m.SetMonitoring(cw) + + ctx := context.Background() + start := fc.Now() + c := createThreeAZCluster(t, m, "metered") + + ids, err := cw.ListMetricsDetailed(ctx) + if err != nil || len(ids) != 0 { + t.Fatalf("metrics published on create: %+v/%v", ids, err) + } + + if m.Tick(fc.Now()) { + t.Fatal("Tick published metrics for a CREATING cluster") + } + + fc.Advance(settleWindow) + + if _, err := m.CreateTopic(ctx, c.ClusterARN, json.RawMessage( + `{"topicName":"orders","partitionCount":6,"replicationFactor":3}`)); err != nil { + t.Fatalf("CreateTopic: %v", err) + } + + if !m.Tick(fc.Now()) { + t.Fatal("Tick published nothing for an ACTIVE cluster") + } + + // A second tick inside the same minute is not due. + fc.Advance(30 * time.Second) + + if m.Tick(fc.Now()) { + t.Fatal("Tick republished within the 1-minute interval") + } + + fc.Advance(30 * time.Second) + + if !m.Tick(fc.Now()) { + t.Fatal("Tick did not publish once the minute elapsed") + } + + end := fc.Now().Add(time.Second) + clusterDims := map[string]string{"Cluster Name": c.ClusterName} + + if got := kafkaSamples(t, cw, "ActiveControllerCount", clusterDims, start, end); len(got) != 2 { + t.Fatalf("ActiveControllerCount samples = %v, want 2 one-minute samples", got) + } + + topics, err := cw.GetMetricData(ctx, mondriver.GetMetricInput{ + Namespace: "AWS/Kafka", MetricName: "GlobalPartitionCount", Dimensions: clusterDims, + StartTime: start, EndTime: end, Period: 3600, Stat: "Maximum", + }) + if err != nil || len(topics.Values) != 1 || topics.Values[0] != 6 || topics.Unit != "Count" { + t.Fatalf("GlobalPartitionCount = %+v/%v, want 6 Count", topics, err) + } + + for broker := 1; broker <= 6; broker++ { + dims := map[string]string{"Cluster Name": c.ClusterName, "Broker ID": strconv.Itoa(broker)} + if got := kafkaSamples(t, cw, "UnderReplicatedPartitions", dims, start, end); len(got) != 2 { + t.Fatalf("broker %d UnderReplicatedPartitions samples = %v", broker, got) + } + } + + // Host and traffic metrics cloudemu cannot know are not fabricated. + ids, _ = cw.ListMetricsDetailed(ctx) + for _, id := range ids { + switch id.MetricName { + case "CpuUser", "CpuIdle", "MemoryUsed", "KafkaDataLogsDiskUsed", "BytesInPerSec": + t.Fatalf("fabricated metric published: %+v", id) + } + } +} + +// TestTickSkipsDeletingAndServerless asserts a DELETING cluster stops +// publishing and serverless clusters (a different metric set) never publish. +func TestTickSkipsDeletingAndServerless(t *testing.T) { + fc := config.NewFakeClock(time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)) + opts := config.NewOptions(config.WithClock(fc), config.WithAsyncSettle()) + m := kafka.New(opts) + m.SetMonitoring(cloudwatch.New(opts)) + + ctx := context.Background() + c := createThreeAZCluster(t, m, "short-lived") + fc.Advance(settleWindow) + + if _, _, err := m.DeleteCluster(ctx, c.ClusterARN, ""); err != nil { + t.Fatalf("DeleteCluster: %v", err) + } + + if m.Tick(fc.Now()) { + t.Fatal("Tick published for a DELETING cluster") + } + + if _, err := m.CreateClusterV2(ctx, driver.CreateClusterV2Input{ + ClusterName: "sls", + Serverless: json.RawMessage(`{"vpcConfigs":[{"subnetIds":["subnet-1","subnet-2"]}]}`), + }); err != nil { + t.Fatalf("CreateClusterV2: %v", err) + } + + fc.Advance(settleWindow) + + // This tick removes the cluster whose DELETING window has ended (a change); + // the next one, with only the serverless cluster left, does nothing. + if !m.Tick(fc.Now()) { + t.Fatal("Tick did not report removing the deleted cluster") + } + + fc.Advance(time.Minute) + + if m.Tick(fc.Now()) { + t.Fatal("Tick published for a serverless cluster") + } +} + +func encryptionInfo(t *testing.T, c *driver.Cluster) map[string]map[string]any { + t.Helper() + + var out map[string]map[string]any + if err := json.Unmarshal(c.RawOptions["encryptionInfo"], &out); err != nil { + t.Fatalf("decode encryptionInfo %s: %v", c.RawOptions["encryptionInfo"], err) + } + + return out +} + +// TestDefaultEncryptionAtRestKey asserts the default at-rest key is a KMS key +// ARN in the cluster's own account and region (one AWS managed aws/kafka key +// per account+region), not a hard-coded key/msk-default. +func TestDefaultEncryptionAtRestKey(t *testing.T) { + m := kafka.New(config.NewOptions(config.WithRegion("eu-west-1"), config.WithAccountID("123456789012"))) + ctx := context.Background() + + a := createThreeAZCluster(t, m, "enc-a") + b := createThreeAZCluster(t, m, "enc-b") + + keyA, _ := encryptionInfo(t, a)["encryptionAtRest"]["dataVolumeKMSKeyId"].(string) + keyB, _ := encryptionInfo(t, b)["encryptionAtRest"]["dataVolumeKMSKeyId"].(string) + + if !strings.HasPrefix(keyA, "arn:aws:kms:eu-west-1:123456789012:key/") || strings.Contains(keyA, "msk-default") { + t.Fatalf("default key = %q", keyA) + } + + if keyA != keyB { + t.Fatalf("managed key differs across clusters: %q vs %q", keyA, keyB) + } + + if len(strings.TrimPrefix(keyA, "arn:aws:kms:eu-west-1:123456789012:key/")) != 36 { + t.Fatalf("key id is not a UUID: %q", keyA) + } + + // An explicit key is kept; an in-transit-only block gets the managed key. + explicit := "arn:aws:kms:eu-west-1:123456789012:key/11111111-2222-3333-4444-555555555555" + + c, err := m.CreateCluster(ctx, driver.CreateClusterInput{ + ClusterName: "enc-explicit", NumberOfBrokerNodes: 3, BrokerNodeGroupInfo: bng(), + RawOptions: map[string]json.RawMessage{ + "encryptionInfo": json.RawMessage(`{"encryptionAtRest":{"dataVolumeKMSKeyId":"` + explicit + `"}}`), + }, + }) + if err != nil { + t.Fatalf("CreateCluster explicit: %v", err) + } + + if got := encryptionInfo(t, c)["encryptionAtRest"]["dataVolumeKMSKeyId"]; got != explicit { + t.Fatalf("explicit key = %v, want %s", got, explicit) + } + + d, err := m.CreateCluster(ctx, driver.CreateClusterInput{ + ClusterName: "enc-transit", NumberOfBrokerNodes: 3, BrokerNodeGroupInfo: bng(), + RawOptions: map[string]json.RawMessage{ + "encryptionInfo": json.RawMessage(`{"encryptionInTransit":{"clientBroker":"TLS_PLAINTEXT","inCluster":true}}`), + }, + }) + if err != nil { + t.Fatalf("CreateCluster transit-only: %v", err) + } + + info := encryptionInfo(t, d) + if info["encryptionAtRest"]["dataVolumeKMSKeyId"] != keyA || info["encryptionInTransit"]["clientBroker"] != "TLS_PLAINTEXT" { + t.Fatalf("transit-only block = %+v", info) + } +} + +// TestSnapshotSettleStates asserts the lifecycle windows survive a snapshot: a +// cluster and a VPC connection snapshotted mid-create restore still CREATING +// and settle on schedule, and a cluster snapshotted mid-delete restores +// DELETING and is gone once its window ends. +func TestSnapshotSettleStates(t *testing.T) { + src, fc := newSettleMock(t) + ctx := context.Background() + + target := createThreeAZCluster(t, src, "vpc-target") + deleting := createThreeAZCluster(t, src, "mid-delete") + + fc.Advance(settleWindow) + + creating := createThreeAZCluster(t, src, "mid-create") + + if _, _, err := src.DeleteCluster(ctx, deleting.ClusterARN, ""); err != nil { + t.Fatalf("DeleteCluster: %v", err) + } + + vpc, err := src.CreateVpcConnection(ctx, json.RawMessage( + `{"targetClusterArn":"`+target.ClusterARN+`","authentication":"SASL_IAM","vpcId":"vpc-1"}`)) + if err != nil { + t.Fatalf("CreateVpcConnection: %v", err) + } + + raw, err := src.Snapshot(ctx, true) + if err != nil { + t.Fatalf("Snapshot: %v", err) + } + + dst := kafka.New(config.NewOptions(config.WithClock(fc), config.WithAsyncSettle())) + if err := dst.Restore(ctx, raw); err != nil { + t.Fatalf("Restore: %v", err) + } + + got, err := dst.DescribeCluster(ctx, creating.ClusterARN) + if err != nil || got.State != driver.ClusterStateCreating { + t.Fatalf("restored mid-create = %+v/%v, want CREATING", got, err) + } + + got, err = dst.DescribeCluster(ctx, deleting.ClusterARN) + if err != nil || got.State != driver.ClusterStateDeleting { + t.Fatalf("restored mid-delete = %+v/%v, want DELETING", got, err) + } + + v, err := dst.DescribeVpcConnection(ctx, vpc.VpcConnectionARN) + if err != nil || v.State != "CREATING" { + t.Fatalf("restored vpc connection = %+v/%v, want CREATING", v, err) + } + + fc.Advance(settleWindow) + + if got, _ = dst.DescribeCluster(ctx, creating.ClusterARN); got.State != driver.ClusterStateActive { + t.Fatalf("restored cluster after window = %s, want ACTIVE", got.State) + } + + _, err = dst.DescribeCluster(ctx, deleting.ClusterARN) + wantException(t, err, driver.ExNotFound) + + if v, _ = dst.DescribeVpcConnection(ctx, vpc.VpcConnectionARN); v.State != "AVAILABLE" { + t.Fatalf("restored vpc connection after window = %s, want AVAILABLE", v.State) + } +} diff --git a/providers/aws/kafka/review_test.go b/providers/aws/kafka/review_test.go new file mode 100644 index 000000000..bce0ce4ab --- /dev/null +++ b/providers/aws/kafka/review_test.go @@ -0,0 +1,316 @@ +package kafka_test + +import ( + "context" + "encoding/json" + "strconv" + "sync" + "testing" + + "github.com/stackshy/cloudemu/v2/config" + "github.com/stackshy/cloudemu/v2/providers/aws/cloudwatch" + "github.com/stackshy/cloudemu/v2/providers/aws/kafka" + "github.com/stackshy/cloudemu/v2/services/kafka/driver" +) + +// TestSnapshotConcurrentWithMutations runs Snapshot while topics, SCRAM +// secrets, tags and broker counts change on a cluster, VPC connection, +// configuration and replicator. Snapshot must deep-copy each record under its +// lock, so the JSON encoding never reads a map or slice a writer is changing +// (run with -race). The final snapshot then restores every record. +func TestSnapshotConcurrentWithMutations(t *testing.T) { + m := newMock(t) + ctx := context.Background() + c := createThreeAZCluster(t, m, "busy") + + vpc, err := m.CreateVpcConnection(ctx, json.RawMessage( + `{"targetClusterArn":"`+c.ClusterARN+`","authentication":"SASL_IAM","vpcId":"vpc-1"}`)) + if err != nil { + t.Fatalf("CreateVpcConnection: %v", err) + } + + cfg, err := m.CreateConfiguration(ctx, driver.CreateConfigurationInput{ + Name: "busy-cfg", KafkaVersions: []string{"3.6.0"}, ServerProperties: []byte("a=b"), + }) + if err != nil { + t.Fatalf("CreateConfiguration: %v", err) + } + + repl, err := m.CreateReplicator(ctx, json.RawMessage(`{"replicatorName":"busy-rep","kafkaClusters":[],`+ + `"replicationInfoList":[],"serviceExecutionRoleArn":"arn:aws:iam::123456789012:role/r"}`)) + if err != nil { + t.Fatalf("CreateReplicator: %v", err) + } + + const n = 50 + + var wg sync.WaitGroup + + wg.Add(2) + + go func() { + defer wg.Done() + + for i := range n { + name := "t" + strconv.Itoa(i) + _, _ = m.CreateTopic(ctx, c.ClusterARN, json.RawMessage(`{"topicName":"`+name+`","partitionCount":1}`)) + _, _ = m.UpdateTopic(ctx, c.ClusterARN, name, json.RawMessage(`{"partitionCount":2}`)) + _, _ = m.BatchAssociateScramSecret(ctx, c.ClusterARN, + []string{"arn:aws:secretsmanager:us-east-1:123456789012:secret:AmazonMSK_" + name}) + _ = m.TagResource(ctx, c.ClusterARN, map[string]string{name: "v"}) + _ = m.TagResource(ctx, vpc.VpcConnectionARN, map[string]string{name: "v"}) + _ = m.TagResource(ctx, cfg.ARN, map[string]string{name: "v"}) + _ = m.TagResource(ctx, repl.ReplicatorARN, map[string]string{name: "v"}) + _, _ = m.UpdateBrokerCount(ctx, c.ClusterARN, "", int32(3+i%3)) + } + }() + + go func() { + defer wg.Done() + + for range n { + if _, err := m.Snapshot(ctx, false); err != nil { + t.Errorf("Snapshot: %v", err) + + return + } + } + }() + + wg.Wait() + + raw, err := m.Snapshot(ctx, false) + if err != nil { + t.Fatalf("Snapshot: %v", err) + } + + dst := newMock(t) + if err := dst.Restore(ctx, raw); err != nil { + t.Fatalf("Restore: %v", err) + } + + if topics, _, err := dst.ListTopics(ctx, c.ClusterARN, driver.Page{MaxResults: 100}); err != nil || len(topics) != n { + t.Fatalf("restored topics = %d/%v, want %d", len(topics), err, n) + } + + for _, arn := range []string{vpc.VpcConnectionARN, cfg.ARN, repl.ReplicatorARN} { + if tags, err := dst.ListTagsForResource(ctx, arn); err != nil || len(tags) < n { + t.Fatalf("restored tags on %s = %d/%v", arn, len(tags), err) + } + } +} + +// TestDefaultEncryptionInTransitCompleted asserts a partial encryptionInfo gets +// MSK's documented in-transit defaults (ClientBroker TLS, InCluster true) for +// any field it leaves out, while explicit values, including InCluster false, +// are kept. +func TestDefaultEncryptionInTransitCompleted(t *testing.T) { + m := newMock(t) + ctx := context.Background() + + cases := []struct { + name, block, client string + inCluster bool + }{ + {"at-rest-only", `{"encryptionAtRest":{"dataVolumeKMSKeyId":"arn:aws:kms:us-east-1:123456789012:key/mine"}}`, "TLS", true}, + {"client-only", `{"encryptionInTransit":{"clientBroker":"TLS_PLAINTEXT"}}`, "TLS_PLAINTEXT", true}, + {"in-cluster-only", `{"encryptionInTransit":{"inCluster":false}}`, "TLS", false}, + {"empty-block", `{}`, "TLS", true}, + } + + for _, tc := range cases { + c, err := m.CreateCluster(ctx, driver.CreateClusterInput{ + ClusterName: "enc-" + tc.name, NumberOfBrokerNodes: 3, BrokerNodeGroupInfo: bng(), + RawOptions: map[string]json.RawMessage{"encryptionInfo": json.RawMessage(tc.block)}, + }) + if err != nil { + t.Fatalf("%s: CreateCluster: %v", tc.name, err) + } + + info := encryptionInfo(t, c) + if info["encryptionInTransit"]["clientBroker"] != tc.client || + info["encryptionInTransit"]["inCluster"] != tc.inCluster { + t.Fatalf("%s: encryptionInTransit = %+v, want %s/%v", + tc.name, info["encryptionInTransit"], tc.client, tc.inCluster) + } + + if key, _ := info["encryptionAtRest"]["dataVolumeKMSKeyId"].(string); key == "" { + t.Fatalf("%s: no at-rest key: %+v", tc.name, info) + } + } +} + +// TestTickUsesItsNow asserts Tick judges a cluster's state by the time it is +// given, not the options clock: a Tick at a time past the CREATING window +// publishes even though the options clock has not moved. +func TestTickUsesItsNow(t *testing.T) { + m, fc := newSettleMock(t) + m.SetMonitoring(cloudwatch.New(config.NewOptions(config.WithClock(fc)))) + + createThreeAZCluster(t, m, "ahead") + + if !m.Tick(fc.Now().Add(settleWindow)) { + t.Fatal("Tick at a time past the CREATING window published nothing") + } + + if m.Tick(fc.Now()) { + t.Fatal("Tick before the CREATING window ends published metrics") + } +} + +// TestTickReapsFinishedDelete asserts Tick removes a cluster whose DELETING +// window has ended and reports the change (so serve persists it), even with no +// monitoring backend wired and no read in between. +func TestTickReapsFinishedDelete(t *testing.T) { + m, fc := newSettleMock(t) + ctx := context.Background() + + c := createThreeAZCluster(t, m, "reaped") + fc.Advance(settleWindow) + + if m.Tick(fc.Now()) { + t.Fatal("Tick reported a change with nothing to do") + } + + if _, _, err := m.DeleteCluster(ctx, c.ClusterARN, ""); err != nil { + t.Fatalf("DeleteCluster: %v", err) + } + + if m.Tick(fc.Now()) { + t.Fatal("Tick reaped a cluster inside its DELETING window") + } + + fc.Advance(settleWindow) + + if !m.Tick(fc.Now()) { + t.Fatal("Tick did not report reaping a finished delete") + } + + raw, err := m.Snapshot(ctx, false) + if err != nil { + t.Fatalf("Snapshot: %v", err) + } + + var snap struct { + Clusters map[string]json.RawMessage `json:"clusters"` + ClusterNames map[string]string `json:"clusterNames"` + } + if err := json.Unmarshal(raw, &snap); err != nil { + t.Fatalf("decode snapshot: %v", err) + } + + if len(snap.Clusters) != 0 || len(snap.ClusterNames) != 0 { + t.Fatalf("snapshot after reap still holds the cluster: %s", raw) + } + + if m.Tick(fc.Now()) { + t.Fatal("Tick reported a change after the cluster was gone") + } +} + +// TestClusterWritesRequireActive asserts topic, SCRAM secret and cluster +// policy writes are rejected while a cluster is CREATING or DELETING (real MSK +// accepts no API operations on it in those states) and accepted once ACTIVE. +func TestClusterWritesRequireActive(t *testing.T) { + m, fc := newSettleMock(t) + ctx := context.Background() + + c := createThreeAZCluster(t, m, "guarded") + secret := []string{"arn:aws:secretsmanager:us-east-1:123456789012:secret:AmazonMSK_s"} + + writes := map[string]func() error{ + "CreateTopic": func() error { + _, err := m.CreateTopic(ctx, c.ClusterARN, json.RawMessage(`{"topicName":"t","partitionCount":1}`)) + + return err + }, + "UpdateTopic": func() error { + _, err := m.UpdateTopic(ctx, c.ClusterARN, "t", json.RawMessage(`{"partitionCount":2}`)) + + return err + }, + "BatchAssociateScramSecret": func() error { + _, err := m.BatchAssociateScramSecret(ctx, c.ClusterARN, secret) + + return err + }, + "BatchDisassociateScramSecret": func() error { + _, err := m.BatchDisassociateScramSecret(ctx, c.ClusterARN, secret) + + return err + }, + "PutClusterPolicy": func() error { + _, err := m.PutClusterPolicy(ctx, c.ClusterARN, json.RawMessage(`{"policy":"{}"}`)) + + return err + }, + "DeleteClusterPolicy": func() error { return m.DeleteClusterPolicy(ctx, c.ClusterARN) }, + "DeleteTopic": func() error { return m.DeleteTopic(ctx, c.ClusterARN, "t") }, + } + order := []string{ + "CreateTopic", "UpdateTopic", "BatchAssociateScramSecret", "BatchDisassociateScramSecret", + "PutClusterPolicy", "DeleteClusterPolicy", "DeleteTopic", + } + + for _, op := range order { + wantException(t, writes[op](), driver.ExBadRequest) + } + + fc.Advance(settleWindow) + + for _, op := range order { + if err := writes[op](); err != nil { + t.Fatalf("%s on ACTIVE cluster: %v", op, err) + } + } + + if _, _, err := m.DeleteCluster(ctx, c.ClusterARN, ""); err != nil { + t.Fatalf("DeleteCluster: %v", err) + } + + for _, op := range order { + wantException(t, writes[op](), driver.ExBadRequest) + } +} + +// TestCompatibleVersionsAfterDeleteWindow asserts GetCompatibleKafkaVersions +// stops answering for a cluster once its DELETING window has ended. +func TestCompatibleVersionsAfterDeleteWindow(t *testing.T) { + m, fc := newSettleMock(t) + ctx := context.Background() + + c := createThreeAZCluster(t, m, "versioned") + fc.Advance(settleWindow) + + if _, _, err := m.DeleteCluster(ctx, c.ClusterARN, ""); err != nil { + t.Fatalf("DeleteCluster: %v", err) + } + + if _, err := m.GetCompatibleKafkaVersions(ctx, c.ClusterARN); err != nil { + t.Fatalf("GetCompatibleKafkaVersions while DELETING: %v", err) + } + + fc.Advance(settleWindow) + + _, err := m.GetCompatibleKafkaVersions(ctx, c.ClusterARN) + wantException(t, err, driver.ExNotFound) +} + +// TestCreateReclaimsDanglingName asserts a create succeeds when the name index +// still points at a cluster ARN that is no longer stored (a snapshot taken +// between the two removals). +func TestCreateReclaimsDanglingName(t *testing.T) { + m := kafka.New(config.NewOptions()) + ctx := context.Background() + + snap := `{"clusterNames":{"orphan":"arn:aws:kafka:us-east-1:123456789012:cluster/orphan/gone-1"}}` + if err := m.Restore(ctx, json.RawMessage(snap)); err != nil { + t.Fatalf("Restore: %v", err) + } + + if _, err := m.CreateCluster(ctx, driver.CreateClusterInput{ + ClusterName: "orphan", NumberOfBrokerNodes: 3, BrokerNodeGroupInfo: bng(), + }); err != nil { + t.Fatalf("CreateCluster over a dangling name: %v", err) + } +} diff --git a/providers/aws/kafka/scram_secrets.go b/providers/aws/kafka/scram_secrets.go index 292e23c25..f808d90c2 100644 --- a/providers/aws/kafka/scram_secrets.go +++ b/providers/aws/kafka/scram_secrets.go @@ -24,6 +24,10 @@ func (m *Mock) BatchAssociateScramSecret( cd.mu.Lock() defer cd.mu.Unlock() + if err := m.requireActive(cd); err != nil { + return nil, err + } + var unprocessed []json.RawMessage for _, arn := range secretARNs { @@ -57,6 +61,10 @@ func (m *Mock) BatchDisassociateScramSecret( cd.mu.Lock() defer cd.mu.Unlock() + if err := m.requireActive(cd); err != nil { + return nil, err + } + var unprocessed []json.RawMessage for _, arn := range secretARNs { diff --git a/providers/aws/kafka/snapshot.go b/providers/aws/kafka/snapshot.go index cf989dcc0..bfc4583ba 100644 --- a/providers/aws/kafka/snapshot.go +++ b/providers/aws/kafka/snapshot.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "time" "github.com/stackshy/cloudemu/v2/internal/snapshot" "github.com/stackshy/cloudemu/v2/services/kafka/driver" @@ -19,13 +20,20 @@ var _ snapshot.Snapshottable = (*Mock)(nil) // index of *clusterData pointers shared with the clusters store, rebuilt on // restore from each cluster's operations slice so the shared identity (a // DescribeClusterOperation resolves to the same cluster object) is preserved. -// The per-record mutexes, createMu, and the wired opts are not serialized. +// The AsyncSettle lifecycle windows (a cluster's CREATING/DELETING deadline, a +// VPC connection's CREATING deadline) and the metric sample time are +// serialized, so a restored resource keeps its transient state until its window +// ends and Tick keeps its publishing cadence. The per-record mutexes, +// createMu/reapMu, the wired opts and the monitoring backend are not. type kafkaSnapshot struct { Clusters map[string]*clusterSnapshot `json:"clusters,omitempty"` ClusterNames json.RawMessage `json:"clusterNames,omitempty"` Configs map[string]driver.Configuration `json:"configs,omitempty"` VPCConns map[string]driver.VpcConnection `json:"vpcConns,omitempty"` - Replicators map[string]*replicatorSnapshot `json:"replicators,omitempty"` + // VPCConnReadyAt holds the CREATING deadline of each VPC connection still + // inside its window (kept beside VPCConns so older snapshots still load). + VPCConnReadyAt map[string]time.Time `json:"vpcConnReadyAt,omitempty"` + Replicators map[string]*replicatorSnapshot `json:"replicators,omitempty"` } // clusterSnapshot mirrors clusterData (all fields unexported). @@ -36,6 +44,10 @@ type clusterSnapshot struct { ScramSecrets []string `json:"scramSecrets,omitempty"` Policy string `json:"policy,omitempty"` PolicyVersion string `json:"policyVersion,omitempty"` + // Lifecycle windows (AsyncSettle) and the last metric sample time. + ReadyAt time.Time `json:"readyAt,omitzero"` + DeleteAt time.Time `json:"deleteAt,omitzero"` + MetricsSampleAt time.Time `json:"metricsSampleAt,omitzero"` } // replicatorSnapshot mirrors replicatorData. @@ -45,22 +57,10 @@ type replicatorSnapshot struct { } // Snapshot captures the mock's entire state as JSON. includeAssets is unused. MSK holds resource -// metadata, not bulk object bodies. +// metadata, not bulk object bodies. Each record is deep-copied under its own +// lock, so the encoding never reads a map or slice a concurrent writer changes. func (m *Mock) Snapshot(_ context.Context, _ bool) (json.RawMessage, error) { - snap := kafkaSnapshot{} - - if m.clusters.Len() > 0 { - snap.Clusters = make(map[string]*clusterSnapshot, m.clusters.Len()) - - for arn, cd := range m.clusters.All() { - cd.mu.RLock() - snap.Clusters[arn] = &clusterSnapshot{ - Cluster: cd.cluster, Operations: cd.operations, Topics: cd.topics, - ScramSecrets: cd.scramSecrets, Policy: cd.policy, PolicyVersion: cd.policyVersion, - } - cd.mu.RUnlock() - } - } + snap := kafkaSnapshot{Clusters: m.clustersForSnapshot()} names, err := m.clusterNames.Snapshot() if err != nil { @@ -68,38 +68,117 @@ func (m *Mock) Snapshot(_ context.Context, _ bool) (json.RawMessage, error) { } snap.ClusterNames = names + snap.Configs = m.configsForSnapshot() + snap.VPCConns, snap.VPCConnReadyAt = m.vpcConnsForSnapshot() + snap.Replicators = m.replicatorsForSnapshot() - if m.configs.Len() > 0 { - snap.Configs = make(map[string]driver.Configuration, m.configs.Len()) + return json.Marshal(snap) +} - for arn, cd := range m.configs.All() { - cd.mu.RLock() - snap.Configs[arn] = cd.config - cd.mu.RUnlock() - } +// clustersForSnapshot deep-copies every cluster record, or returns nil when +// there are none. +func (m *Mock) clustersForSnapshot() map[string]*clusterSnapshot { + if m.clusters.Len() == 0 { + return nil } - if m.vpcConns.Len() > 0 { - snap.VPCConns = make(map[string]driver.VpcConnection, m.vpcConns.Len()) + out := make(map[string]*clusterSnapshot, m.clusters.Len()) - for arn, vd := range m.vpcConns.All() { - vd.mu.RLock() - snap.VPCConns[arn] = vd.vpc - vd.mu.RUnlock() + for arn, cd := range m.clusters.All() { + cd.mu.RLock() + out[arn] = cd.snapshotLocked() + cd.mu.RUnlock() + } + + return out +} + +// snapshotLocked deep-copies a cluster record. The caller holds cd.mu. +func (cd *clusterData) snapshotLocked() *clusterSnapshot { + cs := &clusterSnapshot{ + Cluster: snapshotCluster(cd.cluster), + ScramSecrets: copyStrings(cd.scramSecrets), + Policy: cd.policy, PolicyVersion: cd.policyVersion, + ReadyAt: cd.readyAt, DeleteAt: cd.deleteAt, MetricsSampleAt: cd.metricsSampleAt, + } + + if cd.operations != nil { + cs.Operations = make([]driver.ClusterOperation, len(cd.operations)) + for i := range cd.operations { + cs.Operations[i] = copyOperation(cd.operations[i]) + } + } + + if cd.topics != nil { + cs.Topics = make(map[string]driver.Topic, len(cd.topics)) + for name, t := range cd.topics { + cs.Topics[name] = snapshotTopic(t) } } - if m.replicators.Len() > 0 { - snap.Replicators = make(map[string]*replicatorSnapshot, m.replicators.Len()) + return cs +} + +// configsForSnapshot deep-copies every configuration, or returns nil when there +// are none. +func (m *Mock) configsForSnapshot() map[string]driver.Configuration { + if m.configs.Len() == 0 { + return nil + } + + out := make(map[string]driver.Configuration, m.configs.Len()) - for arn, rd := range m.replicators.All() { - rd.mu.RLock() - snap.Replicators[arn] = &replicatorSnapshot{Replicator: rd.replicator, Version: rd.version} - rd.mu.RUnlock() + for arn, cd := range m.configs.All() { + cd.mu.RLock() + out[arn] = snapshotConfig(cd.config) + cd.mu.RUnlock() + } + + return out +} + +// vpcConnsForSnapshot deep-copies every VPC connection, plus the CREATING +// deadline of each one still inside its window (nil when none is). +func (m *Mock) vpcConnsForSnapshot() (conns map[string]driver.VpcConnection, readyAt map[string]time.Time) { + if m.vpcConns.Len() == 0 { + return nil, nil + } + + conns = make(map[string]driver.VpcConnection, m.vpcConns.Len()) + + for arn, vd := range m.vpcConns.All() { + vd.mu.RLock() + conns[arn] = snapshotVpcConnection(vd.vpc) + + if !vd.readyAt.IsZero() { + if readyAt == nil { + readyAt = map[string]time.Time{} + } + + readyAt[arn] = vd.readyAt } + vd.mu.RUnlock() } - return json.Marshal(snap) + return conns, readyAt +} + +// replicatorsForSnapshot deep-copies every replicator, or returns nil when +// there are none. +func (m *Mock) replicatorsForSnapshot() map[string]*replicatorSnapshot { + if m.replicators.Len() == 0 { + return nil + } + + out := make(map[string]*replicatorSnapshot, m.replicators.Len()) + + for arn, rd := range m.replicators.All() { + rd.mu.RLock() + out[arn] = &replicatorSnapshot{Replicator: snapshotReplicator(rd.replicator), Version: rd.version} + rd.mu.RUnlock() + } + + return out } // Restore rebuilds the mock's state under the original identities: every cluster, @@ -112,9 +191,16 @@ func (m *Mock) Restore(_ context.Context, data json.RawMessage) error { } for arn, cs := range snap.Clusters { + // A snapshot from before the provider completed encryptionInfo at create + // lacks it; backfill MSK's default so the restored cluster reports it. + if cs.Cluster.ClusterType == driver.ClusterTypeProvisioned { + cs.Cluster.RawOptions = m.withDefaultEncryption(cs.Cluster.RawOptions) + } + cd := &clusterData{ cluster: cs.Cluster, operations: cs.Operations, topics: cs.Topics, scramSecrets: cs.ScramSecrets, policy: cs.Policy, policyVersion: cs.PolicyVersion, + readyAt: cs.ReadyAt, deleteAt: cs.DeleteAt, metricsSampleAt: cs.MetricsSampleAt, } m.clusters.Set(arn, cd) @@ -137,7 +223,7 @@ func (m *Mock) Restore(_ context.Context, data json.RawMessage) error { } for arn, vpc := range snap.VPCConns { - m.vpcConns.Set(arn, &vpcConnData{vpc: vpc}) + m.vpcConns.Set(arn, &vpcConnData{vpc: vpc, readyAt: snap.VPCConnReadyAt[arn]}) } for arn, rs := range snap.Replicators { diff --git a/providers/aws/kafka/topics.go b/providers/aws/kafka/topics.go index c6f767089..f4208523d 100644 --- a/providers/aws/kafka/topics.go +++ b/providers/aws/kafka/topics.go @@ -62,6 +62,10 @@ func (m *Mock) CreateTopic(_ context.Context, clusterARN string, body json.RawMe cd.mu.Lock() defer cd.mu.Unlock() + if err := m.requireActive(cd); err != nil { + return nil, err + } + if cd.topics == nil { cd.topics = map[string]driver.Topic{} } @@ -168,6 +172,10 @@ func (m *Mock) UpdateTopic( cd.mu.Lock() defer cd.mu.Unlock() + if err := m.requireActive(cd); err != nil { + return nil, err + } + t, ok := cd.getTopic(topicName) if !ok { return nil, notFound("topic not found: %s", topicName) @@ -198,6 +206,10 @@ func (m *Mock) DeleteTopic(_ context.Context, clusterARN, topicName string) erro cd.mu.Lock() defer cd.mu.Unlock() + if err := m.requireActive(cd); err != nil { + return err + } + if _, ok := cd.getTopic(topicName); !ok { return notFound("topic not found: %s", topicName) } diff --git a/providers/aws/kafka/versions.go b/providers/aws/kafka/versions.go index 53dcb64f0..5a63196e5 100644 --- a/providers/aws/kafka/versions.go +++ b/providers/aws/kafka/versions.go @@ -92,8 +92,8 @@ func (m *Mock) compatibilitySources(clusterARN string) []string { return all } - cd, ok := m.clusters.Get(clusterARN) - if !ok { + cd, err := m.getCluster(clusterARN) + if err != nil { return nil } diff --git a/providers/aws/kafka/vpc_connections.go b/providers/aws/kafka/vpc_connections.go index ba3af342a..cbb1a0e36 100644 --- a/providers/aws/kafka/vpc_connections.go +++ b/providers/aws/kafka/vpc_connections.go @@ -38,7 +38,8 @@ type createVpcConnectionRequest struct { // CreateVpcConnection mints a VPC connection in the AVAILABLE state, carrying // the client subnets and security groups verbatim in raw options so a later -// Describe reflects them. +// Describe reflects them. Under AsyncSettle it reports CREATING for a settle +// window first (real VpcConnectionState has CREATING and no PENDING value). func (m *Mock) CreateVpcConnection(_ context.Context, body json.RawMessage) (*driver.VpcConnection, error) { var req createVpcConnectionRequest if len(body) > 0 { @@ -79,11 +80,14 @@ func (m *Mock) CreateVpcConnection(_ context.Context, body json.RawMessage) (*dr // The ARN embeds a fresh UUID; SetIfAbsent keeps the create atomic and guards // the (impossible-but-cheap) collision. - if !m.vpcConns.SetIfAbsent(vpc.VpcConnectionARN, &vpcConnData{vpc: vpc}) { + vd := &vpcConnData{vpc: vpc, readyAt: m.settleUntil(vpcConnCreateSettle)} + if !m.vpcConns.SetIfAbsent(vpc.VpcConnectionARN, vd) { return nil, badRequest("vpc connection already exists: %s", vpc.VpcConnectionARN) } - out := snapshotVpcConnection(vpc) + vd.mu.RLock() + out := m.observedVpcConnectionLocked(vd) + vd.mu.RUnlock() return &out, nil } @@ -139,9 +143,8 @@ func (m *Mock) DescribeVpcConnection(_ context.Context, arn string) (*driver.Vpc } vd.mu.RLock() - defer vd.mu.RUnlock() - - out := snapshotVpcConnection(vd.vpc) + out := m.observedVpcConnectionLocked(vd) + vd.mu.RUnlock() return &out, nil } @@ -154,7 +157,7 @@ func (m *Mock) DeleteVpcConnection(_ context.Context, arn string) (*driver.VpcCo } vd.mu.RLock() - out := snapshotVpcConnection(vd.vpc) + out := m.observedVpcConnectionLocked(vd) vd.mu.RUnlock() m.vpcConns.Delete(arn) @@ -204,7 +207,7 @@ func (m *Mock) snapshotVpcConnections(targetCluster string) []driver.VpcConnecti for _, vd := range vals { vd.mu.RLock() - snap := snapshotVpcConnection(vd.vpc) + snap := m.observedVpcConnectionLocked(vd) vd.mu.RUnlock() if targetCluster != "" && snap.TargetClusterARN != targetCluster { diff --git a/providers/aws/tickables_test.go b/providers/aws/tickables_test.go index ab76f1d5e..6f0dffaec 100644 --- a/providers/aws/tickables_test.go +++ b/providers/aws/tickables_test.go @@ -8,17 +8,23 @@ import ( ) // TestTickablesRegistersTimeDrivenServices checks the serve ticker reaches the -// region's own SQS, EFS, CloudWatch, SSM, CloudFormation and Batch, so queue -// gauges, EFS metrics, due alarms, parameter policies, settling stack operations -// and Batch job state-change events run without a read. SQS and EFS come before -// CloudWatch so alarms see the metrics published in the same tick. +// region's own SQS, EFS, Kafka, CloudWatch, SSM, CloudFormation and Batch, so +// queue gauges, EFS and Kafka metrics, due alarms, parameter policies, settling +// stack operations and Batch job state-change events run without a read. SQS, +// EFS and Kafka come before CloudWatch so alarms see the metrics published in +// the same tick. func TestTickablesRegistersTimeDrivenServices(t *testing.T) { p := aws.New() got := p.Tickables() - if len(got) != 6 || got[0] != config.Tickable(p.SQS) || got[1] != config.Tickable(p.EFS) || - got[2] != config.Tickable(p.CloudWatch) || got[3] != config.Tickable(p.SSM) || - got[4] != config.Tickable(p.CloudFormation) || got[5] != config.Tickable(p.Batch) { - t.Fatalf("Tickables() = %v, want this provider's SQS, EFS, CloudWatch, SSM, CloudFormation and Batch", got) + want := []config.Tickable{p.SQS, p.EFS, p.Kafka, p.CloudWatch, p.SSM, p.CloudFormation, p.Batch} + if len(got) != len(want) { + t.Fatalf("Tickables() = %v, want this provider's SQS, EFS, Kafka, CloudWatch, SSM, CloudFormation and Batch", got) + } + + for i := range want { + if got[i] != want[i] { + t.Fatalf("Tickables()[%d] = %v, want %v", i, got[i], want[i]) + } } } diff --git a/server/aws/kafka/parity_sdk_test.go b/server/aws/kafka/parity_sdk_test.go new file mode 100644 index 000000000..f34fc0932 --- /dev/null +++ b/server/aws/kafka/parity_sdk_test.go @@ -0,0 +1,291 @@ +package kafka_test + +import ( + "context" + "errors" + "net/http/httptest" + "strings" + "testing" + "time" + + "github.com/aws/aws-sdk-go-v2/aws" + awsconfig "github.com/aws/aws-sdk-go-v2/config" + "github.com/aws/aws-sdk-go-v2/credentials" + awscw "github.com/aws/aws-sdk-go-v2/service/cloudwatch" + cwtypes "github.com/aws/aws-sdk-go-v2/service/cloudwatch/types" + awskafka "github.com/aws/aws-sdk-go-v2/service/kafka" + kafkatypes "github.com/aws/aws-sdk-go-v2/service/kafka/types" + + "github.com/stackshy/cloudemu/v2" + cloudconfig "github.com/stackshy/cloudemu/v2/config" + awsprovider "github.com/stackshy/cloudemu/v2/providers/aws" + awsserver "github.com/stackshy/cloudemu/v2/server/aws" +) + +// parityEnv is an in-process AWS server (Kafka + CloudWatch from one provider, +// so the AWS/Kafka metrics land in the CloudWatch the client reads) with real +// SDK clients, the FakeClock driving settle windows, and the provider whose +// Tickables the serve scheduler would run. +type parityEnv struct { + kafka *awskafka.Client + cw *awscw.Client + clock *cloudconfig.FakeClock + cloud *awsprovider.Provider +} + +// tick runs one round of the serve scheduler at the current FakeClock time. +func (e *parityEnv) tick() { + for _, tk := range e.cloud.Tickables() { + tk.Tick(e.clock.Now()) + } +} + +func newParityEnv(t *testing.T, opts ...cloudconfig.Option) *parityEnv { + t.Helper() + + c, cw, fc, cloud := newParityClientsFull(t, opts...) + + return &parityEnv{kafka: c, cw: cw, clock: fc, cloud: cloud} +} + +func newParityClients(t *testing.T, opts ...cloudconfig.Option) (*awskafka.Client, *awscw.Client, *cloudconfig.FakeClock) { + t.Helper() + + c, cw, fc, _ := newParityClientsFull(t, opts...) + + return c, cw, fc +} + +func newParityClientsFull( + t *testing.T, opts ...cloudconfig.Option, +) (*awskafka.Client, *awscw.Client, *cloudconfig.FakeClock, *awsprovider.Provider) { + t.Helper() + + fc := cloudconfig.NewFakeClock(time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)) + cloud := cloudemu.NewAWS(append([]cloudconfig.Option{cloudconfig.WithClock(fc)}, opts...)...) + srv := awsserver.New(awsserver.Drivers{Kafka: cloud.Kafka, CloudWatch: cloud.CloudWatch}) + + ts := httptest.NewServer(srv) + t.Cleanup(ts.Close) + + cfg, err := awsconfig.LoadDefaultConfig(context.Background(), + awsconfig.WithRegion("us-east-1"), + awsconfig.WithCredentialsProvider(credentials.NewStaticCredentialsProvider("test", "test", "")), + ) + if err != nil { + t.Fatalf("aws config: %v", err) + } + + cfg.BaseEndpoint = aws.String(ts.URL) + + return awskafka.NewFromConfig(cfg), awscw.NewFromConfig(cfg), fc, cloud +} + +func createSDKCluster(ctx context.Context, t *testing.T, c *awskafka.Client, name string) string { + t.Helper() + + out, err := c.CreateCluster(ctx, &awskafka.CreateClusterInput{ + ClusterName: aws.String(name), + KafkaVersion: aws.String("3.6.0"), + NumberOfBrokerNodes: aws.Int32(3), + BrokerNodeGroupInfo: &kafkatypes.BrokerNodeGroupInfo{ + ClientSubnets: []string{"subnet-0a1", "subnet-0b2", "subnet-0c3"}, + InstanceType: aws.String("kafka.m5.large"), + }, + }) + if err != nil { + t.Fatalf("CreateCluster: %v", err) + } + + return aws.ToString(out.ClusterArn) +} + +// TestSDKListNodesClientSubnet asserts ListNodes reports the cluster's own +// client subnets through the wire. +func TestSDKListNodesClientSubnet(t *testing.T) { + ctx := context.Background() + c, _, _ := newParityClients(t) + arn := createSDKCluster(ctx, t, c, "sdk-subnets") + + out, err := c.ListNodes(ctx, &awskafka.ListNodesInput{ClusterArn: aws.String(arn)}) + if err != nil { + t.Fatalf("ListNodes: %v", err) + } + + want := []string{"subnet-0a1", "subnet-0b2", "subnet-0c3"} + for i, n := range out.NodeInfoList { + if got := aws.ToString(n.BrokerNodeInfo.ClientSubnet); got != want[i] { + t.Fatalf("node %d clientSubnet = %q, want %q", i, got, want[i]) + } + } +} + +// TestSDKAsyncClusterLifecycle drives CREATING -> ACTIVE -> DELETING -> gone +// through the real SDK under AsyncSettle. +func TestSDKAsyncClusterLifecycle(t *testing.T) { + ctx := context.Background() + c, _, fc := newParityClients(t, cloudconfig.WithAsyncSettle()) + arn := createSDKCluster(ctx, t, c, "sdk-async") + + desc, err := c.DescribeCluster(ctx, &awskafka.DescribeClusterInput{ClusterArn: aws.String(arn)}) + if err != nil || desc.ClusterInfo.State != kafkatypes.ClusterStateCreating { + t.Fatalf("describe while creating = %v/%v", desc, err) + } + + _, err = c.UpdateMonitoring(ctx, &awskafka.UpdateMonitoringInput{ + ClusterArn: aws.String(arn), CurrentVersion: desc.ClusterInfo.CurrentVersion, + EnhancedMonitoring: kafkatypes.EnhancedMonitoringPerBroker, + }) + + var br *kafkatypes.BadRequestException + if !errors.As(err, &br) { + t.Fatalf("update while CREATING = %v, want BadRequestException", err) + } + + fc.Advance(10 * time.Second) + + v2, err := c.DescribeClusterV2(ctx, &awskafka.DescribeClusterV2Input{ClusterArn: aws.String(arn)}) + if err != nil || v2.ClusterInfo.State != kafkatypes.ClusterStateActive { + t.Fatalf("describe v2 after settle = %v/%v", v2, err) + } + + del, err := c.DeleteCluster(ctx, &awskafka.DeleteClusterInput{ClusterArn: aws.String(arn)}) + if err != nil || del.State != kafkatypes.ClusterStateDeleting { + t.Fatalf("DeleteCluster = %v/%v", del, err) + } + + desc, err = c.DescribeCluster(ctx, &awskafka.DescribeClusterInput{ClusterArn: aws.String(arn)}) + if err != nil || desc.ClusterInfo.State != kafkatypes.ClusterStateDeleting { + t.Fatalf("describe while deleting = %v/%v", desc, err) + } + + fc.Advance(10 * time.Second) + + _, err = c.DescribeCluster(ctx, &awskafka.DescribeClusterInput{ClusterArn: aws.String(arn)}) + + var nf *kafkatypes.NotFoundException + if !errors.As(err, &nf) { + t.Fatalf("describe after delete = %v, want NotFoundException", err) + } +} + +// TestSDKAsyncVpcConnectionCreating asserts a VPC connection reports CREATING +// then AVAILABLE through the wire under AsyncSettle. +func TestSDKAsyncVpcConnectionCreating(t *testing.T) { + ctx := context.Background() + c, _, fc := newParityClients(t, cloudconfig.WithAsyncSettle()) + arn := createSDKCluster(ctx, t, c, "sdk-vpc") + + fc.Advance(10 * time.Second) + + v, err := c.CreateVpcConnection(ctx, &awskafka.CreateVpcConnectionInput{ + TargetClusterArn: aws.String(arn), Authentication: aws.String("SASL_IAM"), + VpcId: aws.String("vpc-123"), ClientSubnets: []string{"subnet-x"}, SecurityGroups: []string{"sg-1"}, + }) + if err != nil || v.State != kafkatypes.VpcConnectionStateCreating { + t.Fatalf("CreateVpcConnection = %v/%v, want CREATING", v, err) + } + + fc.Advance(10 * time.Second) + + d, err := c.DescribeVpcConnection(ctx, &awskafka.DescribeVpcConnectionInput{Arn: v.VpcConnectionArn}) + if err != nil || d.State != kafkatypes.VpcConnectionStateAvailable { + t.Fatalf("DescribeVpcConnection = %v/%v, want AVAILABLE", d, err) + } +} + +// TestSDKKafkaMetricsAndDefaultKey asserts the AWS/Kafka metrics are published +// by the serve scheduler (none on create, one sample per minute after), +// readable through the CloudWatch API under the documented dimensions, and the +// default at-rest key is a real-shaped KMS key ARN in the v1 and v2 shapes. +func TestSDKKafkaMetricsAndDefaultKey(t *testing.T) { + ctx := context.Background() + env := newParityEnv(t) + c, cw := env.kafka, env.cw + start := env.clock.Now() + arn := createSDKCluster(ctx, t, c, "sdk-metrics") + + listed := func() []cwtypes.Metric { + out, err := cw.ListMetrics(ctx, &awscw.ListMetricsInput{Namespace: aws.String("AWS/Kafka")}) + if err != nil { + t.Fatalf("ListMetrics: %v", err) + } + + return out.Metrics + } + + if got := listed(); len(got) != 0 { + t.Fatalf("metrics published by a request: %+v", got) + } + + env.tick() + env.clock.Advance(time.Minute) + env.tick() + + var sawController, sawBroker bool + + for _, m := range listed() { + dims := map[string]string{} + for _, d := range m.Dimensions { + dims[aws.ToString(d.Name)] = aws.ToString(d.Value) + } + + if dims["Cluster Name"] != "sdk-metrics" { + continue + } + + switch aws.ToString(m.MetricName) { + case "ActiveControllerCount": + sawController = len(dims) == 1 + case "UnderReplicatedPartitions": + sawBroker = sawBroker || dims["Broker ID"] != "" + } + } + + if !sawController || !sawBroker { + t.Fatalf("AWS/Kafka metrics missing (controller=%v broker=%v)", sawController, sawBroker) + } + + stats, err := cw.GetMetricStatistics(ctx, &awscw.GetMetricStatisticsInput{ + Namespace: aws.String("AWS/Kafka"), + MetricName: aws.String("ActiveControllerCount"), + Dimensions: []cwtypes.Dimension{{Name: aws.String("Cluster Name"), Value: aws.String("sdk-metrics")}}, + StartTime: aws.Time(start), + EndTime: aws.Time(env.clock.Now().Add(time.Minute)), + Period: aws.Int32(60), + Statistics: []cwtypes.Statistic{cwtypes.StatisticSampleCount, cwtypes.StatisticMaximum}, + }) + if err != nil { + t.Fatalf("GetMetricStatistics: %v", err) + } + + if len(stats.Datapoints) != 2 { + t.Fatalf("ActiveControllerCount datapoints = %+v, want 2 one-minute samples", stats.Datapoints) + } + + for _, dp := range stats.Datapoints { + if aws.ToFloat64(dp.SampleCount) != 1 || aws.ToFloat64(dp.Maximum) != 1 || dp.Unit != cwtypes.StandardUnitCount { + t.Fatalf("datapoint = %+v, want one sample of 1 Count", dp) + } + } + + desc, err := c.DescribeCluster(ctx, &awskafka.DescribeClusterInput{ClusterArn: aws.String(arn)}) + if err != nil { + t.Fatalf("DescribeCluster: %v", err) + } + + key := aws.ToString(desc.ClusterInfo.EncryptionInfo.EncryptionAtRest.DataVolumeKMSKeyId) + if !strings.HasPrefix(key, "arn:aws:kms:us-east-1:") || strings.Contains(key, "msk-default") { + t.Fatalf("default key = %q", key) + } + + v2, err := c.DescribeClusterV2(ctx, &awskafka.DescribeClusterV2Input{ClusterArn: aws.String(arn)}) + if err != nil { + t.Fatalf("DescribeClusterV2: %v", err) + } + + enc := v2.ClusterInfo.Provisioned.EncryptionInfo + if enc == nil || aws.ToString(enc.EncryptionAtRest.DataVolumeKMSKeyId) != key { + t.Fatalf("v2 provisioned encryptionInfo = %+v, want key %s", enc, key) + } +} diff --git a/server/aws/kafka/types.go b/server/aws/kafka/types.go index ce35abdcc..651477db4 100644 --- a/server/aws/kafka/types.go +++ b/server/aws/kafka/types.go @@ -136,10 +136,8 @@ func clusterToWire(c *driver.Cluster) map[string]json.RawMessage { base["brokerNodeGroupInfo"] = bngToWire(c.BrokerNodeGroupInfo) } - if _, ok := c.RawOptions["encryptionInfo"]; !ok { - base["encryptionInfo"] = defaultEncryptionInfo() - } - + // encryptionInfo (including MSK's default at-rest key) is completed by the + // provider at create time and carried in RawOptions; mergeRaw surfaces it. addZookeeperConnect(base, c) return mergeRaw(base, c.RawOptions) @@ -191,22 +189,6 @@ func regionFromARN(arn string) string { return parts[regionIndex] } -// defaultEncryptionInfo returns the MSK default encryption configuration that -// real DescribeCluster surfaces when none is specified at create time: -// in-transit TLS (client-broker + in-cluster) and at-rest with an -// AWS-managed data-volume KMS key. -func defaultEncryptionInfo() map[string]any { - return map[string]any{ - "encryptionAtRest": map[string]any{ - "dataVolumeKMSKeyId": "arn:aws:kms:us-east-1:000000000000:key/msk-default", - }, - "encryptionInTransit": map[string]any{ - "clientBroker": "TLS", - "inCluster": true, - }, - } -} - // enhancedMonitoringOrDefault returns the cluster's EnhancedMonitoring level, // falling back to MSK's DEFAULT. Real DescribeCluster always surfaces this enum // (never omits it), so a cluster created without one still reports "DEFAULT". @@ -305,9 +287,9 @@ func provisionedBlock(c *driver.Cluster) map[string]any { block["brokerNodeGroupInfo"] = bngToWire(c.BrokerNodeGroupInfo) } - if _, ok := c.RawOptions["encryptionInfo"]; !ok { - block["encryptionInfo"] = defaultEncryptionInfo() - } + // The provider stores the cluster's (defaulted) encryptionInfo in RawOptions; + // v2 reports it inside the provisioned block. + overlayRaw(block, c.RawOptions, "encryptionInfo", "encryptionInfo") addZookeeperConnect(block, c) From 07436a70c6b9d17f8c176d232340a2362f86776f Mon Sep 17 00:00:00 2001 From: Satyam-Trivedi-ZS Date: Sat, 24 Oct 2026 02:25:36 +0530 Subject: [PATCH 29/31] fix(aws-keyspaces): GetTable ttl/clientSideTimestamps omission and rpc-v2-cbor route (#1680) * fix(aws-keyspaces): report ttl and clientSideTimestamps only once enabled GetTable always returned ttl and clientSideTimestamps as {status: DISABLED}, even for a table that never set them. Both model enums (TimeToLiveStatus, ClientSideTimestampsStatus) hold only ENABLED, so real Keyspaces never reports DISABLED and omits the members on such a table. hashicorp/aws reads a present block into state, so a table that sets neither planned a forced replacement on every run. The provider now records "" for a setting that was never enabled (DISABLED, the documented default, maps to "" too) and GetTable emits the members only when set. An UpdateTable DISABLED no longer turns an enabled setting off, matching the docs ("can't disable it"). Restore clears the DISABLED that older builds stored on every table. * fix(aws-keyspaces): serve the rpc-v2-cbor protocol aws-sdk-go-v2/service/keyspaces v1.37.0 added Smithy RPC v2 CBOR beside AWS JSON 1.0 and prefers it, so hashicorp/aws 6.69+ sends every Keyspaces call to POST /service/KeyspacesService/operation/ with Smithy-Protocol: rpc-v2-cbor. CloudWatch claimed every rpc-v2-cbor request under /service/ and answered UnknownOperationException, so that provider could not even create a keyspace. keyspaces.CBORHandler matches only the KeyspacesService path and registers before CloudWatch. It only converts the wire format: the CBOR body is re-encoded as JSON and run through the existing JSON handler, and the answer is re-encoded as CBOR, with integral numbers as CBOR integers and the four model Timestamp members as tag 1, which the SDK requires. Errors keep their status and code as a CBOR __type/message map. Under --enforce-auth the handler is a Resolver naming cassandra: from the path, and a deny is a CBOR AccessDeniedException. * docs(aws-keyspaces): note the rpc-v2-cbor route in the SDK server docs --- docs/sdk-server.md | 6 +- providers/aws/keyspaces/keyspaces_test.go | 2 +- providers/aws/keyspaces/snapshot.go | 21 ++ .../aws/keyspaces/table_settings_test.go | 151 ++++++++ providers/aws/keyspaces/tables.go | 29 +- server/aws/authz_completeness_test.go | 2 +- server/aws/aws.go | 11 +- server/aws/keyspaces/cbor.go | 325 ++++++++++++++++++ server/aws/keyspaces/cbor_test.go | 229 ++++++++++++ server/aws/keyspaces/handler.go | 7 +- server/aws/keyspaces/tables.go | 13 +- server/aws/keyspaces/tables_test.go | 49 +++ server/aws/keyspaces_cbor_test.go | 92 +++++ 13 files changed, 922 insertions(+), 15 deletions(-) create mode 100644 providers/aws/keyspaces/table_settings_test.go create mode 100644 server/aws/keyspaces/cbor.go create mode 100644 server/aws/keyspaces/cbor_test.go create mode 100644 server/aws/keyspaces_cbor_test.go diff --git a/docs/sdk-server.md b/docs/sdk-server.md index 52d6a2013..12808989d 100644 --- a/docs/sdk-server.md +++ b/docs/sdk-server.md @@ -184,7 +184,7 @@ The tables below list the main handlers. They are not the full list: the generat | **RDS / Aurora** *(query protocol)* | DBInstances (Create/Describe/Modify/Delete/Start/Stop/Reboot), DBClusters (Create/Describe/Modify/Delete/Start/Stop), DBSnapshots + DBClusterSnapshots (Create/Describe/Delete/Restore). One handler also serves the **Neptune** and **DocumentDB** engines: both reuse the same `aws-sdk-go-v2/service/{neptune,docdb}` client surface. | | **Redshift** *(query protocol)* | CreateCluster, DescribeClusters, ModifyCluster, DeleteCluster, RebootCluster, CreateClusterSnapshot, DescribeClusterSnapshots, DeleteClusterSnapshot, RestoreFromClusterSnapshot | | **MemoryDB** *(JSON 1.1, `AmazonMemoryDB.*`)* | Clusters (Create/Describe/Update/Delete/FailoverShard/ListAllowedNodeTypeUpdates), ACLs & Users, Parameter Groups, Subnet Groups, Snapshots (Create/Describe/Copy/Delete + restore), tags, engine-version & event catalogs. Optional (type-asserted): Multi-Region clusters, Reserved Nodes. Server-side `MaxResults`/`NextToken` pagination on every Describe. | -| **Keyspaces** *(JSON 1.0, `KeyspacesService.*`)* | Keyspaces (Create/Get/List/Update/Delete, single/multi-region replication), Tables (Create/Get/List/Update/Delete/Restore: full schema, capacity, encryption, PITR, TTL, CDC), user-defined types, tags. Optional: `GetTableAutoScalingSettings`. Responses use lower-camel keys so the case-sensitive SDK deserializer decodes them; pagination on every list. | +| **Keyspaces** *(JSON 1.0, `KeyspacesService.*`, and Smithy rpc-v2-cbor)* | Keyspaces (Create/Get/List/Update/Delete, single/multi-region replication), Tables (Create/Get/List/Update/Delete/Restore: full schema, capacity, encryption, PITR, TTL, CDC), user-defined types, tags. Optional: `GetTableAutoScalingSettings`. Responses use lower-camel keys so the case-sensitive SDK deserializer decodes them; pagination on every list. | | **EKS** *(REST + JSON)* | Clusters (Create/Describe/List/UpdateConfig/UpdateVersion/Delete), NodeGroups (Create/Describe/List/UpdateConfig/UpdateVersion/Delete), Fargate Profiles (Create/Describe/List/Delete), Addons (Create/Describe/List/Update/Delete). The kubeconfig points at the shared [Kubernetes data plane](#kubernetes). | | **IAM** *(query protocol)* | Users (Create/Get/List/Delete), Roles (Create/Get/List/Delete), Policies (Create/Get/List/Delete), Attach/Detach/ListAttached for both Users and Roles, Groups (Create/Get/List/Delete + AddUserToGroup/RemoveUserFromGroup/ListGroupsForUser), AccessKeys (Create/List/Delete), InstanceProfiles (Create/Get/List/Delete + AddRoleToInstanceProfile/RemoveRoleFromInstanceProfile). Errors surface as typed `*types.NoSuchEntityException` / `*types.EntityAlreadyExistsException`. | | **Resource Explorer 2** *(JSON)* | Search: free-text plus filter expression over the cross-service inventory; results include ARN, resource type, region, owning account, and tags | @@ -302,13 +302,13 @@ Each handler matches on a different signal, so dispatch within a provider is una | AWS DynamoDB | `X-Amz-Target: DynamoDB_20120810.*` header | | AWS SQS | `X-Amz-Target: AmazonSQS.*` header | | AWS MemoryDB | `X-Amz-Target: AmazonMemoryDB.*` header | -| AWS Keyspaces | `X-Amz-Target: KeyspacesService.*` header | +| AWS Keyspaces | `X-Amz-Target: KeyspacesService.*` header, or `Smithy-Protocol: rpc-v2-cbor` on `/service/KeyspacesService/operation/*` (registered before CloudWatch) | | AWS Lambda | URL prefix `/2015-03-31/functions` | | AWS EKS | URL prefix `/clusters` | | AWS RDS | Form-encoded POST whose `Action=` is a known RDS operation (registered before EC2) | | AWS Redshift | Form-encoded POST whose `Action=` is a known Redshift operation (registered before EC2) | | AWS EC2 | `Action=โ€ฆ` in URL query or `Content-Type: application/x-www-form-urlencoded` POST | -| AWS CloudWatch | `Smithy-Protocol: rpc-v2-cbor` header | +| AWS CloudWatch | `Smithy-Protocol: rpc-v2-cbor` header (any other `/service/` path) | | AWS Bedrock | URL prefix `/foundation-models`, `/model-customization-jobs`, `/custom-models`, `/guardrails`, `/provisioned-model-throughput`, `/logging/modelinvocations`, `/tagResource`, `/untagResource`, `/listTagsForResource`, `/model-import-jobs`, `/model-copy-jobs`, `/evaluation-jobs`, `/evaluation-job/`, `/inference-profiles`, `/prompt-routers`, `/marketplace-model/endpoints`, `/automated-reasoning-policies`, `/create-foundation-model-agreement`, `/delete-foundation-model-agreement`, `/list-foundation-model-agreement-offers/`, `/foundation-model-availability/`, or bedrock-runtime `/model/{id}/{invoke,converse,converse-stream,invoke-with-response-stream,count-tokens}`, `/guardrail/{id}/version/{version}/apply`, and `/async-invoke` | | AWS Bedrock Agent | Runtime routes are registered before the control plane and the query-protocol fallback, so they match whatever the Content-Type (except the roots that are also valid S3 bucket names, `/sessions`, `/rerank` and `/optimize-prompt`, which are claimed only for requests signed for `bedrock` or unsigned JSON requests): `/agents/{id}/agentAliases/{a}/sessions/{s}/text` (InvokeAgent), POST `/agents/{sessionId}` without a trailing slash (InvokeInlineAgent), `/agents/{id}/agentAliases/{a}/memories`, `/knowledgebases/{id}/retrieve`, `/knowledgebases/{id}/datasources/{ds}/documents/{doc}/content`, `/retrieveAndGenerate[Stream]`, `/rerank`, `/generateQuery`, `/optimize-prompt`, `/agenticRetrieveStream`, `/sessions...`, `/flows/{id}/aliases/{a}[/executions...]`, `/flows/{id}/executions`, and `/tags/{bedrock session ARN}`. Everything else under `/agents`, `/knowledgebases`, `/flows`, `/prompts` is the control plane | | AWS S3 | Fallback (everything else REST-shaped) | diff --git a/providers/aws/keyspaces/keyspaces_test.go b/providers/aws/keyspaces/keyspaces_test.go index aa1135c89..45381c274 100644 --- a/providers/aws/keyspaces/keyspaces_test.go +++ b/providers/aws/keyspaces/keyspaces_test.go @@ -145,7 +145,7 @@ func TestTableLifecycleAndValidation(t *testing.T) { t.Fatalf("CreateTable: %v", err) } - // Default capacity mode is PAY_PER_REQUEST; PITR/TTL default DISABLED. + // Default capacity mode is PAY_PER_REQUEST; PITR defaults to DISABLED. if tbl.CapacitySpecification.ThroughputMode != ksdriver.ThroughputPayPerRequest { t.Fatalf("default throughput mode: %+v", tbl.CapacitySpecification) } diff --git a/providers/aws/keyspaces/snapshot.go b/providers/aws/keyspaces/snapshot.go index 9410446c8..441c00775 100644 --- a/providers/aws/keyspaces/snapshot.go +++ b/providers/aws/keyspaces/snapshot.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/stackshy/cloudemu/v2/internal/snapshot" + ksdriver "github.com/stackshy/cloudemu/v2/services/keyspaces/driver" ) var _ snapshot.Snapshottable = (*Mock)(nil) @@ -86,6 +87,10 @@ func (m *Mock) Restore(_ context.Context, data json.RawMessage) error { } } + m.mu.Lock() + m.normalizeRestoredTablesLocked() + m.mu.Unlock() + if snap.Tags != nil { m.mu.Lock() m.tags = m.canonicalTagsLocked(snap.Tags) @@ -95,6 +100,22 @@ func (m *Mock) Restore(_ context.Context, data json.RawMessage) error { return nil } +// normalizeRestoredTablesLocked clears the ttl and clientSideTimestamps +// status of restored tables that never enabled them. An older build stored +// DISABLED for both on every table, which GetTable then reported although real +// Keyspaces omits the members (see enabledOnly). The caller holds the write +// lock, and the tables store is already restored. +func (m *Mock) normalizeRestoredTablesLocked() { + for _, key := range m.tables.Keys() { + m.tables.Update(key, func(t ksdriver.Table) ksdriver.Table { + t.TTLStatus = enabledOnly(t.TTLStatus) + t.ClientSideTimestamps = enabledOnly(t.ClientSideTimestamps) + + return t + }) + } +} + // canonicalTagsLocked keys restored tags by the canonical ARN of the keyspace // or table they belong to. Tag reads and writes resolve to the canonical ARN, // but an older build stored tags under whatever ARN the caller passed (for diff --git a/providers/aws/keyspaces/table_settings_test.go b/providers/aws/keyspaces/table_settings_test.go new file mode 100644 index 000000000..5c3884458 --- /dev/null +++ b/providers/aws/keyspaces/table_settings_test.go @@ -0,0 +1,151 @@ +package keyspaces + +import ( + "context" + "encoding/json" + "testing" + + ksdriver "github.com/stackshy/cloudemu/v2/services/keyspaces/driver" +) + +// The ttl and clientSideTimestamps enums hold only ENABLED, so a table that +// never enabled them records "" (not set) and GetTable omits the members. An +// explicit DISABLED is the documented default and records "" too, and once +// enabled neither setting can be turned off. +func TestTTLAndClientSideTimestampsTrackSet(t *testing.T) { + ctx := context.Background() + m := newTestMock() + mustKeyspace(t, m, "app") + + create := func(name, ttl, cst string) *ksdriver.Table { + t.Helper() + + tbl, err := m.CreateTable(ctx, ksdriver.CreateTableConfig{ + KeyspaceName: "app", Name: name, SchemaDefinition: sampleSchema(), + TTLStatus: ttl, ClientSideTimestamps: cst, + }) + if err != nil { + t.Fatalf("CreateTable %s: %v", name, err) + } + + return tbl + } + + check := func(name, wantTTL, wantCST string) { + t.Helper() + + got, err := m.GetTable(ctx, "app", name) + if err != nil { + t.Fatalf("GetTable %s: %v", name, err) + } + + if got.TTLStatus != wantTTL || got.ClientSideTimestamps != wantCST { + t.Fatalf("%s: ttl=%q cst=%q, want ttl=%q cst=%q", name, got.TTLStatus, got.ClientSideTimestamps, wantTTL, wantCST) + } + } + + create("unset", "", "") + check("unset", "", "") + + create("disabled", "DISABLED", "DISABLED") + check("disabled", "", "") + + create("enabled", "ENABLED", "ENABLED") + check("enabled", "ENABLED", "ENABLED") + + // Enabling later records the setting. + if _, err := m.UpdateTable(ctx, ksdriver.UpdateTableConfig{ + KeyspaceName: "app", Name: "unset", TTLStatus: "ENABLED", ClientSideTimestamps: "ENABLED", + }); err != nil { + t.Fatalf("UpdateTable enable: %v", err) + } + + check("unset", "ENABLED", "ENABLED") + + // DISABLED never turns an enabled setting back off. + if _, err := m.UpdateTable(ctx, ksdriver.UpdateTableConfig{ + KeyspaceName: "app", Name: "enabled", TTLStatus: "DISABLED", ClientSideTimestamps: "DISABLED", + }); err != nil { + t.Fatalf("UpdateTable disable: %v", err) + } + + check("enabled", "ENABLED", "ENABLED") +} + +// A snapshot keeps whether each setting was set, and a snapshot from an older +// build, which stored DISABLED on every table, restores as not set. +func TestSnapshotTracksTTLAndClientSideTimestamps(t *testing.T) { + ctx := context.Background() + src := newTestMock() + mustKeyspace(t, src, "app") + + for name, status := range map[string]string{"plain": "", "on": "ENABLED"} { + if _, err := src.CreateTable(ctx, ksdriver.CreateTableConfig{ + KeyspaceName: "app", Name: name, SchemaDefinition: sampleSchema(), + TTLStatus: status, ClientSideTimestamps: status, + }); err != nil { + t.Fatalf("CreateTable %s: %v", name, err) + } + } + + raw, err := src.Snapshot(ctx, false) + if err != nil { + t.Fatalf("snapshot: %v", err) + } + + legacy := legacyDisabledSnapshot(t, raw) + + for label, data := range map[string]json.RawMessage{"current": raw, "legacy": legacy} { + dst := newTestMock() + if err := dst.Restore(ctx, data); err != nil { + t.Fatalf("%s restore: %v", label, err) + } + + for name, want := range map[string]string{"plain": "", "on": "ENABLED"} { + got, err := dst.GetTable(ctx, "app", name) + if err != nil { + t.Fatalf("%s GetTable %s: %v", label, name, err) + } + + if got.TTLStatus != want || got.ClientSideTimestamps != want { + t.Fatalf("%s %s: ttl=%q cst=%q, want %q", label, name, got.TTLStatus, got.ClientSideTimestamps, want) + } + } + } +} + +// legacyDisabledSnapshot rewrites raw the way an older build stored it: every +// table that never set ttl or clientSideTimestamps carries DISABLED. +func legacyDisabledSnapshot(t *testing.T, raw json.RawMessage) json.RawMessage { + t.Helper() + + var snap keyspacesSnapshot + if err := json.Unmarshal(raw, &snap); err != nil { + t.Fatalf("parse snapshot: %v", err) + } + + var tables map[string]ksdriver.Table + if err := json.Unmarshal(snap.Tables, &tables); err != nil { + t.Fatalf("parse tables: %v", err) + } + + for k, tbl := range tables { + tbl.TTLStatus = disabledIfEmpty(tbl.TTLStatus) + tbl.ClientSideTimestamps = disabledIfEmpty(tbl.ClientSideTimestamps) + tables[k] = tbl + } + + b, err := json.Marshal(tables) + if err != nil { + t.Fatalf("encode tables: %v", err) + } + + snap.Tables = b + + out, err := json.Marshal(snap) + if err != nil { + t.Fatalf("encode snapshot: %v", err) + } + + return out +} diff --git a/providers/aws/keyspaces/tables.go b/providers/aws/keyspaces/tables.go index c97de5e36..35cd445f2 100644 --- a/providers/aws/keyspaces/tables.go +++ b/providers/aws/keyspaces/tables.go @@ -51,7 +51,26 @@ func cloneTable(in *ksdriver.Table) ksdriver.Table { // disabledIfEmpty defaults an unset status field to "DISABLED". func disabledIfEmpty(v string) string { if v == "" { - return "DISABLED" + return statusDisabled + } + + return v +} + +// statusDisabled is the not-enabled value callers may send for the TTL and +// client-side timestamps settings. +const statusDisabled = "DISABLED" + +// enabledOnly normalizes the ttl and clientSideTimestamps status. Their model +// enums (TimeToLiveStatus, ClientSideTimestampsStatus) hold only ENABLED, so +// real Keyspaces never reports DISABLED: a table that never enabled the +// setting omits the member from GetTable. "" records "not set"; DISABLED is +// the documented default and maps to "" as well. The settings cannot be +// turned off once enabled, so on UpdateTable a DISABLED request is kept as a +// no-op by orKeep. +func enabledOnly(v string) string { + if strings.EqualFold(v, statusDisabled) { + return "" } return v @@ -146,9 +165,9 @@ func (m *Mock) CreateTable(_ context.Context, cfg ksdriver.CreateTableConfig) (* CapacitySpecification: resolveCapacity(cfg.CapacitySpecification), EncryptionSpecification: resolveEncryption(cfg.EncryptionSpecification), PointInTimeRecoveryStatus: disabledIfEmpty(cfg.PointInTimeRecovery), - TTLStatus: disabledIfEmpty(cfg.TTLStatus), + TTLStatus: enabledOnly(cfg.TTLStatus), DefaultTimeToLive: cfg.DefaultTimeToLive, - ClientSideTimestamps: disabledIfEmpty(cfg.ClientSideTimestamps), + ClientSideTimestamps: enabledOnly(cfg.ClientSideTimestamps), CdcStatus: disabledIfEmpty(cfg.CdcStatus), Comment: cfg.Comment, ReplicaRegions: append([]string(nil), cfg.ReplicaRegions...), @@ -287,8 +306,8 @@ func (m *Mock) UpdateTable(_ context.Context, cfg ksdriver.UpdateTableConfig) (* } t.PointInTimeRecoveryStatus = orKeep(cfg.PointInTimeRecovery, t.PointInTimeRecoveryStatus) - t.TTLStatus = orKeep(cfg.TTLStatus, t.TTLStatus) - t.ClientSideTimestamps = orKeep(cfg.ClientSideTimestamps, t.ClientSideTimestamps) + t.TTLStatus = orKeep(enabledOnly(cfg.TTLStatus), t.TTLStatus) + t.ClientSideTimestamps = orKeep(enabledOnly(cfg.ClientSideTimestamps), t.ClientSideTimestamps) t.CdcStatus = orKeep(cfg.CdcStatus, t.CdcStatus) t.Comment = orKeep(cfg.Comment, t.Comment) diff --git a/server/aws/authz_completeness_test.go b/server/aws/authz_completeness_test.go index 3109fc553..e38a243f0 100644 --- a/server/aws/authz_completeness_test.go +++ b/server/aws/authz_completeness_test.go @@ -93,7 +93,7 @@ func TestHandlerIAMServicesMatchTable(t *testing.T) { "*athena.Handler": "athena", "*cognito.Handler": "cognito-idp", "*cognito.WellKnown": "cognito-idp", "*configservice.Handler": "config", "*wafv2.Handler": "wafv2", "*ecs.Handler": "ecs", "*ecr.Handler": "ecr", "*route53resolver.Handler": "route53resolver", "*eventbridge.Handler": "events", "*cloudwatchlogs.Handler": "logs", "*secretsmanager.Handler": "secretsmanager", - "*keyspaces.Handler": "cassandra", "*memorydb.Handler": "memorydb", "*networkfirewall.Handler": "network-firewall", + "*keyspaces.Handler": "cassandra", "*keyspaces.CBORHandler": "cassandra", "*memorydb.Handler": "memorydb", "*networkfirewall.Handler": "network-firewall", "*resourcegroupstaggingapi.Handler": "tag", "*transfer.Handler": "transfer", "*timestreamwrite.Handler": "timestream", "*healthlake.Handler": "healthlake", "*apprunner.Handler": "apprunner", "*globalaccelerator.Handler": "globalaccelerator", "*costexplorer.Handler": "ce", "*servicequotas.Handler": "servicequotas", "*emr.Handler": "elasticmapreduce", diff --git a/server/aws/aws.go b/server/aws/aws.go index ffe0533fe..a861a308b 100644 --- a/server/aws/aws.go +++ b/server/aws/aws.go @@ -587,7 +587,8 @@ func NewFromProvider(p *awsprovider.Provider) *server.Server { // non-nil driver in d. Handlers are registered most-specific-first so the // dispatch is unambiguous: // -// - CloudWatch matches on Smithy-Protocol: rpc-v2-cbor header. +// - Keyspaces rpc-v2-cbor matches its own /service/KeyspacesService/ path; +// CloudWatch then matches any other Smithy-Protocol: rpc-v2-cbor request. // - DynamoDB matches on X-Amz-Target header (JSON-RPC). // - RDS matches form-encoded POSTs whose Action is one of the known RDS // operations. It must register before EC2 because both speak the AWS @@ -658,6 +659,14 @@ func newServer(d Drivers) (*server.Server, authzSets) { srv.Register(bedrockagentruntime.New(d.BedrockAgentRuntime)) } + // Keyspaces rpc-v2-cbor (aws-sdk-go-v2/service/keyspaces v1.37.0+, so + // hashicorp/aws 6.69+) matches only /service/KeyspacesService/operation/*. + // It registers before CloudWatch, which claims every other rpc-v2-cbor + // request under /service/. The AWS JSON 1.0 handler registers below. + if d.Keyspaces != nil { + srv.Register(keyspacessrv.NewCBOR(d.Keyspaces)) + } + if d.CloudWatch != nil { // The VPC driver optionally supplies derived AWS/IPAM metrics; surface // them through CloudWatch when it implements the capability. diff --git a/server/aws/keyspaces/cbor.go b/server/aws/keyspaces/cbor.go new file mode 100644 index 000000000..e2e2a3170 --- /dev/null +++ b/server/aws/keyspaces/cbor.go @@ -0,0 +1,325 @@ +package keyspaces + +import ( + "bytes" + "encoding/base64" + "encoding/json" + "errors" + "fmt" + "io" + "math" + "net/http" + "reflect" + "strings" + "time" + + "github.com/fxamacker/cbor/v2" + + "github.com/stackshy/cloudemu/v2/server/wire/awsauthz" + ksdriver "github.com/stackshy/cloudemu/v2/services/keyspaces/driver" +) + +// Smithy RPC v2 CBOR. aws-sdk-go-v2/service/keyspaces v1.37.0 added it beside +// AWS JSON 1.0 and prefers it, so hashicorp/aws 6.69+ sends every Keyspaces +// call as +// +// POST /service/KeyspacesService/operation/ +// Smithy-Protocol: rpc-v2-cbor +// Content-Type: application/cbor +// +// with a CBOR body that uses the same member names as the JSON protocol. +const ( + smithyProtocolHeader = "Smithy-Protocol" + rpcV2CBOR = "rpc-v2-cbor" + cborContentType = "application/cbor" + cborPathPrefix = "/service/KeyspacesService/operation/" + jsonContentType = "application/x-amz-json-1.0" + maxCBORBodyBytes = 1 << 20 +) + +var errNotCBORMap = errors.New("request body must be a CBOR map") + +// timestampMembers are the Timestamp members of the Keyspaces model. JSON +// carries them as epoch-second numbers; rpc-v2-cbor requires CBOR tag 1, and +// the SDK rejects an untagged number. +// +//nolint:gochecknoglobals // static protocol lookup table +var timestampMembers = map[string]bool{ + "creationTimestamp": true, + "lastModifiedTimestamp": true, + "earliestRestorableTimestamp": true, + "lastUpdateToPayPerRequestTimestamp": true, +} + +//nolint:gochecknoglobals // reused codec modes +var ( + cborDecMode = mustCBORDecMode() + cborEncMode = mustCBOREncMode() +) + +func mustCBORDecMode() cbor.DecMode { + mode, err := cbor.DecOptions{DefaultMapType: reflect.TypeOf(map[string]any(nil))}.DecMode() + if err != nil { + panic(err) + } + + return mode +} + +// mustCBOREncMode writes time.Time as tag 1 over epoch seconds, the Smithy +// timestamp encoding. +func mustCBOREncMode() cbor.EncMode { + mode, err := cbor.EncOptions{Time: cbor.TimeUnixDynamic, TimeTag: cbor.EncTagRequired}.EncMode() + if err != nil { + panic(err) + } + + return mode +} + +// CBORHandler serves the rpc-v2-cbor form of the Keyspaces API. It only +// converts the wire format: each request is re-encoded as AWS JSON 1.0 and run +// through the JSON Handler, and the answer is re-encoded as CBOR, so both +// protocols share one dispatch and one set of business rules. +type CBORHandler struct { + json *Handler +} + +// NewCBOR returns an rpc-v2-cbor Keyspaces handler backed by db. +func NewCBOR(db ksdriver.Keyspaces) *CBORHandler { + return &CBORHandler{json: New(db)} +} + +// Matches claims rpc-v2-cbor requests addressed to the KeyspacesService. +func (*CBORHandler) Matches(r *http.Request) bool { + return r.Header.Get(smithyProtocolHeader) == rpcV2CBOR && strings.HasPrefix(r.URL.Path, cborPathPrefix) +} + +// cborOperation returns the operation named in the URL path, or "". +func cborOperation(r *http.Request) string { + op := strings.TrimPrefix(r.URL.Path, cborPathPrefix) + if strings.Contains(op, "/") { + return "" + } + + return op +} + +// ServeHTTP converts the CBOR request to JSON, runs the JSON handler, and +// converts its response back to CBOR. +func (h *CBORHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + op := cborOperation(r) + if op == "" { + writeCBORError(w, http.StatusBadRequest, "ValidationException", "missing operation in path") + return + } + + raw, err := io.ReadAll(http.MaxBytesReader(w, r.Body, maxCBORBodyBytes)) + if err != nil { + writeCBORError(w, http.StatusBadRequest, "SerializationException", "cannot read body: "+err.Error()) + return + } + + body, err := cborToJSON(raw) + if err != nil { + writeCBORError(w, http.StatusBadRequest, "SerializationException", "invalid CBOR: "+err.Error()) + return + } + + jr := r.Clone(r.Context()) + jr.Body = io.NopCloser(bytes.NewReader(body)) + jr.ContentLength = int64(len(body)) + jr.Header.Set("X-Amz-Target", targetPrefix+op) + jr.Header.Set("Content-Type", jsonContentType) + + rec := &bufferedResponse{header: http.Header{}, status: http.StatusOK} + h.json.ServeHTTP(rec, jr) + + writeCBORFromJSON(w, rec) +} + +// IAMService returns the IAM service prefix of the operations this handler +// serves. +func (*CBORHandler) IAMService() string { return iamService } + +// IAMChecks names the IAM action of a request from the operation in the URL +// path, the same value ServeHTTP dispatches on. The resource is unknown, as for +// the JSON protocol. +func (h *CBORHandler) IAMChecks(r *http.Request, _ awsauthz.Scope) ([]awsauthz.Check, bool) { + op := cborOperation(r) + if op == "" { + return nil, false + } + + return awsauthz.Single(h.IAMService()+":"+op, ""), true +} + +// WriteAccessDenied writes the authorization 403 as an rpc-v2-cbor error. +func (*CBORHandler) WriteAccessDenied(w http.ResponseWriter, _ *http.Request, msg string) { + writeCBORError(w, http.StatusForbidden, "AccessDeniedException", msg) +} + +// bufferedResponse captures the JSON handler's response for re-encoding. +type bufferedResponse struct { + header http.Header + status int + body bytes.Buffer +} + +func (b *bufferedResponse) Header() http.Header { return b.header } +func (b *bufferedResponse) Write(p []byte) (int, error) { return b.body.Write(p) } +func (b *bufferedResponse) WriteHeader(status int) { b.status = status } + +// writeCBORFromJSON re-encodes a captured JSON response as rpc-v2-cbor. An +// error keeps its status and carries __type and message, as the Smithy +// protocol expects. +func writeCBORFromJSON(w http.ResponseWriter, rec *bufferedResponse) { + dec := json.NewDecoder(&rec.body) + dec.UseNumber() + + var decoded any + if err := dec.Decode(&decoded); err != nil { + writeCBORError(w, http.StatusInternalServerError, "InternalServerException", "cannot re-encode response: "+err.Error()) + return + } + + if rec.status >= http.StatusBadRequest { + obj, _ := decoded.(map[string]any) + errType, _ := obj["__type"].(string) + msg, _ := obj["Message"].(string) + writeCBORError(w, rec.status, errType, msg) + + return + } + + payload, err := jsonValueToCBOR("", decoded) + if err != nil { + writeCBORError(w, http.StatusInternalServerError, "InternalServerException", err.Error()) + return + } + + writeCBOR(w, http.StatusOK, payload) +} + +func writeCBORError(w http.ResponseWriter, status int, errType, msg string) { + writeCBOR(w, status, map[string]any{"__type": errType, "message": msg}) +} + +func writeCBOR(w http.ResponseWriter, status int, payload any) { + body, err := cborEncMode.Marshal(payload) + if err != nil { + status = http.StatusInternalServerError + body, _ = cborEncMode.Marshal(map[string]any{"__type": "InternalServerException", "message": err.Error()}) + } + + w.Header().Set(smithyProtocolHeader, rpcV2CBOR) + w.Header().Set("Content-Type", cborContentType) + w.WriteHeader(status) + _, _ = w.Write(body) +} + +// jsonValueToCBOR converts a decoded JSON response value for CBOR encoding: +// integral numbers become integers (the SDK reads Integer and Long members +// only from CBOR integers), other numbers float64, and the Timestamp members +// time.Time so they encode with tag 1. key is the member name v belongs to. +func jsonValueToCBOR(key string, v any) (any, error) { + switch t := v.(type) { + case map[string]any: + out := make(map[string]any, len(t)) + + for k, e := range t { + c, err := jsonValueToCBOR(k, e) + if err != nil { + return nil, err + } + + out[k] = c + } + + return out, nil + case []any: + out := make([]any, len(t)) + + for i, e := range t { + c, err := jsonValueToCBOR(key, e) + if err != nil { + return nil, err + } + + out[i] = c + } + + return out, nil + case json.Number: + return jsonNumberToCBOR(key, t) + default: + return v, nil + } +} + +func jsonNumberToCBOR(key string, n json.Number) (any, error) { + if timestampMembers[key] { + f, err := n.Float64() + if err != nil { + return nil, fmt.Errorf("member %s: %w", key, err) + } + + sec, frac := math.Modf(f) + + return time.Unix(int64(sec), int64(frac*float64(time.Second))).UTC(), nil + } + + if i, err := n.Int64(); err == nil { + return i, nil + } + + f, err := n.Float64() + if err != nil { + return nil, fmt.Errorf("member %s: %w", key, err) + } + + return f, nil +} + +// cborToJSON re-encodes a CBOR request body as AWS JSON 1.0: a tag-1 +// timestamp becomes epoch seconds and a byte string base64, the JSON forms of +// those Smithy types. An empty body is an empty input structure. +func cborToJSON(raw []byte) ([]byte, error) { + if len(raw) == 0 { + return []byte("{}"), nil + } + + var v any + if err := cborDecMode.Unmarshal(raw, &v); err != nil { + return nil, err + } + + if _, ok := v.(map[string]any); !ok { + return nil, fmt.Errorf("%w, got %T", errNotCBORMap, v) + } + + return json.Marshal(cborValueToJSON(v)) +} + +func cborValueToJSON(v any) any { + switch t := v.(type) { + case map[string]any: + for k, e := range t { + t[k] = cborValueToJSON(e) + } + + return t + case []any: + for i, e := range t { + t[i] = cborValueToJSON(e) + } + + return t + case time.Time: + return float64(t.UnixNano()) / float64(time.Second) + case []byte: + return base64.StdEncoding.EncodeToString(t) + default: + return v + } +} diff --git a/server/aws/keyspaces/cbor_test.go b/server/aws/keyspaces/cbor_test.go new file mode 100644 index 000000000..9b22c3154 --- /dev/null +++ b/server/aws/keyspaces/cbor_test.go @@ -0,0 +1,229 @@ +package keyspaces_test + +import ( + "bytes" + "io" + "net/http" + "net/http/httptest" + "testing" + "time" + + "github.com/fxamacker/cbor/v2" + + "github.com/stackshy/cloudemu/v2/config" + ksprovider "github.com/stackshy/cloudemu/v2/providers/aws/keyspaces" + ksserver "github.com/stackshy/cloudemu/v2/server/aws/keyspaces" +) + +// cborClient sends raw rpc-v2-cbor requests the way aws-sdk-go-v2 keyspaces +// v1.37+ does (see its request_snapshot/*.request.snap). +type cborClient struct { + t *testing.T + url string +} + +func newCBORClient(t *testing.T) *cborClient { + t.Helper() + + opts := config.NewOptions(config.WithRegion("us-east-1"), config.WithAccountID("123456789012")) + srv := httptest.NewServer(ksserver.NewCBOR(ksprovider.New(opts))) + t.Cleanup(srv.Close) + + return &cborClient{t: t, url: srv.URL} +} + +// call returns the status and the decoded CBOR body. Tag-1 timestamps decode +// as time.Time and integers as uint64/int64, so a test can check the types. +func (c *cborClient) call(op string, in map[string]any) (int, map[string]any) { + c.t.Helper() + + payload, err := cbor.Marshal(in) + if err != nil { + c.t.Fatalf("%s: encode: %v", op, err) + } + + req, err := http.NewRequest(http.MethodPost, c.url+"/service/KeyspacesService/operation/"+op, bytes.NewReader(payload)) + if err != nil { + c.t.Fatalf("%s: request: %v", op, err) + } + + req.Header.Set("Smithy-Protocol", "rpc-v2-cbor") + req.Header.Set("Content-Type", "application/cbor") + req.Header.Set("Accept", "application/cbor") + + resp, err := http.DefaultClient.Do(req) + if err != nil { + c.t.Fatalf("%s: do: %v", op, err) + } + defer resp.Body.Close() + + if got := resp.Header.Get("Smithy-Protocol"); got != "rpc-v2-cbor" { + c.t.Fatalf("%s: Smithy-Protocol header %q", op, got) + } + + if got := resp.Header.Get("Content-Type"); got != "application/cbor" { + c.t.Fatalf("%s: Content-Type %q", op, got) + } + + raw, err := io.ReadAll(resp.Body) + if err != nil { + c.t.Fatalf("%s: read: %v", op, err) + } + + var out map[string]any + if err := cbor.Unmarshal(raw, &out); err != nil { + c.t.Fatalf("%s: decode %x: %v", op, raw, err) + } + + return resp.StatusCode, out +} + +func (c *cborClient) ok(op string, in map[string]any) map[string]any { + c.t.Helper() + + status, out := c.call(op, in) + if status != http.StatusOK { + c.t.Fatalf("%s: status %d: %v", op, status, out) + } + + return out +} + +func cborSchema() map[string]any { + return map[string]any{ + "allColumns": []any{map[string]any{"name": "id", "type": "int"}}, + "partitionKeys": []any{map[string]any{"name": "id"}}, + } +} + +// The rpc-v2-cbor form of the API serves the same operations as AWS JSON 1.0, +// with Integer/Long members as CBOR integers and Timestamp members as tag 1. +func TestCBORTableRoundTrip(t *testing.T) { + c := newCBORClient(t) + + out := c.ok("CreateKeyspace", map[string]any{"keyspaceName": "app"}) + if out["resourceArn"] != "arn:aws:cassandra:us-east-1:123456789012:/keyspace/app/" { + t.Fatalf("CreateKeyspace resourceArn: %v", out) + } + + c.ok("CreateTable", map[string]any{ + "keyspaceName": "app", "tableName": "t1", "schemaDefinition": cborSchema(), + "capacitySpecification": map[string]any{"throughputMode": "PROVISIONED", "readCapacityUnits": 5, "writeCapacityUnits": 7}, + "pointInTimeRecovery": map[string]any{"status": "ENABLED"}, + "ttl": map[string]any{"status": "ENABLED"}, + }) + + got := c.ok("GetTable", map[string]any{"keyspaceName": "app", "tableName": "t1"}) + + if _, ok := got["creationTimestamp"].(time.Time); !ok { + t.Fatalf("creationTimestamp = %T %v, want a tag-1 timestamp", got["creationTimestamp"], got["creationTimestamp"]) + } + + capSpec, _ := got["capacitySpecification"].(map[any]any) + if capSpec["readCapacityUnits"] != uint64(5) || capSpec["writeCapacityUnits"] != uint64(7) { + t.Fatalf("capacitySpecification = %#v, want integer units 5/7", capSpec) + } + + if ttl, _ := got["ttl"].(map[any]any); ttl["status"] != "ENABLED" { + t.Fatalf("ttl = %v", got["ttl"]) + } + + if _, ok := got["clientSideTimestamps"]; ok { + t.Fatalf("clientSideTimestamps present on a table that never set it: %v", got) + } + + // A tag-1 restoreTimestamp decodes like the JSON epoch number. + c.ok("RestoreTable", map[string]any{ + "sourceKeyspaceName": "app", "sourceTableName": "t1", + "targetKeyspaceName": "app", "targetTableName": "t2", + "restoreTimestamp": time.Now().Add(-time.Minute), + }) + + list := c.ok("ListTables", map[string]any{"keyspaceName": "app"}) + if tables, _ := list["tables"].([]any); len(tables) != 2 { + t.Fatalf("ListTables = %v, want 2 tables", list) + } + + c.ok("DeleteTable", map[string]any{"keyspaceName": "app", "tableName": "t2"}) +} + +// Errors keep the JSON protocol's status and code, as a CBOR __type/message +// map. +func TestCBORErrors(t *testing.T) { + c := newCBORClient(t) + + cases := []struct { + name, op string + in map[string]any + status int + code string + }{ + {"missing table", "GetTable", map[string]any{"keyspaceName": "nope", "tableName": "t"}, http.StatusBadRequest, "ResourceNotFoundException"}, + {"bad name", "CreateKeyspace", map[string]any{"keyspaceName": "bad-name"}, http.StatusBadRequest, "ValidationException"}, + {"future restore", "RestoreTable", map[string]any{ + "sourceKeyspaceName": "a", "sourceTableName": "b", "targetKeyspaceName": "a", "targetTableName": "c", + "restoreTimestamp": time.Now().Add(time.Hour), + }, http.StatusBadRequest, "ValidationException"}, + {"unknown operation", "Frobnicate", map[string]any{}, http.StatusBadRequest, "ValidationException"}, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + status, out := c.call(tc.op, tc.in) + if status != tc.status || out["__type"] != tc.code { + t.Fatalf("status %d body %v, want %d %s", status, out, tc.status, tc.code) + } + + if msg, _ := out["message"].(string); msg == "" { + t.Fatalf("empty message: %v", out) + } + }) + } +} + +func TestCBORRejectsMalformedBody(t *testing.T) { + c := newCBORClient(t) + + req, _ := http.NewRequest(http.MethodPost, c.url+"/service/KeyspacesService/operation/ListKeyspaces", bytes.NewReader([]byte{0xff, 0x00})) + req.Header.Set("Smithy-Protocol", "rpc-v2-cbor") + req.Header.Set("Content-Type", "application/cbor") + + resp, err := http.DefaultClient.Do(req) + if err != nil { + t.Fatalf("do: %v", err) + } + defer resp.Body.Close() + + var out map[string]any + if err := cbor.NewDecoder(resp.Body).Decode(&out); err != nil { + t.Fatalf("decode: %v", err) + } + + if resp.StatusCode != http.StatusBadRequest || out["__type"] != "SerializationException" { + t.Fatalf("status %d body %v, want 400 SerializationException", resp.StatusCode, out) + } +} + +func TestCBORMatches(t *testing.T) { + h := ksserver.NewCBOR(nil) + + cases := []struct { + name, path, proto string + want bool + }{ + {"keyspaces cbor", "/service/KeyspacesService/operation/GetTable", "rpc-v2-cbor", true}, + {"cloudwatch cbor", "/service/GraniteServiceVersion20100801/operation/ListMetrics", "rpc-v2-cbor", false}, + {"keyspaces path without the protocol header", "/service/KeyspacesService/operation/GetTable", "", false}, + } + + for _, tc := range cases { + req := httptest.NewRequest(http.MethodPost, tc.path, nil) + if tc.proto != "" { + req.Header.Set("Smithy-Protocol", tc.proto) + } + + if got := h.Matches(req); got != tc.want { + t.Errorf("%s: Matches = %v, want %v", tc.name, got, tc.want) + } + } +} diff --git a/server/aws/keyspaces/handler.go b/server/aws/keyspaces/handler.go index 449912229..269ce0f53 100644 --- a/server/aws/keyspaces/handler.go +++ b/server/aws/keyspaces/handler.go @@ -14,7 +14,10 @@ import ( ksdriver "github.com/stackshy/cloudemu/v2/services/keyspaces/driver" ) -const targetPrefix = "KeyspacesService." +const ( + targetPrefix = "KeyspacesService." + iamService = "cassandra" +) // Handler serves Keyspaces requests against a keyspaces driver. type Handler struct { @@ -110,4 +113,4 @@ func wireMessage(err error) string { // IAMService returns the IAM service prefix of the operations this handler // serves. -func (*Handler) IAMService() string { return "cassandra" } +func (*Handler) IAMService() string { return iamService } diff --git a/server/aws/keyspaces/tables.go b/server/aws/keyspaces/tables.go index 7d80b49cb..cb00d72f1 100644 --- a/server/aws/keyspaces/tables.go +++ b/server/aws/keyspaces/tables.go @@ -134,10 +134,19 @@ func tableOutput(t *ksdriver.Table) keyspaces.GetTableOutput { EncryptionSpecification: toWireEncryption(t.EncryptionSpecification), DefaultTimeToLive: aws.Int32(int32(t.DefaultTimeToLive)), //nolint:gosec // mock TTL seconds never overflow int32. PointInTimeRecovery: &types.PointInTimeRecoverySummary{Status: types.PointInTimeRecoveryStatus(t.PointInTimeRecoveryStatus)}, - Ttl: &types.TimeToLive{Status: types.TimeToLiveStatus(t.TTLStatus)}, - ClientSideTimestamps: &types.ClientSideTimestamps{Status: types.ClientSideTimestampsStatus(t.ClientSideTimestamps)}, CdcSpecification: &types.CdcSpecificationSummary{Status: types.CdcStatus(t.CdcStatus)}, } + // ttl and clientSideTimestamps are reported only once enabled: their enums + // hold only ENABLED, and real Keyspaces omits them on a table that never + // set them. + if t.TTLStatus != "" { + out.Ttl = &types.TimeToLive{Status: types.TimeToLiveStatus(t.TTLStatus)} + } + + if t.ClientSideTimestamps != "" { + out.ClientSideTimestamps = &types.ClientSideTimestamps{Status: types.ClientSideTimestampsStatus(t.ClientSideTimestamps)} + } + if t.Comment != "" { out.Comment = &types.Comment{Message: aws.String(t.Comment)} } diff --git a/server/aws/keyspaces/tables_test.go b/server/aws/keyspaces/tables_test.go index 4922733c9..adb03c44f 100644 --- a/server/aws/keyspaces/tables_test.go +++ b/server/aws/keyspaces/tables_test.go @@ -118,3 +118,52 @@ func TestGetTableOmitsUnsetFields(t *testing.T) { t.Fatalf("provisioned capacity: %v", pcap) } } + +// GetTable reports ttl and clientSideTimestamps only once they are enabled, +// so a Terraform aws_keyspaces_table that sets neither reads back no block +// and plans no replacement. +func TestSDKGetTableOmitsUnsetTTLAndClientSideTimestamps(t *testing.T) { + client := newSDKClient(t) + ctx := context.Background() + + if _, err := client.CreateKeyspace(ctx, &awskeyspaces.CreateKeyspaceInput{KeyspaceName: aws.String("app")}); err != nil { + t.Fatalf("CreateKeyspace: %v", err) + } + + if _, err := client.CreateTable(ctx, &awskeyspaces.CreateTableInput{ + KeyspaceName: aws.String("app"), TableName: aws.String("plain"), SchemaDefinition: sampleSchema(), + }); err != nil { + t.Fatalf("CreateTable plain: %v", err) + } + + if _, err := client.CreateTable(ctx, &awskeyspaces.CreateTableInput{ + KeyspaceName: aws.String("app"), TableName: aws.String("on"), SchemaDefinition: sampleSchema(), + Ttl: &kstypes.TimeToLive{Status: kstypes.TimeToLiveStatusEnabled}, + ClientSideTimestamps: &kstypes.ClientSideTimestamps{Status: kstypes.ClientSideTimestampsStatusEnabled}, + }); err != nil { + t.Fatalf("CreateTable on: %v", err) + } + + plain, err := client.GetTable(ctx, &awskeyspaces.GetTableInput{KeyspaceName: aws.String("app"), TableName: aws.String("plain")}) + if err != nil { + t.Fatalf("GetTable plain: %v", err) + } + + if plain.Ttl != nil || plain.ClientSideTimestamps != nil { + t.Fatalf("plain table: ttl=%+v clientSideTimestamps=%+v, want both omitted", plain.Ttl, plain.ClientSideTimestamps) + } + + if plain.PointInTimeRecovery == nil || plain.PointInTimeRecovery.Status != kstypes.PointInTimeRecoveryStatusDisabled { + t.Fatalf("plain table pointInTimeRecovery: %+v", plain.PointInTimeRecovery) + } + + on, err := client.GetTable(ctx, &awskeyspaces.GetTableInput{KeyspaceName: aws.String("app"), TableName: aws.String("on")}) + if err != nil { + t.Fatalf("GetTable on: %v", err) + } + + if on.Ttl == nil || on.Ttl.Status != kstypes.TimeToLiveStatusEnabled || + on.ClientSideTimestamps == nil || on.ClientSideTimestamps.Status != kstypes.ClientSideTimestampsStatusEnabled { + t.Fatalf("enabled table: ttl=%+v clientSideTimestamps=%+v", on.Ttl, on.ClientSideTimestamps) + } +} diff --git a/server/aws/keyspaces_cbor_test.go b/server/aws/keyspaces_cbor_test.go new file mode 100644 index 000000000..7880c9e02 --- /dev/null +++ b/server/aws/keyspaces_cbor_test.go @@ -0,0 +1,92 @@ +package aws + +import ( + "bytes" + "net/http" + "net/http/httptest" + "testing" + + "github.com/fxamacker/cbor/v2" + + cloudemu "github.com/stackshy/cloudemu/v2" +) + +// keyspacesCBOR is a Keyspaces rpc-v2-cbor request as aws-sdk-go-v2 keyspaces +// v1.37+ (hashicorp/aws 6.69+) sends it. +func keyspacesCBOR(t *testing.T, op string, in map[string]any) sreq { + t.Helper() + + payload, err := cbor.Marshal(in) + if err != nil { + t.Fatalf("cbor: %v", err) + } + + return sreq{ + path: "/service/KeyspacesService/operation/" + op, ctype: "application/cbor", body: string(payload), + service: "cassandra", header: map[string]string{"Smithy-Protocol": "rpc-v2-cbor"}, + } +} + +func decodeCBORMap(t *testing.T, body string) map[string]any { + t.Helper() + + var out map[string]any + if err := cbor.Unmarshal([]byte(body), &out); err != nil { + t.Fatalf("decode %q: %v", body, err) + } + + return out +} + +// The full AWS server routes Keyspaces rpc-v2-cbor to Keyspaces, not to +// CloudWatch, which claims every other rpc-v2-cbor request. +func TestKeyspacesCBORDispatch(t *testing.T) { + ts := httptest.NewServer(New(DriversFrom(cloudemu.NewAWS()))) + t.Cleanup(ts.Close) + + rq := keyspacesCBOR(t, "CreateKeyspace", map[string]any{"keyspaceName": "app"}) + + req, err := http.NewRequest(http.MethodPost, ts.URL+rq.path, bytes.NewReader([]byte(rq.body))) + if err != nil { + t.Fatalf("request: %v", err) + } + + req.Header.Set("Content-Type", rq.ctype) + req.Header.Set("Smithy-Protocol", "rpc-v2-cbor") + + resp, err := http.DefaultClient.Do(req) + if err != nil { + t.Fatalf("do: %v", err) + } + defer resp.Body.Close() + + var out map[string]any + if err := cbor.NewDecoder(resp.Body).Decode(&out); err != nil { + t.Fatalf("decode: %v", err) + } + + if resp.StatusCode != http.StatusOK || out["resourceArn"] == nil { + t.Fatalf("CreateKeyspace over rpc-v2-cbor: %d %v", resp.StatusCode, out) + } +} + +// Under --enforce-auth a Keyspaces rpc-v2-cbor request is authorized as the +// cassandra: named in its path, and a deny is a CBOR error. +func TestKeyspacesCBORAuthorization(t *testing.T) { + ts, cloud := matrixServer(t, nil) + lister := userWithPolicy(t, cloud, "kslister", allow("cassandra:ListKeyspaces")) + + status, body := doSigned(t, ts, lister, keyspacesCBOR(t, "ListKeyspaces", map[string]any{})) + if status != http.StatusOK { + t.Fatalf("ListKeyspaces: %d %v", status, decodeCBORMap(t, body)) + } + + status, body = doSigned(t, ts, lister, keyspacesCBOR(t, "CreateKeyspace", map[string]any{"keyspaceName": "app"})) + if out := decodeCBORMap(t, body); status != http.StatusForbidden || out["__type"] != accessDeny { + t.Fatalf("CreateKeyspace: %d %v, want 403 %s", status, out, accessDeny) + } + + if _, err := cloud.Keyspaces.GetKeyspace(t.Context(), "app"); err == nil { + t.Fatal("a denied CreateKeyspace created the keyspace") + } +} From 7ef1708bc4306fa1fbcda6755332144001ea5386 Mon Sep 17 00:00:00 2001 From: Satyam-Trivedi-ZS Date: Sat, 24 Oct 2026 02:43:59 +0530 Subject: [PATCH 30/31] fix(aws): skip resources deleted mid-walk in every RGT discovery walker (#1681) * fix(aws): skip resources deleted mid-walk in the RGT walkers that bypass project The Kafka, Kinesis, GuardDuty and Glue walkers and the OpenSearch walker read tags in a loop of their own instead of going through project, so a resource deleted between the list call and its tag read failed the whole GetResources call for every service. Paginated walkers now collect every page with listAll and then go through project, which skips a NotFound tag read and propagates any other error. - Kafka, Kinesis and OpenSearch tag reads return NotFound for a deleted resource, so project skips them as is. - GetDetector reports a missing detector as BadRequestException, as real GuardDuty does, so the GuardDuty row re-lists detectors when GetDetector fails and reports NotFound only when the detector is gone. - Glue GetTags never fails for a deleted job, so the walk never aborted, but it emitted a row for the deleted job. The Glue row now checks the job with GetJob first. * fix(aws): skip resources deleted mid-walk in SageMaker and Redshift discovery sagemakerDiscovery and the Redshift part of rdsDiscovery list resources and then read each one's tags in a separate call. Both tag reads return NotFound for a resource that is gone, which failed the whole discovery walk (Resource Groups Tagging API GetResources and Resource Explorer) when a delete landed in between. Skip that resource, the same as project does. Any other error still propagates. * test(aws): cover deletes racing every discovery walker that reads tags separately - TestDiscoverySurvivesConcurrentDelete runs each walker 2000 times next to a create/delete loop for Kafka, Kinesis, OpenSearch, GuardDuty, Glue, SageMaker and Redshift. Before the fix every case except Glue failed within the first 512 walks. - TestTaggedWalkersSkipResourceDeletedAfterList deletes a resource after listing and projects the stale list through each walker's row function. - TestSageMakerDiscoverySkipsVanishedResource and TestRedshiftDiscoverySkipsVanishedCluster check that NotFound is skipped and any other tag-read error propagates. --- providers/aws/rds_discovery.go | 7 + providers/aws/rgt_discovery.go | 209 +++++------ providers/aws/rgt_discovery_test.go | 502 +++++++++++++++++++++++++++ providers/aws/sagemaker_discovery.go | 8 + 4 files changed, 625 insertions(+), 101 deletions(-) diff --git a/providers/aws/rds_discovery.go b/providers/aws/rds_discovery.go index 866a0cd73..544620552 100644 --- a/providers/aws/rds_discovery.go +++ b/providers/aws/rds_discovery.go @@ -133,6 +133,13 @@ func (a rdsDiscovery) discoverRedshift(ctx context.Context) ([]resourcediscovery c := clusters[i] tags, err := a.redshift.DescribeTags(ctx, c.ARN) + // NotFound means the cluster was deleted between DescribeClusters and + // this tag read. Skip it, the same as one that was never listed. Any + // other error propagates. + if cerrors.IsNotFound(err) { + continue + } + if err != nil { return nil, err } diff --git a/providers/aws/rgt_discovery.go b/providers/aws/rgt_discovery.go index cd2d207e4..41be8e688 100644 --- a/providers/aws/rgt_discovery.go +++ b/providers/aws/rgt_discovery.go @@ -2,6 +2,7 @@ package aws import ( "context" + "slices" cerrors "github.com/stackshy/cloudemu/v2/errors" "github.com/stackshy/cloudemu/v2/internal/idgen" @@ -14,9 +15,11 @@ import ( guarddutydriver "github.com/stackshy/cloudemu/v2/services/guardduty/driver" kafkadriver "github.com/stackshy/cloudemu/v2/services/kafka/driver" ksdriver "github.com/stackshy/cloudemu/v2/services/keyspaces/driver" + kinesisdriver "github.com/stackshy/cloudemu/v2/services/kinesis/driver" kmsdriver "github.com/stackshy/cloudemu/v2/services/kms/driver" mdbdriver "github.com/stackshy/cloudemu/v2/services/memorydb/driver" nfdriver "github.com/stackshy/cloudemu/v2/services/networkfirewall/driver" + opensearchdriver "github.com/stackshy/cloudemu/v2/services/opensearch/driver" ssmdriver "github.com/stackshy/cloudemu/v2/services/parameterstore/driver" "github.com/stackshy/cloudemu/v2/services/resourcediscovery" r53rdriver "github.com/stackshy/cloudemu/v2/services/route53resolver/driver" @@ -85,21 +88,14 @@ func (d taggedDiscovery) discoverOpenSearch(ctx context.Context) ([]resourcedisc return nil, err } - out := make([]resourcediscovery.DiscoveredResource, 0, len(domains)) - - for i := range domains { - name := domains[i].DomainName - arn := idgen.AWSARN("es", d.p.Region, d.p.AccountID, "domain/"+name) - - tags, err := d.p.OpenSearch.ListTags(ctx, arn) - if err != nil { - return nil, err - } + return project(ctx, domains, d.openSearchRow) +} - out = append(out, row("es", "domain", name, arn, tags)) - } +func (d taggedDiscovery) openSearchRow(ctx context.Context, dom opensearchdriver.DomainInfo) (resourcediscovery.DiscoveredResource, error) { + arn := idgen.AWSARN("es", d.p.Region, d.p.AccountID, "domain/"+dom.DomainName) + tags, err := d.p.OpenSearch.ListTags(ctx, arn) - return out, nil + return row("es", "domain", dom.DomainName, arn, tags), err } func (d taggedDiscovery) discoverSFN(ctx context.Context) ([]resourcediscovery.DiscoveredResource, error) { @@ -272,124 +268,111 @@ func (d taggedDiscovery) discoverWAFv2(ctx context.Context) ([]resourcediscovery } func (d taggedDiscovery) discoverGlue(ctx context.Context) ([]resourcediscovery.DiscoveredResource, error) { - var ( - out []resourcediscovery.DiscoveredResource - token string - ) - - for { - names, next, err := d.p.Glue.ListJobs(ctx, gluedriver.TablePagination{NextToken: token, MaxResults: pageLimit}) - if err != nil { - return nil, err - } - - for _, name := range names { - arn := idgen.AWSARN("glue", d.p.Region, d.p.AccountID, "job/"+name) + names, err := listAll(func(token string) ([]string, string, error) { + return d.p.Glue.ListJobs(ctx, gluedriver.TablePagination{NextToken: token, MaxResults: pageLimit}) + }) + if err != nil { + return nil, err + } - tags, err := d.p.Glue.GetTags(ctx, arn) - if err != nil { - return nil, err - } + return project(ctx, names, d.glueJobRow) +} - out = append(out, row("glue", "job", name, arn, tags)) - } +// glueJobRow reads a Glue job's tags. GetTags reads an ARN-keyed store and +// returns no error for a job that no longer exists, so GetJob runs first: a job +// deleted after ListJobs then reports NotFound and is skipped, instead of +// surfacing as a row for a resource that is gone. +func (d taggedDiscovery) glueJobRow(ctx context.Context, name string) (resourcediscovery.DiscoveredResource, error) { + if _, err := d.p.Glue.GetJob(ctx, name); err != nil { + return resourcediscovery.DiscoveredResource{}, err + } - if next == "" { - return out, nil - } + arn := idgen.AWSARN("glue", d.p.Region, d.p.AccountID, "job/"+name) + tags, err := d.p.Glue.GetTags(ctx, arn) - token = next - } + return row("glue", "job", name, arn, tags), err } func (d taggedDiscovery) discoverKafka(ctx context.Context) ([]resourcediscovery.DiscoveredResource, error) { - var ( - out []resourcediscovery.DiscoveredResource - token string - ) - - for { - clusters, next, err := d.p.Kafka.ListClusters(ctx, "", kafkadriver.Page{NextToken: token, MaxResults: pageLimit}) - if err != nil { - return nil, err - } - - for i := range clusters { - tags, err := d.p.Kafka.ListTagsForResource(ctx, clusters[i].ClusterARN) - if err != nil { - return nil, err - } + clusters, err := listAll(func(token string) ([]kafkadriver.Cluster, string, error) { + return d.p.Kafka.ListClusters(ctx, "", kafkadriver.Page{NextToken: token, MaxResults: pageLimit}) + }) + if err != nil { + return nil, err + } - out = append(out, row("kafka", "cluster", clusters[i].ClusterName, clusters[i].ClusterARN, tags)) - } + return project(ctx, clusters, d.kafkaRow) +} - if next == "" { - return out, nil - } +//nolint:gocritic // hugeParam: the signature is project's by-value per-item callback. +func (d taggedDiscovery) kafkaRow(ctx context.Context, c kafkadriver.Cluster) (resourcediscovery.DiscoveredResource, error) { + tags, err := d.p.Kafka.ListTagsForResource(ctx, c.ClusterARN) - token = next - } + return row("kafka", "cluster", c.ClusterName, c.ClusterARN, tags), err } func (d taggedDiscovery) discoverKinesis(ctx context.Context) ([]resourcediscovery.DiscoveredResource, error) { - var ( - out []resourcediscovery.DiscoveredResource - token string - ) - - for { + streams, err := listAll(func(token string) ([]kinesisdriver.StreamSummary, string, error) { res, err := d.p.Kinesis.ListStreams(ctx, token, "", pageLimit) if err != nil { - return nil, err + return nil, "", err } - for i := range res.StreamSummaries { - s := res.StreamSummaries[i] + if !res.HasMoreStreams { + return res.StreamSummaries, "", nil + } - tags, err := d.p.Kinesis.ListTagsForResource(ctx, s.StreamARN) - if err != nil { - return nil, err - } + return res.StreamSummaries, res.NextToken, nil + }) + if err != nil { + return nil, err + } - out = append(out, row("kinesis", "stream", s.StreamName, s.StreamARN, tags)) - } + return project(ctx, streams, d.kinesisRow) +} - if !res.HasMoreStreams || res.NextToken == "" { - return out, nil - } +//nolint:gocritic // hugeParam: the signature is project's by-value per-item callback. +func (d taggedDiscovery) kinesisRow(ctx context.Context, s kinesisdriver.StreamSummary) (resourcediscovery.DiscoveredResource, error) { + tags, err := d.p.Kinesis.ListTagsForResource(ctx, s.StreamARN) - token = res.NextToken - } + return row("kinesis", "stream", s.StreamName, s.StreamARN, tags), err } func (d taggedDiscovery) discoverGuardDuty(ctx context.Context) ([]resourcediscovery.DiscoveredResource, error) { - var ( - out []resourcediscovery.DiscoveredResource - token string - ) - - for { - ids, next, err := d.p.GuardDuty.ListDetectors(ctx, guarddutydriver.Page{NextToken: token, MaxResults: pageLimit}) - if err != nil { - return nil, err - } + ids, err := d.listGuardDutyDetectors(ctx) + if err != nil { + return nil, err + } - for _, id := range ids { - det, err := d.p.GuardDuty.GetDetector(ctx, id) - if err != nil { - return nil, err - } + return project(ctx, ids, d.guardDutyRow) +} - arn := idgen.AWSARN("guardduty", d.p.Region, d.p.AccountID, "detector/"+id) - out = append(out, row("guardduty", "detector", id, arn, det.Tags)) - } +func (d taggedDiscovery) listGuardDutyDetectors(ctx context.Context) ([]string, error) { + return listAll(func(token string) ([]string, string, error) { + return d.p.GuardDuty.ListDetectors(ctx, guarddutydriver.Page{NextToken: token, MaxResults: pageLimit}) + }) +} - if next == "" { - return out, nil +// guardDutyRow reads a detector's tags through GetDetector. Like real GuardDuty, +// GetDetector reports a detector that does not exist as BadRequestException, +// not NotFound, so the error alone can't tell a deleted detector from a real +// failure. When GetDetector fails, a fresh ListDetectors decides: if the +// detector is no longer listed it was deleted after the walk listed it, and the +// row is reported as NotFound so project skips it. Otherwise the original error +// propagates. +func (d taggedDiscovery) guardDutyRow(ctx context.Context, id string) (resourcediscovery.DiscoveredResource, error) { + det, err := d.p.GuardDuty.GetDetector(ctx, id) + if err != nil { + if ids, lerr := d.listGuardDutyDetectors(ctx); lerr == nil && !slices.Contains(ids, id) { + return resourcediscovery.DiscoveredResource{}, cerrors.Newf(cerrors.NotFound, "detector %s was deleted", id) } - token = next + return resourcediscovery.DiscoveredResource{}, err } + + arn := idgen.AWSARN("guardduty", d.p.Region, d.p.AccountID, "detector/"+id) + + return row("guardduty", "detector", id, arn, det.Tags), nil } func (d taggedDiscovery) discoverConfig(ctx context.Context) ([]resourcediscovery.DiscoveredResource, error) { @@ -447,6 +430,30 @@ func project[T any](ctx context.Context, items []T, return out, nil } +// listAll follows a NextToken-paginated list call to its last page and returns +// every item, so a paginated walker can hand the full list to project. +func listAll[T any](list func(token string) ([]T, string, error)) ([]T, error) { + var ( + all []T + token string + ) + + for { + items, next, err := list(token) + if err != nil { + return nil, err + } + + all = append(all, items...) + + if next == "" { + return all, nil + } + + token = next + } +} + // row builds a DiscoveredResource, dropping an empty tag map so a resource with // no tags surfaces with nil Tags (matching the other walkers). func row(service, typ, id, arn string, tags map[string]string) resourcediscovery.DiscoveredResource { diff --git a/providers/aws/rgt_discovery_test.go b/providers/aws/rgt_discovery_test.go index 3a1255891..a25925250 100644 --- a/providers/aws/rgt_discovery_test.go +++ b/providers/aws/rgt_discovery_test.go @@ -3,12 +3,20 @@ package aws import ( "context" "errors" + "strings" "sync" "testing" cerrors "github.com/stackshy/cloudemu/v2/errors" + gluedriver "github.com/stackshy/cloudemu/v2/services/glue/driver" + guarddutydriver "github.com/stackshy/cloudemu/v2/services/guardduty/driver" + kafkadriver "github.com/stackshy/cloudemu/v2/services/kafka/driver" ksdriver "github.com/stackshy/cloudemu/v2/services/keyspaces/driver" + kinesisdriver "github.com/stackshy/cloudemu/v2/services/kinesis/driver" + opensearchdriver "github.com/stackshy/cloudemu/v2/services/opensearch/driver" + rdbdriver "github.com/stackshy/cloudemu/v2/services/relationaldb/driver" "github.com/stackshy/cloudemu/v2/services/resourcediscovery" + sagemakerdriver "github.com/stackshy/cloudemu/v2/services/sagemaker/driver" ) // A resource deleted between the list call and its tag read makes the tag read @@ -103,3 +111,497 @@ func TestKeyspacesDiscoverySurvivesConcurrentDelete(t *testing.T) { } } } + +// discoveryChurnCase drives one discovery walker while another goroutine keeps +// creating and deleting a resource of the same service. +type discoveryChurnCase struct { + name string + // setup creates the long-lived resource the walk must keep reporting. It + // returns that resource's ID, or "" for a service that allows only one + // resource (GuardDuty has one detector per account and Region). + setup func(ctx context.Context, t *testing.T, p *Provider) string + // churn creates and deletes one short-lived resource. + churn func(ctx context.Context, p *Provider) + walk func(ctx context.Context, p *Provider) ([]resourcediscovery.DiscoveredResource, error) +} + +func discoveryChurnCases() []discoveryChurnCase { + tagged := map[string]string{"env": "prod"} + + return []discoveryChurnCase{ + { + name: "kafka", + setup: func(ctx context.Context, t *testing.T, p *Provider) string { + t.Helper() + + if _, err := p.Kafka.CreateCluster(ctx, kafkaCluster("stable", tagged)); err != nil { + t.Fatalf("CreateCluster: %v", err) + } + + return "stable" + }, + churn: func(ctx context.Context, p *Provider) { + if c, err := p.Kafka.CreateCluster(ctx, kafkaCluster("churn", nil)); err == nil { + _, _, _ = p.Kafka.DeleteCluster(ctx, c.ClusterARN, "") + } + }, + walk: func(ctx context.Context, p *Provider) ([]resourcediscovery.DiscoveredResource, error) { + return taggedDiscovery{p}.discoverKafka(ctx) + }, + }, + { + name: "kinesis", + setup: func(ctx context.Context, t *testing.T, p *Provider) string { + t.Helper() + + err := p.Kinesis.CreateStream(ctx, kinesisdriver.CreateStreamInput{StreamName: "stable", ShardCount: 1, Tags: tagged}) + if err != nil { + t.Fatalf("CreateStream: %v", err) + } + + return "stable" + }, + churn: func(ctx context.Context, p *Provider) { + if p.Kinesis.CreateStream(ctx, kinesisdriver.CreateStreamInput{StreamName: "churn", ShardCount: 1}) == nil { + _ = p.Kinesis.DeleteStream(ctx, "churn", "", true) + } + }, + walk: func(ctx context.Context, p *Provider) ([]resourcediscovery.DiscoveredResource, error) { + return taggedDiscovery{p}.discoverKinesis(ctx) + }, + }, + { + name: "opensearch", + setup: func(ctx context.Context, t *testing.T, p *Provider) string { + t.Helper() + + if _, err := p.OpenSearch.CreateDomain(ctx, openSearchDomain("stable", tagged)); err != nil { + t.Fatalf("CreateDomain: %v", err) + } + + return "stable" + }, + churn: func(ctx context.Context, p *Provider) { + if _, err := p.OpenSearch.CreateDomain(ctx, openSearchDomain("churn", nil)); err == nil { + _, _ = p.OpenSearch.DeleteDomain(ctx, "churn") + } + }, + walk: func(ctx context.Context, p *Provider) ([]resourcediscovery.DiscoveredResource, error) { + return taggedDiscovery{p}.discoverOpenSearch(ctx) + }, + }, + { + name: "guardduty", + setup: func(context.Context, *testing.T, *Provider) string { + return "" + }, + churn: func(ctx context.Context, p *Provider) { + if det, err := p.GuardDuty.CreateDetector(ctx, guarddutydriver.CreateDetectorInput{Enable: true, Tags: tagged}); err == nil { + _ = p.GuardDuty.DeleteDetector(ctx, det.ID) + } + }, + walk: func(ctx context.Context, p *Provider) ([]resourcediscovery.DiscoveredResource, error) { + return taggedDiscovery{p}.discoverGuardDuty(ctx) + }, + }, + { + name: "glue", + setup: func(ctx context.Context, t *testing.T, p *Provider) string { + t.Helper() + + if _, err := p.Glue.CreateJob(ctx, gluedriver.Job{Name: "stable", Role: "r", Tags: tagged}); err != nil { + t.Fatalf("CreateJob: %v", err) + } + + return "stable" + }, + churn: func(ctx context.Context, p *Provider) { + if _, err := p.Glue.CreateJob(ctx, gluedriver.Job{Name: "churn", Role: "r"}); err == nil { + _, _ = p.Glue.DeleteJob(ctx, "churn") + } + }, + walk: func(ctx context.Context, p *Provider) ([]resourcediscovery.DiscoveredResource, error) { + return taggedDiscovery{p}.discoverGlue(ctx) + }, + }, + { + name: "sagemaker", + setup: func(ctx context.Context, t *testing.T, p *Provider) string { + t.Helper() + + if _, err := p.SageMaker.CreateModel(ctx, sagemakerdriver.ModelConfig{ + ModelName: "stable", Tags: []sagemakerdriver.Tag{{Key: "env", Value: "prod"}}, + }); err != nil { + t.Fatalf("CreateModel: %v", err) + } + + return "stable" + }, + churn: func(ctx context.Context, p *Provider) { + if _, err := p.SageMaker.CreateModel(ctx, sagemakerdriver.ModelConfig{ModelName: "churn"}); err == nil { + _ = p.SageMaker.DeleteModel(ctx, "churn") + } + }, + walk: func(ctx context.Context, p *Provider) ([]resourcediscovery.DiscoveredResource, error) { + return sagemakerDiscovery{p.SageMaker}.DiscoverResources(ctx) + }, + }, + { + name: "redshift", + setup: func(ctx context.Context, t *testing.T, p *Provider) string { + t.Helper() + + if _, err := p.Redshift.CreateCluster(ctx, rdbdriver.ClusterConfig{ID: "stable", Tags: tagged}); err != nil { + t.Fatalf("CreateCluster: %v", err) + } + + return "stable" + }, + churn: func(ctx context.Context, p *Provider) { + if _, err := p.Redshift.CreateCluster(ctx, rdbdriver.ClusterConfig{ID: "churn"}); err == nil { + _ = p.Redshift.DeleteCluster(ctx, "churn") + } + }, + walk: func(ctx context.Context, p *Provider) ([]resourcediscovery.DiscoveredResource, error) { + dbs, err := rdsDiscovery{m: p.RDS, redshift: p.Redshift}.discoverRedshift(ctx) + + out := make([]resourcediscovery.DiscoveredResource, 0, len(dbs)) + for _, db := range dbs { + out = append(out, resourcediscovery.DiscoveredResource{ID: db.Name, ARN: db.ARN, Tags: db.Tags}) + } + + return out, err + }, + }, + } +} + +// A delete racing a discovery walk must not fail it, for every walker that +// reads tags in a call separate from its list call. The long-lived resource +// must keep surfacing with its tags on every walk. +func TestDiscoverySurvivesConcurrentDelete(t *testing.T) { + for _, tc := range discoveryChurnCases() { + t.Run(tc.name, func(t *testing.T) { + ctx := context.Background() + p := New() + stable := tc.setup(ctx, t, p) + + stop := make(chan struct{}) + + var wg sync.WaitGroup + + wg.Add(1) + + go func() { + defer wg.Done() + + for { + select { + case <-stop: + return + default: + } + + tc.churn(ctx, p) + } + }() + + defer func() { + close(stop) + wg.Wait() + }() + + for i := 0; i < 2000; i++ { + rows, err := tc.walk(ctx, p) + if err != nil { + t.Fatalf("walk %d: %v", i, err) + } + + if stable != "" && !hasTaggedRow(rows, stable) { + t.Fatalf("walk %d: %s missing or untagged: %+v", i, stable, rows) + } + } + }) + } +} + +func hasTaggedRow(rows []resourcediscovery.DiscoveredResource, id string) bool { + for _, r := range rows { + if r.ID == id && r.Tags["env"] == "prod" { + return true + } + } + + return false +} + +func kafkaCluster(name string, tags map[string]string) kafkadriver.CreateClusterInput { + return kafkadriver.CreateClusterInput{ + ClusterName: name, KafkaVersion: "3.6.0", NumberOfBrokerNodes: 2, + BrokerNodeGroupInfo: &kafkadriver.BrokerNodeGroupInfo{ + ClientSubnets: []string{"subnet-1", "subnet-2"}, InstanceType: "kafka.m5.large", + }, + Tags: tags, + } +} + +func openSearchDomain(name string, tags map[string]string) opensearchdriver.CreateDomainInput { + return opensearchdriver.CreateDomainInput{ + DomainName: name, EngineVersion: "OpenSearch_2.11", + ClusterConfig: opensearchdriver.ClusterConfig{InstanceType: "t3.small.search", InstanceCount: 1}, + Tags: tags, + } +} + +// Each paginated or non-project walker reads tags with its own row function. +// These tests take the list the walker would see, delete one resource, and +// then project that stale list through the walker's row function. The deleted +// resource must be skipped and the live one must still surface with its tags. +func TestTaggedWalkersSkipResourceDeletedAfterList(t *testing.T) { + ctx := context.Background() + tagged := map[string]string{"env": "prod"} + + t.Run("kafka", func(t *testing.T) { + p := New() + d := taggedDiscovery{p} + + if _, err := p.Kafka.CreateCluster(ctx, kafkaCluster("live", tagged)); err != nil { + t.Fatal(err) + } + + gone, err := p.Kafka.CreateCluster(ctx, kafkaCluster("gone", nil)) + mustNoErr(t, err) + + listed, _, err := p.Kafka.ListClusters(ctx, "", kafkadriver.Page{MaxResults: pageLimit}) + mustNoErr(t, err) + + _, _, err = p.Kafka.DeleteCluster(ctx, gone.ClusterARN, "") + mustNoErr(t, err) + + rows, err := project(ctx, listed, d.kafkaRow) + wantOnlyTagged(t, rows, err, "live") + }) + + t.Run("kinesis", func(t *testing.T) { + p := New() + d := taggedDiscovery{p} + + mustNoErr(t, p.Kinesis.CreateStream(ctx, kinesisdriver.CreateStreamInput{StreamName: "live", ShardCount: 1, Tags: tagged})) + mustNoErr(t, p.Kinesis.CreateStream(ctx, kinesisdriver.CreateStreamInput{StreamName: "gone", ShardCount: 1})) + + listed, err := p.Kinesis.ListStreams(ctx, "", "", pageLimit) + mustNoErr(t, err) + mustNoErr(t, p.Kinesis.DeleteStream(ctx, "gone", "", true)) + + rows, err := project(ctx, listed.StreamSummaries, d.kinesisRow) + wantOnlyTagged(t, rows, err, "live") + }) + + t.Run("opensearch", func(t *testing.T) { + p := New() + d := taggedDiscovery{p} + + if _, err := p.OpenSearch.CreateDomain(ctx, openSearchDomain("live", tagged)); err != nil { + t.Fatal(err) + } + + if _, err := p.OpenSearch.CreateDomain(ctx, openSearchDomain("gone", nil)); err != nil { + t.Fatal(err) + } + + listed, err := p.OpenSearch.ListDomainNames(ctx, "") + mustNoErr(t, err) + + if _, err := p.OpenSearch.DeleteDomain(ctx, "gone"); err != nil { + t.Fatal(err) + } + + rows, err := project(ctx, listed, d.openSearchRow) + wantOnlyTagged(t, rows, err, "live") + }) + + t.Run("glue", func(t *testing.T) { + p := New() + d := taggedDiscovery{p} + + if _, err := p.Glue.CreateJob(ctx, gluedriver.Job{Name: "live", Role: "r", Tags: tagged}); err != nil { + t.Fatal(err) + } + + if _, err := p.Glue.CreateJob(ctx, gluedriver.Job{Name: "gone", Role: "r"}); err != nil { + t.Fatal(err) + } + + listed, _, err := p.Glue.ListJobs(ctx, gluedriver.TablePagination{MaxResults: pageLimit}) + mustNoErr(t, err) + + if _, err := p.Glue.DeleteJob(ctx, "gone"); err != nil { + t.Fatal(err) + } + + rows, err := project(ctx, listed, d.glueJobRow) + wantOnlyTagged(t, rows, err, "live") + }) + + // GuardDuty allows one detector per account and Region, so there is no + // live detector to keep. The deleted one must be skipped even though + // GetDetector reports it as BadRequest, not NotFound. + t.Run("guardduty", func(t *testing.T) { + p := New() + d := taggedDiscovery{p} + + det, err := p.GuardDuty.CreateDetector(ctx, guarddutydriver.CreateDetectorInput{Enable: true, Tags: tagged}) + mustNoErr(t, err) + + listed, err := d.listGuardDutyDetectors(ctx) + mustNoErr(t, err) + + rows, err := project(ctx, listed, d.guardDutyRow) + wantOnlyTagged(t, rows, err, det.ID) + + mustNoErr(t, p.GuardDuty.DeleteDetector(ctx, det.ID)) + + if _, err := p.GuardDuty.GetDetector(ctx, det.ID); err == nil || cerrors.IsNotFound(err) { + t.Fatalf("GetDetector on a deleted detector: err = %v, want a non-NotFound error", err) + } + + rows, err = project(ctx, listed, d.guardDutyRow) + if err != nil || len(rows) != 0 { + t.Fatalf("rows = %+v, err = %v, want no rows and no error", rows, err) + } + }) +} + +// The SageMaker walker must skip a resource whose tag read reports NotFound and +// still fail on any other tag-read error. +func TestSageMakerDiscoverySkipsVanishedResource(t *testing.T) { + ctx := context.Background() + boom := errors.New("boom") + + for _, tc := range []struct { + name string + readErr error + wantErr error + }{ + {name: "not found", readErr: cerrors.Newf(cerrors.NotFound, "model gone")}, + {name: "other error", readErr: boom, wantErr: boom}, + } { + t.Run(tc.name, func(t *testing.T) { + p := New() + + for _, name := range []string{"live", "gone"} { + _, err := p.SageMaker.CreateModel(ctx, sagemakerdriver.ModelConfig{ + ModelName: name, Tags: []sagemakerdriver.Tag{{Key: "env", Value: "prod"}}, + }) + mustNoErr(t, err) + } + + m := failingTagRead{smMock: p.SageMaker, failARN: "model/gone", err: tc.readErr} + rows, err := sagemakerDiscovery{m}.DiscoverResources(ctx) + + if tc.wantErr != nil { + if !errors.Is(err, tc.wantErr) { + t.Fatalf("err = %v, want %v", err, tc.wantErr) + } + + return + } + + wantOnlyTagged(t, rows, err, "live") + }) + } +} + +// failingTagRead wraps the SageMaker mock and fails ListTags for one ARN, as if +// that resource were deleted after the walk listed it. +type failingTagRead struct { + smMock + + failARN string + err error +} + +func (f failingTagRead) ListTags(ctx context.Context, arn string) ([]sagemakerdriver.Tag, error) { + if strings.HasSuffix(arn, f.failARN) { + return nil, f.err + } + + return f.smMock.ListTags(ctx, arn) +} + +// The Redshift walker must skip a cluster whose tag read reports NotFound and +// still fail on any other tag-read error. +func TestRedshiftDiscoverySkipsVanishedCluster(t *testing.T) { + ctx := context.Background() + boom := errors.New("boom") + + for _, tc := range []struct { + name string + readErr error + wantErr error + }{ + {name: "not found", readErr: cerrors.Newf(cerrors.NotFound, "cluster gone")}, + {name: "other error", readErr: boom, wantErr: boom}, + } { + t.Run(tc.name, func(t *testing.T) { + p := New() + + for _, id := range []string{"live", "gone"} { + _, err := p.Redshift.CreateCluster(ctx, rdbdriver.ClusterConfig{ID: id, Tags: map[string]string{"env": "prod"}}) + mustNoErr(t, err) + } + + rs := failingRedshiftTags{redshiftClusters: p.Redshift, failARN: "cluster:gone", err: tc.readErr} + dbs, err := rdsDiscovery{m: p.RDS, redshift: rs}.discoverRedshift(ctx) + + if tc.wantErr != nil { + if !errors.Is(err, tc.wantErr) { + t.Fatalf("err = %v, want %v", err, tc.wantErr) + } + + return + } + + if err != nil || len(dbs) != 1 || dbs[0].Name != "live" || dbs[0].Tags["env"] != "prod" { + t.Fatalf("dbs = %+v, err = %v, want only the tagged live cluster", dbs, err) + } + }) + } +} + +// failingRedshiftTags wraps the Redshift mock and fails DescribeTags for one +// ARN, as if that cluster were deleted after DescribeClusters. +type failingRedshiftTags struct { + redshiftClusters + + failARN string + err error +} + +func (f failingRedshiftTags) DescribeTags(ctx context.Context, arn string) (map[string]string, error) { + if strings.HasSuffix(arn, f.failARN) { + return nil, f.err + } + + return f.redshiftClusters.DescribeTags(ctx, arn) +} + +func wantOnlyTagged(t *testing.T, rows []resourcediscovery.DiscoveredResource, err error, id string) { + t.Helper() + + if err != nil { + t.Fatalf("walk: %v", err) + } + + if len(rows) != 1 || !hasTaggedRow(rows, id) { + t.Fatalf("rows = %+v, want only %s tagged env=prod", rows, id) + } +} + +func mustNoErr(t *testing.T, err error) { + t.Helper() + + if err != nil { + t.Fatal(err) + } +} diff --git a/providers/aws/sagemaker_discovery.go b/providers/aws/sagemaker_discovery.go index 2f5b15380..20158f02b 100644 --- a/providers/aws/sagemaker_discovery.go +++ b/providers/aws/sagemaker_discovery.go @@ -3,6 +3,7 @@ package aws import ( "context" + cerrors "github.com/stackshy/cloudemu/v2/errors" "github.com/stackshy/cloudemu/v2/services/resourcediscovery" sagemakerdriver "github.com/stackshy/cloudemu/v2/services/sagemaker/driver" ) @@ -169,6 +170,13 @@ func (a sagemakerDiscovery) DiscoverResources( for _, it := range items { tags, err := a.liveTags(ctx, it.arn) + // NotFound means the resource was deleted between its list call and + // this tag read. Skip it, the same as one that was never listed. Any + // other error propagates. + if cerrors.IsNotFound(err) { + continue + } + if err != nil { return nil, err } From 689b30b25b41ebe56eed201baf7872b0618f126f Mon Sep 17 00:00:00 2001 From: Satyam-Trivedi-ZS Date: Sat, 24 Oct 2026 02:53:46 +0530 Subject: [PATCH 31/31] fix(aws-kms): InvalidMarkerException on list operations, key ARN in DisabledException (#1682) * fix(aws-kms): return InvalidMarkerException for an unknown Marker on list operations A Marker the server never issued now gives InvalidMarkerException on ListKeys, ListAliases, ListGrants, ListRetirableGrants, ListKeyRotations, ListResourceTags and DescribeCustomKeyStores, which all model it. ListKeyPolicies does not model it, so it keeps ValidationException. A Marker longer than 1024 characters or with a character outside U+0020-U+00FF fails the modeled MarkerType constraint and gives ValidationException on every operation. * fix(aws-kms): name the key in the DisabledException message Real KMS answers an operation on a Disabled key with " is disabled." instead of a generic "key is disabled". The error is built with driver.WithMessage, so errors.Is still matches driver.ErrKeyDisabled for Secrets Manager and other callers. Every operation that returns DisabledException goes through requireUsable, so they all get the same message. --- providers/aws/kms/crypto.go | 3 +- providers/aws/kms/key_state_test.go | 57 +++++++++++++++++ server/aws/kms/customkeystore_ops.go | 2 +- server/aws/kms/key_state_sdk_test.go | 39 ++++++++++++ server/aws/kms/management_ops.go | 8 +-- server/aws/kms/operations.go | 6 +- server/aws/kms/paginate.go | 66 ++++++++++++++++++-- server/aws/kms/pagination_test.go | 93 ++++++++++++++++++++++++++++ services/kms/driver/errors.go | 1 + 9 files changed, 261 insertions(+), 14 deletions(-) create mode 100644 providers/aws/kms/key_state_test.go create mode 100644 server/aws/kms/key_state_sdk_test.go diff --git a/providers/aws/kms/crypto.go b/providers/aws/kms/crypto.go index 62a2548d3..36a190076 100644 --- a/providers/aws/kms/crypto.go +++ b/providers/aws/kms/crypto.go @@ -160,7 +160,8 @@ func (m *Mock) requireUsable(kd *keyData) error { case driver.StateEnabled: return nil case driver.StateDisabled: - return driver.ErrKeyDisabled + // Real KMS names the key: " is disabled." + return driver.WithMessage(driver.ErrKeyDisabled, "%s is disabled.", kd.meta.ARN) default: return driver.ErrKeyInvalidState } diff --git a/providers/aws/kms/key_state_test.go b/providers/aws/kms/key_state_test.go new file mode 100644 index 000000000..3094a65cf --- /dev/null +++ b/providers/aws/kms/key_state_test.go @@ -0,0 +1,57 @@ +package kms_test + +import ( + "context" + "errors" + "testing" + + cerrors "github.com/stackshy/cloudemu/v2/errors" + "github.com/stackshy/cloudemu/v2/services/kms/driver" +) + +// TestDisabledKeyMessageNamesKey checks that every operation refused for a +// Disabled key returns DisabledException with the key ARN in the message, as +// real KMS does (" is disabled."), while errors.Is still matches +// driver.ErrKeyDisabled for callers such as Secrets Manager. +func TestDisabledKeyMessageNamesKey(t *testing.T) { + ctx := context.Background() + m := newMock(t) + + k, err := m.CreateKey(ctx, driver.CreateKeyInput{}) + if err != nil { + t.Fatalf("CreateKey: %v", err) + } + + if err := m.DisableKey(ctx, k.KeyID); err != nil { + t.Fatalf("DisableKey: %v", err) + } + + want := k.ARN + " is disabled." + + ops := map[string]func() error{ + "Encrypt": func() error { + _, err := m.Encrypt(ctx, driver.EncryptInput{KeyID: k.KeyID, Plaintext: []byte("x")}) + return err + }, + "GenerateDataKey": func() error { + _, err := m.GenerateDataKey(ctx, driver.GenerateDataKeyInput{KeyID: k.KeyID, KeySpec: "AES_256"}) + return err + }, + "EnableKeyRotation": func() error { + return m.EnableKeyRotation(ctx, k.KeyID, 0) + }, + } + + for name, call := range ops { + t.Run(name, func(t *testing.T) { + err := call() + if !errors.Is(err, driver.ErrKeyDisabled) { + t.Fatalf("err = %v, want ErrKeyDisabled", err) + } + + if got := cerrors.Message(err); got != want { + t.Fatalf("message = %q, want %q", got, want) + } + }) + } +} diff --git a/server/aws/kms/customkeystore_ops.go b/server/aws/kms/customkeystore_ops.go index 2adca2f89..a101a655b 100644 --- a/server/aws/kms/customkeystore_ops.go +++ b/server/aws/kms/customkeystore_ops.go @@ -151,7 +151,7 @@ func (h *Handler) describeCustomKeyStores(w http.ResponseWriter, r *http.Request return nil, err } - start, end, next, truncated, err := pageWindow(req.Marker, req.Limit, len(stores)) + start, end, next, truncated, err := pageWindow(req.Marker, req.Limit, len(stores), markerInvalid) if err != nil { return nil, err } diff --git a/server/aws/kms/key_state_sdk_test.go b/server/aws/kms/key_state_sdk_test.go new file mode 100644 index 000000000..dd93e2999 --- /dev/null +++ b/server/aws/kms/key_state_sdk_test.go @@ -0,0 +1,39 @@ +package kms_test + +import ( + "context" + "errors" + "testing" + + "github.com/aws/aws-sdk-go-v2/aws" + awskms "github.com/aws/aws-sdk-go-v2/service/kms" + kmstypes "github.com/aws/aws-sdk-go-v2/service/kms/types" +) + +// TestSDKDisabledExceptionMessage checks that DisabledException reaches the +// client with the message real KMS sends, " is disabled.". +func TestSDKDisabledExceptionMessage(t *testing.T) { + ctx := context.Background() + c := newKMSClient(t) + + key, err := c.CreateKey(ctx, &awskms.CreateKeyInput{}) + if err != nil { + t.Fatalf("CreateKey: %v", err) + } + + if _, err := c.DisableKey(ctx, &awskms.DisableKeyInput{KeyId: key.KeyMetadata.KeyId}); err != nil { + t.Fatalf("DisableKey: %v", err) + } + + _, err = c.Encrypt(ctx, &awskms.EncryptInput{KeyId: key.KeyMetadata.KeyId, Plaintext: []byte("x")}) + + var disabled *kmstypes.DisabledException + if !errors.As(err, &disabled) { + t.Fatalf("want DisabledException, got %v", err) + } + + want := aws.ToString(key.KeyMetadata.Arn) + " is disabled." + if got := disabled.ErrorMessage(); got != want { + t.Fatalf("message = %q, want %q", got, want) + } +} diff --git a/server/aws/kms/management_ops.go b/server/aws/kms/management_ops.go index 9de0dc325..7f8f311af 100644 --- a/server/aws/kms/management_ops.go +++ b/server/aws/kms/management_ops.go @@ -36,7 +36,7 @@ func (h *Handler) listGrants(w http.ResponseWriter, r *http.Request) { sort.Slice(grants, func(i, j int) bool { return grants[i].GrantID < grants[j].GrantID }) - start, end, next, truncated, perr := pageWindow(req.Marker, req.Limit, len(grants)) + start, end, next, truncated, perr := pageWindow(req.Marker, req.Limit, len(grants), markerInvalid) if perr != nil { return nil, perr } @@ -80,7 +80,7 @@ func (h *Handler) listRetirableGrants(w http.ResponseWriter, r *http.Request) { sort.Slice(grants, func(i, j int) bool { return grants[i].GrantID < grants[j].GrantID }) - start, end, next, truncated, perr := pageWindow(req.Marker, req.Limit, len(grants)) + start, end, next, truncated, perr := pageWindow(req.Marker, req.Limit, len(grants), markerInvalid) if perr != nil { return nil, perr } @@ -138,7 +138,7 @@ func (h *Handler) listKeyRotations(w http.ResponseWriter, r *http.Request) { return nil, err } - start, end, next, truncated, perr := pageWindow(req.Marker, req.Limit, len(events)) + start, end, next, truncated, perr := pageWindow(req.Marker, req.Limit, len(events), markerInvalid) if perr != nil { return nil, perr } @@ -201,7 +201,7 @@ func (h *Handler) listKeyPolicies(w http.ResponseWriter, r *http.Request) { sort.Strings(names) - start, end, next, truncated, perr := pageWindow(req.Marker, req.Limit, len(names)) + start, end, next, truncated, perr := pageWindow(req.Marker, req.Limit, len(names), markerValidation) if perr != nil { return nil, perr } diff --git a/server/aws/kms/operations.go b/server/aws/kms/operations.go index c7ad38656..54cb04d78 100644 --- a/server/aws/kms/operations.go +++ b/server/aws/kms/operations.go @@ -70,7 +70,7 @@ func (h *Handler) listKeys(w http.ResponseWriter, r *http.Request) { // unsorted offset Marker could repeat or skip keys across pages. sort.Slice(keys, func(i, j int) bool { return keys[i].KeyID < keys[j].KeyID }) - start, end, next, truncated, err := pageWindow(req.Marker, req.Limit, len(keys)) + start, end, next, truncated, err := pageWindow(req.Marker, req.Limit, len(keys), markerInvalid) if err != nil { writeErr(w, err) return @@ -217,7 +217,7 @@ func (h *Handler) listAliases(w http.ResponseWriter, r *http.Request) { // Sort before paging: the provider returns aliases in map-iteration order. sort.Slice(aliases, func(i, j int) bool { return aliases[i].Name < aliases[j].Name }) - start, end, next, truncated, err := pageWindow(req.Marker, req.Limit, len(aliases)) + start, end, next, truncated, err := pageWindow(req.Marker, req.Limit, len(aliases), markerInvalid) if err != nil { writeErr(w, err) return @@ -280,7 +280,7 @@ func (h *Handler) listResourceTags(w http.ResponseWriter, r *http.Request) { all := mapToTags(tags) sort.Slice(all, func(i, j int) bool { return all[i].TagKey < all[j].TagKey }) - start, end, next, truncated, err := pageWindow(req.Marker, req.Limit, len(all)) + start, end, next, truncated, err := pageWindow(req.Marker, req.Limit, len(all), markerInvalid) if err != nil { writeErr(w, err) return diff --git a/server/aws/kms/paginate.go b/server/aws/kms/paginate.go index 506a3f24f..736b5a85f 100644 --- a/server/aws/kms/paginate.go +++ b/server/aws/kms/paginate.go @@ -1,20 +1,42 @@ package kms import ( + "unicode/utf8" + cerrors "github.com/stackshy/cloudemu/v2/errors" "github.com/stackshy/cloudemu/v2/server/wire" + kmsdriver "github.com/stackshy/cloudemu/v2/services/kms/driver" ) // defaultPageLimit is the page size KMS list operations use when Limit is unset. const defaultPageLimit = 100 +// maxMarkerLen is the modeled MarkerType length limit (1-1024). +const maxMarkerLen = 1024 + +// markerFault says which exception a well-formed Marker that this server did +// not issue produces. Most KMS list operations model InvalidMarkerException; +// ListKeyPolicies does not, so it answers ValidationException. +type markerFault int + +const ( + // markerInvalid answers InvalidMarkerException. + markerInvalid markerFault = iota + // markerValidation answers ValidationException, for operations whose error + // list has no InvalidMarkerException. + markerValidation +) + // pageWindow resolves an opaque Marker + Limit into a [start,end) slice window // over a stable result set of length total. It returns the NextMarker to hand // back ("" on the last page) and whether the result was truncated. KMS keys its // pagination on Marker/Limit and echoes Truncated/NextMarker; an offset-based // marker is stable because each list op returns a deterministically ordered set. -func pageWindow(marker string, limit int32, total int) (start, end int, nextMarker string, truncated bool, err error) { - start, err = decodeMarker(marker) +// fault picks the exception for a Marker that decodes to no position. +func pageWindow( + marker string, limit int32, total int, fault markerFault, +) (start, end int, nextMarker string, truncated bool, err error) { + start, err = decodeMarker(marker, fault) if err != nil { return 0, 0, "", false, err } @@ -36,11 +58,45 @@ func pageWindow(marker string, limit int32, total int) (start, end int, nextMark return start, end, wire.EncodeOffset(end), true, nil } -func decodeMarker(marker string) (int, error) { +// decodeMarker turns a Marker back into an offset. A Marker that breaks the +// modeled MarkerType constraints (length up to 1024, characters U+0020-U+00FF) +// fails request validation with ValidationException on every operation. A +// Marker that passes them but was not issued by this server is +// InvalidMarkerException where the operation models it. +func decodeMarker(marker string, fault markerFault) (int, error) { + if err := validateMarkerConstraint(marker); err != nil { + return 0, err + } + n, err := wire.DecodeOffset(marker) - if err != nil { + if err == nil { + return n, nil + } + + if fault == markerValidation { return 0, cerrors.New(cerrors.InvalidArgument, "invalid Marker") } - return n, nil + return 0, kmsdriver.NewAPIError(kmsdriver.ExInvalidMarker, cerrors.InvalidArgument, + "The request was rejected because the marker that specifies where pagination should next begin is not valid.") +} + +// validateMarkerConstraint enforces the MarkerType length and pattern, with +// the message AWS returns for a failed constraint. +func validateMarkerConstraint(marker string) error { + const prefix = "1 validation error detected: Value '%s' at 'marker' failed to satisfy constraint: " + + if utf8.RuneCountInString(marker) > maxMarkerLen { + return cerrors.Newf(cerrors.InvalidArgument, + prefix+"Member must have length less than or equal to %d", marker, maxMarkerLen) + } + + for _, r := range marker { + if r < 0x20 || r > 0xFF { + return cerrors.Newf(cerrors.InvalidArgument, + prefix+"Member must satisfy regular expression pattern: [\\u0020-\\u00FF]*", marker) + } + } + + return nil } diff --git a/server/aws/kms/pagination_test.go b/server/aws/kms/pagination_test.go index 5980d4a1c..8cee8b434 100644 --- a/server/aws/kms/pagination_test.go +++ b/server/aws/kms/pagination_test.go @@ -2,10 +2,14 @@ package kms_test import ( "context" + "errors" + "strings" "testing" "github.com/aws/aws-sdk-go-v2/aws" awskms "github.com/aws/aws-sdk-go-v2/service/kms" + kmstypes "github.com/aws/aws-sdk-go-v2/service/kms/types" + "github.com/aws/smithy-go" ) // TestSDKListKeysPaginationDeterministic walks ListKeys one key per page and @@ -53,3 +57,92 @@ func TestSDKListKeysPaginationDeterministic(t *testing.T) { } } } + +// TestSDKListBadMarker sends a Marker the server never issued to every KMS list +// operation. Operations that model InvalidMarkerException return it; only +// ListKeyPolicies, whose error list has no InvalidMarkerException, answers +// ValidationException. A Marker over the modeled 1024-character limit, or with +// a character outside U+0020-U+00FF, fails request validation with +// ValidationException everywhere. +func TestSDKListBadMarker(t *testing.T) { + ctx := context.Background() + c := newKMSClient(t) + + key, err := c.CreateKey(ctx, &awskms.CreateKeyInput{}) + if err != nil { + t.Fatalf("CreateKey: %v", err) + } + + keyID := key.KeyMetadata.KeyId + + if _, err := c.CreateGrant(ctx, &awskms.CreateGrantInput{ + KeyId: keyID, + GranteePrincipal: aws.String("arn:aws:iam::111122223333:role/R"), + RetiringPrincipal: aws.String("arn:aws:iam::111122223333:role/R"), + Operations: []kmstypes.GrantOperation{kmstypes.GrantOperationEncrypt}, + }); err != nil { + t.Fatalf("CreateGrant: %v", err) + } + + ops := []struct { + name string + call func(marker *string) error + want string + }{ + {"ListKeys", func(m *string) error { + _, err := c.ListKeys(ctx, &awskms.ListKeysInput{Marker: m}) + return err + }, "InvalidMarkerException"}, + {"ListAliases", func(m *string) error { + _, err := c.ListAliases(ctx, &awskms.ListAliasesInput{Marker: m}) + return err + }, "InvalidMarkerException"}, + {"ListGrants", func(m *string) error { + _, err := c.ListGrants(ctx, &awskms.ListGrantsInput{KeyId: keyID, Marker: m}) + return err + }, "InvalidMarkerException"}, + {"ListRetirableGrants", func(m *string) error { + _, err := c.ListRetirableGrants(ctx, &awskms.ListRetirableGrantsInput{ + RetiringPrincipal: aws.String("arn:aws:iam::111122223333:role/R"), Marker: m, + }) + return err + }, "InvalidMarkerException"}, + {"ListKeyRotations", func(m *string) error { + _, err := c.ListKeyRotations(ctx, &awskms.ListKeyRotationsInput{KeyId: keyID, Marker: m}) + return err + }, "InvalidMarkerException"}, + {"ListResourceTags", func(m *string) error { + _, err := c.ListResourceTags(ctx, &awskms.ListResourceTagsInput{KeyId: keyID, Marker: m}) + return err + }, "InvalidMarkerException"}, + {"DescribeCustomKeyStores", func(m *string) error { + _, err := c.DescribeCustomKeyStores(ctx, &awskms.DescribeCustomKeyStoresInput{Marker: m}) + return err + }, "InvalidMarkerException"}, + {"ListKeyPolicies", func(m *string) error { + _, err := c.ListKeyPolicies(ctx, &awskms.ListKeyPoliciesInput{KeyId: keyID, Marker: m}) + return err + }, "ValidationException"}, + } + + for _, op := range ops { + t.Run(op.name, func(t *testing.T) { + assertErrorCode(t, op.call(aws.String("not-a-marker")), op.want) + assertErrorCode(t, op.call(aws.String(strings.Repeat("a", 1025))), "ValidationException") + assertErrorCode(t, op.call(aws.String("bad\u0100marker")), "ValidationException") + }) + } +} + +func assertErrorCode(t *testing.T, err error, want string) { + t.Helper() + + var apiErr smithy.APIError + if !errors.As(err, &apiErr) { + t.Fatalf("want %s, got %v", want, err) + } + + if apiErr.ErrorCode() != want { + t.Fatalf("want %s, got %s: %s", want, apiErr.ErrorCode(), apiErr.ErrorMessage()) + } +} diff --git a/services/kms/driver/errors.go b/services/kms/driver/errors.go index 867d8c5fa..bfd6f5510 100644 --- a/services/kms/driver/errors.go +++ b/services/kms/driver/errors.go @@ -12,6 +12,7 @@ import ( const ( ExTag = "TagException" ExInvalidArn = "InvalidArnException" + ExInvalidMarker = "InvalidMarkerException" ExCustomKeyStoreNotFound = "CustomKeyStoreNotFoundException" ExCustomKeyStoreNameInUse = "CustomKeyStoreNameInUseException" ExCustomKeyStoreInvalidState = "CustomKeyStoreInvalidStateException"