From b3047784b8b595848f07fd48cd89223c8c70ace6 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Thu, 12 Sep 2013 16:50:13 -0400
Subject: [PATCH] Connected TPS certificate database to LDAP.

The TPSCertDatabase has been reimplemented using LDAPDatabase. The
TPSCertRecord has been modified to specify the object class and
attribute mappings.

Ticket #652
---
 .../src/org/dogtagpki/server/tps/TPSSubsystem.java |  5 +-
 .../dogtagpki/server/tps/cert/TPSCertDatabase.java | 62 +++++++++-------------
 .../dogtagpki/server/tps/cert/TPSCertRecord.java   | 30 +++++++++--
 .../dogtagpki/server/tps/cert/TPSCertService.java  |  6 +--
 4 files changed, 57 insertions(+), 46 deletions(-)

diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSSubsystem.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSSubsystem.java
index 937ac6de0ad59523a5ccfa03f83cae58dbe2e394..d00378a1d0f34abfb35eeed974bfcfddc935188c 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSSubsystem.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSSubsystem.java
@@ -54,8 +54,8 @@ public class TPSSubsystem implements IAuthority, ISubsystem {
 
     public ActivityDatabase activityDatabase;
     public AuthenticatorDatabase authenticatorDatabase = new AuthenticatorDatabase();
+    public TPSCertDatabase certDatabase;
     public ConnectionDatabase connectionDatabase = new ConnectionDatabase();
-    public TPSCertDatabase certDatabase = new TPSCertDatabase();
     public TokenDatabase tokenDatabase;
 
     @Override
@@ -79,6 +79,9 @@ public class TPSSubsystem implements IAuthority, ISubsystem {
         String activityDatabaseDN = cs.getString("tokendb.activityBaseDN");
         activityDatabase = new ActivityDatabase(dbSubsystem, activityDatabaseDN);
 
+        String certDatabaseDN = cs.getString("tokendb.certBaseDN");
+        certDatabase = new TPSCertDatabase(dbSubsystem, certDatabaseDN);
+
         String tokenDatabaseDN = cs.getString("tokendb.baseDN");
         tokenDatabase = new TokenDatabase(dbSubsystem, tokenDatabaseDN);
     }
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/cert/TPSCertDatabase.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/cert/TPSCertDatabase.java
index 274ef0ec2750d16c17899d72db9e9ece843b209f..d07a11f6d417ec910467e4981047c0ab9ce7f352 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/cert/TPSCertDatabase.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/cert/TPSCertDatabase.java
@@ -20,7 +20,9 @@ package org.dogtagpki.server.tps.cert;
 
 import java.util.Date;
 
-import com.netscape.cmscore.dbs.Database;
+import com.netscape.certsrv.base.EBaseException;
+import com.netscape.certsrv.dbs.IDBSubsystem;
+import com.netscape.cmscore.dbs.LDAPDatabase;
 
 /**
  * This class implements in-memory activity database. In the future this
@@ -28,49 +30,33 @@ import com.netscape.cmscore.dbs.Database;
  *
  * @author Endi S. Dewata
  */
-public class TPSCertDatabase extends Database<TPSCertRecord> {
+public class TPSCertDatabase extends LDAPDatabase<TPSCertRecord> {
 
-    public TPSCertDatabase() {
-        super("Certificate");
-
-        // add sample records
-        try {
-            TPSCertRecord record1 = new TPSCertRecord();
-            record1.setID("cert1");
-            record1.setSerialNumber("16");
-            record1.setSubject("cn=someone");
-            record1.setTokenID("TOKEN0001");
-            record1.setKeyType("something");
-            record1.setStatus("active");
-            record1.setUserID("user1");
-            record1.setCreateTime(new Date());
-            record1.setModifyTime(new Date());
-            addRecord(record1);
-
-            TPSCertRecord record2 = new TPSCertRecord();
-            record2.setID("cert2");
-            record2.setSerialNumber("17");
-            record2.setSubject("cn=someone");
-            record2.setTokenID("TOKEN0002");
-            record2.setKeyType("something");
-            record2.setStatus("revoked");
-            record2.setUserID("user2");
-            record2.setCreateTime(new Date());
-            record2.setModifyTime(new Date());
-            addRecord(record2);
-
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
+    public TPSCertDatabase(IDBSubsystem dbSubsystem, String baseDN) throws EBaseException {
+        super("Certificate", dbSubsystem, baseDN, TPSCertRecord.class);
     }
 
-    public void addRecord(TPSCertRecord certRecord) throws Exception {
+    @Override
+    public void addRecord(String id, TPSCertRecord certRecord) throws Exception {
         certRecord.setCreateTime(new Date());
 
-        addRecord(certRecord.getID(), certRecord);
+        super.addRecord(id, certRecord);
     }
 
-    public void updateRecord(TPSCertRecord certRecord) throws Exception {
-        updateRecord(certRecord.getID(), certRecord);
+    @Override
+    public void updateRecord(String id, TPSCertRecord certRecord) throws Exception {
+        certRecord.setModifyTime(new Date());
+
+        super.updateRecord(id, certRecord);
+    }
+
+    @Override
+    public String createDN(String id) {
+        return "cn=" + id + "," + baseDN;
+    }
+
+    @Override
+    public String createFilter(String filter) {
+        return "(id=*)";
     }
 }
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/cert/TPSCertRecord.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/cert/TPSCertRecord.java
index 26b2c156d34c564adf6b8a50ef979aeb3da0fd5a..6a55e60d67e8d4a205c7b720098ed664dda4eb04 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/cert/TPSCertRecord.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/cert/TPSCertRecord.java
@@ -20,10 +20,17 @@ package org.dogtagpki.server.tps.cert;
 
 import java.util.Date;
 
+import com.netscape.cmscore.dbs.DBAttribute;
+import com.netscape.cmscore.dbs.DBObjectClasses;
+import com.netscape.cmscore.dbs.DBRecord;
+
 /**
  * @author Endi S. Dewata
  */
-public class TPSCertRecord {
+@DBObjectClasses({ "top", "tokenCert" })
+public class TPSCertRecord extends DBRecord {
+
+    private static final long serialVersionUID = 1L;
 
     String id;
     String serialNumber;
@@ -42,14 +49,16 @@ public class TPSCertRecord {
     Date createTime;
     Date modifyTime;
 
-    public String getID() {
+    @DBAttribute("cn")
+    public String getId() {
         return id;
     }
 
-    public void setID(String id) {
+    public void setId(String id) {
         this.id = id;
     }
 
+    @DBAttribute("tokenSerial")
     public String getSerialNumber() {
         return serialNumber;
     }
@@ -58,6 +67,7 @@ public class TPSCertRecord {
         this.serialNumber = serialNumber;
     }
 
+    @DBAttribute("tokenSubject")
     public String getSubject() {
         return subject;
     }
@@ -66,6 +76,7 @@ public class TPSCertRecord {
         this.subject = subject;
     }
 
+    @DBAttribute("tokenID")
     public String getTokenID() {
         return tokenID;
     }
@@ -74,6 +85,7 @@ public class TPSCertRecord {
         this.tokenID = tokenID;
     }
 
+    @DBAttribute("tokenKeyType")
     public String getKeyType() {
         return keyType;
     }
@@ -82,6 +94,7 @@ public class TPSCertRecord {
         this.keyType = keyType;
     }
 
+    @DBAttribute("tokenStatus")
     public String getStatus() {
         return status;
     }
@@ -90,6 +103,7 @@ public class TPSCertRecord {
         this.status = status;
     }
 
+    @DBAttribute("tokenUserID")
     public String getUserID() {
         return userID;
     }
@@ -98,7 +112,7 @@ public class TPSCertRecord {
         this.userID = userID;
     }
 
-
+    @DBAttribute("userCertificate")
     public String getCertificate() {
         return certificate;
     }
@@ -107,6 +121,7 @@ public class TPSCertRecord {
         this.certificate = certificate;
     }
 
+    @DBAttribute("tokenIssuer")
     public String getIssuedBy() {
         return issuedBy;
     }
@@ -115,6 +130,7 @@ public class TPSCertRecord {
         this.issuedBy = issuedBy;
     }
 
+    @DBAttribute("tokenOrigin")
     public String getOrigin() {
         return origin;
     }
@@ -123,6 +139,7 @@ public class TPSCertRecord {
         this.origin = origin;
     }
 
+    @DBAttribute("tokenType")
     public String getType() {
         return type;
     }
@@ -131,6 +148,7 @@ public class TPSCertRecord {
         this.type = type;
     }
 
+    @DBAttribute("tokenNotBefore")
     public Date getValidNotBefore() {
         return validNotBefore;
     }
@@ -139,6 +157,7 @@ public class TPSCertRecord {
         this.validNotBefore = validNotBefore;
     }
 
+    @DBAttribute("tokenNotAfter")
     public Date getValidNotAfter() {
         return validNotAfter;
     }
@@ -147,6 +166,7 @@ public class TPSCertRecord {
         this.validNotAfter = validNotAfter;
     }
 
+    @DBAttribute("extensions")
     public String getExtensions() {
         return extensions;
     }
@@ -155,6 +175,7 @@ public class TPSCertRecord {
         this.extensions = extensions;
     }
 
+    @DBAttribute("dateOfCreate")
     public Date getCreateTime() {
         return createTime;
     }
@@ -163,6 +184,7 @@ public class TPSCertRecord {
         this.createTime = createTime;
     }
 
+    @DBAttribute("dateOfModify")
     public Date getModifyTime() {
         return modifyTime;
     }
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/cert/TPSCertService.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/cert/TPSCertService.java
index 140a8cc2a25dbe891b05da0c971130b88f66fc11..944c5f227236e83e3a324028f44928aadbdacef3 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/cert/TPSCertService.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/cert/TPSCertService.java
@@ -47,7 +47,7 @@ public class TPSCertService extends PKIService implements TPSCertResource {
     public TPSCertData createCertData(TPSCertRecord certRecord) {
 
         TPSCertData certData = new TPSCertData();
-        certData.setID(certRecord.getID());
+        certData.setID(certRecord.getId());
         certData.setSerialNumber(certRecord.getSerialNumber());
         certData.setSubject(certRecord.getSubject());
         certData.setTokenID(certRecord.getTokenID());
@@ -57,7 +57,7 @@ public class TPSCertService extends PKIService implements TPSCertResource {
         certData.setCreateTime(certRecord.getCreateTime());
         certData.setModifyTime(certRecord.getModifyTime());
 
-        String certID = certRecord.getID();
+        String certID = certRecord.getId();
         try {
             certID = URLEncoder.encode(certID, "UTF-8");
         } catch (UnsupportedEncodingException e) {
@@ -74,7 +74,7 @@ public class TPSCertService extends PKIService implements TPSCertResource {
     public TPSCertRecord createCertRecord(TPSCertData certData) {
 
         TPSCertRecord certRecord = new TPSCertRecord();
-        certRecord.setID(certData.getID());
+        certRecord.setId(certData.getID());
         certRecord.setSerialNumber(certData.getSerialNumber());
         certRecord.setSubject(certData.getSubject());
         certRecord.setTokenID(certData.getTokenID());
-- 
1.8.1.4

