Skip to content

Commit

Permalink
Add nativeTest that uses DIGEST-MD5 as SASL mechanism for ElasticJob …
Browse files Browse the repository at this point in the history
…ZookeeperRegistryCenter under GraalVM Native Image
  • Loading branch information
linghengqian committed Aug 12, 2024
1 parent 756de29 commit e8939c9
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 2 deletions.
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<mockito.version>4.11.0</mockito.version>
<awaitility.version>4.2.0</awaitility.version>
<bytebuddy.version>1.14.18</bytebuddy.version>
<testcontainers-bom.version>1.20.1</testcontainers-bom.version>

<h2.version>2.2.224</h2.version>
<hikari-cp.version>4.0.3</hikari-cp.version>
Expand Down Expand Up @@ -350,6 +351,13 @@
<version>${awaitility.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-bom</artifactId>
<version>${testcontainers-bom.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
Expand Down
5 changes: 5 additions & 0 deletions test/native/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@
<artifactId>curator-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* Spring Boot Web Server for testing only.
*/
@SpringBootApplication
public class TestMain {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ static void beforeAll() throws Exception {
client.start();
Awaitility.await().atMost(Duration.ofMillis(500 * 60)).ignoreExceptions().until(client::isConnected);
}
firstRegCenter = new ZookeeperRegistryCenter(new ZookeeperConfiguration(testingServer.getConnectString(), "elasticjob-test-native-java"));
firstRegCenter = new ZookeeperRegistryCenter(new ZookeeperConfiguration(testingServer.getConnectString(), "elasticjob-test-native-operation-java"));
firstRegCenter.init();
secondRegCenter = new ZookeeperRegistryCenter(new ZookeeperConfiguration(testingServer.getConnectString(), "elasticjob-test-native-java"));
secondRegCenter = new ZookeeperRegistryCenter(new ZookeeperConfiguration(testingServer.getConnectString(), "elasticjob-test-native-operation-java"));
secondRegCenter.init();
HikariConfig config = new HikariConfig();
config.setDriverClassName("org.h2.Driver");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shardingsphere.elasticjob.test.natived.it.staticd;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.curator.CuratorZookeeperClient;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.shardingsphere.elasticjob.api.JobConfiguration;
import org.apache.shardingsphere.elasticjob.bootstrap.type.ScheduleJobBootstrap;
import org.apache.shardingsphere.elasticjob.kernel.tracing.config.TracingConfiguration;
import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter;
import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperConfiguration;
import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperRegistryCenter;
import org.apache.shardingsphere.elasticjob.test.natived.commons.job.simple.JavaSimpleJob;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledInNativeImage;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.images.builder.Transferable;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.MountableFile;

import javax.security.auth.login.AppConfigurationEntry;
import javax.security.auth.login.Configuration;
import javax.sql.DataSource;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

@EnabledInNativeImage
@Testcontainers
public class ZookeeperAuthTest {

@SuppressWarnings("resource")
@Container
private static final GenericContainer<?> container = new GenericContainer<>("zookeeper:3.9.2")
.withCopyFileToContainer(
MountableFile.forClasspathResource("test-native/conf/jaas-server-test-native.conf", Transferable.DEFAULT_FILE_MODE),
"/jaas-server-test-native.conf")
.withEnv("JVMFLAGS", "-Djava.security.auth.login.config=/jaas-server-test-native.conf")
.withEnv("ZOO_CFG_EXTRA", "authProvider.1=org.apache.zookeeper.server.auth.SASLAuthenticationProvider sessionRequireClientSASLAuth=true")
.withExposedPorts(2181);

@BeforeAll
static void beforeAll() {
Configuration configuration = new Configuration() {
@Override
public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
Map<String, String> options = new HashMap<>();
options.put("username", "nativeTestSuper");
options.put("password", "1234");
AppConfigurationEntry entry = new AppConfigurationEntry(
"org.apache.zookeeper.server.auth.DigestLoginModule",
AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
options
);
AppConfigurationEntry[] array = new AppConfigurationEntry[1];
array[0] = entry;
return array;
}
};
Configuration.setConfiguration(configuration);
}

@AfterAll
static void afterAll() {
Configuration.setConfiguration(null);
}

/**
* For the Zookeeper Client of the `org.apache.zookeeper:zookeeper:3.9.2` module, a lot of system properties are set in the background,
* refer to <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> .
* Therefore, in order to test Zookeeper Server with SASL mechanism enabled under ElasticJob Java API,
* ElasticJob should never start Zookeeper Server through TestingServer.
* Running Zookeeper Server and Curator Client in the same JVM process will pollute system properties.
* For more information on this unit test,
* refer to <a href="https://zookeeper.apache.org/doc/r3.9.2/zookeeperAdmin.html">ZooKeeper Administrator's Guide</a> ,
* <a href="https://cwiki.apache.org/confluence/display/ZOOKEEPER/ZooKeeper+and+SASL">ZooKeeper and SASL</a> ,
* and <a href="https://github.com/junit-team/junit5/pull/2382">junit-team/junit5#2382</a> .
*
* @throws Exception exception
*/
@Test
void testSaslDigestMd5() throws Exception {
String connectionString = container.getHost() + ":" + container.getMappedPort(2181);
try (
CuratorZookeeperClient client = new CuratorZookeeperClient(connectionString,
60 * 1000, 500, null,
new ExponentialBackoffRetry(500, 3, 500 * 3))) {
client.start();
Awaitility.await().atMost(Duration.ofMillis(500 * 60)).ignoreExceptions().until(client::isConnected);
}
CoordinatorRegistryCenter regCenter = new ZookeeperRegistryCenter(
new ZookeeperConfiguration(connectionString, "elasticjob-test-native-sasl-digest-md5"));
regCenter.init();
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setDriverClassName("org.h2.Driver");
hikariConfig.setJdbcUrl("jdbc:h2:mem:job_event_storage");
hikariConfig.setUsername("sa");
hikariConfig.setPassword("");
TracingConfiguration<DataSource> tracingConfig = new TracingConfiguration<>("RDB", new HikariDataSource(hikariConfig));
ScheduleJobBootstrap jobBootstrap = new ScheduleJobBootstrap(
regCenter,
new JavaSimpleJob(),
JobConfiguration.newBuilder("testSaslDigestMd5", 3)
.cron("0/5 * * * * ?")
.shardingItemParameters("0=Norddorf,1=Bordeaux,2=Somerset")
.addExtraConfigurations(tracingConfig)
.build());
assertDoesNotThrow(() -> {
jobBootstrap.schedule();
jobBootstrap.shutdown();
});
regCenter.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"resources":{
"includes":[{
"condition":{"typeReachable":"org.apache.shardingsphere.elasticjob.test.natived.it.staticd.ZookeeperAuthTest"},
"pattern":".*test-native/conf/.+\\.conf$"
}]},
"bundles":[]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Server {
org.apache.zookeeper.server.auth.DigestLoginModule required
user_nativeTestSuper="1234";
};

0 comments on commit e8939c9

Please sign in to comment.