forked from benrady/learnjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sspa
executable file
·285 lines (253 loc) · 7.29 KB
/
sspa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/env bash
abspath="$(cd "${0%/*}" 2>/dev/null; echo "$PWD"/"${0##*/}")"
root_dir=`dirname "$abspath"`
function check_python() {
if ! which python > /dev/null; then
echo "Can't find Python. You need Python 2.7 or later to use this."
exit 1
fi
}
function check_aws() {
if ! which aws > /dev/null; then
echo "Can't find AWS CLI. Install 'awscli' using pip."
exit 1
fi
}
function check_node_deps() {
if ! which npm > /dev/null; then
echo "This action requires Node.js and NPM."
exit 1
fi
}
function dev_server() {
cd public
exec python -m SimpleHTTPServer 9292
popd
}
function create_s3_bucket() {
local bucket_name=$1
local bucket_uri="s3://${bucket_name}"
aws s3 mb $bucket_uri
aws s3 website \
--index-document index.html \
--error-document error.html \
$bucket_uri
local region=$(aws configure get region)
echo "Website endpoint is: http://${1}.s3-website-${region}.amazonaws.com"
}
function deploy_s3_bucket() {
local bucket_uri="s3://$1"
aws s3 sync public/ $bucket_uri --acl public-read
}
function generate_assume_role_policy() {
local identity_pool_arn=$1
cat <<DOC
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Federated": "cognito-identity.amazonaws.com"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"cognito-identity.amazonaws.com:aud": "${identity_pool_arn}"
},
"ForAnyValue:StringLike": {
"cognito-identity.amazonaws.com:amr": "authenticated"
}
}
}
]
}
DOC
}
function generate_table_policy() {
local table_arn=$1
cat <<DOC
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": [
"dynamodb:BatchGetItem",
"dynamodb:BatchWriteItem",
"dynamodb:DeleteItem",
"dynamodb:GetItem",
"dynamodb:PutItem",
"dynamodb:Query",
"dynamodb:UpdateItem"
],
"Resource": ["${table_arn}"],
"Condition": {
"ForAllValues:StringEquals": {
"dynamodb:LeadingKeys": ["\${cognito-identity.amazonaws.com:sub}"]}
}
}]
}
DOC
}
function create_cognito_auth_role() {
local identity_pool_dir=$1
local pool_id=$(support/jsed.py ${identity_pool_dir}/pool_info.json 'IdentityPoolId')
local pool_name=$(support/jsed.py ${identity_pool_dir}/pool_info.json 'IdentityPoolName')
generate_assume_role_policy ${pool_id} > ${identity_pool_dir}/assume_role_policy.json
if [[ ! -s ${identity_pool_dir}/role_info.json ]]; then
local role_name="${pool_name}_cognito_authenticated"
echo "Creaing role: $role_name"
aws iam create-role \
--role-name "$role_name" \
--assume-role-policy-document "file://${identity_pool_dir}/assume_role_policy.json" \
> ${identity_pool_dir}/role_info.json
fi
}
function create_identity_pool() {
local identity_pool_dir=${1%/}
if [[ ! -e ${identity_pool_dir}/config.json ]]; then
echo "Can't find pool config file ${identity_pool_dir}/config.json"
exit 1
fi
local identity_pool_name=$(basename $identity_pool_dir)
if [[ ! -s ${identity_pool_dir}/pool_info.json ]]; then
echo "Creating identity pool: $identity_pool_name"
aws cognito-identity create-identity-pool \
--identity-pool-name $identity_pool_name \
--cli-input-json "file://${identity_pool_dir}/config.json" \
> ${identity_pool_dir}/pool_info.json
fi
create_cognito_auth_role ${identity_pool_dir}
local pool_id=$(support/jsed.py ${identity_pool_dir}/pool_info.json 'IdentityPoolId')
local role_arn=$(support/jsed.py ${identity_pool_dir}/role_info.json 'Role.Arn')
echo "Updating identity pool roles"
aws cognito-identity set-identity-pool-roles \
--identity-pool-id ${pool_id} \
--roles authenticated=${role_arn}
}
function create_table() {
local table_dir=${1%/}
local table_name=$(basename $table_dir)
local role_name="${2}_cognito_authenticated"
if [[ ! -s ${table_dir}/table_info.json ]]; then
aws dynamodb create-table \
--table-name $table_name \
--cli-input-json "file://${table_dir}/config.json" \
> ${table_dir}/table_info.json
fi
local table_arn=$(support/jsed.py ${table_dir}/table_info.json 'TableDescription.TableArn')
generate_table_policy ${table_arn} > ${table_dir}/role_policy.json
aws iam put-role-policy \
--policy-document file://${table_dir}/role_policy.json \
--role-name ${role_name} \
--policy-name ${table_name}_table_access
}
function build_bundle() {
check_node_deps
pushd services
rm archive.zip
npm install || exit 1
mkdir -p dist
cp -r node_modules dist/
cp -r lib/* dist/
zip -r archive.zip dist/*
rm -rf dist
popd
}
function deploy_bundle {
pushd services
node ./lib/deploy.js prod
popd
}
function test_services() {
check_node_deps
cd services
if [[ ! -x node_modules/.bin/jasmine ]]; then
npm install || exit 1
fi
export PATH=node_modules/.bin:${PATH}
NODE_PATH=./lib jasmine
cd ..
}
function help() {
echo "#################################################"
echo "# Serverless Single Page Apps - The Bash Helper #"
echo "#################################################"
echo
echo "Usage: sspa <action> [argument]"
echo
echo "Where <action> is one of:"
echo " server - Run a local development server"
echo " create_bucket <bucket name> - Create a web-accessible bucket in S3"
echo " deploy_bucket <bucket name> - Deploy the application to S3 bucket"
echo " create_pool <config dir> - Create a new Cognito identity pool"
echo " create_table <config dir> <pool_name> - Create a new DynamoDB table"
echo " test - Run Lambda service automated tests"
echo " build_bundle - Build the Lambda service bundle"
echo " deploy_bundle - Deploy the Lambda service bundle"
echo
echo "Examples:"
echo
echo "Deploy the web app to an S3 bucket:"
echo " $ sspa deploy_public learnjs.benrady.com"
echo
echo "Create the 'learnjs' identity pool"
echo " $ sspa create_pool conf/cognito/identity_pools/learnjs"
echo
echo "Create a 'learnjs' table accessible by users in the 'learnjs' identity pool"
echo " $ sspa create_pool conf/cognito/identity_pools/learnjs learnjs"
}
action=${1:-"help"}
cd "$root_dir"
check_python
case "$action" in
server)
dev_server
;;
test)
test_services
;;
build_bundle)
build_bundle
;;
deploy_bundle)
build_bundle
deploy_bundle
;;
create_bucket)
if [[ $# -eq 2 ]]; then
create_s3_bucket ${2}
else
echo "Please specify a bucket name"
exit 1
fi
;;
deploy_bucket)
if [[ $# -eq 2 ]]; then
deploy_s3_bucket ${2}
else
echo "Please specify an S3 bucket name"
exit 1
fi
;;
create_pool)
if [[ $# -eq 2 ]]; then
create_identity_pool ${2}
else
echo "Please specify a Cognito identity pool configuration directory"
exit 1
fi
;;
create_table)
if [[ $# -eq 3 ]]; then
create_table ${2} ${3}
else
echo "Please specify a DynamoDB table configuration directory and identity pool name"
exit 1
fi
;;
*)
help
;;
esac