url_preprocessing.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include "url_preprocessing.h"
  2. #include <yql/essentials/providers/common/proto/gateways_config.pb.h>
  3. #include <yql/essentials/utils/log/log.h>
  4. #include <util/generic/yexception.h>
  5. namespace NYql {
  6. void TUrlPreprocessing::Configure(bool restrictedUser, const TGatewaysConfig& cfg) {
  7. RestrictedUser_ = restrictedUser;
  8. try {
  9. if (cfg.HasFs()) {
  10. const auto fsCfg = cfg.GetFs();
  11. for (auto& s: fsCfg.GetCustomSchemes()) {
  12. Mapper_.AddMapping(s.GetPattern(), s.GetTargetUrl());
  13. }
  14. if (restrictedUser) {
  15. for (auto& a: fsCfg.GetExternalAllowedUrls()) {
  16. AllowedUrls_.Add(a.GetPattern(), a.GetAlias());
  17. }
  18. } else {
  19. for (auto& a: fsCfg.GetAllowedUrls()) {
  20. AllowedUrls_.Add(a.GetPattern(), a.GetAlias());
  21. }
  22. }
  23. }
  24. } catch (const yexception& e) {
  25. ythrow yexception() << "UrlPreprocessing: " << e.what();
  26. }
  27. }
  28. std::pair<TString, TString> TUrlPreprocessing::Preprocess(const TString& url) {
  29. TString convertedUrl;
  30. if (!Mapper_.MapUrl(url, convertedUrl)) {
  31. convertedUrl = url;
  32. } else {
  33. YQL_LOG(INFO) << "Remap url from " << url << " to " << convertedUrl;
  34. }
  35. TString alias;
  36. if (RestrictedUser_ || !AllowedUrls_.IsEmpty()) {
  37. if (auto a = AllowedUrls_.Match(convertedUrl)) {
  38. alias = *a;
  39. } else {
  40. YQL_LOG(WARN) << "Url " << convertedUrl << " is not in allowed list, reject accessing";
  41. ythrow yexception() << "It is not allowed to access url " << url;
  42. }
  43. }
  44. YQL_LOG(INFO) << "UrlPreprocessing: " << convertedUrl << ", alias=" << alias;
  45. return {convertedUrl, alias};
  46. }
  47. } // NYql