From 958fa839df2e0bd1a88539a05613539609da3e12 Mon Sep 17 00:00:00 2001
From: Ade Lee <alee@redhat.com>
Date: Sat, 16 Apr 2016 11:21:17 -0400
Subject: [PATCH 287/293] Add realm to the key record

Part of Trac Ticket# 2041
---
 .../src/com/netscape/certsrv/dbs/keydb/IKeyRecord.java   | 15 ++++++++++++---
 base/common/src/com/netscape/certsrv/key/KeyInfo.java    | 11 +++++++++++
 .../java-tools/src/com/netscape/cmstools/key/KeyCLI.java |  2 ++
 .../src/com/netscape/cmscore/dbs/KeyDBSchema.java        |  1 +
 .../cmscore/src/com/netscape/cmscore/dbs/KeyRecord.java  | 16 ++++++++++++----
 .../src/com/netscape/cmscore/dbs/KeyRepository.java      |  8 ++++++--
 6 files changed, 44 insertions(+), 9 deletions(-)

diff --git a/base/common/src/com/netscape/certsrv/dbs/keydb/IKeyRecord.java b/base/common/src/com/netscape/certsrv/dbs/keydb/IKeyRecord.java
index d1eb05f3d9e79a0b194116374b8f8ca5aaff537f..f66d53f1ebf1536835469847f43620441fac4189 100644
--- a/base/common/src/com/netscape/certsrv/dbs/keydb/IKeyRecord.java
+++ b/base/common/src/com/netscape/certsrv/dbs/keydb/IKeyRecord.java
@@ -20,8 +20,8 @@ package com.netscape.certsrv.dbs.keydb;
 import java.math.BigInteger;
 import java.util.Date;
 
-import com.netscape.certsrv.base.MetaInfo;
 import com.netscape.certsrv.base.EBaseException;
+import com.netscape.certsrv.base.MetaInfo;
 
 /**
  * An interface contains constants for key record.
@@ -44,6 +44,7 @@ public interface IKeyRecord {
     public static final String ATTR_CLIENT_ID = "clientId";
     public static final String ATTR_DATA_TYPE = "dataType";
     public static final String ATTR_STATUS = "status";
+    public static final String ATTR_REALM = "realm";
 
 
     // key state
@@ -93,9 +94,9 @@ public interface IKeyRecord {
     /**
      * Retrieves meta info.
      *
-     * @return MetaInfo 
+     * @return MetaInfo
      */
-    public MetaInfo getMetaInfo(); 
+    public MetaInfo getMetaInfo();
 
     /**
      * Retrieves client ID.
@@ -158,4 +159,12 @@ public interface IKeyRecord {
      * @exception EBaseException failed to retrieve public key data
      */
     public byte[] getPublicKeyData() throws EBaseException;
+
+    /**
+     * Retrieves authorization realm.
+     *
+     * @return authorization realm
+     * @exception EBaseException failed to retrieve authorization realm
+     */
+    public String getRealm() throws EBaseException;
 }
diff --git a/base/common/src/com/netscape/certsrv/key/KeyInfo.java b/base/common/src/com/netscape/certsrv/key/KeyInfo.java
index 204cac493456f91ea5cac86977b6bc64095587af..016b98c418484ccee7e81af0b111ec6dcc5f242d 100644
--- a/base/common/src/com/netscape/certsrv/key/KeyInfo.java
+++ b/base/common/src/com/netscape/certsrv/key/KeyInfo.java
@@ -58,6 +58,9 @@ public class KeyInfo {
     @XmlElement
     private String publicKey;
 
+    @XmlElement
+    private String realm;
+
     public KeyInfo() {
         // required for JAXB (defaults)
     }
@@ -157,4 +160,12 @@ public class KeyInfo {
         }
     }
 
+    public String getRealm() {
+        return realm;
+    }
+
+    public void setRealm(String realm) {
+        this.realm = realm;
+    }
+
 }
diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyCLI.java
index f242ece14df24705b8db85a3588a1e3ee6fe7d84..d2b425b9fbe0e9616b579dc2f8490ce13d55b82c 100644
--- a/base/java-tools/src/com/netscape/cmstools/key/KeyCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/key/KeyCLI.java
@@ -108,6 +108,7 @@ public class KeyCLI extends CLI {
         if (info.getAlgorithm() != null) System.out.println("  Algorithm: "+info.getAlgorithm());
         if (info.getSize() != null) System.out.println("  Size: "+info.getSize());
         if (info.getOwnerName() != null) System.out.println("  Owner: "+info.getOwnerName());
+        if (info.getRealm() != null) System.out.println("  Realm: " + info.getRealm());
         if (info.getPublicKey() != null) {
             // Print out the Base64 encoded public key in the form of a blob,
             // where the max line length is 64.
@@ -123,5 +124,6 @@ public class KeyCLI extends CLI {
         if (info.getKeyId() != null) System.out.println("  Key ID: "+info.getKeyId().toHexString());
         if (info.getRequestType() != null) System.out.println("  Type: "+info.getRequestType());
         if (info.getRequestStatus() != null) System.out.println("  Status: "+info.getRequestStatus());
+        if (info.getRealm() != null) System.out.println("  Realm: "+ info.getRealm());
     }
 }
diff --git a/base/server/cmscore/src/com/netscape/cmscore/dbs/KeyDBSchema.java b/base/server/cmscore/src/com/netscape/cmscore/dbs/KeyDBSchema.java
index 74317631853fc1871c8a6ff8c1c5c88676ce861c..12f69950a25333ede74557975a2b8a9955d1d10e 100644
--- a/base/server/cmscore/src/com/netscape/cmscore/dbs/KeyDBSchema.java
+++ b/base/server/cmscore/src/com/netscape/cmscore/dbs/KeyDBSchema.java
@@ -48,4 +48,5 @@ public class KeyDBSchema {
     public static final String LDAP_ATTR_CLIENT_ID = "clientId";
     public static final String LDAP_ATTR_STATUS = "status";
     public static final String LDAP_ATTR_DATA_TYPE = "dataType";
+    public static final String LDAP_ATTR_REALM = "realm";
 }
diff --git a/base/server/cmscore/src/com/netscape/cmscore/dbs/KeyRecord.java b/base/server/cmscore/src/com/netscape/cmscore/dbs/KeyRecord.java
index 7ae5461849741c9133caeb169eaaef70731c9da2..fbf2ee227a4f629506337be63c28c6c8d2d72d48 100644
--- a/base/server/cmscore/src/com/netscape/cmscore/dbs/KeyRecord.java
+++ b/base/server/cmscore/src/com/netscape/cmscore/dbs/KeyRecord.java
@@ -40,9 +40,6 @@ import com.netscape.certsrv.dbs.keydb.KeyState;
  */
 public class KeyRecord implements IDBObj, IKeyRecord {
 
-    /**
-     *
-     */
     private static final long serialVersionUID = -3765000841161998984L;
     private BigInteger mSerialNo = null;
     private KeyState mState = null;
@@ -59,6 +56,7 @@ public class KeyRecord implements IDBObj, IKeyRecord {
     private String mClientId = null;
     private String mStatus = null;
     private String mDataType = null;
+    private String mRealm = null;
 
 
     protected static Vector<String> mNames = new Vector<String>();
@@ -78,6 +76,7 @@ public class KeyRecord implements IDBObj, IKeyRecord {
         mNames.addElement(ATTR_CLIENT_ID);
         mNames.addElement(ATTR_STATUS);
         mNames.addElement(ATTR_DATA_TYPE);
+        mNames.addElement(ATTR_REALM);
     }
 
     /**
@@ -141,6 +140,8 @@ public class KeyRecord implements IDBObj, IKeyRecord {
             mDataType = (String) object;
         } else if (name.equalsIgnoreCase(ATTR_STATUS)) {
             mStatus = (String) object;
+        } else if (name.equalsIgnoreCase(ATTR_REALM)) {
+            mRealm = (String) object;
         } else {
             throw new EBaseException(com.netscape.certsrv.apps.CMS.getUserMessage("CMS_BASE_INVALID_ATTRIBUTE", name));
         }
@@ -181,6 +182,8 @@ public class KeyRecord implements IDBObj, IKeyRecord {
             return mDataType;
         } else if (name.equalsIgnoreCase(ATTR_STATUS)) {
             return mStatus;
+        } else if (name.equalsIgnoreCase(ATTR_REALM)) {
+            return mRealm;
         } else {
             throw new EBaseException(com.netscape.certsrv.apps.CMS.getUserMessage("CMS_BASE_INVALID_ATTRIBUTE", name));
         }
@@ -339,7 +342,7 @@ public class KeyRecord implements IDBObj, IKeyRecord {
     }
 
     /**
-     * Sets the dateso of revocation.
+     * Sets the date of revocation.
      * <P>
      */
     public void setDateOfRevocation(Date dates[]) throws EBaseException {
@@ -389,4 +392,9 @@ public class KeyRecord implements IDBObj, IKeyRecord {
     public String getDataType() throws EBaseException {
         return mDataType;
     }
+
+    @Override
+    public String getRealm() throws EBaseException {
+        return mRealm;
+    }
 }
diff --git a/base/server/cmscore/src/com/netscape/cmscore/dbs/KeyRepository.java b/base/server/cmscore/src/com/netscape/cmscore/dbs/KeyRepository.java
index 556cab9f5d44c946437a5b5cf2c8b0ad831a7e0b..88028d7989af93c0854adfa38d6a0d34cb21d928 100644
--- a/base/server/cmscore/src/com/netscape/cmscore/dbs/KeyRepository.java
+++ b/base/server/cmscore/src/com/netscape/cmscore/dbs/KeyRepository.java
@@ -27,8 +27,6 @@ import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.ThreadFactory;
 import java.util.concurrent.TimeUnit;
 
-import netscape.security.x509.X500Name;
-
 import com.netscape.certsrv.apps.CMS;
 import com.netscape.certsrv.base.EBaseException;
 import com.netscape.certsrv.dbs.EDBException;
@@ -43,6 +41,8 @@ import com.netscape.certsrv.dbs.keydb.IKeyRecordList;
 import com.netscape.certsrv.dbs.keydb.IKeyRepository;
 import com.netscape.certsrv.dbs.repository.IRepository;
 
+import netscape.security.x509.X500Name;
+
 /**
  * A class represents a Key repository. This is the container of
  * archived keys.
@@ -148,6 +148,10 @@ public class KeyRepository extends Repository implements IKeyRepository {
             reg.registerAttribute(KeyRecord.ATTR_DATA_TYPE, new
                     StringMapper(KeyDBSchema.LDAP_ATTR_DATA_TYPE));
         }
+        if (!reg.isAttributeRegistered(KeyRecord.ATTR_REALM)) {
+            reg.registerAttribute(KeyRecord.ATTR_REALM, new
+                    StringMapper(KeyDBSchema.LDAP_ATTR_REALM));
+        }
 
     }
 
-- 
2.4.3

