diff --git a/.gitignore b/.gitignore index 62dfe10..168f44c 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,4 @@ project/boot project/build/target project/plugins/lib_managed project/plugins/src_managed -project/plugins/target \ No newline at end of file +project/plugins/target diff --git a/src/main/scala/com/decodified/scalassh/Command.scala b/src/main/scala/com/decodified/scalassh/Command.scala index 031046e..fc1a9b4 100644 --- a/src/main/scala/com/decodified/scalassh/Command.scala +++ b/src/main/scala/com/decodified/scalassh/Command.scala @@ -18,11 +18,14 @@ package com.decodified.scalassh import java.io.{ByteArrayInputStream, File, FileInputStream, InputStream} import net.schmizz.sshj.connection.channel.direct.Session - -final case class Command(command: String, input: CommandInput = CommandInput.NoInput, timeout: Option[Int] = None) +final case class Command(command: String, + input: CommandInput = CommandInput.NoInput, + timeout: Option[Int] = None, + safeToLog: Boolean = true) object Command { - implicit def fromString(cmd: String): Command = Command(cmd) + def apply(cmd: String, safeToLog: Boolean): Command = new Command(cmd, safeToLog = safeToLog) + def apply(cmd: String): Command = new Command(cmd, safeToLog = true) } final case class CommandInput(inputStream: Option[InputStream]) diff --git a/src/main/scala/com/decodified/scalassh/SshClient.scala b/src/main/scala/com/decodified/scalassh/SshClient.scala index 25fb4f6..812de27 100644 --- a/src/main/scala/com/decodified/scalassh/SshClient.scala +++ b/src/main/scala/com/decodified/scalassh/SshClient.scala @@ -26,7 +26,6 @@ import net.schmizz.sshj.SSHClient import net.schmizz.sshj.connection.channel.direct.Session import net.schmizz.sshj.userauth.keyprovider.KeyProvider import net.schmizz.sshj.userauth.method.AuthMethod - import scala.io.Source import scala.util.{Failure, Success, Try} import scala.util.control.NonFatal @@ -36,7 +35,7 @@ final class SshClient(val config: HostConfig) extends ScpTransferable { lazy val endpoint = config.hostName + ':' + config.port lazy val authenticatedClient = connect(client).flatMap(authenticate) val client = createClient(config) - + val unsafeLogMsg = "Sensitive data, not logged" def exec(command: Command): Try[CommandResult] = authenticatedClient.flatMap { client => startSession(client).flatMap { session => @@ -55,7 +54,9 @@ final class SshClient(val config: HostConfig) extends ScpTransferable { } def execWithSession(command: Command, session: Session): Try[CommandResult] = { - log.info("Executing SSH command on {}: \"{}\"", Seq(endpoint, command.command): _*) + log.info( + "Executing SSH command on {}: \"{}\"", + Seq(endpoint, if (command.safeToLog) command.command else unsafeLogMsg): _*) protect("Could not execute SSH command on") { val channel = session.exec(command.command) command.input.inputStream.foreach(new StreamCopier().copy(_, channel.getOutputStream)) @@ -155,7 +156,10 @@ final class SshClient(val config: HostConfig) extends ScpTransferable { protected def protect[T](errorMsg: => String)(f: => T): Try[T] = try Success(f) - catch { case NonFatal(e) => Failure(SSH.Error(errorMsg + " " + endpoint, e)) } + catch { + case NonFatal(e) => + Failure(SSH.Error(s"$errorMsg executing on $endpoint - message: ${e.getMessage}", e)) + } } object SshClient {