1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.sra;
20
21 import static org.junit.jupiter.api.Assertions.fail;
22
23 import java.io.InputStream;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.Properties;
27 import java.util.concurrent.atomic.AtomicReference;
28 import javax.security.auth.login.AppConfigurationEntry;
29 import javax.security.auth.login.Configuration;
30 import org.apache.commons.lang3.StringUtils;
31 import org.apache.curator.test.InstanceSpec;
32 import org.apache.curator.test.TestingServer;
33 import org.apache.zookeeper.server.auth.DigestLoginModule;
34 import org.apache.zookeeper.server.auth.SASLAuthenticationProvider;
35 import org.springframework.context.ApplicationContextInitializer;
36 import org.springframework.context.ConfigurableApplicationContext;
37
38 public class ZookeeperTestingServer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
39
40 @Override
41 public void initialize(final ConfigurableApplicationContext ctx) {
42 AtomicReference<Integer> port = new AtomicReference<>();
43 AtomicReference<String> username = new AtomicReference<>();
44 AtomicReference<String> password = new AtomicReference<>();
45 try (InputStream propStream = getClass().getResourceAsStream("/test.properties")) {
46 Properties props = new Properties();
47 props.load(propStream);
48
49 port.set(Integer.valueOf(StringUtils.substringAfter(props.getProperty("keymaster.address"), ":")));
50 username.set(props.getProperty("keymaster.username"));
51 password.set(props.getProperty("keymaster.password"));
52 } catch (Exception e) {
53 throw new IllegalStateException("Could not load /test.properties", e);
54 }
55
56 if (AbstractTest.available(port.get())) {
57 Configuration.setConfiguration(new Configuration() {
58
59 private final AppConfigurationEntry[] entries = {
60 new AppConfigurationEntry(
61 DigestLoginModule.class.getName(),
62 AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
63 Map.of("user_" + username.get(), password.get()))
64 };
65
66 @Override
67 public AppConfigurationEntry[] getAppConfigurationEntry(final String name) {
68 return entries;
69 }
70 });
71
72 Map<String, Object> customProperties = new HashMap<>();
73 customProperties.put("authProvider.1", SASLAuthenticationProvider.class.getName());
74 InstanceSpec spec = new InstanceSpec(null, port.get(), -1, -1, true, 1, -1, -1, customProperties);
75
76 try {
77 new TestingServer(spec, true);
78 } catch (Exception e) {
79 fail(e);
80 }
81 }
82 }
83 }