Skip to content

Commit f333355

Browse files
Merge branch 'main' into add-iae-when-shard-is-non-integer
2 parents d1adf91 + 2404dac commit f333355

File tree

17 files changed

+289
-381
lines changed

17 files changed

+289
-381
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/xcontent/XContentUtils.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,7 @@ public static void addAuthorizationInfo(final XContentBuilder builder, final Map
112112
private static void addSubjectInfo(XContentBuilder builder, Subject subject) throws IOException {
113113
switch (subject.getType()) {
114114
case USER -> builder.array(User.Fields.ROLES.getPreferredName(), subject.getUser().roles());
115-
case API_KEY -> {
116-
addApiKeyInfo(builder, subject);
117-
}
115+
case API_KEY -> addApiKeyInfo(builder, subject);
118116
case SERVICE_ACCOUNT -> builder.field("service_account", subject.getUser().principal());
119117
case CROSS_CLUSTER_ACCESS -> {
120118
builder.startObject("cross_cluster_access");
@@ -129,7 +127,16 @@ private static void addSubjectInfo(XContentBuilder builder, Subject subject) thr
129127
builder.endObject();
130128
}
131129
case CLOUD_API_KEY -> {
132-
// TODO Add cloud API key information here
130+
builder.startObject("cloud_api_key");
131+
Map<String, Object> metadata = subject.getUser().metadata();
132+
builder.field("id", subject.getUser().principal());
133+
Object name = metadata.get(AuthenticationField.API_KEY_NAME_KEY);
134+
if (name instanceof String) {
135+
builder.field("name", name);
136+
}
137+
builder.field("internal", metadata.get(AuthenticationField.API_KEY_INTERNAL_KEY));
138+
builder.array(User.Fields.ROLES.getPreferredName(), subject.getUser().roles());
139+
builder.endObject();
133140
}
134141
}
135142
}

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/AuthenticationTestHelper.java

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,27 @@ public static User randomUser() {
9292
);
9393
}
9494

95+
public static User randomCloudApiKeyUser() {
96+
return randomCloudApiKeyUser(null);
97+
}
98+
99+
public static User randomCloudApiKeyUser(String principal) {
100+
final Map<String, Object> metadata = ESTestCase.randomBoolean()
101+
? null
102+
: Map.ofEntries(
103+
Map.entry(AuthenticationField.API_KEY_NAME_KEY, ESTestCase.randomAlphanumericOfLength(64)),
104+
Map.entry(AuthenticationField.API_KEY_INTERNAL_KEY, ESTestCase.randomBoolean())
105+
);
106+
return new User(
107+
principal == null ? ESTestCase.randomAlphanumericOfLength(64) : principal,
108+
ESTestCase.randomArray(1, 3, String[]::new, () -> "role_" + ESTestCase.randomAlphaOfLengthBetween(3, 8)),
109+
null,
110+
null,
111+
metadata,
112+
true
113+
);
114+
}
115+
95116
public static InternalUser randomInternalUser() {
96117
return ESTestCase.randomFrom(InternalUsers.get());
97118
}
@@ -260,27 +281,14 @@ public static Authentication randomCloudApiKeyAuthentication(User user, String a
260281
if (apiKeyId == null) {
261282
apiKeyId = user != null ? user.principal() : ESTestCase.randomAlphanumericOfLength(64);
262283
}
263-
final Map<String, Object> metadata = ESTestCase.randomBoolean()
264-
? null
265-
: Map.ofEntries(
266-
Map.entry(AuthenticationField.API_KEY_NAME_KEY, ESTestCase.randomAlphanumericOfLength(64)),
267-
Map.entry(AuthenticationField.API_KEY_INTERNAL_KEY, ESTestCase.randomBoolean())
268-
);
269284
if (user == null) {
270-
user = new User(
271-
apiKeyId,
272-
ESTestCase.randomArray(1, 3, String[]::new, () -> "role_" + ESTestCase.randomAlphaOfLengthBetween(3, 8)),
273-
null,
274-
null,
275-
metadata,
276-
true
277-
);
285+
user = randomCloudApiKeyUser(apiKeyId);
278286
}
279287

280288
assert user.principal().equals(apiKeyId) : "user principal must match cloud API key ID";
281289

282290
return Authentication.newCloudApiKeyAuthentication(
283-
AuthenticationResult.success(user, metadata),
291+
AuthenticationResult.success(user, user.metadata()),
284292
"node_" + ESTestCase.randomAlphaOfLengthBetween(3, 8)
285293
);
286294

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/xcontent/XContentUtilsTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
import java.util.stream.Collectors;
2525

2626
import static org.elasticsearch.xpack.core.security.authc.AuthenticationField.API_KEY_ID_KEY;
27+
import static org.elasticsearch.xpack.core.security.authc.AuthenticationField.API_KEY_INTERNAL_KEY;
2728
import static org.elasticsearch.xpack.core.security.authc.AuthenticationField.API_KEY_NAME_KEY;
2829
import static org.elasticsearch.xpack.core.security.authc.AuthenticationField.CROSS_CLUSTER_ACCESS_AUTHENTICATION_KEY;
30+
import static org.hamcrest.Matchers.containsString;
2931
import static org.hamcrest.Matchers.equalTo;
3032

3133
public class XContentUtilsTests extends ESTestCase {
@@ -62,6 +64,21 @@ public void testAddAuthorizationInfoWithApiKey() throws IOException {
6264
assertThat(json, equalTo("{\"authorization\":{\"api_key\":{\"id\":\"" + apiKeyId + "\",\"name\":\"" + apiKeyName + "\"}}}"));
6365
}
6466

67+
public void testAddAuthorizationInfoWithCloudApiKey() throws IOException {
68+
User user = AuthenticationTestHelper.randomCloudApiKeyUser();
69+
Authentication authentication = AuthenticationTestHelper.randomCloudApiKeyAuthentication(user);
70+
String json = generateJson(Map.of(AuthenticationField.AUTHENTICATION_KEY, authentication.encode()));
71+
assertThat(json, containsString("{\"authorization\":{\"cloud_api_key\":{\"id\":\"" + user.principal()));
72+
assertThat(json, containsString("\"internal\":" + user.metadata().getOrDefault(API_KEY_INTERNAL_KEY, null)));
73+
if (user.metadata().containsKey(API_KEY_NAME_KEY)) {
74+
assertThat(json, containsString("\"name\":\"" + user.metadata().getOrDefault(API_KEY_NAME_KEY, null) + "\""));
75+
}
76+
for (String role : user.roles()) {
77+
assertThat(json, containsString(role));
78+
}
79+
80+
}
81+
6582
public void testAddAuthorizationInfoWithServiceAccount() throws IOException {
6683
String account = "elastic/" + randomFrom("kibana", "fleet-server");
6784
User user = new User(account);

x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecker.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
import org.apache.logging.log4j.LogManager;
1111
import org.apache.logging.log4j.Logger;
12-
import org.elasticsearch.cluster.ClusterState;
13-
import org.elasticsearch.common.TriConsumer;
1412
import org.elasticsearch.xcontent.NamedXContentRegistry;
1513
import org.elasticsearch.xpack.core.deprecation.DeprecationIssue;
1614
import org.elasticsearch.xpack.core.transform.transforms.TransformConfig;
@@ -25,26 +23,19 @@
2523
public class ClusterDeprecationChecker {
2624

2725
private static final Logger logger = LogManager.getLogger(ClusterDeprecationChecker.class);
28-
private final List<TriConsumer<ClusterState, List<TransformConfig>, List<DeprecationIssue>>> CHECKS = List.of(
29-
this::checkTransformSettings
30-
);
3126
private final NamedXContentRegistry xContentRegistry;
3227

3328
ClusterDeprecationChecker(NamedXContentRegistry xContentRegistry) {
3429
this.xContentRegistry = xContentRegistry;
3530
}
3631

37-
public List<DeprecationIssue> check(ClusterState clusterState, List<TransformConfig> transformConfigs) {
32+
public List<DeprecationIssue> check(List<TransformConfig> transformConfigs) {
3833
List<DeprecationIssue> allIssues = new ArrayList<>();
39-
CHECKS.forEach(check -> check.apply(clusterState, transformConfigs, allIssues));
34+
checkTransformSettings(transformConfigs, allIssues);
4035
return allIssues;
4136
}
4237

43-
private void checkTransformSettings(
44-
ClusterState clusterState,
45-
List<TransformConfig> transformConfigs,
46-
List<DeprecationIssue> allIssues
47-
) {
38+
private void checkTransformSettings(List<TransformConfig> transformConfigs, List<DeprecationIssue> allIssues) {
4839
for (var config : transformConfigs) {
4940
try {
5041
allIssues.addAll(config.checkForDeprecations(xContentRegistry));

x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/DataStreamDeprecationChecker.java

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
import org.elasticsearch.Version;
1111
import org.elasticsearch.action.support.IndicesOptions;
12-
import org.elasticsearch.cluster.ClusterState;
1312
import org.elasticsearch.cluster.metadata.DataStream;
1413
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
14+
import org.elasticsearch.cluster.metadata.ProjectMetadata;
1515
import org.elasticsearch.index.Index;
1616
import org.elasticsearch.xpack.core.deprecation.DeprecatedIndexPredicate;
1717
import org.elasticsearch.xpack.core.deprecation.DeprecationIssue;
@@ -33,7 +33,7 @@
3333
public class DataStreamDeprecationChecker implements ResourceDeprecationChecker {
3434

3535
public static final String NAME = "data_streams";
36-
private static final List<BiFunction<DataStream, ClusterState, DeprecationIssue>> DATA_STREAM_CHECKS = List.of(
36+
private static final List<BiFunction<DataStream, ProjectMetadata, DeprecationIssue>> DATA_STREAM_CHECKS = List.of(
3737
DataStreamDeprecationChecker::oldIndicesCheck,
3838
DataStreamDeprecationChecker::ignoredOldIndicesCheck
3939
);
@@ -44,38 +44,38 @@ public DataStreamDeprecationChecker(IndexNameExpressionResolver indexNameExpress
4444
}
4545

4646
/**
47-
* @param clusterState The cluster state provided for the checker
47+
* @param project The project metadata provided for the checker
4848
* @param request not used yet in these checks
4949
* @param precomputedData not used yet in these checks
5050
* @return the name of the data streams that have violated the checks with their respective warnings.
5151
*/
5252
@Override
5353
public Map<String, List<DeprecationIssue>> check(
54-
ClusterState clusterState,
54+
ProjectMetadata project,
5555
DeprecationInfoAction.Request request,
5656
TransportDeprecationInfoAction.PrecomputedData precomputedData
5757
) {
58-
return check(clusterState);
58+
return check(project);
5959
}
6060

6161
/**
62-
* @param clusterState The cluster state provided for the checker
62+
* @param project The project metadata provided for the checker
6363
* @return the name of the data streams that have violated the checks with their respective warnings.
6464
*/
65-
public Map<String, List<DeprecationIssue>> check(ClusterState clusterState) {
65+
public Map<String, List<DeprecationIssue>> check(ProjectMetadata project) {
6666
List<String> dataStreamNames = indexNameExpressionResolver.dataStreamNames(
67-
clusterState,
67+
project,
6868
IndicesOptions.LENIENT_EXPAND_OPEN_CLOSED_HIDDEN
6969
);
7070
if (dataStreamNames.isEmpty()) {
7171
return Map.of();
7272
}
7373
Map<String, List<DeprecationIssue>> dataStreamIssues = new HashMap<>();
7474
for (String dataStreamName : dataStreamNames) {
75-
DataStream dataStream = clusterState.metadata().getProject().dataStreams().get(dataStreamName);
75+
DataStream dataStream = project.dataStreams().get(dataStreamName);
7676
if (dataStream.isSystem() == false) {
7777
List<DeprecationIssue> issuesForSingleDataStream = DATA_STREAM_CHECKS.stream()
78-
.map(c -> c.apply(dataStream, clusterState))
78+
.map(c -> c.apply(dataStream, project))
7979
.filter(Objects::nonNull)
8080
.toList();
8181
if (issuesForSingleDataStream.isEmpty() == false) {
@@ -86,10 +86,10 @@ public Map<String, List<DeprecationIssue>> check(ClusterState clusterState) {
8686
return dataStreamIssues.isEmpty() ? Map.of() : dataStreamIssues;
8787
}
8888

89-
static DeprecationIssue oldIndicesCheck(DataStream dataStream, ClusterState clusterState) {
89+
static DeprecationIssue oldIndicesCheck(DataStream dataStream, ProjectMetadata project) {
9090
List<Index> backingIndices = dataStream.getIndices();
9191

92-
Set<String> indicesNeedingUpgrade = getReindexRequiredIndices(backingIndices, clusterState, false);
92+
Set<String> indicesNeedingUpgrade = getReindexRequiredIndices(backingIndices, project, false);
9393

9494
if (indicesNeedingUpgrade.isEmpty() == false) {
9595
return new DeprecationIssue(
@@ -110,9 +110,9 @@ static DeprecationIssue oldIndicesCheck(DataStream dataStream, ClusterState clus
110110
return null;
111111
}
112112

113-
static DeprecationIssue ignoredOldIndicesCheck(DataStream dataStream, ClusterState clusterState) {
113+
static DeprecationIssue ignoredOldIndicesCheck(DataStream dataStream, ProjectMetadata project) {
114114
List<Index> backingIndices = dataStream.getIndices();
115-
Set<String> ignoredIndices = getReindexRequiredIndices(backingIndices, clusterState, true);
115+
Set<String> ignoredIndices = getReindexRequiredIndices(backingIndices, project, true);
116116
if (ignoredIndices.isEmpty() == false) {
117117
return new DeprecationIssue(
118118
DeprecationIssue.Level.WARNING,
@@ -135,13 +135,11 @@ static DeprecationIssue ignoredOldIndicesCheck(DataStream dataStream, ClusterSta
135135

136136
private static Set<String> getReindexRequiredIndices(
137137
List<Index> backingIndices,
138-
ClusterState clusterState,
138+
ProjectMetadata project,
139139
boolean filterToBlockedStatus
140140
) {
141141
return backingIndices.stream()
142-
.filter(
143-
DeprecatedIndexPredicate.getReindexRequiredPredicate(clusterState.metadata().getProject(), filterToBlockedStatus, false)
144-
)
142+
.filter(DeprecatedIndexPredicate.getReindexRequiredPredicate(project, filterToBlockedStatus, false))
145143
.map(Index::getName)
146144
.collect(Collectors.toUnmodifiableSet());
147145
}

x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/IlmPolicyDeprecationChecker.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
package org.elasticsearch.xpack.deprecation;
99

10-
import org.elasticsearch.cluster.ClusterState;
10+
import org.elasticsearch.cluster.metadata.ProjectMetadata;
1111
import org.elasticsearch.xpack.core.deprecation.DeprecationIssue;
1212
import org.elasticsearch.xpack.core.ilm.AllocateAction;
1313
import org.elasticsearch.xpack.core.ilm.FreezeAction;
@@ -36,26 +36,26 @@ public class IlmPolicyDeprecationChecker implements ResourceDeprecationChecker {
3636
private final List<Function<LifecyclePolicy, DeprecationIssue>> checks = List.of(this::checkLegacyTiers, this::checkFrozenAction);
3737

3838
/**
39-
* @param clusterState The cluster state provided for the checker
39+
* @param project The project metadata provided for the checker
4040
* @param request not used yet in these checks
4141
* @param precomputedData not used yet in these checks
4242
* @return the name of the data streams that have violated the checks with their respective warnings.
4343
*/
4444
@Override
4545
public Map<String, List<DeprecationIssue>> check(
46-
ClusterState clusterState,
46+
ProjectMetadata project,
4747
DeprecationInfoAction.Request request,
4848
TransportDeprecationInfoAction.PrecomputedData precomputedData
4949
) {
50-
return check(clusterState);
50+
return check(project);
5151
}
5252

5353
/**
54-
* @param clusterState The cluster state provided for the checker
54+
* @param project The project metadata provided for the checker
5555
* @return the name of the data streams that have violated the checks with their respective warnings.
5656
*/
57-
Map<String, List<DeprecationIssue>> check(ClusterState clusterState) {
58-
IndexLifecycleMetadata lifecycleMetadata = clusterState.metadata().getProject().custom(IndexLifecycleMetadata.TYPE);
57+
Map<String, List<DeprecationIssue>> check(ProjectMetadata project) {
58+
IndexLifecycleMetadata lifecycleMetadata = project.custom(IndexLifecycleMetadata.TYPE);
5959
if (lifecycleMetadata == null || lifecycleMetadata.getPolicyMetadatas().isEmpty()) {
6060
return Map.of();
6161
}

0 commit comments

Comments
 (0)