Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
Merge branch 'bot' into bot
BlackMagiq96caa587d682
diff --cc backend/src/config/index.tsindex aa02d08a18,e3d62c42c4..99c72ab3d4--- a/backend/src/config/index.ts+++ b/backend/src/config/index.ts@@@ -14,13 -14,9 +14,12 @@@ const CLIENT_SECRET_HEROKU = process.enconst CLIENT_ID_HEROKU = process.env.CLIENT_ID_HEROKU!;const CLIENT_ID_VERCEL = process.env.CLIENT_ID_VERCEL!;const CLIENT_ID_NETLIFY = process.env.CLIENT_ID_NETLIFY!;+const CLIENT_ID_GITHUB =+ process.env.CLIENT_ID_GITHUB! || 'e787fc24bcec43ecd5d5';const CLIENT_SECRET_VERCEL = process.env.CLIENT_SECRET_VERCEL!;const CLIENT_SECRET_NETLIFY = process.env.CLIENT_SECRET_NETLIFY!;- const CLIENT_SECRET_GITHUB =- process.env.CLIENT_SECRET_GITHUB! ||- '407f32da788f63559abd662c6de08bb2911ca8ae';++const CLIENT_SECRET_GITHUB = process.env.CLIENT_SECRET_GITHUB! || '407f32da788f63559abd662c6de08bb2911ca8ae';+ const CLIENT_SLUG_VERCEL= process.env.CLIENT_SLUG_VERCEL!;const POSTHOG_HOST = process.env.POSTHOG_HOST! || 'https://app.posthog.com';const POSTHOG_PROJECT_API_KEY =process.env.POSTHOG_PROJECT_API_KEY! ||@@@ -62,7 -57,7 +61,8 @@@ exportCLIENT_SECRET_HEROKU,CLIENT_SECRET_VERCEL,CLIENT_SECRET_NETLIFY,+ CLIENT_SECRET_GITHUB,+ CLIENT_SLUG_VERCEL,POSTHOG_HOST,POSTHOG_PROJECT_API_KEY,PRIVATE_KEY,diff --cc backend/src/integrations/sync.tsindex 363e96f34d,b26bdee740..9c6b66674c--- a/backend/src/integrations/sync.ts+++ b/backend/src/integrations/sync.ts@@@ -29,55 -26,49 +29,54 @@@ import { access, appendFile } from 'fs'* @param {String} obj.accessToken - access token for integration*/const syncSecrets = async ({- integration,- integrationAuth,- secrets,- accessToken,+ integration,+ integrationAuth,+ secrets,+ accessToken}: {- integration: IIntegration;- integrationAuth: IIntegrationAuth;- secrets: any;- accessToken: string;+ integration: IIntegration;+ integrationAuth: IIntegrationAuth;+ secrets: any;+ accessToken: string;}) => {- try {- switch (integration.integration) {- case INTEGRATION_HEROKU:- await syncSecretsHeroku({- integration,- secrets,- accessToken- });- break;- case INTEGRATION_VERCEL:- await syncSecretsVercel({- integration,- secrets,- accessToken- });- break;- case INTEGRATION_NETLIFY:- await syncSecretsNetlify({- integration,- integrationAuth,- secrets,- accessToken- });- break;- }-- // TODO: set integration to inactive if it was not synced correctly (send alert?)- } catch (err) {- Sentry.setUser(null);- Sentry.captureException(err);- throw new Error('Failed to sync secrets to integration');- }-}+ try {+ switch (integration.integration) {+ case INTEGRATION_HEROKU:+ await syncSecretsHeroku({+ integration,+ secrets,+ accessToken+ });+ break;+ case INTEGRATION_VERCEL:+ await syncSecretsVercel({+ integration,+ secrets,+ accessToken+ });+ break;+ case INTEGRATION_NETLIFY:+ await syncSecretsNetlify({+ integration,+ integrationAuth,+ secrets,+ accessToken+ });+ break;+ case INTEGRATION_GITHUB:- await syncSecretsNetlify({++ await syncSecretsGitHub({+ integration,+ integrationAuth,+ secrets,+ accessToken+ });+ break;- }+ } catch (err) {+ Sentry.setUser(null);+ Sentry.captureException(err);+ throw new Error('Failed to sync secrets to integration');+ }+};/*** Sync/push [secrets] to Heroku [app]@@@ -297,266 -278,204 +288,312 @@@ const syncSecretsVercel = async (* @param {Object} obj.secrets - secrets to push to integration (object where keys are secret keys and values are secret values)*/const syncSecretsNetlify = async ({- integration,- integrationAuth,- secrets,- accessToken+ integration,+ integrationAuth,+ secrets,+ accessToken}: {- integration: IIntegration;- integrationAuth: IIntegrationAuth;- secrets: any;- accessToken: string;+ integration: IIntegration;+ integrationAuth: IIntegrationAuth;+ secrets: any;+ accessToken: string;}) => {- try {- const getParams = new URLSearchParams({- context_name: integration.context,- site_id: integration.siteId- });+ try {- const res = (- await axios.get(- `${INTEGRATION_NETLIFY_API_URL}/api/v1/accounts/${integrationAuth.accountId}/env`,- {- params: getParams,- headers: {- Authorization: `Bearer ${accessToken}`- }+ interface NetlifyValue {+ id?: string;+ context: string; // 'dev' | 'branch-deploy' | 'deploy-preview' | 'production',+ value: string;}- )- ).data.reduce(- (obj: any, secret: any) => ({- ...obj,- [secret.key]: secret.values[0].value- }),- {}- );-- interface UpdateNetlifySecret {- key: string;- context: string;- value: string;- }-- interface DeleteNetlifySecret {- key: string;- }-- interface NewNetlifySecretValue {- value: string;- context: string;- }-- interface NewNetlifySecret {- key: string;- values: NewNetlifySecretValue[];- }-- const updateSecrets: UpdateNetlifySecret[] = [];- const deleteSecrets: DeleteNetlifySecret[] = [];- const newSecrets: NewNetlifySecret[] = [];-- // Identify secrets to create- Object.keys(secrets).map((key) => {- if (!(key in res)) {- // case: secret has been created- newSecrets.push({- key: key,- values: [++ interface NetlifySecret {+ key: string;+ values: NetlifyValue[];+ }++ interface NetlifySecretsRes {+ [index: string]: NetlifySecret;+ }++ const getParams = new URLSearchParams({+ context_name: 'all', // integration.context or all+ site_id: integration.siteId+ });++ const res = (await axios.get(+ `${INTEGRATION_NETLIFY_API_URL}/api/v1/accounts/${integrationAuth.accountId}/env`,{- value: secrets[key], // include id?- context: integration.context+ params: getParams,+ headers: {+ Authorization: `Bearer ${accessToken}`+ }+ }+ ))+ .data+ .reduce((obj: any, secret: any) => ({+ ...obj,+ [secret.key]: secret+ }), {});++ const newSecrets: NetlifySecret[] = []; // createEnvVars+ const deleteSecrets: string[] = []; // deleteEnvVar+ const deleteSecretValues: NetlifySecret[] = []; // deleteEnvVarValue+ const updateSecrets: NetlifySecret[] = []; // setEnvVarValue++ // identify secrets to create and update+ Object.keys(secrets).map((key) => {+ if (!(key in res)) {+ // case: Infisical secret does not exist in Netlify -> create secret+ newSecrets.push({+ key,+ values: [{+ value: secrets[key],+ context: integration.context+ }]+ });+ } else {+ // case: Infisical secret exists in Netlify+ const contexts = res[key].values+ .reduce((obj: any, value: NetlifyValue) => ({+ ...obj,+ [value.context]: value+ }), {});++ if (integration.context in contexts) {+ // case: Netlify secret value exists in integration context+ if (secrets[key] !== contexts[integration.context].value) {+ // case: Infisical and Netlify secret values are different+ // -> update Netlify secret context and value+ updateSecrets.push({+ key,+ values: [{+ context: integration.context,+ value: secrets[key]+ }]+ });+ }+ } else {+ // case: Netlify secret value does not exist in integration context+ // -> add the new Netlify secret context and value+ updateSecrets.push({+ key,+ values: [{+ context: integration.context,+ value: secrets[key]+ }]+ });+ }+ }+ })++ // identify secrets to delete+ // TODO: revise (patch case where 1 context was deleted but others still there+ Object.keys(res).map((key) => {+ // loop through each key's context+ if (!(key in secrets)) {+ // case: Netlify secret does not exist in Infisical++ const numberOfValues = res[key].values.length;++ res[key].values.forEach((value: NetlifyValue) => {+ if (value.context === integration.context) {+ if (numberOfValues <= 1) {+ // case: Netlify secret value has less than 1 context -> delete secret+ deleteSecrets.push(key);+ } else {+ // case: Netlify secret value has more than 1 context -> delete secret value context+ deleteSecretValues.push({+ key,+ values: [{+ id: value.id,+ context: integration.context,+ value: value.value+ }]+ });+ }+ }+ });}- ]});- }- });- // Identify secrets to update and delete- Object.keys(res).map((key) => {- if (key in secrets) {- if (res[key] !== secrets[key]) {- // case: secret value has changed- updateSecrets.push({- key: key,- context: integration.context,- value: secrets[key]- });- }- } else {- // case: secret has been deleted- deleteSecrets.push({- key+ const syncParams = new URLSearchParams({+ site_id: integration.siteId});- }- });- const syncParams = new URLSearchParams({- site_id: integration.siteId- });+ if (newSecrets.length > 0) {+ await axios.post(+ `${INTEGRATION_NETLIFY_API_URL}/api/v1/accounts/${integrationAuth.accountId}/env`,+ newSecrets,+ {+ params: syncParams,+ headers: {+ Authorization: `Bearer ${accessToken}`+ }+ }+ );+ }- // Sync/push new secrets- if (newSecrets.length > 0) {- await axios.post(- `${INTEGRATION_NETLIFY_API_URL}/api/v1/accounts/${integrationAuth.accountId}/env`,- newSecrets,- {- params: syncParams,- headers: {- Authorization: `Bearer ${accessToken}`- }+ if (updateSecrets.length > 0) {+ updateSecrets.forEach(async (secret: NetlifySecret) => {+ await axios.patch(+ `${INTEGRATION_NETLIFY_API_URL}/api/v1/accounts/${integrationAuth.accountId}/env/${secret.key}`,+ {+ context: secret.values[0].context,+ value: secret.values[0].value+ },+ {+ params: syncParams,+ headers: {+ Authorization: `Bearer ${accessToken}`+ }+ }+ );+ });}- );- }- // Sync/push updated secrets- if (updateSecrets.length > 0) {- updateSecrets.forEach(async (secret: UpdateNetlifySecret) => {- await axios.patch(- `${INTEGRATION_NETLIFY_API_URL}/api/v1/accounts/${integrationAuth.accountId}/env/${secret.key}`,- {- context: secret.context,- value: secret.value- },- {- params: syncParams,- headers: {- Authorization: `Bearer ${accessToken}`- }- }- );- });- }+ if (deleteSecrets.length > 0) {+ deleteSecrets.forEach(async (key: string) => {+ await axios.delete(+ `${INTEGRATION_NETLIFY_API_URL}/api/v1/accounts/${integrationAuth.accountId}/env/${key}`,+ {+ params: syncParams,+ headers: {+ Authorization: `Bearer ${accessToken}`+ }+ }+ );+ });+ }- // Delete secrets- if (deleteSecrets.length > 0) {- deleteSecrets.forEach(async (secret: DeleteNetlifySecret) => {- await axios.delete(- `${INTEGRATION_NETLIFY_API_URL}/api/v1/accounts/${integrationAuth.accountId}/env/${secret.key}`,- {- params: syncParams,- headers: {- Authorization: `Bearer ${accessToken}`- }- }- );- });+ if (deleteSecretValues.length > 0) {+ deleteSecretValues.forEach(async (secret: NetlifySecret) => {+ await axios.delete(+ `${INTEGRATION_NETLIFY_API_URL}/api/v1/accounts/${integrationAuth.accountId}/env/${secret.key}/value/${secret.values[0].id}`,+ {+ params: syncParams,+ headers: {+ Authorization: `Bearer ${accessToken}`+ }+ }+ );+ });+ }+ } catch (err) {+ Sentry.setUser(null);+ Sentry.captureException(err);+ throw new Error('Failed to sync secrets to Heroku');}- } catch (err) {- Sentry.setUser(null);- Sentry.captureException(err);- throw new Error('Failed to sync secrets to Heroku');- }- };+ }--export {- syncSecrets-}+/**- * Sync/push [secrets] to Github site [app]++ * Sync/push [secrets] to GitHub site [app]+ * @param {Object} obj+ * @param {IIntegration} obj.integration - integration details+ * @param {IIntegrationAuth} obj.integrationAuth - integration auth details+ * @param {Object} obj.secrets - secrets to push to integration (object where keys are secret keys and values are secret values)+ */- const syncSecretsGithub = async ({++const syncSecretsGitHub = async ({+ integration,+ integrationAuth,+ secrets,+ accessToken+}: {+ integration: IIntegration;+ integrationAuth: IIntegrationAuth;+ secrets: any;+ accessToken: string;+}) => {+ try {+ const deleteSecrets: Array<string> = [];++ const octokit = new Octokit({+ auth: accessToken+ });++ const loggedInUser = await octokit.request('GET /user', {});+ // TODO: Check loggedInUser.login == repo owner+ const repoPublicKey = await octokit.request(+ 'GET /repos/{owner}/{repo}/actions/secrets/public-key',+ {+ owner: loggedInUser.login,+ repo: integration.app+ }+ ).key;++ const userRepos = await octokit.request('GET /user/repos', {});++ // Get local copy of decrypted secrets. We cannot decrypt them as we dont have access to GH private key+ const encryptedSecrets = await octokit.request(+ 'GET /repos/{owner}/{repo}/actions/secrets',+ {+ owner: loggedInUser.name,+ repo: integration.app+ }+ );++ Object.keys(secrets).map((key) => {+ if (!(key in encryptedSecrets)) {+ deleteSecrets.push(key);+ }+ });++ if (!Object.values(userRepos).includes(integration.app)) {+ if (deleteSecrets.length == 0) {+ throw new Error('Failed to sync secrets to Github');+ }+ }++ // Sync/push all secrets+ for (const i in secrets) {+ let encryptedSecret;+ sodium.ready.then(() => {+ // Convert Secret & Base64 key to Uint8Array.+ const binkey = sodium.from_base64(+ repoPublicKey,+ sodium.base64_variants.ORIGINAL+ );+ const binsec = sodium.from_string(secrets[i]);++ //Encrypt the secret using LibSodium+ const encBytes = sodium.crypto_box_seal(binsec, binkey);++ // Convert encrypted Uint8Array to Base64+ encryptedSecret = sodium.to_base64(+ encBytes,+ sodium.base64_variants.ORIGINAL+ );+ });++ const res = await octokit.request(+ 'PUT /repos/{owner}/{repo}/actions/secrets/{secret_name}',+ {+ owner: loggedInUser.login,+ repo: integration.app,+ secret_name: Object.keys(secrets[i]),+ encrypted_value: encryptedSecret,+ key_id: '' //TODO: Not sure if we need this? https://docs.github.com/en/rest/actions/secrets?apiVersion=2022-11-28#create-or-update-a-repository-secret+ }+ );+ }++ // Delete secrets+ if (deleteSecrets.length > 0) {+ for (const i in deleteSecrets) {+ const res = await octokit.request(+ 'DELETE /repos/{owner}/{repo}/actions/secrets/{secret_name}',+ {+ owner: loggedInUser.login,+ repo: integration.app,+ secret_name: secrets[i]+ }+ );+ }+ }+ } catch (err) {+ Sentry.setUser(null);+ Sentry.captureException(err);+ throw new Error('Failed to sync secrets to Github');+ }+};++export { syncSecrets };diff --cc frontend/pages/integrations/[id].jsindex 624874f422,a7b5f6b133..c47822c801--- a/frontend/pages/integrations/[id].js+++ b/frontend/pages/integrations/[id].js@@@ -132,17 -134,9 +134,17 @@@ export default function Integrations()window.location = `https://id.heroku.com/oauth/authorize?client_id=${integrationOption.clientId}&response_type=code&scope=write-protected&state=${state}`;break;case 'Vercel':- window.location = `https://vercel.com/integrations/infisical-dev/new?state=${state}`;+ window.location = `https://vercel.com/integrations/${integrationOption.clientSlug}/new?state=${state}`;break;case 'Netlify':+ window.location = `https://app.netlify.com/authorize?client_id=${integrationOption.clientId}&response_type=code&redirect_uri=${integrationOption.redirectURL}&state=${state}`;+ break;+ case 'Github':+ window.location = `https://github.com.com/login/oauth/authorize?client_id=${integrationOption.clientId}&response_type=code&redirect_uri=${integrationOption.redirectURL}&state=${state}`;+ break;+ case 'Fly.io':+ console.log('fly.io');+ setIntegrationAccessTokenDialogOpen(true);window.location = `https://app.netlify.com/authorize?client_id=${integrationOption.clientId}&response_type=code&state=${state}&redirect_uri=${window.location.origin}/netlify`;break;// case 'Fly.io':