gravitee-io / issues

Gravitee.io - API Platform - Issues
64 stars 26 forks source link

[policy] Unable to get custom Policy to work #1669

Open marckamerbeek opened 5 years ago

marckamerbeek commented 5 years ago

I was building my own policy to test how it works. Just a simple policy that adds something in a header on the request and response.

Created a policy project according to the maven archetype provided by the policy documentation of gravitee.io. Build it an got an zip file with the policy in it. Build a new docker container and added the plugin to /opt/graviteeio-gateway/plugins.

Deployed the gateway as a docker container in Kubernetes. Gateway is working fine together with the UI and Manager. It sees my plugin: image

@OnRequest
    public void onRequest(Request request, Response response, PolicyChain policyChain) {
        // Add a dummy header
        System.out.println("Adding X-JWT header to request");
        request.headers().set("X-Test-Request-Header-OAPI", configuration.getStringParam());

        // Finally continue chaining
        policyChain.doNext(request, response);
    }

    @OnResponse
    public void onResponse(Request request, Response response, PolicyChain policyChain) {
        System.out.println("Adding X-JWT header to response");
        response.headers().set("X-Test-Response-Header-OAPI", "Test value for testing purpose");
        if (isASuccessfulResponse(response)) {
            policyChain.doNext(request, response);
        } else {
            policyChain.failWith(
                    PolicyResult.failure(HttpStatusCode.INTERNAL_SERVER_ERROR_500, "Not a successful response :-("));
        }
    }

Expected Behavior

See a header on the response and some logging of the system.out in std-out.

Current Behavior

The policy is shown in the startup logging of gravitee, but thats it. Nothing else happens if a request is processed.

Am I missing something? Do I need to register it somewhere? I added the generated policy zip file.

Your Environment

gravitee-policy-add-jwt-1.0.0-SNAPSHOT.zip

NicolasGeraud commented 5 years ago

hi,

firstly, your policy already exists. It's called "Transform Headers" (https://github.com/gravitee-io/gravitee-policy-transformheaders)

secondly, you need to add the policy in the "Design" section of the api.

capture d ecran 2018-11-15 15 43 54
marckamerbeek commented 5 years ago

@NicolasGeraud Sorry for not being clear...my goal was to investigate how to make my own policy. I know about all the policies added to gravitee by default. My own policy is loaded:

08:57:04.899 [gravitee] [] INFO  i.g.p.c.internal.PluginRegistryImpl -  > gravitee-policy-add-jwt [1.0.0-SNAPSHOT] has been loaded

But I don't see it in the list in the design section in the UI. I restarted the UI, API and the Gateway. I'm still missing something...

brasseld commented 5 years ago

Hi @marckamerbeek

Did you deploy the zip into plugins directory for both API Gateway AND Management-API ?

marckamerbeek commented 5 years ago

Ahhhhh....that should be the missing thing...I only added it in the gateway. I was assuming (yeah, mother of all....) that it should only be added on one place. I thought the management api was communicating with the gateway about the available policies. I'm gonna add it right away!

brasseld commented 5 years ago

@marckamerbeek How is it going ?

It was the missing piece ?

marckamerbeek commented 5 years ago

Not yet..Got some production issues I had to take care of. Now I'm making a new docker file with the plugin copied in, but its crashing in Kubernetes. I'll let you know as soon it runs again.

brasseld commented 5 years ago

Ok.

To add your custom policy, let me advice you to make use of environment variables. Please have a look to the sample from a docker-compose: https://github.com/brasseld/jugsummercamp-2018/blob/master/docker-compose.yml#L77

(It should also be done for both components ;) )

marckamerbeek commented 5 years ago

Yes...it works! Thank you very much. I could not find this in the manual.

But now I have another question. Is it possible to add a policy for all the endpoints? Like a default policy. Every call on our gateway needs the same set of policies.

brasseld commented 5 years ago

Yep, I did not yet put that part in documentation... too many things should be done about documentation :(.

But now I have another question. Is it possible to add a policy for all the endpoints? Like a default policy. Every call on our gateway needs the same set of policies.

Just put the policies on the default / and they will be applied for all subpaths.

marckamerbeek commented 5 years ago

Hey, no worries....i'm impressed already by the work you guys have been doing. Learned a lot while playing around.

I work for a very big ecommerce company in the Netherlands and have the tasks to look for a new API Gateway. We have been using Zuul for a while now, but missing a UI for admistration, api gallery, subscription...etc. Already did some perfomance comparisons between those two. Zuul looks quite the same as Gravitee. Both performing really well.

I'll check that / for defaults.