From 17097965d95801ee43e0ee2366300b979d178351 Mon Sep 17 00:00:00 2001 From: Stijn-Kuijper Date: Mon, 19 Jun 2023 15:14:57 +0200 Subject: [PATCH] feat: cloudflare pages integration sync --- backend/src/integrations/sync.ts | 72 ++++++++++++++++++++++++++++++- backend/src/models/integration.ts | 5 ++- 2 files changed, 75 insertions(+), 2 deletions(-) diff --git a/backend/src/integrations/sync.ts b/backend/src/integrations/sync.ts index 76d009944..7d003b4f5 100644 --- a/backend/src/integrations/sync.ts +++ b/backend/src/integrations/sync.ts @@ -37,7 +37,9 @@ import { INTEGRATION_SUPABASE_API_URL, INTEGRATION_CHECKLY, INTEGRATION_CHECKLY_API_URL, - INTEGRATION_HASHICORP_VAULT + INTEGRATION_HASHICORP_VAULT, + INTEGRATION_CLOUDFLARE_PAGES, + INTEGRATION_CLOUDFLARE_PAGES_API_URL } from "../variables"; import { standardRequest} from '../config/request'; @@ -210,6 +212,14 @@ const syncSecrets = async ({ accessToken }); break; + case INTEGRATION_CLOUDFLARE_PAGES: + await syncSecretsCloudflarePages({ + integration, + secrets, + accessId, + accessToken + }); + break; } }; @@ -1833,4 +1843,64 @@ const syncSecretsHashiCorpVault = async ({ ); }; +const syncSecretsCloudflarePages = async ({ + integration, + secrets, + accessId, + accessToken, +}: { + integration: IIntegration; + secrets: any; + accessId: string | null; + accessToken: string; +}) => { + // get secrets from cloudflare pages + const getSecretsRes = ( + await standardRequest.get( + `${INTEGRATION_CLOUDFLARE_PAGES_API_URL}/client/v4/accounts/${accessId}/pages/projects/${integration.app}`, + { + headers: { + Authorization: `Bearer ${accessToken}`, + "Accept": "application/json", + }, + } + ) + ) + .data.result['deployment_configs'].production['env_vars']; + + // copy the secrets object, so we can set deleted keys to null + const secretsObj: any = {...secrets}; + + for (const [key, val] of Object.entries(secretsObj)) { + secretsObj[key] = { value: val }; + } + + for await (const key of Object.keys(getSecretsRes)) { + if (!(key in secrets)) { + // case: secret deos not exist in infisical + // -> delete secret from cloudflare pages + secretsObj[key] = null; + } + } + + await standardRequest.patch( + `${INTEGRATION_CLOUDFLARE_PAGES_API_URL}/client/v4/accounts/${accessId}/pages/projects/${integration.app}`, + { + data: { + "deployment_configs": { + "production": { + "env_vars": secretsObj + } + } + } + }, + { + headers: { + Authorization: `Bearer ${accessToken}`, + "Accept": "application/json", + }, + } + ) +} + export { syncSecrets }; diff --git a/backend/src/models/integration.ts b/backend/src/models/integration.ts index c23cf69f1..678cdcc56 100644 --- a/backend/src/models/integration.ts +++ b/backend/src/models/integration.ts @@ -16,6 +16,7 @@ import { INTEGRATION_SUPABASE, INTEGRATION_CHECKLY, INTEGRATION_HASHICORP_VAULT, + INTEGRATION_CLOUDFLARE_PAGES } from "../variables"; export interface IIntegration { @@ -50,7 +51,8 @@ export interface IIntegration { | "travisci" | "supabase" | "checkly" - | "hashicorp-vault"; + | "hashicorp-vault" + | "cloudflare-pages"; integrationAuth: Types.ObjectId; } @@ -138,6 +140,7 @@ const integrationSchema = new Schema( INTEGRATION_SUPABASE, INTEGRATION_CHECKLY, INTEGRATION_HASHICORP_VAULT, + INTEGRATION_CLOUDFLARE_PAGES, ], required: true, },