From ca96ae806f79063922f27c3b4628026b9930fd89 Mon Sep 17 00:00:00 2001 From: Pritha Srivastava Date: Sun, 29 Mar 2020 15:50:32 +0530 Subject: [PATCH] rgw: adds a commom method to remove prefix(www, http and https) from a url. Signed-off-by: Pritha Srivastava --- src/rgw/rgw_auth.cc | 10 +--------- src/rgw/rgw_common.cc | 21 +++++++++++++++++++++ src/rgw/rgw_common.h | 1 + src/rgw/rgw_oidc_provider.cc | 16 +--------------- src/rgw/rgw_rest_oidc_provider.cc | 16 +--------------- 5 files changed, 25 insertions(+), 39 deletions(-) diff --git a/src/rgw/rgw_auth.cc b/src/rgw/rgw_auth.cc index 46eddfcb08a..a949ed10eed 100644 --- a/src/rgw/rgw_auth.cc +++ b/src/rgw/rgw_auth.cc @@ -346,15 +346,7 @@ void rgw::auth::WebIdentityApplier::to_str(std::ostream& out) const string rgw::auth::WebIdentityApplier::get_idp_url() const { string idp_url = token_claims.iss; - auto pos = idp_url.find("http://"); - if (pos == std::string::npos) { - pos = idp_url.find("https://"); - if (pos != std::string::npos) { - idp_url.erase(pos, 8); - } - } else { - idp_url.erase(pos, 7); - } + idp_url = url_remove_prefix(idp_url); return idp_url; } diff --git a/src/rgw/rgw_common.cc b/src/rgw/rgw_common.cc index 8e482e2144a..a6df0e947e5 100644 --- a/src/rgw/rgw_common.cc +++ b/src/rgw/rgw_common.cc @@ -1594,6 +1594,27 @@ std::string url_encode(const std::string& src, bool encode_slash) return dst; } +std::string url_remove_prefix(const std::string& url) +{ + std::string dst = url; + auto pos = dst.find("http://"); + if (pos == std::string::npos) { + pos = dst.find("https://"); + if (pos != std::string::npos) { + dst.erase(pos, 8); + } else { + pos = dst.find("www."); + if (pos != std::string::npos) { + dst.erase(pos, 4); + } + } + } else { + dst.erase(pos, 7); + } + + return dst; +} + string rgw_trim_whitespace(const string& src) { if (src.empty()) { diff --git a/src/rgw/rgw_common.h b/src/rgw/rgw_common.h index d0cbcd1eb82..56b0d1590dd 100644 --- a/src/rgw/rgw_common.h +++ b/src/rgw/rgw_common.h @@ -2261,6 +2261,7 @@ extern std::string url_decode(const boost::string_view& src_str, extern void url_encode(const std::string& src, string& dst, bool encode_slash = true); extern std::string url_encode(const std::string& src, bool encode_slash = true); +extern std::string url_remove_prefix(const std::string& url); // Removes hhtp, https and www from url /* destination should be CEPH_CRYPTO_HMACSHA1_DIGESTSIZE bytes long */ extern void calc_hmac_sha1(const char *key, int key_len, const char *msg, int msg_len, char *dest); diff --git a/src/rgw/rgw_oidc_provider.cc b/src/rgw/rgw_oidc_provider.cc index ee28d2cd162..14eea2f76f8 100644 --- a/src/rgw/rgw_oidc_provider.cc +++ b/src/rgw/rgw_oidc_provider.cc @@ -64,21 +64,7 @@ int RGWOIDCProvider::create(bool exclusive) return -EINVAL; } - string idp_url = provider_url; - auto pos = idp_url.find("http://"); - if (pos == std::string::npos) { - pos = idp_url.find("https://"); - if (pos != std::string::npos) { - idp_url.erase(pos, 8); - } else { - pos = idp_url.find("www."); - if (pos != std::string::npos) { - idp_url.erase(pos, 4); - } - } - } else { - idp_url.erase(pos, 7); - } + string idp_url = url_remove_prefix(provider_url); /* check to see the name is not used */ ret = read_url(idp_url, tenant); diff --git a/src/rgw/rgw_rest_oidc_provider.cc b/src/rgw/rgw_rest_oidc_provider.cc index e2f6ddc00d6..a72e3c64b78 100644 --- a/src/rgw/rgw_rest_oidc_provider.cc +++ b/src/rgw/rgw_rest_oidc_provider.cc @@ -79,21 +79,7 @@ int RGWCreateOIDCProvider::verify_permission() return ret; } - string idp_url = provider_url; - auto pos = idp_url.find("http://"); - if (pos == std::string::npos) { - pos = idp_url.find("https://"); - if (pos != std::string::npos) { - idp_url.erase(pos, 8); - } else { - pos = idp_url.find("www."); - if (pos != std::string::npos) { - idp_url.erase(pos, 4); - } - } - } else { - idp_url.erase(pos, 7); - } + string idp_url = url_remove_prefix(provider_url); if (!verify_user_permission(this, s, rgw::ARN(idp_url, -- 2.39.5