Skip to content

Commit

Permalink
Merge pull request #183 from ing-bank/fix/presign
Browse files Browse the repository at this point in the history
 fixes
  • Loading branch information
kr7ysztof authored Jun 1, 2023
2 parents 1572982 + 3b9e0ba commit d88d9c7
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ trait HealthService extends S3Client {
final val healthRoute: Route =
path("ping") {
get {
logger.debug("ping-pong")(RequestId("ping-1"))
complete(StatusCodes.OK -> "pong")
}
} ~ path("pingstorage") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ trait ProxyService {
complete(StatusCodes.ServiceUnavailable -> AwsErrorCodes.response(StatusCodes.ServiceUnavailable))
}

val proxyServiceRoute: Route = {
def proxyServiceRoute: Route = {
implicit val requestId: RequestId = RequestId(UUID.randomUUID().toString)
handleExceptions(rokkuExceptionHandler) {
metricDuration {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.ing.wbaa.rokku.proxy.api.directive

import akka.http.scaladsl.model._
import akka.http.scaladsl.model.headers.{ RawHeader, `Access-Control-Allow-Origin` }
import akka.http.scaladsl.model.headers.{ RawHeader, `Access-Control-Allow-Origin`, `Raw-Request-URI` }
import akka.http.scaladsl.server.{ Directive, Directive0, Directive1 }
import com.ing.wbaa.rokku.proxy.data._
import com.ing.wbaa.rokku.proxy.metrics.MetricsFactory
import com.ing.wbaa.rokku.proxy.provider.aws.SignatureHelpersCommon._
import com.ing.wbaa.rokku.proxy.util.S3Utils
import com.typesafe.scalalogging.LazyLogging
import org.apache.commons.lang.StringUtils

import java.net.InetAddress
import scala.language.postfixOps
Expand Down Expand Up @@ -137,11 +138,13 @@ object ProxyDirectives extends LazyLogging {

if (presignParams.isDefined) {
val authStr = s"${presignParams.get(X_AMZ_ALGORITHM)} Credential=${presignParams.get(X_AMZ_CREDENTIAL)}, SignedHeaders=${presignParams.get(X_AMZ_SIGNED_HEADERS)}, Signature=${presignParams.get(X_AMZ_SIGNATURE)}"
val path = newHeaders.find(_.is(RAW_REQUEST_URI.toLowerCase)).map(r => StringUtils.substringBefore(r.value(), "?")).getOrElse("")
uri = httpRequest.uri.withQuery(Uri.Query.Empty)
newHeaders = newHeaders.filter(_.isNot(RAW_REQUEST_URI.toLowerCase)) ++ List(
RawHeader(AUTHORIZATION_HTTP_HEADER_NAME, authStr),
RawHeader(X_AMZ_DATE, presignParams.get(X_AMZ_DATE)),
RawHeader(X_AMZ_CONTENT_SHA256, "UNSIGNED-PAYLOAD")
RawHeader(X_AMZ_CONTENT_SHA256, "UNSIGNED-PAYLOAD"),
`Raw-Request-URI`(path)
)
}
httpRequest.withHeaders(newHeaders.toList).withUri(uri)
Expand Down Expand Up @@ -189,18 +192,21 @@ object ProxyDirectives extends LazyLogging {
requestCtx =>
val start = System.nanoTime()
val requestMethodName = requestCtx.request.method.value.toLowerCase
val requestContentLength = requestCtx.request.entity.contentLengthOption.getOrElse(0L)
metricsContentLengthCount(requestMethodName, requestContentLength, "out")
val requestContentLengthOut = requestCtx.request.entity.contentLengthOption.getOrElse(0L)
mapResponse { response =>
val took = System.nanoTime() - start
MetricsFactory.markRequestTime(took)
response.status match {
case StatusCodes.InternalServerError => MetricsFactory.countRequest(MetricsFactory.FAILURE_REQUEST)
case StatusCodes.Forbidden => MetricsFactory.countRequest(MetricsFactory.UNAUTHENTICATED_REQUEST)
case _ => MetricsFactory.countRequest(MetricsFactory.SUCCESS_REQUEST)
case StatusCodes.Unauthorized => MetricsFactory.countRequest(MetricsFactory.UNAUTHORIZED_REQUEST)
case StatusCodes.OK | StatusCodes.PartialContent =>
MetricsFactory.countRequest(MetricsFactory.SUCCESS_REQUEST)
metricsContentLengthCount(requestMethodName, requestContentLengthOut, "out")
val responseContentLengthIn = response.entity.contentLengthOption.getOrElse(0L)
metricsContentLengthCount(requestMethodName, responseContentLengthIn, "in")
case status => logger.warn("no count status in metrics {}", status)
}
val responseContentLength = response.entity.contentLengthOption.getOrElse(0L)
metricsContentLengthCount(requestMethodName, responseContentLength, "in")
response
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ object MetricsFactory {
val SUCCESS_REQUEST = "requests.status.success.total"
val FAILURE_REQUEST = "requests.status.failure.total"
val UNAUTHENTICATED_REQUEST = "requests.status.unauthenticated.total"
val UNAUTHORIZED_REQUEST = "requests.status.unauthorized.total"
val REQUEST_TIME = "requests.nanoseconds.total"
val REQUEST_TIME_HIST = "requests.time.histogram"
val HTTP_METHOD = "{httpMethod}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ import com.amazonaws.regions.Regions
import com.amazonaws.services.s3.model.{ ListObjectsV2Request, ListObjectsV2Result }
import com.amazonaws.services.s3.{ AmazonS3, AmazonS3ClientBuilder }
import com.ing.wbaa.rokku.proxy.config.StorageS3Settings
import com.ing.wbaa.rokku.proxy.data.RequestId
import com.ing.wbaa.rokku.proxy.handler.LoggerHandlerWithId
import com.ing.wbaa.rokku.proxy.metrics.MetricsFactory

import scala.concurrent.ExecutionContext

trait S3Client {

private val logger = new LoggerHandlerWithId
protected[this] implicit def executionContext: ExecutionContext

protected[this] def storageS3Settings: StorageS3Settings
Expand Down Expand Up @@ -59,6 +63,7 @@ trait S3Client {
val start = System.nanoTime()
val result = listBucket(storageS3Settings.bucketName)
val took = System.nanoTime() - start
logger.info("list bucket {} took={}", storageS3Settings.bucketName, took)(RequestId("listBucket-1"))
MetricsFactory.markRequestStorageTime(took)
result
}
Expand Down

0 comments on commit d88d9c7

Please sign in to comment.