-
Notifications
You must be signed in to change notification settings - Fork 6
[kafka pr-1 · 03/6] Slice 03 — Default-private KAs with --public override #392
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zsculac
wants to merge
12
commits into
feat/kafka-walking-skeleton
Choose a base branch
from
feat/kafka-default-private
base: feat/kafka-walking-skeleton
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
65a7e64
feat(kafka): default-private KAs with envelope selection in route ada…
d1a2c6e
feat(cli): add --public flag and privacy echo to kafka endpoint register
5825411
test(kafka): add private and public e2e scenarios to walking-skeleton…
fa41600
test(cli): align registerKafkaEndpoint mock with default-private resp…
2792699
fix(kafka): reject non-boolean "private" field with 400 and document …
b93d866
refactor(daemon): promote isNonEmptyString to shared http-utils
eab48b4
refactor(daemon): extract wrapJsonLdContent envelope helper
f90d68a
refactor(daemon): add validateOptionalBoolean http-utils helper
b0a0dde
refactor(test): extract reusable route test helpers
c1a5273
docs(test): clarify onPublish/publishCalls semantics in route-test-utils
655cb68
refactor(daemon): tighten docstrings and predicate comment
43d58ca
docs(cli): drop unverified "encrypted to CG participants" claim from …
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import type { JsonLdContent, JsonLdDocument } from '@origintrail-official/dkg-agent'; | ||
|
|
||
| /** Wrap a JSON-LD document in the `{ public }` or `{ private }` envelope `DKGAgent.publish` expects. */ | ||
| export function wrapJsonLdContent( | ||
| content: JsonLdDocument, | ||
| options: { private: boolean }, | ||
| ): JsonLdContent { | ||
| return options.private ? { private: content } : { public: content }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /** | ||
| * Unit tests for `wrapJsonLdContent` — the small helper that produces the | ||
| * `{ public }` / `{ private }` envelope shape DKGAgent.publish() expects. | ||
| * | ||
| * The helper is the privacy boundary for any route that publishes a | ||
| * JSON-LD KA. Keeping it tiny and well-tested means slice-02/07 (and any | ||
| * future route that publishes a KA) can adopt it without re-deriving | ||
| * envelope semantics. | ||
| */ | ||
|
|
||
| import { describe, expect, it } from 'vitest'; | ||
| import { wrapJsonLdContent } from '../src/daemon/json-ld-envelope.js'; | ||
|
|
||
| describe('wrapJsonLdContent', () => { | ||
| it('wraps the document in { private: ... } when options.private is true', () => { | ||
| const doc = { '@id': 'urn:test:1', 'foo:bar': 'baz' }; | ||
|
|
||
| const envelope = wrapJsonLdContent(doc, { private: true }); | ||
|
|
||
| expect(envelope).toEqual({ private: doc }); | ||
| expect(envelope).not.toHaveProperty('public'); | ||
| }); | ||
|
|
||
| it('wraps the document in { public: ... } when options.private is false', () => { | ||
| const doc = { '@id': 'urn:test:2', 'foo:bar': 'qux' }; | ||
|
|
||
| const envelope = wrapJsonLdContent(doc, { private: false }); | ||
|
|
||
| expect(envelope).toEqual({ public: doc }); | ||
| expect(envelope).not.toHaveProperty('private'); | ||
| }); | ||
|
|
||
| it('preserves the document by reference (no copy)', () => { | ||
| // The helper is a thin wrapper — it must NOT clone the document. A | ||
| // copy would silently break callers that rely on identity (e.g. for | ||
| // post-publish hooks that mutate the original). | ||
| const doc = { '@id': 'urn:test:3' }; | ||
|
|
||
| const privateEnv = wrapJsonLdContent(doc, { private: true }) as { private: object }; | ||
| const publicEnv = wrapJsonLdContent(doc, { private: false }) as { public: object }; | ||
|
|
||
| expect(privateEnv.private).toBe(doc); | ||
| expect(publicEnv.public).toBe(doc); | ||
| }); | ||
|
|
||
| it('accepts an array of JSON-LD documents (graph form)', () => { | ||
| // JsonLdDocument is `Record<string, unknown> | Record<string, unknown>[]` | ||
| // so an array of objects must round-trip through the envelope unchanged. | ||
| const docs = [{ '@id': 'urn:a' }, { '@id': 'urn:b' }]; | ||
|
|
||
| const envelope = wrapJsonLdContent(docs, { private: true }) as { private: unknown }; | ||
|
|
||
| expect(envelope.private).toBe(docs); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| /** | ||
| * Unit tests for the Kafka route adapter's privacy-envelope logic. | ||
| * | ||
| * The route adapter (packages/cli/src/daemon/routes/kafka.ts) is responsible | ||
| * for wrapping the bare KA in either `{ private: KA }` or `{ public: KA }` | ||
| * before passing it to agent.publish(). The kafka package itself stays agnostic. | ||
| * | ||
| * These tests invoke handleKafkaRoutes directly with a minimal RequestContext | ||
| * mock — no real daemon, no network, no chain. The fakes live in | ||
| * test/helpers/route-test-utils.ts and are reused by future route tests. | ||
| */ | ||
|
|
||
| import { describe, it, expect } from 'vitest'; | ||
| import { handleKafkaRoutes } from '../src/daemon/routes/kafka.js'; | ||
| import { | ||
| makeFakeRequest, | ||
| makeFakeResponse, | ||
| makeRequestContext, | ||
| } from './helpers/route-test-utils.js'; | ||
|
|
||
| const KAFKA_ENDPOINT_URL = '/api/kafka/endpoint'; | ||
|
|
||
| const VALID_BASE_BODY = { | ||
| contextGraphId: 'devnet-test', | ||
| broker: 'kafka.example.com:9092', | ||
| topic: 'orders.created', | ||
| messageFormat: 'application/json', | ||
| }; | ||
|
|
||
| describe('Kafka route adapter — privacy envelope', () => { | ||
| it('wraps with { private: KA } when private: true is in request body', async () => { | ||
| const req = makeFakeRequest({ ...VALID_BASE_BODY, private: true }, { url: KAFKA_ENDPOINT_URL }); | ||
| const { res, getResult } = makeFakeResponse(); | ||
| const { ctx, publishCalls } = makeRequestContext(req, res); | ||
|
|
||
| await handleKafkaRoutes(ctx); | ||
|
|
||
| const { status, body } = getResult(); | ||
| expect(status).toBe(200); | ||
|
|
||
| expect(publishCalls).toHaveLength(1); | ||
| const { envelope } = publishCalls[0]!; | ||
| expect(envelope).toHaveProperty('private'); | ||
| expect(envelope).not.toHaveProperty('public'); | ||
|
|
||
| // Response must echo the resolved private flag | ||
| expect((body as Record<string, unknown>).private).toBe(true); | ||
| }); | ||
|
|
||
| it('wraps with { public: KA } when private: false is in request body', async () => { | ||
| const req = makeFakeRequest({ ...VALID_BASE_BODY, private: false }, { url: KAFKA_ENDPOINT_URL }); | ||
| const { res, getResult } = makeFakeResponse(); | ||
| const { ctx, publishCalls } = makeRequestContext(req, res); | ||
|
|
||
| await handleKafkaRoutes(ctx); | ||
|
|
||
| const { status, body } = getResult(); | ||
| expect(status).toBe(200); | ||
|
|
||
| expect(publishCalls).toHaveLength(1); | ||
| const { envelope } = publishCalls[0]!; | ||
| expect(envelope).toHaveProperty('public'); | ||
| expect(envelope).not.toHaveProperty('private'); | ||
|
|
||
| // Response must echo the resolved private flag | ||
| expect((body as Record<string, unknown>).private).toBe(false); | ||
| }); | ||
|
|
||
| it('defaults to { private: KA } when private field is omitted from request body', async () => { | ||
| // No `private` field — route defaults to private: true | ||
| const req = makeFakeRequest(VALID_BASE_BODY, { url: KAFKA_ENDPOINT_URL }); | ||
| const { res, getResult } = makeFakeResponse(); | ||
| const { ctx, publishCalls } = makeRequestContext(req, res); | ||
|
|
||
| await handleKafkaRoutes(ctx); | ||
|
|
||
| const { status, body } = getResult(); | ||
| expect(status).toBe(200); | ||
|
|
||
| expect(publishCalls).toHaveLength(1); | ||
| const { envelope } = publishCalls[0]!; | ||
| expect(envelope).toHaveProperty('private'); | ||
| expect(envelope).not.toHaveProperty('public'); | ||
|
|
||
| // Response echoes resolved private flag = true (default) | ||
| expect((body as Record<string, unknown>).private).toBe(true); | ||
| }); | ||
|
|
||
| it('returns 400 when contextGraphId is missing', async () => { | ||
| const req = makeFakeRequest( | ||
| { broker: 'x:9092', topic: 't', messageFormat: 'application/json' }, | ||
| { url: KAFKA_ENDPOINT_URL }, | ||
| ); | ||
| const { res, getResult } = makeFakeResponse(); | ||
| const { ctx } = makeRequestContext(req, res); | ||
|
|
||
| await handleKafkaRoutes(ctx); | ||
|
|
||
| expect(getResult().status).toBe(400); | ||
| }); | ||
|
|
||
| it('returns 400 when broker is missing', async () => { | ||
| const req = makeFakeRequest( | ||
| { contextGraphId: 'devnet-test', topic: 't', messageFormat: 'application/json' }, | ||
| { url: KAFKA_ENDPOINT_URL }, | ||
| ); | ||
| const { res, getResult } = makeFakeResponse(); | ||
| const { ctx } = makeRequestContext(req, res); | ||
|
|
||
| await handleKafkaRoutes(ctx); | ||
|
|
||
| expect(getResult().status).toBe(400); | ||
| }); | ||
|
|
||
| it('returns 400 when "private" is a non-boolean value (e.g. string "false")', async () => { | ||
| // The route enforces a strict boolean for `private` to keep the privacy | ||
| // contract unambiguous. Truthy/falsy coercion would create an unsafe | ||
| // ambiguity at a privacy boundary. | ||
| const req = makeFakeRequest({ ...VALID_BASE_BODY, private: 'false' }, { url: KAFKA_ENDPOINT_URL }); | ||
| const { res, getResult } = makeFakeResponse(); | ||
| const { ctx, publishCalls } = makeRequestContext(req, res); | ||
|
|
||
| await handleKafkaRoutes(ctx); | ||
|
|
||
| const { status, body } = getResult(); | ||
| expect(status).toBe(400); | ||
| expect((body as Record<string, unknown>).error).toMatch(/"private" must be a boolean/); | ||
| // No publish should have happened | ||
| expect(publishCalls).toHaveLength(0); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.