feat: add encryptedKey column to Credential table for calendar integrations (#27154)
Adds encrypted credential storage using a new keyring system: - New `encryptedKey` column with AES-256-GCM encryption - Decryption in getCalendarsEvents with fallback to legacy `key` - buildCredentialCreateData service for credential creation - Phase 1: Google Calendar only, other integrations follow
This commit is contained in:
1 parent
80e4b1af1f
commit
fc9c26e8dd
36 files changed
+308
-34
No files matched your search
@@ -17,6 +17,7 @@ import {
|
||||
import { LicenseKeySingleton } from "@calcom/ee/common/server/LicenseKeyService";
|
||||
import { getBillingProviderService } from "@calcom/features/ee/billing/di/containers/Billing";
|
||||
import { CredentialRepository } from "@calcom/features/credentials/repositories/CredentialRepository";
|
||||
import { buildCredentialCreateData } from "@calcom/features/credentials/services/CredentialDataService";
|
||||
import type { TrackingData } from "@calcom/lib/tracking";
|
||||
import { DeploymentRepository } from "@calcom/features/ee/deployment/repositories/DeploymentRepository";
|
||||
import createUsersAndConnectToOrg from "@calcom/features/ee/dsync/lib/users/createUsersAndConnectToOrg";
|
||||
@@ -730,12 +731,13 @@ export const getOptions = ({
|
||||
token_type: account.token_type,
|
||||
expires_at: account.expires_at,
|
||||
};
|
||||
const gcalCredential = await CredentialRepository.create({
|
||||
const gcalCredentialData = buildCredentialCreateData({
|
||||
userId: Number(user.id),
|
||||
key: credentialkey,
|
||||
appId: "google-calendar",
|
||||
type: "google_calendar",
|
||||
});
|
||||
const gcalCredential = await CredentialRepository.create(gcalCredentialData);
|
||||
const gCalService = createGoogleCalendarServiceWithGoogleType({
|
||||
...gcalCredential,
|
||||
user: null,
|
||||
@@ -748,12 +750,13 @@ export const getOptions = ({
|
||||
type: "google_video",
|
||||
}))
|
||||
) {
|
||||
await CredentialRepository.create({
|
||||
const googleMeetCredentialData = buildCredentialCreateData({
|
||||
type: "google_video",
|
||||
key: {},
|
||||
userId: Number(user.id),
|
||||
appId: "google-meet",
|
||||
});
|
||||
await CredentialRepository.create(googleMeetCredentialData);
|
||||
}
|
||||
|
||||
const oAuth2Client = new OAuth2Client(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET);
|
||||
|
||||
@@ -939,6 +939,7 @@ export default class EventManager {
|
||||
invalid: credentialFromDB.invalid,
|
||||
appId: credentialFromDB.appId,
|
||||
user: credentialFromDB.user,
|
||||
encryptedKey: credentialFromDB.encryptedKey,
|
||||
delegatedToId: credentialFromDB.delegatedToId,
|
||||
delegatedTo: credentialFromDB.delegatedTo,
|
||||
delegationCredentialId: credentialFromDB.delegationCredentialId,
|
||||
@@ -1155,6 +1156,7 @@ export default class EventManager {
|
||||
invalid: credentialFromDB.invalid,
|
||||
appId: credentialFromDB.appId,
|
||||
user: credentialFromDB.user,
|
||||
encryptedKey: credentialFromDB.encryptedKey,
|
||||
delegatedToId: credentialFromDB.delegatedToId,
|
||||
delegatedTo: credentialFromDB.delegatedTo,
|
||||
delegationCredentialId: credentialFromDB.delegationCredentialId,
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
import process from "node:process";
|
||||
import { getCalendar } from "@calcom/app-store/_utils/getCalendar";
|
||||
import { symmetricDecrypt } from "@calcom/lib/crypto";
|
||||
import { decryptSecret } from "@calcom/lib/crypto/keyring";
|
||||
import { isDelegationCredential } from "@calcom/lib/delegationCredential";
|
||||
import logger from "@calcom/lib/logger";
|
||||
import { getPiiFreeSelectedCalendar, getPiiFreeCredential } from "@calcom/lib/piiFreeData";
|
||||
import { getPiiFreeCredential, getPiiFreeSelectedCalendar } from "@calcom/lib/piiFreeData";
|
||||
import { safeStringify } from "@calcom/lib/safeStringify";
|
||||
import { performance } from "@calcom/lib/server/perfObserver";
|
||||
import type { CalendarFetchMode, EventBusyDate, SelectedCalendar } from "@calcom/types/Calendar";
|
||||
import type { CredentialForCalendarService } from "@calcom/types/Credential";
|
||||
|
||||
import { normalizeTimezone } from "./timezone-conversion";
|
||||
|
||||
const log = logger.getSubLogger({ prefix: ["getCalendarsEvents"] });
|
||||
@@ -101,7 +102,33 @@ const getCalendarsEvents = async (
|
||||
|
||||
const calendarAndCredentialPairs = await Promise.all(
|
||||
calendarCredentials.map(async (credential) => {
|
||||
const calendar = await getCalendar(credential, mode);
|
||||
let key: typeof credential.key;
|
||||
try {
|
||||
if (credential.encryptedKey) {
|
||||
key = JSON.parse(
|
||||
decryptSecret({
|
||||
envelope: JSON.parse(credential.encryptedKey),
|
||||
aad: { type: credential.type },
|
||||
})
|
||||
);
|
||||
} else {
|
||||
key = credential.key;
|
||||
}
|
||||
} catch {
|
||||
log.warn("Failed to decrypt credential key, falling back to legacy key", {
|
||||
credentialId: credential.id,
|
||||
});
|
||||
key = credential.key;
|
||||
}
|
||||
|
||||
const calendar = await getCalendar(
|
||||
{
|
||||
...credential,
|
||||
// use encrypted secret to get unencrypted calendar creds
|
||||
key,
|
||||
},
|
||||
mode
|
||||
);
|
||||
return [calendar, credential] as const;
|
||||
})
|
||||
);
|
||||
|
||||
@@ -180,6 +180,7 @@ const createMeetingWithCalVideo = async (calEvent: CalendarEvent) => {
|
||||
user: { email: "" },
|
||||
teamId: null,
|
||||
key: dailyAppKeys,
|
||||
encryptedKey: null,
|
||||
invalid: false,
|
||||
delegationCredentialId: null,
|
||||
},
|
||||
@@ -203,6 +204,7 @@ export const createInstantMeetingWithCalVideo = async (endTime: string) => {
|
||||
user: { email: "" },
|
||||
teamId: null,
|
||||
key: dailyAppKeys,
|
||||
encryptedKey: null,
|
||||
invalid: false,
|
||||
delegationCredentialId: null,
|
||||
},
|
||||
@@ -229,6 +231,7 @@ const getRecordingsOfCalVideoByRoomName = async (
|
||||
user: { email: "" },
|
||||
teamId: null,
|
||||
key: dailyAppKeys,
|
||||
encryptedKey: null,
|
||||
invalid: false,
|
||||
delegationCredentialId: null,
|
||||
},
|
||||
@@ -255,6 +258,7 @@ const getDownloadLinkOfCalVideoByRecordingId = async (
|
||||
user: { email: "" },
|
||||
teamId: null,
|
||||
key: dailyAppKeys,
|
||||
encryptedKey: null,
|
||||
invalid: false,
|
||||
delegationCredentialId: null,
|
||||
},
|
||||
@@ -279,6 +283,7 @@ const getAllTranscriptsAccessLinkFromRoomName = async (roomName: string) => {
|
||||
user: { email: "" },
|
||||
teamId: null,
|
||||
key: dailyAppKeys,
|
||||
encryptedKey: null,
|
||||
invalid: false,
|
||||
delegationCredentialId: null,
|
||||
},
|
||||
@@ -303,6 +308,7 @@ const getAllTranscriptsAccessLinkFromMeetingId = async (meetingId: string) => {
|
||||
user: { email: "" },
|
||||
teamId: null,
|
||||
key: dailyAppKeys,
|
||||
encryptedKey: null,
|
||||
invalid: false,
|
||||
delegationCredentialId: null,
|
||||
},
|
||||
@@ -327,6 +333,7 @@ const submitBatchProcessorTranscriptionJob = async (recordingId: string) => {
|
||||
user: { email: "" },
|
||||
teamId: null,
|
||||
key: dailyAppKeys,
|
||||
encryptedKey: null,
|
||||
invalid: false,
|
||||
delegationCredentialId: null,
|
||||
},
|
||||
@@ -363,6 +370,7 @@ const getTranscriptsAccessLinkFromRecordingId = async (recordingId: string) => {
|
||||
user: { email: "" },
|
||||
teamId: null,
|
||||
key: dailyAppKeys,
|
||||
encryptedKey: null,
|
||||
invalid: false,
|
||||
delegationCredentialId: null,
|
||||
},
|
||||
@@ -388,6 +396,7 @@ const checkIfRoomNameMatchesInRecording = async (roomName: string, recordingId:
|
||||
user: { email: "" },
|
||||
teamId: null,
|
||||
key: dailyAppKeys,
|
||||
encryptedKey: null,
|
||||
invalid: false,
|
||||
delegationCredentialId: null,
|
||||
},
|
||||
@@ -413,6 +422,7 @@ const getCalVideoMeetingSessionsByRoomName = async (roomName: string) => {
|
||||
user: { email: "" },
|
||||
teamId: null,
|
||||
key: dailyAppKeys,
|
||||
encryptedKey: null,
|
||||
invalid: false,
|
||||
delegationCredentialId: null,
|
||||
},
|
||||
|
||||
@@ -13,6 +13,7 @@ type CredentialCreateInput = {
|
||||
userId: number;
|
||||
appId: string;
|
||||
delegationCredentialId?: string | null;
|
||||
encryptedKey?: string | null;
|
||||
};
|
||||
|
||||
type CredentialUpdateInput = {
|
||||
@@ -45,7 +46,9 @@ export class CredentialRepository {
|
||||
}
|
||||
|
||||
static async create(data: CredentialCreateInput) {
|
||||
const credential = await prisma.credential.create({ data: { ...data } });
|
||||
const credential = await prisma.credential.create({
|
||||
data,
|
||||
});
|
||||
return buildNonDelegationCredential(credential);
|
||||
}
|
||||
static async findByAppIdAndUserId({
|
||||
@@ -81,7 +84,7 @@ export class CredentialRepository {
|
||||
static async findFirstByIdWithKeyAndUser({ id }: { id: number }) {
|
||||
const credential = await prisma.credential.findUnique({
|
||||
where: { id },
|
||||
select: { ...safeCredentialSelect, key: true },
|
||||
select: { ...safeCredentialSelect, key: true, encryptedKey: true },
|
||||
});
|
||||
return buildNonDelegationCredential(credential);
|
||||
}
|
||||
@@ -276,15 +279,17 @@ export class CredentialRepository {
|
||||
type,
|
||||
key,
|
||||
appId,
|
||||
encryptedKey,
|
||||
}: {
|
||||
userId: number;
|
||||
delegationCredentialId: string;
|
||||
type: string;
|
||||
key: Prisma.InputJsonValue;
|
||||
appId: string;
|
||||
encryptedKey?: string | null;
|
||||
}) {
|
||||
return prisma.credential.create({
|
||||
data: { userId, delegationCredentialId, type, key, appId },
|
||||
data: { userId, delegationCredentialId, type, key, appId, ...(encryptedKey && { encryptedKey }) },
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
import { encryptSecret } from "@calcom/lib/crypto/keyring";
|
||||
|
||||
export type CredentialCreateData = {
|
||||
type: string;
|
||||
key: object;
|
||||
userId: number;
|
||||
appId: string;
|
||||
delegationCredentialId?: string | null;
|
||||
encryptedKey?: string | null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Builds the data object for creating a credential, including the encrypted key.
|
||||
* This service handles the encryption logic so the repository stays focused on data access.
|
||||
*
|
||||
* @param data The credential data without encryptedKey
|
||||
* @returns The credential data with encryptedKey populated if encryption key is available
|
||||
*/
|
||||
export function buildCredentialCreateData(data: {
|
||||
type: string;
|
||||
key: object;
|
||||
userId: number;
|
||||
appId: string;
|
||||
delegationCredentialId?: string | null;
|
||||
}): CredentialCreateData {
|
||||
const aad = {
|
||||
type: data.type,
|
||||
};
|
||||
|
||||
let encryptedKey: ReturnType<typeof encryptSecret> | null = null;
|
||||
try {
|
||||
encryptedKey = encryptSecret({ ring: "CREDENTIALS", plaintext: JSON.stringify(data.key), aad });
|
||||
} catch {
|
||||
// Encryption keyring not configured - this is expected in tests and some environments
|
||||
// The encryptedKey will be null, which is fine for backwards compatibility
|
||||
}
|
||||
|
||||
return {
|
||||
...data,
|
||||
key: data.key,
|
||||
...(encryptedKey && { encryptedKey: JSON.stringify(encryptedKey) }),
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user