From 7ff9308e6da25c0b05f5a6a28fc901e3a83f553e Mon Sep 17 00:00:00 2001 From: Alvin Huang Date: Mon, 17 Jun 2019 11:48:29 -0400 Subject: [PATCH] Support relative and external URL rewrites (#5970) * switch to relative path redirects for non external links * update website deploy script to support relative+full url redirects --- website/scripts/deploy.sh | 79 +++++++++++++++++++++++++----------- website/source/redirects.txt | 32 +++++++-------- 2 files changed, 72 insertions(+), 39 deletions(-) diff --git a/website/scripts/deploy.sh b/website/scripts/deploy.sh index 4c4c49c7a..235dd859d 100755 --- a/website/scripts/deploy.sh +++ b/website/scripts/deploy.sh @@ -4,8 +4,37 @@ set -e PROJECT="consul" PROJECT_URL="www.consul.io" FASTLY_SERVICE_ID="7GrxRJP3PVBuqQbyxYQ0MV" -FASTLY_DICTIONARY_ID="7d0yAgSHAQ2efWKeUC3kqW" REDIRECTS_FILE="./source/redirects.txt" +EXTERNAL_REDIRECTS_DICT_ID="6jIf0fzbdc8TQerhjkPBc4" +RELATIVE_REDIRECTS_DICT_ID="431fGfpCzjVIESAwTBVjlf" + +# This function posts a dictionary of key-value pairs for redirects to Fastly +function post_redirects { + # Arguments: + # $1 - jq_query + # $2 - dictionary_id + # $3 - jq_args + # + # Returns: + # 0 - success + # * - failure + + declare -a arr=("${!3}") + + # Do not post empty items (the API gets sad) + if [ "${#arr[@]}" -ne 0 ]; then + json="$(jq "${arr[@]}" "$1" <<<'{"items": []}')" + + # Post the JSON body + curl \ + --request "PATCH" \ + --header "Fastly-Key: $FASTLY_API_KEY" \ + --header "Content-type: application/json" \ + --header "Accept: application/json" \ + --data "$json"\ + "https://api.fastly.com/service/$FASTLY_SERVICE_ID/dictionary/$2/items" + fi +} # Ensure the proper AWS environment variables are set if [ -z "$AWS_ACCESS_KEY_ID" ]; then @@ -137,30 +166,34 @@ if [ -z "$NO_REDIRECTS" ] || [ ! test -f $REDIRECTS_FILE ]; then done # Build the payload for single-request updates. - jq_args=() - jq_query="." + jq_args_external=() + jq_query_external="." + jq_args_relative=() + jq_query_relative="." for (( i=0; i<${#fields[@]}; i+=2 )); do - original="${fields[i]}" - redirect="${fields[i+1]}" - echo "Redirecting ${original} -> ${redirect}" - jq_args+=(--arg "key$((i/2))" "${original}") - jq_args+=(--arg "value$((i/2))" "${redirect}") - jq_query+="| .items |= (. + [{op: \"upsert\", item_key: \$key$((i/2)), item_value: \$value$((i/2))}])" + # if the redirect is external, add the entries to the external list + if [[ "${fields[i+1]}" =~ http[s]*:\/\/ ]]; then + external_original="${fields[i]}" + external_redirect="${fields[i+1]}" + echo "Redirecting external ${external_original} -> ${external_redirect}" + # The key and value indexes are for the whole redirects file so it may not be contiguous + # This doesn't matter at the end as long as the query matches the same key/value items + jq_args_external+=(--arg "key$((i/2))" "${external_original}") + jq_args_external+=(--arg "value$((i/2))" "${external_redirect}") + jq_query_external+="| .items |= (. + [{op: \"upsert\", item_key: \$key$((i/2)), item_value: \$value$((i/2))}])" + else + relative_original="${fields[i]}" + relative_redirect="${fields[i+1]}" + echo "Redirecting relative ${relative_original} -> ${relative_redirect}" + # The key and value indexes are for the whole redirects file so it may not be contiguous + # This doesn't matter at the end as long as the query matches the same key/value items + jq_args_relative+=(--arg "key$((i/2))" "${relative_original}") + jq_args_relative+=(--arg "value$((i/2))" "${relative_redirect}") + jq_query_relative+="| .items |= (. + [{op: \"upsert\", item_key: \$key$((i/2)), item_value: \$value$((i/2))}])" + fi done - - # Do not post empty items (the API gets sad) - if [ "${#jq_args[@]}" -ne 0 ]; then - json="$(jq "${jq_args[@]}" "${jq_query}" <<<'{"items": []}')" - - # Post the JSON body - curl \ - --request "PATCH" \ - --header "Fastly-Key: $FASTLY_API_KEY" \ - --header "Content-type: application/json" \ - --header "Accept: application/json" \ - --data "$json"\ - "https://api.fastly.com/service/$FASTLY_SERVICE_ID/dictionary/$FASTLY_DICTIONARY_ID/items" - fi + post_redirects "$jq_query_external" $EXTERNAL_REDIRECTS_DICT_ID jq_args_external[@] + post_redirects "$jq_query_relative" $RELATIVE_REDIRECTS_DICT_ID jq_args_relative[@] fi # Perform a purge of the surrogate key. diff --git a/website/source/redirects.txt b/website/source/redirects.txt index 7a02fa88e..99f264173 100644 --- a/website/source/redirects.txt +++ b/website/source/redirects.txt @@ -38,24 +38,24 @@ # # Consul Redirects -/api.html https://www.consul.io/api/index.html -/api/acl.html https://www.consul.io/api/acl/acl.html -/docs/agent/acl-rules.html https://www.consul.io/docs/acl/acl-rules.html -/docs/agent/acl-system.html https://www.consul.io/docs/acl/acl-system.html -/docs/agent/http.html https://www.consul.io/api/index.html -/docs/guides/acl-legacy.html https://www.consul.io/docs/acl/acl-legacy.html -/docs/guide/acl-migrate-tokens.html https://www.consul.io/docs/acl/acl-migrate-tokens.html -/docs/guides/acl-migrate-tokens.html https://www.consul.io/docs/acl/acl-migrate-tokens.html -/docs/guides/bootstrapping.html https://www.consul.io/docs/install/bootstrapping.html -/docs/guides/sentinel.html https://www.consul.io/docs/agent/sentinel.html -/docs/connect/proxies/sidecar-service.html https://www.consul.io/docs/connect/registration/sidecar-service.html +/api.html /api/index.html +/api/acl.html /api/acl/acl.html +/docs/agent/acl-rules.html /docs/acl/acl-rules.html +/docs/agent/acl-system.html /docs/acl/acl-system.html +/docs/agent/http.html /api/index.html +/docs/guides/acl-legacy.html /docs/acl/acl-legacy.html +/docs/guide/acl-migrate-tokens.html /docs/acl/acl-migrate-tokens.html +/docs/guides/acl-migrate-tokens.html /docs/acl/acl-migrate-tokens.html +/docs/guides/bootstrapping.html /docs/install/bootstrapping.html +/docs/guides/sentinel.html /docs/agent/sentinel.html +/docs/connect/proxies/sidecar-service.html /docs/connect/registration/sidecar-service.html # CLI renames -/docs/commands/acl/acl-bootstrap.html https://www.consul.io/docs/commands/acl/bootstrap.html -/docs/commands/acl/acl-policy.html https://www.consul.io/docs/commands/acl/policy.html -/docs/commands/acl/acl-set-agent-token.html https://www.consul.io/docs/commands/acl/set-agent-token.html -/docs/commands/acl/acl-token.html https://www.consul.io/docs/commands/acl/token.html -/docs/commands/acl/acl-translate-rules.html https://www.consul.io/docs/commands/acl/translate-rules.html +/docs/commands/acl/acl-bootstrap.html /docs/commands/acl/bootstrap.html +/docs/commands/acl/acl-policy.html /docs/commands/acl/policy.html +/docs/commands/acl/acl-set-agent-token.html /docs/commands/acl/set-agent-token.html +/docs/commands/acl/acl-token.html /docs/commands/acl/token.html +/docs/commands/acl/acl-translate-rules.html /docs/commands/acl/translate-rules.html # Consul Learn Redirects /docs/guides/acl.html https://learn.hashicorp.com/consul/security-networking/production-acls