Skip to content

Commit

Permalink
Implement EnvironmentVariablesCredentialsProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
friduchin committed Aug 19, 2022
1 parent 2873de1 commit d720eb1
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 2 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased] - yyyy-mm-dd
### Added
Support features: status, advanced options for steps (timeout, allow failure and mark test as failed)

### Changed

Expand All @@ -16,6 +15,11 @@ Support features: status, advanced options for steps (timeout, allow failure and

### Security

## [1.0.2] - 2022-08-19
### Added
- Support features: status, advanced options for steps (timeout, allow failure and mark test as failed)
- EnvironmentVariablesCredentialsProvider to fetch Datadog API credentials from environment variables

## [1.0.1] - 2022-08-09
### Fixed
Fixing uploadFileStep function by adding element as a parameter
Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ dependencies {
testImplementation("org.junit.jupiter:junit-jupiter-engine:5.9.0")
testImplementation("org.mockito:mockito-inline:4.7.0")
testImplementation("org.mockito.kotlin:mockito-kotlin:4.0.0")
testImplementation("io.kotest:kotest-runner-junit5-jvm:5.4.1")
testImplementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.13.3")
}

Expand Down
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"
}
}
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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal class SyntheticsApiClientTest {
@Test
fun `server variables are set properly in the client`() {
val expectedServerVariables = mapOf("site" to TestConfig.DATADOG_API_HOST)
val credentialsProvider: AwsSecretsManagerCredentialsProvider = mock()
val credentialsProvider: CredentialsProvider = mock()
whenever(credentialsProvider.getCredentials()).thenReturn(ApiCredentials("apikey", "appkey"))
val apiClient = SyntheticsApiClient(credentialsProvider)
assertEquals(expectedServerVariables, apiClient.apiClient.serverVariables)
Expand Down

0 comments on commit d720eb1

Please sign in to comment.