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

chore: upgrade aws-amplify from v4 to v6 #13430

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/amplify-e2e-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@aws-sdk/client-sts": "^3.303.0",
"@aws-sdk/credential-providers": "^3.303.0",
"amplify-headless-interface": "1.17.6",
"aws-amplify": "^4.2.8",
"aws-amplify": "^6.0.4",
"aws-appsync": "^4.1.1",
"aws-sdk": "^2.1464.0",
"chalk": "^4.1.1",
Expand Down
83 changes: 35 additions & 48 deletions packages/amplify-e2e-core/src/utils/auth-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Amplify, { Auth } from 'aws-amplify';
import AWSAppSyncClient, { AUTH_TYPE } from 'aws-appsync';
import { Amplify } from 'aws-amplify';
import { generateClient } from 'aws-amplify/api';
import { signIn, signOut, resetPassword } from 'aws-amplify/auth';
import { CognitoIdentityServiceProvider } from 'aws-sdk';
import fs from 'fs-extra';
import path from 'path';
Expand Down Expand Up @@ -72,65 +73,52 @@ export function getConfiguredCognitoClient(region = process.env.CLI_REGION): Cog
return cognitoClient;
}

export function getConfiguredAppsyncClientCognitoAuth(url: string, region: string, user: any) {
return new AWSAppSyncClient({
url,
region,
disableOffline: true,
auth: {
type: AUTH_TYPE.AMAZON_COGNITO_USER_POOLS,
jwtToken: user.signInUserSession.idToken.jwtToken,
export function getConfiguredAppsyncClientCognitoAuth(config, user) {
config.aws_appsync_authenticationType = 'AMAZON_COGNITO_USER_POOLS';
Amplify.configure(config, {
API: {
REST: {
headers: async () => {
return { Authorization: user.signInUserSession.idToken.jwtToken };
},
},
},
});
return generateClient();
}

export function getConfiguredAppsyncClientOIDCAuth(url: string, region: string, user: any) {
return new AWSAppSyncClient({
url,
region,
disableOffline: true,
auth: {
type: AUTH_TYPE.OPENID_CONNECT,
jwtToken: user.signInUserSession.idToken.jwtToken,
export function getConfiguredAppsyncClientOIDCAuth(config, user) {
config.aws_appsync_authenticationType = 'OPENID_CONNECT';
Amplify.configure(config, {
API: {
REST: {
headers: async () => {
return { Authorization: user.signInUserSession.idToken.jwtToken };
},
},
},
});
return generateClient();
}

export function getConfiguredAppsyncClientAPIKeyAuth(url: string, region: string, apiKey: string): any {
return new AWSAppSyncClient({
url,
region,
disableOffline: true,
auth: {
type: AUTH_TYPE.API_KEY,
apiKey,
},
});
export function getConfiguredAppsyncClientAPIKeyAuth(config, apiKey) {
config.aws_appsync_authenticationType = 'API_KEY';
Amplify.configure({ ...config, aws_appsync_apiKey: apiKey });
return generateClient();
}

export function getConfiguredAppsyncClientIAMAuth(url: string, region: string) {
return new AWSAppSyncClient({
url,
region,
disableOffline: true,
auth: {
type: AUTH_TYPE.AWS_IAM,
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
sessionToken: process.env.AWS_SESSION_TOKEN,
},
},
});
export function getConfiguredAppsyncClientIAMAuth(config) {
Amplify.configure(config);
return generateClient();
}

export async function signInUser(username: string, password: string) {
const user = await Auth.signIn(username, password);
const user = await signIn({ username, password });
return user;
}

export async function signOutUser(): Promise<void> {
await Auth.signOut({ global: true });
await signOut({ global: true });
}

export function configureAmplify(projectDir: string) {
Expand Down Expand Up @@ -179,10 +167,9 @@ export function getApiKey(projectDir: string): string {
}

export async function authenticateUser(username: string, tempPassword: string, password: string) {
const signinResult = await Auth.signIn(username, tempPassword);
if (signinResult.challengeName === 'NEW_PASSWORD_REQUIRED') {
const { requiredAttributes } = signinResult.challengeParam; // the array of required attributes, e.g [‘email’, ‘phone_number’]
await Auth.completeNewPassword(signinResult, password, requiredAttributes);
const signinResult = await signIn({ username, password: tempPassword });
if (signinResult.nextStep.signInStep === 'RESET_PASSWORD') {
await resetPassword({ username, options: { password } });
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/amplify-e2e-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"amplify-dynamodb-simulator": "2.9.8",
"amplify-headless-interface": "1.17.6",
"amplify-storage-simulator": "1.11.3",
"aws-amplify": "^4.2.8",
"aws-amplify": "^6.0.4",
"aws-appsync": "^4.1.1",
"aws-cdk-lib": "~2.80.0",
"aws-sdk": "^2.1464.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
signOutUser,
updateAuthAddUserGroups,
} from '@aws-amplify/amplify-e2e-core';
import { Auth } from 'aws-amplify';
import { fetchAuthSession } from 'aws-amplify/auth';
import { S3Client, GetObjectCommand, PutObjectCommand } from '@aws-sdk/client-s3';

const defaultsSettings = {
Expand Down Expand Up @@ -62,11 +62,11 @@ describe('user group tests', () => {

// Check that user 1 can interact with S3 bucket
await signInUser(username1, password1);
const user1Credentials = await Auth.currentCredentials();
const user1Credentials = await fetchAuthSession();

const s3Client1 = new S3Client({
region,
credentials: Auth.essentialCredentials(user1Credentials),
credentials: user1Credentials.credentials,
});
await s3Client1.send(
new PutObjectCommand({
Expand All @@ -88,10 +88,10 @@ describe('user group tests', () => {

// Check that user 2 does not have permissions to interact with S3 bucket
await signInUser(username2, password2);
const user2Credentials = await Auth.currentCredentials();
const user2Credentials = await fetchAuthSession();
const s3Client2 = new S3Client({
region,
credentials: Auth.essentialCredentials(user2Credentials),
credentials: user2Credentials.credentials,
});
await expect(
s3Client2.send(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
updateApiWithMultiAuth,
updateAuthAddUserGroups,
} from '@aws-amplify/amplify-e2e-core';
import gql from 'graphql-tag';

(global as any).fetch = require('node-fetch');

Expand Down Expand Up @@ -61,17 +60,9 @@ describe('transformer @auth migration test', () => {

let apiKey = getApiKey(projRoot);

let appSyncClientViaUser = getConfiguredAppsyncClientCognitoAuth(
awsconfig.aws_appsync_graphqlEndpoint,
awsconfig.aws_appsync_region,
user,
);
let appSyncClientViaApiKey = getConfiguredAppsyncClientAPIKeyAuth(
awsconfig.aws_appsync_graphqlEndpoint,
awsconfig.aws_appsync_region,
apiKey,
);
const appSyncClientViaIAM = getConfiguredAppsyncClientIAMAuth(awsconfig.aws_appsync_graphqlEndpoint, awsconfig.aws_appsync_region);
let appSyncClientViaUser = getConfiguredAppsyncClientCognitoAuth(awsconfig, user);
let appSyncClientViaApiKey = getConfiguredAppsyncClientAPIKeyAuth(awsconfig, apiKey);
const appSyncClientViaIAM = getConfiguredAppsyncClientIAMAuth(awsconfig);

let createPostMutation = /* GraphQL */ `
mutation CreatePost {
Expand All @@ -81,12 +72,9 @@ describe('transformer @auth migration test', () => {
}
`;

let createPostResult = await appSyncClientViaUser.mutate({
mutation: gql(createPostMutation),
fetchPolicy: 'no-cache',
});
let createPostResult = (await appSyncClientViaUser.graphql({ query: createPostMutation }, { fetchPolicy: 'no-cache' })) as any;

expect(createPostResult.errors).toBeUndefined();
expect(createPostResult.error).toBeUndefined();
expect(createPostResult.data).toBeDefined();

let createPostPublicMutation = /* GraphQL */ `
Expand All @@ -97,10 +85,10 @@ describe('transformer @auth migration test', () => {
}
`;

let createPostPublicResult = await appSyncClientViaApiKey.mutate({
mutation: gql(createPostPublicMutation),
fetchPolicy: 'no-cache',
});
let createPostPublicResult = (await appSyncClientViaApiKey.graphql(
{ query: createPostPublicMutation },
{ fetchPolicy: 'no-cache' },
)) as any;

expect(createPostPublicResult.errors).toBeUndefined();
expect(createPostPublicResult.data).toBeDefined();
Expand All @@ -113,10 +101,10 @@ describe('transformer @auth migration test', () => {
}
`;

const createPostPublicIAMResult = await appSyncClientViaIAM.mutate({
mutation: gql(createPostPublicIAMMutation),
fetchPolicy: 'no-cache',
});
const createPostPublicIAMResult = (await appSyncClientViaIAM.graphql(
{ query: createPostPublicIAMMutation },
{ fetchPolicy: 'no-cache' },
)) as any;

expect(createPostPublicIAMResult.errors).toBeUndefined();
expect(createPostPublicIAMResult.data).toBeDefined();
Expand All @@ -130,10 +118,7 @@ describe('transformer @auth migration test', () => {
}
`;

let createSalaryResult = await appSyncClientViaUser.mutate({
mutation: gql(createSalaryMutation),
fetchPolicy: 'no-cache',
});
let createSalaryResult = (await appSyncClientViaUser.graphql({ query: createSalaryMutation }, { fetchPolicy: 'no-cache' })) as any;

expect(createSalaryResult.errors).toBeUndefined();
expect(createSalaryResult.data).toBeDefined();
Expand All @@ -144,7 +129,7 @@ describe('transformer @auth migration test', () => {
await updateApiSchema(projRoot, projectName, modelSchemaV2);
await amplifyPushUpdate(projRoot);

appSyncClientViaUser = getConfiguredAppsyncClientCognitoAuth(awsconfig.aws_appsync_graphqlEndpoint, awsconfig.aws_appsync_region, user);
appSyncClientViaUser = getConfiguredAppsyncClientCognitoAuth(awsconfig, user);

createPostMutation = /* GraphQL */ `
mutation CreatePost {
Expand All @@ -154,20 +139,13 @@ describe('transformer @auth migration test', () => {
}
`;

createPostResult = await appSyncClientViaUser.mutate({
mutation: gql(createPostMutation),
fetchPolicy: 'no-cache',
});
createPostResult = (await appSyncClientViaUser.graphql({ query: createPostMutation }, { fetchPolicy: 'no-cache' })) as any;

expect(createPostResult.errors).toBeUndefined();
expect(createPostResult.data).toBeDefined();

apiKey = getApiKey(projRoot);
appSyncClientViaApiKey = getConfiguredAppsyncClientAPIKeyAuth(
awsconfig.aws_appsync_graphqlEndpoint,
awsconfig.aws_appsync_region,
apiKey,
);
appSyncClientViaApiKey = getConfiguredAppsyncClientAPIKeyAuth(awsconfig, apiKey);

createPostPublicMutation = /* GraphQL */ `
mutation CreatePostPublic {
Expand All @@ -177,10 +155,10 @@ describe('transformer @auth migration test', () => {
}
`;

createPostPublicResult = await appSyncClientViaApiKey.mutate({
mutation: gql(createPostPublicMutation),
fetchPolicy: 'no-cache',
});
createPostPublicResult = (await appSyncClientViaApiKey.graphql(
{ query: createPostPublicMutation },
{ fetchPolicy: 'no-cache' },
)) as any;

expect(createPostPublicResult.errors).toBeUndefined();
expect(createPostPublicResult.data).toBeDefined();
Expand All @@ -194,10 +172,7 @@ describe('transformer @auth migration test', () => {
}
`;

createSalaryResult = await appSyncClientViaUser.mutate({
mutation: gql(createSalaryMutation),
fetchPolicy: 'no-cache',
});
createSalaryResult = (await appSyncClientViaUser.graphql({ query: createSalaryMutation }, { fetchPolicy: 'no-cache' })) as any;

expect(createSalaryResult.errors).toBeUndefined();
expect(createSalaryResult.data).toBeDefined();
Expand All @@ -213,10 +188,7 @@ describe('transformer @auth migration test', () => {
}
`;

let queryResult = await appSyncClientViaUser.query({
query: gql(postsQuery),
fetchPolicy: 'no-cache',
});
let queryResult = (await appSyncClientViaUser.graphql({ query: postsQuery }, { fetchPolicy: 'no-cache' })) as any;

expect(queryResult.errors).toBeUndefined();
expect(queryResult.data).toBeDefined();
Expand All @@ -233,10 +205,7 @@ describe('transformer @auth migration test', () => {
}
`;

queryResult = await appSyncClientViaApiKey.query({
query: gql(postPublicsQuery),
fetchPolicy: 'no-cache',
});
queryResult = (await appSyncClientViaApiKey.graphql({ query: postPublicsQuery }, { fetchPolicy: 'no-cache' })) as any;

expect(queryResult.errors).toBeUndefined();
expect(queryResult.data).toBeDefined();
Expand All @@ -252,10 +221,7 @@ describe('transformer @auth migration test', () => {
}
`;

queryResult = await appSyncClientViaUser.query({
query: gql(salaryQuery),
fetchPolicy: 'no-cache',
});
queryResult = (await appSyncClientViaUser.graphql({ query: salaryQuery }, { fetchPolicy: 'no-cache' })) as any;

expect(queryResult.errors).toBeUndefined();
expect(queryResult.data).toBeDefined();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('api directives @function v1 to v2 migration', () => {

let awsconfig = configureAmplify(projectDir);
let apiKey = getApiKey(projectDir);
let appSyncClient = getConfiguredAppsyncClientAPIKeyAuth(awsconfig.aws_appsync_graphqlEndpoint, awsconfig.aws_appsync_region, apiKey);
let appSyncClient = getConfiguredAppsyncClientAPIKeyAuth(awsconfig, apiKey);

await testQueries(testModule, appSyncClient);

Expand All @@ -62,7 +62,7 @@ describe('api directives @function v1 to v2 migration', () => {

awsconfig = configureAmplify(projectDir);
apiKey = getApiKey(projectDir);
appSyncClient = getConfiguredAppsyncClientAPIKeyAuth(awsconfig.aws_appsync_graphqlEndpoint, awsconfig.aws_appsync_region, apiKey);
appSyncClient = getConfiguredAppsyncClientAPIKeyAuth(awsconfig, apiKey);

await testQueries(testModule, appSyncClient);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export async function runTest(projectDir: string, testModule: any) {

const awsconfig = configureAmplify(projectDir);
const apiKey = getApiKey(projectDir);
const appSyncClient = getConfiguredAppsyncClientAPIKeyAuth(awsconfig.aws_appsync_graphqlEndpoint, awsconfig.aws_appsync_region, apiKey);
const appSyncClient = getConfiguredAppsyncClientAPIKeyAuth(awsconfig, apiKey);

await testMutations(testModule, appSyncClient);
await testQueries(testModule, appSyncClient);
Expand All @@ -47,7 +47,7 @@ export async function runAuthTest(projectDir: string, testModule: any) {
await setupUser(userPoolId, USERNAME, PASSWORD, GROUPNAME);

const user = await signInUser(USERNAME, PASSWORD);
const appSyncClient = getConfiguredAppsyncClientCognitoAuth(awsconfig.aws_appsync_graphqlEndpoint, awsconfig.aws_appsync_region, user);
const appSyncClient = getConfiguredAppsyncClientCognitoAuth(awsconfig, user);

await testMutations(testModule, appSyncClient);
await testQueries(testModule, appSyncClient);
Expand All @@ -71,7 +71,7 @@ export async function runMultiAutTest(projectDir: string, testModule: any) {
await setupUser(userPoolId, USERNAME, PASSWORD, GROUPNAME);

const user = await signInUser(USERNAME, PASSWORD);
const appSyncClient = getConfiguredAppsyncClientCognitoAuth(awsconfig.aws_appsync_graphqlEndpoint, awsconfig.aws_appsync_region, user);
const appSyncClient = getConfiguredAppsyncClientCognitoAuth(awsconfig, user);

await testMutations(testModule, appSyncClient);
await testQueries(testModule, appSyncClient);
Expand Down