Skip to content
This repository was archived by the owner on Jan 25, 2023. It is now read-only.

Commit f1ac700

Browse files
authored
Merge pull request #232 from anouarchattouna/fixing_tests_using_consul_for_dns
Fixing tests using consul for dns
2 parents a0bc093 + c3bd168 commit f1ac700

File tree

15 files changed

+234
-45
lines changed

15 files changed

+234
-45
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/gruntwork-io/pre-commit
3-
rev: v0.1.10
3+
rev: v0.1.12
44
hooks:
55
- id: terraform-fmt
66
- id: gofmt

examples/vault-consul-ami/vault-consul.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"min_packer_version": "0.12.0",
2+
"min_packer_version": "1.5.4",
33
"variables": {
44
"aws_region": "us-east-1",
5-
"vault_version": "1.5.4",
5+
"vault_version": "1.6.1",
66
"consul_module_version": "v0.8.0",
7-
"consul_version": "1.5.3",
7+
"consul_version": "1.9.2",
88
"consul_download_url": "{{env `CONSUL_DOWNLOAD_URL`}}",
99
"vault_download_url": "{{env `VAULT_DOWNLOAD_URL`}}",
1010
"install_auth_signing_script": "true",

examples/vault-dynamodb-backend/main.tf

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ terraform {
99
}
1010

1111
# ---------------------------------------------------------------------------------------------------------------------
12-
# DEPLOY THE VAULT SERVER CLUSTER
12+
# DEPLOY THE DYNAMODB STORAGE BACKEND
1313
# ---------------------------------------------------------------------------------------------------------------------
1414

1515
module "backend" {
@@ -19,6 +19,10 @@ module "backend" {
1919
write_capacity = var.dynamo_write_capacity
2020
}
2121

22+
# ---------------------------------------------------------------------------------------------------------------------
23+
# DEPLOY THE VAULT SERVER CLUSTER
24+
# ---------------------------------------------------------------------------------------------------------------------
25+
2226
module "vault_cluster" {
2327
# When using these modules in your own templates, you will need to use a Git URL with a ref attribute that pins you
2428
# to a specific version of the modules, such as the following example:
@@ -32,6 +36,11 @@ module "vault_cluster" {
3236
ami_id = var.ami_id
3337
user_data = data.template_file.user_data_vault_cluster.rendered
3438

39+
# Enable S3 storage backend
40+
enable_s3_backend = true
41+
s3_bucket_name = var.s3_bucket_name
42+
force_destroy_s3_bucket = var.force_destroy_s3_bucket
43+
3544
vpc_id = data.aws_vpc.default.id
3645
subnet_ids = data.aws_subnet_ids.default.ids
3746

@@ -44,16 +53,23 @@ module "vault_cluster" {
4453
allowed_inbound_security_group_count = 0
4554
ssh_key_name = var.ssh_key_name
4655

56+
# Enable DynamoDB high availability storage backend
4757
enable_dynamo_backend = true
4858
dynamo_table_name = var.dynamo_table_name
4959
}
5060

61+
# ---------------------------------------------------------------------------------------------------------------------
62+
# THE USER DATA SCRIPT THAT WILL RUN ON EACH VAULT SERVER WHEN IT'S BOOTING
63+
# This script will configure and start Vault
64+
# ---------------------------------------------------------------------------------------------------------------------
65+
5166
data "template_file" "user_data_vault_cluster" {
5267
template = file("${path.module}/user-data-vault.sh")
5368

5469
vars = {
5570
aws_region = data.aws_region.current.name
5671
dynamo_table_name = var.dynamo_table_name
72+
s3_bucket_name = var.s3_bucket_name
5773
}
5874
}
5975

examples/vault-dynamodb-backend/user-data-vault.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,7 @@ readonly VAULT_TLS_KEY_FILE="/opt/vault/tls/vault.key.pem"
2020
--dynamo-table "${dynamo_table_name}" \
2121
--dynamo-region "${aws_region}" \
2222
--tls-cert-file "$VAULT_TLS_CERT_FILE" \
23-
--tls-key-file "$VAULT_TLS_KEY_FILE"
23+
--tls-key-file "$VAULT_TLS_KEY_FILE" \
24+
--enable-s3-backend \
25+
--s3-bucket "${s3_bucket_name}" \
26+
--s3-bucket-region "${aws_region}"

examples/vault-dynamodb-backend/variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,15 @@ variable "dynamo_write_capacity" {
6565
description = "Sets the DynamoDB write capacity for storage backend"
6666
default = 5
6767
}
68+
69+
variable "s3_bucket_name" {
70+
description = "The name of an S3 bucket to create and use as a storage backend (if configured). Note: S3 bucket names must be *globally* unique."
71+
type = string
72+
default = "my-vault-bucket"
73+
}
74+
75+
variable "force_destroy_s3_bucket" {
76+
description = "If you set this to true, when you run terraform destroy, this tells Terraform to delete all the objects in the S3 bucket used for backend storage (if configured). You should NOT set this to true in production or you risk losing all your data! This property is only here so automated tests of this module can clean up after themselves."
77+
type = bool
78+
default = false
79+
}

examples/vault-s3-backend/variables.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,3 @@ variable "force_destroy_s3_bucket" {
8686
type = bool
8787
default = false
8888
}
89-

modules/run-vault/run-vault

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ readonly DEFAULT_AGENT_AUTH_MOUNT_PATH="auth/aws"
1414
readonly DEFAULT_PORT=8200
1515
readonly DEFAULT_LOG_LEVEL="info"
1616

17+
readonly DEFAULT_CONSUL_AGENT_SERVICE_REGISTRATION_ADDRESS="localhost:8500"
18+
1719
readonly EC2_INSTANCE_METADATA_URL="http://169.254.169.254/latest/meta-data"
1820

1921
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
@@ -44,6 +46,7 @@ function print_usage {
4446
echo -e " --s3-bucket\tSpecifies the S3 bucket to use to store Vault data. Only used if '--enable-s3-backend' is set."
4547
echo -e " --s3-bucket-path\tSpecifies the S3 bucket path to use to store Vault data. Only used if '--enable-s3-backend' is set."
4648
echo -e " --s3-bucket-region\tSpecifies the AWS region where '--s3-bucket' lives. Only used if '--enable-s3-backend' is set."
49+
echo -e " --consul-agent-service-registration-address\tSpecifies the address of the Consul agent to communicate with when using a different storage backend, in this case an S3 backend. Only used if '--enable-s3-backend' is set. Default is ${DEFAULT_CONSUL_AGENT_SERVICE_REGISTRATION_ADDRESS}."
4750
echo -e " --enable-dynamo-backend\tIf this flag is set, DynamoDB will be enabled as the backend storage (HA)"
4851
echo -e " --dynamo-region\tSpecifies the AWS region where --dynamo-table lives. Only used if '--enable-dynamo-backend is on'"
4952
echo -e " --dynamo--table\tSpecifies the DynamoDB table to use for HA Storage. Only used if '--enable-dynamo-backend is on'"
@@ -237,13 +240,14 @@ function generate_vault_config {
237240
local -r s3_bucket="$9"
238241
local -r s3_bucket_path="${10}"
239242
local -r s3_bucket_region="${11}"
240-
local -r enable_dynamo_backend="${12}"
241-
local -r dynamo_region="${13}"
242-
local -r dynamo_table="${14}"
243-
local -r enable_auto_unseal="${15}"
244-
local -r auto_unseal_kms_key_id="${16}"
245-
local -r auto_unseal_kms_key_region="${17}"
246-
local -r auto_unseal_endpoint="${18}"
243+
local -r consul_agent_service_registration_address="${12}"
244+
local -r enable_dynamo_backend="${13}"
245+
local -r dynamo_region="${14}"
246+
local -r dynamo_table="${15}"
247+
local -r enable_auto_unseal="${16}"
248+
local -r auto_unseal_kms_key_id="${17}"
249+
local -r auto_unseal_kms_key_region="${18}"
250+
local -r auto_unseal_endpoint="${19}"
247251
local -r config_path="$config_dir/$VAULT_CONFIG_FILE"
248252

249253
local instance_ip_address
@@ -288,6 +292,7 @@ EOF
288292
local dynamodb_storage_type="storage"
289293
local s3_config=""
290294
local vault_storage_backend=""
295+
local service_registration=""
291296
if [[ "$enable_s3_backend" == "true" ]]; then
292297
s3_config=$(cat <<EOF
293298
storage "s3" {
@@ -299,9 +304,14 @@ EOF
299304
)
300305
consul_storage_type="ha_storage"
301306
dynamodb_storage_type="ha_storage"
307+
service_registration=$(cat <<EOF
308+
service_registration "consul" {
309+
address = "${consul_agent_service_registration_address}"
310+
}\n
311+
EOF
312+
)
302313
fi
303314

304-
305315
if [[ "$enable_dynamo_backend" == "true" ]]; then
306316
vault_storage_backend=$(cat <<EOF
307317
$dynamodb_storage_type "dynamodb" {
@@ -335,6 +345,7 @@ EOF
335345
echo -e "$listener_config" >> "$config_path"
336346
echo -e "$s3_config" >> "$config_path"
337347
echo -e "$vault_storage_backend" >> "$config_path"
348+
echo -e "$service_registration" >> "$config_path"
338349

339350
chown "$user:$user" "$config_path"
340351
}
@@ -368,6 +379,8 @@ Documentation=https://www.vaultproject.io/docs/
368379
Requires=network-online.target
369380
After=network-online.target
370381
ConditionFileNotEmpty=$config_path
382+
StartLimitIntervalSec=60
383+
StartLimitBurst=3
371384
372385
EOF
373386
)
@@ -392,9 +405,12 @@ KillSignal=SIGINT
392405
Restart=on-failure
393406
RestartSec=5
394407
TimeoutStopSec=30
408+
StartLimitInterval=60
395409
StartLimitIntervalSec=60
396410
StartLimitBurst=3
397411
LimitNOFILE=65536
412+
LimitMEMLOCK=infinity
413+
398414
EOF
399415
)
400416

@@ -449,6 +465,7 @@ function run {
449465
local s3_bucket=""
450466
local s3_bucket_path=""
451467
local s3_bucket_region=""
468+
local consul_agent_service_registration_address="${DEFAULT_CONSUL_AGENT_SERVICE_REGISTRATION_ADDRESS}"
452469
local enable_dynamo_backend="false"
453470
local dynamo_region=""
454471
local dynamo_table=""
@@ -547,6 +564,11 @@ function run {
547564
s3_bucket_region="$2"
548565
shift
549566
;;
567+
--consul-agent-service-registration-address)
568+
assert_not_empty "$key" "$2"
569+
consul_agent_service_registration_address="$2"
570+
shift
571+
;;
550572
--enable-dynamo-backend)
551573
enable_dynamo_backend="true"
552574
;;
@@ -639,6 +661,7 @@ function run {
639661
if [[ "$enable_s3_backend" == "true" ]]; then
640662
assert_not_empty "--s3-bucket" "$s3_bucket"
641663
assert_not_empty "--s3-bucket-region" "$s3_bucket_region"
664+
assert_not_empty "--consul-agent-service-registration-address" "${consul_agent_service_registration_address}"
642665
fi
643666
fi
644667

@@ -714,6 +737,7 @@ function run {
714737
"$s3_bucket" \
715738
"$s3_bucket_path" \
716739
"$s3_bucket_region" \
740+
"${consul_agent_service_registration_address}" \
717741
"$enable_dynamo_backend" \
718742
"$dynamo_region" \
719743
"$dynamo_table" \

test/go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ github.com/gruntwork-io/gruntwork-cli v0.5.1 h1:mVmVsFubUSLSCO8bGigI63HXzvzkC0uW
197197
github.com/gruntwork-io/gruntwork-cli v0.5.1/go.mod h1:IBX21bESC1/LGoV7jhXKUnTQTZgQ6dYRsoj/VqxUSZQ=
198198
github.com/gruntwork-io/terratest v0.28.15 h1:in1DRBq8/RjxMyb6Amr1SRrczOK/hGnPi+gQXOOtbZI=
199199
github.com/gruntwork-io/terratest v0.28.15/go.mod h1:PkVylPuUNmItkfOTwSiFreYA4FkanK8AluBuNeGxQOw=
200+
github.com/gruntwork-io/terratest v0.32.1 h1:Uho3H7VWD4tEulWov7pWW90V3XATLKxSh88AtrxTYvU=
201+
github.com/gruntwork-io/terratest v0.32.3 h1:GSe/mkSQe0rD7Z92NKTUjDKg2FBuy0w82Ttd5gcK7kU=
200202
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
201203
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
202204
github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
@@ -362,6 +364,7 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
362364
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
363365
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
364366
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
367+
github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48=
365368
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
366369
github.com/stretchr/testify v0.0.0-20151208002404-e3a8ff8ce365/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
367370
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=

test/vault_cluster_dynamodb_backend_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ const VAR_DYNAMO_TABLE_NAME = "dynamo_table_name"
2020
// 3. Deploy that AMI using the example Terraform code
2121
// 4. SSH to a Vault node and initialize the Vault cluster
2222
// 5. SSH to each Vault node and unseal it
23-
// 6. Connect to the Vault cluster via the ELB
2423
func runVaultWithDynamoBackendClusterTest(t *testing.T, amiId string, awsRegion, sshUserName string) {
2524
examplesDir := test_structure.CopyTerraformFolderToTemp(t, REPO_ROOT, VAULT_CLUSTER_DYNAMODB_BACKEND_PATH)
2625

@@ -36,10 +35,13 @@ func runVaultWithDynamoBackendClusterTest(t *testing.T, amiId string, awsRegion,
3635
})
3736

3837
test_structure.RunTestStage(t, "deploy", func() {
38+
uniqueId := random.UniqueId()
3939
terraformVars := map[string]interface{}{
40-
VAR_DYNAMO_TABLE_NAME: fmt.Sprintf("vault-dynamo-test-%s", random.UniqueId()),
40+
VAR_DYNAMO_TABLE_NAME: fmt.Sprintf("vault-dynamo-test-%s", uniqueId),
41+
VAR_S3_BUCKET_NAME: s3BucketName(uniqueId),
42+
VAR_FORCE_DESTROY_S3_BUCKET: true,
4143
}
42-
deployCluster(t, amiId, awsRegion, examplesDir, random.UniqueId(), terraformVars)
44+
deployCluster(t, amiId, awsRegion, examplesDir, uniqueId, terraformVars)
4345
})
4446

4547
test_structure.RunTestStage(t, "validate", func() {

test/vault_cluster_enterprise_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,18 @@ func runVaultEnterpriseClusterTest(t *testing.T, amiId string, awsRegion string,
5959
deployCluster(t, amiId, awsRegion, examplesDir, uniqueId, terraformVars)
6060
})
6161

62+
test_structure.RunTestStage(t, "initialize_unseal", func() {
63+
terraformOptions := test_structure.LoadTerraformOptions(t, examplesDir)
64+
keyPair := test_structure.LoadEc2KeyPair(t, examplesDir)
65+
66+
initializeAndUnsealVaultCluster(t, OUTPUT_VAULT_CLUSTER_ASG_NAME, sshUserName, terraformOptions, awsRegion, keyPair)
67+
})
68+
6269
test_structure.RunTestStage(t, "validate", func() {
6370
terraformOptions := test_structure.LoadTerraformOptions(t, examplesDir)
6471
keyPair := test_structure.LoadEc2KeyPair(t, examplesDir)
6572

66-
cluster := initializeAndUnsealVaultCluster(t, OUTPUT_VAULT_CLUSTER_ASG_NAME, sshUserName, terraformOptions, awsRegion, keyPair)
73+
cluster := getInitializedAndUnsealedVaultCluster(t, OUTPUT_VAULT_CLUSTER_ASG_NAME, sshUserName, terraformOptions, awsRegion, keyPair)
6774
testVaultUsesConsulForDns(t, cluster)
6875
checkEnterpriseInstall(t, OUTPUT_VAULT_CLUSTER_ASG_NAME, sshUserName, terraformOptions, awsRegion, keyPair)
6976
})

0 commit comments

Comments
 (0)