Skip to content

[RORDEV-1414] ES node details audit reporting #1116

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

Open
wants to merge 30 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* This file is part of ReadonlyREST.
*
* ReadonlyREST is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ReadonlyREST is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ReadonlyREST. If not, see http://www.gnu.org/licenses/
*/
package tech.beshu.ror.audit

final case class AuditEnvironmentContext(esNodeName: String,
esClusterName: String)
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ package tech.beshu.ror.audit
import org.json.JSONObject

trait AuditLogSerializer {
def onResponse(responseContext: AuditResponseContext): Option[JSONObject]
def onResponse(responseContext: AuditResponseContext,
Copy link
Collaborator

Choose a reason for hiding this comment

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

What about backward compatibility here? The existing custom serializers should work with the new approach too.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I modified it in a following way:

  • the def onResponse is restored to its previous state
  • the AuditEnvironmentContext is instead provided in a Serializer's constructor, with modified class Decoder implementation

environmentContext: AuditEnvironmentContext): Option[JSONObject]
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package tech.beshu.ror.audit.adapters
import org.json.JSONObject
import tech.beshu.ror.audit.AuditResponseContext.{Allowed, Verbosity}
import tech.beshu.ror.audit.instances.SerializeUser
import tech.beshu.ror.audit.{AuditLogSerializer, AuditRequestContext, AuditResponseContext}
import tech.beshu.ror.audit.{AuditEnvironmentContext, AuditLogSerializer, AuditRequestContext, AuditResponseContext}
import tech.beshu.ror.commons.ResponseContext.FinalState
import tech.beshu.ror.commons.shims.request.RequestContextShim

Expand All @@ -30,7 +30,8 @@ import scala.collection.JavaConverters._
class DeprecatedAuditLogSerializerAdapter[T](underlying: tech.beshu.ror.requestcontext.AuditLogSerializer[T])
extends AuditLogSerializer {

override def onResponse(responseContext: AuditResponseContext): Option[JSONObject] = {
override def onResponse(responseContext: AuditResponseContext,
environmentContext: AuditEnvironmentContext): Option[JSONObject] = {
val deprecatedResponseContext = responseContext match {
case Allowed(_, verbosity, _) =>
verbosity match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,83 +16,4 @@
*/
package tech.beshu.ror.audit.instances

import java.time.ZoneId
import java.time.format.DateTimeFormatter
import org.json.JSONObject
import tech.beshu.ror.audit.AuditResponseContext._
import tech.beshu.ror.audit.{AuditLogSerializer, AuditRequestContext, AuditResponseContext}

import scala.collection.JavaConverters._
import scala.concurrent.duration.FiniteDuration

class DefaultAuditLogSerializer extends AuditLogSerializer {

private val timestampFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").withZone(ZoneId.of("GMT"))

override def onResponse(responseContext: AuditResponseContext): Option[JSONObject] = responseContext match {
case Allowed(requestContext, verbosity, reason) =>
verbosity match {
case Verbosity.Info =>
Some(createEntry(matched = true, "ALLOWED", reason, responseContext.duration, requestContext, None))
case Verbosity.Error =>
None
}
case ForbiddenBy(requestContext, _, reason) =>
Some(createEntry(matched = true, "FORBIDDEN", reason, responseContext.duration, requestContext, None))
case Forbidden(requestContext) =>
Some(createEntry(matched = false, "FORBIDDEN", "default", responseContext.duration, requestContext, None))
case RequestedIndexNotExist(requestContext) =>
Some(createEntry(matched = false, "INDEX NOT EXIST", "Requested index doesn't exist", responseContext.duration, requestContext, None))
case Errored(requestContext, cause) =>
Some(createEntry(matched = false, "ERRORED", "error", responseContext.duration, requestContext, Some(cause)))
}

private def createEntry(matched: Boolean,
finalState: String,
reason: String,
duration: FiniteDuration,
requestContext: AuditRequestContext,
error: Option[Throwable]) = {
new JSONObject()
.put("match", matched)
.put("block", reason)
.put("id", requestContext.id)
.put("final_state", finalState)
.put("@timestamp", timestampFormatter.format(requestContext.timestamp))
.put("correlation_id", requestContext.correlationId)
.put("processingMillis", duration.toMillis)
.put("error_type", error.map(_.getClass.getSimpleName).orNull)
.put("error_message", error.map(_.getMessage).orNull)
.put("content_len", requestContext.contentLength)
.put("content_len_kb", requestContext.contentLength / 1024)
.put("type", requestContext.`type`)
.put("origin", requestContext.remoteAddress)
.put("destination", requestContext.localAddress)
.put("xff", requestContext.requestHeaders.getValue("X-Forwarded-For").flatMap(_.headOption).orNull)
.put("task_id", requestContext.taskId)
.put("req_method", requestContext.httpMethod)
.put("headers", requestContext.requestHeaders.names.asJava)
.put("path", requestContext.uriPath)
.put("user", SerializeUser.serialize(requestContext).orNull)
.put("impersonated_by", requestContext.impersonatedByUserName.orNull)
.put("action", requestContext.action)
.put("indices", if (requestContext.involvesIndices) requestContext.indices.toList.asJava else List.empty.asJava)
.put("acl_history", requestContext.history)
.mergeWith(requestContext.generalAuditEvents)
}

private implicit class JsonObjectOps(val mainJson: JSONObject) {
def mergeWith(secondaryJson: JSONObject): JSONObject = {
jsonKeys(secondaryJson).foldLeft(mainJson) {
case (json, name) if !json.has(name) =>
json.put(name, secondaryJson.get(name))
case (json, _) =>
json
}
}

private def jsonKeys(json: JSONObject) = {
Option(JSONObject.getNames(json)).toList.flatten
}
}
}
class DefaultAuditLogSerializer extends DefaultAuditLogSerializerV2
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* This file is part of ReadonlyREST.
*
* ReadonlyREST is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ReadonlyREST is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ReadonlyREST. If not, see http://www.gnu.org/licenses/
*/
package tech.beshu.ror.audit.instances

import org.json.JSONObject
import tech.beshu.ror.audit.AuditResponseContext._
import tech.beshu.ror.audit.{AuditEnvironmentContext, AuditLogSerializer, AuditRequestContext, AuditResponseContext}

import java.time.ZoneId
import java.time.format.DateTimeFormatter
import scala.collection.JavaConverters._
import scala.concurrent.duration.FiniteDuration

class DefaultAuditLogSerializerV1 extends AuditLogSerializer {

private val timestampFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").withZone(ZoneId.of("GMT"))

override def onResponse(responseContext: AuditResponseContext,
environmentContext: AuditEnvironmentContext): Option[JSONObject] = responseContext match {
case Allowed(requestContext, verbosity, reason) =>
verbosity match {
case Verbosity.Info =>
Some(createEntry(matched = true, "ALLOWED", reason, responseContext.duration, requestContext, None))
case Verbosity.Error =>
None
}
case ForbiddenBy(requestContext, _, reason) =>
Some(createEntry(matched = true, "FORBIDDEN", reason, responseContext.duration, requestContext, None))
case Forbidden(requestContext) =>
Some(createEntry(matched = false, "FORBIDDEN", "default", responseContext.duration, requestContext, None))
case RequestedIndexNotExist(requestContext) =>
Some(createEntry(matched = false, "INDEX NOT EXIST", "Requested index doesn't exist", responseContext.duration, requestContext, None))
case Errored(requestContext, cause) =>
Some(createEntry(matched = false, "ERRORED", "error", responseContext.duration, requestContext, Some(cause)))
}

private def createEntry(matched: Boolean,
finalState: String,
reason: String,
duration: FiniteDuration,
requestContext: AuditRequestContext,
error: Option[Throwable]) = {
new JSONObject()
.put("match", matched)
.put("block", reason)
.put("id", requestContext.id)
.put("final_state", finalState)
.put("@timestamp", timestampFormatter.format(requestContext.timestamp))
.put("correlation_id", requestContext.correlationId)
.put("processingMillis", duration.toMillis)
.put("error_type", error.map(_.getClass.getSimpleName).orNull)
.put("error_message", error.map(_.getMessage).orNull)
.put("content_len", requestContext.contentLength)
.put("content_len_kb", requestContext.contentLength / 1024)
.put("type", requestContext.`type`)
.put("origin", requestContext.remoteAddress)
.put("destination", requestContext.localAddress)
.put("xff", requestContext.requestHeaders.getValue("X-Forwarded-For").flatMap(_.headOption).orNull)
.put("task_id", requestContext.taskId)
.put("req_method", requestContext.httpMethod)
.put("headers", requestContext.requestHeaders.names.asJava)
.put("path", requestContext.uriPath)
.put("user", SerializeUser.serialize(requestContext).orNull)
.put("impersonated_by", requestContext.impersonatedByUserName.orNull)
.put("action", requestContext.action)
.put("indices", if (requestContext.involvesIndices) requestContext.indices.toList.asJava else List.empty.asJava)
.put("acl_history", requestContext.history)
.mergeWith(requestContext.generalAuditEvents)
}

private implicit class JsonObjectOps(val mainJson: JSONObject) {
def mergeWith(secondaryJson: JSONObject): JSONObject = {
jsonKeys(secondaryJson).foldLeft(mainJson) {
case (json, name) if !json.has(name) =>
json.put(name, secondaryJson.get(name))
case (json, _) =>
json
}
}

private def jsonKeys(json: JSONObject) = {
Option(JSONObject.getNames(json)).toList.flatten
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* This file is part of ReadonlyREST.
*
* ReadonlyREST is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ReadonlyREST is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ReadonlyREST. If not, see http://www.gnu.org/licenses/
*/
package tech.beshu.ror.audit.instances

import org.json.JSONObject
import tech.beshu.ror.audit.{AuditEnvironmentContext, AuditResponseContext}

class DefaultAuditLogSerializerV2 extends DefaultAuditLogSerializerV1 {

override def onResponse(responseContext: AuditResponseContext,
environmentContext: AuditEnvironmentContext): Option[JSONObject] = {
lazy val additionalFields = Map(
"es_node_name" -> environmentContext.esNodeName,
"es_cluster_name" -> environmentContext.esClusterName
)
super.onResponse(responseContext, environmentContext)
.map(additionalFields.foldLeft(_) { case (soFar, (key, value)) => soFar.put(key, value) })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,4 @@
*/
package tech.beshu.ror.audit.instances

import org.json.JSONObject
import tech.beshu.ror.audit.AuditResponseContext

class QueryAuditLogSerializer extends DefaultAuditLogSerializer {

override def onResponse(responseContext: AuditResponseContext): Option[JSONObject] = {
super.onResponse(responseContext)
.map(_.put("content", responseContext.requestContext.content))
}
}
class QueryAuditLogSerializer extends QueryAuditLogSerializerV2
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* This file is part of ReadonlyREST.
*
* ReadonlyREST is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ReadonlyREST is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ReadonlyREST. If not, see http://www.gnu.org/licenses/
*/
package tech.beshu.ror.audit.instances

import org.json.JSONObject
import tech.beshu.ror.audit.{AuditEnvironmentContext, AuditResponseContext}

class QueryAuditLogSerializerV1 extends DefaultAuditLogSerializerV1 {

override def onResponse(responseContext: AuditResponseContext,
environmentContext: AuditEnvironmentContext): Option[JSONObject] = {
super.onResponse(responseContext, environmentContext)
.map(_.put("content", responseContext.requestContext.content))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* This file is part of ReadonlyREST.
*
* ReadonlyREST is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ReadonlyREST is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ReadonlyREST. If not, see http://www.gnu.org/licenses/
*/
package tech.beshu.ror.audit.instances

import org.json.JSONObject
import tech.beshu.ror.audit.{AuditEnvironmentContext, AuditResponseContext}

class QueryAuditLogSerializerV2 extends DefaultAuditLogSerializerV2 {

override def onResponse(responseContext: AuditResponseContext,
environmentContext: AuditEnvironmentContext): Option[JSONObject] = {
super.onResponse(responseContext, environmentContext)
.map(_.put("content", responseContext.requestContext.content))
}
}
Loading
Loading