UNPKG

@aws-amplify/cli-internal

Version:
181 lines • 9.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AwsFetcher = void 0; const client_cognito_identity_provider_1 = require("@aws-sdk/client-cognito-identity-provider"); const client_cognito_identity_1 = require("@aws-sdk/client-cognito-identity"); const client_lambda_1 = require("@aws-sdk/client-lambda"); const client_cloudwatch_events_1 = require("@aws-sdk/client-cloudwatch-events"); const client_s3_1 = require("@aws-sdk/client-s3"); const client_appsync_1 = require("@aws-sdk/client-appsync"); const client_dynamodb_1 = require("@aws-sdk/client-dynamodb"); const client_amplify_1 = require("@aws-sdk/client-amplify"); const client_api_gateway_1 = require("@aws-sdk/client-api-gateway"); const client_cloudformation_1 = require("@aws-sdk/client-cloudformation"); const amplify_cli_core_1 = require("@aws-amplify/amplify-cli-core"); class AwsFetcher { constructor(clients) { this.clients = clients; } async fetchUserPool(userPoolId) { const { UserPool } = await this.clients.cognitoIdentityProvider.send(new client_cognito_identity_provider_1.DescribeUserPoolCommand({ UserPoolId: userPoolId })); if (!UserPool) { throw new amplify_cli_core_1.AmplifyError('UserPoolNotFoundError', { message: `User pool '${userPoolId}' not found`, resolution: 'Verify the Cognito User Pool exists and the CLI has the correct AWS credentials and region configured.', }); } return UserPool; } async fetchMfaConfig(userPoolId) { return this.clients.cognitoIdentityProvider.send(new client_cognito_identity_provider_1.GetUserPoolMfaConfigCommand({ UserPoolId: userPoolId })); } async fetchUserPoolClient(userPoolId, clientId) { const { UserPoolClient } = await this.clients.cognitoIdentityProvider.send(new client_cognito_identity_provider_1.DescribeUserPoolClientCommand({ UserPoolId: userPoolId, ClientId: clientId })); if (!UserPoolClient) { throw new amplify_cli_core_1.AmplifyError('MigrationError', { message: `Unable to find user pool client with id ${clientId} (UserPool: ${userPoolId})`, }); } return UserPoolClient; } async fetchIdentityProviders(userPoolId) { var _a; const paginator = (0, client_cognito_identity_provider_1.paginateListIdentityProviders)({ client: this.clients.cognitoIdentityProvider }, { UserPoolId: userPoolId }); const details = []; for await (const page of paginator) { for (const provider of (_a = page.Providers) !== null && _a !== void 0 ? _a : []) { const { IdentityProvider } = await this.clients.cognitoIdentityProvider.send(new client_cognito_identity_provider_1.DescribeIdentityProviderCommand({ UserPoolId: userPoolId, ProviderName: provider.ProviderName })); if (IdentityProvider) details.push(IdentityProvider); } } return details; } async fetchIdentityGroups(userPoolId) { var _a; const paginator = (0, client_cognito_identity_provider_1.paginateListGroups)({ client: this.clients.cognitoIdentityProvider }, { UserPoolId: userPoolId }); const groups = []; for await (const page of paginator) { groups.push(...((_a = page.Groups) !== null && _a !== void 0 ? _a : [])); } return groups; } async fetchIdentityPool(identityPoolId) { return this.clients.cognitoIdentity.send(new client_cognito_identity_1.DescribeIdentityPoolCommand({ IdentityPoolId: identityPoolId })); } async fetchIdentityPoolRoles(identityPoolId) { const { Roles } = await this.clients.cognitoIdentity.send(new client_cognito_identity_1.GetIdentityPoolRolesCommand({ IdentityPoolId: identityPoolId })); if (!Roles) return undefined; return { authenticated: Roles.authenticated, unauthenticated: Roles.unauthenticated }; } async fetchGroupsByUserPoolId(userPoolId) { var _a; const paginator = (0, client_cognito_identity_provider_1.paginateListGroups)({ client: this.clients.cognitoIdentityProvider }, { UserPoolId: userPoolId }); const result = {}; for await (const page of paginator) { for (const { GroupName, RoleArn } of (_a = page.Groups) !== null && _a !== void 0 ? _a : []) { if (GroupName && RoleArn) result[GroupName] = RoleArn; } } return Object.keys(result).length > 0 ? result : undefined; } async fetchFunctionConfig(deployedName) { const result = await this.clients.lambda.send(new client_lambda_1.GetFunctionCommand({ FunctionName: deployedName })); return result.Configuration; } async fetchFunctionSchedule(deployedName) { var _a, _b; let ruleName; try { const policyResponse = await this.clients.lambda.send(new client_lambda_1.GetPolicyCommand({ FunctionName: deployedName })); if (!(policyResponse === null || policyResponse === void 0 ? void 0 : policyResponse.Policy)) return undefined; const policy = JSON.parse(policyResponse.Policy); ruleName = (_b = (_a = policy.Statement) === null || _a === void 0 ? void 0 : _a.find((s) => { var _a, _b, _c; return (_c = (_b = (_a = s.Condition) === null || _a === void 0 ? void 0 : _a.ArnLike) === null || _b === void 0 ? void 0 : _b['AWS:SourceArn']) === null || _c === void 0 ? void 0 : _c.includes('rule/'); })) === null || _b === void 0 ? void 0 : _b.Condition.ArnLike['AWS:SourceArn'].split('/').pop(); } catch (e) { if (e instanceof Error && e.name === 'ResourceNotFoundException') { return undefined; } throw e; } if (!ruleName) return undefined; const ruleResponse = await this.clients.cloudWatchEvents.send(new client_cloudwatch_events_1.DescribeRuleCommand({ Name: ruleName })); return ruleResponse.ScheduleExpression; } async fetchBucketNotifications(bucketName) { return this.clients.s3.send(new client_s3_1.GetBucketNotificationConfigurationCommand({ Bucket: bucketName })); } async fetchBucketAccelerate(bucketName) { const { Status } = await this.clients.s3.send(new client_s3_1.GetBucketAccelerateConfigurationCommand({ Bucket: bucketName })); return Status; } async fetchBucketVersioning(bucketName) { const { Status } = await this.clients.s3.send(new client_s3_1.GetBucketVersioningCommand({ Bucket: bucketName })); return Status; } async fetchBucketEncryption(bucketName) { const { ServerSideEncryptionConfiguration } = await this.clients.s3.send(new client_s3_1.GetBucketEncryptionCommand({ Bucket: bucketName })); return ServerSideEncryptionConfiguration; } async fetchGraphqlApi(apiId) { const { graphqlApi } = await this.clients.appSync.send(new client_appsync_1.GetGraphqlApiCommand({ apiId })); return graphqlApi; } async fetchTableDescription(tableName) { const { Table } = await this.clients.dynamoDB.send(new client_dynamodb_1.DescribeTableCommand({ TableName: tableName })); return Table; } async fetchAppBuildSpec(appId) { const { app } = await this.clients.amplify.send(new client_amplify_1.GetAppCommand({ appId })); return app === null || app === void 0 ? void 0 : app.buildSpec; } async fetchRestApiRootResourceId(restApiId) { const { items } = await this.clients.apiGateway.send(new client_api_gateway_1.GetResourcesCommand({ restApiId })); const root = items === null || items === void 0 ? void 0 : items.find((r) => r.path === '/'); if (!(root === null || root === void 0 ? void 0 : root.id)) { throw new amplify_cli_core_1.AmplifyError('RestApiResourceNotFoundError', { message: `Root resource not found for REST API '${restApiId}'`, resolution: 'Verify the API Gateway REST API exists and the CLI has the correct AWS credentials and region configured.', }); } return root.id; } async listNestedStacks(stackName) { var _a; const results = []; const paginator = (0, client_cloudformation_1.paginateListStackResources)({ client: this.clients.cloudFormation }, { StackName: stackName }); for await (const page of paginator) { for (const resource of (_a = page.StackResourceSummaries) !== null && _a !== void 0 ? _a : []) { if (resource.ResourceType === 'AWS::CloudFormation::Stack') { results.push(resource); } } } return results; } async listStackResources(stackName) { var _a; const results = []; const paginator = (0, client_cloudformation_1.paginateListStackResources)({ client: this.clients.cloudFormation }, { StackName: stackName }); for await (const page of paginator) { results.push(...((_a = page.StackResourceSummaries) !== null && _a !== void 0 ? _a : [])); } return results; } async findResourcePhysicalId(stackName, logicalResourceId) { var _a; const paginator = (0, client_cloudformation_1.paginateListStackResources)({ client: this.clients.cloudFormation }, { StackName: stackName }); for await (const page of paginator) { const match = ((_a = page.StackResourceSummaries) !== null && _a !== void 0 ? _a : []).find((r) => r.LogicalResourceId === logicalResourceId); if (match) return match.PhysicalResourceId; } return undefined; } } exports.AwsFetcher = AwsFetcher; //# sourceMappingURL=aws-fetcher.js.map