|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.openwhisk.core.entitlement |
| 19 | + |
| 20 | +import scala.concurrent.Future |
| 21 | +import akka.actor.ActorSystem |
| 22 | +import akka.http.scaladsl.model.StatusCodes.TooManyRequests |
| 23 | +import org.apache.openwhisk.common.{Logging, TransactionId, UserEvents} |
| 24 | +import org.apache.openwhisk.core.WhiskConfig |
| 25 | +import org.apache.openwhisk.core.connector.{EventMessage, Metric} |
| 26 | +import org.apache.openwhisk.core.controller.RejectRequest |
| 27 | +import org.apache.openwhisk.core.entitlement.Privilege.ACTIVATE |
| 28 | +import org.apache.openwhisk.core.entity.{ControllerInstanceId, Identity} |
| 29 | +import org.apache.openwhisk.core.loadBalancer.LoadBalancer |
| 30 | + |
| 31 | +protected[core] class FPCEntitlementProvider( |
| 32 | + private val config: WhiskConfig, |
| 33 | + private val loadBalancer: LoadBalancer, |
| 34 | + private val controllerInstance: ControllerInstanceId)(implicit actorSystem: ActorSystem, logging: Logging) |
| 35 | + extends LocalEntitlementProvider(config, loadBalancer, controllerInstance) { |
| 36 | + |
| 37 | + override protected[core] def checkThrottles(user: Identity, right: Privilege, resources: Set[Resource])( |
| 38 | + implicit transid: TransactionId): Future[Unit] = { |
| 39 | + if (right == ACTIVATE) { |
| 40 | + val checks = resources.filter(_.collection.path == Collection.ACTIONS).map { res => |
| 41 | + loadBalancer.checkThrottle(user.namespace.name.toPath, res.fqname) |
| 42 | + } |
| 43 | + if (checks.contains(true)) { |
| 44 | + val metric = Metric("ConcurrentRateLimit", 1) |
| 45 | + UserEvents.send( |
| 46 | + eventProducer, |
| 47 | + EventMessage( |
| 48 | + s"controller${controllerInstance.asString}", |
| 49 | + metric, |
| 50 | + user.subject, |
| 51 | + user.namespace.name.toString, |
| 52 | + user.namespace.uuid, |
| 53 | + metric.typeName)) |
| 54 | + Future.failed(RejectRequest(TooManyRequests, "Too many requests")) |
| 55 | + } else Future.successful(()) |
| 56 | + } else Future.successful(()) |
| 57 | + } |
| 58 | + |
| 59 | +} |
| 60 | + |
| 61 | +private object FPCEntitlementProvider extends EntitlementSpiProvider { |
| 62 | + |
| 63 | + override def instance(config: WhiskConfig, loadBalancer: LoadBalancer, instance: ControllerInstanceId)( |
| 64 | + implicit actorSystem: ActorSystem, |
| 65 | + logging: Logging) = |
| 66 | + new FPCEntitlementProvider(config: WhiskConfig, loadBalancer: LoadBalancer, instance) |
| 67 | +} |
0 commit comments