PublicKey: Add methods to evaluate expiration dates
authorMichael Andres <ma@suse.de>
Tue, 11 May 2010 14:14:07 +0000 (16:14 +0200)
committerMichael Andres <ma@suse.de>
Wed, 12 May 2010 17:05:13 +0000 (19:05 +0200)
zypp/PublicKey.cc
zypp/PublicKey.h

index 80671f6..aea5bad 100644 (file)
@@ -9,11 +9,14 @@
 /** \file      zypp/PublicKey.cc
  *
 */
+#include <climits>
+
 #include <iostream>
 #include <vector>
 
 //#include "zypp/base/Logger.h"
 
+#include "zypp/base/Gettext.h"
 #include "zypp/base/String.h"
 #include "zypp/base/Regex.h"
 #include "zypp/PublicKey.h"
@@ -70,10 +73,13 @@ namespace zypp
       }
 
       std::string asString() const
-      { return "[" + id() + "-" + str::hexstring(created(),8).substr(2) + "] [" + name() + "] [" + fingerprint() + "]"; }
-
-      std::string armoredData() const
-      { return _data; }
+      {
+       return str::form( "[%s-%s] [%s] [%s] [TTL %d]",
+                         id().c_str(), str::hexstring(created(),8).substr(2).c_str(),
+                         name().c_str(),
+                         fingerprint().c_str(),
+                         daysToLive() );
+      }
 
       std::string id() const
       { return _id; }
@@ -84,15 +90,63 @@ namespace zypp
       std::string fingerprint() const
       { return _fingerprint; }
 
+      std::string gpgPubkeyVersion() const
+      { return _id.empty() ? _id : str::toLower( _id.substr(8,8) ); }
+
+      std::string gpgPubkeyRelease() const
+      { return _created ? str::hexstring( _created ).substr(2) : std::string(); }
+
       Date created() const
       { return _created; }
 
       Date expires() const
       { return _expires; }
 
+      std::string expiresAsString() const
+      {
+       if ( !_expires )
+       { // translators: an annotation to a gpg keys expiry date
+         return _("(does not expire)");
+       }
+       std::string ret( _expires.asString() );
+       int ttl( daysToLive() );
+       if ( ttl <= 90 )
+       {
+         ret += " ";
+         if ( ttl < 0 )
+         { // translators: an annotation to a gpg keys expiry date
+           ret += _("(EXPIRED)");
+         }
+         else if ( ttl == 0 )
+         { // translators: an annotation to a gpg keys expiry date
+           ret += _("(expires within 24h)");
+         }
+         else
+         { // translators: an annotation to a gpg keys expiry date
+           ret += str::form( _PL("(expires in %d day)", "(expires in %d days)", ttl ), ttl );
+         }
+       }
+       return ret;
+      }
+
       Pathname path() const
       { return _data_file.path(); }
 
+      bool expired() const
+      {
+       Date exp( expires() );
+       return( exp && exp < Date::now() );
+      }
+
+      int daysToLive() const
+      {
+       Date exp( expires() );
+       if ( ! expires() )
+         return INT_MAX;
+       exp -= Date::now();
+       return exp < 0 ? exp / Date::day - 1 : exp / Date::day;
+      }
+
     protected:
 
       void readFromFile()
@@ -199,7 +253,6 @@ namespace zypp
       std::string _id;
       std::string _name;
       std::string _fingerprint;
-      std::string _data;
       Date        _created;
       Date        _expires;
 
@@ -245,9 +298,6 @@ namespace zypp
   std::string PublicKey::asString() const
   { return _pimpl->asString(); }
 
-  std::string PublicKey::armoredData() const
-  { return _pimpl->armoredData(); }
-
   std::string PublicKey::id() const
   { return _pimpl->id(); }
 
@@ -257,12 +307,27 @@ namespace zypp
   std::string PublicKey::fingerprint() const
   { return _pimpl->fingerprint(); }
 
+  std::string PublicKey::gpgPubkeyVersion() const
+  { return _pimpl->gpgPubkeyVersion(); }
+
+  std::string PublicKey::gpgPubkeyRelease() const
+  { return _pimpl->gpgPubkeyRelease(); }
+
   Date PublicKey::created() const
   { return _pimpl->created(); }
 
   Date PublicKey::expires() const
   { return _pimpl->expires(); }
 
+  std::string PublicKey::expiresAsString() const
+  { return _pimpl->expiresAsString(); }
+
+  bool PublicKey::expired() const
+  { return _pimpl->expired(); }
+
+  int PublicKey::daysToLive() const
+  { return _pimpl->daysToLive(); }
+
   Pathname PublicKey::path() const
   { return _pimpl->path(); }
 
@@ -284,7 +349,9 @@ namespace zypp
     str << "  fpr " << obj.fingerprint() << endl;
     str << "   id " << obj.id() << endl;
     str << "  cre " << obj.created() << endl;
-    str << "  exp " << obj.expires() << endl;
+    str << "  exp " << obj.expiresAsString() << endl;
+    str << "  ttl " << obj.daysToLive() << endl;
+    str << "  rpm " << obj.gpgPubkeyVersion() << "-" << obj.gpgPubkeyRelease() << endl;
     str << "]";
     return str;
   }
index d2544b4..480d925 100644 (file)
@@ -111,22 +111,73 @@ namespace zypp
     { return ( ! id().empty() && ! fingerprint().empty() && !path().empty() ); }
 
     std::string asString() const;
-    std::string armoredData() const;
     std::string id() const;
     std::string name() const;
     std::string fingerprint() const;
 
+    /** Version rpm would assign to this key if imported into the rpm database.
+     * Rpm uses the lowercased trailing 8 byte from \ref id as \c version, and the
+     * creations dates lowercased hexadecimal representation as \c release.
+     * \see \ref gpgPubkeyRelease
+     * \code
+     * [zypp OBS Project <zypp@build.opensuse.org>]
+     *   fpr 47D7CE1DD600935B3B90365733D38EBC7FB7F464
+     *    id 33D38EBC7FB7F464           <-- trailing 8 byte
+     *   cre Thu Mar 13 19:15:40 2008   <-- converted to hex
+     *   exp Sat May 22 20:15:40 2010
+     * ]
+     *
+     * Converting the creation date to its hexadecimal representation:
+     * $ bc <<<"obase=16;$(date -d 'Thu Mar 13 19:15:40 2008' +%s)"
+     * 47D96F4C
+     *
+     * Rpms name for this key: gpg-pubkey-7fb7f464-47d96f4c
+     * \endcode
+     */
+    std::string gpgPubkeyVersion() const;
+
+    /** Release rpm would assign to this key if imported into the rpm database.
+     * This is the creations dates hexadecimal representation as \c release lowercased.
+     * \see \ref gpgPubkeyVersion
+     */
+    std::string gpgPubkeyRelease() const;
+
     /**
-     * Date when the key was created (time is 00:00:00)
+     * Date when the key was created.
      */
     Date created() const;
 
     /**
-     * Date when the key expires (time is 00:00:00)
+     * Date when the key expires.
      * If the key never expires the date is Date() (i.e. 0 seconds since the epoch (1.1.1970))
      */
     Date expires() const;
 
+    /**
+     * Expiry info in a human readable form.
+     * The exipry daye plus an annotation if the key has expired, or will
+     * expire within 90 days.
+     * \code
+     * (does not expire)
+     * Tue May 11 13:37:33 CEST 2010
+     * Tue May 11 13:37:33 CEST 2010 (expires in 90 days)
+     * Tue May 11 13:37:33 CEST 2010 (expires in 1 day)
+     * Tue May 11 13:37:33 CEST 2010 (expires within 24h)
+     * Tue May 11 13:37:33 CEST 2010 (EXPIRED)
+     * \endcode
+     */
+    std::string expiresAsString() const;
+
+    /** Whether the key has expired. */
+    bool expired() const;
+
+    /** Number of days (24h) until the key expires (or since it exired).
+     * A value of \c 0 means the key will expire within the next 24h.
+     * Negative values indicate the key has expired less than \c N days ago.
+     * For keys without expiration date \c INT_MAX is returned.
+     */
+    int daysToLive() const;
+
     Pathname path() const;
 
     bool operator==( PublicKey b ) const;