#!/bin/sh # # DESCRIPTION: # bootstrap-state-bucket sets up a terraform state bucket that is used by the # rest of the terraform modules, and creates a secure note in 1Password for # the credentials of the state key. This state is kept in backblaze's b2 # service, which provides object storage 'buckets' similar to Amazon's S3. # # PREREQUISITES: # - `b2` (backblaze CLI) # - `op` (1password CLI) # - `jq` # - `awk` # # The `b2` and `op` CLI must both already be authenticated. # - `b2 account auth` # - `op signin` # # USAGE: # ./bootstrap-state-bucket.sh set -o errexit set -o nounset KEY=terraform-state-key BUCKET=eugenetriguba-terraform-state CAPS=listBuckets,listFiles,readFiles,writeFiles,deleteFiles OP_VAULT=Private main() { if ! b2 bucket get $BUCKET >/dev/null 2>&1; then echo "$BUCKET does not exist, creating.." b2 bucket create $BUCKET allPrivate >/dev/null else echo "$BUCKET exists." fi if ! b2 key list | grep --quiet $KEY; then echo "$KEY does not exist, creating.." OUT=$(b2 key create $KEY $CAPS --bucket "$BUCKET") KEY_ID=$(echo "$OUT" | awk '{ print $1 }') KEY_VAL=$(echo "$OUT" | awk '{ print $2 }') OP_ITEM_TITLE="Backblaze B2 Terraform State Key" op item template get 'Secure Note' | op item create \ --title "$OP_ITEM_TITLE" \ --vault $OP_VAULT \ key_id="$KEY_ID" \ key_value="$KEY_VAL" >/dev/null echo "Created $KEY in B2 and $OP_ITEM_TITLE in 1Password" else echo "$KEY exists." fi } main