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

Commit c8cdb30

Browse files
authored
Revert "Revert "Enable custom service endpoints for sample application""
1 parent dc94029 commit c8cdb30

File tree

7 files changed

+88
-31
lines changed

7 files changed

+88
-31
lines changed

.gitignore

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

1213
# IDEs and editors
1314
/.idea

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,18 @@ eb deploy
9494
eb open
9595
```
9696

97+
## Local Testing
9798

99+
This section contains instructions on how to test the application locally (using mocked services instead of the real AWS services).
100+
101+
### LocalStack
102+
103+
To test this application using [LocalStack](https://github.com/localstack/localstack), you can use the `awslocal` CLI (https://github.com/localstack/awscli-local).
104+
```
105+
pip install awscli-local
106+
```
107+
Simply parameterize the `./createResources.sh` installation script with `aws_cmd=awslocal`:
108+
```
109+
cd aws; aws_cmd=awslocal ./createResources.sh
110+
```
111+
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

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

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

@@ -24,64 +26,63 @@ IDENTITY_POOL_ID=""
2426
USER_POOL_ID=""
2527
USER_POOL_CLIENT_ID=""
2628

27-
2829
createCognitoResources() {
2930
# Create a Cognito Identity and Set roles
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
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
3132
IDENTITY_POOL_ID=$(cat /tmp/poolId)
3233
echo "Created an identity pool with id of " $IDENTITY_POOL_ID
3334

3435
# Create an IAM role for unauthenticated users
3536
cat unauthrole-trust-policy.json | sed 's/IDENTITY_POOL/'$IDENTITY_POOL_ID'/' > /tmp/unauthrole-trust-policy.json
36-
aws iam create-role --role-name $ROLE_NAME_PREFIX-unauthenticated-role --assume-role-policy-document file:///tmp/unauthrole-trust-policy.json > /tmp/iamUnauthRole
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
3738
if [ $? -eq 0 ]
3839
then
3940
echo "IAM unauthenticated role successfully created"
4041
else
4142
echo "Using the existing role ..."
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
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
4445
fi
45-
aws iam put-role-policy --role-name $ROLE_NAME_PREFIX-unauthenticated-role --policy-name CognitoPolicy --policy-document file://unauthrole.json
46+
$aws_cmd iam put-role-policy --role-name $ROLE_NAME_PREFIX-unauthenticated-role --policy-name CognitoPolicy --policy-document file://unauthrole.json
4647

4748
# Create an IAM role for authenticated users
4849
cat authrole-trust-policy.json | sed 's/IDENTITY_POOL/'$IDENTITY_POOL_ID'/' > /tmp/authrole-trust-policy.json
49-
aws iam create-role --role-name $ROLE_NAME_PREFIX-authenticated-role --assume-role-policy-document file:///tmp/authrole-trust-policy.json > /tmp/iamAuthRole
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
5051
if [ $? -eq 0 ]
5152
then
5253
echo "IAM authenticated role successfully created"
5354
else
5455
echo "Using the existing role ..."
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
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
5758
fi
5859
cat authrole.json | sed 's~DDB_TABLE_ARN~'$DDB_TABLE_ARN'~' > /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
60+
$aws_cmd iam put-role-policy --role-name $ROLE_NAME_PREFIX-authenticated-role --policy-name CognitoPolicy --policy-document file:///tmp/authrole.json
6061

6162
# Create the 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
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
6364
USER_POOL_ID=$(grep -E '"Id":' /tmp/$POOL_NAME-create-user-pool | awk -F'"' '{print $4}')
6465
echo "Created user pool with an id of " $USER_POOL_ID
6566

6667
# Create the 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
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
6869
USER_POOL_CLIENT_ID=$(grep -E '"ClientId":' /tmp/$POOL_NAME-create-user-pool-client | awk -F'"' '{print $4}')
6970
echo "Created user pool client with id of " $USER_POOL_CLIENT_ID
7071

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

7677
# Update cognito identity with the roles
7778
UNAUTH_ROLE_ARN=$(perl -nle 'print $& if m{"Arn":\s*"\K([^"]*)}' /tmp/iamUnauthRole | awk -F'"' '{print $1}')
7879
AUTH_ROLE_ARN=$(perl -nle 'print $& if m{"Arn":\s*"\K([^"]*)}' /tmp/iamAuthRole | awk -F'"' '{print $1}')
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
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
8081
}
8182

8283
createDDBTable() {
8384
# Create DDB Table
84-
aws dynamodb create-table \
85+
$aws_cmd dynamodb create-table \
8586
--table-name $TABLE_NAME \
8687
--attribute-definitions \
8788
AttributeName=userId,AttributeType=S \
@@ -96,7 +97,7 @@ createDDBTable() {
9697
echo "DynamoDB table successfully created"
9798
else
9899
echo "Using the existing table ..."
99-
aws dynamodb describe-table --table-name $TABLE_NAME > /tmp/dynamoTable
100+
$aws_cmd dynamodb describe-table --table-name $TABLE_NAME > /tmp/dynamoTable
100101
fi
101102

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

129130
createS3Bucket() {
130131
# Create the bucket
131-
aws s3 mb s3://$BUCKET_NAME/ --region $REGION 2>/tmp/s3-mb-status
132+
$aws_cmd s3 mb s3://$BUCKET_NAME/ --region $REGION 2>/tmp/s3-mb-status
132133
status=$?
133134

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

153154
uploadS3Bucket() {
154155
# Add the ‘website’ configuration and bucket policy
155-
aws s3 website s3://$BUCKET_NAME/ --index-document index.html --error-document index.html --region $REGION
156+
$aws_cmd s3 website s3://$BUCKET_NAME/ --index-document index.html --error-document index.html --region $REGION
156157
cat s3-bucket-policy.json | sed 's/BUCKET_NAME/'$BUCKET_NAME'/' > /tmp/s3-bucket-policy.json
157-
aws s3api put-bucket-policy --bucket $BUCKET_NAME --policy file:///tmp/s3-bucket-policy.json --region $REGION
158+
$aws_cmd s3api put-bucket-policy --bucket $BUCKET_NAME --policy file:///tmp/s3-bucket-policy.json --region $REGION
158159
#Build the project and sync it up to the bucket
159160
if [ ! -d "$NPM_DIR" ]; then
160161
npm install
161162
fi
162163
cd ..
163164
echo "Building the project"
164-
ng build
165+
ng build $( if [ "$aws_cmd" == "awslocal" ]; then echo "--base-href /$BUCKET_NAME/"; fi )
165166
cd -
166167
echo "Syncing files to the S3 bucket from " $ROOT_DIR/dist/
167-
aws s3 sync $ROOT_DIR/dist/ s3://$BUCKET_NAME/ --region $REGION
168+
$aws_cmd s3 sync $ROOT_DIR/dist/ s3://$BUCKET_NAME/ --region $REGION
168169
}
169170

170171
printConfig() {
@@ -207,7 +208,13 @@ export const environment = {
207208
albumName: "usercontent",
208209
bucketRegion: '$REGION',
209210
210-
ddbTableName: '$TABLE_NAME'
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 )'
211218
};
212219
213220
EOF
@@ -228,7 +235,13 @@ export const environment = {
228235
albumName: "usercontent",
229236
bucketRegion: '$REGION',
230237
231-
ddbTableName: '$TABLE_NAME'
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 )'
232245
};
233246
234247
EOF

src/app/service/cognito.service.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ 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";
1112
import * as CognitoIdentity from "aws-sdk/clients/cognitoidentity";
1213

1314

@@ -37,14 +38,17 @@ export class CognitoUtil {
3738
public static _USER_POOL_ID = environment.userPoolId;
3839
public static _CLIENT_ID = environment.clientId;
3940

40-
public static _POOL_DATA = {
41+
public static _POOL_DATA:any = {
4142
UserPoolId: CognitoUtil._USER_POOL_ID,
4243
ClientId: CognitoUtil._CLIENT_ID
4344
};
4445

4546
public cognitoCreds: AWS.CognitoIdentityCredentials;
4647

4748
getUserPool() {
49+
if (environment.cognito_idp_endpoint) {
50+
CognitoUtil._POOL_DATA.endpoint = environment.cognito_idp_endpoint;
51+
}
4852
return new CognitoUserPool(CognitoUtil._POOL_DATA);
4953
}
5054

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

7276
buildCognitoCreds(idTokenJwt: string) {
7377
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+
}
7481
let logins: CognitoIdentity.LoginsMap = {};
7582
logins[url] = idTokenJwt;
7683
let params = {
7784
IdentityPoolId: CognitoUtil._IDENTITY_POOL_ID, /* required */
7885
Logins: logins
7986
};
80-
let creds = new AWS.CognitoIdentityCredentials(params);
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);
8192
this.setCognitoCreds(creds);
8293
return creds;
8394
}

src/app/service/ddb.service.ts

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

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

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

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

6776
// Write the item to the table
6877
var itemParams =

src/app/service/s3.service.ts

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

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

2832
return s3
2933
}

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

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

0 commit comments

Comments
 (0)