Skip to content

Commit 616010e

Browse files
authored
Merge pull request #47 from CodeNow/SAN-6195-create-vault-token
San 6195 create vault token
2 parents 38fac85 + 545469f commit 616010e

File tree

10 files changed

+120
-99
lines changed

10 files changed

+120
-99
lines changed

Diff for: .gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ npm-debug.log
22
consul-resources/template-config.hcl
33
consul-resources/vault/vault.hcl
44
hosts-registry.txt
5-
util/get-org-id.sh
5+
util/get-aws-creds.sh
66
consul-resources/vault/**/auth-token
77
consul-resources/vault/**/token-01
88
consul-resources/vault/**/token-02

Diff for: consul-resources/templates/get-aws-creds.sh.ctmpl

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
set -e
3+
4+
{{ with vault "aws_1h/creds/dock-init" }}
5+
export AWS_ACCESS_KEY="{{ .Data.access_key }}"
6+
export AWS_SECRET_KEY="{{ .Data.secret_key }}"
7+
{{ end }}

Diff for: consul-resources/templates/get-org-tag.sh.ctmpl

-29
This file was deleted.

Diff for: consul-resources/templates/registry_policy.tmpl

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
path "secret/organization/{{bpid}}/*" {
2+
policy = "read"
3+
}

Diff for: init.sh

+16-1
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,25 @@ else
3333
export VAULT_HOSTNAME
3434
fi
3535

36+
if [ -z "${USER_VAULT_PORT+x}" ]; then
37+
export USER_VAULT_PORT=8200
38+
else
39+
export USER_VAULT_PORT
40+
fi
41+
42+
if [ -z "${USER_VAULT_HOSTNAME+x}" ]; then
43+
export USER_VAULT_HOSTNAME=$USER_VAULT_HOSTNAME
44+
else
45+
export USER_VAULT_HOSTNAME
46+
fi
47+
48+
3649
export DOCKER_NETWORK=172.17.0.0/16
3750

3851
source "${DOCK_INIT_BASE}/lib/consul.sh"
3952
source "${DOCK_INIT_BASE}/lib/aws.sh"
4053
source "${DOCK_INIT_BASE}/lib/dock.sh"
54+
source "${DOCK_INIT_BASE}/lib/vault.sh"
4155
source "${DOCK_INIT_BASE}/lib/container.sh"
4256
source "${DOCK_INIT_BASE}/lib/iptables.sh"
4357
source "${DOCK_INIT_BASE}/lib/cleanup.sh"
@@ -50,9 +64,10 @@ main() {
5064
consul::get_environment
5165
consul::configure_consul_template
5266
dock::generate_certs
53-
aws::get_org_id
67+
aws::get_org_ids
5468
dock::set_hostname
5569
dock::set_config_org
70+
vault::store_private_registry_token
5671
container::start
5772
# rules must be run after docker has started
5873
iptables::run_rules

Diff for: lib/aws.sh

+52-53
Original file line numberDiff line numberDiff line change
@@ -8,64 +8,52 @@ source "${DOCK_INIT_BASE}/lib/util/halter.sh"
88
# @author Ryan Sandor Richards
99
# @module aws
1010

11-
# Backoff routine that attempts to fetch the dock's org id from EC2 tags
12-
aws::fetch_org_id_from_tags() {
13-
local attempt=${1}
11+
# get aws creds for these scripts...
12+
aws::get_aws_creds() {
13+
# Generate the org-tag fetching script
14+
rollbar::fatal_trap \
15+
"Dock-Init: Failed to Render Org Script" \
16+
"Consule-Template was unable to realize the given template."
1417

15-
log::info 'Attempting to get org id...'
16-
data='{"attempt":'"${attempt}"'}'
18+
ORG_SCRIPT=$DOCK_INIT_BASE/util/get-aws-creds.sh
19+
20+
local config="$DOCK_INIT_BASE/consul-resources/template-config.hcl"
21+
local template="$DOCK_INIT_BASE"
22+
template+="/consul-resources/templates/get-aws-creds.sh.ctmpl:$ORG_SCRIPT"
23+
24+
consul-template -config="${config}" -once -template="${template}"
1725

18-
rollbar::warning_trap \
19-
"Dock-Init: Cannot Fetch Org" \
20-
"Attempting to get the Org Tag from AWS and failing." \
21-
"$data"
22-
ORG_ID=$(bash "$ORG_SCRIPT")
23-
log::trace "Script Output: $ORG_ID"
2426
rollbar::clear_trap
27+
# give amazon a chance to get the auth
28+
sleep 5
2529

26-
if [[ "$ORG_ID" != "" ]]; then
27-
# Assume first value in host_tags comma separated list is org ID...
28-
ORG_ID=$(echo "$ORG_ID" | cut -d, -f 1)
29-
export ORG_ID
30-
return 0
31-
else
32-
# report the attempt to rollbar, since we don't want this to always fail
33-
rollbar::report_warning \
34-
"Dock-Init: Failed to Fetch Org" \
35-
"Org Script returned an empty string. Retrying."
36-
return 1
37-
fi
30+
source "${DOCK_INIT_BASE}/util/get-aws-creds.sh"
3831
}
3932

4033
# Fetches the org tags from EC2 and sets it to the `ORG_ID` environment variable
41-
aws::get_org_id() {
34+
aws::get_org_ids() {
4235
log::info "Setting Github Org ID"
4336

4437
# Generate the org-tag fetching script
4538
rollbar::fatal_trap \
4639
"Dock-Init: Failed to Render Org Script" \
4740
"Consule-Template was unable to realize the given template."
4841
if [ -z ${AWS_ACCESS_KEY+x} ] || [ -z ${AWS_SECRET_KEY+x} ]; then
49-
ORG_SCRIPT=$DOCK_INIT_BASE/util/get-org-id.sh
50-
51-
local config="$DOCK_INIT_BASE/consul-resources/template-config.hcl"
52-
local template="$DOCK_INIT_BASE"
53-
template+="/consul-resources/templates/get-org-tag.sh.ctmpl:$ORG_SCRIPT"
54-
55-
consul-template -config="${config}" -once -template="${template}"
42+
backoff aws::get_aws_creds
43+
fi
5644

57-
rollbar::clear_trap
45+
EC2_HOME=/usr/local/ec2
46+
export EC2_HOME
5847

59-
# give amazon a chance to get the auth
60-
sleep 5
48+
JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/jre
49+
export JAVA_HOME
6150

62-
# Attempt to fetch the org id from the tags via the fetch script
63-
backoff aws::fetch_org_id_from_tags
64-
else
65-
log::info "Taking aws creds from system"
66-
backoff aws::get_org_id_onprem
67-
fi
51+
export INSTANCE_ID=$(ec2-metadata -i | awk '{print $2}')
52+
# Note: this only works for us-.{4}-\d
53+
export REGION=$(ec2-metadata --availability-zone | awk '{ where = match($2, /us\-.+\-[1|2]/); print substr($2, where, 9); }')
6854

55+
backoff aws::fetch_org_id
56+
backoff aws::fetch_poppa_id
6957
if [[ "$ORG_ID" == "" ]]; then
7058
# this will print an error, so that's good
7159
rollbar::report_error \
@@ -76,9 +64,10 @@ aws::get_org_id() {
7664
fi
7765

7866
log::info "Got Org ID: $ORG_ID"
67+
log::info "Got Poppa ID: $POPPA_ID"
7968
}
8069

81-
aws::get_org_id_onprem() {
70+
aws::fetch_org_id() {
8271
local attempt=${1}
8372
log::info 'Attempting to get org id on prem'
8473
data='{"attempt":'"${attempt}"'}'
@@ -88,25 +77,35 @@ aws::get_org_id_onprem() {
8877
"Attempting to get the Org Tag from AWS and failing." \
8978
"$data"
9079

91-
EC2_HOME=/usr/local/ec2
92-
export EC2_HOME
80+
ORG_ID=$(bash /usr/local/ec2/bin/ec2-describe-tags \
81+
--aws-access-key="${AWS_ACCESS_KEY}" \
82+
--aws-secret-key="${AWS_SECRET_KEY}" \
83+
--filter "resource-type=instance" \
84+
--filter "resource-id=${INSTANCE_ID}" \
85+
--filter "key=org" \
86+
--region "${REGION}" \
87+
| awk '{print $5}')
9388

94-
JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/jre
95-
export JAVA_HOME
89+
export ORG_ID
90+
}
9691

97-
local instance_id=$(ec2-metadata -i | awk '{print $2}')
92+
# Fetches the poppa tags from EC2 and sets it to the `POPPA_ID` environment variable
93+
aws::fetch_poppa_id() {
94+
log::info "Setting Poppa ID"
9895

99-
# Note: this only works for us-.{4}-\d
100-
local region=$(ec2-metadata --availability-zone | awk '{ where = match($2, /us\-.+\-[1|2]/); print substr($2, where, 9); }')
96+
# Generate the org-tag fetching script
97+
rollbar::fatal_trap \
98+
"Dock-Init: Failed to Render Org Script" \
99+
"Consule-Template was unable to realize the given template."
101100

102-
ORG_ID=$(bash /usr/local/ec2/bin/ec2-describe-tags \
101+
POPPA_ID=$(bash /usr/local/ec2/bin/ec2-describe-tags \
103102
--aws-access-key="${AWS_ACCESS_KEY}" \
104103
--aws-secret-key="${AWS_SECRET_KEY}" \
105104
--filter "resource-type=instance" \
106-
--filter "resource-id=${instance_id}" \
107-
--filter "key=org" \
108-
--region "${region}" \
105+
--filter "resource-id=${INSTANCE_ID}" \
106+
--filter "key=runnable-org-id" \
107+
--region "${REGION}" \
109108
| awk '{print $5}')
110109

111-
export ORG_ID
110+
export POPPA_ID
112111
}

Diff for: lib/cleanup.sh

+2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ cleanup::exit_trap() {
1111
"${CERT_PATH}"/pass \
1212
"${DOCK_INIT_BASE}"/consul-resources/template-config.hcl \
1313
"${DOCK_INIT_BASE}"/consul-resources/vault/**/auth-token \
14+
"${DOCK_INIT_BASE}"/consul-resources/vault/**/user-vault-auth-token \
1415
"${DOCK_INIT_BASE}"/consul-resources/vault/**/token-* \
16+
"${DOCK_INIT_BASE}"/util/get-aws-creds.sh \
1517
"${DOCK_INIT_BASE}"/key/rollbar.token
1618
fi
1719
}

Diff for: lib/consul.sh

+17-14
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ consul::connect() {
2525
backoff consul::connect_backoff
2626
}
2727

28-
# Echos a value from consul foer the given keypath
28+
# Echos a value from consul for the given keypath
2929
# @param $1 keypath Keypath for the value to get from consul
3030
consul::get() {
3131
# Strip leading slashes so it works with both '/my/path' and 'my/path'
@@ -54,20 +54,23 @@ consul::configure_consul_template() {
5454
"Consul-Template was unable to realize the config template."
5555

5656
# expose VAULT_TOKEN for consul-template config
57-
if [ -z ${AWS_ACCESS_KEY+x} ] || [ -z ${AWS_SECRET_KEY+x} ]; then
58-
local NODE_ENV=$(consul::get node/env)
59-
local token_path="${DOCK_INIT_BASE}/consul-resources/vault/${NODE_ENV}"
60-
log::info "$token_path"
61-
VAULT_TOKEN=$(cat "${token_path}"/auth-token)
62-
export VAULT_TOKEN
57+
if [ -z ${AWS_ACCESS_KEY+x} ] || [ -z ${AWS_SECRET_KEY+x} ]; then
58+
local NODE_ENV=$(consul::get node/env)
59+
local token_path="${DOCK_INIT_BASE}/consul-resources/vault/${NODE_ENV}"
60+
log::info "$token_path"
61+
VAULT_TOKEN=$(cat "${token_path}"/auth-token)
62+
export VAULT_TOKEN
6363

64-
local template="$DOCK_INIT_BASE/consul-resources/templates/"
65-
template+="template-config.hcl.ctmpl"
66-
template+=":$DOCK_INIT_BASE/consul-resources/template-config.hcl"
64+
USER_VAULT_TOKEN=$(cat "${token_path}"/user-vault-auth-token)
65+
export USER_VAULT_TOKEN
6766

68-
consul-template -once -template="$template"
69-
else
70-
log::info "AWS access key and secret already created, skipping template creation"
71-
fi
67+
local template="$DOCK_INIT_BASE/consul-resources/templates/"
68+
template+="template-config.hcl.ctmpl"
69+
template+=":$DOCK_INIT_BASE/consul-resources/template-config.hcl"
70+
71+
consul-template -once -template="$template"
72+
else
73+
log::info "AWS access key and secret already created, skipping template creation"
74+
fi
7275
rollbar::clear_trap
7376
}

Diff for: lib/vault.sh

+21
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# @author Anandkumar Patel
55
# @module vault
66

7+
source "${DOCK_INIT_BASE}/lib/consul.sh"
78
source "${DOCK_INIT_BASE}/lib/util/log.sh"
89
source "${DOCK_INIT_BASE}/lib/util/rollbar.sh"
910

@@ -52,3 +53,23 @@ vault::set_s3_keys() {
5253
export S3_SECRET_KEY
5354
rollbar::clear_trap
5455
}
56+
57+
# creates a token for a the organizations-readonly policy
58+
vault::store_private_registry_token() {
59+
log::info "Storing vault token for private registry key"
60+
local NODE_ENV=$(consul::get node/env)
61+
local token_path="${DOCK_INIT_BASE}/consul-resources/vault/${NODE_ENV}"
62+
unset VAULT_TOKEN
63+
# this will pull from the vault currently running (our vault)
64+
export VAULT_ADDR="http://${USER_VAULT_HOSTNAME}:${USER_VAULT_PORT}"
65+
# this might also be needed if we use a different root token
66+
67+
USER_VAULT_TOKEN=$(cat "${token_path}"/user-vault-auth-token)
68+
vault auth ${USER_VAULT_TOKEN}
69+
log::info "Creating new policy and token for dock-$POPPA_ID"
70+
sed "s/{{bpid}}/${POPPA_ID}/g" "${DOCK_INIT_BASE}/consul-resources/templates/registry_policy.tmpl" > "${DOCK_INIT_BASE}/consul-resources/templates/registry_policy.hcl"
71+
vault policy-write dock-${POPPA_ID} "${DOCK_INIT_BASE}/consul-resources/templates/registry_policy.hcl"
72+
vault token-create -policy=dock-${POPPA_ID} | awk '/token/ { print $2 }' | awk 'NR==1 { print $1 }' > /opt/runnable/dock-init/user-private-registry-token
73+
VAULT_TOKEN=$(cat "${token_path}"/auth-token)
74+
export VAULT_TOKEN
75+
}

Diff for: test/consul.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe 'consul.sh'
2929

3030
it 'should read in the vault token'
3131
consul::configure_consul_template
32-
cat::called_with "${DOCK_INIT_BASE}/consul-resources/vault/TEST-NODE-ENV/auth-token"
32+
cat::called_with "${DOCK_INIT_BASE}/consul-resources/vault/TEST-NODE-ENV/user-vault-auth-token"
3333
end
3434

3535
it 'generate the consul-template configuration'

0 commit comments

Comments
 (0)