Skip to content

Commit

Permalink
Fixes #107 - Defining the test case
Browse files Browse the repository at this point in the history
  • Loading branch information
TebaleloS committed Nov 30, 2023
1 parent fc3972c commit a96e577
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,30 @@ import software.amazon.awssdk.regions.Region
import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient
import software.amazon.awssdk.services.secretsmanager.model.{GetSecretValueRequest, SecretsManagerException}

object RetrieveAwsSecret {
def retrieveAwsSecret(): Unit = {
val secretName = "atum_user"

// Create a Secrets Manager client
val client = SecretsManagerClient.builder()
.region(Region.AF_SOUTH_1)
.build()
class RetrieveAwsSecret {
private val secretsManagerClient: SecretsManagerClient = SecretsManagerClient.builder()
.region(Region.AF_SOUTH_1)
.build()

def retrieveAwsSecret(secretName: String = "atum_user"): Seq[String] = {
try {
val request = GetSecretValueRequest.builder()
.secretId(secretName)
.build()

val response = client.getSecretValue(request)
val response = secretsManagerClient.getSecretValue(request)

response.secretString.foreach { secretString =>
println(s"Secret Key: $secretString")
}
response.secretString.map(_.toString)
} catch {
case e: SecretsManagerException =>
println(s"Error retrieving secret key: ${e.getMessage}")
e.getMessage.map(_.toString)
} finally {
// Close the client when done
client.close()
secretsManagerClient.close()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2021 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package za.co.absa.atum.server.api


import org.scalatest.BeforeAndAfterEach
import org.scalatest.flatspec.AnyFlatSpec
import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient
import software.amazon.awssdk.services.secretsmanager.model.{GetSecretValueRequest, GetSecretValueResponse, SecretsManagerException}

class RetrieveAwsSecretTest extends AnyFlatSpec with BeforeAndAfterEach{

class MockSecretsManagerClient extends SecretsManagerClient {
override def getSecretValue(request: GetSecretValueRequest): GetSecretValueResponse = {
// Simulate a response from AWS Secrets Manager
if (request.secretId() == "ValidSecretName") {
GetSecretValueResponse.builder().secretString("ValidSecretKey").build()
} else {
throw SecretsManagerException.builder().message("Invalid secret ID").build()
}
}

override def serviceName(): String = "SecretManager"

override def close(): Unit = close()
}

var extractor: RetrieveAwsSecret = _

override def beforeEach(): Unit = {
// Initialize the object with a mock client for testing
val mockClient = new MockSecretsManagerClient()
extractor = new RetrieveAwsSecret()
}

"AWSSecretKeyExtractor" should "retrieve secret key from AWS Secrets Manager" in {
val secretName = "ValidSecretName"
val secretKey = extractor.retrieveAwsSecret(secretName)
println("Secret key: ", secretKey)
assert(secretKey == Some("ValidSecretKey"))
}

it should "return None for an invalid secret name" in {
val secretName = "InvalidSecretName"
val secretKey = extractor.retrieveAwsSecret(secretName)
println("Secret key: " + secretKey.map(_.toSeq))
}
}

0 comments on commit a96e577

Please sign in to comment.