Skip to content

Commit bc9d023

Browse files
committed
Support using DIGEST-MD5 as the SASL mechanism for ElasticJob CoordinatorRegistryCenter under GraalVM Native Image
1 parent b03ad56 commit bc9d023

File tree

8 files changed

+200
-20
lines changed

8 files changed

+200
-20
lines changed

docs/content/user-manual/configuration/external-integration/sasl.cn.md

+7-8
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,15 @@ public class ExampleUtils {
4848
Configuration configuration = new Configuration() {
4949
@Override
5050
public AppConfigurationEntry[] getAppConfigurationEntry(final String name) {
51-
Map<String, String> options = new HashMap<>();
52-
options.put("username", "bob");
53-
options.put("password", "bobsecret");
54-
AppConfigurationEntry entry = new AppConfigurationEntry(
51+
Map<String, String> conf = new HashMap<>();
52+
conf.put("username", "bob");
53+
conf.put("password", "bobsecret");
54+
AppConfigurationEntry[] entries = new AppConfigurationEntry[1];
55+
entries[0] = new AppConfigurationEntry(
5556
"org.apache.zookeeper.server.auth.DigestLoginModule",
5657
AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
57-
options);
58-
AppConfigurationEntry[] array = new AppConfigurationEntry[1];
59-
array[0] = entry;
60-
return array;
58+
conf);
59+
return entries;
6160
}
6261
};
6362
Configuration.setConfiguration(configuration);

docs/content/user-manual/configuration/external-integration/sasl.en.md

+7-8
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,15 @@ public class ExampleUtils {
5252
Configuration configuration = new Configuration() {
5353
@Override
5454
public AppConfigurationEntry[] getAppConfigurationEntry(final String name) {
55-
Map<String, String> options = new HashMap<>();
56-
options.put("username", "bob");
57-
options.put("password", "bobsecret");
58-
AppConfigurationEntry entry = new AppConfigurationEntry(
55+
Map<String, String> conf = new HashMap<>();
56+
conf.put("username", "bob");
57+
conf.put("password", "bobsecret");
58+
AppConfigurationEntry[] entries = new AppConfigurationEntry[1];
59+
entries[0] = new AppConfigurationEntry(
5960
"org.apache.zookeeper.server.auth.DigestLoginModule",
6061
AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
61-
options);
62-
AppConfigurationEntry[] array = new AppConfigurationEntry[1];
63-
array[0] = entry;
64-
return array;
62+
conf);
63+
return entries;
6564
}
6665
};
6766
Configuration.setConfiguration(configuration);

pom.xml

+19-4
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
<mockito.version>4.11.0</mockito.version>
9090
<awaitility.version>4.2.0</awaitility.version>
9191
<bytebuddy.version>1.14.18</bytebuddy.version>
92+
<testcontainers-bom.version>1.20.1</testcontainers-bom.version>
9293

9394
<h2.version>2.2.224</h2.version>
9495
<hikari-cp.version>4.0.3</hikari-cp.version>
@@ -350,6 +351,13 @@
350351
<version>${awaitility.version}</version>
351352
<scope>test</scope>
352353
</dependency>
354+
<dependency>
355+
<groupId>org.testcontainers</groupId>
356+
<artifactId>testcontainers-bom</artifactId>
357+
<version>${testcontainers-bom.version}</version>
358+
<type>pom</type>
359+
<scope>import</scope>
360+
</dependency>
353361

354362
<dependency>
355363
<groupId>com.h2database</groupId>
@@ -951,8 +959,8 @@
951959
<defaultMode>Conditional</defaultMode>
952960
<modes>
953961
<conditional>
954-
<userCodeFilterPath>${user.dir}/test/native/native-image-filter/user-code-filter.json</userCodeFilterPath>
955-
<extraFilterPath>${user.dir}/test/native/native-image-filter/extra-filter.json</extraFilterPath>
962+
<userCodeFilterPath>${user.dir}/test/native/native-image-filter/user-code-filter-zookeeper.json</userCodeFilterPath>
963+
<extraFilterPath>${user.dir}/test/native/native-image-filter/extra-filter-zookeeper.json</extraFilterPath>
956964
<parallel>true</parallel>
957965
</conditional>
958966
</modes>
@@ -961,7 +969,7 @@
961969
<stage>main</stage>
962970
</disabledStages>
963971
<merge>false</merge>
964-
<outputDirectory>${user.dir}/reachability-metadata/src/main/resources/META-INF/native-image/org.apache.shardingsphere.elasticjob/generated-reachability-metadata/</outputDirectory>
972+
<outputDirectory>${user.dir}/reachability-metadata/src/main/resources/META-INF/native-image/org.apache.zookeeper/zookeeper/3.9.2/</outputDirectory>
965973
</metadataCopy>
966974
</agent>
967975
</configuration>
@@ -995,7 +1003,7 @@
9951003
<artifactId>maven-surefire-plugin</artifactId>
9961004
<configuration>
9971005
<includes>
998-
<include>org.apache.shardingsphere.elasticjob.test.natived.**</include>
1006+
<include>org.apache.shardingsphere.elasticjob.test.natived.it.staticd.ZookeeperAuthTest</include>
9991007
</includes>
10001008
</configuration>
10011009
</plugin>
@@ -1006,6 +1014,13 @@
10061014
<extensions>true</extensions>
10071015
<configuration>
10081016
<quickBuild>true</quickBuild>
1017+
<buildArgs>
1018+
<buildArg>-H:AdditionalSecurityProviders=com.sun.security.sasl.Provider</buildArg>
1019+
<buildArg>-H:AdditionalSecurityProviders=com.sun.security.sasl.gsskerb.JdkSASL</buildArg>
1020+
<buildArg>-H:+UnlockExperimentalVMOptions</buildArg>
1021+
<buildArg>-H:ThrowMissingRegistrationErrors=</buildArg>
1022+
<buildArg>-H:MissingRegistrationReportingMode=Warn</buildArg>
1023+
</buildArgs>
10091024
</configuration>
10101025
<executions>
10111026
<execution>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"condition":{"typeReachable":"com.github.dockerjava.api.model.RuntimeInfo"},
4+
"name":"com.github.dockerjava.api.model.RuntimeInfo",
5+
"allPublicConstructors": true
6+
}
7+
]

test/native/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@
6262
<artifactId>curator-test</artifactId>
6363
<scope>test</scope>
6464
</dependency>
65+
<dependency>
66+
<groupId>org.testcontainers</groupId>
67+
<artifactId>junit-jupiter</artifactId>
68+
<scope>test</scope>
69+
</dependency>
6570
<dependency>
6671
<groupId>org.springframework.boot</groupId>
6772
<artifactId>spring-boot-starter-jdbc</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.shardingsphere.elasticjob.test.natived.it.staticd;
19+
20+
import com.zaxxer.hikari.HikariConfig;
21+
import com.zaxxer.hikari.HikariDataSource;
22+
import org.apache.shardingsphere.elasticjob.api.JobConfiguration;
23+
import org.apache.shardingsphere.elasticjob.bootstrap.type.ScheduleJobBootstrap;
24+
import org.apache.shardingsphere.elasticjob.kernel.tracing.config.TracingConfiguration;
25+
import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter;
26+
import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperConfiguration;
27+
import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperRegistryCenter;
28+
import org.apache.shardingsphere.elasticjob.test.natived.commons.job.simple.JavaSimpleJob;
29+
import org.junit.jupiter.api.AfterAll;
30+
import org.junit.jupiter.api.BeforeAll;
31+
import org.junit.jupiter.api.Test;
32+
import org.junit.jupiter.api.condition.EnabledInNativeImage;
33+
import org.testcontainers.containers.GenericContainer;
34+
import org.testcontainers.images.builder.Transferable;
35+
import org.testcontainers.junit.jupiter.Container;
36+
import org.testcontainers.junit.jupiter.Testcontainers;
37+
import org.testcontainers.utility.MountableFile;
38+
39+
import javax.security.auth.login.AppConfigurationEntry;
40+
import javax.security.auth.login.Configuration;
41+
import javax.sql.DataSource;
42+
import java.time.Duration;
43+
import java.util.HashMap;
44+
import java.util.Map;
45+
46+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
47+
48+
@EnabledInNativeImage
49+
@Testcontainers
50+
public class ZookeeperAuthTest {
51+
52+
@SuppressWarnings("resource")
53+
@Container
54+
private static final GenericContainer<?> CONTAINER = new GenericContainer<>("zookeeper:3.9.2")
55+
.withCopyFileToContainer(
56+
MountableFile.forClasspathResource("test-native/conf/jaas-server-test-native.conf", Transferable.DEFAULT_FILE_MODE),
57+
"/jaas-server-test-native.conf")
58+
.withEnv("JVMFLAGS", "-Djava.security.auth.login.config=/jaas-server-test-native.conf")
59+
.withEnv("ZOO_CFG_EXTRA", "authProvider.1=org.apache.zookeeper.server.auth.SASLAuthenticationProvider sessionRequireClientSASLAuth=true")
60+
.withExposedPorts(2181);
61+
62+
@BeforeAll
63+
static void beforeAll() {
64+
Configuration.setConfiguration(new Configuration() {
65+
66+
@Override
67+
public AppConfigurationEntry[] getAppConfigurationEntry(final String name) {
68+
Map<String, String> conf = new HashMap<>();
69+
conf.put("username", "bob");
70+
conf.put("password", "bobsecret");
71+
AppConfigurationEntry[] entries = new AppConfigurationEntry[1];
72+
entries[0] = new AppConfigurationEntry(
73+
"org.apache.zookeeper.server.auth.DigestLoginModule",
74+
AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
75+
conf);
76+
return entries;
77+
}
78+
});
79+
}
80+
81+
@AfterAll
82+
static void afterAll() {
83+
Configuration.setConfiguration(null);
84+
}
85+
86+
/**
87+
* For {@link org.apache.curator.test.TestingServer}, a lot of system properties are set in the background,
88+
* refer to
89+
* <a href="https://github.com/apache/zookeeper/blob/release-3.9.2/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslDigestAuthOverSSLTest.java">SaslDigestAuthOverSSLTest.java</a> .
90+
* Therefore, in order to test Zookeeper Server with SASL mechanism enabled under ElasticJob {@link CoordinatorRegistryCenter},
91+
* ElasticJob should never start Zookeeper Server through {@link org.apache.curator.test.TestingServer}.
92+
* Running Zookeeper Server and Curator Client in the same JVM process will pollute system properties.
93+
* For more information on this unit test,
94+
* refer to <a href="https://zookeeper.apache.org/doc/r3.9.2/zookeeperAdmin.html">ZooKeeper Administrator's Guide</a> and
95+
* <a href="https://cwiki.apache.org/confluence/display/ZOOKEEPER/ZooKeeper+and+SASL">ZooKeeper and SASL</a> .
96+
*
97+
* @throws Exception exception
98+
*/
99+
@Test
100+
void testSaslDigestMd5() throws Exception {
101+
String connectionString = CONTAINER.getHost() + ":" + CONTAINER.getMappedPort(2181);
102+
Thread.sleep(Duration.ofSeconds(5L).toMillis());
103+
CoordinatorRegistryCenter regCenter = new ZookeeperRegistryCenter(
104+
new ZookeeperConfiguration(connectionString, "elasticjob-test-native-sasl-digest-md5"));
105+
regCenter.init();
106+
HikariConfig hikariConfig = new HikariConfig();
107+
hikariConfig.setDriverClassName("org.h2.Driver");
108+
hikariConfig.setJdbcUrl("jdbc:h2:mem:job_event_storage");
109+
hikariConfig.setUsername("sa");
110+
hikariConfig.setPassword("");
111+
TracingConfiguration<DataSource> tracingConfig = new TracingConfiguration<>("RDB", new HikariDataSource(hikariConfig));
112+
ScheduleJobBootstrap jobBootstrap = new ScheduleJobBootstrap(
113+
regCenter,
114+
new JavaSimpleJob(),
115+
JobConfiguration.newBuilder("testSaslDigestMd5", 3)
116+
.cron("0/5 * * * * ?")
117+
.shardingItemParameters("0=Norddorf,1=Bordeaux,2=Somerset")
118+
.addExtraConfigurations(tracingConfig)
119+
.build());
120+
assertDoesNotThrow(() -> {
121+
jobBootstrap.schedule();
122+
jobBootstrap.shutdown();
123+
});
124+
regCenter.close();
125+
}
126+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"resources":{
3+
"includes":[{
4+
"condition":{"typeReachable":"org.apache.shardingsphere.elasticjob.test.natived.it.staticd.ZookeeperAuthTest"},
5+
"pattern":".*test-native/conf/.+\\.conf$"
6+
}]},
7+
"bundles":[]
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one or more
3+
// contributor license agreements. See the NOTICE file distributed with
4+
// this work for additional information regarding copyright ownership.
5+
// The ASF licenses this file to You under the Apache License, Version 2.0
6+
// (the "License"); you may not use this file except in compliance with
7+
// the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
//
17+
18+
Server {
19+
org.apache.zookeeper.server.auth.DigestLoginModule required
20+
user_bob="bobsecret";
21+
};

0 commit comments

Comments
 (0)