* @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) */
| 568 | * TSREMAP_DID_REMAP (access validation = failure and rejection of failed requests is configured) |
| 569 | */ |
| 570 | TSRemapStatus |
| 571 | TSRemapDoRemap(void *instance, TSHttpTxn txnp, TSRemapRequestInfo *rri) |
| 572 | { |
| 573 | TSRemapStatus remapStatus = TSREMAP_NO_REMAP; |
| 574 | AccessControlConfig *config = static_cast<AccessControlConfig *>(instance); |
| 575 | |
| 576 | if (nullptr != config) { |
| 577 | /* Plugin is designed to be used only with TLS, check the scheme */ |
| 578 | int schemeLen = 0; |
| 579 | const char *scheme = TSUrlSchemeGet(rri->requestBufp, rri->requestUrl, &schemeLen); |
| 580 | if (nullptr != scheme) { |
| 581 | if (/* strlen("https") */ 5 == schemeLen && 0 == strncmp(scheme, "https", schemeLen)) { |
| 582 | AccessControlDebug("validate the access token"); |
| 583 | |
| 584 | String reqPath; |
| 585 | int pathLen = 0; |
| 586 | const char *path = TSUrlPathGet(rri->requestBufp, rri->requestUrl, &pathLen); |
| 587 | if (nullptr != path && 0 < pathLen) { |
| 588 | reqPath.assign(path, pathLen); |
| 589 | } |
| 590 | /* Check if any of the uri-path multi-pattern matched and if yes enforce access control. */ |
| 591 | String filename; |
| 592 | String pattern; |
| 593 | if (config->_uriPathScope.empty()) { |
| 594 | /* Scope match enforce access control */ |
| 595 | AccessControlDebug("no plugin scope specified, enforcing access control"); |
| 596 | remapStatus = enforceAccessControl(txnp, rri, config); |
| 597 | } else { |
| 598 | if (true == config->_uriPathScope.matchAll(reqPath, filename, pattern)) { |
| 599 | AccessControlDebug("matched plugin scope enforcing access control for path %s", reqPath.c_str()); |
| 600 | |
| 601 | /* Scope match enforce access control */ |
| 602 | remapStatus = enforceAccessControl(txnp, rri, config); |
| 603 | } else { |
| 604 | AccessControlDebug("not matching plugin scope (file: %s, pattern %s), skipping access control for path '%s'", |
| 605 | filename.c_str(), pattern.c_str(), reqPath.c_str()); |
| 606 | } |
| 607 | } |
| 608 | } else { |
| 609 | TSHttpTxnStatusSet(txnp, config->_invalidRequest); |
| 610 | AccessControlDebug("https is the only allowed scheme (plugin should be used only with TLS)"); |
| 611 | remapStatus = TSREMAP_DID_REMAP; |
| 612 | } |
| 613 | } else { |
| 614 | TSHttpTxnStatusSet(txnp, config->_internalError); |
| 615 | AccessControlError("failed to get request uri-scheme"); |
| 616 | remapStatus = TSREMAP_DID_REMAP; |
| 617 | } |
| 618 | } else { |
| 619 | /* Something is terribly wrong, we cannot get the configuration */ |
| 620 | TSHttpTxnStatusSet(txnp, TS_HTTP_STATUS_INTERNAL_SERVER_ERROR); |
| 621 | AccessControlError("configuration unavailable"); |
| 622 | remapStatus = TSREMAP_DID_REMAP; |
| 623 | } |
| 624 | |
| 625 | return remapStatus; |
| 626 | } |
nothing calls this directly
no test coverage detected