Skip to content

Commit eb293d8

Browse files
authored
[RORDEV-1813] some exceptions should be handled by ES, not ROR (#1178)
1 parent 227ceed commit eb293d8

File tree

38 files changed

+530
-689
lines changed

38 files changed

+530
-689
lines changed

es67x/src/main/scala/tech/beshu/ror/es/utils/ChannelInterceptingRestHandlerDecorator.scala

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package tech.beshu.ror.es.utils
1818

19+
import cats.Show
1920
import cats.implicits.*
2021
import org.apache.logging.log4j.scala.Logging
2122
import org.elasticsearch.ElasticsearchException
@@ -31,7 +32,7 @@ import tech.beshu.ror.es.utils.ThreadContextOps.createThreadContextOps
3132
import tech.beshu.ror.implicits.*
3233
import tech.beshu.ror.utils.AccessControllerHelper.doPrivileged
3334

34-
import scala.util.{Failure, Success, Try}
35+
import scala.util.Try
3536

3637
class ChannelInterceptingRestHandlerDecorator private(val underlying: RestHandler,
3738
settings: Settings)
@@ -42,23 +43,16 @@ class ChannelInterceptingRestHandlerDecorator private(val underlying: RestHandle
4243
}
4344

4445
override def handleRequest(request: RestRequest, channel: RestChannel, client: NodeClient): Unit = {
45-
Try {
46-
RorRestChannel.from(channel) match {
47-
case Right(rorRestChannel) =>
48-
ThreadRepo.safeSetRestChannel(rorRestChannel) {
49-
addXpackUserAuthenticationHeaderForInCaseOfSecurityRequest(request, client)
50-
wrapped.handleRequest(request, rorRestChannel, client)
51-
}
52-
case Left(error) =>
53-
logError(error)
54-
implicit val show = authorizationValueErrorSanitizedShow
55-
channel.sendResponse(new BytesRestResponse(channel, RestStatus.BAD_REQUEST, new ElasticsearchException(error.show)))
56-
}
57-
} match {
58-
case Success(_) =>
59-
case Failure(ex) =>
60-
logger.error(s"The incoming request handling error:", ex)
61-
channel.sendResponse(new BytesRestResponse(channel, RestStatus.INTERNAL_SERVER_ERROR, new ElasticsearchException("ROR internal error")))
46+
RorRestChannel.from(channel) match {
47+
case Right(rorRestChannel) =>
48+
ThreadRepo.safeSetRestChannel(rorRestChannel) {
49+
addXpackUserAuthenticationHeaderForInCaseOfSecurityRequest(request, client)
50+
wrapped.handleRequest(request, rorRestChannel, client)
51+
}
52+
case Left(error) =>
53+
logError(error)
54+
implicit val show: Show[AuthorizationValueError] = authorizationValueErrorSanitizedShow
55+
channel.sendResponse(new BytesRestResponse(channel, RestStatus.BAD_REQUEST, new ElasticsearchException(error.show)))
6256
}
6357
}
6458

@@ -99,11 +93,11 @@ class ChannelInterceptingRestHandlerDecorator private(val underlying: RestHandle
9993

10094
private def logError(error: AuthorizationValueError): Unit = {
10195
{
102-
implicit val show = authorizationValueErrorSanitizedShow
96+
implicit val show: Show[AuthorizationValueError] = authorizationValueErrorSanitizedShow
10397
logger.warn(s"The incoming request was malformed. Cause: ${error.show}")
10498
}
10599
if (logger.delegate.isDebugEnabled()) {
106-
implicit val show = authorizationValueErrorWithDetailsShow
100+
implicit val show: Show[AuthorizationValueError] = authorizationValueErrorWithDetailsShow
107101
logger.debug(s"Malformed request detailed cause: ${error.show}")
108102
}
109103
}

es70x/src/main/scala/tech/beshu/ror/es/utils/ChannelInterceptingRestHandlerDecorator.scala

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package tech.beshu.ror.es.utils
1818

19+
import cats.Show
1920
import cats.implicits.*
2021
import org.apache.logging.log4j.scala.Logging
2122
import org.elasticsearch.ElasticsearchException
@@ -31,7 +32,7 @@ import tech.beshu.ror.es.utils.ThreadContextOps.createThreadContextOps
3132
import tech.beshu.ror.implicits.*
3233
import tech.beshu.ror.utils.AccessControllerHelper.doPrivileged
3334

34-
import scala.util.{Failure, Success, Try}
35+
import scala.util.Try
3536

3637
class ChannelInterceptingRestHandlerDecorator private(val underlying: RestHandler,
3738
settings: Settings)
@@ -42,23 +43,16 @@ class ChannelInterceptingRestHandlerDecorator private(val underlying: RestHandle
4243
}
4344

4445
override def handleRequest(request: RestRequest, channel: RestChannel, client: NodeClient): Unit = {
45-
Try {
46-
RorRestChannel.from(channel) match {
47-
case Right(rorRestChannel) =>
48-
ThreadRepo.safeSetRestChannel(rorRestChannel) {
49-
addXpackUserAuthenticationHeaderForInCaseOfSecurityRequest(request, client)
50-
wrapped.handleRequest(request, rorRestChannel, client)
51-
}
52-
case Left(error) =>
53-
logError(error)
54-
implicit val show = authorizationValueErrorSanitizedShow
55-
channel.sendResponse(new BytesRestResponse(channel, RestStatus.BAD_REQUEST, new ElasticsearchException(error.show)))
56-
}
57-
} match {
58-
case Success(_) =>
59-
case Failure(ex) =>
60-
logger.error(s"The incoming request handling error:", ex)
61-
channel.sendResponse(new BytesRestResponse(channel, RestStatus.INTERNAL_SERVER_ERROR, new ElasticsearchException("ROR internal error")))
46+
RorRestChannel.from(channel) match {
47+
case Right(rorRestChannel) =>
48+
ThreadRepo.safeSetRestChannel(rorRestChannel) {
49+
addXpackUserAuthenticationHeaderForInCaseOfSecurityRequest(request, client)
50+
wrapped.handleRequest(request, rorRestChannel, client)
51+
}
52+
case Left(error) =>
53+
logError(error)
54+
implicit val show: Show[AuthorizationValueError] = authorizationValueErrorSanitizedShow
55+
channel.sendResponse(new BytesRestResponse(channel, RestStatus.BAD_REQUEST, new ElasticsearchException(error.show)))
6256
}
6357
}
6458

@@ -99,11 +93,11 @@ class ChannelInterceptingRestHandlerDecorator private(val underlying: RestHandle
9993

10094
private def logError(error: AuthorizationValueError): Unit = {
10195
{
102-
implicit val show = authorizationValueErrorSanitizedShow
96+
implicit val show: Show[AuthorizationValueError] = authorizationValueErrorSanitizedShow
10397
logger.warn(s"The incoming request was malformed. Cause: ${error.show}")
10498
}
10599
if (logger.delegate.isDebugEnabled()) {
106-
implicit val show = authorizationValueErrorWithDetailsShow
100+
implicit val show: Show[AuthorizationValueError] = authorizationValueErrorWithDetailsShow
107101
logger.debug(s"Malformed request detailed cause: ${error.show}")
108102
}
109103
}

es710x/src/main/scala/tech/beshu/ror/es/utils/ChannelInterceptingRestHandlerDecorator.scala

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package tech.beshu.ror.es.utils
1818

19+
import cats.Show
1920
import cats.implicits.*
2021
import org.apache.logging.log4j.scala.Logging
2122
import org.elasticsearch.ElasticsearchException
@@ -31,7 +32,7 @@ import tech.beshu.ror.implicits.*
3132
import tech.beshu.ror.utils.AccessControllerHelper.doPrivileged
3233

3334
import java.util
34-
import scala.util.{Failure, Success, Try}
35+
import scala.util.Try
3536

3637
class ChannelInterceptingRestHandlerDecorator private(val underlying: RestHandler)
3738
extends RestHandler with Logging {
@@ -41,23 +42,16 @@ class ChannelInterceptingRestHandlerDecorator private(val underlying: RestHandle
4142
}
4243

4344
override def handleRequest(request: RestRequest, channel: RestChannel, client: NodeClient): Unit = {
44-
Try {
45-
RorRestChannel.from(channel) match {
46-
case Right(rorRestChannel) =>
47-
ThreadRepo.safeSetRestChannel(rorRestChannel) {
48-
addXpackUserAuthenticationHeaderForInCaseOfSecurityRequest(request, client)
49-
wrapped.handleRequest(request, rorRestChannel, client)
50-
}
51-
case Left(error) =>
52-
logError(error)
53-
implicit val show = authorizationValueErrorSanitizedShow
54-
channel.sendResponse(new BytesRestResponse(channel, RestStatus.BAD_REQUEST, new ElasticsearchException(error.show)))
55-
}
56-
} match {
57-
case Success(_) =>
58-
case Failure(ex) =>
59-
logger.error(s"The incoming request handling error:", ex)
60-
channel.sendResponse(new BytesRestResponse(channel, RestStatus.INTERNAL_SERVER_ERROR, new ElasticsearchException("ROR internal error")))
45+
RorRestChannel.from(channel) match {
46+
case Right(rorRestChannel) =>
47+
ThreadRepo.safeSetRestChannel(rorRestChannel) {
48+
addXpackUserAuthenticationHeaderForInCaseOfSecurityRequest(request, client)
49+
wrapped.handleRequest(request, rorRestChannel, client)
50+
}
51+
case Left(error) =>
52+
logError(error)
53+
implicit val show: Show[AuthorizationValueError] = authorizationValueErrorSanitizedShow
54+
channel.sendResponse(new BytesRestResponse(channel, RestStatus.BAD_REQUEST, new ElasticsearchException(error.show)))
6155
}
6256
}
6357

@@ -108,11 +102,11 @@ class ChannelInterceptingRestHandlerDecorator private(val underlying: RestHandle
108102

109103
private def logError(error: AuthorizationValueError): Unit = {
110104
{
111-
implicit val show = authorizationValueErrorSanitizedShow
105+
implicit val show: Show[AuthorizationValueError] = authorizationValueErrorSanitizedShow
112106
logger.warn(s"The incoming request was malformed. Cause: ${error.show}")
113107
}
114108
if (logger.delegate.isDebugEnabled()) {
115-
implicit val show = authorizationValueErrorWithDetailsShow
109+
implicit val show: Show[AuthorizationValueError] = authorizationValueErrorWithDetailsShow
116110
logger.debug(s"Malformed request detailed cause: ${error.show}")
117111
}
118112
}

es711x/src/main/scala/tech/beshu/ror/es/utils/ChannelInterceptingRestHandlerDecorator.scala

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package tech.beshu.ror.es.utils
1818

19+
import cats.Show
1920
import cats.implicits.*
2021
import org.apache.logging.log4j.scala.Logging
2122
import org.elasticsearch.ElasticsearchException
@@ -31,7 +32,7 @@ import tech.beshu.ror.implicits.*
3132
import tech.beshu.ror.utils.AccessControllerHelper.doPrivileged
3233

3334
import java.util
34-
import scala.util.{Failure, Success, Try}
35+
import scala.util.Try
3536

3637
class ChannelInterceptingRestHandlerDecorator private(val underlying: RestHandler)
3738
extends RestHandler with Logging {
@@ -41,23 +42,16 @@ class ChannelInterceptingRestHandlerDecorator private(val underlying: RestHandle
4142
}
4243

4344
override def handleRequest(request: RestRequest, channel: RestChannel, client: NodeClient): Unit = {
44-
Try {
45-
RorRestChannel.from(channel) match {
46-
case Right(rorRestChannel) =>
47-
ThreadRepo.safeSetRestChannel(rorRestChannel) {
48-
addXpackUserAuthenticationHeaderForInCaseOfSecurityRequest(request, client)
49-
wrapped.handleRequest(request, rorRestChannel, client)
50-
}
51-
case Left(error) =>
52-
logError(error)
53-
implicit val show = authorizationValueErrorSanitizedShow
54-
channel.sendResponse(new BytesRestResponse(channel, RestStatus.BAD_REQUEST, new ElasticsearchException(error.show)))
55-
}
56-
} match {
57-
case Success(_) =>
58-
case Failure(ex) =>
59-
logger.error(s"The incoming request handling error:", ex)
60-
channel.sendResponse(new BytesRestResponse(channel, RestStatus.INTERNAL_SERVER_ERROR, new ElasticsearchException("ROR internal error")))
45+
RorRestChannel.from(channel) match {
46+
case Right(rorRestChannel) =>
47+
ThreadRepo.safeSetRestChannel(rorRestChannel) {
48+
addXpackUserAuthenticationHeaderForInCaseOfSecurityRequest(request, client)
49+
wrapped.handleRequest(request, rorRestChannel, client)
50+
}
51+
case Left(error) =>
52+
logError(error)
53+
implicit val show: Show[AuthorizationValueError] = authorizationValueErrorSanitizedShow
54+
channel.sendResponse(new BytesRestResponse(channel, RestStatus.BAD_REQUEST, new ElasticsearchException(error.show)))
6155
}
6256
}
6357

@@ -104,11 +98,11 @@ class ChannelInterceptingRestHandlerDecorator private(val underlying: RestHandle
10498

10599
private def logError(error: AuthorizationValueError): Unit = {
106100
{
107-
implicit val show = authorizationValueErrorSanitizedShow
101+
implicit val show: Show[AuthorizationValueError] = authorizationValueErrorSanitizedShow
108102
logger.warn(s"The incoming request was malformed. Cause: ${error.show}")
109103
}
110104
if (logger.delegate.isDebugEnabled()) {
111-
implicit val show = authorizationValueErrorWithDetailsShow
105+
implicit val show: Show[AuthorizationValueError] = authorizationValueErrorWithDetailsShow
112106
logger.debug(s"Malformed request detailed cause: ${error.show}")
113107
}
114108
}

es714x/src/main/scala/tech/beshu/ror/es/utils/ChannelInterceptingRestHandlerDecorator.scala

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package tech.beshu.ror.es.utils
1818

19+
import cats.Show
1920
import cats.implicits.*
2021
import org.apache.logging.log4j.scala.Logging
2122
import org.elasticsearch.ElasticsearchException
@@ -31,7 +32,7 @@ import tech.beshu.ror.implicits.*
3132
import tech.beshu.ror.utils.AccessControllerHelper.doPrivileged
3233

3334
import java.util
34-
import scala.util.{Failure, Success, Try}
35+
import scala.util.Try
3536

3637
class ChannelInterceptingRestHandlerDecorator private(val underlying: RestHandler)
3738
extends RestHandler with Logging {
@@ -41,23 +42,16 @@ class ChannelInterceptingRestHandlerDecorator private(val underlying: RestHandle
4142
}
4243

4344
override def handleRequest(request: RestRequest, channel: RestChannel, client: NodeClient): Unit = {
44-
Try {
45-
RorRestChannel.from(channel) match {
46-
case Right(rorRestChannel) =>
47-
ThreadRepo.safeSetRestChannel(rorRestChannel) {
48-
addXpackUserAuthenticationHeaderForInCaseOfSecurityRequest(request, client)
49-
wrapped.handleRequest(request, rorRestChannel, client)
50-
}
51-
case Left(error) =>
52-
logError(error)
53-
implicit val show = authorizationValueErrorSanitizedShow
54-
channel.sendResponse(new BytesRestResponse(channel, RestStatus.BAD_REQUEST, new ElasticsearchException(error.show)))
55-
}
56-
} match {
57-
case Success(_) =>
58-
case Failure(ex) =>
59-
logger.error(s"The incoming request handling error:", ex)
60-
channel.sendResponse(new BytesRestResponse(channel, RestStatus.INTERNAL_SERVER_ERROR, new ElasticsearchException("ROR internal error")))
45+
RorRestChannel.from(channel) match {
46+
case Right(rorRestChannel) =>
47+
ThreadRepo.safeSetRestChannel(rorRestChannel) {
48+
addXpackUserAuthenticationHeaderForInCaseOfSecurityRequest(request, client)
49+
wrapped.handleRequest(request, rorRestChannel, client)
50+
}
51+
case Left(error) =>
52+
logError(error)
53+
implicit val show: Show[AuthorizationValueError] = authorizationValueErrorSanitizedShow
54+
channel.sendResponse(new BytesRestResponse(channel, RestStatus.BAD_REQUEST, new ElasticsearchException(error.show)))
6155
}
6256
}
6357

@@ -104,11 +98,11 @@ class ChannelInterceptingRestHandlerDecorator private(val underlying: RestHandle
10498

10599
private def logError(error: AuthorizationValueError): Unit = {
106100
{
107-
implicit val show = authorizationValueErrorSanitizedShow
101+
implicit val show: Show[AuthorizationValueError] = authorizationValueErrorSanitizedShow
108102
logger.warn(s"The incoming request was malformed. Cause: ${error.show}")
109103
}
110104
if (logger.delegate.isDebugEnabled()) {
111-
implicit val show = authorizationValueErrorWithDetailsShow
105+
implicit val show: Show[AuthorizationValueError] = authorizationValueErrorWithDetailsShow
112106
logger.debug(s"Malformed request detailed cause: ${error.show}")
113107
}
114108
}

es716x/src/main/scala/tech/beshu/ror/es/utils/ChannelInterceptingRestHandlerDecorator.scala

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package tech.beshu.ror.es.utils
1818

19+
import cats.Show
1920
import cats.implicits.*
2021
import org.apache.logging.log4j.scala.Logging
2122
import org.elasticsearch.ElasticsearchException
@@ -31,7 +32,7 @@ import tech.beshu.ror.implicits.*
3132
import tech.beshu.ror.utils.AccessControllerHelper.doPrivileged
3233

3334
import java.util
34-
import scala.util.{Failure, Success, Try}
35+
import scala.util.Try
3536

3637
class ChannelInterceptingRestHandlerDecorator private(val underlying: RestHandler)
3738
extends RestHandler with Logging {
@@ -41,23 +42,16 @@ class ChannelInterceptingRestHandlerDecorator private(val underlying: RestHandle
4142
}
4243

4344
override def handleRequest(request: RestRequest, channel: RestChannel, client: NodeClient): Unit = {
44-
Try {
45-
RorRestChannel.from(channel) match {
46-
case Right(rorRestChannel) =>
47-
ThreadRepo.safeSetRestChannel(rorRestChannel) {
48-
addXpackUserAuthenticationHeaderForInCaseOfSecurityRequest(request, client)
49-
wrapped.handleRequest(request, rorRestChannel, client)
50-
}
51-
case Left(error) =>
52-
logError(error)
53-
implicit val show = authorizationValueErrorSanitizedShow
54-
channel.sendResponse(new BytesRestResponse(channel, RestStatus.BAD_REQUEST, new ElasticsearchException(error.show)))
55-
}
56-
} match {
57-
case Success(_) =>
58-
case Failure(ex) =>
59-
logger.error(s"The incoming request handling error:", ex)
60-
channel.sendResponse(new BytesRestResponse(channel, RestStatus.INTERNAL_SERVER_ERROR, new ElasticsearchException("ROR internal error")))
45+
RorRestChannel.from(channel) match {
46+
case Right(rorRestChannel) =>
47+
ThreadRepo.safeSetRestChannel(rorRestChannel) {
48+
addXpackUserAuthenticationHeaderForInCaseOfSecurityRequest(request, client)
49+
wrapped.handleRequest(request, rorRestChannel, client)
50+
}
51+
case Left(error) =>
52+
logError(error)
53+
implicit val show: Show[AuthorizationValueError] = authorizationValueErrorSanitizedShow
54+
channel.sendResponse(new BytesRestResponse(channel, RestStatus.BAD_REQUEST, new ElasticsearchException(error.show)))
6155
}
6256
}
6357

@@ -104,11 +98,11 @@ class ChannelInterceptingRestHandlerDecorator private(val underlying: RestHandle
10498

10599
private def logError(error: AuthorizationValueError): Unit = {
106100
{
107-
implicit val show = authorizationValueErrorSanitizedShow
101+
implicit val show: Show[AuthorizationValueError] = authorizationValueErrorSanitizedShow
108102
logger.warn(s"The incoming request was malformed. Cause: ${error.show}")
109103
}
110104
if (logger.delegate.isDebugEnabled()) {
111-
implicit val show = authorizationValueErrorWithDetailsShow
105+
implicit val show: Show[AuthorizationValueError] = authorizationValueErrorWithDetailsShow
112106
logger.debug(s"Malformed request detailed cause: ${error.show}")
113107
}
114108
}

0 commit comments

Comments
 (0)