Skip to content

Commit

Permalink
Merge pull request #419 from wttech/fix/review-414
Browse files Browse the repository at this point in the history
Fix/review 414
  • Loading branch information
janeksmielowski authored Nov 14, 2021
2 parents 6d49c96 + d6a616f commit b3bd8b4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
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
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()) }
Expand All @@ -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"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b3bd8b4

Please sign in to comment.