feat(infisical-pg): fixed bugs in integrations and self hosted license failure

This commit is contained in:
Akhil Mohan
2024-01-15 13:14:49 +05:30
parent 87709dc86f
commit d90fdac5ce
9 changed files with 63 additions and 49 deletions

View File

@@ -85,7 +85,11 @@ export const licenseServiceFactory = ({
logger.info(`Instance type: ${InstanceType.EnterpriseOnPrem}`);
isValidLicense = true;
}
return;
}
// this means this is self hosted oss version
// else it would reach catch statement
isValidLicense = true;
} catch (error) {
logger.error(error);
}

View File

@@ -19,6 +19,7 @@ import {
import { TSnapshotDalFactory } from "./snapshot-dal";
import { TSnapshotFolderDalFactory } from "./snapshot-folder-dal";
import { TSnapshotSecretDalFactory } from "./snapshot-secret-dal";
import { logger } from "@app/lib/logger";
type TSecretSnapshotServiceFactoryDep = {
snapshotDal: TSnapshotDalFactory;
@@ -112,44 +113,50 @@ export const secretSnapshotServiceFactory = ({
};
const performSnapshot = async (folderId: string) => {
if (!licenseService.isValidLicense)
throw new InternalServerError({ message: "Invalid license" });
try {
if (!licenseService.isValidLicense)
throw new InternalServerError({ message: "Invalid license" });
const snapshot = await snapshotDal.transaction(async (tx) => {
const folder = await folderDal.findById(folderId, tx);
if (!folder) throw new BadRequestError({ message: "Folder not found" });
const snapshot = await snapshotDal.transaction(async (tx) => {
const folder = await folderDal.findById(folderId, tx);
if (!folder) throw new BadRequestError({ message: "Folder not found" });
const secretVersions = await secretVersionDal.findLatestVersionByFolderId(folderId, tx);
const folderVersions = await folderVersionDal.findLatestVersionByFolderId(folderId, tx);
const newSnapshot = await snapshotDal.create(
{
folderId,
envId: folder.environment.envId,
parentFolderId: folder.parentId
},
tx
);
const snapshotSecrets = await snapshotSecretDal.insertMany(
secretVersions.map(({ id }) => ({
secretVersionId: id,
envId: folder.environment.envId,
snapshotId: newSnapshot.id
})),
tx
);
const snapshotFolders = await snapshotFolderDal.insertMany(
folderVersions.map(({ id }) => ({
folderVersionId: id,
envId: folder.environment.envId,
snapshotId: newSnapshot.id
})),
tx
);
const secretVersions = await secretVersionDal.findLatestVersionByFolderId(folderId, tx);
const folderVersions = await folderVersionDal.findLatestVersionByFolderId(folderId, tx);
const newSnapshot = await snapshotDal.create(
{
folderId,
envId: folder.environment.envId,
parentFolderId: folder.parentId
},
tx
);
const snapshotSecrets = await snapshotSecretDal.insertMany(
secretVersions.map(({ id }) => ({
secretVersionId: id,
envId: folder.environment.envId,
snapshotId: newSnapshot.id
})),
tx
);
const snapshotFolders = await snapshotFolderDal.insertMany(
folderVersions.map(({ id }) => ({
folderVersionId: id,
envId: folder.environment.envId,
snapshotId: newSnapshot.id
})),
tx
);
return { ...newSnapshot, secrets: snapshotSecrets, folder: snapshotFolders };
});
return { ...newSnapshot, secrets: snapshotSecrets, folder: snapshotFolders };
});
return snapshot;
return snapshot;
} catch (error) {
// this to avoid snapshot errors
logger.error("Failed to perform snasphot");
logger.error(error);
}
};
const rollbackSnapshot = async ({ id: snapshotId, actor, actorId }: TRollbackSnapshotDTO) => {

View File

@@ -330,7 +330,7 @@ export const registerProjectRouter = async (server: FastifyZodProvider) => {
},
onRequest: verifyAuth([AuthMode.JWT]),
handler: async (req) => {
const authorizations = await server.services.integration.listIntegrationByProject({
const authorizations = await server.services.integrationAuth.listIntegrationAuthByProjectId({
actorId: req.permission.id,
actor: req.permission.type,
projectId: req.params.workspaceId

View File

@@ -16,7 +16,6 @@ export const registerSsoRouter = async (server: FastifyZodProvider) => {
await server.register(fastifySession, { secret: appCfg.COOKIE_SECRET_SIGN_KEY });
await server.register(passport.initialize());
await server.register(passport.secureSession());
// passport oauth strategy for Google
const isGoogleOauthActive = Boolean(
appCfg.CLIENT_ID_GOOGLE_LOGIN && appCfg.CLIENT_SECRET_GOOGLE_LOGIN
@@ -123,7 +122,7 @@ export const registerSsoRouter = async (server: FastifyZodProvider) => {
}
server.route({
url: "/sso/redirect/google",
url: "/redirect/google",
method: "GET",
schema: {
querystring: z.object({
@@ -144,7 +143,7 @@ export const registerSsoRouter = async (server: FastifyZodProvider) => {
});
server.route({
url: "/sso/google",
url: "/google",
method: "GET",
preValidation: passport.authenticate("google", {
session: false,
@@ -169,7 +168,7 @@ export const registerSsoRouter = async (server: FastifyZodProvider) => {
});
server.route({
url: "/sso/redirect/github",
url: "/redirect/github",
method: "GET",
schema: {
querystring: z.object({
@@ -189,7 +188,7 @@ export const registerSsoRouter = async (server: FastifyZodProvider) => {
});
server.route({
url: "/sso/github",
url: "/github",
method: "GET",
preValidation: passport.authenticate("github", {
session: false,
@@ -214,7 +213,7 @@ export const registerSsoRouter = async (server: FastifyZodProvider) => {
});
server.route({
url: "/sso/redirect/gitlab",
url: "/redirect/gitlab",
method: "GET",
schema: {
querystring: z.object({
@@ -234,7 +233,7 @@ export const registerSsoRouter = async (server: FastifyZodProvider) => {
});
server.route({
url: "/sso/gitlab",
url: "/gitlab",
method: "GET",
preValidation: passport.authenticate("gitlab", {
session: false,

View File

@@ -102,7 +102,7 @@ export const getIntegrationOptions = async () => {
isAvailable: true,
type: "oauth",
clientId: "",
clientSlug: appCfg.CLIENT_ID_VERCEL,
clientSlug: appCfg.CLIENT_SLUG_VERCEL,
docsLink: ""
},
{
@@ -120,7 +120,7 @@ export const getIntegrationOptions = async () => {
image: "GitHub.png",
isAvailable: true,
type: "oauth",
clientId: appCfg.CLIENT_ID_BITBUCKET,
clientId: appCfg.CLIENT_ID_GITHUB,
docsLink: ""
},
{

View File

@@ -50,7 +50,9 @@ export const integrationDalFactory = (db: TDbClient) => {
const findById = async (id: string, tx?: Knex) => {
try {
const doc = await integrationFindQuery(tx || db, { id }).first();
const doc = await integrationFindQuery(tx || db, {
[`${TableName.Integration}.id` as "id"]: id
}).first();
if (!doc) return;
const { envName: name, envSlug: slug, envId, ...el } = doc;

View File

@@ -159,7 +159,6 @@ export const secretQueueFactory = ({
};
queueService.start(QueueName.IntegrationSync, async (job) => {
logger.info("Secret integration sync started", job.data, job.id);
const { environment, projectId, secretPath } = job.data;
const folder = await folderDal.findBySecretPath(projectId, environment, secretPath);
if (!folder) {
@@ -173,6 +172,8 @@ export const secretQueueFactory = ({
isActive && isSamePath(secretPath, integrationSecPath)
);
if (!integrations.length) return;
logger.info("Secret integration sync started", job.data, job.id);
for (const integration of toBeSyncedIntegrations) {
const integrationAuth = {
...integration.integrationAuth,
@@ -222,9 +223,7 @@ export const secretQueueFactory = ({
});
queueService.start(QueueName.SecretWebhook, async (job) => {
logger.info("Secret webhook job started", job.data, job.id);
await fnTriggerWebhook({ ...job.data, projectEnvDal, webhookDal });
logger.info("Secret webhook job ended", job.id);
});
return { syncSecrets };

View File

@@ -569,7 +569,7 @@ export const secretServiceFactory = ({
secretDal,
folderDal
});
for (let i = importedSecrets.length; i >= 0; i -= 1) {
for (let i = importedSecrets.length - 1; i >= 0; i -= 1) {
for (let j = 0; j < importedSecrets[i].secrets.length; j += 1) {
if (secretBlindIndex === importedSecrets[i].secrets[j].secretBlindIndex) {
return importedSecrets[i].secrets[j];

View File

@@ -10,6 +10,7 @@ import { BadRequestError } from "@app/lib/errors";
import { TProjectEnvDalFactory } from "../project-env/project-env-dal";
import { TWebhookDalFactory } from "./webhook-dal";
import { logger } from "@app/lib/logger";
const WEBHOOK_TRIGGER_TIMEOUT = 15 * 1000;
export const triggerWebhookRequest = async (
@@ -93,6 +94,7 @@ export const fnTriggerWebhook = async ({
!isDisabled && picomatch.isMatch(secretPath, hookSecretPath, { strictSlashes: false })
);
if (!toBeTriggeredHooks.length) return;
logger.info("Secret webhook job started", { environment, secretPath, projectId });
const webhooksTriggered = await Promise.allSettled(
toBeTriggeredHooks.map((hook) =>
triggerWebhookRequest(
@@ -133,4 +135,5 @@ export const fnTriggerWebhook = async ({
);
}
});
logger.info("Secret webhook job ended", { environment, secretPath, projectId });
};