Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature(Backend): Secret Syncs #2952

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backend/src/@types/fastify.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ import { TSecretFolderServiceFactory } from "@app/services/secret-folder/secret-
import { TSecretImportServiceFactory } from "@app/services/secret-import/secret-import-service";
import { TSecretReplicationServiceFactory } from "@app/services/secret-replication/secret-replication-service";
import { TSecretSharingServiceFactory } from "@app/services/secret-sharing/secret-sharing-service";
import { TSecretSyncServiceFactory } from "@app/services/secret-sync/secret-sync-service";
import { TSecretTagServiceFactory } from "@app/services/secret-tag/secret-tag-service";
import { TServiceTokenServiceFactory } from "@app/services/service-token/service-token-service";
import { TSlackServiceFactory } from "@app/services/slack/slack-service";
Expand Down Expand Up @@ -210,6 +211,7 @@ declare module "fastify" {
projectTemplate: TProjectTemplateServiceFactory;
totp: TTotpServiceFactory;
appConnection: TAppConnectionServiceFactory;
secretSync: TSecretSyncServiceFactory;
};
// this is exclusive use for middlewares in which we need to inject data
// everywhere else access using service layer
Expand Down
2 changes: 2 additions & 0 deletions backend/src/@types/knex.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ import {
TExternalGroupOrgRoleMappingsInsert,
TExternalGroupOrgRoleMappingsUpdate
} from "@app/db/schemas/external-group-org-role-mappings";
import { TSecretSyncs, TSecretSyncsInsert, TSecretSyncsUpdate } from "@app/db/schemas/secret-syncs";
import {
TSecretV2TagJunction,
TSecretV2TagJunctionInsert,
Expand Down Expand Up @@ -892,5 +893,6 @@ declare module "knex/types/tables" {
TAppConnectionsInsert,
TAppConnectionsUpdate
>;
[TableName.SecretSync]: KnexOriginal.CompositeTableType<TSecretSyncs, TSecretSyncsInsert, TSecretSyncsUpdate>;
}
}
47 changes: 47 additions & 0 deletions backend/src/db/migrations/20241219210911_secret-sync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Knex } from "knex";

import { TableName } from "@app/db/schemas";
import { createOnUpdateTrigger, dropOnUpdateTrigger } from "@app/db/utils";

export async function up(knex: Knex): Promise<void> {
if (!(await knex.schema.hasTable(TableName.SecretSync))) {
await knex.schema.createTable(TableName.SecretSync, (t) => {
t.uuid("id", { primaryKey: true }).defaultTo(knex.fn.uuid());
t.string("name", 32).notNullable();
t.string("description");
t.string("destination").notNullable();
t.boolean("isEnabled").notNullable().defaultTo(true);
t.integer("version").defaultTo(1).notNullable();
t.jsonb("destinationConfig").notNullable();
t.jsonb("syncOptions").notNullable();
t.string("secretPath").notNullable();
scott-ray-wilson marked this conversation as resolved.
Show resolved Hide resolved
t.uuid("envId").notNullable();
t.foreign("envId").references("id").inTable(TableName.Environment).onDelete("CASCADE");
t.uuid("connectionId").notNullable();
t.foreign("connectionId").references("id").inTable(TableName.AppConnection);
t.timestamps(true, true, true);
// sync
t.enum("syncStatus", ["pending", "success", "failed"]);
scott-ray-wilson marked this conversation as resolved.
Show resolved Hide resolved
t.string("lastSyncJobId");
t.string("lastSyncMessage");
t.datetime("lastSyncedAt");
// import
t.enum("importStatus", ["pending", "success", "failed"]);
scott-ray-wilson marked this conversation as resolved.
Show resolved Hide resolved
t.string("lastImportJobId");
t.string("lastImportMessage");
t.datetime("lastImportedAt");
// erase
t.enum("eraseStatus", ["pending", "success", "failed"]);
t.string("lastEraseJobId");
t.string("lastEraseMessage");
t.datetime("lastErasedAt");
});

await createOnUpdateTrigger(knex, TableName.SecretSync);
}
}

export async function down(knex: Knex): Promise<void> {
await knex.schema.dropTableIfExists(TableName.SecretSync);
await dropOnUpdateTrigger(knex, TableName.SecretSync);
}
3 changes: 2 additions & 1 deletion backend/src/db/schemas/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ export enum TableName {
WorkflowIntegrations = "workflow_integrations",
SlackIntegrations = "slack_integrations",
ProjectSlackConfigs = "project_slack_configs",
AppConnection = "app_connections"
AppConnection = "app_connections",
SecretSync = "secret_syncs"
}

export type TImmutableDBKeys = "id" | "createdAt" | "updatedAt";
Expand Down
40 changes: 40 additions & 0 deletions backend/src/db/schemas/secret-syncs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Code generated by automation script, DO NOT EDIT.
// Automated by pulling database and generating zod schema
// To update. Just run npm run generate:schema
// Written by akhilmhdh.

import { z } from "zod";

import { TImmutableDBKeys } from "./models";

export const SecretSyncsSchema = z.object({
id: z.string().uuid(),
name: z.string(),
description: z.string().nullable().optional(),
destination: z.string(),
isEnabled: z.boolean().default(true),
version: z.number().default(1),
destinationConfig: z.unknown(),
syncOptions: z.unknown(),
secretPath: z.string(),
envId: z.string().uuid(),
connectionId: z.string().uuid(),
createdAt: z.date(),
updatedAt: z.date(),
syncStatus: z.string().nullable().optional(),
lastSyncJobId: z.string().nullable().optional(),
lastSyncMessage: z.string().nullable().optional(),
lastSyncedAt: z.date().nullable().optional(),
importStatus: z.string().nullable().optional(),
lastImportJobId: z.string().nullable().optional(),
lastImportMessage: z.string().nullable().optional(),
lastImportedAt: z.date().nullable().optional(),
eraseStatus: z.string().nullable().optional(),
lastEraseJobId: z.string().nullable().optional(),
lastEraseMessage: z.string().nullable().optional(),
lastErasedAt: z.date().nullable().optional()
});

export type TSecretSyncs = z.infer<typeof SecretSyncsSchema>;
export type TSecretSyncsInsert = Omit<z.input<typeof SecretSyncsSchema>, TImmutableDBKeys>;
export type TSecretSyncsUpdate = Partial<Omit<z.input<typeof SecretSyncsSchema>, TImmutableDBKeys>>;
5 changes: 3 additions & 2 deletions backend/src/ee/routes/v1/org-role-router.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { z } from "zod";

import { OrgMembershipRole, OrgMembershipsSchema, OrgRolesSchema } from "@app/db/schemas";
import { OrgPermissionSchema } from "@app/ee/services/permission/org-permission";
import { readLimit, writeLimit } from "@app/server/config/rateLimiter";
import { slugSchema } from "@app/server/lib/schemas";
import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
Expand All @@ -24,7 +25,7 @@ export const registerOrgRoleRouter = async (server: FastifyZodProvider) => {
),
name: z.string().trim(),
description: z.string().trim().nullish(),
permissions: z.any().array()
permissions: OrgPermissionSchema.array()
}),
response: {
200: z.object({
Expand Down Expand Up @@ -96,7 +97,7 @@ export const registerOrgRoleRouter = async (server: FastifyZodProvider) => {
.optional(),
name: z.string().trim().optional(),
description: z.string().trim().nullish(),
permissions: z.any().array().optional()
permissions: OrgPermissionSchema.array().optional()
}),
response: {
200: z.object({
Expand Down
3 changes: 2 additions & 1 deletion backend/src/ee/services/audit-log/audit-log-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ export const auditLogServiceFactory = ({
}
// add all cases in which project id or org id cannot be added
if (data.event.type !== EventType.LOGIN_IDENTITY_UNIVERSAL_AUTH) {
if (!data.projectId && !data.orgId) throw new BadRequestError({ message: "Must either project id or org id" });
if (!data.projectId && !data.orgId)
throw new BadRequestError({ message: "Must specify either project id or org id" });
}

return auditLogQueue.pushToLog(data);
Expand Down
109 changes: 107 additions & 2 deletions backend/src/ee/services/audit-log/audit-log-types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { TSecretSyncs } from "@app/db/schemas/secret-syncs";
import {
TCreateProjectTemplateDTO,
TUpdateProjectTemplateDTO
Expand All @@ -13,6 +14,12 @@ import { CertKeyAlgorithm } from "@app/services/certificate/certificate-types";
import { CaStatus } from "@app/services/certificate-authority/certificate-authority-types";
import { TIdentityTrustedIp } from "@app/services/identity/identity-types";
import { PkiItemType } from "@app/services/pki-collection/pki-collection-types";
import { SecretSync } from "@app/services/secret-sync/secret-sync-enums";
import {
TCreateSecretSyncDTO,
TDeleteSecretSyncDTO,
TUpdateSecretSyncDTO
} from "@app/services/secret-sync/secret-sync-types";

export type TListProjectAuditLogDTO = {
filter: {
Expand Down Expand Up @@ -226,10 +233,19 @@ export enum EventType {
DELETE_PROJECT_TEMPLATE = "delete-project-template",
APPLY_PROJECT_TEMPLATE = "apply-project-template",
GET_APP_CONNECTIONS = "get-app-connections",
GET_AVAILABLE_APP_CONNECTIONS_DETAILS = "get-available-app-connections-details",
GET_APP_CONNECTION = "get-app-connection",
CREATE_APP_CONNECTION = "create-app-connection",
UPDATE_APP_CONNECTION = "update-app-connection",
DELETE_APP_CONNECTION = "delete-app-connection"
DELETE_APP_CONNECTION = "delete-app-connection",
GET_SECRET_SYNCS = "get-secret-syncs",
GET_SECRET_SYNC = "get-secret-sync",
CREATE_SECRET_SYNC = "create-secret-sync",
UPDATE_SECRET_SYNC = "update-secret-sync",
DELETE_SECRET_SYNC = "delete-secret-sync",
SYNC_SECRET_SYNC = "sync-secret-sync",
IMPORT_SECRET_SYNC = "import-secret-sync",
ERASE_SECRET_SYNC = "erase-secret-sync"
}

interface UserActorMetadata {
Expand Down Expand Up @@ -1883,6 +1899,15 @@ interface GetAppConnectionsEvent {
};
}

interface GetAvailableAppConnectionsDetailsEvent {
type: EventType.GET_AVAILABLE_APP_CONNECTIONS_DETAILS;
metadata: {
app?: AppConnection;
count: number;
connectionIds: string[];
};
}

interface GetAppConnectionEvent {
type: EventType.GET_APP_CONNECTION;
metadata: {
Expand All @@ -1907,6 +1932,77 @@ interface DeleteAppConnectionEvent {
};
}

interface GetSecretSyncsEvent {
type: EventType.GET_SECRET_SYNCS;
metadata: {
destination?: SecretSync;
count: number;
syncIds: string[];
};
}

interface GetSecretSyncEvent {
type: EventType.GET_SECRET_SYNC;
metadata: {
destination: SecretSync;
syncId: string;
};
}

interface CreateSecretSyncEvent {
type: EventType.CREATE_SECRET_SYNC;
metadata: TCreateSecretSyncDTO & { syncId: string };
}

interface UpdateSecretSyncEvent {
type: EventType.UPDATE_SECRET_SYNC;
metadata: TUpdateSecretSyncDTO;
}

interface DeleteSecretSyncEvent {
type: EventType.DELETE_SECRET_SYNC;
metadata: TDeleteSecretSyncDTO;
}

interface SyncSecretSyncEvent {
type: EventType.SYNC_SECRET_SYNC;
metadata: Pick<
TSecretSyncs,
"syncOptions" | "destinationConfig" | "destination" | "syncStatus" | "envId" | "secretPath" | "connectionId"
> & {
syncId: string;
syncMessage: string | null;
jobId: string;
jobRanAt: Date;
};
}

interface ImportSecretSyncEvent {
type: EventType.IMPORT_SECRET_SYNC;
metadata: Pick<
TSecretSyncs,
"syncOptions" | "destinationConfig" | "destination" | "importStatus" | "envId" | "secretPath" | "connectionId"
> & {
syncId: string;
importMessage: string | null;
jobId: string;
jobRanAt: Date;
};
}

interface EraseSecretSyncEvent {
type: EventType.ERASE_SECRET_SYNC;
metadata: Pick<
TSecretSyncs,
"syncOptions" | "destinationConfig" | "destination" | "eraseStatus" | "envId" | "secretPath" | "connectionId"
> & {
syncId: string;
eraseMessage: string | null;
jobId: string;
jobRanAt: Date;
};
}

export type Event =
| GetSecretsEvent
| GetSecretEvent
Expand Down Expand Up @@ -2080,7 +2176,16 @@ export type Event =
| DeleteProjectTemplateEvent
| ApplyProjectTemplateEvent
| GetAppConnectionsEvent
| GetAvailableAppConnectionsDetailsEvent
| GetAppConnectionEvent
| CreateAppConnectionEvent
| UpdateAppConnectionEvent
| DeleteAppConnectionEvent;
| DeleteAppConnectionEvent
| GetSecretSyncsEvent
| GetSecretSyncEvent
| CreateSecretSyncEvent
| UpdateSecretSyncEvent
| DeleteSecretSyncEvent
| SyncSecretSyncEvent
| ImportSecretSyncEvent
| EraseSecretSyncEvent;
Loading
Loading