openwhisk-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dube...@apache.org
Subject [incubator-openwhisk] branch master updated: Reject requests to create packages having the name 'default' (#2964)
Date Wed, 15 Nov 2017 20:30:58 GMT
This is an automated email from the ASF dual-hosted git repository.

dubeejw pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-openwhisk.git


The following commit(s) were added to refs/heads/master by this push:
     new e79e6e3  Reject requests to create packages having the name 'default' (#2964)
e79e6e3 is described below

commit e79e6e327953ecf082dd11a3247c13ad31d8186d
Author: Mark Deuser <mdeuser@us.ibm.com>
AuthorDate: Wed Nov 15 15:30:55 2017 -0500

    Reject requests to create packages having the name 'default' (#2964)
    
    * Reject requests to create packages having the name 'default'
    
    * Comment updates
---
 .../src/main/scala/whisk/http/ErrorResponse.scala  |  3 ++
 .../scala/whisk/core/controller/Packages.scala     | 40 +++++++++++++---------
 .../core/controller/test/PackagesApiTests.scala    | 29 +++++++++++++++-
 3 files changed, 54 insertions(+), 18 deletions(-)

diff --git a/common/scala/src/main/scala/whisk/http/ErrorResponse.scala b/common/scala/src/main/scala/whisk/http/ErrorResponse.scala
index 5130578..f8d948d 100644
--- a/common/scala/src/main/scala/whisk/http/ErrorResponse.scala
+++ b/common/scala/src/main/scala/whisk/http/ErrorResponse.scala
@@ -106,6 +106,9 @@ object Messages {
   val bindingCannotReferenceBinding = "Cannot bind to another package binding."
   val requestedBindingIsNotValid = "Cannot bind to a resource that is not a package."
   val notAllowedOnBinding = "Operation not permitted on package binding."
+  def packageNameIsReserved(name: String) = {
+    s"Package name '$name' is reserved."
+  }
 
   /** Error messages for sequence activations. */
   def sequenceRetrieveActivationTimeout(id: ActivationId) =
diff --git a/core/controller/src/main/scala/whisk/core/controller/Packages.scala b/core/controller/src/main/scala/whisk/core/controller/Packages.scala
index 4ea7657..e97f316 100644
--- a/core/controller/src/main/scala/whisk/core/controller/Packages.scala
+++ b/core/controller/src/main/scala/whisk/core/controller/Packages.scala
@@ -43,6 +43,8 @@ trait WhiskPackagesApi extends WhiskCollectionAPI with ReferencedEntities
{
 
   protected override val collection = Collection(Collection.PACKAGES)
 
+  protected[core] val RESERVED_NAMES = Array("default")
+
   /** Database service to CRUD packages. */
   protected val entityStore: EntityStore
 
@@ -76,25 +78,29 @@ trait WhiskPackagesApi extends WhiskCollectionAPI with ReferencedEntities
{
    */
   override def create(user: Identity, entityName: FullyQualifiedEntityName)(implicit transid:
TransactionId) = {
     parameter('overwrite ? false) { overwrite =>
-      entity(as[WhiskPackagePut]) { content =>
-        val request = content.resolve(entityName.namespace)
+      if (!overwrite && (RESERVED_NAMES contains entityName.name.asString)) {
+        terminate(BadRequest, Messages.packageNameIsReserved(entityName.name.asString))
+      } else {
+        entity(as[WhiskPackagePut]) { content =>
+          val request = content.resolve(entityName.namespace)
 
-        request.binding.map { b =>
-          logging.info(this, "checking if package is accessible")
-        }
-        val referencedentities = referencedEntities(request)
+          request.binding.map { b =>
+            logging.info(this, "checking if package is accessible")
+          }
+          val referencedentities = referencedEntities(request)
 
-        onComplete(entitlementProvider.check(user, Privilege.READ, referencedentities)) {
-          case Success(_) =>
-            putEntity(
-              WhiskPackage,
-              entityStore,
-              entityName.toDocId,
-              overwrite,
-              update(request) _,
-              () => create(request, entityName))
-          case Failure(f) =>
-            rewriteEntitlementFailure(f)
+          onComplete(entitlementProvider.check(user, Privilege.READ, referencedentities))
{
+            case Success(_) =>
+              putEntity(
+                WhiskPackage,
+                entityStore,
+                entityName.toDocId,
+                overwrite,
+                update(request) _,
+                () => create(request, entityName))
+            case Failure(f) =>
+              rewriteEntitlementFailure(f)
+          }
         }
       }
     }
diff --git a/tests/src/test/scala/whisk/core/controller/test/PackagesApiTests.scala b/tests/src/test/scala/whisk/core/controller/test/PackagesApiTests.scala
index 54e20e7..aad482d 100644
--- a/tests/src/test/scala/whisk/core/controller/test/PackagesApiTests.scala
+++ b/tests/src/test/scala/whisk/core/controller/test/PackagesApiTests.scala
@@ -41,7 +41,6 @@ import whisk.http.Messages
  * These tests exercise a fresh instance of the service object in memory -- these
  * tests do NOT communication with a whisk deployment.
  *
- *
  * @Idioglossia
  * "using Specification DSL to write unit tests, as in should, must, not, be"
  * "using Specs2RouteTest DSL to chain HTTP requests for unit testing, as in ~>"
@@ -370,6 +369,34 @@ class PackagesApiTests extends ControllerTestCommon with WhiskPackagesApi
{
     }
   }
 
+  it should "reject create package when package name is a reserved name" in {
+    implicit val tid = transid()
+    RESERVED_NAMES foreach { reservedName =>
+      val provider = WhiskPackage(namespace, EntityName(s"$reservedName"), None)
+      val content = WhiskPackagePut()
+      Put(s"$collectionPath/${provider.name}", content) ~> Route.seal(routes(creds)) ~>
check {
+        status should be(BadRequest)
+        val response = responseAs[String]
+        response should include {
+          Messages.packageNameIsReserved(reservedName)
+        }
+      }
+    }
+  }
+
+  it should "allow package update even when package name a reserved name" in {
+    implicit val tid = transid()
+    RESERVED_NAMES foreach { reservedName =>
+      val provider = WhiskPackage(namespace, EntityName(s"$reservedName"), None)
+      val content = WhiskPackagePut()
+      Put(s"$collectionPath/${provider.name}?overwrite=true", content) ~> Route.seal(routes(creds))
~> check {
+        status should be(OK)
+        val response = responseAs[WhiskPackage]
+        response should be(provider)
+      }
+    }
+  }
+
   it should "create package reference with explicit namespace" in {
     implicit val tid = transid()
     val provider = WhiskPackage(namespace, aname())

-- 
To stop receiving notification emails like this one, please contact
['"commits@openwhisk.apache.org" <commits@openwhisk.apache.org>'].

Mime
View raw message