Skip to content

Commit

Permalink
PHDO-468 - Fixing issues with latest version of code found in dev dep…
Browse files Browse the repository at this point in the history
…loyments (#300)

* Fixed issues with schema validator S3 configuration health checks

* Additional fixed to schema validator health checks

---------

Co-authored-by: Matt B Krystof <[email protected]>
  • Loading branch information
mkrystof and Matt B Krystof authored Jan 21, 2025
1 parent dc67ff4 commit 7e969e1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@

package gov.cdc.ocio.reportschemavalidator.health.schemaLoadersystem

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import gov.cdc.ocio.reportschemavalidator.utils.AWSS3Configuration
import gov.cdc.ocio.types.health.HealthCheckResult
import gov.cdc.ocio.types.health.HealthCheckSystem
import gov.cdc.ocio.types.health.HealthStatusType
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import software.amazon.awssdk.services.s3.S3Client
import software.amazon.awssdk.services.s3.model.ListObjectsV2Request
import software.amazon.awssdk.services.s3.model.ListBucketsRequest


/**
* Concrete implementation of the S3 Bucket health checks.
*/
@JsonIgnoreProperties("koin")
class HealthCheckS3Bucket(private val s3Client: S3Client) : HealthCheckSystem("s3"), KoinComponent {

private val awsServiceConfiguration by inject<AWSS3Configuration>()
class HealthCheckS3Bucket(
private val getS3ClientFunc: () -> S3Client,
private val s3Bucket: String,
) : HealthCheckSystem("s3") {

/**
* Checks and sets S3 Bucket accessible status
* @return HealthCheckResult
*/
override fun doHealthCheck(): HealthCheckResult {
val result = isS3FolderHealthy(awsServiceConfiguration)
val result = isS3FolderHealthy()
result.onFailure { error ->
val reason = "S3 bucket is not accessible and hence not healthy ${error.localizedMessage}"
val reason = "S3 bucket is not accessible and hence not healthy: ${error.localizedMessage}"
logger.error(reason)
return HealthCheckResult(service, HealthStatusType.STATUS_DOWN, reason)
}
Expand All @@ -37,22 +33,21 @@ class HealthCheckS3Bucket(private val s3Client: S3Client) : HealthCheckSystem("s
/**
* Check whether S3 Buket is accessible
*
* @param config AWSS3Configuration
* @return Result<Boolean>
*/
private fun isS3FolderHealthy(config: AWSS3Configuration): Result<Boolean> {
private fun isS3FolderHealthy(): Result<Boolean> {
return try {
val request = ListObjectsV2Request.builder()
.bucket(config.s3Bucket)
.maxKeys(1) // one file - lightweight check
val s3Client = getS3ClientFunc()
val request = ListBucketsRequest.builder()
.build()
val response = s3Client.listObjectsV2(request)
if (response.contents().isNotEmpty())
val response = s3Client.listBuckets(request)
s3Client.close()
if (response.buckets().any { it.name() == s3Bucket })
Result.success(true)
else
Result.failure(Exception("Established connection to S3 bucket, but failed list objects check."))
Result.failure(Exception("Established connection to S3, but failed to verify the expected bucket exists."))
} catch (e: Exception) {
throw Exception("Failed to establish connection to S3 bucket.")
Result.failure(Exception("Failed to establish connection to S3 bucket: ${e.localizedMessage}"))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ class S3SchemaStorageClient(
*/
private fun getS3Client(): S3Client {

val credentialsProvider = if (roleArn.isNullOrEmpty() ||
webIdentityTokenFile.isNullOrEmpty()) {
val credentialsProvider = if (roleArn.isNullOrEmpty() ||
webIdentityTokenFile.isNullOrEmpty()
) {
// Fallback to default credentials provider (access key and secret)
DefaultCredentialsProvider.create()
} else {
Expand All @@ -41,6 +42,7 @@ class S3SchemaStorageClient(
.webIdentityTokenFile(webIdentityTokenFile.let { Path.of(it) })
.build()
}

// Load credentials from the AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and AWS_SESSION_TOKEN environment variables.
return S3Client.builder()
.region(Region.of(region))
Expand All @@ -67,6 +69,8 @@ class S3SchemaStorageClient(
.readAllBytes()
.decodeToString()

s3Client.close()

return result
}

Expand Down Expand Up @@ -150,5 +154,5 @@ class S3SchemaStorageClient(
return getSchemaContent("$schemaName.$schemaVersion.schema.json")
}

override var healthCheckSystem = HealthCheckS3Bucket(getS3Client()) as HealthCheckSystem
override var healthCheckSystem = HealthCheckS3Bucket(::getS3Client, bucketName) as HealthCheckSystem
}

This file was deleted.

0 comments on commit 7e969e1

Please sign in to comment.