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
import type {
CopySecretsEnvironment,
CopySecretsFolder,
CopySecretsInvocation,
CopySecretsSource
} from "./copySecrets.types";
export const getOtherCopyEnvironmentSlug = (
environments: CopySecretsEnvironment[],
excludedSlug: string
) => environments.find(({ slug }) => slug !== excludedSlug)?.slug ?? "";
export const normalizeCopyPath = (path: string) => {
const segments = path.trim().split("/").filter(Boolean);
return segments.length ? `/${segments.join("/")}` : "/";
};
export const joinCopyPath = (...paths: string[]) =>
normalizeCopyPath(
paths
.flatMap((path) => path.split("/"))
.filter(Boolean)
.join("/")
);
export const getRelativeCopyPath = (path: string, rootPath: string) => {
const normalizedPath = normalizeCopyPath(path);
const normalizedRoot = normalizeCopyPath(rootPath);
if (normalizedPath === normalizedRoot) return "/";
if (normalizedRoot === "/") return normalizedPath;
if (!normalizedPath.startsWith(`${normalizedRoot}/`)) return null;
return normalizeCopyPath(normalizedPath.slice(normalizedRoot.length));
};
export const filterCopyPreviewSecrets = ({
secrets,
rootPath,
changesOnly = false
}: {
secrets: CopySecretsSource[];
rootPath: string;
changesOnly?: boolean;
}) =>
secrets.filter(
(secret) =>
getRelativeCopyPath(secret.path, rootPath) !== null &&
(!changesOnly || Boolean(secret.previewStatus))
);
export const isCopySecretSelectable = (secret: CopySecretsSource) =>
!secret.isRotated && !secret.isHoneyToken;
export const getCopyDestinationPath = ({
sourcePath,
sourceRootPath,
destinationRootPath
}: {
sourcePath: string;
sourceRootPath: string;
destinationRootPath: string;
}) => {
const relativePath = getRelativeCopyPath(sourcePath, sourceRootPath);
if (relativePath === null) return null;
return joinCopyPath(destinationRootPath, relativePath);
};
export const isCopyingToSameLocation = ({
sourceEnvironment,
destinationEnvironment,
sourcePath,
destinationPath
}: {
sourceEnvironment: string;
destinationEnvironment: string;
sourcePath: string;
destinationPath: string;
}) =>
sourceEnvironment === destinationEnvironment &&
getCopyDestinationPath({
sourcePath,
sourceRootPath: sourcePath,
destinationRootPath: destinationPath
}) === normalizeCopyPath(sourcePath);
export type CopyFolderCreationStep = {
parentPath: string;
name: string;
};
export const getCopyFolderCreationSteps = (path: string) => {
let parentPath = "/";
return normalizeCopyPath(path)
.split("/")
.filter(Boolean)
.map<CopyFolderCreationStep>((name) => {
const step = { parentPath, name };
parentPath = joinCopyPath(parentPath, name);
return step;
});
};
export const getMissingCopyFolderCreationSteps = ({
paths,
existingFolderPaths
}: {
paths: string[];
existingFolderPaths: string[];
}) => {
const knownFolderPaths = new Set(existingFolderPaths.map(normalizeCopyPath));
const missingSteps = new Map<string, CopyFolderCreationStep>();
paths.flatMap(getCopyFolderCreationSteps).forEach((step) => {
const path = joinCopyPath(step.parentPath, step.name);
if (!knownFolderPaths.has(path)) {
missingSteps.set(path, step);
knownFolderPaths.add(path);
}
});
return [...missingSteps.values()];
};
export type CopySecretsRequestGroup = {
sourcePath: string;
destinationPath: string;
secretIds: string[];
includeValues: boolean;
};
export type CopySecretConflict = {
sourceSecretId: string;
name: string;
destinationPath: string;
};
export const getCopySecretConflicts = ({
secrets,
destinationSecrets,
requestGroups
}: {
secrets: CopySecretsSource[];
destinationSecrets: CopySecretsSource[];
requestGroups: CopySecretsRequestGroup[];
}) => {
const destinationPathBySourcePath = new Map(
requestGroups.map(({ sourcePath, destinationPath }) => [
normalizeCopyPath(sourcePath),
normalizeCopyPath(destinationPath)
])
);
const destinationLocations = new Set(
destinationSecrets.map(({ path, name }) => `${normalizeCopyPath(path)}\u0000${name}`)
);
return secrets.flatMap<CopySecretConflict>((secret) => {
const destinationPath = destinationPathBySourcePath.get(normalizeCopyPath(secret.path));
if (!destinationPath || !destinationLocations.has(`${destinationPath}\u0000${secret.name}`)) {
return [];
}
return [{ sourceSecretId: secret.id, name: secret.name, destinationPath }];
});
};
export const groupCopySecretsRequests = ({
secrets,
sourceRootPath,
destinationRootPath,
includeValues = true
}: {
secrets: CopySecretsSource[];
sourceRootPath: string;
destinationRootPath: string;
includeValues?: boolean;
}) => {
const groups = new Map<string, CopySecretsRequestGroup>();
secrets.forEach((secret) => {
if (!isCopySecretSelectable(secret)) return;
const sourcePath = normalizeCopyPath(secret.path);
const destinationPath = getCopyDestinationPath({
sourcePath,
sourceRootPath,
destinationRootPath
});
if (!destinationPath) return;
const copyValue = includeValues && !secret.isValueHidden;
const key = `${sourcePath}\u0000${destinationPath}\u0000${copyValue}`;
const group = groups.get(key) ?? {
sourcePath,
destinationPath,
secretIds: [],
includeValues: copyValue
};
group.secretIds.push(secret.id);
groups.set(key, group);
});
return [...groups.values()];
};
export const chunkCopySecretIds = (secretIds: string[], size = 50) =>
Array.from({ length: Math.ceil(secretIds.length / size) }, (_, index) =>
secretIds.slice(index * size, (index + 1) * size)
);
export const getInitialCopyState = (
invocation: CopySecretsInvocation,
environments: CopySecretsEnvironment[]
) => {
const availableSlugs =
invocation.origin === "bulk"
? [
...new Set([
...Object.keys(invocation.secretsByEnvironment),
...Object.keys(invocation.foldersByEnvironment)
])
]
: [];
const sourceEnvironmentSlug =
invocation.sourceEnvironmentSlug ?? (availableSlugs.length === 1 ? availableSlugs[0] : "");
const sourcePath = normalizeCopyPath(invocation.sourcePath);
return {
sourceEnvironmentSlug,
sourcePath,
destinationEnvironmentSlug: sourceEnvironmentSlug
? getOtherCopyEnvironmentSlug(environments, sourceEnvironmentSlug) || sourceEnvironmentSlug
: "",
destinationPath: sourcePath
};
};
export const getInvocationCopySelection = ({
invocation,
sourcePath,
secrets,
folders
}: {
invocation: CopySecretsInvocation;
sourcePath: string;
secrets: CopySecretsSource[];
folders: CopySecretsFolder[];
}) => {
let invocationSecrets: CopySecretsSource[] = [];
if (invocation.origin === "row") invocationSecrets = invocation.secrets;
if (invocation.origin === "bulk")
invocationSecrets = Object.values(invocation.secretsByEnvironment).flat();
const names = new Set(invocationSecrets.map(({ name }) => name));
const folderRoots =
invocation.origin === "bulk"
? invocation.folderNames.map((name) => joinCopyPath(sourcePath, name))
: [];
const isInSelectedFolder = (path: string) =>
folderRoots.some((root) => getRelativeCopyPath(path, root) !== null);
return {
secretIds: secrets
.filter(
(secret) =>
isCopySecretSelectable(secret) &&
((normalizeCopyPath(secret.path) === normalizeCopyPath(sourcePath) &&
names.has(secret.name)) ||
isInSelectedFolder(secret.path))
)
.map(({ id }) => id),
folderPaths: folders.filter(({ path }) => isInSelectedFolder(path)).map(({ path }) => path)
};
};
export const getCopyDestinationFolderPaths = ({
folderPaths,
sourceRootPath,
destinationRootPath
}: {
folderPaths: string[];
sourceRootPath: string;
destinationRootPath: string;
}) => [
...new Set(
folderPaths.flatMap((sourcePath) => {
const path = getCopyDestinationPath({
sourcePath,
sourceRootPath,
destinationRootPath
});
return path ? [path] : [];
})
)
];