From d6a616f7bfdb15f0fcb284095183457c176d0b65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Przypa=C5=9Bniak?= Date: Sun, 14 Nov 2021 17:26:12 +0100 Subject: [PATCH] Fixes after WTTech review #414 --- .../com/cognifide/cogboard/ssh/SSHClient.kt | 13 +++++---- .../cogboard/ssh/auth/SSHAuthData.kt | 27 +++++++++---------- .../strategy/SSHKeyAuthSessionStrategy.kt | 3 ++- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/cogboard-app/src/main/kotlin/com/cognifide/cogboard/ssh/SSHClient.kt b/cogboard-app/src/main/kotlin/com/cognifide/cogboard/ssh/SSHClient.kt index 695a1523..1163deb8 100644 --- a/cogboard-app/src/main/kotlin/com/cognifide/cogboard/ssh/SSHClient.kt +++ b/cogboard-app/src/main/kotlin/com/cognifide/cogboard/ssh/SSHClient.kt @@ -46,25 +46,23 @@ class SSHClient : AbstractVerticle() { } fun tryToConnect(config: JsonObject) { - LOGGER.info(config) coroutineScope.launch { try { connect(config) } catch (e: JSchException) { - LOGGER.error(e.message) val eventBusAddress = config.getString(CogboardConstants.Props.EVENT_ADDRESS) - vertx.eventBus().send(eventBusAddress, e.message) + sendError(e, eventBusAddress) } } } - private suspend fun connect(config: JsonObject) { + private fun connect(config: JsonObject) { val authData = SSHAuthData(config) createSSHChannel(authData) executeCommandAndSendResult(config) } - private suspend fun createSSHChannel(authData: SSHAuthData) { + private fun createSSHChannel(authData: SSHAuthData) { with(authData) { initSSHSession(authData) if (session.isConnected) { @@ -111,6 +109,11 @@ class SSHClient : AbstractVerticle() { return responseBuffer } + private fun sendError(e: Exception, eventBusAddress: String) { + LOGGER.error(e.message) + vertx.eventBus().send(eventBusAddress, e.message) + } + companion object { val LOGGER: Logger = LoggerFactory.getLogger(SSHClient::class.java) diff --git a/cogboard-app/src/main/kotlin/com/cognifide/cogboard/ssh/auth/SSHAuthData.kt b/cogboard-app/src/main/kotlin/com/cognifide/cogboard/ssh/auth/SSHAuthData.kt index 26ef790f..867adcb9 100644 --- a/cogboard-app/src/main/kotlin/com/cognifide/cogboard/ssh/auth/SSHAuthData.kt +++ b/cogboard-app/src/main/kotlin/com/cognifide/cogboard/ssh/auth/SSHAuthData.kt @@ -1,6 +1,6 @@ package com.cognifide.cogboard.ssh.auth -import com.cognifide.cogboard.CogboardConstants +import com.cognifide.cogboard.CogboardConstants.Props import com.cognifide.cogboard.ssh.auth.AuthenticationType.BASIC import com.cognifide.cogboard.ssh.auth.AuthenticationType.SSH_KEY import io.vertx.core.json.Json @@ -8,18 +8,17 @@ import io.vertx.core.json.JsonArray import io.vertx.core.json.JsonObject class SSHAuthData(private val config: JsonObject) { - val user = config.getString(CogboardConstants.Props.USER) ?: "" - val password = config.getString(CogboardConstants.Props.PASSWORD) ?: "" - val token = config.getString(CogboardConstants.Props.TOKEN) ?: "" - val key = config.getString(CogboardConstants.Props.SSH_KEY) ?: "" - val host = config.getString(CogboardConstants.Props.SSH_HOST) ?: "" - val port = config.getInteger(CogboardConstants.Props.SSH_PORT) ?: 22 + val user = config.getString(Props.USER) ?: "" + val password = config.getString(Props.PASSWORD) ?: "" + val token = config.getString(Props.TOKEN) ?: "" + val key = config.getString(Props.SSH_KEY) ?: "" + val host = config.getString(Props.SSH_HOST) ?: "" + val port = config.getInteger(Props.SSH_PORT) ?: 22 val authenticationType = fromConfigAuthenticationType() private fun fromConfigAuthenticationType(): AuthenticationType { - val authTypesString = config.getString(CogboardConstants.Props.AUTHENTICATION_TYPES) - - val authTypes = authTypesString?.let { Json.decodeValue(authTypesString) } ?: JsonArray() + val authTypes = config.getString(Props.AUTHENTICATION_TYPES)?.let { + Json.decodeValue(it) } ?: JsonArray() return (authTypes as JsonArray) .map { AuthenticationType.valueOf(it.toString()) } @@ -34,13 +33,13 @@ class SSHAuthData(private val config: JsonObject) { fun getAuthenticationString(): String = when (authenticationType) { - BASIC -> config.getString(CogboardConstants.Props.PASSWORD) - SSH_KEY -> config.getString(CogboardConstants.Props.SSH_KEY) + BASIC -> config.getString(Props.PASSWORD) + SSH_KEY -> config.getString(Props.SSH_KEY) } fun createCommand(): String { - val logLines = config.getInteger(CogboardConstants.Props.LOG_LINES) ?: 0 - val logFilePath = config.getString(CogboardConstants.Props.PATH) ?: "" + val logLines = config.getInteger(Props.LOG_LINES, 0) + val logFilePath = config.getString(Props.PATH, "") return "cat $logFilePath | tail -$logLines" } diff --git a/cogboard-app/src/main/kotlin/com/cognifide/cogboard/ssh/session/strategy/SSHKeyAuthSessionStrategy.kt b/cogboard-app/src/main/kotlin/com/cognifide/cogboard/ssh/session/strategy/SSHKeyAuthSessionStrategy.kt index 07978dae..38133eec 100644 --- a/cogboard-app/src/main/kotlin/com/cognifide/cogboard/ssh/session/strategy/SSHKeyAuthSessionStrategy.kt +++ b/cogboard-app/src/main/kotlin/com/cognifide/cogboard/ssh/session/strategy/SSHKeyAuthSessionStrategy.kt @@ -3,10 +3,11 @@ package com.cognifide.cogboard.ssh.session.strategy import com.cognifide.cogboard.ssh.auth.SSHAuthData import com.jcraft.jsch.JSch import com.jcraft.jsch.Session +import io.netty.util.internal.StringUtil.EMPTY_STRING class SSHKeyAuthSessionStrategy(jSch: JSch, authData: SSHAuthData) : SessionStrategy(jSch, authData) { override fun initSession(): Session { - if (authData.password == "") { + if (authData.password == EMPTY_STRING) { jsch.addIdentity(securityString) } else { jsch.addIdentity(securityString, authData.password)