Skip to content
This repository was archived by the owner on Jun 26, 2020. It is now read-only.

Commit 68635cb

Browse files
authored
Merge pull request #92 from awslabs/revert-87-enable-local-endpoints
Revert "Enable custom service endpoints for sample application"
2 parents c33a1ea + 79557ca commit 68635cb

File tree

7 files changed

+31
-88
lines changed

7 files changed

+31
-88
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
/node_modules
99
/src/bower_components
1010
/public/bower_components
11-
package-lock.json
1211

1312
# IDEs and editors
1413
/.idea

README.md

-14
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,4 @@ eb deploy
8989
eb open
9090
```
9191

92-
## Local Testing
9392

94-
This section contains instructions on how to test the application locally (using mocked services instead of the real AWS services).
95-
96-
### LocalStack
97-
98-
To test this application using [LocalStack](https://github.com/localstack/localstack), you can use the `awslocal` CLI (https://github.com/localstack/awscli-local).
99-
```
100-
pip install awscli-local
101-
```
102-
Simply parameterize the `./createResources.sh` installation script with `aws_cmd=awslocal`:
103-
```
104-
cd aws; aws_cmd=awslocal ./createResources.sh
105-
```
106-
Once the code is deployed to the local S3 server, the application is accessible via http://localhost:4572/cognitosample-localapp/index.html (Assuming "localapp" has been chosen as resource name in the previous step)

aws/createResources.sh

+23-36
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#!/usr/bin/env bash
22

3-
aws_cmd=${aws_cmd:-aws}
4-
53
# Bucket name must be all lowercase, and start/end with lowecase letter or number
64
# $(echo...) code to work with versions of bash older than 4.0
75

@@ -26,63 +24,64 @@ IDENTITY_POOL_ID=""
2624
USER_POOL_ID=""
2725
USER_POOL_CLIENT_ID=""
2826

27+
2928
createCognitoResources() {
3029
# Create a Cognito Identity and Set roles
31-
$aws_cmd cognito-identity create-identity-pool --identity-pool-name $IDENTITY_POOL_NAME --allow-unauthenticated-identities --region $REGION| grep IdentityPoolId | awk '{print $2}' | xargs |sed -e 's/^"//' -e 's/"$//' -e 's/,$//' > /tmp/poolId
30+
aws cognito-identity create-identity-pool --identity-pool-name $IDENTITY_POOL_NAME --allow-unauthenticated-identities --region $REGION| grep IdentityPoolId | awk '{print $2}' | xargs |sed -e 's/^"//' -e 's/"$//' -e 's/,$//' > /tmp/poolId
3231
IDENTITY_POOL_ID=$(cat /tmp/poolId)
3332
echo "Created an identity pool with id of " $IDENTITY_POOL_ID
3433

3534
# Create an IAM role for unauthenticated users
3635
cat unauthrole-trust-policy.json | sed 's/IDENTITY_POOL/'$IDENTITY_POOL_ID'/' > /tmp/unauthrole-trust-policy.json
37-
$aws_cmd iam create-role --role-name $ROLE_NAME_PREFIX-unauthenticated-role --assume-role-policy-document file:///tmp/unauthrole-trust-policy.json > /tmp/iamUnauthRole
36+
aws iam create-role --role-name $ROLE_NAME_PREFIX-unauthenticated-role --assume-role-policy-document file:///tmp/unauthrole-trust-policy.json > /tmp/iamUnauthRole
3837
if [ $? -eq 0 ]
3938
then
4039
echo "IAM unauthenticated role successfully created"
4140
else
4241
echo "Using the existing role ..."
43-
$aws_cmd iam get-role --role-name $ROLE_NAME_PREFIX-unauthenticated-role > /tmp/iamUnauthRole
44-
$aws_cmd iam update-assume-role-policy --role-name $ROLE_NAME_PREFIX-unauthenticated-role --policy-document file:///tmp/unauthrole-trust-policy.json
42+
aws iam get-role --role-name $ROLE_NAME_PREFIX-unauthenticated-role > /tmp/iamUnauthRole
43+
aws iam update-assume-role-policy --role-name $ROLE_NAME_PREFIX-unauthenticated-role --policy-document file:///tmp/unauthrole-trust-policy.json
4544
fi
46-
$aws_cmd iam put-role-policy --role-name $ROLE_NAME_PREFIX-unauthenticated-role --policy-name CognitoPolicy --policy-document file://unauthrole.json
45+
aws iam put-role-policy --role-name $ROLE_NAME_PREFIX-unauthenticated-role --policy-name CognitoPolicy --policy-document file://unauthrole.json
4746

4847
# Create an IAM role for authenticated users
4948
cat authrole-trust-policy.json | sed 's/IDENTITY_POOL/'$IDENTITY_POOL_ID'/' > /tmp/authrole-trust-policy.json
50-
$aws_cmd iam create-role --role-name $ROLE_NAME_PREFIX-authenticated-role --assume-role-policy-document file:///tmp/authrole-trust-policy.json > /tmp/iamAuthRole
49+
aws iam create-role --role-name $ROLE_NAME_PREFIX-authenticated-role --assume-role-policy-document file:///tmp/authrole-trust-policy.json > /tmp/iamAuthRole
5150
if [ $? -eq 0 ]
5251
then
5352
echo "IAM authenticated role successfully created"
5453
else
5554
echo "Using the existing role ..."
56-
$aws_cmd iam get-role --role-name $ROLE_NAME_PREFIX-authenticated-role > /tmp/iamAuthRole
57-
$aws_cmd iam update-assume-role-policy --role-name $ROLE_NAME_PREFIX-authenticated-role --policy-document file:///tmp/authrole-trust-policy.json
55+
aws iam get-role --role-name $ROLE_NAME_PREFIX-authenticated-role > /tmp/iamAuthRole
56+
aws iam update-assume-role-policy --role-name $ROLE_NAME_PREFIX-authenticated-role --policy-document file:///tmp/authrole-trust-policy.json
5857
fi
5958
cat authrole.json | sed 's~DDB_TABLE_ARN~'$DDB_TABLE_ARN'~' > /tmp/authrole.json
60-
$aws_cmd iam put-role-policy --role-name $ROLE_NAME_PREFIX-authenticated-role --policy-name CognitoPolicy --policy-document file:///tmp/authrole.json
59+
aws iam put-role-policy --role-name $ROLE_NAME_PREFIX-authenticated-role --policy-name CognitoPolicy --policy-document file:///tmp/authrole.json
6160

6261
# Create the user pool
63-
$aws_cmd cognito-idp create-user-pool --pool-name $POOL_NAME --auto-verified-attributes email --policies file://user-pool-policy.json --region $REGION > /tmp/$POOL_NAME-create-user-pool
62+
aws cognito-idp create-user-pool --pool-name $POOL_NAME --auto-verified-attributes email --policies file://user-pool-policy.json --region $REGION > /tmp/$POOL_NAME-create-user-pool
6463
USER_POOL_ID=$(grep -E '"Id":' /tmp/$POOL_NAME-create-user-pool | awk -F'"' '{print $4}')
6564
echo "Created user pool with an id of " $USER_POOL_ID
6665

6766
# Create the user pool client
68-
$aws_cmd cognito-idp create-user-pool-client --user-pool-id $USER_POOL_ID --no-generate-secret --client-name webapp --region $REGION > /tmp/$POOL_NAME-create-user-pool-client
67+
aws cognito-idp create-user-pool-client --user-pool-id $USER_POOL_ID --no-generate-secret --client-name webapp --region $REGION > /tmp/$POOL_NAME-create-user-pool-client
6968
USER_POOL_CLIENT_ID=$(grep -E '"ClientId":' /tmp/$POOL_NAME-create-user-pool-client | awk -F'"' '{print $4}')
7069
echo "Created user pool client with id of " $USER_POOL_CLIENT_ID
7170

7271
# Add the user pool and user pool client id to the identity pool
73-
$aws_cmd cognito-identity update-identity-pool --allow-unauthenticated-identities --identity-pool-id $IDENTITY_POOL_ID --identity-pool-name $IDENTITY_POOL_NAME \
72+
aws cognito-identity update-identity-pool --allow-unauthenticated-identities --identity-pool-id $IDENTITY_POOL_ID --identity-pool-name $IDENTITY_POOL_NAME \
7473
--cognito-identity-providers ProviderName=cognito-idp.$REGION.amazonaws.com/$USER_POOL_ID,ClientId=$USER_POOL_CLIENT_ID --region $REGION \
7574
> /tmp/$IDENTITY_POOL_ID-add-user-pool
7675

7776
# Update cognito identity with the roles
7877
UNAUTH_ROLE_ARN=$(perl -nle 'print $& if m{"Arn":\s*"\K([^"]*)}' /tmp/iamUnauthRole | awk -F'"' '{print $1}')
7978
AUTH_ROLE_ARN=$(perl -nle 'print $& if m{"Arn":\s*"\K([^"]*)}' /tmp/iamAuthRole | awk -F'"' '{print $1}')
80-
$aws_cmd cognito-identity set-identity-pool-roles --identity-pool-id $IDENTITY_POOL_ID --roles authenticated=$AUTH_ROLE_ARN,unauthenticated=$UNAUTH_ROLE_ARN --region $REGION
79+
aws cognito-identity set-identity-pool-roles --identity-pool-id $IDENTITY_POOL_ID --roles authenticated=$AUTH_ROLE_ARN,unauthenticated=$UNAUTH_ROLE_ARN --region $REGION
8180
}
8281

8382
createDDBTable() {
8483
# Create DDB Table
85-
$aws_cmd dynamodb create-table \
84+
aws dynamodb create-table \
8685
--table-name $TABLE_NAME \
8786
--attribute-definitions \
8887
AttributeName=userId,AttributeType=S \
@@ -97,7 +96,7 @@ createDDBTable() {
9796
echo "DynamoDB table successfully created"
9897
else
9998
echo "Using the existing table ..."
100-
$aws_cmd dynamodb describe-table --table-name $TABLE_NAME > /tmp/dynamoTable
99+
aws dynamodb describe-table --table-name $TABLE_NAME > /tmp/dynamoTable
101100
fi
102101

103102
DDB_TABLE_ARN=$(perl -nle 'print $& if m{"TableArn":\s*"\K([^"]*)}' /tmp/dynamoTable | awk -F'"' '{print $1}')
@@ -129,7 +128,7 @@ EOT
129128

130129
createS3Bucket() {
131130
# Create the bucket
132-
$aws_cmd s3 mb s3://$BUCKET_NAME/ --region $REGION 2>/tmp/s3-mb-status
131+
aws s3 mb s3://$BUCKET_NAME/ --region $REGION 2>/tmp/s3-mb-status
133132
status=$?
134133

135134
if [ $status -eq 0 ]
@@ -153,19 +152,19 @@ createS3Bucket() {
153152

154153
uploadS3Bucket() {
155154
# Add the ‘website’ configuration and bucket policy
156-
$aws_cmd s3 website s3://$BUCKET_NAME/ --index-document index.html --error-document index.html --region $REGION
155+
aws s3 website s3://$BUCKET_NAME/ --index-document index.html --error-document index.html --region $REGION
157156
cat s3-bucket-policy.json | sed 's/BUCKET_NAME/'$BUCKET_NAME'/' > /tmp/s3-bucket-policy.json
158-
$aws_cmd s3api put-bucket-policy --bucket $BUCKET_NAME --policy file:///tmp/s3-bucket-policy.json --region $REGION
157+
aws s3api put-bucket-policy --bucket $BUCKET_NAME --policy file:///tmp/s3-bucket-policy.json --region $REGION
159158
#Build the project and sync it up to the bucket
160159
if [ ! -d "$NPM_DIR" ]; then
161160
npm install
162161
fi
163162
cd ..
164163
echo "Building the project"
165-
ng build $( if [ "$aws_cmd" == "awslocal" ]; then echo "--base-href /$BUCKET_NAME/"; fi )
164+
ng build
166165
cd -
167166
echo "Syncing files to the S3 bucket from " $ROOT_DIR/dist/
168-
$aws_cmd s3 sync $ROOT_DIR/dist/ s3://$BUCKET_NAME/ --region $REGION
167+
aws s3 sync $ROOT_DIR/dist/ s3://$BUCKET_NAME/ --region $REGION
169168
}
170169

171170
printConfig() {
@@ -208,13 +207,7 @@ export const environment = {
208207
albumName: "usercontent",
209208
bucketRegion: '$REGION',
210209
211-
ddbTableName: '$TABLE_NAME',
212-
213-
cognito_idp_endpoint: '$( if [ "$aws_cmd" == "awslocal" ]; then echo 'http://localhost:4590'; fi )',
214-
cognito_identity_endpoint: '$( if [ "$aws_cmd" == "awslocal" ]; then echo 'http://localhost:4591'; fi )',
215-
sts_endpoint: '$( if [ "$aws_cmd" == "awslocal" ]; then echo 'http://localhost:4592'; fi )',
216-
dynamodb_endpoint: '$( if [ "$aws_cmd" == "awslocal" ]; then echo 'http://localhost:4569'; fi )',
217-
s3_endpoint: '$( if [ "$aws_cmd" == "awslocal" ]; then echo 'http://localhost:4572'; fi )'
210+
ddbTableName: '$TABLE_NAME'
218211
};
219212
220213
EOF
@@ -235,13 +228,7 @@ export const environment = {
235228
albumName: "usercontent",
236229
bucketRegion: '$REGION',
237230
238-
ddbTableName: '$TABLE_NAME',
239-
240-
cognito_idp_endpoint: '$( if [ "$aws_cmd" == "awslocal" ]; then echo 'http://localhost:4590'; fi )',
241-
cognito_identity_endpoint: '$( if [ "$aws_cmd" == "awslocal" ]; then echo 'http://localhost:4591'; fi )',
242-
sts_endpoint: '$( if [ "$aws_cmd" == "awslocal" ]; then echo 'http://localhost:4592'; fi )',
243-
dynamodb_endpoint: '$( if [ "$aws_cmd" == "awslocal" ]; then echo 'http://localhost:4569'; fi )',
244-
s3_endpoint: '$( if [ "$aws_cmd" == "awslocal" ]; then echo 'http://localhost:4572'; fi )'
231+
ddbTableName: '$TABLE_NAME'
245232
};
246233
247234
EOF

src/app/service/cognito.service.ts

+2-13
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
CognitoUserPool
99
} from "amazon-cognito-identity-js";
1010
import * as AWS from "aws-sdk/global";
11-
import * as awsservice from "aws-sdk/lib/service";
1211
import * as CognitoIdentity from "aws-sdk/clients/cognitoidentity";
1312

1413

@@ -38,17 +37,14 @@ export class CognitoUtil {
3837
public static _USER_POOL_ID = environment.userPoolId;
3938
public static _CLIENT_ID = environment.clientId;
4039

41-
public static _POOL_DATA:any = {
40+
public static _POOL_DATA = {
4241
UserPoolId: CognitoUtil._USER_POOL_ID,
4342
ClientId: CognitoUtil._CLIENT_ID
4443
};
4544

4645
public cognitoCreds: AWS.CognitoIdentityCredentials;
4746

4847
getUserPool() {
49-
if (environment.cognito_idp_endpoint) {
50-
CognitoUtil._POOL_DATA.endpoint = environment.cognito_idp_endpoint;
51-
}
5248
return new CognitoUserPool(CognitoUtil._POOL_DATA);
5349
}
5450

@@ -75,20 +71,13 @@ export class CognitoUtil {
7571

7672
buildCognitoCreds(idTokenJwt: string) {
7773
let url = 'cognito-idp.' + CognitoUtil._REGION.toLowerCase() + '.amazonaws.com/' + CognitoUtil._USER_POOL_ID;
78-
if (environment.cognito_idp_endpoint) {
79-
url = environment.cognito_idp_endpoint + '/' + CognitoUtil._USER_POOL_ID;
80-
}
8174
let logins: CognitoIdentity.LoginsMap = {};
8275
logins[url] = idTokenJwt;
8376
let params = {
8477
IdentityPoolId: CognitoUtil._IDENTITY_POOL_ID, /* required */
8578
Logins: logins
8679
};
87-
let serviceConfigs : awsservice.ServiceConfigurationOptions = {};
88-
if (environment.cognito_identity_endpoint) {
89-
serviceConfigs.endpoint = environment.cognito_identity_endpoint;
90-
}
91-
let creds = new AWS.CognitoIdentityCredentials(params, serviceConfigs);
80+
let creds = new AWS.CognitoIdentityCredentials(params);
9281
this.setCognitoCreds(creds);
9382
return creds;
9483
}

src/app/service/ddb.service.ts

+3-12
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,7 @@ export class DynamoDBService {
3131
}
3232
};
3333

34-
var clientParams:any = {};
35-
if (environment.dynamodb_endpoint) {
36-
clientParams.endpoint = environment.dynamodb_endpoint;
37-
}
38-
var docClient = new DynamoDB.DocumentClient(clientParams);
34+
var docClient = new DynamoDB.DocumentClient();
3935
docClient.query(params, onQuery);
4036

4137
function onQuery(err, data) {
@@ -64,14 +60,9 @@ export class DynamoDBService {
6460

6561
write(data: string, date: string, type: string): void {
6662
console.log("DynamoDBService: writing " + type + " entry");
67-
68-
let clientParams:any = {
63+
var DDB = new DynamoDB({
6964
params: {TableName: environment.ddbTableName}
70-
};
71-
if (environment.dynamodb_endpoint) {
72-
clientParams.endpoint = environment.dynamodb_endpoint;
73-
}
74-
var DDB = new DynamoDB(clientParams);
65+
});
7566

7667
// Write the item to the table
7768
var itemParams =

src/app/service/s3.service.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,11 @@ export class S3Service {
1919
region: environment.bucketRegion,
2020
});
2121

22-
let clientParams:any = {
22+
var s3 = new S3({
2323
region: environment.bucketRegion,
2424
apiVersion: '2006-03-01',
2525
params: {Bucket: environment.rekognitionBucket}
26-
};
27-
if (environment.s3_endpoint) {
28-
clientParams.endpoint = environment.s3_endpoint;
29-
}
30-
var s3 = new S3(clientParams);
26+
});
3127

3228
return s3
3329
}

src/app/service/user-login.service.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import {environment} from "../../environments/environment";
21
import {Injectable} from "@angular/core";
32
import {DynamoDBService} from "./ddb.service";
43
import {CognitoCallback, CognitoUtil, LoggedInCallback} from "./cognito.service";
@@ -49,11 +48,7 @@ export class UserLoginService {
4948
// If the first SDK call we make wants to use our IdentityID, we have a
5049
// chicken and egg problem on our hands. We resolve this problem by "priming" the AWS SDK by calling a
5150
// very innocuous API call that forces this behavior.
52-
let clientParams:any = {};
53-
if (environment.sts_endpoint) {
54-
clientParams.endpoint = environment.sts_endpoint;
55-
}
56-
let sts = new STS(clientParams);
51+
let sts = new STS();
5752
sts.getCallerIdentity(function (err, data) {
5853
console.log("UserLoginService: Successfully set the AWS credentials");
5954
callback.cognitoCallback(null, result);

0 commit comments

Comments
 (0)