-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: base implementation to provide the openness level in the config
issue: #218
- Loading branch information
1 parent
b68576a
commit aff116c
Showing
9 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
lapis2/src/main/kotlin/org/genspectrum/lapis/auth/DataOpennessAuthorizationFilter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package org.genspectrum.lapis.auth | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import jakarta.servlet.FilterChain | ||
import jakarta.servlet.http.HttpServletRequest | ||
import jakarta.servlet.http.HttpServletResponse | ||
import org.genspectrum.lapis.config.DatabaseConfig | ||
import org.genspectrum.lapis.config.OpennessLevel | ||
import org.genspectrum.lapis.controller.LapisHttpErrorResponse | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.http.MediaType | ||
import org.springframework.web.filter.OncePerRequestFilter | ||
|
||
abstract class DataOpennessAuthorizationFilter(val objectMapper: ObjectMapper) : OncePerRequestFilter() { | ||
override fun doFilterInternal( | ||
request: HttpServletRequest, | ||
response: HttpServletResponse, | ||
filterChain: FilterChain, | ||
) { | ||
when (val result = isAuthorizedForEndpoint(request)) { | ||
AuthorizationResult.Success -> filterChain.doFilter(request, response) | ||
is AuthorizationResult.Failure -> { | ||
response.status = HttpStatus.FORBIDDEN.value() | ||
response.contentType = MediaType.APPLICATION_JSON_VALUE | ||
response.writer.write( | ||
objectMapper.writeValueAsString( | ||
LapisHttpErrorResponse( | ||
"Forbidden", | ||
result.message, | ||
), | ||
), | ||
) | ||
} | ||
} | ||
} | ||
|
||
abstract fun isAuthorizedForEndpoint(request: HttpServletRequest): AuthorizationResult | ||
|
||
companion object { | ||
fun createFromConfig(databaseConfig: DatabaseConfig, objectMapper: ObjectMapper) = | ||
when (databaseConfig.schema.opennessLevel) { | ||
OpennessLevel.OPEN -> NoOpAuthorizationFilter(objectMapper) | ||
OpennessLevel.GISAID -> ProtectedGisaidDataAuthorizationFilter(objectMapper) | ||
} | ||
} | ||
} | ||
|
||
sealed interface AuthorizationResult { | ||
companion object { | ||
fun success(): AuthorizationResult = Success | ||
|
||
fun failure(message: String): AuthorizationResult = Failure(message) | ||
} | ||
|
||
fun isSuccessful(): Boolean | ||
|
||
object Success : AuthorizationResult { | ||
override fun isSuccessful() = true | ||
} | ||
|
||
class Failure(val message: String) : AuthorizationResult { | ||
override fun isSuccessful() = false | ||
} | ||
} | ||
|
||
private class NoOpAuthorizationFilter(objectMapper: ObjectMapper) : DataOpennessAuthorizationFilter(objectMapper) { | ||
override fun isAuthorizedForEndpoint(request: HttpServletRequest) = AuthorizationResult.success() | ||
} | ||
|
||
private class ProtectedGisaidDataAuthorizationFilter(objectMapper: ObjectMapper) : | ||
DataOpennessAuthorizationFilter(objectMapper) { | ||
|
||
override fun isAuthorizedForEndpoint(request: HttpServletRequest) = | ||
AuthorizationResult.failure("An access key is required to access this endpoint.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
lapis2/src/test/kotlin/org/genspectrum/lapis/auth/GisaidAuthorizationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package org.genspectrum.lapis.auth | ||
|
||
import com.ninjasquad.springmockk.MockkBean | ||
import io.mockk.MockKAnnotations | ||
import io.mockk.MockKMatcherScope | ||
import io.mockk.every | ||
import org.genspectrum.lapis.controller.LapisController | ||
import org.genspectrum.lapis.response.AggregatedResponse | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.http.MediaType | ||
import org.springframework.test.web.servlet.MockMvc | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers | ||
|
||
@SpringBootTest(properties = ["lapis.databaseConfig.path=src/test/resources/config/gisaidDatabaseConfig.yaml"]) | ||
@AutoConfigureMockMvc | ||
class GisaidAuthorizationTest(@Autowired val mockMvc: MockMvc) { | ||
|
||
@MockkBean | ||
lateinit var lapisController: LapisController | ||
|
||
private fun MockKMatcherScope.validControllerCall() = lapisController.aggregated(any()) | ||
private val validRoute = "/aggregated" | ||
|
||
@BeforeEach | ||
fun setUp() { | ||
every { validControllerCall() } returns AggregatedResponse(1) | ||
|
||
MockKAnnotations.init(this) | ||
} | ||
|
||
@Test | ||
fun `given no access key in request to GISAID instance, then access is denied`() { | ||
mockMvc.perform(MockMvcRequestBuilders.get(validRoute)) | ||
.andExpect(MockMvcResultMatchers.status().isForbidden) | ||
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect( | ||
MockMvcResultMatchers.content().json( | ||
""" | ||
{ | ||
"title": "Forbidden", | ||
"message": "An access key is required to access this endpoint." | ||
} | ||
""", | ||
), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
schema: | ||
instanceName: gisaidTestConfig | ||
opennessLevel: GISAID | ||
metadata: | ||
- name: pangoLineage | ||
type: pango_lineage | ||
primaryKey: pangoLineage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters