6.0 KiB
| title | overview | order | layout | type |
|---|---|---|---|---|
| Setting up Basic Access Control | This task shows how to control access to a service using the Kubernetes labels. | 20 | docs | markdown |
{% include home.html %}
This task shows how to control access to a service using the Kubernetes labels.
Before you begin
-
Set up Istio on Kubernetes by following the instructions in the Installation guide.
-
Deploy the BookInfo sample application.
-
Initialize the application version routing to direct
reviewsservice requests from test user "jason" to version v2 and requests from any other user to v3.istioctl create -f samples/bookinfo/kube/route-rule-reviews-test-v2.yaml istioctl create -f samples/bookinfo/kube/route-rule-reviews-v3.yamlNote: if you have conflicting rules that you set in previous tasks, use
istioctl replaceinstead ofistioctl create.Note: if you are using a namespace other than
default, useistioctl -n namespace ...to specify the namespace.
Access control using denials
Using Istio you can control access to a service based on any attributes that are available within Mixer. This simple form of access control is based on conditionally denying requests using Mixer selectors.
Consider the BookInfo sample application where the ratings service is accessed by multiple versions
of the reviews service. We would like to cut off access to version v3 of the reviews service.
-
Point your browser at the BookInfo
productpage(http://$GATEWAY_URL/productpage).If you log in as user "jason", you should see black rating stars with each review, indicating that the
ratingsservice is being called by the "v2" version of thereviewsservice.If you log in as any other user (or logout) you should see red rating stars with each review, indicating that the
ratingsservice is being called by the "v3" version of thereviewsservice. -
Explicitly deny access to version
v3of thereviewsservice.Run the following command to set up the deny rule along with a handler and an instance.
istioctl create -f samples/bookinfo/kube/mixer-rule-deny-label.yamlYou can expect to see the output similar to the following:
Created config denier/default/denyreviewsv3handler at revision 2882105 Created config checknothing/default/denyreviewsv3request at revision 2882106 Created config rule/default/denyreviewsv3 at revision 2882107Notice the following in the
denyreviewsv3rule:match: destination.labels["app"] == "ratings" && source.labels["app"]=="reviews" && source.labels["version"] == "v3"It matches requests coming from the service
reviewswith labelv3to the serviceratings.This rule uses the
denieradapter to deny requests coming from versionv3of the reviews service. The adapter always denies requests with a pre-configured status code and message. The status code and the message is specified in the denier adapter configuration. -
Refresh the
productpagein your browser.If you are logged out or logged in as any user other than "jason" you will no longer see red ratings stars because the
reviews:v3service has been denied access to theratingsservice. In contrast, if you log in as user "jason" (thereviews:v2user) you continue to see the black ratings stars.
Access control using whitelists
Istio also supports attribute-based whitelists and blacklists.
-
Add an adapter definition for the
listcheckeradapter that lists versionsv1, v2:apiVersion: config.istio.io/v1alpha2 kind: listchecker metadata: name: staticversion namespace: default spec: # providerUrl: ordinarily black and white lists are maintained # externally and fetched asynchronously using the providerUrl. overrides: ["v1", "v2"] # overrides provide a static list blacklist: false -
Extract the version label by creating an instance of the
listentrytemplate:apiVersion: config.istio.io/v1alpha2 kind: listentry metadata: name: appversion namespace: default spec: value: source.labels["version"] -
Enable
whitelistchecking for the ratings service:apiVersion: config.istio.io/v1alpha2 kind: rule metadata: name: checkversion namespace: default spec: match: destination.labels["app"] == "ratings" actions: - handler: staticversion.listchecker instances: - appversion.listentry
Cleanup
-
Remove the mixer configuration:
istioctl delete -f /path/to/file.yaml -
Remove the application routing rules:
istioctl delete -f samples/bookinfo/kube/route-rule-reviews-test-v2.yaml istioctl delete -f samples/bookinfo/kube/route-rule-reviews-v3.yaml -
If you are not planning to explore any follow-on tasks, refer to the BookInfo cleanup instructions to shutdown the application.
Further reading
-
Learn how to securely control access based on the service account here.
-
Learn more about Mixer and Mixer Config.
-
Discover the full Attribute Vocabulary.
-
Read the reference guide to Writing Config.
-
Understand the differences between Kubernetes network policies and Istio access control policies from this blog.