Skip to content

Commit

Permalink
Introduced "secure" boolean property for S3 which controls whether HT…
Browse files Browse the repository at this point in the history
…TPS is used akka#247
  • Loading branch information
thereisnospoon authored and johanandren committed Apr 4, 2017
1 parent 4f91219 commit 254abdc
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 9 deletions.
3 changes: 3 additions & 0 deletions s3/src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ akka.stream.alpakka.s3 {
# hostname of the proxy. If undefined ("") proxy is not enabled.
host = ""
port = 8000

# if "secure" is set to "true" then HTTPS will be used for all requests to S3, otherwise HTTP will be used
secure = true
}

# default values for AWS configuration. If credentials and/or region are not specified when creating S3Client,
Expand Down
9 changes: 5 additions & 4 deletions s3/src/main/scala/akka/stream/alpakka/s3/S3Settings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import akka.actor.ActorSystem
import akka.stream.alpakka.s3.auth.AWSCredentials
import com.typesafe.config.Config

final case class Proxy(host: String, port: Int)
final case class Proxy(host: String, port: Int, scheme: String)

final class S3Settings(val bufferType: BufferType,
val diskBufferPath: String,
Expand Down Expand Up @@ -45,9 +45,10 @@ object S3Settings {
},
diskBufferPath = config.getString("disk-buffer-path"),
proxy = {
if (config.getString("proxy.host") != "")
Some(Proxy(config.getString("proxy.host"), config.getInt("proxy.port")))
else None
if (config.getString("proxy.host") != "") {
val scheme = if (config.getBoolean("proxy.secure")) "https" else "http"
Some(Proxy(config.getString("proxy.host"), config.getInt("proxy.port"), scheme))
} else None
},
awsCredentials = AWSCredentials(config.getString("aws.access-key-id"), config.getString("aws.secret-access-key")),
s3Region = config.getString("aws.default-region"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ private[alpakka] object HttpRequests {

def requestUri(s3Location: S3Location, region: String)(implicit conf: S3Settings): Uri = {
val uri = if (conf.pathStyleAccess) {
Uri(s"/${s3Location.bucket}/${s3Location.key}").withHost(requestHost(s3Location, region)).withScheme("https")
Uri(s"/${s3Location.bucket}/${s3Location.key}").withHost(requestHost(s3Location, region))
} else {
Uri(s"/${s3Location.key}").withHost(requestHost(s3Location, region)).withScheme("https")
Uri(s"/${s3Location.key}").withHost(requestHost(s3Location, region))
}
conf.proxy match {
case None => uri
case Some(proxy) => uri.withPort(proxy.port)
case None => uri.withScheme("https")
case Some(proxy) => uri.withPort(proxy.port).withScheme(proxy.scheme)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import akka.http.scaladsl.model.headers.RawHeader
import akka.http.scaladsl.model.{HttpEntity, MediaTypes}
import akka.stream.alpakka.s3.S3Settings
import akka.stream.alpakka.s3.acl.CannedAcl
import akka.stream.scaladsl.Source
import com.typesafe.config.ConfigFactory
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.{FlatSpec, Matchers}

class HttpRequestsSpec extends FlatSpec with Matchers {
class HttpRequestsSpec extends FlatSpec with Matchers with ScalaFutures {

// test fixtures
val pathStyleAcessConfig =
Expand All @@ -31,10 +33,29 @@ class HttpRequestsSpec extends FlatSpec with Matchers {
|path-style-access = true
""".stripMargin

val proxyConfig =
"""
|buffer = "memory"
|disk-buffer-path = ""
|debug-logging = false
|proxy {
| host = "localhost"
| port = 8080
| secure = false
|}
|aws {
| access-key-id = ""
| secret-access-key = ""
| default-region = "us-east-1"
|}
|path-style-access = false
""".stripMargin

val location = S3Location("bucket", "image-1024@2x")
val contentType = MediaTypes.`image/jpeg`
val acl = CannedAcl.PublicRead
val metaHeaders: Map[String, String] = Map("location" -> "San Francisco", "orientation" -> "portrait")
val multipartUpload = MultipartUpload(S3Location("testBucket", "testKey"), "uploadId")

it should "initiate multipart upload when the region is us-east-1" in {
implicit val settings = S3Settings(ActorSystem())
Expand Down Expand Up @@ -106,4 +127,40 @@ class HttpRequestsSpec extends FlatSpec with Matchers {
req.uri.authority.host.toString shouldEqual "s3-eu-west-1.amazonaws.com"
req.uri.path.toString shouldEqual "/bucket/image-1024@2x"
}

it should "support download requests via HTTP when such scheme configured for `proxy`" in {
implicit val settings = S3Settings(ConfigFactory.parseString(proxyConfig))

val req = HttpRequests.getDownloadRequest(location, "region")

req.uri.scheme shouldEqual "http"
}

it should "support multipart init upload requests via HTTP when such scheme configured for `proxy`" in {
implicit val settings = S3Settings(ConfigFactory.parseString(proxyConfig))

val req =
HttpRequests.initiateMultipartUploadRequest(location, contentType, acl, "region", MetaHeaders(metaHeaders))

req.uri.scheme shouldEqual "http"
}

it should "support multipart upload part requests via HTTP when such scheme configured for `proxy`" in {
implicit val settings = S3Settings(ConfigFactory.parseString(proxyConfig))

val req =
HttpRequests.uploadPartRequest(multipartUpload, 1, Source.empty, 1, "region")

req.uri.scheme shouldEqual "http"
}

it should "support multipart upload complete requests via HTTP when such scheme configured for `proxy`" in {
implicit val settings = S3Settings(ConfigFactory.parseString(proxyConfig))
implicit val executionContext = scala.concurrent.ExecutionContext.global

val reqFuture =
HttpRequests.completeMultipartUploadRequest(multipartUpload, (1, "part") :: Nil, "region")

reqFuture.futureValue.uri.scheme shouldEqual "http"
}
}

0 comments on commit 254abdc

Please sign in to comment.