Skip to content

Commit

Permalink
bin: make update swift ip simpler
Browse files Browse the repository at this point in the history
  • Loading branch information
robinAwallace committed Oct 8, 2024
1 parent 83f21bf commit 5ea9fde
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 27 deletions.
13 changes: 5 additions & 8 deletions bin/update-ips.bash
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,8 @@ get_swift_url() {
auth_url="$(yq_read "sc" "${swift_config_option}.authUrl" "")"
swift_region="$(yq_read "sc" "${swift_config_option}.region" "")"

headers=$(mktemp --suffix="_headers")
append_trap "rm ${headers}" EXIT

if [ -n "$(yq_read_secret "${swift_config_option}.username" "")" ]; then
response=$(curl -s -D "${headers}" -H "Content-Type: application/json" -d '
response=$(curl -s -i -H "Content-Type: application/json" -d '
{
"auth": {
"identity": {
Expand All @@ -208,7 +205,7 @@ get_swift_url() {
}
}' "${auth_url}/auth/tokens")
elif [ -n "$(yq_read_secret "${swift_config_option}.applicationCredentialID" "")" ]; then
response=$(curl -s -D "${headers}" -H "Content-Type: application/json" -d '
response=$(curl -s -i -H "Content-Type: application/json" -d '
{
"auth": {
"identity": {
Expand All @@ -224,9 +221,9 @@ get_swift_url() {
log_error "Could not find Swift credentials in ${swift_config_option}"
exit 1
fi

os_token=$(grep -oP "x-subject-token:\s+\K\S+" "${headers}")
swift_url=$(echo "${response}" | jq -r '.token.catalog[] | select( .type == "object-store" and .name == "swift") | .endpoints[] | select(.interface == "public" and .region == "'"${swift_region}"'") | .url')
body=$(echo "${response}" | tr '\r\n' '!' | sed 's/!!/\n/g' | tail -n 1 | tr '!!' '\n')
os_token=$(echo "${response}" | grep -oP "x-subject-token:\s+\K\S+")
swift_url=$(echo "${body}" | jq -r '.token.catalog[] | select( .type == "object-store" and .name == "swift") | .endpoints[] | select(.interface == "public" and .region == "'"${swift_region}"'") | .url')

curl -i -s -X DELETE -H "X-Auth-Token: ${os_token}" -H "X-Subject-Token: ${os_token}" "${auth_url}/auth/tokens" >/dev/null

Expand Down
18 changes: 3 additions & 15 deletions tests/common/bats/update-ips.bash
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,6 @@ update_ips.setup_mocks() {
export -f kubectl
}

update_ips.setup_mktemp_mock(){
mock_mktemp="$(mock_create)"
export mock_mktemp
mktemp() {
# shellcheck disable=SC2317
touch /tmp/tmp.mock_headers
# shellcheck disable=SC2317
"${mock_mktemp}" "${@}"
}
export -f mktemp

mock_set_output "${mock_mktemp}" /tmp/tmp.mock_headers
}

update_ips.assert_mocks_none() {
assert_equal "$(mock_get_call_num "${mock_curl}")" 0
assert_equal "$(mock_get_call_num "${mock_dig}")" 0
Expand Down Expand Up @@ -134,7 +120,9 @@ update_ips.mock_swift() {
update_ips.mock_minimal

# GET /auth/tokens
mock_set_output "${mock_curl}" '{"token":{"catalog":[{"type": "object-store", "name": "swift", "endpoints": [{"interface":"public", "region": "swift-region", "url": "https://swift.foo.dev-ck8s.com:91011"}]}]}}' 1
mock_set_output "${mock_curl}" 'x-subject-token: 123456789
{"token":{"catalog":[{"type": "object-store", "name": "swift", "endpoints": [{"interface":"public", "region": "swift-region", "url": "https://swift.foo.dev-ck8s.com:91011"}]}]}}' 1
mock_set_output "${mock_curl}" "" 2 # DELETE /auth/tokens

mock_set_output "${mock_dig}" "127.0.0.4" 4 # keystone endpoint
Expand Down
2 changes: 0 additions & 2 deletions tests/unit/general/bin-update-ips-rclone.bats
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,6 @@ _test_apply_rclone_sync_s3_and_swift() {
@test "ck8s update-ips gets swift url for rclone sync" {
_setup_swift '.objectStorage.sync.destinationType'

update_ips.setup_mktemp_mock

mock_set_output "${mock_dig}" ""

run ck8s update-ips both apply
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/general/bin-update-ips-swift.bats
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,12 @@ _test_apply_swift_application_credential_id() {
yq.set sc .harbor.persistence.type '"swift"'

update_ips.mock_swift
update_ips.setup_mktemp_mock

sops --set '["objectStorage"]["swift"]["username"] "swift-username"' "${CK8S_CONFIG_PATH}/secrets.yaml"
sops --set '["objectStorage"]["swift"]["password"] "swift-password"' "${CK8S_CONFIG_PATH}/secrets.yaml"

run ck8s update-ips both apply

assert_equal "$(mock_get_call_args "${mock_curl}" 1)" "$(cat "${BATS_TEST_DIRNAME}/resources/get-swift-url.out")"
assert_equal "$(mock_get_call_args "${mock_curl}" 2)" '-i -s -X DELETE -H X-Auth-Token: 123456789 -H X-Subject-Token: 123456789 https://keystone.foo.dev-ck8s.com:5678/auth/tokens'
}
2 changes: 1 addition & 1 deletion tests/unit/general/resources/get-swift-url.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-s -D /tmp/tmp.mock_headers -H Content-Type: application/json -d
-s -i -H Content-Type: application/json -d
{
"auth": {
"identity": {
Expand Down

0 comments on commit 5ea9fde

Please sign in to comment.