Merge pull request #622 from Infisical/folder-patch-v2

Patch backfill data
This commit is contained in:
Maidul Islam
2023-06-05 23:14:14 -07:00
committed by GitHub
3 changed files with 49 additions and 7 deletions

View File

@@ -19,7 +19,13 @@ export const getSecretSnapshot = async (req: Request, res: Response) => {
secretSnapshot = await SecretSnapshot.findById(secretSnapshotId) secretSnapshot = await SecretSnapshot.findById(secretSnapshotId)
.lean() .lean()
.populate<{ secretVersions: ISecretVersion[] }>("secretVersions") .populate<{ secretVersions: ISecretVersion[] }>({
path: 'secretVersions',
populate: {
path: 'tags',
model: 'Tag'
}
})
.populate<{ folderVersion: TFolderRootVersionSchema }>("folderVersion"); .populate<{ folderVersion: TFolderRootVersionSchema }>("folderVersion");
if (!secretSnapshot) throw new Error("Failed to find secret snapshot"); if (!secretSnapshot) throw new Error("Failed to find secret snapshot");

View File

@@ -27,6 +27,7 @@ export interface ISecretVersion {
keyEncoding: "utf8" | "base64"; keyEncoding: "utf8" | "base64";
createdAt: string; createdAt: string;
folder?: string; folder?: string;
tags?: string[];
} }
const secretVersionSchema = new Schema<ISecretVersion>( const secretVersionSchema = new Schema<ISecretVersion>(
@@ -112,6 +113,11 @@ const secretVersionSchema = new Schema<ISecretVersion>(
type: String, type: String,
required: true, required: true,
}, },
tags: {
ref: 'Tag',
type: [Schema.Types.ObjectId],
default: []
},
}, },
{ {
timestamps: true, timestamps: true,

View File

@@ -335,6 +335,33 @@ export const backfillSecretFolders = async () => {
} }
); );
await SecretVersion.updateMany(
{
folder: {
$exists: false,
},
},
{
$set: {
folder: "root",
},
}
);
// Back fill because tags were missing in secret versions
await SecretVersion.updateMany(
{
tags: {
$exists: false,
},
},
{
$set: {
tags: [],
},
}
);
let secretSnapshots = await SecretSnapshot.find({ let secretSnapshots = await SecretSnapshot.find({
environment: { environment: {
$exists: false, $exists: false,
@@ -352,12 +379,15 @@ export const backfillSecretFolders = async () => {
groupSnapByEnv[secVer.environment].push(secVer); groupSnapByEnv[secVer.environment].push(secVer);
}); });
const newSnapshots = Object.keys(groupSnapByEnv).map((snapEnv) => ({ const newSnapshots = Object.keys(groupSnapByEnv).map((snapEnv) => {
...secSnapshot.toObject({ virtuals: false }), const secretIdsOfEnvGroup = groupSnapByEnv[snapEnv] ? groupSnapByEnv[snapEnv].map(secretVersion => secretVersion._id) : []
_id: new Types.ObjectId(), return {
environment: snapEnv, ...secSnapshot.toObject({ virtuals: false }),
secretVersions: groupSnapByEnv[snapEnv], _id: new Types.ObjectId(),
})); environment: snapEnv,
secretVersions: secretIdsOfEnvGroup,
}
});
await SecretSnapshot.insertMany(newSnapshots); await SecretSnapshot.insertMany(newSnapshots);
await secSnapshot.delete(); await secSnapshot.delete();