Update Checkly groups integration

This commit is contained in:
Tuan Dang
2023-11-02 09:37:36 +02:00
parent f256493cb3
commit 58ff6a43bc
13 changed files with 190 additions and 272 deletions
@@ -1,7 +1,7 @@
import { Request, Response } from "express";
import { Types } from "mongoose";
import { standardRequest } from "../../config/request";
import { getApps, getTeams, getGroups, revokeAccess } from "../../integrations";
import { getApps, getTeams, revokeAccess } from "../../integrations";
import { Bot, IntegrationAuth, Workspace } from "../../models";
import { EventType } from "../../ee/models";
import { IntegrationService } from "../../services";
@@ -10,6 +10,7 @@ import {
ALGORITHM_AES_256_GCM,
ENCODING_SCHEME_UTF8,
INTEGRATION_BITBUCKET_API_URL,
INTEGRATION_CHECKLY_API_URL,
INTEGRATION_GCP_SECRET_MANAGER,
INTEGRATION_NORTHFLANK_API_URL,
INTEGRATION_QOVERY_API_URL,
@@ -207,40 +208,6 @@ export const saveIntegrationToken = async (req: Request, res: Response) => {
});
};
/**
* Return list of groups allowed for integration with integration authorization id [integrationAuthId]
* @param req
* @param res
* @returns
*/
export const getIntegrationAuthGroups = async (req: Request, res: Response) => {
const {
params: { integrationAuthId }
} = await validateRequest(reqValidator.GetIntegrationAuthGroupsV1, req);
const { integrationAuth, accessToken } = await getIntegrationAuthAccessHelper({
integrationAuthId: new ObjectId(integrationAuthId)
});
const { permission } = await getUserProjectPermissions(
req.user._id,
integrationAuth.workspace.toString()
);
ForbiddenError.from(permission).throwUnlessCan(
ProjectPermissionActions.Read,
ProjectPermissionSub.Integrations
);
const groups = await getGroups({
integrationAuth: integrationAuth,
accessToken: accessToken
});
return res.status(200).send({
groups
});
};
/**
* Return list of applications allowed for integration with integration authorization id [integrationAuthId]
* @param req
@@ -378,6 +345,59 @@ export const getIntegrationAuthVercelBranches = async (req: Request, res: Respon
});
};
/**
* Return list of Checkly groups for a specific user
* @param req
* @param res
*/
export const getIntegrationAuthChecklyGroups = async (req: Request, res: Response) => {
const {
params: { integrationAuthId },
query: { accountId }
} = await validateRequest(reqValidator.GetIntegrationAuthChecklyGroupsV1, req);
const { integrationAuth, accessToken } = await getIntegrationAuthAccessHelper({
integrationAuthId: new Types.ObjectId(integrationAuthId)
});
const { permission } = await getUserProjectPermissions(
req.user._id,
integrationAuth.workspace.toString()
);
ForbiddenError.from(permission).throwUnlessCan(
ProjectPermissionActions.Read,
ProjectPermissionSub.Integrations
);
interface ChecklyGroup {
id: number;
name: string;
}
if (accountId && accountId !== "") {
const { data }: { data: ChecklyGroup[] } = (
await standardRequest.get(`${INTEGRATION_CHECKLY_API_URL}/v1/check-groups`, {
headers: {
Authorization: `Bearer ${accessToken}`,
Accept: "application/json",
"X-Checkly-Account": accountId
}
})
);
return res.status(200).send({
groups: data.map((g: ChecklyGroup) => ({
name: g.name,
groupId: g.id,
}))
});
}
return res.status(200).send({
groups: []
});
}
/**
* Return list of Qovery Orgs for a specific user
* @param req
+1 -1
View File
@@ -911,7 +911,7 @@ const getAppsSupabase = async ({ accessToken }: { accessToken: string }) => {
};
/**
* Return list of projects for the Checkly integration
* Return list of accounts for the Checkly integration
* @param {Object} obj
* @param {String} obj.accessToken - api key for the Checkly API
* @returns {Object[]} apps - Сheckly accounts
-96
View File
@@ -1,96 +0,0 @@
import {
IIntegrationAuth,
} from "../models";
import {
INTEGRATION_CHECKLY,
INTEGRATION_CHECKLY_API_URL,
} from "../variables";
import { standardRequest } from "../config/request";
interface Group {
name: string;
groupId: string;
}
/**
* Return list of groups for checkly integration authorization [integrationAuth]
* @param {Object} obj
* @param {String} obj.integrationAuth - integration authorization to get groups
* @param {String} obj.accessToken - access token for integration authorization
* @returns {Object[]} groups - groups for integration authorization
* @returns {String} groups.name - name of group
* @returns {String} groups.groupId - id of group
*/
const getGroups = async ({
integrationAuth,
accessToken,
}: {
integrationAuth: IIntegrationAuth;
accessToken: string;
}) => {
let groups: Group[] = [];
switch (integrationAuth.integration) {
case INTEGRATION_CHECKLY:
groups = await getGroupsCheckly({
accessToken,
});
break;
}
return groups;
}
/**
* Return list of groups for Checkly integration
* @param {Object} obj
* @param {String} obj.accessToken - access token for Checkly API
* @returns {Object[]} groups - list of groups in Checkly
* @returns {String} groups.name - name of group
* @returns {String} groups.groupId - id of group
*/
const getGroupsCheckly = async ({
accessToken,
}: {
accessToken: string;
}) => {
let groups: Group[] = [];
// case: fetch account id
const { data } = await standardRequest.get(`${INTEGRATION_CHECKLY_API_URL}/v1/accounts`, {
headers: {
Authorization: `Bearer ${accessToken}`,
Accept: "application/json"
}
});
const accountId = data.map((a: any) => {
return {
id: a.id,
};
});
// case: fetch list of groups in Checkly
const res = accountId.length > 0 && (
await standardRequest.get(`${INTEGRATION_CHECKLY_API_URL}/v1/check-groups`, {
headers: {
Authorization: `Bearer ${accessToken}`,
Accept: "application/json",
"X-Checkly-Account": accountId[0].id,
}
})
).data;
groups = res.map((g: any) => ({
name: g.name,
groupId: g.id,
}));
return groups;
}
export {
getGroups,
}
-2
View File
@@ -2,7 +2,6 @@ import { exchangeCode } from "./exchange";
import { exchangeRefresh } from "./refresh";
import { getApps } from "./apps";
import { getTeams } from "./teams";
import { getGroups } from "./groups";
import { revokeAccess } from "./revoke";
export {
@@ -10,6 +9,5 @@ export {
exchangeRefresh,
getApps,
getTeams,
getGroups,
revokeAccess,
}
+16 -40
View File
@@ -2104,7 +2104,7 @@ const syncSecretsSupabase = async ({
};
/**
* Sync/push [secrets] to Checkly app
* Sync/push [secrets] to Checkly app/group
* @param {Object} obj
* @param {IIntegration} obj.integration - integration details
* @param {Object} obj.secrets - secrets to push to integration (object where keys are secret keys and values are secret values)
@@ -2123,6 +2123,8 @@ const syncSecretsCheckly = async ({
}) => {
if (integration.targetServiceId) {
// sync secrets to checkly group envars
let getGroupSecretsRes = (
await standardRequest.get(`${INTEGRATION_CHECKLY_API_URL}/v1/check-groups/${integration.targetServiceId}`, {
headers: {
@@ -2162,48 +2164,22 @@ const syncSecretsCheckly = async ({
value: secrets[key].value
}));
// add secrets
for await (const key of Object.keys(secrets)) {
if (!(key in getGroupSecretsRes)) {
// case: secret does not exist in checkly group
// -> add secret
await standardRequest.put(
`${INTEGRATION_CHECKLY_API_URL}/v1/check-groups/${integration.targetServiceId}`,
{
environmentVariables: groupEnvironmentVariables
},
{
headers: {
Authorization: `Bearer ${accessToken}`,
Accept: "application/json",
"X-Checkly-Account": integration.appId
}
}
);
} else {
// case: secret exists in checkly group
// -> update/set secret
if (secrets[key] !== getGroupSecretsRes[key]) {
await standardRequest.put(
`${INTEGRATION_CHECKLY_API_URL}/v1/check-groups/${integration.targetServiceId}`,
{
environmentVariables: groupEnvironmentVariables
},
{
headers: {
Authorization: `Bearer ${accessToken}`,
Accept: "application/json",
"X-Checkly-Account": integration.appId
}
}
);
await standardRequest.put(
`${INTEGRATION_CHECKLY_API_URL}/v1/check-groups/${integration.targetServiceId}`,
{
environmentVariables: groupEnvironmentVariables
},
{
headers: {
Authorization: `Bearer ${accessToken}`,
Accept: "application/json",
"X-Checkly-Account": integration.appId
}
}
}
);
} else {
// sync secrets to checkly global envars
let getSecretsRes = (
await standardRequest.get(`${INTEGRATION_CHECKLY_API_URL}/v1/variables`, {
headers: {
+8 -8
View File
@@ -52,14 +52,6 @@ router.get(
integrationAuthController.getIntegrationAuthTeams
);
router.get(
"/:integrationAuthId/groups",
requireAuth({
acceptedAuthModes: [AuthMode.JWT]
}),
integrationAuthController.getIntegrationAuthGroups
);
router.get(
"/:integrationAuthId/vercel/branches",
requireAuth({
@@ -68,6 +60,14 @@ router.get(
integrationAuthController.getIntegrationAuthVercelBranches
);
router.get(
"/:integrationAuthId/checkly/groups",
requireAuth({
acceptedAuthModes: [AuthMode.JWT]
}),
integrationAuthController.getIntegrationAuthChecklyGroups
);
router.get(
"/:integrationAuthId/qovery/orgs",
requireAuth({
+2 -1
View File
@@ -1,11 +1,12 @@
import Redis, { Redis as TRedis } from "ioredis";
import { logger } from "../utils/logging";
let redisClient: TRedis | null;
if (process.env.REDIS_URL) {
redisClient = new Redis(process.env.REDIS_URL as string);
} else {
console.warn("Redis URL not set, skipping Redis initialization.");
logger.warn("Redis URL not set, skipping Redis initialization.");
redisClient = null;
}
+2 -2
View File
@@ -11,7 +11,6 @@ import {
backfillBots,
backfillEncryptionMetadata,
backfillIntegration,
backfillPermission,
backfillSecretBlindIndexData,
backfillSecretFolders,
backfillSecretVersions,
@@ -28,6 +27,7 @@ import {
} from "./reencryptData";
import { getMongoURL, getNodeEnv, getRedisUrl, getSentryDSN } from "../../config";
import { initializePassport } from "../auth";
import { logger } from "../logging";
/**
* Prepare Infisical upon startup. This includes tasks like:
@@ -41,7 +41,7 @@ import { initializePassport } from "../auth";
*/
export const setup = async () => {
if ((await getRedisUrl()) === undefined || (await getRedisUrl()) === "") {
console.error(
logger.error(
"WARNING: Redis is not yet configured. Infisical may not function as expected without it."
);
}
+4 -1
View File
@@ -117,9 +117,12 @@ export const GetIntegrationAuthVercelBranchesV1 = z.object({
})
});
export const GetIntegrationAuthGroupsV1 = z.object({
export const GetIntegrationAuthChecklyGroupsV1 = z.object({
params: z.object({
integrationAuthId: z.string().trim()
}),
query: z.object({
accountId: z.string().trim()
})
});