Skip to content
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

fix(redshift-alpha): extract tableName from custom resource functions #32452

Merged
merged 7 commits into from
Dec 13, 2024

Conversation

5d
Copy link
Contributor

@5d 5d commented Dec 10, 2024

Issue # (if applicable)

Closes

Reason for this change

This regression was introduced in PR#24308, which added support for executing the ALT TABLE SQL statement when the table name is changed in the CDK construct. The root cause is in the modification of the physical ID of the custom resource Lambda function. Previously, this Lambda function was returning the tableName after table is created. However, the physical ID was updated to a combined ID in the format clusterId:databaseName:tableNamePrefix:stackIdSuffix.

This change breaks the logic that depends on the return value being used as the tableName. For example, in the reported issue, the integration test relies on this value to grant permissions to specific users. The new combined ID format causes SQL statement errors, as described in the issue.

Description of changes

The fix involves adding logic to detect whether the resource's physical ID is in the new format. If it is, the logic reconstructs the tableName from the combined ID. This ensures that Lambda functions referencing the tableName receive the correct value.

Description of how you validated changes

  • rerun unit test cases
  • deployed integration test cases

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@github-actions github-actions bot added bug This issue is a bug. effort/medium Medium work item – several days of effort p2 labels Dec 10, 2024
@aws-cdk-automation aws-cdk-automation requested a review from a team December 10, 2024 00:50
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Dec 10, 2024
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

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

The pull request linter has failed. See the aws-cdk-automation comment below for failure reasons. If you believe this pull request should receive an exemption, please comment and provide a justification.

A comment requesting an exemption should contain the text Exemption Request. Additionally, if clarification is needed add Clarification Request to a comment.

@5d 5d marked this pull request as ready for review December 10, 2024 00:55
Copy link

codecov bot commented Dec 10, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 78.66%. Comparing base (934c9dc) to head (6487792).

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #32452   +/-   ##
=======================================
  Coverage   78.66%   78.66%           
=======================================
  Files         107      107           
  Lines        7161     7161           
  Branches     1320     1320           
=======================================
  Hits         5633     5633           
  Misses       1343     1343           
  Partials      185      185           
Flag Coverage Δ
suite.unit 78.66% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
packages/aws-cdk 78.66% <ø> (ø)

@5d 5d changed the title fix(redshift-alpha): use non-optional tableName directly fix(redshift-alpha): extract tableName from custom resource functions Dec 11, 2024
@aws-cdk-automation aws-cdk-automation dismissed their stale review December 11, 2024 00:26

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

@5d 5d requested a review from a team as a code owner December 11, 2024 16:40
@5d 5d force-pushed the 5d/fix-31817 branch 4 times, most recently from a2eb3e1 to 5d9c19d Compare December 11, 2024 18:21
@aws-cdk-automation aws-cdk-automation added the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Dec 11, 2024
Copy link
Contributor

@GavinZZ GavinZZ left a comment

Choose a reason for hiding this comment

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

Left some changes mainly to help me understand the change.

Comment on lines 66 to 77
(privileges, { table, actions }) => ({
...privileges,
[table.node.id]: {
actions: [
...(privileges[table.node.id]?.actions ?? []),
...actions,
],
tableName: table.tableName,
actions: [],
};
}
actions = actions.concat(privileges[tableId].actions);
if (actions.includes(TableAction.ALL)) {
actions = [TableAction.ALL];
}
if (actions.includes(TableAction.UPDATE) || actions.includes(TableAction.DELETE)) {
actions.push(TableAction.SELECT);
}
privileges[tableId] = {
tableName: table.tableName,
actions: Array.from(new Set(actions)),
};
return privileges;
}, {} as { [key: string]: { tableName: string; actions: TableAction[] } });
const serializedPrivileges: SerializedTablePrivilege[] = Object.entries(reducedPrivileges).map(([tableId, config]) => ({
},
}),
{} as Record<string, { tableName: string; actions: TableAction[] }>,
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we make this code more readable? It's already inside reduce and the use of spread operator with arrow function make this code hard to read. IMO, this can be simple and straightforward like

const groupedPrivileges = this.privileges.reduce(
  (acc, { table, actions }) => {
    const currentActions = acc[table.node.id]?.actions ?? [];
    const updatedActions = [...currentActions, ...actions];

    acc[table.node.id] = {
      actions: updatedActions,
      tableName: table.tableName,
    };

    return acc;
  },
  {} as Record<string, { tableName: string; actions: TableAction[] }>
);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, I was using a single expression to do it here. I will update it with a function.


/**
* We need this normalization logic because some of the `TableName` values
* are physical IDs generated in the `./util.ts` module.
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see a util.ts file in this module so I'm not sure how the TableName was generated. Can you share the link to the function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will add the module link in the comment.

@aws-cdk-automation aws-cdk-automation removed the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Dec 12, 2024
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 6487792
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

Copy link
Contributor

mergify bot commented Dec 13, 2024

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot merged commit 283edd6 into main Dec 13, 2024
20 checks passed
@mergify mergify bot deleted the 5d/fix-31817 branch December 13, 2024 19:14
Copy link

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug This issue is a bug. contribution/core This is a PR that came from AWS. effort/medium Medium work item – several days of effort p2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants