envoy icon indicating copy to clipboard operation
envoy copied to clipboard

Network filter ext-authz gRPC not adding header

Open kikogolk opened this issue 7 years ago • 8 comments

Hi all!

Title: Network filter ext-authz gRPC not adding header

Description:

I am using Envoy as sidecar of one of my pods in order to contact an external gRPC service for authorization and proxy the http flow to different clusters depending on a header added by the Authorization server. For the auth logic I am using the network filter ext-authz gRPC and I created a very simple gRPC Auth service using as base your proto files. The problem comes when I cannot see the header added in the response from the auth server. I guess I am not adding the header properly (it is my first time playing with RPC), so I would appreciate if you could tell me if I am understanding properly how to build the CheckResponse object. I was using Envoy v1.9.0 but the same is happening with v1.10.0

Dockerfile:

FROM envoyproxy/envoy:v1.9.0
CMD envoy -c /etc/envoy/envoy.yaml -l debug --component-log-level "grpc:trace,router:trace"

This is my envoy.yaml:

    admin:
      access_log_path: /tmp/admin_access.log
      address:
        socket_address: { address: 0.0.0.0, port_value: 9901 }

    static_resources:
      listeners:
      - name: listener_0
        address:
          socket_address:
            address: 0.0.0.0
            port_value: 8888
        filter_chains:
          - filters:
            - name: envoy.ext_authz
              config:
                stat_prefix: ext_authz
                grpc_service:
                  envoy_grpc:
                    cluster_name: ext-authz-grpc
            - name: envoy.http_connection_manager
              config:
                codec_type: auto
                stat_prefix: ingress_http
                access_log:
                  - name: envoy.file_access_log
                    config:
                      path: "/var/log/access.log"
                route_config:
                  name: local_route
                  virtual_hosts:
                    - name: backend
                      domains: ["*"]
                      routes:
                        - match:
                            prefix: "/"
                            headers:
                              name: "next_hop"
                              exact_match: "human"
                          route:
                            cluster: human
                        - match:
                            prefix: "/"
                            headers:
                              name: "next_hop"
                              exact_match: "cat"
                          route:
                            cluster: cat
                http_filters:
                  - name: envoy.router
      clusters:
      - name: human
        connect_timeout: 0.25s
        type: strict_dns
        http2_protocol_options:  {}
        hosts: [{ socket_address: { address: human, port_value: 8080 }}]
      - name: cat
        connect_timeout: 0.25s
        type: strict_dns
        http2_protocol_options:  {}
        hosts: [{ socket_address: { address: cat, port_value: 8080 }}]
      - name: ext-authz-grpc
        connect_timeout: 0.25s
        type: strict_dns
        http2_protocol_options:  {}
        hosts: [{ socket_address: { address: authz-grpc, port_value: 50051 }}]

And this is the code I did in the Auth server:

  static class AuthorizationImpl extends AuthorizationImplBase {

    @Override
    public void check(CheckRequest req, StreamObserver<CheckResponse> responseObserver) {
      Status statusOk = Status.newBuilder().setCode(Code.OK_VALUE).build();
      HeaderValue header = HeaderValue.newBuilder().setKey("next_hop").setValue("cat").build();
      HeaderValueOption newHeader = HeaderValueOption.newBuilder().setAppend(BoolValue.of(true)).setHeader(header).build();
      OkHttpResponse httpResponse = OkHttpResponse.newBuilder().addHeaders(newHeader).build();
      CheckResponse response = CheckResponse.newBuilder()
        .setStatus(statusOk)
        .setOkResponse(httpResponse)
        .build();

      responseObserver.onNext(response);
      responseObserver.onCompleted();
    }
  }

As you can see, I am authorizing all the queries and adding the header "next_hop" with the hardcoded value "cat". The purpose is to add the header and see how envoy proxies the connection to the cat cluster.

Taking a look to the logs from Envoy:

2019-04-09 10:45:17.311][000016][debug][router] [source/common/router/router.cc:270] [C0][S18166035339641235100] cluster 'ext-authz-grpc' match for URL '/envoy.service.auth.v2alpha.Authorization/Check'
[2019-04-09 10:45:17.312][000016][debug][router] [source/common/router/router.cc:328] [C0][S18166035339641235100] router decoding headers:
':method', 'POST'
':path', '/envoy.service.auth.v2alpha.Authorization/Check'
':authority', 'ext-authz-grpc'
':scheme', 'http'
'te', 'trailers'
'grpc-timeout', '200m'
'content-type', 'application/grpc'
'x-envoy-internal', 'true'
'x-forwarded-for', '192.168.0.9'
'x-envoy-expected-rq-timeout-ms', '200'

[2019-04-09 10:45:17.312][000016][debug][client] [source/common/http/codec_client.cc:26] [C1] connecting
[2019-04-09 10:45:17.312][000016][debug][connection] [source/common/network/connection_impl.cc:634] [C1] connecting to 10.111.95.67:50051
[2019-04-09 10:45:17.312][000016][debug][connection] [source/common/network/connection_impl.cc:643] [C1] connection in progress
[2019-04-09 10:45:17.312][000016][debug][http2] [source/common/http/http2/codec_impl.cc:721] [C1] setting stream-level initial window size to 268435456
[2019-04-09 10:45:17.312][000016][debug][http2] [source/common/http/http2/codec_impl.cc:743] [C1] updating connection-level initial window size to 268435456
[2019-04-09 10:45:17.312][000016][debug][pool] [source/common/http/conn_pool_base.cc:20] queueing request due to no available connections
[2019-04-09 10:45:17.312][000016][trace][router] [source/common/router/router.cc:913] [C0][S18166035339641235100] buffering 50 bytes
[2019-04-09 10:45:17.312][000016][debug][connection] [source/common/network/connection_impl.cc:516] [C1] connected
[2019-04-09 10:45:17.312][000016][debug][client] [source/common/http/codec_client.cc:64] [C1] connected
[2019-04-09 10:45:17.312][000016][debug][pool] [source/common/http/http2/conn_pool.cc:83] [C1] creating stream
[2019-04-09 10:45:17.312][000016][debug][router] [source/common/router/router.cc:1023] [C0][S18166035339641235100] pool ready
[2019-04-09 10:45:17.513][000016][debug][router] [source/common/router/router.cc:462] [C0][S18166035339641235100] upstream timeout
[2019-04-09 10:45:17.513][000016][debug][router] [source/common/router/router.cc:968] [C0][S18166035339641235100] resetting pool request
[2019-04-09 10:45:17.513][000016][debug][client] [source/common/http/codec_client.cc:105] [C1] request reset
[2019-04-09 10:45:17.513][000016][debug][pool] [source/common/http/http2/conn_pool.cc:222] [C1] destroying stream: 0 remaining
[2019-04-09 10:45:17.513][000016][debug][http2] [source/common/http/http2/codec_impl.cc:518] [C1] sent reset code=0
[2019-04-09 10:45:17.513][000016][debug][http2] [source/common/http/http2/codec_impl.cc:563] [C1] stream closed: 0
[2019-04-09 10:45:17.517][000016][debug][http] [source/common/http/async_client_impl.cc:93] async http request response headers (end_stream=true):
':status', '200'
'content-type', 'application/grpc'
'grpc-status', '14'
'grpc-message', 'upstream request timeout'

[2019-04-09 10:45:17.517][000016][debug][connection] [source/common/network/connection_impl.cc:101] [C0] closing data_to_write=0 type=1
[2019-04-09 10:45:17.517][000016][debug][connection] [source/common/network/connection_impl.cc:183] [C0] closing socket: 1
[2019-04-09 10:45:17.517][000016][debug][main] [source/server/connection_handler_impl.cc:68] [C0] adding to cleanup list
[2019-04-09 10:45:18.388][000007][debug][main] [source/server/server.cc:143] flushing stats

I can see the filter is calling my Auth service and it is answering 200 OK, but I cannot see the "next_hop" header. Could you please tell me if I am adding the header properly in the CheckResponse object?

Thanks a lot. \Mariano.

kikogolk avatar Apr 09 '19 13:04 kikogolk

@gsagula

mattklein123 avatar Apr 09 '19 13:04 mattklein123

@kikogolk This is a problem with the append logic. If the header that you are trying to append is already part of the request, it will work, otherwise filter won't create a new header. I already have a fix for that, but I still need to open a proper PR. Meanwhile, you can try HeaderValueOption.newBuilder().setAppend(BoolValue.of(false)) if append is not important in your case.

Please, let me know if it works.

gsagula avatar Apr 09 '19 16:04 gsagula

Thanks @gsagula for you quick answer!

I have tested your proposal (setting append to false) and I am afraid the header is still not there. These are the logs using Envoy v1.10.0 (sorry because I pasted a wrong log in my first message, in that log the grpc was answering timeout. I guess I have to tune the timeout values because the first attemp is always returning timeout):

[2019-04-10 08:12:51.389][13][debug][router] [source/common/router/router.cc:320] [C0][S3071285557094235203] cluster 'ext-authz-grpc' match for URL '/envoy.service.auth.v2.Authorization/Check'
[2019-04-10 08:12:51.389][13][debug][router] [source/common/router/router.cc:381] [C0][S3071285557094235203] router decoding headers:
':method', 'POST'
':path', '/envoy.service.auth.v2.Authorization/Check'
':authority', 'ext-authz-grpc'
':scheme', 'http'
'te', 'trailers'
'grpc-timeout', '200m'
'content-type', 'application/grpc'
'x-envoy-internal', 'true'
'x-forwarded-for', '192.168.0.9'
'x-envoy-expected-rq-timeout-ms', '200'

[2019-04-10 08:12:51.389][13][debug][client] [source/common/http/codec_client.cc:26] [C3] connecting
[2019-04-10 08:12:51.389][13][debug][connection] [source/common/network/connection_impl.cc:644] [C3] connecting to 10.98.128.244:50051
[2019-04-10 08:12:51.390][13][debug][connection] [source/common/network/connection_impl.cc:653] [C3] connection in progress
[2019-04-10 08:12:51.390][13][debug][http2] [source/common/http/http2/codec_impl.cc:735] [C3] setting stream-level initial window size to 268435456
[2019-04-10 08:12:51.390][13][debug][http2] [source/common/http/http2/codec_impl.cc:757] [C3] updating connection-level initial window size to 268435456
[2019-04-10 08:12:51.390][13][debug][pool] [source/common/http/conn_pool_base.cc:20] queueing request due to no available connections
[2019-04-10 08:12:51.390][13][trace][router] [source/common/router/router.cc:1058] [C0][S3071285557094235203] buffering 50 bytes
[2019-04-10 08:12:51.390][13][debug][connection] [source/common/network/connection_impl.cc:517] [C3] connected
[2019-04-10 08:12:51.390][13][debug][client] [source/common/http/codec_client.cc:64] [C3] connected
[2019-04-10 08:12:51.390][13][debug][pool] [source/common/http/http2/conn_pool.cc:96] [C3] creating stream
[2019-04-10 08:12:51.390][13][debug][router] [source/common/router/router.cc:1165] [C0][S3071285557094235203] pool ready
[2019-04-10 08:12:51.402][13][debug][router] [source/common/router/router.cc:717] [C0][S3071285557094235203] upstream headers complete: end_stream=false
[2019-04-10 08:12:51.402][13][debug][http] [source/common/http/async_client_impl.cc:94] async http request response headers (end_stream=false):
':status', '200'
'content-type', 'application/grpc'
'grpc-encoding', 'identity'
'grpc-accept-encoding', 'gzip'
'x-envoy-upstream-service-time', '12'

[2019-04-10 08:12:51.402][13][debug][client] [source/common/http/codec_client.cc:95] [C3] response complete
[2019-04-10 08:12:51.402][13][debug][pool] [source/common/http/http2/conn_pool.cc:237] [C3] destroying stream: 0 remaining
[2019-04-10 08:12:51.402][13][debug][http] [source/common/http/async_client_impl.cc:109] async http request response trailers:
'grpc-status', '0'

[2019-04-10 08:12:51.402][13][debug][http] [source/common/http/conn_manager_impl.cc:243] [C2] new stream
[2019-04-10 08:12:51.402][13][debug][http] [source/common/http/conn_manager_impl.cc:580] [C2][S18017938375058352559] request headers complete (end_stream=true):
':authority', '10.109.131.210:8080'
':path', '/'
':method', 'GET'
'cache-control', 'no-cache'
'postman-token', '9087dbf5-bf75-4791-9fa6-465ea6af27b6'
'user-agent', 'PostmanRuntime/7.6.1'
'accept', '*/*'
'accept-encoding', 'gzip, deflate'
'connection', 'keep-alive'

[2019-04-10 08:12:51.402][13][debug][http] [source/common/http/conn_manager_impl.cc:1037] [C2][S18017938375058352559] request end stream
[2019-04-10 08:12:51.402][13][debug][router] [source/common/router/router.cc:277] [C2][S18017938375058352559] no cluster match for URL '/'
[2019-04-10 08:12:51.402][13][debug][http] [source/common/http/conn_manager_impl.cc:1278] [C2][S18017938375058352559] encoding headers via codec (end_stream=true):
':status', '404'
'date', 'Wed, 10 Apr 2019 08:12:51 GMT'
'server', 'envoy'

kikogolk avatar Apr 10 '19 08:04 kikogolk

Hi!

I was playing a little today with your filter code and adding some logs to trace how it was working (the ones with KIKOGOLK string). I do not have too much experience coding in C++ and Bazel, but finally I could check that the header is in the CheckResponse object:



[2019-04-10 16:55:04.216][13][debug][config] [source/extensions/filters/common/ext_authz/ext_authz_grpc_impl.cc:26] KIKOGOLK >>> CREATING GrpcClientImpl
[2019-04-10 16:55:04.216][13][debug][main] [source/server/connection_handler_impl.cc:257] [C2] new connection
[2019-04-10 16:55:04.217][13][debug][config] [source/extensions/filters/common/ext_authz/ext_authz_grpc_impl.cc:43] KIKOGOLK >>> GrpcClientImpl::check()
[2019-04-10 16:55:04.217][13][debug][router] [source/common/router/router.cc:320] [C0][S6735508158765147663] cluster 'ext-authz-grpc' match for URL '/envoy.service.auth.v2.Authorization/Check'
[2019-04-10 16:55:04.217][13][debug][router] [source/common/router/router.cc:381] [C0][S6735508158765147663] router decoding headers:
':method', 'POST'
':path', '/envoy.service.auth.v2.Authorization/Check'
':authority', 'ext-authz-grpc'
':scheme', 'http'
'te', 'trailers'
'grpc-timeout', '200m'
'content-type', 'application/grpc'
'x-envoy-internal', 'true'
'x-forwarded-for', '192.168.0.10'
'x-envoy-expected-rq-timeout-ms', '200'

[2019-04-10 16:55:04.217][13][debug][client] [source/common/http/codec_client.cc:26] [C3] connecting
[2019-04-10 16:55:04.217][13][debug][connection] [source/common/network/connection_impl.cc:644] [C3] connecting to 10.106.153.146:50051
[2019-04-10 16:55:04.217][13][debug][connection] [source/common/network/connection_impl.cc:653] [C3] connection in progress
[2019-04-10 16:55:04.217][13][debug][http2] [source/common/http/http2/codec_impl.cc:735] [C3] setting stream-level initial window size to 268435456
[2019-04-10 16:55:04.217][13][debug][http2] [source/common/http/http2/codec_impl.cc:757] [C3] updating connection-level initial window size to 268435456
[2019-04-10 16:55:04.217][13][debug][pool] [source/common/http/conn_pool_base.cc:20] queueing request due to no available connections
[2019-04-10 16:55:04.217][13][trace][router] [source/common/router/router.cc:1058] [C0][S6735508158765147663] buffering 51 bytes
[2019-04-10 16:55:04.217][13][debug][connection] [source/common/network/connection_impl.cc:517] [C3] connected
[2019-04-10 16:55:04.217][13][debug][client] [source/common/http/codec_client.cc:64] [C3] connected
[2019-04-10 16:55:04.217][13][debug][pool] [source/common/http/http2/conn_pool.cc:96] [C3] creating stream
[2019-04-10 16:55:04.217][13][debug][router] [source/common/router/router.cc:1165] [C0][S6735508158765147663] pool ready
[2019-04-10 16:55:04.230][13][debug][router] [source/common/router/router.cc:717] [C0][S6735508158765147663] upstream headers complete: end_stream=false
[2019-04-10 16:55:04.231][13][debug][http] [source/common/http/async_client_impl.cc:94] async http request response headers (end_stream=false):
':status', '200'
'content-type', 'application/grpc'
'grpc-encoding', 'identity'
'grpc-accept-encoding', 'gzip'
'x-envoy-upstream-service-time', '13'

[2019-04-10 16:55:04.231][13][debug][client] [source/common/http/codec_client.cc:95] [C3] response complete
[2019-04-10 16:55:04.231][13][debug][pool] [source/common/http/http2/conn_pool.cc:237] [C3] destroying stream: 0 remaining
[2019-04-10 16:55:04.231][13][debug][http] [source/common/http/async_client_impl.cc:109] async http request response trailers:
'grpc-status', '0'

[2019-04-10 16:55:04.231][13][debug][config] [source/extensions/filters/common/ext_authz/ext_authz_grpc_impl.cc:53] KIKOGOLK >>> GrpcClientImpl::onSuccess()
[2019-04-10 16:55:04.231][13][debug][config] [source/extensions/filters/common/ext_authz/ext_authz_grpc_impl.cc:56] KIKOGOLK >>> GrpcClientImpl::onSuccess() - GRPC STATUS OK
[2019-04-10 16:55:04.231][13][debug][config] [source/extensions/filters/common/ext_authz/ext_authz_grpc_impl.cc:60] KIKOGOLK >>> GrpcClientImpl::onSuccess() - HAS OK RESPONSE
[2019-04-10 16:55:04.231][13][debug][config] [source/extensions/filters/common/ext_authz/ext_authz_grpc_impl.cc:93] KIKOGOLK >>> GrpcClientImpl::toAuthzResponseHeader()
[2019-04-10 16:55:04.231][13][debug][config] [source/extensions/filters/common/ext_authz/ext_authz_grpc_impl.cc:95] KIKOGOLK >>> header [next_hop, cat]
[2019-04-10 16:55:04.231][13][debug][config] [source/extensions/filters/common/ext_authz/ext_authz_grpc_impl.cc:101] KIKOGOLK >>> GrpcClientImpl::toAuthzResponseHeader() - Adding the header
[2019-04-10 16:55:04.231][13][debug][http] [source/common/http/conn_manager_impl.cc:243] [C2] new stream
[2019-04-10 16:55:04.231][13][debug][http] [source/common/http/conn_manager_impl.cc:580] [C2][S1913616105050349090] request headers complete (end_stream=true):
':authority', '10.103.22.144:8080'
':path', '/'
':method', 'GET'
'cache-control', 'no-cache'
'postman-token', '3eb60a26-880f-4d91-be0e-12ddacbb8c3e'
'user-agent', 'PostmanRuntime/7.6.1'
'accept', '*/*'
'accept-encoding', 'gzip, deflate'
'connection', 'keep-alive'

[2019-04-10 16:55:04.231][13][debug][http] [source/common/http/conn_manager_impl.cc:1037] [C2][S1913616105050349090] request end stream
[2019-04-10 16:55:04.231][13][debug][router] [source/common/router/router.cc:277] [C2][S1913616105050349090] no cluster match for URL '/'
[2019-04-10 16:55:04.231][13][debug][http] [source/common/http/conn_manager_impl.cc:1278] [C2][S1913616105050349090] encoding headers via codec (end_stream=true):
':status', '404'
'date', 'Wed, 10 Apr 2019 16:55:03 GMT'
'server', 'envoy'

I can see in the logs that the header is in the response, when onSuccess method is called.

From here I am a little lost. Hope this can help you.

Thanks!

kikogolk avatar Apr 10 '19 17:04 kikogolk

@kikogolk This helps, thanks! I'm stuck with a bunch of other stuff right now. I can take a look at this a bit later. Please, feel free to ping me if for some reason I forget to get back to you.

gsagula avatar Apr 10 '19 18:04 gsagula

Hi!

No problem @gsagula, it was not my intention to rush you. I will update the ticket with my findings and you can take a look when you want.

I realized that I could not see any HTTP Request data inside the CheckRequest object so, after thinking a little, I realized this could be due to I was using the gRPC Ext Auth filter in layer 3. It makes sense. So probably I did my testing not properly and this ticket could be closed.

Anyway, I changed my Envoy's configuration and I put the gRPC Ext Auth filter as part of HTTP Connection Manager Filter (I was checking another open issue you are working on it #6481). Now I can see the HTTP Request inside the CheckRequest gRPC message object and I can see the header I added inside the Response. But, as issue #6481 declares, routing cannot be done using headers added by the Auth Server.

Thanks a lot!

kikogolk avatar Apr 15 '19 10:04 kikogolk

This issue has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed in the next 7 days unless it is tagged "help wanted" or other activity occurs. Thank you for your contributions.

stale[bot] avatar May 15 '19 10:05 stale[bot]

This issue has been automatically closed because it has not had activity in the last 37 days. If this issue is still valid, please ping a maintainer and ask them to label it as "help wanted". Thank you for your contributions.

stale[bot] avatar May 22 '19 10:05 stale[bot]