Skip to content

Commit ad1ce7e

Browse files
authored
Move isOperator to x-pack-core (#122468)
This moves from `isOperator(ThreadContext`) method from org.elasticsearch.xpack.security.operator.OperatorPrivileges to org.elasticsearch.xpack.core.security.operator.OperatorPrivilegesUtil so that it can be used by modules that depend on x-pack-core (without needing to depend on x-pack-security)
1 parent 5c00341 commit ad1ce7e

File tree

5 files changed

+27
-16
lines changed

5 files changed

+27
-16
lines changed

x-pack/plugin/core/src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@
172172
exports org.elasticsearch.xpack.core.security.authz.store;
173173
exports org.elasticsearch.xpack.core.security.authz.support;
174174
exports org.elasticsearch.xpack.core.security.authz;
175+
exports org.elasticsearch.xpack.core.security.operator;
175176
exports org.elasticsearch.xpack.core.security.support;
176177
exports org.elasticsearch.xpack.core.security.user;
177178
exports org.elasticsearch.xpack.core.security.xcontent;

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/api/filtering/ApiFilteringActionFilter.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
import org.elasticsearch.action.support.MappedActionFilter;
1515
import org.elasticsearch.common.util.concurrent.ThreadContext;
1616
import org.elasticsearch.tasks.Task;
17-
import org.elasticsearch.xpack.core.security.authc.AuthenticationField;
17+
18+
import static org.elasticsearch.xpack.core.security.operator.OperatorPrivilegesUtil.isOperator;
1819

1920
public abstract class ApiFilteringActionFilter<Res extends ActionResponse> implements MappedActionFilter {
2021

@@ -45,7 +46,7 @@ public <Request extends ActionRequest, Response extends ActionResponse> void app
4546
ActionFilterChain<Request, Response> chain
4647
) {
4748
final ActionListener<Response> responseFilteringListener;
48-
if (isOperator() == false && actionName.equals(action)) {
49+
if (isOperator(threadContext) == false && actionName.equals(action)) {
4950
responseFilteringListener = listener.map(this::filter);
5051
} else {
5152
responseFilteringListener = listener;
@@ -62,11 +63,5 @@ private <Response extends ActionResponse> Response filter(Response response) thr
6263
}
6364
}
6465

65-
private boolean isOperator() {
66-
return AuthenticationField.PRIVILEGE_CATEGORY_VALUE_OPERATOR.equals(
67-
threadContext.getHeader(AuthenticationField.PRIVILEGE_CATEGORY_KEY)
68-
);
69-
}
70-
7166
protected abstract Res filterResponse(Res response) throws Exception;
7267
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.core.security.operator;
9+
10+
import org.elasticsearch.common.util.concurrent.ThreadContext;
11+
import org.elasticsearch.xpack.core.security.authc.AuthenticationField;
12+
13+
public class OperatorPrivilegesUtil {
14+
public static boolean isOperator(ThreadContext threadContext) {
15+
return AuthenticationField.PRIVILEGE_CATEGORY_VALUE_OPERATOR.equals(
16+
threadContext.getHeader(AuthenticationField.PRIVILEGE_CATEGORY_KEY)
17+
);
18+
}
19+
}

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/user/TransportAuthenticateAction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
import org.elasticsearch.xpack.core.security.action.user.AuthenticateRequest;
2020
import org.elasticsearch.xpack.core.security.action.user.AuthenticateResponse;
2121
import org.elasticsearch.xpack.core.security.authc.Authentication;
22+
import org.elasticsearch.xpack.core.security.operator.OperatorPrivilegesUtil;
2223
import org.elasticsearch.xpack.core.security.user.AnonymousUser;
2324
import org.elasticsearch.xpack.core.security.user.InternalUser;
2425
import org.elasticsearch.xpack.core.security.user.User;
25-
import org.elasticsearch.xpack.security.operator.OperatorPrivileges;
2626

2727
public class TransportAuthenticateAction extends HandledTransportAction<AuthenticateRequest, AuthenticateResponse> {
2828

@@ -61,7 +61,7 @@ protected void doExecute(Task task, AuthenticateRequest request, ActionListener<
6161
listener.onResponse(
6262
new AuthenticateResponse(
6363
authentication.maybeAddAnonymousRoles(anonymousUser),
64-
OperatorPrivileges.isOperator(securityContext.getThreadContext())
64+
OperatorPrivilegesUtil.isOperator(securityContext.getThreadContext())
6565
)
6666
);
6767
}

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/operator/OperatorPrivileges.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import org.elasticsearch.xpack.core.security.user.User;
2626
import org.elasticsearch.xpack.security.Security;
2727

28+
import static org.elasticsearch.xpack.core.security.operator.OperatorPrivilegesUtil.isOperator;
29+
2830
public class OperatorPrivileges {
2931

3032
private static final Logger logger = LogManager.getLogger(OperatorPrivileges.class);
@@ -35,12 +37,6 @@ public class OperatorPrivileges {
3537
Setting.Property.NodeScope
3638
);
3739

38-
public static boolean isOperator(ThreadContext threadContext) {
39-
return AuthenticationField.PRIVILEGE_CATEGORY_VALUE_OPERATOR.equals(
40-
threadContext.getHeader(AuthenticationField.PRIVILEGE_CATEGORY_KEY)
41-
);
42-
}
43-
4440
public interface OperatorPrivilegesService {
4541
/**
4642
* Set a ThreadContext Header {@link AuthenticationField#PRIVILEGE_CATEGORY_KEY} if authentication

0 commit comments

Comments
 (0)