Skip to content

Commit cb21ee0

Browse files
authored
Fix auto-setup when TLS is used and when bind-address is specified as a CLI arg. (#487)
1 parent 638aee9 commit cb21ee0

File tree

5 files changed

+111
-9
lines changed

5 files changed

+111
-9
lines changed

influxdb/2.0/alpine/entrypoint.sh

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,16 +250,25 @@ function init_influxd () {
250250
return
251251
fi
252252

253-
# Capture final bind address, and check it is distinct from init addr
254253
local -r final_bind_addr="$(influxd print-config --key-name http-bind-address "${@}")"
255254
local -r init_bind_addr=":${INFLUXD_INIT_PORT}"
256255
if [ "${init_bind_addr}" = "${final_bind_addr}" ]; then
257256
log warn "influxd setup binding to same addr as final config, server will be exposed before ready" addr "${init_bind_addr}"
258257
fi
258+
local final_host_scheme="http"
259+
if [ "$(influxd print-config --key-name tls-cert "${@}")" != '""' ] && [ "$(influxd print-config --key-name tls-key "${@}")" != '""' ]; then
260+
final_host_scheme="https"
261+
fi
262+
263+
# Generate a config file with a known HTTP port, and TLS disabled.
264+
local -r init_config=/tmp/config.yml
265+
influxd print-config "${@}" | \
266+
sed -e "s#${final_bind_addr}#${init_bind_addr}#" -e '/^tls/d' > \
267+
"${init_config}"
259268

260269
# Start influxd in the background.
261270
log info "booting influxd server in the background"
262-
INFLUXD_HTTP_BIND_ADDRESS="${init_bind_addr}" influxd "${@}" &
271+
INFLUXD_CONFIG_PATH="${init_config}" INFLUXD_HTTP_BIND_ADDRESS="${init_bind_addr}" INFLUXD_TLS_CERT='' INFLUXD_TLS_KEY='' influxd &
263272
local -r influxd_init_pid="$!"
264273
trap "handle_signal TERM ${influxd_init_pid}" TERM
265274
trap "handle_signal INT ${influxd_init_pid}" INT
@@ -282,7 +291,7 @@ function init_influxd () {
282291

283292
# Rewrite the ClI configs to point at the server's final HTTP address.
284293
local -r final_port="$(echo "${final_bind_addr}" | sed -E 's#[^:]*:(.*)#\1#')"
285-
sed -i "s#http://localhost:${INFLUXD_INIT_PORT}#http://localhost:${final_port}#g" "${INFLUX_CONFIGS_PATH}"
294+
sed -i "s#http://localhost:${INFLUXD_INIT_PORT}#${final_host_scheme}://localhost:${final_port}#g" "${INFLUX_CONFIGS_PATH}"
286295
}
287296

288297
# Run influxd, with optional setup logic.

influxdb/2.0/entrypoint.sh

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,16 +250,25 @@ function init_influxd () {
250250
return
251251
fi
252252

253-
# Capture final bind address, and check it is distinct from init addr
254253
local -r final_bind_addr="$(influxd print-config --key-name http-bind-address "${@}")"
255254
local -r init_bind_addr=":${INFLUXD_INIT_PORT}"
256255
if [ "${init_bind_addr}" = "${final_bind_addr}" ]; then
257256
log warn "influxd setup binding to same addr as final config, server will be exposed before ready" addr "${init_bind_addr}"
258257
fi
258+
local final_host_scheme="http"
259+
if [ "$(influxd print-config --key-name tls-cert "${@}")" != '""' ] && [ "$(influxd print-config --key-name tls-key "${@}")" != '""' ]; then
260+
final_host_scheme="https"
261+
fi
262+
263+
# Generate a config file with a known HTTP port, and TLS disabled.
264+
local -r init_config=/tmp/config.yml
265+
influxd print-config "${@}" | \
266+
sed -e "s#${final_bind_addr}#${init_bind_addr}#" -e '/^tls/d' > \
267+
"${init_config}"
259268

260269
# Start influxd in the background.
261270
log info "booting influxd server in the background"
262-
INFLUXD_HTTP_BIND_ADDRESS="${init_bind_addr}" influxd "${@}" &
271+
INFLUXD_CONFIG_PATH="${init_config}" INFLUXD_HTTP_BIND_ADDRESS="${init_bind_addr}" INFLUXD_TLS_CERT='' INFLUXD_TLS_KEY='' influxd &
263272
local -r influxd_init_pid="$!"
264273
trap "handle_signal TERM ${influxd_init_pid}" TERM
265274
trap "handle_signal INT ${influxd_init_pid}" INT
@@ -282,7 +291,7 @@ function init_influxd () {
282291

283292
# Rewrite the ClI configs to point at the server's final HTTP address.
284293
local -r final_port="$(echo "${final_bind_addr}" | sed -E 's#[^:]*:(.*)#\1#')"
285-
sed -i "s#http://localhost:${INFLUXD_INIT_PORT}#http://localhost:${final_port}#g" "${INFLUX_CONFIGS_PATH}"
294+
sed -i "s#http://localhost:${INFLUXD_INIT_PORT}#${final_host_scheme}://localhost:${final_port}#g" "${INFLUX_CONFIGS_PATH}"
286295
}
287296

288297
# Run influxd, with optional setup logic.

influxdb/test/cases/common.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,15 @@ function log_msg () {
4040
}
4141

4242
function wait_container_ready () {
43+
wait_container_ready_at_url localhost:8086/health
44+
}
45+
46+
function wait_container_ready_at_url () {
47+
local -r url=$1
4348
local attempt_count=0
4449

4550
while [ ${attempt_count} -lt ${ATTEMPTS} ]; do
46-
if curl -s localhost:8086/health >/dev/null; then
51+
if curl -k -s "${url}" >/dev/null; then
4752
return 0
4853
fi
4954
sleep 2
@@ -54,7 +59,7 @@ function wait_container_ready () {
5459
}
5560

5661
function extract_token () {
57-
docker exec -i ${1} influx auth list --user ${TEST_USER} --hide-headers | cut -f 3
62+
docker exec -i ${1} influx auth list --skip-verify --user ${TEST_USER} --hide-headers | cut -f 3
5863
}
5964

6065
function join_array () {

influxdb/test/cases/test-auto-setup-init-bind

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,18 @@ declare -ra docker_run_influxd=(
1818
-e DOCKER_INFLUXDB_INIT_PASSWORD=${TEST_PASSWORD}
1919
-e DOCKER_INFLUXDB_INIT_ORG=${TEST_ORG}
2020
-e DOCKER_INFLUXDB_INIT_BUCKET=${TEST_BUCKET}
21-
-e INFLUXD_HTTP_BIND_ADDRESS=:3333
21+
-e INFLUXD_HTTP_BIND_ADDRESS=:2222
2222
-e INFLUXD_INIT_PORT=9998
2323
influxdb:${tag} influxd run
24+
# NOTE: The CLI arg here is redundant with the INFLUXD_HTTP_BIND_ADDRESS env var above.
25+
# We include both because `entrypoint.sh` needs to cover both when overriding the bind-
26+
# address used by the "init" instance of the server. The initial implementation of the
27+
# script covered the CLI arg, but missed the env var. A follow-up fix covered the env
28+
# var but dropped coverage for the CLI arg. Now we test for both.
29+
#
30+
# The CLI arg is expected to "win" once the final server boots up, according to our config
31+
# precedence rules.
32+
--http-bind-address ":3333"
2433
)
2534

2635
# Boot the container
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/bin/bash
2+
set -eo pipefail
3+
4+
declare -r SCRIPT_DIR=$(cd $(dirname $0) >/dev/null 2>&1 && pwd)
5+
source ${SCRIPT_DIR}/common.sh
6+
7+
declare -r tag=$1 container_name=$2 data=$3 config=$4 logs=$5
8+
9+
# Generate a self-signed TLS cert.
10+
openssl req -batch -new \
11+
-newkey rsa:4096 \
12+
-x509 \
13+
-sha256 \
14+
-days 1 \
15+
-nodes \
16+
-subj "/C=US/ST=CA/L=./O=./OU=./CN=." \
17+
-out "${config}/tls.crt" \
18+
-keyout "${config}/tls.key"
19+
20+
declare -ra docker_run_influxd=(
21+
docker run -i -d
22+
--name=${container_name}
23+
-u $(id -u):influxdb
24+
-p 443:8086
25+
-v ${data}:/var/lib/influxdb2
26+
-v ${config}:/etc/influxdb2
27+
-e DOCKER_INFLUXDB_INIT_MODE=setup
28+
-e DOCKER_INFLUXDB_INIT_USERNAME=${TEST_USER}
29+
-e DOCKER_INFLUXDB_INIT_PASSWORD=${TEST_PASSWORD}
30+
-e DOCKER_INFLUXDB_INIT_ORG=${TEST_ORG}
31+
-e DOCKER_INFLUXDB_INIT_BUCKET=${TEST_BUCKET}
32+
-e INFLUXD_TLS_CERT=/etc/influxdb2/tls.crt \
33+
-e INFLUXD_TLS_KEY=/etc/influxdb2/tls.key \
34+
influxdb:${tag} influxd
35+
)
36+
37+
log_msg Booting 2.x container in setup mode
38+
if ! ${docker_run_influxd[@]} > /dev/null; then
39+
log_msg Error: Failed to launch container
40+
exit 1
41+
fi
42+
wait_container_ready_at_url "https://localhost/health"
43+
44+
# Check that the DB reports it's been set up.
45+
log_msg Checking onboarding API post-start
46+
declare onboarding_allowed=$(curl -sk https://localhost/api/v2/setup | jq .allowed)
47+
if [[ ${onboarding_allowed} != 'false' ]]; then
48+
log_msg Error: Onboarding allowed post-start
49+
exit 1
50+
fi
51+
52+
# Get the auth token generated by setup.
53+
declare -r auth_token=$(extract_token ${container_name})
54+
55+
# Make sure we can use the generated auth token to find the resources we expect.
56+
log_msg Checking org list post-setup
57+
declare orgs=$(curl -sk -H "Authorization: Token ${auth_token}" https://localhost/api/v2/orgs | jq -r .orgs[].name)
58+
if [[ ${orgs} != ${TEST_ORG} ]]; then
59+
log_msg Error: Bad org list post-setup
60+
echo ${orgs}
61+
exit 1
62+
fi
63+
64+
log_msg Checking bucket list post-setup
65+
declare buckets=$(curl -sk -H "Authorization: Token ${auth_token}" "https://localhost/api/v2/buckets?name=${TEST_BUCKET}" | jq -r .buckets[].name)
66+
if [[ ${buckets} != ${TEST_BUCKET} ]]; then
67+
log_msg Error: Bad bucket list post-setup
68+
echo ${buckets}
69+
exit 1
70+
fi

0 commit comments

Comments
 (0)