1515import zowe .client .sdk .teamconfig .keytar .IKeyTar ;
1616import zowe .client .sdk .teamconfig .keytar .KeyTarConfig ;
1717
18- import java .util .ArrayList ;
1918import java .util .List ;
2019
2120/**
@@ -32,10 +31,22 @@ public class KeyTarService {
3231 /**
3332 * List of service names used for KeyTar querying of OS credential store
3433 */
35- private final List <String > serviceNames = List .of ("Zowe" , "Zowe-Plugin" );
34+ private static final List <String > SERVICE_NAMES = List .of ("Zowe" , "Zowe-Plugin" );
3635
3736 /**
38- * IKeyTar implementation holder
37+ * Account name used for KeyTar querying of OS credential store
38+ */
39+ private static final String ACCOUNT_NAME = "secure_config_props" ;
40+
41+ /**
42+ * Duplicate Zowe credential store types found.
43+ */
44+ private static final String MULTIPLE_WARNING_MSG =
45+ "Multiple OS credential stores found related to Zowe. Returning the first one on list." ;
46+
47+ /**
48+ * This service is not thread-safe. The injected IKeyTar instance
49+ * is stateful and mutated during processing.
3950 */
4051 private final IKeyTar keyTar ;
4152
@@ -57,29 +68,25 @@ public KeyTarService(final IKeyTar keyTar) {
5768 * @author Frank Giordano
5869 */
5970 public KeyTarConfig getKeyTarConfig () throws TeamConfigException {
60- List <KeyTarConfig > keyTarConfigs = new ArrayList <>();
61- // Account name used for KeyTar querying of OS credential store
62- final String accountName = "secure_config_props" ;
63- keyTar .setAccountName (accountName );
64- for (final String serviceName : serviceNames ) {
71+ keyTar .setAccountName (ACCOUNT_NAME );
72+ for (final String serviceName : SERVICE_NAMES ) {
6573 keyTar .setServiceName (serviceName );
6674 try {
6775 keyTar .processKey ();
6876 } catch (TeamConfigException e ) {
69- LOG .debug (e . getMessage () );
77+ LOG .debug ("KeyTar lookup failed for service {}" , serviceName , e );
7078 continue ;
7179 }
72- LOG .debug ("KeyTar Value {}" , keyTar .getKeyTarValue ());
73- keyTarConfigs = keyTar .getKeyConfigs ();
74- break ;
75- }
76- if (keyTarConfigs .isEmpty ()) {
77- throw new IllegalStateException ("No OS credential store related to Zowe found." );
78- }
79- if (keyTarConfigs .size () > 1 ) {
80- LOG .debug ("Multiple OS credential stores found related to Zowe. Returning the first one on list." );
80+ List <KeyTarConfig > keyTarConfigs = keyTar .getKeyConfigs ();
81+ if (!keyTarConfigs .isEmpty ()) {
82+ if (keyTarConfigs .size () > 1 ) {
83+ LOG .debug ("{} Service={}" , MULTIPLE_WARNING_MSG , serviceName );
84+ }
85+ return keyTarConfigs .get (0 );
86+ }
8187 }
82- return keyTarConfigs .get (0 );
88+
89+ throw new TeamConfigException ("No OS credential store related to Zowe found." );
8390 }
8491
8592}
0 commit comments