Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.
Python awsaws-emulatoraws-localaws-sdkdevtoolsdockerdynamodbec2emulatorlambdalocalstacklocalstack-alternativeministackmock-awsopen-sourcepythons3sqsterraform
feat(signer): sign IoT-platform jobs with the profile's ACM key (#2114)
iot-rocket0f5394055fda
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 2437754b..a075ff39 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -7,6 +7,8 @@ Versioning follows [Semantic Versioning](https://semver.org/).## [Unreleased]+### Added+- **Signer — real signatures on the IoT platform** — when ACM holds the key of the profile's certificate, `StartSigningJob` on `AWSIoTDeviceManagement-SHA256-ECDSA` writes AWS's signed document instead of a JSON receipt, and an RSA key fails the job. Without a key the receipt stays and a warning is logged. Contributed by @iot-rocket.### Fixed- **Error envelope Content-Types now match AWS per protocol** — real AWS picks an error's `Content-Type` from the service's protocol, and services disagree: `application/x-amz-json-1.1` for jsonVersion-1.1 services (Kinesis, KMS, Secrets Manager, Athena, Cognito, EventBridge, WAFv2, CloudWatch Logs) vs `1.0` (DynamoDB, Step Functions, SQS); `text/xml` for query services (IAM, SNS, RDS, CloudFormation) and `text/xml;charset=UTF-8` for EC2; `application/json` for rest-json (Lambda, AppSync, API Gateway) vs `application/x-amz-json-1.1` for SESv2; `text/xml` for rest-xml except S3's `application/xml`. Errors are now normalized at dispatch using the botocore service model plus wire-captured quirks. Evidence: real AWS wire captures diffed per probe (7/28 → 23/28 probes conforming).- **Unimplemented services no longer answered by S3** — a request whose SigV4 credential scope names a real AWS service MiniStack does not implement (e.g. Glacier, Pinpoint, Route53 Resolver) used to fall through to the S3 path catch-all and return `NoSuchBucket`. It now surfaces as `Unsupported service: <name>`. Evidence: same conformance run — `describe_vault`/`get_app` previously returned an S3 error body.diff --git a/README.md b/README.mdindex f4974292..98f34cae 100644--- a/README.md+++ b/README.md@@ -653,7 +653,7 @@ MiniStack expands AWS SAM transforms (`Transform: AWS::Serverless-2016-10-31`) i| **Organizations** | DescribeOrganization, ListRoots, ListAccounts, ListAccountsForParent, DescribeAccount, CreateAccount, DescribeCreateAccountStatus, MoveAccount, CloseAccount, CreateOrganizationalUnit, DescribeOrganizationalUnit, DeleteOrganizationalUnit, ListOrganizationalUnitsForParent, ListParents, CreatePolicy, DescribePolicy, UpdatePolicy, DeletePolicy, ListPolicies, AttachPolicy, DetachPolicy, ListPoliciesForTarget, ListTargetsForPolicy, EnablePolicyType, DisablePolicyType, TagResource, UntagResource, ListTagsForResource | Single-master-account org auto-initialised on first call; `aws_organizations_account`, `aws_organizations_policy` and `aws_organizations_policy_attachment` apply — `CreateAccount` answers with a `CreateAccountStatus` read back through `DescribeCreateAccountStatus`, the root carries `SERVICE_CONTROL_POLICY` enabled plus the AWS-managed `p-FullAWSAccess`, attaching a policy whose type the root has disabled is `PolicyTypeNotEnabledException` and deleting an attached one is `PolicyInUseException`; nested OUs with `Path` field (additive 2026-03 AWS change); the `aws_organizations_organizational_unit` Read calls `ListParents` (single parent, `ROOT`/`ORGANIZATIONAL_UNIT`, to populate `parent_id`) **and** `ListTagsForResource` on every refresh, so both — plus `TagResource`/`UntagResource` — round-trip and a multi-OU hierarchy applies cleanly + idempotently; `FeatureSet=ALL` || **Account** | GetAccountInformation, GetContactInformation, ListRegions, GetRegionOptStatus | rest-json `/getAccountInformation`, etc.; returns `AccountState=ACTIVE` (additive 2026-04 AWS change); 31-region opt-in matrix || **WAF (Classic + Regional)** | List* (17 ops), Get*, GetChangeToken, GetChangeTokenStatus, GetPermissionPolicy, Create*/Update*/Delete* (stubbed) | Minimal v1 stub — empty lists for all `List*`, valid ChangeToken on Create/Update/Delete; for full features use **wafv2** (also supported) |-| **Signer** | StartSigningJob, DescribeSigningJob, ListSigningJobs, PutSigningProfile, GetSigningProfile, CancelSigningProfile, AddProfilePermission, ListProfilePermissions, RemoveProfilePermission | **Signing is synchronous and synthetic**: `StartSigningJob` validates the source object against the local S3 store and writes a JSON signature marker (source reference + SHA-256 of the source bytes — not real cryptography) to `destination.s3.bucketName` at key `prefix + jobId` **before returning**, so the job is already `Succeeded` and a caller whose contract is the signed object at `prefix + jobId` never polls `DescribeSigningJob` (`.zip` is appended for a `.zip` source on `AWSLambda-SHA384-ECDSA`, as measured on AWS). A missing source object or destination bucket is `ResourceNotFoundException` at Start with **no job recorded** (there is no async pipeline, so nothing ever lists as `Failed`); an unknown `profileName` is `ResourceNotFoundException` like on AWS, so create profiles with `PutSigningProfile` first. `clientRequestToken` replays the first response. `source.s3.version` is required by the service model, but an absent value (what `head_object` reports on an unversioned bucket) reads the current object and records the version id S3 holds for it, which is `"null"` only on a bucket without versioning; a literal `"null"` reads the stored `"null"` version when one exists, falling back to the current object. The job carries the profile's `signingMaterial`, `overrides`, `signingParameters` and a `signatureExpiresAt` from its validity period (default 135 months). `ListSigningJobs` filters on `status`, `isRevoked`, `platformId`, `requestedBy` and `jobInvoker`, and pages: `maxResults` (1..25) limits the page and a `nextToken` carries the sort position of the last job, so a paginator reaches every job; without `maxResults` the whole list comes back in one page |+| **Signer** | StartSigningJob, DescribeSigningJob, ListSigningJobs, PutSigningProfile, GetSigningProfile, CancelSigningProfile, AddProfilePermission, ListProfilePermissions, RemoveProfilePermission | **Signing is synchronous**: `StartSigningJob` validates the source object against the local S3 store and writes the signed object to `destination.s3.bucketName` at key `prefix + jobId` **before returning**, so the job is already `Succeeded` and a caller whose contract is the signed object at `prefix + jobId` never polls `DescribeSigningJob` (`.zip` is appended for a `.zip` source on `AWSLambda-SHA384-ECDSA`, as measured on AWS). On `AWSIoTDeviceManagement-SHA256-ECDSA`, when ACM holds the private key of the profile's certificate, the signed object is the JSON AWS writes, with an ECDSA SHA-256 signature over the source bytes that the certificate verifies, and an RSA key fails the job; otherwise it is a JSON receipt (source reference + SHA-256 of the source bytes) and a warning is logged where AWS signs with the certificate (imported keys are not persisted, so re-import after a warm boot). A missing source object or destination bucket is `ResourceNotFoundException` at Start with **no job recorded** (there is no async pipeline that could fail later); an unknown `profileName` is `ResourceNotFoundException` like on AWS, so create profiles with `PutSigningProfile` first. `clientRequestToken` replays the first response. `source.s3.version` is required by the service model, but an absent value (what `head_object` reports on an unversioned bucket) reads the current object and records the version id S3 holds for it, which is `"null"` only on a bucket without versioning; a literal `"null"` reads the stored `"null"` version when one exists, falling back to the current object. The job carries the profile's `signingMaterial`, `overrides`, `signingParameters` and a `signatureExpiresAt` from its validity period (default 135 months). `ListSigningJobs` filters on `status`, `isRevoked`, `platformId`, `requestedBy` and `jobInvoker`, and pages: `maxResults` (1..25) limits the page and a `nextToken` carries the sort position of the last job, so a paginator reaches every job; without `maxResults` the whole list comes back in one page || **Batch** | CreateComputeEnvironment, UpdateComputeEnvironment, DescribeComputeEnvironments, CreateJobQueue, DescribeJobQueues, RegisterJobDefinition, DescribeJobDefinitions, SubmitJob, DescribeJobs, ListJobs | Control-plane stub — submitted jobs immediately transition to `SUCCEEDED`; multi-revision job definitions; `jobQueue` lookup by name or ARN; account-scoped state |---diff --git a/ministack/services/signer.py b/ministack/services/signer.pyindex bcb24200..fd6d96d4 100644--- a/ministack/services/signer.py+++ b/ministack/services/signer.py@@ -48,16 +48,22 @@ Behaviour that follows the live service (measured 2026-08-26 unless noted):Deliberate divergences from AWS, each pinned by a test:- * Signing is SYNCHRONOUS and SYNTHETIC. Real Signer queues an async job;- here StartSigningJob validates the source object against MiniStack's own- S3 store in-process, writes a JSON signature marker (source reference +- SHA-256 of the source bytes, not real cryptography) to+ * Signing is SYNCHRONOUS. Real Signer queues an async job; here+ StartSigningJob validates the source object against MiniStack's own S3+ store in-process, writes the signed object to`destination.s3.bucketName`, and returns with the job already`Succeeded`. Callers whose contract is the S3 side effect work without- polling DescribeSigningJob; the signature BYTES are not a real signature.+ polling DescribeSigningJob.+ * On AWSIoTDeviceManagement-SHA256-ECDSA, when ACM holds the private+ key of the profile's certificate, the job signs the source bytes with+ it and writes the JSON document AWS writes; a non-EC key fails the job+ as on AWS. Otherwise the signed object is a JSON receipt (source+ reference + SHA-256 of the source bytes), which is not a signature, and+ a warning is logged when AWS would have signed with the certificate.* A missing source object or destination bucket fails at Start withResourceNotFoundException and records NO job — there is no async- pipeline that could fail later, so nothing ever lists as `Failed`.+ pipeline that could fail later. The only job that lists as `Failed` is+ the non-EC key above.* ListSigningJobs pages: `maxResults` (1..25) limits the page and a`nextToken` carries the sort position of the last job returned, so apaginator walks the whole list. `status`, `isRevoked`,@@ -89,6 +95,7 @@ import urllib.parseimport uuidfrom datetime import datetime, timedelta, timezone+import ministack.services.acm as acm_svcimport ministack.services.s3 as s3_svcfrom ministack.core.responses import (REST_JSON_CONTENT_TYPE,@@ -167,6 +174,10 @@ _ACCOUNT_ID_RE = re.compile(r"[0-9]{12}")_DEFAULT_VALIDITY = {"type": "MONTHS", "value": 135}# The one platform whose signed-object key was measured to differ._LAMBDA_PLATFORM_ID = "AWSLambda-SHA384-ECDSA"+# The platform whose signed object is emulated.+_IOT_PLATFORM_ID = "AWSIoTDeviceManagement-SHA256-ECDSA"+# Platforms that sign with the profile's ACM certificate on AWS.+_ACM_SIGNED_PLATFORMS = (_IOT_PLATFORM_ID, "AmazonFreeRTOS-Default", "AmazonFreeRTOS-TI-CC3220SF")def _now():@@ -271,9 +282,7 @@ def _validate_overrides(overrides):def _validate_signing_material(material):- """certificateArn is the shape's one member and is Required: Yes. The- certificate itself is not resolved: nothing signs for real here, so there- is no ACM lookup to make."""+ """certificateArn is required; StartSigningJob resolves it, so an ARN ACM does not hold is accepted."""if material is None:return Noneif not isinstance(material, dict):@@ -414,6 +423,66 @@ def _signed_key(prefix, job_id, platform_id, src_key):return key+def _acm_private_key(profile):+ """(key, None) for the private key ACM holds for the profile's certificate, else (None, reason)."""+ arn = (profile.get("signingMaterial") or {}).get("certificateArn")+ if not arn:+ return None, "the profile has no signingMaterial"+ cert = acm_svc._get_local_certificate(arn)+ if cert is None:+ return None, f"ACM in this account and region has no certificate {arn}"+ pem = cert.get("_private_key")+ if not pem:+ if cert.get("Type") == "IMPORTED" and "_private_key" not in cert:+ return None, f"the private key of {arn} is not persisted across restarts; import it again"+ return None, f"ACM holds no private key for {arn}"+ try:+ from cryptography.exceptions import UnsupportedAlgorithm+ from cryptography.hazmat.primitives.serialization import load_pem_private_key+ except ImportError:+ return None, "the cryptography package is not installed"+ try:+ return load_pem_private_key(pem.encode(), password=None), None+ except (ValueError, TypeError, UnsupportedAlgorithm):+ return None, f"the private key of {arn} cannot be loaded"+++def _signing_key(profile):+ """The key that signs the profile's jobs, or None for the receipt, with a warning where AWS would sign."""+ platform_id = profile.get("platformId")+ if platform_id not in _ACM_SIGNED_PLATFORMS:+ return None+ if platform_id == _IOT_PLATFORM_ID:+ key, reason = _acm_private_key(profile)+ else:+ key, reason = None, f"the {platform_id} signed object format is not emulated"+ if key is None:+ logger.warning("Signer profile %s writes a receipt instead of a signature: %s",+ profile.get("profileName"), reason)+ return key+++def _iot_signed_object(key, data, src_bucket, src_key, version_id):+ """The compact JSON AWS writes on the IoT platform, or None for a non-EC key."""+ from cryptography.hazmat.primitives import hashes+ from cryptography.hazmat.primitives.asymmetric import ec++ if not isinstance(key, ec.EllipticCurvePrivateKey):+ return None+ signature = key.sign(data, ec.ECDSA(hashes.SHA256()))+ document = {+ "rawPayloadSize": len(data),+ "signature": base64.b64encode(signature).decode("ascii"),+ "signatureAlgorithm": "SHA256withECDSA",+ "payloadLocation": {"s3": {+ "bucketName": src_bucket,+ "key": src_key,+ "version": version_id,+ }},+ }+ return json.dumps(document, separators=(",", ":")).encode("utf-8")++# ---------------------------------------------------------------------------# Handlers# ---------------------------------------------------------------------------@@ -487,38 +556,49 @@ def _start_signing_job(body, headers=None):signed_key = _signed_key(prefix, job_id, platform_id, src_key)now = _now()- marker = {- "jobId": job_id,- "profileName": profile_name,- "platformId": platform_id,- "source": {- "bucketName": src_bucket,- "key": src_key,- "version": version_id,- },- "sourceSha256": hashlib.sha256(data).hexdigest(),- "signedAt": now,- "signedBy": "ministack-signer",- }- marker_bytes = json.dumps(marker, ensure_ascii=False).encode("utf-8")- status, _headers, resp_body = s3_svc._put_object(- dst_bucket, signed_key, marker_bytes,- {"content-type": "application/json",- "content-length": str(len(marker_bytes))},- )- if status >= 300:- # Bucket existence was checked above, so this is some other S3- # rejection (Object Lock, SSE config, ...) — surface it as the- # service-side failure it is instead of mislabeling it "not found".- logger.error(- "Signed-object write to s3://%s/%s failed with S3 status %s: %s",- dst_bucket, signed_key, status, resp_body,+ key = _signing_key(profile)+ failed = False+ if key is not None:+ signed_bytes = _iot_signed_object(key, data, src_bucket, src_key, version_id)+ # AWS fails the job and writes nothing.+ failed = signed_bytes is None+ content_type = "application/octet-stream"+ else:+ marker = {+ "jobId": job_id,+ "profileName": profile_name,+ "platformId": platform_id,+ "source": {+ "bucketName": src_bucket,+ "key": src_key,+ "version": version_id,+ },+ "sourceSha256": hashlib.sha256(data).hexdigest(),+ "signedAt": now,+ "signedBy": "ministack-signer",+ }+ signed_bytes = json.dumps(marker, ensure_ascii=False).encode("utf-8")+ content_type = "application/json"+ if not failed:+ status, _headers, resp_body = s3_svc._put_object(+ dst_bucket, signed_key, signed_bytes,+ {"content-type": content_type,+ "content-length": str(len(signed_bytes))},)- return _error(500, "InternalServiceErrorException",- f"Writing the signed object to s3://{dst_bucket}/"- f"{signed_key} failed with S3 status {status}.")+ if status >= 300:+ # Bucket existence was checked above, so this is some other S3+ # rejection (Object Lock, SSE config, ...) — surface it as the+ # service-side failure it is instead of mislabeling it "not found".+ logger.error(+ "Signed-object write to s3://%s/%s failed with S3 status %s: %s",+ dst_bucket, signed_key, status, resp_body,+ )+ return _error(500, "InternalServiceErrorException",+ f"Writing the signed object to s3://{dst_bucket}/"+ f"{signed_key} failed with S3 status {status}.")account = get_account_id()+ # A failed job has no signedObject and no signatureExpiresAt._jobs[job_id] = {"jobId": job_id,"source": {"s3": {@@ -526,18 +606,18 @@ def _start_signing_job(body, headers=None):"key": src_key,"version": version_id,}},- "signedObject": {"s3": {"bucketName": dst_bucket, "key": signed_key}},+ "signedObject": None if failed else {"s3": {"bucketName": dst_bucket, "key": signed_key}},"profileName": profile_name,"profileVersion": profile.get("profileVersion"),"platformId": platform_id,"signingMaterial": profile.get("signingMaterial"),"overrides": profile.get("overrides"),"signingParameters": profile.get("signingParameters"),- "signatureExpiresAt": _signature_expires_at(+ "signatureExpiresAt": None if failed else _signature_expires_at(now, profile.get("signatureValidityPeriod")),- "status": "Succeeded",- "statusReason": "Signing Succeeded", # live-measured Describe wording+ "status": "Failed" if failed else "Succeeded",+ "statusReason": "can't identify EC private key." if failed else "Signing Succeeded","createdAt": now,"completedAt": now,"requestedBy": _requested_by(headers),diff --git a/tests/test_signer.py b/tests/test_signer.pyindex 5cc23879..b3e1dffb 100644--- a/tests/test_signer.py+++ b/tests/test_signer.py@@ -1,17 +1,22 @@# Copyright (c) 2026 MiniStack Contributors. SPDX-License-Identifier: MIT# Copies or substantial portions, including AI-assisted ports or rewrites, must retain this notice (see LICENSE)."""-Integration tests for the AWS Signer emulator (synchronous, synthetic signing).+Integration tests for the AWS Signer emulator (synchronous signing).The contract under test is the S3 side effect: StartSigningJob validates the-source object against the local S3 store and synchronously writes a JSON-signature marker to `destination.prefix + jobId`, so a caller whose contract-is the signed object at `prefix + jobId` never has to poll-DescribeSigningJob.+source object against the local S3 store and synchronously writes the signed+object to `destination.prefix + jobId`, so a caller whose contract is the+signed object at `prefix + jobId` never has to poll DescribeSigningJob. The+object is a real signature on the IoT platform when ACM holds the profile+certificate's key, and a JSON receipt otherwise."""+import base64+import datetimeimport hashlibimport json+import loggingimport re+import sysimport urllib.errorimport urllib.requestimport uuid@@ -22,6 +27,7 @@ from conftest import ENDPOINT, make_client, sqs_policy_allow_s3_IOT_PLATFORM = "AWSIoTDeviceManagement-SHA256-ECDSA"_LAMBDA_PLATFORM = "AWSLambda-SHA384-ECDSA"+_CERT_ARN = "arn:aws:acm:us-east-1:000000000000:certificate/9ec626ca-0bbb-4be5-83a2-ee563f8386ca"_SIGNER_AUTH = ("AWS4-HMAC-SHA256 "@@ -80,6 +86,35 @@ def _keys(s3, bucket):return [o["Key"] for o in s3.list_objects_v2(Bucket=bucket).get("Contents", [])]+def _import_code_signing_cert(acm_client, key):+ """Import a self-signed code-signing certificate for `key` into ACM; returns (arn, cert)."""+ from cryptography import x509+ from cryptography.hazmat.primitives import hashes, serialization+ from cryptography.x509.oid import ExtendedKeyUsageOID, NameOID++ name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, "signer-test.example.invalid")])+ now = datetime.datetime.now(datetime.timezone.utc)+ cert = (+ x509.CertificateBuilder()+ .subject_name(name).issuer_name(name)+ .public_key(key.public_key())+ .serial_number(x509.random_serial_number())+ .not_valid_before(now).not_valid_after(now + datetime.timedelta(days=7))+ .add_extension(x509.KeyUsage(True, False, False, False, False, False, False, False, False), critical=True)+ .add_extension(x509.ExtendedKeyUsage([ExtendedKeyUsageOID.CODE_SIGNING]), critical=True)+ .sign(key, hashes.SHA256())+ )+ arn = acm_client.import_certificate(+ Certificate=cert.public_bytes(serialization.Encoding.PEM),+ PrivateKey=key.private_bytes(+ serialization.Encoding.PEM,+ serialization.PrivateFormat.PKCS8,+ serialization.NoEncryption(),+ ),+ )["CertificateArn"]+ return arn, cert++# ---------------------------------------------------------------------------# StartSigningJob — the S3 side effect# ---------------------------------------------------------------------------@@ -105,6 +140,168 @@ def test_signer_start_writes_marker_at_prefix_plus_job_id(signer, s3, buckets, passert marker["sourceSha256"] == hashlib.sha256(payload).hexdigest()+def test_signer_iot_platform_signs_with_the_imported_certificate_key(signer, s3, acm_client, buckets):+ """The signed object is AWS's compact JSON with a DER ECDSA signature the certificate verifies."""+ from cryptography.exceptions import InvalidSignature+ from cryptography.hazmat.primitives import hashes+ from cryptography.hazmat.primitives.asymmetric import ec++ src, dst = buckets+ s3.put_bucket_versioning(Bucket=src, VersioningConfiguration={"Status": "Enabled"})+ payload = b"ssl = false\nserver = mqtt.example.invalid\nport = 8883\n"+ version = s3.put_object(Bucket=src, Key="cfg/config.cfg", Body=payload)["VersionId"]+ cert_arn, cert = _import_code_signing_cert(acm_client, ec.generate_private_key(ec.SECP256R1()))+ name = f"real_{_uid()}"+ signer.put_signing_profile(profileName=name, platformId=_IOT_PLATFORM,+ signingMaterial={"certificateArn": cert_arn})++ job_id = _start(signer, src, dst, key="cfg/config.cfg", profile=name,+ prefix="cfg/", version=version)["jobId"]++ obj = s3.get_object(Bucket=dst, Key=f"cfg/{job_id}")+ assert obj["ContentType"] == "application/octet-stream"+ raw = obj["Body"].read()+ document = json.loads(raw)+ assert list(document) == ["rawPayloadSize", "signature", "signatureAlgorithm", "payloadLocation"]+ assert json.dumps(document, separators=(",", ":")).encode() == raw+ assert document["rawPayloadSize"] == len(payload)+ assert document["signatureAlgorithm"] == "SHA256withECDSA"+ assert document["payloadLocation"] == {+ "s3": {"bucketName": src, "key": "cfg/config.cfg", "version": version}+ }+ signature = base64.b64decode(document["signature"])+ cert.public_key().verify(signature, payload, ec.ECDSA(hashes.SHA256()))+ with pytest.raises(InvalidSignature):+ cert.public_key().verify(signature, payload.replace(b"false", b"true "), ec.ECDSA(hashes.SHA256()))+ assert signer.describe_signing_job(jobId=job_id)["status"] == "Succeeded"+++def test_signer_iot_platform_rsa_certificate_fails_the_job(signer, s3, acm_client, buckets):+ """An RSA certificate on the ECDSA-only IoT platform fails the job and writes nothing."""+ from cryptography.hazmat.primitives.asymmetric import rsa++ src, dst = buckets+ s3.put_object(Bucket=src, Key="c.cfg", Body=b"ssl = false\n")+ cert_arn, _cert = _import_code_signing_cert(+ acm_client, rsa.generate_private_key(public_exponent=65537, key_size=2048))+ name = f"rsa_{_uid()}"+ signer.put_signing_profile(profileName=name, platformId=_IOT_PLATFORM,+ signingMaterial={"certificateArn": cert_arn})++ job_id = _start(signer, src, dst, key="c.cfg", profile=name, prefix="out/")["jobId"]++ job = signer.describe_signing_job(jobId=job_id)+ assert job["status"] == "Failed"+ assert job["statusReason"] == "can't identify EC private key."+ assert "signedObject" not in job+ assert "signatureExpiresAt" not in job+ assert _keys(s3, dst) == []+ failed = signer.list_signing_jobs(status="Failed")["jobs"]+ assert job_id in [j["jobId"] for j in failed]+++def test_signer_iot_platform_without_an_imported_key_writes_the_receipt(signer, s3, buckets):+ """Control: with no key in ACM for the certificate ARN the receipt is written as before."""+ src, dst = buckets+ s3.put_object(Bucket=src, Key="r.cfg", Body=b"r")+ name = f"receipt_{_uid()}"+ signer.put_signing_profile(profileName=name, platformId=_IOT_PLATFORM,+ signingMaterial={"certificateArn": _CERT_ARN})++ job_id = _start(signer, src, dst, key="r.cfg", profile=name)["jobId"]++ obj = s3.get_object(Bucket=dst, Key=f"signed/{job_id}")+ assert obj["ContentType"] == "application/json"+ assert json.loads(obj["Body"].read())["signedBy"] == "ministack-signer"+++def _ec_private_key_pem():+ from cryptography.hazmat.primitives import serialization+ from cryptography.hazmat.primitives.asymmetric import ec++ return ec.generate_private_key(ec.SECP256R1()).private_bytes(+ serialization.Encoding.PEM,+ serialization.PrivateFormat.PKCS8,+ serialization.NoEncryption(),+ ).decode()+++def _signing_key_and_warnings(caplog, platform, material):+ from ministack.services import signer as signer_module++ profile = {"profileName": "warned", "platformId": platform, "signingMaterial": material}+ caplog.clear()+ with caplog.at_level(logging.WARNING, logger="signer"):+ key = signer_module._signing_key(profile)+ return key, [r.getMessage() for r in caplog.records if r.name == "signer"]++[email protected]("platform, material, cert, reason", [+ pytest.param(_IOT_PLATFORM, None, None, "the profile has no signingMaterial", id="no-material"),+ pytest.param(_IOT_PLATFORM, {"certificateArn": _CERT_ARN}, None, "has no certificate", id="unknown-arn"),+ pytest.param(_IOT_PLATFORM, {"certificateArn": _CERT_ARN}, {"Type": "AMAZON_ISSUED", "_private_key": ""},+ "holds no private key", id="requested-certificate"),+ pytest.param(_IOT_PLATFORM, {"certificateArn": _CERT_ARN}, {"Type": "IMPORTED", "_private_key": "-----BEGIN"},+ "cannot be loaded", id="unreadable-key"),+ pytest.param("AmazonFreeRTOS-Default", {"certificateArn": _CERT_ARN}, None, "format is not emulated",+ id="freertos-default"),+ pytest.param("AmazonFreeRTOS-TI-CC3220SF", {"certificateArn": _CERT_ARN}, None, "format is not emulated",+ id="freertos-cc3220sf"),+])+def test_signer_receipt_on_an_acm_signed_platform_logs_a_warning(monkeypatch, caplog, platform, material,+ cert, reason):+ """Every receipt written where AWS signs with the profile's ACM certificate logs one warning."""+ from ministack.services import acm as acm_module++ monkeypatch.setattr(acm_module, "_get_local_certificate", lambda arn: cert)+ key, warnings = _signing_key_and_warnings(caplog, platform, material)+ assert key is None+ assert len(warnings) == 1+ assert "warned" in warnings[0] and reason in warnings[0]+++def test_signer_receipt_without_cryptography_logs_a_warning(monkeypatch, caplog):+ """Without the cryptography package nothing can sign, so the receipt is written with a warning."""+ from ministack.services import acm as acm_module++ cert = {"Type": "IMPORTED", "_private_key": _ec_private_key_pem()}+ monkeypatch.setattr(acm_module, "_get_local_certificate", lambda arn: cert)+ monkeypatch.setitem(sys.modules, "cryptography.hazmat.primitives.serialization", None)+ key, warnings = _signing_key_and_warnings(caplog, _IOT_PLATFORM, {"certificateArn": _CERT_ARN})+ assert key is None+ assert len(warnings) == 1 and "cryptography package is not installed" in warnings[0]++[email protected]("platform", [_LAMBDA_PLATFORM, "Notation-OCI-SHA384-ECDSA"])+def test_signer_receipt_on_a_signer_managed_platform_is_silent(caplog, platform):+ """Signer signs Lambda and container images with its own keys, so the receipt there warns nothing."""+ key, warnings = _signing_key_and_warnings(caplog, platform, {"certificateArn": _CERT_ARN})+ assert key is None+ assert warnings == []+++def test_signer_warm_boot_drops_the_key_and_logs_a_warning(caplog):+ """ACM does not persist private keys, so after a restart the IoT profile writes the receipt and warns."""+ from ministack.services import acm as acm_module++ acm_module._certificates._data.clear()+ try:+ _status, _headers, body = acm_module._import_certificate({+ "Certificate": "-----BEGIN CERTIFICATE-----\nBODY\n-----END CERTIFICATE-----\n",+ "PrivateKey": _ec_private_key_pem(),+ })+ material = {"certificateArn": json.loads(body)["CertificateArn"]}+ key, warnings = _signing_key_and_warnings(caplog, _IOT_PLATFORM, material)+ assert key is not None and warnings == []++ acm_module.load_persisted_state(acm_module.get_state())+ key, warnings = _signing_key_and_warnings(caplog, _IOT_PLATFORM, material)+ assert key is None+ assert len(warnings) == 1 and "not persisted across restarts" in warnings[0]+ finally:+ acm_module._certificates._data.clear()++def test_signer_empty_prefix_lands_marker_at_job_id(signer, s3, buckets, profile):src, dst = bucketss3.put_object(Bucket=src, Key="cfg.json", Body=b"{}")@@ -587,11 +784,17 @@ def test_signer_list_jobs_last_page_has_no_next_token(signer, s3, buckets, profipaginator."""src, dst = bucketss3.put_object(Bucket=src, Key="t.bin", Body=b"t")- _start(signer, src, dst, key="t.bin", profile=profile)+ job_id = _start(signer, src, dst, key="t.bin", profile=profile)["jobId"]+ # Other tests share the account, so the list can be longer than a page:+ # walk to the end rather than assume the first page is the last.+ seen = []page = signer.list_signing_jobs(maxResults=25)+ while "nextToken" in page:+ seen += [j["jobId"] for j in page["jobs"]]+ page = signer.list_signing_jobs(maxResults=25, nextToken=page["nextToken"])assert page["jobs"]- assert "nextToken" not in page+ assert job_id in seen + [j["jobId"] for j in page["jobs"]]def test_signer_job_records_the_calling_principal(signer, s3, buckets, profile):