mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-05 06:26:27 +00:00
feat: add delete method
This commit is contained in:
@@ -65,7 +65,10 @@ import { Octokit } from "@octokit/rest";
|
|||||||
import _ from "lodash";
|
import _ from "lodash";
|
||||||
import sodium from "libsodium-wrappers";
|
import sodium from "libsodium-wrappers";
|
||||||
import { standardRequest } from "../config/request";
|
import { standardRequest } from "../config/request";
|
||||||
import { ZGetTenantEnv } from "../validation/hasuraCloudIntegration";
|
import {
|
||||||
|
ZGetTenantEnv,
|
||||||
|
ZUpdateTenantEnv
|
||||||
|
} from "../validation/hasuraCloudIntegration";
|
||||||
|
|
||||||
const getSecretKeyValuePair = (
|
const getSecretKeyValuePair = (
|
||||||
secrets: Record<string, { value: string | null; comment?: string } | null>
|
secrets: Record<string, { value: string | null; comment?: string } | null>
|
||||||
@@ -3136,37 +3139,91 @@ const syncSecretsHasuraCloud = async ({
|
|||||||
|
|
||||||
const {
|
const {
|
||||||
data: {
|
data: {
|
||||||
getTenantEnv: { hash: currentHash }
|
getTenantEnv: { hash, envVars }
|
||||||
}
|
}
|
||||||
} = ZGetTenantEnv.parse(res.data);
|
} = ZGetTenantEnv.parse(res.data);
|
||||||
|
|
||||||
const envs = Object.keys(secrets).reduce<{ key: string; value: any }[]>((prev, key) => {
|
let currentEnvKeys = new Set<string>();
|
||||||
if (secrets?.[key]?.value) {
|
|
||||||
prev.push({ key, value: secrets[key].value });
|
if (envVars.environment) {
|
||||||
|
currentEnvKeys = new Set(Object.keys(envVars.environment));
|
||||||
|
}
|
||||||
|
const keysToAdd = new Set(Object.keys(secrets));
|
||||||
|
|
||||||
|
const envKeysToDelete = new Set<string>();
|
||||||
|
const envs: { key: string; value: any }[] = [];
|
||||||
|
|
||||||
|
for (const [key, value] of Object.entries(secrets)) {
|
||||||
|
if (value.value) {
|
||||||
|
envs.push({ key, value: value.value });
|
||||||
}
|
}
|
||||||
return prev;
|
}
|
||||||
}, []);
|
|
||||||
|
|
||||||
const variables = {
|
for (const currentKey of currentEnvKeys) {
|
||||||
currentHash,
|
if (!keysToAdd.has(currentKey)) {
|
||||||
envs,
|
envKeysToDelete.add(currentKey);
|
||||||
tenantId: integration.appId
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
await standardRequest.post(
|
let currentHash = hash;
|
||||||
INTEGRATION_HASURA_CLOUD_API_URL,
|
|
||||||
{
|
if (envs.length) {
|
||||||
query:
|
const addRequest = await standardRequest.post(
|
||||||
"mutation MyQuery($currentHash: String!, $envs: [UpdateEnvObject!]!, $tenantId: uuid!) { updateTenantEnv(currentHash: $currentHash, envs: $envs, tenantId: $tenantId) { hash envVars} }",
|
INTEGRATION_HASURA_CLOUD_API_URL,
|
||||||
variables
|
{
|
||||||
},
|
query:
|
||||||
{
|
"mutation MyQuery($currentHash: String!, $envs: [UpdateEnvObject!]!, $tenantId: uuid!) { updateTenantEnv(currentHash: $currentHash, envs: $envs, tenantId: $tenantId) { hash envVars} }",
|
||||||
headers: {
|
variables: {
|
||||||
Authorization: `pat ${accessToken}`,
|
currentHash,
|
||||||
"Content-Type": "application/json"
|
envs,
|
||||||
|
tenantId: integration.appId
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
Authorization: `pat ${accessToken}`,
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
const addRequestResponse = ZUpdateTenantEnv.safeParse(addRequest.data);
|
||||||
|
if (addRequestResponse.success) {
|
||||||
|
currentHash = addRequestResponse.data.data.updateTenantEnv.hash;
|
||||||
}
|
}
|
||||||
);
|
}
|
||||||
|
|
||||||
|
if (envKeysToDelete.size > 0) {
|
||||||
|
await standardRequest.post(
|
||||||
|
INTEGRATION_HASURA_CLOUD_API_URL,
|
||||||
|
{
|
||||||
|
query: `
|
||||||
|
mutation deleteTenantEnv($id: uuid!, $currentHash: String!, $env: [String!]!) {
|
||||||
|
deleteTenantEnv(tenantId: $id, currentHash: $currentHash, deleteEnvs: $env) {
|
||||||
|
hash
|
||||||
|
envVars
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
`,
|
||||||
|
variables: {
|
||||||
|
id: integration.appId,
|
||||||
|
currentHash,
|
||||||
|
env: Array.from(envKeysToDelete)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
Authorization: `pat ${accessToken}`,
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export { syncSecrets };
|
export { syncSecrets };
|
||||||
|
|||||||
@@ -3,6 +3,17 @@ import * as z from "zod";
|
|||||||
export const ZGetTenantEnv = z.object({
|
export const ZGetTenantEnv = z.object({
|
||||||
data: z.object({
|
data: z.object({
|
||||||
getTenantEnv: z.object({
|
getTenantEnv: z.object({
|
||||||
|
hash: z.string(),
|
||||||
|
envVars: z.object({
|
||||||
|
environment: z.record(z.any()).optional()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
export const ZUpdateTenantEnv = z.object({
|
||||||
|
data: z.object({
|
||||||
|
updateTenantEnv: z.object({
|
||||||
hash: z.string(),
|
hash: z.string(),
|
||||||
envVars: z.record(z.any())
|
envVars: z.record(z.any())
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user