-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement EnvironmentVariablesCredentialsProvider
- Loading branch information
Showing
5 changed files
with
68 additions
and
2 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
12 changes: 12 additions & 0 deletions
12
src/main/kotlin/com/personio/synthetics/client/EnvironmentVariablesCredentialsProvider.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,12 @@ | ||
package com.personio.synthetics.client | ||
|
||
class EnvironmentVariablesCredentialsProvider() : CredentialsProvider { | ||
override fun getCredentials() = ApiCredentials(getValueFromEnvVar(DD_API_KEY), getValueFromEnvVar(DD_APP_KEY)) | ||
|
||
private fun getValueFromEnvVar(name: String) = requireNotNull(System.getenv(name).takeUnless(String::isNullOrBlank)) { "Environment variable $name not set" } | ||
|
||
companion object { | ||
internal const val DD_API_KEY = "DD_API_KEY" | ||
internal const val DD_APP_KEY = "DD_APP_KEY" | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...test/kotlin/com/personio/synthetics/client/EnvironmentVariablesCredentialsProviderTest.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,49 @@ | ||
package com.personio.synthetics.client | ||
|
||
import io.kotest.extensions.system.OverrideMode | ||
import io.kotest.extensions.system.withEnvironment | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.assertThrows | ||
|
||
internal class EnvironmentVariablesCredentialsProviderTest { | ||
@Test | ||
fun `getCredentials returns expected API credentials given the environment variables are set`() { | ||
val apiKeyValue = "apiKeyValue" | ||
val appKeyValue = "appKeyValue" | ||
val credentialsProvider = EnvironmentVariablesCredentialsProvider() | ||
withEnvironment( | ||
mapOf( | ||
EnvironmentVariablesCredentialsProvider.DD_API_KEY to apiKeyValue, | ||
EnvironmentVariablesCredentialsProvider.DD_APP_KEY to appKeyValue, | ||
), | ||
OverrideMode.SetOrOverride | ||
) { | ||
assertEquals(ApiCredentials(apiKeyValue, appKeyValue), credentialsProvider.getCredentials()) | ||
} | ||
} | ||
|
||
@Test | ||
fun `getCredentials throws exception given the DD_API_KEY environment variable is not set`() { | ||
val credentialsProvider = EnvironmentVariablesCredentialsProvider() | ||
withEnvironment( | ||
mapOf(EnvironmentVariablesCredentialsProvider.DD_APP_KEY to "appKeyValue"), | ||
OverrideMode.SetOrOverride | ||
) { | ||
val exception = assertThrows<IllegalArgumentException> { credentialsProvider.getCredentials() } | ||
assertEquals("Environment variable ${EnvironmentVariablesCredentialsProvider.DD_API_KEY} not set", exception.message) | ||
} | ||
} | ||
|
||
@Test | ||
fun `getCredentials throws exception given the DD_APP_KEY environment variable is not set`() { | ||
val credentialsProvider = EnvironmentVariablesCredentialsProvider() | ||
withEnvironment( | ||
mapOf(EnvironmentVariablesCredentialsProvider.DD_API_KEY to "apiKeyValue"), | ||
OverrideMode.SetOrOverride | ||
) { | ||
val exception = assertThrows<IllegalArgumentException> { credentialsProvider.getCredentials() } | ||
assertEquals("Environment variable ${EnvironmentVariablesCredentialsProvider.DD_APP_KEY} not set", exception.message) | ||
} | ||
} | ||
} |
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