>From adaef8e7ca9d0c2a57eda09c52d0ed149e176224 Mon Sep 17 00:00:00 2001
From: Amol Kahat <akahat@redhat.com>
Date: Tue, 21 Jun 2016 13:20:59 +0530
Subject: [PATCH] Fixes: Invalid instance exception issue.

Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1348433
---
 base/server/python/pki/server/cli/instance.py | 56 ++++++++++++++++++++++-----
 1 file changed, 46 insertions(+), 10 deletions(-)

diff --git a/base/server/python/pki/server/cli/instance.py b/base/server/python/pki/server/cli/instance.py
index 6e336e11113cb8b9af7745143440ede95022cb7b..b2b31e1b806162caf74277504cfb08f79810a3ef 100644
--- a/base/server/python/pki/server/cli/instance.py
+++ b/base/server/python/pki/server/cli/instance.py
@@ -157,7 +157,11 @@ class InstanceCertExportCLI(pki.cli.CLI):
             sys.exit(1)
 
         instance = pki.server.PKIInstance(instance_name)
-        instance.load()
+        if instance.is_active():
+             instance.load()
+        else:
+            print('ERROR: Invalid tomcat instance specified.')
+            sys.exit(1)
 
         if not pkcs12_password and not pkcs12_password_file:
             pkcs12_password = getpass.getpass(prompt='Enter password for PKCS #12 file: ')
@@ -233,7 +237,11 @@ class InstanceFindCLI(pki.cli.CLI):
                 print()
 
             instance = pki.server.PKIInstance(instance_name)
-            instance.load()
+            if instance.is_valid():
+                instance.load()
+            else:
+                print('ERROR: Invalid tomcat instance specfified.')
+                sys.exit(1)
 
             InstanceCLI.print_instance(instance)
 
@@ -282,8 +290,12 @@ class InstanceShowCLI(pki.cli.CLI):
         instance_name = args[0]
 
         instance = pki.server.PKIInstance(instance_name)
-        instance.load()
-
+        if instance.is_valid():
+            instance.load()
+        else:
+            print('ERROR: Invalid tomcat instance specified.')
+            sys.exit(1)
+        
         InstanceCLI.print_instance(instance)
 
 
@@ -470,7 +482,12 @@ class InstanceMigrateCLI(pki.cli.CLI):
         module.set_debug(self.debug)
 
         instance = pki.server.PKIInstance(instance_name)
-        instance.load()
+
+        if instance.is_valid():
+            instance.load()
+        else:
+            print('ERROR: Invalid tomcat instance specified.')
+            sys.exit(1)
 
         module.migrate(  # pylint: disable=no-member,maybe-no-member
             instance,
@@ -526,7 +543,12 @@ class InstanceNuxwdogEnableCLI(pki.cli.CLI):
         module.set_verbose(self.verbose)
 
         instance = pki.server.PKIInstance(instance_name)
-        instance.load()
+        
+        if instance.is_valid():
+            instance.load()
+        else:
+            print('ERROR: Invalid tomcat instance specified.')
+            sys.exit(1)
 
         module.enable_nuxwdog(  # pylint: disable=no-member,maybe-no-member
             instance)
@@ -580,7 +602,11 @@ class InstanceNuxwdogDisableCLI(pki.cli.CLI):
         module.set_verbose(self.verbose)
 
         instance = pki.server.PKIInstance(instance_name)
-        instance.load()
+        if instance.is_valid():
+            instance.load()
+        else:
+            print('ERROR: Invalid tomcat instance specified.')
+            sys.exit(1)
 
         module.disable_nuxwdog(
             instance)  # pylint: disable=no-member,maybe-no-member
@@ -664,7 +690,12 @@ class InstanceExternalCertAddCLI(pki.cli.CLI):
             sys.exit(1)
 
         instance = pki.server.PKIInstance(instance_name)
-        instance.load()
+        
+        if instance.is_valid():
+            instance.load()
+        else:
+            print('ERROR: Invalid tomcat instance specified.')
+            sys.exit(1)
 
         if instance.external_cert_exists(nickname, token):
             print('ERROR: Certificate already imported for instance %s.' %
@@ -753,8 +784,13 @@ class InstanceExternalCertDeleteCLI(pki.cli.CLI):
             sys.exit(1)
 
         instance = pki.server.PKIInstance(instance_name)
-        instance.load()
-
+        
+        if instance.is_valid():
+            instance.load()
+        else:
+            print('ERROR: Invalid tomcat instance specified.')
+            sys.exit(1)
+        
         self.remove_cert(instance, nickname, token)
         instance.delete_external_cert(nickname, token)
 
-- 
2.5.5

