5
5
import io .kafbat .ui .service .ClustersStorage ;
6
6
import java .net .URLDecoder ;
7
7
import java .nio .charset .StandardCharsets ;
8
+ import java .util .Set ;
8
9
import java .util .regex .Pattern ;
9
10
import lombok .RequiredArgsConstructor ;
10
11
import org .jetbrains .annotations .NotNull ;
@@ -23,6 +24,10 @@ public class ReadOnlyModeFilter implements WebFilter {
23
24
private static final Pattern CLUSTER_NAME_REGEX =
24
25
Pattern .compile ("/api/clusters/(?<clusterName>[^/]++)" );
25
26
27
+ private static final Set <Pattern > SAFE_ENDPOINTS = Set .of (
28
+ Pattern .compile ("/api/clusters/[^/]+/topics/[^/]+/(smartfilters)$" )
29
+ );
30
+
26
31
private final ClustersStorage clustersStorage ;
27
32
28
33
@ NotNull
@@ -35,10 +40,12 @@ public Mono<Void> filter(ServerWebExchange exchange, @NotNull WebFilterChain cha
35
40
36
41
var path = exchange .getRequest ().getPath ().pathWithinApplication ().value ();
37
42
var decodedPath = URLDecoder .decode (path , StandardCharsets .UTF_8 );
43
+
38
44
var matcher = CLUSTER_NAME_REGEX .matcher (decodedPath );
39
45
if (!matcher .find ()) {
40
46
return chain .filter (exchange );
41
47
}
48
+
42
49
var clusterName = matcher .group ("clusterName" );
43
50
var kafkaCluster = clustersStorage .getClusterByName (clusterName )
44
51
.orElseThrow (
@@ -49,6 +56,15 @@ public Mono<Void> filter(ServerWebExchange exchange, @NotNull WebFilterChain cha
49
56
return chain .filter (exchange );
50
57
}
51
58
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
+
52
68
return Mono .error (ReadOnlyModeException ::new );
53
69
}
54
70
}
0 commit comments