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

Fix/review 414 #419

Merged
merged 1 commit into from
Nov 14, 2021
Merged
Show file tree
Hide file tree
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
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