directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From plusplusjia...@apache.org
Subject directory-kerby git commit: DIRKRB-505 Apply Krb5Parser to both kerb-server and kerb-client. Contributed by Yan.
Date Tue, 05 Jan 2016 05:59:16 GMT
Repository: directory-kerby
Updated Branches:
  refs/heads/trunk 73837df23 -> 6e3e6e956


DIRKRB-505 Apply Krb5Parser to both kerb-server and kerb-client. Contributed by Yan.


Project: http://git-wip-us.apache.org/repos/asf/directory-kerby/repo
Commit: http://git-wip-us.apache.org/repos/asf/directory-kerby/commit/6e3e6e95
Tree: http://git-wip-us.apache.org/repos/asf/directory-kerby/tree/6e3e6e95
Diff: http://git-wip-us.apache.org/repos/asf/directory-kerby/diff/6e3e6e95

Branch: refs/heads/trunk
Commit: 6e3e6e956d2de6b8b5ef0eb312858e0810f264d4
Parents: 73837df
Author: plusplusjiajia <jiajia.li@intel.com>
Authored: Tue Jan 5 14:04:55 2016 +0800
Committer: plusplusjiajia <jiajia.li@intel.com>
Committed: Tue Jan 5 14:04:55 2016 +0800

----------------------------------------------------------------------
 .../main/java/org/apache/kerby/config/Conf.java |  2 +-
 .../apache/kerby/config/MapConfigLoader.java    | 52 ++++++++++++++++++--
 .../java/org/apache/kerby/config/Resource.java  |  2 +-
 .../java/org/apache/kerby/config/ConfTest.java  |  6 +--
 .../test/jaas/TokenAuthLoginModule.java         |  2 +-
 .../kerby/kerberos/kerb/client/ClientUtil.java  |  4 +-
 .../kerb/client/KrbClientSettingTest.java       |  2 +-
 .../client/KrbConfigLoadForSpecialsTest.java    |  2 +-
 .../kerberos/kerb/client/KrbConfigLoadTest.java |  2 +-
 .../kerby/kerberos/kerb/common/Krb5Conf.java    | 11 +++++
 .../kerby/kerberos/kerb/common/Krb5Parser.java  | 25 +++++++---
 .../kerby/kerberos/kerb/server/KdcUtil.java     |  2 +-
 .../kerberos/kerb/server/KdcConfigLoadTest.java |  2 +-
 13 files changed, 92 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/6e3e6e95/kerby-config/src/main/java/org/apache/kerby/config/Conf.java
----------------------------------------------------------------------
diff --git a/kerby-config/src/main/java/org/apache/kerby/config/Conf.java b/kerby-config/src/main/java/org/apache/kerby/config/Conf.java
index 86555e9..c0bea4b 100644
--- a/kerby-config/src/main/java/org/apache/kerby/config/Conf.java
+++ b/kerby-config/src/main/java/org/apache/kerby/config/Conf.java
@@ -64,7 +64,7 @@ public class Conf implements Config {
         addResource(Resource.createPropertiesResource(propertiesConfig));
     }
 
-    public void addMapConfig(Map<String, String> mapConfig) {
+    public void addMapConfig(Map<String, Object> mapConfig) {
         addResource(Resource.createMapResource(mapConfig));
     }
 

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/6e3e6e95/kerby-config/src/main/java/org/apache/kerby/config/MapConfigLoader.java
----------------------------------------------------------------------
diff --git a/kerby-config/src/main/java/org/apache/kerby/config/MapConfigLoader.java b/kerby-config/src/main/java/org/apache/kerby/config/MapConfigLoader.java
index 9a212c3..3ba5d4c 100644
--- a/kerby-config/src/main/java/org/apache/kerby/config/MapConfigLoader.java
+++ b/kerby-config/src/main/java/org/apache/kerby/config/MapConfigLoader.java
@@ -19,15 +19,61 @@
  */
 package org.apache.kerby.config;
 
+import java.util.Iterator;
 import java.util.Map;
 
 public class MapConfigLoader extends ConfigLoader {
     @Override
     protected void loadConfig(ConfigImpl config, Resource resource) {
         @SuppressWarnings("unchecked")
-        Map<String, String> mapConfig = (Map<String, String>) resource.getResource();
-        for (Map.Entry<String, String> entry : mapConfig.entrySet()) {
-            config.set(entry.getKey(), entry.getValue());
+        Map<String, Object> mapConfig = (Map<String, Object>) resource.getResource();
+        Iterator<Map.Entry<String, Object>> iter = mapConfig.entrySet().iterator();
+        if (iter.hasNext()) {
+            Map.Entry entry = iter.next();
+            if (entry.getValue() instanceof String) {
+                //insert StringMap
+                loadStringMap(config, mapConfig);
+            }   else {
+                //insert objectMap
+                loadObjectMap(config, mapConfig);
+            }
+        }
+    }
+
+    private void loadStringMap(ConfigImpl config, Map<String, Object> stringMap) {
+        for (Map.Entry<String, Object> entry: stringMap.entrySet()) {
+            config.set(entry.getKey(), (String) entry.getValue());
+        }
+    }
+
+    private void loadObjectMap(ConfigImpl config, Map<String, Object> objectMap) {
+        for (Map.Entry<String, Object> entry: objectMap.entrySet()) {
+            String key = entry.getKey();
+            Object value = entry.getValue();
+
+            if (value instanceof Map) {
+                ConfigImpl subConfig = new ConfigImpl(key); //new section
+                loadSubmap(subConfig, (Map) value);
+                config.add(subConfig);
+            }   else {
+                throw new RuntimeException("Unable to resolve config:" + key);
+            }
+        }
+    }
+
+    private void loadSubmap(ConfigImpl config, Map<String, Object> map) {
+        for (Map.Entry<String, Object> entry: map.entrySet()) {
+            String key = entry.getKey();
+            Object value = entry.getValue();
+
+            if (value instanceof String) {
+                config.set(key, (String) value);
+            }
+            if (value instanceof Map) {
+                ConfigImpl subConfig = new ConfigImpl(key);
+                loadSubmap(subConfig, (Map) value);
+                config.add(subConfig);
+            }
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/6e3e6e95/kerby-config/src/main/java/org/apache/kerby/config/Resource.java
----------------------------------------------------------------------
diff --git a/kerby-config/src/main/java/org/apache/kerby/config/Resource.java b/kerby-config/src/main/java/org/apache/kerby/config/Resource.java
index 9988ffa..0c5d23e 100644
--- a/kerby-config/src/main/java/org/apache/kerby/config/Resource.java
+++ b/kerby-config/src/main/java/org/apache/kerby/config/Resource.java
@@ -75,7 +75,7 @@ public final class Resource {
         return new Resource(jsonUrl, Format.JSON_FILE);
     }
 
-    public static Resource createMapResource(Map<String, String> mapConfig) {
+    public static Resource createMapResource(Map<String, Object> mapConfig) {
         return new Resource("mapConfig", mapConfig, Format.MAP);
     }
 

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/6e3e6e95/kerby-config/src/test/java/org/apache/kerby/config/ConfTest.java
----------------------------------------------------------------------
diff --git a/kerby-config/src/test/java/org/apache/kerby/config/ConfTest.java b/kerby-config/src/test/java/org/apache/kerby/config/ConfTest.java
index 33bc532..3dc4b70 100644
--- a/kerby-config/src/test/java/org/apache/kerby/config/ConfTest.java
+++ b/kerby-config/src/test/java/org/apache/kerby/config/ConfTest.java
@@ -38,7 +38,7 @@ public class ConfTest {
         String strProp = "hello";
         Integer intProp = 123456;
         Boolean boolProp = true;
-        Map<String, String> mapConfig = new HashMap<String, String>();
+        Map<String, Object> mapConfig = new HashMap<String, Object>();
         mapConfig.put("strProp", strProp);
         mapConfig.put("intProp", String.valueOf(intProp));
         mapConfig.put("boolProp", String.valueOf(boolProp));
@@ -74,7 +74,7 @@ public class ConfTest {
     public void testMixedConfig() {
         String mapStrProp = "hello map";
         Integer intProp = 123456;
-        Map<String, String> mapConfig = new HashMap<String, String>();
+        Map<String, Object> mapConfig = new HashMap<String, Object>();
         mapConfig.put("mapStrProp", mapStrProp);
         mapConfig.put("intProp", String.valueOf(intProp));
 
@@ -121,7 +121,7 @@ public class ConfTest {
         Conf conf = new Conf();
         assertThat(conf.getString(TestConfKey.ADDRESS, true)).isEqualTo(
                 TestConfKey.ADDRESS.getDefaultValue());
-        Map<String, String> mapConfig = new HashMap<String, String>();
+        Map<String, Object> mapConfig = new HashMap<String, Object>();
         String myAddress = "www.google.com";
         mapConfig.put(TestConfKey.ADDRESS.getPropertyKey(), myAddress);
         conf.addMapConfig(mapConfig);

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/6e3e6e95/kerby-kerb/integration-test/src/main/java/org/apache/kerby/kerberos/kerb/integration/test/jaas/TokenAuthLoginModule.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/integration-test/src/main/java/org/apache/kerby/kerberos/kerb/integration/test/jaas/TokenAuthLoginModule.java
b/kerby-kerb/integration-test/src/main/java/org/apache/kerby/kerberos/kerb/integration/test/jaas/TokenAuthLoginModule.java
index d3c55d9..5e1b27b 100644
--- a/kerby-kerb/integration-test/src/main/java/org/apache/kerby/kerberos/kerb/integration/test/jaas/TokenAuthLoginModule.java
+++ b/kerby-kerb/integration-test/src/main/java/org/apache/kerby/kerberos/kerb/integration/test/jaas/TokenAuthLoginModule.java
@@ -232,7 +232,7 @@ public class TokenAuthLoginModule implements LoginModule {
         try {
             File confFile = new File(System.getProperty(Krb5Conf.KRB5_CONF));
             KrbConfig krbConfig = new KrbConfig();
-            krbConfig.addIniConfig(confFile);
+            krbConfig.addKrb5Config(confFile);
             krbClient = new KrbClient(krbConfig);
             krbClient.init();
         } catch (KrbException e) {

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/6e3e6e95/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/ClientUtil.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/ClientUtil.java
b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/ClientUtil.java
index 3dd4ae0..49b7666 100644
--- a/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/ClientUtil.java
+++ b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/ClientUtil.java
@@ -48,7 +48,7 @@ public final class ClientUtil {
         if (confFile != null && confFile.exists()) {
             KrbConfig krbConfig = new KrbConfig();
             try {
-                krbConfig.addIniConfig(confFile);
+                krbConfig.addKrb5Config(confFile);
                 return krbConfig;
             } catch (IOException e) {
                 throw new KrbException("Failed to load krb config "
@@ -91,7 +91,7 @@ public final class ClientUtil {
         KrbConfig krbConfig = new KrbConfig();
         if (confFile != null && confFile.exists()) {
             try {
-                krbConfig.addIniConfig(confFile);
+                krbConfig.addKrb5Config(confFile);
             } catch (IOException e) {
                 throw new KrbException("Failed to load krb config "
                         + confFile.getAbsolutePath());

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/6e3e6e95/kerby-kerb/kerb-client/src/test/java/org/apache/kerby/kerberos/kerb/client/KrbClientSettingTest.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-client/src/test/java/org/apache/kerby/kerberos/kerb/client/KrbClientSettingTest.java
b/kerby-kerb/kerb-client/src/test/java/org/apache/kerby/kerberos/kerb/client/KrbClientSettingTest.java
index 3c65f22..731e2e3 100644
--- a/kerby-kerb/kerb-client/src/test/java/org/apache/kerby/kerberos/kerb/client/KrbClientSettingTest.java
+++ b/kerby-kerb/kerb-client/src/test/java/org/apache/kerby/kerberos/kerb/client/KrbClientSettingTest.java
@@ -35,7 +35,7 @@ public class KrbClientSettingTest {
         File confFile = new File(confFileUrl.toURI());
 
         KrbConfig conf = new KrbConfig();
-        conf.addIniConfig(confFile);
+        conf.addKrb5Config(confFile);
         KrbClient krbClient = new KrbClient(conf);
 
         KrbSetting krbSetting = krbClient.getSetting();

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/6e3e6e95/kerby-kerb/kerb-client/src/test/java/org/apache/kerby/kerberos/kerb/client/KrbConfigLoadForSpecialsTest.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-client/src/test/java/org/apache/kerby/kerberos/kerb/client/KrbConfigLoadForSpecialsTest.java
b/kerby-kerb/kerb-client/src/test/java/org/apache/kerby/kerberos/kerb/client/KrbConfigLoadForSpecialsTest.java
index 87b6f21..51d72e1 100644
--- a/kerby-kerb/kerb-client/src/test/java/org/apache/kerby/kerberos/kerb/client/KrbConfigLoadForSpecialsTest.java
+++ b/kerby-kerb/kerb-client/src/test/java/org/apache/kerby/kerberos/kerb/client/KrbConfigLoadForSpecialsTest.java
@@ -39,7 +39,7 @@ public class KrbConfigLoadForSpecialsTest {
         File confFile = new File(confFileUrl.toURI());
 
         KrbConfig krbConfig = new KrbConfig();
-        krbConfig.addIniConfig(confFile);
+        krbConfig.addKrb5Config(confFile);
         assertThat(krbConfig.getKdcRealm()).isEqualTo("KRB.COM");
         assertThat(krbConfig.getKdcPort()).isEqualTo(88);
 

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/6e3e6e95/kerby-kerb/kerb-client/src/test/java/org/apache/kerby/kerberos/kerb/client/KrbConfigLoadTest.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-client/src/test/java/org/apache/kerby/kerberos/kerb/client/KrbConfigLoadTest.java
b/kerby-kerb/kerb-client/src/test/java/org/apache/kerby/kerberos/kerb/client/KrbConfigLoadTest.java
index 967d17d..cfd3929 100644
--- a/kerby-kerb/kerb-client/src/test/java/org/apache/kerby/kerberos/kerb/client/KrbConfigLoadTest.java
+++ b/kerby-kerb/kerb-client/src/test/java/org/apache/kerby/kerberos/kerb/client/KrbConfigLoadTest.java
@@ -39,7 +39,7 @@ public class KrbConfigLoadTest {
         File confFile = new File(confFileUrl.toURI());
 
         KrbConfig krbConfig = new KrbConfig();
-        krbConfig.addIniConfig(confFile);
+        krbConfig.addKrb5Config(confFile);
         assertThat(krbConfig.getDefaultRealm()).isEqualTo("KRB.COM");
         assertThat(krbConfig.getKdcRealm()).isEqualTo("TEST.COM");
         assertThat(krbConfig.getKdcHost()).isEqualTo("kdc-server.example.com");

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/6e3e6e95/kerby-kerb/kerb-common/src/main/java/org/apache/kerby/kerberos/kerb/common/Krb5Conf.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-common/src/main/java/org/apache/kerby/kerberos/kerb/common/Krb5Conf.java
b/kerby-kerb/kerb-common/src/main/java/org/apache/kerby/kerberos/kerb/common/Krb5Conf.java
index c503d65..1834ae5 100644
--- a/kerby-kerb/kerb-common/src/main/java/org/apache/kerby/kerberos/kerb/common/Krb5Conf.java
+++ b/kerby-kerb/kerb-common/src/main/java/org/apache/kerby/kerberos/kerb/common/Krb5Conf.java
@@ -22,11 +22,15 @@ package org.apache.kerby.kerberos.kerb.common;
 import org.apache.kerby.config.Conf;
 import org.apache.kerby.config.Config;
 import org.apache.kerby.config.ConfigKey;
+import org.apache.kerby.config.Resource;
 import org.apache.kerby.kerberos.kerb.type.base.EncryptionType;
 
+import java.io.File;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.Map;
 
 /**
  * A krb5.conf format support.
@@ -38,6 +42,13 @@ public class Krb5Conf extends Conf {
      */
     private static final String LIST_SPLITTER = " |,";
 
+    public void addKrb5Config(File krb5File) throws IOException {
+        Krb5Parser krb5Parser = new Krb5Parser(krb5File);
+        krb5Parser.load();
+        Map<String, Object> krb5Map = krb5Parser.getItems();
+        addResource(Resource.createMapResource(krb5Map));
+    }
+
     protected String getString(ConfigKey key, boolean useDefault,
                             String ... sections) {
         String value = getString(key, false);

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/6e3e6e95/kerby-kerb/kerb-common/src/main/java/org/apache/kerby/kerberos/kerb/common/Krb5Parser.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-common/src/main/java/org/apache/kerby/kerberos/kerb/common/Krb5Parser.java
b/kerby-kerb/kerb-common/src/main/java/org/apache/kerby/kerberos/kerb/common/Krb5Parser.java
index a7654dc..269daae 100644
--- a/kerby-kerb/kerb-common/src/main/java/org/apache/kerby/kerberos/kerb/common/Krb5Parser.java
+++ b/kerby-kerb/kerb-common/src/main/java/org/apache/kerby/kerberos/kerb/common/Krb5Parser.java
@@ -61,7 +61,7 @@ public class Krb5Parser {
         while (originLine != null) {
             String line = originLine.trim();
             /*parse through comments*/
-            if (line.startsWith("#")) {
+            if (line.startsWith("#") || line.length() == 0) {
                 originLine = br.readLine();
             }   else if (line.startsWith("[")) {
                 insertSections(line, br, items);
@@ -74,6 +74,16 @@ public class Krb5Parser {
     }
 
     /**
+     * Get the whole map.
+     * @return member variable items.
+     */
+    public Map<String, Object> getItems() {
+        return items;
+    }
+
+
+
+    /**
      * Get all the names of sections in a list.
      * @return a list of section names.
      */
@@ -108,6 +118,12 @@ public class Krb5Parser {
             String sectionName = line.substring(1, line.length() - 1);
             Map<String, Object> entries = new HashMap<String, Object>();
             line = br.readLine();
+            while (line.startsWith("#")) {
+                line = br.readLine();
+                if (line == null) {
+                    break;
+                }
+            }
             if (line != null) {
                 line = line.trim();
                 line = insertEntries(line, br, entries);
@@ -128,7 +144,7 @@ public class Krb5Parser {
         if (line == null) {
             return line;
         }
-        if (line.startsWith("[")) {
+        if (line.startsWith("[") || line.startsWith("#")) {
             return line;
         }
         if (line.startsWith("}")) {
@@ -147,10 +163,7 @@ public class Krb5Parser {
             return line;
         }
         /*some special cases above*/
-        String[] kv = line.split("=");
-        if (kv.length > 2) {
-            throw new RuntimeException("Unable to parse:" + line);
-        }
+        String[] kv = line.split("=", 2);
         kv[0] = kv[0].trim();
         kv[1] = kv[1].trim();
 

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/6e3e6e95/kerby-kerb/kerb-server/src/main/java/org/apache/kerby/kerberos/kerb/server/KdcUtil.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-server/src/main/java/org/apache/kerby/kerberos/kerb/server/KdcUtil.java
b/kerby-kerb/kerb-server/src/main/java/org/apache/kerby/kerberos/kerb/server/KdcUtil.java
index 198a2f0..738194d 100644
--- a/kerby-kerb/kerb-server/src/main/java/org/apache/kerby/kerberos/kerb/server/KdcUtil.java
+++ b/kerby-kerb/kerb-server/src/main/java/org/apache/kerby/kerberos/kerb/server/KdcUtil.java
@@ -47,7 +47,7 @@ public final class KdcUtil {
         if (kdcConfFile.exists()) {
             KdcConfig kdcConfig = new KdcConfig();
             try {
-                kdcConfig.addIniConfig(kdcConfFile);
+                kdcConfig.addKrb5Config(kdcConfFile);
             } catch (IOException e) {
                 throw new KrbException("Can not load the kdc configuration file "
                         + kdcConfFile.getAbsolutePath());

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/6e3e6e95/kerby-kerb/kerb-server/src/test/java/org/apache/kerby/kerberos/kerb/server/KdcConfigLoadTest.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-server/src/test/java/org/apache/kerby/kerberos/kerb/server/KdcConfigLoadTest.java
b/kerby-kerb/kerb-server/src/test/java/org/apache/kerby/kerberos/kerb/server/KdcConfigLoadTest.java
index bf761d5..94919a7 100644
--- a/kerby-kerb/kerb-server/src/test/java/org/apache/kerby/kerberos/kerb/server/KdcConfigLoadTest.java
+++ b/kerby-kerb/kerb-server/src/test/java/org/apache/kerby/kerberos/kerb/server/KdcConfigLoadTest.java
@@ -36,7 +36,7 @@ public class KdcConfigLoadTest {
         File confFile = new File(confFileUrl.toURI());
 
         KdcConfig kdcConfig = new KdcConfig();
-        kdcConfig.addIniConfig(confFile);
+        kdcConfig.addKrb5Config(confFile);
 
         assertThat(kdcConfig.getKdcHost()).isEqualTo("localhost");
         assertThat(kdcConfig.getKdcUdpPort()).isEqualTo(88);


Mime
View raw message