[GitHub] markusthoemmes commented on a change in pull request #3661: Restrict allowed namespaces when creating action of certain kinds
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
[GitHub] markusthoemmes commented on a change in pull request #3661: Restrict allowed namespaces when creating action of certain kinds
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_r195189983 ## 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 +} Review comment: This is equivalent to: ```scala exec.map(e => kinds.contains(e.kind)).getOrElse(false) ``` In prose it says: If exec has a value, see if it is contained in the set and `get` that value. If exec has no value (`orElse`) default to false. 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
[GitHub] markusthoemmes commented on a change in pull request #3661: Restrict allowed namespaces when creating action of certain kinds
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_r195189406 ## File path: core/controller/src/main/scala/whisk/core/controller/Actions.scala ## @@ -119,15 +119,22 @@ trait WhiskActionsApi extends WhiskCollectionAPI with PostActionActivation with protected override def innerRoutes(user: Identity, ns: EntityPath)(implicit transid: TransactionId) = { (entityPrefix & entityOps & requestMethod) { (segment, m) => entityname(segment) { outername => -pathEnd { +(pathEnd & put & entity(as[WhiskActionPut])) { (content) => + val request = content.resolve(user.namespace) + val resourceData = request.exec match { +case Some(e) => Some(ResourceData(e)) +case None=> None + } Review comment: Throughout your code you can replace many if not all the occurences of pattern-matching on `Option` with methods on Option. `map` is only executed on `Some` and will pass `None` on for instance so this code is equal to: ```scala val resourceData = request.exec.map(e => ResourceData(e)) ``` I'll comment on some more occurences to show some more combinators. 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