Skip to content

Commit 38c446d

Browse files
authored
BE: Allow smart filters endpoint in r/o mode (#277)
1 parent 42c6a43 commit 38c446d

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

api/src/main/java/io/kafbat/ui/config/ReadOnlyModeFilter.java

+16
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.kafbat.ui.service.ClustersStorage;
66
import java.net.URLDecoder;
77
import java.nio.charset.StandardCharsets;
8+
import java.util.Set;
89
import java.util.regex.Pattern;
910
import lombok.RequiredArgsConstructor;
1011
import org.jetbrains.annotations.NotNull;
@@ -23,6 +24,10 @@ public class ReadOnlyModeFilter implements WebFilter {
2324
private static final Pattern CLUSTER_NAME_REGEX =
2425
Pattern.compile("/api/clusters/(?<clusterName>[^/]++)");
2526

27+
private static final Set<Pattern> SAFE_ENDPOINTS = Set.of(
28+
Pattern.compile("/api/clusters/[^/]+/topics/[^/]+/(smartfilters)$")
29+
);
30+
2631
private final ClustersStorage clustersStorage;
2732

2833
@NotNull
@@ -35,10 +40,12 @@ public Mono<Void> filter(ServerWebExchange exchange, @NotNull WebFilterChain cha
3540

3641
var path = exchange.getRequest().getPath().pathWithinApplication().value();
3742
var decodedPath = URLDecoder.decode(path, StandardCharsets.UTF_8);
43+
3844
var matcher = CLUSTER_NAME_REGEX.matcher(decodedPath);
3945
if (!matcher.find()) {
4046
return chain.filter(exchange);
4147
}
48+
4249
var clusterName = matcher.group("clusterName");
4350
var kafkaCluster = clustersStorage.getClusterByName(clusterName)
4451
.orElseThrow(
@@ -49,6 +56,15 @@ public Mono<Void> filter(ServerWebExchange exchange, @NotNull WebFilterChain cha
4956
return chain.filter(exchange);
5057
}
5158

59+
var isSafeEndpoint = SAFE_ENDPOINTS
60+
.stream()
61+
.parallel()
62+
.anyMatch(endpoint -> endpoint.matcher(decodedPath).matches());
63+
64+
if (isSafeEndpoint) {
65+
return chain.filter(exchange);
66+
}
67+
5268
return Mono.error(ReadOnlyModeException::new);
5369
}
5470
}

0 commit comments

Comments
 (0)