Control Headers and Routing (Deprecated)

The mixer policy is deprecated in Istio 1.5 and not recommended for production usage.

Consider using Envoy ext_authz filter, lua filter, or write a filter using the Envoy-wasm sandbox.

This task demonstrates how to use a policy adapter to manipulate request headers and routing.

Before you begin

  • Set up Istio on Kubernetes by following the instructions in the Installation guide.

    Policy enforcement must be enabled in your cluster for this task. Follow the steps in Enabling Policy Enforcement to ensure that policy enforcement is enabled.

  • Follow the set-up instructions in the ingress task to configure an ingress using a gateway.

  • Customize the virtual service configuration for the httpbin service containing two route rules that allow traffic for paths /headers and /status:

    1. $ kubectl apply -f - <<EOF
    2. apiVersion: networking.istio.io/v1alpha3
    3. kind: VirtualService
    4. metadata:
    5. name: httpbin
    6. spec:
    7. hosts:
    8. - "*"
    9. gateways:
    10. - httpbin-gateway
    11. http:
    12. - match:
    13. - uri:
    14. prefix: /headers
    15. - uri:
    16. prefix: /status
    17. route:
    18. - destination:
    19. port:
    20. number: 8000
    21. host: httpbin
    22. EOF

Output-producing adapters

In this task, we are using a sample policy adapter keyval. In addition to a policy check result, this adapter returns an output with a single field called value. The adapter is configured with a lookup table, which it uses to populate the output value, or return NOT_FOUND error status if the input instance key is not present in the lookup table.

  1. Deploy the demo adapter:

    1. $ kubectl run keyval --image=gcr.io/istio-testing/keyval:release-1.1 --namespace istio-system --port 9070 --expose
  2. Enable the keyval adapter by deploying its template and configuration descriptors:

    ZipZip

    1. $ kubectl apply -f @samples/httpbin/policy/keyval-template.yaml@
    2. $ kubectl apply -f @samples/httpbin/policy/keyval.yaml@
  3. Create a handler for the demo adapter with a fixed lookup table:

    1. $ kubectl apply -f - <<EOF
    2. apiVersion: config.istio.io/v1alpha2
    3. kind: handler
    4. metadata:
    5. name: keyval
    6. namespace: istio-system
    7. spec:
    8. adapter: keyval
    9. connection:
    10. address: keyval:9070
    11. params:
    12. table:
    13. jason: admin
    14. EOF
  4. Create an instance for the handler with the user request header as a lookup key:

    1. $ kubectl apply -f - <<EOF
    2. apiVersion: config.istio.io/v1alpha2
    3. kind: instance
    4. metadata:
    5. name: keyval
    6. namespace: istio-system
    7. spec:
    8. template: keyval
    9. params:
    10. key: request.headers["user"] | ""
    11. EOF

Request header operations

  1. Ensure the httpbin service is accessible through the ingress gateway:

    1. $ curl http://$INGRESS_HOST:$INGRESS_PORT/headers
    2. {
    3. "headers": {
    4. "Accept": "*/*",
    5. "Content-Length": "0",
    6. ...
    7. "X-Envoy-Internal": "true"
    8. }
    9. }

    The output should be the request headers as they are received by the httpbin service.

  2. Create a rule for the demo adapter:

    1. $ kubectl apply -f - <<EOF
    2. apiVersion: config.istio.io/v1alpha2
    3. kind: rule
    4. metadata:
    5. name: keyval
    6. namespace: istio-system
    7. spec:
    8. actions:
    9. - handler: keyval.istio-system
    10. instances: [ keyval ]
    11. name: x
    12. requestHeaderOperations:
    13. - name: user-group
    14. values: [ x.output.value ]
    15. EOF
  3. Issue a new request to the ingress gateway with the header key set to value jason:

    1. $ curl -Huser:jason http://$INGRESS_HOST:$INGRESS_PORT/headers
    2. {
    3. "headers": {
    4. "Accept": "*/*",
    5. "Content-Length": "0",
    6. "User": "jason",
    7. "User-Agent": "curl/7.58.0",
    8. "User-Group": "admin",
    9. ...
    10. "X-Envoy-Internal": "true"
    11. }
    12. }

    Note the presence of the user-group header with the value derived from the rule application of the adapter. The expression x.output.value in the rule evaluates to the populated value field returned by the keyval adapter.

  4. Modify the rule to rewrite the URI path to a different virtual service route if the check succeeds:

    1. $ kubectl apply -f - <<EOF
    2. apiVersion: config.istio.io/v1alpha2
    3. kind: rule
    4. metadata:
    5. name: keyval
    6. namespace: istio-system
    7. spec:
    8. match: source.labels["istio"] == "ingressgateway"
    9. actions:
    10. - handler: keyval.istio-system
    11. instances: [ keyval ]
    12. requestHeaderOperations:
    13. - name: :path
    14. values: [ '"/status/418"' ]
    15. EOF
  5. Repeat the request to the ingress gateway:

    1. $ curl -Huser:jason -I http://$INGRESS_HOST:$INGRESS_PORT/headers
    2. HTTP/1.1 418 Unknown
    3. server: istio-envoy
    4. ...

    Note that the ingress gateway changed the route after the rule application of the policy adapter. The modified request may use a different route and destination and is subject to the traffic management configuration.

    The modified request is not checked again by the policy engine within the same proxy. Therefore, we recommend to use this feature in gateways, so that the server-side policy checks take effect.

Cleanup

Delete the policy resources for the demo adapter:

  1. $ kubectl delete rule/keyval handler/keyval instance/keyval adapter/keyval template/keyval -n istio-system
  2. $ kubectl delete service keyval -n istio-system
  3. $ kubectl delete deployment keyval -n istio-system

Complete the clean-up instructions in ingress task.

See also

App Identity and Access Adapter

Using Istio to secure multi-cloud Kubernetes applications with zero code changes.

Mixer and the SPOF Myth

Improving availability and reducing latency.

Mixer Adapter Model

Provides an overview of Mixer’s plug-in architecture.

Denials and White/Black Listing (Deprecated)

Shows how to control access to a service using simple denials or white/black listing.

Enabling Policy Enforcement (Deprecated)

This task shows you how to enable Istio policy enforcement.

Enabling Rate Limits (Deprecated)

This task shows you how to use Istio to dynamically limit the traffic to a service.