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

escalate plugin: handle origin not reachable #11847

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
102 changes: 65 additions & 37 deletions plugins/escalate/escalate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
#include <iterator>
#include <map>

#include "swoc/IPEndpoint.h"
#include "swoc/TextView.h"

// Constants and some declarations

const char PLUGIN_NAME[] = "escalate";
Expand Down Expand Up @@ -72,12 +75,19 @@ struct EscalationState {
char *
MakeEscalateUrl(TSMBuffer mbuf, TSMLoc url, const char *host, size_t host_len, int &url_len)
{
char *url_str = nullptr;

swoc::TextView input_host_view{host, host_len};
std::string_view host_view;
std::string_view port_view;
swoc::IPEndpoint::tokenize(input_host_view, &host_view, &port_view);
// Update the request URL with the new Host to try.
TSUrlHostSet(mbuf, url, host, host_len);
url_str = TSUrlStringGet(mbuf, url, &url_len);
Dbg(dbg_ctl, "Setting new URL to %.*s", url_len, url_str);
TSUrlHostSet(mbuf, url, host_view.data(), host_view.size());
if (port_view.size()) {
int const port_int = swoc::svtou(port_view);
TSUrlPortSet(mbuf, url, port_int);
Dbg(dbg_ctl, "Setting port to %d", port_int);
}
char *url_str = TSUrlStringGet(mbuf, url, &url_len);
Dbg(dbg_ctl, "Setting new URL from configured %.*s to %.*s", (int)host_len, host, url_len, url_str);

return url_str;
}
Expand All @@ -88,67 +98,84 @@ MakeEscalateUrl(TSMBuffer mbuf, TSMLoc url, const char *host, size_t host_len, i
static int
EscalateResponse(TSCont cont, TSEvent event, void *edata)
{
TSHttpTxn txn = static_cast<TSHttpTxn>(edata);
EscalationState *es = static_cast<EscalationState *>(TSContDataGet(cont));
EscalationState::StatusMapType::const_iterator entry;
TSMBuffer mbuf;
TSMLoc hdrp, url;
TSHttpStatus status;
char *url_str = nullptr;
int url_len, tries;

TSAssert(event == TS_EVENT_HTTP_READ_RESPONSE_HDR);

// First, we need the server response ...
if (TS_SUCCESS != TSHttpTxnServerRespGet(txn, &mbuf, &hdrp)) {
goto no_action;
TSHttpTxn txn = static_cast<TSHttpTxn>(edata);
EscalationState *es = static_cast<EscalationState *>(TSContDataGet(cont));
TSMBuffer mbuf;
TSMLoc hdrp, url;

TSAssert(event == TS_EVENT_HTTP_READ_RESPONSE_HDR || event == TS_EVENT_HTTP_SEND_RESPONSE_HDR);
bool const processing_connection_error = (event == TS_EVENT_HTTP_SEND_RESPONSE_HDR);

if (processing_connection_error) {
TSServerState const state = TSHttpTxnServerStateGet(txn);
if (state == TS_SRVSTATE_CONNECTION_ALIVE) {
// There is no connection error, so nothing to do.
TSHttpTxnReenable(txn, TS_EVENT_HTTP_CONTINUE);
return TS_EVENT_NONE;
}
}

tries = TSHttpTxnRedirectRetries(txn);
int const tries = TSHttpTxnRedirectRetries(txn);
if (0 != tries) { // ToDo: Future support for more than one retry-URL
goto no_action;
Dbg(dbg_ctl, "Not pursuing failover due previous redirect already, num tries: %d", tries);
TSHttpTxnReenable(txn, TS_EVENT_HTTP_CONTINUE);
return TS_EVENT_NONE;
}

int ret = 0;
if (processing_connection_error) {
ret = TSHttpTxnClientRespGet(txn, &mbuf, &hdrp);
} else {
ret = TSHttpTxnServerRespGet(txn, &mbuf, &hdrp);
}
if (TS_SUCCESS != ret) {
TSHttpTxnReenable(txn, TS_EVENT_HTTP_CONTINUE);
return TS_EVENT_NONE;
}
Dbg(dbg_ctl, "This is try %d, proceeding", tries);

// Next, the response status ...
status = TSHttpHdrStatusGet(mbuf, hdrp);
TSHandleMLocRelease(mbuf, TS_NULL_MLOC, hdrp); // Don't need this any more
TSHttpStatus const status = TSHttpHdrStatusGet(mbuf, hdrp);
TSHandleMLocRelease(mbuf, TS_NULL_MLOC, hdrp);

// See if we have an escalation retry config for this response code
entry = es->status_map.find(static_cast<unsigned>(status));
// See if we have an escalation retry config for this response code.
auto const entry = es->status_map.find(status);
if (entry == es->status_map.end()) {
goto no_action;
TSHttpTxnReenable(txn, TS_EVENT_HTTP_CONTINUE);
return TS_EVENT_NONE;
}

Dbg(dbg_ctl, "Found an entry for HTTP status %u", static_cast<unsigned>(status));
if (EscalationState::RETRY_URL == entry->second.type) {
url_str = TSstrdup(entry->second.target.c_str());
url_len = entry->second.target.size();
EscalationState::RetryInfo const &retry_info = entry->second;

Dbg(dbg_ctl, "Handling failover redirect for HTTP status %d", status);
char const *url_str = nullptr;
int url_len = 0;
if (EscalationState::RETRY_URL == retry_info.type) {
url_str = TSstrdup(retry_info.target.c_str());
url_len = retry_info.target.size();
Dbg(dbg_ctl, "Setting new URL to %.*s", url_len, url_str);
} else if (EscalationState::RETRY_HOST == entry->second.type) {
} else if (EscalationState::RETRY_HOST == retry_info.type) {
if (es->use_pristine) {
if (TS_SUCCESS == TSHttpTxnPristineUrlGet(txn, &mbuf, &url)) {
url_str = MakeEscalateUrl(mbuf, url, entry->second.target.c_str(), entry->second.target.size(), url_len);
url_str = MakeEscalateUrl(mbuf, url, retry_info.target.c_str(), retry_info.target.size(), url_len);
TSHandleMLocRelease(mbuf, TS_NULL_MLOC, url);
}
} else {
if (TS_SUCCESS == TSHttpTxnClientReqGet(txn, &mbuf, &hdrp)) {
if (TS_SUCCESS == TSHttpHdrUrlGet(mbuf, hdrp, &url)) {
url_str = MakeEscalateUrl(mbuf, url, entry->second.target.c_str(), entry->second.target.size(), url_len);
url_str = MakeEscalateUrl(mbuf, url, retry_info.target.c_str(), retry_info.target.size(), url_len);
}
// Release the request MLoc
TSHandleMLocRelease(mbuf, TS_NULL_MLOC, hdrp);
}
}
Dbg(dbg_ctl, "Setting host URL to %.*s", url_len, url_str);
}

// Now update the Redirect URL, if set
if (url_str) {
TSHttpTxnRedirectUrlSet(txn, url_str, url_len); // Transfers ownership
}

// Set the transaction free ...
no_action:
// Set the transaction free ...
TSHttpTxnReenable(txn, TS_EVENT_HTTP_CONTINUE);
return TS_EVENT_NONE;
}
Expand Down Expand Up @@ -229,5 +256,6 @@ TSRemapDoRemap(void *instance, TSHttpTxn txn, TSRemapRequestInfo * /* rri */)
EscalationState *es = static_cast<EscalationState *>(instance);

TSHttpTxnHookAdd(txn, TS_HTTP_READ_RESPONSE_HDR_HOOK, es->cont);
TSHttpTxnHookAdd(txn, TS_HTTP_SEND_RESPONSE_HDR_HOOK, es->cont);
return TSREMAP_NO_REMAP;
}
130 changes: 130 additions & 0 deletions tests/gold_tests/pluginTest/escalate/escalate.test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
'''
'''
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
from ports import get_port

Test.Summary = '''
Test the escalate plugin.
'''

Test.SkipUnless(Condition.PluginExists('escalate.so'))


class EscalateTest:
"""
Test the escalate plugin.
"""

_replay_original_file: str = 'escalate_original.replay.yaml'
_replay_failover_file: str = 'escalate_failover.replay.yaml'

def __init__(self):
'''Configure the test run.'''
tr = Test.AddTestRun('Test escalate plugin.')
self._setup_dns(tr)
self._setup_servers(tr)
self._setup_ts(tr)
self._setup_client(tr)

def _setup_dns(self, tr: 'Process') -> None:
'''Set up the DNS server.

:param tr: The test run to add the DNS server to.
'''
self._dns = tr.MakeDNServer(f"dns", default='127.0.0.1')

def _setup_servers(self, tr: 'Process') -> None:
'''Set up the origin and failover servers.

:param tr: The test run to add the servers to.
'''
tr.Setup.Copy(self._replay_original_file)
tr.Setup.Copy(self._replay_failover_file)
self._server_origin = tr.AddVerifierServerProcess(f"server_origin", self._replay_original_file)
self._server_failover = tr.AddVerifierServerProcess(f"server_failover", self._replay_failover_file)

self._server_origin.Streams.All += Testers.ContainsExpression(
'uuid: GET', "Verify the origin server received the GET request.")
self._server_origin.Streams.All += Testers.ContainsExpression(
'uuid: GET_chunked', "Verify the origin server GET request for chunked content.")
self._server_origin.Streams.All += Testers.ContainsExpression(
'uuid: GET_failed', "Verify the origin server received the GET request that it returns a 502 with.")
self._server_origin.Streams.All += Testers.ExcludesExpression(
'uuid: GET_down_origin', "Verify the origin server did not receive the down origin request.")

self._server_failover.Streams.All += Testers.ContainsExpression(
'uuid: GET_failed', "Verify the failover server received the failed GET request.")
self._server_failover.Streams.All += Testers.ContainsExpression(
'uuid: GET_down_origin', "Verify the failover server received the GET request for the down origin.")

self._server_failover.Streams.All += Testers.ExcludesExpression(
'x-request: first', "Verify the failover server did not receive the GET request.")
self._server_failover.Streams.All += Testers.ExcludesExpression(
'uuid: GET_chunked', "Verify the failover server did not receive the GET request for chunked content.")

def _setup_ts(self, tr: 'Process') -> None:
'''Set up Traffic Server.

:param tr: The test run to add Traffic Server to.
'''
self._ts = tr.MakeATSProcess(f"ts", enable_cache=False)
# Select a port that is guaranteed to not be used at the moment.
dead_port = get_port(self._ts, "dead_port")
self._ts.Disk.records_config.update(
{
'proxy.config.diags.debug.enabled': 1,
'proxy.config.diags.debug.tags': 'http|escalate',
'proxy.config.dns.nameservers': f'127.0.0.1:{self._dns.Variables.Port}',
'proxy.config.dns.resolv_conf': 'NULL',
'proxy.config.http.redirect.actions': 'self:follow',
'proxy.config.http.number_of_redirections': 4,
})
self._ts.Disk.remap_config.AddLines(
[
f'map http://origin.server.com http://backend.origin.server.com:{self._server_origin.Variables.http_port} '
f'@plugin=escalate.so @pparam=500,502:failover.server.com:{self._server_failover.Variables.http_port}',

# Now create remap entries for the multiplexed hosts: one that
# verifies HTTP, and another that verifies HTTPS.
f'map http://down_origin.server.com http://backend.down_origin.server.com:{dead_port} '
f'@plugin=escalate.so @pparam=500,502:failover.server.com:{self._server_failover.Variables.http_port} ',
])

def _setup_client(self, tr: 'Process') -> None:
'''Set up the client.

:param tr: The test run to add the client to.
'''
client = tr.AddVerifierClientProcess(f"client", self._replay_original_file, http_ports=[self._ts.Variables.port])
client.StartBefore(self._dns)
client.StartBefore(self._server_origin)
client.StartBefore(self._server_failover)
client.StartBefore(self._ts)

client.Streams.All += Testers.ExcludesExpression(r'\[ERROR\]', 'Verify there were no errors in the replay.')
client.Streams.All += Testers.ExcludesExpression('400 Bad', 'Verify none of the 400 responses make it to the client.')
client.Streams.All += Testers.ExcludesExpression('502 Bad', 'Verify none of the 502 responses make it to the client.')
client.Streams.All += Testers.ExcludesExpression('500 Internal', 'Verify none of the 500 responses make it to the client.')
client.Streams.All += Testers.ContainsExpression('x-response: first', 'Verify that the first response was received.')
client.Streams.All += Testers.ContainsExpression('x-response: second', 'Verify that the second response was received.')
client.Streams.All += Testers.ContainsExpression('x-response: third', 'Verify that the third response was received.')
client.Streams.All += Testers.ContainsExpression('x-response: fourth', 'Verify that the fourth response was received.')


EscalateTest()
Loading