Skip to content

Commit caa96d5

Browse files
authored
yubikey reset ssh session fix (#231)
1 parent 570cb36 commit caa96d5

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

src/main/kotlin/com/vk/admstorm/ssh/YubikeyHandler.kt

+18-13
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ class YubikeyHandler {
2222
private val LOG = logger<YubikeyHandler>()
2323
}
2424

25+
private val openscPath = if (SystemInfo.isLinux) {
26+
"usr/lib/x86_64-linux-gnu/opensc-pkcs11.so"
27+
} else {
28+
"/usr/local/lib/opensc-pkcs11.so"
29+
}
30+
2531
fun autoReset(project: Project, onFail: Runnable): Boolean {
2632
LOG.info("Try auto reset Yubikey")
2733
val resetScript = createScriptIfNotExists(project) ?: return false
@@ -44,10 +50,7 @@ class YubikeyHandler {
4450
PasswordSafe.instance.getPassword(credentialAttributes)!!
4551
}
4652

47-
val killOutput = CommandRunner.runLocally(project, "pkill ssh-agen")
48-
if (killOutput.exitCode != 0) {
49-
LOG.warn("pkill ssh-agen exited with non-zero code while Yubikey reset")
50-
}
53+
5154

5255
val evalOutput = CommandRunner.runLocallyEval("ssh-agent -s")
5356
if (evalOutput == null) {
@@ -63,11 +66,13 @@ class YubikeyHandler {
6366

6467
val echoBuilder = ProcessBuilder("echo", password)
6568

66-
val openscPath = if (SystemInfo.isLinux) {
67-
"usr/lib/x86_64-linux-gnu/opensc-pkcs11.so"
68-
} else {
69-
"/usr/local/lib/opensc-pkcs11.so"
69+
val sshResetKey = CommandRunner.runLocally(project, "ssh-add -e $openscPath")
70+
val resetOk = sshResetKey.stderr.contains("Card removed")
71+
if (!resetOk) {
72+
LOG.warn("Yubikey reset error: ${sshResetKey.stderr}")
73+
showYubikeyResetFailNotification(project, "Unable to reset yubikey", null, onFail)
7074
}
75+
7176
val sshAddBuilder = ProcessBuilder("ssh-add", "-s", openscPath)
7277

7378
sshAddBuilder.environment().apply {
@@ -82,9 +87,9 @@ class YubikeyHandler {
8287
sshAddBuilder
8388
)
8489
)
85-
} catch (e: IOException) {
86-
LOG.warn("Unexpected exception while startPipeline for Yubikey reset", e)
87-
showYubikeyResetFailNotification(project, "Unable to run reset commands", null, onFail)
90+
} catch (ex: IOException) {
91+
LOG.warn("Unexpected exception while startPipeline for Yubikey add", ex)
92+
showYubikeyResetFailNotification(project, "Unable to run add commands", null, onFail)
8893
return false
8994
}
9095

@@ -160,12 +165,12 @@ class YubikeyHandler {
160165
)
161166
return null
162167
}
163-
} catch (e: Exception) {
168+
} catch (ex: Exception) {
164169
MessageDialog.showWarning(
165170
"""
166171
Can't create script '${GitUIUtil.code(resetScript.absolutePath)}' for reset Yubikey:
167172
168-
${e.message}
173+
${ex.message}
169174
""".trimIndent(),
170175
"Problem with creating Yubikey reset script"
171176
)

0 commit comments

Comments
 (0)