-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Changes from 5 commits
b4d5a43
157b225
355c1bc
8dc18f4
957ccf7
a20d133
6487792
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ import { ITable, TableAction } from '../table'; | |
import { IUser } from '../user'; | ||
import { DatabaseQuery } from './database-query'; | ||
import { HandlerName } from './database-query-provider/handler-name'; | ||
import { TablePrivilege as SerializedTablePrivilege, UserTablePrivilegesHandlerProps } from './handler-props'; | ||
import { UserTablePrivilegesHandlerProps } from './handler-props'; | ||
|
||
/** | ||
* The Redshift table and action that make up a privilege that can be granted to a Redshift user. | ||
|
@@ -62,33 +62,25 @@ export class UserTablePrivileges extends Construct { | |
username: props.user.username, | ||
tablePrivileges: cdk.Lazy.any({ | ||
produce: () => { | ||
const reducedPrivileges = this.privileges.reduce((privileges, { table, actions }) => { | ||
const tableId = table.node.id; | ||
if (!(tableId in privileges)) { | ||
privileges[tableId] = { | ||
const groupedPrivileges = this.privileges.reduce( | ||
(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[] }>, | ||
); | ||
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. Can we make this code more readable? It's already inside
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. ok, I was using a single expression to do it here. I will update it with a function. |
||
|
||
return Object.entries(groupedPrivileges).map(([tableId, config]) => ({ | ||
tableId, | ||
tableName: config.tableName, | ||
actions: config.actions.map(action => TableAction[action]), | ||
actions: unifyTableActions(config.actions).map(action => TableAction[action]), | ||
})); | ||
return serializedPrivileges; | ||
GavinZZ marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
}) as any, | ||
}, | ||
|
@@ -102,3 +94,17 @@ export class UserTablePrivileges extends Construct { | |
this.privileges.push({ table, actions }); | ||
} | ||
} | ||
|
||
const unifyTableActions = (tableActions: TableAction[]): TableAction[] => { | ||
const set = new Set<TableAction>(tableActions); | ||
|
||
if (set.has(TableAction.ALL)) { | ||
return [TableAction.ALL]; | ||
} | ||
|
||
if (set.has(TableAction.UPDATE) || set.has(TableAction.DELETE)) { | ||
set.add(TableAction.SELECT); | ||
} | ||
|
||
return [...set]; | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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.