Skip to content

Commit

Permalink
324 - fmt (#355)
Browse files Browse the repository at this point in the history
* 354 - Missing option for Implicit FTP over TLS
  • Loading branch information
regis-leray authored Aug 7, 2023
1 parent d62295e commit 157b1f7
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 47 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ jobs:
strategy:
fail-fast: false
matrix:
java: ['adopt@1.8']
scala: ['2.11.12', '2.12.16', '2.13.8', '3.2.2']
java: ['amazon-corretto@1.17']
scala: ['2.12.18', '2.13.11', '3.3.0']
steps:
- name: Checkout current branch
uses: actions/checkout@v3.5.1
Expand Down
6 changes: 3 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ addCommandAlias("fmt", "all scalafmtSbt scalafmt test:scalafmt")
addCommandAlias("check", "all scalafmtSbtCheck scalafmtCheck test:scalafmtCheck")
addCommandAlias("fix", "; all compile:scalafix test:scalafix; all scalafmtSbt scalafmtAll")

val zioVersion = "2.0.0"
val zioVersion = "2.0.15"

lazy val root =
project.in(file(".")).settings(publish / skip := true).aggregate(`zio-ftp`, docs)
Expand All @@ -39,9 +39,9 @@ lazy val `zio-ftp` = project
libraryDependencies ++= Seq(
"dev.zio" %% "zio" % zioVersion,
"dev.zio" %% "zio-streams" % zioVersion,
("dev.zio" %% "zio-nio" % "2.0.0").exclude("org.scala-lang.modules", "scala-collection-compat_2.13"),
("dev.zio" %% "zio-nio" % "2.0.1").exclude("org.scala-lang.modules", "scala-collection-compat_2.13"),
"com.hierynomus" % "sshj" % "0.35.0",
"commons-net" % "commons-net" % "3.8.0",
"commons-net" % "commons-net" % "3.9.0",
"org.scala-lang.modules" %% "scala-collection-compat" % "2.8.1",
"org.apache.logging.log4j" % "log4j-api" % "2.13.1" % Test,
"org.apache.logging.log4j" % "log4j-core" % "2.13.1" % Test,
Expand Down
24 changes: 4 additions & 20 deletions project/BuildHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ object BuildHelper {
def stdSettings(prjName: String) =
Seq(
name := s"$prjName",
crossScalaVersions := Seq(Scala211, Scala212, Scala213, Scala3),
crossScalaVersions := Seq(Scala212, Scala213, Scala3),
ThisBuild / scalaVersion := Scala213,
scalacOptions := stdOptions ++ extraOptions(scalaVersion.value),
incOptions ~= (_.withLogRecompileOnMacro(false))
)

final val Scala211 = "2.11.12"
final val Scala212 = "2.12.16"
final val Scala213 = "2.13.8"
final val Scala3: String = "3.2.2"
final val Scala212 = "2.12.18"
final val Scala213 = "2.13.11"
final val Scala3 = "3.3.0"

final private val stdOptions = Seq(
"-deprecation",
Expand Down Expand Up @@ -69,21 +68,6 @@ object BuildHelper {
"-Ywarn-nullary-unit",
"-Ywarn-unused-import"
) ++ std2xOptions
case Some((2, 11)) =>
Seq(
"-Ypartial-unification",
"-Yno-adapted-args",
"-Ywarn-inaccessible",
"-Ywarn-infer-any",
"-Ywarn-nullary-override",
"-Ywarn-nullary-unit",
"-Xexperimental",
"-Ywarn-unused-import",
"-Xfuture",
"-Xsource:2.13",
"-Xmax-classfile-name",
"242"
) ++ std2xOptions
case _ => Seq.empty
}
}
31 changes: 28 additions & 3 deletions zio-ftp/src/main/scala/zio/ftp/FtpSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,31 @@ object KeyFileSftpIdentity {
KeyFileSftpIdentity(privateKey, None)
}

sealed abstract class ProtectionLevel(val s: String)

object ProtectionLevel {
case object Clear extends ProtectionLevel("C")
case object Private extends ProtectionLevel("P")
//S - Safe(SSL protocol only)
case object Safe extends ProtectionLevel("S")
//E - Confidential(SSL protocol only)
case object Confidential extends ProtectionLevel("E")
}

/**
* @param isImplicit // security mode (Implicit/Explicit)
* @param pbzs // Protection Buffer SiZe default value 0
* @param prot // data channel PROTection level default value C
*
* More informations regarding these settings
* https://enterprisedt.com/products/edtftpjssl/doc/manual/html/ftpscommands.html
*/
final case class SslParams(isImplicit: Boolean, pbzs: Long, prot: ProtectionLevel)

object SslParams {
val default: SslParams = SslParams(false, 0L, ProtectionLevel.Clear)
}

/**
* Settings to connect unsecure Ftp server
*
Expand All @@ -144,16 +169,16 @@ final case class UnsecureFtpSettings(
binary: Boolean,
passiveMode: Boolean,
proxy: Option[Proxy],
secure: Boolean,
sslParams: Option[SslParams] = None,
dataTimeout: Option[Duration] = None,
controlEncoding: Option[String] = None
)

object UnsecureFtpSettings {

def apply(host: String, port: Int, creds: FtpCredentials): UnsecureFtpSettings =
new UnsecureFtpSettings(host, port, creds, true, true, None, false)
new UnsecureFtpSettings(host, port, creds, true, true, None, None)

def secure(host: String, port: Int, creds: FtpCredentials): UnsecureFtpSettings =
new UnsecureFtpSettings(host, port, creds, true, true, None, true)
new UnsecureFtpSettings(host, port, creds, true, true, None, Some(SslParams.default))
}
14 changes: 12 additions & 2 deletions zio-ftp/src/main/scala/zio/ftp/UnsecureFtp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ object UnsecureFtp {
def connect(settings: UnsecureFtpSettings): ZIO[Scope, ConnectionError, FtpAccessors[Client]] =
acquireRelease(
attemptBlockingIO {
val ftpClient = if (settings.secure) new JFTPSClient() else new JFTPClient()
val ftpClient = settings.sslParams.fold(new JFTPClient()) { ssl =>
new JFTPSClient(ssl.isImplicit)
}

settings.controlEncoding match {
case Some(enc) => ftpClient.setControlEncoding(enc)
Expand All @@ -123,7 +125,15 @@ object UnsecureFtp {
if (settings.passiveMode)
ftpClient.enterLocalPassiveMode()

settings.dataTimeout.map(_.toMillis.toInt).foreach(ftpClient.setDataTimeout)
//https://enterprisedt.com/products/edtftpjssl/doc/manual/html/ftpscommands.html
(ftpClient, settings.sslParams) match {
case (c: JFTPSClient, Some(ssl)) =>
c.execPBSZ(ssl.pbzs)
c.execPROT(ssl.prot.s)
case _ => ()
}

settings.dataTimeout.foreach(ftpClient.setDataTimeout)

new UnsecureFtp(ftpClient) -> success
}.mapError(e => ConnectionError(e.getMessage, e))
Expand Down
6 changes: 2 additions & 4 deletions zio-ftp/src/test/scala/zio/ftp/SecureFtpSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ object SecureFtpSpec extends ZIOSpecDefault {

val home = Paths.get("ftp-home/sftp/home/foo")

val sftp = Scope.default >+> secure(settings).mapError(TestFailure.die(_))

override def spec =
override def spec: Spec[TestEnvironment & Scope, Any] =
suite("SecureFtpSpec")(
test("invalid credentials")(
for {
Expand Down Expand Up @@ -215,5 +213,5 @@ object SecureFtpSpec extends ZIOSpecDefault {
invalid <- rename("/dont-exist", "dont-exist-destination").flip.map(_.getMessage)
} yield assertTrue(invalid == "No such file")
}
).provideCustomLayerShared(sftp)
).provideSomeLayerShared[Scope](secure(settings))
}
13 changes: 5 additions & 8 deletions zio-ftp/src/test/scala/zio/ftp/StubFtpSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ import zio.stream.ZPipeline.utf8Decode
import zio.stream.ZStream
import zio.test.Assertion._
import zio.test._
import zio.{ Chunk, Scope, ZIO }
import zio.{ Chunk, Scope }

import scala.io.Source

object StubFtpSpec extends ZIOSpecDefault {
val home = ZPath("ftp-home/stub/home")

val stubFtp = Scope.default >+> stub(home).mapError(TestFailure.fail)

override def spec =
suite("StubFtpSpec")(
test("ls")(
Expand Down Expand Up @@ -124,10 +122,9 @@ object StubFtpSpec extends ZIOSpecDefault {
(for {
_ <- upload("/hello-world.txt", data)
result <-
ZIO.scoped(
acquireRelease(attemptBlockingIO(Source.fromFile(path.toFile)))(b => attemptBlockingIO(b.close()).ignore)
.map(_.mkString)
)
acquireRelease(attemptBlockingIO(Source.fromFile(path.toFile)))(b => attemptBlockingIO(b.close()).ignore)
.map(_.mkString)

} yield assert(result)(equalTo("Hello F World"))) <* Files.delete(path)
},
test("upload fail when path is invalid") {
Expand Down Expand Up @@ -155,5 +152,5 @@ object StubFtpSpec extends ZIOSpecDefault {
failure <- rename("/dont-exist.txt", "/dont-exist-destination.txt").flip.map(_.getMessage)
} yield assertTrue(failure == "Path is invalid. Cannot rename /dont-exist.txt to /dont-exist-destination.txt")
}
).provideCustomLayerShared(stubFtp)
).provideSomeLayerShared[Scope](stub(home))
}
7 changes: 2 additions & 5 deletions zio-ftp/src/test/scala/zio/ftp/UnsecureFtpSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,15 @@ import scala.io.Source
object UnsecureSslFtpSpec extends ZIOSpecDefault {
val settings = UnsecureFtpSettings.secure("127.0.0.1", 2121, FtpCredentials("username", "userpass"))

val ftp = Scope.default >+> unsecure(settings).mapError(TestFailure.die(_))

override def spec =
FtpSuite.spec("UnsecureSslFtpSpec", settings).provideCustomLayer(ftp) @@ sequential
FtpSuite.spec("UnsecureSslFtpSpec", settings).provideSomeLayer[Scope](unsecure(settings)) @@ sequential
}

object UnsecureFtpSpec extends ZIOSpecDefault {
val settings = UnsecureFtpSettings("127.0.0.1", port = 2121, FtpCredentials("username", "userpass"))
val ftp = Scope.default >+> unsecure(settings).mapError(TestFailure.die(_))

override def spec =
FtpSuite.spec("UnsecureFtpSpec", settings).provideCustomLayer(ftp) @@ sequential
FtpSuite.spec("UnsecureFtpSpec", settings).provideSomeLayer[Scope](unsecure(settings)) @@ sequential
}

object FtpSuite {
Expand Down

0 comments on commit 157b1f7

Please sign in to comment.