Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
feat(secret-sync): route portainer requests through gateways and accept oauth tokens
Yeongjin Leed6e5501cf35e
diff --git a/backend/src/server/routes/v1/app-connection-routers/portainer-connection-router.ts b/backend/src/server/routes/v1/app-connection-routers/portainer-connection-router.tsindex d555eeab33..446f0c9748 100644--- a/backend/src/server/routes/v1/app-connection-routers/portainer-connection-router.ts+++ b/backend/src/server/routes/v1/app-connection-routers/portainer-connection-router.ts@@ -44,7 +44,7 @@ export const registerPortainerConnectionRouter = async (server: FastifyZodProvid})}},- onRequest: verifyAuth([AuthMode.JWT]),+ onRequest: verifyAuth([AuthMode.JWT, AuthMode.OAUTH]),handler: async (req) => {const { connectionId } = req.params;@@ -78,7 +78,7 @@ export const registerPortainerConnectionRouter = async (server: FastifyZodProvid})}},- onRequest: verifyAuth([AuthMode.JWT]),+ onRequest: verifyAuth([AuthMode.JWT, AuthMode.OAUTH]),handler: async (req) => {const { connectionId } = req.params;diff --git a/backend/src/services/app-connection/app-connection-service.ts b/backend/src/services/app-connection/app-connection-service.tsindex a348ee2ffa..9287e8dfdf 100644--- a/backend/src/services/app-connection/app-connection-service.ts+++ b/backend/src/services/app-connection/app-connection-service.ts@@ -1489,7 +1489,7 @@ export const appConnectionServiceFactory = ({checkly: checklyConnectionService(connectAppConnectionById),supabase: supabaseConnectionService(connectAppConnectionById),rundeck: rundeckConnectionService(connectAppConnectionById),- portainer: portainerConnectionService(connectAppConnectionById),+ portainer: portainerConnectionService(connectAppConnectionById, gatewayV2Service, gatewayPoolService),digitalOcean: digitalOceanAppPlatformConnectionService(connectAppConnectionById),netlify: netlifyConnectionService(connectAppConnectionById),northflank: northflankConnectionService(connectAppConnectionById),diff --git a/backend/src/services/app-connection/portainer/portainer-connection-enums.ts b/backend/src/services/app-connection/portainer/portainer-connection-enums.tsindex e511787aff..e3aaedd057 100644--- a/backend/src/services/app-connection/portainer/portainer-connection-enums.ts+++ b/backend/src/services/app-connection/portainer/portainer-connection-enums.ts@@ -1,3 +1,16 @@export enum PortainerConnectionMethod {ApiToken = "api-token"}++export enum PortainerStackType {+ Swarm = 1,+ Compose = 2,+ Kubernetes = 3+}++export enum PortainerStackStatus {+ Active = 1,+ Inactive = 2,+ Deploying = 3,+ Error = 4+}diff --git a/backend/src/services/app-connection/portainer/portainer-connection-fns.ts b/backend/src/services/app-connection/portainer/portainer-connection-fns.tsindex 01557108e0..6a18b895da 100644--- a/backend/src/services/app-connection/portainer/portainer-connection-fns.ts+++ b/backend/src/services/app-connection/portainer/portainer-connection-fns.ts@@ -1,16 +1,25 @@-import { AxiosError } from "axios";+import { AxiosRequestConfig, isAxiosError } from "axios";+import { verifyHostInputValidity } from "@app/ee/services/dynamic-secret/dynamic-secret-fns";+import { TGatewayV2ServiceFactory } from "@app/ee/services/gateway-v2/gateway-v2-service";+import { request } from "@app/lib/config/request";import { BadRequestError } from "@app/lib/errors";import { removeTrailingSlash } from "@app/lib/fn";-import { safeRequest } from "@app/lib/validator";+import { withGatewayV2Proxy } from "@app/lib/gateway-v2/gateway-v2";+import { GatewayProxyProtocol } from "@app/lib/gateway-v2/types";+import { logger, sanitizeUrlForLog } from "@app/lib/logger";+import { getSharedHttpsAgent, safeRequest } from "@app/lib/validator/safe-request";import { AppConnection } from "../app-connection-enums";-import { PortainerConnectionMethod } from "./portainer-connection-enums";+import { PortainerConnectionMethod, PortainerStackType } from "./portainer-connection-enums";import {TPortainerConnection,TPortainerConnectionConfig,TPortainerEnvironment,- TPortainerStack+ TPortainerGatewayDeps,+ TPortainerRequestConfig,+ TPortainerStack,+ TPortainerStackResponse} from "./portainer-connection-types";type TPortainerEnvironmentResponse = {@@ -18,22 +27,19 @@ type TPortainerEnvironmentResponse = {Name: string;};-type TPortainerStackResponse = {- Id: number;- Name: string;- EndpointId: number;- GitConfig?: { URL: string } | null;+type TPortainerRequest = {+ method: "GET" | "POST" | "PUT";+ path: string;+ data?: unknown;+ params?: Record<string, string>;+ timeoutMs?: number;};-// URL validation and private-IP blocking happen at request time inside `safeRequest`, which-// also pins the connection to the validated IPs, preventing DNS rebinding between check and request.-export const getPortainerInstanceUrl = (config: TPortainerConnectionConfig) =>- removeTrailingSlash(config.credentials.instanceUrl);+export const PORTAINER_REQUEST_TIMEOUT_MS = 30_000;+const PORTAINER_MAX_RESPONSE_BYTES = 10 * 1024 * 1024;-export const getPortainerRequestHeaders = (apiToken: string) => ({- "X-API-Key": apiToken,- Accept: "application/json"-});+export const getPortainerInstanceUrl = (config: Pick<TPortainerConnectionConfig, "credentials">) =>+ removeTrailingSlash(config.credentials.instanceUrl);export const getPortainerConnectionListItem = () => {return {@@ -43,27 +49,146 @@ export const getPortainerConnectionListItem = () => {};};-export const validatePortainerConnectionCredentials = async (config: TPortainerConnectionConfig) => {- const instanceUrl = getPortainerInstanceUrl(config);- const { apiToken } = config.credentials;+// Portainer 2.45 moved the Git settings onto a Source referenced by WorkflowID; older releases only have GitConfig+export const isPortainerGitStack = (stack: Pick<TPortainerStackResponse, "WorkflowID" | "GitConfig">) =>+ Boolean(stack.WorkflowID) || Boolean(stack.GitConfig?.URL);++const toLoggableError = (error: unknown) => {+ if (isAxiosError(error)) {+ return {+ name: error.name,+ code: error.code,+ status: error.response?.status,+ message: error.message,+ url: error.config?.url ? sanitizeUrlForLog(error.config.url) : undefined,+ stack: error.stack+ };+ }- try {- await safeRequest.get(`${instanceUrl}/api/users/me`, {- headers: getPortainerRequestHeaders(apiToken)+ if (error instanceof Error) return { name: error.name, message: error.message, stack: error.stack };++ return { message: String(error) };+};++export const getPortainerErrorMessage = (error: unknown, fallback: string) => {+ if (isAxiosError(error)) {+ const body = error.response?.data as { message?: string; details?: string } | undefined;+ const detail = [body?.message, body?.details].filter(Boolean).join(": ");++ if (error.response?.status === 401) return "Portainer rejected the API token";+ if (error.response?.status === 403) {+ return `the API token's user is not allowed to perform this action in Portainer${detail ? ` (${detail})` : ""}`;+ }+ if (detail) return detail;++ return error.message || fallback;+ }++ return error instanceof Error ? error.message : fallback;+};++export const portainerRequest = async <T>(+ config: TPortainerRequestConfig,+ { method, path, data, params, timeoutMs = PORTAINER_REQUEST_TIMEOUT_MS }: TPortainerRequest,+ { gatewayV2Service, gatewayPoolService }: TPortainerGatewayDeps = {}+): Promise<T> => {+ const { credentials, gatewayId: directGatewayId, gatewayPoolId } = config;++ if (gatewayPoolId && !gatewayPoolService) {+ throw new BadRequestError({+ message: "This Portainer connection uses a Gateway pool, which is not supported on this operation"+ });+ }++ const gatewayId =+ gatewayPoolId && gatewayPoolService+ ? await gatewayPoolService.resolveEffectiveGatewayId({ gatewayId: directGatewayId, gatewayPoolId })+ : directGatewayId;++ if (gatewayId && !gatewayV2Service) {+ throw new BadRequestError({+ message: "This Portainer connection uses a Gateway, which is not supported on this operation"});- } catch (error: unknown) {- if (error instanceof AxiosError) {- throw new BadRequestError({- message: `Failed to validate Portainer credentials: ${error.message || "Unknown error"}`+ }++ const url = new URL(`${getPortainerInstanceUrl(config)}/api${path}`);+ const headers = {+ "X-API-Key": credentials.apiToken,+ Accept: "application/json",+ ...(data !== undefined && { "Content-Type": "application/json" })+ };++ const baseRequestConfig: AxiosRequestConfig = {+ method,+ params,+ ...(data !== undefined && { data }),+ timeout: timeoutMs,+ maxContentLength: PORTAINER_MAX_RESPONSE_BYTES,+ maxBodyLength: PORTAINER_MAX_RESPONSE_BYTES+ };++ if (!gatewayId || !gatewayV2Service) {+ // URL validation and private-IP blocking happen inside `safeRequest`, which also pins the+ // connection to the validated IPs so DNS rebinding cannot redirect the request after the check+ const { data: responseData } = await safeRequest.request<T>({+ ...baseRequestConfig,+ url: url.toString(),+ headers+ });++ return responseData;+ }++ const [targetHost] = await verifyHostInputValidity({ host: url.hostname, isGateway: true, isDynamicSecret: false });+ const isHttps = url.protocol === "https:";+ const targetPort = url.port ? Number(url.port) : (isHttps && 443) || 80;++ const platformConnectionDetails = await gatewayV2Service.getPlatformConnectionDetailsByGatewayId({+ gatewayId,+ targetHost,+ targetPort+ });++ if (!platformConnectionDetails) {+ throw new BadRequestError({ message: "Unable to connect to the Gateway assigned to this Portainer connection" });+ }++ return withGatewayV2Proxy(+ async (proxyPort) => {+ const proxiedUrl = new URL(url.toString());+ proxiedUrl.host = `localhost:${proxyPort}`;++ const { data: responseData } = await request.request<T>({+ ...baseRequestConfig,+ url: proxiedUrl.toString(),+ headers: { ...headers, Host: url.host },+ maxRedirects: 0,+ ...(isHttps && {+ httpsAgent: getSharedHttpsAgent({ servername: targetHost, rejectUnauthorized: true })+ })});- }- if (error instanceof BadRequestError) {- throw error;+ return responseData;+ },+ {+ protocol: GatewayProxyProtocol.Tcp,+ ...platformConnectionDetails}+ );+};+export const validatePortainerConnectionCredentials = async (+ config: TPortainerConnectionConfig,+ gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">+) => {+ try {+ await portainerRequest(config, { method: "GET", path: "/users/me" }, { gatewayV2Service });+ } catch (error) {+ if (error instanceof BadRequestError) throw error;++ logger.error(toLoggableError(error), "Failed to validate Portainer connection credentials");throw new BadRequestError({- message: `Failed to validate Portainer credentials - verify the instance URL and API token are correct`+ message: `Unable to validate Portainer connection: ${getPortainerErrorMessage(error, "verify the instance URL and API token")}`});}@@ -71,67 +196,57 @@ export const validatePortainerConnectionCredentials = async (config: TPortainerC};export const listPortainerEnvironments = async (- appConnection: TPortainerConnection+ appConnection: TPortainerConnection,+ deps: TPortainerGatewayDeps = {}): Promise<TPortainerEnvironment[]> => {- const instanceUrl = getPortainerInstanceUrl(appConnection);- const { apiToken } = appConnection.credentials;-try {// Returns the complete list when the optional start/limit query parameters are omitted- const { data } = await safeRequest.get<TPortainerEnvironmentResponse[]>(`${instanceUrl}/api/endpoints`, {- headers: getPortainerRequestHeaders(apiToken)- });+ const environments = await portainerRequest<TPortainerEnvironmentResponse[]>(+ appConnection,+ { method: "GET", path: "/endpoints" },+ deps+ );- return data.map((environment) => ({ id: environment.Id, name: environment.Name }));- } catch (error: unknown) {- if (error instanceof AxiosError) {- throw new BadRequestError({- message: `Failed to list Portainer environments: ${error.message || "Unknown error"}`- });- }-- if (error instanceof BadRequestError) {- throw error;- }+ return environments.map((environment) => ({ id: environment.Id, name: environment.Name }));+ } catch (error) {+ if (error instanceof BadRequestError) throw error;+ logger.error(toLoggableError(error), "Failed to list Portainer environments");throw new BadRequestError({- message: "Unable to list Portainer environments",- error+ message: `Failed to list Portainer environments: ${getPortainerErrorMessage(error, "verify the instance URL and API token")}`});}};-export const listPortainerStacks = async (appConnection: TPortainerConnection): Promise<TPortainerStack[]> => {- const instanceUrl = getPortainerInstanceUrl(appConnection);- const { apiToken } = appConnection.credentials;-+export const listPortainerStacks = async (+ appConnection: TPortainerConnection,+ deps: TPortainerGatewayDeps = {}+): Promise<TPortainerStack[]> => {try {// The stack list endpoint is not paginated- const { data } = await safeRequest.get<TPortainerStackResponse[]>(`${instanceUrl}/api/stacks`, {- headers: getPortainerRequestHeaders(apiToken)- });-- return data.map((stack) => ({- id: stack.Id,- name: stack.Name,- environmentId: stack.EndpointId,- // Git-backed stacks accept environment variable updates without a redeploy; file-based ones do not- isGitBased: Boolean(stack.GitConfig?.URL)- }));- } catch (error: unknown) {- if (error instanceof AxiosError) {- throw new BadRequestError({- message: `Failed to list Portainer stacks: ${error.message || "Unknown error"}`- });- }-- if (error instanceof BadRequestError) {- throw error;- }-+ const stacks = await portainerRequest<TPortainerStackResponse[]>(+ appConnection,+ { method: "GET", path: "/stacks" },+ deps+ );++ return (+ stacks+ // Portainer ignores `env` when updating a Kubernetes stack, so syncing to one would silently do nothing+ .filter((stack) => stack.Type !== PortainerStackType.Kubernetes)+ .map((stack) => ({+ id: stack.Id,+ name: stack.Name,+ environmentId: stack.EndpointId,+ isGitBased: isPortainerGitStack(stack)+ }))+ );+ } catch (error) {+ if (error instanceof BadRequestError) throw error;++ logger.error(toLoggableError(error), "Failed to list Portainer stacks");throw new BadRequestError({- message: "Unable to list Portainer stacks",- error+ message: `Failed to list Portainer stacks: ${getPortainerErrorMessage(error, "verify the instance URL and API token")}`});}};diff --git a/backend/src/services/app-connection/portainer/portainer-connection-schemas.ts b/backend/src/services/app-connection/portainer/portainer-connection-schemas.tsindex 96c4fa2297..3adc37110a 100644--- a/backend/src/services/app-connection/portainer/portainer-connection-schemas.ts+++ b/backend/src/services/app-connection/portainer/portainer-connection-schemas.ts@@ -57,7 +57,7 @@ export const ValidatePortainerConnectionCredentialsSchema = z.discriminatedUnion]);export const CreatePortainerConnectionSchema = ValidatePortainerConnectionCredentialsSchema.and(- GenericCreateAppConnectionFieldsSchema(AppConnection.Portainer)+ GenericCreateAppConnectionFieldsSchema(AppConnection.Portainer, { supportsGateways: true }));export const UpdatePortainerConnectionSchema = z@@ -66,7 +66,7 @@ export const UpdatePortainerConnectionSchema = zAppConnections.UPDATE(AppConnection.Portainer).credentials)})- .and(GenericUpdateAppConnectionFieldsSchema(AppConnection.Portainer));+ .and(GenericUpdateAppConnectionFieldsSchema(AppConnection.Portainer, { supportsGateways: true }));export const PortainerConnectionListItemSchema = z.object({diff --git a/backend/src/services/app-connection/portainer/portainer-connection-service.ts b/backend/src/services/app-connection/portainer/portainer-connection-service.tsindex 3d0f0f3fed..5cafed68d2 100644--- a/backend/src/services/app-connection/portainer/portainer-connection-service.ts+++ b/backend/src/services/app-connection/portainer/portainer-connection-service.ts@@ -1,4 +1,5 @@-import { logger } from "@app/lib/logger";+import { TGatewayPoolServiceFactory } from "@app/ee/services/gateway-pool/gateway-pool-service";+import { TGatewayV2ServiceFactory } from "@app/ee/services/gateway-v2/gateway-v2-service";import { OrgServiceActor } from "@app/lib/types";import { AppConnection } from "../app-connection-enums";@@ -11,29 +12,21 @@ type TGetAppConnectionFunc = (actor: OrgServiceActor) => Promise<TPortainerConnection>;-export const portainerConnectionService = (getAppConnection: TGetAppConnectionFunc) => {+export const portainerConnectionService = (+ getAppConnection: TGetAppConnectionFunc,+ gatewayV2Service?: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">,+ gatewayPoolService?: Pick<TGatewayPoolServiceFactory, "resolveEffectiveGatewayId">+) => {const listEnvironments = async (connectionId: string, actor: OrgServiceActor) => {const appConnection = await getAppConnection(AppConnection.Portainer, connectionId, actor);- try {- const environments = await listPortainerEnvironments(appConnection);- return environments;- } catch (error) {- logger.error(error, "Failed to establish connection with Portainer");- return [];- }+ return listPortainerEnvironments(appConnection, { gatewayV2Service, gatewayPoolService });};const listStacks = async (connectionId: string, actor: OrgServiceActor) => {const appConnection = await getAppConnection(AppConnection.Portainer, connectionId, actor);- try {- const stacks = await listPortainerStacks(appConnection);- return stacks;- } catch (error) {- logger.error(error, "Failed to establish connection with Portainer");- return [];- }+ return listPortainerStacks(appConnection, { gatewayV2Service, gatewayPoolService });};return {diff --git a/backend/src/services/app-connection/portainer/portainer-connection-types.ts b/backend/src/services/app-connection/portainer/portainer-connection-types.tsindex e873f094f0..933bcfac89 100644--- a/backend/src/services/app-connection/portainer/portainer-connection-types.ts+++ b/backend/src/services/app-connection/portainer/portainer-connection-types.ts@@ -1,8 +1,11 @@import z from "zod";+import { TGatewayPoolServiceFactory } from "@app/ee/services/gateway-pool/gateway-pool-service";+import { TGatewayV2ServiceFactory } from "@app/ee/services/gateway-v2/gateway-v2-service";import { DiscriminativePick } from "@app/lib/types";import { AppConnection } from "../app-connection-enums";+import { PortainerStackStatus, PortainerStackType } from "./portainer-connection-enums";import {CreatePortainerConnectionSchema,PortainerConnectionSchema,@@ -19,9 +22,16 @@ export type TValidatePortainerConnectionCredentialsSchema = typeof ValidatePortaexport type TPortainerConnectionConfig = DiscriminativePick<TPortainerConnectionInput,- "method" | "app" | "credentials"+ "method" | "app" | "credentials" | "gatewayId" | "gatewayPoolId">;+export type TPortainerRequestConfig = Pick<TPortainerConnectionConfig, "credentials" | "gatewayId" | "gatewayPoolId">;++export type TPortainerGatewayDeps = {+ gatewayV2Service?: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">;+ gatewayPoolService?: Pick<TGatewayPoolServiceFactory, "resolveEffectiveGatewayId">;+};+export type TPortainerEnvironment = {id: number;name: string;@@ -33,3 +43,45 @@ export type TPortainerStack = {environmentId: number;isGitBased: boolean;};++export type TPortainerEnvVar = {+ name: string;+ value: string;+};++export type TPortainerStackResponse = {+ Id: number;+ Name: string;+ Type: PortainerStackType;+ EndpointId: number;+ Status?: PortainerStackStatus;+ Env?: TPortainerEnvVar[] | null;+ Option?: {+ Prune?: boolean;+ } | null;+ // Set on Portainer 2.45+, where the Git settings live on a Source instead of the stack+ WorkflowID?: number;+ GitSourceId?: number;+ GitConfig?: {+ URL: string;+ ReferenceName?: string;+ ConfigFilePath?: string;+ TLSSkipVerify?: boolean;+ Authentication?: {+ Username?: string;+ Password?: string;+ } | null;+ } | null;+ AutoUpdate?: {+ Interval?: string;+ Webhook?: string;+ JobID?: string;+ ForceUpdate?: boolean;+ ForcePullImage?: boolean;+ } | null;+ DeploymentStatus?: {+ Status: PortainerStackStatus;+ Time: number;+ Message?: string;+ }[];+};diff --git a/frontend/src/pages/organization/AppConnections/AppConnectionsPage/components/AppConnectionForm/PortainerConnectionForm.tsx b/frontend/src/pages/organization/AppConnections/AppConnectionsPage/components/AppConnectionForm/PortainerConnectionForm.tsxindex 7f5c0f70b4..fa8e7128c9 100644--- a/frontend/src/pages/organization/AppConnections/AppConnectionsPage/components/AppConnectionForm/PortainerConnectionForm.tsx+++ b/frontend/src/pages/organization/AppConnections/AppConnectionsPage/components/AppConnectionForm/PortainerConnectionForm.tsx@@ -3,8 +3,10 @@ import { zodResolver } from "@hookform/resolvers/zod";import { Info } from "lucide-react";import { z } from "zod";+import { OrgPermissionCan } from "@app/components/permissions";import {Field,+ FieldDescription,FieldError,FieldLabel,Input,@@ -18,6 +20,9 @@ import {TooltipContent,TooltipTrigger} from "@app/components/v3";+import { GatewayPicker } from "@app/components/v3/platform/GatewayPicker";+import { OrgPermissionSubjects } from "@app/context";+import { OrgGatewayPermissionActions } from "@app/context/OrgPermissionContext/types";import { APP_CONNECTION_MAP, getAppConnectionMethodDetails } from "@app/helpers/appConnections";import { PortainerConnectionMethod, TPortainerConnection } from "@app/hooks/api/appConnections";import { AppConnection } from "@app/hooks/api/appConnections/enums";@@ -61,16 +66,61 @@ export const PortainerConnectionForm = ({ appConnection, onSubmit }: Props) => {resolver: zodResolver(formSchema),defaultValues: appConnection ?? {app: AppConnection.Portainer,- method: PortainerConnectionMethod.ApiToken+ method: PortainerConnectionMethod.ApiToken,+ gatewayId: null,+ gatewayPoolId: null}});- const { handleSubmit, control } = form;+ const { handleSubmit, control, setValue, watch } = form;++ const gatewayId = watch("gatewayId");+ const gatewayPoolId = watch("gatewayPoolId");return (<FormProvider {...form}><form onSubmit={handleSubmit(onSubmit)}>{!isUpdate && <GenericAppConnectionsFields />}+ <OrgPermissionCan+ I={OrgGatewayPermissionActions.AttachGateways}+ a={OrgPermissionSubjects.Gateway}+ >+ {(isAllowed) => {+ const picker = (+ <GatewayPicker+ isDisabled={!isAllowed}+ value={{ gatewayId: gatewayId ?? null, gatewayPoolId: gatewayPoolId ?? null }}+ onChange={({ gatewayId: newGwId, gatewayPoolId: newPoolId }) => {+ setValue("gatewayId", newGwId, { shouldDirty: true });+ setValue("gatewayPoolId", newPoolId, { shouldDirty: true });+ }}+ />+ );++ return (+ <Field className="mb-4">+ <FieldLabel>Gateway</FieldLabel>+ <FieldDescription>+ Route requests through a Gateway when your Portainer instance is not reachable+ from the internet.+ </FieldDescription>+ {isAllowed ? (+ picker+ ) : (+ <Tooltip>+ <TooltipTrigger asChild>+ <span className="block w-full cursor-not-allowed">{picker}</span>+ </TooltipTrigger>+ <TooltipContent>+ Restricted access. You don't have permission to attach gateways to+ resources.+ </TooltipContent>+ </Tooltip>+ )}+ </Field>+ );+ }}+ </OrgPermissionCan><Controllername="method"control={control}