History

fix openapi oauth cleanup (#418)

Rhys Sullivan committed 6 months ago

Commit 96a33621 parent b965f7b4 files · +34 −4Browse files

Unified · Split

4 files
  1. packages/plugins/openapi/src/react/AddOpenApiSource.tsx +1 −1
  2. packages/plugins/openapi/src/react/EditOpenApiSource.tsx +2 −3
  3. packages/plugins/workos-vault/src/sdk/secret-store.test.ts +30 −0
  4. packages/plugins/workos-vault/src/sdk/secret-store.ts +1 −0
packages/plugins/openapi/src/react/AddOpenApiSource.tsx+1−1
@@ -95,7 +95,7 @@ const substituteUrlVariables = (url: string, values: Record<string, string>): st · 94 unmodified lines
9595 return out;
9696 };
9797
98−const openApiOAuthConnectionId = (
98+export const openApiOAuthConnectionId = (
9999 namespaceSlug: string,
100100 flow: OAuth2Preset["flow"],
101101 ): string =>
packages/plugins/openapi/src/react/EditOpenApiSource.tsx+2−3
@@ -41,6 +41,7 @@ import { · 40 unmodified lines
4141 OPENAPI_OAUTH_CALLBACK_PATH,
4242 OPENAPI_OAUTH_CHANNEL,
4343 OPENAPI_OAUTH_POPUP_NAME,
44+ openApiOAuthConnectionId,
4445 resolveOAuthUrl,
4546 } from "./AddOpenApiSource";
4647 import { oauth2ClientSecretSlot } from "../sdk/store";
@@ -393,9 +394,7 @@ export default function EditOpenApiSource(props: { · 346 unmodified lines
393394 const connectionId =
394395 existingConnection && isConnectionBindingValue(existingConnection.value)
395396 ? existingConnection.value.connectionId
396− : ConnectionId.make(
397− `openapi-oauth-${slugify(props.sourceId)}-${slugify(oauth2.securitySchemeName)}-${slugify(targetScope)}`,
398− );
397+ : ConnectionId.make(openApiOAuthConnectionId(props.sourceId, oauth2.flow));
399398
400399 setBusyKey(`${targetScope}:${oauth2.connectionSlot}:connect`);
401400 setError(null);
packages/plugins/workos-vault/src/sdk/secret-store.test.ts+30−0
@@ -64,6 +64,7 @@ const makeMetadata = ( · 63 unmodified lines
6464 const makeFakeClient = (options?: {
6565 readonly conflictOnNextSecretUpdate?: boolean;
6666 readonly rejectNamesWithColon?: boolean;
67+ readonly rejectReadNamesLongerThan?: number;
6768 }): WorkOSVaultClient => {
6869 const objects = new Map<string, WorkOSVaultObject>();
6970 let sequence = 0;
@@ -106,6 +107,12 @@ const makeFakeClient = (options?: { · 36 unmodified lines
106107 if (options?.rejectNamesWithColon && name.includes(":")) {
107108 throw new FakeInvalidRequestError(`Invalid object name "${name}"`);
108109 }
110+ if (
111+ options?.rejectReadNamesLongerThan !== undefined &&
112+ name.length > options.rejectReadNamesLongerThan
113+ ) {
114+ throw new FakeInvalidRequestError(`Invalid object name "${name}"`);
115+ }
109116 const object = objects.get(name);
110117 if (!object) throw new FakeNotFoundError(`Object "${name}" not found`);
111118 return object;
@@ -260,6 +267,29 @@ describe("WorkOS Vault secret provider", () => { · 148 unmodified lines
260267 }),
261268 );
262269
270+ it.effect("treats invalid Vault object names as missing during removal", () =>
271+ Effect.gen(function* () {
272+ const client = makeFakeClient({ rejectReadNamesLongerThan: 80 });
273+ const executor = yield* makeExecutor(client);
274+ const longSecretId = SecretId.make(
275+ "openapi-oauth-dealcloud-api-oauth2-user-org-user-01kp6xm1zpvqvtpj77f0yv4eax.access_token",
276+ );
277+
278+ yield* executor.secrets.set(
279+ new SetSecretInput({
280+ id: longSecretId,
281+ scope: ScopeId.make("test-scope"),
282+ name: "Long connection token",
283+ value: "token",
284+ }),
285+ );
286+
287+ yield* executor.secrets.remove(longSecretId);
288+
289+ expect(yield* executor.secrets.list()).toHaveLength(0);
290+ }),
291+ );
292+
263293 it.effect("retries secret value writes on 409 version conflicts", () =>
264294 Effect.gen(function* () {
265295 // Inject one conflict on the next update against `/secrets/conflict`.
packages/plugins/workos-vault/src/sdk/secret-store.ts+1−0
@@ -238,6 +238,7 @@ const loadSecretObject = ( · 237 unmodified lines
238238 ): Effect.Effect<WorkOSVaultObject | null, WorkOSVaultClientError, never> =>
239239 client.readObjectByName(secretObjectName(prefix, scopeId, secretId)).pipe(
240240 Effect.catchAll((error) => {
241+ if (isStatusError(error, 400)) return Effect.succeed(null);
241242 if (!isStatusError(error, 404)) return Effect.fail(error);
242243
243244 const encodedName = secretObjectName(prefix, scopeId, secretId);