Skip to content

Commit

Permalink
Bruk Aiven Redis overalt
Browse files Browse the repository at this point in the history
  • Loading branch information
bjerga committed Jan 20, 2025
1 parent 823feea commit 1ae2c37
Show file tree
Hide file tree
Showing 32 changed files with 112 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ import no.nav.helsearbeidsgiver.utils.log.logger
private val logger = "helsearbeidsgiver-im-aktiveorgnrservice".logger()

fun main() {
val redisConnection = RedisConnection(Env.redisUrl)
val redisConnection =
RedisConnection(
host = Env.redisHost,
port = Env.redisPort,
username = Env.redisUsername,
password = Env.redisPassword,
)

RapidApplication
.create(System.getenv())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ package no.nav.helsearbeidsgiver.inntektsmelding.aktiveorgnrservice
import no.nav.helsearbeidsgiver.felles.utils.fromEnv

object Env {
val redisUrl = "REDIS_URL".fromEnv()
val redisHost = "REDIS_HOST_INNTEKTSMELDING".fromEnv()
val redisPort = "REDIS_PORT_INNTEKTSMELDING".fromEnv().toInt()
val redisUsername = "REDIS_USERNAME_INNTEKTSMELDING".fromEnv()
val redisPassword = "REDIS_PASSWORD_INNTEKTSMELDING".fromEnv()
}
1 change: 0 additions & 1 deletion apps/api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ tasks {
test {
environment("IDPORTEN_WELL_KNOWN_URL", "http://localhost:6666/idporten-issuer/.well-known/openid-configuration")
environment("IDPORTEN_AUDIENCE", "aud-localhost")
environment("REDIS_URL", "redis://test_url:6379/0")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,31 +58,28 @@ object Routes {

fun main() {
val rapid = RapidApplication.create(System.getenv())
val aivenRedisConnection =
val redisConnection =
RedisConnection(
host = Env.Redis.host,
port = Env.Redis.port,
username = Env.Redis.username,
password = Env.Redis.password,
)
val redisConnection = RedisConnection(Env.Redis.url)

embeddedServer(
factory = Netty,
port = 8080,
module = { apiModule(rapid, aivenRedisConnection, redisConnection) },
module = { apiModule(rapid, redisConnection) },
).start(wait = true)

rapid
.registerShutdownLifecycle {
aivenRedisConnection.close()
redisConnection.close()
}.start()
}

fun Application.apiModule(
rapid: RapidsConnection,
aivenRedisConnection: RedisConnection,
redisConnection: RedisConnection,
) {
val tilgangskontroll =
Expand Down Expand Up @@ -121,8 +118,8 @@ fun Application.apiModule(

authenticate {
route(Routes.PREFIX) {
hentForespoersel(rapid, tilgangskontroll, aivenRedisConnection)
hentForespoerselIdListe(rapid, tilgangskontroll, aivenRedisConnection)
hentForespoersel(rapid, tilgangskontroll, redisConnection)
hentForespoerselIdListe(rapid, tilgangskontroll, redisConnection)
inntektRoute(rapid, tilgangskontroll, redisConnection)
inntektSelvbestemtRoute(rapid, tilgangskontroll, redisConnection)
innsending(rapid, tilgangskontroll, redisConnection)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,5 @@ object Env {
val port = "REDIS_PORT_INNTEKTSMELDING".fromEnv().toInt()
val username = "REDIS_USERNAME_INNTEKTSMELDING".fromEnv()
val password = "REDIS_PASSWORD_INNTEKTSMELDING".fromEnv()

val url = "REDIS_URL".fromEnv()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class AuthorizationTest : ApiTest() {
val path = "/test/auth"

application {
apiModule(mockk(relaxed = true), mockk(), mockk())
apiModule(mockk(relaxed = true), mockk())

routing {
authenticate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ abstract class ApiTest : MockAuthToken() {
fun testApi(block: suspend TestClient.() -> Unit): Unit =
testApplication {
application {
apiModule(mockk(relaxed = true), mockRedisConnection, mockRedisConnection)
apiModule(mockk(relaxed = true), mockRedisConnection)
}

val testClient = TestClient(this, ::mockAuthToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ import io.lettuce.core.api.sync.RedisCommands
import no.nav.helsearbeidsgiver.utils.collection.mapValuesNotNull

class RedisConnection(
redisUri: String,
host: String,
port: Int,
username: String,
password: String,
) {
constructor(
host: String,
port: Int,
username: String,
password: String,
) : this(
private val client: RedisClient =
RedisURI
.builder()
.withSsl(true)
Expand All @@ -23,11 +21,7 @@ class RedisConnection(
.withAuthentication(username, password)
.withDatabase(0)
.build()
.toURI()
.toString(),
)

private val client: RedisClient = RedisClient.create(redisUri)
.let(RedisClient::create)
private val connection: StatefulRedisConnection<String, String> = client.connect()
private val syncCommands: RedisCommands<String, String> = connection.sync()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package no.nav.helsearbeidsgiver.felles.test.mock

import io.lettuce.core.RedisClient
import io.lettuce.core.RedisURI
import io.lettuce.core.api.sync.RedisCommands
import io.mockk.every
import io.mockk.mockk
Expand All @@ -16,8 +17,8 @@ class MockRedis(
private val mockCommands = mockk<RedisCommands<String, String>>()
private val redis =
mockStatic(RedisClient::class) {
every { RedisClient.create(any<String>()) } returns mockRedisClient(mockCommands)
RedisConnection("")
every { RedisClient.create(any<RedisURI>()) } returns mockRedisClient(mockCommands)
RedisConnection("host", 0, "username", "password")
}

val store: RedisStore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package no.nav.helsearbeidsgiver.felles.test.mock

import io.lettuce.core.KeyValue
import io.lettuce.core.RedisClient
import io.lettuce.core.RedisURI
import io.lettuce.core.api.StatefulRedisConnection
import io.lettuce.core.api.sync.RedisCommands
import io.mockk.every
Expand All @@ -13,8 +14,8 @@ import no.nav.helsearbeidsgiver.utils.test.mock.mockStatic
fun redisWithMockRedisClient(mockStorageInit: Map<String, String?>): RedisConnection {
val mockCommands = mockk<RedisCommands<String, String>>().setupMock(mockStorageInit)
return mockStatic(RedisClient::class) {
every { RedisClient.create(any<String>()) } returns mockRedisClient(mockCommands)
RedisConnection("")
every { RedisClient.create(any<RedisURI>()) } returns mockRedisClient(mockCommands)
RedisConnection("host", 0, "username", "password")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ import no.nav.helsearbeidsgiver.utils.log.logger
private val logger = "helsearbeidsgiver-im-innsending".logger()

fun main() {
val redisConnection = RedisConnection(Env.redisUrl)
val redisConnection =
RedisConnection(
host = Env.redisHost,
port = Env.redisPort,
username = Env.redisUsername,
password = Env.redisPassword,
)

RapidApplication
.create(System.getenv())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ package no.nav.helsearbeidsgiver.inntektsmelding.innsending
import no.nav.helsearbeidsgiver.felles.utils.fromEnv

object Env {
val redisUrl = "REDIS_URL".fromEnv()
val redisHost = "REDIS_HOST_INNTEKTSMELDING".fromEnv()
val redisPort = "REDIS_PORT_INNTEKTSMELDING".fromEnv().toInt()
val redisUsername = "REDIS_USERNAME_INNTEKTSMELDING".fromEnv()
val redisPassword = "REDIS_PASSWORD_INNTEKTSMELDING".fromEnv()
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ import no.nav.helsearbeidsgiver.utils.log.logger
private val logger = "helsearbeidsgiver-im-inntektselvbestemtservice".logger()

fun main() {
val redisConnection = RedisConnection(Env.redisUrl)
val redisConnection =
RedisConnection(
host = Env.redisHost,
port = Env.redisPort,
username = Env.redisUsername,
password = Env.redisPassword,
)

RapidApplication
.create(System.getenv())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ package no.nav.helsearbeidsgiver.inntektsmelding.inntektselvbestemtservice
import no.nav.helsearbeidsgiver.felles.utils.fromEnv

object Env {
val redisUrl = "REDIS_URL".fromEnv()
val redisHost = "REDIS_HOST_INNTEKTSMELDING".fromEnv()
val redisPort = "REDIS_PORT_INNTEKTSMELDING".fromEnv().toInt()
val redisUsername = "REDIS_USERNAME_INNTEKTSMELDING".fromEnv()
val redisPassword = "REDIS_PASSWORD_INNTEKTSMELDING".fromEnv()
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ import no.nav.helsearbeidsgiver.utils.log.logger
private val logger = "helsearbeidsgiver-im-inntektservice".logger()

fun main() {
val redisConnection = RedisConnection(Env.redisUrl)
val redisConnection =
RedisConnection(
host = Env.redisHost,
port = Env.redisPort,
username = Env.redisUsername,
password = Env.redisPassword,
)

RapidApplication
.create(System.getenv())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ package no.nav.helsearbeidsgiver.inntektsmelding.inntektservice
import no.nav.helsearbeidsgiver.felles.utils.fromEnv

object Env {
val redisUrl = "REDIS_URL".fromEnv()
val redisHost = "REDIS_HOST_INNTEKTSMELDING".fromEnv()
val redisPort = "REDIS_PORT_INNTEKTSMELDING".fromEnv().toInt()
val redisUsername = "REDIS_USERNAME_INNTEKTSMELDING".fromEnv()
val redisPassword = "REDIS_PASSWORD_INNTEKTSMELDING".fromEnv()
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ abstract class ContainerTest {
private val topic = "helsearbeidsgiver.inntektsmelding"

private val kafkaContainer = KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:7.7.2"))
val redisContainer = RedisContainer(DockerImageName.parse("redis:7"))
val redisContainer = RedisContainer(DockerImageName.parse("redis:latest"))
val postgresContainerOne = postgresContainer()
val postgresContainerTwo = postgresContainer()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package no.nav.helsearbeidsgiver.inntektsmelding.integrasjonstest.utils
import com.github.navikt.tbd_libs.rapids_and_rivers.JsonMessage
import com.github.navikt.tbd_libs.rapids_and_rivers_api.MessageProblems
import io.kotest.matchers.nulls.shouldNotBeNull
import io.lettuce.core.RedisClient
import io.lettuce.core.RedisURI
import io.micrometer.core.instrument.simple.SimpleMeterRegistry
import io.mockk.clearAllMocks
import io.mockk.coEvery
Expand Down Expand Up @@ -70,6 +72,7 @@ import no.nav.helsearbeidsgiver.utils.json.parseJson
import no.nav.helsearbeidsgiver.utils.json.toJson
import no.nav.helsearbeidsgiver.utils.test.date.august
import no.nav.helsearbeidsgiver.utils.test.date.mai
import no.nav.helsearbeidsgiver.utils.test.mock.mockStatic
import no.nav.helsearbeidsgiver.utils.test.wrapper.genererGyldig
import no.nav.helsearbeidsgiver.utils.wrapper.Fnr
import org.intellij.lang.annotations.Language
Expand Down Expand Up @@ -156,7 +159,11 @@ abstract class EndToEndTest : ContainerTest() {
return@lazy withRetries(
feilmelding = "Klarte ikke koble til Redis.",
) {
RedisConnection(redisContainer.redisURI)
// Hijacker RedisClient her pga. vanskeligheter med å sette opp RedisContainer med SSL og autentisering
mockStatic(RedisClient::class) {
every { RedisClient.create(any<RedisURI>()) } returns RedisClient.create(redisContainer.redisURI)
RedisConnection("host", 0, "username", "password")
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ import no.nav.helsearbeidsgiver.utils.log.logger
private val logger = "im-selvbestemt-hent-im-service".logger()

fun main() {
val redisConnection = RedisConnection(Env.redisUrl)
val redisConnection =
RedisConnection(
host = Env.redisHost,
port = Env.redisPort,
username = Env.redisUsername,
password = Env.redisPassword,
)

RapidApplication
.create(System.getenv())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ package no.nav.helsearbeidsgiver.inntektsmelding.selvbestemthentimservice
import no.nav.helsearbeidsgiver.felles.utils.fromEnv

object Env {
val redisUrl = "REDIS_URL".fromEnv()
val redisHost = "REDIS_HOST_INNTEKTSMELDING".fromEnv()
val redisPort = "REDIS_PORT_INNTEKTSMELDING".fromEnv().toInt()
val redisUsername = "REDIS_USERNAME_INNTEKTSMELDING".fromEnv()
val redisPassword = "REDIS_PASSWORD_INNTEKTSMELDING".fromEnv()
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ import no.nav.helsearbeidsgiver.utils.log.logger
private val logger = "im-selvbestemt-lagre-im-service".logger()

fun main() {
val redisConnection = RedisConnection(Env.redisUrl)
val redisConnection =
RedisConnection(
host = Env.redisHost,
port = Env.redisPort,
username = Env.redisUsername,
password = Env.redisPassword,
)

RapidApplication
.create(System.getenv())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ package no.nav.helsearbeidsgiver.inntektsmelding.selvbestemtlagreimservice
import no.nav.helsearbeidsgiver.felles.utils.fromEnv

object Env {
val redisUrl = "REDIS_URL".fromEnv()
val redisHost = "REDIS_HOST_INNTEKTSMELDING".fromEnv()
val redisPort = "REDIS_PORT_INNTEKTSMELDING".fromEnv().toInt()
val redisUsername = "REDIS_USERNAME_INNTEKTSMELDING".fromEnv()
val redisPassword = "REDIS_PASSWORD_INNTEKTSMELDING".fromEnv()
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ import no.nav.helsearbeidsgiver.utils.log.logger
private val logger = "helsearbeidsgiver-im-tilgangservice".logger()

fun main() {
val redisConnection = RedisConnection(Env.redisUrl)
val redisConnection =
RedisConnection(
host = Env.redisHost,
port = Env.redisPort,
username = Env.redisUsername,
password = Env.redisPassword,
)

RapidApplication
.create(System.getenv())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ package no.nav.helsearbeidsgiver.inntektsmelding.tilgangservice
import no.nav.helsearbeidsgiver.felles.utils.fromEnv

object Env {
val redisUrl = "REDIS_URL".fromEnv()
val redisHost = "REDIS_HOST_INNTEKTSMELDING".fromEnv()
val redisPort = "REDIS_PORT_INNTEKTSMELDING".fromEnv().toInt()
val redisUsername = "REDIS_USERNAME_INNTEKTSMELDING".fromEnv()
val redisPassword = "REDIS_PASSWORD_INNTEKTSMELDING".fromEnv()
}
6 changes: 1 addition & 5 deletions config/aktiveorgnrservice/dev-gcp.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
kafkaPool: nav-dev
env:
- name: REDIS_URL
value: redis://helsearbeidsgiver-redis.helsearbeidsgiver.svc.cluster.local:6379/0
apps:
- name: helsearbeidsgiver-redis
redisAccess: readwrite
7 changes: 1 addition & 6 deletions config/api/dev-gcp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@ kafkaPool: nav-dev
idportenEnabled: true
azure:
enabled: true
ingress: https://helsearbeidsgiver-im-api.intern.dev.nav.no
redisAccess: read
env:
- name: REDIS_URL
value: redis://helsearbeidsgiver-redis.helsearbeidsgiver.svc.cluster.local:6379/0
apps:
- name: helsearbeidsgiver-redis
ingress: https://helsearbeidsgiver-im-api.intern.dev.nav.no
inboundApps:
- name: spinntektsmelding-frontend

9 changes: 1 addition & 8 deletions config/innsending/dev-gcp.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,2 @@
kafkaPool: nav-dev
azure:
enabled: true
ingress: https://helsearbeidsgiver-im-innsending.intern.dev.nav.no
env:
- name: REDIS_URL
value: redis://helsearbeidsgiver-redis.helsearbeidsgiver.svc.cluster.local:6379/0
apps:
- name: helsearbeidsgiver-redis
redisAccess: readwrite
Loading

0 comments on commit 1ae2c37

Please sign in to comment.