You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@openwhisk.apache.org by GitBox <gi...@apache.org> on 2018/06/29 01:51:49 UTC

[GitHub] rabbah closed pull request #3821: Avoid regenerating auth key multiple times

rabbah closed pull request #3821: Avoid regenerating auth key multiple times
URL: https://github.com/apache/incubator-openwhisk/pull/3821
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/tests/src/test/scala/whisk/core/database/UserCommandTests.scala b/tests/src/test/scala/whisk/core/database/UserCommandTests.scala
index 9e69b0dc48..680e2ea874 100644
--- a/tests/src/test/scala/whisk/core/database/UserCommandTests.scala
+++ b/tests/src/test/scala/whisk/core/database/UserCommandTests.scala
@@ -62,6 +62,12 @@ class UserCommandTests extends FlatSpec with WhiskAdminCliTestBase {
     admin.executeCommand().futureValue.right.get shouldBe key.compact
   }
 
+  it should "create a user with default key" in {
+    val subject = newSubject()
+    val generatedKey = resultOk("user", "create", subject)
+    resultOk("user", "get", subject) shouldBe generatedKey
+  }
+
   it should "add namespace to existing user" in {
     val subject = newSubject()
     val key = AuthKey()
diff --git a/tools/admin/src/main/scala/whisk/core/database/UserCommand.scala b/tools/admin/src/main/scala/whisk/core/database/UserCommand.scala
index e32f39b4d3..f4119dd961 100644
--- a/tools/admin/src/main/scala/whisk/core/database/UserCommand.scala
+++ b/tools/admin/src/main/scala/whisk/core/database/UserCommand.scala
@@ -85,9 +85,7 @@ class UserCommand extends Subcommand("user") with WhiskCommand {
 
     def isUUID(u: String) = Try(UUID.fromString(u)).isSuccess
 
-    def desiredNamespace = Namespace(EntityName(namespace.getOrElse(subject()).trim), authKey.uuid)
-
-    def authKey: AuthKey = auth.map(AuthKey(_)).getOrElse(AuthKey())
+    def desiredNamespace(authKey: AuthKey) = Namespace(EntityName(namespace.getOrElse(subject()).trim), authKey.uuid)
   }
 
   val create = new CreateUserCmd
@@ -171,22 +169,26 @@ class UserCommand extends Subcommand("user") with WhiskCommand {
 
   def createUser(authStore: AuthStore)(implicit transid: TransactionId,
                                        ec: ExecutionContext): Future[Either[CommandError, String]] = {
-    authStore.get[ExtendedAuth](DocInfo(create.subject())).flatMap { auth =>
-      if (auth.isBlocked) {
-        Future.successful(Left(IllegalState(CommandMessages.subjectBlocked)))
-      } else if (auth.namespaces.exists(_.namespace.name == create.desiredNamespace.name)) {
-        Future.successful(Left(IllegalState(CommandMessages.namespaceExists)))
-      } else {
-        val newNS = auth.namespaces + WhiskNamespace(create.desiredNamespace, create.authKey)
-        val newAuth = WhiskAuth(auth.subject, newNS).revision[WhiskAuth](auth.rev)
-        authStore.put(newAuth).map(_ => Right(create.authKey.compact))
+    val authKey = create.auth.map(AuthKey(_)).getOrElse(AuthKey())
+    authStore
+      .get[ExtendedAuth](DocInfo(create.subject()))
+      .flatMap { auth =>
+        if (auth.isBlocked) {
+          Future.successful(Left(IllegalState(CommandMessages.subjectBlocked)))
+        } else if (auth.namespaces.exists(_.namespace.name == create.desiredNamespace(authKey).name)) {
+          Future.successful(Left(IllegalState(CommandMessages.namespaceExists)))
+        } else {
+          val newNS = auth.namespaces + WhiskNamespace(create.desiredNamespace(authKey), authKey)
+          val newAuth = WhiskAuth(auth.subject, newNS).revision[WhiskAuth](auth.rev)
+          authStore.put(newAuth).map(_ => Right(authKey.compact))
+        }
+      }
+      .recoverWith {
+        case _: NoDocumentException =>
+          val auth =
+            WhiskAuth(Subject(create.subject()), Set(WhiskNamespace(create.desiredNamespace(authKey), authKey)))
+          authStore.put(auth).map(_ => Right(authKey.compact))
       }
-    }
-  }.recoverWith {
-    case _: NoDocumentException =>
-      val auth =
-        WhiskAuth(Subject(create.subject()), Set(WhiskNamespace(create.desiredNamespace, create.authKey)))
-      authStore.put(auth).map(_ => Right(create.authKey.compact))
   }
 
   def deleteUser(authStore: AuthStore)(implicit transid: TransactionId,


 

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services