Skip to content

Commit 2c8e63a

Browse files
committed
Merge branch 'flush-user-cache' of https://github.com/dlin2028/security into flush-user-cache
Signed-off-by: David Lin <dlin2028@gmail.com>
2 parents cc6c4ac + e1a5b3f commit 2c8e63a

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

src/main/java/org/opensearch/security/dlic/rest/api/FlushCacheApiAction.java

+20-10
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,21 @@ public List<Route> routes() {
6262
}
6363

6464
private void flushCacheApiRequestHandlers(RequestHandler.RequestHandlersBuilder requestHandlersBuilder) {
65+
requestHandlersBuilder.allMethodsNotImplemented().override(Method.DELETE, (channel, request, client) -> {
66+
final ConfigUpdateRequest configUpdateRequest;
67+
if (request.path().contains("/user/")) {
68+
// Extract the username from the request
69+
final String username = request.param("username");
70+
if (username == null || username.isEmpty()) {
71+
internalSeverError(channel, "No username provided for cache invalidation.");
72+
return;
73+
}
74+
// Validate and handle user-specific cache invalidation
75+
configUpdateRequest = new ConfigUpdateRequest(CType.INTERNALUSERS.toLCString(), new String[] { username });
76+
} else {
77+
configUpdateRequest = new ConfigUpdateRequest(CType.lcStringValues().toArray(new String[0]));
78+
}
79+
client.execute(ConfigUpdateAction.INSTANCE, configUpdateRequest, new ActionListener<>() {
6580
requestHandlersBuilder.allMethodsNotImplemented().override(Method.DELETE, (channel, request, client) -> {
6681
final ConfigUpdateRequest configUpdateRequest;
6782
if (request.path().contains("/user/")) {
@@ -105,16 +120,11 @@ public void onResponse(ConfigUpdateResponse configUpdateResponse) {
105120
ok(channel, "Cache flushed successfully.");
106121
}
107122

108-
@Override
109-
public void onFailure(final Exception e) {
110-
LOGGER.error("Cannot flush cache due to", e);
111-
internalSeverError(channel, "Cannot flush cache due to " + e.getMessage() + ".");
112-
}
113-
@Override
114-
public void onFailure(final Exception e) {
115-
LOGGER.error("Cannot flush cache due to", e);
116-
internalServerError(channel, "Cannot flush cache due to " + e.getMessage() + ".");
117-
}
123+
@Override
124+
public void onFailure(final Exception e) {
125+
LOGGER.error("Cannot flush cache due to", e);
126+
internalSeverError(channel, "Cannot flush cache due to " + e.getMessage() + ".");
127+
}
118128

119129
});
120130
});

0 commit comments

Comments
 (0)