1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
|
/*
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.
*/
/**
* @file plugin.cc
* @brief traffic server plugin entry points.
*/
#include <ctime> /* strftime */
#include "common.h" /* Common definitions */
#include "config.h" /* AccessControlConfig */
#include "access_control.h" /* AccessToken */
#include "ts/remap.h" /* TSRemapInterface, TSRemapStatus, apiInfo */
#include "ts/ts.h" /* ATS API */
#include "utils.h" /* cryptoBase64Decode.* functions */
#include "headers.h" /* getHeader, setHeader, removeHeader */
static const std::string_view UNKNOWN{"unknown"};
static const char *
getEventName(TSEvent event)
{
switch (event) {
case TS_EVENT_HTTP_CONTINUE:
return "TS_EVENT_HTTP_CONTINUE";
case TS_EVENT_HTTP_ERROR:
return "TS_EVENT_HTTP_ERROR";
case TS_EVENT_HTTP_READ_REQUEST_HDR:
return "TS_EVENT_HTTP_READ_REQUEST_HDR";
case TS_EVENT_HTTP_OS_DNS:
return "TS_EVENT_HTTP_OS_DNS";
case TS_EVENT_HTTP_SEND_REQUEST_HDR:
return "TS_EVENT_HTTP_SEND_REQUEST_HDR";
case TS_EVENT_HTTP_READ_CACHE_HDR:
return "TS_EVENT_HTTP_READ_CACHE_HDR";
case TS_EVENT_HTTP_READ_RESPONSE_HDR:
return "TS_EVENT_HTTP_READ_RESPONSE_HDR";
case TS_EVENT_HTTP_SEND_RESPONSE_HDR:
return "TS_EVENT_HTTP_SEND_RESPONSE_HDR";
case TS_EVENT_HTTP_REQUEST_TRANSFORM:
return "TS_EVENT_HTTP_REQUEST_TRANSFORM";
case TS_EVENT_HTTP_RESPONSE_TRANSFORM:
return "TS_EVENT_HTTP_RESPONSE_TRANSFORM";
case TS_EVENT_HTTP_SELECT_ALT:
return "TS_EVENT_HTTP_SELECT_ALT";
case TS_EVENT_HTTP_TXN_START:
return "TS_EVENT_HTTP_TXN_START";
case TS_EVENT_HTTP_TXN_CLOSE:
return "TS_EVENT_HTTP_TXN_CLOSE";
case TS_EVENT_HTTP_SSN_START:
return "TS_EVENT_HTTP_SSN_START";
case TS_EVENT_HTTP_SSN_CLOSE:
return "TS_EVENT_HTTP_SSN_CLOSE";
case TS_EVENT_HTTP_CACHE_LOOKUP_COMPLETE:
return "TS_EVENT_HTTP_CACHE_LOOKUP_COMPLETE";
case TS_EVENT_HTTP_PRE_REMAP:
return "TS_EVENT_HTTP_PRE_REMAP";
case TS_EVENT_HTTP_POST_REMAP:
return "TS_EVENT_HTTP_POST_REMAP";
default:
return "UNHANDLED";
}
return "UNHANDLED";
}
/**
* @brief Plugin transaction data.
*/
class AccessControlTxnData
{
public:
AccessControlTxnData(AccessControlConfig *config) : _config(config) {}
const AccessControlConfig *_config; /** @brief pointer to the plugin config */
String _subject = ""; /** @brief subject for debugging purposes */
AccessTokenStatus _vaState = UNUSED; /** @brief VA access control token validation status */
AccessTokenStatus _originState = UNUSED; /** @brief Origin access control token validation status */
};
/**
* @brief Plugin initialization.
* @param apiInfo remap interface info pointer
* @param errBuf error message buffer
* @param errBufSize error message buffer size
* @return always TS_SUCCESS.
*/
TSReturnCode
TSRemapInit(TSRemapInterface *apiInfo, char *errBuf, int erroBufSize)
{
return TS_SUCCESS;
}
/**
* @brief Plugin new instance entry point.
*
* Processes the configuration and initializes the plugin instance.
* @param argc plugin arguments number
* @param argv plugin arguments
* @param instance new plugin instance pointer (initialized in this function)
* @param errBuf error message buffer
* @param errBufSize error message buffer size
* @return TS_SUCCES if success or TS_ERROR if failure
*/
TSReturnCode
TSRemapNewInstance(int argc, char *argv[], void **instance, char *errBuf, int errBufSize)
{
AccessControlConfig *config = new AccessControlConfig();
if (nullptr != config && config->init(argc, argv)) {
*instance = config;
} else {
AccessControlDebug("failed to initialize the " PLUGIN_NAME " plugin");
*instance = nullptr;
delete config;
return TS_ERROR;
}
return TS_SUCCESS;
}
/**
* @brief Plugin instance deletion clean-up entry point.
* @param plugin instance pointer.
*/
void
TSRemapDeleteInstance(void *instance)
{
AccessControlConfig *config = static_cast<AccessControlConfig *>(instance);
delete config;
}
/**
* @brief A mapping between various failures and HTTP status code and message to be returned to the UA.
* @param state Access Token validation status
* @param config pointer to the plugin configuration to get the desired response for each failure.
* @return HTTP status
*/
static TSHttpStatus
accessTokenStateToHttpStatus(AccessTokenStatus state, AccessControlConfig *config)
{
TSHttpStatus httpStatus = TS_HTTP_STATUS_NONE;
const char *message = "VALID";
switch (state) {
case VALID:
break;
case INVALID_SIGNATURE:
httpStatus = config->_invalidSignature;
message = "invalid signature";
break;
case UNUSED:
httpStatus = config->_internalError;
message = "uninitialized token";
break;
case INVALID_SECRET:
httpStatus = config->_internalError;
message = "failed to find secrets";
break;
case INVALID_SYNTAX:
case MISSING_REQUIRED_FIELD:
case INVALID_FIELD:
case INVALID_FIELD_VALUE:
case INVALID_VERSION:
case INVALID_HASH_FUNCTION:
case INVALID_KEYID:
httpStatus = config->_invalidSyntax;
message = "invalid syntax";
break;
case INVALID_SCOPE:
case OUT_OF_SCOPE:
httpStatus = config->_invalidScope;
message = "invalid scope";
break;
case TOO_EARLY:
case TOO_LATE:
httpStatus = config->_invalidTiming;
message = "invalid timing ";
break;
default:
/* Validation failed. */
httpStatus = config->_invalidRequest;
message = "unknown error";
break;
}
AccessControlDebug("token validation: %s", message);
return httpStatus;
}
/**
* @brief a quick utility function to trim leading spaces.
*/
static void
ltrim(String &target)
{
String::size_type p(target.find_first_not_of(' '));
if (p != target.npos) {
target.erase(0, p);
}
}
/**
* @brief a quick utility function to get next duplicate header.
*/
static TSMLoc
nextDuplicate(TSMBuffer buffer, TSMLoc hdr, TSMLoc field)
{
TSMLoc next = TSMimeHdrFieldNextDup(buffer, hdr, field);
TSHandleMLocRelease(buffer, hdr, field);
return next;
}
/**
* @brief Append cookies by following the rules specified in the cookies config object.
* @param config cookies-related configuration containing information about which cookies need to be appended to the key.
* @note Add the cookies to "hier-part" (RFC 3986), always sort them in the cache key.
*/
bool
getCookieByName(TSHttpTxn txn, TSMBuffer buf, TSMLoc hdrs, const String &cookieName, String &cookieValue)
{
TSMLoc field;
for (field = TSMimeHdrFieldFind(buf, hdrs, TS_MIME_FIELD_COOKIE, TS_MIME_LEN_COOKIE); field != TS_NULL_MLOC;
field = ::nextDuplicate(buf, hdrs, field)) {
int count = TSMimeHdrFieldValuesCount(buf, hdrs, field);
for (int i = 0; i < count; ++i) {
const char *val;
int len;
val = TSMimeHdrFieldValueStringGet(buf, hdrs, field, i, &len);
if (val == nullptr || len == 0) {
continue;
}
String cookie;
std::istringstream istr(String(val, len));
while (std::getline(istr, cookie, ';')) {
::ltrim(cookie); // Trim leading spaces.
String::size_type pos(cookie.find_first_of('='));
String name(cookie.substr(0, pos == String::npos ? cookie.size() : pos));
AccessControlDebug("cookie name: %s", name.c_str());
if (0 == cookieName.compare(name)) {
cookieValue.assign(cookie.substr(pos == String::npos ? pos : pos + 1));
return true;
}
}
}
}
return false;
}
/**
* @brief Handle token validation failures.
* @param txn transaction handle
* @param data transaction data
* @param reject true - reject requests if configured, false - don't reject
* @param httpStatus HTTP status
* @param status Access Token validation status.
*/
static TSRemapStatus
handleInvalidToken(TSHttpTxn txnp, AccessControlTxnData *data, bool reject, const TSHttpStatus httpStatus, AccessTokenStatus status)
{
TSRemapStatus resultStatus = TSREMAP_NO_REMAP;
if (reject) {
TSHttpTxnStatusSet(txnp, httpStatus);
resultStatus = TSREMAP_DID_REMAP;
} else {
data->_vaState = status;
}
TSHttpTxnConfigIntSet(txnp, TS_CONFIG_HTTP_CACHE_HTTP, 0);
return resultStatus;
}
/**
* @brief Formats the time stamp into expires cookie field format
* @param expires Unix Time
* @return String containing the date in the appropriate format for the Expires cookie attribute.
*/
String
getCookieExpiresTime(time_t expires)
{
struct tm tm;
char dateTime[1024];
size_t dateTimeLen = 1024;
size_t len = strftime(dateTime, dateTimeLen, "%a, %d %b %Y %H:%M:%S GMT", gmtime_r(&expires, &tm));
return String(dateTime, len);
}
/**
* @brief Callback function that handles cache lookup complete state where the access token is checked before serving from cache.
*
* If cache-miss or cache-skip don't validate - request will be forwarded to the origin and will be validated anyway.
* If cache-hit or cache-hit-stale - validate access token and if validation fails force a cache-miss so request will be forwarded
* to origin and validated.
*
* @param contp continuation associated with this function.
* @param event corresponding event triggered at different hooks.
* @param edata HTTP transaction structures (access control plugin config).
* @return always 0
*/
int
contHandleAccessControl(const TSCont contp, TSEvent event, void *edata)
{
TSHttpTxn txnp = static_cast<TSHttpTxn>(edata);
AccessControlTxnData *data = static_cast<AccessControlTxnData *>(TSContDataGet(contp));
const AccessControlConfig *config = data->_config;
TSEvent resultEvent = TS_EVENT_HTTP_CONTINUE;
AccessControlDebug("event: '%s'", getEventName(event));
switch (event) {
case TS_EVENT_HTTP_SEND_RESPONSE_HDR: {
if (VALID != data->_vaState && !config->_respTokenHeaderName.empty() && !config->_cookieName.empty()) {
/* Set the cookie only if
* - the initial client cookie validation failed (missing or invalid cookie)
* - we expect a new access token from the origin
* - provided token from the origin is valid
* - and we know the name of the cookie to do set-cookie */
TSMBuffer clientRespBufp;
TSMLoc clientRespHdrLoc;
if (TS_SUCCESS == TSHttpTxnClientRespGet(txnp, &clientRespBufp, &clientRespHdrLoc)) {
TSMBuffer serverRespBufp;
TSMLoc serverRespHdrLoc;
if (TS_SUCCESS == TSHttpTxnServerRespGet(txnp, &serverRespBufp, &serverRespHdrLoc)) {
AccessControlDebug("got the response now create the cookie");
static const size_t MAX_HEADER_LEN = 4096;
int tokenHdrValueLen = MAX_HEADER_LEN;
char tokenHdrValue[MAX_HEADER_LEN];
getHeader(serverRespBufp, serverRespHdrLoc, config->_respTokenHeaderName.c_str(), config->_respTokenHeaderName.size(),
tokenHdrValue, &tokenHdrValueLen);
if (0 < tokenHdrValueLen) {
AccessControlDebug("origin response access token '%.*s'", tokenHdrValueLen, tokenHdrValue);
AccessToken *token = config->_tokenFactory->getAccessToken();
if (nullptr != token &&
VALID == (data->_originState = token->validate(StringView(tokenHdrValue, tokenHdrValueLen), time(nullptr)))) {
/*
* From RFC 6265 "HTTP State Management Mechanism":
* To maximize compatibility with user agents, servers that wish to
* store arbitrary data in a cookie-value SHOULD encode that data, for
* example, using Base64 [RFC4648].
*/
int b64TokenHdrValueLen = cryptoBase64EncodedSize(tokenHdrValueLen);
char b64TokenHdrValue[b64TokenHdrValueLen];
size_t b64CookieLen =
cryptoModifiedBase64Encode(tokenHdrValue, tokenHdrValueLen, b64TokenHdrValue, b64TokenHdrValueLen);
String cookieValue;
cookieValue.append(config->_cookieName).append("=").append(b64TokenHdrValue, b64CookieLen).append("; ");
/** Currently Access Token implementation requires expiration to be set but the following is still a good
* consideration. Set the cookie Expires field to the token expiration field set by the origin if the time specified
* is invalid or not specified then don't set Expires attribute.
* @todo TBD may be adding a default / overriding Expires attribute configured by parameter would make sense ? */
time_t t = token->getExpiration();
if (0 != t) {
cookieValue.append("Expires=").append(getCookieExpiresTime(t)).append("; ");
}
/* Secure - instructs the UA to include the cookie in an HTTP request only if the request is transmitted over
* a secure channel, typically HTTP over Transport Layer Security (TLS)
* HttpOnly - instructs the UA to omit the cookie when providing access to cookies via "non-HTTP" APIs such as a web
* browser API that exposes cookies to scripts */
cookieValue.append("path=/; Secure; HttpOnly");
AccessControlDebug("%.*s: %s", TS_MIME_LEN_SET_COOKIE, TS_MIME_FIELD_SET_COOKIE, cookieValue.c_str());
setHeader(clientRespBufp, clientRespHdrLoc, TS_MIME_FIELD_SET_COOKIE, TS_MIME_LEN_SET_COOKIE, cookieValue.c_str(),
cookieValue.size(), /* duplicateOk = */ true);
delete token;
} else {
AccessControlDebug("failed to construct a valid origin access token, did not set-cookie with it");
/* Don't set any cookie, fail the request here returning appropriate status code and body.*/
TSHttpTxnStatusSet(txnp, config->_invalidOriginResponse);
static const char *body = "Unexpected Response From the Origin Server\n";
char *buf = static_cast<char *>(TSmalloc(strlen(body) + 1));
sprintf(buf, "%s", body);
TSHttpTxnErrorBodySet(txnp, buf, strlen(buf), nullptr);
resultEvent = TS_EVENT_HTTP_ERROR;
break;
}
} else {
AccessControlDebug("no access token response header found");
}
/* Remove the origin response access token header. */
int numberOfFields = removeHeader(clientRespBufp, clientRespHdrLoc, config->_respTokenHeaderName.c_str(),
config->_respTokenHeaderName.size());
AccessControlDebug("removed %d %s client response header(s)", numberOfFields, config->_respTokenHeaderName.c_str());
TSHandleMLocRelease(serverRespBufp, TS_NULL_MLOC, serverRespHdrLoc);
} else {
int len;
char *url = TSHttpTxnEffectiveUrlStringGet(txnp, &len);
AccessControlError("failed to retrieve server response header for request url:%.*s",
(len ? len : static_cast<int>(UNKNOWN.size())), (url ? url : UNKNOWN.data()));
}
TSHandleMLocRelease(clientRespBufp, TS_NULL_MLOC, clientRespHdrLoc);
} else {
int len;
char *url = TSHttpTxnEffectiveUrlStringGet(txnp, &len);
AccessControlError("failed to retrieve client response header for request url:%.*s",
(len ? len : static_cast<int>(UNKNOWN.size())), (url ? url : UNKNOWN.data()));
}
}
} break;
case TS_EVENT_HTTP_TXN_CLOSE: {
if (!config->_extrValidationHdrName.empty()) {
TSMBuffer clientRespBufp;
TSMLoc clientRespHdrLoc;
/* Add some debugging / logging to the client request so it can be extracted through headers */
if (TS_SUCCESS == TSHttpTxnClientReqGet(txnp, &clientRespBufp, &clientRespHdrLoc)) {
String statusHeader;
StringView vaState(accessTokenStatusToString(data->_vaState));
StringView originState(accessTokenStatusToString(data->_originState));
/* UC_ = UA Cookie, to store the token validation status when extracted from HTTP cookie */
if (!vaState.empty()) {
statusHeader.append("UC_").append(vaState);
}
/* OH_ = origin response header, to store the token validation status when extracted from origin response header. */
if (!originState.empty()) {
statusHeader.append(vaState.empty() ? "" : ",");
statusHeader.append("OH_").append(originState);
}
AccessControlDebug("adding header %s: '%s'", config->_extrValidationHdrName.c_str(), statusHeader.c_str());
setHeader(clientRespBufp, clientRespHdrLoc, config->_extrValidationHdrName.c_str(), config->_extrValidationHdrName.size(),
statusHeader.c_str(), statusHeader.length());
} else {
AccessControlError("failed to retrieve client response header");
}
}
/* Destroy the txn continuation and its data */
delete data;
TSContDestroy(contp);
} break;
default:
break;
}
TSHttpTxnReenable(txnp, resultEvent);
return 0;
}
/**
* @brief Enforces access control, currently supports access token from a cookie.
*
* @param instance plugin instance pointer
* @param txn transaction handle
* @param rri remap request info pointer
* @param config pointer to the plugin configuration
* @return TSREMAP_NO_REMAP (access validation = success)
* TSREMAP_DID_REMAP (access validation = failure and rejection of failed requests is configured)
*/
TSRemapStatus
enforceAccessControl(TSHttpTxn txnp, TSRemapRequestInfo *rri, AccessControlConfig *config)
{
if (config->_cookieName.empty()) {
/* For now only checking a cookie is supported and if its name is unknown (checking cookie disabled) then do nothing. */
return TSREMAP_NO_REMAP;
}
TSRemapStatus remapStatus = TSREMAP_NO_REMAP;
/* Create txn data and register hooks */
AccessControlTxnData *data = new AccessControlTxnData(config);
TSCont cont = TSContCreate(contHandleAccessControl, TSMutexCreate());
TSContDataSet(cont, static_cast<void *>(data));
TSHttpTxnHookAdd(txnp, TS_HTTP_SEND_RESPONSE_HDR_HOOK, cont);
TSHttpTxnHookAdd(txnp, TS_HTTP_TXN_CLOSE_HOOK, cont);
/* Validate the token */
bool reject = config->_rejectRequestsWithInvalidTokens;
String cookie;
bool found = getCookieByName(txnp, rri->requestBufp, rri->requestHdrp, config->_cookieName, cookie);
if (found) {
AccessControlDebug("%s cookie: '%s'", config->_cookieName.c_str(), cookie.c_str());
/*
* From RFC 6265 "HTTP State Management Mechanism":
* To maximize compatibility with user agents, servers that wish to
* store arbitrary data in a cookie-value SHOULD encode that data, for
* example, using Base64 [RFC4648].
*/
size_t decodedCookieBufferSize = cryptoBase64DecodeSize(cookie.c_str(), cookie.size());
char decodedCookie[decodedCookieBufferSize];
size_t decryptedCookieSize = cryptoModifiedBase64Decode(cookie.c_str(), cookie.size(), decodedCookie, decodedCookieBufferSize);
if (0 < decryptedCookieSize) {
AccessToken *token = config->_tokenFactory->getAccessToken();
if (nullptr != token) {
data->_vaState = token->validate(StringView(decodedCookie, decryptedCookieSize), time(nullptr));
if (VALID != data->_vaState) {
remapStatus =
handleInvalidToken(txnp, data, reject, accessTokenStateToHttpStatus(data->_vaState, config), data->_vaState);
} else {
/* Valid token, if configured extract the token subject to a header,
* only if we can trust it - token is valid to prevent using it by mistake */
if (!config->_extrSubHdrName.empty()) {
String sub(token->getSubject());
setHeader(rri->requestBufp, rri->requestHdrp, config->_extrSubHdrName.c_str(), config->_extrSubHdrName.size(),
sub.c_str(), sub.size());
}
}
/* If configure extract the UA token id into a header likely for debugging,
* extract it even if token validation fails and we don't trust it */
if (!config->_extrTokenIdHdrName.empty()) {
String tokeId(token->getTokenId());
setHeader(rri->requestBufp, rri->requestHdrp, config->_extrTokenIdHdrName.c_str(), config->_extrTokenIdHdrName.size(),
tokeId.c_str(), tokeId.size());
}
delete token;
} else {
AccessControlDebug("failed to construct access token");
remapStatus = handleInvalidToken(txnp, data, reject, config->_internalError, UNUSED);
}
} else {
AccessControlDebug("failed to decode cookie value");
remapStatus = handleInvalidToken(txnp, data, reject, config->_invalidRequest, UNUSED);
}
} else {
AccessControlDebug("failed to find cookie %s", config->_cookieName.c_str());
remapStatus = handleInvalidToken(txnp, data, reject, config->_invalidRequest, UNUSED);
}
return remapStatus;
}
/**
* @brief Remap and sets up access control based on whether access control is required, failed, etc.
*
* @param instance plugin instance pointer
* @param txn transaction handle
* @param rri remap request info pointer
* @return TSREMAP_NO_REMAP (access validation = success)
* TSREMAP_DID_REMAP (access validation = failure and rejection of failed requests is configured)
*/
TSRemapStatus
TSRemapDoRemap(void *instance, TSHttpTxn txnp, TSRemapRequestInfo *rri)
{
TSRemapStatus remapStatus = TSREMAP_NO_REMAP;
AccessControlConfig *config = static_cast<AccessControlConfig *>(instance);
if (nullptr != config) {
/* Plugin is designed to be used only with TLS, check the scheme */
int schemeLen = 0;
const char *scheme = TSUrlSchemeGet(rri->requestBufp, rri->requestUrl, &schemeLen);
if (nullptr != scheme) {
if (/* strlen("https") */ 5 == schemeLen && 0 == strncmp(scheme, "https", schemeLen)) {
AccessControlDebug("validate the access token");
String reqPath;
int pathLen = 0;
const char *path = TSUrlPathGet(rri->requestBufp, rri->requestUrl, &pathLen);
if (nullptr != path && 0 < pathLen) {
reqPath.assign(path, pathLen);
}
/* Check if any of the uri-path multi-pattern matched and if yes enforce access control. */
String filename;
String pattern;
if (config->_uriPathScope.empty()) {
/* Scope match enforce access control */
AccessControlDebug("no plugin scope specified, enforcing access control");
remapStatus = enforceAccessControl(txnp, rri, config);
} else {
if (true == config->_uriPathScope.matchAll(reqPath, filename, pattern)) {
AccessControlDebug("matched plugin scope enforcing access control for path %s", reqPath.c_str());
/* Scope match enforce access control */
remapStatus = enforceAccessControl(txnp, rri, config);
} else {
AccessControlDebug("not matching plugin scope (file: %s, pattern %s), skipping access control for path '%s'",
filename.c_str(), pattern.c_str(), reqPath.c_str());
}
}
} else {
TSHttpTxnStatusSet(txnp, config->_invalidRequest);
AccessControlDebug("https is the only allowed scheme (plugin should be used only with TLS)");
remapStatus = TSREMAP_DID_REMAP;
}
} else {
TSHttpTxnStatusSet(txnp, config->_internalError);
AccessControlError("failed to get request uri-scheme");
remapStatus = TSREMAP_DID_REMAP;
}
} else {
/* Something is terribly wrong, we cannot get the configuration */
TSHttpTxnStatusSet(txnp, TS_HTTP_STATUS_INTERNAL_SERVER_ERROR);
AccessControlError("configuration unavailable");
remapStatus = TSREMAP_DID_REMAP;
}
return remapStatus;
}
|