From 985451a1d2862b59b6de4607fed44de15aa2afd0 Mon Sep 17 00:00:00 2001 From: Richardas Kuchinskas Date: Wed, 10 Apr 2024 22:08:18 +0300 Subject: [PATCH 1/2] solution prototype --- .../com/vk/admstorm/ssh/YubikeyHandler.kt | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/com/vk/admstorm/ssh/YubikeyHandler.kt b/src/main/kotlin/com/vk/admstorm/ssh/YubikeyHandler.kt index 79c6b85d..d6cae0f7 100644 --- a/src/main/kotlin/com/vk/admstorm/ssh/YubikeyHandler.kt +++ b/src/main/kotlin/com/vk/admstorm/ssh/YubikeyHandler.kt @@ -44,9 +44,10 @@ class YubikeyHandler { PasswordSafe.instance.getPassword(credentialAttributes)!! } - val killOutput = CommandRunner.runLocally(project, "pkill ssh-agen") - if (killOutput.exitCode != 0) { - LOG.warn("pkill ssh-agen exited with non-zero code while Yubikey reset") + val openscPath = if (SystemInfo.isLinux) { + "usr/lib/x86_64-linux-gnu/opensc-pkcs11.so" + } else { + "/usr/local/lib/opensc-pkcs11.so" } val evalOutput = CommandRunner.runLocallyEval("ssh-agent -s") @@ -63,11 +64,15 @@ class YubikeyHandler { val echoBuilder = ProcessBuilder("echo", password) - val openscPath = if (SystemInfo.isLinux) { - "usr/lib/x86_64-linux-gnu/opensc-pkcs11.so" - } else { - "/usr/local/lib/opensc-pkcs11.so" + val sshResetKey = CommandRunner.runLocally(project,"ssh-add -e $openscPath") + + val resetOk = sshResetKey.stderr.contains("Card removed") + + if (!resetOk) { + LOG.warn("Yubikey reset error: ${sshResetKey.stderr}") + showYubikeyResetFailNotification(project, "Unable to reset yubikey", null, onFail) } + val sshAddBuilder = ProcessBuilder("ssh-add", "-s", openscPath) sshAddBuilder.environment().apply { @@ -82,9 +87,9 @@ class YubikeyHandler { sshAddBuilder ) ) - } catch (e: IOException) { - LOG.warn("Unexpected exception while startPipeline for Yubikey reset", e) - showYubikeyResetFailNotification(project, "Unable to run reset commands", null, onFail) + } catch (ex: IOException) { + LOG.warn("Unexpected exception while startPipeline for Yubikey add", ex) + showYubikeyResetFailNotification(project, "Unable to run add commands", null, onFail) return false } @@ -160,12 +165,12 @@ class YubikeyHandler { ) return null } - } catch (e: Exception) { + } catch (ex: Exception) { MessageDialog.showWarning( """ Can't create script '${GitUIUtil.code(resetScript.absolutePath)}' for reset Yubikey: - ${e.message} + ${ex.message} """.trimIndent(), "Problem with creating Yubikey reset script" ) From b6c3199a1fc60b33db63958172dff5613b7ad76b Mon Sep 17 00:00:00 2001 From: Richardas Kuchinskas Date: Fri, 24 May 2024 13:33:02 +0300 Subject: [PATCH 2/2] review fix --- .../kotlin/com/vk/admstorm/ssh/YubikeyHandler.kt | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/com/vk/admstorm/ssh/YubikeyHandler.kt b/src/main/kotlin/com/vk/admstorm/ssh/YubikeyHandler.kt index d6cae0f7..e29a62e8 100644 --- a/src/main/kotlin/com/vk/admstorm/ssh/YubikeyHandler.kt +++ b/src/main/kotlin/com/vk/admstorm/ssh/YubikeyHandler.kt @@ -22,6 +22,12 @@ class YubikeyHandler { private val LOG = logger() } + private val openscPath = if (SystemInfo.isLinux) { + "usr/lib/x86_64-linux-gnu/opensc-pkcs11.so" + } else { + "/usr/local/lib/opensc-pkcs11.so" + } + fun autoReset(project: Project, onFail: Runnable): Boolean { LOG.info("Try auto reset Yubikey") val resetScript = createScriptIfNotExists(project) ?: return false @@ -44,11 +50,7 @@ class YubikeyHandler { PasswordSafe.instance.getPassword(credentialAttributes)!! } - val openscPath = if (SystemInfo.isLinux) { - "usr/lib/x86_64-linux-gnu/opensc-pkcs11.so" - } else { - "/usr/local/lib/opensc-pkcs11.so" - } + val evalOutput = CommandRunner.runLocallyEval("ssh-agent -s") if (evalOutput == null) { @@ -64,10 +66,8 @@ class YubikeyHandler { val echoBuilder = ProcessBuilder("echo", password) - val sshResetKey = CommandRunner.runLocally(project,"ssh-add -e $openscPath") - + val sshResetKey = CommandRunner.runLocally(project, "ssh-add -e $openscPath") val resetOk = sshResetKey.stderr.contains("Card removed") - if (!resetOk) { LOG.warn("Yubikey reset error: ${sshResetKey.stderr}") showYubikeyResetFailNotification(project, "Unable to reset yubikey", null, onFail)