Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

yubikey reset ssh session fix #231

Merged
merged 2 commits into from
May 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions src/main/kotlin/com/vk/admstorm/ssh/YubikeyHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ class YubikeyHandler {
private val LOG = logger<YubikeyHandler>()
}

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
Expand All @@ -44,10 +50,7 @@ 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 evalOutput = CommandRunner.runLocallyEval("ssh-agent -s")
if (evalOutput == null) {
Expand All @@ -63,11 +66,13 @@ 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 {
Expand All @@ -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
}

Expand Down Expand Up @@ -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"
)
Expand Down
Loading