Skip to content

Scala build korjaus #397

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

Merged
merged 6 commits into from
Nov 13, 2024
Merged
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
13 changes: 3 additions & 10 deletions parser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,10 @@ https://eu-west-1.console.aws.amazon.com/cloudwatch/home?region=eu-west-1#logsV2
Lambda-funktion tarvitsemat DynamoDB ja SQS ajetaan Docker-konteissa. `Makefile` sisältää komennot, joilla kontit voidaan käynnistää ja pysäyttää:

``` shell
make dynamodb-start
make sqs-start
make localstack
./createqueue.sh

make dynamodb-stop
make sqs-stop
```

Molemmat voidaan käynnistää ja pysäyttää myös näin:
```shell
make docker-up
make docker-down
make localstack-stop
```

Katso alempaa muutamia [hyödyllisiä komentoja Docker-konttien kanssa operoimiseen](#paikallisten-docker-konttien-kanssa-operoiminen).
Expand Down
10 changes: 5 additions & 5 deletions parser/create-buildversion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ set -euo pipefail

if [ -z "$2" ]; then
echo "Usage: create-buildversion.sh file version"
exit 1
exit 1
fi

file="$1"
version="$2"

if [ "$version" = "1.0-SNAPSHOT" ]; then
version="local"
fi
fi

cat >$file <<EOL
cat >"$file" <<EOL
artifactId=oma-opintopolku-loki/parser
version=$version
vcsRevision=`git rev-parse HEAD`
buildDate=`date`
vcsRevision=$(git rev-parse HEAD)
buildDate=$(date)
EOL
31 changes: 13 additions & 18 deletions parser/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,21 @@
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<encoding>UTF-8</encoding>
<scala.version>2.12.16</scala.version>
<scala.version>2.12.18</scala.version>
<scala.compat.version>2.12</scala.compat.version>
<ch.qos.logback.contrib.version>0.1.5</ch.qos.logback.contrib.version>
<http4s.version>0.23.13</http4s.version>
<http4s.version>0.23.16</http4s.version>
<org.json4s.version>4.0.6</org.json4s.version>
<com.amazonaws.version>1.12.772</com.amazonaws.version>
<spec2.version>4.16.1</spec2.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>

<!-- Internal dependencies -->
<dependency>
<groupId>fi.vm.sade</groupId>
<artifactId>scala-cas_2.12</artifactId>
<version>3.0.2-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>org.http4s</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- https://mvnrepository.com/artifact/org.http4s/http4s-blaze-client -->
<!-- https://mvnrepository.com/artifact/org.http4s/http4s-ember-client -->
<dependency>
<groupId>org.http4s</groupId>
<artifactId>http4s-blaze-client_2.12</artifactId>
<artifactId>http4s-ember-client_2.12</artifactId>
<version>${http4s.version}</version>
</dependency>

Expand Down Expand Up @@ -88,6 +75,12 @@
<version>${scala.version}</version>
</dependency>

<dependency>
<groupId>org.scala-lang.modules</groupId>
<artifactId>scala-xml_2.12</artifactId>
<version>2.0.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/io.symphonia/lambda-logging -->
<dependency>
<groupId>io.symphonia</groupId>
Expand Down Expand Up @@ -301,6 +294,7 @@
<exclude>junit:junit</exclude>
<exclude>jmock:*</exclude>
<exclude>org.apache.maven:lib:tests</exclude>
<exclude>module-info.java</exclude>
</excludes>
</artifactSet>
</configuration>
Expand All @@ -322,6 +316,7 @@
<args>
<arg>-dependencyfile</arg>
<arg>${project.build.directory}/.scala_dependencies</arg>
<arg>-deprecation</arg>
</args>
</configuration>
</execution>
Expand Down
10 changes: 4 additions & 6 deletions parser/src/main/scala/fi/oph/omaopintopolkuloki/http/Http.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package fi.oph.omaopintopolkuloki.http
import cats.effect.IO
import cats.effect.unsafe.IORuntime
import fi.oph.omaopintopolkuloki.conf.Configuration._
import org.http4s.blaze.client.BlazeClientBuilder
import org.http4s.ember.client.EmberClientBuilder
import org.http4s.client.Client
import org.http4s.{Request, Uri}

import scala.concurrent.ExecutionContext.global

trait HttpClient {
type Decode[ResultType] = (Int, String, Request[IO]) => ResultType

Expand All @@ -19,9 +17,9 @@ object Http {
implicit private val runtime: IORuntime = cats.effect.unsafe.IORuntime.global

def apply(useCas: Boolean = false): HttpClient = {
val client = BlazeClientBuilder[IO](global)
.withMaxTotalConnections(maxRequestThreads)
.allocated
val client = EmberClientBuilder.default[IO]
.withMaxTotal(maxRequestThreads)
.build.allocated
.map(_._1)
.unsafeRunSync()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package fi.vm.sade.utils.cas

import cats.effect.IO
import cats.effect.kernel.Resource
import cats.effect.std.Hotswap
import fi.vm.sade.utils.cas.CasClient.SessionCookie
import org.http4s.client.Client
import org.http4s.{Request, Response, Status}
import org.typelevel.ci.CIString


/**
* Middleware that handles CAS authentication automatically. Sessions are maintained by keeping
* a central cache of session cookies per service url. If a session cookie is not found for requested service, it is obtained using
* CasClient. Stale sessions are detected and refreshed automatically.
*/
object CasAuthenticatingClient extends Logging {
val DefaultSessionCookieName = "JSESSIONID"

private val sessions: collection.mutable.Map[CasParams, SessionCookie] = collection.mutable.Map.empty

def apply(
casClient: CasClient,
casParams: CasParams,
serviceClient: Client[IO],
clientCallerId: String,
sessionCookieName: String = DefaultSessionCookieName
): Client[IO] = {
def openWithCasSession(request: Request[IO], hotswap: Hotswap[IO, Response[IO]]): IO[Response[IO]] = {
getCasSession(casParams).flatMap(requestWithCasSession(request, hotswap, retry = true))
}

def requestWithCasSession
(request: Request[IO], hotswap: Hotswap[IO, Response[IO]], retry: Boolean)
(sessionCookie: SessionCookie)
: IO[Response[IO]] = {
val fullRequest = FetchHelper.addDefaultHeaders(
request.addCookie(sessionCookieName, sessionCookie),
clientCallerId
)
// Hotswap use inspired by http4s Retry middleware:
hotswap.swap(serviceClient.run(fullRequest)).flatMap {
case r: Response[IO] if sessionExpired(r) && retry =>
logger.info("Session for " + casParams + " expired")
refreshSession(casParams).flatMap(requestWithCasSession(request, hotswap, retry = false))
case r: Response[IO] => IO.pure(r)
}
}

def isRedirectToLogin(resp: Response[IO]): Boolean =
resp.headers.get(CIString("Location")).exists(_.exists(header =>
header.value.contains("/cas/login") || header.value.contains("/cas-oppija/login")
))

def sessionExpired(resp: Response[IO]): Boolean =
isRedirectToLogin(resp) || resp.status == Status.Unauthorized

def getCasSession(params: CasParams): IO[SessionCookie] = {
synchronized(sessions.get(params)) match {
case None =>
logger.debug(s"No existing $sessionCookieName found for " + params + ", creating new")
refreshSession(params)
case Some(session) =>
IO.pure(session)
}
}

def refreshSession(params: CasParams): IO[SessionCookie] = {
casClient.fetchCasSession(params, sessionCookieName).map { session =>
logger.debug("Storing new session for " + params)
synchronized(sessions.put(params, session))
session
}
}

Client { req =>
Hotswap.create[IO, Response[IO]].flatMap { hotswap =>
Resource.eval(openWithCasSession(req, hotswap))
}
}
}
}
Loading