blob: 02cf0999fd11de01ea6181be0c103489a11b1e4e (
plain) (
blame)
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
|
#!/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
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
|