static_resources:
  listeners:
  - name: listener_0
    address:
      socket_address:
        protocol: TCP
        address: 0.0.0.0
        port_value: 10000
    filter_chains:
    - filters:
      - name: envoy.filters.network.http_connection_manager
        typed_config:
          "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
          stat_prefix: ingress_http
          route_config:
            name: local_route
            virtual_hosts:
            - name: local_service
              domains: ["*"]
              routes:
              - match:
                  prefix: "/"
                route:
                  host_rewrite_literal: upstream.com
                  cluster: upstream_com
          http_filters:
          # Lua filter sets dynamic metadata that controls whether ext_authz runs.
          - name: envoy.filters.http.lua
            typed_config:
              "@type": type.googleapis.com/envoy.extensions.filters.http.lua.v3.Lua
              default_source_code:
                inline_string: |
                  function envoy_on_request(request_handle)
                    -- Set metadata to conditionally enable ext_authz.
                    -- For example, enable auth for requests to /secure paths.
                    local path = request_handle:headers():get(":path")
                    if string.match(path, "^/secure") then
                      request_handle:streamInfo():dynamicMetadata():set("envoy.filters.http.ext_authz", "require_auth", "true")
                    else
                      request_handle:streamInfo():dynamicMetadata():set("envoy.filters.http.ext_authz", "require_auth", "false")
                    end
                  end
          # ExtensionWithMatcher wraps ext_authz and conditionally invokes it based on dynamic metadata.
          - name: ext-authz-with-matcher
            typed_config:
              "@type": type.googleapis.com/envoy.extensions.common.matching.v3.ExtensionWithMatcher
              extension_config:
                name: envoy.filters.http.ext_authz
                typed_config:
                  "@type": type.googleapis.com/envoy.extensions.filters.http.ext_authz.v3.ExtAuthz
                  grpc_service:
                    envoy_grpc:
                      cluster_name: ext-authz
                    timeout: 0.5s
                  include_peer_certificate: true
              # The xds matcher evaluates dynamic metadata to decide whether to invoke ext_authz.
              # We use matcher_list with custom_match because DynamicMetadataInput returns a custom
              # MetadataMatchData type that requires a custom matcher and not exact_match_map.
              xds_matcher:
                matcher_list:
                  matchers:
                  - predicate:
                      single_predicate:
                        input:
                          name: envoy.matching.inputs.dynamic_metadata
                          typed_config:
                            "@type": type.googleapis.com/envoy.extensions.matching.common_inputs.network.v3.DynamicMetadataInput
                            filter: envoy.filters.http.ext_authz
                            path:
                            - key: require_auth
                        custom_match:
                          name: envoy.matching.matchers.metadata_matcher
                          typed_config:
                            "@type": type.googleapis.com/envoy.extensions.matching.input_matchers.metadata.v3.Metadata
                            value:
                              string_match:
                                exact: "false"
                    # When require_auth is "false", skip ext_authz.
                    on_match:
                      action:
                        name: skip
                        typed_config:
                          "@type": type.googleapis.com/envoy.extensions.filters.common.matcher.action.v3.SkipFilter
          - name: envoy.filters.http.router
            typed_config:
              "@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router

  clusters:
  - name: ext-authz
    type: STATIC
    typed_extension_protocol_options:
      envoy.extensions.upstreams.http.v3.HttpProtocolOptions:
        "@type": type.googleapis.com/envoy.extensions.upstreams.http.v3.HttpProtocolOptions
        explicit_http_config:
          http2_protocol_options: {}
    load_assignment:
      cluster_name: ext-authz
      endpoints:
      - lb_endpoints:
        - endpoint:
            address:
              socket_address:
                address: 127.0.0.1
                port_value: 10003
  - name: upstream_com
    type: LOGICAL_DNS
    # Comment out the following line to test on v6 networks.
    dns_lookup_family: V4_ONLY
    lb_policy: ROUND_ROBIN
    load_assignment:
      cluster_name: service_upstream_com
      endpoints:
      - lb_endpoints:
        - endpoint:
            address:
              socket_address:
                address: upstream.com
                port_value: 443
    transport_socket:
      name: envoy.transport_sockets.tls
      typed_config:
        "@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.UpstreamTlsContext
        sni: upstream.com
