|
| 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 | +} |
0 commit comments