markusthoemmes commented on a change in pull request #3661: Restrict allowed 
namespaces when creating action of certain kinds
URL: 
https://github.com/apache/incubator-openwhisk/pull/3661#discussion_r195190902
 
 

 ##########
 File path: 
core/controller/src/main/scala/whisk/core/entitlement/KindRestrictor.scala
 ##########
 @@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package whisk.core.entitlement
+
+import whisk.common.Logging
+import whisk.common.TransactionId
+import whisk.core.ConfigKeys
+import whisk.core.entity.{Exec, Identity}
+import pureconfig.loadConfigOrThrow
+
+/**
+ * Restrict allowed kinds via provided whitelists
+ *
+ * @param whitelist set of default allowed kinds when not explicitly available 
via namespace limits
+ */
+protected[core] case class DefaultAllowedKinds(whitelist: Option[Set[String]] 
= None)
+
+/**
+ * A class for determining whether a given namespace is allowed to contain a 
given kind.
+ *
+ * @param allowedKinds a default set of allowed kinds
+ */
+class KindRestrictor(allowedKinds: DefaultAllowedKinds = 
loadConfigOrThrow[DefaultAllowedKinds](ConfigKeys.runtimes))(
+  implicit logging: Logging) {
+
+  logging.info(this, s"allowedKinds = $allowedKinds")(TransactionId.controller)
+
+  private def execKindAllowed(exec: Option[Exec], kinds: Set[String]): Boolean 
= {
+    exec match {
+      case Some(e) if kinds.contains(e.kind) => true
+      case _                                 => false
+    }
+  }
+
+  /**
+   * Checks whether a given kind is allowed for a given user.
+   */
+  def check(user: Identity, exec: Option[Exec])(implicit tid: TransactionId): 
Boolean = {
+    // 1. Check explicit limit set on namespace
+    user.limits.allowedKinds match {
+      case Some(limitKinds) => execKindAllowed(exec, limitKinds)
+      case None => {
+        // 2. Check for a default setting
+        allowedKinds.whitelist match {
+          case Some(defaultKinds) => execKindAllowed(exec, defaultKinds)
+          case None               =>
+            // 3. Fallback to allowing all system-accepted kinds
+            true
+        }
+      }
+    }
 
 Review comment:
   This is a very nice one :tada:, the equivalent with combinators would be:
   
   ```scala
   user.limits.allowedKinds
     .orElse(allowedKinds.whitelist)
     .map(allowed => execKindAllowed(exec, allowed)
     .getOrElse(true)
   ```
   
   It essentially says: If userlimits is defined, pass that value on, if not 
pass the whitelist on. If either of those two is defined, check if the kind is 
allowed and get that value. If not, default to true.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to