https://github.com/jcasbin/kafka-casbin
Kafka authorization plugin based on Casbin
https://github.com/jcasbin/kafka-casbin
auth authorization authz casbin kafka rbac
Last synced: 23 days ago
JSON representation
Kafka authorization plugin based on Casbin
- Host: GitHub
- URL: https://github.com/jcasbin/kafka-casbin
- Owner: jcasbin
- License: apache-2.0
- Created: 2021-10-24T11:47:17.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-10-05T00:23:21.000Z (7 months ago)
- Last Synced: 2025-03-27T17:51:57.514Z (about 1 month ago)
- Topics: auth, authorization, authz, casbin, kafka, rbac
- Language: Java
- Homepage: https://github.com/casbin/jcasbin
- Size: 16.6 KB
- Stars: 1
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# kafka-casbin

[](https://central.sonatype.com/artifact/org.casbin/kafka-casbin)[Kafka](https://kafka.apache.org) authorization plugin based on Casbin.
## Installation
``` xml
org.casbin
kafka-casbin
1.0.0```
## Simple Example```java
public class Main {public static void main(String[] args) throws InstantiationException, IllegalAccessException {
// create the CasbinAuthorizer object.
CasbinAuthorizer authorizer = new CasbinAuthorizer();
Map configs = new HashMap<>();
configs.put("casbin.authorizer.model", "examples/acl_model.conf");
configs.put("casbin.authorizer.policy", "examples/acl_policy.csv");
// Set the configs for the authorizer.
authorizer.configure(configs);// You should create the real serverInfo object
AuthorizerServerInfo serverInfo = mock(AuthorizerServerInfo.class);
when(serverInfo.endpoints()).thenReturn(new ArrayList<>());
authorizer.start(serverInfo);// create ResourcePattern
ResourcePattern TOPIC_RESOURCE = new ResourcePattern(
org.apache.kafka.common.resource.ResourceType.TOPIC,
"target_topic",
PatternType.LITERAL
);
Action action = new Action(AclOperation.READ, TOPIC_RESOURCE, 0, true, true);// Check the permission.
List results =
authorizer.authorize(requestContext("User", "alice"),
Arrays.asList(action));for (AuthorizationResult result : results) {
if (result == AuthorizationResult.ALLOWED) {
System.out.println("有权限");
} else {
System.out.println("无权限");
}
}}
private static AuthorizableRequestContext requestContext(String principalType, String name) {
return new RequestContext(
new RequestHeader(ApiKeys.METADATA, (short) 0, "test-client-id", 123),
"test-connection-id",
InetAddress.getLoopbackAddress(),
new KafkaPrincipal(principalType, name),
new ListenerName("PLAINTEXT"),
SecurityProtocol.PLAINTEXT,
ClientInformation.EMPTY,
false
);
}
}
```## Getting Help
- [jCasbin](https://github.com/casbin/jCasbin)
- [kafka-client](https://github.com/apache/kafka)## License
This project is under Apache 2.0 License. See the [LICENSE](https://github.com/jcasbin/kafka-casbin/blob/master/LICENSE) file for the full license text.