mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-14 14:34:09 +00:00
Merge branch 'main' into feat/remindersImprovement
This commit is contained in:
@@ -27,7 +27,10 @@ export enum AppConnection {
|
||||
Render = "render",
|
||||
Flyio = "flyio",
|
||||
GitLab = "gitlab",
|
||||
Cloudflare = "cloudflare"
|
||||
Cloudflare = "cloudflare",
|
||||
Zabbix = "zabbix",
|
||||
Railway = "railway",
|
||||
Bitbucket = "bitbucket"
|
||||
}
|
||||
|
||||
export enum AWSRegion {
|
||||
|
||||
@@ -50,6 +50,11 @@ import {
|
||||
getAzureKeyVaultConnectionListItem,
|
||||
validateAzureKeyVaultConnectionCredentials
|
||||
} from "./azure-key-vault";
|
||||
import {
|
||||
BitbucketConnectionMethod,
|
||||
getBitbucketConnectionListItem,
|
||||
validateBitbucketConnectionCredentials
|
||||
} from "./bitbucket";
|
||||
import { CamundaConnectionMethod, getCamundaConnectionListItem, validateCamundaConnectionCredentials } from "./camunda";
|
||||
import { CloudflareConnectionMethod } from "./cloudflare/cloudflare-connection-enum";
|
||||
import {
|
||||
@@ -86,6 +91,7 @@ import { getMsSqlConnectionListItem, MsSqlConnectionMethod } from "./mssql";
|
||||
import { MySqlConnectionMethod } from "./mysql/mysql-connection-enums";
|
||||
import { getMySqlConnectionListItem } from "./mysql/mysql-connection-fns";
|
||||
import { getPostgresConnectionListItem, PostgresConnectionMethod } from "./postgres";
|
||||
import { getRailwayConnectionListItem, validateRailwayConnectionCredentials } from "./railway";
|
||||
import { RenderConnectionMethod } from "./render/render-connection-enums";
|
||||
import { getRenderConnectionListItem, validateRenderConnectionCredentials } from "./render/render-connection-fns";
|
||||
import {
|
||||
@@ -105,6 +111,7 @@ import {
|
||||
validateWindmillConnectionCredentials,
|
||||
WindmillConnectionMethod
|
||||
} from "./windmill";
|
||||
import { getZabbixConnectionListItem, validateZabbixConnectionCredentials, ZabbixConnectionMethod } from "./zabbix";
|
||||
|
||||
export const listAppConnectionOptions = () => {
|
||||
return [
|
||||
@@ -136,7 +143,10 @@ export const listAppConnectionOptions = () => {
|
||||
getRenderConnectionListItem(),
|
||||
getFlyioConnectionListItem(),
|
||||
getGitLabConnectionListItem(),
|
||||
getCloudflareConnectionListItem()
|
||||
getCloudflareConnectionListItem(),
|
||||
getZabbixConnectionListItem(),
|
||||
getRailwayConnectionListItem(),
|
||||
getBitbucketConnectionListItem()
|
||||
].sort((a, b) => a.name.localeCompare(b.name));
|
||||
};
|
||||
|
||||
@@ -216,7 +226,10 @@ export const validateAppConnectionCredentials = async (
|
||||
[AppConnection.Render]: validateRenderConnectionCredentials as TAppConnectionCredentialsValidator,
|
||||
[AppConnection.Flyio]: validateFlyioConnectionCredentials as TAppConnectionCredentialsValidator,
|
||||
[AppConnection.GitLab]: validateGitLabConnectionCredentials as TAppConnectionCredentialsValidator,
|
||||
[AppConnection.Cloudflare]: validateCloudflareConnectionCredentials as TAppConnectionCredentialsValidator
|
||||
[AppConnection.Cloudflare]: validateCloudflareConnectionCredentials as TAppConnectionCredentialsValidator,
|
||||
[AppConnection.Zabbix]: validateZabbixConnectionCredentials as TAppConnectionCredentialsValidator,
|
||||
[AppConnection.Railway]: validateRailwayConnectionCredentials as TAppConnectionCredentialsValidator,
|
||||
[AppConnection.Bitbucket]: validateBitbucketConnectionCredentials as TAppConnectionCredentialsValidator
|
||||
};
|
||||
|
||||
return VALIDATE_APP_CONNECTION_CREDENTIALS_MAP[appConnection.app](appConnection);
|
||||
@@ -253,6 +266,8 @@ export const getAppConnectionMethodName = (method: TAppConnection["method"]) =>
|
||||
case VercelConnectionMethod.ApiToken:
|
||||
case OnePassConnectionMethod.ApiToken:
|
||||
case CloudflareConnectionMethod.APIToken:
|
||||
case BitbucketConnectionMethod.ApiToken:
|
||||
case ZabbixConnectionMethod.ApiToken:
|
||||
return "API Token";
|
||||
case PostgresConnectionMethod.UsernameAndPassword:
|
||||
case MsSqlConnectionMethod.UsernameAndPassword:
|
||||
@@ -332,7 +347,10 @@ export const TRANSITION_CONNECTION_CREDENTIALS_TO_PLATFORM: Record<
|
||||
[AppConnection.Render]: platformManagedCredentialsNotSupported,
|
||||
[AppConnection.Flyio]: platformManagedCredentialsNotSupported,
|
||||
[AppConnection.GitLab]: platformManagedCredentialsNotSupported,
|
||||
[AppConnection.Cloudflare]: platformManagedCredentialsNotSupported
|
||||
[AppConnection.Cloudflare]: platformManagedCredentialsNotSupported,
|
||||
[AppConnection.Zabbix]: platformManagedCredentialsNotSupported,
|
||||
[AppConnection.Railway]: platformManagedCredentialsNotSupported,
|
||||
[AppConnection.Bitbucket]: platformManagedCredentialsNotSupported
|
||||
};
|
||||
|
||||
export const enterpriseAppCheck = async (
|
||||
|
||||
@@ -29,7 +29,10 @@ export const APP_CONNECTION_NAME_MAP: Record<AppConnection, string> = {
|
||||
[AppConnection.Render]: "Render",
|
||||
[AppConnection.Flyio]: "Fly.io",
|
||||
[AppConnection.GitLab]: "GitLab",
|
||||
[AppConnection.Cloudflare]: "Cloudflare"
|
||||
[AppConnection.Cloudflare]: "Cloudflare",
|
||||
[AppConnection.Zabbix]: "Zabbix",
|
||||
[AppConnection.Railway]: "Railway",
|
||||
[AppConnection.Bitbucket]: "Bitbucket"
|
||||
};
|
||||
|
||||
export const APP_CONNECTION_PLAN_MAP: Record<AppConnection, AppConnectionPlanType> = {
|
||||
@@ -61,5 +64,8 @@ export const APP_CONNECTION_PLAN_MAP: Record<AppConnection, AppConnectionPlanTyp
|
||||
[AppConnection.Render]: AppConnectionPlanType.Regular,
|
||||
[AppConnection.Flyio]: AppConnectionPlanType.Regular,
|
||||
[AppConnection.GitLab]: AppConnectionPlanType.Regular,
|
||||
[AppConnection.Cloudflare]: AppConnectionPlanType.Regular
|
||||
[AppConnection.Cloudflare]: AppConnectionPlanType.Regular,
|
||||
[AppConnection.Zabbix]: AppConnectionPlanType.Regular,
|
||||
[AppConnection.Railway]: AppConnectionPlanType.Regular,
|
||||
[AppConnection.Bitbucket]: AppConnectionPlanType.Regular
|
||||
};
|
||||
|
||||
@@ -45,6 +45,8 @@ import { azureClientSecretsConnectionService } from "./azure-client-secrets/azur
|
||||
import { ValidateAzureDevOpsConnectionCredentialsSchema } from "./azure-devops/azure-devops-schemas";
|
||||
import { azureDevOpsConnectionService } from "./azure-devops/azure-devops-service";
|
||||
import { ValidateAzureKeyVaultConnectionCredentialsSchema } from "./azure-key-vault";
|
||||
import { ValidateBitbucketConnectionCredentialsSchema } from "./bitbucket";
|
||||
import { bitbucketConnectionService } from "./bitbucket/bitbucket-connection-service";
|
||||
import { ValidateCamundaConnectionCredentialsSchema } from "./camunda";
|
||||
import { camundaConnectionService } from "./camunda/camunda-connection-service";
|
||||
import { ValidateCloudflareConnectionCredentialsSchema } from "./cloudflare/cloudflare-connection-schema";
|
||||
@@ -70,6 +72,8 @@ import { ValidateLdapConnectionCredentialsSchema } from "./ldap";
|
||||
import { ValidateMsSqlConnectionCredentialsSchema } from "./mssql";
|
||||
import { ValidateMySqlConnectionCredentialsSchema } from "./mysql";
|
||||
import { ValidatePostgresConnectionCredentialsSchema } from "./postgres";
|
||||
import { ValidateRailwayConnectionCredentialsSchema } from "./railway";
|
||||
import { railwayConnectionService } from "./railway/railway-connection-service";
|
||||
import { ValidateRenderConnectionCredentialsSchema } from "./render/render-connection-schema";
|
||||
import { renderConnectionService } from "./render/render-connection-service";
|
||||
import { ValidateTeamCityConnectionCredentialsSchema } from "./teamcity";
|
||||
@@ -80,6 +84,8 @@ import { ValidateVercelConnectionCredentialsSchema } from "./vercel";
|
||||
import { vercelConnectionService } from "./vercel/vercel-connection-service";
|
||||
import { ValidateWindmillConnectionCredentialsSchema } from "./windmill";
|
||||
import { windmillConnectionService } from "./windmill/windmill-connection-service";
|
||||
import { ValidateZabbixConnectionCredentialsSchema } from "./zabbix";
|
||||
import { zabbixConnectionService } from "./zabbix/zabbix-connection-service";
|
||||
|
||||
export type TAppConnectionServiceFactoryDep = {
|
||||
appConnectionDAL: TAppConnectionDALFactory;
|
||||
@@ -119,7 +125,10 @@ const VALIDATE_APP_CONNECTION_CREDENTIALS_MAP: Record<AppConnection, TValidateAp
|
||||
[AppConnection.Render]: ValidateRenderConnectionCredentialsSchema,
|
||||
[AppConnection.Flyio]: ValidateFlyioConnectionCredentialsSchema,
|
||||
[AppConnection.GitLab]: ValidateGitLabConnectionCredentialsSchema,
|
||||
[AppConnection.Cloudflare]: ValidateCloudflareConnectionCredentialsSchema
|
||||
[AppConnection.Cloudflare]: ValidateCloudflareConnectionCredentialsSchema,
|
||||
[AppConnection.Zabbix]: ValidateZabbixConnectionCredentialsSchema,
|
||||
[AppConnection.Railway]: ValidateRailwayConnectionCredentialsSchema,
|
||||
[AppConnection.Bitbucket]: ValidateBitbucketConnectionCredentialsSchema
|
||||
};
|
||||
|
||||
export const appConnectionServiceFactory = ({
|
||||
@@ -529,6 +538,9 @@ export const appConnectionServiceFactory = ({
|
||||
render: renderConnectionService(connectAppConnectionById),
|
||||
flyio: flyioConnectionService(connectAppConnectionById),
|
||||
gitlab: gitlabConnectionService(connectAppConnectionById, appConnectionDAL, kmsService),
|
||||
cloudflare: cloudflareConnectionService(connectAppConnectionById)
|
||||
cloudflare: cloudflareConnectionService(connectAppConnectionById),
|
||||
zabbix: zabbixConnectionService(connectAppConnectionById),
|
||||
railway: railwayConnectionService(connectAppConnectionById),
|
||||
bitbucket: bitbucketConnectionService(connectAppConnectionById)
|
||||
};
|
||||
};
|
||||
|
||||
@@ -56,6 +56,12 @@ import {
|
||||
TAzureKeyVaultConnectionInput,
|
||||
TValidateAzureKeyVaultConnectionCredentialsSchema
|
||||
} from "./azure-key-vault";
|
||||
import {
|
||||
TBitbucketConnection,
|
||||
TBitbucketConnectionConfig,
|
||||
TBitbucketConnectionInput,
|
||||
TValidateBitbucketConnectionCredentialsSchema
|
||||
} from "./bitbucket";
|
||||
import {
|
||||
TCamundaConnection,
|
||||
TCamundaConnectionConfig,
|
||||
@@ -135,6 +141,12 @@ import {
|
||||
TPostgresConnectionInput,
|
||||
TValidatePostgresConnectionCredentialsSchema
|
||||
} from "./postgres";
|
||||
import {
|
||||
TRailwayConnection,
|
||||
TRailwayConnectionConfig,
|
||||
TRailwayConnectionInput,
|
||||
TValidateRailwayConnectionCredentialsSchema
|
||||
} from "./railway";
|
||||
import {
|
||||
TRenderConnection,
|
||||
TRenderConnectionConfig,
|
||||
@@ -165,6 +177,12 @@ import {
|
||||
TWindmillConnectionConfig,
|
||||
TWindmillConnectionInput
|
||||
} from "./windmill";
|
||||
import {
|
||||
TValidateZabbixConnectionCredentialsSchema,
|
||||
TZabbixConnection,
|
||||
TZabbixConnectionConfig,
|
||||
TZabbixConnectionInput
|
||||
} from "./zabbix";
|
||||
|
||||
export type TAppConnection = { id: string } & (
|
||||
| TAwsConnection
|
||||
@@ -196,6 +214,9 @@ export type TAppConnection = { id: string } & (
|
||||
| TFlyioConnection
|
||||
| TGitLabConnection
|
||||
| TCloudflareConnection
|
||||
| TBitbucketConnection
|
||||
| TZabbixConnection
|
||||
| TRailwayConnection
|
||||
);
|
||||
|
||||
export type TAppConnectionRaw = NonNullable<Awaited<ReturnType<TAppConnectionDALFactory["findById"]>>>;
|
||||
@@ -232,6 +253,9 @@ export type TAppConnectionInput = { id: string } & (
|
||||
| TFlyioConnectionInput
|
||||
| TGitLabConnectionInput
|
||||
| TCloudflareConnectionInput
|
||||
| TBitbucketConnectionInput
|
||||
| TZabbixConnectionInput
|
||||
| TRailwayConnectionInput
|
||||
);
|
||||
|
||||
export type TSqlConnectionInput =
|
||||
@@ -275,7 +299,10 @@ export type TAppConnectionConfig =
|
||||
| TRenderConnectionConfig
|
||||
| TFlyioConnectionConfig
|
||||
| TGitLabConnectionConfig
|
||||
| TCloudflareConnectionConfig;
|
||||
| TCloudflareConnectionConfig
|
||||
| TBitbucketConnectionConfig
|
||||
| TZabbixConnectionConfig
|
||||
| TRailwayConnectionConfig;
|
||||
|
||||
export type TValidateAppConnectionCredentialsSchema =
|
||||
| TValidateAwsConnectionCredentialsSchema
|
||||
@@ -306,7 +333,10 @@ export type TValidateAppConnectionCredentialsSchema =
|
||||
| TValidateRenderConnectionCredentialsSchema
|
||||
| TValidateFlyioConnectionCredentialsSchema
|
||||
| TValidateGitLabConnectionCredentialsSchema
|
||||
| TValidateCloudflareConnectionCredentialsSchema;
|
||||
| TValidateCloudflareConnectionCredentialsSchema
|
||||
| TValidateBitbucketConnectionCredentialsSchema
|
||||
| TValidateZabbixConnectionCredentialsSchema
|
||||
| TValidateRailwayConnectionCredentialsSchema;
|
||||
|
||||
export type TListAwsConnectionKmsKeys = {
|
||||
connectionId: string;
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
export enum BitbucketConnectionMethod {
|
||||
ApiToken = "api-token"
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
import { AxiosError } from "axios";
|
||||
|
||||
import { request } from "@app/lib/config/request";
|
||||
import { BadRequestError } from "@app/lib/errors";
|
||||
import { AppConnection } from "@app/services/app-connection/app-connection-enums";
|
||||
import { IntegrationUrls } from "@app/services/integration-auth/integration-list";
|
||||
|
||||
import { BitbucketConnectionMethod } from "./bitbucket-connection-enums";
|
||||
import {
|
||||
TBitbucketConnection,
|
||||
TBitbucketConnectionConfig,
|
||||
TBitbucketRepo,
|
||||
TBitbucketWorkspace
|
||||
} from "./bitbucket-connection-types";
|
||||
|
||||
export const getBitbucketConnectionListItem = () => {
|
||||
return {
|
||||
name: "Bitbucket" as const,
|
||||
app: AppConnection.Bitbucket as const,
|
||||
methods: Object.values(BitbucketConnectionMethod) as [BitbucketConnectionMethod.ApiToken]
|
||||
};
|
||||
};
|
||||
|
||||
export const getBitbucketUser = async ({ email, apiToken }: { email: string; apiToken: string }) => {
|
||||
try {
|
||||
const { data } = await request.get<{ username: string }>(`${IntegrationUrls.BITBUCKET_API_URL}/2.0/user`, {
|
||||
headers: {
|
||||
Authorization: `Basic ${Buffer.from(`${email}:${apiToken}`).toString("base64")}`,
|
||||
Accept: "application/json"
|
||||
}
|
||||
});
|
||||
|
||||
return data;
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof AxiosError) {
|
||||
throw new BadRequestError({
|
||||
message: `Failed to validate credentials: ${error.message || "Unknown error"}`
|
||||
});
|
||||
}
|
||||
throw new BadRequestError({
|
||||
message: "Unable to validate connection: verify credentials"
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const validateBitbucketConnectionCredentials = async (config: TBitbucketConnectionConfig) => {
|
||||
await getBitbucketUser(config.credentials);
|
||||
return config.credentials;
|
||||
};
|
||||
|
||||
interface BitbucketWorkspacesResponse {
|
||||
values: TBitbucketWorkspace[];
|
||||
next?: string;
|
||||
}
|
||||
|
||||
export const listBitbucketWorkspaces = async (appConnection: TBitbucketConnection) => {
|
||||
const { email, apiToken } = appConnection.credentials;
|
||||
|
||||
const headers = {
|
||||
Authorization: `Basic ${Buffer.from(`${email}:${apiToken}`).toString("base64")}`,
|
||||
Accept: "application/json"
|
||||
};
|
||||
|
||||
let allWorkspaces: TBitbucketWorkspace[] = [];
|
||||
let nextUrl: string | undefined = `${IntegrationUrls.BITBUCKET_API_URL}/2.0/workspaces?pagelen=100`;
|
||||
let iterationCount = 0;
|
||||
|
||||
// Limit to 10 iterations, fetching at most 10 * 100 = 1000 workspaces
|
||||
while (nextUrl && iterationCount < 10) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const { data }: { data: BitbucketWorkspacesResponse } = await request.get<BitbucketWorkspacesResponse>(nextUrl, {
|
||||
headers
|
||||
});
|
||||
|
||||
allWorkspaces = allWorkspaces.concat(data.values.map((workspace) => ({ slug: workspace.slug })));
|
||||
nextUrl = data.next;
|
||||
iterationCount += 1;
|
||||
}
|
||||
|
||||
return allWorkspaces;
|
||||
};
|
||||
|
||||
interface BitbucketRepositoriesResponse {
|
||||
values: TBitbucketRepo[];
|
||||
next?: string;
|
||||
}
|
||||
|
||||
export const listBitbucketRepositories = async (appConnection: TBitbucketConnection, workspaceSlug: string) => {
|
||||
const { email, apiToken } = appConnection.credentials;
|
||||
|
||||
const headers = {
|
||||
Authorization: `Basic ${Buffer.from(`${email}:${apiToken}`).toString("base64")}`,
|
||||
Accept: "application/json"
|
||||
};
|
||||
|
||||
let allRepos: TBitbucketRepo[] = [];
|
||||
let nextUrl: string | undefined =
|
||||
`${IntegrationUrls.BITBUCKET_API_URL}/2.0/repositories/${encodeURIComponent(workspaceSlug)}?pagelen=100`;
|
||||
let iterationCount = 0;
|
||||
|
||||
// Limit to 10 iterations, fetching at most 10 * 100 = 1000 repositories
|
||||
while (nextUrl && iterationCount < 10) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const { data }: { data: BitbucketRepositoriesResponse } = await request.get<BitbucketRepositoriesResponse>(
|
||||
nextUrl,
|
||||
{
|
||||
headers
|
||||
}
|
||||
);
|
||||
|
||||
allRepos = allRepos.concat(data.values);
|
||||
nextUrl = data.next;
|
||||
iterationCount += 1;
|
||||
}
|
||||
|
||||
return allRepos;
|
||||
};
|
||||
@@ -0,0 +1,72 @@
|
||||
import z from "zod";
|
||||
|
||||
import { AppConnections } from "@app/lib/api-docs";
|
||||
import { AppConnection } from "@app/services/app-connection/app-connection-enums";
|
||||
import {
|
||||
BaseAppConnectionSchema,
|
||||
GenericCreateAppConnectionFieldsSchema,
|
||||
GenericUpdateAppConnectionFieldsSchema
|
||||
} from "@app/services/app-connection/app-connection-schemas";
|
||||
|
||||
import { BitbucketConnectionMethod } from "./bitbucket-connection-enums";
|
||||
|
||||
export const BitbucketConnectionAccessTokenCredentialsSchema = z.object({
|
||||
apiToken: z
|
||||
.string()
|
||||
.trim()
|
||||
.min(1, "API Token required")
|
||||
.max(255)
|
||||
.describe(AppConnections.CREDENTIALS.BITBUCKET.apiToken),
|
||||
email: z
|
||||
.string()
|
||||
.email()
|
||||
.trim()
|
||||
.min(1, "Email required")
|
||||
.max(255)
|
||||
.describe(AppConnections.CREDENTIALS.BITBUCKET.email)
|
||||
});
|
||||
|
||||
const BaseBitbucketConnectionSchema = BaseAppConnectionSchema.extend({ app: z.literal(AppConnection.Bitbucket) });
|
||||
|
||||
export const BitbucketConnectionSchema = BaseBitbucketConnectionSchema.extend({
|
||||
method: z.literal(BitbucketConnectionMethod.ApiToken),
|
||||
credentials: BitbucketConnectionAccessTokenCredentialsSchema
|
||||
});
|
||||
|
||||
export const SanitizedBitbucketConnectionSchema = z.discriminatedUnion("method", [
|
||||
BaseBitbucketConnectionSchema.extend({
|
||||
method: z.literal(BitbucketConnectionMethod.ApiToken),
|
||||
credentials: BitbucketConnectionAccessTokenCredentialsSchema.pick({
|
||||
email: true
|
||||
})
|
||||
})
|
||||
]);
|
||||
|
||||
export const ValidateBitbucketConnectionCredentialsSchema = z.discriminatedUnion("method", [
|
||||
z.object({
|
||||
method: z
|
||||
.literal(BitbucketConnectionMethod.ApiToken)
|
||||
.describe(AppConnections.CREATE(AppConnection.Bitbucket).method),
|
||||
credentials: BitbucketConnectionAccessTokenCredentialsSchema.describe(
|
||||
AppConnections.CREATE(AppConnection.Bitbucket).credentials
|
||||
)
|
||||
})
|
||||
]);
|
||||
|
||||
export const CreateBitbucketConnectionSchema = ValidateBitbucketConnectionCredentialsSchema.and(
|
||||
GenericCreateAppConnectionFieldsSchema(AppConnection.Bitbucket)
|
||||
);
|
||||
|
||||
export const UpdateBitbucketConnectionSchema = z
|
||||
.object({
|
||||
credentials: BitbucketConnectionAccessTokenCredentialsSchema.optional().describe(
|
||||
AppConnections.UPDATE(AppConnection.Bitbucket).credentials
|
||||
)
|
||||
})
|
||||
.and(GenericUpdateAppConnectionFieldsSchema(AppConnection.Bitbucket));
|
||||
|
||||
export const BitbucketConnectionListItemSchema = z.object({
|
||||
name: z.literal("Bitbucket"),
|
||||
app: z.literal(AppConnection.Bitbucket),
|
||||
methods: z.nativeEnum(BitbucketConnectionMethod).array()
|
||||
});
|
||||
@@ -0,0 +1,33 @@
|
||||
import { OrgServiceActor } from "@app/lib/types";
|
||||
|
||||
import { AppConnection } from "../app-connection-enums";
|
||||
import { listBitbucketRepositories, listBitbucketWorkspaces } from "./bitbucket-connection-fns";
|
||||
import { TBitbucketConnection, TGetBitbucketRepositoriesDTO } from "./bitbucket-connection-types";
|
||||
|
||||
type TGetAppConnectionFunc = (
|
||||
app: AppConnection,
|
||||
connectionId: string,
|
||||
actor: OrgServiceActor
|
||||
) => Promise<TBitbucketConnection>;
|
||||
|
||||
export const bitbucketConnectionService = (getAppConnection: TGetAppConnectionFunc) => {
|
||||
const listWorkspaces = async (connectionId: string, actor: OrgServiceActor) => {
|
||||
const appConnection = await getAppConnection(AppConnection.Bitbucket, connectionId, actor);
|
||||
const workspaces = await listBitbucketWorkspaces(appConnection);
|
||||
return workspaces;
|
||||
};
|
||||
|
||||
const listRepositories = async (
|
||||
{ connectionId, workspaceSlug }: TGetBitbucketRepositoriesDTO,
|
||||
actor: OrgServiceActor
|
||||
) => {
|
||||
const appConnection = await getAppConnection(AppConnection.Bitbucket, connectionId, actor);
|
||||
const repositories = await listBitbucketRepositories(appConnection, workspaceSlug);
|
||||
return repositories;
|
||||
};
|
||||
|
||||
return {
|
||||
listWorkspaces,
|
||||
listRepositories
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,40 @@
|
||||
import z from "zod";
|
||||
|
||||
import { DiscriminativePick } from "@app/lib/types";
|
||||
|
||||
import { AppConnection } from "../app-connection-enums";
|
||||
import {
|
||||
BitbucketConnectionSchema,
|
||||
CreateBitbucketConnectionSchema,
|
||||
ValidateBitbucketConnectionCredentialsSchema
|
||||
} from "./bitbucket-connection-schemas";
|
||||
|
||||
export type TBitbucketConnection = z.infer<typeof BitbucketConnectionSchema>;
|
||||
|
||||
export type TBitbucketConnectionInput = z.infer<typeof CreateBitbucketConnectionSchema> & {
|
||||
app: AppConnection.Bitbucket;
|
||||
};
|
||||
|
||||
export type TValidateBitbucketConnectionCredentialsSchema = typeof ValidateBitbucketConnectionCredentialsSchema;
|
||||
|
||||
export type TBitbucketConnectionConfig = DiscriminativePick<
|
||||
TBitbucketConnectionInput,
|
||||
"method" | "app" | "credentials"
|
||||
> & {
|
||||
orgId: string;
|
||||
};
|
||||
|
||||
export type TGetBitbucketRepositoriesDTO = {
|
||||
connectionId: string;
|
||||
workspaceSlug: string;
|
||||
};
|
||||
|
||||
export type TBitbucketWorkspace = {
|
||||
slug: string;
|
||||
};
|
||||
|
||||
export type TBitbucketRepo = {
|
||||
uuid: string;
|
||||
full_name: string; // workspace-slug/repo-slug
|
||||
slug: string;
|
||||
};
|
||||
@@ -0,0 +1,4 @@
|
||||
export * from "./bitbucket-connection-enums";
|
||||
export * from "./bitbucket-connection-fns";
|
||||
export * from "./bitbucket-connection-schemas";
|
||||
export * from "./bitbucket-connection-types";
|
||||
@@ -0,0 +1,4 @@
|
||||
export * from "./railway-connection-constants";
|
||||
export * from "./railway-connection-fns";
|
||||
export * from "./railway-connection-schemas";
|
||||
export * from "./railway-connection-types";
|
||||
@@ -0,0 +1,5 @@
|
||||
export enum RailwayConnectionMethod {
|
||||
AccountToken = "account-token",
|
||||
ProjectToken = "project-token",
|
||||
TeamToken = "team-token"
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/* eslint-disable no-await-in-loop */
|
||||
import { AxiosError } from "axios";
|
||||
|
||||
import { BadRequestError } from "@app/lib/errors";
|
||||
import { AppConnection } from "@app/services/app-connection/app-connection-enums";
|
||||
|
||||
import { RailwayConnectionMethod } from "./railway-connection-constants";
|
||||
import { RailwayPublicAPI } from "./railway-connection-public-client";
|
||||
import { TRailwayConnection, TRailwayConnectionConfig } from "./railway-connection-types";
|
||||
|
||||
export const getRailwayConnectionListItem = () => {
|
||||
return {
|
||||
name: "Railway" as const,
|
||||
app: AppConnection.Railway as const,
|
||||
methods: Object.values(RailwayConnectionMethod)
|
||||
};
|
||||
};
|
||||
|
||||
export const validateRailwayConnectionCredentials = async (config: TRailwayConnectionConfig) => {
|
||||
const { credentials, method } = config;
|
||||
|
||||
try {
|
||||
await RailwayPublicAPI.healthcheck({
|
||||
method,
|
||||
credentials
|
||||
});
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof AxiosError) {
|
||||
throw new BadRequestError({
|
||||
message: `Failed to validate credentials: ${error.message || "Unknown error"}`
|
||||
});
|
||||
}
|
||||
|
||||
throw new BadRequestError({
|
||||
message: "Unable to validate connection - verify credentials"
|
||||
});
|
||||
}
|
||||
|
||||
return credentials;
|
||||
};
|
||||
|
||||
export const listProjects = async (appConnection: TRailwayConnection) => {
|
||||
const { credentials, method } = appConnection;
|
||||
|
||||
try {
|
||||
return await RailwayPublicAPI.listProjects({
|
||||
method,
|
||||
credentials
|
||||
});
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof AxiosError) {
|
||||
throw new BadRequestError({
|
||||
message: `Failed to list projects: ${error.message || "Unknown error"}`
|
||||
});
|
||||
}
|
||||
|
||||
if (error instanceof BadRequestError) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
throw new BadRequestError({
|
||||
message: "Unable to list projects",
|
||||
error
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,237 @@
|
||||
/* eslint-disable class-methods-use-this */
|
||||
import { AxiosError, AxiosInstance, AxiosResponse } from "axios";
|
||||
|
||||
import { createRequestClient } from "@app/lib/config/request";
|
||||
import { BadRequestError } from "@app/lib/errors";
|
||||
import { IntegrationUrls } from "@app/services/integration-auth/integration-list";
|
||||
|
||||
import { RailwayConnectionMethod } from "./railway-connection-constants";
|
||||
import {
|
||||
RailwayAccountWorkspaceListSchema,
|
||||
RailwayGetProjectsByProjectTokenSchema,
|
||||
RailwayGetSubscriptionTypeSchema,
|
||||
RailwayProjectsListSchema
|
||||
} from "./railway-connection-schemas";
|
||||
import { RailwayProject, TRailwayConnectionConfig, TRailwayResponse } from "./railway-connection-types";
|
||||
|
||||
type RailwaySendReqOptions = Pick<TRailwayConnectionConfig, "credentials" | "method">;
|
||||
|
||||
export function getRailwayAuthHeaders(method: RailwayConnectionMethod, token: string): Record<string, string> {
|
||||
switch (method) {
|
||||
case RailwayConnectionMethod.AccountToken:
|
||||
case RailwayConnectionMethod.TeamToken:
|
||||
return {
|
||||
Authorization: token
|
||||
};
|
||||
case RailwayConnectionMethod.ProjectToken:
|
||||
return {
|
||||
"Project-Access-Token": token
|
||||
};
|
||||
default:
|
||||
throw new Error(`Unsupported Railway connection method`);
|
||||
}
|
||||
}
|
||||
|
||||
export function getRailwayRatelimiter(headers: AxiosResponse["headers"]): {
|
||||
isRatelimited: boolean;
|
||||
maxAttempts: number;
|
||||
wait: () => Promise<void>;
|
||||
} {
|
||||
const retryAfter: number | undefined = headers["Retry-After"] as number | undefined;
|
||||
const requestsLeft = parseInt(headers["X-RateLimit-Remaining"] as string, 10);
|
||||
const limitResetAt = headers["X-RateLimit-Reset"] as string;
|
||||
|
||||
const now = +new Date();
|
||||
const nextReset = +new Date(limitResetAt);
|
||||
|
||||
const remaining = Math.min(0, nextReset - now);
|
||||
|
||||
const wait = () => {
|
||||
return new Promise<void>((res) => {
|
||||
setTimeout(res, remaining);
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
isRatelimited: Boolean(retryAfter || requestsLeft === 0),
|
||||
wait,
|
||||
maxAttempts: 3
|
||||
};
|
||||
}
|
||||
|
||||
class RailwayPublicClient {
|
||||
private client: AxiosInstance;
|
||||
|
||||
constructor() {
|
||||
this.client = createRequestClient({
|
||||
method: "POST",
|
||||
baseURL: IntegrationUrls.RAILWAY_API_URL,
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async send<T extends TRailwayResponse>(
|
||||
query: string,
|
||||
options: RailwaySendReqOptions,
|
||||
variables: Record<string, string | Record<string, string>> = {},
|
||||
retryAttempt: number = 0
|
||||
): Promise<T["data"] | undefined> {
|
||||
const body = {
|
||||
query,
|
||||
variables
|
||||
};
|
||||
|
||||
const response = await this.client.request<T>({
|
||||
data: body,
|
||||
headers: getRailwayAuthHeaders(options.method, options.credentials.apiToken)
|
||||
});
|
||||
|
||||
const { errors } = response.data;
|
||||
|
||||
if (Array.isArray(errors) && errors.length > 0) {
|
||||
throw new AxiosError(errors[0].message);
|
||||
}
|
||||
|
||||
const limiter = getRailwayRatelimiter(response.headers);
|
||||
|
||||
if (limiter.isRatelimited && retryAttempt <= limiter.maxAttempts) {
|
||||
await limiter.wait();
|
||||
return this.send(query, options, variables, retryAttempt + 1);
|
||||
}
|
||||
|
||||
return response.data.data;
|
||||
}
|
||||
|
||||
healthcheck(config: RailwaySendReqOptions) {
|
||||
switch (config.method) {
|
||||
case RailwayConnectionMethod.AccountToken:
|
||||
return this.send(`{ me { teams { edges { node { id } } } } }`, config);
|
||||
case RailwayConnectionMethod.ProjectToken:
|
||||
return this.send(`{ projectToken { projectId environmentId project { id } } }`, config);
|
||||
case RailwayConnectionMethod.TeamToken:
|
||||
return this.send(`{ projects { edges { node { id name team { id } } } } }`, config);
|
||||
default:
|
||||
throw new Error(`Unsupported Railway connection method`);
|
||||
}
|
||||
}
|
||||
|
||||
async getSubscriptionType(config: RailwaySendReqOptions & { projectId: string }) {
|
||||
const res = await this.send(
|
||||
`query project($projectId: String!) { project(id: $projectId) { subscriptionType }}`,
|
||||
config,
|
||||
{
|
||||
projectId: config.projectId
|
||||
}
|
||||
);
|
||||
|
||||
const data = await RailwayGetSubscriptionTypeSchema.parseAsync(res);
|
||||
|
||||
return data.project.subscriptionType;
|
||||
}
|
||||
|
||||
async listProjects(config: RailwaySendReqOptions): Promise<RailwayProject[]> {
|
||||
switch (config.method) {
|
||||
case RailwayConnectionMethod.TeamToken: {
|
||||
const res = await this.send(
|
||||
`{ projects { edges { node { id, name, services{ edges{ node { id, name } } } environments { edges { node { name, id } } } } } } }`,
|
||||
config
|
||||
);
|
||||
|
||||
const data = await RailwayProjectsListSchema.parseAsync(res);
|
||||
|
||||
return data.projects.edges.map((p) => ({
|
||||
id: p.node.id,
|
||||
name: p.node.name,
|
||||
environments: p.node.environments.edges.map((e) => e.node),
|
||||
services: p.node.services.edges.map((s) => s.node)
|
||||
}));
|
||||
}
|
||||
|
||||
case RailwayConnectionMethod.AccountToken: {
|
||||
const res = await this.send(
|
||||
`{ me { workspaces { id, name, team{ projects{ edges{ node{ id, name, services{ edges { node { name, id } } } environments { edges { node { name, id } } } } } } } } } }`,
|
||||
config
|
||||
);
|
||||
|
||||
const data = await RailwayAccountWorkspaceListSchema.parseAsync(res);
|
||||
|
||||
return data.me.workspaces.flatMap((w) =>
|
||||
w.team.projects.edges.map((p) => ({
|
||||
id: p.node.id,
|
||||
name: p.node.name,
|
||||
environments: p.node.environments.edges.map((e) => e.node),
|
||||
services: p.node.services.edges.map((s) => s.node)
|
||||
}))
|
||||
);
|
||||
}
|
||||
|
||||
case RailwayConnectionMethod.ProjectToken: {
|
||||
const res = await this.send(
|
||||
`query { projectToken { project { id, name, services { edges { node { name, id } } } environments { edges { node { name, id } } } } } }`,
|
||||
config
|
||||
);
|
||||
|
||||
const data = await RailwayGetProjectsByProjectTokenSchema.parseAsync(res);
|
||||
|
||||
const p = data.projectToken.project;
|
||||
|
||||
return [
|
||||
{
|
||||
id: p.id,
|
||||
name: p.name,
|
||||
environments: p.environments.edges.map((e) => e.node),
|
||||
services: p.services.edges.map((s) => s.node)
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
default:
|
||||
throw new Error(`Unsupported Railway connection method`);
|
||||
}
|
||||
}
|
||||
|
||||
async getVariables(
|
||||
config: RailwaySendReqOptions,
|
||||
variables: { projectId: string; environmentId: string; serviceId?: string }
|
||||
) {
|
||||
const res = await this.send<TRailwayResponse<{ variables: Record<string, string> }>>(
|
||||
`query variables($environmentId: String!, $projectId: String!, $serviceId: String) { variables( projectId: $projectId, environmentId: $environmentId, serviceId: $serviceId ) }`,
|
||||
config,
|
||||
variables
|
||||
);
|
||||
|
||||
if (!res?.variables) {
|
||||
throw new BadRequestError({
|
||||
message: "Failed to get railway variables - empty response"
|
||||
});
|
||||
}
|
||||
|
||||
return res.variables;
|
||||
}
|
||||
|
||||
async deleteVariable(
|
||||
config: RailwaySendReqOptions,
|
||||
variables: { input: { projectId: string; environmentId: string; name: string; serviceId?: string } }
|
||||
) {
|
||||
await this.send<TRailwayResponse<{ variables: Record<string, string> }>>(
|
||||
`mutation variableDelete($input: VariableDeleteInput!) { variableDelete(input: $input) }`,
|
||||
config,
|
||||
variables
|
||||
);
|
||||
}
|
||||
|
||||
async upsertVariable(
|
||||
config: RailwaySendReqOptions,
|
||||
variables: { input: { projectId: string; environmentId: string; name: string; value: string; serviceId?: string } }
|
||||
) {
|
||||
await this.send<TRailwayResponse<{ variables: Record<string, string> }>>(
|
||||
`mutation variableUpsert($input: VariableUpsertInput!) { variableUpsert(input: $input) }`,
|
||||
config,
|
||||
variables
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export const RailwayPublicAPI = new RailwayPublicClient();
|
||||
@@ -0,0 +1,117 @@
|
||||
import z from "zod";
|
||||
|
||||
import { AppConnections } from "@app/lib/api-docs";
|
||||
import { AppConnection } from "@app/services/app-connection/app-connection-enums";
|
||||
import {
|
||||
BaseAppConnectionSchema,
|
||||
GenericCreateAppConnectionFieldsSchema,
|
||||
GenericUpdateAppConnectionFieldsSchema
|
||||
} from "@app/services/app-connection/app-connection-schemas";
|
||||
|
||||
import { RailwayConnectionMethod } from "./railway-connection-constants";
|
||||
|
||||
export const RailwayConnectionMethodSchema = z
|
||||
.nativeEnum(RailwayConnectionMethod)
|
||||
.describe(AppConnections.CREATE(AppConnection.Railway).method);
|
||||
|
||||
export const RailwayConnectionAccessTokenCredentialsSchema = z.object({
|
||||
apiToken: z
|
||||
.string()
|
||||
.trim()
|
||||
.min(1, "API Token required")
|
||||
.max(255)
|
||||
.describe(AppConnections.CREDENTIALS.RAILWAY.apiToken)
|
||||
});
|
||||
|
||||
const BaseRailwayConnectionSchema = BaseAppConnectionSchema.extend({
|
||||
app: z.literal(AppConnection.Railway)
|
||||
});
|
||||
|
||||
export const RailwayConnectionSchema = BaseRailwayConnectionSchema.extend({
|
||||
method: RailwayConnectionMethodSchema,
|
||||
credentials: RailwayConnectionAccessTokenCredentialsSchema
|
||||
});
|
||||
|
||||
export const SanitizedRailwayConnectionSchema = z.discriminatedUnion("method", [
|
||||
BaseRailwayConnectionSchema.extend({
|
||||
method: RailwayConnectionMethodSchema,
|
||||
credentials: RailwayConnectionAccessTokenCredentialsSchema.pick({})
|
||||
})
|
||||
]);
|
||||
|
||||
export const ValidateRailwayConnectionCredentialsSchema = z.discriminatedUnion("method", [
|
||||
z.object({
|
||||
method: RailwayConnectionMethodSchema,
|
||||
credentials: RailwayConnectionAccessTokenCredentialsSchema.describe(
|
||||
AppConnections.CREATE(AppConnection.Railway).credentials
|
||||
)
|
||||
})
|
||||
]);
|
||||
|
||||
export const CreateRailwayConnectionSchema = ValidateRailwayConnectionCredentialsSchema.and(
|
||||
GenericCreateAppConnectionFieldsSchema(AppConnection.Railway)
|
||||
);
|
||||
|
||||
export const UpdateRailwayConnectionSchema = z
|
||||
.object({
|
||||
credentials: RailwayConnectionAccessTokenCredentialsSchema.optional().describe(
|
||||
AppConnections.UPDATE(AppConnection.Railway).credentials
|
||||
)
|
||||
})
|
||||
.and(GenericUpdateAppConnectionFieldsSchema(AppConnection.Railway));
|
||||
|
||||
export const RailwayConnectionListItemSchema = z.object({
|
||||
name: z.literal("Railway"),
|
||||
app: z.literal(AppConnection.Railway),
|
||||
methods: z.nativeEnum(RailwayConnectionMethod).array()
|
||||
});
|
||||
|
||||
export const RailwayResourceSchema = z.object({
|
||||
node: z.object({
|
||||
id: z.string(),
|
||||
name: z.string()
|
||||
})
|
||||
});
|
||||
|
||||
export const RailwayProjectEdgeSchema = z.object({
|
||||
node: z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
services: z.object({
|
||||
edges: z.array(RailwayResourceSchema)
|
||||
}),
|
||||
environments: z.object({
|
||||
edges: z.array(RailwayResourceSchema)
|
||||
})
|
||||
})
|
||||
});
|
||||
|
||||
export const RailwayProjectsListSchema = z.object({
|
||||
projects: z.object({
|
||||
edges: z.array(RailwayProjectEdgeSchema)
|
||||
})
|
||||
});
|
||||
|
||||
export const RailwayAccountWorkspaceListSchema = z.object({
|
||||
me: z.object({
|
||||
workspaces: z.array(
|
||||
z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
team: RailwayProjectsListSchema
|
||||
})
|
||||
)
|
||||
})
|
||||
});
|
||||
|
||||
export const RailwayGetProjectsByProjectTokenSchema = z.object({
|
||||
projectToken: z.object({
|
||||
project: RailwayProjectEdgeSchema.shape.node
|
||||
})
|
||||
});
|
||||
|
||||
export const RailwayGetSubscriptionTypeSchema = z.object({
|
||||
project: z.object({
|
||||
subscriptionType: z.enum(["free", "hobby", "pro", "trial"])
|
||||
})
|
||||
});
|
||||
@@ -0,0 +1,30 @@
|
||||
import { logger } from "@app/lib/logger";
|
||||
import { OrgServiceActor } from "@app/lib/types";
|
||||
|
||||
import { AppConnection } from "../app-connection-enums";
|
||||
import { listProjects as getRailwayProjects } from "./railway-connection-fns";
|
||||
import { TRailwayConnection } from "./railway-connection-types";
|
||||
|
||||
type TGetAppConnectionFunc = (
|
||||
app: AppConnection,
|
||||
connectionId: string,
|
||||
actor: OrgServiceActor
|
||||
) => Promise<TRailwayConnection>;
|
||||
|
||||
export const railwayConnectionService = (getAppConnection: TGetAppConnectionFunc) => {
|
||||
const listProjects = async (connectionId: string, actor: OrgServiceActor) => {
|
||||
const appConnection = await getAppConnection(AppConnection.Railway, connectionId, actor);
|
||||
try {
|
||||
const projects = await getRailwayProjects(appConnection);
|
||||
|
||||
return projects;
|
||||
} catch (error) {
|
||||
logger.error(error, "Failed to establish connection with Railway");
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
listProjects
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,79 @@
|
||||
import z from "zod";
|
||||
|
||||
import { DiscriminativePick } from "@app/lib/types";
|
||||
|
||||
import { AppConnection } from "../app-connection-enums";
|
||||
import {
|
||||
CreateRailwayConnectionSchema,
|
||||
RailwayConnectionSchema,
|
||||
ValidateRailwayConnectionCredentialsSchema
|
||||
} from "./railway-connection-schemas";
|
||||
|
||||
export type TRailwayConnection = z.infer<typeof RailwayConnectionSchema>;
|
||||
|
||||
export type TRailwayConnectionInput = z.infer<typeof CreateRailwayConnectionSchema> & {
|
||||
app: AppConnection.Railway;
|
||||
};
|
||||
|
||||
export type TValidateRailwayConnectionCredentialsSchema = typeof ValidateRailwayConnectionCredentialsSchema;
|
||||
|
||||
export type TRailwayConnectionConfig = DiscriminativePick<TRailwayConnection, "method" | "app" | "credentials"> & {
|
||||
orgId: string;
|
||||
};
|
||||
|
||||
export type TRailwayService = {
|
||||
id: string;
|
||||
name: string;
|
||||
};
|
||||
|
||||
export type TRailwayEnvironment = {
|
||||
id: string;
|
||||
name: string;
|
||||
};
|
||||
|
||||
export type RailwayProject = {
|
||||
id: string;
|
||||
name: string;
|
||||
services: TRailwayService[];
|
||||
environments: TRailwayEnvironment[];
|
||||
};
|
||||
|
||||
export type TRailwayResponse<T = unknown> = {
|
||||
data?: T;
|
||||
errors?: {
|
||||
message: string;
|
||||
}[];
|
||||
};
|
||||
|
||||
export type TAccountProjectListResponse = TRailwayResponse<{
|
||||
projects: {
|
||||
edges: TProjectEdge[];
|
||||
};
|
||||
}>;
|
||||
|
||||
export interface TProjectEdge {
|
||||
node: {
|
||||
id: string;
|
||||
name: string;
|
||||
services: {
|
||||
edges: TServiceEdge[];
|
||||
};
|
||||
environments: {
|
||||
edges: TEnvironmentEdge[];
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
type TServiceEdge = {
|
||||
node: {
|
||||
id: string;
|
||||
name: string;
|
||||
};
|
||||
};
|
||||
|
||||
type TEnvironmentEdge = {
|
||||
node: {
|
||||
id: string;
|
||||
name: string;
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,4 @@
|
||||
export * from "./zabbix-connection-enums";
|
||||
export * from "./zabbix-connection-fns";
|
||||
export * from "./zabbix-connection-schemas";
|
||||
export * from "./zabbix-connection-types";
|
||||
@@ -0,0 +1,3 @@
|
||||
export enum ZabbixConnectionMethod {
|
||||
ApiToken = "api-token"
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
import { AxiosError } from "axios";
|
||||
import RE2 from "re2";
|
||||
|
||||
import { request } from "@app/lib/config/request";
|
||||
import { BadRequestError } from "@app/lib/errors";
|
||||
import { blockLocalAndPrivateIpAddresses } from "@app/lib/validator";
|
||||
import { AppConnection } from "@app/services/app-connection/app-connection-enums";
|
||||
|
||||
import { ZabbixConnectionMethod } from "./zabbix-connection-enums";
|
||||
import {
|
||||
TZabbixConnection,
|
||||
TZabbixConnectionConfig,
|
||||
TZabbixHost,
|
||||
TZabbixHostListResponse
|
||||
} from "./zabbix-connection-types";
|
||||
|
||||
const TRAILING_SLASH_REGEX = new RE2("/+$");
|
||||
|
||||
export const getZabbixConnectionListItem = () => {
|
||||
return {
|
||||
name: "Zabbix" as const,
|
||||
app: AppConnection.Zabbix as const,
|
||||
methods: Object.values(ZabbixConnectionMethod) as [ZabbixConnectionMethod.ApiToken]
|
||||
};
|
||||
};
|
||||
|
||||
export const validateZabbixConnectionCredentials = async (config: TZabbixConnectionConfig) => {
|
||||
const { apiToken, instanceUrl } = config.credentials;
|
||||
await blockLocalAndPrivateIpAddresses(instanceUrl);
|
||||
|
||||
try {
|
||||
const apiUrl = `${instanceUrl.replace(TRAILING_SLASH_REGEX, "")}/api_jsonrpc.php`;
|
||||
|
||||
const payload = {
|
||||
jsonrpc: "2.0",
|
||||
method: "authentication.get",
|
||||
params: {
|
||||
output: "extend"
|
||||
},
|
||||
id: 1
|
||||
};
|
||||
|
||||
const response: { data: { error?: { message: string }; result?: string } } = await request.post(apiUrl, payload, {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${apiToken}`
|
||||
}
|
||||
});
|
||||
|
||||
if (response.data.error) {
|
||||
throw new BadRequestError({
|
||||
message: response.data.error.message
|
||||
});
|
||||
}
|
||||
|
||||
return config.credentials;
|
||||
} catch (error) {
|
||||
if (error instanceof AxiosError) {
|
||||
throw new BadRequestError({
|
||||
message: `Failed to connect to Zabbix instance: ${error.message}`
|
||||
});
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
export const listZabbixHosts = async (appConnection: TZabbixConnection): Promise<TZabbixHost[]> => {
|
||||
const { apiToken, instanceUrl } = appConnection.credentials;
|
||||
await blockLocalAndPrivateIpAddresses(instanceUrl);
|
||||
|
||||
try {
|
||||
const apiUrl = `${instanceUrl.replace(TRAILING_SLASH_REGEX, "")}/api_jsonrpc.php`;
|
||||
|
||||
const payload = {
|
||||
jsonrpc: "2.0",
|
||||
method: "host.get",
|
||||
params: {
|
||||
output: ["hostid", "host"],
|
||||
sortfield: "host",
|
||||
sortorder: "ASC"
|
||||
},
|
||||
id: 1
|
||||
};
|
||||
|
||||
const response: { data: TZabbixHostListResponse } = await request.post(apiUrl, payload, {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${apiToken}`
|
||||
}
|
||||
});
|
||||
|
||||
return response.data.result
|
||||
? response.data.result.map((host) => ({
|
||||
hostId: host.hostid,
|
||||
host: host.host
|
||||
}))
|
||||
: [];
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof AxiosError) {
|
||||
throw new BadRequestError({
|
||||
message: `Failed to validate credentials: ${error.message || "Unknown error"}`
|
||||
});
|
||||
}
|
||||
throw new BadRequestError({
|
||||
message: "Unable to validate connection: verify credentials"
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,62 @@
|
||||
import z from "zod";
|
||||
|
||||
import { AppConnections } from "@app/lib/api-docs";
|
||||
import { AppConnection } from "@app/services/app-connection/app-connection-enums";
|
||||
import {
|
||||
BaseAppConnectionSchema,
|
||||
GenericCreateAppConnectionFieldsSchema,
|
||||
GenericUpdateAppConnectionFieldsSchema
|
||||
} from "@app/services/app-connection/app-connection-schemas";
|
||||
|
||||
import { ZabbixConnectionMethod } from "./zabbix-connection-enums";
|
||||
|
||||
export const ZabbixConnectionApiTokenCredentialsSchema = z.object({
|
||||
apiToken: z
|
||||
.string()
|
||||
.trim()
|
||||
.min(1, "API Token required")
|
||||
.max(1000)
|
||||
.describe(AppConnections.CREDENTIALS.ZABBIX.apiToken),
|
||||
instanceUrl: z.string().trim().url("Invalid Instance URL").describe(AppConnections.CREDENTIALS.ZABBIX.instanceUrl)
|
||||
});
|
||||
|
||||
const BaseZabbixConnectionSchema = BaseAppConnectionSchema.extend({ app: z.literal(AppConnection.Zabbix) });
|
||||
|
||||
export const ZabbixConnectionSchema = BaseZabbixConnectionSchema.extend({
|
||||
method: z.literal(ZabbixConnectionMethod.ApiToken),
|
||||
credentials: ZabbixConnectionApiTokenCredentialsSchema
|
||||
});
|
||||
|
||||
export const SanitizedZabbixConnectionSchema = z.discriminatedUnion("method", [
|
||||
BaseZabbixConnectionSchema.extend({
|
||||
method: z.literal(ZabbixConnectionMethod.ApiToken),
|
||||
credentials: ZabbixConnectionApiTokenCredentialsSchema.pick({ instanceUrl: true })
|
||||
})
|
||||
]);
|
||||
|
||||
export const ValidateZabbixConnectionCredentialsSchema = z.discriminatedUnion("method", [
|
||||
z.object({
|
||||
method: z.literal(ZabbixConnectionMethod.ApiToken).describe(AppConnections.CREATE(AppConnection.Zabbix).method),
|
||||
credentials: ZabbixConnectionApiTokenCredentialsSchema.describe(
|
||||
AppConnections.CREATE(AppConnection.Zabbix).credentials
|
||||
)
|
||||
})
|
||||
]);
|
||||
|
||||
export const CreateZabbixConnectionSchema = ValidateZabbixConnectionCredentialsSchema.and(
|
||||
GenericCreateAppConnectionFieldsSchema(AppConnection.Zabbix)
|
||||
);
|
||||
|
||||
export const UpdateZabbixConnectionSchema = z
|
||||
.object({
|
||||
credentials: ZabbixConnectionApiTokenCredentialsSchema.optional().describe(
|
||||
AppConnections.UPDATE(AppConnection.Zabbix).credentials
|
||||
)
|
||||
})
|
||||
.and(GenericUpdateAppConnectionFieldsSchema(AppConnection.Zabbix));
|
||||
|
||||
export const ZabbixConnectionListItemSchema = z.object({
|
||||
name: z.literal("Zabbix"),
|
||||
app: z.literal(AppConnection.Zabbix),
|
||||
methods: z.nativeEnum(ZabbixConnectionMethod).array()
|
||||
});
|
||||
@@ -0,0 +1,30 @@
|
||||
import { logger } from "@app/lib/logger";
|
||||
import { OrgServiceActor } from "@app/lib/types";
|
||||
|
||||
import { AppConnection } from "../app-connection-enums";
|
||||
import { listZabbixHosts } from "./zabbix-connection-fns";
|
||||
import { TZabbixConnection } from "./zabbix-connection-types";
|
||||
|
||||
type TGetAppConnectionFunc = (
|
||||
app: AppConnection,
|
||||
connectionId: string,
|
||||
actor: OrgServiceActor
|
||||
) => Promise<TZabbixConnection>;
|
||||
|
||||
export const zabbixConnectionService = (getAppConnection: TGetAppConnectionFunc) => {
|
||||
const listHosts = async (connectionId: string, actor: OrgServiceActor) => {
|
||||
const appConnection = await getAppConnection(AppConnection.Zabbix, connectionId, actor);
|
||||
|
||||
try {
|
||||
const hosts = await listZabbixHosts(appConnection);
|
||||
return hosts;
|
||||
} catch (error) {
|
||||
logger.error(error, "Failed to establish connection with zabbix");
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
listHosts
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,33 @@
|
||||
import z from "zod";
|
||||
|
||||
import { DiscriminativePick } from "@app/lib/types";
|
||||
|
||||
import { AppConnection } from "../app-connection-enums";
|
||||
import {
|
||||
CreateZabbixConnectionSchema,
|
||||
ValidateZabbixConnectionCredentialsSchema,
|
||||
ZabbixConnectionSchema
|
||||
} from "./zabbix-connection-schemas";
|
||||
|
||||
export type TZabbixConnection = z.infer<typeof ZabbixConnectionSchema>;
|
||||
|
||||
export type TZabbixConnectionInput = z.infer<typeof CreateZabbixConnectionSchema> & {
|
||||
app: AppConnection.Zabbix;
|
||||
};
|
||||
|
||||
export type TValidateZabbixConnectionCredentialsSchema = typeof ValidateZabbixConnectionCredentialsSchema;
|
||||
|
||||
export type TZabbixConnectionConfig = DiscriminativePick<TZabbixConnectionInput, "method" | "app" | "credentials"> & {
|
||||
orgId: string;
|
||||
};
|
||||
|
||||
export type TZabbixHost = {
|
||||
hostId: string;
|
||||
host: string;
|
||||
};
|
||||
|
||||
export type TZabbixHostListResponse = {
|
||||
jsonrpc: string;
|
||||
result: { hostid: string; host: string }[];
|
||||
error?: { message: string };
|
||||
};
|
||||
@@ -93,23 +93,25 @@ export const identityProjectServiceFactory = ({
|
||||
projectId
|
||||
);
|
||||
|
||||
const permissionBoundary = validatePrivilegeChangeOperation(
|
||||
membership.shouldUseNewPrivilegeSystem,
|
||||
ProjectPermissionIdentityActions.GrantPrivileges,
|
||||
ProjectPermissionSub.Identity,
|
||||
permission,
|
||||
rolePermission
|
||||
);
|
||||
if (!permissionBoundary.isValid)
|
||||
throw new PermissionBoundaryError({
|
||||
message: constructPermissionErrorMessage(
|
||||
"Failed to assign to role",
|
||||
membership.shouldUseNewPrivilegeSystem,
|
||||
ProjectPermissionIdentityActions.GrantPrivileges,
|
||||
ProjectPermissionSub.Identity
|
||||
),
|
||||
details: { missingPermissions: permissionBoundary.missingPermissions }
|
||||
});
|
||||
if (requestedRoleChange !== ProjectMembershipRole.NoAccess) {
|
||||
const permissionBoundary = validatePrivilegeChangeOperation(
|
||||
membership.shouldUseNewPrivilegeSystem,
|
||||
ProjectPermissionIdentityActions.GrantPrivileges,
|
||||
ProjectPermissionSub.Identity,
|
||||
permission,
|
||||
rolePermission
|
||||
);
|
||||
if (!permissionBoundary.isValid)
|
||||
throw new PermissionBoundaryError({
|
||||
message: constructPermissionErrorMessage(
|
||||
"Failed to assign to role",
|
||||
membership.shouldUseNewPrivilegeSystem,
|
||||
ProjectPermissionIdentityActions.GrantPrivileges,
|
||||
ProjectPermissionSub.Identity
|
||||
),
|
||||
details: { missingPermissions: permissionBoundary.missingPermissions }
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// validate custom roles input
|
||||
|
||||
@@ -69,23 +69,25 @@ export const identityServiceFactory = ({
|
||||
orgId
|
||||
);
|
||||
const isCustomRole = Boolean(customRole);
|
||||
const permissionBoundary = validatePrivilegeChangeOperation(
|
||||
membership.shouldUseNewPrivilegeSystem,
|
||||
OrgPermissionIdentityActions.GrantPrivileges,
|
||||
OrgPermissionSubjects.Identity,
|
||||
permission,
|
||||
rolePermission
|
||||
);
|
||||
if (!permissionBoundary.isValid)
|
||||
throw new PermissionBoundaryError({
|
||||
message: constructPermissionErrorMessage(
|
||||
"Failed to create identity",
|
||||
membership.shouldUseNewPrivilegeSystem,
|
||||
OrgPermissionIdentityActions.GrantPrivileges,
|
||||
OrgPermissionSubjects.Identity
|
||||
),
|
||||
details: { missingPermissions: permissionBoundary.missingPermissions }
|
||||
});
|
||||
if (role !== OrgMembershipRole.NoAccess) {
|
||||
const permissionBoundary = validatePrivilegeChangeOperation(
|
||||
membership.shouldUseNewPrivilegeSystem,
|
||||
OrgPermissionIdentityActions.GrantPrivileges,
|
||||
OrgPermissionSubjects.Identity,
|
||||
permission,
|
||||
rolePermission
|
||||
);
|
||||
if (!permissionBoundary.isValid)
|
||||
throw new PermissionBoundaryError({
|
||||
message: constructPermissionErrorMessage(
|
||||
"Failed to create identity",
|
||||
membership.shouldUseNewPrivilegeSystem,
|
||||
OrgPermissionIdentityActions.GrantPrivileges,
|
||||
OrgPermissionSubjects.Identity
|
||||
),
|
||||
details: { missingPermissions: permissionBoundary.missingPermissions }
|
||||
});
|
||||
}
|
||||
|
||||
const plan = await licenseService.getPlan(orgId);
|
||||
|
||||
@@ -187,6 +189,7 @@ export const identityServiceFactory = ({
|
||||
),
|
||||
details: { missingPermissions: appliedRolePermissionBoundary.missingPermissions }
|
||||
});
|
||||
|
||||
if (isCustomRole) customRole = customOrgRole;
|
||||
}
|
||||
|
||||
|
||||
@@ -814,9 +814,9 @@ const getAppsCloudflareWorkers = async ({ accessToken, accountId }: { accessToke
|
||||
};
|
||||
|
||||
/**
|
||||
* Return list of repositories for the BitBucket integration based on provided BitBucket workspace
|
||||
* Return list of repositories for the Bitbucket integration based on provided Bitbucket workspace
|
||||
*/
|
||||
const getAppsBitBucket = async ({ accessToken, workspaceSlug }: { accessToken: string; workspaceSlug?: string }) => {
|
||||
const getAppsBitbucket = async ({ accessToken, workspaceSlug }: { accessToken: string; workspaceSlug?: string }) => {
|
||||
interface RepositoriesResponse {
|
||||
size: number;
|
||||
page: number;
|
||||
@@ -1302,7 +1302,7 @@ export const getApps = async ({
|
||||
});
|
||||
|
||||
case Integrations.BITBUCKET:
|
||||
return getAppsBitBucket({
|
||||
return getAppsBitbucket({
|
||||
accessToken,
|
||||
workspaceSlug
|
||||
});
|
||||
|
||||
@@ -342,7 +342,7 @@ export const getIntegrationOptions = async () => {
|
||||
{
|
||||
name: "Bitbucket",
|
||||
slug: "bitbucket",
|
||||
image: "BitBucket.png",
|
||||
image: "Bitbucket.png",
|
||||
isAvailable: true,
|
||||
type: "oauth",
|
||||
clientId: appCfg.CLIENT_ID_BITBUCKET,
|
||||
|
||||
@@ -3921,9 +3921,9 @@ const syncSecretsCloudflareWorkers = async ({
|
||||
};
|
||||
|
||||
/**
|
||||
* Sync/push [secrets] to BitBucket repo with name [integration.app]
|
||||
* Sync/push [secrets] to Bitbucket repo with name [integration.app]
|
||||
*/
|
||||
const syncSecretsBitBucket = async ({
|
||||
const syncSecretsBitbucket = async ({
|
||||
integration,
|
||||
secrets,
|
||||
accessToken
|
||||
@@ -4832,7 +4832,7 @@ export const syncIntegrationSecrets = async ({
|
||||
});
|
||||
break;
|
||||
case Integrations.BITBUCKET:
|
||||
await syncSecretsBitBucket({
|
||||
await syncSecretsBitbucket({
|
||||
integration,
|
||||
secrets,
|
||||
accessToken
|
||||
|
||||
@@ -64,7 +64,7 @@ type ExchangeCodeGitlabResponse = {
|
||||
created_at: number;
|
||||
};
|
||||
|
||||
type ExchangeCodeBitBucketResponse = {
|
||||
type ExchangeCodeBitbucketResponse = {
|
||||
access_token: string;
|
||||
token_type: string;
|
||||
expires_in: number;
|
||||
@@ -392,10 +392,10 @@ const exchangeCodeGitlab = async ({ code, url }: { code: string; url?: string })
|
||||
};
|
||||
|
||||
/**
|
||||
* Return [accessToken], [accessExpiresAt], and [refreshToken] for BitBucket
|
||||
* Return [accessToken], [accessExpiresAt], and [refreshToken] for Bitbucket
|
||||
* code-token exchange
|
||||
*/
|
||||
const exchangeCodeBitBucket = async ({ code }: { code: string }) => {
|
||||
const exchangeCodeBitbucket = async ({ code }: { code: string }) => {
|
||||
const accessExpiresAt = new Date();
|
||||
const appCfg = getConfig();
|
||||
if (!appCfg.CLIENT_SECRET_BITBUCKET || !appCfg.CLIENT_ID_BITBUCKET) {
|
||||
@@ -403,7 +403,7 @@ const exchangeCodeBitBucket = async ({ code }: { code: string }) => {
|
||||
}
|
||||
|
||||
const res = (
|
||||
await request.post<ExchangeCodeBitBucketResponse>(
|
||||
await request.post<ExchangeCodeBitbucketResponse>(
|
||||
IntegrationUrls.BITBUCKET_TOKEN_URL,
|
||||
new URLSearchParams({
|
||||
grant_type: "authorization_code",
|
||||
@@ -490,7 +490,7 @@ export const exchangeCode = async ({
|
||||
url
|
||||
});
|
||||
case Integrations.BITBUCKET:
|
||||
return exchangeCodeBitBucket({
|
||||
return exchangeCodeBitbucket({
|
||||
code
|
||||
});
|
||||
default:
|
||||
@@ -524,7 +524,7 @@ type RefreshTokenGitLabResponse = {
|
||||
created_at: number;
|
||||
};
|
||||
|
||||
type RefreshTokenBitBucketResponse = {
|
||||
type RefreshTokenBitbucketResponse = {
|
||||
access_token: string;
|
||||
token_type: string;
|
||||
expires_in: number;
|
||||
@@ -653,9 +653,9 @@ const exchangeRefreshGitLab = async ({ refreshToken, url }: { url?: string | nul
|
||||
|
||||
/**
|
||||
* Return new access token by exchanging refresh token [refreshToken] for the
|
||||
* BitBucket integration
|
||||
* Bitbucket integration
|
||||
*/
|
||||
const exchangeRefreshBitBucket = async ({ refreshToken }: { refreshToken: string }) => {
|
||||
const exchangeRefreshBitbucket = async ({ refreshToken }: { refreshToken: string }) => {
|
||||
const accessExpiresAt = new Date();
|
||||
const appCfg = getConfig();
|
||||
if (!appCfg.CLIENT_SECRET_BITBUCKET || !appCfg.CLIENT_ID_BITBUCKET) {
|
||||
@@ -664,7 +664,7 @@ const exchangeRefreshBitBucket = async ({ refreshToken }: { refreshToken: string
|
||||
const {
|
||||
data
|
||||
}: {
|
||||
data: RefreshTokenBitBucketResponse;
|
||||
data: RefreshTokenBitbucketResponse;
|
||||
} = await request.post(
|
||||
IntegrationUrls.BITBUCKET_TOKEN_URL,
|
||||
new URLSearchParams({
|
||||
@@ -794,7 +794,7 @@ export const exchangeRefresh = async (
|
||||
url
|
||||
});
|
||||
case Integrations.BITBUCKET:
|
||||
return exchangeRefreshBitBucket({
|
||||
return exchangeRefreshBitbucket({
|
||||
refreshToken
|
||||
});
|
||||
case Integrations.GCP_SECRET_MANAGER:
|
||||
|
||||
@@ -103,8 +103,54 @@ export const orgMembershipDALFactory = (db: TDbClient) => {
|
||||
}
|
||||
};
|
||||
|
||||
const findRecentInvitedMemberships = async () => {
|
||||
try {
|
||||
const now = new Date();
|
||||
const oneWeekAgo = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000);
|
||||
const oneMonthAgo = new Date(now.getTime() - 30 * 24 * 60 * 60 * 1000);
|
||||
const twelveMonthsAgo = new Date(now.getTime() - 360 * 24 * 60 * 60 * 1000);
|
||||
|
||||
const memberships = await db
|
||||
.replicaNode()(TableName.OrgMembership)
|
||||
.where("status", "invited")
|
||||
.where((qb) => {
|
||||
// lastInvitedAt is null AND createdAt is between 1 week and 12 months ago
|
||||
void qb
|
||||
.whereNull(`${TableName.OrgMembership}.lastInvitedAt`)
|
||||
.whereBetween(`${TableName.OrgMembership}.createdAt`, [twelveMonthsAgo, oneWeekAgo]);
|
||||
})
|
||||
.orWhere((qb) => {
|
||||
// lastInvitedAt is older than 1 week ago AND createdAt is younger than 1 month ago
|
||||
void qb
|
||||
.where(`${TableName.OrgMembership}.lastInvitedAt`, "<", oneWeekAgo)
|
||||
.where(`${TableName.OrgMembership}.createdAt`, ">", oneMonthAgo);
|
||||
});
|
||||
|
||||
return memberships;
|
||||
} catch (error) {
|
||||
throw new DatabaseError({
|
||||
error,
|
||||
name: "Find recent invited memberships"
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const updateLastInvitedAtByIds = async (membershipIds: string[]) => {
|
||||
try {
|
||||
if (membershipIds.length === 0) return;
|
||||
await db(TableName.OrgMembership).whereIn("id", membershipIds).update({ lastInvitedAt: new Date() });
|
||||
} catch (error) {
|
||||
throw new DatabaseError({
|
||||
error,
|
||||
name: "Update last invited at by ids"
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
...orgMembershipOrm,
|
||||
findOrgMembershipById
|
||||
findOrgMembershipById,
|
||||
findRecentInvitedMemberships,
|
||||
updateLastInvitedAtByIds
|
||||
};
|
||||
};
|
||||
|
||||
@@ -36,6 +36,8 @@ import { getConfig } from "@app/lib/config/env";
|
||||
import { generateAsymmetricKeyPair } from "@app/lib/crypto";
|
||||
import { generateSymmetricKey, infisicalSymmetricDecrypt, infisicalSymmetricEncypt } from "@app/lib/crypto/encryption";
|
||||
import { generateUserSrpKeys } from "@app/lib/crypto/srp";
|
||||
import { applyJitter } from "@app/lib/dates";
|
||||
import { delay as delayMs } from "@app/lib/delay";
|
||||
import {
|
||||
BadRequestError,
|
||||
ForbiddenRequestError,
|
||||
@@ -44,8 +46,10 @@ import {
|
||||
UnauthorizedError
|
||||
} from "@app/lib/errors";
|
||||
import { groupBy } from "@app/lib/fn";
|
||||
import { logger } from "@app/lib/logger";
|
||||
import { alphaNumericNanoId } from "@app/lib/nanoid";
|
||||
import { isDisposableEmail } from "@app/lib/validator";
|
||||
import { QueueName } from "@app/queue";
|
||||
import { getDefaultOrgMembershipRoleForUpdateOrg } from "@app/services/org/org-role-fns";
|
||||
import { TOrgMembershipDALFactory } from "@app/services/org-membership/org-membership-dal";
|
||||
import { TUserAliasDALFactory } from "@app/services/user-alias/user-alias-dal";
|
||||
@@ -107,7 +111,15 @@ type TOrgServiceFactoryDep = {
|
||||
"findProjectMembershipsByUserId" | "delete" | "create" | "find" | "insertMany" | "transaction"
|
||||
>;
|
||||
projectKeyDAL: Pick<TProjectKeyDALFactory, "find" | "delete" | "insertMany" | "findLatestProjectKey" | "create">;
|
||||
orgMembershipDAL: Pick<TOrgMembershipDALFactory, "findOrgMembershipById" | "findOne" | "findById">;
|
||||
orgMembershipDAL: Pick<
|
||||
TOrgMembershipDALFactory,
|
||||
| "findOrgMembershipById"
|
||||
| "findOne"
|
||||
| "findById"
|
||||
| "findRecentInvitedMemberships"
|
||||
| "updateById"
|
||||
| "updateLastInvitedAtByIds"
|
||||
>;
|
||||
incidentContactDAL: TIncidentContactsDALFactory;
|
||||
samlConfigDAL: Pick<TSamlConfigDALFactory, "findOne">;
|
||||
oidcConfigDAL: Pick<TOidcConfigDALFactory, "findOne">;
|
||||
@@ -760,6 +772,10 @@ export const orgServiceFactory = ({
|
||||
}
|
||||
});
|
||||
|
||||
await orgMembershipDAL.updateById(inviteeOrgMembership.id, {
|
||||
lastInvitedAt: new Date()
|
||||
});
|
||||
|
||||
return { signupToken: undefined };
|
||||
};
|
||||
|
||||
@@ -1422,6 +1438,63 @@ export const orgServiceFactory = ({
|
||||
return incidentContact;
|
||||
};
|
||||
|
||||
/**
|
||||
* Re-send emails to users who haven't accepted an invite yet
|
||||
*/
|
||||
const notifyInvitedUsers = async () => {
|
||||
logger.info(`${QueueName.DailyResourceCleanUp}: notify invited users started`);
|
||||
|
||||
const invitedUsers = await orgMembershipDAL.findRecentInvitedMemberships();
|
||||
const appCfg = getConfig();
|
||||
|
||||
const orgCache: Record<string, { name: string; id: string } | undefined> = {};
|
||||
const notifiedUsers: string[] = [];
|
||||
|
||||
await Promise.all(
|
||||
invitedUsers.map(async (invitedUser) => {
|
||||
let org = orgCache[invitedUser.orgId];
|
||||
if (!org) {
|
||||
org = await orgDAL.findById(invitedUser.orgId);
|
||||
orgCache[invitedUser.orgId] = org;
|
||||
}
|
||||
|
||||
if (!org || !invitedUser.userId) return;
|
||||
|
||||
const token = await tokenService.createTokenForUser({
|
||||
type: TokenType.TOKEN_EMAIL_ORG_INVITATION,
|
||||
userId: invitedUser.userId,
|
||||
orgId: org.id
|
||||
});
|
||||
|
||||
if (invitedUser.inviteEmail) {
|
||||
await delayMs(Math.max(0, applyJitter(0, 2000)));
|
||||
|
||||
try {
|
||||
await smtpService.sendMail({
|
||||
template: SmtpTemplates.OrgInvite,
|
||||
subjectLine: `Reminder: You have been invited to ${org.name} on Infisical`,
|
||||
recipients: [invitedUser.inviteEmail],
|
||||
substitutions: {
|
||||
organizationName: org.name,
|
||||
email: invitedUser.inviteEmail,
|
||||
organizationId: org.id.toString(),
|
||||
token,
|
||||
callback_url: `${appCfg.SITE_URL}/signupinvite`
|
||||
}
|
||||
});
|
||||
notifiedUsers.push(invitedUser.id);
|
||||
} catch (err) {
|
||||
logger.error(err, `${QueueName.DailyResourceCleanUp}: notify invited users failed to send email`);
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
await orgMembershipDAL.updateLastInvitedAtByIds(notifiedUsers);
|
||||
|
||||
logger.info(`${QueueName.DailyResourceCleanUp}: notify invited users completed`);
|
||||
};
|
||||
|
||||
return {
|
||||
findOrganizationById,
|
||||
findAllOrgMembers,
|
||||
@@ -1445,6 +1518,7 @@ export const orgServiceFactory = ({
|
||||
listProjectMembershipsByOrgMembershipId,
|
||||
findOrgBySlug,
|
||||
resendOrgMemberInvitation,
|
||||
upgradePrivilegeSystem
|
||||
upgradePrivilegeSystem,
|
||||
notifyInvitedUsers
|
||||
};
|
||||
};
|
||||
|
||||
@@ -5,6 +5,7 @@ import { QueueJobs, QueueName, TQueueServiceFactory } from "@app/queue";
|
||||
|
||||
import { TIdentityAccessTokenDALFactory } from "../identity-access-token/identity-access-token-dal";
|
||||
import { TIdentityUaClientSecretDALFactory } from "../identity-ua/identity-ua-client-secret-dal";
|
||||
import { TOrgServiceFactory } from "../org/org-service";
|
||||
import { TSecretVersionDALFactory } from "../secret/secret-version-dal";
|
||||
import { TSecretFolderVersionDALFactory } from "../secret-folder/secret-folder-version-dal";
|
||||
import { TSecretSharingDALFactory } from "../secret-sharing/secret-sharing-dal";
|
||||
@@ -22,6 +23,7 @@ type TDailyResourceCleanUpQueueServiceFactoryDep = {
|
||||
secretSharingDAL: Pick<TSecretSharingDALFactory, "pruneExpiredSharedSecrets" | "pruneExpiredSecretRequests">;
|
||||
serviceTokenService: Pick<TServiceTokenServiceFactory, "notifyExpiringTokens">;
|
||||
queueService: TQueueServiceFactory;
|
||||
orgService: TOrgServiceFactory;
|
||||
};
|
||||
|
||||
export type TDailyResourceCleanUpQueueServiceFactory = ReturnType<typeof dailyResourceCleanUpQueueServiceFactory>;
|
||||
@@ -36,11 +38,11 @@ export const dailyResourceCleanUpQueueServiceFactory = ({
|
||||
secretSharingDAL,
|
||||
secretVersionV2DAL,
|
||||
identityUniversalAuthClientSecretDAL,
|
||||
serviceTokenService
|
||||
serviceTokenService,
|
||||
orgService
|
||||
}: TDailyResourceCleanUpQueueServiceFactoryDep) => {
|
||||
queueService.start(QueueName.DailyResourceCleanUp, async () => {
|
||||
logger.info(`${QueueName.DailyResourceCleanUp}: queue task started`);
|
||||
await auditLogDAL.pruneAuditLog();
|
||||
await identityAccessTokenDAL.removeExpiredTokens();
|
||||
await identityUniversalAuthClientSecretDAL.removeExpiredClientSecrets();
|
||||
await secretSharingDAL.pruneExpiredSharedSecrets();
|
||||
@@ -50,6 +52,8 @@ export const dailyResourceCleanUpQueueServiceFactory = ({
|
||||
await secretVersionV2DAL.pruneExcessVersions();
|
||||
await secretFolderVersionDAL.pruneExcessVersions();
|
||||
await serviceTokenService.notifyExpiringTokens();
|
||||
await orgService.notifyInvitedUsers();
|
||||
await auditLogDAL.pruneAuditLog();
|
||||
logger.info(`${QueueName.DailyResourceCleanUp}: queue task completed`);
|
||||
});
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ import { ForbiddenError, subject } from "@casl/ability";
|
||||
import path from "path";
|
||||
import { v4 as uuidv4, validate as uuidValidate } from "uuid";
|
||||
|
||||
import { TSecretFolders, TSecretFoldersInsert } from "@app/db/schemas";
|
||||
import { TProjectEnvironments, TSecretFolders, TSecretFoldersInsert } from "@app/db/schemas";
|
||||
import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service-types";
|
||||
import { ProjectPermissionActions, ProjectPermissionSub } from "@app/ee/services/permission/project-permission";
|
||||
import { TSecretApprovalPolicyServiceFactory } from "@app/ee/services/secret-approval-policy/secret-approval-policy-service";
|
||||
@@ -214,7 +214,7 @@ export const secretFolderServiceFactory = ({
|
||||
}
|
||||
},
|
||||
message: "Folder created",
|
||||
folderId: doc.id,
|
||||
folderId: parentFolder.id,
|
||||
changes: [
|
||||
{
|
||||
type: CommitType.ADD,
|
||||
@@ -469,15 +469,41 @@ export const secretFolderServiceFactory = ({
|
||||
|
||||
const $checkFolderPolicy = async ({
|
||||
projectId,
|
||||
environment,
|
||||
parentId
|
||||
env,
|
||||
parentId,
|
||||
idOrName
|
||||
}: {
|
||||
projectId: string;
|
||||
environment: string;
|
||||
env: TProjectEnvironments;
|
||||
parentId: string;
|
||||
idOrName: string;
|
||||
}) => {
|
||||
let targetFolder = await folderDAL
|
||||
.findOne({
|
||||
envId: env.id,
|
||||
name: idOrName,
|
||||
parentId,
|
||||
isReserved: false
|
||||
})
|
||||
.catch(() => null);
|
||||
|
||||
if (!targetFolder && uuidValidate(idOrName)) {
|
||||
targetFolder = await folderDAL
|
||||
.findOne({
|
||||
envId: env.id,
|
||||
id: idOrName,
|
||||
parentId,
|
||||
isReserved: false
|
||||
})
|
||||
.catch(() => null);
|
||||
}
|
||||
|
||||
if (!targetFolder) {
|
||||
throw new NotFoundError({ message: `Target folder not found` });
|
||||
}
|
||||
|
||||
// get environment root folder (as it's needed to get all folders under it)
|
||||
const rootFolder = await folderDAL.findBySecretPath(projectId, environment, "/");
|
||||
const rootFolder = await folderDAL.findBySecretPath(projectId, env.slug, "/");
|
||||
if (!rootFolder) throw new NotFoundError({ message: `Root folder not found` });
|
||||
// get all folders under environment root folder
|
||||
const folderPaths = await folderDAL.findByEnvsDeep({ parentIds: [rootFolder.id] });
|
||||
@@ -492,7 +518,13 @@ export const secretFolderServiceFactory = ({
|
||||
folderMap.get(normalizeKey(folder.parentId))?.push(folder);
|
||||
}
|
||||
|
||||
// Recursively collect all folders under the given parentId
|
||||
// Find the target folder in the folderPaths to get its full details
|
||||
const targetFolderWithPath = folderPaths.find((f) => f.id === targetFolder!.id);
|
||||
if (!targetFolderWithPath) {
|
||||
throw new NotFoundError({ message: `Target folder path not found` });
|
||||
}
|
||||
|
||||
// Recursively collect all folders under the target folder (descendants only)
|
||||
const collectDescendants = (
|
||||
id: string
|
||||
): (TSecretFolders & { path: string; depth: number; environment: string })[] => {
|
||||
@@ -500,23 +532,31 @@ export const secretFolderServiceFactory = ({
|
||||
return [...children, ...children.flatMap((child) => collectDescendants(child.id))];
|
||||
};
|
||||
|
||||
const foldersUnderParent = collectDescendants(parentId);
|
||||
const targetFolderDescendants = collectDescendants(targetFolder.id);
|
||||
|
||||
const folderPolicyPaths = foldersUnderParent.map((folder) => ({
|
||||
// Include the target folder itself plus all its descendants
|
||||
const foldersToCheck = [targetFolderWithPath, ...targetFolderDescendants];
|
||||
|
||||
const folderPolicyPaths = foldersToCheck.map((folder) => ({
|
||||
path: folder.path,
|
||||
id: folder.id
|
||||
}));
|
||||
|
||||
// get secrets under the given folders
|
||||
const secrets = await secretV2BridgeDAL.findByFolderIds({ folderIds: folderPolicyPaths.map((p) => p.id) });
|
||||
const secrets = await secretV2BridgeDAL.findByFolderIds({
|
||||
folderIds: folderPolicyPaths.map((p) => p.id)
|
||||
});
|
||||
|
||||
for await (const folderPolicyPath of folderPolicyPaths) {
|
||||
// eslint-disable-next-line no-continue
|
||||
if (!secrets.some((s) => s.folderId === folderPolicyPath.id)) continue;
|
||||
|
||||
const policy = await secretApprovalPolicyService.getSecretApprovalPolicy(
|
||||
projectId,
|
||||
environment,
|
||||
env.slug,
|
||||
folderPolicyPath.path
|
||||
);
|
||||
|
||||
// if there is a policy and there are secrets under the given folder, throw error
|
||||
if (policy) {
|
||||
throw new BadRequestError({
|
||||
@@ -560,20 +600,42 @@ export const secretFolderServiceFactory = ({
|
||||
message: `Folder with path '${secretPath}' in environment with slug '${environment}' not found`
|
||||
});
|
||||
|
||||
await $checkFolderPolicy({ projectId, environment, parentId: parentFolder.id });
|
||||
await $checkFolderPolicy({ projectId, env, parentId: parentFolder.id, idOrName });
|
||||
|
||||
let folderToDelete = await folderDAL
|
||||
.findOne({
|
||||
envId: env.id,
|
||||
name: idOrName,
|
||||
parentId: parentFolder.id,
|
||||
isReserved: false
|
||||
})
|
||||
.catch(() => null);
|
||||
|
||||
if (!folderToDelete && uuidValidate(idOrName)) {
|
||||
folderToDelete = await folderDAL
|
||||
.findOne({
|
||||
envId: env.id,
|
||||
id: idOrName,
|
||||
parentId: parentFolder.id,
|
||||
isReserved: false
|
||||
})
|
||||
.catch(() => null);
|
||||
}
|
||||
|
||||
if (!folderToDelete) {
|
||||
throw new NotFoundError({ message: `Folder with ID '${idOrName}' not found` });
|
||||
}
|
||||
|
||||
const [doc] = await folderDAL.delete(
|
||||
{
|
||||
envId: env.id,
|
||||
[uuidValidate(idOrName) ? "id" : "name"]: idOrName,
|
||||
id: folderToDelete.id,
|
||||
parentId: parentFolder.id,
|
||||
isReserved: false
|
||||
},
|
||||
tx
|
||||
);
|
||||
|
||||
if (!doc) throw new NotFoundError({ message: `Failed to delete folder with ID '${idOrName}', not found` });
|
||||
|
||||
const folderVersions = await folderVersionDAL.findLatestFolderVersions([doc.id], tx);
|
||||
|
||||
await folderCommitService.createCommit(
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
import { AppConnection } from "@app/services/app-connection/app-connection-enums";
|
||||
import { SecretSync } from "@app/services/secret-sync/secret-sync-enums";
|
||||
import { TSecretSyncListItem } from "@app/services/secret-sync/secret-sync-types";
|
||||
|
||||
export const RAILWAY_SYNC_LIST_OPTION: TSecretSyncListItem = {
|
||||
name: "Railway",
|
||||
destination: SecretSync.Railway,
|
||||
connection: AppConnection.Railway,
|
||||
canImportSecrets: true
|
||||
};
|
||||
@@ -0,0 +1,124 @@
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
||||
|
||||
import { RailwayPublicAPI } from "@app/services/app-connection/railway/railway-connection-public-client";
|
||||
import { matchesSchema } from "@app/services/secret-sync/secret-sync-fns";
|
||||
|
||||
import { SecretSyncError } from "../secret-sync-errors";
|
||||
import { TSecretMap } from "../secret-sync-types";
|
||||
import { TRailwaySyncWithCredentials } from "./railway-sync-types";
|
||||
|
||||
export const RailwaySyncFns = {
|
||||
async getSecrets(secretSync: TRailwaySyncWithCredentials): Promise<TSecretMap> {
|
||||
try {
|
||||
const config = secretSync.destinationConfig;
|
||||
|
||||
const variables = await RailwayPublicAPI.getVariables(secretSync.connection, {
|
||||
projectId: config.projectId,
|
||||
environmentId: config.environmentId,
|
||||
serviceId: config.serviceId || undefined
|
||||
});
|
||||
|
||||
const entries = {} as TSecretMap;
|
||||
|
||||
for (const [key, value] of Object.entries(variables)) {
|
||||
// Skip importing private railway variables
|
||||
// eslint-disable-next-line no-continue
|
||||
if (key.startsWith("RAILWAY_")) continue;
|
||||
|
||||
entries[key] = {
|
||||
value
|
||||
};
|
||||
}
|
||||
|
||||
return entries;
|
||||
} catch (error) {
|
||||
throw new SecretSyncError({
|
||||
error,
|
||||
message: "Failed to import secrets from Railway"
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
async syncSecrets(secretSync: TRailwaySyncWithCredentials, secretMap: TSecretMap) {
|
||||
const {
|
||||
environment,
|
||||
syncOptions: { disableSecretDeletion, keySchema }
|
||||
} = secretSync;
|
||||
const railwaySecrets = await this.getSecrets(secretSync);
|
||||
const config = secretSync.destinationConfig;
|
||||
|
||||
for await (const key of Object.keys(secretMap)) {
|
||||
try {
|
||||
const existing = railwaySecrets[key];
|
||||
|
||||
if (existing === undefined || existing.value !== secretMap[key].value) {
|
||||
await RailwayPublicAPI.upsertVariable(secretSync.connection, {
|
||||
input: {
|
||||
projectId: config.projectId,
|
||||
environmentId: config.environmentId,
|
||||
serviceId: config.serviceId || undefined,
|
||||
name: key,
|
||||
value: secretMap[key].value ?? ""
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
throw new SecretSyncError({
|
||||
error,
|
||||
secretKey: key
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (disableSecretDeletion) return;
|
||||
|
||||
for await (const key of Object.keys(railwaySecrets)) {
|
||||
try {
|
||||
// eslint-disable-next-line no-continue
|
||||
if (!matchesSchema(key, environment?.slug || "", keySchema)) continue;
|
||||
|
||||
if (!secretMap[key]) {
|
||||
await RailwayPublicAPI.deleteVariable(secretSync.connection, {
|
||||
input: {
|
||||
projectId: config.projectId,
|
||||
environmentId: config.environmentId,
|
||||
serviceId: config.serviceId || undefined,
|
||||
name: key
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
throw new SecretSyncError({
|
||||
error,
|
||||
secretKey: key
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
async removeSecrets(secretSync: TRailwaySyncWithCredentials, secretMap: TSecretMap) {
|
||||
const existing = await this.getSecrets(secretSync);
|
||||
const config = secretSync.destinationConfig;
|
||||
|
||||
for await (const secret of Object.keys(existing)) {
|
||||
try {
|
||||
if (secret in secretMap) {
|
||||
await RailwayPublicAPI.deleteVariable(secretSync.connection, {
|
||||
input: {
|
||||
projectId: config.projectId,
|
||||
environmentId: config.environmentId,
|
||||
serviceId: config.serviceId || undefined,
|
||||
name: secret
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
throw new SecretSyncError({
|
||||
error,
|
||||
secretKey: secret
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,56 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import { SecretSyncs } from "@app/lib/api-docs";
|
||||
import { AppConnection } from "@app/services/app-connection/app-connection-enums";
|
||||
import { SecretSync } from "@app/services/secret-sync/secret-sync-enums";
|
||||
import {
|
||||
BaseSecretSyncSchema,
|
||||
GenericCreateSecretSyncFieldsSchema,
|
||||
GenericUpdateSecretSyncFieldsSchema
|
||||
} from "@app/services/secret-sync/secret-sync-schemas";
|
||||
import { TSyncOptionsConfig } from "@app/services/secret-sync/secret-sync-types";
|
||||
|
||||
const RailwaySyncDestinationConfigSchema = z.object({
|
||||
projectId: z
|
||||
.string()
|
||||
.trim()
|
||||
.min(1, "Railway project ID required")
|
||||
.describe(SecretSyncs.DESTINATION_CONFIG.RAILWAY.projectId),
|
||||
projectName: z.string().trim().describe(SecretSyncs.DESTINATION_CONFIG.RAILWAY.projectName),
|
||||
environmentId: z
|
||||
.string()
|
||||
.trim()
|
||||
.min(1, "Railway environment ID required")
|
||||
.describe(SecretSyncs.DESTINATION_CONFIG.RAILWAY.environmentId),
|
||||
environmentName: z.string().trim().describe(SecretSyncs.DESTINATION_CONFIG.RAILWAY.environmentName),
|
||||
serviceId: z.string().optional().describe(SecretSyncs.DESTINATION_CONFIG.RAILWAY.serviceId),
|
||||
serviceName: z.string().optional().describe(SecretSyncs.DESTINATION_CONFIG.RAILWAY.serviceName)
|
||||
});
|
||||
|
||||
const RailwaySyncOptionsConfig: TSyncOptionsConfig = { canImportSecrets: true };
|
||||
|
||||
export const RailwaySyncSchema = BaseSecretSyncSchema(SecretSync.Railway, RailwaySyncOptionsConfig).extend({
|
||||
destination: z.literal(SecretSync.Railway),
|
||||
destinationConfig: RailwaySyncDestinationConfigSchema
|
||||
});
|
||||
|
||||
export const CreateRailwaySyncSchema = GenericCreateSecretSyncFieldsSchema(
|
||||
SecretSync.Railway,
|
||||
RailwaySyncOptionsConfig
|
||||
).extend({
|
||||
destinationConfig: RailwaySyncDestinationConfigSchema
|
||||
});
|
||||
|
||||
export const UpdateRailwaySyncSchema = GenericUpdateSecretSyncFieldsSchema(
|
||||
SecretSync.Railway,
|
||||
RailwaySyncOptionsConfig
|
||||
).extend({
|
||||
destinationConfig: RailwaySyncDestinationConfigSchema.optional()
|
||||
});
|
||||
|
||||
export const RailwaySyncListItemSchema = z.object({
|
||||
name: z.literal("Railway"),
|
||||
connection: z.literal(AppConnection.Railway),
|
||||
destination: z.literal(SecretSync.Railway),
|
||||
canImportSecrets: z.literal(true)
|
||||
});
|
||||
@@ -0,0 +1,31 @@
|
||||
import z from "zod";
|
||||
|
||||
import { TRailwayConnection } from "@app/services/app-connection/railway";
|
||||
|
||||
import { CreateRailwaySyncSchema, RailwaySyncListItemSchema, RailwaySyncSchema } from "./railway-sync-schemas";
|
||||
|
||||
export type TRailwaySyncListItem = z.infer<typeof RailwaySyncListItemSchema>;
|
||||
|
||||
export type TRailwaySync = z.infer<typeof RailwaySyncSchema>;
|
||||
|
||||
export type TRailwaySyncInput = z.infer<typeof CreateRailwaySyncSchema>;
|
||||
|
||||
export type TRailwaySyncWithCredentials = TRailwaySync & {
|
||||
connection: TRailwayConnection;
|
||||
};
|
||||
|
||||
export type TRailwaySecret = {
|
||||
createdAt: string;
|
||||
environmentId?: string | null;
|
||||
id: string;
|
||||
isSealed: boolean;
|
||||
name: string;
|
||||
serviceId?: string | null;
|
||||
updatedAt: string;
|
||||
};
|
||||
|
||||
export type TRailwayVariablesGraphResponse = {
|
||||
data: {
|
||||
variables: Record<string, string>;
|
||||
};
|
||||
};
|
||||
@@ -20,7 +20,9 @@ export enum SecretSync {
|
||||
Render = "render",
|
||||
Flyio = "flyio",
|
||||
GitLab = "gitlab",
|
||||
CloudflarePages = "cloudflare-pages"
|
||||
CloudflarePages = "cloudflare-pages",
|
||||
Zabbix = "zabbix",
|
||||
Railway = "railway"
|
||||
}
|
||||
|
||||
export enum SecretSyncInitialSyncBehavior {
|
||||
|
||||
@@ -39,12 +39,15 @@ import { HC_VAULT_SYNC_LIST_OPTION, HCVaultSyncFns } from "./hc-vault";
|
||||
import { HEROKU_SYNC_LIST_OPTION, HerokuSyncFns } from "./heroku";
|
||||
import { HUMANITEC_SYNC_LIST_OPTION } from "./humanitec";
|
||||
import { HumanitecSyncFns } from "./humanitec/humanitec-sync-fns";
|
||||
import { RAILWAY_SYNC_LIST_OPTION } from "./railway/railway-sync-constants";
|
||||
import { RailwaySyncFns } from "./railway/railway-sync-fns";
|
||||
import { RENDER_SYNC_LIST_OPTION, RenderSyncFns } from "./render";
|
||||
import { SECRET_SYNC_PLAN_MAP } from "./secret-sync-maps";
|
||||
import { TEAMCITY_SYNC_LIST_OPTION, TeamCitySyncFns } from "./teamcity";
|
||||
import { TERRAFORM_CLOUD_SYNC_LIST_OPTION, TerraformCloudSyncFns } from "./terraform-cloud";
|
||||
import { VERCEL_SYNC_LIST_OPTION, VercelSyncFns } from "./vercel";
|
||||
import { WINDMILL_SYNC_LIST_OPTION, WindmillSyncFns } from "./windmill";
|
||||
import { ZABBIX_SYNC_LIST_OPTION, ZabbixSyncFns } from "./zabbix";
|
||||
|
||||
const SECRET_SYNC_LIST_OPTIONS: Record<SecretSync, TSecretSyncListItem> = {
|
||||
[SecretSync.AWSParameterStore]: AWS_PARAMETER_STORE_SYNC_LIST_OPTION,
|
||||
@@ -68,7 +71,9 @@ const SECRET_SYNC_LIST_OPTIONS: Record<SecretSync, TSecretSyncListItem> = {
|
||||
[SecretSync.Render]: RENDER_SYNC_LIST_OPTION,
|
||||
[SecretSync.Flyio]: FLYIO_SYNC_LIST_OPTION,
|
||||
[SecretSync.GitLab]: GITLAB_SYNC_LIST_OPTION,
|
||||
[SecretSync.CloudflarePages]: CLOUDFLARE_PAGES_SYNC_LIST_OPTION
|
||||
[SecretSync.CloudflarePages]: CLOUDFLARE_PAGES_SYNC_LIST_OPTION,
|
||||
[SecretSync.Zabbix]: ZABBIX_SYNC_LIST_OPTION,
|
||||
[SecretSync.Railway]: RAILWAY_SYNC_LIST_OPTION
|
||||
};
|
||||
|
||||
export const listSecretSyncOptions = () => {
|
||||
@@ -236,6 +241,10 @@ export const SecretSyncFns = {
|
||||
return GitLabSyncFns.syncSecrets(secretSync, schemaSecretMap, { appConnectionDAL, kmsService });
|
||||
case SecretSync.CloudflarePages:
|
||||
return CloudflarePagesSyncFns.syncSecrets(secretSync, schemaSecretMap);
|
||||
case SecretSync.Zabbix:
|
||||
return ZabbixSyncFns.syncSecrets(secretSync, schemaSecretMap);
|
||||
case SecretSync.Railway:
|
||||
return RailwaySyncFns.syncSecrets(secretSync, schemaSecretMap);
|
||||
default:
|
||||
throw new Error(
|
||||
`Unhandled sync destination for sync secrets fns: ${(secretSync as TSecretSyncWithCredentials).destination}`
|
||||
@@ -328,6 +337,12 @@ export const SecretSyncFns = {
|
||||
case SecretSync.CloudflarePages:
|
||||
secretMap = await CloudflarePagesSyncFns.getSecrets(secretSync);
|
||||
break;
|
||||
case SecretSync.Zabbix:
|
||||
secretMap = await ZabbixSyncFns.getSecrets(secretSync);
|
||||
break;
|
||||
case SecretSync.Railway:
|
||||
secretMap = await RailwaySyncFns.getSecrets(secretSync);
|
||||
break;
|
||||
default:
|
||||
throw new Error(
|
||||
`Unhandled sync destination for get secrets fns: ${(secretSync as TSecretSyncWithCredentials).destination}`
|
||||
@@ -405,6 +420,10 @@ export const SecretSyncFns = {
|
||||
return GitLabSyncFns.removeSecrets(secretSync, schemaSecretMap, { appConnectionDAL, kmsService });
|
||||
case SecretSync.CloudflarePages:
|
||||
return CloudflarePagesSyncFns.removeSecrets(secretSync, schemaSecretMap);
|
||||
case SecretSync.Zabbix:
|
||||
return ZabbixSyncFns.removeSecrets(secretSync, schemaSecretMap);
|
||||
case SecretSync.Railway:
|
||||
return RailwaySyncFns.removeSecrets(secretSync, schemaSecretMap);
|
||||
default:
|
||||
throw new Error(
|
||||
`Unhandled sync destination for remove secrets fns: ${(secretSync as TSecretSyncWithCredentials).destination}`
|
||||
|
||||
@@ -23,7 +23,9 @@ export const SECRET_SYNC_NAME_MAP: Record<SecretSync, string> = {
|
||||
[SecretSync.Render]: "Render",
|
||||
[SecretSync.Flyio]: "Fly.io",
|
||||
[SecretSync.GitLab]: "GitLab",
|
||||
[SecretSync.CloudflarePages]: "Cloudflare Pages"
|
||||
[SecretSync.CloudflarePages]: "Cloudflare Pages",
|
||||
[SecretSync.Zabbix]: "Zabbix",
|
||||
[SecretSync.Railway]: "Railway"
|
||||
};
|
||||
|
||||
export const SECRET_SYNC_CONNECTION_MAP: Record<SecretSync, AppConnection> = {
|
||||
@@ -48,7 +50,9 @@ export const SECRET_SYNC_CONNECTION_MAP: Record<SecretSync, AppConnection> = {
|
||||
[SecretSync.Render]: AppConnection.Render,
|
||||
[SecretSync.Flyio]: AppConnection.Flyio,
|
||||
[SecretSync.GitLab]: AppConnection.GitLab,
|
||||
[SecretSync.CloudflarePages]: AppConnection.Cloudflare
|
||||
[SecretSync.CloudflarePages]: AppConnection.Cloudflare,
|
||||
[SecretSync.Zabbix]: AppConnection.Zabbix,
|
||||
[SecretSync.Railway]: AppConnection.Railway
|
||||
};
|
||||
|
||||
export const SECRET_SYNC_PLAN_MAP: Record<SecretSync, SecretSyncPlanType> = {
|
||||
@@ -73,5 +77,7 @@ export const SECRET_SYNC_PLAN_MAP: Record<SecretSync, SecretSyncPlanType> = {
|
||||
[SecretSync.Render]: SecretSyncPlanType.Regular,
|
||||
[SecretSync.Flyio]: SecretSyncPlanType.Regular,
|
||||
[SecretSync.GitLab]: SecretSyncPlanType.Regular,
|
||||
[SecretSync.CloudflarePages]: SecretSyncPlanType.Regular
|
||||
[SecretSync.CloudflarePages]: SecretSyncPlanType.Regular,
|
||||
[SecretSync.Zabbix]: SecretSyncPlanType.Regular,
|
||||
[SecretSync.Railway]: SecretSyncPlanType.Regular
|
||||
};
|
||||
|
||||
@@ -94,6 +94,12 @@ import {
|
||||
THumanitecSyncListItem,
|
||||
THumanitecSyncWithCredentials
|
||||
} from "./humanitec";
|
||||
import {
|
||||
TRailwaySync,
|
||||
TRailwaySyncInput,
|
||||
TRailwaySyncListItem,
|
||||
TRailwaySyncWithCredentials
|
||||
} from "./railway/railway-sync-types";
|
||||
import {
|
||||
TRenderSync,
|
||||
TRenderSyncInput,
|
||||
@@ -113,6 +119,7 @@ import {
|
||||
TTerraformCloudSyncWithCredentials
|
||||
} from "./terraform-cloud";
|
||||
import { TVercelSync, TVercelSyncInput, TVercelSyncListItem, TVercelSyncWithCredentials } from "./vercel";
|
||||
import { TZabbixSync, TZabbixSyncInput, TZabbixSyncListItem, TZabbixSyncWithCredentials } from "./zabbix";
|
||||
|
||||
export type TSecretSync =
|
||||
| TAwsParameterStoreSync
|
||||
@@ -136,7 +143,9 @@ export type TSecretSync =
|
||||
| TRenderSync
|
||||
| TFlyioSync
|
||||
| TGitLabSync
|
||||
| TCloudflarePagesSync;
|
||||
| TCloudflarePagesSync
|
||||
| TZabbixSync
|
||||
| TRailwaySync;
|
||||
|
||||
export type TSecretSyncWithCredentials =
|
||||
| TAwsParameterStoreSyncWithCredentials
|
||||
@@ -160,7 +169,9 @@ export type TSecretSyncWithCredentials =
|
||||
| TRenderSyncWithCredentials
|
||||
| TFlyioSyncWithCredentials
|
||||
| TGitLabSyncWithCredentials
|
||||
| TCloudflarePagesSyncWithCredentials;
|
||||
| TCloudflarePagesSyncWithCredentials
|
||||
| TZabbixSyncWithCredentials
|
||||
| TRailwaySyncWithCredentials;
|
||||
|
||||
export type TSecretSyncInput =
|
||||
| TAwsParameterStoreSyncInput
|
||||
@@ -184,7 +195,9 @@ export type TSecretSyncInput =
|
||||
| TRenderSyncInput
|
||||
| TFlyioSyncInput
|
||||
| TGitLabSyncInput
|
||||
| TCloudflarePagesSyncInput;
|
||||
| TCloudflarePagesSyncInput
|
||||
| TZabbixSyncInput
|
||||
| TRailwaySyncInput;
|
||||
|
||||
export type TSecretSyncListItem =
|
||||
| TAwsParameterStoreSyncListItem
|
||||
@@ -208,7 +221,9 @@ export type TSecretSyncListItem =
|
||||
| TRenderSyncListItem
|
||||
| TFlyioSyncListItem
|
||||
| TGitLabSyncListItem
|
||||
| TCloudflarePagesSyncListItem;
|
||||
| TCloudflarePagesSyncListItem
|
||||
| TZabbixSyncListItem
|
||||
| TRailwaySyncListItem;
|
||||
|
||||
export type TSyncOptionsConfig = {
|
||||
canImportSecrets: boolean;
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
export * from "./zabbix-sync-constants";
|
||||
export * from "./zabbix-sync-enums";
|
||||
export * from "./zabbix-sync-fns";
|
||||
export * from "./zabbix-sync-schemas";
|
||||
export * from "./zabbix-sync-types";
|
||||
@@ -0,0 +1,10 @@
|
||||
import { AppConnection } from "@app/services/app-connection/app-connection-enums";
|
||||
import { SecretSync } from "@app/services/secret-sync/secret-sync-enums";
|
||||
import { TSecretSyncListItem } from "@app/services/secret-sync/secret-sync-types";
|
||||
|
||||
export const ZABBIX_SYNC_LIST_OPTION: TSecretSyncListItem = {
|
||||
name: "Zabbix",
|
||||
destination: SecretSync.Zabbix,
|
||||
connection: AppConnection.Zabbix,
|
||||
canImportSecrets: true
|
||||
};
|
||||
@@ -0,0 +1,4 @@
|
||||
export enum ZabbixSyncScope {
|
||||
Global = "global",
|
||||
Host = "host"
|
||||
}
|
||||
@@ -0,0 +1,285 @@
|
||||
import RE2 from "re2";
|
||||
|
||||
import { request } from "@app/lib/config/request";
|
||||
import { blockLocalAndPrivateIpAddresses } from "@app/lib/validator";
|
||||
import { SecretSyncError } from "@app/services/secret-sync/secret-sync-errors";
|
||||
import { matchesSchema } from "@app/services/secret-sync/secret-sync-fns";
|
||||
import { TSecretMap } from "@app/services/secret-sync/secret-sync-types";
|
||||
import {
|
||||
TZabbixSecret,
|
||||
TZabbixSyncWithCredentials,
|
||||
ZabbixApiResponse,
|
||||
ZabbixMacroCreateResponse,
|
||||
ZabbixMacroDeleteResponse
|
||||
} from "@app/services/secret-sync/zabbix/zabbix-sync-types";
|
||||
|
||||
import { ZabbixSyncScope } from "./zabbix-sync-enums";
|
||||
|
||||
const TRAILING_SLASH_REGEX = new RE2("/+$");
|
||||
const MACRO_START_REGEX = new RE2("^\\{\\$");
|
||||
const MACRO_END_REGEX = new RE2("\\}$");
|
||||
|
||||
const extractMacroKey = (macro: string): string => {
|
||||
return macro.replace(MACRO_START_REGEX, "").replace(MACRO_END_REGEX, "");
|
||||
};
|
||||
|
||||
// Helper function to handle Zabbix API responses and errors
|
||||
const handleZabbixResponse = <T>(response: ZabbixApiResponse<T>): T => {
|
||||
if (response.data.error) {
|
||||
const errorMessage = response.data.error.data
|
||||
? `${response.data.error.message}: ${response.data.error.data}`
|
||||
: response.data.error.message;
|
||||
throw new SecretSyncError({
|
||||
error: new Error(`Zabbix API Error (${response.data.error.code}): ${errorMessage}`)
|
||||
});
|
||||
}
|
||||
|
||||
if (response.data.result === undefined) {
|
||||
throw new SecretSyncError({
|
||||
error: new Error("Zabbix API returned no result")
|
||||
});
|
||||
}
|
||||
|
||||
return response.data.result;
|
||||
};
|
||||
|
||||
const listZabbixSecrets = async (apiToken: string, instanceUrl: string, hostId?: string): Promise<TZabbixSecret[]> => {
|
||||
const apiUrl = `${instanceUrl.replace(TRAILING_SLASH_REGEX, "")}/api_jsonrpc.php`;
|
||||
|
||||
// - jsonrpc: Specifies the JSON-RPC protocol version.
|
||||
// - method: The API method to call, in this case "usermacro.get" for retrieving user macros.
|
||||
// - id: A unique identifier for the request. Required by JSON-RPC but not used by the API for logic. Typically set to any integer.
|
||||
const payload = {
|
||||
jsonrpc: "2.0" as const,
|
||||
method: "usermacro.get",
|
||||
params: hostId ? { output: "extend", hostids: hostId } : { output: "extend", globalmacro: true },
|
||||
id: 1
|
||||
};
|
||||
|
||||
try {
|
||||
const response: ZabbixApiResponse<TZabbixSecret[]> = await request.post(apiUrl, payload, {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${apiToken}`
|
||||
}
|
||||
});
|
||||
|
||||
return handleZabbixResponse(response) || [];
|
||||
} catch (error) {
|
||||
throw new SecretSyncError({
|
||||
error: error instanceof Error ? error : new Error("Failed to list Zabbix secrets")
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const putZabbixSecrets = async (
|
||||
apiToken: string,
|
||||
instanceUrl: string,
|
||||
secretMap: TSecretMap,
|
||||
destinationConfig: TZabbixSyncWithCredentials["destinationConfig"],
|
||||
existingSecrets: TZabbixSecret[]
|
||||
): Promise<void> => {
|
||||
const apiUrl = `${instanceUrl.replace(TRAILING_SLASH_REGEX, "")}/api_jsonrpc.php`;
|
||||
const hostId = destinationConfig.scope === ZabbixSyncScope.Host ? destinationConfig.hostId : undefined;
|
||||
|
||||
const existingMacroMap = new Map(existingSecrets.map((secret) => [secret.macro, secret]));
|
||||
|
||||
for (const [key, secret] of Object.entries(secretMap)) {
|
||||
const macroKey = `{$${key.toUpperCase()}}`;
|
||||
const existingMacro = existingMacroMap.get(macroKey);
|
||||
|
||||
try {
|
||||
if (existingMacro) {
|
||||
// Update existing macro
|
||||
const updatePayload = {
|
||||
jsonrpc: "2.0" as const,
|
||||
method: hostId ? "usermacro.update" : "usermacro.updateglobal",
|
||||
params: {
|
||||
[hostId ? "hostmacroid" : "globalmacroid"]: existingMacro[hostId ? "hostmacroid" : "globalmacroid"],
|
||||
value: secret.value,
|
||||
type: destinationConfig.macroType,
|
||||
description: secret.comment
|
||||
},
|
||||
id: 1
|
||||
};
|
||||
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const response: ZabbixApiResponse<ZabbixMacroCreateResponse> = await request.post(apiUrl, updatePayload, {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${apiToken}`
|
||||
}
|
||||
});
|
||||
|
||||
handleZabbixResponse(response);
|
||||
} else {
|
||||
// Create new macro
|
||||
const createPayload = {
|
||||
jsonrpc: "2.0" as const,
|
||||
method: hostId ? "usermacro.create" : "usermacro.createglobal",
|
||||
params: hostId
|
||||
? {
|
||||
hostid: hostId,
|
||||
macro: macroKey,
|
||||
value: secret.value,
|
||||
type: destinationConfig.macroType,
|
||||
description: secret.comment
|
||||
}
|
||||
: {
|
||||
macro: macroKey,
|
||||
value: secret.value,
|
||||
type: destinationConfig.macroType,
|
||||
description: secret.comment
|
||||
},
|
||||
id: 1
|
||||
};
|
||||
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const response: ZabbixApiResponse<ZabbixMacroCreateResponse> = await request.post(apiUrl, createPayload, {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${apiToken}`
|
||||
}
|
||||
});
|
||||
|
||||
handleZabbixResponse(response);
|
||||
}
|
||||
} catch (error) {
|
||||
throw new SecretSyncError({
|
||||
error: error instanceof Error ? error : new Error(`Failed to sync secret ${key}`)
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const deleteZabbixSecrets = async (
|
||||
apiToken: string,
|
||||
instanceUrl: string,
|
||||
keys: string[],
|
||||
hostId?: string
|
||||
): Promise<void> => {
|
||||
if (keys.length === 0) return;
|
||||
|
||||
const apiUrl = `${instanceUrl.replace(TRAILING_SLASH_REGEX, "")}/api_jsonrpc.php`;
|
||||
|
||||
try {
|
||||
// Get existing macros to find their IDs
|
||||
const existingSecrets = await listZabbixSecrets(apiToken, instanceUrl, hostId);
|
||||
const macroIds = existingSecrets
|
||||
.filter((secret) => keys.includes(secret.macro))
|
||||
.map((secret) => secret[hostId ? "hostmacroid" : "globalmacroid"])
|
||||
.filter(Boolean);
|
||||
|
||||
if (macroIds.length === 0) return;
|
||||
|
||||
const payload = {
|
||||
jsonrpc: "2.0" as const,
|
||||
method: hostId ? "usermacro.delete" : "usermacro.deleteglobal",
|
||||
params: macroIds,
|
||||
id: 1
|
||||
};
|
||||
|
||||
const response: ZabbixApiResponse<ZabbixMacroDeleteResponse> = await request.post(apiUrl, payload, {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${apiToken}`
|
||||
}
|
||||
});
|
||||
|
||||
handleZabbixResponse(response);
|
||||
} catch (error) {
|
||||
throw new SecretSyncError({
|
||||
error: error instanceof Error ? error : new Error("Failed to delete Zabbix secrets")
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const ZabbixSyncFns = {
|
||||
syncSecrets: async (secretSync: TZabbixSyncWithCredentials, secretMap: TSecretMap) => {
|
||||
const { connection, environment, destinationConfig } = secretSync;
|
||||
const { apiToken, instanceUrl } = connection.credentials;
|
||||
await blockLocalAndPrivateIpAddresses(instanceUrl);
|
||||
|
||||
const hostId = destinationConfig.scope === ZabbixSyncScope.Host ? destinationConfig.hostId : undefined;
|
||||
let secrets: TZabbixSecret[] = [];
|
||||
try {
|
||||
secrets = await listZabbixSecrets(apiToken, instanceUrl, hostId);
|
||||
} catch (error) {
|
||||
throw new SecretSyncError({
|
||||
error: error instanceof Error ? error : new Error("Failed to list Zabbix secrets")
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
await putZabbixSecrets(apiToken, instanceUrl, secretMap, destinationConfig, secrets);
|
||||
} catch (error) {
|
||||
throw new SecretSyncError({
|
||||
error: error instanceof Error ? error : new Error("Failed to sync secrets")
|
||||
});
|
||||
}
|
||||
|
||||
if (secretSync.syncOptions.disableSecretDeletion) return;
|
||||
|
||||
try {
|
||||
const shapedSecretMapKeys = Object.keys(secretMap).map((key) => key.toUpperCase());
|
||||
|
||||
const keys = secrets
|
||||
.filter(
|
||||
(secret) =>
|
||||
matchesSchema(secret.macro, environment?.slug || "", secretSync.syncOptions.keySchema) &&
|
||||
!shapedSecretMapKeys.includes(extractMacroKey(secret.macro))
|
||||
)
|
||||
.map((secret) => secret.macro);
|
||||
|
||||
await deleteZabbixSecrets(apiToken, instanceUrl, keys, hostId);
|
||||
} catch (error) {
|
||||
throw new SecretSyncError({
|
||||
error: error instanceof Error ? error : new Error("Failed to delete orphaned secrets")
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
removeSecrets: async (secretSync: TZabbixSyncWithCredentials, secretMap: TSecretMap) => {
|
||||
const { connection, destinationConfig } = secretSync;
|
||||
const { apiToken, instanceUrl } = connection.credentials;
|
||||
await blockLocalAndPrivateIpAddresses(instanceUrl);
|
||||
|
||||
const hostId = destinationConfig.scope === ZabbixSyncScope.Host ? destinationConfig.hostId : undefined;
|
||||
|
||||
try {
|
||||
const secrets = await listZabbixSecrets(apiToken, instanceUrl, hostId);
|
||||
|
||||
const shapedSecretMapKeys = Object.keys(secretMap).map((key) => key.toUpperCase());
|
||||
const keys = secrets
|
||||
.filter((secret) => shapedSecretMapKeys.includes(extractMacroKey(secret.macro)))
|
||||
.map((secret) => secret.macro);
|
||||
|
||||
await deleteZabbixSecrets(apiToken, instanceUrl, keys, hostId);
|
||||
} catch (error) {
|
||||
throw new SecretSyncError({
|
||||
error: error instanceof Error ? error : new Error("Failed to remove secrets")
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
getSecrets: async (secretSync: TZabbixSyncWithCredentials) => {
|
||||
const { connection, destinationConfig } = secretSync;
|
||||
const { apiToken, instanceUrl } = connection.credentials;
|
||||
await blockLocalAndPrivateIpAddresses(instanceUrl);
|
||||
const hostId = destinationConfig.scope === ZabbixSyncScope.Host ? destinationConfig.hostId : undefined;
|
||||
|
||||
try {
|
||||
const secrets = await listZabbixSecrets(apiToken, instanceUrl, hostId);
|
||||
return Object.fromEntries(
|
||||
secrets.map((secret) => [
|
||||
extractMacroKey(secret.macro),
|
||||
{ value: secret.value ?? "", comment: secret.description }
|
||||
])
|
||||
);
|
||||
} catch (error) {
|
||||
throw new SecretSyncError({
|
||||
error: error instanceof Error ? error : new Error("Failed to get secrets")
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,67 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import { SecretSyncs } from "@app/lib/api-docs";
|
||||
import { AppConnection } from "@app/services/app-connection/app-connection-enums";
|
||||
import { SecretSync } from "@app/services/secret-sync/secret-sync-enums";
|
||||
import {
|
||||
BaseSecretSyncSchema,
|
||||
GenericCreateSecretSyncFieldsSchema,
|
||||
GenericUpdateSecretSyncFieldsSchema
|
||||
} from "@app/services/secret-sync/secret-sync-schemas";
|
||||
import { TSyncOptionsConfig } from "@app/services/secret-sync/secret-sync-types";
|
||||
|
||||
import { ZabbixSyncScope } from "./zabbix-sync-enums";
|
||||
|
||||
const ZabbixSyncDestinationConfigSchema = z.discriminatedUnion("scope", [
|
||||
z.object({
|
||||
scope: z.literal(ZabbixSyncScope.Host).describe(SecretSyncs.DESTINATION_CONFIG.ZABBIX.scope),
|
||||
hostId: z.string().trim().min(1, "Host required").max(255).describe(SecretSyncs.DESTINATION_CONFIG.ZABBIX.hostId),
|
||||
hostName: z
|
||||
.string()
|
||||
.trim()
|
||||
.min(1, "Host name required")
|
||||
.max(255)
|
||||
.describe(SecretSyncs.DESTINATION_CONFIG.ZABBIX.hostName),
|
||||
macroType: z
|
||||
.number()
|
||||
.min(0, "Macro type required")
|
||||
.max(1, "Macro type required")
|
||||
.describe(SecretSyncs.DESTINATION_CONFIG.ZABBIX.macroType)
|
||||
}),
|
||||
z.object({
|
||||
scope: z.literal(ZabbixSyncScope.Global).describe(SecretSyncs.DESTINATION_CONFIG.ZABBIX.scope),
|
||||
macroType: z
|
||||
.number()
|
||||
.min(0, "Macro type required")
|
||||
.max(1, "Macro type required")
|
||||
.describe(SecretSyncs.DESTINATION_CONFIG.ZABBIX.macroType)
|
||||
})
|
||||
]);
|
||||
|
||||
const ZabbixSyncOptionsConfig: TSyncOptionsConfig = { canImportSecrets: true };
|
||||
|
||||
export const ZabbixSyncSchema = BaseSecretSyncSchema(SecretSync.Zabbix, ZabbixSyncOptionsConfig).extend({
|
||||
destination: z.literal(SecretSync.Zabbix),
|
||||
destinationConfig: ZabbixSyncDestinationConfigSchema
|
||||
});
|
||||
|
||||
export const CreateZabbixSyncSchema = GenericCreateSecretSyncFieldsSchema(
|
||||
SecretSync.Zabbix,
|
||||
ZabbixSyncOptionsConfig
|
||||
).extend({
|
||||
destinationConfig: ZabbixSyncDestinationConfigSchema
|
||||
});
|
||||
|
||||
export const UpdateZabbixSyncSchema = GenericUpdateSecretSyncFieldsSchema(
|
||||
SecretSync.Zabbix,
|
||||
ZabbixSyncOptionsConfig
|
||||
).extend({
|
||||
destinationConfig: ZabbixSyncDestinationConfigSchema.optional()
|
||||
});
|
||||
|
||||
export const ZabbixSyncListItemSchema = z.object({
|
||||
name: z.literal("Zabbix"),
|
||||
connection: z.literal(AppConnection.Zabbix),
|
||||
destination: z.literal(SecretSync.Zabbix),
|
||||
canImportSecrets: z.literal(true)
|
||||
});
|
||||
@@ -0,0 +1,75 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import { TZabbixConnection } from "@app/services/app-connection/zabbix";
|
||||
|
||||
import { CreateZabbixSyncSchema, ZabbixSyncListItemSchema, ZabbixSyncSchema } from "./zabbix-sync-schemas";
|
||||
|
||||
export type TZabbixSync = z.infer<typeof ZabbixSyncSchema>;
|
||||
export type TZabbixSyncInput = z.infer<typeof CreateZabbixSyncSchema>;
|
||||
export type TZabbixSyncListItem = z.infer<typeof ZabbixSyncListItemSchema>;
|
||||
|
||||
export type TZabbixSyncWithCredentials = TZabbixSync & {
|
||||
connection: TZabbixConnection;
|
||||
};
|
||||
|
||||
export type TZabbixSecret = {
|
||||
macro: string;
|
||||
value: string;
|
||||
description?: string;
|
||||
globalmacroid?: string;
|
||||
hostmacroid?: string;
|
||||
hostid?: string;
|
||||
type: number;
|
||||
automatic?: string;
|
||||
};
|
||||
|
||||
export interface ZabbixApiResponse<T = unknown> {
|
||||
data: {
|
||||
jsonrpc: "2.0";
|
||||
result?: T;
|
||||
error?: {
|
||||
code: number;
|
||||
message: string;
|
||||
data?: string;
|
||||
};
|
||||
id: number;
|
||||
};
|
||||
}
|
||||
|
||||
export interface ZabbixMacroCreateResponse {
|
||||
hostmacroids?: string[];
|
||||
globalmacroids?: string[];
|
||||
}
|
||||
|
||||
export interface ZabbixMacroUpdateResponse {
|
||||
hostmacroids?: string[];
|
||||
globalmacroids?: string[];
|
||||
}
|
||||
|
||||
export interface ZabbixMacroDeleteResponse {
|
||||
hostmacroids?: string[];
|
||||
globalmacroids?: string[];
|
||||
}
|
||||
|
||||
export enum ZabbixMacroType {
|
||||
TEXT = 0,
|
||||
SECRET = 1
|
||||
}
|
||||
|
||||
export interface ZabbixMacroInput {
|
||||
hostid?: string;
|
||||
macro: string;
|
||||
value: string;
|
||||
description?: string;
|
||||
type?: ZabbixMacroType;
|
||||
automatic?: "0" | "1";
|
||||
}
|
||||
|
||||
export interface ZabbixMacroUpdate {
|
||||
hostmacroid?: string;
|
||||
globalmacroid?: string;
|
||||
value?: string;
|
||||
description?: string;
|
||||
type?: ZabbixMacroType;
|
||||
automatic?: "0" | "1";
|
||||
}
|
||||
@@ -5,7 +5,13 @@ import jwt from "jsonwebtoken";
|
||||
import { IdentityAuthMethod, OrgMembershipRole, TSuperAdmin, TSuperAdminUpdate } from "@app/db/schemas";
|
||||
import { TLicenseServiceFactory } from "@app/ee/services/license/license-service";
|
||||
import { PgSqlLock, TKeyStoreFactory } from "@app/keystore/keystore";
|
||||
import { getConfig } from "@app/lib/config/env";
|
||||
import {
|
||||
getConfig,
|
||||
getOriginalConfig,
|
||||
overrideEnvConfig,
|
||||
overwriteSchema,
|
||||
validateOverrides
|
||||
} from "@app/lib/config/env";
|
||||
import { infisicalSymmetricEncypt } from "@app/lib/crypto/encryption";
|
||||
import { generateUserSrpKeys, getUserPrivateKey } from "@app/lib/crypto/srp";
|
||||
import { BadRequestError, NotFoundError } from "@app/lib/errors";
|
||||
@@ -33,6 +39,7 @@ import { TInvalidateCacheQueueFactory } from "./invalidate-cache-queue";
|
||||
import { TSuperAdminDALFactory } from "./super-admin-dal";
|
||||
import {
|
||||
CacheType,
|
||||
EnvOverrides,
|
||||
LoginMethod,
|
||||
TAdminBootstrapInstanceDTO,
|
||||
TAdminGetIdentitiesDTO,
|
||||
@@ -234,6 +241,45 @@ export const superAdminServiceFactory = ({
|
||||
adminIntegrationsConfig = config;
|
||||
};
|
||||
|
||||
const getEnvOverrides = async () => {
|
||||
const serverCfg = await serverCfgDAL.findById(ADMIN_CONFIG_DB_UUID);
|
||||
|
||||
if (!serverCfg || !serverCfg.encryptedEnvOverrides) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const decrypt = kmsService.decryptWithRootKey();
|
||||
|
||||
const overrides = JSON.parse(decrypt(serverCfg.encryptedEnvOverrides).toString()) as Record<string, string>;
|
||||
|
||||
return overrides;
|
||||
};
|
||||
|
||||
const getEnvOverridesOrganized = async (): Promise<EnvOverrides> => {
|
||||
const overrides = await getEnvOverrides();
|
||||
const ogConfig = getOriginalConfig();
|
||||
|
||||
return Object.fromEntries(
|
||||
Object.entries(overwriteSchema).map(([groupKey, groupDef]) => [
|
||||
groupKey,
|
||||
{
|
||||
name: groupDef.name,
|
||||
fields: groupDef.fields.map(({ key, description }) => ({
|
||||
key,
|
||||
description,
|
||||
value: overrides[key] || "",
|
||||
hasEnvEntry: !!(ogConfig as unknown as Record<string, string | undefined>)[key]
|
||||
}))
|
||||
}
|
||||
])
|
||||
);
|
||||
};
|
||||
|
||||
const $syncEnvConfig = async () => {
|
||||
const config = await getEnvOverrides();
|
||||
overrideEnvConfig(config);
|
||||
};
|
||||
|
||||
const updateServerCfg = async (
|
||||
data: TSuperAdminUpdate & {
|
||||
slackClientId?: string;
|
||||
@@ -246,6 +292,7 @@ export const superAdminServiceFactory = ({
|
||||
gitHubAppConnectionSlug?: string;
|
||||
gitHubAppConnectionId?: string;
|
||||
gitHubAppConnectionPrivateKey?: string;
|
||||
envOverrides?: Record<string, string>;
|
||||
},
|
||||
userId: string
|
||||
) => {
|
||||
@@ -374,6 +421,17 @@ export const superAdminServiceFactory = ({
|
||||
gitHubAppConnectionSettingsUpdated = true;
|
||||
}
|
||||
|
||||
let envOverridesUpdated = false;
|
||||
if (data.envOverrides !== undefined) {
|
||||
// Verify input format
|
||||
validateOverrides(data.envOverrides);
|
||||
|
||||
const encryptedEnvOverrides = encryptWithRoot(Buffer.from(JSON.stringify(data.envOverrides)));
|
||||
updatedData.encryptedEnvOverrides = encryptedEnvOverrides;
|
||||
updatedData.envOverrides = undefined;
|
||||
envOverridesUpdated = true;
|
||||
}
|
||||
|
||||
const updatedServerCfg = await serverCfgDAL.updateById(ADMIN_CONFIG_DB_UUID, updatedData);
|
||||
|
||||
await keyStore.setItemWithExpiry(ADMIN_CONFIG_KEY, ADMIN_CONFIG_KEY_EXP, JSON.stringify(updatedServerCfg));
|
||||
@@ -382,6 +440,10 @@ export const superAdminServiceFactory = ({
|
||||
await $syncAdminIntegrationConfig();
|
||||
}
|
||||
|
||||
if (envOverridesUpdated) {
|
||||
await $syncEnvConfig();
|
||||
}
|
||||
|
||||
if (
|
||||
updatedServerCfg.encryptedMicrosoftTeamsAppId &&
|
||||
updatedServerCfg.encryptedMicrosoftTeamsClientSecret &&
|
||||
@@ -814,6 +876,18 @@ export const superAdminServiceFactory = ({
|
||||
return job;
|
||||
};
|
||||
|
||||
const initializeEnvConfigSync = async () => {
|
||||
logger.info("Setting up background sync process for environment overrides");
|
||||
|
||||
await $syncEnvConfig();
|
||||
|
||||
// sync every 5 minutes
|
||||
const job = new CronJob("*/5 * * * *", $syncEnvConfig);
|
||||
job.start();
|
||||
|
||||
return job;
|
||||
};
|
||||
|
||||
return {
|
||||
initServerCfg,
|
||||
updateServerCfg,
|
||||
@@ -833,6 +907,9 @@ export const superAdminServiceFactory = ({
|
||||
getOrganizations,
|
||||
deleteOrganization,
|
||||
deleteOrganizationMembership,
|
||||
initializeAdminIntegrationConfigSync
|
||||
initializeAdminIntegrationConfigSync,
|
||||
initializeEnvConfigSync,
|
||||
getEnvOverrides,
|
||||
getEnvOverridesOrganized
|
||||
};
|
||||
};
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { TEnvConfig } from "@app/lib/config/env";
|
||||
|
||||
export type TAdminSignUpDTO = {
|
||||
email: string;
|
||||
password: string;
|
||||
@@ -74,3 +76,10 @@ export type TAdminIntegrationConfig = {
|
||||
privateKey: string;
|
||||
};
|
||||
};
|
||||
|
||||
export interface EnvOverrides {
|
||||
[key: string]: {
|
||||
name: string;
|
||||
fields: { key: keyof TEnvConfig; value: string; hasEnvEntry: boolean; description?: string }[];
|
||||
};
|
||||
}
|
||||
|
||||
@@ -71,6 +71,15 @@ export const telemetryQueueServiceFactory = ({
|
||||
QueueName.TelemetryInstanceStats // just a job id
|
||||
);
|
||||
|
||||
if (postHog) {
|
||||
await queueService.queue(QueueName.TelemetryInstanceStats, QueueJobs.TelemetryInstanceStats, undefined, {
|
||||
jobId: QueueName.TelemetryInstanceStats,
|
||||
repeat: { pattern: "0 0 * * *", utc: true }
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const startAggregatedEventsJob = async () => {
|
||||
// clear previous aggregated events job
|
||||
await queueService.stopRepeatableJob(
|
||||
QueueName.TelemetryAggregatedEvents,
|
||||
@@ -80,11 +89,6 @@ export const telemetryQueueServiceFactory = ({
|
||||
);
|
||||
|
||||
if (postHog) {
|
||||
await queueService.queue(QueueName.TelemetryInstanceStats, QueueJobs.TelemetryInstanceStats, undefined, {
|
||||
jobId: QueueName.TelemetryInstanceStats,
|
||||
repeat: { pattern: "0 0 * * *", utc: true }
|
||||
});
|
||||
|
||||
// Start aggregated events job (runs every five minutes)
|
||||
await queueService.queue(QueueName.TelemetryAggregatedEvents, QueueJobs.TelemetryAggregatedEvents, undefined, {
|
||||
jobId: QueueName.TelemetryAggregatedEvents,
|
||||
@@ -102,6 +106,7 @@ export const telemetryQueueServiceFactory = ({
|
||||
});
|
||||
|
||||
return {
|
||||
startTelemetryCheck
|
||||
startTelemetryCheck,
|
||||
startAggregatedEventsJob
|
||||
};
|
||||
};
|
||||
|
||||
@@ -14,7 +14,7 @@ export const TELEMETRY_SECRET_PROCESSED_KEY = "telemetry-secret-processed";
|
||||
export const TELEMETRY_SECRET_OPERATIONS_KEY = "telemetry-secret-operations";
|
||||
|
||||
export const POSTHOG_AGGREGATED_EVENTS = [PostHogEventTypes.SecretPulled];
|
||||
const TELEMETRY_AGGREGATED_KEY_EXP = 900; // 15mins
|
||||
const TELEMETRY_AGGREGATED_KEY_EXP = 600; // 10mins
|
||||
|
||||
// Bucket configuration
|
||||
const TELEMETRY_BUCKET_COUNT = 30;
|
||||
@@ -102,13 +102,6 @@ To opt into telemetry, you can set "TELEMETRY_ENABLED=true" within the environme
|
||||
const instanceType = licenseService.getInstanceType();
|
||||
// capture posthog only when its cloud or signup event happens in self-hosted
|
||||
if (instanceType === InstanceType.Cloud || event.event === PostHogEventTypes.UserSignedUp) {
|
||||
if (event.organizationId) {
|
||||
try {
|
||||
postHog.groupIdentify({ groupType: "organization", groupKey: event.organizationId });
|
||||
} catch (error) {
|
||||
logger.error(error, "Failed to identify PostHog organization");
|
||||
}
|
||||
}
|
||||
if (POSTHOG_AGGREGATED_EVENTS.includes(event.event)) {
|
||||
const eventKey = createTelemetryEventKey(event.event, event.distinctId);
|
||||
await keyStore.setItemWithExpiry(
|
||||
@@ -122,6 +115,13 @@ To opt into telemetry, you can set "TELEMETRY_ENABLED=true" within the environme
|
||||
})
|
||||
);
|
||||
} else {
|
||||
if (event.organizationId) {
|
||||
try {
|
||||
postHog.groupIdentify({ groupType: "organization", groupKey: event.organizationId });
|
||||
} catch (error) {
|
||||
logger.error(error, "Failed to identify PostHog organization");
|
||||
}
|
||||
}
|
||||
postHog.capture({
|
||||
event: event.event,
|
||||
distinctId: event.distinctId,
|
||||
|
||||
Reference in New Issue
Block a user