Skip to content

Commit cfe2203

Browse files
committed
RCS 1 tests
1 parent 4313760 commit cfe2203

File tree

2 files changed

+210
-64
lines changed

2 files changed

+210
-64
lines changed
Lines changed: 55 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import org.elasticsearch.test.MapMatcher;
1616
import org.elasticsearch.test.cluster.ElasticsearchCluster;
1717
import org.elasticsearch.test.cluster.util.resource.Resource;
18-
import org.elasticsearch.test.junit.RunnableTestRuleAdapter;
1918
import org.elasticsearch.xcontent.XContentBuilder;
2019
import org.elasticsearch.xcontent.json.JsonXContent;
2120
import org.junit.ClassRule;
@@ -24,41 +23,26 @@
2423

2524
import java.io.IOException;
2625
import java.util.Locale;
27-
import java.util.Map;
28-
import java.util.concurrent.atomic.AtomicBoolean;
29-
import java.util.concurrent.atomic.AtomicReference;
3026

3127
import static org.elasticsearch.test.ListMatcher.matchesList;
3228
import static org.elasticsearch.test.MapMatcher.assertMap;
3329
import static org.elasticsearch.test.MapMatcher.matchesMap;
3430

35-
public class RemoteClusterSecurityDataStreamEsqlIT extends AbstractRemoteClusterSecurityTestCase {
36-
private static final AtomicReference<Map<String, Object>> API_KEY_MAP_REF = new AtomicReference<>();
37-
private static final AtomicBoolean SSL_ENABLED_REF = new AtomicBoolean();
38-
private static final AtomicBoolean NODE1_RCS_SERVER_ENABLED = new AtomicBoolean();
39-
private static final AtomicBoolean NODE2_RCS_SERVER_ENABLED = new AtomicBoolean();
40-
31+
// TODO consolidate me with RemoteClusterSecurityDataStreamEsqlRcs2IT
32+
public class RemoteClusterSecurityDataStreamEsqlRcs1IT extends AbstractRemoteClusterSecurityTestCase {
4133
static {
4234
fulfillingCluster = ElasticsearchCluster.local()
4335
.name("fulfilling-cluster")
44-
.nodes(3)
4536
.module("x-pack-autoscaling")
4637
.module("x-pack-esql")
4738
.module("x-pack-enrich")
4839
.module("x-pack-ml")
4940
.module("x-pack-ilm")
5041
.module("ingest-common")
5142
.apply(commonClusterConfig)
52-
.setting("remote_cluster.port", "0")
5343
.setting("xpack.ml.enabled", "false")
54-
.setting("xpack.security.remote_cluster_server.ssl.enabled", () -> String.valueOf(SSL_ENABLED_REF.get()))
55-
.setting("xpack.security.remote_cluster_server.ssl.key", "remote-cluster.key")
56-
.setting("xpack.security.remote_cluster_server.ssl.certificate", "remote-cluster.crt")
5744
.setting("xpack.security.authc.token.enabled", "true")
58-
.keystore("xpack.security.remote_cluster_server.ssl.secure_key_passphrase", "remote-cluster-password")
59-
.node(0, spec -> spec.setting("remote_cluster_server.enabled", "true"))
60-
.node(1, spec -> spec.setting("remote_cluster_server.enabled", () -> String.valueOf(NODE1_RCS_SERVER_ENABLED.get())))
61-
.node(2, spec -> spec.setting("remote_cluster_server.enabled", () -> String.valueOf(NODE2_RCS_SERVER_ENABLED.get())))
45+
.rolesFile(Resource.fromClasspath("roles.yml"))
6246
.build();
6347

6448
queryCluster = ElasticsearchCluster.local()
@@ -71,23 +55,7 @@ public class RemoteClusterSecurityDataStreamEsqlIT extends AbstractRemoteCluster
7155
.module("ingest-common")
7256
.apply(commonClusterConfig)
7357
.setting("xpack.ml.enabled", "false")
74-
.setting("xpack.security.remote_cluster_client.ssl.enabled", () -> String.valueOf(SSL_ENABLED_REF.get()))
75-
.setting("xpack.security.remote_cluster_client.ssl.certificate_authorities", "remote-cluster-ca.crt")
7658
.setting("xpack.security.authc.token.enabled", "true")
77-
.keystore("cluster.remote.my_remote_cluster.credentials", () -> {
78-
if (API_KEY_MAP_REF.get() == null) {
79-
final Map<String, Object> apiKeyMap = createCrossClusterAccessApiKey("""
80-
{
81-
"search": [
82-
{
83-
"names": ["logs-*", "alias-*"]
84-
}
85-
]
86-
}""");
87-
API_KEY_MAP_REF.set(apiKeyMap);
88-
}
89-
return (String) API_KEY_MAP_REF.get().get("encoded");
90-
})
9159
.rolesFile(Resource.fromClasspath("roles.yml"))
9260
.user("logs_foo_all", "x-pack-test-password", "logs_foo_all", false)
9361
.user("logs_foo_16_only", "x-pack-test-password", "logs_foo_16_only", false)
@@ -98,37 +66,62 @@ public class RemoteClusterSecurityDataStreamEsqlIT extends AbstractRemoteCluster
9866
}
9967

10068
@ClassRule
101-
// Use a RuleChain to ensure that fulfilling cluster is started before query cluster
102-
// `SSL_ENABLED_REF` is used to control the SSL-enabled setting on the test clusters
103-
// We set it here, since randomization methods are not available in the static initialize context above
104-
public static TestRule clusterRule = RuleChain.outerRule(new RunnableTestRuleAdapter(() -> {
105-
SSL_ENABLED_REF.set(usually());
106-
NODE1_RCS_SERVER_ENABLED.set(randomBoolean());
107-
NODE2_RCS_SERVER_ENABLED.set(randomBoolean());
108-
})).around(fulfillingCluster).around(queryCluster);
69+
public static TestRule clusterRule = RuleChain.outerRule(fulfillingCluster).around(queryCluster);
10970

11071
public void testDataStreamsWithDls() throws Exception {
111-
configureRemoteCluster();
112-
createDataStream();
72+
configureRemoteCluster(REMOTE_CLUSTER_ALIAS, fulfillingCluster, true, randomBoolean(), randomBoolean());
73+
createDataStreamOnFulfillingCluster();
74+
11375
MapMatcher twoResults = matchesMap().extraOk().entry("values", matchesList().item(matchesList().item(2)));
11476
MapMatcher oneResult = matchesMap().extraOk().entry("values", matchesList().item(matchesList().item(1)));
115-
assertMap(entityAsMap(runESQLCommand("logs_foo_all", "FROM my_remote_cluster:logs-foo | STATS COUNT(*)")), twoResults);
116-
assertMap(entityAsMap(runESQLCommand("logs_foo_16_only", "FROM my_remote_cluster:logs-foo | STATS COUNT(*)")), oneResult);
117-
assertMap(entityAsMap(runESQLCommand("logs_foo_after_2021", "FROM my_remote_cluster:logs-foo | STATS COUNT(*)")), oneResult);
11877
assertMap(
119-
entityAsMap(runESQLCommand("logs_foo_after_2021_pattern", "FROM my_remote_cluster:logs-foo | STATS COUNT(*)")),
78+
entityAsMap(runESQLCommandAgainstQueryCluster("logs_foo_all", "FROM my_remote_cluster:logs-foo | STATS COUNT(*)")),
79+
twoResults
80+
);
81+
assertMap(
82+
entityAsMap(runESQLCommandAgainstQueryCluster("logs_foo_16_only", "FROM my_remote_cluster:logs-foo | STATS COUNT(*)")),
83+
oneResult
84+
);
85+
assertMap(
86+
entityAsMap(runESQLCommandAgainstQueryCluster("logs_foo_after_2021", "FROM my_remote_cluster:logs-foo | STATS COUNT(*)")),
87+
oneResult
88+
);
89+
assertMap(
90+
entityAsMap(
91+
runESQLCommandAgainstQueryCluster("logs_foo_after_2021_pattern", "FROM my_remote_cluster:logs-foo | STATS COUNT(*)")
92+
),
93+
oneResult
94+
);
95+
assertMap(
96+
entityAsMap(runESQLCommandAgainstQueryCluster("logs_foo_all", "FROM my_remote_cluster:logs-* | STATS COUNT(*)")),
97+
twoResults
98+
);
99+
assertMap(
100+
entityAsMap(runESQLCommandAgainstQueryCluster("logs_foo_16_only", "FROM my_remote_cluster:logs-* | STATS COUNT(*)")),
101+
oneResult
102+
);
103+
assertMap(
104+
entityAsMap(runESQLCommandAgainstQueryCluster("logs_foo_after_2021", "FROM my_remote_cluster:logs-* | STATS COUNT(*)")),
105+
oneResult
106+
);
107+
assertMap(
108+
entityAsMap(runESQLCommandAgainstQueryCluster("logs_foo_after_2021_pattern", "FROM my_remote_cluster:logs-* | STATS COUNT(*)")),
120109
oneResult
121110
);
122-
assertMap(entityAsMap(runESQLCommand("logs_foo_all", "FROM my_remote_cluster:logs-* | STATS COUNT(*)")), twoResults);
123-
assertMap(entityAsMap(runESQLCommand("logs_foo_16_only", "FROM my_remote_cluster:logs-* | STATS COUNT(*)")), oneResult);
124-
assertMap(entityAsMap(runESQLCommand("logs_foo_after_2021", "FROM my_remote_cluster:logs-* | STATS COUNT(*)")), oneResult);
125-
assertMap(entityAsMap(runESQLCommand("logs_foo_after_2021_pattern", "FROM my_remote_cluster:logs-* | STATS COUNT(*)")), oneResult);
126111

127-
assertMap(entityAsMap(runESQLCommand("logs_foo_after_2021_alias", "FROM my_remote_cluster:alias-foo | STATS COUNT(*)")), oneResult);
128-
assertMap(entityAsMap(runESQLCommand("logs_foo_after_2021_alias", "FROM my_remote_cluster:alias-* | STATS COUNT(*)")), oneResult);
112+
assertMap(
113+
entityAsMap(
114+
runESQLCommandAgainstQueryCluster("logs_foo_after_2021_alias", "FROM my_remote_cluster:alias-foo | STATS COUNT(*)")
115+
),
116+
oneResult
117+
);
118+
assertMap(
119+
entityAsMap(runESQLCommandAgainstQueryCluster("logs_foo_after_2021_alias", "FROM my_remote_cluster:alias-* | STATS COUNT(*)")),
120+
oneResult
121+
);
129122
}
130123

131-
protected Response runESQLCommand(String user, String command) throws IOException {
124+
static Response runESQLCommandAgainstQueryCluster(String user, String command) throws IOException {
132125
if (command.toLowerCase(Locale.ROOT).contains("limit") == false) {
133126
// add a (high) limit to avoid warnings on default limit
134127
command += " | limit 10000000";
@@ -176,15 +169,15 @@ static Settings randomPragmas() {
176169
return settings.build();
177170
}
178171

179-
private void createDataStream() throws IOException {
172+
static void createDataStreamOnFulfillingCluster() throws IOException {
180173
createDataStreamPolicy();
181174
createDataStreamComponentTemplate();
182175
createDataStreamIndexTemplate();
183176
createDataStreamDocuments();
184177
createDataStreamAlias();
185178
}
186179

187-
private void createDataStreamPolicy() throws IOException {
180+
private static void createDataStreamPolicy() throws IOException {
188181
Request request = new Request("PUT", "_ilm/policy/my-lifecycle-policy");
189182
request.setJsonEntity("""
190183
{
@@ -210,7 +203,7 @@ private void createDataStreamPolicy() throws IOException {
210203
performRequestAgainstFulfillingCluster(request);
211204
}
212205

213-
private void createDataStreamComponentTemplate() throws IOException {
206+
private static void createDataStreamComponentTemplate() throws IOException {
214207
Request request = new Request("PUT", "_component_template/my-template");
215208
request.setJsonEntity("""
216209
{
@@ -236,7 +229,7 @@ private void createDataStreamComponentTemplate() throws IOException {
236229
performRequestAgainstFulfillingCluster(request);
237230
}
238231

239-
private void createDataStreamIndexTemplate() throws IOException {
232+
private static void createDataStreamIndexTemplate() throws IOException {
240233
Request request = new Request("PUT", "_index_template/my-index-template");
241234
request.setJsonEntity("""
242235
{
@@ -248,7 +241,7 @@ private void createDataStreamIndexTemplate() throws IOException {
248241
performRequestAgainstFulfillingCluster(request);
249242
}
250243

251-
private void createDataStreamDocuments() throws IOException {
244+
private static void createDataStreamDocuments() throws IOException {
252245
Request request = new Request("POST", "logs-foo/_bulk");
253246
request.addParameter("refresh", "");
254247
request.setJsonEntity("""
@@ -260,7 +253,7 @@ private void createDataStreamDocuments() throws IOException {
260253
assertMap(entityAsMap(performRequestAgainstFulfillingCluster(request)), matchesMap().extraOk().entry("errors", false));
261254
}
262255

263-
private void createDataStreamAlias() throws IOException {
256+
private static void createDataStreamAlias() throws IOException {
264257
Request request = new Request("PUT", "_alias");
265258
request.setJsonEntity("""
266259
{
@@ -275,6 +268,4 @@ private void createDataStreamAlias() throws IOException {
275268
}""");
276269
assertMap(entityAsMap(performRequestAgainstFulfillingCluster(request)), matchesMap().extraOk().entry("errors", false));
277270
}
278-
279-
record ExpectedCluster(String clusterAlias, String indexExpression, String status, Integer totalShards) {}
280271
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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.remotecluster;
9+
10+
import org.elasticsearch.test.MapMatcher;
11+
import org.elasticsearch.test.cluster.ElasticsearchCluster;
12+
import org.elasticsearch.test.cluster.util.resource.Resource;
13+
import org.elasticsearch.test.junit.RunnableTestRuleAdapter;
14+
import org.junit.ClassRule;
15+
import org.junit.rules.RuleChain;
16+
import org.junit.rules.TestRule;
17+
18+
import java.util.Map;
19+
import java.util.concurrent.atomic.AtomicBoolean;
20+
import java.util.concurrent.atomic.AtomicReference;
21+
22+
import static org.elasticsearch.test.ListMatcher.matchesList;
23+
import static org.elasticsearch.test.MapMatcher.assertMap;
24+
import static org.elasticsearch.test.MapMatcher.matchesMap;
25+
import static org.elasticsearch.xpack.remotecluster.RemoteClusterSecurityDataStreamEsqlRcs1IT.createDataStreamOnFulfillingCluster;
26+
import static org.elasticsearch.xpack.remotecluster.RemoteClusterSecurityDataStreamEsqlRcs1IT.runESQLCommandAgainstQueryCluster;
27+
28+
// TODO consolidate me with RemoteClusterSecurityDataStreamEsqlRcs1IT
29+
public class RemoteClusterSecurityDataStreamEsqlRcs2IT extends AbstractRemoteClusterSecurityTestCase {
30+
private static final AtomicReference<Map<String, Object>> API_KEY_MAP_REF = new AtomicReference<>();
31+
private static final AtomicBoolean SSL_ENABLED_REF = new AtomicBoolean();
32+
private static final AtomicBoolean NODE1_RCS_SERVER_ENABLED = new AtomicBoolean();
33+
private static final AtomicBoolean NODE2_RCS_SERVER_ENABLED = new AtomicBoolean();
34+
35+
static {
36+
fulfillingCluster = ElasticsearchCluster.local()
37+
.name("fulfilling-cluster")
38+
.nodes(3)
39+
.module("x-pack-autoscaling")
40+
.module("x-pack-esql")
41+
.module("x-pack-enrich")
42+
.module("x-pack-ml")
43+
.module("x-pack-ilm")
44+
.module("ingest-common")
45+
.apply(commonClusterConfig)
46+
.setting("remote_cluster.port", "0")
47+
.setting("xpack.ml.enabled", "false")
48+
.setting("xpack.security.remote_cluster_server.ssl.enabled", () -> String.valueOf(SSL_ENABLED_REF.get()))
49+
.setting("xpack.security.remote_cluster_server.ssl.key", "remote-cluster.key")
50+
.setting("xpack.security.remote_cluster_server.ssl.certificate", "remote-cluster.crt")
51+
.setting("xpack.security.authc.token.enabled", "true")
52+
.keystore("xpack.security.remote_cluster_server.ssl.secure_key_passphrase", "remote-cluster-password")
53+
.node(0, spec -> spec.setting("remote_cluster_server.enabled", "true"))
54+
.node(1, spec -> spec.setting("remote_cluster_server.enabled", () -> String.valueOf(NODE1_RCS_SERVER_ENABLED.get())))
55+
.node(2, spec -> spec.setting("remote_cluster_server.enabled", () -> String.valueOf(NODE2_RCS_SERVER_ENABLED.get())))
56+
.build();
57+
58+
queryCluster = ElasticsearchCluster.local()
59+
.name("query-cluster")
60+
.module("x-pack-autoscaling")
61+
.module("x-pack-esql")
62+
.module("x-pack-enrich")
63+
.module("x-pack-ml")
64+
.module("x-pack-ilm")
65+
.module("ingest-common")
66+
.apply(commonClusterConfig)
67+
.setting("xpack.ml.enabled", "false")
68+
.setting("xpack.security.remote_cluster_client.ssl.enabled", () -> String.valueOf(SSL_ENABLED_REF.get()))
69+
.setting("xpack.security.remote_cluster_client.ssl.certificate_authorities", "remote-cluster-ca.crt")
70+
.setting("xpack.security.authc.token.enabled", "true")
71+
.keystore("cluster.remote.my_remote_cluster.credentials", () -> {
72+
if (API_KEY_MAP_REF.get() == null) {
73+
final Map<String, Object> apiKeyMap = createCrossClusterAccessApiKey("""
74+
{
75+
"search": [
76+
{
77+
"names": ["logs-*", "alias-*"]
78+
}
79+
]
80+
}""");
81+
API_KEY_MAP_REF.set(apiKeyMap);
82+
}
83+
return (String) API_KEY_MAP_REF.get().get("encoded");
84+
})
85+
.rolesFile(Resource.fromClasspath("roles.yml"))
86+
.user("logs_foo_all", "x-pack-test-password", "logs_foo_all", false)
87+
.user("logs_foo_16_only", "x-pack-test-password", "logs_foo_16_only", false)
88+
.user("logs_foo_after_2021", "x-pack-test-password", "logs_foo_after_2021", false)
89+
.user("logs_foo_after_2021_pattern", "x-pack-test-password", "logs_foo_after_2021_pattern", false)
90+
.user("logs_foo_after_2021_alias", "x-pack-test-password", "logs_foo_after_2021_alias", false)
91+
.build();
92+
}
93+
94+
@ClassRule
95+
// Use a RuleChain to ensure that fulfilling cluster is started before query cluster
96+
// `SSL_ENABLED_REF` is used to control the SSL-enabled setting on the test clusters
97+
// We set it here, since randomization methods are not available in the static initialize context above
98+
public static TestRule clusterRule = RuleChain.outerRule(new RunnableTestRuleAdapter(() -> {
99+
SSL_ENABLED_REF.set(usually());
100+
NODE1_RCS_SERVER_ENABLED.set(randomBoolean());
101+
NODE2_RCS_SERVER_ENABLED.set(randomBoolean());
102+
})).around(fulfillingCluster).around(queryCluster);
103+
104+
public void testDataStreamsWithDls() throws Exception {
105+
configureRemoteCluster();
106+
createDataStreamOnFulfillingCluster();
107+
MapMatcher twoResults = matchesMap().extraOk().entry("values", matchesList().item(matchesList().item(2)));
108+
MapMatcher oneResult = matchesMap().extraOk().entry("values", matchesList().item(matchesList().item(1)));
109+
assertMap(
110+
entityAsMap(runESQLCommandAgainstQueryCluster("logs_foo_all", "FROM my_remote_cluster:logs-foo | STATS COUNT(*)")),
111+
twoResults
112+
);
113+
assertMap(
114+
entityAsMap(runESQLCommandAgainstQueryCluster("logs_foo_16_only", "FROM my_remote_cluster:logs-foo | STATS COUNT(*)")),
115+
oneResult
116+
);
117+
assertMap(
118+
entityAsMap(runESQLCommandAgainstQueryCluster("logs_foo_after_2021", "FROM my_remote_cluster:logs-foo | STATS COUNT(*)")),
119+
oneResult
120+
);
121+
assertMap(
122+
entityAsMap(
123+
runESQLCommandAgainstQueryCluster("logs_foo_after_2021_pattern", "FROM my_remote_cluster:logs-foo | STATS COUNT(*)")
124+
),
125+
oneResult
126+
);
127+
assertMap(
128+
entityAsMap(runESQLCommandAgainstQueryCluster("logs_foo_all", "FROM my_remote_cluster:logs-* | STATS COUNT(*)")),
129+
twoResults
130+
);
131+
assertMap(
132+
entityAsMap(runESQLCommandAgainstQueryCluster("logs_foo_16_only", "FROM my_remote_cluster:logs-* | STATS COUNT(*)")),
133+
oneResult
134+
);
135+
assertMap(
136+
entityAsMap(runESQLCommandAgainstQueryCluster("logs_foo_after_2021", "FROM my_remote_cluster:logs-* | STATS COUNT(*)")),
137+
oneResult
138+
);
139+
assertMap(
140+
entityAsMap(runESQLCommandAgainstQueryCluster("logs_foo_after_2021_pattern", "FROM my_remote_cluster:logs-* | STATS COUNT(*)")),
141+
oneResult
142+
);
143+
144+
assertMap(
145+
entityAsMap(
146+
runESQLCommandAgainstQueryCluster("logs_foo_after_2021_alias", "FROM my_remote_cluster:alias-foo | STATS COUNT(*)")
147+
),
148+
oneResult
149+
);
150+
assertMap(
151+
entityAsMap(runESQLCommandAgainstQueryCluster("logs_foo_after_2021_alias", "FROM my_remote_cluster:alias-* | STATS COUNT(*)")),
152+
oneResult
153+
);
154+
}
155+
}

0 commit comments

Comments
 (0)