Skip to content

Commit

Permalink
Test downloading vedlegg by signed url.
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvind-wedoe committed Jul 10, 2024
1 parent f6777c5 commit 13f4158
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
17 changes: 17 additions & 0 deletions src/main/kotlin/no/nav/klage/clients/FileClient.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package no.nav.klage.clients

import no.nav.klage.util.getLogger
import no.nav.klage.util.getSecureLogger
import no.nav.klage.util.logErrorResponse
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpStatusCode
import org.springframework.http.client.MultipartBodyBuilder
import org.springframework.stereotype.Component
import org.springframework.web.reactive.function.BodyInserters
Expand All @@ -17,6 +20,7 @@ class FileClient(
companion object {
@Suppress("JAVA_CLASS_ON_COMPANION")
private val logger = getLogger(javaClass.enclosingClass)
private val secureLogger = getSecureLogger()
}

//TODO: Rydd i fillageret nå som vi ikke lenger trenger det.
Expand Down Expand Up @@ -52,6 +56,19 @@ class FileClient(
.block() ?: throw RuntimeException("Attachment could not be fetched")
}

fun getVedleggFileAsSignedUrl(vedleggRef: String): String {
logger.debug("Fetching vedlegg file (signed URL) with vedlegg ref {}", vedleggRef)
return fileWebClient.get()
.uri { it.path("/attachment/{id}/signedurl").build(vedleggRef) }
.header(HttpHeaders.AUTHORIZATION, "Bearer ${azureADClient.klageFileApiOidcToken()}")
.retrieve()
.onStatus(HttpStatusCode::isError) { response ->
logErrorResponse(response, ::getVedleggFileAsSignedUrl.name, secureLogger)
}
.bodyToMono<String>()
.block()!!
}

fun deleteVedleggFile(vedleggRef: String): Boolean {
logger.debug("Deleting vedlegg file with vedlegg ref {}", vedleggRef)
val deletedInFileStore = fileWebClient.delete()
Expand Down
15 changes: 4 additions & 11 deletions src/main/kotlin/no/nav/klage/controller/KlankeController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import org.springframework.http.ResponseEntity
import org.springframework.http.codec.ServerSentEvent
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile
import org.springframework.web.servlet.ModelAndView
import reactor.core.publisher.Flux
import java.io.FileInputStream
import java.io.InputStream
Expand Down Expand Up @@ -338,7 +339,7 @@ class KlankeController(
fun getVedleggFromKlanke(
@PathVariable klankeId: UUID,
@PathVariable vedleggId: UUID
): ResponseEntity<ByteArray> {
): ModelAndView {
val bruker = brukerService.getBruker()
logger.debug("Get vedlegg to klanke is requested. KlankeId: {} - VedleggId: {}", klankeId, vedleggId)
secureLogger.debug(
Expand All @@ -348,16 +349,8 @@ class KlankeController(
bruker.folkeregisteridentifikator.identifikasjonsnummer
)

val content = vedleggService.getVedleggFromKlanke(klankeId, vedleggId, bruker)

val responseHeaders = HttpHeaders()
responseHeaders.contentType = MediaType.valueOf("application/pdf")
responseHeaders.add("Content-Disposition", "inline; filename=" + "vedlegg.pdf")
return ResponseEntity(
content,
responseHeaders,
HttpStatus.OK
)
val url = vedleggService.getVedleggFromKlankeAsSignedUrl(klankeId, vedleggId, bruker)
return ModelAndView("redirect:$url")
}

@ResponseBody
Expand Down
14 changes: 14 additions & 0 deletions src/main/kotlin/no/nav/klage/service/VedleggService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ class VedleggService(
}
}

fun getVedleggFromKlankeAsSignedUrl(klankeId: UUID, vedleggId: UUID, bruker: Bruker): String {
val existingKlanke = klankeRepository.findById(klankeId).get()
validationService.checkKlankeStatus(existingKlanke, false)
validationService.validateKlankeAccess(existingKlanke, bruker)

val vedlegg = existingKlanke.vedlegg.find { it.id == vedleggId }

if (vedlegg != null) {
return fileClient.getVedleggFileAsSignedUrl(vedlegg.ref)
} else {
throw RuntimeException("No vedlegg found with this id: $vedleggId")
}
}


private fun Klanke.attachmentsTotalSize() = this.vedlegg.sumOf { it.sizeInBytes }
}
13 changes: 12 additions & 1 deletion src/main/kotlin/no/nav/klage/util/LoggerUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,20 @@ package no.nav.klage.util

import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.web.reactive.function.client.ClientResponse
import reactor.core.publisher.Mono

fun getLogger(forClass: Class<*>): Logger = LoggerFactory.getLogger(forClass)
fun getSecureLogger(): Logger = LoggerFactory.getLogger("secure")

fun rootCause(t: Throwable): Throwable = t.cause?.run { rootCause(this) } ?: t
fun causeClass(t: Throwable) = t.stackTrace?.firstOrNull()?.className ?: ""
fun causeClass(t: Throwable) = t.stackTrace?.firstOrNull()?.className ?: ""

fun logErrorResponse(response: ClientResponse, functionName: String, logger: Logger): Mono<RuntimeException> {
return response.bodyToMono(String::class.java).map {
val errorString =
"Got ${response.statusCode()} when requesting $functionName - response body: '$it'"
logger.error(errorString)
RuntimeException(errorString)
}
}
2 changes: 1 addition & 1 deletion src/main/resources/application-dev-gcp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ FSS_CLUSTER: dev-fss
#STS_APIKEY
#PDL_APIKEY

DRAFT_CLEANUP_CRON: 0 13 13 * * *
DRAFT_CLEANUP_CRON: 0 13 13 * * THU
MAX_DRAFT_AGE_IN_DAYS: 90

TENANT_ID: 966ac572-f5b7-4bbe-aa88-c76419c0f851
Expand Down

0 comments on commit 13f4158

Please sign in to comment.