Skip to content

Commit e3f0fc0

Browse files
committed
WIP #600 - Control authorized entities before create or update usage plan
1 parent 5ec0a7d commit e3f0fc0

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

daikoku/app/controllers/ApiController.scala

+2
Original file line numberDiff line numberDiff line change
@@ -4730,6 +4730,7 @@ class ApiController(
47304730
}
47314731

47324732
(for {
4733+
_ <- newPlan.checkAuthorizedEntities(team)
47334734
api <- EitherT.fromOptionF[Future, AppError, Api](
47344735
env.dataStore.apiRepo
47354736
.forTenant(ctx.tenant)
@@ -5059,6 +5060,7 @@ class ApiController(
50595060
}
50605061

50615062
val value: EitherT[Future, AppError, Result] = for {
5063+
_ <- updatedPlan.checkAuthorizedEntities(team)
50625064
api <- EitherT.fromOptionF(
50635065
env.dataStore.apiRepo
50645066
.forTenant(ctx.tenant)

daikoku/app/domain/apiEntities.scala

+18
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,24 @@ sealed trait UsagePlan {
296296
case (_, TenantDisplay.Default) => EitherT.pure[Future, AppError](())
297297
}
298298
}
299+
300+
def checkAuthorizedEntities(team: Team)(implicit ec: ExecutionContext): EitherT[Future, AppError, Unit] = {
301+
otoroshiTarget match {
302+
case Some(otoroshiTarget) if team.authorizedOtoroshiEntities.isDefined =>
303+
val teamAuthorizedEntities = team.authorizedOtoroshiEntities.get.find(_.otoroshiSettingsId == otoroshiTarget.otoroshiSettings)
304+
305+
teamAuthorizedEntities match {
306+
case Some(authorizedEntities) =>
307+
for {
308+
_ <- EitherT.cond[Future](authorizedEntities.authorizedEntities.groups.diff(otoroshiTarget.authorizedEntities.map(_.groups).getOrElse(Set.empty)).isEmpty, (), AppError.Unauthorized)
309+
_ <- EitherT.cond[Future](authorizedEntities.authorizedEntities.services.diff(otoroshiTarget.authorizedEntities.map(_.services).getOrElse(Set.empty)).isEmpty, (), AppError.Unauthorized)
310+
_ <- EitherT.cond[Future](authorizedEntities.authorizedEntities.routes.diff(otoroshiTarget.authorizedEntities.map(_.routes).getOrElse(Set.empty)).isEmpty, (), AppError.Unauthorized)
311+
} yield ()
312+
case None => EitherT.leftT[Future, Unit](AppError.Unauthorized)
313+
}
314+
case _ => EitherT.pure[Future, AppError](())
315+
}
316+
}
299317
}
300318

301319
case object UsagePlan {

0 commit comments

Comments
 (0)