mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-17 01:12:59 +00:00
feat: secret sync foundation
This commit is contained in:
@@ -1,12 +1,22 @@
|
||||
/* eslint-disable no-await-in-loop */
|
||||
import { AxiosError } from "axios";
|
||||
|
||||
import { request } from "@app/lib/config/request";
|
||||
import { BadRequestError } from "@app/lib/errors";
|
||||
import { BadRequestError, InternalServerError } from "@app/lib/errors";
|
||||
import { IntegrationUrls } from "@app/services/integration-auth/integration-list";
|
||||
|
||||
import { AppConnection } from "../app-connection-enums";
|
||||
import { LaravelForgeConnectionMethod } from "./laravel-forge-connection-enums";
|
||||
import { TLaravelForgeConnectionConfig } from "./laravel-forge-connection-types";
|
||||
import {
|
||||
TLaravelForgeConnection,
|
||||
TLaravelForgeConnectionConfig,
|
||||
TLaravelForgeOrganization,
|
||||
TLaravelForgeServer,
|
||||
TLaravelForgeSite,
|
||||
TRawLaravelForgeOrganization,
|
||||
TRawLaravelForgeServer,
|
||||
TRawLaravelForgeSite
|
||||
} from "./laravel-forge-connection-types";
|
||||
|
||||
export const getLaravelForgeConnectionListItem = () => {
|
||||
return {
|
||||
@@ -41,3 +51,115 @@ export const validateLaravelForgeConnectionCredentials = async (config: TLaravel
|
||||
|
||||
return inputCredentials;
|
||||
};
|
||||
|
||||
type TLaravelForgeApiResponse<T> = {
|
||||
data: T[];
|
||||
links?: {
|
||||
next?: string;
|
||||
};
|
||||
meta?: {
|
||||
next_cursor?: string;
|
||||
prev_cursor?: string | null;
|
||||
};
|
||||
};
|
||||
|
||||
const fetchAllPages = async <T>(
|
||||
apiToken: string,
|
||||
url: string,
|
||||
params?: Record<string, string | number>
|
||||
): Promise<T[]> => {
|
||||
const allItems: T[] = [];
|
||||
let nextUrl: string | null = url;
|
||||
const queryParams = params || {};
|
||||
|
||||
while (nextUrl) {
|
||||
try {
|
||||
const response: { data: TLaravelForgeApiResponse<T> } = await request.get<TLaravelForgeApiResponse<T>>(nextUrl, {
|
||||
params: queryParams,
|
||||
headers: {
|
||||
Authorization: `Bearer ${apiToken}`,
|
||||
Accept: "application/json",
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
});
|
||||
|
||||
if (!response?.data?.data) {
|
||||
throw new InternalServerError({
|
||||
message: `Failed to fetch data from ${url}: Response was empty or malformed`
|
||||
});
|
||||
}
|
||||
|
||||
allItems.push(...response.data.data);
|
||||
|
||||
if (response.data.links?.next) {
|
||||
nextUrl = response.data.links.next;
|
||||
} else {
|
||||
nextUrl = null;
|
||||
}
|
||||
} catch (error) {
|
||||
if (error instanceof AxiosError) {
|
||||
throw new BadRequestError({
|
||||
message: `Failed to fetch data from ${url}: ${error.message || "Unknown error"}`
|
||||
});
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
return allItems;
|
||||
};
|
||||
|
||||
export const listLaravelForgeOrganizations = async (
|
||||
appConnection: TLaravelForgeConnection
|
||||
): Promise<TLaravelForgeOrganization[]> => {
|
||||
const { credentials } = appConnection;
|
||||
const { apiToken } = credentials;
|
||||
|
||||
const rawOrganizations = await fetchAllPages<TRawLaravelForgeOrganization>(
|
||||
apiToken,
|
||||
`${IntegrationUrls.LARAVELFORGE_API_URL}/api/orgs`
|
||||
);
|
||||
|
||||
return rawOrganizations.map((org: TRawLaravelForgeOrganization) => ({
|
||||
id: org.id,
|
||||
name: org.attributes.name,
|
||||
slug: org.attributes.slug
|
||||
}));
|
||||
};
|
||||
|
||||
export const listLaravelForgeServers = async (
|
||||
appConnection: TLaravelForgeConnection,
|
||||
organizationSlug: string
|
||||
): Promise<TLaravelForgeServer[]> => {
|
||||
const { credentials } = appConnection;
|
||||
const { apiToken } = credentials;
|
||||
|
||||
const rawServers = await fetchAllPages<TRawLaravelForgeServer>(
|
||||
apiToken,
|
||||
`${IntegrationUrls.LARAVELFORGE_API_URL}/api/orgs/${organizationSlug}/servers`
|
||||
);
|
||||
|
||||
return rawServers.map((server: TRawLaravelForgeServer) => ({
|
||||
id: server.id,
|
||||
name: server.attributes.name
|
||||
}));
|
||||
};
|
||||
|
||||
export const listLaravelForgeSites = async (
|
||||
appConnection: TLaravelForgeConnection,
|
||||
organizationSlug: string,
|
||||
serverId: string
|
||||
): Promise<TLaravelForgeSite[]> => {
|
||||
const { credentials } = appConnection;
|
||||
const { apiToken } = credentials;
|
||||
|
||||
const rawSites = await fetchAllPages<TRawLaravelForgeSite>(
|
||||
apiToken,
|
||||
`${IntegrationUrls.LARAVELFORGE_API_URL}/api/orgs/${organizationSlug}/servers/${serverId}/sites`
|
||||
);
|
||||
|
||||
return rawSites.map((site: TRawLaravelForgeSite) => ({
|
||||
id: site.id,
|
||||
name: site.attributes.name
|
||||
}));
|
||||
};
|
||||
|
||||
+62
-2
@@ -1,7 +1,18 @@
|
||||
import { logger } from "@app/lib/logger";
|
||||
import { OrgServiceActor } from "@app/lib/types";
|
||||
|
||||
import { AppConnection } from "../app-connection-enums";
|
||||
import { TLaravelForgeConnection } from "./laravel-forge-connection-types";
|
||||
import {
|
||||
listLaravelForgeOrganizations,
|
||||
listLaravelForgeServers,
|
||||
listLaravelForgeSites
|
||||
} from "./laravel-forge-connection-fns";
|
||||
import {
|
||||
TLaravelForgeConnection,
|
||||
TLaravelForgeOrganization,
|
||||
TLaravelForgeServer,
|
||||
TLaravelForgeSite
|
||||
} from "./laravel-forge-connection-types";
|
||||
|
||||
type TGetAppConnectionFunc = (
|
||||
app: AppConnection,
|
||||
@@ -10,5 +21,54 @@ type TGetAppConnectionFunc = (
|
||||
) => Promise<TLaravelForgeConnection>;
|
||||
|
||||
export const laravelForgeConnectionService = (getAppConnection: TGetAppConnectionFunc) => {
|
||||
console.log("laravelForgeConnectionService", getAppConnection);
|
||||
const listOrganizations = async (
|
||||
connectionId: string,
|
||||
actor: OrgServiceActor
|
||||
): Promise<TLaravelForgeOrganization[]> => {
|
||||
const appConnection = await getAppConnection(AppConnection.LaravelForge, connectionId, actor);
|
||||
try {
|
||||
const organizations = await listLaravelForgeOrganizations(appConnection);
|
||||
return organizations;
|
||||
} catch (error) {
|
||||
logger.error(error, "Failed to list organizations for Laravel Forge connection");
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
const listServers = async (
|
||||
connectionId: string,
|
||||
actor: OrgServiceActor,
|
||||
organizationSlug: string
|
||||
): Promise<TLaravelForgeServer[]> => {
|
||||
const appConnection = await getAppConnection(AppConnection.LaravelForge, connectionId, actor);
|
||||
try {
|
||||
const servers = await listLaravelForgeServers(appConnection, organizationSlug);
|
||||
return servers;
|
||||
} catch (error) {
|
||||
logger.error(error, "Failed to list servers for Laravel Forge connection");
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
const listSites = async (
|
||||
connectionId: string,
|
||||
actor: OrgServiceActor,
|
||||
organizationSlug: string,
|
||||
serverId: string
|
||||
): Promise<TLaravelForgeSite[]> => {
|
||||
const appConnection = await getAppConnection(AppConnection.LaravelForge, connectionId, actor);
|
||||
try {
|
||||
const sites = await listLaravelForgeSites(appConnection, organizationSlug, serverId);
|
||||
return sites;
|
||||
} catch (error) {
|
||||
logger.error(error, "Failed to list sites for Laravel Forge connection");
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
listOrganizations,
|
||||
listServers,
|
||||
listSites
|
||||
};
|
||||
};
|
||||
|
||||
+39
-1
@@ -21,5 +21,43 @@ export type TLaravelForgeConnectionConfig = DiscriminativePick<
|
||||
TLaravelForgeConnectionInput,
|
||||
"method" | "app" | "credentials"
|
||||
> & {
|
||||
orgId: string;
|
||||
orgSlug: string;
|
||||
};
|
||||
|
||||
export type TLaravelForgeOrganization = {
|
||||
id: string;
|
||||
name: string;
|
||||
slug: string;
|
||||
};
|
||||
|
||||
export type TLaravelForgeServer = {
|
||||
id: string;
|
||||
name: string;
|
||||
};
|
||||
|
||||
export type TLaravelForgeSite = {
|
||||
id: string;
|
||||
name: string;
|
||||
};
|
||||
|
||||
export type TRawLaravelForgeOrganization = {
|
||||
id: string;
|
||||
attributes: {
|
||||
name: string;
|
||||
slug: string;
|
||||
};
|
||||
};
|
||||
|
||||
export type TRawLaravelForgeServer = {
|
||||
id: string;
|
||||
attributes: {
|
||||
name: string;
|
||||
};
|
||||
};
|
||||
|
||||
export type TRawLaravelForgeSite = {
|
||||
id: string;
|
||||
attributes: {
|
||||
name: string;
|
||||
};
|
||||
};
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
export * from "./laravel-forge-sync-constants";
|
||||
export * from "./laravel-forge-sync-schemas";
|
||||
export * from "./laravel-forge-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 LARAVEL_FORGE_SYNC_LIST_OPTION: TSecretSyncListItem = {
|
||||
name: "Laravel Forge",
|
||||
destination: SecretSync.LaravelForge,
|
||||
connection: AppConnection.LaravelForge,
|
||||
canImportSecrets: true
|
||||
};
|
||||
@@ -0,0 +1,48 @@
|
||||
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 LaravelForgeSyncDestinationConfigSchema = z.object({
|
||||
orgSlug: z.string().min(1, "Org Slug is required").describe(SecretSyncs.DESTINATION_CONFIG.LARAVEL_FORGE.orgSlug),
|
||||
serverId: z.string().min(1, "Server ID is required").describe(SecretSyncs.DESTINATION_CONFIG.LARAVEL_FORGE.serverId),
|
||||
siteId: z.string().min(1, "Site ID is required").describe(SecretSyncs.DESTINATION_CONFIG.LARAVEL_FORGE.siteId)
|
||||
});
|
||||
|
||||
const LaravelForgeSyncOptionsConfig: TSyncOptionsConfig = { canImportSecrets: true };
|
||||
|
||||
export const LaravelForgeSyncSchema = BaseSecretSyncSchema(
|
||||
SecretSync.LaravelForge,
|
||||
LaravelForgeSyncOptionsConfig
|
||||
).extend({
|
||||
destination: z.literal(SecretSync.LaravelForge),
|
||||
destinationConfig: LaravelForgeSyncDestinationConfigSchema
|
||||
});
|
||||
|
||||
export const CreateLaravelForgeSyncSchema = GenericCreateSecretSyncFieldsSchema(
|
||||
SecretSync.LaravelForge,
|
||||
LaravelForgeSyncOptionsConfig
|
||||
).extend({
|
||||
destinationConfig: LaravelForgeSyncDestinationConfigSchema
|
||||
});
|
||||
|
||||
export const UpdateLaravelForgeSyncSchema = GenericUpdateSecretSyncFieldsSchema(
|
||||
SecretSync.LaravelForge,
|
||||
LaravelForgeSyncOptionsConfig
|
||||
).extend({
|
||||
destinationConfig: LaravelForgeSyncDestinationConfigSchema.optional()
|
||||
});
|
||||
|
||||
export const LaravelForgeSyncListItemSchema = z.object({
|
||||
name: z.literal("Laravel Forge"),
|
||||
connection: z.literal(AppConnection.LaravelForge),
|
||||
destination: z.literal(SecretSync.LaravelForge),
|
||||
canImportSecrets: z.literal(true)
|
||||
});
|
||||
@@ -0,0 +1,41 @@
|
||||
import z from "zod";
|
||||
|
||||
import { TLaravelForgeConnection } from "@app/services/app-connection/laravel-forge";
|
||||
|
||||
import {
|
||||
CreateLaravelForgeSyncSchema,
|
||||
LaravelForgeSyncListItemSchema,
|
||||
LaravelForgeSyncSchema
|
||||
} from "./laravel-forge-sync-schemas";
|
||||
|
||||
export type TLaravelForgeSyncListItem = z.infer<typeof LaravelForgeSyncListItemSchema>;
|
||||
|
||||
export type TLaravelForgeSync = z.infer<typeof LaravelForgeSyncSchema>;
|
||||
|
||||
export type TLaravelForgeSyncInput = z.infer<typeof CreateLaravelForgeSyncSchema>;
|
||||
|
||||
export type TLaravelForgeSyncWithCredentials = TLaravelForgeSync & {
|
||||
connection: TLaravelForgeConnection;
|
||||
};
|
||||
|
||||
export type LaravelForgeSecret = {
|
||||
description: string;
|
||||
is_secret: boolean;
|
||||
key: string;
|
||||
source: "app" | "env";
|
||||
value: string;
|
||||
};
|
||||
|
||||
export interface LaravelForgeApiSecret {
|
||||
id: string;
|
||||
key: string;
|
||||
value: string;
|
||||
type: string;
|
||||
target: string[];
|
||||
customEnvironmentIds?: string[];
|
||||
gitBranch?: string;
|
||||
createdAt?: number;
|
||||
updatedAt?: number;
|
||||
configurationId?: string;
|
||||
system?: boolean;
|
||||
}
|
||||
@@ -49,6 +49,7 @@ 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 { LARAVEL_FORGE_SYNC_LIST_OPTION } from "./laravel-forge";
|
||||
import { NETLIFY_SYNC_LIST_OPTION, NetlifySyncFns } from "./netlify";
|
||||
import { RAILWAY_SYNC_LIST_OPTION } from "./railway/railway-sync-constants";
|
||||
import { RailwaySyncFns } from "./railway/railway-sync-fns";
|
||||
@@ -91,7 +92,8 @@ const SECRET_SYNC_LIST_OPTIONS: Record<SecretSync, TSecretSyncListItem> = {
|
||||
[SecretSync.Checkly]: CHECKLY_SYNC_LIST_OPTION,
|
||||
[SecretSync.DigitalOceanAppPlatform]: DIGITAL_OCEAN_APP_PLATFORM_SYNC_LIST_OPTION,
|
||||
[SecretSync.Netlify]: NETLIFY_SYNC_LIST_OPTION,
|
||||
[SecretSync.Bitbucket]: BITBUCKET_SYNC_LIST_OPTION
|
||||
[SecretSync.Bitbucket]: BITBUCKET_SYNC_LIST_OPTION,
|
||||
[SecretSync.LaravelForge]: LARAVEL_FORGE_SYNC_LIST_OPTION
|
||||
};
|
||||
|
||||
export const listSecretSyncOptions = () => {
|
||||
|
||||
@@ -117,6 +117,12 @@ import {
|
||||
THumanitecSyncListItem,
|
||||
THumanitecSyncWithCredentials
|
||||
} from "./humanitec";
|
||||
import {
|
||||
TLaravelForgeSync,
|
||||
TLaravelForgeSyncInput,
|
||||
TLaravelForgeSyncListItem,
|
||||
TLaravelForgeSyncWithCredentials
|
||||
} from "./laravel-forge";
|
||||
import { TNetlifySync, TNetlifySyncInput, TNetlifySyncListItem, TNetlifySyncWithCredentials } from "./netlify";
|
||||
import {
|
||||
TRailwaySync,
|
||||
@@ -164,6 +170,7 @@ export type TSecretSync =
|
||||
| TTerraformCloudSync
|
||||
| TCamundaSync
|
||||
| TVercelSync
|
||||
| TLaravelForgeSync
|
||||
| TWindmillSync
|
||||
| THCVaultSync
|
||||
| TTeamCitySync
|
||||
@@ -212,7 +219,8 @@ export type TSecretSyncWithCredentials =
|
||||
| TSupabaseSyncWithCredentials
|
||||
| TDigitalOceanAppPlatformSyncWithCredentials
|
||||
| TNetlifySyncWithCredentials
|
||||
| TBitbucketSyncWithCredentials;
|
||||
| TBitbucketSyncWithCredentials
|
||||
| TLaravelForgeSyncWithCredentials;
|
||||
|
||||
export type TSecretSyncInput =
|
||||
| TAwsParameterStoreSyncInput
|
||||
@@ -244,7 +252,8 @@ export type TSecretSyncInput =
|
||||
| TSupabaseSyncInput
|
||||
| TDigitalOceanAppPlatformSyncInput
|
||||
| TNetlifySyncInput
|
||||
| TBitbucketSyncInput;
|
||||
| TBitbucketSyncInput
|
||||
| TLaravelForgeSyncInput;
|
||||
|
||||
export type TSecretSyncListItem =
|
||||
| TAwsParameterStoreSyncListItem
|
||||
@@ -259,6 +268,7 @@ export type TSecretSyncListItem =
|
||||
| TTerraformCloudSyncListItem
|
||||
| TCamundaSyncListItem
|
||||
| TVercelSyncListItem
|
||||
| TLaravelForgeSyncListItem
|
||||
| TWindmillSyncListItem
|
||||
| THCVaultSyncListItem
|
||||
| TTeamCitySyncListItem
|
||||
|
||||
Reference in New Issue
Block a user