ACL_OPERATIONS = Arrays.stream(AclOperation.values())
.filter(t -> !(t == AclOperation.UNKNOWN || t == AclOperation.ANY))
.collect(Collectors.toSet());
- private static final String PRINCIPAL_KEY = "principal";
- private static final String PERMISSION_TYPE_KEY = "permissionType";
- private static final String OPERATION_KEY = "operation";
- private static final String HOSTS_KEY = "host";
- public static final String VERSION_KEY = "version";
- public static final int CURRENT_VERSION = 1;
- private static final String ACLS_KEY = "acls";
-
- public final AccessControlEntry ace;
- public final KafkaPrincipal kafkaPrincipal;
-
- public AclEntry(AccessControlEntry ace) {
- super(ace.principal(), ace.host(), ace.operation(), ace.permissionType());
- this.ace = ace;
-
- kafkaPrincipal = ace.principal() == null
- ? null
- : SecurityUtils.parseKafkaPrincipal(ace.principal());
- }
-
- /**
- * Parse JSON representation of ACLs
- * @param bytes of acls json string
- *
- *
- {
- "version": 1,
- "acls": [
- {
- "host":"host1",
- "permissionType": "Deny",
- "operation": "Read",
- "principal": "User:alice"
- }
- ]
- }
- *
- *
- * @return set of AclEntry objects from the JSON string
- */
- public static Set fromBytes(byte[] bytes) throws IOException {
- if (bytes == null || bytes.length == 0)
- return Collections.emptySet();
-
- Optional jsonValue = Json.parseBytes(bytes);
- if (jsonValue.isEmpty())
- return Collections.emptySet();
-
- JsonObject js = jsonValue.get().asJsonObject();
-
- //the acl json version.
- Utils.require(js.apply(VERSION_KEY).to(INT) == CURRENT_VERSION);
-
- Set res = new HashSet<>();
-
- Iterator aclsIter = js.apply(ACLS_KEY).asJsonArray().iterator();
- while (aclsIter.hasNext()) {
- JsonObject itemJs = aclsIter.next().asJsonObject();
- KafkaPrincipal principal = SecurityUtils.parseKafkaPrincipal(itemJs.apply(PRINCIPAL_KEY).to(STRING));
- AclPermissionType permissionType = SecurityUtils.permissionType(itemJs.apply(PERMISSION_TYPE_KEY).to(STRING));
- String host = itemJs.apply(HOSTS_KEY).to(STRING);
- AclOperation operation = SecurityUtils.operation(itemJs.apply(OPERATION_KEY).to(STRING));
-
- res.add(new AclEntry(new AccessControlEntry(principal.toString(),
- host, operation, permissionType)));
- }
-
- return res;
- }
-
- public static Map toJsonCompatibleMap(Set acls) {
- Map res = new HashMap<>();
- res.put(AclEntry.VERSION_KEY, AclEntry.CURRENT_VERSION);
- res.put(AclEntry.ACLS_KEY, acls.stream().map(AclEntry::toMap).collect(Collectors.toList()));
- return res;
- }
-
public static Set supportedOperations(ResourceType resourceType) {
switch (resourceType) {
case TOPIC:
@@ -182,28 +86,4 @@ public static Errors authorizationError(ResourceType resourceType) {
throw new IllegalArgumentException("Authorization error type not known");
}
}
-
- public Map toMap() {
- Map res = new HashMap<>();
- res.put(AclEntry.PRINCIPAL_KEY, principal());
- res.put(AclEntry.PERMISSION_TYPE_KEY, SecurityUtils.permissionTypeName(permissionType()));
- res.put(AclEntry.OPERATION_KEY, SecurityUtils.operationName(operation()));
- res.put(AclEntry.HOSTS_KEY, host());
- return res;
- }
-
- @Override
- public int hashCode() {
- return ace.hashCode();
- }
-
- @Override
- public boolean equals(Object o) {
- return super.equals(o); // to keep spotbugs happy
- }
-
- @Override
- public String toString() {
- return String.format("%s has %s permission for operations: %s from hosts: %s", principal(), permissionType().name(), operation(), host());
- }
}