]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
AuthMonitor: refactor valid_caps() 52008/head
authorRishabh Dave <ridave@redhat.com>
Fri, 16 Jun 2023 19:19:39 +0000 (00:49 +0530)
committerRishabh Dave <ridave@redhat.com>
Mon, 19 Jun 2023 11:28:58 +0000 (16:58 +0530)
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 <ridave@redhat.com>
src/mon/AuthMonitor.cc
src/mon/AuthMonitor.h

index a73c4ba23be50cac0d6d891bca800efb9df77ca9..8cb6789394b36dd08cc8aa99f250f14f47cbeb1f 100644 (file)
@@ -1316,48 +1316,45 @@ int AuthMonitor::do_osd_new(
   return 0;
 }
 
-bool AuthMonitor::valid_caps(
-    const string& type,
-    const string& caps,
-    ostream *out)
+template<typename CAP_ENTITY_CLASS>
+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<MonCap>(entity, caps, out);
   }
 
   if (!g_conf().get_val<bool>("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<MgrCap>(entity, caps, out);
+  } else if (entity == "osd") {
+    return _was_parsing_fine<OSDCap>(entity, caps, out);
+  } else if (entity == "mds") {
+    return _was_parsing_fine<MDSAuthCaps>(entity, caps, out);
   } else {
     if (out) {
-      *out << "unknown cap type '" << type << "'";
+      *out << "unknown cap type '" << entity << "'";
     }
     return false;
   }
+
   return true;
 }
 
index 9251ac29710f9dfcc74be53cf264f69dd20a7c26..51fba9949774082131a6e2012d6776c322b5c4b7 100644 (file)
@@ -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<typename CAP_ENTITY_CLASS>
+  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;