-
Notifications
You must be signed in to change notification settings - Fork 50
Safe logging #47
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
base: master
Are you sure you want to change the base?
Safe logging #47
Changes from 6 commits
1af2dad
7d920e4
9de1370
e3bb812
917426c
65481b7
1e7374d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,11 +18,26 @@ 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) | ||
| trait Command { | ||
|
||
| val command: String | ||
|
||
| val input: CommandInput = CommandInput.NoInput | ||
| val timeout: Option[Int] = None | ||
| val safe: Boolean | ||
|
||
| } | ||
| case class SafeCommand(command: String, | ||
| override val input: CommandInput = CommandInput.NoInput, | ||
| override val timeout: Option[Int] = None, | ||
| safe: Boolean = true) | ||
| extends Command | ||
| case class UnsafeCommand(command: String, | ||
|
||
| override val input: CommandInput = CommandInput.NoInput, | ||
| override val timeout: Option[Int] = None, | ||
| safe: Boolean = false) | ||
| extends Command | ||
|
|
||
| object Command { | ||
| implicit def fromString(cmd: String): Command = Command(cmd) | ||
| def apply(cmd: String, safe: Boolean): Command = if (safe) SafeCommand(cmd) else UnsafeCommand(cmd) | ||
| def apply(cmd: String): Command = SafeCommand(cmd) | ||
| } | ||
|
|
||
| final case class CommandInput(inputStream: Option[InputStream]) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -53,9 +52,10 @@ final class SshClient(val config: HostConfig) extends ScpTransferable { | |
| execWithSession(command, session) | ||
| } | ||
| } | ||
| def logCmd(command: Command): String = if (command.safe) command.command else "Sensitive data, not logged" | ||
|
||
|
|
||
| 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, logCmd(command)): _*) | ||
| protect("Could not execute SSH command on") { | ||
| val channel = session.exec(command.command) | ||
| command.input.inputStream.foreach(new StreamCopier().copy(_, channel.getOutputStream)) | ||
|
|
@@ -155,7 +155,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 { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should rather go into your dev machine's global .gitignore, not this project-level file.
(The comment at the top of this file has more details...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed.