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

Feature/ddb backend #77

Closed
wants to merge 26 commits into from
Closed

Feature/ddb backend #77

wants to merge 26 commits into from

Conversation

BonzTM
Copy link

@BonzTM BonzTM commented Jul 5, 2018

Issue: #76

  • Adding functionality for DynamoDB HA storage backend in the run-vault script. Cannot be used in conjunction with S3 storage backend.
  • Documented usage in README.
  • Added a small if statement to check if supervisor is already installed and to skip (for airgapped installations that have no outbound access).
  • Fixed typo in print_usage function causing it to bomb out.
  • Added actual Dynamo TF infrastructure. Basic DDB table (No Autoscale, Global Tables etc). No logic added to main module to disable Consul if DDB enabled at this time. Only at the nested module level.
  • Quick example of code in the examples/ folder using the S3 example as a template. Not really any different.

@@ -35,7 +35,10 @@ function print_usage {
echo -e " --skip-vault-config\tIf this flag is set, don't generate a Vault configuration file. Optional. Default is false."
echo -e " --enable-s3-backend\tIf this flag is set, an S3 backend will be enabled in addition to the HA Consul backend. Default is false."
echo -e " --s3-bucket\tSpecifies the S3 bucket to use to store Vault data. Only used if '--enable-s3-backend' is set."
echo -e " --s3-bucket-region\tSpecifies the AWS region where `--s3-bucket` lives. Only used if `--enable-s3-backend` is set."
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

backticks were previously used causing this function to behave unexpectedly. Replaced with single quotes per all other items in the list.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops! Good fix, thanks!

local readonly config_path="$config_dir/$VAULT_CONFIG_FILE"

local instance_ip_address
instance_ip_address=$(get_instance_ip_address)

log_info "Creating default Vault config file in $config_path"
local readonly listener_config=$(cat <<EOF
ui = true
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enabled vault UI by default

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx! We should probably make this configurable though with a --disable-ui flag for those that want to leave it off.

@@ -194,15 +194,46 @@ resource "aws_s3_bucket" "vault_storage" {
}
}

resource "aws_dynamodb_table" "vault_dynamo" {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default dynamo table with small read/write capacity. Anything beyond (autoscaled, global tables) would need to be added or used in client code.

Copy link
Collaborator

@brikis98 brikis98 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a fantastic PR, thank you! We were just talking about adding DynamoDB as a backend, so this is great timing.

I've made some detailed code comments below. One high level comment: we should add an automated test for this and make sure all existing tests pass. See the test folder for details.

This example creates a Vault cluster spread across the subnets in the default VPC of the AWS account. For an example of a Vault cluster
that is publicly accessible, see [vault-cluster-public](https://github.com/hashicorp/terraform-aws-vault/tree/master/examples/vault-cluster-public).

![Vault architecture]()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing image?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% certain where to get a documentation template for Cloudcraft that includes the Hashicorp custom images. If you could point me in the right direction that would be great.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brikis98 Do you have a template used for previous diagrams?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To deploy a Vault Cluster:

1. `git clone` this repo to your computer.
1. Optional: build a Vault and Consul AMI. See the [vault-consul-ami
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: build a Vault AMI. See the [vault-consul-ami example](https://github.com/hashicorp/terraform-aws-vault/tree/master/examples/vault-consul-ami) documentation for instructions on how to build an AMI that has both Vault and Consul installed (note that for this example, you'll only need Vault, but having both won't hurt anything).

# ---------------------------------------------------------------------------------------------------------------------
# DEPLOY A VAULT SERVER CLUSTER AND A CONSUL SERVER CLUSTER IN AWS
# This is an example of how to use the vault-cluster module to deploy a Vault cluster in AWS. This cluster uses Consul,
# running in a separate cluster, as its storage backend.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update comment

user_data = "${data.template_file.user_data_vault_cluster.rendered}"

enable_dynamo_backend = true
dynamo_table_name = "${var.dynamo_table_name}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice API 👍

aws_region = "${data.aws_region.current.name}"
s3_bucket_name = "${var.s3_bucket_name}"
consul_cluster_tag_key = "${var.consul_cluster_tag_key}"
consul_cluster_tag_value = "${var.consul_cluster_name}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume these Consul and S3 params are no longer necessary, either as input variables, or to pass to User Data?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh yes. Removing these and adding the Dynamo table variable.

local readonly consul_storage=$(cat <<EOF

if [[ "$enable_dynamo" == "true" ]]; then
consul_storage=""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please declare local consul_storage and local vault_storage up above this if/else block. That way, both default to empty strings, and only one gets set to a proper value.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. Adjusting.

local readonly enable_s3_backend=""
local readonly s3_bucket=""
local readonly s3_bucket_region=""
fi
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find changing the list of input params a bit confusing. I'd recommend making this a single list:

local readonly enable_s3_backend="$8"
local readonly s3_bucket="$9"
local readonly s3_bucket_region="${10}"
local readonly enable_dynamo="${11}"
local readonly dynamo_region="${12}"
local readonly dynamo_table="${13}"

The run method should pass all these params, setting them to true, false, empty strings, or proper values, depending on what the user passed in.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, however, by default the vault config generation just adds the S3 params at the end (no harm if they are empty). I can add extra vault config generation logic (at the bottom) if you'd rather see this as a list.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed it back to a list and updated logic at the bottom (vault generation script) to match

}

tags {
Description = "Used for HA storage with Vault."
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we already take in a list of custom tags; we should use it here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added Name tag. Didn't see any other list of tags anywhere.


output "dynamo_table_arn" {
value = "${aws_dynamodb_table.vault_dynamo.arn}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work, as aws_dynamodb_table might not be created. Instead, use:

value = "${element(concat(aws_dynamodb_table.vault_dynamo.*.arn, list("")), 0)}"


variable "dynamo_write_capacity" {
description = "Sets the DynamoDB write capacity for storage backend"
default = "5"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these be numbers (i.e., not wrapped in quotes)?

Copy link
Author

@BonzTM BonzTM Jul 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They can be but don't have to be. Changed them for consistency. Typically as tf only has 3 types of variables, I wrap all 'string' variables in double quotes for consistency. But I see this isn't done in this module.

@BonzTM
Copy link
Author

BonzTM commented Jul 6, 2018

I'm merely a novice when it comes to software development. I'll do my best to look over the tests and see what I can come up with in the meantime.

@brikis98
Copy link
Collaborator

brikis98 commented Jul 6, 2018

I'm merely a novice when it comes to software development. I'll do my best to look over the tests and see what I can come up with in the meantime.

Well, learning automated testing is a very valuable skill, and we're happy to help :)

There's lots of examples already in the repo, so you should be able to follow their structure. The first step, of course, is to test everything manually and make sure it works! Once it works manually, automate those steps as shown in the existing tests in the test folder. If you get stuck, don't hesitate to ask. Thank you!

if [[ "$enable_dynamo" == "true" ]]; then
consul_storage=""
vault_storage=$(cat <<EOF
storage "dynamodb" {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to have both dynamodb and s3 backend we would need to be able to make that block similar to how its handled for consul. Currently what I have adjusted on my end, and is working so far with manual testing:

  local consul_storage_type="storage"
  local dynamodb_storage_type="storage"
  local s3_config=""
  local consul_storage=""
  local vault_storage=""

if [[ "$enable_s3_backend" == "true" ]]; then
    s3_config=$(cat <<EOF
storage "s3" {
  bucket = "$s3_bucket"
  region = "$s3_bucket_region"
}\n
EOF
)
    consul_storage_type="ha_storage"
    dynamodb_storage_type="ha_storage"
  fi


  if [[ "$enable_dynamo_backend" == "true" ]]; then
    vault_storage=$(cat <<EOF
$dynamodb_storage_type "dynamodb" {
  ha_enabled = "true"
  region = "$dynamo_region"
  table  = "$dynamo_table"
}

# HA settings
cluster_addr  = "https://$instance_ip_address:$cluster_port"
api_addr      = "$api_addr"

EOF
)

@jp
Copy link

jp commented Sep 27, 2018

Hello, I'm checking here and it seems to be awesome and very useful, that would be sad to have this getting stale ;)
Is there anything I could do to help? I'm definitely going to try this branch and provide feedback.

@BonzTM
Copy link
Author

BonzTM commented Oct 1, 2018

Hello, I'm checking here and it seems to be awesome and very useful, that would be sad to have this getting stale ;)
Is there anything I could do to help? I'm definitely going to try this branch and provide feedback.

@jp I ended up going back a different direction (Consul) due to certain encryption specifics. This module with DynamoDB was in use and functioning, I just never wrote the testing pieces for it to be merged. I haven't really had the time to do so.

You're more than welcome to update the testing functionality and/or update the functionality to resolve the conflicts into the master branch if you'd like.

@idris
Copy link

idris commented Oct 30, 2018

@brikis98 can someone at Hashicorp take a look at this to take it over the finish line?

briandbecker added a commit to briandbecker/terraform-aws-vault that referenced this pull request Dec 10, 2018
* Backticks causing unexpected behavior
* Removes backticks in favor of single quote to match other items in the
  list
* Credit to @BonzTM in hashicorp#77 for initially finding/fixing; isolated to get
  fix merged
@luizm
Copy link

luizm commented May 30, 2019

It's a great PR, someone is looking at this? 😄

@felipejfc
Copy link

This PR needed rebase to resolve conflicts with master, I've made them and it's working great, do you guys want to go ahead with this? If so I can send an updated PR.

Best

@hashicorp-cla
Copy link

hashicorp-cla commented Jun 8, 2020

CLA assistant check

Thank you for your submission! We require that all contributors sign our Contributor License Agreement ("CLA") before we can accept the contribution. Read and sign the agreement

Learn more about why HashiCorp requires a CLA and what the CLA includes


0 out of 2 committers have signed the CLA.

  • A120670
  • Joshua Wells

Joshua Wells seems not to be a GitHub user.
You need a GitHub account to be able to sign the CLA. If you already have a GitHub account, please add the email address used for this commit to your account.

Have you signed the CLA already but the status is still pending? Recheck it.

@BonzTM BonzTM closed this Feb 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants