Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ type MongoServiceOptions = {
type: 'MONGO';
serviceName: string;
username: pulumi.Input<string>;
password: pulumi.Input<string>;
password?: pulumi.Input<string>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also describe in the readme that if the password is not provided it will be auto-generated.

port?: pulumi.Input<number>;
size?: pulumi.Input<Size>;
tags?: pulumi.Input<{
Expand Down Expand Up @@ -586,7 +586,7 @@ export type MongoArgs = {
vpcCidrBlock: pulumi.Input<string>;
privateSubnetIds: pulumi.Input<pulumi.Input<string>[]>;
username: pulumi.Input<string>;
password: pulumi.Input<string>;
password?: pulumi.Input<string>;
port?: pulumi.Input<number>;
size?: pulumi.Input<Size>;
tags?: pulumi.Input<{
Expand All @@ -595,6 +595,10 @@ export type MongoArgs = {
};
```

If the password is not specified it will be autogenerated.
The mongo password is stored as a secret inside AWS Secret Manager.
The secret will be available on the `Mongo` resource as `passwordSecret`.

### Ecs Service

AWS ECS Fargate.
Expand Down
19 changes: 16 additions & 3 deletions src/components/mongo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as pulumi from '@pulumi/pulumi';
import * as aws from '@pulumi/aws';
import * as random from '@pulumi/random';
import { commonTags } from '../constants';
import { EcsService, EcsServiceArgs } from './ecs-service';

Expand All @@ -12,10 +13,10 @@ export type MongoArgs = Pick<
*/
username: pulumi.Input<string>;
/**
* Password for the master DB user.
* Password for the master DB user. If not specified it will be autogenerated.
* The value will be stored as a secret in AWS Secret Manager.
*/
password: pulumi.Input<string>;
password?: pulumi.Input<string>;
privateSubnetIds: pulumi.Input<pulumi.Input<string>[]>;
/**
* Exposed service port. Defaults to 27017.
Expand All @@ -40,7 +41,9 @@ export class Mongo extends pulumi.ComponentResource {
const { username, password, privateSubnetIds, ...ecsServiceArgs } = args;

this.name = name;
this.passwordSecret = this.createPasswordSecret(password);

const mongoPassword = password || this.createRandomPassword();
this.passwordSecret = this.createPasswordSecret(mongoPassword);

this.service = new EcsService(
name,
Expand Down Expand Up @@ -75,6 +78,16 @@ export class Mongo extends pulumi.ComponentResource {
this.registerOutputs();
}

private createRandomPassword() {
const password = new random.RandomPassword(`${this.name}-mongo-password`, {
length: 16,
overrideSpecial: '_%$',
special: true,
});

return password.result;
}

private createPasswordSecret(password: MongoArgs['password']) {
const project = pulumi.getProject();
const stack = pulumi.getStack();
Expand Down