in src/main/scala/com/gu/ssm/SSH.scala [158:200]
def scpCmdStandard(rawOutput: Boolean)(privateKeyFile: File, instance: Instance, user: String, ipAddress: String, targetInstancePortNumberOpt: Option[Int], useAgent: Option[Boolean], hostsFile: Option[File], sourceFile: String, targetFile: String, profile: Option[String], region: Region, tunnelThroughSystemsManager: Boolean): (InstanceId, Seq[Output]) = {
def isRemote(filepath: String): Boolean = {
filepath.startsWith(":")
}
def exactlyOneArgumentIsRemote(filepath1: String, filepath2: String): Boolean = {
List(filepath1, filepath2).map(isRemote).count(_ == true) == 1
}
val targetPortSpecifications = targetInstancePortNumberOpt match {
case Some(portNumber) => s" -p ${portNumber}"
case _ => ""
}
val hostsFileString = hostsFile.map(file => s""" -o "UserKnownHostsFile $file" -o "StrictHostKeyChecking yes"""").getOrElse("")
val proxyFragment = if(tunnelThroughSystemsManager) { s""" -o "ProxyCommand sh -c \\"aws ssm start-session --target ${instance.id.id} --document-name AWS-StartSSHSession --parameters 'portNumber=22' --region $region ${profile.map("--profile " + _).getOrElse("")}\\""""" } else { "" }
val useAgentFragment = useAgent match {
case None => ""
case Some(decision) => if(decision) " -A" else " -a"
}
// We are using colon to designate the remote file.
// There should be only one.
if (exactlyOneArgumentIsRemote(sourceFile, targetFile)) {
val connectionString =
if (isRemote(sourceFile)) {
s"""scp -o "IdentitiesOnly yes"$useAgentFragment$hostsFileString$proxyFragment${targetPortSpecifications} -i ${privateKeyFile.getCanonicalFile.toString} $user@$ipAddress:${sourceFile.stripPrefix(":")} ${targetFile}"""
}else {
s"""scp -o "IdentitiesOnly yes"$useAgentFragment$hostsFileString$proxyFragment${targetPortSpecifications} -i ${privateKeyFile.getCanonicalFile.toString} ${sourceFile} $user@$ipAddress:${targetFile.stripPrefix(":")}"""
}
val cmd = if(rawOutput) {
Seq(Out(s"$connectionString", newline = false))
}else{
Seq(
Metadata(s"# Dryrun mode. The command below will remain valid for $sshCredentialsLifetimeSeconds seconds:"),
Out(s"$connectionString;")
)
}
(instance.id, cmd)
}else{
(instance.id, Seq(Err("Incorrect remote server specifications, only one file should carry the starting colon")))
}
}