-
Notifications
You must be signed in to change notification settings - Fork 821
feat(gen2-migration): lambda access to auth #14454
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
iliapolo
merged 24 commits into
aws-amplify:gen2-migration
from
dgandhi62:codegen-func-gen-v6
Jan 15, 2026
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
5660055
feat: add Lambda access to Auth migration
dgandhi62 e19a507
fix: fixed stuff
dgandhi62 c0e03e6
fix: separate access rules per line and for each function
dgandhi62 6eebf82
fix: fix formatting
dgandhi62 a2a84a0
fix: fix parenthesis
dgandhi62 080564a
fix: fix parenthesis
dgandhi62 2f842ea
fix: please work
dgandhi62 811be7b
fix: fix mapping logic
dgandhi62 1c4cd84
fix: fix mapping
dgandhi62 24003d9
fix: fix formatting
dgandhi62 8a98b2c
chore: remove unnecessary try catch blocks
dgandhi62 816fbaa
feat: standardize parser for auth
dgandhi62 1e3f4ed
feat: remove unnecessary collector file
dgandhi62 9b4e040
feat: remove unnecessary file
dgandhi62 690bcf4
chore: remove import
dgandhi62 f924c5c
feat: restructure auth access
dgandhi62 f68333d
feat: combine functionality
dgandhi62 c2f8642
chore: remove try catch
dgandhi62 1aa262f
fix: clean up
dgandhi62 a9d8590
chore: mid work
dgandhi62 0610456
feat: add unit tests
dgandhi62 de35f08
chore: remove unused import
dgandhi62 9d2fb88
Merge branch 'gen2-migration' into codegen-func-gen-v6
iliapolo 1787941
fix compilation
iliapolo 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
196 changes: 196 additions & 0 deletions
196
...ges/amplify-cli/src/commands/gen2-migration/generate/codegen-head/auth_access_analyzer.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,196 @@ | ||
| import { BackendEnvironmentResolver } from './backend_environment_selector'; | ||
| import { BackendDownloader } from './backend_downloader'; | ||
| import { JSONUtilities, $TSMeta } from '@aws-amplify/amplify-cli-core'; | ||
| import { fileOrDirectoryExists } from './directory_exists'; | ||
| import { AuthAccess } from '../generators/functions/index'; | ||
| import path from 'node:path'; | ||
| import fs from 'node:fs/promises'; | ||
| import assert from 'node:assert'; | ||
|
|
||
| // Define grouped permissions and their required actions | ||
|
iliapolo marked this conversation as resolved.
|
||
| const GROUPED_PERMISSIONS = { | ||
| manageUsers: [ | ||
| 'cognito-idp:AdminConfirmSignUp', | ||
| 'cognito-idp:AdminCreateUser', | ||
| 'cognito-idp:AdminDeleteUser', | ||
| 'cognito-idp:AdminDeleteUserAttributes', | ||
| 'cognito-idp:AdminDisableUser', | ||
| 'cognito-idp:AdminEnableUser', | ||
| 'cognito-idp:AdminGetUser', | ||
| 'cognito-idp:AdminListGroupsForUser', | ||
| 'cognito-idp:AdminRespondToAuthChallenge', | ||
| 'cognito-idp:AdminSetUserMFAPreference', | ||
| 'cognito-idp:AdminSetUserSettings', | ||
| 'cognito-idp:AdminUpdateUserAttributes', | ||
| 'cognito-idp:AdminUserGlobalSignOut', | ||
| ], | ||
| manageGroupMembership: ['cognito-idp:AdminAddUserToGroup', 'cognito-idp:AdminRemoveUserFromGroup'], | ||
| manageGroups: [ | ||
| 'cognito-idp:GetGroup', | ||
| 'cognito-idp:ListGroups', | ||
| 'cognito-idp:CreateGroup', | ||
| 'cognito-idp:DeleteGroup', | ||
| 'cognito-idp:UpdateGroup', | ||
| ], | ||
| manageUserDevices: [ | ||
| 'cognito-idp:AdminForgetDevice', | ||
| 'cognito-idp:AdminGetDevice', | ||
| 'cognito-idp:AdminListDevices', | ||
| 'cognito-idp:AdminUpdateDeviceStatus', | ||
| ], | ||
| managePasswordRecovery: ['cognito-idp:AdminResetUserPassword', 'cognito-idp:AdminSetUserPassword'], | ||
| }; | ||
|
|
||
| const AUTH_ACTION_MAPPING: Record<string, keyof AuthAccess> = { | ||
| // Individual permissions only - no conflicts with grouped permissions | ||
| 'cognito-idp:AdminAddUserToGroup': 'addUserToGroup', | ||
| 'cognito-idp:AdminCreateUser': 'createUser', | ||
| 'cognito-idp:AdminDeleteUser': 'deleteUser', | ||
| 'cognito-idp:AdminDeleteUserAttributes': 'deleteUserAttributes', | ||
| 'cognito-idp:AdminDisableUser': 'disableUser', | ||
| 'cognito-idp:AdminEnableUser': 'enableUser', | ||
| 'cognito-idp:AdminForgetDevice': 'forgetDevice', | ||
| 'cognito-idp:AdminGetDevice': 'getDevice', | ||
| 'cognito-idp:AdminGetUser': 'getUser', | ||
| 'cognito-idp:AdminListDevices': 'listDevices', | ||
| 'cognito-idp:AdminListGroupsForUser': 'listGroupsForUser', | ||
| 'cognito-idp:AdminRemoveUserFromGroup': 'removeUserFromGroup', | ||
| 'cognito-idp:AdminResetUserPassword': 'resetUserPassword', | ||
| 'cognito-idp:AdminSetUserMFAPreference': 'setUserMfaPreference', | ||
| 'cognito-idp:AdminSetUserPassword': 'setUserPassword', | ||
| 'cognito-idp:AdminSetUserSettings': 'setUserSettings', | ||
| 'cognito-idp:AdminUpdateDeviceStatus': 'updateDeviceStatus', | ||
| 'cognito-idp:AdminUpdateUserAttributes': 'updateUserAttributes', | ||
| 'cognito-idp:ListUsers': 'listUsers', | ||
| 'cognito-idp:ListUsersInGroup': 'listUsersInGroup', | ||
|
|
||
| // Actions that don't have individual permissions - map to grouped | ||
| 'cognito-idp:AdminConfirmSignUp': 'manageUsers', | ||
| 'cognito-idp:AdminRespondToAuthChallenge': 'manageUsers', | ||
| 'cognito-idp:AdminUserGlobalSignOut': 'manageUsers', | ||
| 'cognito-idp:AdminInitiateAuth': 'manageUsers', | ||
| 'cognito-idp:AdminUpdateAuthEventFeedback': 'manageUsers', | ||
|
|
||
| // Other actions without individual permissions | ||
| 'cognito-idp:ForgetDevice': 'forgetDevice', | ||
| 'cognito-idp:VerifyUserAttribute': 'updateUserAttributes', | ||
| 'cognito-idp:UpdateUserAttributes': 'updateUserAttributes', | ||
| 'cognito-idp:SetUserMFAPreference': 'setUserMfaPreference', | ||
| 'cognito-idp:SetUserSettings': 'setUserSettings', | ||
| }; | ||
|
|
||
| function extractCognitoActionsFromPolicy(amplifyResourcesPolicy: any): string[] { | ||
| const actions: string[] = []; | ||
|
|
||
| const policyDocument = amplifyResourcesPolicy.Properties?.PolicyDocument; | ||
| const statements = Array.isArray(policyDocument?.Statement) ? policyDocument.Statement : [policyDocument?.Statement].filter(Boolean); | ||
|
|
||
| for (const statement of statements) { | ||
| const statementActions = Array.isArray(statement.Action) ? statement.Action : [statement.Action]; | ||
|
|
||
| for (const action of statementActions) { | ||
| if (typeof action === 'string' && action.startsWith('cognito-idp:')) { | ||
| if (!actions.includes(action)) { | ||
| actions.push(action); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return actions; | ||
| } | ||
|
|
||
| export function parseAuthAccessFromTemplate(templateContent: string): AuthAccess { | ||
| const authAccess: AuthAccess = {}; | ||
|
|
||
| const cfnTemplate = JSON.parse(templateContent); | ||
|
|
||
| // Check only AmplifyResourcesPolicy for consistency with other parsers | ||
| const amplifyResourcesPolicy = cfnTemplate.Resources?.AmplifyResourcesPolicy; | ||
|
|
||
| if (!amplifyResourcesPolicy || amplifyResourcesPolicy.Type !== 'AWS::IAM::Policy') { | ||
| return {}; | ||
| } | ||
|
|
||
| const cognitoActions = extractCognitoActionsFromPolicy(amplifyResourcesPolicy); | ||
| const coveredActions = new Set<string>(); | ||
|
|
||
| // First, check for complete grouped permissions | ||
| Object.entries(GROUPED_PERMISSIONS).forEach(([groupedPermission, requiredActions]) => { | ||
| const hasAllActions = requiredActions.every((action) => cognitoActions.includes(action)); | ||
| if (hasAllActions) { | ||
| authAccess[groupedPermission as keyof AuthAccess] = true; | ||
| // Mark these actions as covered by the group permission | ||
| requiredActions.forEach((action) => coveredActions.add(action)); | ||
| } | ||
| }); | ||
|
|
||
| // Then, map remaining individual actions to individual permissions | ||
| cognitoActions.forEach((action) => { | ||
| if (!coveredActions.has(action)) { | ||
| const permission = AUTH_ACTION_MAPPING[action]; | ||
| if (permission) { | ||
| authAccess[permission] = true; | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| return authAccess; | ||
| } | ||
|
|
||
| /** | ||
| * Combined auth access analyzer that handles both template fetching and parsing. | ||
| * Provides centralized functionality for auth-related CloudFormation analysis. | ||
| */ | ||
| export class AuthAccessAnalyzer { | ||
| constructor(private backendEnvironmentResolver: BackendEnvironmentResolver, private ccbFetcher: BackendDownloader) {} | ||
|
|
||
| /** | ||
| * Fetches CloudFormation templates for all functions in the project. | ||
| * @returns Map of function names to their CloudFormation template content | ||
| */ | ||
| async getFunctionTemplates(): Promise<Map<string, string>> { | ||
| const backendEnvironment = await this.backendEnvironmentResolver.selectBackendEnvironment(); | ||
| assert(backendEnvironment?.deploymentArtifacts); | ||
|
|
||
| const currentCloudBackendDirectory = await this.ccbFetcher.getCurrentCloudBackend(backendEnvironment.deploymentArtifacts); | ||
| const amplifyMetaPath = path.join(currentCloudBackendDirectory, 'amplify-meta.json'); | ||
|
|
||
| const meta = JSONUtilities.readJson<$TSMeta>(amplifyMetaPath, { throwIfNotExist: true }); | ||
| const functions = meta?.function ?? {}; | ||
|
|
||
| const functionTemplates = new Map<string, string>(); | ||
| for (const functionName of Object.keys(functions)) { | ||
| const templatePath = path.join( | ||
| currentCloudBackendDirectory, | ||
| 'function', | ||
| functionName, | ||
| `${functionName}-cloudformation-template.json`, | ||
| ); | ||
| if (await fileOrDirectoryExists(templatePath)) { | ||
| const templateContent = await fs.readFile(templatePath, 'utf8'); | ||
| functionTemplates.set(functionName, templateContent); | ||
| } | ||
| } | ||
|
|
||
| return functionTemplates; | ||
| } | ||
|
|
||
| /** | ||
| * Analyzes auth access for all functions by fetching templates and parsing them. | ||
| * @returns Map of function names to their auth access permissions | ||
| */ | ||
| async getFunctionAuthAccess(): Promise<Map<string, AuthAccess>> { | ||
| const templates = await this.getFunctionTemplates(); | ||
| const authAccessMap = new Map<string, AuthAccess>(); | ||
|
|
||
| for (const [functionName, templateContent] of templates) { | ||
| const authAccess = parseAuthAccessFromTemplate(templateContent); | ||
| if (Object.keys(authAccess).length > 0) { | ||
| authAccessMap.set(functionName, authAccess); | ||
| } | ||
| } | ||
|
|
||
| return authAccessMap; | ||
| } | ||
| } | ||
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.
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.