Hi,

I tried to fix NumberFormatException when i did tps-cert-find with
non-integer/invalid range value for size and start.I was doing testing
for tps-cert and then i came across this.I thought giving some
additional info to users inplace of numberformat.I have done similar
fix  on rhel7  compile it and make a jar and test on rhel7 .I can share
that patch if needed. Below are the test result.

Before fix testing:

1. pki -h pki1.example.com -p 25080 tps-cert-find --start "gy"
NumberFormatException: For input string: "gy"

2. pki -h pki1.example.com -p 25080 tps-cert-find --size "gy"
NumberFormatException: For input string: "gy"

3. pki -p 25080 tps-cert-find --start
1789999999999999999999999999999999999999999999
NumberFormatException: For input string:
"1789999999999999999999999999999999999999999999"


After fix testing:

1. [root@pki1 ~]# pki -d /opt/rhqa_pki/certdb -c Secret123 -h
pki1.example.com -p 25080 tps-cert-find --start "gy"
Error: Enter valid integer value for size/start option
usage: tps-cert-find [FILTER] [OPTIONS...]
    --help            Show help options
    --size <size>     Page size
    --start <start>   Page start
    --token <ID>      Token ID

2. [root@pki1 ~]# pki -d /opt/rhqa_pki/certdb -c Secret123 -h
pki1.example.com -p 25080 tps-cert-find --size "hy"
Error: Enter valid integer value for size/start option
usage: tps-cert-find [FILTER] [OPTIONS...]
    --help            Show help options
    --size <size>     Page size
    --start <start>   Page start
    --token <ID>      Token ID

3. [root@pki1 ~]# pki -d /opt/rhqa_pki/certdb -c Secret123 -h
pki1.example.com -p 25080 tps-cert-find --start 1
-----------------
2 entries matched
-----------------
  Cert ID: 3d.20160720042931
  Serial Number: 0x3d
  Subject: UID=ldapuser7,O=Token Key User
  Token ID: 40906145C76224192D78
  Key Type: encryption
  Status: active
  User ID: ldapuser7
  Create Time: Wed Jul 20 04:29:31 EDT 2016
----------------------------
Number of entries returned 1
----------------------------

4. [root@pki1 ~]# pki -d /opt/rhqa_pki/certdb -c Secret123 -h
pki1.example.com -p 25080 tps-cert-find --size 1
-----------------
2 entries matched
-----------------
  Cert ID: 3c.20160720042931
  Serial Number: 0x3c
  Subject: UID=ldapuser7,O=Token Key User
  Token ID: 40906145C76224192D78
  Key Type: signing
  Status: active
  User ID: ldapuser7
  Create Time: Wed Jul 20 04:29:31 EDT 2016
----------------------------
Number of entries returned 1
----------------------------

5. [root@pki1 cert]# pki -d /opt/rhqa_pki/certdb -c Secret123 -h
pki1.example.com -p 25080 tps-cert-find --start
1789999999999999999999999999999999999999999999
Error: Enter valid integer value for size/start option
usage: tps-cert-find [FILTER] [OPTIONS...]
    --help            Show help options
    --size <size>     Page size
    --start <start>   Page start
    --token <ID>      Token ID

Thanks
Geetika
From 6ed113a347f392c5237c59f4e3b7bda71dd1aee3 Mon Sep 17 00:00:00 2001
From: Geetika Kapoor <[email protected]>
Date: Wed, 27 Jul 2016 08:15:59 -0400
Subject: [PATCH] Added logging inplace of NumberFormatException for tps-cert
 size and start

Signed-off-by: Geetika Kapoor <[email protected]>
---
 .../netscape/cmstools/tps/cert/TPSCertFindCLI.java | 54 +++++++++++-----------
 1 file changed, 28 insertions(+), 26 deletions(-)

diff --git a/base/java-tools/src/com/netscape/cmstools/tps/cert/TPSCertFindCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/cert/TPSCertFindCLI.java
index 9cbdad6da5ea26a618c8fda05a575ced9b4942d0..ec03c1a0e923453da8ed2c1141792f29c00c3616 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/cert/TPSCertFindCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/cert/TPSCertFindCLI.java
@@ -84,32 +84,34 @@ public class TPSCertFindCLI extends CLI {
         String filter = cmdArgs.length > 0 ? cmdArgs[0] : null;
 
         String tokenID = cmd.getOptionValue("token");
-
-        String s = cmd.getOptionValue("start");
-        Integer start = s == null ? null : Integer.valueOf(s);
-
-        s = cmd.getOptionValue("size");
-        Integer size = s == null ? null : Integer.valueOf(s);
-
-        TPSCertCollection result = certCLI.certClient.findCerts(filter, tokenID, start, size);
-
-        MainCLI.printMessage(result.getTotal() + " entries matched");
-        if (result.getTotal() == 0) return;
-
-        Collection<TPSCertData> certs = result.getEntries();
-        boolean first = true;
-
-        for (TPSCertData certData : certs) {
-
-            if (first) {
-                first = false;
-            } else {
-                System.out.println();
-            }
-
-            TPSCertCLI.printCert(certData);
+        try {
+
+       	    String s = cmd.getOptionValue("start");
+            Integer start = s == null ? null : Integer.valueOf(s);
+	    s = cmd.getOptionValue("size");
+            Integer size = s == null ? null : Integer.valueOf(s);
+            TPSCertCollection result = certCLI.certClient.findCerts(filter, tokenID, start, size);
+            MainCLI.printMessage(result.getTotal() + " entries matched");
+            if (result.getTotal() == 0) return;
+            Collection<TPSCertData> certs = result.getEntries();
+            boolean first = true;
+            for (TPSCertData certData : certs) {
+                if (first) {
+                    first = false;
+                } else {
+                    System.out.println();
+                }
+                TPSCertCLI.printCert(certData);
+            }  
+            MainCLI.printMessage("Number of entries returned " + certs.size());
+           } catch (NumberFormatException e) {
+            System.err.println("Error: Enter valid integer value for size/start option");
+            printHelp();
+            System.exit(-1);
+           } catch (Exception e) {
+            System.err.println("Error: " + e.getMessage());
+            printHelp();
+            System.exit(-1);
         }
-
-        MainCLI.printMessage("Number of entries returned " + certs.size());
     }
 }
-- 
1.8.3.1

_______________________________________________
Pki-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/pki-devel

Reply via email to