-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #171 from Infisical/secret-versioning
- Loading branch information
Showing
24 changed files
with
638 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
import * as stripeController from './stripeController'; | ||
import * as secretController from './secretController'; | ||
import * as workspaceController from './workspaceController'; | ||
|
||
export { | ||
stripeController | ||
stripeController, | ||
secretController, | ||
workspaceController | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Request, Response } from 'express'; | ||
import * as Sentry from '@sentry/node'; | ||
import { SecretVersion } from '../models'; | ||
|
||
/** | ||
* Return secret versions for secret with id [secretId] | ||
* @param req | ||
* @param res | ||
*/ | ||
export const getSecretVersions = async (req: Request, res: Response) => { | ||
let secretVersions; | ||
try { | ||
const { secretId } = req.params; | ||
|
||
const offset: number = parseInt(req.query.offset as string); | ||
const limit: number = parseInt(req.query.limit as string); | ||
|
||
secretVersions = await SecretVersion.find({ | ||
secret: secretId | ||
}) | ||
.skip(offset) | ||
.limit(limit); | ||
|
||
} catch (err) { | ||
Sentry.setUser({ email: req.user.email }); | ||
Sentry.captureException(err); | ||
return res.status(400).send({ | ||
message: 'Failed to get secret versions' | ||
}); | ||
} | ||
|
||
return res.status(200).send({ | ||
secretVersions | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Request, Response } from 'express'; | ||
import * as Sentry from '@sentry/node'; | ||
import { SecretSnapshot } from '../models'; | ||
|
||
/** | ||
* Return secret snapshots for workspace with id [workspaceId] | ||
* @param req | ||
* @param res | ||
*/ | ||
export const getWorkspaceSecretSnapshots = async (req: Request, res: Response) => { | ||
let secretSnapshots; | ||
try { | ||
const { workspaceId } = req.params; | ||
|
||
const offset: number = parseInt(req.query.offset as string); | ||
const limit: number = parseInt(req.query.limit as string); | ||
|
||
secretSnapshots = await SecretSnapshot.find({ | ||
workspace: workspaceId | ||
}) | ||
.skip(offset) | ||
.limit(limit); | ||
|
||
} catch (err) { | ||
Sentry.setUser({ email: req.user.email }); | ||
Sentry.captureException(err); | ||
return res.status(400).send({ | ||
message: 'Failed to get secret snapshots' | ||
}); | ||
} | ||
|
||
return res.status(200).send({ | ||
secretSnapshots | ||
}); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import * as Sentry from '@sentry/node'; | ||
import { | ||
Secret | ||
} from '../../models'; | ||
import { | ||
SecretSnapshot, | ||
SecretVersion, | ||
ISecretVersion | ||
} from '../models'; | ||
|
||
/** | ||
* Save a copy of the current state of secrets in workspace with id | ||
* [workspaceId] under a new snapshot with incremented version under the | ||
* secretsnapshots collection. | ||
* @param {Object} obj | ||
* @param {String} obj.workspaceId | ||
*/ | ||
const takeSecretSnapshotHelper = async ({ | ||
workspaceId | ||
}: { | ||
workspaceId: string; | ||
}) => { | ||
try { | ||
const secrets = await Secret.find({ | ||
workspace: workspaceId | ||
}); | ||
|
||
const latestSecretSnapshot = await SecretSnapshot.findOne({ | ||
workspace: workspaceId | ||
}).sort({ version: -1 }); | ||
|
||
if (!latestSecretSnapshot) { | ||
// case: no snapshots exist for workspace -> create first snapshot | ||
await new SecretSnapshot({ | ||
workspace: workspaceId, | ||
version: 1, | ||
secrets | ||
}).save(); | ||
|
||
return; | ||
} | ||
|
||
// case: snapshots exist for workspace | ||
await new SecretSnapshot({ | ||
workspace: workspaceId, | ||
version: latestSecretSnapshot.version + 1, | ||
secrets | ||
}).save(); | ||
|
||
} catch (err) { | ||
Sentry.setUser(null); | ||
Sentry.captureException(err); | ||
throw new Error('Failed to take a secret snapshot'); | ||
} | ||
} | ||
|
||
const addSecretVersionsHelper = async ({ | ||
secretVersions | ||
}: { | ||
secretVersions: ISecretVersion[] | ||
}) => { | ||
try { | ||
await SecretVersion.insertMany(secretVersions); | ||
} catch (err) { | ||
Sentry.setUser(null); | ||
Sentry.captureException(err); | ||
throw new Error('Failed to add secret versions'); | ||
} | ||
} | ||
|
||
export { | ||
takeSecretSnapshotHelper, | ||
addSecretVersionsHelper | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import SecretSnapshot, { ISecretSnapshot } from "./secretSnapshot"; | ||
import SecretVersion, { ISecretVersion } from "./secretVersion"; | ||
|
||
export { | ||
SecretSnapshot, | ||
ISecretSnapshot, | ||
SecretVersion, | ||
ISecretVersion | ||
} |
Oops, something went wrong.