Skip to content

Commit 9b98728

Browse files
authored
[WX-1346] Scalafmt (#7257)
1 parent 408bc29 commit 9b98728

File tree

1,553 files changed

+49441
-32619
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,553 files changed

+49441
-32619
lines changed

.scalafmt.conf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
version = 3.7.17
2+
align.preset = none
3+
align.openParenCallSite = true
4+
align.openParenDefnSite = true
5+
maxColumn = 120
6+
continuationIndent.defnSite = 2
7+
assumeStandardLibraryStripMargin = true
8+
align.stripMargin = true
9+
danglingParentheses.preset = true
10+
rewrite.rules = [Imports, RedundantBraces, RedundantParens, SortModifiers]
11+
rewrite.imports.sort = scalastyle
12+
docstrings.style = keep
13+
project.excludeFilters = [
14+
Dependencies.scala,
15+
Settings.scala,
16+
build.sbt
17+
]
18+
runner.dialect = scala213

CromIAM/src/main/scala/cromiam/auth/Collection.scala

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import scala.util.{Success, Try}
99
final case class Collection(name: String) extends AnyVal
1010

1111
object Collection {
12+
1213
/**
1314
* Parses a raw JSON string to make sure it fits the standard pattern (see below) for labels,
1415
* performs some CromIAM-specific checking to ensure the user isn't attempting to manipulate the
@@ -19,13 +20,14 @@ object Collection {
1920
*/
2021
def validateLabels(labelsJson: Option[String]): Directive1[Option[Map[String, JsValue]]] = {
2122

22-
val labels = labelsJson map { l =>
23-
Try(l.parseJson) match {
24-
case Success(JsObject(json)) if json.keySet.contains(CollectionLabelName) => throw new LabelContainsCollectionException
25-
case Success(JsObject(json)) => json
26-
case _ => throw InvalidLabelsException(l)
27-
}
23+
val labels = labelsJson map { l =>
24+
Try(l.parseJson) match {
25+
case Success(JsObject(json)) if json.keySet.contains(CollectionLabelName) =>
26+
throw new LabelContainsCollectionException
27+
case Success(JsObject(json)) => json
28+
case _ => throw InvalidLabelsException(l)
2829
}
30+
}
2931

3032
provide(labels)
3133
}
@@ -34,15 +36,16 @@ object Collection {
3436
val LabelsKey = "labels"
3537

3638
// LabelContainsCollectionException is a class because of ScalaTest, some of the constructs don't play well w/ case objects
37-
final class LabelContainsCollectionException extends Exception(s"Submitted labels contain the key $CollectionLabelName, which is not allowed\n")
38-
final case class InvalidLabelsException(labels: String) extends Exception(s"Labels must be a valid JSON object, received: $labels\n")
39+
final class LabelContainsCollectionException
40+
extends Exception(s"Submitted labels contain the key $CollectionLabelName, which is not allowed\n")
41+
final case class InvalidLabelsException(labels: String)
42+
extends Exception(s"Labels must be a valid JSON object, received: $labels\n")
3943

4044
/**
4145
* Returns the default collection for a user.
4246
*/
43-
def forUser(user: User): Collection = {
47+
def forUser(user: User): Collection =
4448
Collection(user.userId.value)
45-
}
4649

4750
implicit val collectionJsonReader = new JsonReader[Collection] {
4851
import spray.json.DefaultJsonProtocol._

CromIAM/src/main/scala/cromiam/auth/User.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@ import org.broadinstitute.dsde.workbench.model.WorkbenchUserId
77
* Wraps the concept of an authenticated workbench user including their numeric ID as well as their bearer token
88
*/
99
final case class User(userId: WorkbenchUserId, authorization: Authorization)
10-

CromIAM/src/main/scala/cromiam/cromwell/CromwellClient.scala

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,16 @@ import scala.concurrent.{ExecutionContextExecutor, Future}
2525
*
2626
* FIXME: Look for ways to synch this up with the mothership
2727
*/
28-
class CromwellClient(scheme: String, interface: String, port: Int, log: LoggingAdapter, serviceRegistryActorRef: ActorRef)(implicit system: ActorSystem,
29-
ece: ExecutionContextExecutor,
30-
materializer: ActorMaterializer)
31-
extends SprayJsonSupport with DefaultJsonProtocol with StatusCheckedSubsystem with CromIamInstrumentation{
28+
class CromwellClient(scheme: String,
29+
interface: String,
30+
port: Int,
31+
log: LoggingAdapter,
32+
serviceRegistryActorRef: ActorRef
33+
)(implicit system: ActorSystem, ece: ExecutionContextExecutor, materializer: ActorMaterializer)
34+
extends SprayJsonSupport
35+
with DefaultJsonProtocol
36+
with StatusCheckedSubsystem
37+
with CromIamInstrumentation {
3238

3339
val cromwellUrl = new URL(s"$scheme://$interface:$port")
3440
val cromwellApiVersion = "v1"
@@ -41,35 +47,38 @@ class CromwellClient(scheme: String, interface: String, port: Int, log: LoggingA
4147

4248
def collectionForWorkflow(workflowId: String,
4349
user: User,
44-
cromIamRequest: HttpRequest): FailureResponseOrT[Collection] = {
50+
cromIamRequest: HttpRequest
51+
): FailureResponseOrT[Collection] = {
4552
import CromwellClient.EnhancedWorkflowLabels
4653

4754
log.info("Requesting collection for " + workflowId + " for user " + user.userId + " from metadata")
4855

4956
// Look up in Cromwell what the collection is for this workflow. If it doesn't exist, fail the Future
50-
val cromwellApiLabelFunc = () => cromwellApiClient.labels(WorkflowId.fromString(workflowId), headers = List(user.authorization)) flatMap {
51-
_.caasCollection match {
52-
case Some(c) => FailureResponseOrT.pure[IO, HttpResponse](c)
53-
case None =>
54-
val exception = new IllegalArgumentException(s"Workflow $workflowId has no associated collection")
55-
val failure = IO.raiseError[Collection](exception)
56-
FailureResponseOrT.right[HttpResponse](failure)
57+
val cromwellApiLabelFunc = () =>
58+
cromwellApiClient.labels(WorkflowId.fromString(workflowId), headers = List(user.authorization)) flatMap {
59+
_.caasCollection match {
60+
case Some(c) => FailureResponseOrT.pure[IO, HttpResponse](c)
61+
case None =>
62+
val exception = new IllegalArgumentException(s"Workflow $workflowId has no associated collection")
63+
val failure = IO.raiseError[Collection](exception)
64+
FailureResponseOrT.right[HttpResponse](failure)
65+
}
5766
}
58-
}
5967

6068
instrumentRequest(cromwellApiLabelFunc, cromIamRequest, wfCollectionPrefix)
6169
}
6270

6371
def forwardToCromwell(httpRequest: HttpRequest): FailureResponseOrT[HttpResponse] = {
6472
val future = {
6573
// See CromwellClient's companion object for info on these header modifications
66-
val headers = httpRequest.headers.filterNot(header => header.name == TimeoutAccessHeader || header.name == HostHeader)
74+
val headers =
75+
httpRequest.headers.filterNot(header => header.name == TimeoutAccessHeader || header.name == HostHeader)
6776
val cromwellRequest = httpRequest
6877
.copy(uri = httpRequest.uri.withAuthority(interface, port).withScheme(scheme))
6978
.withHeaders(headers)
7079
Http().singleRequest(cromwellRequest)
71-
} recoverWith {
72-
case e => Future.failed(CromwellConnectionFailure(e))
80+
} recoverWith { case e =>
81+
Future.failed(CromwellConnectionFailure(e))
7382
}
7483
future.asFailureResponseOrT
7584
}
@@ -86,7 +95,7 @@ class CromwellClient(scheme: String, interface: String, port: Int, log: LoggingA
8695
use the current workflow id.
8796
8897
This is all called from inside the context of a Future, so exceptions will be properly caught.
89-
*/
98+
*/
9099
metadata.value.parseJson.asJsObject.fields.get("rootWorkflowId").map(_.convertTo[String]).getOrElse(workflowId)
91100
}
92101

@@ -96,11 +105,13 @@ class CromwellClient(scheme: String, interface: String, port: Int, log: LoggingA
96105
Grab the metadata from Cromwell filtered down to the rootWorkflowId. Then transform the response to get just the
97106
root workflow ID itself
98107
*/
99-
val cromwellApiMetadataFunc = () => cromwellApiClient.metadata(
100-
WorkflowId.fromString(workflowId),
101-
args = Option(Map("includeKey" -> List("rootWorkflowId"))),
102-
headers = List(user.authorization)).map(metadataToRootWorkflowId
103-
)
108+
val cromwellApiMetadataFunc = () =>
109+
cromwellApiClient
110+
.metadata(WorkflowId.fromString(workflowId),
111+
args = Option(Map("includeKey" -> List("rootWorkflowId"))),
112+
headers = List(user.authorization)
113+
)
114+
.map(metadataToRootWorkflowId)
104115

105116
instrumentRequest(cromwellApiMetadataFunc, cromIamRequest, rootWfIdPrefix)
106117
}
@@ -120,14 +131,14 @@ object CromwellClient {
120131
// See: https://broadworkbench.atlassian.net/browse/DDO-2190
121132
val HostHeader = "Host"
122133

123-
final case class CromwellConnectionFailure(f: Throwable) extends Exception(s"Unable to connect to Cromwell (${f.getMessage})", f)
134+
final case class CromwellConnectionFailure(f: Throwable)
135+
extends Exception(s"Unable to connect to Cromwell (${f.getMessage})", f)
124136

125137
implicit class EnhancedWorkflowLabels(val wl: WorkflowLabels) extends AnyVal {
126138

127-
import Collection.{CollectionLabelName, collectionJsonReader}
139+
import Collection.{collectionJsonReader, CollectionLabelName}
128140

129-
def caasCollection: Option[Collection] = {
141+
def caasCollection: Option[Collection] =
130142
wl.labels.fields.get(CollectionLabelName).map(_.convertTo[Collection])
131-
}
132143
}
133144
}

CromIAM/src/main/scala/cromiam/instrumentation/CromIamInstrumentation.scala

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ trait CromIamInstrumentation extends CromwellInstrumentation {
2727
val rootWfIdPrefix = NonEmptyList.one("root-workflow-id")
2828
val wfCollectionPrefix = NonEmptyList.one("workflow-collection")
2929

30-
3130
def convertRequestToPath(httpRequest: HttpRequest): NonEmptyList[String] = NonEmptyList.of(
3231
// Returns the path of the URI only, without query parameters (e.g: api/engine/workflows/metadata)
33-
httpRequest.uri.path.toString().stripPrefix("/")
32+
httpRequest.uri.path
33+
.toString()
34+
.stripPrefix("/")
3435
// Replace UUIDs with [id] to keep paths same regardless of the workflow
3536
.replaceAll(CromIamInstrumentation.UUIDRegex, "[id]"),
3637
// Name of the method (e.g: GET)
@@ -43,15 +44,19 @@ trait CromIamInstrumentation extends CromwellInstrumentation {
4344
def makePathFromRequestAndResponse(httpRequest: HttpRequest, httpResponse: HttpResponse): InstrumentationPath =
4445
convertRequestToPath(httpRequest).concatNel(NonEmptyList.of(httpResponse.status.intValue.toString))
4546

46-
def sendTimingApi(statsDPath: InstrumentationPath, timing: FiniteDuration, prefixToStatsd: NonEmptyList[String]): Unit = {
47+
def sendTimingApi(statsDPath: InstrumentationPath,
48+
timing: FiniteDuration,
49+
prefixToStatsd: NonEmptyList[String]
50+
): Unit =
4751
sendTiming(prefixToStatsd.concatNel(statsDPath), timing, CromIamPrefix)
48-
}
4952

50-
def instrumentationPrefixForSam(methodPrefix: NonEmptyList[String]): NonEmptyList[String] = samPrefix.concatNel(methodPrefix)
53+
def instrumentationPrefixForSam(methodPrefix: NonEmptyList[String]): NonEmptyList[String] =
54+
samPrefix.concatNel(methodPrefix)
5155

5256
def instrumentRequest[A](func: () => FailureResponseOrT[A],
5357
httpRequest: HttpRequest,
54-
prefix: NonEmptyList[String]): FailureResponseOrT[A] = {
58+
prefix: NonEmptyList[String]
59+
): FailureResponseOrT[A] = {
5560
def now(): Deadline = Deadline.now
5661

5762
val startTimestamp = now()

CromIAM/src/main/scala/cromiam/sam/SamClient.scala

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,21 @@ class SamClient(scheme: String,
3333
port: Int,
3434
checkSubmitWhitelist: Boolean,
3535
log: LoggingAdapter,
36-
serviceRegistryActorRef: ActorRef)
37-
(implicit system: ActorSystem, ece: ExecutionContextExecutor, materializer: ActorMaterializer) extends StatusCheckedSubsystem with CromIamInstrumentation {
36+
serviceRegistryActorRef: ActorRef
37+
)(implicit system: ActorSystem, ece: ExecutionContextExecutor, materializer: ActorMaterializer)
38+
extends StatusCheckedSubsystem
39+
with CromIamInstrumentation {
3840

39-
private implicit val cs = IO.contextShift(ece)
41+
implicit private val cs = IO.contextShift(ece)
4042

4143
override val statusUri = uri"$samBaseUri/status"
4244
override val serviceRegistryActor: ActorRef = serviceRegistryActorRef
4345

44-
def isSubmitWhitelisted(user: User, cromIamRequest: HttpRequest): FailureResponseOrT[Boolean] = {
46+
def isSubmitWhitelisted(user: User, cromIamRequest: HttpRequest): FailureResponseOrT[Boolean] =
4547
checkSubmitWhitelist.fold(
4648
isSubmitWhitelistedSam(user, cromIamRequest),
4749
FailureResponseOrT.pure(true)
4850
)
49-
}
5051

5152
def isSubmitWhitelistedSam(user: User, cromIamRequest: HttpRequest): FailureResponseOrT[Boolean] = {
5253
val request = HttpRequest(
@@ -64,7 +65,7 @@ class SamClient(scheme: String,
6465
whitelisted <- response.status match {
6566
case StatusCodes.OK =>
6667
// Does not seem to be already provided?
67-
implicit val entityToBooleanUnmarshaller : Unmarshaller[HttpEntity, Boolean] =
68+
implicit val entityToBooleanUnmarshaller: Unmarshaller[HttpEntity, Boolean] =
6869
(Unmarshaller.stringUnmarshaller flatMap Unmarshaller.booleanFromStringUnmarshaller).asScala
6970
val unmarshal = IO.fromFuture(IO(Unmarshal(response.entity).to[Boolean]))
7071
FailureResponseOrT.right[HttpResponse](unmarshal)
@@ -95,14 +96,19 @@ class SamClient(scheme: String,
9596
userInfo.enabled
9697
}
9798
case _ =>
98-
log.error("Could not verify access with Sam for user {}, error was {} {}", user.userId, response.status, response.toString().take(100))
99+
log.error("Could not verify access with Sam for user {}, error was {} {}",
100+
user.userId,
101+
response.status,
102+
response.toString().take(100)
103+
)
99104
FailureResponseOrT.pure[IO, HttpResponse](false)
100105
}
101106
} yield userEnabled
102107
}
103108

104109
def collectionsForUser(user: User, cromIamRequest: HttpRequest): FailureResponseOrT[List[Collection]] = {
105-
val request = HttpRequest(method = HttpMethods.GET, uri = samBaseCollectionUri, headers = List[HttpHeader](user.authorization))
110+
val request =
111+
HttpRequest(method = HttpMethods.GET, uri = samBaseCollectionUri, headers = List[HttpHeader](user.authorization))
106112

107113
for {
108114
response <- instrumentRequest(
@@ -120,24 +126,25 @@ class SamClient(scheme: String,
120126
* @return Successful future if the auth is accepted, a Failure otherwise.
121127
*/
122128
def requestAuth(authorizationRequest: CollectionAuthorizationRequest,
123-
cromIamRequest: HttpRequest): FailureResponseOrT[Unit] = {
129+
cromIamRequest: HttpRequest
130+
): FailureResponseOrT[Unit] = {
124131
val logString = authorizationRequest.action + " access for user " + authorizationRequest.user.userId +
125-
" on a request to " + authorizationRequest.action + " for collection " + authorizationRequest.collection.name
132+
" on a request to " + authorizationRequest.action + " for collection " + authorizationRequest.collection.name
126133

127-
def validateEntityBytes(byteString: ByteString): FailureResponseOrT[Unit] = {
134+
def validateEntityBytes(byteString: ByteString): FailureResponseOrT[Unit] =
128135
if (byteString.utf8String == "true") {
129136
Monad[FailureResponseOrT].unit
130137
} else {
131138
log.warning("Sam denied " + logString)
132139
FailureResponseOrT[IO, HttpResponse, Unit](IO.raiseError(new SamDenialException))
133140
}
134-
}
135141

136142
log.info("Requesting authorization for " + logString)
137143

138144
val request = HttpRequest(method = HttpMethods.GET,
139-
uri = samAuthorizeActionUri(authorizationRequest),
140-
headers = List[HttpHeader](authorizationRequest.user.authorization))
145+
uri = samAuthorizeActionUri(authorizationRequest),
146+
headers = List[HttpHeader](authorizationRequest.user.authorization)
147+
)
141148

142149
for {
143150
response <- instrumentRequest(
@@ -158,26 +165,28 @@ class SamClient(scheme: String,
158165
- If user has the 'add' permission we're ok
159166
- else fail the future
160167
*/
161-
def requestSubmission(user: User,
162-
collection: Collection,
163-
cromIamRequest: HttpRequest
164-
): FailureResponseOrT[Unit] = {
168+
def requestSubmission(user: User, collection: Collection, cromIamRequest: HttpRequest): FailureResponseOrT[Unit] = {
165169
log.info("Verifying user " + user.userId + " can submit a workflow to collection " + collection.name)
166170
val createCollection = registerCreation(user, collection, cromIamRequest)
167171

168172
createCollection flatMap {
169173
case r if r.status == StatusCodes.NoContent => Monad[FailureResponseOrT].unit
170174
case r => FailureResponseOrT[IO, HttpResponse, Unit](IO.raiseError(SamRegisterCollectionException(r.status)))
171175
} recoverWith {
172-
case r if r.status == StatusCodes.Conflict => requestAuth(CollectionAuthorizationRequest(user, collection, "add"), cromIamRequest)
176+
case r if r.status == StatusCodes.Conflict =>
177+
requestAuth(CollectionAuthorizationRequest(user, collection, "add"), cromIamRequest)
173178
case r => FailureResponseOrT[IO, HttpResponse, Unit](IO.raiseError(SamRegisterCollectionException(r.status)))
174179
}
175180
}
176181

177182
protected def registerCreation(user: User,
178183
collection: Collection,
179-
cromIamRequest: HttpRequest): FailureResponseOrT[HttpResponse] = {
180-
val request = HttpRequest(method = HttpMethods.POST, uri = samRegisterUri(collection), headers = List[HttpHeader](user.authorization))
184+
cromIamRequest: HttpRequest
185+
): FailureResponseOrT[HttpResponse] = {
186+
val request = HttpRequest(method = HttpMethods.POST,
187+
uri = samRegisterUri(collection),
188+
headers = List[HttpHeader](user.authorization)
189+
)
181190

182191
instrumentRequest(
183192
() => Http().singleRequest(request).asFailureResponseOrT,
@@ -186,9 +195,9 @@ class SamClient(scheme: String,
186195
)
187196
}
188197

189-
private def samAuthorizeActionUri(authorizationRequest: CollectionAuthorizationRequest) = {
190-
akka.http.scaladsl.model.Uri(s"${samBaseUriForWorkflow(authorizationRequest.collection)}/action/${authorizationRequest.action}")
191-
}
198+
private def samAuthorizeActionUri(authorizationRequest: CollectionAuthorizationRequest) =
199+
akka.http.scaladsl.model
200+
.Uri(s"${samBaseUriForWorkflow(authorizationRequest.collection)}/action/${authorizationRequest.action}")
192201

193202
private def samRegisterUri(collection: Collection) = akka.http.scaladsl.model.Uri(samBaseUriForWorkflow(collection))
194203

@@ -207,15 +216,18 @@ object SamClient {
207216

208217
class SamDenialException extends Exception("Access Denied")
209218

210-
final case class SamConnectionFailure(phase: String, f: Throwable) extends Exception(s"Unable to connect to Sam during $phase (${f.getMessage})", f)
219+
final case class SamConnectionFailure(phase: String, f: Throwable)
220+
extends Exception(s"Unable to connect to Sam during $phase (${f.getMessage})", f)
211221

212-
final case class SamRegisterCollectionException(errorCode: StatusCode) extends Exception(s"Can't register collection with Sam. Status code: ${errorCode.value}")
222+
final case class SamRegisterCollectionException(errorCode: StatusCode)
223+
extends Exception(s"Can't register collection with Sam. Status code: ${errorCode.value}")
213224

214225
final case class CollectionAuthorizationRequest(user: User, collection: Collection, action: String)
215226

216227
val SamDenialResponse = HttpResponse(status = StatusCodes.Forbidden, entity = new SamDenialException().getMessage)
217228

218-
def SamRegisterCollectionExceptionResp(statusCode: StatusCode) = HttpResponse(status = statusCode, entity = SamRegisterCollectionException(statusCode).getMessage)
229+
def SamRegisterCollectionExceptionResp(statusCode: StatusCode) =
230+
HttpResponse(status = statusCode, entity = SamRegisterCollectionException(statusCode).getMessage)
219231

220232
case class UserStatusInfo(adminEnabled: Boolean, enabled: Boolean, userEmail: String, userSubjectId: String)
221233

0 commit comments

Comments
 (0)