Skip to content

Commit

Permalink
fix: Skip Firebase init in test profile
Browse files Browse the repository at this point in the history
  • Loading branch information
cybersokari committed May 19, 2024
1 parent fdc97ba commit b2b7297
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: test.yml
name: Test
on:
pull_request:
branches:
Expand Down
51 changes: 25 additions & 26 deletions src/main/kotlin/ng/cove/web/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import org.springframework.cache.annotation.EnableCaching
import org.springframework.cache.caffeine.CaffeineCacheManager
import org.springframework.context.ApplicationListener
import org.springframework.context.annotation.Bean
import org.springframework.core.env.StandardEnvironment
import org.springframework.scheduling.annotation.EnableAsync
import org.springframework.scheduling.annotation.EnableScheduling
import java.util.concurrent.TimeUnit
Expand Down Expand Up @@ -45,35 +44,35 @@ fun main(args: Array<String>) {

val startedEvent = ApplicationListener<ApplicationStartedEvent> { event ->

val profiles = StandardEnvironment().activeProfiles
val profiles = event.applicationContext.environment.activeProfiles
// profiles is sometimes empty when running on a production server because it
// has not been loaded from the application.properties file at this point
val runningOnProd = profiles.isEmpty() || profiles[0] == "prod"
val profile = profiles.getOrNull(0) ?: "prod"

if (!runningOnProd) {
// Init Firebase with Application default credentials from Gcloud or Firebase CLI
FirebaseApp.initializeApp()
} else {
when (profile) {
"test" -> {}
"dev" -> FirebaseApp.initializeApp()
"prod" -> {
// Get secrets from GCP
val gcpProjectId = "gatedaccessdev"
val firebaseSecret = SecretVersionName.of(gcpProjectId, "firebase-service-account", "1")
val termiiSecret = SecretVersionName.of(gcpProjectId, "termii-key", "1")

// Get secrets from GCP
val gcpProjectId = "gatedaccessdev"
val firebaseSecret = SecretVersionName.of(gcpProjectId, "firebase-service-account", "1")
val termiiSecret = SecretVersionName.of(gcpProjectId, "termii-key", "1")

// Auto-closable
SecretManagerServiceClient.create().use {
// Set Termii API Key to service
val termiiSecretPayload =
it.accessSecretVersion(termiiSecret).payload.data.toByteArray().inputStream()
val smsOtpService = event.applicationContext.getBean(SmsOtpService::class.java)
smsOtpService.termiiApiKey = String(termiiSecretPayload.readAllBytes())
// Init Firebase with service account
val serviceAccountStream =
it.accessSecretVersion(firebaseSecret).payload.data.toByteArray().inputStream()
val options = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccountStream))
.build()
FirebaseApp.initializeApp(options)
// Auto-closable
SecretManagerServiceClient.create().use {
// Set Termii API Key to service
val termiiSecretPayload =
it.accessSecretVersion(termiiSecret).payload.data.toByteArray().inputStream()
val smsOtpService = event.applicationContext.getBean(SmsOtpService::class.java)
smsOtpService.termiiApiKey = String(termiiSecretPayload.readAllBytes())
// Init Firebase with service account
val serviceAccountStream =
it.accessSecretVersion(firebaseSecret).payload.data.toByteArray().inputStream()
val options = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccountStream))
.build()
FirebaseApp.initializeApp(options)
}
}
}

Expand Down

0 comments on commit b2b7297

Please sign in to comment.