-
Notifications
You must be signed in to change notification settings - Fork 640
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Audit trail elements are sent in JSON format to an URL using http POST. A new configuration section "webhooks" has been added. The format is: webhooks { webhook-name-1 { url = "http://my.webhook.url" } webhook-name-2 { url = "http://my.other.webhook.url" } } HTTP client configuration (timeout, proxy, SSL, ...) can be configured in each level like MISP and Cortex configuration.
- Loading branch information
Showing
2 changed files
with
56 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package services | ||
|
||
import javax.inject.Inject | ||
|
||
import scala.concurrent.ExecutionContext | ||
import scala.util.{ Failure, Success, Try } | ||
|
||
import play.api.{ Configuration, Logger } | ||
import play.api.libs.json.JsObject | ||
import play.api.libs.ws.WSRequest | ||
|
||
case class WebHook(name: String, ws: WSRequest)(implicit ec: ExecutionContext) { | ||
private[WebHook] lazy val logger = Logger(getClass.getName + "." + name) | ||
|
||
def send(obj: JsObject) = ws.post(obj).onComplete { | ||
case Success(resp) if resp.status / 100 != 2 ⇒ logger.error(s"WebHook returns status ${resp.status} ${resp.statusText}") | ||
case Failure(error) ⇒ logger.error("WebHook call error", error) | ||
case _ ⇒ | ||
} | ||
} | ||
|
||
class WebHooks( | ||
webhooks: Seq[WebHook]) { | ||
@Inject() def this( | ||
configuration: Configuration, | ||
globalWS: CustomWSAPI, | ||
ec: ExecutionContext) = { | ||
this( | ||
for { | ||
cfg ← configuration.getOptional[Configuration]("webhooks").toSeq | ||
whWS = globalWS.withConfig(cfg) | ||
name ← cfg.subKeys | ||
whConfig ← Try(cfg.get[Configuration](name)).toOption | ||
url ← whConfig.getOptional[String]("url") | ||
instanceWS = whWS.withConfig(whConfig).url(url) | ||
} yield WebHook(name, instanceWS)(ec)) | ||
} | ||
|
||
def send(obj: JsObject): Unit = webhooks.foreach(_.send(obj)) | ||
} |