forked from 9652040795/aws-policies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkms-encryption
151 lines (98 loc) · 6.1 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
#bin/bash
# Purpose: KMS Encryption Decryption
docker network ls
docker network create cloudelligent
# 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 cloudelligent -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 cloudelligent \
--link=mysql:db \
-v ~/cloudelligent.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 cloudelligent
# 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 cloudelligent -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 cloudelligent \
--link=mysql:db \
-v ~/cloudelligent.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 "