You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by gg...@apache.org on 2017/03/10 09:54:15 UTC

karaf git commit: [KARAF-5017] Always use first user from etc/users.properties when running bin/client without "-u"

Repository: karaf
Updated Branches:
  refs/heads/master eeb09556e -> 52ff2d2fb


[KARAF-5017] Always use first user from etc/users.properties when running bin/client without "-u"


Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/52ff2d2f
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/52ff2d2f
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/52ff2d2f

Branch: refs/heads/master
Commit: 52ff2d2fbececfff8a9bbd1e1ca465e20be58de6
Parents: eeb0955
Author: Grzegorz Grzybek <gr...@gmail.com>
Authored: Fri Mar 10 10:47:43 2017 +0100
Committer: Grzegorz Grzybek <gr...@gmail.com>
Committed: Fri Mar 10 10:53:37 2017 +0100

----------------------------------------------------------------------
 .../org/apache/karaf/client/ClientConfig.java   | 39 +++++++++-------
 .../apache/karaf/client/ClientConfigTest.java   | 49 ++++++++++++++++++++
 .../src/test/resources/etc1/custom.properties   | 18 +++++++
 .../resources/etc1/org.apache.karaf.shell.cfg   | 18 +++++++
 client/src/test/resources/etc1/users.properties | 21 +++++++++
 .../src/test/resources/etc2/custom.properties   | 18 +++++++
 .../resources/etc2/org.apache.karaf.shell.cfg   | 18 +++++++
 client/src/test/resources/etc2/users.properties | 23 +++++++++
 8 files changed, 187 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/52ff2d2f/client/src/main/java/org/apache/karaf/client/ClientConfig.java
----------------------------------------------------------------------
diff --git a/client/src/main/java/org/apache/karaf/client/ClientConfig.java b/client/src/main/java/org/apache/karaf/client/ClientConfig.java
index a02eef3..cea1241 100644
--- a/client/src/main/java/org/apache/karaf/client/ClientConfig.java
+++ b/client/src/main/java/org/apache/karaf/client/ClientConfig.java
@@ -19,16 +19,14 @@ package org.apache.karaf.client;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
-import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
 import java.util.Properties;
 import java.util.Set;
 import java.util.Map;
-import java.util.Properties;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-import org.slf4j.impl.SimpleLogger;
-
 public class ClientConfig {
 
     private static final String ROLE_DELIMITER = ",";
@@ -49,10 +47,10 @@ public class ClientConfig {
     private boolean interactiveMode = false;
 
     public ClientConfig(String[] args) throws IOException {
-        Properties shellCfg = loadProps(new File(System.getProperty("karaf.etc"), "org.apache.karaf.shell.cfg"));
-        Properties customCfg = loadProps(new File(System.getProperty("karaf.etc"), "custom.properties"));
+        Properties shellCfg = loadProps(new File(System.getProperty("karaf.etc"), "org.apache.karaf.shell.cfg"), null);
+        Properties customCfg = loadProps(new File(System.getProperty("karaf.etc"), "custom.properties"), null);
         
-        host = shellCfg.getProperty("sshHost", "localhost");        
+        host = shellCfg.getProperty("sshHost", "localhost");
         host = expandEnvVars(host);
         String portString = shellCfg.getProperty("sshPort", "8101");
         portString = expandEnvVars(portString);
@@ -173,23 +171,24 @@ public class ClientConfig {
         }
         command = commandBuilder.toString();
 
-        Properties usersCfg = loadProps(new File(System.getProperty("karaf.etc") + "/users.properties"));
+        Map<String, String> usersCfg = new LinkedHashMap<>();
+        loadProps(new File(System.getProperty("karaf.etc") + "/users.properties"), usersCfg);
         if (!usersCfg.isEmpty()) {
-            Set<String> users = new HashSet<>();
-            for (String user : usersCfg.stringPropertyNames()) {
+            Set<String> users = new LinkedHashSet<>();
+            for (String user : usersCfg.keySet()) {
                 if (!user.startsWith(GROUP_PREFIX)) {
                     users.add(user);
                 }
             }
             if (user == null) {
                 if (users.iterator().hasNext()) {
-                    user = (String) users.iterator().next();
+                    user = users.iterator().next();
                 }
             }
             if (interactiveMode) {
                 password = null;
             } else {
-                password = (String) usersCfg.getProperty(user);
+                password = usersCfg.get(user);
                 if (password != null && password.contains(ROLE_DELIMITER)) {
                     password = password.substring(0, password.indexOf(ROLE_DELIMITER));
                 }
@@ -230,14 +229,20 @@ public class ClientConfig {
         }
     }
 
-    private static Properties loadProps(File file) {
-        Properties props = new Properties();
+    private static Properties loadProps(File file, final Map<String, String> additionalStorage) {
+        Properties props = new Properties() {
+            @Override
+            public synchronized Object put(Object key, Object value) {
+                if (additionalStorage != null) {
+                    additionalStorage.put((String) key, (String) value);
+                }
+                return super.put(key, value);
+            }
+        };
         FileInputStream is = null;
         try {
             is = new FileInputStream(file);
-            if (is != null) {
-                props.load(is);
-            }
+            props.load(is);
 
         } catch (Exception e) {
                 System.err.println("Warning: could not load properties from: " + file + ", Reason: " + e.getMessage());

http://git-wip-us.apache.org/repos/asf/karaf/blob/52ff2d2f/client/src/test/java/org/apache/karaf/client/ClientConfigTest.java
----------------------------------------------------------------------
diff --git a/client/src/test/java/org/apache/karaf/client/ClientConfigTest.java b/client/src/test/java/org/apache/karaf/client/ClientConfigTest.java
new file mode 100644
index 0000000..0732e56
--- /dev/null
+++ b/client/src/test/java/org/apache/karaf/client/ClientConfigTest.java
@@ -0,0 +1,49 @@
+/*
+ * 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.karaf.client;
+
+import java.io.IOException;
+
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.junit.Assert.assertThat;
+
+public class ClientConfigTest {
+
+    @Test
+    public void testDefaultUser() throws IOException {
+
+        String etc = System.getProperty("karaf.etc");
+
+        System.setProperty("karaf.etc", "src/test/resources/etc1");
+        ClientConfig cc = new ClientConfig(new String[0]);
+        assertThat(cc.getUser(), equalTo("karaf"));
+
+        cc = new ClientConfig(new String[] { "-u", "different-one" });
+        assertThat(cc.getUser(), equalTo("different-one"));
+
+        System.setProperty("karaf.etc", "src/test/resources/etc2");
+        cc = new ClientConfig(new String[0]);
+        assertThat(cc.getUser(), equalTo("test"));
+
+        if (etc != null) {
+            System.setProperty("karaf.etc", etc);
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/karaf/blob/52ff2d2f/client/src/test/resources/etc1/custom.properties
----------------------------------------------------------------------
diff --git a/client/src/test/resources/etc1/custom.properties b/client/src/test/resources/etc1/custom.properties
new file mode 100644
index 0000000..60d4e35
--- /dev/null
+++ b/client/src/test/resources/etc1/custom.properties
@@ -0,0 +1,18 @@
+################################################################################
+#
+#    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.
+#
+################################################################################

http://git-wip-us.apache.org/repos/asf/karaf/blob/52ff2d2f/client/src/test/resources/etc1/org.apache.karaf.shell.cfg
----------------------------------------------------------------------
diff --git a/client/src/test/resources/etc1/org.apache.karaf.shell.cfg b/client/src/test/resources/etc1/org.apache.karaf.shell.cfg
new file mode 100644
index 0000000..60d4e35
--- /dev/null
+++ b/client/src/test/resources/etc1/org.apache.karaf.shell.cfg
@@ -0,0 +1,18 @@
+################################################################################
+#
+#    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.
+#
+################################################################################

http://git-wip-us.apache.org/repos/asf/karaf/blob/52ff2d2f/client/src/test/resources/etc1/users.properties
----------------------------------------------------------------------
diff --git a/client/src/test/resources/etc1/users.properties b/client/src/test/resources/etc1/users.properties
new file mode 100644
index 0000000..67a6510
--- /dev/null
+++ b/client/src/test/resources/etc1/users.properties
@@ -0,0 +1,21 @@
+################################################################################
+#
+#    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.
+#
+################################################################################
+
+karaf = karaf,_g_:admingroup
+_g_\:admingroup = group,admin,manager,viewer,systembundles

http://git-wip-us.apache.org/repos/asf/karaf/blob/52ff2d2f/client/src/test/resources/etc2/custom.properties
----------------------------------------------------------------------
diff --git a/client/src/test/resources/etc2/custom.properties b/client/src/test/resources/etc2/custom.properties
new file mode 100644
index 0000000..60d4e35
--- /dev/null
+++ b/client/src/test/resources/etc2/custom.properties
@@ -0,0 +1,18 @@
+################################################################################
+#
+#    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.
+#
+################################################################################

http://git-wip-us.apache.org/repos/asf/karaf/blob/52ff2d2f/client/src/test/resources/etc2/org.apache.karaf.shell.cfg
----------------------------------------------------------------------
diff --git a/client/src/test/resources/etc2/org.apache.karaf.shell.cfg b/client/src/test/resources/etc2/org.apache.karaf.shell.cfg
new file mode 100644
index 0000000..60d4e35
--- /dev/null
+++ b/client/src/test/resources/etc2/org.apache.karaf.shell.cfg
@@ -0,0 +1,18 @@
+################################################################################
+#
+#    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.
+#
+################################################################################

http://git-wip-us.apache.org/repos/asf/karaf/blob/52ff2d2f/client/src/test/resources/etc2/users.properties
----------------------------------------------------------------------
diff --git a/client/src/test/resources/etc2/users.properties b/client/src/test/resources/etc2/users.properties
new file mode 100644
index 0000000..02bfd0a
--- /dev/null
+++ b/client/src/test/resources/etc2/users.properties
@@ -0,0 +1,23 @@
+################################################################################
+#
+#    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.
+#
+################################################################################
+
+_g_\:admingroup = group,admin,manager,viewer,systembundles
+test = admin,_g_:admingroup
+admin = admin,_g_:admingroup
+karaf = karaf,_g_:admingroup