Apache Kafka is a high-performance distributed streaming platform deployed by thousands of companies. In many deployments, administrators require fine-grained access control over Kafka topics to enforce important requirements around confidentiality and integrity.

Goals

This tutorial shows how to enforce fine-grained access control over Kafka topics. In this tutorial you will use OPA to define and enforce an authorization policy stating:

  • Consumers of topics containing Personally Identifiable Information (PII) must be on allow list.
  • Producers to topics with high fanout must be on allow list.

In addition, this tutorial shows how to break up a policy with small helper rules to reuse logic and improve overall readability.

Prerequisites

This tutorial requires Docker Compose to run Kafka, ZooKeeper, and OPA.

Additionally, we’ll use Nginx for serving policy and data bundles to OPA. This component is however easily replaceable by any other bundle server implementation.

Steps

1. Bootstrap the tutorial environment using Docker Compose.

First, let’s create some directories. We’ll create one for our policy files, a second one for built bundles, and a third one or the OPA authorizer plugin.

  1. mkdir policies bundles plugin

Next, create an OPA policy that allows all requests. You will update this policy later in the tutorial.

policies/tutorial.rego:

  1. package kafka.authz
  2. allow := true

With the policy in place, build a bundle from the contents of the policies directory and place it in the bundles directory. The bundles directory will later be mounted into the Nginx container in order to distribute policy updates to OPA.

  1. opa build --bundle policies/ --output bundles/bundle.tar.gz

Kafka Authorizer JAR File

Next, download the latest version of the Open Policy Agent plugin for Kafka authorization plugin from the projects release pages.

Store the plugin in the plugin directory (replace ${version} with the version number of the plugin just downloaded):

  1. mv opa-authorizer-${version}-all.jar plugin/

For more information on how to configure the OPA plugin for Kafka, see the plugin repository.

Next, create a docker-compose.yaml file that runs OPA, Nginx, ZooKeeper, and Kafka.

docker-compose.yaml:

  1. services:
  2. nginx:
  3. image: nginx:1.21.4
  4. volumes:
  5. - "./bundles:/usr/share/nginx/html"
  6. ports:
  7. - "80:80"
  8. opa:
  9. image: openpolicyagent/opa:0.40.0-rootless
  10. ports:
  11. - "8181:8181"
  12. command:
  13. - "run"
  14. - "--server"
  15. - "--set=decision_logs.console=true"
  16. - "--set=services.authz.url=http://nginx"
  17. - "--set=bundles.authz.service=authz"
  18. - "--set=bundles.authz.resource=bundle.tar.gz"
  19. depends_on:
  20. - nginx
  21. zookeeper:
  22. image: confluentinc/cp-zookeeper:6.2.1
  23. ports:
  24. - "2181:2181"
  25. environment:
  26. - ALLOW_ANONYMOUS_LOGIN=yes
  27. - ZOOKEEPER_CLIENT_PORT=2181
  28. broker:
  29. image: confluentinc/cp-kafka:6.2.1
  30. ports:
  31. - "9093:9093"
  32. environment:
  33. # Set cache expiry to low value for development in order to see decisions
  34. KAFKA_OPA_AUTHORIZER_CACHE_EXPIRE_AFTER_SECONDS: 10
  35. KAFKA_OPA_AUTHORIZER_URL: http://opa:8181/v1/data/kafka/authz/allow
  36. KAFKA_AUTHORIZER_CLASS_NAME: org.openpolicyagent.kafka.OpaAuthorizer
  37. KAFKA_BROKER_ID: 1
  38. KAFKA_ZOOKEEPER_CONNECT: "zookeeper:2181"
  39. KAFKA_ADVERTISED_LISTENERS: SSL://localhost:9093
  40. KAFKA_SECURITY_INTER_BROKER_PROTOCOL: SSL
  41. KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
  42. KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
  43. KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
  44. KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
  45. KAFKA_AUTO_CREATE_TOPICS_ENABLE: "true"
  46. KAFKA_SSL_KEYSTORE_FILENAME: server.keystore
  47. KAFKA_SSL_KEYSTORE_CREDENTIALS: credentials.txt
  48. KAFKA_SSL_KEY_CREDENTIALS: credentials.txt
  49. KAFKA_SSL_TRUSTSTORE_FILENAME: server.truststore
  50. KAFKA_SSL_TRUSTSTORE_CREDENTIALS: credentials.txt
  51. KAFKA_SSL_CLIENT_AUTH: required
  52. CLASSPATH: "/plugin/*"
  53. volumes:
  54. - "./plugin:/plugin"
  55. - "./cert/server:/etc/kafka/secrets"
  56. depends_on:
  57. - opa
  58. - zookeeper

Authentication

The Docker Compose file defined above requires SSL client authentication for clients that connect to the broker. Enabling SSL client authentication allows for service identities to be provided as input to your policy. The example below shows the input structure.

  1. {
  2. "action": {
  3. "logIfAllowed": true,
  4. "logIfDenied": true,
  5. "operation": "WRITE",
  6. "resourcePattern": {
  7. "name": "credit-scores",
  8. "patternType": "LITERAL",
  9. "resourceType": "TOPIC",
  10. "unknown": false
  11. },
  12. "resourceReferenceCount": 1
  13. },
  14. "requestContext": {
  15. "clientAddress": "/172.22.0.1",
  16. "clientInformation": {
  17. "softwareName": "apache-kafka-java",
  18. "softwareVersion": "2.8.1"
  19. },
  20. "connectionId": "172.22.0.5:9093-172.22.0.1:62744-2",
  21. "header": {
  22. "headerVersion": 2,
  23. "name": {
  24. "clientId": "consumer-console-producer-63933-1",
  25. "correlationId": 10,
  26. "requestApiKey": 1,
  27. "requestApiVersion": 12
  28. }
  29. },
  30. "listenerName": "SSL",
  31. "principal": {
  32. "name": "CN=anon_producer,OU=Developers",
  33. "principalType": "User"
  34. },
  35. "securityProtocol": "SSL"
  36. }
  37. }

The client identity is extracted from the SSL certificates that clients present when they connect to the broker. The user identity information is encoded in the input.requestContext.principal.name field. This field can be used inside the policy.

A detailed rundown of generating SSL certificates and JKS files required for SSL client authentication is outside the scope of this tutorial, but the plugin repository provides an example script that demonstrates the creation of client certificates for the four different users used in this tutorial:

  • anon_producer
  • anon_consumer
  • pii_consumer
  • fanout_producer

Lets’ download the script and run it:

  1. curl -O https://raw.githubusercontent.com/anderseknert/opa-kafka-plugin/main/example/opa_tutorial/create_cert.sh
  2. chmod +x create_cert.sh
  3. ./create_cert.sh

We should now find a new cert directory created by the script, containing the server and client certificates we’ll need for TLS authentication.

Note: Do not rely on these SSL certificates in real-world scenarios. They are only provided for convenience/test purposes.

If you’d rather set up these users by other means, like one of the available SASL mechanisms Kafka provides, that should work just as well. Just make sure to update the Docker compose file accordingly.

Once you have created the files needed for authentication, you may launch the containers for this tutorial.

  1. docker-compose --project-name opa-kafka-tutorial up

Now that the tutorial environment is running, we can define an authorization policy using OPA and test it.

2. Define a policy to restrict consumer access to topics containing Personally Identifiable Information (PII).

Update the policies/tutorial.rego with the following content.

  1. #-----------------------------------------------------------------------------
  2. # High level policy for controlling access to Kafka.
  3. #
  4. # * Deny operations by default.
  5. # * Allow operations if no explicit denial.
  6. #
  7. # The kafka-authorizer-opa plugin will query OPA for decisions at
  8. # /kafka/authz/allow. If the policy decision is _true_ the request is allowed.
  9. # If the policy decision is _false_ the request is denied.
  10. #-----------------------------------------------------------------------------
  11. package kafka.authz
  12. import future.keywords.in
  13. default allow := false
  14. allow {
  15. not deny
  16. }
  17. deny {
  18. is_read_operation
  19. topic_contains_pii
  20. not consumer_is_allowlisted_for_pii
  21. }
  22. #-----------------------------------------------------------------------------
  23. # Data structures for controlling access to topics. In real-world deployments,
  24. # these data structures could be loaded into OPA as raw JSON data. The JSON
  25. # data could be pulled from external sources like AD, Git, etc.
  26. #-----------------------------------------------------------------------------
  27. consumer_allowlist := {"pii": {"pii_consumer"}}
  28. topic_metadata := {"credit-scores": {"tags": ["pii"]}}
  29. #-----------------------------------
  30. # Helpers for checking topic access.
  31. #-----------------------------------
  32. topic_contains_pii {
  33. "pii" in topic_metadata[topic_name].tags
  34. }
  35. consumer_is_allowlisted_for_pii {
  36. principal.name in consumer_allowlist.pii
  37. }
  38. #-----------------------------------------------------------------------------
  39. # Helpers for processing Kafka operation input. This logic could be split out
  40. # into a separate file and shared. For conciseness, we have kept it all in one
  41. # place.
  42. #-----------------------------------------------------------------------------
  43. is_write_operation {
  44. input.action.operation == "WRITE"
  45. }
  46. is_read_operation {
  47. input.action.operation == "READ"
  48. }
  49. is_topic_resource {
  50. input.action.resourcePattern.resourceType == "TOPIC"
  51. }
  52. topic_name := input.action.resourcePattern.name {
  53. is_topic_resource
  54. }
  55. principal := {"fqn": parsed.CN, "name": cn_parts[0]} {
  56. parsed := parse_user(input.requestContext.principal.name)
  57. cn_parts := split(parsed.CN, ".")
  58. }
  59. # If client certificates aren't used for authentication
  60. else := {"fqn": "", "name": input.requestContext.principal.name}
  61. parse_user(user) := {key: value |
  62. parts := split(user, ",")
  63. [key, value] := split(parts[_], "=")
  64. }

The Kafka authorization plugin is configured to query for the data.kafka.authz.allow decision. If the response is true the operation is allowed, otherwise the operation is denied. When the integration queries OPA it supplies a JSON representation of the operation, resource, client, and principal.

  1. data.kafka.authz.allow
  1. {
  2. "action": {
  3. "logIfAllowed": true,
  4. "logIfDenied": true,
  5. "operation": "READ",
  6. "resourcePattern": {
  7. "name": "credit-scores",
  8. "patternType": "LITERAL",
  9. "resourceType": "TOPIC",
  10. "unknown": false
  11. },
  12. "resourceReferenceCount": 1
  13. },
  14. "requestContext": {
  15. "clientAddress": "/172.22.0.1",
  16. "clientInformation": {
  17. "softwareName": "apache-kafka-java",
  18. "softwareVersion": "2.8.1"
  19. },
  20. "connectionId": "172.22.0.5:9093-172.22.0.1:62744-2",
  21. "header": {
  22. "headerVersion": 2,
  23. "name": {
  24. "clientId": "consumer-console-producer-63933-1",
  25. "correlationId": 10,
  26. "requestApiKey": 1,
  27. "requestApiVersion": 12
  28. }
  29. },
  30. "listenerName": "SSL",
  31. "principal": {
  32. "name": "CN=pii_consumer,OU=developers",
  33. "principalType": "User"
  34. },
  35. "securityProtocol": "SSL"
  36. }
  37. }

With the input value above, the answer is:

  1. true

The ./bundles directory is mounted into the Docker container running Nginx. When the bundle under this directory change, OPA is notified via the bundle API, and the policies are automatically reloaded.

You can update the bundle at any time by rebuilding it.

  1. opa build --bundle policies/ --output bundles/bundle.tar.gz

At this point, you can exercise the policy.

3. Exercise the policy that restricts consumer access to topics containing PII.

This step shows how you can grant fine-grained access to services using Kafka. In this scenario, some services are allowed to read PII data while others are not.

First, run kafka-console-producer to generate some data on the credit-scores topic.

This tutorial uses the kafka-console-producer and kafka-console-consumer scripts provided by Kafka to generate and display Kafka messages. These scripts read from STDIN and write to STDOUT and are frequently used to send and receive data via Kafka over the command line. If you are not familiar with these scripts you can learn more in Kafka’s Quick Start documentation.

  1. docker run -v $(pwd)/cert/client:/tmp/client --rm --network opa-kafka-tutorial_default \
  2. confluentinc/cp-kafka:6.2.1 \
  3. bash -c 'for i in {1..10}; do echo "{\"user\": \"bob\", \"score\": $i}"; done | kafka-console-producer --topic credit-scores --broker-list broker:9093 -producer.config /tmp/client/anon_producer.properties'

This command will send 10 messages to the credit-scores topic. Bob’s credit score seems to be improving.

Next, run kafka-console-consumer and try to read data off the topic. Use the pii_consumer credentials to simulate a service that is allowed to read PII data.

  1. docker run -v $(pwd)/cert/client:/tmp/client --rm --network opa-kafka-tutorial_default \
  2. confluentinc/cp-kafka:6.2.1 \
  3. kafka-console-consumer --bootstrap-server broker:9093 --topic credit-scores --from-beginning --consumer.config /tmp/client/pii_consumer.properties

This command will output the 10 messages sent to the topic in the first part of this step. Once the 10 messages have been printed, exit out of the script (^C).

Finally, run kafka-console-consumer again but this time try to use the anon_consumer credentials. The anon_consumer credentials simulate a service that has not been explicitly granted access to PII data.

  1. docker run -v $(pwd)/cert/client:/tmp/client --rm --network opa-kafka-tutorial_default \
  2. confluentinc/cp-kafka:6.2.1 \
  3. kafka-console-consumer --bootstrap-server broker:9093 --topic credit-scores --from-beginning --consumer.config /tmp/client/anon_consumer.properties

Because the anon_consumer is not allowed to read PII data, the request will be denied and the consumer will output an error message.

  1. Not authorized to read from topic credit-scores.
  2. ...
  3. Processed a total of 0 messages

4. Extend the policy to prevent services from accidentally writing to topics with large fanout.

First, add the following content to the policy file (./policies/tutorial.rego):

  1. deny {
  2. is_write_operation
  3. topic_has_large_fanout
  4. not producer_is_allowlisted_for_large_fanout
  5. }
  6. producer_allowlist := {
  7. "large-fanout": {
  8. "fanout_producer",
  9. }
  10. }
  11. topic_has_large_fanout {
  12. topic_metadata[topic_name].tags[_] == "large-fanout"
  13. }
  14. producer_is_allowlisted_for_large_fanout {
  15. producer_allowlist["large-fanout"][_] == principal.name
  16. }

Next, update the topic_metadata data structure in the same file to indicate that the click-stream topic has a high fanout.

  1. topic_metadata := {
  2. "click-stream": {
  3. "tags": ["large-fanout"],
  4. },
  5. "credit-scores": {
  6. "tags": ["pii"],
  7. }
  8. }

Last, build a bundle from the updated policy.

  1. opa build --bundle policies/ --output bundles/bundle.tar.gz

5. Exercise the policy that restricts producer access to topics with high fanout.

First, run kafka-console-producer and simulate a service with access to the click-stream topic.

  1. docker run -v $(pwd)/cert/client:/tmp/client --rm --network opa-kafka-tutorial_default \
  2. confluentinc/cp-kafka:6.2.1 \
  3. bash -c 'for i in {1..10}; do echo "{\"user\": \"alice\", \"button\": $i}"; done | kafka-console-producer --topic click-stream --broker-list broker:9093 -producer.config /tmp/client/fanout_producer.properties'

Next, run the kafka-console-consumer to confirm that the messages were published.

  1. docker run -v $(pwd)/cert/client:/tmp/client --rm --network opa-kafka-tutorial_default \
  2. confluentinc/cp-kafka:6.2.1 \
  3. kafka-console-consumer --bootstrap-server broker:9093 --topic click-stream --from-beginning --consumer.config /tmp/client/anon_consumer.properties

Once you see the 10 messages produced by the first part of this step, exit the console consumer (^C).

Lastly, run kafka-console-producer to simulate a service that should not have access to high fanout topics.

  1. docker run -v $(pwd)/cert/client:/tmp/client --rm --network opa-kafka-tutorial_default \
  2. confluentinc/cp-kafka:6.2.1 \
  3. bash -c 'echo "{\"user\": \"alice\", \"button\": \"bogus\"}" | kafka-console-producer --topic click-stream --broker-list broker:9093 -producer.config /tmp/client/anon_producer.properties'

Because anon_producer is not authorized to write to high fanout topics, the request will be denied and the producer will output an error message.

  1. Not authorized to access topics: [click-stream]

Wrap Up

Congratulations on finishing the tutorial!

At this point you have learned how to enforce fine-grained access control over Kafka topics. In addition, you have seen how to break down policies into smaller rules that can be reused and improve the overall readability over the policy.

If you want to use the Kafka Authorizer plugin that integrates Kafka with OPA, see the build and install instructions in the opa-kafka-plugin repository.