-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Feature: mongodb secret rotation #4889
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
victorvhs017
wants to merge
11
commits into
main
Choose a base branch
from
feature/mongodb-secret-rotation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8121db6
Add MongoDB connection support
055a663
Add MongoDB connection form and related updates
ed475f0
Add MongoDB integration and documentation updates
51fc4a5
Add MongoDB credentials rotation support
8b31eff
Refactor MongoDB host validation using RE2 regex
b20ccf4
Add MongoDB credentials rotation documentation and images
406f0a1
Merge branch 'main' into feature/mongodb-secret-rotation
58a5688
refactor: remove unused import from index.ts
c01444d
Merge branch 'main' into feature/mongodb-secret-rotation
06fb205
Merge branch 'main' into feature/mongodb-secret-rotation
4ed75e3
fix: correct host normalization logic in MongoDB connection functions
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
19 changes: 19 additions & 0 deletions
19
backend/src/ee/routes/v2/secret-rotation-v2-routers/mongodb-credentials-rotation-router.ts
This file contains hidden or 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,19 @@ | ||
| import { | ||
| CreateMongoDBCredentialsRotationSchema, | ||
| MongoDBCredentialsRotationGeneratedCredentialsSchema, | ||
| MongoDBCredentialsRotationSchema, | ||
| UpdateMongoDBCredentialsRotationSchema | ||
| } from "@app/ee/services/secret-rotation-v2/mongodb-credentials"; | ||
| import { SecretRotation } from "@app/ee/services/secret-rotation-v2/secret-rotation-v2-enums"; | ||
|
|
||
| import { registerSecretRotationEndpoints } from "./secret-rotation-v2-endpoints"; | ||
|
|
||
| export const registerMongoDBCredentialsRotationRouter = async (server: FastifyZodProvider) => | ||
| registerSecretRotationEndpoints({ | ||
| type: SecretRotation.MongoDBCredentials, | ||
| server, | ||
| responseSchema: MongoDBCredentialsRotationSchema, | ||
| createSchema: CreateMongoDBCredentialsRotationSchema, | ||
| updateSchema: UpdateMongoDBCredentialsRotationSchema, | ||
| generatedCredentialsSchema: MongoDBCredentialsRotationGeneratedCredentialsSchema | ||
| }); |
This file contains hidden or 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
4 changes: 4 additions & 0 deletions
4
backend/src/ee/services/secret-rotation-v2/mongodb-credentials/index.ts
This file contains hidden or 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,4 @@ | ||
| export * from "./mongodb-credentials-rotation-constants"; | ||
| export * from "./mongodb-credentials-rotation-fns"; | ||
| export * from "./mongodb-credentials-rotation-schemas"; | ||
| export * from "./mongodb-credentials-rotation-types"; |
28 changes: 28 additions & 0 deletions
28
...services/secret-rotation-v2/mongodb-credentials/mongodb-credentials-rotation-constants.ts
This file contains hidden or 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,28 @@ | ||
| import { SecretRotation } from "@app/ee/services/secret-rotation-v2/secret-rotation-v2-enums"; | ||
| import { TSecretRotationV2ListItem } from "@app/ee/services/secret-rotation-v2/secret-rotation-v2-types"; | ||
| import { AppConnection } from "@app/services/app-connection/app-connection-enums"; | ||
|
|
||
| export const MONGODB_CREDENTIALS_ROTATION_LIST_OPTION: TSecretRotationV2ListItem = { | ||
| name: "MongoDB Credentials", | ||
| type: SecretRotation.MongoDBCredentials, | ||
| connection: AppConnection.MongoDB, | ||
| template: { | ||
| createUserStatement: `use [DATABASE_NAME] | ||
| db.createUser({ | ||
| user: "infisical_user_1", | ||
| pwd: "temporary_password", | ||
| roles: [{ role: "readWrite", db: "[DATABASE_NAME]" }] | ||
| }) | ||
|
|
||
| db.createUser({ | ||
| user: "infisical_user_2", | ||
| pwd: "temporary_password", | ||
| roles: [{ role: "readWrite", db: "[DATABASE_NAME]" }] | ||
| })`, | ||
| secretsMapping: { | ||
| username: "MONGODB_DB_USERNAME", | ||
| password: "MONGODB_DB_PASSWORD" | ||
| } | ||
| } | ||
| }; | ||
|
|
||
248 changes: 248 additions & 0 deletions
248
...rc/ee/services/secret-rotation-v2/mongodb-credentials/mongodb-credentials-rotation-fns.ts
This file contains hidden or 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,248 @@ | ||
| /* eslint-disable no-await-in-loop */ | ||
| import { MongoClient } from "mongodb"; | ||
|
|
||
| import { verifyHostInputValidity } from "@app/ee/services/dynamic-secret/dynamic-secret-fns"; | ||
| import { | ||
| TRotationFactory, | ||
| TRotationFactoryGetSecretsPayload, | ||
| TRotationFactoryIssueCredentials, | ||
| TRotationFactoryRevokeCredentials, | ||
| TRotationFactoryRotateCredentials | ||
| } from "@app/ee/services/secret-rotation-v2/secret-rotation-v2-types"; | ||
|
|
||
| import { DEFAULT_PASSWORD_REQUIREMENTS, generatePassword } from "../shared/utils"; | ||
| import { | ||
| TMongoDBCredentialsRotationGeneratedCredentials, | ||
| TMongoDBCredentialsRotationWithConnection | ||
| } from "./mongodb-credentials-rotation-types"; | ||
|
|
||
| const redactPasswords = (e: unknown, credentials: TMongoDBCredentialsRotationGeneratedCredentials) => { | ||
| const error = e as Error; | ||
|
|
||
| if (!error?.message) return "Unknown error"; | ||
|
|
||
| let redactedMessage = error.message; | ||
|
|
||
| credentials.forEach(({ password }) => { | ||
| redactedMessage = redactedMessage.replaceAll(password, "*******************"); | ||
| }); | ||
|
|
||
| return redactedMessage; | ||
| }; | ||
|
|
||
| export const mongodbCredentialsRotationFactory: TRotationFactory< | ||
| TMongoDBCredentialsRotationWithConnection, | ||
| TMongoDBCredentialsRotationGeneratedCredentials | ||
| > = (secretRotation) => { | ||
| const { | ||
| connection, | ||
| parameters: { username1, username2 }, | ||
| activeIndex, | ||
| secretsMapping | ||
| } = secretRotation; | ||
|
|
||
| const passwordRequirement = DEFAULT_PASSWORD_REQUIREMENTS; | ||
|
|
||
| // Helper function to create MongoDB client with given credentials | ||
| const $createMongoClient = async ( | ||
| authCredentials: { username: string; password: string }, | ||
| options?: { validateConnection?: boolean; requireTlsForSrv?: boolean } | ||
| ): Promise<MongoClient> => { | ||
| let normalizedHost = connection.credentials.host.trim(); | ||
| const isSrvFromHost = normalizedHost.startsWith("mongodb+srv://"); | ||
| if (isSrvFromHost) { | ||
| normalizedHost = normalizedHost.replace(/^mongodb\+srv:\/\//, ""); | ||
| } else if (normalizedHost.startsWith("mongodb://")) { | ||
| normalizedHost = normalizedHost.replace(/^mongodb:\/\//, ""); | ||
| } | ||
|
|
||
| const [hostIp] = await verifyHostInputValidity(normalizedHost); | ||
|
|
||
| const isSrv = !connection.credentials.port || isSrvFromHost; | ||
| const uri = isSrv ? `mongodb+srv://${hostIp}` : `mongodb://${hostIp}:${connection.credentials.port}`; | ||
|
|
||
| const clientOptions: { | ||
| auth?: { username: string; password?: string }; | ||
| authSource?: string; | ||
| tls?: boolean; | ||
| tlsInsecure?: boolean; | ||
| ca?: string; | ||
| directConnection?: boolean; | ||
| } = { | ||
| auth: { | ||
| username: authCredentials.username, | ||
| password: authCredentials.password | ||
| }, | ||
| directConnection: !isSrv | ||
| }; | ||
|
|
||
| // SSL is enabled if explicitly enabled OR if using SRV (which requires TLS) and requireTlsForSrv is true | ||
| if (connection.credentials.sslEnabled || (isSrv && options?.requireTlsForSrv)) { | ||
| clientOptions.tls = true; | ||
| clientOptions.tlsInsecure = !connection.credentials.sslRejectUnauthorized; | ||
| if (connection.credentials.sslCertificate) { | ||
| clientOptions.ca = connection.credentials.sslCertificate; | ||
| } | ||
| } | ||
|
|
||
| const client = new MongoClient(uri, clientOptions); | ||
|
|
||
| if (options?.validateConnection) { | ||
| await client.db(connection.credentials.database).command({ ping: 1 }); | ||
| } | ||
|
|
||
| return client; | ||
| }; | ||
|
|
||
| const $getClient = async () => { | ||
| let client: MongoClient | null = null; | ||
| try { | ||
| client = await $createMongoClient( | ||
| { | ||
| username: connection.credentials.username, | ||
| password: connection.credentials.password | ||
| }, | ||
| { validateConnection: true } | ||
| ); | ||
| return client; | ||
| } catch (err) { | ||
| if (client) await client.close(); | ||
| throw err; | ||
| } | ||
| }; | ||
|
|
||
| const $validateCredentials = async (credentials: TMongoDBCredentialsRotationGeneratedCredentials[number]) => { | ||
| let client: MongoClient | null = null; | ||
| try { | ||
| client = await $createMongoClient( | ||
| { | ||
| username: credentials.username, | ||
| password: credentials.password | ||
| }, | ||
| { validateConnection: true, requireTlsForSrv: true } | ||
| ); | ||
| } catch (error) { | ||
| throw new Error(redactPasswords(error, [credentials])); | ||
| } finally { | ||
| if (client) await client.close(); | ||
| } | ||
| }; | ||
|
|
||
| const issueCredentials: TRotationFactoryIssueCredentials<TMongoDBCredentialsRotationGeneratedCredentials> = async ( | ||
| callback | ||
| ) => { | ||
| // For MongoDB, since we get existing users, we change both their passwords | ||
| // on issue to invalidate their existing passwords | ||
| const credentialsSet = [ | ||
| { username: username1, password: generatePassword(passwordRequirement) }, | ||
| { username: username2, password: generatePassword(passwordRequirement) } | ||
| ]; | ||
|
|
||
| let client: MongoClient | null = null; | ||
| try { | ||
| client = await $getClient(); | ||
| const db = client.db(connection.credentials.database); | ||
|
|
||
| for (const credentials of credentialsSet) { | ||
| await db.command({ | ||
| updateUser: credentials.username, | ||
| pwd: credentials.password | ||
| }); | ||
| } | ||
| } catch (error) { | ||
| throw new Error(redactPasswords(error, credentialsSet)); | ||
| } finally { | ||
| if (client) await client.close(); | ||
| } | ||
|
|
||
| for (const credentials of credentialsSet) { | ||
| await $validateCredentials(credentials); | ||
| } | ||
|
|
||
| return callback(credentialsSet[0]); | ||
| }; | ||
|
|
||
| const revokeCredentials: TRotationFactoryRevokeCredentials<TMongoDBCredentialsRotationGeneratedCredentials> = async ( | ||
| credentialsToRevoke, | ||
| callback | ||
| ) => { | ||
| const revokedCredentials = credentialsToRevoke.map(({ username }) => ({ | ||
| username, | ||
| password: generatePassword(passwordRequirement) | ||
| })); | ||
|
|
||
| let client: MongoClient | null = null; | ||
| try { | ||
| client = await $getClient(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for both issue and revoke I think we want to use a mongodb session (transaction) - that way if either command fails neither commits |
||
| const db = client.db(connection.credentials.database); | ||
|
|
||
| for (const credentials of revokedCredentials) { | ||
| await db.command({ | ||
| updateUser: credentials.username, | ||
| pwd: credentials.password | ||
| }); | ||
| } | ||
| } catch (error) { | ||
| throw new Error(redactPasswords(error, revokedCredentials)); | ||
| } finally { | ||
| if (client) await client.close(); | ||
| } | ||
|
|
||
| return callback(); | ||
| }; | ||
|
|
||
| const rotateCredentials: TRotationFactoryRotateCredentials<TMongoDBCredentialsRotationGeneratedCredentials> = async ( | ||
| _, | ||
| callback | ||
| ) => { | ||
| const credentials = { | ||
| username: activeIndex === 0 ? username2 : username1, | ||
| password: generatePassword(passwordRequirement) | ||
| }; | ||
|
|
||
| let client: MongoClient | null = null; | ||
| try { | ||
| client = await $getClient(); | ||
| const db = client.db(connection.credentials.database); | ||
|
|
||
| await db.command({ | ||
| updateUser: credentials.username, | ||
| pwd: credentials.password | ||
| }); | ||
| } catch (error) { | ||
| throw new Error(redactPasswords(error, [credentials])); | ||
| } finally { | ||
| if (client) await client.close(); | ||
| } | ||
|
|
||
| await $validateCredentials(credentials); | ||
|
|
||
| return callback(credentials); | ||
| }; | ||
|
|
||
| const getSecretsPayload: TRotationFactoryGetSecretsPayload<TMongoDBCredentialsRotationGeneratedCredentials> = ( | ||
| generatedCredentials | ||
| ) => { | ||
| const { username, password } = secretsMapping; | ||
|
|
||
| const secrets = [ | ||
| { | ||
| key: username, | ||
| value: generatedCredentials.username | ||
| }, | ||
| { | ||
| key: password, | ||
| value: generatedCredentials.password | ||
| } | ||
| ]; | ||
|
|
||
| return secrets; | ||
| }; | ||
|
|
||
| return { | ||
| issueCredentials, | ||
| revokeCredentials, | ||
| rotateCredentials, | ||
| getSecretsPayload | ||
| }; | ||
| }; | ||
52 changes: 52 additions & 0 deletions
52
...e/services/secret-rotation-v2/mongodb-credentials/mongodb-credentials-rotation-schemas.ts
This file contains hidden or 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,52 @@ | ||
| import { z } from "zod"; | ||
|
|
||
| import { SecretRotation } from "@app/ee/services/secret-rotation-v2/secret-rotation-v2-enums"; | ||
| import { | ||
| BaseCreateSecretRotationSchema, | ||
| BaseSecretRotationSchema, | ||
| BaseUpdateSecretRotationSchema | ||
| } from "@app/ee/services/secret-rotation-v2/secret-rotation-v2-schemas"; | ||
| import { | ||
| SqlCredentialsRotationGeneratedCredentialsSchema, | ||
| SqlCredentialsRotationParametersSchema, | ||
| SqlCredentialsRotationTemplateSchema | ||
| } from "@app/ee/services/secret-rotation-v2/shared/sql-credentials/sql-credentials-rotation-schemas"; | ||
| import { SecretRotations } from "@app/lib/api-docs"; | ||
| import { SecretNameSchema } from "@app/server/lib/schemas"; | ||
| import { AppConnection } from "@app/services/app-connection/app-connection-enums"; | ||
|
|
||
| export const MongoDBCredentialsRotationGeneratedCredentialsSchema = SqlCredentialsRotationGeneratedCredentialsSchema; | ||
| export const MongoDBCredentialsRotationParametersSchema = SqlCredentialsRotationParametersSchema; | ||
| export const MongoDBCredentialsRotationTemplateSchema = SqlCredentialsRotationTemplateSchema; | ||
|
|
||
| const MongoDBCredentialsRotationSecretsMappingSchema = z.object({ | ||
| username: SecretNameSchema.describe(SecretRotations.SECRETS_MAPPING.MONGODB_CREDENTIALS.username), | ||
| password: SecretNameSchema.describe(SecretRotations.SECRETS_MAPPING.MONGODB_CREDENTIALS.password) | ||
| }); | ||
|
|
||
| export const MongoDBCredentialsRotationSchema = BaseSecretRotationSchema(SecretRotation.MongoDBCredentials).extend({ | ||
| type: z.literal(SecretRotation.MongoDBCredentials), | ||
| parameters: MongoDBCredentialsRotationParametersSchema, | ||
| secretsMapping: MongoDBCredentialsRotationSecretsMappingSchema | ||
| }); | ||
|
|
||
| export const CreateMongoDBCredentialsRotationSchema = BaseCreateSecretRotationSchema( | ||
| SecretRotation.MongoDBCredentials | ||
| ).extend({ | ||
| parameters: MongoDBCredentialsRotationParametersSchema, | ||
| secretsMapping: MongoDBCredentialsRotationSecretsMappingSchema | ||
| }); | ||
|
|
||
| export const UpdateMongoDBCredentialsRotationSchema = BaseUpdateSecretRotationSchema( | ||
| SecretRotation.MongoDBCredentials | ||
| ).extend({ | ||
| parameters: MongoDBCredentialsRotationParametersSchema.optional(), | ||
| secretsMapping: MongoDBCredentialsRotationSecretsMappingSchema.optional() | ||
| }); | ||
|
|
||
| export const MongoDBCredentialsRotationListItemSchema = z.object({ | ||
| name: z.literal("MongoDB Credentials"), | ||
| connection: z.literal(AppConnection.MongoDB), | ||
| type: z.literal(SecretRotation.MongoDBCredentials), | ||
| template: MongoDBCredentialsRotationTemplateSchema | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.