From 37ed27111aee96715a1b360e394eb509ae09338d Mon Sep 17 00:00:00 2001 From: Tuan Dang Date: Wed, 11 Jan 2023 16:53:53 +0700 Subject: [PATCH] Patch Vercel API teamId requirement for team integrations --- backend/src/integrations/apps.ts | 23 +++++++----- backend/src/integrations/exchange.ts | 3 +- backend/src/integrations/sync.ts | 53 +++++++++++++++------------- backend/src/variables/index.ts | 2 -- backend/src/variables/integration.ts | 2 -- frontend/pages/dashboard/[id].tsx | 1 + frontend/pages/integrations/[id].js | 5 +-- 7 files changed, 47 insertions(+), 42 deletions(-) diff --git a/backend/src/integrations/apps.ts b/backend/src/integrations/apps.ts index e3b78c481..02a1904c7 100644 --- a/backend/src/integrations/apps.ts +++ b/backend/src/integrations/apps.ts @@ -9,14 +9,9 @@ import { INTEGRATION_GITHUB, INTEGRATION_HEROKU_API_URL, INTEGRATION_VERCEL_API_URL, - INTEGRATION_NETLIFY_API_URL, - INTEGRATION_GITHUB_API_URL + INTEGRATION_NETLIFY_API_URL } from '../variables'; -interface GitHubApp { - name: string; -} - /** * Return list of names of apps for integration named [integration] * @param {Object} obj @@ -47,6 +42,7 @@ const getApps = async ({ break; case INTEGRATION_VERCEL: apps = await getAppsVercel({ + integrationAuth, accessToken }); break; @@ -110,17 +106,28 @@ const getAppsHeroku = async ({ accessToken }: { accessToken: string }) => { * @returns {Object[]} apps - names of Vercel apps * @returns {String} apps.name - name of Vercel app */ -const getAppsVercel = async ({ accessToken }: { accessToken: string }) => { +const getAppsVercel = async ({ + integrationAuth, + accessToken +}: { + integrationAuth: IIntegrationAuth; + accessToken: string; +}) => { let apps; try { const res = ( await axios.get(`${INTEGRATION_VERCEL_API_URL}/v9/projects`, { headers: { Authorization: `Bearer ${accessToken}` + }, + ...( integrationAuth?.teamId ? { + params: { + teamId: integrationAuth.teamId } + } : {}) }) ).data; - + apps = res.projects.map((a: any) => ({ name: a.name })); diff --git a/backend/src/integrations/exchange.ts b/backend/src/integrations/exchange.ts index 3e7076118..26aca5fdb 100644 --- a/backend/src/integrations/exchange.ts +++ b/backend/src/integrations/exchange.ts @@ -8,8 +8,7 @@ import { INTEGRATION_HEROKU_TOKEN_URL, INTEGRATION_VERCEL_TOKEN_URL, INTEGRATION_NETLIFY_TOKEN_URL, - INTEGRATION_GITHUB_TOKEN_URL, - INTEGRATION_GITHUB_API_URL + INTEGRATION_GITHUB_TOKEN_URL } from '../variables'; import { SITE_URL, diff --git a/backend/src/integrations/sync.ts b/backend/src/integrations/sync.ts index 30628fb9a..c4a4f16ee 100644 --- a/backend/src/integrations/sync.ts +++ b/backend/src/integrations/sync.ts @@ -12,14 +12,10 @@ import { INTEGRATION_GITHUB, INTEGRATION_HEROKU_API_URL, INTEGRATION_VERCEL_API_URL, - INTEGRATION_NETLIFY_API_URL, - INTEGRATION_GITHUB_API_URL + INTEGRATION_NETLIFY_API_URL } from '../variables'; import { access, appendFile } from 'fs'; -// TODO: need a helper function in the future to handle integration -// envar priorities (i.e. prioritize secrets within integration or those on Infisical) - /** * Sync/push [secrets] to [app] in integration named [integration] * @param {Object} obj @@ -53,6 +49,7 @@ const syncSecrets = async ({ case INTEGRATION_VERCEL: await syncSecretsVercel({ integration, + integrationAuth, secrets, accessToken }); @@ -139,10 +136,12 @@ const syncSecretsHeroku = async ({ */ const syncSecretsVercel = async ({ integration, + integrationAuth, secrets, accessToken }: { integration: IIntegration, + integrationAuth: IIntegrationAuth, secrets: any; accessToken: string; }) => { @@ -158,9 +157,12 @@ const syncSecretsVercel = async ({ try { // Get all (decrypted) secrets back from Vercel in // decrypted format - const params = new URLSearchParams({ - decrypt: "true" - }); + const params: { [key: string]: string } = { + decrypt: 'true', + ...( integrationAuth?.teamId ? { + teamId: integrationAuth.teamId + } : {}) + } const res = (await Promise.all((await axios.get( `${INTEGRATION_VERCEL_API_URL}/v9/projects/${integration.app}/env`, @@ -177,10 +179,10 @@ const syncSecretsVercel = async ({ .map(async (secret: VercelSecret) => (await axios.get( `${INTEGRATION_VERCEL_API_URL}/v9/projects/${integration.app}/env/${secret.id}`, { - headers: { - Authorization: `Bearer ${accessToken}` - } - + params, + headers: { + Authorization: `Bearer ${accessToken}` + } } )).data) )).reduce((obj: any, secret: any) => ({ @@ -236,9 +238,10 @@ const syncSecretsVercel = async ({ `${INTEGRATION_VERCEL_API_URL}/v10/projects/${integration.app}/env`, newSecrets, { - headers: { - Authorization: `Bearer ${accessToken}` - } + params, + headers: { + Authorization: `Bearer ${accessToken}` + } } ); } @@ -254,9 +257,10 @@ const syncSecretsVercel = async ({ `${INTEGRATION_VERCEL_API_URL}/v9/projects/${integration.app}/env/${secret.id}`, updatedSecret, { - headers: { - Authorization: `Bearer ${accessToken}` - } + params, + headers: { + Authorization: `Bearer ${accessToken}` + } } ); }); @@ -268,17 +272,18 @@ const syncSecretsVercel = async ({ await axios.delete( `${INTEGRATION_VERCEL_API_URL}/v9/projects/${integration.app}/env/${secret.id}`, { - headers: { - Authorization: `Bearer ${accessToken}` - } + params, + headers: { + Authorization: `Bearer ${accessToken}` + } } ); }); } } catch (err) { - Sentry.setUser(null); - Sentry.captureException(err); - throw new Error('Failed to sync secrets to Vercel'); + Sentry.setUser(null); + Sentry.captureException(err); + throw new Error('Failed to sync secrets to Vercel'); } } diff --git a/backend/src/variables/index.ts b/backend/src/variables/index.ts index 16c068925..4f7ffd8b0 100644 --- a/backend/src/variables/index.ts +++ b/backend/src/variables/index.ts @@ -19,7 +19,6 @@ import { INTEGRATION_HEROKU_API_URL, INTEGRATION_VERCEL_API_URL, INTEGRATION_NETLIFY_API_URL, - INTEGRATION_GITHUB_API_URL, INTEGRATION_OPTIONS } from './integration'; import { @@ -66,7 +65,6 @@ export { INTEGRATION_HEROKU_API_URL, INTEGRATION_VERCEL_API_URL, INTEGRATION_NETLIFY_API_URL, - INTEGRATION_GITHUB_API_URL, EVENT_PUSH_SECRETS, EVENT_PULL_SECRETS, ACTION_ADD_SECRETS, diff --git a/backend/src/variables/integration.ts b/backend/src/variables/integration.ts index 00e817c57..1625c3954 100644 --- a/backend/src/variables/integration.ts +++ b/backend/src/variables/integration.ts @@ -32,7 +32,6 @@ const INTEGRATION_GITHUB_TOKEN_URL = const INTEGRATION_HEROKU_API_URL = 'https://api.heroku.com'; const INTEGRATION_VERCEL_API_URL = 'https://api.vercel.com'; const INTEGRATION_NETLIFY_API_URL = 'https://api.netlify.com'; -const INTEGRATION_GITHUB_API_URL = 'https://api.github.com'; const INTEGRATION_OPTIONS = [ { @@ -134,6 +133,5 @@ export { INTEGRATION_HEROKU_API_URL, INTEGRATION_VERCEL_API_URL, INTEGRATION_NETLIFY_API_URL, - INTEGRATION_GITHUB_API_URL, INTEGRATION_OPTIONS }; diff --git a/frontend/pages/dashboard/[id].tsx b/frontend/pages/dashboard/[id].tsx index cb3717f68..4ce91002d 100644 --- a/frontend/pages/dashboard/[id].tsx +++ b/frontend/pages/dashboard/[id].tsx @@ -222,6 +222,7 @@ export default function Dashboard() { dataToSort?.map((item) => item.key).indexOf(item) ).includes(row.key) && row.type == 'shared'))?.map((item) => item.id) ) + setIsLoading(false); } catch (error) { console.log('Error', error); diff --git a/frontend/pages/integrations/[id].js b/frontend/pages/integrations/[id].js index 32ff8d2d9..2a5460dfb 100644 --- a/frontend/pages/integrations/[id].js +++ b/frontend/pages/integrations/[id].js @@ -132,9 +132,6 @@ export default function Integrations() { * @returns */ const handleIntegrationOption = async ({ integrationOption }) => { - - console.log('handleIntegrationOption', integrationOption); - try { // generate CSRF token for OAuth2 code-token exchange integrations const state = crypto.randomBytes(16).toString("hex"); @@ -218,7 +215,7 @@ export default function Integrations() { handleIntegrationOption={handleIntegrationOption} /> */} - {cloudIntegrationOptions.length > 0 ? ( + {(cloudIntegrationOptions.length > 0 && bot) ? (