Skip to content

Commit e97fbf8

Browse files
author
Henry Mollman
committed
Remove need for AWS credentials as well
1 parent 2b226ba commit e97fbf8

3 files changed

Lines changed: 61 additions & 20 deletions

File tree

lib/aws.sh

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,27 @@ 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+
sleep 5
67+
backoff aws::get_org_id_onprem
68+
fi
6469

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

7479
log::info "Got Org ID: $ORG_ID"
7580
}
81+
82+
aws::get_org_id_onprem() {
83+
local attempt=${1}
84+
log::info 'Attempting to get org id on prem'
85+
data='{"attempt":'"${attempt}"'}'
86+
87+
rollbar::warning_trap \
88+
"Dock-Init: Cannot Fetch Org" \
89+
"Attempting to get the Org Tag from AWS and failing." \
90+
"$data"
91+
92+
EC2_HOME=/usr/local/ec2
93+
export EC2_HOME
94+
95+
JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/jre
96+
export JAVA_HOME
97+
98+
local instance_id=$(ec2-metadata -i | awk '{print $2}')
99+
100+
# Note: this only works for us-.{4}-\d
101+
local region=$(ec2-metadata --availability-zone | awk '{ where = match($2, /us\-.+\-[1|2]/); print substr($2, where, 9); }')
102+
103+
ORG_ID=$(bash /usr/local/ec2/bin/ec2-describe-tags \
104+
--aws-access-key="${AWS_ACCESS_KEY}" \
105+
--aws-secret-key="${AWS_SECRET_KEY}" \
106+
--filter "resource-type=instance" \
107+
--filter "resource-id=${instance_id}" \
108+
--filter "key=org" \
109+
--region "${region}" \
110+
| awk '{print $5}')
111+
112+
export ORG_ID
113+
}

lib/consul.sh

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,18 @@ 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+
fi
6870
rollbar::clear_trap
6971
}

lib/container.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ container::_start_registry_container() {
4646
log::trace "region: ${region} bucket: ${bucket}"
4747

4848
if [ -z ${S3_ACCESS_KEY+x} ] || [ -z ${S3_SECRET_KEY+x} ]; then
49+
log::info "Creating S3 credentials"
4950
vault::create_s3_policy "${bucket}"
5051
vault::set_s3_keys
5152
fi

0 commit comments

Comments
 (0)