-
Notifications
You must be signed in to change notification settings - Fork 6
feat: allow granting rds_replication #20
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| export * from "./database"; | ||
| export * from "./role"; | ||
| export * from "./role-membership"; | ||
| export * from "./provider"; |
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
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,115 @@ | ||
| import { | ||
| CloudFormationCustomResourceEvent, | ||
| CloudFormationCustomResourceCreateEvent, | ||
| CloudFormationCustomResourceUpdateEvent, | ||
| CloudFormationCustomResourceDeleteEvent, | ||
| } from "aws-lambda/trigger/cloudformation-custom-resource"; | ||
|
|
||
| import { validateConnection, hashCode, getConnectedClient } from "./util"; | ||
| import { Connection } from "./lambda.types"; | ||
| import * as postgres from "./postgres"; | ||
|
|
||
| interface Props { | ||
| ServiceToken: string; | ||
| Connection: Connection; | ||
| Role: string; | ||
| Member: string; | ||
| } | ||
|
|
||
| export const handler = async (event: CloudFormationCustomResourceEvent) => { | ||
| switch (event.RequestType) { | ||
| case "Create": | ||
| return handleCreate(event); | ||
| case "Update": | ||
| return handleUpdate(event); | ||
| case "Delete": | ||
| return handleDelete(event); | ||
| } | ||
| }; | ||
|
|
||
| const handleCreate = async (event: CloudFormationCustomResourceCreateEvent) => { | ||
| const props = event.ResourceProperties as Props; | ||
| validateProps(props); | ||
| await grantRoleMembership(props.Connection, props.Role, props.Member); | ||
| return { | ||
| PhysicalResourceId: generatePhysicalId(props), | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Returning a physical id that encodes both the role and the member makes | ||
| * CloudFormation grant the new membership and then revoke the old one whenever | ||
| * either of them changes, which is safe because the two are never the same grant. | ||
| */ | ||
| const handleUpdate = async (event: CloudFormationCustomResourceUpdateEvent) => { | ||
| const props = event.ResourceProperties as Props; | ||
| validateProps(props); | ||
|
|
||
| const physicalResourceId = generatePhysicalId(props); | ||
| const isDifferentMembership = physicalResourceId != event.PhysicalResourceId; | ||
|
|
||
| if (isDifferentMembership) { | ||
| await grantRoleMembership(props.Connection, props.Role, props.Member); | ||
| } | ||
|
|
||
| return { PhysicalResourceId: physicalResourceId }; | ||
| }; | ||
|
|
||
| const handleDelete = async (event: CloudFormationCustomResourceDeleteEvent) => { | ||
| const props = event.ResourceProperties as Props; | ||
| validateProps(props); | ||
| await revokeRoleMembership(props.Connection, props.Role, props.Member); | ||
| return {}; | ||
| }; | ||
|
|
||
| const validateProps = (props: Props) => { | ||
| if (!("Connection" in props)) { | ||
| throw "Connection property is required"; | ||
| } | ||
| validateConnection(props.Connection); | ||
|
|
||
| if (!("Role" in props)) { | ||
| throw "Role property is required"; | ||
| } | ||
| if (!("Member" in props)) { | ||
| throw "Member property is required"; | ||
| } | ||
| }; | ||
|
|
||
| const generatePhysicalId = (props: Props): string => { | ||
| const { Host, Port } = props.Connection; | ||
| const suffix = Math.abs( | ||
| hashCode(JSON.stringify([Host, Port, props.Role, props.Member])) | ||
| ); | ||
|
pascal-botpress marked this conversation as resolved.
|
||
| return `role-membership-${suffix}`; | ||
| }; | ||
|
|
||
| export const grantRoleMembership = async ( | ||
| connection: Connection, | ||
| role: string, | ||
| member: string | ||
| ) => { | ||
| console.log(`Granting ${role} to ${member}`); | ||
| const client = await getConnectedClient(connection); | ||
|
|
||
| try { | ||
| await postgres.grantRoleMembership({ client, role, member }); | ||
| } finally { | ||
| await client.end(); | ||
| } | ||
| }; | ||
|
pascal-botpress marked this conversation as resolved.
|
||
|
|
||
| export const revokeRoleMembership = async ( | ||
| connection: Connection, | ||
| role: string, | ||
| member: string | ||
| ) => { | ||
| console.log(`Revoking ${role} from ${member}`); | ||
| const client = await getConnectedClient(connection); | ||
|
|
||
| try { | ||
| await postgres.revokeRoleMembership({ client, role, member }); | ||
| } finally { | ||
| await client.end(); | ||
| } | ||
| }; | ||
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,80 @@ | ||
| import { Construct } from "constructs"; | ||
| import * as cdk from "aws-cdk-lib"; | ||
| import { RemovalPolicy } from "aws-cdk-lib"; | ||
| import { Provider } from "./provider"; | ||
|
|
||
| /** | ||
| * Predefined roles, listed so they auto-complete in vscode. | ||
| */ | ||
| export type PredefinedRoleName = | ||
| | "rds_replication" | ||
| | "rds_superuser" | ||
| | "rds_iam" | ||
| | "rds_password" | ||
| | "pg_monitor" | ||
| | "pg_read_all_data" | ||
| | "pg_write_all_data" | ||
| | "pg_read_all_settings" | ||
| | "pg_read_all_stats" | ||
| | "pg_stat_scan_tables" | ||
| | "pg_signal_backend"; | ||
|
|
||
| export interface RoleMembershipProps { | ||
| /** | ||
| * Provider required to connect to the Postgresql server | ||
| */ | ||
| provider: Provider; | ||
|
|
||
| /** | ||
| * The role whose membership is granted, as in `GRANT <role> TO <member>`. It | ||
| * has to exist already, whether it is predefined or was created elsewhere in | ||
| * the stack. | ||
| */ | ||
| role: PredefinedRoleName | (string & {}); | ||
|
|
||
| /** | ||
| * The role receiving the membership, as in `GRANT <role> TO <member>`. It | ||
| * has to exist already, like the role it is made a member of. | ||
| */ | ||
| member: string; | ||
|
|
||
| /** | ||
| * Policy to apply when the membership is removed from this stack. | ||
| * | ||
| * @default - The membership will be revoked. | ||
| */ | ||
| removalPolicy?: RemovalPolicy; | ||
| } | ||
|
|
||
| /** | ||
| * Membership of one Postgresql role in another, which is how a role receives | ||
| * privileges it cannot be granted directly. On RDS and Aurora the master role | ||
| * cannot grant the replication attribute, so a role that has to read the | ||
| * write-ahead log is made a member of `rds_replication` instead. | ||
| * | ||
| * Both roles are named by plain strings, which creates no CloudFormation | ||
| * dependency. When either of them is created by a `Role` construct in the same | ||
| * stack, call `addDependency` on this construct so that the grant does not run | ||
| * first and fail with `role "..." does not exist`. | ||
| */ | ||
| export class RoleMembership extends Construct { | ||
| constructor(scope: Construct, id: string, props: RoleMembershipProps) { | ||
| super(scope, id); | ||
|
|
||
| const { provider, role, member, removalPolicy } = props; | ||
|
pascal-botpress marked this conversation as resolved.
|
||
|
|
||
| const cr = new cdk.CustomResource(this, "CustomResource", { | ||
| serviceToken: provider.serviceToken, | ||
| resourceType: "Custom::Postgresql-RoleMembership", | ||
| properties: { | ||
| connection: provider.buildConnectionProperty(), | ||
| role, | ||
| member, | ||
| }, | ||
| pascalCaseProperties: true, | ||
| }); | ||
|
|
||
| cr.applyRemovalPolicy(removalPolicy || cdk.RemovalPolicy.DESTROY); | ||
| cr.node.addDependency(provider); | ||
|
pascal-botpress marked this conversation as resolved.
|
||
| } | ||
| } | ||
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
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
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.