Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

feat(secret-sync): encrypt chef sync secrets as encrypted data bag items

Dimitris Staratzis0d570553c179
diff --git a/backend/src/ee/services/secret-sync/chef/chef-sync-fns.ts b/backend/src/ee/services/secret-sync/chef/chef-sync-fns.ts
index 3825466ac0..f5eca00cd3 100644
--- a/backend/src/ee/services/secret-sync/chef/chef-sync-fns.ts
+++ b/backend/src/ee/services/secret-sync/chef/chef-sync-fns.ts
@@ -1,6 +1,8 @@
import { getChefDataBagItem, updateChefDataBagItem } from "@app/ee/services/app-connections/chef";
import { TGatewayPoolServiceFactory } from "@app/ee/services/gateway-pool/gateway-pool-service";
import { TGatewayV2ServiceFactory } from "@app/ee/services/gateway-v2/gateway-v2-service";
+import { crypto } from "@app/lib/crypto";
+import { SecretSyncError } from "@app/services/secret-sync/secret-sync-errors";
import { matchesSchema } from "@app/services/secret-sync/secret-sync-fns";
import { TSecretSyncPayload } from "@app/services/secret-sync/secret-sync-payload";
import { TSecretMap } from "@app/services/secret-sync/secret-sync-types";
@@ -8,6 +10,7 @@ import { TSecretMap } from "@app/services/secret-sync/secret-sync-types";
import {
ChefSecret,
TChefDataBagItemContent,
+ TChefEncryptedValue,
TChefSecret,
TChefSecrets,
TChefSyncWithCredentials,
@@ -27,6 +30,72 @@ const resolveGatewayId = async (
});
};
+const CHEF_ENCRYPTED_DATA_BAG_VERSION = 3;
+const CHEF_ENCRYPTED_DATA_BAG_CIPHER = "aes-256-gcm";
+const CHEF_ENCRYPTED_DATA_BAG_IV_BYTES = 12;
+
+// Chef derives the cipher key from the SHA-256 of the shared secret and wraps each value as
+// {"json_wrapper": value}; matching both is what lets knife and cookbooks decrypt what we write.
+const getDataBagKey = (dataBagSecret: string) =>
+ crypto.nativeCrypto.createHash("sha256").update(dataBagSecret).digest();
+
+const isEncryptedValue = (value: unknown): value is TChefEncryptedValue =>
+ typeof value === "object" && value !== null && "encrypted_data" in value;
+
+const encryptValue = (value: TChefSecret, key: Buffer): TChefEncryptedValue => {
+ const iv = crypto.randomBytes(CHEF_ENCRYPTED_DATA_BAG_IV_BYTES);
+ const cipher = crypto.nativeCrypto.createCipheriv(CHEF_ENCRYPTED_DATA_BAG_CIPHER, key, iv);
+ const encryptedData = Buffer.concat([cipher.update(JSON.stringify({ json_wrapper: value }), "utf8"), cipher.final()]);
+
+ return {
+ encrypted_data: encryptedData.toString("base64"),
+ iv: iv.toString("base64"),
+ auth_tag: cipher.getAuthTag().toString("base64"),
+ version: CHEF_ENCRYPTED_DATA_BAG_VERSION,
+ cipher: CHEF_ENCRYPTED_DATA_BAG_CIPHER
+ };
+};
+
+const decryptValue = (secretKey: string, value: TChefEncryptedValue, secretSync: TChefSyncWithCredentials) => {
+ const { dataBagName, dataBagItemName } = secretSync.destinationConfig;
+ const { dataBagSecret } = secretSync.connection.credentials;
+ const itemLabel = `"${dataBagName}/${dataBagItemName}"`;
+
+ if (!dataBagSecret) {
+ throw new SecretSyncError({
+ message: `Chef data bag item ${itemLabel} is encrypted. Add the data bag secret it was encrypted with to the sync's Chef connection.`,
+ shouldRetry: false
+ });
+ }
+
+ if (value.version !== CHEF_ENCRYPTED_DATA_BAG_VERSION) {
+ throw new SecretSyncError({
+ message: `Value "${secretKey}" in Chef data bag item ${itemLabel} uses encrypted data bag version ${value.version}. Infisical supports only version 3, the default since Chef Infra Client 13. Re-encrypt the item with version 3.`,
+ shouldRetry: false
+ });
+ }
+
+ try {
+ const decipher = crypto.nativeCrypto.createDecipheriv(
+ CHEF_ENCRYPTED_DATA_BAG_CIPHER,
+ getDataBagKey(dataBagSecret),
+ Buffer.from(value.iv, "base64")
+ );
+ decipher.setAuthTag(Buffer.from(value.auth_tag, "base64"));
+ const plaintext = Buffer.concat([
+ decipher.update(Buffer.from(value.encrypted_data, "base64")),
+ decipher.final()
+ ]).toString("utf8");
+
+ return (JSON.parse(plaintext) as { json_wrapper: TChefSecret }).json_wrapper;
+ } catch {
+ throw new SecretSyncError({
+ message: `Couldn't decrypt value "${secretKey}" in Chef data bag item ${itemLabel}. Check that the data bag secret on the sync's Chef connection is the one the item was encrypted with.`,
+ shouldRetry: false
+ });
+ }
+};
+
const getChefSecretsRaw = async (
{ serverUrl, userName, privateKey, orgName, dataBagName, dataBagItemName, gatewayId }: TGetChefSecrets,
gatewayV2Service: TChefSyncGatewayV2Service
@@ -81,7 +150,8 @@ const getChefSecrets = async (
// Convert data bag item to key-value pairs
const secrets: ChefSecret[] = [];
- Object.entries(existingSecrets).forEach(([key, value]) => {
+ Object.entries(existingSecrets).forEach(([key, rawValue]) => {
+ const value = isEncryptedValue(rawValue) ? decryptValue(key, rawValue, secretSync) : rawValue;
if (key !== "id" && value !== null && value !== undefined) {
secrets.push({ key, value: String(value) });
}
@@ -99,15 +169,32 @@ const updateChefSecrets = async (
) => {
const {
connection,
- destinationConfig: { dataBagName, dataBagItemName }
+ destinationConfig: { dataBagName, dataBagItemName, shouldEncryptSecrets }
} = secretSync;
- const { serverUrl, userName, privateKey, orgName } = connection.credentials;
+ const { serverUrl, userName, privateKey, orgName, dataBagSecret } = connection.credentials;
+
+ let values: Record<string, TChefSecret | TChefEncryptedValue> = secrets;
+
+ if (shouldEncryptSecrets) {
+ if (!dataBagSecret) {
+ throw new SecretSyncError({
+ message:
+ "Encryption is turned on for this sync, but its Chef connection has no data bag secret. Add the secret to the connection, or turn off encryption for this sync.",
+ shouldRetry: false
+ });
+ }
+
+ const key = getDataBagKey(dataBagSecret);
+ values = Object.fromEntries(
+ Object.entries(secrets).map(([secretKey, value]) => [secretKey, encryptValue(value, key)])
+ );
+ }
// Chef data bag items must have an 'id' field
const dataBagItemContent: TChefDataBagItemContent = {
id,
- ...secrets
+ ...values
};
await updateChefDataBagItem(
diff --git a/backend/src/ee/services/secret-sync/chef/chef-sync-schemas.ts b/backend/src/ee/services/secret-sync/chef/chef-sync-schemas.ts
index 20e9e115ce..102353a42a 100644
--- a/backend/src/ee/services/secret-sync/chef/chef-sync-schemas.ts
+++ b/backend/src/ee/services/secret-sync/chef/chef-sync-schemas.ts
@@ -21,7 +21,12 @@ const ChefSyncDestinationConfigSchema = z.object({
.string()
.min(1, "Data Bag Item Name is required")
.max(256, "Data Bag Item Name cannot exceed 256 characters")
- .describe(SecretSyncs.DESTINATION_CONFIG.CHEF.dataBagItemName)
+ .describe(SecretSyncs.DESTINATION_CONFIG.CHEF.dataBagItemName),
+ shouldEncryptSecrets: z
+ .boolean()
+ .optional()
+ .default(false)
+ .describe(SecretSyncs.DESTINATION_CONFIG.CHEF.shouldEncryptSecrets)
});
const ChefSyncOptionsConfig: TSyncOptionsConfig = { canImportSecrets: true };
diff --git a/backend/src/ee/services/secret-sync/chef/chef-sync-types.ts b/backend/src/ee/services/secret-sync/chef/chef-sync-types.ts
index c95bfacb56..bbc01844d2 100644
--- a/backend/src/ee/services/secret-sync/chef/chef-sync-types.ts
+++ b/backend/src/ee/services/secret-sync/chef/chef-sync-types.ts
@@ -26,9 +26,17 @@ export type TGetChefSecrets = {
export type TChefSecret = string | number | boolean | null;
+export type TChefEncryptedValue = {
+ encrypted_data: string;
+ iv: string;
+ auth_tag: string;
+ version: number;
+ cipher: string;
+};
+
export type TChefDataBagItemContent = {
id: string;
- [key: string]: TChefSecret;
+ [key: string]: TChefSecret | TChefEncryptedValue;
};
export type TChefSecrets = {
diff --git a/backend/src/lib/api-docs/constants.ts b/backend/src/lib/api-docs/constants.ts
index 1164184cc9..259ae954d2 100644
--- a/backend/src/lib/api-docs/constants.ts
+++ b/backend/src/lib/api-docs/constants.ts
@@ -3520,7 +3520,9 @@ export const SecretSyncs = {
},
CHEF: {
dataBagName: "The name of the Chef data bag to sync secrets to.",
- dataBagItemName: "The name of the Chef data bag item to sync secrets to."
+ dataBagItemName: "The name of the Chef data bag item to sync secrets to.",
+ shouldEncryptSecrets:
+ "Whether to write the data bag item as a Chef encrypted data bag item (version 3), using the data bag secret on the Chef connection."
},
CLOUD66: {
stackId: "The unique identifier (uid) of the Cloud 66 stack to sync secrets to.",