]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
auth: CryptoKey, use secret in CryptoKeyHandler
authorMarcus Watts <mwatts@redhat.com>
Tue, 4 Nov 2025 01:50:24 +0000 (20:50 -0500)
committerPatrick Donnelly <pdonnell@ibm.com>
Mon, 26 Jan 2026 15:27:51 +0000 (10:27 -0500)
Keep only one copy of secret in CryptoKeyHandler.  This will reduce
the number of copies made in memory.  Also introduce bool() and ==
opeartors so we can hide implementation details.

Signed-off-by: Marcus Watts <mwatts@redhat.com>
src/auth/Crypto.cc
src/auth/Crypto.h
src/auth/cephx/CephxClientHandler.cc
src/mon/AuthMonitor.cc

index 053dc31afa804795418eacad3a96919eee2a0967..8b25040c920833cb7f991b24c2693d84dc00872b 100644 (file)
@@ -1069,10 +1069,10 @@ CryptoKeyHandler *CryptoAES256KRB5::get_key_handler_ext(const bufferptr& secret,
 
 // ---------------------------------------------------
 
-
 void CryptoKey::encode(bufferlist& bl) const
 {
   using ceph::encode;
+  const bufferptr &secret = get_secret();
   encode(type, bl);
   encode(created, bl);
   __u16 len = secret.length();
@@ -1124,7 +1124,7 @@ int CryptoKey::set_secret(int type, const bufferptr& s, utime_t c)
 int CryptoKey::_set_secret(int t, const bufferptr& s)
 {
   if (s.length() == 0) {
-    secret = s;
+//    secret = s;
     ckh.reset();
     return 0;
   }
@@ -1146,7 +1146,7 @@ int CryptoKey::_set_secret(int t, const bufferptr& s)
       return -EOPNOTSUPP;
   }
   type = t;
-  secret = s;
+//  secret = s;
   return 0;
 }
 
@@ -1178,6 +1178,7 @@ void CryptoKey::print(std::ostream &out) const
 
 void CryptoKey::to_str(std::string& s) const
 {
+  const bufferptr &secret = get_secret();
   int len = secret.length() * 4;
   char buf[len];
   hex2str(secret.c_str(), secret.length(), buf, len);
@@ -1197,6 +1198,14 @@ void CryptoKey::encode_plaintext(bufferlist &bl)
   bl.append(encode_base64());
 }
 
+static bufferptr z;
+
+const bufferptr& CryptoKey::get_secret() const
+{
+  const bufferptr &secret = ckh ? ckh->secret : z;
+  return secret;
+}
+
 
 // ------------------
 
index ef31ac5a09bb3702358b51ba99587cf109465163..a8839c34c69a2967a75e91ffecba69e643a5f032 100644 (file)
@@ -75,6 +75,14 @@ public:
 
   virtual ~CryptoKeyHandler() {}
 
+  operator bool()const {
+    return secret.length() > 0;
+  }
+
+  bool operator==(const CryptoKeyHandler &rhs) const {
+    return 0 == secret.cmp(rhs.secret);
+  }
+
   virtual int encrypt(CephContext *cct,
                       const ceph::buffer::list& in,
                      ceph::buffer::list& out, std::string *error) const {
@@ -164,7 +172,7 @@ class CryptoKey {
 protected:
   __u16 type;
   utime_t created;
-  ceph::buffer::ptr secret;   // must set this via set_secret()!
+//  ceph::buffer::ptr secret;   // must set this via set_secret()!
 
   // cache a pointer to the implementation-specific key handler, so we
   // don't have to create it for every crypto operation.
@@ -181,6 +189,13 @@ public:
   ~CryptoKey() {
   }
 
+  operator bool()const {
+    return ckh && *ckh;
+  }
+  bool operator==(const CryptoKey &rhs) const {
+    return !ckh ? !rhs.ckh
+                : rhs.ckh && *ckh == *rhs.ckh;
+  }
   void encode(ceph::buffer::list& bl) const;
   void decode(ceph::buffer::list::const_iterator& bl);
   void dump(ceph::Formatter *f) const;
@@ -195,8 +210,8 @@ public:
   void print(std::ostream& out) const;
 
   int set_secret(int type, const ceph::buffer::ptr& s, utime_t created);
-  const ceph::buffer::ptr& get_secret() { return secret; }
-  const ceph::buffer::ptr& get_secret() const { return secret; }
+//  const ceph::buffer::ptr& get_secret() { return secret; }
+//  const ceph::buffer::ptr& get_secret() const { return secret; }
 
   bool empty() const { return ckh.get() == nullptr; }
 
@@ -270,6 +285,8 @@ public:
   }
 
   void to_str(std::string& s) const;
+private:
+  const ceph::bufferptr& get_secret() const;
 };
 WRITE_CLASS_ENCODER(CryptoKey)
 
index 42153fd04e6fcd627dc700fb108bb3be6a3fad39..451fdb4fc28b4041903601db1f9834f5a956a450 100644 (file)
@@ -73,7 +73,7 @@ int CephxClientHandler::build_request(bufferlist& bl) const
     }
 
     // is the key OK?
-    if (!secret.get_secret().length()) {
+    if (!secret) {
       ldout(cct, 20) << "secret for entity " << cct->_conf->name << " is invalid" << dendl;
       return -EINVAL;
     }
index 20c0e0205e98aaaa52d4e7a57e69a07f2acb926f..1c4db822dc2adae421819049024971084205fb4b 100644 (file)
@@ -1180,7 +1180,7 @@ int AuthMonitor::exists_and_matches_entity(
   if (mon.get_auth(name, existing_auth)) {
     // key match?
     if (has_secret) {
-      if (existing_auth.key.get_secret().cmp(auth.key.get_secret())) {
+      if (!(existing_auth.key == auth.key)) {
         ss << "entity " << name << " exists but key does not match";
         return -EEXIST;
       }