]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
AuthMonitor: add a method for encoding keyring
authorRishabh Dave <ridave@redhat.com>
Wed, 9 Jun 2021 07:51:45 +0000 (13:21 +0530)
committerRishabh Dave <ridave@redhat.com>
Fri, 16 Jun 2023 20:46:32 +0000 (02:16 +0530)
Instead of repeating same lines of code to encode entity's auth keyring
(or just key) before printing, add a method for this and use it instead.
This commit is nothing more than refactoring to avoid duplication and
improve readability; it shouldn't make any functional changes.

Signed-off-by: Rishabh Dave <ridave@redhat.com>
src/mon/AuthMonitor.cc
src/mon/AuthMonitor.h

index ac32a57b9b88be4fe05fc7e6b9a4ff3bf4e0dfa0..878262058cd750b9f33c15a5add8f1cf312002bc 100644 (file)
@@ -907,12 +907,7 @@ bool AuthMonitor::preprocess_command(MonOpRequestRef op)
     if (!entity_name.empty()) {
       EntityAuth eauth;
       if (keyring.get_auth(entity, eauth)) {
-       KeyRing kr;
-       kr.add(entity, eauth);
-       if (f)
-         kr.encode_formatted("auth", f.get(), rdata);
-       else
-         kr.encode_plaintext(rdata);
+       _encode_auth(entity, eauth, rdata, f.get());
        r = 0;
       } else {
        ss << "no key for " << eauth;
@@ -926,17 +921,12 @@ bool AuthMonitor::preprocess_command(MonOpRequestRef op)
       r = 0;
     }
   } else if (prefix == "auth get" && !entity_name.empty()) {
-    KeyRing keyring;
     EntityAuth entity_auth;
     if (!mon.key_server.get_auth(entity, entity_auth)) {
       ss << "failed to find " << entity_name << " in keyring";
       r = -ENOENT;
     } else {
-      keyring.add(entity, entity_auth);
-      if (f)
-       keyring.encode_formatted("auth", f.get(), rdata);
-      else
-       keyring.encode_plaintext(rdata);
+      _encode_auth(entity, entity_auth, rdata, f.get());
       r = 0;
     }
   } else if (prefix == "auth print-key" ||
@@ -1390,7 +1380,7 @@ bool AuthMonitor::prepare_command(MonOpRequestRef op)
 {
   auto m = op->get_req<MMonCommand>();
   stringstream ss, ds;
-  bufferlist rdata;
+  bufferlist rdata; // holds data that'll be printed on client's stdout
   string rs;
   int err = -EINVAL;
 
@@ -1682,14 +1672,8 @@ bool AuthMonitor::prepare_command(MonOpRequestRef op)
           ds << entity_auth.key;
         }
       } else {
-       KeyRing kr;
-       kr.add(entity, entity_auth.key, entity_auth.pending_key);
-        if (f) {
-          kr.set_caps(entity, entity_auth.caps);
-          kr.encode_formatted("auth", f.get(), rdata);
-        } else {
-          kr.encode_plaintext(rdata);
-        }
+       _encode_key(entity, entity_auth, rdata, f.get(), true,
+                   &entity_auth.caps);
       }
       err = 0;
       goto done;
@@ -1728,14 +1712,8 @@ bool AuthMonitor::prepare_command(MonOpRequestRef op)
         ds << auth_inc.auth.key;
       }
     } else {
-      KeyRing kr;
-      kr.add(entity, auth_inc.auth.key);
-      if (f) {
-        kr.set_caps(entity, wanted_caps);
-        kr.encode_formatted("auth", f.get(), rdata);
-      } else {
-        kr.encode_plaintext(rdata);
-      }
+      _encode_key(entity, auth_inc.auth, rdata, f.get(), false,
+                 &wanted_caps);
     }
 
     rdata.append(ds);
@@ -1850,14 +1828,7 @@ bool AuthMonitor::prepare_command(MonOpRequestRef op)
        }
       }
 
-      KeyRing kr;
-      kr.add(entity, entity_auth.key);
-      if (f) {
-       kr.set_caps(entity, entity_auth.caps);
-       kr.encode_formatted("auth", f.get(), rdata);
-      } else {
-       kr.encode_plaintext(rdata);
-      }
+      _encode_key(entity, entity_auth, rdata, f.get(), false, &wanted_caps);
       err = 0;
       goto done;
     }
@@ -1869,15 +1840,9 @@ bool AuthMonitor::prepare_command(MonOpRequestRef op)
     auth_inc.auth.caps = wanted_caps;
 
     push_cephx_inc(auth_inc);
-    KeyRing kr;
-    kr.add(entity, auth_inc.auth.key);
-    if (f) {
-      kr.set_caps(entity, wanted_caps);
-      kr.encode_formatted("auth", f.get(), rdata);
-    } else {
-      kr.encode_plaintext(rdata);
-    }
 
+    _encode_key(entity, auth_inc.auth, rdata, f.get(), false,
+               &wanted_caps);
     rdata.append(ds);
     getline(ss, rs);
     wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, 0, rs, rdata,
@@ -1933,6 +1898,49 @@ done:
   return false;
 }
 
+void AuthMonitor::_encode_keyring(KeyRing& kr, const EntityName& entity,
+  bufferlist& rdata, Formatter* fmtr, map<string, bufferlist>* caps)
+{
+  if (not fmtr) {
+    kr.encode_plaintext(rdata);
+  } else {
+    if (caps != nullptr) {
+      kr.set_caps(entity, *caps);
+    }
+    kr.encode_formatted("auth", fmtr, rdata);
+  }
+}
+
+void AuthMonitor::_encode_auth(const EntityName& entity,
+  const EntityAuth& eauth, bufferlist& rdata, Formatter* fmtr,
+  bool pending_key, map<string, bufferlist>* caps)
+{
+  KeyRing kr;
+
+  if (not pending_key) {
+    kr.add(entity, eauth);
+  } else {
+    kr.add(entity, eauth.key, eauth.pending_key);
+  }
+
+  _encode_keyring(kr, entity, rdata, fmtr, caps);
+}
+
+void AuthMonitor::_encode_key(const EntityName& entity,
+  const EntityAuth& eauth, bufferlist& rdata, Formatter* fmtr,
+  bool pending_key, map<string, bufferlist>* caps)
+{
+  KeyRing kr;
+
+  if (not pending_key) {
+    kr.add(entity, eauth.key);
+  } else {
+    kr.add(entity, eauth.key, eauth.pending_key);
+  }
+
+  _encode_keyring(kr, entity, rdata, fmtr, caps);
+}
+
 bool AuthMonitor::prepare_global_id(MonOpRequestRef op)
 {
   dout(10) << "AuthMonitor::prepare_global_id" << dendl;
index 993b18a02b2423a0a8df952cd715b55c297a2d83..8007d0fd1b11740e7e19f6f333adef053313f846 100644 (file)
@@ -166,6 +166,16 @@ private:
   bool preprocess_command(MonOpRequestRef op);
   bool prepare_command(MonOpRequestRef op);
 
+  void _encode_keyring(KeyRing& kr, const EntityName& entity,
+    bufferlist& rdata, Formatter* fmtr,
+    std::map<std::string, bufferlist>* wanted_caps=nullptr);
+  void _encode_auth(const EntityName& entity, const EntityAuth& eauth,
+    bufferlist& rdata, Formatter* fmtr, bool pending_key=false,
+    std::map<std::string, bufferlist>* caps=nullptr);
+  void _encode_key(const EntityName& entity, const EntityAuth& eauth,
+    bufferlist& rdata, Formatter* fmtr, bool pending_key=false,
+    std::map<std::string, bufferlist>* caps=nullptr);
+
   bool check_rotate();
   void process_used_pending_keys(const std::map<EntityName,CryptoKey>& keys);