From 4367a2c63c9676efd921d4858c81562e206791eb Mon Sep 17 00:00:00 2001 From: "ning.yougang" Date: Tue, 8 Sep 2020 13:36:10 +0800 Subject: [PATCH] Fix test cases error --- .../core/entitlement/Entitlement.scala | 178 ++++++++++-------- 1 file changed, 103 insertions(+), 75 deletions(-) diff --git a/core/controller/src/main/scala/org/apache/openwhisk/core/entitlement/Entitlement.scala b/core/controller/src/main/scala/org/apache/openwhisk/core/entitlement/Entitlement.scala index a3d865cd852..e23810fb56e 100644 --- a/core/controller/src/main/scala/org/apache/openwhisk/core/entitlement/Entitlement.scala +++ b/core/controller/src/main/scala/org/apache/openwhisk/core/entitlement/Entitlement.scala @@ -264,96 +264,124 @@ protected[core] abstract class EntitlementProvider( } .getOrElse(Future.successful(())) } else if (operation == "update") { - get(entityStore, entityName.toDocId, DocRevision.empty, true).flatMap { whiskAction => - val currentPermissions = whiskAction.annotations - .get(WhiskAction.permissionsFieldName) - .map(value => value.convertTo[String]) - .getOrElse(WhiskAction.defaultPermissions) - - val errorInfo = s"have no permission to ${operation} this action" - permissions match { - case Some(value) => - if (!WhiskAction.permissionList.contains(value)) { - val errorInfo = - s"give error permission code: ${value}, available permission is in ${WhiskAction.permissionList}" - Future.failed(RejectRequest(Forbidden, Some(ErrorResponse(errorInfo, transid)))) - } else { - val passedUpdatePermission = value.charAt(1) - if (passedUpdatePermission == 'w') { // make it to modifiable - Future.successful(()) + get(entityStore, entityName.toDocId, DocRevision.empty, true) + .flatMap { whiskAction => + val currentPermissions = whiskAction.annotations + .get(WhiskAction.permissionsFieldName) + .map(value => value.convertTo[String]) + .getOrElse(WhiskAction.defaultPermissions) + + val errorInfo = s"have no permission to ${operation} this action" + permissions match { + case Some(value) => + if (!WhiskAction.permissionList.contains(value)) { + val errorInfo = + s"give error permission code: ${value}, available permission is in ${WhiskAction.permissionList}" + Future.failed(RejectRequest(Forbidden, Some(ErrorResponse(errorInfo, transid)))) } else { - val currentUpdatePermission = currentPermissions.charAt(1) - if (currentUpdatePermission == '-') { - Future.failed(RejectRequest(Forbidden, Some(ErrorResponse(errorInfo, transid)))) - } else { + val passedUpdatePermission = value.charAt(1) + if (passedUpdatePermission == 'w') { // make it to modifiable Future.successful(()) + } else { + val currentUpdatePermission = currentPermissions.charAt(1) + if (currentUpdatePermission == '-') { + Future.failed(RejectRequest(Forbidden, Some(ErrorResponse(errorInfo, transid)))) + } else { + Future.successful(()) + } } } - } - case None => - val currentUpdatePermission = currentPermissions.charAt(1) - if (currentUpdatePermission == '-') { - Future.failed(RejectRequest(Forbidden, Some(ErrorResponse(errorInfo, transid)))) - } else { - Future.successful(()) - } + case None => + val currentUpdatePermission = currentPermissions.charAt(1) + if (currentUpdatePermission == '-') { + Future.failed(RejectRequest(Forbidden, Some(ErrorResponse(errorInfo, transid)))) + } else { + Future.successful(()) + } + } + } + .recoverWith { + case t: RejectRequest => + Future.failed(t) + case _ => + Future.successful(()) } - } } else if (operation == "remove") { - get(entityStore, entityName.toDocId, DocRevision.empty, true).flatMap { whiskAction => - val currentPermissions = whiskAction.annotations - .get(WhiskAction.permissionsFieldName) - .map(value => value.convertTo[String]) - .getOrElse(WhiskAction.defaultPermissions) - - val currentUpdatePermission = currentPermissions.charAt(1) - if (currentUpdatePermission == '-') { - val errorInfo = s"have no permission to ${operation} this action" - Future.failed(RejectRequest(Forbidden, Some(ErrorResponse(errorInfo, transid)))) - } else { - Future.successful(()) + get(entityStore, entityName.toDocId, DocRevision.empty, true) + .flatMap { whiskAction => + val currentPermissions = whiskAction.annotations + .get(WhiskAction.permissionsFieldName) + .map(value => value.convertTo[String]) + .getOrElse(WhiskAction.defaultPermissions) + + val currentUpdatePermission = currentPermissions.charAt(1) + if (currentUpdatePermission == '-') { + val errorInfo = s"have no permission to ${operation} this action" + Future.failed(RejectRequest(Forbidden, Some(ErrorResponse(errorInfo, transid)))) + } else { + Future.successful(()) + } + } + .recoverWith { + case t: RejectRequest => + Future.failed(t) + case _ => + Future.successful(()) } - } } else if (operation == "invoke") { - get(entityStore, entityName.toDocId, DocRevision.empty, true).flatMap { whiskAction => - val currentPermissions = whiskAction.annotations - .get(WhiskAction.permissionsFieldName) - .map(value => value.convertTo[String]) - .getOrElse(WhiskAction.defaultPermissions) - - // the user who is owner by default - var currentExecutePermission = currentPermissions.charAt(2) - var errorInfo = s"have no permission to ${operation} this action" - if (user.namespace.name.asString != entityName.path.root.asString) { // the user who invoke the shared action - currentExecutePermission = currentPermissions.charAt(5) - errorInfo = s"have no permission to ${operation} this shared action" + get(entityStore, entityName.toDocId, DocRevision.empty, true) + .flatMap { whiskAction => + val currentPermissions = whiskAction.annotations + .get(WhiskAction.permissionsFieldName) + .map(value => value.convertTo[String]) + .getOrElse(WhiskAction.defaultPermissions) + + // the user who is owner by default + var currentExecutePermission = currentPermissions.charAt(2) + var errorInfo = s"have no permission to ${operation} this action" + if (user.namespace.name.asString != entityName.path.root.asString) { // the user who invoke the shared action + currentExecutePermission = currentPermissions.charAt(5) + errorInfo = s"have no permission to ${operation} this shared action" + } + if (currentExecutePermission == '-') { //have no permission + Future.failed(RejectRequest(Forbidden, Some(ErrorResponse(errorInfo, transid)))) + } else { + Future.successful(()) + } } - if (currentExecutePermission == '-') { //have no permission - Future.failed(RejectRequest(Forbidden, Some(ErrorResponse(errorInfo, transid)))) - } else { - Future.successful(()) + .recoverWith { + case t: RejectRequest => + Future.failed(t) + case _ => + Future.successful(()) } - } } else { // download the code - get(entityStore, entityName.toDocId, DocRevision.empty, true).flatMap { whiskAction => - val currentPermissions = whiskAction.annotations - .get(WhiskAction.permissionsFieldName) - .map(value => value.convertTo[String]) - .getOrElse(WhiskAction.defaultPermissions) - - val errorInfo = s"have no permission to download this shared action" - val currentDownloadPermission = currentPermissions.charAt(3) - if (user.namespace.name.asString != entityName.path.root.asString) { // the shared user who download the action - if (currentDownloadPermission == '-') { - Future.failed(RejectRequest(Forbidden, Some(ErrorResponse(errorInfo, transid)))) + get(entityStore, entityName.toDocId, DocRevision.empty, true) + .flatMap { whiskAction => + val currentPermissions = whiskAction.annotations + .get(WhiskAction.permissionsFieldName) + .map(value => value.convertTo[String]) + .getOrElse(WhiskAction.defaultPermissions) + + val errorInfo = s"have no permission to download this shared action" + val currentDownloadPermission = currentPermissions.charAt(3) + if (user.namespace.name.asString != entityName.path.root.asString) { // the shared user who download the action + if (currentDownloadPermission == '-') { + Future.failed(RejectRequest(Forbidden, Some(ErrorResponse(errorInfo, transid)))) + } else { + Future.successful(()) + } } else { + // the owner has download permission on any situation Future.successful(()) } - } else { - // the owner has download permission on any situation - Future.successful(()) } - } + .recoverWith { + case t: RejectRequest => + Future.failed(t) + case _ => + Future.successful(()) + } } }