From 3f1e48fb4ced3d8236e1563af7072d8b6e6b6a64 Mon Sep 17 00:00:00 2001 From: Katsiaryna Tsytsenia Date: Fri, 11 Oct 2024 09:38:54 +0300 Subject: [PATCH] IJMP-1990 Fixed work with encrypted/plaintext credentials in the zowe config. Signed-off-by: Katsiaryna Tsytsenia --- .../zowe/kotlinsdk/zowe/config/ZoweConfig.kt | 32 +++++++++++++------ .../kotlinsdk/zowe/ZoweConfigParsingTest.kt | 19 +++++++++-- src/test/resources/zowe.config.json | 21 +++++++++++- 3 files changed, 58 insertions(+), 14 deletions(-) diff --git a/src/main/kotlin/org/zowe/kotlinsdk/zowe/config/ZoweConfig.kt b/src/main/kotlin/org/zowe/kotlinsdk/zowe/config/ZoweConfig.kt index c432c621..41a27467 100644 --- a/src/main/kotlin/org/zowe/kotlinsdk/zowe/config/ZoweConfig.kt +++ b/src/main/kotlin/org/zowe/kotlinsdk/zowe/config/ZoweConfig.kt @@ -274,10 +274,12 @@ data class ZoweConfig( ps.forEach { (_, profile) -> if (profile.profiles != null) extractSecureProperties(configCredentialsMap, profile.profiles) profile.secure?.forEach { secureProfileProp -> - profile.properties?.set( - secureProfileProp, - configCredentialsMap["profiles.${buildCredPath(profile, ".profiles.")}.properties.${secureProfileProp}"] - ) + if (profile.properties?.get(secureProfileProp) == null && + configCredentialsMap["profiles.${buildCredPath(profile, ".profiles.")}.properties.${secureProfileProp}"] != null) + profile.properties?.set( + "secure"+secureProfileProp, + configCredentialsMap["profiles.${buildCredPath(profile, ".profiles.")}.properties.${secureProfileProp}"] + ) } } } @@ -345,11 +347,11 @@ data class ZoweConfig( } val curr = buildCredPath(profile, ".profiles.") profile.secure?.forEach { propName -> - if (profile.properties?.containsKey(propName) == true) + if (profile.properties?.containsKey("secure"+propName) == true) if (curr == zosmfProfileName) - zosmfConfigCredentialsMap["profiles.${curr}.properties.${propName}"] = profile.properties[propName] + zosmfConfigCredentialsMap["profiles.${curr}.properties.${propName}"] = profile.properties["secure"+propName] else if (curr == baseProfileName) - baseConfigCredentialsMap["profiles.${curr}.properties.${propName}"] = profile.properties[propName] + baseConfigCredentialsMap["profiles.${curr}.properties.${propName}"] = profile.properties["secure"+propName] } } } @@ -395,7 +397,7 @@ data class ZoweConfig( private fun removeSecure(ps: Map?) { ps?.forEach { (_, v) -> v.secure?.forEach { propName -> - v.properties?.remove(propName) + v.properties?.remove("secure"+propName) } removeSecure(v.profiles) } @@ -425,11 +427,21 @@ data class ZoweConfig( var user: String? get() = searchProperty("user") { zosmf(); base() } as String? - set(el) { updateProperty("user", el ?: "") { zosmf(); base() } } + ?: searchProperty("secureuser") { zosmf(); base() } as String? + set(el) { if (searchProperty("user") { zosmf(); base() } as String? != null) + updateProperty("user", el ?: "") { zosmf(); base() } + else + updateProperty("secureuser", el ?: "") { zosmf(); base() } } var password: String? get() = searchProperty("password") { zosmf(); base() } as String? - set(el) { updateProperty("password", el ?: "") { zosmf(); base() } } + ?: searchProperty("securepassword") { zosmf(); base() } as String? + set(el) { + if (searchProperty("password") { zosmf(); base() } as String? != null) + updateProperty("password", el ?: "") { zosmf(); base() } + else + updateProperty("securepassword", el ?: "") { zosmf(); base() } + } var host: String? get() = searchProperty("host") { zosmf(); base() } as String? diff --git a/src/test/kotlin/org/zowe/kotlinsdk/zowe/ZoweConfigParsingTest.kt b/src/test/kotlin/org/zowe/kotlinsdk/zowe/ZoweConfigParsingTest.kt index c50c07c8..72618ffe 100644 --- a/src/test/kotlin/org/zowe/kotlinsdk/zowe/ZoweConfigParsingTest.kt +++ b/src/test/kotlin/org/zowe/kotlinsdk/zowe/ZoweConfigParsingTest.kt @@ -147,13 +147,26 @@ class ZoweConfigParsingTest: ZoweConfigTestBase() { Assertions.assertEquals(zoweConfig.encoding, 1037) zoweConfig.responseTimeout = 300 Assertions.assertEquals(zoweConfig.responseTimeout, 300) + Assertions.assertEquals(zoweConfig.user, "zosmfUser") + zoweConfig.user = null + Assertions.assertEquals(zoweConfig.user, "") + zoweConfig.user = "zUser" + Assertions.assertEquals(zoweConfig.user, "zUser") + Assertions.assertEquals(zoweConfig.password, "zosmfPassword") + zoweConfig.password = null + Assertions.assertEquals(zoweConfig.password, "") + zoweConfig.password = "zPassword" + Assertions.assertEquals(zoweConfig.password, "zPassword") zoweConfig.restoreProfile() Assertions.assertEquals(fullProfileName(zoweConfig.zosmfProfile), "lpar1.zosmf") Assertions.assertEquals(zoweConfig.sshProfile?.name, "ssh") Assertions.assertEquals(zoweConfig.tsoProfile?.name, "tso") Assertions.assertNull(zoweConfig.profile(null)) Assertions.assertNull(zoweConfig.profile(".")) - Assertions.assertNull(zoweConfig.profile("non.existent.profile")) + zoweConfig.setProfile("lpar1.section1.section2.emptyZosmfProfile") + Assertions.assertEquals(zoweConfig.user, "testUser") + zoweConfig.user = "zUser1" + Assertions.assertEquals(zoweConfig.user, "zUser1") zoweConfig.extractSecureProperties("/wrong/zowe/config/path", keytarWrapper) } @@ -164,8 +177,8 @@ class ZoweConfigParsingTest: ZoweConfigTestBase() { ZOSConnection( "example.host2", "443", - "testUser", - "testPassword", + "zosmfUser", + "zosmfPassword", profileName = "lpar1.section1.testParametersProfile", rejectUnauthorized = false, basePath = "/api", diff --git a/src/test/resources/zowe.config.json b/src/test/resources/zowe.config.json index 50932356..edb12169 100644 --- a/src/test/resources/zowe.config.json +++ b/src/test/resources/zowe.config.json @@ -18,6 +18,16 @@ "basePath": "/api" }, "profiles": { + "section2": { + "profiles": { + "emptyZosmfProfile": { + "secure": [ + "user", + "password" + ] + } + } + }, "testParametersProfile": { "type": "zosmf", "properties": { @@ -26,7 +36,9 @@ "rejectUnauthorized": false, "protocol": "https", "encoding": 37, - "responseTimeout": 600 + "responseTimeout": 600, + "user": "zosmfUser", + "password": "zosmfPassword" } } } @@ -37,6 +49,13 @@ "port": 10443 }, "secure": [] + }, + "base": { + "type": "base", + "secure": [ + "user", + "password" + ] } } },