Skip to content

Commit

Permalink
Improve error message when entity is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
To-om committed May 16, 2020
1 parent 00832aa commit b95aa9b
Show file tree
Hide file tree
Showing 34 changed files with 148 additions and 134 deletions.
2 changes: 1 addition & 1 deletion ScalliGraph
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ class ActionCtrl @Inject() (
implicit val entityWrites: OWrites[Entity] = OWrites[Entity] { entity =>
db.roTransaction { implicit graph =>
entity match {
case c: Case => caseSrv.get(c).richCaseWithoutPerms.getOrFail().map(_.toJson.as[JsObject])
case t: Task => taskSrv.get(t).richTask.getOrFail().map(_.toJson.as[JsObject])
case o: Observable => observableSrv.get(o).richObservable.getOrFail().map(_.toJson.as[JsObject])
case l: Log => logSrv.get(l).richLog.getOrFail().map(_.toJson.as[JsObject])
case a: Alert => alertSrv.get(a).richAlert.getOrFail().map(_.toJson.as[JsObject])
case c: Case => caseSrv.get(c).richCaseWithoutPerms.getOrFail("Case").map(_.toJson.as[JsObject])
case t: Task => taskSrv.get(t).richTask.getOrFail("Task").map(_.toJson.as[JsObject])
case o: Observable => observableSrv.get(o).richObservable.getOrFail("Observable").map(_.toJson.as[JsObject])
case l: Log => logSrv.get(l).richLog.getOrFail("Log").map(_.toJson.as[JsObject])
case a: Alert => alertSrv.get(a).richAlert.getOrFail("Alert").map(_.toJson.as[JsObject])
}
}
.getOrElse(Json.obj("_type" -> entity._model.label, "_id" -> entity._id))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ class JobCtrl @Inject() (
db.roTransaction { implicit graph =>
val artifactId: String = request.body("artifactId")
for {
o <- observableSrv.getByIds(artifactId).richObservable.getOrFail()
c <- observableSrv.getByIds(artifactId).`case`.getOrFail()
o <- observableSrv.getByIds(artifactId).richObservable.getOrFail("Observable")
c <- observableSrv.getByIds(artifactId).`case`.getOrFail("Case")
} yield (o, c)
}
.fold(error => errorHandler.onServerError(request, error), {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class JobCtrlTest extends PlaySpecification with TestAppBuilder {
"job controller" should {
"get a job" in testApp { app =>
val observable = app[Database].roTransaction { implicit graph =>
app[ObservableSrv].initSteps.has("message", "Some weird domain").getOrFail().get
app[ObservableSrv].initSteps.has("message", "Some weird domain").getOrFail("Observable").get
}

val requestSearch = FakeRequest("POST", s"/api/connector/cortex/job/_search?range=0-200&sort=-startDate")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class EntityHelperTest extends PlaySpecification with TestAppBuilder {
"return task info" in testApp { app =>
app[Database].roTransaction { implicit graph =>
for {
task <- app[TaskSrv].initSteps.has("title", "case 1 task 1").getOrFail()
task <- app[TaskSrv].initSteps.has("title", "case 1 task 1").getOrFail("Task")
(title, tlp, pap) <- app[EntityHelper].entityInfo(task)
} yield (title, tlp, pap)
} must beASuccessfulTry.which {
Expand All @@ -31,7 +31,7 @@ class EntityHelperTest extends PlaySpecification with TestAppBuilder {
"return observable info" in testApp { app =>
app[Database].roTransaction { implicit graph =>
for {
observable <- app[ObservableSrv].initSteps.has("message", "Some weird domain").getOrFail()
observable <- app[ObservableSrv].initSteps.has("message", "Some weird domain").getOrFail("Observable")
(title, tlp, pap) <- app[EntityHelper].entityInfo(observable)
} yield (title, tlp, pap)
} must beASuccessfulTry.which {
Expand All @@ -45,7 +45,7 @@ class EntityHelperTest extends PlaySpecification with TestAppBuilder {
"find a manageable entity only (task)" in testApp { app =>
app[Database].roTransaction { implicit graph =>
for {
task <- app[TaskSrv].initSteps.has("title", "case 1 task 1").getOrFail()
task <- app[TaskSrv].initSteps.has("title", "case 1 task 1").getOrFail("Task")
t <- app[EntityHelper].get("Task", task._id, Permissions.manageAction)
} yield t
} must beSuccessfulTry
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class JobSrvTest extends PlaySpecification with TestAppBuilder {

val createdJobTry = app[Database].tryTransaction { implicit graph =>
for {
observable <- app[ObservableSrv].initSteps.has("message", "hello world").getOrFail()
observable <- app[ObservableSrv].initSteps.has("message", "hello world").getOrFail("Observable")
createdJob <- app[JobSrv].create(job, observable)
} yield createdJob
}
Expand All @@ -71,9 +71,9 @@ class JobSrvTest extends PlaySpecification with TestAppBuilder {
}

for {
audit <- app[AuditSrv].initSteps.has("objectId", updatedJob._id).getOrFail()
organisation <- app[OrganisationSrv].get("cert").getOrFail()
user <- app[UserSrv].initSteps.getByName("[email protected]").getOrFail()
audit <- app[AuditSrv].initSteps.has("objectId", updatedJob._id).getOrFail("Audit")
organisation <- app[OrganisationSrv].get("cert").getOrFail("Organisation")
user <- app[UserSrv].initSteps.getByName("[email protected]").getOrFail("User")
} yield new JobFinished().filter(audit, Some(updatedJob), organisation, Some(user))
} must beASuccessfulTry(true)
}
Expand All @@ -82,7 +82,7 @@ class JobSrvTest extends PlaySpecification with TestAppBuilder {
"submit a job" in testApp { app =>
val x = for {
observable <- app[Database].roTransaction { implicit graph =>
app[ObservableSrv].initSteps.has("message", "Some weird domain").richObservable.getOrFail()
app[ObservableSrv].initSteps.has("message", "Some weird domain").richObservable.getOrFail("Observable")
}
case0 <- app[Database].roTransaction { implicit graph =>
app[CaseSrv].getOrFail("#1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class MispCtrl @Inject() (
c <- Future.fromTry(db.roTransaction { implicit graph =>
caseSrv
.get(caseIdOrNumber)
.getOrFail()
.getOrFail("Case")
})
_ <- mispExportSrv.export(mispId, c)
} yield Results.NoContent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class MispImportSrvTest(implicit ec: ExecutionContext) extends PlaySpecification
await(app[MispImportSrv].syncMispEvents(app[TheHiveMispClient])(authContext))(1.minute)

app[Database].roTransaction { implicit graph =>
app[AlertSrv].initSteps.getBySourceId("misp", "ORGNAME", "1").getOrFail()
app[AlertSrv].initSteps.getBySourceId("misp", "ORGNAME", "1").getOrFail("Alert")
} must beSuccessfulTry(
Alert(
`type` = "misp",
Expand Down
8 changes: 6 additions & 2 deletions thehive/app/org/thp/thehive/controllers/v0/AlertCtrl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import org.thp.scalligraph.query.{ParamQuery, PropertyUpdater, PublicProperty, Q
import org.thp.scalligraph.services._
import org.thp.scalligraph.steps.StepsOps._
import org.thp.scalligraph.steps.{PagedResult, Traversal}
import org.thp.scalligraph.{InvalidFormatAttributeError, RichJMap, RichSeq}
import org.thp.scalligraph.{AuthorizationError, InvalidFormatAttributeError, RichJMap, RichSeq}
import org.thp.thehive.controllers.v0.Conversion._
import org.thp.thehive.dto.v0.{InputAlert, InputObservable, OutputSimilarCase}
import org.thp.thehive.models._
Expand Down Expand Up @@ -87,7 +87,11 @@ class AlertCtrl @Inject() (
val customFields = inputAlert.customFields.map(c => c.name -> c.value).toMap
val caseTemplate = caseTemplateName.flatMap(caseTemplateSrv.get(_).visible.headOption())
for {
organisation <- userSrv.current.organisations(Permissions.manageAlert).get(request.organisation).getOrFail()
organisation <- userSrv
.current
.organisations(Permissions.manageAlert)
.get(request.organisation)
.orFail(AuthorizationError("Operation not permitted"))
richObservables <- observables.toTry(createObservable).map(_.flatten)
richAlert <- alertSrv.create(request.body("alert").toAlert, organisation, inputAlert.tags, customFields, caseTemplate)
_ <- auditSrv.mergeAudits(richObservables.toTry(o => alertSrv.addObservable(richAlert.alert, o)))(_ => Success(()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class AttachmentCtrl @Inject() (entrypoint: Entrypoint, appConfig: ApplicationCo
attachmentSrv
.get(id)
.visible
.getOrFail()
.getOrFail("Attachment")
.filter(attachmentSrv.exists)
.map { attachment =>
Result(
Expand Down Expand Up @@ -62,7 +62,7 @@ class AttachmentCtrl @Inject() (entrypoint: Entrypoint, appConfig: ApplicationCo
attachmentSrv
.get(id)
.visible
.getOrFail()
.getOrFail("Attachment")
.filter(attachmentSrv.exists)
.flatMap { attachment =>
Try {
Expand Down
10 changes: 7 additions & 3 deletions thehive/app/org/thp/thehive/controllers/v0/CaseCtrl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,13 @@ class CaseCtrl @Inject() (
val inputTasks: Seq[InputTask] = request.body("tasks")
val customFields = inputCase.customFields.map(c => c.name -> c.value).toMap
for {
organisation <- userSrv.current.organisations(Permissions.manageCase).get(request.organisation).getOrFail()
caseTemplate <- caseTemplateName.map(caseTemplateSrv.get(_).visible.richCaseTemplate.getOrFail()).flip
user <- inputCase.user.map(userSrv.get(_).visible.getOrFail()).flip
organisation <- userSrv
.current
.organisations(Permissions.manageCase)
.get(request.organisation)
.orFail(AuthorizationError("Operation not permitted"))
caseTemplate <- caseTemplateName.map(caseTemplateSrv.get(_).visible.richCaseTemplate.getOrFail("CaseTemplate")).flip
user <- inputCase.user.map(userSrv.get(_).visible.getOrFail("User")).flip
tags <- inputCase.tags.toTry(tagSrv.getOrCreate)
tasks <- inputTasks.toTry(t => t.owner.map(userSrv.getOrFail).flip.map(owner => t.toTask -> owner))
richCase <- caseSrv.create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class CustomFieldCtrl @Inject() (entrypoint: Entrypoint, db: Database, propertie
def get(id: String): Action[AnyContent] =
entrypoint("get custom field")
.authRoTransaction(db) { _ => implicit graph =>
customFieldSrv.get(id).getOrFail().map(cf => Results.Ok(cf.toJson))
customFieldSrv.get(id).getOrFail("CustomField").map(cf => Results.Ok(cf.toJson))
}

def delete(id: String): Action[AnyContent] =
Expand All @@ -62,7 +62,7 @@ class CustomFieldCtrl @Inject() (entrypoint: Entrypoint, db: Database, propertie

for {
updated <- customFieldSrv.update(customFieldSrv.get(id), propertyUpdaters)
cf <- updated._1.getOrFail()
cf <- updated._1.getOrFail("CustomField")
} yield Results.Ok(cf.toJson)
}

Expand Down
2 changes: 1 addition & 1 deletion thehive/app/org/thp/thehive/controllers/v0/ListCtrl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class ListCtrl @Inject() (entrypoint: Entrypoint, db: Database, customFieldSrv:
customFieldSrv
.initSteps
.get(v)
.getOrFail()
.getOrFail("CustomField")
.map(f => Results.Conflict(Json.obj("found" -> f.toJson)))
.orElse(Success(Results.Ok))
case _ => Success(Results.Locked(""))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class ObservableCtrl @Inject() (
case0 <- caseSrv
.get(caseId)
.can(Permissions.manageObservable)
.getOrFail()
.orFail(AuthorizationError("Operation not permitted"))
observableType <- observableTypeSrv.getOrFail(inputObservable.dataType)
observablesWithData <- inputObservable
.data
Expand All @@ -90,7 +90,7 @@ class ObservableCtrl @Inject() (
.getByIds(observableId)
.visible
.richObservable
.getOrFail()
.getOrFail("Observable")
.map { observable =>
Results.Ok(observable.toJson)
}
Expand Down Expand Up @@ -145,7 +145,7 @@ class ObservableCtrl @Inject() (
observable <- observableSrv
.getByIds(obsId)
.can(Permissions.manageObservable)
.getOrFail()
.getOrFail("Observable")
_ <- observableSrv.cascadeRemove(observable)
} yield Results.NoContent
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ObservableTypeCtrl @Inject() (
def get(idOrName: String): Action[AnyContent] = entrypoint("get observable type").authRoTransaction(db) { _ => implicit graph =>
observableTypeSrv
.get(idOrName)
.getOrFail()
.getOrFail("Observable")
.map(ot => Results.Ok(ot.toJson))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class OrganisationCtrl @Inject() (
.get(organisationId)
.visible
.richOrganisation
.getOrFail()
.getOrFail("Organisation")
.map(organisation => Results.Ok(organisation.toJson))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class ProfileCtrl @Inject() (entrypoint: Entrypoint, db: Database, properties: P
if (request.isPermitted(Permissions.manageProfile)) {
profileSrv
.update(_.get(profileId), propertyUpdaters)
.flatMap { case (profileSteps, _) => profileSteps.getOrFail() }
.flatMap { case (profileSteps, _) => profileSteps.getOrFail("Profile") }
.map(profile => Results.Ok(profile.toJson))
} else
Failure(AuthorizationError("You don't have permission to update profiles"))
Expand Down
Loading

0 comments on commit b95aa9b

Please sign in to comment.