Skip to content

IAM Policy Mixin for HttpJson matches incorrect Mux Handler #1338

@lqiu96

Description

@lqiu96

Showcase test makes a call to getIamPolicy RPC: GET http://localhost:7469/v1beta1/users/5da0e7e4:getIamPolicy.

The userId should be users/5da0e7e4, but the error message (A user with name users/5da0e7e4:getIamPolicy not found.) suggests that the userId is matched as users/5da0e7e4:getIamPolicy

Local Gapic Showcase Server Error:

2023/06/22 11:32:55 Received GET request matching '/v1beta1/{name=users/*}': "/v1beta1/users/bbe489bd:getIamPolicy"
2023/06/22 11:32:55   urlPathParams (expect 1, have 1): map["name":"users/bbe489bd:getIamPolicy"]
2023/06/22 11:32:55   request: {
  "name": "users/bbe489bd:getIamPolicy"
}
2023/06/22 11:32:55 A user with name users/bbe489bd:getIamPolicy not found.

It looks to have matched with this handleFunc:

router.HandleFunc("/v1beta1/{name:users/.+}", rest.HandleGetUser).Methods("GET")
instead of
router.HandleFunc("/v1beta1/{resource:users/.+}:getIamPolicy", rest.HandleGetIamPolicy).Methods("GET")

Full Java Stacktrace:

com.google.api.gax.rpc.NotFoundException: Not Found

	at com.google.api.gax.rpc.ApiExceptionFactory.createException(ApiExceptionFactory.java:50)
	at com.google.api.gax.httpjson.HttpJsonApiExceptionFactory.createApiException(HttpJsonApiExceptionFactory.java:76)
	at com.google.api.gax.httpjson.HttpJsonApiExceptionFactory.create(HttpJsonApiExceptionFactory.java:54)
	at com.google.api.gax.httpjson.HttpJsonExceptionCallable$ExceptionTransformingFuture.onFailure(HttpJsonExceptionCallable.java:97)
	at com.google.api.core.ApiFutures$1.onFailure(ApiFutures.java:84)
	at com.google.common.util.concurrent.Futures$CallbackListener.run(Futures.java:1132)
	at com.google.common.util.concurrent.DirectExecutor.execute(DirectExecutor.java:31)
	at com.google.common.util.concurrent.AbstractFuture.executeListener(AbstractFuture.java:1270)
	at com.google.common.util.concurrent.AbstractFuture.complete(AbstractFuture.java:1038)
	at com.google.common.util.concurrent.AbstractFuture.setException(AbstractFuture.java:808)
	at com.google.api.core.AbstractApiFuture$InternalSettableFuture.setException(AbstractApiFuture.java:92)
	at com.google.api.core.AbstractApiFuture.setException(AbstractApiFuture.java:74)
	at com.google.api.gax.httpjson.HttpJsonClientCalls$HttpJsonFuture.setException(HttpJsonClientCalls.java:128)
	at com.google.api.gax.httpjson.HttpJsonClientCalls$FutureListener.onClose(HttpJsonClientCalls.java:162)
	at com.google.api.gax.httpjson.HttpJsonClientCallImpl$OnCloseNotificationTask.call(HttpJsonClientCallImpl.java:530)
	at com.google.api.gax.httpjson.HttpJsonClientCallImpl.notifyListeners(HttpJsonClientCallImpl.java:379)
	at com.google.api.gax.httpjson.HttpJsonClientCallImpl.deliver(HttpJsonClientCallImpl.java:306)
	at com.google.api.gax.httpjson.HttpJsonClientCallImpl.setResult(HttpJsonClientCallImpl.java:156)
	at com.google.api.gax.httpjson.HttpRequestRunnable.run(HttpRequestRunnable.java:149)
	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:829)
	Suppressed: com.google.api.gax.rpc.AsyncTaskException: Asynchronous task failed
		at com.google.api.gax.rpc.ApiExceptions.callAndTranslateApiException(ApiExceptions.java:57)
		at com.google.api.gax.rpc.UnaryCallable.call(UnaryCallable.java:112)
		at com.google.showcase.v1beta1.IdentityClient.getIamPolicy(IdentityClient.java:939)
		at com.google.showcase.v1beta1.it.ITIam.testHttpJson_getIamPolicy(ITIam.java:151)
		at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
		at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
		at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
		at java.base/java.lang.reflect.Method.invoke(Method.java:566)
		at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
		at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
		at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
		at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
		at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
		at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
		at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
		at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
		at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
		at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
		at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
		at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
		at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
		at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
		at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
		at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
		at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
		at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
		at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
		at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
		at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
		at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
		at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
		at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
		at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
		at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: com.google.api.client.http.HttpResponseException: 404 Not Found
GET http://localhost:7469/v1beta1/users/5da0e7e4:getIamPolicy
{"error":{"code":404,"message":"A user with name users/5da0e7e4:getIamPolicy not found.","details":[],"Body":"","Header":null,"Errors":null}}
	at com.google.api.client.http.HttpResponseException$Builder.build(HttpResponseException.java:293)
	at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1118)
	at com.google.api.gax.httpjson.HttpRequestRunnable.run(HttpRequestRunnable.java:115)
	... 6 more

Possible Fixes:

  1. handleFunc RegExps match exact cases
    I'm not sure this is something Mux supports. Online references seem to suggest fixing the ordering of the handleFuncs.

  2. Move the Mixin handleFuncs to the top
    Adding something like:

var mixinNames = []string{"ListLocations", "GetLocation", "SetIamPolicy", "GetIamPolicy", "TestIamPermissions", "ListOperations", "GetOperation", "DeleteOperation", "CancelOperation", "WaitOperation"}
...
registeredMixins := []*registeredHandler{}
if isMixin(handler.GoMethod) {
registeredMixins = append(registeredMixins, &registeredHandler{pathMatch, handlerName, handler.HTTPMethod})
} else {
registered = append(registered, &registeredHandler{pathMatch, handlerName, handler.HTTPMethod})
}
...
// Print out the mixin handleFuncs first

Seems to work

  1. Sort the handleFuncs pattern by longest to shortest
    Adding something like:
	sort.Slice(registered, func(i, j int) bool {
		return len(registered[i].pattern) > len(registered[j].pattern)
	})

solves the current issue, but may not cover every case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions