Skip to content

Commit

Permalink
#326 Check TLP and severity value
Browse files Browse the repository at this point in the history
  • Loading branch information
To-om committed Oct 12, 2017
1 parent 9253988 commit 371c06a
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion thehive-backend/app/models/AttributeFormat.scala
Original file line number Diff line number Diff line change
@@ -1,26 +1,80 @@
package models

import play.api.libs.json.JsNumber
import play.api.libs.json.{ JsNumber, JsValue }

import org.scalactic.{ Every, Good, One, Or }

import org.elastic4play.controllers.{ InputValue, JsonInputValue, StringInputValue }
import org.elastic4play.models.{ Attribute, AttributeDefinition, NumberAttributeFormat }
import org.elastic4play.services.DBLists
import org.elastic4play.{ AttributeError, InvalidFormatAttributeError }

object SeverityAttributeFormat extends NumberAttributeFormat {

def isValidValue(value: Long): Boolean = 1 <= value && value <= 3

override def definition(dblists: DBLists, attribute: Attribute[Long]): Seq[AttributeDefinition] =
Seq(AttributeDefinition(
attribute.name,
name,
attribute.description,
Seq(JsNumber(1), JsNumber(2), JsNumber(3)),
Seq("low", "medium", "high")))

override def checkJson(subNames: Seq[String], value: JsValue): Or[JsValue, One[InvalidFormatAttributeError]] = {
value match {
case JsNumber(v) if subNames.isEmpty && isValidValue(v.toLong) Good(value)
case _ formatError(JsonInputValue(value))
}
}

override def fromInputValue(subNames: Seq[String], value: InputValue): Long Or Every[AttributeError] = {
value match {
case StringInputValue(Seq(v)) if subNames.isEmpty
try {
val longValue = v.toLong
if (isValidValue(longValue)) Good(longValue)
else formatError(value)
}
catch {
case _: Throwable formatError(value)
}
case JsonInputValue(JsNumber(v)) Good(v.longValue)
case _ formatError(value)
}
}
}

object TlpAttributeFormat extends NumberAttributeFormat {

def isValidValue(value: Long): Boolean = 0 <= value && value <= 3

This comment has been minimized.

Copy link
@rolinh

rolinh Oct 12, 2017

The doc mentions that -1 is a valid TLP value corresponding to "unknown". Shouldn't it be accepted here then? Or the doc adjusted accordingly?

This comment has been minimized.

Copy link
@To-om

To-om Oct 12, 2017

Author Contributor

Yes, the documentation is not up-to-date. "Unknown" TLP is not yet supported (cf. TLP directive in front end).

Idem for "not set" severity (severity directive).

I'll update the documentation.


override def definition(dblists: DBLists, attribute: Attribute[Long]): Seq[AttributeDefinition] =
Seq(AttributeDefinition(
attribute.name,
name,
attribute.description,
Seq(JsNumber(0), JsNumber(1), JsNumber(2), JsNumber(3)),
Seq("white", "green", "amber", "red")))

override def checkJson(subNames: Seq[String], value: JsValue): Or[JsValue, One[InvalidFormatAttributeError]] = value match {
case JsNumber(v) if subNames.isEmpty && isValidValue(v.toLong) Good(value)
case _ formatError(JsonInputValue(value))
}

override def fromInputValue(subNames: Seq[String], value: InputValue): Long Or Every[AttributeError] = {
value match {
case StringInputValue(Seq(v)) if subNames.isEmpty
try {
val longValue = v.toLong
if (isValidValue(longValue)) Good(longValue)
else formatError(value)
}
catch {
case _: Throwable formatError(value)
}
case JsonInputValue(JsNumber(v)) Good(v.longValue)
case _ formatError(value)
}
}
}

0 comments on commit 371c06a

Please sign in to comment.