Skip to content
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

Add support for AWS Elasticsearch Service by signing request properly. #310

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions app/elastic/AwsSigner.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package elastic

import java.net.URI

import com.amazonaws.DefaultRequest
import com.amazonaws.auth.AWS4Signer
import com.amazonaws.auth.AWSCredentials
import com.amazonaws.auth.BasicAWSCredentials
import com.amazonaws.http.HttpMethodName
import scala.collection.JavaConverters._
import java.io.StringReader
import java.io.ByteArrayInputStream
import java.io.InputStream

object AwsSigner {

def sing(method: String, url: String, headers: Seq[(String, String)], body: Option[String], secret: String, key: String) : Seq[(String, String)] = {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this method should be: sign and not sing, right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. Should I fix it and do another PR?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, I don't know, because this PR is open for so long. Did you build a docker image from your branch? if so, could share it with us, meanwhile they don't merge your PR.


val uri = URI.create(url)

if(uri.getHost.endsWith(".es.amazonaws.com")) {

val credentials: AWSCredentials = new BasicAWSCredentials(secret, key)


val host = if(uri.getPort() == -1) {
s"""${uri.getScheme}://${uri.getHost()}"""
} else {
s"""${uri.getScheme}://${uri.getHost()}:${uri.getPort()}"""
}

val pattern = """^https:\/\/.+\.(.+)\.es\.amazonaws\.com$""".r

val region = host match {
case pattern(region) => region
}

val endpoint = uri.getPath();
val queryStringOpt = Option(uri.getQuery());

val params = queryStringOpt
.map( queryString => {
queryString
.split("&")
.map( text => {
val keyVal = text.split("=", 2)
keyVal(0) -> Seq(keyVal(1)).asJava
})
.toMap
})
.getOrElse(Map.empty)
.asJava

val content: InputStream = new ByteArrayInputStream(body.getOrElse("").getBytes)

val request = new DefaultRequest[Unit]("es")
request.setHttpMethod(HttpMethodName.fromValue(method))
request.setEndpoint(URI.create(host))
request.setResourcePath(endpoint)
request.setHeaders(headers.toMap[String, String].asJava)
request.setParameters(params)
request.setContent(content)

val signer = new AWS4Signer()
signer.setRegionName(region);
signer.setServiceName("es");
signer.sign(request, credentials);

val hdrs = request.getHeaders();

Seq(
"Host" -> hdrs.get("Host"),
"Authorization" -> hdrs.get("Authorization"),
"X-Amz-Date" -> hdrs.get("X-Amz-Date")
)

} else {
Seq.empty
}

}


}
3 changes: 2 additions & 1 deletion app/elastic/HTTPElasticClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,8 @@ class HTTPElasticClient @Inject()(client: WSClient) extends ElasticClient {
val request =
authentication.foldLeft(client.url(url).withMethod(method).withHttpHeaders(headers: _*)) {
case (request, auth) =>
request.withAuth(auth.username, auth.password, WSAuthScheme.BASIC)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we keep basic auth? and possibly add aws so config would look like:

  {
    host = "https://some-aws-es-domain"
    name = "AWS ES Cluster"
    aws = {
      access_key = "access"
      secrety_key = "secret"
    }
  }

BTW, its working as expected :) but cannot be mixed with basic auth

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree here. Specially if the AWS credentials are gonna be provided through an instance profile

val awsHeaders = AwsSigner.sing(method, url, headers, body, auth.username, auth.password)
request.addHttpHeaders(awsHeaders: _*)
}

body.fold(request)(request.withBody((_))).execute.map { response =>
Expand Down
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ libraryDependencies ++= Seq(
"com.typesafe.play" %% "play-slick" % "3.0.3",
"com.typesafe.play" %% "play-slick-evolutions" % "3.0.3",
"org.xerial" % "sqlite-jdbc" % "3.23.1",
"com.amazonaws" % "aws-java-sdk-core" % "1.11.359",
"org.specs2" %% "specs2-junit" % "3.9.2" % "test",
"org.specs2" %% "specs2-core" % "3.9.2" % "test",
"org.specs2" %% "specs2-mock" % "3.9.2" % "test"
Expand Down