Skip to content
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

fix: fixed local auth handling for streamed flows #921

Open
wants to merge 2 commits into
base: next
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions js/flow/src/flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ export class Flow<
}) as S extends z.ZodVoid
? undefined
: StreamingCallback<z.infer<S>>,
auth: opts?.withLocalAuthContext,
}
).then((s) => s.result)
)
Expand Down
78 changes: 78 additions & 0 deletions js/flow/tests/flow_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import assert from 'node:assert';
import { beforeEach, describe, it } from 'node:test';
import { z } from 'zod';
import { defineFlow, defineStreamingFlow } from '../src/flow.js';
import { getFlowAuth } from '../src/utils.js';

function createTestFlow() {
return defineFlow(
Expand All @@ -34,6 +35,42 @@ function createTestFlow() {
);
}

function createTestFlowWithAuth() {
return defineFlow(
{
name: 'testFlowWithAuth',
inputSchema: z.string(),
outputSchema: z.string(),
authPolicy: async (auth) => {
if (auth != 'open sesame') {
throw 'forty thieves!';
}
},
},
async (input) => {
return `foo ${input}, auth ${JSON.stringify(getFlowAuth())}`;
}
);
}

function createTestStreamingFlowWithAuth() {
return defineStreamingFlow(
{
name: 'testFlowWithAuth',
inputSchema: z.string(),
outputSchema: z.string(),
authPolicy: async (auth) => {
if (auth != 'open sesame') {
throw 'forty thieves!';
}
},
},
async (input) => {
return `foo ${input}, auth ${JSON.stringify(getFlowAuth())}`;
}
);
}

function createTestStreamingFlow() {
return defineStreamingFlow(
{
Expand Down Expand Up @@ -111,6 +148,28 @@ describe('flow', () => {
}
);
});

it('should pass auth context all the way', async () => {
const testFlow = createTestFlowWithAuth();

const result = await testFlow('bar', {
withLocalAuthContext: 'open sesame',
});

assert.equal(result, 'foo bar, auth "open sesame"');
});

it('should fail auth', async () => {
const testFlow = createTestFlowWithAuth();

await assert.rejects(
() =>
testFlow('bar', {
withLocalAuthContext: 'yolo',
}),
/forty thieves/
);
});
});

describe('streamFlow', () => {
Expand Down Expand Up @@ -145,5 +204,24 @@ describe('flow', () => {
message: 'stream bad happened: foo',
});
});

it('should pass auth context all the way', async () => {
const testFlow = createTestStreamingFlowWithAuth();

const result = testFlow('bar', {
withLocalAuthContext: 'open sesame',
});

assert.equal(await result.output, 'foo bar, auth "open sesame"');
});

it('should fail auth', async () => {
const testFlow = createTestStreamingFlowWithAuth();
const response = testFlow('bar', {
withLocalAuthContext: 'yolo',
});

await assert.rejects(() => response.output, /forty thieves/);
});
});
});
2 changes: 1 addition & 1 deletion js/plugins/google-cloud/tests/logs_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import { defineModel, GenerateResponseData } from '@genkit-ai/ai/model';
import { configureGenkit } from '@genkit-ai/core';
import { defineFlow, run } from '@genkit-ai/flow';
import {
googleCloud,
__addTransportStreamForTesting,
__forceFlushSpansForTesting,
__getSpanExporterForTesting,
googleCloud,
} from '@genkit-ai/google-cloud';
import { ReadableSpan } from '@opentelemetry/sdk-trace-base';
import assert from 'node:assert';
Expand Down
4 changes: 2 additions & 2 deletions js/plugins/google-cloud/tests/metrics_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ import { defineModel } from '@genkit-ai/ai/model';
import { configureGenkit, defineAction } from '@genkit-ai/core';
import { defineFlow, run } from '@genkit-ai/flow';
import {
GcpOpenTelemetry,
googleCloud,
__forceFlushSpansForTesting,
__getMetricExporterForTesting,
__getSpanExporterForTesting,
GcpOpenTelemetry,
googleCloud,
} from '@genkit-ai/google-cloud';
import {
DataPoint,
Expand Down
Loading