-
Notifications
You must be signed in to change notification settings - Fork 716
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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)] = { | ||
|
||
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 | ||
} | ||
|
||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could we keep basic
BTW, its working as expected :) but cannot be mixed with basic auth There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 => | ||
|
There was a problem hiding this comment.
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 notsing
, right?There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.