summaryrefslogtreecommitdiffstats
path: root/base/tps
diff options
context:
space:
mode:
authorEndi S. Dewata <edewata@redhat.com>2013-07-22 15:28:50 -0400
committerEndi S. Dewata <edewata@redhat.com>2013-08-13 10:38:12 -0400
commit74f60e95a9fd5984f81aeda492e157d1c9b6dedd (patch)
tree18b086682bb02da702ae985a75e9f94de6b1794e /base/tps
parent23cce978aedc948723a3cce211b4e743b0e7c444 (diff)
downloadpki-74f60e95a9fd5984f81aeda492e157d1c9b6dedd.tar.gz
pki-74f60e95a9fd5984f81aeda492e157d1c9b6dedd.tar.xz
pki-74f60e95a9fd5984f81aeda492e157d1c9b6dedd.zip
Added skeleton for token services.
A skeleton for token service and the clients has been added. Currently it's storing the database in memory. The actual implementation using LDAP database will be added after the TPS configuration code is ready. Ticket #652
Diffstat (limited to 'base/tps')
-rw-r--r--base/tps/java/CMakeLists.txt9
-rw-r--r--base/tps/java/org/dogtagpki/tps/server/TPSApplication.java5
-rw-r--r--base/tps/java/org/dogtagpki/tps/server/TPSSubsystem.java (renamed from base/tps/java/org/dogtagpki/tps/server/TPSAuthority.java)15
-rw-r--r--base/tps/java/org/dogtagpki/tps/token/TokenDatabase.java76
-rw-r--r--base/tps/java/org/dogtagpki/tps/token/TokenRecord.java188
-rw-r--r--base/tps/java/org/dogtagpki/tps/token/TokenService.java245
-rw-r--r--base/tps/shared/conf/CS.cfg.in2
7 files changed, 537 insertions, 3 deletions
diff --git a/base/tps/java/CMakeLists.txt b/base/tps/java/CMakeLists.txt
index 9cfad9f0c..c8f90e44e 100644
--- a/base/tps/java/CMakeLists.txt
+++ b/base/tps/java/CMakeLists.txt
@@ -70,6 +70,13 @@ find_file(PKI_NSUTIL_JAR
/usr/share/java/pki
)
+find_file(RESTEASY_ATOM_PROVIDER_JAR
+ NAMES
+ resteasy-atom-provider.jar
+ PATHS
+ ${RESTEASY_LIB}
+)
+
find_file(SERVLET_JAR
NAMES
servlet.jar
@@ -93,7 +100,7 @@ javac(pki-tps-classes
CLASSPATH
${PKI_CERTSRV_JAR} ${PKI_CMS_JAR} ${PKI_CMSCORE_JAR}
${PKI_CMSUTIL_JAR} ${PKI_NSUTIL_JAR}
- ${LDAPJDK_JAR} ${JAXRS_API_JAR}
+ ${LDAPJDK_JAR} ${JAXRS_API_JAR} ${RESTEASY_ATOM_PROVIDER_JAR}
${JSS_JAR} ${COMMONS_CODEC_JAR} ${SYMKEY_JAR} ${SERVLET_JAR}
OUTPUT_DIR
${CMAKE_BINARY_DIR}/classes
diff --git a/base/tps/java/org/dogtagpki/tps/server/TPSApplication.java b/base/tps/java/org/dogtagpki/tps/server/TPSApplication.java
index 4b05be934..2f2b2a63a 100644
--- a/base/tps/java/org/dogtagpki/tps/server/TPSApplication.java
+++ b/base/tps/java/org/dogtagpki/tps/server/TPSApplication.java
@@ -22,6 +22,8 @@ import java.util.Set;
import javax.ws.rs.core.Application;
+import org.dogtagpki.tps.token.TokenService;
+
import com.netscape.certsrv.acls.ACLInterceptor;
import com.netscape.certsrv.authentication.AuthMethodInterceptor;
import com.netscape.certsrv.base.PKIException;
@@ -60,6 +62,9 @@ public class TPSApplication extends Application {
// system certs
classes.add(SystemCertService.class);
+ // tokens
+ classes.add(TokenService.class);
+
// exception mapper
classes.add(PKIException.Mapper.class);
diff --git a/base/tps/java/org/dogtagpki/tps/server/TPSAuthority.java b/base/tps/java/org/dogtagpki/tps/server/TPSSubsystem.java
index 326945a64..92017812c 100644
--- a/base/tps/java/org/dogtagpki/tps/server/TPSAuthority.java
+++ b/base/tps/java/org/dogtagpki/tps/server/TPSSubsystem.java
@@ -17,6 +17,8 @@
// --- END COPYRIGHT BLOCK ---
package org.dogtagpki.tps.server;
+import org.dogtagpki.tps.token.TokenDatabase;
+
import com.netscape.certsrv.apps.CMS;
import com.netscape.certsrv.authority.IAuthority;
import com.netscape.certsrv.base.EBaseException;
@@ -29,7 +31,9 @@ import com.netscape.certsrv.request.IRequestQueue;
/**
* @author Endi S. Dewata <edewata@redhat.com>
*/
-public class TPSAuthority implements IAuthority, ISubsystem {
+public class TPSSubsystem implements IAuthority, ISubsystem {
+
+ public final static TPSSubsystem INSTANCE = new TPSSubsystem();
public ILogger logger = CMS.getLogger();
@@ -38,6 +42,12 @@ public class TPSAuthority implements IAuthority, ISubsystem {
public ISubsystem owner;
public IConfigStore config;
+ public TokenDatabase tokenDatabase = new TokenDatabase();
+
+ public static TPSSubsystem getInstance() {
+ return INSTANCE;
+ }
+
@Override
public String getId() {
return id;
@@ -99,4 +109,7 @@ public class TPSAuthority implements IAuthority, ISubsystem {
return "tps";
}
+ public TokenDatabase getTokenDatabase() {
+ return tokenDatabase;
+ }
}
diff --git a/base/tps/java/org/dogtagpki/tps/token/TokenDatabase.java b/base/tps/java/org/dogtagpki/tps/token/TokenDatabase.java
new file mode 100644
index 000000000..3db76649f
--- /dev/null
+++ b/base/tps/java/org/dogtagpki/tps/token/TokenDatabase.java
@@ -0,0 +1,76 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+
+package org.dogtagpki.tps.token;
+
+import java.util.Collection;
+import java.util.Date;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+/**
+ * This class implements in-memory token database. In the future this
+ * will be replaced with LDAP database.
+ *
+ * @author Endi S. Dewata
+ */
+public class TokenDatabase {
+
+ public final static int DEFAULT_SIZE = 20;
+
+ Map<String, TokenRecord> tokens = new LinkedHashMap<String, TokenRecord>();
+
+ public Collection<TokenRecord> getTokens() throws Exception {
+ return tokens.values();
+ }
+
+ public TokenRecord getToken(String tokenID) throws Exception {
+ if (!tokens.containsKey(tokenID)) {
+ throw new Exception("Token "+ tokenID + " does not exist.");
+ }
+ return tokens.get(tokenID);
+ }
+
+ public void addToken(TokenRecord tokenRecord) throws Exception {
+ if (tokens.containsKey(tokenRecord.getID())) {
+ throw new Exception("Token "+ tokenRecord.getID() + " already exists.");
+ }
+
+ tokenRecord.setStatus("ENABLED");
+ tokenRecord.setCreateTimestamp(new Date());
+
+ tokens.put(tokenRecord.getID(), tokenRecord);
+ }
+
+ public void updateToken(String tokenID, TokenRecord tokenRecord) throws Exception {
+ if (!tokens.containsKey(tokenRecord.getID())) {
+ throw new Exception("Token "+ tokenRecord.getID() + " does not exist.");
+ }
+
+ tokenRecord.setModifyTimestamp(new Date());
+
+ tokens.put(tokenRecord.getID(), tokenRecord);
+ }
+
+ public void removeToken(String tokenID) throws Exception {
+ if (!tokens.containsKey(tokenID)) {
+ throw new Exception("Token "+ tokenID + " does not exist.");
+ }
+ tokens.remove(tokenID);
+ }
+}
diff --git a/base/tps/java/org/dogtagpki/tps/token/TokenRecord.java b/base/tps/java/org/dogtagpki/tps/token/TokenRecord.java
new file mode 100644
index 000000000..1f9d9caf5
--- /dev/null
+++ b/base/tps/java/org/dogtagpki/tps/token/TokenRecord.java
@@ -0,0 +1,188 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+
+package org.dogtagpki.tps.token;
+
+import java.util.Date;
+
+import com.netscape.certsrv.token.TokenData;
+
+/**
+ * @author Endi S. Dewata
+ */
+public class TokenRecord {
+
+ String id;
+ String userID;
+ String status;
+ String reason;
+ String appletID;
+ String keyInfo;
+ Date createTimestamp;
+ Date modifyTimestamp;
+
+ public String getID() {
+ return id;
+ }
+
+ public void setID(String id) {
+ this.id = id;
+ }
+
+ public String getUserID() {
+ return userID;
+ }
+
+ public void setUserID(String userID) {
+ this.userID = userID;
+ }
+
+ public String getStatus() {
+ return status;
+ }
+
+ public void setStatus(String status) {
+ this.status = status;
+ }
+
+ public String getReason() {
+ return reason;
+ }
+
+ public void setReason(String reason) {
+ this.reason = reason;
+ }
+
+ public String getAppletID() {
+ return appletID;
+ }
+
+ public void setAppletID(String appletID) {
+ this.appletID = appletID;
+ }
+
+ public String getKeyInfo() {
+ return keyInfo;
+ }
+
+ public void setKeyInfo(String keyInfo) {
+ this.keyInfo = keyInfo;
+ }
+
+ public Date getCreateTimestamp() {
+ return createTimestamp;
+ }
+
+ public void setCreateTimestamp(Date createTimestamp) {
+ this.createTimestamp = createTimestamp;
+ }
+
+ public Date getModifyTimestamp() {
+ return modifyTimestamp;
+ }
+
+ public void setModifyTimestamp(Date modifyTimestamp) {
+ this.modifyTimestamp = modifyTimestamp;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((appletID == null) ? 0 : appletID.hashCode());
+ result = prime * result + ((createTimestamp == null) ? 0 : createTimestamp.hashCode());
+ result = prime * result + ((id == null) ? 0 : id.hashCode());
+ result = prime * result + ((keyInfo == null) ? 0 : keyInfo.hashCode());
+ result = prime * result + ((modifyTimestamp == null) ? 0 : modifyTimestamp.hashCode());
+ result = prime * result + ((reason == null) ? 0 : reason.hashCode());
+ result = prime * result + ((status == null) ? 0 : status.hashCode());
+ result = prime * result + ((userID == null) ? 0 : userID.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ TokenRecord other = (TokenRecord) obj;
+ if (appletID == null) {
+ if (other.appletID != null)
+ return false;
+ } else if (!appletID.equals(other.appletID))
+ return false;
+ if (createTimestamp == null) {
+ if (other.createTimestamp != null)
+ return false;
+ } else if (!createTimestamp.equals(other.createTimestamp))
+ return false;
+ if (id == null) {
+ if (other.id != null)
+ return false;
+ } else if (!id.equals(other.id))
+ return false;
+ if (keyInfo == null) {
+ if (other.keyInfo != null)
+ return false;
+ } else if (!keyInfo.equals(other.keyInfo))
+ return false;
+ if (modifyTimestamp == null) {
+ if (other.modifyTimestamp != null)
+ return false;
+ } else if (!modifyTimestamp.equals(other.modifyTimestamp))
+ return false;
+ if (reason == null) {
+ if (other.reason != null)
+ return false;
+ } else if (!reason.equals(other.reason))
+ return false;
+ if (status == null) {
+ if (other.status != null)
+ return false;
+ } else if (!status.equals(other.status))
+ return false;
+ if (userID == null) {
+ if (other.userID != null)
+ return false;
+ } else if (!userID.equals(other.userID))
+ return false;
+ return true;
+ }
+
+ public static void main(String args[]) throws Exception {
+
+ TokenData before = new TokenData();
+ before.setID("token1");
+ before.setUserID("user1");
+ before.setStatus("revoked");
+ before.setReason("lost");
+ before.setAppletID("APPLET1234");
+ before.setKeyInfo("key info");
+ before.setCreateTimestamp(new Date());
+ before.setModifyTimestamp(new Date());
+
+ String string = before.toString();
+ System.out.println(string);
+
+ TokenData after = TokenData.valueOf(string);
+ System.out.println(before.equals(after));
+ }
+}
diff --git a/base/tps/java/org/dogtagpki/tps/token/TokenService.java b/base/tps/java/org/dogtagpki/tps/token/TokenService.java
new file mode 100644
index 000000000..bc8b35d59
--- /dev/null
+++ b/base/tps/java/org/dogtagpki/tps/token/TokenService.java
@@ -0,0 +1,245 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+
+package org.dogtagpki.tps.token;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URI;
+import java.net.URLEncoder;
+import java.util.Iterator;
+
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import org.dogtagpki.tps.server.TPSSubsystem;
+import org.jboss.resteasy.plugins.providers.atom.Link;
+
+import com.netscape.certsrv.base.PKIException;
+import com.netscape.certsrv.token.TokenCollection;
+import com.netscape.certsrv.token.TokenData;
+import com.netscape.certsrv.token.TokenModifyRequest;
+import com.netscape.certsrv.token.TokenResource;
+import com.netscape.cms.servlet.base.PKIService;
+
+/**
+ * @author Endi S. Dewata
+ */
+public class TokenService extends PKIService implements TokenResource {
+
+ public final static int DEFAULT_SIZE = 20;
+
+ public TokenService() {
+ System.out.println("TokenService.<init>()");
+ }
+
+ public TokenData createTokenData(TokenRecord tokenRecord) {
+
+ TokenData tokenData = new TokenData();
+ tokenData.setID(tokenRecord.getID());
+ tokenData.setUserID(tokenRecord.getUserID());
+ tokenData.setStatus(tokenRecord.getStatus());
+ tokenData.setReason(tokenRecord.getReason());
+ tokenData.setAppletID(tokenRecord.getAppletID());
+ tokenData.setKeyInfo(tokenRecord.getKeyInfo());
+ tokenData.setCreateTimestamp(tokenRecord.getCreateTimestamp());
+ tokenData.setModifyTimestamp(tokenRecord.getModifyTimestamp());
+
+ String tokenID = tokenRecord.getID();
+ try {
+ tokenID = URLEncoder.encode(tokenID, "UTF-8");
+ } catch (UnsupportedEncodingException e) {
+ e.printStackTrace();
+ throw new PKIException(e.getMessage());
+ }
+
+ URI uri = uriInfo.getBaseUriBuilder().path(TokenResource.class).path("{tokenID}").build(tokenID);
+ tokenData.setLink(new Link("self", uri));
+
+ return tokenData;
+ }
+
+ public TokenRecord createTokenRecord(TokenData tokenData) {
+
+ TokenRecord tokenRecord = new TokenRecord();
+ tokenRecord.setID(tokenData.getID());
+ tokenRecord.setUserID(tokenData.getUserID());
+ tokenRecord.setStatus(tokenData.getStatus());
+ tokenRecord.setReason(tokenData.getReason());
+ tokenRecord.setAppletID(tokenData.getAppletID());
+ tokenRecord.setKeyInfo(tokenData.getKeyInfo());
+ tokenRecord.setCreateTimestamp(tokenData.getCreateTimestamp());
+ tokenRecord.setModifyTimestamp(tokenData.getModifyTimestamp());
+
+ return tokenRecord;
+ }
+
+ @Override
+ public TokenCollection findTokens(Integer start, Integer size) {
+
+ System.out.println("TokenService.findTokens()");
+
+ try {
+ start = start == null ? 0 : start;
+ size = size == null ? DEFAULT_SIZE : size;
+
+ TPSSubsystem subsystem = TPSSubsystem.getInstance();
+ TokenDatabase database = subsystem.getTokenDatabase();
+
+ Iterator<TokenRecord> tokens = database.getTokens().iterator();
+
+ TokenCollection response = new TokenCollection();
+
+ int i = 0;
+
+ // skip to the start of the page
+ for ( ; i<start && tokens.hasNext(); i++) tokens.next();
+
+ // return entries up to the page size
+ for ( ; i<start+size && tokens.hasNext(); i++) {
+ response.addToken(createTokenData(tokens.next()));
+ }
+
+ // count the total entries
+ for ( ; tokens.hasNext(); i++) tokens.next();
+
+ if (start > 0) {
+ URI uri = uriInfo.getRequestUriBuilder().replaceQueryParam("start", Math.max(start-size, 0)).build();
+ response.addLink(new Link("prev", uri));
+ }
+
+ if (start+size < i) {
+ URI uri = uriInfo.getRequestUriBuilder().replaceQueryParam("start", start+size).build();
+ response.addLink(new Link("next", uri));
+ }
+
+ return response;
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ throw new PKIException(e.getMessage());
+ }
+ }
+
+ @Override
+ public TokenData getToken(String tokenID) {
+
+ System.out.println("TokenService.getToken(\"" + tokenID + "\")");
+
+ try {
+ TPSSubsystem subsystem = TPSSubsystem.getInstance();
+ TokenDatabase database = subsystem.getTokenDatabase();
+
+ return createTokenData(database.getToken(tokenID));
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ throw new PKIException(e.getMessage());
+ }
+ }
+
+ @Override
+ public Response addToken(TokenData tokenData) {
+
+ System.out.println("TokenService.addToken(\"" + tokenData.getID() + "\")");
+
+ try {
+ TPSSubsystem subsystem = TPSSubsystem.getInstance();
+ TokenDatabase database = subsystem.getTokenDatabase();
+
+ database.addToken(createTokenRecord(tokenData));
+ tokenData = createTokenData(database.getToken(tokenData.getID()));
+
+ return Response
+ .created(tokenData.getLink().getHref())
+ .entity(tokenData)
+ .type(MediaType.APPLICATION_XML)
+ .build();
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ throw new PKIException(e.getMessage());
+ }
+ }
+
+ @Override
+ public Response updateToken(String tokenID, TokenData tokenData) {
+
+ System.out.println("TokenService.updateToken(\"" + tokenID + "\")");
+
+ try {
+ TPSSubsystem subsystem = TPSSubsystem.getInstance();
+ TokenDatabase database = subsystem.getTokenDatabase();
+
+ TokenRecord tokenRecord = database.getToken(tokenID);
+ tokenRecord.setUserID(tokenData.getUserID());
+ database.updateToken(tokenData.getID(), tokenRecord);
+
+ tokenData = createTokenData(database.getToken(tokenID));
+
+ return Response
+ .ok(tokenData)
+ .type(MediaType.APPLICATION_XML)
+ .build();
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ throw new PKIException(e.getMessage());
+ }
+ }
+
+ @Override
+ public Response modifyToken(String tokenID, TokenModifyRequest request) {
+
+ System.out.println("TokenService.modifyToken(\"" + tokenID + "\", request");
+
+ try {
+ TPSSubsystem subsystem = TPSSubsystem.getInstance();
+ TokenDatabase database = subsystem.getTokenDatabase();
+
+ TokenRecord tokenRecord = database.getToken(tokenID);
+ // TODO: perform modification
+
+ TokenData tokenData = createTokenData(tokenRecord);
+
+ return Response
+ .ok(tokenData)
+ .type(MediaType.APPLICATION_XML)
+ .build();
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ throw new PKIException(e.getMessage());
+ }
+ }
+
+ @Override
+ public void removeToken(String tokenID) {
+
+ System.out.println("TokenService.removeToken(\"" + tokenID + "\")");
+
+ try {
+ TPSSubsystem subsystem = TPSSubsystem.getInstance();
+ TokenDatabase database = subsystem.getTokenDatabase();
+ database.removeToken(tokenID);
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ throw new PKIException(e.getMessage());
+ }
+ }
+}
diff --git a/base/tps/shared/conf/CS.cfg.in b/base/tps/shared/conf/CS.cfg.in
index 046bb8308..e972bcb9d 100644
--- a/base/tps/shared/conf/CS.cfg.in
+++ b/base/tps/shared/conf/CS.cfg.in
@@ -1509,7 +1509,7 @@ op.format.tokenKey.issuerinfo.value=http://[PKI_HOSTNAME]:[PKI_UNSECURE_PORT]/cg
passwordFile=[PKI_INSTANCE_PATH]/conf/password.conf
passwordClass=com.netscape.cmsutil.password.PlainPasswordFile
registry.file=[PKI_INSTANCE_PATH]/conf/registry.cfg
-subsystem.0.class=org.dogtagpki.tps.server.TPSAuthority
+subsystem.0.class=org.dogtagpki.tps.server.TPSSubsystem
subsystem.0.id=tps
tokendb._000=#########################################
tokendb._001=# tokendb.auditLog: