Skip to content

Commit

Permalink
Merge pull request #712 from guardian/update-sbt
Browse files Browse the repository at this point in the history
Update sbt
  • Loading branch information
michaelwmcnamara committed Apr 27, 2022
2 parents 3f0bb6b + 55017c7 commit c4fd080
Show file tree
Hide file tree
Showing 80 changed files with 566 additions and 666 deletions.
2 changes: 1 addition & 1 deletion app/ansible/RoleParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import models.{ Markdown, RoleId, RoleSummary, Yaml }

import scala.collection.JavaConverters._
import scala.jdk.CollectionConverters._
import scala.util.Try

object RoleParser {
Expand Down
55 changes: 23 additions & 32 deletions app/components/AppComponents.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.amazonaws.auth.profile.ProfileCredentialsProvider
import com.amazonaws.regions.Regions
import com.amazonaws.retry.{ PredefinedRetryPolicies, RetryPolicy }
import com.amazonaws.retry.PredefinedRetryPolicies.SDKDefaultRetryCondition
import com.amazonaws.services.dynamodbv2.{ AmazonDynamoDB, AmazonDynamoDBClient }
import software.amazon.awssdk.services.dynamodb.DynamoDbClient
import com.amazonaws.services.ec2.{ AmazonEC2, AmazonEC2ClientBuilder }
import com.amazonaws.services.s3.{ AmazonS3, AmazonS3ClientBuilder }
import com.amazonaws.services.securitytoken.{ AWSSecurityTokenService, AWSSecurityTokenServiceClientBuilder }
Expand Down Expand Up @@ -41,7 +41,7 @@ import router.Routes
import schedule.{ BakeScheduler, ScheduledBakeRunner }
import services.{ AmiMetadataLookup, ElkLogging, Loggable, PrismData }
import software.amazon.awssdk.services.ssm.SsmClient
import software.amazon.awssdk.auth.credentials.{ AwsCredentialsProviderChain => AwsCredentialsProviderChainV2, InstanceProfileCredentialsProvider => InstanceProfileCredentialsProviderV2, ProfileCredentialsProvider => ProfileCredentialsProviderV2 }
import software.amazon.awssdk.auth.credentials.{ StaticCredentialsProvider, AwsCredentialsProviderChain => AwsCredentialsProviderChainV2, InstanceProfileCredentialsProvider => InstanceProfileCredentialsProviderV2, ProfileCredentialsProvider => ProfileCredentialsProviderV2 }
import software.amazon.awssdk.regions.Region

import java.time.Duration.{ ofHours, ofMinutes }
Expand All @@ -67,14 +67,14 @@ class LoggingRetryCondition extends SDKDefaultRetryCondition with Loggable {
}

class AppComponents(context: Context, identity: AppIdentity)
extends BuiltInComponentsFromContext(context)
with AhcWSComponents
with I18nComponents
with Loggable
with AssetsComponents
with HttpFiltersComponents
with RotatingSecretComponents
with CSPComponents {
extends BuiltInComponentsFromContext(context)
with AhcWSComponents
with I18nComponents
with Loggable
with AssetsComponents
with HttpFiltersComponents
with RotatingSecretComponents
with CSPComponents {

val stage = identity match {
case DevIdentity(_) => "DEV"
Expand All @@ -86,16 +86,14 @@ class AppComponents(context: Context, identity: AppIdentity)
val awsCredsForV1 = new AWSCredentialsProviderChain(
new ProfileCredentialsProvider("deployTools"),
new ProfileCredentialsProvider(),
InstanceProfileCredentialsProvider.getInstance()
)
InstanceProfileCredentialsProvider.getInstance())

val awsCredsForV2 = AwsCredentialsProviderChainV2
.builder()
.credentialsProviders(
ProfileCredentialsProviderV2.create("deployTools"),
ProfileCredentialsProviderV2.create(),
InstanceProfileCredentialsProviderV2.create()
)
InstanceProfileCredentialsProviderV2.create())
.build()

val region = Regions.EU_WEST_1
Expand All @@ -105,8 +103,7 @@ class AppComponents(context: Context, identity: AppIdentity)
new LoggingRetryCondition(),
PredefinedRetryPolicies.DEFAULT_BACKOFF_STRATEGY,
20,
false
))
false))

val secretStateSupplier: SnapshotProvider = {
new SecretSupplier(
Expand All @@ -115,20 +112,18 @@ class AppComponents(context: Context, identity: AppIdentity)
AwsSdkV2(SsmClient.builder
.credentialsProvider(awsCredsForV2)
.region(Region.of(region.getName))
.build()
)
)
.build()))
}

// initialise logging
val elkLoggingStream = configuration.get[Option[String]]("elk.loggingStream")
val elkLogging = new ElkLogging(identity, elkLoggingStream, awsCredsForV1)

implicit val dynamo = {
val dynamoClient: AmazonDynamoDB = AmazonDynamoDBClient.builder()
.withCredentials(awsCredsForV1)
.withRegion(region)
.withClientConfiguration(clientConfiguration)
val credentialsProvider = StaticCredentialsProvider.create(awsCredsForV2.resolveCredentials())
val dynamoClient: DynamoDbClient = DynamoDbClient.builder()
.credentialsProvider(credentialsProvider)
.region(Region.of(region.getName))
.build()
new Dynamo(dynamoClient, stage)
}
Expand Down Expand Up @@ -196,8 +191,7 @@ class AppComponents(context: Context, identity: AppIdentity)
val eventListeners = Map(
"logWriter" -> Behaviours.writeToLog,
"dynamoWriter" -> Behaviours.persistBakeEvent(notificationConfig),
"snsWriter" -> Behaviours.sendAmiCreatedNotification(sender.sendTopicMessage)
)
"snsWriter" -> Behaviours.sendAmiCreatedNotification(sender.sendTopicMessage))
ActorSystem[BakeEvent](Behaviours.guardian(eventListeners), "EventBus")
}

Expand All @@ -210,16 +204,14 @@ class AppComponents(context: Context, identity: AppIdentity)
domains = List("guardian.co.uk"),
maxAuthAge = Some(Duration.standardDays(90)),
enforceValidity = true,
antiForgeryChecker = AntiForgeryChecker(secretStateSupplier)
)
antiForgeryChecker = AntiForgeryChecker(secretStateSupplier))

implicit val packerConfig = PackerConfig(
stage = stage,
vpcId = configuration.get[Option[String]]("packer.vpcId"),
subnetId = configuration.get[Option[String]]("packer.subnetId"),
instanceProfile = configuration.get[Option[String]]("packer.instanceProfile"),
securityGroupId = configuration.get[Option[String]]("packer.securityGroupId")
)
securityGroupId = configuration.get[Option[String]]("packer.securityGroupId"))

val ansibleVariables: Map[String, String] =
Map("s3_prefix" -> configuration.get[String]("ansible.packages.s3prefix")) ++
Expand Down Expand Up @@ -249,8 +241,7 @@ class AppComponents(context: Context, identity: AppIdentity)
new MarkOldUnusedBakesForDeletion(prismAgents, dynamo),
new MarkOrphanedBakesForDeletion(prismAgents, dynamo),
new TimeOutLongRunningBakes(bakesRepo, packerEC2Client),
new DeleteLongRunningEC2Instances(bakesRepo, packerEC2Client)
)
new DeleteLongRunningEC2Instances(bakesRepo, packerEC2Client))

val housekeepingScheduler = new HousekeepingScheduler(quartzScheduler, houseKeepingJobs)
housekeepingScheduler.initialise()
Expand All @@ -266,7 +257,7 @@ class AppComponents(context: Context, identity: AppIdentity)
*/
override def httpFilters: Seq[EssentialFilter] = Seq(csrfFilter, securityHeadersFilter, cspFilter)

val authAction = new AuthAction[AnyContent](googleAuthConfig, routes.Login.loginAction, controllerComponents.parsers.default)(executionContext)
val authAction = new AuthAction[AnyContent](googleAuthConfig, routes.Login.loginAction(), controllerComponents.parsers.default)(executionContext)

val rootController = new RootController(authAction, controllerComponents)
val baseImageController = new BaseImageController(authAction, prismAgents, controllerComponents)
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/BakeController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class BakeController(
s3Client: AmazonS3,
packerRunner: PackerRunner,
bakeDeletionFrequencyMinutes: Int)(implicit dynamo: Dynamo, packerConfig: PackerConfig, eventBus: EventBus)
extends AbstractController(components) with I18nSupport with Loggable {
extends AbstractController(components) with I18nSupport with Loggable {

def startBaking(recipeId: RecipeId, debug: Boolean): Action[AnyContent] = authAction { request =>
Recipes.findById(recipeId).fold[Result](NotFound) { recipe =>
Expand Down Expand Up @@ -60,9 +60,9 @@ class BakeController(
def bakePackages(recipeId: RecipeId, buildNumber: Int): Action[AnyContent] = authAction {
val list = PackageList.getPackageList(s3Client, BakeId(recipeId, buildNumber), amigoDataBucket)
if (list.isLeft) {
NotFound(s"Could not find package list for recipe $recipeId, bake $buildNumber: ${list.left.get}")
NotFound(s"Could not find package list for recipe $recipeId, bake $buildNumber: ${list.left.toOption.get}")
} else {
Ok(Json.obj("packages" -> Json.toJson(list.right.get)))
Ok(Json.obj("packages" -> Json.toJson(list.toOption.get)))
}
}

Expand Down
43 changes: 18 additions & 25 deletions app/controllers/BaseImageController.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controllers

import com.gu.googleauth.AuthAction
import controllers.ControllerHelpers.parseEnabledRoles
import data._
import models._
import org.joda.time.DateTime
Expand All @@ -12,9 +13,9 @@ import prism.RecipeUsage
import services.PrismData

class BaseImageController(
val authAction: AuthAction[AnyContent],
prismAgents: PrismData,
components: ControllerComponents)(implicit dynamo: Dynamo) extends AbstractController(components) with I18nSupport {
val authAction: AuthAction[AnyContent],
prismAgents: PrismData,
components: ControllerComponents)(implicit dynamo: Dynamo) extends AbstractController(components) with I18nSupport {
import BaseImageController._

def listBaseImages = authAction {
Expand All @@ -38,26 +39,24 @@ class BaseImageController(
image.description,
image.amiId,
image.linuxDist.getOrElse(Ubuntu),
image.eolDate.getOrElse(DateTime.now).toLocalDate.toDate
))
image.eolDate.getOrElse(DateTime.now).toLocalDate.toDate))
Ok(views.html.editBaseImage(image, form, Roles.listIds))
}
}

def updateBaseImage(id: BaseImageId) = authAction(parse.formUrlEncoded) { implicit request =>
BaseImages.findById(id).fold[Result](NotFound) { image =>
Forms.editBaseImage.bindFromRequest.fold({ formWithErrors =>
Forms.editBaseImage.bindFromRequest().fold({ formWithErrors =>
BadRequest(views.html.editBaseImage(image, formWithErrors, Roles.listIds))
}, {
case (description, amiId, linuxDist, eolDate) =>
val customisedRoles = ControllerHelpers.parseEnabledRoles(request.body)
val customisedRoles = parseEnabledRoles(request.body)
customisedRoles.fold(
error => BadRequest(s"Problem parsing roles: $error"),
roles => {
BaseImages.update(image, description, amiId, linuxDist, roles, modifiedBy = request.user.fullName, new DateTime(eolDate))
Redirect(routes.BaseImageController.showBaseImage(id)).flashing("info" -> "Successfully updated base image")
}
)
})
})
}
}
Expand All @@ -67,7 +66,7 @@ class BaseImageController(
}

def createBaseImage = authAction(parse.formUrlEncoded) { implicit request =>
Forms.createBaseImage.bindFromRequest.fold({ formWithErrors =>
Forms.createBaseImage.bindFromRequest().fold({ formWithErrors =>
BadRequest(views.html.newBaseImage(formWithErrors, Roles.listIds))
}, {
case (id, description, amiId, linuxDist, eolDate) =>
Expand All @@ -76,20 +75,19 @@ class BaseImageController(
val formWithError = Forms.createBaseImage.fill((id, description, amiId, linuxDist, eolDate)).withError("id", "This base image ID is already in use")
Conflict(views.html.newBaseImage(formWithError, Roles.listIds))
case None =>
val customisedRoles = ControllerHelpers.parseEnabledRoles(request.body)
val customisedRoles = parseEnabledRoles(request.body)
customisedRoles.fold(
error => BadRequest(s"Problem parsing roles: $error"),
roles => {
BaseImages.create(id, description, amiId, roles, createdBy = request.user.fullName, linuxDist, Some(new DateTime(eolDate)))
Redirect(routes.BaseImageController.showBaseImage(id)).flashing("info" -> "Successfully created base image")
}
)
})
}
})
}

def cloneBaseImage(id: BaseImageId) = authAction { implicit request =>
Forms.cloneBaseImage.bindFromRequest.fold(
Forms.cloneBaseImage.bindFromRequest().fold(
{ form => Redirect(routes.BaseImageController.showBaseImage(id)).flashing("error" -> s"Failed to clone base image: ${form.errors.head.message}") },
{ newId =>
BaseImages.findById(newId).fold[Result] {
Expand All @@ -103,15 +101,13 @@ class BaseImageController(
builtinRoles = baseImage.builtinRoles,
createdBy = request.user.fullName,
linuxDist = linuxDist,
eolDate = baseImage.eolDate
)
eolDate = baseImage.eolDate)
Redirect(routes.BaseImageController.showBaseImage(newId)).flashing("info" -> "Successfully cloned base image")
case None => Redirect(routes.BaseImageController.showBaseImage(id)).flashing("error" -> "Failed to clone base image as it does not have a linux distribution set")
}
}
}(_ => Conflict(s"$newId already exists"))
}
)
})
}

def deleteConfirm(id: BaseImageId) = authAction { implicit request =>
Expand All @@ -126,7 +122,7 @@ class BaseImageController(
val usedByRecipes = Recipes.findByBaseImage(id)
if (usedByRecipes.isEmpty) {
BaseImages.delete(image)
Redirect(routes.BaseImageController.listBaseImages).flashing("info" -> s"Successfully deleted base image ${id.value}")
Redirect(routes.BaseImageController.listBaseImages()).flashing("info" -> s"Successfully deleted base image ${id.value}")
} else {
Redirect(routes.BaseImageController.showBaseImage(id)).flashing("error" -> s"Failed to delete base image as it is in use")
}
Expand All @@ -150,20 +146,17 @@ object BaseImageController {
"description" -> text(maxLength = 10000),
"amiId" -> amiId,
"linuxDist" -> linuxDist,
"eolDate" -> date("yyyy-MM-dd")
))
"eolDate" -> date("yyyy-MM-dd")))

val createBaseImage = Form(tuple(
"id" -> text(minLength = 3, maxLength = 50).transform[BaseImageId](BaseImageId.apply, _.value),
"description" -> text(maxLength = 10000),
"amiId" -> amiId,
"linuxDist" -> linuxDist,
"eolDate" -> date("yyyy-MM-dd")
))
"eolDate" -> date("yyyy-MM-dd")))

val cloneBaseImage = Form(
"newId" -> text(minLength = 3, maxLength = 50).transform[BaseImageId](BaseImageId.apply, _.value)
)
"newId" -> text(minLength = 3, maxLength = 50).transform[BaseImageId](BaseImageId.apply, _.value))
}
}

6 changes: 3 additions & 3 deletions app/controllers/HousekeepingController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import play.api.mvc._
import services.Loggable

class HousekeepingController(val authAction: AuthAction[AnyContent], components: ControllerComponents)(implicit dynamo: Dynamo)
extends AbstractController(components) with Loggable {
extends AbstractController(components) with Loggable {

def showOrphans = authAction {
val (errors, recipes) = Recipes.recipesWithErrors
val (errors, recipes) = Recipes.recipesWithErrors()
val recipeIds = recipes.map(recipe => recipe.id).toSet
val orphanedBakes = MarkOrphanedBakesForDeletion.findOrphanedBakeIds(recipeIds, Bakes.scanForAll())
Ok(views.html.housekeeping(orphanedBakes, errors.length))
Expand All @@ -29,6 +29,6 @@ class HousekeepingController(val authAction: AuthAction[AnyContent], components:
}
}

Redirect(routes.HousekeepingController.showOrphans)
Redirect(routes.HousekeepingController.showOrphans())
}
}
6 changes: 3 additions & 3 deletions app/controllers/Login.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ class Login(val authConfig: GoogleAuthConfig, override val wsClient: WSClient, c
}

def oauth2Callback = Action.async { implicit request =>
processOauth2Callback
processOauth2Callback()
}

override val failureRedirectTarget: Call = routes.Login.loginAction
override val defaultRedirectTarget: Call = routes.RootController.index
override val failureRedirectTarget: Call = routes.Login.loginAction()
override val defaultRedirectTarget: Call = routes.RootController.index()

}
Loading

0 comments on commit c4fd080

Please sign in to comment.