This is an automated email from the ASF dual-hosted git repository.

rabbah 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 5e1571a  Avoid regenerating auth key multiple times (#3821)
5e1571a is described below

commit 5e1571a61aa606eecfef7d7e44d46187d29e0026
Author: Chetan Mehrotra <chet...@apache.org>
AuthorDate: Fri Jun 29 07:21:47 2018 +0530

    Avoid regenerating auth key multiple times (#3821)
---
 .../whisk/core/database/UserCommandTests.scala     |  6 ++++
 .../scala/whisk/core/database/UserCommand.scala    | 38 ++++++++++++----------
 2 files changed, 26 insertions(+), 18 deletions(-)

diff --git a/tests/src/test/scala/whisk/core/database/UserCommandTests.scala 
b/tests/src/test/scala/whisk/core/database/UserCommandTests.scala
index 9e69b0d..680e2ea 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 e32f39b..f4119dd 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,

Reply via email to