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 { Knex } from "knex";
import { TDbClient } from "@app/db";
import { SecretType, TableName } from "@app/db/schemas";
import { DatabaseError } from "@app/lib/errors";
import { ormify, sqlNestRelationships } from "@app/lib/knex";
import { SecretMetadataSearchLogicalOperator, TSearchSecretMetadataDALDTO } from "./resource-metadata-types";
export type TResourceMetadataDALFactory = ReturnType<typeof resourceMetadataDALFactory>;
// defensive cap on the number of matched secrets scanned before permission filtering.
// full pagination is intentionally out of scope for now.
export const MAX_SECRET_METADATA_SEARCH_SECRETS = 100;
export const resourceMetadataDALFactory = (db: TDbClient) => {
const orm = ormify(db, TableName.ResourceMetadata);
// builds the "row matches at least one condition" predicate: OR of (key = ? AND value = ?)
const buildMatchesAnyCondition = (filters: TSearchSecretMetadataDALDTO["filters"]) => (qb: Knex.QueryBuilder) => {
return filters.forEach(({ key, value }) => {
return void qb.orWhere((subFilter) => {
return void subFilter
.where(`${TableName.ResourceMetadata}.key`, key)
.where(`${TableName.ResourceMetadata}.value`, value);
});
});
};
const buildScopedSecretMetadataQuery = (
knex: Knex,
orgId: string,
projectId: string,
tagSlugs?: string[],
folderIds?: string[],
environments?: string[]
) => {
const query = knex(TableName.ResourceMetadata)
.join(TableName.SecretV2, `${TableName.SecretV2}.id`, `${TableName.ResourceMetadata}.secretId`)
.join(TableName.SecretFolder, `${TableName.SecretFolder}.id`, `${TableName.SecretV2}.folderId`)
.join(TableName.Environment, `${TableName.Environment}.id`, `${TableName.SecretFolder}.envId`)
.join(TableName.Project, `${TableName.Project}.id`, `${TableName.Environment}.projectId`)
.where(`${TableName.ResourceMetadata}.orgId`, orgId)
.where(`${TableName.Environment}.projectId`, projectId)
.whereNotNull(`${TableName.ResourceMetadata}.secretId`)
.whereNull(`${TableName.Environment}.deleteAfter`)
.whereNull(`${TableName.Project}.deleteAfter`)
.where(`${TableName.SecretV2}.type`, SecretType.Shared);
if (folderIds) {
void query.whereIn(`${TableName.SecretV2}.folderId`, folderIds);
} else {
void query
.withRecursive("metadata_search_folders", ["id", "envId"], (folders) => {
void folders
.select("root.id", "root.envId")
.from({ root: TableName.SecretFolder })
.join({ scope_env: TableName.Environment }, "scope_env.id", "root.envId")
.whereNull("root.parentId")
.where("scope_env.projectId", projectId)
.whereNull("scope_env.deleteAfter")
.modify((roots) => {
if (environments) void roots.whereIn("scope_env.slug", environments);
})
.union((children) => {
void children
.select("child.id", "child.envId")
.from({ child: TableName.SecretFolder })
.join("metadata_search_folders", (join) => {
join
.on("child.parentId", "metadata_search_folders.id")
.andOn("child.envId", "metadata_search_folders.envId");
})
.where("child.isReserved", false);
});
})
.whereIn(`${TableName.SecretV2}.folderId`, (subQuery) => {
void subQuery.select("id").from("metadata_search_folders");
});
}
if (environments) {
void query.whereIn(`${TableName.Environment}.slug`, environments);
}
// optional tag filter: keep only secrets carrying at least one of the requested tag slugs. A
// correlated existence check keeps the candidate rows 1:1 with the secret so the later
// distinct/limit stays correct (a leftJoin would multiply rows per tag).
if (tagSlugs && tagSlugs.length) {
void query.whereExists((subQuery) => {
return void subQuery
.select(db.raw("1"))
.from(TableName.SecretV2JnTag)
.join(TableName.SecretTag, `${TableName.SecretV2JnTag}.${TableName.SecretTag}Id`, `${TableName.SecretTag}.id`)
.whereRaw(`??.?? = ??.??`, [TableName.SecretV2JnTag, `${TableName.SecretV2}Id`, TableName.SecretV2, "id"])
.whereIn(`${TableName.SecretTag}.slug`, tagSlugs);
});
}
return query;
};
// Shared step-2 hydration: for the resolved candidate secretIds, fetch the metadata rows matching
// `applyMetadataMatch` plus the secret key and tags, and nest into
// { secretId, secretKey, folderId, metadata: [{ key, value, encryptedValue }], tags: [{ id, slug }] }.
// encryptedValue is always selected (null for plaintext rows); the service decrypts it when present.
const hydrateMatchedSecretMetadata = async (
knex: Knex,
secretIds: string[],
applyMetadataMatch: (qb: Knex.QueryBuilder) => void
) => {
const rows = await knex(TableName.ResourceMetadata)
.join(TableName.SecretV2, `${TableName.SecretV2}.id`, `${TableName.ResourceMetadata}.secretId`)
.leftJoin(
TableName.SecretV2JnTag,
`${TableName.SecretV2}.id`,
`${TableName.SecretV2JnTag}.${TableName.SecretV2}Id`
)
.leftJoin(TableName.SecretTag, `${TableName.SecretV2JnTag}.${TableName.SecretTag}Id`, `${TableName.SecretTag}.id`)
.whereIn(`${TableName.ResourceMetadata}.secretId`, secretIds)
.where(applyMetadataMatch)
.select(
db.ref("secretId").withSchema(TableName.ResourceMetadata).as("secretId"),
db.ref("id").withSchema(TableName.ResourceMetadata).as("metadataId"),
db.ref("key").withSchema(TableName.ResourceMetadata).as("metadataKey"),
db.ref("value").withSchema(TableName.ResourceMetadata).as("metadataValue"),
db.ref("encryptedValue").withSchema(TableName.ResourceMetadata).as("metadataEncryptedValue"),
db.ref("key").withSchema(TableName.SecretV2).as("secretKey"),
db.ref("folderId").withSchema(TableName.SecretV2).as("folderId"),
db.ref("id").withSchema(TableName.SecretTag).as("tagId"),
db.ref("slug").withSchema(TableName.SecretTag).as("tagSlug")
);
return sqlNestRelationships({
data: rows,
key: "secretId",
parentMapper: (row) => ({
secretId: row.secretId as string,
secretKey: row.secretKey,
folderId: row.folderId
}),
childrenMapper: [
{
key: "metadataId",
label: "metadata" as const,
mapper: ({ metadataKey, metadataValue, metadataEncryptedValue }) => ({
key: metadataKey,
value: metadataValue as string | null,
encryptedValue: metadataEncryptedValue as Buffer | null
})
},
{
key: "tagId",
label: "tags" as const,
mapper: ({ tagId, tagSlug }) => ({
id: tagId,
slug: tagSlug
})
}
]
});
};
// Project-scoped search of secret metadata. A resource_metadata row holds a single key/value and a
// secret has many rows, so the and/or combinator is evaluated per secret:
// - or -> a secret matches if any condition matches one of its rows
// - and -> a secret matches only if every condition matches some row of that secret
// Only plaintext `value` is matched here; encrypted values are handled by
// searchSecretMetadataWithEncryptedValues.
const searchSecretMetadata = async (
{
orgId,
projectId,
filters,
operator,
tagSlugs,
folderIds,
environments,
limit = MAX_SECRET_METADATA_SEARCH_SECRETS
}: TSearchSecretMetadataDALDTO,
tx?: Knex
) => {
try {
const knex = tx || db.replicaNode();
// step 1: resolve the bounded set of matching secret ids (scoping + tag filter enforced by the shared builder).
const matchedSecretIdsQuery = buildScopedSecretMetadataQuery(
knex,
orgId,
projectId,
tagSlugs,
folderIds,
environments
);
if (operator === SecretMetadataSearchLogicalOperator.And) {
// and: every condition must match a row of the secret. Drive the scan from the first condition
// (so the (orgId, key) index narrows candidates) and require the rest via correlated existence.
filters.forEach(({ key, value }, index) => {
if (index === 0) {
void matchedSecretIdsQuery
.where(`${TableName.ResourceMetadata}.key`, key)
.where(`${TableName.ResourceMetadata}.value`, value);
return;
}
void matchedSecretIdsQuery.whereExists((subQuery) => {
return void subQuery
.select(db.raw("1"))
.from({ rm2: TableName.ResourceMetadata })
.whereRaw(`??.?? = ??.??`, ["rm2", "secretId", TableName.ResourceMetadata, "secretId"])
.where("rm2.key", key)
.where("rm2.value", value);
});
});
} else {
// or: the joined row must match at least one condition
void matchedSecretIdsQuery.where(buildMatchesAnyCondition(filters));
}
const matchedSecretRows = await matchedSecretIdsQuery
.distinct(`${TableName.ResourceMetadata}.secretId`)
.orderBy(`${TableName.ResourceMetadata}.secretId`)
.limit(limit);
const secretIds = matchedSecretRows.map((row) => row.secretId as string);
if (!secretIds.length) return [];
// step 2: hydrate the matched metadata rows + tags for those secrets, then nest.
const docs = await hydrateMatchedSecretMetadata(knex, secretIds, buildMatchesAnyCondition(filters));
return docs;
} catch (error) {
throw new DatabaseError({ error, name: "SearchSecretMetadata" });
}
};
// Companion to searchSecretMetadata for encrypted metadata. Encrypted values are stored as
// non-deterministic KMS ciphertext, so they cannot be equality-matched in SQL. This query therefore
// bounds candidates by KEY only (the requested filter keys) among rows that carry an encryptedValue,
// then returns every requested-key row (plaintext `value` + `encryptedValue`) for those secrets so the
// service can decrypt and match the value in-app. Plaintext rows are included so an `and` query
// satisfied partly by plaintext and partly by encrypted rows still matches after decryption. Kept as a
// separate query so the common plaintext path stays untouched, and it returns early (no hydration) when
// a project has no encrypted metadata for the requested keys.
const searchSecretMetadataWithEncryptedValues = async (
{
orgId,
projectId,
filters,
tagSlugs,
folderIds,
environments,
limit = MAX_SECRET_METADATA_SEARCH_SECRETS
}: TSearchSecretMetadataDALDTO,
tx?: Knex
) => {
try {
const knex = tx || db.replicaNode();
const keys = [...new Set(filters.map((filter) => filter.key))];
// step 1: bounded set of secrets holding at least one encrypted metadata row for a requested key
// — narrowed by the (orgId, key) partial index, and by the optional tag filter.
const matchedSecretRows = await buildScopedSecretMetadataQuery(
knex,
orgId,
projectId,
tagSlugs,
folderIds,
environments
)
.whereNotNull(`${TableName.ResourceMetadata}.encryptedValue`)
.whereIn(`${TableName.ResourceMetadata}.key`, keys)
.distinct(`${TableName.ResourceMetadata}.secretId`)
.orderBy(`${TableName.ResourceMetadata}.secretId`)
.limit(limit);
const secretIds = matchedSecretRows.map((row) => row.secretId as string);
if (!secretIds.length) return [];
// step 2: hydrate every requested-key row (plaintext value + encryptedValue) + tags for those secrets.
const docs = await hydrateMatchedSecretMetadata(knex, secretIds, (qb) => {
void qb.whereIn(`${TableName.ResourceMetadata}.key`, keys);
});
return docs;
} catch (error) {
throw new DatabaseError({ error, name: "SearchSecretMetadataWithEncryptedValues" });
}
};
return { ...orm, searchSecretMetadata, searchSecretMetadataWithEncryptedValues };
};