-
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: get access key from request and read valid access keys from file
issue: #218
- Loading branch information
1 parent
b86cc1d
commit 9cb7953
Showing
9 changed files
with
191 additions
and
34 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
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
23 changes: 23 additions & 0 deletions
23
lapis2/src/main/kotlin/org/genspectrum/lapis/config/AccessKeys.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,23 @@ | ||
package org.genspectrum.lapis.config | ||
|
||
import com.fasterxml.jackson.module.kotlin.readValue | ||
import org.genspectrum.lapis.util.YamlObjectMapper | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.stereotype.Component | ||
import java.io.File | ||
|
||
@Component | ||
class AccessKeysReader( | ||
@Value("\${lapis.accessKeys.path:#{null}}") private val accessKeysFile: String?, | ||
private val yamlObjectMapper: YamlObjectMapper, | ||
) { | ||
fun read(): AccessKeys { | ||
if (accessKeysFile == null) { | ||
throw IllegalArgumentException("Cannot read LAPIS access keys, lapis.accessKeys.path was not set.") | ||
} | ||
|
||
return yamlObjectMapper.objectMapper.readValue(File(accessKeysFile)) | ||
} | ||
} | ||
|
||
data class AccessKeys(val fullAccessKey: String, val aggregatedDataAccessKey: String) |
11 changes: 11 additions & 0 deletions
11
lapis2/src/main/kotlin/org/genspectrum/lapis/util/YamlObjectMapper.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,11 @@ | ||
package org.genspectrum.lapis.util | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory | ||
import com.fasterxml.jackson.module.kotlin.registerKotlinModule | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
object YamlObjectMapper { | ||
val objectMapper: ObjectMapper = ObjectMapper(YAMLFactory()).registerKotlinModule() | ||
} |
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
36 changes: 36 additions & 0 deletions
36
lapis2/src/test/kotlin/org/genspectrum/lapis/config/AccessKeysReaderTest.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,36 @@ | ||
package org.genspectrum.lapis.config | ||
|
||
import org.hamcrest.MatcherAssert.assertThat | ||
import org.hamcrest.Matchers.equalTo | ||
import org.hamcrest.Matchers.`is` | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.assertThrows | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.test.context.ActiveProfiles | ||
|
||
@SpringBootTest | ||
class AccessKeysReaderTest { | ||
@Autowired | ||
lateinit var underTest: AccessKeysReader | ||
|
||
@Test | ||
fun `given access keys file path as property then should successfully read access keys`() { | ||
val result = underTest.read() | ||
|
||
assertThat(result.fullAccessKey, `is`(equalTo("testFullAccessKey"))) | ||
assertThat(result.aggregatedDataAccessKey, `is`(equalTo("testAggregatedDataAccessKey"))) | ||
} | ||
} | ||
|
||
@SpringBootTest | ||
@ActiveProfiles("testWithoutAccessKeys") | ||
class AccessKeysReaderWithPathNotSetTest { | ||
@Autowired | ||
lateinit var underTest: AccessKeysReader | ||
|
||
@Test | ||
fun `given access keys file path property is not set then should throw exception when reading access keys`() { | ||
assertThrows<IllegalArgumentException> { underTest.read() } | ||
} | ||
} |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
silo.url=http://url.to.silo | ||
lapis.databaseConfig.path=src/test/resources/config/testDatabaseConfig.yaml | ||
lapis.accessKeys.path=src/test/resources/config/testAccessKeys.yaml |
4 changes: 4 additions & 0 deletions
4
lapis2/src/test/resources/application-testWithoutAccessKeys.properties
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,4 @@ | ||
spring.config.import=file:src/main/resources/application.properties | ||
|
||
silo.url=http://url.to.silo | ||
lapis.databaseConfig.path=src/test/resources/config/testDatabaseConfig.yaml |
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,2 @@ | ||
fullAccessKey: testFullAccessKey | ||
aggregatedDataAccessKey: testAggregatedDataAccessKey |