Skip to content

Commit

Permalink
#457 Regenerate id of all alerts
Browse files Browse the repository at this point in the history
  • Loading branch information
To-om committed Jan 31, 2018
1 parent 6b0805b commit 68d6c77
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions thehive-backend/app/models/Migration.scala
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ class Migration(
caseTemplate - "metricNames" + ("metrics" -> metrics)
},
addAttribute("case_artifact", "sighted" -> JsFalse))
case DatabaseState(12)
case ds @ DatabaseState(12)
Seq(
// Remove alert artifacts in audit trail
mapEntity("audit") {
Expand All @@ -303,7 +303,33 @@ class Migration(
audit + ("details" -> (details - "artifacts"))
}
case audit audit
})
},
// Regenerate all alert ID
mapEntity("alert") { alert
alert + ("_id" JsString(generateAlertId(alert)))
},
// and overwrite alert id in audit trail
Operation((f: String Source[JsObject, NotUsed]) {
case "audit" f("audit").flatMapConcat {
case audit if (audit \ "objectType").asOpt[String].contains("alert")
val updatedAudit = (audit \ "objectId").asOpt[String].fold(Future.successful(audit)) { alertId
ds.getEntity("alert", alertId)
.map { alert audit + ("objectId" -> JsString(generateAlertId(alert))) }
.recover { case _ audit }
}
Source.fromFuture(updatedAudit)
case audit Source.single(audit)
}
case other f(other)
}))
}

private def generateAlertId(alert: JsObject): String = {
val hasher = Hasher("MD5")
val tpe = (alert \ "type").asOpt[String].getOrElse("<null>")
val source = (alert \ "source").asOpt[String].getOrElse("<null>")
val sourceRef = (alert \ "sourceRef").asOpt[String].getOrElse("<null>")
hasher.fromString(s"$tpe|$source|$sourceRef").head.toString()
}

private def convertDate(json: JsValue): JsValue = {
Expand Down

0 comments on commit 68d6c77

Please sign in to comment.