forked from 9652040795/aws-policies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkms-encryption
280 lines (156 loc) · 10.9 KB
/
kms-encryption
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
#bin/bash
# Purpose: KMS Encryption Decryption
docker network ls
docker network create cloudgeeks.ca
# Secrets with AWS Encryption
MYSQL_USER=`aws kms decrypt --region us-west-2 --ciphertext-blob fileb:///root/secrets/username.txt --output text --query Plaintext | base64 --decode`
MYSQL_PASSWORD=`aws kms decrypt --region us-west-2 --ciphertext-blob fileb:///root/secrets/password.txt --output text --query Plaintext | base64 --decode`
MYSQL_DATABASE=`aws kms decrypt --region us-west-2 --ciphertext-blob fileb:///root/secrets/databasename.txt --output text --query Plaintext | base64 --decode`
# Application
docker run --name mysql --network cloudgeeks.ca -v ~/mysql:/var/lib/mysql -e MYSQL_USER="$MYSQL_USER" -e MYSQL_PASSWORD="$MYSQL_PASSWORD" -e MYSQL_ROOT_PASSWORD="$MYSQL_USER" -e MYSQL_DATABASE="MYSQL_DATABASE" --restart unless-stopped -d mysql:5.7
docker run --name wordpress \
--network cloudgeeks.ca \
--link=mysql:db \
-v ~/cloudgeeks.ca.com:/var/www/html \
--restart unless-stopped \
-p 80:80 -d wordpress
#END
### Encryption ###
#1. aws kms list-keys --region us-west-2
#2. aws kms encrypt --region us-west-2 --key-id 3b7a126e-db34-483b-a249-d985389e06a3 --plaintext "asim" --output text --query CiphertextBlob | base64 --decode > username.txt
#3. aws kms decrypt --region us-west-2 --ciphertext-blob fileb://username.txt --output text --query Plaintext | base64 --decode
#bin/bash
# Purpose: KMS Encryption Decryption
docker network ls
docker network create cloudgeeks.ca
# Secrets with AWS Encryption
MYSQL_USER=`aws kms decrypt --region us-west-2 --ciphertext-blob fileb:///root/secrets/username.txt --output text --query Plaintext | base64 --decode`
MYSQL_PASSWORD=`aws kms decrypt --region us-west-2 --ciphertext-blob fileb:///root/secrets/password.txt --output text --query Plaintext | base64 --decode`
MYSQL_DATABASE=`aws kms decrypt --region us-west-2 --ciphertext-blob fileb:///root/secrets/databasename.txt --output text --query Plaintext | base64 --decode`
# Application
docker run --name mysql --network cloudgeeks.ca -v ~/mysql:/var/lib/mysql -e MYSQL_USER="$MYSQL_USER" -e MYSQL_PASSWORD="$MYSQL_PASSWORD" -e MYSQL_ROOT_PASSWORD="$MYSQL_USER" -e MYSQL_DATABASE="MYSQL_DATABASE" --restart unless-stopped -d mysql:5.7
docker run --name wordpress \
--network cloudgeeks.ca \
--link=mysql:db \
-v ~/cloudgeeks.ca.com:/var/www/html \
--restart unless-stopped \
-p 80:80 -d wordpress
#END
### Encryption ###
#1. aws kms list-keys --region us-west-2
#2. aws kms encrypt --region us-west-2 --key-id 3b7a126e-db34-483b-a249-d985389e06a3 --plaintext "asim" --output text --query CiphertextBlob | base64 --decode > username.txt
#3. aws kms decrypt --region us-west-2 --ciphertext-blob fileb://username.txt --output text --query Plaintext | base64 --decode
# KMS Policy
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"kms:DescribeCustomKeyStores",
"kms:ListKeys",
"kms:DeleteCustomKeyStore",
"kms:GenerateRandom",
"kms:UpdateCustomKeyStore",
"kms:ListAliases",
"kms:DisconnectCustomKeyStore",
"kms:CreateKey",
"kms:ConnectCustomKeyStore",
"kms:CreateCustomKeyStore"
],
"Resource": "*"
},
{
"Sid": "VisualEditor1",
"Effect": "Allow",
"Action": "kms:*",
"Resource": [
"arn:aws:kms:*:*:alias/*",
"arn:aws:kms:us-west-2:898668804275:key/YOURKMSKEYID"
]
}
]
}
############################################## Script for server only ###########################################################
#!/bin/bash
mkdir -p /root/secrets
# Encryption
aws kms encrypt --region us-east-1 --key-id baa7e043-02e1-4a87-8ece-4b13d957d3dd --plaintext 12345678 --output text --query CiphertextBlob | base64 --decode > /root/secrets/mariadb-root-password.txt
aws kms encrypt --region us-east-1 --key-id baa7e043-02e1-4a87-8ece-4b13d957d3dd --plaintext bitnami_wordpress --output text --query CiphertextBlob | base64 --decode > /root/secrets/mariadb-database.txt
aws kms encrypt --region us-east-1 --key-id baa7e043-02e1-4a87-8ece-4b13d957d3dd --plaintext asim.arain --output text --query CiphertextBlob | base64 --decode > /root/secrets/mariadb-user.txt
aws kms encrypt --region us-east-1 --key-id baa7e043-02e1-4a87-8ece-4b13d957d3dd --plaintext 12345678 --output text --query CiphertextBlob | base64 --decode > /root/secrets/mariadb-password.txt
# Decryption
aws kms decrypt --region us-east-1 --ciphertext-blob fileb:///root/secrets/mariadb-root-password.txt --output text --query Plaintext | base64 --decode
aws kms decrypt --region us-east-1 --ciphertext-blob fileb:///root/secrets/mariadb-database.txt --output text --query Plaintext | base64 --decode
aws kms decrypt --region us-east-1 --ciphertext-blob fileb:///root/secrets/mariadb-user.txt --output text --query Plaintext | base64 --decode
aws kms decrypt --region us-east-1 --ciphertext-blob fileb:///root/secrets/mariadb-password.txt --output text --query Plaintext | base64 --decode
#END
MARIADB_ROOT_PASSWORD=`aws kms decrypt --region us-east-1 --ciphertext-blob fileb:///root/secrets/mariadb-root-password.txt --output text --query Plaintext | base64 --decode`
MARIADB_DATABASE=`aws kms decrypt --region us-east-1 --ciphertext-blob fileb:///root/secrets/mariadb-database.txt --output text --query Plaintext | base64 --decode`
MARIADB_USER=`aws kms decrypt --region us-east-1 --ciphertext-blob fileb:///root/secrets/mariadb-user.txt --output text --query Plaintext | base64 --decode`
MARIADB_PASSWORD=`aws kms decrypt --region us-east-1 --ciphertext-blob fileb:///root/secrets/mariadb-password.txt --output text --query Plaintext | base64 --decode`
echo "\n"
echo "Credentials Decrypted"
echo " This is Mariadb root password = $MARIADB_ROOT_PASSWORD "
echo " This is Mariadb database name = $MARIADB_DATABASE "
echo " This is Mariadb User = $MARIADB_USER "
echo " This is Mariadb password = $MARIADB_PASSWORD "
####################################################################################################################################################################
# Note: Mention below data is just a sample data, no actual KMS keys involved.
# Steps to encrypt private key via kms
# KMS Key Creation from console
1. Create a Customer managed key
2. Grab the Key ID: 7702849e-2bc0-4697-ad62-303523cacd9a
3. Use this Key ID with region details in mentioned below examples.
# Example 1
# Encryption ### ### File ### ---> size 4Kb
# Private key Encrypion
aws kms encrypt --region us-east-1 --key-id 7702849e-2bc0-4697-ad62-303523cacd9a --plaintext fileb://id_rsa --output text --query CiphertextBlob | base64 --decode > id_rsa_encrypted
# Private Key Decryption
aws kms decrypt --region us-east-1 --ciphertext-blob fileb://id_rsa_encrypted --output text --query Plaintext | base64 --decode > id_rsa
# Example 2
### Encryption ### ### Plain Text ###
# Steps to encrypt secrets via kms
# 1. aws kms list-keys --region us-east-1
# Encryption
# 2.
aws kms encrypt --region us-east-1 --key-id 7702849e-2bc0-4697-ad62-303523cacd9a --plaintext "abc" --output text --query CiphertextBlob | base64 --decode > password.txt
aws kms encrypt --region us-east-1 --key-id 7702849e-2bc0-4697-ad62-303523cacd9a --plaintext "reptrak" --output text --query CiphertextBlob | base64 --decode > databasename.txt
# Decryption
# 3.
aws kms decrypt --region us-east-1 --ciphertext-blob fileb://password.txt --output text --query Plaintext | base64 --decode
aws kms decrypt --region us-east-1 --ciphertext-blob fileb://databasename.txt --output text --query Plaintext | base64 --decode
# How to use KMS Encryption in docker ?
# Demo application docker
#!/bin/bash
mkdir -p /root/secrets
# Encryption
aws kms encrypt --region us-east-1 --key-id baa7e043-02e1-4a87-8ece-4b13d957d3dd --plaintext 12345678 --output text --query CiphertextBlob | base64 --decode > /root/secrets/mariadb-root-password.txt
aws kms encrypt --region us-east-1 --key-id baa7e043-02e1-4a87-8ece-4b13d957d3dd --plaintext bitnami_wordpress --output text --query CiphertextBlob | base64 --decode > /root/secrets/mariadb-database.txt
aws kms encrypt --region us-east-1 --key-id baa7e043-02e1-4a87-8ece-4b13d957d3dd --plaintext asim.arain --output text --query CiphertextBlob | base64 --decode > /root/secrets/mariadb-user.txt
aws kms encrypt --region us-east-1 --key-id baa7e043-02e1-4a87-8ece-4b13d957d3dd --plaintext 12345678 --output text --query CiphertextBlob | base64 --decode > /root/secrets/mariadb-password.txt
# Decryption
aws kms decrypt --region us-east-1 --ciphertext-blob fileb:///root/secrets/mariadb-root-password.txt --output text --query Plaintext | base64 --decode
aws kms decrypt --region us-east-1 --ciphertext-blob fileb:///root/secrets/mariadb-database.txt --output text --query Plaintext | base64 --decode
aws kms decrypt --region us-east-1 --ciphertext-blob fileb:///root/secrets/mariadb-user.txt --output text --query Plaintext | base64 --decode
aws kms decrypt --region us-east-1 --ciphertext-blob fileb:///root/secrets/mariadb-password.txt --output text --query Plaintext | base64 --decode
#END
MARIADB_ROOT_PASSWORD=`aws kms decrypt --region us-east-1 --ciphertext-blob fileb:///root/secrets/mariadb-root-password.txt --output text --query Plaintext | base64 --decode`
MARIADB_DATABASE=`aws kms decrypt --region us-east-1 --ciphertext-blob fileb:///root/secrets/mariadb-database.txt --output text --query Plaintext | base64 --decode`
MARIADB_USER=`aws kms decrypt --region us-east-1 --ciphertext-blob fileb:///root/secrets/mariadb-user.txt --output text --query Plaintext | base64 --decode`
MARIADB_PASSWORD=`aws kms decrypt --region us-east-1 --ciphertext-blob fileb:///root/secrets/mariadb-password.txt --output text --query Plaintext | base64 --decode`
echo "\n"
echo "Credentials Decrypted"
echo " This is Mariadb root password = $MARIADB_ROOT_PASSWORD "
echo " This is Mariadb database name = $MARIADB_DATABASE "
echo " This is Mariadb User = $MARIADB_USER "
echo " This is Mariadb password = $MARIADB_PASSWORD "
# Demo Application Wordpress
# Create a network
docker network create bitnami-wordpress-tier
# Mariadb Setup
docker run --name mariadb --user=root -e MARIADB_DATABASE="$MARIADB_DATABASE" -e MARIADB_ROOT_PASSWORD="$MARIADB_ROOT_PASSWORD" -v /root/mariadb:/bitnami --network="bitnami-wordpress-tier" --restart unless-stopped -d bitnami/mariadb:latest
# Wordpress Setup
docker run --name bitnami-wordpress -u root --network="bitnami-wordpress-tier" -e WORDPRESS_DATABASE_NAME="$MARIADB_DATABASE" -e WORDPRESS_DATABASE_USER="root" -e WORDPRESS_DATABASE_PASSWORD="$MARIADB_ROOT_PASSWORD" --link mariadb:db -v /root/wordpress:/bitnami -p 8090:8080 --restart unless-stopped -d bitnami/wordpress:latest
# END