Skip to content
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

[DL-715][DL-716][DL-718] update libraries #473

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion app/org/thp/cortex/Module.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import play.api.libs.concurrent.AkkaGuiceSupport
import play.api.{Configuration, Environment, Logger, Mode}

import java.lang.reflect.Modifier
import scala.collection.JavaConverters._
import scala.jdk.CollectionConverters._

class Module(environment: Environment, configuration: Configuration) extends AbstractModule with ScalaModule with AkkaGuiceSupport {

Expand Down
2 changes: 1 addition & 1 deletion app/org/thp/cortex/controllers/AssetCtrl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ trait AssetCtrl {
}

@Singleton
class AssetCtrlProd @Inject() (errorHandler: HttpErrorHandler, meta: AssetsMetadata) extends Assets(errorHandler, meta) with AssetCtrl {
class AssetCtrlProd @Inject() (errorHandler: HttpErrorHandler, meta: AssetsMetadata, env: Environment) extends Assets(errorHandler, meta, env) with AssetCtrl {
def get(file: String): Action[AnyContent] = at("/www", file)
}

Expand Down
18 changes: 9 additions & 9 deletions app/org/thp/cortex/controllers/StatusCtrl.scala
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package org.thp.cortex.controllers

import scala.concurrent.{ExecutionContext, Future}
import com.sksamuel.elastic4s.ElasticDsl
import org.elastic4play.controllers.Authenticated
import org.elastic4play.services.AuthSrv
import org.elastic4play.services.auth.MultiAuthSrv
import org.elasticsearch.client.Node
import org.thp.cortex.models.{Roles, Worker, WorkerType}
import org.thp.cortex.services.WorkerSrv
import play.api.Configuration
import play.api.http.Status
import play.api.libs.json.Json.toJsFieldJsValueWrapper
import play.api.libs.json.{JsBoolean, JsNull, JsString, Json}
import play.api.libs.json.{JsBoolean, JsString, Json}
import play.api.mvc.{AbstractController, Action, AnyContent, ControllerComponents}
import com.sksamuel.elastic4s.ElasticDsl
import org.elastic4play.controllers.Authenticated

import javax.inject.{Inject, Singleton}
import org.elasticsearch.client.Node
import org.thp.cortex.models.{Roles, Worker, WorkerType}
import org.elastic4play.services.AuthSrv
import org.elastic4play.services.auth.MultiAuthSrv
import org.thp.cortex.services.WorkerSrv
import scala.concurrent.ExecutionContext

@Singleton
class StatusCtrl @Inject() (
Expand Down
4 changes: 1 addition & 3 deletions app/org/thp/cortex/models/BaseConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import scala.concurrent.duration.Duration
import play.api.Configuration
import play.api.libs.json._

import org.elastic4play.utils.Collection.distinctBy

case class BaseConfig(name: String, workerNames: Seq[String], items: Seq[ConfigurationDefinitionItem], config: Option[WorkerConfig]) {
def +(other: BaseConfig) = BaseConfig(name, workerNames ++ other.workerNames, distinctBy(items ++ other.items)(_.name), config.orElse(other.config))
def +(other: BaseConfig): BaseConfig = BaseConfig(name, workerNames ++ other.workerNames, (items ++ other.items).distinctBy(_.name), config.orElse(other.config))
}

object BaseConfig {
Expand Down
6 changes: 2 additions & 4 deletions app/org/thp/cortex/models/Job.scala
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package org.thp.cortex.models

import scala.util.Try

import play.api.libs.json.{JsObject, JsString, Json}
import play.api.libs.json.{Format, JsObject, JsString, Json}

import javax.inject.{Inject, Singleton}
import org.thp.cortex.models.JsonFormat.workerTypeFormat

import org.elastic4play.models.JsonFormat.enumFormat
import org.elastic4play.models.{AttributeDef, EntityDef, HiveEnumeration, ModelDef, AttributeFormat => F, AttributeOption => O}

object JobStatus extends Enumeration with HiveEnumeration {
type Type = Value
val Waiting, InProgress, Success, Failure, Deleted = Value
implicit val reads = enumFormat(this)
implicit val reads: Format[Value] = enumFormat(this)
}

trait JobAttributes {
Expand Down
7 changes: 2 additions & 5 deletions app/org/thp/cortex/models/Organization.scala
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package org.thp.cortex.models

import javax.inject.{Inject, Provider, Singleton}

import scala.concurrent.{ExecutionContext, Future}

import play.api.Logger
import play.api.libs.json.{JsNumber, JsObject, JsString, Json}

import play.api.libs.json.{Format, JsNumber, JsObject, JsString, Json}
import org.elastic4play.models.JsonFormat.enumFormat
import org.elastic4play.models.{AttributeDef, BaseEntity, EntityDef, HiveEnumeration, ModelDef, AttributeFormat => F, AttributeOption => O}
import org.elastic4play.services.FindSrv

object OrganizationStatus extends Enumeration with HiveEnumeration {
type Type = Value
val Active, Locked = Value
implicit val reads = enumFormat(this)
implicit val reads: Format[Value] = enumFormat(this)
}

trait OrganizationAttributes { _: AttributeDef =>
Expand Down
6 changes: 2 additions & 4 deletions app/org/thp/cortex/models/User.scala
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package org.thp.cortex.models

import scala.concurrent.Future

import play.api.libs.json.{JsArray, JsBoolean, JsObject, JsString}

import play.api.libs.json.{Format, JsArray, JsBoolean, JsObject, JsString}
import org.elastic4play.models.JsonFormat.enumFormat
import org.elastic4play.models.{AttributeDef, BaseEntity, EntityDef, HiveEnumeration, ModelDef, AttributeFormat => F, AttributeOption => O}
import org.elastic4play.services.{User => EUser}

object UserStatus extends Enumeration with HiveEnumeration {
type Type = Value
val Ok, Locked = Value
implicit val reads = enumFormat(this)
implicit val reads: Format[Value] = enumFormat(this)
}

trait UserAttributes { _: AttributeDef =>
Expand Down
4 changes: 2 additions & 2 deletions app/org/thp/cortex/models/Worker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.elastic4play.models.JsonFormat.enumFormat
import org.elastic4play.models.{AttributeDef, BaseEntity, ChildModelDef, EntityDef, HiveEnumeration, AttributeFormat => F, AttributeOption => O}
import org.elastic4play.utils.Hasher
import org.thp.cortex.models.JsonFormat.workerTypeFormat
import play.api.libs.json.{JsObject, JsString, Json}
import play.api.libs.json.{Format, JsObject, JsString, Json}

import scala.concurrent.Future
import scala.util.Try
Expand All @@ -17,7 +17,7 @@ object RateUnit extends Enumeration with HiveEnumeration {
val Hour = Value(60 * 60)
val Day = Value(60 * 60 * 24)
val Month = Value(60 * 60 * 24 * 30)
implicit val reads = enumFormat(this)
implicit val reads: Format[Value] = enumFormat(this)
}

object WorkerType extends Enumeration with HiveEnumeration {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package org.thp.cortex.services

import javax.inject.{Inject, Singleton}

import scala.concurrent.ExecutionContext
import scala.concurrent.duration.FiniteDuration

import play.api.Logger

import akka.actor.{Actor, ActorRef}
import org.thp.cortex.models.JobStatus

import org.elastic4play.models.BaseEntity
import org.elastic4play.models.{BaseEntity, BaseModelDef}
import org.elastic4play.services._

object AuditActor {
Expand All @@ -24,10 +20,10 @@ class AuditActor @Inject() (eventSrv: EventSrv, implicit val ec: ExecutionContex

import AuditActor._

object EntityExtractor {
def unapply(e: BaseEntity) = Some((e.model, e.id, e.routing))
private object EntityExtractor {
def unapply(e: BaseEntity): Option[(BaseModelDef, String, String)] = Some((e.model, e.id, e.routing))
}
var registration = Map.empty[String, Seq[ActorRef]]
private var registration = Map.empty[String, Seq[ActorRef]]
private[AuditActor] lazy val logger = Logger(getClass)

override def preStart(): Unit = {
Expand All @@ -42,17 +38,17 @@ class AuditActor @Inject() (eventSrv: EventSrv, implicit val ec: ExecutionContex

override def receive: Receive = {
case Register(jobId, timeout) =>
logger.info(s"Register new listener for job $jobId ($sender)")
val newActorList = registration.getOrElse(jobId, Nil) :+ sender
logger.info(s"Register new listener for job $jobId (${sender()})")
val newActorList = registration.getOrElse(jobId, Nil) :+ sender()
registration += (jobId -> newActorList)
context.system.scheduler.scheduleOnce(timeout, self, Unregister(jobId, sender))
context.system.scheduler.scheduleOnce(timeout, self, Unregister(jobId, sender()))

case Unregister(jobId, actorRef) =>
logger.info(s"Unregister listener for job $jobId ($actorRef)")
val newActorList = registration.getOrElse(jobId, Nil).filterNot(_ == actorRef)
registration += (jobId -> newActorList)

case AuditOperation(EntityExtractor(model, id, routing), action, details, authContext, date) =>
case AuditOperation(EntityExtractor(model, id, _), action, details, _, _) =>
if (model.modelName == "job" && action == AuditableAction.Update) {
logger.info(s"Job $id has be updated (${details \ "status"})")
val status = (details \ "status").asOpt[JobStatus.Type].getOrElse(JobStatus.InProgress)
Expand Down
2 changes: 1 addition & 1 deletion app/org/thp/cortex/services/JobSrv.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import org.thp.cortex.models._

import org.elastic4play._
import org.elastic4play.controllers._
import org.elastic4play.services._
import org.elastic4play.services.{UserSrv => _, _}
import org.elastic4play.utils.Hasher

@Singleton
Expand Down
5 changes: 2 additions & 3 deletions app/org/thp/cortex/services/ProcessJobRunnerSrv.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import play.api.libs.json.Json
import java.nio.charset.StandardCharsets
import java.nio.file.{Files, Path, Paths}
import javax.inject.{Inject, Singleton}
import scala.collection.mutable
import scala.concurrent.ExecutionContext
import scala.concurrent.duration.FiniteDuration
import scala.sys.process.{Process, ProcessLogger, _}
Expand All @@ -24,7 +23,7 @@ class ProcessJobRunnerSrv @Inject() (implicit val system: ActorSystem) {
def checkCortexUtilsVersion(pythonVersion: String): Option[(Int, Int, Int)] =
Try {
(s"pip$pythonVersion" :: "show" :: "cortexutils" :: Nil)
.lineStream
.lazyLines
.collectFirst {
case pythonPackageVersionRegex(major, minor, patch) => (major.toInt, minor.toInt, patch.toInt)
}
Expand All @@ -34,7 +33,7 @@ class ProcessJobRunnerSrv @Inject() (implicit val system: ActorSystem) {
ec: ExecutionContext
): Try[Unit] = {
val baseDirectory = Paths.get(command).getParent.getParent
val output = mutable.StringBuilder.newBuilder
val output = new StringBuilder()
logger.info(s"Execute $command in $baseDirectory, timeout is ${timeout.fold("none")(_.toString)}")
val cacertsFile = jobDirectory.resolve("input").resolve("cacerts")
val env = if (Files.exists(cacertsFile)) Seq("REQUESTS_CA_BUNDLE" -> cacertsFile.toString) else Nil
Expand Down
2 changes: 1 addition & 1 deletion app/org/thp/cortex/services/StreamSrv.scala
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class StreamActor(
wr.submit(Nil)
logger.error("Multiple requests !")
}
context.become(receiveWithState(Some(new WaitingRequest(sender)), currentMessages))
context.become(receiveWithState(Some(new WaitingRequest(sender())), currentMessages))

case Submit =>
waitingRequest match {
Expand Down
2 changes: 1 addition & 1 deletion app/org/thp/cortex/services/WorkerConfigSrv.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import org.scalactic.Accumulation._
import org.elastic4play.{AttributeCheckingError, NotFoundError}
import org.elastic4play.controllers.Fields
import org.elastic4play.database.ModifyConfig
import org.elastic4play.services._
import org.elastic4play.services.{UserSrv => _, _}

trait WorkerConfigSrv {
val configuration: Configuration
Expand Down
11 changes: 6 additions & 5 deletions app/org/thp/cortex/services/WorkerSrv.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import akka.stream.scaladsl.{Sink, Source}
import org.elastic4play._
import org.elastic4play.controllers.{Fields, StringInputValue}
import org.elastic4play.database.ModifyConfig
import org.elastic4play.services.QueryDSL.any
import org.elastic4play.services._
import org.elastic4play.services.{UserSrv => _, _}
import org.scalactic.Accumulation._
import org.scalactic._
import org.thp.cortex.models._
Expand All @@ -17,10 +16,10 @@ import play.api.{Configuration, Logger}
import java.net.URL
import java.nio.file.{Files, Path, Paths}
import javax.inject.{Inject, Provider, Singleton}
import scala.collection.JavaConverters._
import scala.concurrent.{ExecutionContext, Future}
import scala.io.Codec
import scala.util.{Failure, Success, Try}
import scala.jdk.CollectionConverters._

@Singleton
class WorkerSrv @Inject() (
Expand Down Expand Up @@ -212,7 +211,9 @@ class WorkerSrv @Inject() (
}
.map { worker =>
val wmap = worker.flatten.map(w => w.id -> w).toMap
workerMapLock.synchronized(workerMap = wmap)
workerMapLock.synchronized {
workerMap = wmap
}
logger.info(s"New worker list:\n\n\t${workerMap.values.map(a => s"${a.name} ${a.version}").mkString("\n\t")}\n")
}

Expand All @@ -229,7 +230,7 @@ class WorkerSrv @Inject() (
.validatedBy(_.read(rawConfig))
.map(JsObject.apply)

val unknownConfigItems = (rawConfig.value.keySet -- configItems.map(_.name))
val unknownConfigItems = (rawConfig.value.keySet.toSet -- configItems.map(_.name))
.foldLeft[Unit Or Every[AttributeError]](Good(())) {
case (Good(_), ci) => Bad(One(UnknownAttributeError("worker.config", JsString(ci))))
case (Bad(e), ci) => Bad(UnknownAttributeError("worker.config", JsString(ci)) +: e)
Expand Down
2 changes: 1 addition & 1 deletion app/org/thp/cortex/services/mappers/GroupUserMapper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class GroupUserMapper(
case Some(groupsEndpointUrl) =>
logger.debug(s"Retreiving groups from $groupsEndpointUrl")
val apiCall = authHeader.fold(ws.url(groupsEndpointUrl))(headers => ws.url(groupsEndpointUrl).addHttpHeaders(headers))
apiCall.get.flatMap { r =>
apiCall.get().flatMap { r =>
extractGroupsThenBuildUserFields(jsValue, r.json)
}
case None =>
Expand Down
2 changes: 1 addition & 1 deletion app/org/thp/cortex/util/JsonConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.typesafe.config.{ConfigList, ConfigObject, ConfigValue}
import play.api.Configuration
import play.api.libs.json._

import scala.collection.JavaConverters._
import scala.jdk.CollectionConverters._

object JsonConfig {
implicit val configValueWrites: Writes[ConfigValue] = Writes((value: ConfigValue) =>
Expand Down
26 changes: 20 additions & 6 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ ThisBuild / evictionErrorLevel := util.Level.Warn

ThisBuild / dependencyOverrides ++= Seq(
Dependencies.Play.twirl,
"com.fasterxml.jackson.core" % "jackson-databind" % "2.13.5",
"com.fasterxml.jackson.core" % "jackson-databind" % "2.14.3",
"org.apache.commons" % "commons-compress" % "1.23.0",
"com.google.guava" % "guava" % "32.1.1-jre"
)
lazy val cortex = (project in file("."))
.enablePlugins(PlayScala)
.dependsOn(elastic4play)
.settings(projectSettings)
.settings(PackageSettings.packageSettings)
.settings(PackageSettings.rpmSettings)
Expand All @@ -25,10 +26,8 @@ lazy val cortex = (project in file("."))
Dependencies.Play.specs2 % Test,
Dependencies.Play.guice,
Dependencies.scalaGuice,
Dependencies.elastic4play,
Dependencies.reflections,
Dependencies.zip4j,
Dependencies.dockerClient,
Dependencies.dockerJavaClient,
Dependencies.dockerJavaTransport,
Dependencies.akkaCluster,
Expand All @@ -40,9 +39,6 @@ lazy val cortex = (project in file("."))
"com.github.jnr" % "jnr-enxio" % "0.32.14",
"com.github.jnr" % "jnr-unixsocket" % "0.38.19"
),
resolvers += Resolver.sbtPluginRepo("releases"),
resolvers += "scalaz-bintray" at "https://dl.bintray.com/scalaz/releases",
resolvers += "elasticsearch-releases" at "https://artifacts.elastic.co/maven",
Compile / packageDoc / publishArtifact := false,
Compile / doc / sources := Seq.empty,
// Front-end //
Expand All @@ -55,6 +51,24 @@ lazy val cortex = (project in file("."))
)
)

val elastic4sVersion = "7.17.4"

lazy val elastic4play = (project in file("elastic4play"))
.enablePlugins(PlayScala)
.settings(
libraryDependencies ++= Seq(
cacheApi,
"com.sksamuel.elastic4s" %% "elastic4s-core" % elastic4sVersion,
"com.sksamuel.elastic4s" %% "elastic4s-http-streams" % elastic4sVersion,
"com.sksamuel.elastic4s" %% "elastic4s-client-esjava" % elastic4sVersion,
"com.typesafe.akka" %% "akka-stream-testkit" % play.core.PlayVersion.akkaVersion % Test,
"org.scalactic" %% "scalactic" % "3.2.19",
specs2 % Test
)

)


lazy val cortexWithDeps = (project in file("target/docker-withdeps"))
.dependsOn(cortex)
.enablePlugins(DockerPlugin)
Expand Down
Loading