Skip to content

Commit

Permalink
chore: Validate handleCustomerAuth registration
Browse files Browse the repository at this point in the history
  • Loading branch information
johnjcsmith committed Dec 10, 2024
1 parent 4261e78 commit 2593832
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 9 deletions.
8 changes: 4 additions & 4 deletions control-plane/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion control-plane/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"fastify": "^4.21.0",
"fastify-plugin": "^4.5.1",
"flagsmith-nodejs": "^3.3.3",
"inferable": "^0.30.39",
"inferable": "^0.30.59",
"jest": "^29.6.4",
"js-tiktoken": "^1.0.12",
"json-schema-to-zod": "^2.1.0",
Expand Down
4 changes: 2 additions & 2 deletions control-plane/src/modules/auth/customer-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { packer } from "../packer";
import * as jobs from "../jobs/jobs";
import { getJobStatusSync } from "../jobs/jobs";

const VERIFY_FUNCTION_NAME = "handleCustomerAuth";
const VERIFY_FUNCTION_SERVICE = "default";
export const VERIFY_FUNCTION_NAME = "handleCustomerAuth";
export const VERIFY_FUNCTION_SERVICE = "default";
const VERIFY_FUNCTION_ID = `${VERIFY_FUNCTION_SERVICE}_${VERIFY_FUNCTION_NAME}`;

/**
Expand Down
93 changes: 92 additions & 1 deletion control-plane/src/modules/service-definitions.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { dereferenceSync, JSONSchema } from "dereference-json-schema";
import { InvalidJobArgumentsError } from "../utilities/errors";
import { InvalidJobArgumentsError, InvalidServiceRegistrationError } from "../utilities/errors";
import { packer } from "./packer";
import {
deserializeFunctionSchema,
embeddableServiceFunction,
parseJobArgs,
serviceFunctionEmbeddingId,
updateServiceEmbeddings,
validateServiceRegistration,
} from "./service-definitions";
import { createOwner } from "./test/util";
import { zodToJsonSchema } from "zod-to-json-schema";
import { z } from "zod";
describe("updateServiceEmbeddings", () => {
let owner: { clusterId: string };
beforeAll(async () => {
Expand Down Expand Up @@ -281,3 +283,92 @@ describe("deserializeFunctionSchema", () => {
});
});
});

describe("validateServiceRegistration", () => {
it("should reject invalid schema", () => {
expect(() => {
validateServiceRegistration({
service: "default",
definition: {
name: "default",
functions: [
{
name: "someFn",
schema: JSON.stringify({
type: "wrong_type",
})
},
],
},
});
}).toThrow(InvalidServiceRegistrationError);
})

it("should accept valid schema", () => {
expect(() => {
validateServiceRegistration({
service: "default",
definition: {
name: "default",
functions: [
{
name: "someFn",
description: "someFn",
schema: JSON.stringify(zodToJsonSchema(
z.object({
test: z.string(),
})
))
},
],
},
});
}).not.toThrow();
})

it("should reject incorrect handleCustomerAuth registration", () => {
expect(() => {
validateServiceRegistration({
service: "default",
definition: {
name: "default",
functions: [
{
name: "handleCustomerAuth",
description: "handleCustomerAuth",
schema: JSON.stringify(zodToJsonSchema(
z.object({
test: z.string(),
})
))
},
],
},
});
}).toThrow(InvalidServiceRegistrationError);
})

it("should accept valid handleCustomerAuth registration", () => {
expect(() => {
validateServiceRegistration({
service: "default",
definition: {
name: "default",
functions: [
{
name: "handleCustomerAuth",
description: "handleCustomerAuth",
schema: JSON.stringify(zodToJsonSchema(
z.object({
token: z.string(),
})
))
},
],
},
});
}).not.toThrow();
})

})

22 changes: 21 additions & 1 deletion control-plane/src/modules/service-definitions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { and, eq, lte } from "drizzle-orm";
import {
handleCustomerAuthSchema,
validateDescription,
validateFunctionName,
validateFunctionSchema,
Expand All @@ -19,6 +20,7 @@ import { embeddableEntitiy } from "./embeddings/embeddings";
import { logger } from "./observability/logger";
import { packer } from "./packer";
import { withThrottle } from "./util";
import { VERIFY_FUNCTION_NAME, VERIFY_FUNCTION_SERVICE } from "./auth/customer-auth";

// The time without a ping before a service is considered expired
const SERVICE_LIVE_THRESHOLD_MS = 30 * 60 * 1000; // 30 minutes
Expand Down Expand Up @@ -369,7 +371,7 @@ export const updateServiceEmbeddings = async ({
);
};

const validateServiceRegistration = ({
export const validateServiceRegistration = ({
service,
definition,
}: {
Expand Down Expand Up @@ -398,6 +400,24 @@ const validateServiceRegistration = ({
);
}
}

// Checks for customer auth handler
if (service === VERIFY_FUNCTION_SERVICE && fn.name === VERIFY_FUNCTION_NAME) {
if (!fn.schema) {
throw new InvalidServiceRegistrationError(
`${fn.name} must have a valid schema`
);
}

// Check that the schema accepts and expected value
const zodSchema = deserializeFunctionSchema(fn.schema);
const schema = zodSchema.safeParse({ token: "test" });
if (!schema.success) {
throw new InvalidServiceRegistrationError(
`${fn.name} schema invalid: ${JSON.stringify(schema.error.issues)}`
);
}
}
}
};

Expand Down

0 comments on commit 2593832

Please sign in to comment.