From 8627af3e0adcb765a3c249fcc209cba9f4873e1b Mon Sep 17 00:00:00 2001 From: Rishabh Dave Date: Sat, 17 Jun 2023 00:49:39 +0530 Subject: [PATCH] AuthMonitor: refactor valid_caps() Since PR #50874 has been merged, this method can do the exact same job in much lesser code. Also since we are here, let's rename variable "type" to "entity". Signed-off-by: Rishabh Dave --- src/mon/AuthMonitor.cc | 57 ++++++++++++++++++++---------------------- src/mon/AuthMonitor.h | 8 ++++-- 2 files changed, 33 insertions(+), 32 deletions(-) diff --git a/src/mon/AuthMonitor.cc b/src/mon/AuthMonitor.cc index a73c4ba23be50..8cb6789394b36 100644 --- a/src/mon/AuthMonitor.cc +++ b/src/mon/AuthMonitor.cc @@ -1316,48 +1316,45 @@ int AuthMonitor::do_osd_new( return 0; } -bool AuthMonitor::valid_caps( - const string& type, - const string& caps, - ostream *out) +template +bool AuthMonitor::_was_parsing_fine(const string& entity, const string& caps, + ostream* out) { - if (type == "mon") { - MonCap moncap; - if (!moncap.parse(caps, out)) { - dout(20) << "Parsing MON caps failed. MON cap: " << caps << dendl; - return false; - } - return true; + CAP_ENTITY_CLASS cap; + + if (!cap.parse(caps, out)) { + dout(20) << "Parsing " << entity << " caps failed. " << entity << + " cap: " << caps << dendl; + return false; + } + + return true; +} + +bool AuthMonitor::valid_caps(const string& entity, const string& caps, + ostream *out) +{ + if (entity == "mon") { + return _was_parsing_fine(entity, caps, out); } if (!g_conf().get_val("mon_auth_validate_all_caps")) { return true; } - if (type == "mgr") { - MgrCap mgrcap; - if (!mgrcap.parse(caps, out)) { - dout(20) << "Parsing MGR caps failed. MGR cap: " << caps << dendl; - return false; - } - } else if (type == "osd") { - OSDCap ocap; - if (!ocap.parse(caps, out)) { - dout(20) << "Parsing OSD caps failed. OSD cap: " << caps << dendl; - return false; - } - } else if (type == "mds") { - MDSAuthCaps mdscap; - if (!mdscap.parse(caps, out)) { - dout(20) << "Parsing MDS caps failed. MDS cap: " << caps << dendl; - return false; - } + if (entity == "mgr") { + return _was_parsing_fine(entity, caps, out); + } else if (entity == "osd") { + return _was_parsing_fine(entity, caps, out); + } else if (entity == "mds") { + return _was_parsing_fine(entity, caps, out); } else { if (out) { - *out << "unknown cap type '" << type << "'"; + *out << "unknown cap type '" << entity << "'"; } return false; } + return true; } diff --git a/src/mon/AuthMonitor.h b/src/mon/AuthMonitor.h index 9251ac29710f9..51fba99497740 100644 --- a/src/mon/AuthMonitor.h +++ b/src/mon/AuthMonitor.h @@ -119,8 +119,12 @@ private: pending_auth.push_back(inc); } - /* validate mon/osd/mds caps; fail on unrecognized service/type */ - bool valid_caps(const std::string& type, const std::string& caps, std::ostream *out); + template + bool _was_parsing_fine(const std::string& entity, const std::string& caps, + std::ostream* out); + /* validate mon/osd/mgr/mds caps; fail on unrecognized service/type */ + bool valid_caps(const std::string& entity, const std::string& caps, + std::ostream *out); bool valid_caps(const std::string& type, const ceph::buffer::list& bl, std::ostream *out) { auto p = bl.begin(); std::string v; -- 2.39.5