Skip to content

Commit

Permalink
Renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
loicgreffier committed Aug 9, 2024
1 parent 3904203 commit 8a1e6c1
Show file tree
Hide file tree
Showing 53 changed files with 707 additions and 516 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,23 @@ public class AclController extends NamespacedResourceController {
* @return A list of ACLs
*/
@Get("{?limit}")
public List<AccessControlEntry> list(String namespace, Optional<AclLimit> limit,
public List<AccessControlEntry> list(String namespace,
Optional<AclLimit> limit,
@QueryValue(defaultValue = "*") String name) {
Namespace ns = getNamespace(namespace);
return switch (limit.orElse(AclLimit.ALL)) {
case GRANTEE -> aclService.findByWildcardNameGrantedToNamespace(ns, name)
case GRANTEE -> aclService.findAllGrantedToNamespaceByWildcardName(ns, name)
.stream()
.sorted(Comparator.comparing((AccessControlEntry acl) -> acl.getMetadata().getNamespace()))
.toList();
case GRANTOR -> aclService.findByWildcardNameGrantedByNamespace(ns, name)
case GRANTOR -> aclService.findAllGrantedByNamespaceByWildcardName(ns, name)
.stream()
.sorted(Comparator.comparing(acl -> acl.getSpec().getGrantedTo()))
.toList();
default -> aclService.findByWildcardNameRelatedToNamespace(ns, name)
default -> aclService.findAllRelatedToNamespaceByWildcardName(ns, name)
.stream()
.sorted(Comparator.comparing((AccessControlEntry acl) -> acl.getMetadata().getNamespace())
.sorted(Comparator
.comparing((AccessControlEntry acl) -> acl.getMetadata().getNamespace())
.thenComparing(acl -> acl.getSpec().getGrantedTo()))
.toList();
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private void verifyInternalTopic()
"The topic " + kafkaTopic + " should have only 1 partition but has " + numPartitions + ".");
}

if (description.partitions().get(0).replicas().size() < kafkaStoreProperties.getReplicationFactor()
if (description.partitions().getFirst().replicas().size() < kafkaStoreProperties.getReplicationFactor()
&& log.isWarnEnabled()) {
log.warn("The replication factor of the topic " + kafkaTopic + " is less than the desired one of "
+ kafkaStoreProperties.getReplicationFactor()
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/com/michelin/ns4kafka/service/AclService.java
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ public List<AccessControlEntry> findAllGrantedByNamespace(Namespace namespace) {
}

/**
* Find all ACLs that a given namespace granted to other namespaces.
* Find all ACLs where the given namespace is either the grantor or the grantee, or the ACL is public.
*
* @param namespace The namespace
* @return A list of ACLs
Expand All @@ -299,11 +299,11 @@ public List<AccessControlEntry> findAllRelatedToNamespace(Namespace namespace) {
* @param name The name parameter
* @return A list of ACLs
*/
public List<AccessControlEntry> findByWildcardNameGrantedToNamespace(Namespace namespace, String name) {
List<String> nameFilterPatterns = RegexUtils.wildcardStringsToRegexPatterns(List.of(name));
public List<AccessControlEntry> findAllGrantedToNamespaceByWildcardName(Namespace namespace, String name) {
List<String> nameFilterPatterns = RegexUtils.convertWildcardStringsToRegex(List.of(name));
return findAllGrantedToNamespace(namespace)
.stream()
.filter(acl -> RegexUtils.filterByPattern(acl.getMetadata().getName(), nameFilterPatterns))
.filter(acl -> RegexUtils.isResourceCoveredByRegex(acl.getMetadata().getName(), nameFilterPatterns))
.toList();
}

Expand All @@ -314,11 +314,11 @@ public List<AccessControlEntry> findByWildcardNameGrantedToNamespace(Namespace n
* @param name The name parameter
* @return A list of ACLs
*/
public List<AccessControlEntry> findByWildcardNameGrantedByNamespace(Namespace namespace, String name) {
List<String> nameFilterPatterns = RegexUtils.wildcardStringsToRegexPatterns(List.of(name));
public List<AccessControlEntry> findAllGrantedByNamespaceByWildcardName(Namespace namespace, String name) {
List<String> nameFilterPatterns = RegexUtils.convertWildcardStringsToRegex(List.of(name));
return findAllGrantedByNamespace(namespace)
.stream()
.filter(acl -> RegexUtils.filterByPattern(acl.getMetadata().getName(), nameFilterPatterns))
.filter(acl -> RegexUtils.isResourceCoveredByRegex(acl.getMetadata().getName(), nameFilterPatterns))
.toList();
}

Expand All @@ -329,11 +329,11 @@ public List<AccessControlEntry> findByWildcardNameGrantedByNamespace(Namespace n
* @param name The name parameter
* @return A list of ACLs
*/
public List<AccessControlEntry> findByWildcardNameRelatedToNamespace(Namespace namespace, String name) {
List<String> nameFilterPatterns = RegexUtils.wildcardStringsToRegexPatterns(List.of(name));
public List<AccessControlEntry> findAllRelatedToNamespaceByWildcardName(Namespace namespace, String name) {
List<String> nameFilterPatterns = RegexUtils.convertWildcardStringsToRegex(List.of(name));
return findAllRelatedToNamespace(namespace)
.stream()
.filter(acl -> RegexUtils.filterByPattern(acl.getMetadata().getName(), nameFilterPatterns))
.filter(acl -> RegexUtils.isResourceCoveredByRegex(acl.getMetadata().getName(), nameFilterPatterns))
.toList();
}

Expand Down Expand Up @@ -436,13 +436,13 @@ public Optional<AccessControlEntry> findByName(String namespace, String name) {
}

/**
* Check if there is any ACL concerning the given resource.
* Check if the given resource is covered by any given ACLs.
*
* @param acls The OWNER ACL list on resource
* @param resourceName The resource name to check ACL against
* @return true if there is any OWNER ACL concerning the given resource, false otherwise
*/
public boolean isAnyAclOfResource(List<AccessControlEntry> acls, String resourceName) {
public boolean isResourceCoveredByAcls(List<AccessControlEntry> acls, String resourceName) {
return acls
.stream()
.anyMatch(acl -> switch (acl.getSpec().getResourcePatternType()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public List<ConnectCluster> findAllForNamespaceByPermissions(Namespace namespace

return connectClusterRepository.findAllForCluster(namespace.getMetadata().getCluster())
.stream()
.filter(connectCluster -> aclService.isAnyAclOfResource(acls, connectCluster.getMetadata().getName()))
.filter(connectCluster -> aclService.isResourceCoveredByAcls(acls, connectCluster.getMetadata().getName()))
.toList();
}

Expand All @@ -157,10 +157,10 @@ public List<ConnectCluster> findAllForNamespaceWithOwnerPermission(Namespace nam
* @return The list of owned Connect cluster
*/
public List<ConnectCluster> findByWildcardNameWithOwnerPermission(Namespace namespace, String name) {
List<String> nameFilterPatterns = RegexUtils.wildcardStringsToRegexPatterns(List.of(name));
List<String> nameFilterPatterns = RegexUtils.convertWildcardStringsToRegex(List.of(name));
return findAllForNamespaceWithOwnerPermission(namespace)
.stream()
.filter(cc -> RegexUtils.filterByPattern(cc.getMetadata().getName(), nameFilterPatterns))
.filter(cc -> RegexUtils.isResourceCoveredByRegex(cc.getMetadata().getName(), nameFilterPatterns))
.map(this::buildConnectClusterWithDecryptedInformation)
.toList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public List<Connector> findAllForNamespace(Namespace namespace) {
.findResourceOwnerGrantedToNamespace(namespace, AccessControlEntry.ResourceType.CONNECT);
return connectorRepository.findAllForCluster(namespace.getMetadata().getCluster())
.stream()
.filter(connector -> aclService.isAnyAclOfResource(acls, connector.getMetadata().getName()))
.filter(connector -> aclService.isResourceCoveredByAcls(acls, connector.getMetadata().getName()))
.toList();
}

Expand All @@ -74,10 +74,11 @@ public List<Connector> findAllForNamespace(Namespace namespace) {
* @return A list of connectors
*/
public List<Connector> findByWildcardName(Namespace namespace, String name) {
List<String> nameFilterPatterns = RegexUtils.wildcardStringsToRegexPatterns(List.of(name));
List<String> nameFilterPatterns = RegexUtils.convertWildcardStringsToRegex(List.of(name));
return findAllForNamespace(namespace)
.stream()
.filter(connector -> RegexUtils.filterByPattern(connector.getMetadata().getName(), nameFilterPatterns))
.filter(connector -> RegexUtils
.isResourceCoveredByRegex(connector.getMetadata().getName(), nameFilterPatterns))
.toList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ public List<Namespace> findAll() {
* @return The list of namespaces
*/
public List<Namespace> findByWildcardName(String name) {
List<String> nameFilterPatterns = RegexUtils.wildcardStringsToRegexPatterns(List.of(name));
List<String> nameFilterPatterns = RegexUtils.convertWildcardStringsToRegex(List.of(name));
return findAll()
.stream()
.filter(ns -> RegexUtils.filterByPattern(ns.getMetadata().getName(), nameFilterPatterns))
.filter(ns -> RegexUtils.isResourceCoveredByRegex(ns.getMetadata().getName(), nameFilterPatterns))
.toList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ public Optional<ResourceQuota> findForNamespace(String namespace) {
* @return The researched resource quota
*/
public List<ResourceQuota> findByWildcardName(String namespace, String name) {
List<String> nameFilterPatterns = RegexUtils.wildcardStringsToRegexPatterns(List.of(name));
List<String> nameFilterPatterns = RegexUtils.convertWildcardStringsToRegex(List.of(name));
return findForNamespace(namespace)
.stream()
.filter(quota -> RegexUtils.filterByPattern(quota.getMetadata().getName(), nameFilterPatterns))
.filter(quota -> RegexUtils.isResourceCoveredByRegex(quota.getMetadata().getName(), nameFilterPatterns))
.toList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public List<RoleBinding> findAllForNamespace(String namespace) {
* @return The list of associated role bindings
*/
public List<RoleBinding> findByWildcardName(String namespace, String name) {
List<String> nameFilterPatterns = RegexUtils.wildcardStringsToRegexPatterns(List.of(name));
List<String> nameFilterPatterns = RegexUtils.convertWildcardStringsToRegex(List.of(name));
return findAllForNamespace(namespace)
.stream()
.filter(rb -> RegexUtils.filterByPattern(rb.getMetadata().getName(), nameFilterPatterns))
.filter(rb -> RegexUtils.isResourceCoveredByRegex(rb.getMetadata().getName(), nameFilterPatterns))
.toList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public Flux<SchemaList> findAllForNamespace(Namespace namespace) {
.getSubjects(namespace.getMetadata().getCluster())
.filter(subject -> {
String underlyingTopicName = subject.replaceAll("-(key|value)$", "");
return aclService.isAnyAclOfResource(acls, underlyingTopicName);
return aclService.isResourceCoveredByAcls(acls, underlyingTopicName);
})
.map(subject -> SchemaList.builder()
.metadata(Metadata.builder()
Expand All @@ -74,9 +74,10 @@ public Flux<SchemaList> findAllForNamespace(Namespace namespace) {
* @return A list of schemas
*/
public Flux<SchemaList> findByWildcardName(Namespace namespace, String name) {
List<String> nameFilterPatterns = RegexUtils.wildcardStringsToRegexPatterns(List.of(name));
List<String> nameFilterPatterns = RegexUtils.convertWildcardStringsToRegex(List.of(name));
return findAllForNamespace(namespace)
.filter(schemaList -> RegexUtils.filterByPattern(schemaList.getMetadata().getName(), nameFilterPatterns));
.filter(schemaList -> RegexUtils
.isResourceCoveredByRegex(schemaList.getMetadata().getName(), nameFilterPatterns));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ public List<KafkaStream> findAllForNamespace(Namespace namespace) {
* @return A list of Kafka Streams
*/
public List<KafkaStream> findByWildcardName(Namespace namespace, String name) {
List<String> nameFilterPatterns = RegexUtils.wildcardStringsToRegexPatterns(List.of(name));
List<String> nameFilterPatterns = RegexUtils.convertWildcardStringsToRegex(List.of(name));
return findAllForNamespace(namespace)
.stream()
.filter(stream -> RegexUtils.filterByPattern(stream.getMetadata().getName(), nameFilterPatterns))
.filter(stream -> RegexUtils.isResourceCoveredByRegex(stream.getMetadata().getName(), nameFilterPatterns))
.toList();
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/michelin/ns4kafka/service/TopicService.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public List<Topic> findAllForNamespace(Namespace namespace) {
.findResourceOwnerGrantedToNamespace(namespace, AccessControlEntry.ResourceType.TOPIC);
return topicRepository.findAllForCluster(namespace.getMetadata().getCluster())
.stream()
.filter(topic -> aclService.isAnyAclOfResource(acls, topic.getMetadata().getName()))
.filter(topic -> aclService.isResourceCoveredByAcls(acls, topic.getMetadata().getName()))
.toList();
}

Expand All @@ -80,10 +80,10 @@ public List<Topic> findAllForNamespace(Namespace namespace) {
* @return A list of topics
*/
public List<Topic> findByWildcardName(Namespace namespace, String name) {
List<String> nameFilterPatterns = RegexUtils.wildcardStringsToRegexPatterns(List.of(name));
List<String> nameFilterPatterns = RegexUtils.convertWildcardStringsToRegex(List.of(name));
return findAllForNamespace(namespace)
.stream()
.filter(topic -> RegexUtils.filterByPattern(topic.getMetadata().getName(), nameFilterPatterns))
.filter(topic -> RegexUtils.isResourceCoveredByRegex(topic.getMetadata().getName(), nameFilterPatterns))
.toList();
}

Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/michelin/ns4kafka/util/RegexUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ public class RegexUtils {
* @param wildcardStrings The list of wildcard strings
* @return A list of regex patterns
*/
public static List<String> wildcardStringsToRegexPatterns(List<String> wildcardStrings) {
return wildcardStrings.stream()
public static List<String> convertWildcardStringsToRegex(List<String> wildcardStrings) {
return wildcardStrings
.stream()
.map(wildcardString -> "^" + wildcardString
.replace(".", "\\.")
.replace("*", ".*")
Expand All @@ -33,8 +34,9 @@ public static List<String> wildcardStringsToRegexPatterns(List<String> wildcardS
* @param regexPatterns The regex patterns
* @return true if any regex pattern matches the resourceName, false otherwise
*/
public static boolean filterByPattern(String resourceName, List<String> regexPatterns) {
return regexPatterns.stream()
public static boolean isResourceCoveredByRegex(String resourceName, List<String> regexPatterns) {
return regexPatterns
.stream()
.anyMatch(pattern -> Pattern.compile(pattern).matcher(resourceName).matches());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@ void shouldListAclsWithoutNameParameter() {
.build();

when(namespaceService.findByName("test")).thenReturn(Optional.of(namespace));
when(aclService.findByWildcardNameGrantedToNamespace(namespace, "*")).thenReturn(
when(aclService.findAllGrantedToNamespaceByWildcardName(namespace, "*")).thenReturn(
List.of(aceTopicPrefixedOwnerAdminToTest, aceConnectPrefixedOwnerAdminToTest,
aceTopicPrefixedReadNamespaceOtherToTest, aceTopicPrefixedReadAdminToAll));
when(aclService.findByWildcardNameGrantedByNamespace(namespace, "*"))
when(aclService.findAllGrantedByNamespaceByWildcardName(namespace, "*"))
.thenReturn(List.of(aceTopicPrefixedReadTestToNamespaceOther));
when(aclService.findByWildcardNameRelatedToNamespace(namespace, "*")).thenReturn(
when(aclService.findAllRelatedToNamespaceByWildcardName(namespace, "*")).thenReturn(
List.of(aceTopicPrefixedReadTestToNamespaceOther, aceTopicPrefixedOwnerAdminToTest,
aceConnectPrefixedOwnerAdminToTest, aceTopicPrefixedReadNamespaceOtherToTest,
aceTopicPrefixedReadAdminToAll));
Expand Down Expand Up @@ -219,15 +219,15 @@ void shouldListAclsWithNameParameter() {
.build();

when(namespaceService.findByName("test")).thenReturn(Optional.of(namespace));
when(aclService.findByWildcardNameGrantedToNamespace(namespace, "aclGrantedToNamespace"))
when(aclService.findAllGrantedToNamespaceByWildcardName(namespace, "aclGrantedToNamespace"))
.thenReturn(List.of(aclGrantedToNamespace));
when(aclService.findByWildcardNameGrantedToNamespace(namespace, "ownerAcl"))
when(aclService.findAllGrantedToNamespaceByWildcardName(namespace, "ownerAcl"))
.thenReturn(List.of());
when(aclService.findByWildcardNameGrantedByNamespace(namespace, "aclGrantedByNamespace"))
when(aclService.findAllGrantedByNamespaceByWildcardName(namespace, "aclGrantedByNamespace"))
.thenReturn(List.of(aclGrantedByNamespace));
when(aclService.findByWildcardNameGrantedToNamespace(namespace, "ownerAcl"))
when(aclService.findAllGrantedToNamespaceByWildcardName(namespace, "ownerAcl"))
.thenReturn(List.of());
when(aclService.findByWildcardNameRelatedToNamespace(namespace, "ownerAcl"))
when(aclService.findAllRelatedToNamespaceByWildcardName(namespace, "ownerAcl"))
.thenReturn(List.of(ownerAcl));

assertEquals(List.of(aclGrantedToNamespace), accessControlListController.list("test",
Expand Down
Loading

0 comments on commit 8a1e6c1

Please sign in to comment.