Skip to content

Commit

Permalink
use service account for GAM
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakeii committed Jun 10, 2024
1 parent 61786af commit 5727328
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
12 changes: 9 additions & 3 deletions admin/app/conf/AdminConfiguration.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package conf
import common.GuardianConfiguration
import conf.Configuration.{OAuthCredentialsWithMultipleCallbacks, OAuthCredentials}
import pa.PaClientConfig
import java.nio.file.Files
import java.nio.file.Files.createTempFile

case class OmnitureCredentials(userName: String, secret: String)

Expand Down Expand Up @@ -39,9 +41,13 @@ object AdminConfiguration {
}

object dfpApi {
lazy val clientId = configuration.getStringProperty("api.dfp.clientId")
lazy val clientSecret = configuration.getStringProperty("api.dfp.clientSecret")
lazy val refreshToken = configuration.getStringProperty("api.dfp.refreshToken")
lazy val serviceAccountKeyFile = configuration
.getStringProperty("api.dfp.serviceAccountJson")
.map(serviceAccountJson => {
val tempFile = createTempFile("dfpApiCredentials", ".json")
Files.writeString(tempFile, serviceAccountJson)
tempFile
})
lazy val appName = configuration.getStringProperty("api.dfp.applicationName")
}

Expand Down
7 changes: 2 additions & 5 deletions admin/app/dfp/SessionWrapper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,12 @@ object SessionWrapper extends GuLogging {
val dfpSession =
try {
for {
clientId <- AdminConfiguration.dfpApi.clientId
clientSecret <- AdminConfiguration.dfpApi.clientSecret
refreshToken <- AdminConfiguration.dfpApi.refreshToken
serviceAccountKeyFile <- AdminConfiguration.dfpApi.serviceAccountKeyFile
appName <- AdminConfiguration.dfpApi.appName
} yield {
val credential = new OfflineCredentials.Builder()
.forApi(Api.AD_MANAGER)
.withClientSecrets(clientId, clientSecret)
.withRefreshToken(refreshToken)
.withJsonKeyFilePath(serviceAccountKeyFile.toString())
.build()
.generateCredential()
new AdManagerSession.Builder()
Expand Down
5 changes: 5 additions & 0 deletions admin/app/model/AdminLifecycle.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package model

import java.util.TimeZone
import java.nio.file.Files.deleteIfExists

import app.LifecycleComponent
import common._
Expand All @@ -13,6 +14,7 @@ import tools.{AssetMetricsCache, CloudWatch, LoadBalancer}

import scala.concurrent.duration._
import scala.concurrent.{ExecutionContext, Future}
import conf.AdminConfiguration

class AdminLifecycle(
appLifecycle: ApplicationLifecycle,
Expand All @@ -32,6 +34,7 @@ class AdminLifecycle(
descheduleJobs()
CloudWatch.shutdown()
emailService.shutdown()
deleteTmpFiles()
}
}

Expand Down Expand Up @@ -119,6 +122,8 @@ class AdminLifecycle(
jobs.deschedule("AssetMetricsCache")
}

private def deleteTmpFiles(): Unit = AdminConfiguration.dfpApi.serviceAccountKeyFile.map(deleteIfExists)

override def start(): Unit = {
descheduleJobs()
scheduleJobs()
Expand Down

0 comments on commit 5727328

Please sign in to comment.