View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.common.keymaster.client.zookeeper;
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.curator.test.InstanceSpec;
31  import org.apache.curator.test.TestingServer;
32  
33  public final class ZookeeperTestingServer {
34  
35      private static TestingServer ZK_SERVER;
36  
37      public static void start() throws Exception {
38          if (ZK_SERVER == null) {
39              AtomicReference<String> username = new AtomicReference<>();
40              AtomicReference<String> password = new AtomicReference<>();
41              try (InputStream propStream = ZookeeperServiceOpsTest.class.getResourceAsStream("/test.properties")) {
42                  Properties props = new Properties();
43                  props.load(propStream);
44  
45                  username.set(props.getProperty("keymaster.username"));
46                  password.set(props.getProperty("keymaster.password"));
47              } catch (Exception e) {
48                  fail("Could not load /test.properties", e);
49              }
50  
51              Configuration.setConfiguration(new Configuration() {
52  
53                  private final AppConfigurationEntry[] entries = {
54                      new AppConfigurationEntry(
55                      "org.apache.zookeeper.server.auth.DigestLoginModule",
56                      AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
57                      Map.of(
58                      "user_" + username.get(), password.get()
59                      ))
60                  };
61  
62                  @Override
63                  public AppConfigurationEntry[] getAppConfigurationEntry(final String name) {
64                      return entries;
65                  }
66              });
67  
68              Map<String, Object> customProperties = new HashMap<>();
69              customProperties.put("authProvider.1", "org.apache.zookeeper.server.auth.SASLAuthenticationProvider");
70              InstanceSpec spec = new InstanceSpec(null, 2181, -1, -1, true, 1, -1, -1, customProperties);
71              ZK_SERVER = new TestingServer(spec, true);
72          }
73      }
74  
75      private ZookeeperTestingServer() {
76          // private constructor for static utility class
77      }
78  }