Skip to content

Commit 7f1141e

Browse files
authored
Merge pull request #45 from CodeNow/check-s3-policy
Conditionally check the S3 policy before creating a new one with vault
2 parents 3fd3c7e + dc252d1 commit 7f1141e

File tree

4 files changed

+70
-24
lines changed

4 files changed

+70
-24
lines changed

lib/aws.sh

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,26 @@ aws::get_org_id() {
4545
rollbar::fatal_trap \
4646
"Dock-Init: Failed to Render Org Script" \
4747
"Consule-Template was unable to realize the given template."
48+
if [ -z ${AWS_ACCESS_KEY+x} ] || [ -z ${AWS_SECRET_KEY+x} ]; then
49+
ORG_SCRIPT=$DOCK_INIT_BASE/util/get-org-id.sh
4850

49-
ORG_SCRIPT=$DOCK_INIT_BASE/util/get-org-id.sh
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"
5054

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"
55+
consul-template -config="${config}" -once -template="${template}"
5456

55-
consul-template -config="${config}" -once -template="${template}"
57+
rollbar::clear_trap
5658

57-
rollbar::clear_trap
58-
59-
# give amazon a chance to get the auth
60-
sleep 5
59+
# give amazon a chance to get the auth
60+
sleep 5
6161

62-
# Attempt to fetch the org id from the tags via the fetch script
63-
backoff aws::fetch_org_id_from_tags
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
6468

6569
if [[ "$ORG_ID" == "" ]]; then
6670
# this will print an error, so that's good
@@ -73,3 +77,36 @@ aws::get_org_id() {
7377

7478
log::info "Got Org ID: $ORG_ID"
7579
}
80+
81+
aws::get_org_id_onprem() {
82+
local attempt=${1}
83+
log::info 'Attempting to get org id on prem'
84+
data='{"attempt":'"${attempt}"'}'
85+
86+
rollbar::warning_trap \
87+
"Dock-Init: Cannot Fetch Org" \
88+
"Attempting to get the Org Tag from AWS and failing." \
89+
"$data"
90+
91+
EC2_HOME=/usr/local/ec2
92+
export EC2_HOME
93+
94+
JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/jre
95+
export JAVA_HOME
96+
97+
local instance_id=$(ec2-metadata -i | awk '{print $2}')
98+
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); }')
101+
102+
ORG_ID=$(bash /usr/local/ec2/bin/ec2-describe-tags \
103+
--aws-access-key="${AWS_ACCESS_KEY}" \
104+
--aws-secret-key="${AWS_SECRET_KEY}" \
105+
--filter "resource-type=instance" \
106+
--filter "resource-id=${instance_id}" \
107+
--filter "key=org" \
108+
--region "${region}" \
109+
| awk '{print $5}')
110+
111+
export ORG_ID
112+
}

lib/consul.sh

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,20 @@ consul::configure_consul_template() {
5454
"Consul-Template was unable to realize the config template."
5555

5656
# expose VAULT_TOKEN for consul-template config
57-
local NODE_ENV=$(consul::get node/env)
58-
local token_path="${DOCK_INIT_BASE}/consul-resources/vault/${NODE_ENV}"
59-
VAULT_TOKEN=$(cat "${token_path}"/auth-token)
60-
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
6163

62-
local template="$DOCK_INIT_BASE/consul-resources/templates/"
63-
template+="template-config.hcl.ctmpl"
64-
template+=":$DOCK_INIT_BASE/consul-resources/template-config.hcl"
65-
66-
consul-template -once -template="$template"
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"
6767

68+
consul-template -once -template="$template"
69+
else
70+
log::info "AWS access key and secret already created, skipping template creation"
71+
fi
6872
rollbar::clear_trap
6973
}

lib/container.sh

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,14 @@ container::_start_registry_container() {
4545
local bucket="$(consul::get s3/bucket)"
4646
log::trace "region: ${region} bucket: ${bucket}"
4747

48-
vault::create_s3_policy "${bucket}"
49-
vault::set_s3_keys
48+
if [ -z ${S3_ACCESS_KEY+x} ] || [ -z ${S3_SECRET_KEY+x} ]; then
49+
log::info "Creating S3 credentials"
50+
vault::create_s3_policy "${bucket}"
51+
vault::set_s3_keys
52+
else
53+
log::info "S3 Credentials already created, setting s3 bucket for registry"
54+
fi
55+
5056
local docker_logs
5157
docker_logs=$(docker run \
5258
--detach=true \

test/container.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ describe 'container.sh'
4848
local region="${registry_version}"
4949
local bucket="${registry_version}"
5050
export ORG_ID='runnabear'
51-
export S3_ACCESS_KEY='thatKey'
52-
export S3_SECRET_KEY='datSecret'
51+
5352
stub::returns 'consul::get' "$registry_version"
5453
stub docker
5554
stub vault::create_s3_policy

0 commit comments

Comments
 (0)