Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle empty or invalid Sumo base URL #2240

Merged
merged 9 commits into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- fix: set source name and category in the FluentD output for events [#2222][#2222]
- fix: proper handling of empty `sumologic.endpoint` in the setup script
sumo-drosiek marked this conversation as resolved.
Show resolved Hide resolved
- docs: FluentD buffer size configuration [#2232][#2232]

[#2232]: https://github.com/SumoLogic/sumologic-kubernetes-collection/pull/2232
sumo-drosiek marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
40 changes: 33 additions & 7 deletions deploy/helm/sumologic/conf/setup/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ readonly DEBUG_MODE_ENABLED_FLAG="true"

# Let's compare the variables ignoring the case with help of ${VARIABLE,,} which makes the string lowercased
# so that we don't have to deal with True vs true vs TRUE
if [[ ${DEBUG_MODE,,} == ${DEBUG_MODE_ENABLED_FLAG} ]]; then
if [[ ${DEBUG_MODE,,} == "${DEBUG_MODE_ENABLED_FLAG}" ]]; then
echo "Entering the debug mode with continuous sleep. No setup will be performed."
echo "Please exec into the setup container and run the setup.sh by hand or set the sumologic.setup.debug=false and reinstall."

Expand All @@ -15,18 +15,41 @@ if [[ ${DEBUG_MODE,,} == ${DEBUG_MODE_ENABLED_FLAG} ]]; then
done
fi

# Fix URL to remove "v1" or "v1/"
export SUMOLOGIC_BASE_URL=${SUMOLOGIC_BASE_URL%v1*}
function fix_sumo_base_url() {
local BASE_URL
BASE_URL=${SUMOLOGIC_BASE_URL}

if [[ "${BASE_URL}" =~ ^\s*$ ]]; then
BASE_URL="https://api.sumologic.com/api/"
fi

OPTIONAL_REDIRECTION="$(curl -XGET -s -o /dev/null -D - \
-u "${SUMOLOGIC_ACCESSID}:${SUMOLOGIC_ACCESSKEY}" \
"${BASE_URL}"v1/collectors \
| grep -Fi location )"

if [[ ! ${OPTIONAL_REDIRECTION} =~ ^\s*$ ]]; then
BASE_URL=$( echo "${OPTIONAL_REDIRECTION}" | sed -E 's/.*: (https:\/\/.*(au|ca|de|eu|fed|in|jp|us2)?\.sumologic\.com\/api\/).*/\1/' )
fi

BASE_URL=${BASE_URL%v1*}

echo "${BASE_URL}"
}

SUMOLOGIC_BASE_URL=$(fix_sumo_base_url)
export SUMOLOGIC_BASE_URL
# Support proxy for Terraform
export HTTP_PROXY=${HTTP_PROXY:=""}
export HTTPS_PROXY=${HTTPS_PROXY:=""}
export NO_PROXY=${NO_PROXY:=""}

function get_remaining_fields() {
local RESPONSE
readonly RESPONSE="$(curl -XGET -s \
RESPONSE="$(curl -XGET -s \
-u "${SUMOLOGIC_ACCESSID}:${SUMOLOGIC_ACCESSKEY}" \
"${SUMOLOGIC_BASE_URL}"v1/fields/quota)"
readonly RESPONSE

echo "${RESPONSE}"
}
Expand All @@ -35,7 +58,8 @@ function get_remaining_fields() {
# would be created for the collection
function should_create_fields() {
local RESPONSE
readonly RESPONSE=$(get_remaining_fields)
RESPONSE=$(get_remaining_fields)
readonly RESPONSE

if ! jq -e <<< "${RESPONSE}" ; then
printf "Failed requesting fields API:\n%s\n" "${RESPONSE}"
Expand All @@ -48,7 +72,8 @@ function should_create_fields() {
fi

local REMAINING
readonly REMAINING=$(jq -e '.remaining' <<< "${RESPONSE}")
REMAINING=$(jq -e '.remaining' <<< "${RESPONSE}")
readonly REMAINING
if [[ $(( REMAINING - {{ len .Values.sumologic.logs.fields }} )) -ge 10 ]] ; then
return 0
else
Expand All @@ -66,9 +91,10 @@ terraform init -input=false -get=false || terraform init -input=false -upgrade
# Sumo Logic fields
if should_create_fields ; then
readonly CREATE_FIELDS=1
readonly FIELDS_RESPONSE="$(curl -XGET -s \
FIELDS_RESPONSE="$(curl -XGET -s \
-u "${SUMOLOGIC_ACCESSID}:${SUMOLOGIC_ACCESSKEY}" \
"${SUMOLOGIC_BASE_URL}"v1/fields | jq '.data[]' )"
readonly FIELDS_RESPONSE

declare -ra FIELDS=({{ include "helm-toolkit.utils.joinListWithSpaces" .Values.sumologic.logs.fields }})
for FIELD in "${FIELDS[@]}" ; do
Expand Down
40 changes: 33 additions & 7 deletions tests/helm/terraform/static/all_fields.output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ data:

# Let's compare the variables ignoring the case with help of ${VARIABLE,,} which makes the string lowercased
# so that we don't have to deal with True vs true vs TRUE
if [[ ${DEBUG_MODE,,} == ${DEBUG_MODE_ENABLED_FLAG} ]]; then
if [[ ${DEBUG_MODE,,} == "${DEBUG_MODE_ENABLED_FLAG}" ]]; then
echo "Entering the debug mode with continuous sleep. No setup will be performed."
echo "Please exec into the setup container and run the setup.sh by hand or set the sumologic.setup.debug=false and reinstall."

Expand All @@ -298,18 +298,41 @@ data:
done
fi

# Fix URL to remove "v1" or "v1/"
export SUMOLOGIC_BASE_URL=${SUMOLOGIC_BASE_URL%v1*}
function fix_sumo_base_url() {
local BASE_URL
BASE_URL=${SUMOLOGIC_BASE_URL}

if [[ "${BASE_URL}" =~ ^\s*$ ]]; then
BASE_URL="https://api.sumologic.com/api/"
fi

OPTIONAL_REDIRECTION="$(curl -XGET -s -o /dev/null -D - \
-u "${SUMOLOGIC_ACCESSID}:${SUMOLOGIC_ACCESSKEY}" \
"${BASE_URL}"v1/collectors \
| grep -Fi location )"

if [[ ! ${OPTIONAL_REDIRECTION} =~ ^\s*$ ]]; then
BASE_URL=$( echo "${OPTIONAL_REDIRECTION}" | sed -E 's/.*: (https:\/\/.*(au|ca|de|eu|fed|in|jp|us2)?\.sumologic\.com\/api\/).*/\1/' )
fi

BASE_URL=${BASE_URL%v1*}

echo "${BASE_URL}"
}

SUMOLOGIC_BASE_URL=$(fix_sumo_base_url)
export SUMOLOGIC_BASE_URL
# Support proxy for Terraform
export HTTP_PROXY=${HTTP_PROXY:=""}
export HTTPS_PROXY=${HTTPS_PROXY:=""}
export NO_PROXY=${NO_PROXY:=""}

function get_remaining_fields() {
local RESPONSE
readonly RESPONSE="$(curl -XGET -s \
RESPONSE="$(curl -XGET -s \
-u "${SUMOLOGIC_ACCESSID}:${SUMOLOGIC_ACCESSKEY}" \
"${SUMOLOGIC_BASE_URL}"v1/fields/quota)"
readonly RESPONSE

echo "${RESPONSE}"
}
Expand All @@ -318,7 +341,8 @@ data:
# would be created for the collection
function should_create_fields() {
local RESPONSE
readonly RESPONSE=$(get_remaining_fields)
RESPONSE=$(get_remaining_fields)
readonly RESPONSE

if ! jq -e <<< "${RESPONSE}" ; then
printf "Failed requesting fields API:\n%s\n" "${RESPONSE}"
Expand All @@ -331,7 +355,8 @@ data:
fi

local REMAINING
readonly REMAINING=$(jq -e '.remaining' <<< "${RESPONSE}")
REMAINING=$(jq -e '.remaining' <<< "${RESPONSE}")
readonly REMAINING
if [[ $(( REMAINING - 8 )) -ge 10 ]] ; then
return 0
else
Expand All @@ -349,9 +374,10 @@ data:
# Sumo Logic fields
if should_create_fields ; then
readonly CREATE_FIELDS=1
readonly FIELDS_RESPONSE="$(curl -XGET -s \
FIELDS_RESPONSE="$(curl -XGET -s \
-u "${SUMOLOGIC_ACCESSID}:${SUMOLOGIC_ACCESSKEY}" \
"${SUMOLOGIC_BASE_URL}"v1/fields | jq '.data[]' )"
readonly FIELDS_RESPONSE

declare -ra FIELDS=("cluster" "container" "deployment" "host" "namespace" "node" "pod" "service")
for FIELD in "${FIELDS[@]}" ; do
Expand Down
40 changes: 33 additions & 7 deletions tests/helm/terraform/static/collector_fields.output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ data:

# Let's compare the variables ignoring the case with help of ${VARIABLE,,} which makes the string lowercased
# so that we don't have to deal with True vs true vs TRUE
if [[ ${DEBUG_MODE,,} == ${DEBUG_MODE_ENABLED_FLAG} ]]; then
if [[ ${DEBUG_MODE,,} == "${DEBUG_MODE_ENABLED_FLAG}" ]]; then
echo "Entering the debug mode with continuous sleep. No setup will be performed."
echo "Please exec into the setup container and run the setup.sh by hand or set the sumologic.setup.debug=false and reinstall."

Expand All @@ -253,18 +253,41 @@ data:
done
fi

# Fix URL to remove "v1" or "v1/"
export SUMOLOGIC_BASE_URL=${SUMOLOGIC_BASE_URL%v1*}
function fix_sumo_base_url() {
local BASE_URL
BASE_URL=${SUMOLOGIC_BASE_URL}

if [[ "${BASE_URL}" =~ ^\s*$ ]]; then
BASE_URL="https://api.sumologic.com/api/"
fi

OPTIONAL_REDIRECTION="$(curl -XGET -s -o /dev/null -D - \
-u "${SUMOLOGIC_ACCESSID}:${SUMOLOGIC_ACCESSKEY}" \
"${BASE_URL}"v1/collectors \
| grep -Fi location )"

if [[ ! ${OPTIONAL_REDIRECTION} =~ ^\s*$ ]]; then
BASE_URL=$( echo "${OPTIONAL_REDIRECTION}" | sed -E 's/.*: (https:\/\/.*(au|ca|de|eu|fed|in|jp|us2)?\.sumologic\.com\/api\/).*/\1/' )
fi

BASE_URL=${BASE_URL%v1*}

echo "${BASE_URL}"
}

SUMOLOGIC_BASE_URL=$(fix_sumo_base_url)
export SUMOLOGIC_BASE_URL
# Support proxy for Terraform
export HTTP_PROXY=${HTTP_PROXY:=""}
export HTTPS_PROXY=${HTTPS_PROXY:=""}
export NO_PROXY=${NO_PROXY:=""}

function get_remaining_fields() {
local RESPONSE
readonly RESPONSE="$(curl -XGET -s \
RESPONSE="$(curl -XGET -s \
-u "${SUMOLOGIC_ACCESSID}:${SUMOLOGIC_ACCESSKEY}" \
"${SUMOLOGIC_BASE_URL}"v1/fields/quota)"
readonly RESPONSE

echo "${RESPONSE}"
}
Expand All @@ -273,7 +296,8 @@ data:
# would be created for the collection
function should_create_fields() {
local RESPONSE
readonly RESPONSE=$(get_remaining_fields)
RESPONSE=$(get_remaining_fields)
readonly RESPONSE

if ! jq -e <<< "${RESPONSE}" ; then
printf "Failed requesting fields API:\n%s\n" "${RESPONSE}"
Expand All @@ -286,7 +310,8 @@ data:
fi

local REMAINING
readonly REMAINING=$(jq -e '.remaining' <<< "${RESPONSE}")
REMAINING=$(jq -e '.remaining' <<< "${RESPONSE}")
readonly REMAINING
if [[ $(( REMAINING - 8 )) -ge 10 ]] ; then
return 0
else
Expand All @@ -304,9 +329,10 @@ data:
# Sumo Logic fields
if should_create_fields ; then
readonly CREATE_FIELDS=1
readonly FIELDS_RESPONSE="$(curl -XGET -s \
FIELDS_RESPONSE="$(curl -XGET -s \
-u "${SUMOLOGIC_ACCESSID}:${SUMOLOGIC_ACCESSKEY}" \
"${SUMOLOGIC_BASE_URL}"v1/fields | jq '.data[]' )"
readonly FIELDS_RESPONSE

declare -ra FIELDS=("cluster" "container" "deployment" "host" "namespace" "node" "pod" "service")
for FIELD in "${FIELDS[@]}" ; do
Expand Down
40 changes: 33 additions & 7 deletions tests/helm/terraform/static/conditional_sources.output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ data:

# Let's compare the variables ignoring the case with help of ${VARIABLE,,} which makes the string lowercased
# so that we don't have to deal with True vs true vs TRUE
if [[ ${DEBUG_MODE,,} == ${DEBUG_MODE_ENABLED_FLAG} ]]; then
if [[ ${DEBUG_MODE,,} == "${DEBUG_MODE_ENABLED_FLAG}" ]]; then
echo "Entering the debug mode with continuous sleep. No setup will be performed."
echo "Please exec into the setup container and run the setup.sh by hand or set the sumologic.setup.debug=false and reinstall."

Expand All @@ -181,18 +181,41 @@ data:
done
fi

# Fix URL to remove "v1" or "v1/"
export SUMOLOGIC_BASE_URL=${SUMOLOGIC_BASE_URL%v1*}
function fix_sumo_base_url() {
local BASE_URL
BASE_URL=${SUMOLOGIC_BASE_URL}

if [[ "${BASE_URL}" =~ ^\s*$ ]]; then
BASE_URL="https://api.sumologic.com/api/"
fi

OPTIONAL_REDIRECTION="$(curl -XGET -s -o /dev/null -D - \
-u "${SUMOLOGIC_ACCESSID}:${SUMOLOGIC_ACCESSKEY}" \
"${BASE_URL}"v1/collectors \
| grep -Fi location )"

if [[ ! ${OPTIONAL_REDIRECTION} =~ ^\s*$ ]]; then
BASE_URL=$( echo "${OPTIONAL_REDIRECTION}" | sed -E 's/.*: (https:\/\/.*(au|ca|de|eu|fed|in|jp|us2)?\.sumologic\.com\/api\/).*/\1/' )
fi

BASE_URL=${BASE_URL%v1*}

echo "${BASE_URL}"
}

SUMOLOGIC_BASE_URL=$(fix_sumo_base_url)
export SUMOLOGIC_BASE_URL
# Support proxy for Terraform
export HTTP_PROXY=${HTTP_PROXY:=""}
export HTTPS_PROXY=${HTTPS_PROXY:=""}
export NO_PROXY=${NO_PROXY:=""}

function get_remaining_fields() {
local RESPONSE
readonly RESPONSE="$(curl -XGET -s \
RESPONSE="$(curl -XGET -s \
-u "${SUMOLOGIC_ACCESSID}:${SUMOLOGIC_ACCESSKEY}" \
"${SUMOLOGIC_BASE_URL}"v1/fields/quota)"
readonly RESPONSE

echo "${RESPONSE}"
}
Expand All @@ -201,7 +224,8 @@ data:
# would be created for the collection
function should_create_fields() {
local RESPONSE
readonly RESPONSE=$(get_remaining_fields)
RESPONSE=$(get_remaining_fields)
readonly RESPONSE

if ! jq -e <<< "${RESPONSE}" ; then
printf "Failed requesting fields API:\n%s\n" "${RESPONSE}"
Expand All @@ -214,7 +238,8 @@ data:
fi

local REMAINING
readonly REMAINING=$(jq -e '.remaining' <<< "${RESPONSE}")
REMAINING=$(jq -e '.remaining' <<< "${RESPONSE}")
readonly REMAINING
if [[ $(( REMAINING - 8 )) -ge 10 ]] ; then
return 0
else
Expand All @@ -232,9 +257,10 @@ data:
# Sumo Logic fields
if should_create_fields ; then
readonly CREATE_FIELDS=1
readonly FIELDS_RESPONSE="$(curl -XGET -s \
FIELDS_RESPONSE="$(curl -XGET -s \
-u "${SUMOLOGIC_ACCESSID}:${SUMOLOGIC_ACCESSKEY}" \
"${SUMOLOGIC_BASE_URL}"v1/fields | jq '.data[]' )"
readonly FIELDS_RESPONSE

declare -ra FIELDS=("cluster" "container" "deployment" "host" "namespace" "node" "pod" "service")
for FIELD in "${FIELDS[@]}" ; do
Expand Down
Loading