Skip to content

Commit

Permalink
feat: authentication on the solr client
Browse files Browse the repository at this point in the history
  • Loading branch information
jachro committed Feb 15, 2024
1 parent 60924fe commit e8fc222
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import cats.syntax.all.*
import ciris.*
import io.renku.queue.client.QueueName
import io.renku.redis.client.RedisUrl
import io.renku.solr.client.SolrConfig
import io.renku.solr.client.{SolrConfig, SolrUser}
import org.http4s.Uri

import scala.concurrent.duration.FiniteDuration
Expand All @@ -43,12 +43,17 @@ object ConfigValues extends ConfigDecoders:
val solrConfig: ConfigValue[Effect, SolrConfig] = {
val url = env(s"${prefix}_SOLR_URL").default("http://localhost:8983/solr").as[Uri]
val core = env(s"${prefix}_SOLR_CORE").default("search-core-test")
val maybeUser =
(env(s"${prefix}_SOLR_USER").option -> env(s"${prefix}_SOLR_PASS").option)
.mapN { case (maybeUsername, maybePass) =>
(maybeUsername, maybePass).mapN(SolrUser.apply)
}
val defaultCommit =
env(s"${prefix}_SOLR_DEFAULT_COMMIT_WITHIN")
.default("0 seconds")
.as[FiniteDuration]
.option
val logMessageBodies =
env(s"${prefix}_SOLR_LOG_MESSAGE_BODIES").default("false").as[Boolean]
(url, core, defaultCommit, logMessageBodies).mapN(SolrConfig.apply)
(url, core, maybeUser, defaultCommit, logMessageBodies).mapN(SolrConfig.apply)
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import io.renku.search.http.borer.BorerEntityJsonCodec
import io.renku.search.http.{HttpClientDsl, ResponseLogging}
import io.renku.solr.client.schema.{SchemaCommand, SchemaJsonCodec}
import org.http4s.client.Client
import org.http4s.{Method, Uri}
import org.http4s.{BasicCredentials, Method, Uri}

import scala.concurrent.duration.Duration

Expand All @@ -39,27 +39,29 @@ private class SolrClientImpl[F[_]: Async](config: SolrConfig, underlying: Client
private[this] val solrUrl: Uri = config.baseUrl / config.core

def modifySchema(cmds: Seq[SchemaCommand], onErrorLog: ResponseLogging): F[Unit] =
val req = Method.POST(cmds, (solrUrl / "schema").withQueryParam("commit", "true"))
val req = Method
.POST(cmds, (solrUrl / "schema").withQueryParam("commit", "true"))
.withBasicAuth(credentials)
underlying.expectOr[String](req)(onErrorLog(logger, req)).void

def query[A: Decoder](q: QueryString): F[QueryResponse[A]] =
query[A](QueryData(q))

def query[A: Decoder](query: QueryData): F[QueryResponse[A]] =
val req = Method.POST(query, solrUrl / "query")
val req = Method.POST(query, solrUrl / "query").withBasicAuth(credentials)
underlying
.expectOr[QueryResponse[A]](req)(ResponseLogging.Error(logger, req))
.flatTap(r => logger.trace(s"Query response: $r"))

def delete(q: QueryString): F[Unit] =
val req = Method.POST(DeleteRequest(q.q), makeUpdateUrl)
val req = Method.POST(DeleteRequest(q.q), makeUpdateUrl).withBasicAuth(credentials)
underlying
.expectOr[InsertResponse](req)(ResponseLogging.Error(logger, req))
.flatTap(r => logger.trace(s"Solr delete response: $r"))
.void

def insert[A: Encoder](docs: Seq[A]): F[InsertResponse] =
val req = Method.POST(docs, makeUpdateUrl)
val req = Method.POST(docs, makeUpdateUrl).withBasicAuth(credentials)
underlying
.expectOr[InsertResponse](req)(ResponseLogging.Error(logger, req))
.flatTap(r => logger.trace(s"Solr inserted response: $r"))
Expand All @@ -71,3 +73,6 @@ private class SolrClientImpl[F[_]: Async](config: SolrConfig, underlying: Client
case Some(d) => base.withQueryParam("commitWithin", d.toMillis)
case None => base
}

private lazy val credentials: Option[BasicCredentials] =
config.maybeUser.map(u => BasicCredentials(u.username, u.password))
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import scala.concurrent.duration.FiniteDuration
final case class SolrConfig(
baseUrl: Uri,
core: String,
maybeUser: Option[SolrUser],
commitWithin: Option[FiniteDuration],
logMessageBodies: Boolean
)

final case class SolrUser(username: String, password: String)
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ trait SolrSpec:
protected lazy val solrConfig: SolrConfig = SolrConfig(
Uri.unsafeFromString(server.url) / "solr",
server.genericCoreName,
maybeUser = None,
commitWithin = Some(Duration.Zero),
logMessageBodies = true
)
Expand Down

0 comments on commit e8fc222

Please sign in to comment.