Skip to content

Commit 4ea8e08

Browse files
authored
Credstash has been removed (#10)
1 parent c7aaa62 commit 4ea8e08

File tree

6 files changed

+89
-73
lines changed

6 files changed

+89
-73
lines changed

README.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ This module setups a VPN server for a VPC to connect to instances.
77

88
After provisioning, don't forget to run commands below:
99

10-
* **credstash**
11-
* `export BACKUP_ENCRYPTION_KEY=$(uuidgen)`
12-
* `credstash -r REGION -t CREDSTASH_TABLE_NAME put -k alias/CREDSTASH_TABLE_NAME BACKUP_ENCRYPTION_KEY $BACKUP_ENCRYPTION_KEY`
13-
* `credstash -r REGION -t CREDSTASH_TABLE_NAME put -k alias/CREDSTASH_TABLE_NAME HEALTHCHECKS_IO_KEY CHANGEME-WITH-THE-KEY-FROM-HEALTHCHECKS-IO`
1410
* **Pritunl setup**
1511
* `sudo pritunl setup-key`
1612

@@ -21,8 +17,10 @@ After provisioning, don't forget to run commands below:
2117
* **public_subnet_id:** One of the public subnets to create the instance
2218
* **ami_id:** Amazon Linux AMI ID
2319
* **instance_type:** Instance type of the VPN box (t2.small is mostly enough)
24-
* **office_ip_cidrs:** List of office IP addresses that you can SSH and non-VPN connected users can reach temporary profile download pages
25-
* **tags**: Map of AWS Tag key and values
20+
* **whitelist:** List of office IP addresses that you can SSH and non-VPN connected users can reach temporary profile download pages
21+
* **tags:** Map of AWS Tag key and values
22+
* **resource_name_prefix:** All the resources will be prefixed with the value of this variable
23+
* **healthchecks_io_key:** Health check key for healthchecks.io
2624

2725
# Outputs
2826
* **vpn_instance_private_ip_address:** Private IP address of the instance
@@ -38,14 +36,15 @@ provider "aws" {
3836
}
3937
4038
module "app_pritunl" {
41-
source = "github.com/opsgang/terraform_pritunl?ref=1.1.0"
39+
source = "github.com/opsgang/terraform_pritunl?ref=2.0.0"
4240
4341
aws_key_name = "org-eu-west-2"
4442
vpc_id = "${module.vpc.vpc_id}"
4543
public_subnet_id = "${module.vpc.public_subnets[1]}"
4644
ami_id = "ami-403e2524"
4745
instance_type = "t2.nano"
48-
resource_name_prefix = "agate-pritunl"
46+
resource_name_prefix = "opsgang-pritunl"
47+
healthchecks_io_key = "NNNNNNNN-NNNN-NNNN-NNNN-NNNNNNNNNNN"
4948
5049
whitelist = [
5150
"8.8.8.8/32",

main.tf

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,37 @@
1-
data "aws_region" "current" {
2-
current = true
3-
}
1+
data "aws_region" "current" {}
42

53
data "aws_caller_identity" "current" {}
64

75
data "template_file" "user_data" {
86
template = "${file("${path.module}/templates/user_data.sh.tpl")}"
97

108
vars {
11-
aws_region = "${data.aws_region.current.name}"
12-
s3_backup_bucket = "${var.resource_name_prefix}-backup"
13-
credstash_table_name = "${var.resource_name_prefix}-credstash"
9+
aws_region = "${data.aws_region.current.name}"
10+
s3_backup_bucket = "${var.resource_name_prefix}-backup"
11+
healthchecks_io_key = "/pritunl/${var.resource_name_prefix}/healthchecks-io-key"
1412
}
1513
}
1614

17-
data "template_file" "credstash_policy" {
15+
data "template_file" "kms_policy" {
1816
template = "${file("${path.module}/templates/key_policy.json.tpl")}"
1917

2018
vars {
2119
resource_name_prefix = "${var.resource_name_prefix}"
22-
key_admin_arn = "${aws_iam_role.role.arn}"
2320
account_id = "${data.aws_caller_identity.current.account_id}"
21+
key_admin_arn = "${aws_iam_role.role.arn}"
2422
}
2523
}
2624

2725
data "template_file" "iam_instance_role_policy" {
2826
template = "${file("${path.module}/templates/iam_instance_role_policy.json.tpl")}"
2927

3028
vars {
29+
s3_backup_bucket = "${var.resource_name_prefix}-backup"
3130
resource_name_prefix = "${var.resource_name_prefix}"
32-
db_credstash_arn = "${aws_dynamodb_table.db_credstash.arn}"
33-
}
34-
}
35-
36-
resource "aws_dynamodb_table" "db_credstash" {
37-
name = "${var.resource_name_prefix}-credstash"
38-
read_capacity = 1
39-
write_capacity = 1
40-
hash_key = "name"
41-
range_key = "version"
42-
43-
attribute {
44-
name = "name"
45-
type = "S"
46-
}
47-
48-
attribute {
49-
name = "version"
50-
type = "S"
31+
aws_region = "${data.aws_region.current.name}"
32+
account_id = "${data.aws_caller_identity.current.account_id}"
33+
ssm_key_prefix = "/pritunl/${var.resource_name_prefix}/*"
5134
}
52-
53-
tags = "${
54-
merge(
55-
map("Name", format("%s-%s", var.resource_name_prefix, "credstash")),
56-
var.tags,
57-
)
58-
}"
5935
}
6036

6137
resource "null_resource" "waiter" {
@@ -66,35 +42,61 @@ resource "null_resource" "waiter" {
6642
}
6743
}
6844

69-
resource "aws_kms_key" "credstash" {
45+
resource "aws_kms_key" "parameter_store" {
7046
depends_on = ["null_resource.waiter"]
7147

72-
description = "Credstash space for ${var.resource_name_prefix}"
48+
description = "Parameter store and backup key for ${var.resource_name_prefix}"
7349

74-
policy = "${data.template_file.credstash_policy.rendered}"
75-
deletion_window_in_days = 7
50+
policy = "${data.template_file.kms_policy.rendered}"
51+
deletion_window_in_days = 30
7652
is_enabled = true
7753
enable_key_rotation = true
7854

7955
tags = "${
8056
merge(
81-
map("Name", format("%s-%s", var.resource_name_prefix, "credstash")),
57+
map("Name", format("%s-%s", var.resource_name_prefix, "parameter-store")),
8258
var.tags,
8359
)
8460
}"
8561
}
8662

87-
resource "aws_kms_alias" "credstash" {
88-
depends_on = ["aws_kms_key.credstash"]
63+
resource "aws_kms_alias" "parameter_store" {
64+
depends_on = ["aws_kms_key.parameter_store"]
8965

90-
name = "alias/${var.resource_name_prefix}-credstash"
91-
target_key_id = "${aws_kms_key.credstash.key_id}"
66+
name = "alias/${var.resource_name_prefix}-parameter-store"
67+
target_key_id = "${aws_kms_key.parameter_store.key_id}"
68+
}
69+
70+
resource "aws_ssm_parameter" "healthchecks_io_key" {
71+
name = "/pritunl/${var.resource_name_prefix}/healthchecks-io-key"
72+
type = "SecureString"
73+
value = "${var.healthchecks_io_key}"
74+
key_id = "${aws_kms_key.parameter_store.arn}"
75+
overwrite = true
76+
77+
tags = "${
78+
merge(
79+
map("Name", format("%s/%s/%s", "pritunl", var.resource_name_prefix, "healthchecks-io-key")),
80+
var.tags,
81+
)
82+
}"
9283
}
9384

9485
resource "aws_s3_bucket" "backup" {
86+
depends_on = ["aws_kms_key.parameter_store"]
87+
9588
bucket = "${var.resource_name_prefix}-backup"
9689
acl = "private"
9790

91+
server_side_encryption_configuration {
92+
rule {
93+
apply_server_side_encryption_by_default {
94+
kms_master_key_id = "${aws_kms_key.parameter_store.arn}"
95+
sse_algorithm = "aws:kms"
96+
}
97+
}
98+
}
99+
98100
lifecycle_rule {
99101
prefix = "backups"
100102
enabled = true

templates/iam_instance_role_policy.json.tpl

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
11
{
22
"Version": "2012-10-17",
33
"Statement": [
4-
{
5-
"Action": [
6-
"dynamodb:PutItem",
7-
"dynamodb:GetItem",
8-
"dynamodb:Query",
9-
"dynamodb:Scan"
10-
],
11-
"Effect": "Allow",
12-
"Resource": "${db_credstash_arn}"
13-
},
144
{
155
"Effect": "Allow",
166
"Action": [
177
"s3:ListBucket",
188
"s3:GetBucketLocation"
199
],
20-
"Resource": [ "arn:aws:s3:::${resource_name_prefix}-backup" ]
10+
"Resource": [ "arn:aws:s3:::${s3_backup_bucket}" ]
2111
},
2212
{
2313
"Effect": "Allow",
@@ -28,7 +18,7 @@
2818
"s3:List*",
2919
"s3:DeleteObject"
3020
],
31-
"Resource": [ "arn:aws:s3:::${resource_name_prefix}-backup/*" ]
21+
"Resource": [ "arn:aws:s3:::${s3_backup_bucket}/*" ]
3222
},
3323
{
3424
"Effect": "Allow",
@@ -41,6 +31,13 @@
4131
],
4232
"Resource": "*"
4333
},
34+
{
35+
"Effect": "Allow",
36+
"Action": [
37+
"ssm:GetParameters"
38+
],
39+
"Resource": "arn:aws:ssm:${aws_region}:${account_id}:parameter${ssm_key_prefix}"
40+
},
4441
{
4542
"Effect": "Allow",
4643
"Action": [

templates/key_policy.json.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"Version": "2012-10-17",
3-
"Id": "${resource_name_prefix}-credstash",
3+
"Id": "${resource_name_prefix}-parameter-store",
44
"Statement": [
55
{
66
"Sid": "Enable IAM User Permissions",

templates/user_data.sh.tpl

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/aws/bin:/root/bin
44

55
yum update -y
6-
yum install -y gcc libffi-devel openssl-devel
76

8-
# credstash installation for secrets
7+
# upgrade pip to latest stable
98
pip install -U pip
10-
/usr/local/bin/pip install -U credstash awscli
9+
# upgrade awscli to latest stable
10+
# upgrading pip from 9.0.3 to 10.0.1 changes the path from /usr/bin/pip to
11+
# /usr/local/bin/pip and the line below throws this error
12+
# /var/lib/cloud/instance/scripts/part-001: line 10: /usr/bin/pip: No such file or directory
13+
# So, I export the PATH in the beggining correctly but still tries to from the old location
14+
# I couldn't see why in the outputs I'm going to hardcode it for now (01:10am)
15+
/usr/local/bin/pip install -U awscli
1116

1217
echo "* hard nofile 64000" >> /etc/security/limits.conf
1318
echo "* soft nofile 64000" >> /etc/security/limits.conf
@@ -49,27 +54,35 @@ status amazon-ssm-agent || start amazon-ssm-agent
4954
cat <<EOF > /usr/sbin/mongobackup.sh
5055
#!/bin/bash -e
5156
57+
set -o errexit # exit on cmd failure
58+
set -o nounset # fail on use of unset vars
59+
set -o pipefail # throw latest exit failure code in pipes
60+
set -o xtrace # print command traces before executing command.
61+
5262
export PATH="/usr/local/bin:\$PATH"
53-
export BACKUP_ENCRYPTION_KEY=\$(credstash --region ${aws_region} --table ${credstash_table_name} get BACKUP_ENCRYPTION_KEY)
5463
export BACKUP_TIME=\$(date +'%Y-%m-%d-%H-%M-%S')
5564
export BACKUP_FILENAME="\$BACKUP_TIME-pritunl-db-backup.tar.gz"
5665
export BACKUP_DEST="/tmp/\$BACKUP_TIME"
5766
mkdir "\$BACKUP_DEST" && cd "\$BACKUP_DEST"
5867
mongodump -d pritunl
5968
tar zcf "\$BACKUP_FILENAME" dump
6069
rm -rf dump
61-
gpg --yes --batch --passphrase="\$BACKUP_ENCRYPTION_KEY" -c "\$BACKUP_FILENAME"
6270
md5sum "\$BACKUP_FILENAME" > "\$BACKUP_FILENAME.md5"
63-
rm "\$BACKUP_FILENAME"
6471
aws s3 sync . s3://${s3_backup_bucket}/backups/
6572
cd && rm -rf "\$BACKUP_DEST"
6673
EOF
6774
chmod 700 /usr/sbin/mongobackup.sh
6875

6976
cat <<EOF > /etc/cron.daily/pritunl-backup
7077
#!/bin/bash -e
71-
export PATH="/usr/local/bin:\$PATH"
72-
/usr/sbin/mongobackup.sh && curl -fsS --retry 3 "https://hchk.io/\$(credstash --region ${aws_region} --table ${credstash_table_name} get HEALTHCHECKS_IO_KEY)"
78+
export PATH="/usr/local/sbin:/usr/local/bin:\$PATH"
79+
mongobackup.sh && \
80+
curl -fsS --retry 3 \
81+
"https://hchk.io/\$( aws --region=${aws_region} --output=text \
82+
ssm get-parameters \
83+
--names ${healthchecks_io_key} \
84+
--with-decryption \
85+
--query 'Parameters[*].Value')"
7386
EOF
7487
chmod 755 /etc/cron.daily/pritunl-backup
7588

variables.tf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ variable "tags" {
2929
}
3030

3131
variable "resource_name_prefix" {
32-
description = "All the resources will be prefixed with this varible"
32+
description = "All the resources will be prefixed with the value of this variable"
3333
default = "pritunl"
3434
}
35+
36+
variable "healthchecks_io_key" {
37+
description = "Health check key for healthchecks.io"
38+
default = "invalid"
39+
}

0 commit comments

Comments
 (0)