Expose and secure a workload with Istio
This tutorial shows how to expose and secure a workload using Istio's built-in security features. You will expose the workload by creating a VirtualService. Then, you will secure access to your workload by adding the JWT validation verified by the Istio security configuration with Authorization Policy and Request Authentication.
Prerequisites
- Sample HttpBin service and sample Function deployed
- JSON Web Token (JWT).
- Set up your custom domain or use a Kyma domain instead.
Depending on whether you use your custom domain or a Kyma domain, export the necessary values as environment variables:
- Custom domain
- Kyma domain
Click to copyexport DOMAIN_TO_EXPOSE_WORKLOADS={DOMAIN_NAME}export GATEWAY=$NAMESPACE/httpbin-gateway
Expose your workload using a Virtual Service
Follow the instructions in the tabs to expose the HttpBin workload or the Function using a VirtualService.
- Expose the HttpBin workload
- Expose the Function
Create a VirtualService:
Click to copycat <<EOF | kubectl apply -f -apiVersion: networking.istio.io/v1alpha3kind: VirtualServicemetadata:name: httpbinnamespace: $NAMESPACEspec:hosts:- "httpbin.$DOMAIN_TO_EXPOSE_WORKLOADS"gateways:- $GATEWAYhttp:- match:- uri:prefix: /route:- destination:port:number: 8000host: httpbin.$NAMESPACE.svc.cluster.localEOF
Secure a workload or the Function using a JWT
To secure the HttpBin workload or the Function using a JWT, create a Request Authentication with Authorization Policy. Workloads with the matchLabels
parameter specified require a JWT for all requests. Follow the instructions in the tabs:
- Secure the Httpbin workload
- Secure the Function
Create the Request Authentication and Authorization Policy resources:
Click to copycat <<EOF | kubectl apply -f -apiVersion: security.istio.io/v1beta1kind: RequestAuthenticationmetadata:name: jwt-auth-httpbinnamespace: $NAMESPACEspec:selector:matchLabels:app: httpbinjwtRules:- issuer: $ISSUERjwksUri: $JWKS_URI---apiVersion: security.istio.io/v1beta1kind: AuthorizationPolicymetadata:name: httpbinnamespace: $NAMESPACEspec:selector:matchLabels:app: httpbinrules:- from:- source:requestPrincipals: ["*"]EOFAccess the workload you secured. You get the code
403 Forbidden
error.Click to copycurl -ik -X GET https://httpbin.$DOMAIN_TO_EXPOSE_WORKLOADS/status/200Now, access the secured workload using the correct JWT. You get the code
200 OK
response.Click to copycurl -ik -X GET https://httpbin.$DOMAIN_TO_EXPOSE_WORKLOADS/status/200 --header "Authorization:Bearer $ACCESS_TOKEN"