>From 756e6dece7fb5043ac7ec8e11f4d6d2f014af4d3 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Tue, 10 May 2016 05:55:05 +0200
Subject: [PATCH] Added token status UNFORMATTED.

A new token status UNFORMATTED has been added for new tokens added
via UI/CLI and for TERMINATED tokens that are to be reused.

https://fedorahosted.org/pki/ticket/2287
---
 .../netscape/certsrv/tps/token/TokenStatus.java    |  2 ++
 base/tps-client/doc/CS.cfg                         | 22 ++++++++++++----------
 base/tps/shared/conf/CS.cfg                        | 22 ++++++++++++----------
 base/tps/shared/conf/token-states.properties       |  2 ++
 base/tps/shared/webapps/tps/js/token.js            |  1 +
 .../src/org/dogtagpki/server/tps/TPSTokendb.java   |  6 ++++--
 .../dogtagpki/server/tps/rest/TokenService.java    | 16 +++++++++++++---
 7 files changed, 46 insertions(+), 25 deletions(-)

diff --git a/base/common/src/com/netscape/certsrv/tps/token/TokenStatus.java b/base/common/src/com/netscape/certsrv/tps/token/TokenStatus.java
index ada2729b735fc03b1039da59f6e96c0983290bf5..76a4cf05b2ab38a3f1de9a479e1f785452a24519 100644
--- a/base/common/src/com/netscape/certsrv/tps/token/TokenStatus.java
+++ b/base/common/src/com/netscape/certsrv/tps/token/TokenStatus.java
@@ -55,6 +55,7 @@ public class TokenStatus {
     public final static int TOKEN_ACTIVE              = 4;
     public final static int TOKEN_TEMP_LOST_PERM_LOST = 5;
     public final static int TOKEN_TERMINATED          = 6;
+    public final static int TOKEN_UNFORMATTED         = 7;
 
     public final static TokenStatus FORMATTED           = new TokenStatus("FORMATTED", TOKEN_FORMATTED);
     public final static TokenStatus DAMAGED             = new TokenStatus("DAMAGED", TOKEN_DAMAGED);
@@ -63,6 +64,7 @@ public class TokenStatus {
     public final static TokenStatus ACTIVE              = new TokenStatus("ACTIVE", TOKEN_ACTIVE);
     public final static TokenStatus TEMP_LOST_PERM_LOST = new TokenStatus("TEMP_LOST_PERM_LOST", TOKEN_TEMP_LOST_PERM_LOST);
     public final static TokenStatus TERMINATED          = new TokenStatus("TERMINATED", TOKEN_TERMINATED);
+    public final static TokenStatus UNFORMATTED         = new TokenStatus("UNFORMATTED", TOKEN_UNFORMATTED);
 
     String name;
     Integer value;
diff --git a/base/tps-client/doc/CS.cfg b/base/tps-client/doc/CS.cfg
index c3f44e35014b5570a58ceddb3861dea3010824a7..6903a4bd541ad7a31339ca3ad9cec61856114f53 100644
--- a/base/tps-client/doc/CS.cfg
+++ b/base/tps-client/doc/CS.cfg
@@ -1487,10 +1487,11 @@ tokendb._069=#     DAMAGED = 1,
 tokendb._070=#     PERM_LOST = 2,
 tokendb._071=#     SUSPENDED = 3,
 tokendb._072=#     ACTIVE = 4,
-tokendb._073=#     TERMINATED = 6
-tokendb._074=# Note: Default transitions can be removed from the list if necessary.
-tokendb._075=# Do not add other transitions since they may not be supported.
-tokendb._076=#########################################
+tokendb._073=#     TERMINATED = 6,
+tokendb._074=#     UNFORMATTED = 7
+tokendb._075=# Note: Default transitions can be removed from the list if necessary.
+tokendb._076=# Do not add other transitions since they may not be supported.
+tokendb._077=#########################################
 tokendb.auditLog=[PKI_INSTANCE_PATH]/logs/tokendb-audit.log
 tokendb.hostport=[TOKENDB_HOST]:[TOKENDB_PORT]
 tokendb.ssl=false
@@ -1546,7 +1547,7 @@ tokendb.confirmDeleteConfigTemplate=confirmDeleteConfig.template
 log.instance.SignedAudit.selected.events=ROLE_ASSUME,CONFIG_CERT_POLICY,CONFIG_CERT_PROFILE,CONFIG_CRL_PROFILE,CONFIG_OCSP_PROFILE,CONFIG_AUTH,CONFIG_ROLE,CONFIG_ACL,CONFIG_SIGNED_AUDIT,CONFIG_ENCRYPTION,CONFIG_TRUSTED_PUBLIC_KEY,CONFIG_DRM,SELFTESTS_EXECUTION,AUDIT_LOG_DELETE,LOG_PATH_CHANGE,LOG_EXPIRATION_CHANGE,CONFIG,CONFIG_ROLE,CONFIG_TOKEN,CONFIG_PROFILE,CONFIG_AUDIT,APPLET_UPGRADE,KEY_CHANGEOVER,RENEWAL
 log.instance.SignedAudit.selectable.events=ROLE_ASSUME,CONFIG_CERT_POLICY,CONFIG_CERT_PROFILE,CONFIG_CRL_PROFILE,CONFIG_OCSP_PROFILE,CONFIG_AUTH,CONFIG_ROLE,CONFIG_ACL,CONFIG_SIGNED_AUDIT,CONFIG_ENCRYPTION,CONFIG_TRUSTED_PUBLIC_KEY,CONFIG_DRM,SELFTESTS_EXECUTION,AUDIT_LOG_DELETE,LOG_PATH_CHANGE,LOG_EXPIRATION_CHANGE,PRIVATE_KEY_ARCHIVE,PRIVATE_KEY_ARCHIVE_PROCESSED,KEY_RECOVERY_REQUEST,KEY_RECOVERY_AGENT_LOGIN,KEY_RECOVERY_PROCESSED,KEY_GEN_ASYMMETRIC,NON_PROFILE_CERT_REQUEST,CONFIG,CONFIG_ROLE,CONFIG_TOKEN,CONFIG_PROFILE,CONFIG_AUDIT,APPLET_UPGRADE,KEY_CHANGEOVER,RENEWAL
 log.instance.SignedAudit.nonselectable.events=AUDIT_LOG_STARTUP,AUDIT_LOG_SHUTDOWN,CERT_REQUEST_PROCESSED,CERT_STATUS_CHANGE_REQUEST,CERT_STATUS_CHANGE_REQUEST_PROCESSED,AUTHZ_SUCCESS,AUTHZ_FAIL,INTER_BOUNDARY,AUTH_FAIL,AUTH_SUCCESS,CERT_PROFILE_APPROVAL,PROOF_OF_POSSESSION,CRL_RETRIEVAL,CRL_VALIDATION,CMC_SIGNED_REQUEST_SIG_VERIFY,SERVER_SIDE_KEYGEN_PROCESSED,SERVER_SIDE_KEYGEN_REQUEST
-tokendb.allowedTransitions=0:1,0:2,0:3,0:6,3:2,3:6,4:1,4:2,4:3,4:6
+tokendb.allowedTransitions=0:1,0:2,0:3,0:6,3:2,3:6,4:1,4:2,4:3,4:6,6:7
 target._000=#########################################
 target._001=# entries to enable configuration of parameter sets through the TPS UI agent and admin tabs
 target._002=#
@@ -1594,11 +1595,12 @@ tps._007=# tps.cert.audit_signing.nickname=xxx
 tps._008=# tps.operations.allowedTransitions:
 tps._009=#   - token operations, like formatting and enrollment have transitions between the following states
 tps._010=#     FORMATTED = 0,
-tps._011=#     ACTIVE = 4
-tps._012=# Note: Default transitions can be removed from the list if necessary.
-tps._013=# Do not add other transitions since they may not be supported.
-tps._014=########################################
-tps.operations.allowedTransitions=0:0,0:4,4:4,4:0
+tps._011=#     ACTIVE = 4,
+tps._012=#     UNFORMATTED = 7
+tps._013=# Note: Default transitions can be removed from the list if necessary.
+tps._014=# Do not add other transitions since they may not be supported.
+tps._015=########################################
+tps.operations.allowedTransitions=0:0,0:4,4:4,4:0,7:0
 tps.cert.list=sslserver,subsystem,audit_signing
 tps.cert.sslserver.nickname=[HSM_LABEL][NICKNAME]
 tps.cert.subsystem.nickname=[HSM_LABEL][NICKNAME]
diff --git a/base/tps/shared/conf/CS.cfg b/base/tps/shared/conf/CS.cfg
index fef2e55f444a6d3f9c076977ae82cb43197150cb..638787d225ca819f051ad999ca92c75c9d51cb88 100644
--- a/base/tps/shared/conf/CS.cfg
+++ b/base/tps/shared/conf/CS.cfg
@@ -1959,16 +1959,17 @@ tokendb._069=#     DAMAGED = 1,
 tokendb._070=#     PERM_LOST = 2,
 tokendb._071=#     SUSPENDED = 3,
 tokendb._072=#     ACTIVE = 4,
-tokendb._073=#     TERMINATED = 6
-tokendb._074=# Note: Default transitions can be removed from the list if necessary.
-tokendb._075=# Do not add other transitions since they may not be supported.
-tokendb._076=#########################################
+tokendb._073=#     TERMINATED = 6,
+tokendb._074=#     UNFORMATTED = 7
+tokendb._075=# Note: Default transitions can be removed from the list if necessary.
+tokendb._076=# Do not add other transitions since they may not be supported.
+tokendb._077=#########################################
 tokendb.activityBaseDN=ou=Activities,[TOKENDB_ROOT]
 tokendb.addConfigTemplate=addConfig.template
 tokendb.addResultTemplate=addResults.template
 tokendb.agentSelectConfigTemplate=agentSelectConfig.template
 tokendb.agentViewConfigTemplate=agentViewConfig.template
-tokendb.allowedTransitions=0:1,0:2,0:3,0:6,3:2,3:6,4:1,4:2,4:3,4:6
+tokendb.allowedTransitions=0:1,0:2,0:3,0:6,3:2,3:6,4:1,4:2,4:3,4:6,6:7
 tokendb.auditAdminTemplate=auditAdmin.template
 tokendb.auditLog=[PKI_INSTANCE_PATH]/logs/tokendb-audit.log
 tokendb.baseDN=ou=Tokens,[TOKENDB_ROOT]
@@ -2025,16 +2026,17 @@ tps._007=# tps.cert.audit_signing.nickname=xxx
 tps._008=# tps.operations.allowedTransitions:
 tps._009=#   - token operations, like formatting and enrollment have transitions between the following states
 tps._010=#     FORMATTED = 0,
-tps._011=#     ACTIVE = 4
-tps._012=# Note: Default transitions can be removed from the list if necessary.
-tps._013=# Do not add other transitions since they may not be supported.
-tps._014=########################################
+tps._011=#     ACTIVE = 4,
+tps._012=#     UNFORMATTED = 7
+tps._013=# Note: Default transitions can be removed from the list if necessary.
+tps._014=# Do not add other transitions since they may not be supported.
+tps._015=########################################
 tps.cert.audit_signing.certusage=ObjectSigner
 tps.cert.audit_signing.nickname=[HSM_LABEL][NICKNAME]
 tps.cert.list=sslserver,subsystem,audit_signing
 tps.cert.sslserver.certusage=SSLServer
 tps.cert.subsystem.certusage=SSLClient
-tps.operations.allowedTransitions=0:0,0:4,4:4,4:0
+tps.operations.allowedTransitions=0:0,0:4,4:4,4:0,7:0
 usrgrp._000=##
 usrgrp._001=## User/Group
 usrgrp._002=##
diff --git a/base/tps/shared/conf/token-states.properties b/base/tps/shared/conf/token-states.properties
index c4bdb56e70abc7cde410a9688d7bc9a03367414c..4c972f3b107d7d573a8ea0ac952ddda02f585372 100644
--- a/base/tps/shared/conf/token-states.properties
+++ b/base/tps/shared/conf/token-states.properties
@@ -1,4 +1,5 @@
 # Token states
+UNFORMATTED         = Unformatted
 FORMATTED           = Formatted (uninitialized)
 ACTIVE              = Active
 SUSPENDED           = Suspended (temporarily lost)
@@ -20,3 +21,4 @@ ACTIVE.DAMAGED           = This token has been physically damaged.
 ACTIVE.PERM_LOST         = This token has been permanently lost.
 ACTIVE.SUSPENDED         = This token has been suspended (temporarily lost).
 ACTIVE.TERMINATED        = This token has been terminated.
+TERMINATED.UNFORMATTED   = Reuse this token.
diff --git a/base/tps/shared/webapps/tps/js/token.js b/base/tps/shared/webapps/tps/js/token.js
index be5ef7bdfdcf2e4e046fbbc7f84e989ab50df1b3..b5c3e9544d89c59dbf76671a2640f47eceb1cb60 100644
--- a/base/tps/shared/webapps/tps/js/token.js
+++ b/base/tps/shared/webapps/tps/js/token.js
@@ -21,6 +21,7 @@
 
 // TODO: load labels from server
 var TokenStatus = {
+    UNFORMATTED         : "Unformatted",
     FORMATTED           : "Formatted (uninitialized)",
     ACTIVE              : "Active",
     SUSPENDED           : "Suspended (temporarily lost)",
diff --git a/base/tps/src/org/dogtagpki/server/tps/TPSTokendb.java b/base/tps/src/org/dogtagpki/server/tps/TPSTokendb.java
index 52eadd35f4fb4829c9c3e1889516e813647d38e7..2e46b037c73a09719d39168041ad649919cc0819 100644
--- a/base/tps/src/org/dogtagpki/server/tps/TPSTokendb.java
+++ b/base/tps/src/org/dogtagpki/server/tps/TPSTokendb.java
@@ -38,6 +38,7 @@ import org.dogtagpki.tps.msg.EndOpMsg.TPSStatus;
 import com.netscape.certsrv.apps.CMS;
 import com.netscape.certsrv.base.EBaseException;
 import com.netscape.certsrv.base.IConfigStore;
+import com.netscape.certsrv.dbs.EDBRecordNotFoundException;
 import com.netscape.certsrv.tps.token.TokenStatus;
 
 import netscape.security.x509.RevocationReason;
@@ -183,8 +184,9 @@ public class TPSTokendb {
         TokenRecord existingTokenRecord;
         try {
             existingTokenRecord = tps.tokenDatabase.getRecord(id);
-        } catch (Exception e) {
-            CMS.debug("TPSTokendb.tdbUpdateTokenEntry: token entry not found; Adding");
+        } catch (EDBRecordNotFoundException e) {
+            CMS.debug("TPSTokendb.tdbUpdateTokenEntry: " + e);
+            CMS.debug("TPSTokendb.tdbUpdateTokenEntry: Adding token " + id);
             // add and exit
             tdbAddTokenEntry(tokenRecord, TokenStatus.FORMATTED);
             return;
diff --git a/base/tps/src/org/dogtagpki/server/tps/rest/TokenService.java b/base/tps/src/org/dogtagpki/server/tps/rest/TokenService.java
index 7d78b7166af53592756c190877bcaf8db22f283a..2104f292b66d76ac166df0a4da67d2fc2091a388 100644
--- a/base/tps/src/org/dogtagpki/server/tps/rest/TokenService.java
+++ b/base/tps/src/org/dogtagpki/server/tps/rest/TokenService.java
@@ -91,6 +91,16 @@ public class TokenService extends PKIService implements TokenResource {
         auditModParams.put("UserID", tokenRecord.getUserID());
 
         switch (tokenState.getValue()) {
+        case TokenStatus.TOKEN_UNFORMATTED:
+            tokenRecord.setUserID(null);
+            tokenRecord.setType(null);
+            tokenRecord.setAppletID(null);
+            tokenRecord.setKeyInfo(null);
+            tokenRecord.setPolicy(null);
+            tokenRecord.setTokenStatus(tokenState);
+            tokenRecord.setReason(null);
+            break;
+
         case TokenStatus.TOKEN_FORMATTED:
             tokenRecord.setTokenStatus(tokenState);
             tokenRecord.setReason(null);
@@ -368,9 +378,9 @@ public class TokenService extends PKIService implements TokenResource {
                 auditModParams.put("Policy", policy);
             }
 
-            // new tokens are FORMATTED when added via UI/CLI
-            tokenRecord.setTokenStatus(TokenStatus.FORMATTED);
-            auditModParams.put("Status", TokenStatus.FORMATTED.toString());
+            // new tokens are UNFORMATTED when added via UI/CLI
+            tokenRecord.setTokenStatus(TokenStatus.UNFORMATTED);
+            auditModParams.put("Status", TokenStatus.UNFORMATTED.toString());
 
             database.addRecord(tokenID, tokenRecord);
             subsystem.tdb.tdbActivity(ActivityDatabase.OP_ADD, tokenRecord,
-- 
2.4.11

