Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rads/pdm-service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Serverless Framework v4, TypeScript. The Personal Data Management Service for th

## Endpoints

- `GET /records` - `?algorithmId=` selects which records are returned. Paginated via `?cursor=` (base64url of DynamoDB's `LastEvaluatedKey`), 500 items per page. The response also carries a standalone `dataSubjectIds` list aligned with `records`: the rads host assembles the GDPR access trail from it after a successful DC app run, and `records` stays `{recordId, data}`, so the DC app never sees subject ids. PDM itself writes nothing to the log on retrieval.
- `GET /records` - `?algorithmId=` selects which records are returned. Paginated via `?cursor=` (base64url of DynamoDB's `LastEvaluatedKey`), up to 2000 items or about 2.5 MB per page. The response also carries a standalone `dataSubjectIds` list aligned with `records`: the rads host assembles the GDPR access trail from it after a successful DC app run, and `records` stays `{recordId, data}`, so the DC app never sees subject ids. PDM itself writes nothing to the log on retrieval.
- `GET /subject/{dataSubjectId}` - the public data-subject access-request endpoint. No auth (the identifier is an opaque UUID) and rate-limited via a small reserved concurrency; it relays to log-service's `/subject-access/{dataSubjectId}` and returns which algorithms/runs touched that data subject. Never returns personal data.

The baseline PDM is fully trusted: there is no access token, verifier round-trip, or envelope encryption, and records are stored and returned as plaintext. Both routes are plain server-auth TLS and the HTTP API sets `cors: true` for the transparency portal.
Expand Down
53 changes: 30 additions & 23 deletions rads/pdm-service/src/handlers/records.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ const ddb = DynamoDBDocumentClient.from(new DynamoDBClient({}));
const tablePrefix = requiredEnv("PDM_TABLE_PREFIX");
const ALGORITHM_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;

const PAGE_SIZE = 500;
// Each HTTP request costs far more than a DynamoDB page, so fill a larger response from several queries.
// The byte budget keeps the response under the Lambda limit for large records (finance averages ~5.9 KB).
const PAGE_ITEMS = 2000;
const PAGE_BYTES = 2_500_000;
const QUERY_LIMIT = 250;
const MAX_QUERY_ATTEMPTS = 8;

async function queryWithRetry(command: QueryCommand) {
Expand Down Expand Up @@ -59,33 +63,36 @@ export const handler: APIGatewayProxyHandlerV2 = async (event) => {
}
}

const result = await queryWithRetry(
new QueryCommand({
TableName: `${tablePrefix}-${algorithmId}`,
KeyConditionExpression: "AlgorithmId = :a",
ExpressionAttributeValues: { ":a": algorithmId },
Limit: PAGE_SIZE,
ExclusiveStartKey: exclusiveStartKey,
ReturnConsumedCapacity: "TOTAL",
}),
);
await costEvent(
algorithmId,
runId,
CostPhase.Execution,
"dynamodb",
"read-capacity",
result.ConsumedCapacity?.CapacityUnits ?? 0,
);
const items: Record<string, unknown>[] = [];
let consumedCapacity = 0;
let pageBytes = 0;
let nextKey = exclusiveStartKey;
do {
const result = await queryWithRetry(
new QueryCommand({
TableName: `${tablePrefix}-${algorithmId}`,
KeyConditionExpression: "AlgorithmId = :a",
ExpressionAttributeValues: { ":a": algorithmId },
Limit: QUERY_LIMIT,
ExclusiveStartKey: nextKey,
ReturnConsumedCapacity: "TOTAL",
}),
);
for (const item of result.Items ?? []) {
items.push(item);
pageBytes += Buffer.byteLength(JSON.stringify(item));
}
consumedCapacity += result.ConsumedCapacity?.CapacityUnits ?? 0;
nextKey = result.LastEvaluatedKey as Record<string, unknown> | undefined;
} while (nextKey && items.length < PAGE_ITEMS && pageBytes < PAGE_BYTES);

await costEvent(algorithmId, runId, CostPhase.Execution, "dynamodb", "read-capacity", consumedCapacity);

// A standalone list aligned with `records`; the rads host assembles the DSAR trail from it and posts after
// a successful DC app run. Record objects stay {recordId, data}, so the DC app never sees subject ids.
const items = result.Items ?? [];
const records = items.map((item) => ({ recordId: item.RecordId, data: item.Data }));
const dataSubjectIds = items.map((item) => (typeof item.DataSubjectId === "string" ? item.DataSubjectId : ""));
const nextCursor = result.LastEvaluatedKey
? Buffer.from(JSON.stringify(result.LastEvaluatedKey)).toString("base64url")
: null;
const nextCursor = nextKey ? Buffer.from(JSON.stringify(nextKey)).toString("base64url") : null;

await recordRequest(algorithmId, runId, CostPhase.Execution, startedAt);
return jsonResponse(200, { algorithmId, records, dataSubjectIds, nextCursor });
Expand Down
2 changes: 1 addition & 1 deletion secure/pdm-service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Serverless Framework v4, TypeScript. The Personal Data Management Service: store

## Endpoints

- `GET /records` - sce-unit presents the bearer access JWT it got from verifier-service's `/attest`. The JWT is verified **locally** (`src/lib/jwt.ts`): ML-DSA-87 signature against verifier-service's JWKS (`GET /.well-known/jwks.json` on the public domain, fetched with SPKI-pinned TLS and cached, refetched on unknown `kid`), plus `iss`/`aud`/`exp` checks. The `algorithmId` always comes from the verified token's `sub` claim and the `runId` from its `run_id` claim - never anything the client sends - and selects which records are returned. Paginated via `?cursor=` (base64url of DynamoDB's `LastEvaluatedKey`), 500 items per page. Records are ciphertext only; the wrapped data key lives in the `DataKeyTable` and is read by kms-service, never returned here. The response also carries a standalone `dataSubjectIds` list aligned with `records`: sce-unit assembles the GDPR access trail from it after a successful DC app run, and `records` stays `{recordId, data}`, so the DC app never sees subject ids. PDM itself writes nothing to the log on retrieval.
- `GET /records` - sce-unit presents the bearer access JWT it got from verifier-service's `/attest`. The JWT is verified **locally** (`src/lib/jwt.ts`): ML-DSA-87 signature against verifier-service's JWKS (`GET /.well-known/jwks.json` on the public domain, fetched with SPKI-pinned TLS and cached, refetched on unknown `kid`), plus `iss`/`aud`/`exp` checks. The `algorithmId` always comes from the verified token's `sub` claim and the `runId` from its `run_id` claim - never anything the client sends - and selects which records are returned. Paginated via `?cursor=` (base64url of DynamoDB's `LastEvaluatedKey`), up to 2000 items or about 2.5 MB per page. Records are ciphertext only; the wrapped data key lives in the `DataKeyTable` and is read by kms-service, never returned here. The response also carries a standalone `dataSubjectIds` list aligned with `records`: sce-unit assembles the GDPR access trail from it after a successful DC app run, and `records` stays `{recordId, data}`, so the DC app never sees subject ids. PDM itself writes nothing to the log on retrieval.
- `GET /subject/{dataSubjectId}` - the public data-subject access-request endpoint. No auth (the identifier is an opaque UUID) and rate-limited via a small reserved concurrency; it relays to log-service's `/subject-access/{dataSubjectId}` and returns which algorithms/runs touched that data subject. Never returns personal data.

Both routes are plain server-auth TLS and the HTTP API sets `cors: true` for the transparency portal. The `records` route's identity proof is the attestation-backed access token, not a client cert (which would be extractable from the EIF). There is no verifier round-trip per request anymore - claims and expiry are defined in verifier-service's README (access JWTs, ML-DSA-87, `iss=verifier-service`, `aud` includes `pdm-service`, `sub=algorithmId`).
Expand Down
53 changes: 30 additions & 23 deletions secure/pdm-service/src/handlers/records.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ const ddb = DynamoDBDocumentClient.from(new DynamoDBClient({}));
const tablePrefix = requiredEnv("PDM_TABLE_PREFIX");
const ALGORITHM_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;

const PAGE_SIZE = 500;
// Each HTTP request costs far more than a DynamoDB page, so fill a larger response from several queries.
// The byte budget keeps the response under the Lambda limit for large records (finance averages ~5.9 KB).
const PAGE_ITEMS = 2000;
const PAGE_BYTES = 2_500_000;
const QUERY_LIMIT = 250;
const MAX_QUERY_ATTEMPTS = 8;

async function queryWithRetry(command: QueryCommand) {
Expand Down Expand Up @@ -66,33 +70,36 @@ export const handler: APIGatewayProxyHandlerV2 = async (event) => {
}
}

const result = await queryWithRetry(
new QueryCommand({
TableName: `${tablePrefix}-${validation.algorithmId}`,
KeyConditionExpression: "AlgorithmId = :a",
ExpressionAttributeValues: { ":a": validation.algorithmId },
Limit: PAGE_SIZE,
ExclusiveStartKey: exclusiveStartKey,
ReturnConsumedCapacity: "TOTAL",
}),
);
await costEvent(
validation.algorithmId,
runId,
CostPhase.Execution,
"dynamodb",
"read-capacity",
result.ConsumedCapacity?.CapacityUnits ?? 0,
);
const items: Record<string, unknown>[] = [];
let consumedCapacity = 0;
let pageBytes = 0;
let nextKey = exclusiveStartKey;
do {
const result = await queryWithRetry(
new QueryCommand({
TableName: `${tablePrefix}-${validation.algorithmId}`,
KeyConditionExpression: "AlgorithmId = :a",
ExpressionAttributeValues: { ":a": validation.algorithmId },
Limit: QUERY_LIMIT,
ExclusiveStartKey: nextKey,
ReturnConsumedCapacity: "TOTAL",
}),
);
for (const item of result.Items ?? []) {
items.push(item);
pageBytes += Buffer.byteLength(JSON.stringify(item));
}
consumedCapacity += result.ConsumedCapacity?.CapacityUnits ?? 0;
nextKey = result.LastEvaluatedKey as Record<string, unknown> | undefined;
} while (nextKey && items.length < PAGE_ITEMS && pageBytes < PAGE_BYTES);

await costEvent(validation.algorithmId, runId, CostPhase.Execution, "dynamodb", "read-capacity", consumedCapacity);

// A standalone list aligned with `records`; the enclave assembles the DSAR trail from it and posts after a
// successful DC app run. Record objects stay {recordId, data}, so the DC app never sees subject ids.
const items = result.Items ?? [];
const records = items.map((item) => ({ recordId: item.RecordId, data: item.Data }));
const dataSubjectIds = items.map((item) => (typeof item.DataSubjectId === "string" ? item.DataSubjectId : ""));
const nextCursor = result.LastEvaluatedKey
? Buffer.from(JSON.stringify(result.LastEvaluatedKey)).toString("base64url")
: null;
const nextCursor = nextKey ? Buffer.from(JSON.stringify(nextKey)).toString("base64url") : null;

await recordRequest(validation.algorithmId, runId, CostPhase.Execution, startedAt);
return jsonResponse(200, { algorithmId: validation.algorithmId, records, dataSubjectIds, nextCursor });
Expand Down