Skip to content

Commit 9d9db52

Browse files
Bhavesh KemburuBhavesh Kemburu
authored andcommitted
Improved logic of fetching actions
1 parent e11d229 commit 9d9db52

3 files changed

Lines changed: 56 additions & 70 deletions

File tree

common/scala/src/main/scala/org/apache/openwhisk/http/ErrorResponse.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ object Messages {
231231
val resourceProvisionError = "Failed to provision resources to run the action."
232232

233233
def forbiddenGetActionBinding(entityDocId: String) =
234-
s"GET not permitted for '$entityDocId'. Resource does not exist or an action in a shared package binding."
234+
s"GET not permitted for '$entityDocId'. Resource does not exist or is an action in a shared package binding."
235235
def forbiddenGetAction(entityPath: String) =
236236
s"GET not permitted for '$entityPath' since it's an action in a shared package"
237237
def forbiddenGetPackageBinding(packageName: String) =

core/controller/src/main/scala/org/apache/openwhisk/core/controller/Actions.scala

Lines changed: 52 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,7 @@ trait WhiskActionsApi extends WhiskCollectionAPI with PostActionActivation with
106106

107107
/** Config flag for Execute Only for Actions in Shared Packages */
108108
protected def executeOnly =
109-
Try({
110-
loadConfigOrThrow[Boolean](ConfigKeys.sharedPackageExecuteOnly)
111-
}).getOrElse(false)
109+
loadConfigOrThrow[Boolean](ConfigKeys.sharedPackageExecuteOnly)
112110

113111
/** Entity normalizer to JSON object. */
114112
import RestApiCommons.emptyEntityToJsObject
@@ -338,6 +336,56 @@ trait WhiskActionsApi extends WhiskCollectionAPI with PostActionActivation with
338336
deleteEntity(WhiskAction, entityStore, entityName.toDocId, (a: WhiskAction) => Future.successful({}))
339337
}
340338

339+
/**GET --save which retrieves all source code for an entity **/
340+
private def getEntityWithSourceCode(entityName: FullyQualifiedEntityName, env: Option[Parameters])(
341+
implicit transid: TransactionId) = {
342+
getEntity(WhiskAction.resolveActionAndMergeParameters(entityStore, entityName), Some { action: WhiskAction =>
343+
val mergedAction = env map {
344+
action inherit _
345+
} getOrElse action
346+
complete(OK, mergedAction)
347+
})
348+
}
349+
350+
/**Standard GET which just retrieves metadata**/
351+
private def getEntityMetaData(entityName: FullyQualifiedEntityName, env: Option[Parameters])(
352+
implicit transid: TransactionId) = {
353+
getEntity(WhiskActionMetaData.resolveActionAndMergeParameters(entityStore, entityName), Some {
354+
action: WhiskActionMetaData =>
355+
val mergedAction = env map {
356+
action inherit _
357+
} getOrElse action
358+
complete(OK, mergedAction)
359+
})
360+
}
361+
362+
/** Checks for package binding case. we don't want to allow get for a package binding in shared package */
363+
private def fetchEntity(entityName: FullyQualifiedEntityName, env: Option[Parameters], code: Boolean)(
364+
implicit transid: TransactionId) = {
365+
if (entityName.path.defaultPackage) {
366+
if (code) {
367+
getEntityWithSourceCode(entityName, env)
368+
} else {
369+
getEntityMetaData(entityName, env)
370+
}
371+
} else {
372+
getEntity(
373+
WhiskPackage.resolveBinding(entityStore, entityName.path.toDocId, mergeParameters = true),
374+
Some { pkg: WhiskPackage =>
375+
val originalPackageLocation = pkg.fullyQualifiedName(withVersion = false).namespace
376+
if (executeOnly && originalPackageLocation != entityName.namespace) {
377+
terminate(Forbidden, forbiddenGetActionBinding(entityName.toDocId.asString))
378+
} else {
379+
if (code) {
380+
getEntityWithSourceCode(entityName, env)
381+
} else {
382+
getEntityMetaData(entityName, env)
383+
}
384+
}
385+
})
386+
}
387+
}
388+
341389
/**
342390
* Gets action. The action name is prefixed with the namespace to create the primary index key.
343391
*
@@ -351,67 +399,10 @@ trait WhiskActionsApi extends WhiskCollectionAPI with PostActionActivation with
351399
parameter('code ? true) { code =>
352400
//check if execute only is enabled, and if there is a discrepancy between the current user's namespace
353401
//and that of the entity we are trying to fetch
354-
355402
if (executeOnly && user.namespace.name != entityName.namespace) {
356403
terminate(Forbidden, forbiddenGetAction(entityName.path.asString))
357404
} else {
358-
code match {
359-
case true =>
360-
//Resolve Binding(Package) of the action
361-
if (entityName.path.defaultPackage) {
362-
getEntity(WhiskAction.resolveActionAndMergeParameters(entityStore, entityName), Some {
363-
action: WhiskAction =>
364-
val mergedAction = env map {
365-
action inherit _
366-
} getOrElse action
367-
complete(OK, mergedAction)
368-
})
369-
} else {
370-
getEntity(
371-
WhiskPackage.resolveBinding(entityStore, entityName.path.toDocId, mergeParameters = true),
372-
Some { pkg: WhiskPackage =>
373-
val originalPackageLocation = pkg.fullyQualifiedName(withVersion = false).namespace
374-
if (executeOnly && originalPackageLocation != entityName.namespace) {
375-
terminate(Forbidden, forbiddenGetActionBinding(entityName.toDocId.asString))
376-
} else {
377-
getEntity(WhiskAction.resolveActionAndMergeParameters(entityStore, entityName), Some {
378-
action: WhiskAction =>
379-
val mergedAction = env map {
380-
action inherit _
381-
} getOrElse action
382-
complete(OK, mergedAction)
383-
})
384-
}
385-
})
386-
}
387-
case false =>
388-
if (entityName.path.defaultPackage) {
389-
getEntity(WhiskActionMetaData.resolveActionAndMergeParameters(entityStore, entityName), Some {
390-
action: WhiskActionMetaData =>
391-
val mergedAction = env map {
392-
action inherit _
393-
} getOrElse action
394-
complete(OK, mergedAction)
395-
})
396-
} else {
397-
getEntity(
398-
WhiskPackage.resolveBinding(entityStore, entityName.path.toDocId, mergeParameters = true),
399-
Some { pkg: WhiskPackage =>
400-
val originalPackageLocation = pkg.fullyQualifiedName(withVersion = false).namespace
401-
if (executeOnly && originalPackageLocation != entityName.namespace) {
402-
terminate(Forbidden, forbiddenGetActionBinding(entityName.toDocId.asString))
403-
} else {
404-
getEntity(WhiskActionMetaData.resolveActionAndMergeParameters(entityStore, entityName), Some {
405-
action: WhiskActionMetaData =>
406-
val mergedAction = env map {
407-
action inherit _
408-
} getOrElse action
409-
complete(OK, mergedAction)
410-
})
411-
}
412-
})
413-
}
414-
}
405+
fetchEntity(entityName, env, code)
415406
}
416407
}
417408
}

core/controller/src/main/scala/org/apache/openwhisk/core/controller/Packages.scala

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
package org.apache.openwhisk.core.controller
1919

2020
import scala.concurrent.Future
21-
import scala.util.{Failure, Success, Try}
21+
import scala.util.{Failure, Success}
2222
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
2323
import akka.http.scaladsl.model.StatusCodes._
2424
import akka.http.scaladsl.server.{RequestContext, RouteResult}
@@ -45,9 +45,7 @@ trait WhiskPackagesApi extends WhiskCollectionAPI with ReferencedEntities {
4545

4646
/** Config flag for Execute Only for Shared Packages */
4747
protected def executeOnly =
48-
Try({
49-
loadConfigOrThrow[Boolean](ConfigKeys.sharedPackageExecuteOnly)
50-
}).getOrElse(false)
48+
loadConfigOrThrow[Boolean](ConfigKeys.sharedPackageExecuteOnly)
5149

5250
/** Notification service for cache invalidation. */
5351
protected implicit val cacheChangeNotification: Some[CacheChangeNotification]
@@ -321,10 +319,7 @@ trait WhiskPackagesApi extends WhiskCollectionAPI with ReferencedEntities {
321319
} else {
322320

323321
/** Here's where I check package execute only case with package binding. */
324-
val packagePath = wp.namespace.asString
325-
val bindingPath = wp.binding.iterator.next().toString
326-
val indexOfSlash = bindingPath.indexOf('/')
327-
if (executeOnly && packagePath != bindingPath.take(indexOfSlash)) {
322+
if (executeOnly && wp.namespace.asString != b.namespace.asString) {
328323
terminate(Forbidden, forbiddenGetPackageBinding(wp.name.asString))
329324
} else {
330325
getEntity(WhiskPackage.get(entityStore, docid), Some {

0 commit comments

Comments
 (0)