@encore.dev/sdk 2.0.0-beta.13 → 2.0.0-beta.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +48 -4
- package/config/config.test.ts +47 -13
- package/config/secrets.ts +6 -2
- package/cron/mod.test.ts +12 -0
- package/cron/mod.ts +18 -2
- package/dist/config/secrets.d.ts +4 -2
- package/dist/config/secrets.d.ts.map +1 -1
- package/dist/config/secrets.js +3 -0
- package/dist/config/secrets.js.map +1 -1
- package/dist/cron/mod.d.ts +17 -2
- package/dist/cron/mod.d.ts.map +1 -1
- package/dist/cron/mod.js +3 -2
- package/dist/cron/mod.js.map +1 -1
- package/dist/internal/otel/http.d.ts +4 -2
- package/dist/internal/otel/http.d.ts.map +1 -1
- package/dist/internal/otel/http.js +32 -5
- package/dist/internal/otel/http.js.map +1 -1
- package/dist/internal/runtime/napi/napi.cjs +6 -5
- package/dist/internal/runtime/napi/napi.d.cts +48 -13
- package/dist/redaction/mod.d.ts +62 -0
- package/dist/redaction/mod.d.ts.map +1 -0
- package/dist/redaction/mod.js +102 -0
- package/dist/redaction/mod.js.map +1 -0
- package/dist/storage/objects/bucket.d.ts +78 -22
- package/dist/storage/objects/bucket.d.ts.map +1 -1
- package/dist/storage/objects/bucket.js +95 -42
- package/dist/storage/objects/bucket.js.map +1 -1
- package/dist/storage/objects/mod.d.ts +1 -1
- package/dist/storage/objects/mod.d.ts.map +1 -1
- package/dist/storage/objects/mod.js.map +1 -1
- package/dist/storage/objects/refs.d.ts +2 -5
- package/dist/storage/objects/refs.d.ts.map +1 -1
- package/dist/storage/objects/refs.js.map +1 -1
- package/dist/tracing/http.d.ts +2 -1
- package/dist/tracing/http.d.ts.map +1 -1
- package/dist/tracing/http.js +2 -1
- package/dist/tracing/http.js.map +1 -1
- package/dist/tracing/node.d.ts.map +1 -1
- package/dist/tracing/node.js +1 -1
- package/dist/tracing/node.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/internal/otel/http.test.ts +50 -0
- package/internal/otel/http.ts +40 -5
- package/internal/runtime/napi/napi.cjs +6 -5
- package/internal/runtime/napi/napi.d.cts +48 -13
- package/package.json +6 -1
- package/redaction/bridge.test.ts +99 -0
- package/redaction/mod.test.ts +188 -0
- package/redaction/mod.ts +226 -0
- package/storage/objects/bucket.test.ts +168 -16
- package/storage/objects/bucket.ts +181 -62
- package/storage/objects/mod.ts +1 -0
- package/storage/objects/refs.ts +2 -5
- package/tracing/http.ts +2 -1
- package/tracing/native.test.ts +158 -8
- package/tracing/node.ts +1 -0
package/internal/otel/http.ts
CHANGED
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
type TextMapGetter,
|
|
9
9
|
} from '@opentelemetry/api';
|
|
10
10
|
import {tracerProvider} from '../../otel';
|
|
11
|
+
import {redactor} from '../../redaction/mod';
|
|
11
12
|
import {extract, type HeaderSource} from '../../reqctx/mod';
|
|
12
13
|
import {W3CPropagator} from './propagator';
|
|
13
14
|
|
|
@@ -25,11 +26,35 @@ const methods = new Set([
|
|
|
25
26
|
'TRACE',
|
|
26
27
|
]);
|
|
27
28
|
|
|
29
|
+
/** Record only request values approved by the deployed route policy. */
|
|
30
|
+
export function recordSafeHTTPRequest(
|
|
31
|
+
attributes: Attributes,
|
|
32
|
+
method: string,
|
|
33
|
+
target: string,
|
|
34
|
+
userAgent?: string,
|
|
35
|
+
): void {
|
|
36
|
+
try {
|
|
37
|
+
const http = redactor().http(method, target);
|
|
38
|
+
const {route, path, query} = http.requestUrl();
|
|
39
|
+
if (route !== null) attributes['http.route'] = route;
|
|
40
|
+
if (path !== null) attributes['url.path'] = path;
|
|
41
|
+
if (query) attributes['url.query'] = query;
|
|
42
|
+
if (userAgent) {
|
|
43
|
+
const {headers} = http.request({headers: [['user-agent', userAgent]]});
|
|
44
|
+
const safe = headers.find(([name]) => name === 'user-agent')?.[1];
|
|
45
|
+
if (safe) attributes['user_agent.original'] = safe;
|
|
46
|
+
}
|
|
47
|
+
} catch {
|
|
48
|
+
// Invalid or unavailable policy must not fall back to raw values.
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
28
52
|
export function startHTTPSpan<Headers extends HeaderSource>(
|
|
29
53
|
headers: Headers,
|
|
30
54
|
getter: TextMapGetter<Headers>,
|
|
31
55
|
requestMethod: string,
|
|
32
56
|
url: URL,
|
|
57
|
+
target = url.pathname + url.search,
|
|
33
58
|
) {
|
|
34
59
|
const parent = extract(
|
|
35
60
|
headers,
|
|
@@ -50,20 +75,30 @@ export function startHTTPSpan<Headers extends HeaderSource>(
|
|
|
50
75
|
url.port ||
|
|
51
76
|
(url.protocol === 'https:' ? '443' : url.protocol === 'http:' ? '80' : '');
|
|
52
77
|
if (port) attributes['server.port'] = Number(port);
|
|
78
|
+
const agent = getter.get(headers, 'user-agent');
|
|
79
|
+
recordSafeHTTPRequest(
|
|
80
|
+
attributes,
|
|
81
|
+
requestMethod,
|
|
82
|
+
target,
|
|
83
|
+
Array.isArray(agent) ? agent[0] : agent,
|
|
84
|
+
);
|
|
53
85
|
|
|
86
|
+
const route = attributes['http.route'] as string | undefined;
|
|
54
87
|
const span = tracerProvider()
|
|
55
88
|
.getTracer('encore')
|
|
56
89
|
.startSpan(
|
|
57
|
-
method
|
|
58
|
-
{
|
|
59
|
-
kind: SpanKind.SERVER,
|
|
60
|
-
attributes,
|
|
61
|
-
},
|
|
90
|
+
httpSpanName(method, route),
|
|
91
|
+
{kind: SpanKind.SERVER, attributes},
|
|
62
92
|
parent,
|
|
63
93
|
);
|
|
64
94
|
return {span, context: trace.setSpan(parent, span)};
|
|
65
95
|
}
|
|
66
96
|
|
|
97
|
+
function httpSpanName(method: string, route?: string): string {
|
|
98
|
+
const name = method === '_OTHER' ? 'HTTP' : method;
|
|
99
|
+
return route === undefined ? name : `${name} ${route}`;
|
|
100
|
+
}
|
|
101
|
+
|
|
67
102
|
export function recordHTTPStatus(span: Span, status: number): void {
|
|
68
103
|
span.setAttribute('http.response.status_code', status);
|
|
69
104
|
if (status >= 500 && status <= 599) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// The version of the runtime this JS bundle was generated for.
|
|
2
|
-
const version = "v2.0.0-beta.
|
|
2
|
+
const version = "v2.0.0-beta.15";
|
|
3
3
|
|
|
4
4
|
// Load the native module. ENCORE_RUNTIME_LIB overrides the location; by
|
|
5
5
|
// default the library is found by walking up from this file (which the
|
|
@@ -188,7 +188,7 @@ const {
|
|
|
188
188
|
ConfigWatch,
|
|
189
189
|
Cursor,
|
|
190
190
|
Decimal,
|
|
191
|
-
|
|
191
|
+
Download,
|
|
192
192
|
EnvironmentType,
|
|
193
193
|
GatewayHeaders,
|
|
194
194
|
GatewayView,
|
|
@@ -247,7 +247,7 @@ module.exports = {
|
|
|
247
247
|
ConfigWatch,
|
|
248
248
|
Cursor,
|
|
249
249
|
Decimal,
|
|
250
|
-
|
|
250
|
+
Download,
|
|
251
251
|
EnvironmentType,
|
|
252
252
|
GatewayHeaders,
|
|
253
253
|
GatewayView,
|
|
@@ -301,8 +301,9 @@ if (version !== Runtime.version() && installedRuntimeLib) {
|
|
|
301
301
|
);
|
|
302
302
|
}
|
|
303
303
|
if (version !== Runtime.version()) {
|
|
304
|
-
|
|
304
|
+
const runtimeVersion = Runtime.version().replace(/^v/, "");
|
|
305
|
+
console.warn(`⚠️ WARNING: this @encore.dev/sdk was built for the Encore runtime ${version}, but it is running in the Encore runtime ${Runtime.version()}.
|
|
305
306
|
This may cause unexpected behaviour in your application.
|
|
306
307
|
|
|
307
|
-
To resolve this,
|
|
308
|
+
To resolve this, install the SDK of the runtime's version: "npm install @encore.dev/sdk@${runtimeVersion}" (and "@encore.dev/framework@${runtimeVersion}" if you use the API framework). If the SDK is the newer, upgrade Encore with "encore version upgrade" instead.`);
|
|
308
309
|
}
|
|
@@ -45,9 +45,12 @@ export class BucketObject {
|
|
|
45
45
|
extendRetention(until: number, options?: ExtendRetentionOptions | undefined | null, source?: Buffer | undefined | null): Promise<ObjectAttrs | TypedObjectError>
|
|
46
46
|
signedUploadUrl(options?: UploadUrlOptions | undefined | null, source?: Buffer | undefined | null): Promise<SignedUploadUrl | TypedObjectError>
|
|
47
47
|
signedDownloadUrl(options?: DownloadUrlOptions | undefined | null, source?: Buffer | undefined | null): Promise<SignedDownloadUrl | TypedObjectError>
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
48
|
+
/**
|
|
49
|
+
* Begins a download: resolves once the provider has answered, with what
|
|
50
|
+
* it says of the object and its bytes still to come. A missing object
|
|
51
|
+
* fails here, before any bytes.
|
|
52
|
+
*/
|
|
53
|
+
download(options?: DownloadOptions | undefined | null, source?: Buffer | undefined | null): Promise<Download | TypedObjectError>
|
|
51
54
|
delete(options?: DeleteOptions | undefined | null, source?: Buffer | undefined | null): Promise<TypedObjectError | null>
|
|
52
55
|
publicUrl(): string
|
|
53
56
|
}
|
|
@@ -56,9 +59,14 @@ export interface BuildMeta {
|
|
|
56
59
|
revision: string
|
|
57
60
|
}
|
|
58
61
|
|
|
62
|
+
/**
|
|
63
|
+
* `length` bytes from `offset` (to the end of the object without a
|
|
64
|
+
* length), or the object's last `last` bytes.
|
|
65
|
+
*/
|
|
59
66
|
export interface ByteRange {
|
|
60
|
-
offset
|
|
67
|
+
offset?: number
|
|
61
68
|
length?: number
|
|
69
|
+
last?: number
|
|
62
70
|
}
|
|
63
71
|
|
|
64
72
|
/** Which of a database's users a call is made as. See [`clickhouse::Access`]. */
|
|
@@ -224,22 +232,40 @@ export interface DeployMeta {
|
|
|
224
232
|
worker?: string
|
|
225
233
|
}
|
|
226
234
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
235
|
+
/**
|
|
236
|
+
* A download under way: what the provider said of the object, and its
|
|
237
|
+
* bytes, read whole or one chunk at a time.
|
|
238
|
+
*/
|
|
239
|
+
export class Download {
|
|
240
|
+
get info(): DownloadInfo
|
|
241
|
+
/** The rest of the download, in one piece. It closes the download. */
|
|
242
|
+
bytes(): Promise<Buffer | TypedObjectError>
|
|
234
243
|
/**
|
|
235
|
-
* The next chunk, or null at the end. The
|
|
236
|
-
* on an error.
|
|
244
|
+
* The next chunk, or null at the end. The download closes at the end
|
|
245
|
+
* and on an error.
|
|
237
246
|
*/
|
|
238
247
|
next(): Promise<Buffer | null | TypedObjectError>
|
|
239
248
|
/** Stops the download, which ends its span. */
|
|
240
249
|
close(): Promise<void>
|
|
241
250
|
}
|
|
242
251
|
|
|
252
|
+
/** What a download's response says of the object being read. */
|
|
253
|
+
export interface DownloadInfo {
|
|
254
|
+
/** With `version`, the revision the bytes are of. */
|
|
255
|
+
etag: string
|
|
256
|
+
version?: string
|
|
257
|
+
/** The size of the whole object, also when a range of it is read. */
|
|
258
|
+
size: number
|
|
259
|
+
contentType?: string
|
|
260
|
+
/** Milliseconds since the Unix epoch. */
|
|
261
|
+
lastModified?: number
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export interface DownloadOptions {
|
|
265
|
+
version?: string
|
|
266
|
+
range?: ByteRange
|
|
267
|
+
}
|
|
268
|
+
|
|
243
269
|
export interface DownloadUrlOptions {
|
|
244
270
|
ttl?: number
|
|
245
271
|
}
|
|
@@ -407,6 +433,8 @@ export interface ListEntry {
|
|
|
407
433
|
name: string
|
|
408
434
|
size: number
|
|
409
435
|
etag: string
|
|
436
|
+
/** Milliseconds since the Unix epoch. None for a prefix. */
|
|
437
|
+
lastModified?: number
|
|
410
438
|
/** A common prefix of a delimited listing rather than an object. */
|
|
411
439
|
isPrefix: boolean
|
|
412
440
|
}
|
|
@@ -756,12 +784,19 @@ export class Runtime {
|
|
|
756
784
|
* a name has one scope across the app.
|
|
757
785
|
*/
|
|
758
786
|
secret(encoreName: string): Secret | null
|
|
787
|
+
/**
|
|
788
|
+
* Whether a secret [`secret`](Self::secret) has no value of reads as `""`
|
|
789
|
+
* rather than failing: only where the app runs locally, as `encore run`
|
|
790
|
+
* and `encore test` do, where a secret lacks a value until it is set.
|
|
791
|
+
*/
|
|
792
|
+
unsetSecretsReadEmpty(): boolean
|
|
759
793
|
/** The telemetry pipeline's span lifecycle calls. */
|
|
760
794
|
telemetry(): Pipeline
|
|
761
795
|
/** The meter provider's instrument calls. */
|
|
762
796
|
meters(): Meters
|
|
763
797
|
/** Returns the version of the Encore runtime being used */
|
|
764
798
|
static version(): string
|
|
799
|
+
redact(spec: string, payload: Buffer): string
|
|
765
800
|
appMeta(): AppMeta
|
|
766
801
|
runtimeConfig(): RuntimeConfig
|
|
767
802
|
}
|
package/package.json
CHANGED
|
@@ -75,6 +75,11 @@
|
|
|
75
75
|
"default": "./dist/pubsub/mod.js",
|
|
76
76
|
"types": "./dist/pubsub/mod.d.ts"
|
|
77
77
|
},
|
|
78
|
+
"./redaction": {
|
|
79
|
+
"bun": "./redaction/mod.ts",
|
|
80
|
+
"default": "./dist/redaction/mod.js",
|
|
81
|
+
"types": "./dist/redaction/mod.d.ts"
|
|
82
|
+
},
|
|
78
83
|
"./reqctx": {
|
|
79
84
|
"bun": "./reqctx/mod.ts",
|
|
80
85
|
"default": "./dist/reqctx/mod.js",
|
|
@@ -206,5 +211,5 @@
|
|
|
206
211
|
},
|
|
207
212
|
"sideEffects": false,
|
|
208
213
|
"type": "module",
|
|
209
|
-
"version": "2.0.0-beta.
|
|
214
|
+
"version": "2.0.0-beta.15"
|
|
210
215
|
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import {expect, test} from 'bun:test';
|
|
2
|
+
|
|
3
|
+
// Isolate the runtime mock from native integration tests.
|
|
4
|
+
test('flat exchanges preserve the bridge contract and return copies', () => {
|
|
5
|
+
const child = Bun.spawnSync({
|
|
6
|
+
cmd: [
|
|
7
|
+
process.execPath,
|
|
8
|
+
'-e',
|
|
9
|
+
`
|
|
10
|
+
import {mock, expect} from 'bun:test';
|
|
11
|
+
import {create, createFileRegistry} from '@bufbuild/protobuf';
|
|
12
|
+
import {FileDescriptorSetSchema} from '@bufbuild/protobuf/wkt';
|
|
13
|
+
const calls = [];
|
|
14
|
+
const bytes = Buffer.from([0, 255]);
|
|
15
|
+
const pair = [['x-secret', 'café']];
|
|
16
|
+
const encoded = pair;
|
|
17
|
+
let withheld = false;
|
|
18
|
+
let url = {route: '/items/{id}', path: '/items/REDACTED', query: 'safe=1'};
|
|
19
|
+
mock.module('../internal/runtime/mod', () => ({RT: {
|
|
20
|
+
redact(raw, payload) {
|
|
21
|
+
const spec = JSON.parse(raw);
|
|
22
|
+
calls.push({spec, payload: [...payload]});
|
|
23
|
+
const result = {kind: 'recorded', body: payload.toString('base64'), truncated: false};
|
|
24
|
+
if (spec.operation === 'request_url') return JSON.stringify({kind: 'request_url', ...url});
|
|
25
|
+
if (spec.kind === 'topic') return JSON.stringify(result);
|
|
26
|
+
const captured = spec.capture ? (withheld ? {kind: 'withheld', reason: 'streaming'} : result) : null;
|
|
27
|
+
return JSON.stringify(spec.kind === 'http'
|
|
28
|
+
? {kind: 'http', headers: spec.headers, body: captured}
|
|
29
|
+
: {kind: 'grpc', metadata: spec.metadata, message: captured, trailers: spec.trailers});
|
|
30
|
+
},
|
|
31
|
+
}}));
|
|
32
|
+
const {redactor} = await import('./mod');
|
|
33
|
+
const sdk = redactor();
|
|
34
|
+
const http = sdk.http('POST', '/items/private?safe=1');
|
|
35
|
+
expect(Object.keys(http).sort()).toEqual(['request', 'requestUrl', 'response']);
|
|
36
|
+
expect(http.requestUrl()).toEqual(url);
|
|
37
|
+
expect(calls.at(-1)).toEqual({spec: {kind:'http', method:'POST', target:'/items/private?safe=1', operation:'request_url'}, payload: []});
|
|
38
|
+
url = {route: null, path: null, query: null};
|
|
39
|
+
expect(http.requestUrl()).toEqual(url);
|
|
40
|
+
const safe = http.request({headers: pair, body: bytes});
|
|
41
|
+
expect(safe).toEqual({headers: pair, body: {kind:'recorded', body: bytes, truncated:false}});
|
|
42
|
+
expect(calls.at(-1)).toEqual({spec: {kind:'http', method:'POST', target:'/items/private?safe=1', operation:'exchange', response:false, headers:encoded, capture:true}, payload:[0,255]});
|
|
43
|
+
safe.headers[0][1] = 'changed';
|
|
44
|
+
expect(pair[0][1]).toBe('café');
|
|
45
|
+
safe.body.body[0] = 42;
|
|
46
|
+
expect([...bytes]).toEqual([0,255]);
|
|
47
|
+
expect(http.response({headers: pair})).toEqual({headers: pair});
|
|
48
|
+
expect(calls.at(-1).spec).toMatchObject({response:true, capture:false});
|
|
49
|
+
expect(http.request({headers: [], body: new Uint8Array()}).body.kind).toBe('recorded');
|
|
50
|
+
const files = createFileRegistry(create(FileDescriptorSetSchema, {file:[{
|
|
51
|
+
name:'probe.proto', package:'probe', syntax:'proto3', messageType:[{name:'Message'}],
|
|
52
|
+
service:[{name:'Check', method:[{name:'Run'}, {name:'Repeat', serverStreaming:true}, {name:'Join', clientStreaming:true}, {name:'Chat', clientStreaming:true, serverStreaming:true}].map(m => ({...m,inputType:'.probe.Message',outputType:'.probe.Message'}))}],
|
|
53
|
+
}]}));
|
|
54
|
+
for (const method of files.getService('probe.Check').methods) {
|
|
55
|
+
const rpc = sdk.grpc(method);
|
|
56
|
+
expect(Object.keys(rpc).sort()).toEqual(['request','response']);
|
|
57
|
+
const safe = rpc.request({metadata:pair, message:bytes});
|
|
58
|
+
expect(safe).toEqual({metadata:pair, message:{kind:'recorded',body:bytes,truncated:false}});
|
|
59
|
+
expect(calls.at(-1)).toEqual({spec:{kind:'grpc', service:'probe.Check', method:method.name,
|
|
60
|
+
client_streaming:['client_streaming','bidi_streaming'].includes(method.methodKind),
|
|
61
|
+
server_streaming:['server_streaming','bidi_streaming'].includes(method.methodKind),
|
|
62
|
+
operation:'exchange', response:false, metadata:encoded, trailers:[], capture:true},payload:[0,255]});
|
|
63
|
+
safe.metadata[0][1] = 'changed';
|
|
64
|
+
expect(pair[0][1]).toBe('café');
|
|
65
|
+
safe.message.body[0] = 42;
|
|
66
|
+
expect([...bytes]).toEqual([0,255]);
|
|
67
|
+
const response = rpc.response({metadata:pair, trailers:pair});
|
|
68
|
+
expect(response).toEqual({metadata:pair, trailers:pair});
|
|
69
|
+
expect(calls.at(-1).spec).toMatchObject({response:true, capture:false, metadata:encoded, trailers:encoded});
|
|
70
|
+
response.trailers[0][1] = 'changed';
|
|
71
|
+
expect(pair[0][1]).toBe('café');
|
|
72
|
+
expect([...bytes]).toEqual([0,255]);
|
|
73
|
+
expect(rpc.response({metadata:[] })).toEqual({metadata:[]});
|
|
74
|
+
withheld = true;
|
|
75
|
+
expect(rpc.response({metadata:[],message:bytes}).message).toEqual({kind:'withheld',reason:'streaming'});
|
|
76
|
+
withheld = false;
|
|
77
|
+
}
|
|
78
|
+
expect(sdk.topic('events').publish(bytes)).toEqual({kind:'recorded',body:bytes,truncated:false});
|
|
79
|
+
expect(calls.at(-1)).toEqual({spec:{kind:'topic',name:'events',operation:'publish'},payload:[0,255]});
|
|
80
|
+
const topic = sdk.topic('events');
|
|
81
|
+
expect(Object.keys(topic).sort()).toEqual(['delivery','publish']);
|
|
82
|
+
for (const original of [bytes, new Uint8Array()]) {
|
|
83
|
+
const safe = topic.delivery('worker', original);
|
|
84
|
+
expect(safe).toEqual({kind:'recorded',body:original,truncated:false});
|
|
85
|
+
expect(calls.at(-1)).toEqual({spec:{kind:'topic',name:'events',operation:'delivery',subscription:'worker'},payload:[...original]});
|
|
86
|
+
if (safe.body.length) safe.body[0] = 42;
|
|
87
|
+
expect([...bytes]).toEqual([0,255]);
|
|
88
|
+
topic.publish(original);
|
|
89
|
+
expect(calls.at(-1)).toEqual({spec:{kind:'topic',name:'events',operation:'publish'},payload:[...original]});
|
|
90
|
+
}
|
|
91
|
+
`,
|
|
92
|
+
],
|
|
93
|
+
cwd: import.meta.dir,
|
|
94
|
+
});
|
|
95
|
+
if (child.exitCode !== 0) {
|
|
96
|
+
throw new Error(child.stdout.toString() + child.stderr.toString());
|
|
97
|
+
}
|
|
98
|
+
expect(child.exitCode).toBe(0);
|
|
99
|
+
});
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import {expect, test} from 'bun:test';
|
|
2
|
+
import type {DescMethod} from '@bufbuild/protobuf';
|
|
3
|
+
import type {Redactor} from './mod';
|
|
4
|
+
|
|
5
|
+
// The public adapter accepts library descriptors, not descriptor-shaped objects.
|
|
6
|
+
function descriptorTypes(sdk: Redactor, descriptor: DescMethod) {
|
|
7
|
+
const grpc = sdk.grpc(descriptor);
|
|
8
|
+
grpc.request({metadata: [], message: new Uint8Array()});
|
|
9
|
+
grpc.response({metadata: [], trailers: []});
|
|
10
|
+
// @ts-expect-error Requests do not accept trailers.
|
|
11
|
+
grpc.request({metadata: [], trailers: []});
|
|
12
|
+
// @ts-expect-error Exchanges require their metadata.
|
|
13
|
+
grpc.request();
|
|
14
|
+
// @ts-expect-error No chained metadata view.
|
|
15
|
+
grpc.headers();
|
|
16
|
+
const http = sdk.http('GET', '/items');
|
|
17
|
+
const url: {route: string | null; path: string | null; query: string | null} =
|
|
18
|
+
http.requestUrl();
|
|
19
|
+
void url;
|
|
20
|
+
http.request({headers: []});
|
|
21
|
+
http.response({headers: [], body: new Uint8Array()});
|
|
22
|
+
// @ts-expect-error No chained request view.
|
|
23
|
+
http.request();
|
|
24
|
+
// @ts-expect-error No chained header view.
|
|
25
|
+
http.headers();
|
|
26
|
+
sdk.grpc({
|
|
27
|
+
// @ts-expect-error A method requires the full protobuf-es descriptor.
|
|
28
|
+
parent: {typeName: 'probe.Check'},
|
|
29
|
+
name: 'Run',
|
|
30
|
+
methodKind: 'unary',
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
void descriptorTypes;
|
|
34
|
+
|
|
35
|
+
const jsonRules = {
|
|
36
|
+
rules: {
|
|
37
|
+
json: {paths: [{entries: [{kind: {object_field: {field_name: 'token'}}}]}]},
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
const protobufRules = {
|
|
41
|
+
rules: {
|
|
42
|
+
protobuf: {paths: [{elements: [{field_number: 1, field_type: 'string'}]}]},
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
test('redacts copies using the configured policy and library descriptors', () => {
|
|
47
|
+
const config = {
|
|
48
|
+
environment: {
|
|
49
|
+
app_id: 'app',
|
|
50
|
+
env_name: 'test',
|
|
51
|
+
env_type: 'development',
|
|
52
|
+
cloud: 'local',
|
|
53
|
+
},
|
|
54
|
+
deployment: {
|
|
55
|
+
hosted_services: [
|
|
56
|
+
{
|
|
57
|
+
name: 'app',
|
|
58
|
+
routes: [
|
|
59
|
+
{
|
|
60
|
+
path: '/private/{id}',
|
|
61
|
+
methods: ['POST'],
|
|
62
|
+
endpoint: {
|
|
63
|
+
name: 'private',
|
|
64
|
+
recording: {
|
|
65
|
+
redaction: {
|
|
66
|
+
path_params: ['id'],
|
|
67
|
+
request_headers: ['user-agent'],
|
|
68
|
+
request: [jsonRules],
|
|
69
|
+
response: [jsonRules],
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
...['Run', 'Repeat', 'Join', 'Chat'].map(name => ({
|
|
75
|
+
path: `/probe.Check/${name}`,
|
|
76
|
+
methods: ['POST'],
|
|
77
|
+
endpoint: {
|
|
78
|
+
name,
|
|
79
|
+
recording: {
|
|
80
|
+
redaction: {
|
|
81
|
+
request: [protobufRules],
|
|
82
|
+
response: [protobufRules],
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
})),
|
|
87
|
+
],
|
|
88
|
+
},
|
|
89
|
+
],
|
|
90
|
+
},
|
|
91
|
+
infra: {
|
|
92
|
+
resources: {
|
|
93
|
+
pubsub_clusters: [
|
|
94
|
+
{topics: [{encore_name: 'submitted', payload_redaction: jsonRules}]},
|
|
95
|
+
],
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
const child = Bun.spawnSync({
|
|
100
|
+
cmd: [
|
|
101
|
+
process.execPath,
|
|
102
|
+
'-e',
|
|
103
|
+
`
|
|
104
|
+
import {create, createFileRegistry, fromBinary, toBinary} from '@bufbuild/protobuf';
|
|
105
|
+
import {FileDescriptorSetSchema} from '@bufbuild/protobuf/wkt';
|
|
106
|
+
import {redactor} from './mod';
|
|
107
|
+
const files = createFileRegistry(create(FileDescriptorSetSchema, {
|
|
108
|
+
file: [{
|
|
109
|
+
name: 'probe.proto', package: 'probe', syntax: 'proto3',
|
|
110
|
+
messageType: [{name: 'Message', field: [
|
|
111
|
+
{name: 'token', number: 1, type: 9},
|
|
112
|
+
{name: 'label', number: 2, type: 9},
|
|
113
|
+
]}],
|
|
114
|
+
service: [{name: 'Check', method: [
|
|
115
|
+
{name: 'Run'},
|
|
116
|
+
{name: 'Repeat', serverStreaming: true},
|
|
117
|
+
{name: 'Join', clientStreaming: true},
|
|
118
|
+
{name: 'Chat', clientStreaming: true, serverStreaming: true},
|
|
119
|
+
].map(method => ({...method, inputType: '.probe.Message', outputType: '.probe.Message'}))}],
|
|
120
|
+
}],
|
|
121
|
+
}));
|
|
122
|
+
const schema = files.getMessage('probe.Message');
|
|
123
|
+
const protobuf = toBinary(schema, create(schema, {token: 'private', label: 'ok'}));
|
|
124
|
+
const sdk = redactor();
|
|
125
|
+
const grpc = files.getService('probe.Check').methods.map(method => {
|
|
126
|
+
const rpc = sdk.grpc(method);
|
|
127
|
+
const decode = payload => payload.kind === 'recorded'
|
|
128
|
+
? {...payload, body: fromBinary(schema, payload.body)} : payload;
|
|
129
|
+
return [decode(rpc.request({metadata: [], message: protobuf}).message), decode(rpc.response({metadata: [], message: protobuf}).message)];
|
|
130
|
+
});
|
|
131
|
+
const http = sdk.http('POST', '/private/synthetic-secret');
|
|
132
|
+
const original = 'synthetic-agent';
|
|
133
|
+
const safe = http.request({headers: [['user-agent', original]]}).headers;
|
|
134
|
+
const json = new TextEncoder().encode('{"token":"private","label":"ok"}');
|
|
135
|
+
const decode = payload => payload.kind === 'recorded'
|
|
136
|
+
? {...payload, body: JSON.parse(new TextDecoder().decode(payload.body))} : payload;
|
|
137
|
+
console.log(JSON.stringify({
|
|
138
|
+
path: http.requestUrl().path,
|
|
139
|
+
headers: safe,
|
|
140
|
+
original: original,
|
|
141
|
+
jsonOriginal: new TextDecoder().decode(json),
|
|
142
|
+
protobufOriginal: fromBinary(schema, protobuf),
|
|
143
|
+
bodies: [http.request({headers: [], body: json}).body, http.response({headers: [], body: json}).body, sdk.topic('submitted').publish(json)].map(decode),
|
|
144
|
+
unknownTopic: sdk.topic('unknown').publish(json),
|
|
145
|
+
unknownDelivery: sdk.topic('unknown').delivery('worker', json),
|
|
146
|
+
grpc,
|
|
147
|
+
}));
|
|
148
|
+
`,
|
|
149
|
+
],
|
|
150
|
+
cwd: import.meta.dir,
|
|
151
|
+
env: {...process.env, ENCORE_RUNTIME_CONFIG: JSON.stringify(config)},
|
|
152
|
+
});
|
|
153
|
+
if (child.exitCode !== 0) {
|
|
154
|
+
throw new Error(`${child.stdout.toString()}\n${child.stderr.toString()}`);
|
|
155
|
+
}
|
|
156
|
+
const recorded = {
|
|
157
|
+
kind: 'recorded',
|
|
158
|
+
body: {token: '[REDACTED]', label: 'ok'},
|
|
159
|
+
truncated: false,
|
|
160
|
+
};
|
|
161
|
+
const protobufRecorded = {
|
|
162
|
+
...recorded,
|
|
163
|
+
body: {...recorded.body, $typeName: 'probe.Message'},
|
|
164
|
+
};
|
|
165
|
+
const withheld = {kind: 'withheld', reason: 'streaming'};
|
|
166
|
+
expect(
|
|
167
|
+
JSON.parse(child.stdout.toString().trim().split('\n').at(-1)!),
|
|
168
|
+
).toEqual({
|
|
169
|
+
path: '/private/REDACTED',
|
|
170
|
+
headers: [['user-agent', '[REDACTED]']],
|
|
171
|
+
original: 'synthetic-agent',
|
|
172
|
+
jsonOriginal: '{"token":"private","label":"ok"}',
|
|
173
|
+
protobufOriginal: {
|
|
174
|
+
$typeName: 'probe.Message',
|
|
175
|
+
token: 'private',
|
|
176
|
+
label: 'ok',
|
|
177
|
+
},
|
|
178
|
+
bodies: [recorded, recorded, recorded],
|
|
179
|
+
unknownTopic: {kind: 'omit'},
|
|
180
|
+
unknownDelivery: {kind: 'omit'},
|
|
181
|
+
grpc: [
|
|
182
|
+
[protobufRecorded, protobufRecorded],
|
|
183
|
+
[protobufRecorded, withheld],
|
|
184
|
+
[withheld, protobufRecorded],
|
|
185
|
+
[withheld, withheld],
|
|
186
|
+
],
|
|
187
|
+
});
|
|
188
|
+
});
|