directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dran...@apache.org
Subject directory-kerby git commit: Handling default config value in better way and solved DIRKRB-457
Date Mon, 16 Nov 2015 13:09:29 GMT
Repository: directory-kerby
Updated Branches:
  refs/heads/master 955a84585 -> cf6e39b6d


Handling default config value in better way and solved DIRKRB-457


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

Branch: refs/heads/master
Commit: cf6e39b6d4be9681dbf513a0c1417e5cf6ad839f
Parents: 955a845
Author: Kai Zheng <kai.zheng@intel.com>
Authored: Mon Nov 16 21:09:09 2015 +0800
Committer: Kai Zheng <kai.zheng@intel.com>
Committed: Mon Nov 16 21:09:09 2015 +0800

----------------------------------------------------------------------
 .../ZookeeperIdentityBackend.java               |  8 +--
 .../main/java/org/apache/kerby/config/Conf.java | 48 ++++++-------
 .../java/org/apache/kerby/config/Config.java    | 35 +++++-----
 .../org/apache/kerby/config/ConfigImpl.java     | 71 ++++++++++++--------
 .../java/org/apache/kerby/config/ConfTest.java  | 11 +--
 .../kerby/kerberos/kerb/client/KrbConfig.java   | 43 ++++++++----
 .../kerberos/kerb/client/KrbConfigKey.java      |  2 +-
 .../TestKrbConfigLoadWithDefaultRealm.java      | 45 +++++++++++++
 .../src/test/resources/krb5-kdcrealm.conf       | 19 ++++++
 .../kerberos/kerb/common/KrbConfHelper.java     | 17 ++---
 .../kerby/kerberos/kerb/server/KdcConfig.java   | 44 ++++++------
 .../kerby/kerberos/kerb/server/KdcUtil.java     |  2 +-
 12 files changed, 225 insertions(+), 120 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/cf6e39b6/kerby-backend/zookeeper-backend/src/main/java/org/apache/kerby/kerberos/kdc/identitybackend/ZookeeperIdentityBackend.java
----------------------------------------------------------------------
diff --git a/kerby-backend/zookeeper-backend/src/main/java/org/apache/kerby/kerberos/kdc/identitybackend/ZookeeperIdentityBackend.java
b/kerby-backend/zookeeper-backend/src/main/java/org/apache/kerby/kerberos/kdc/identitybackend/ZookeeperIdentityBackend.java
index 3c91498..8449110 100644
--- a/kerby-backend/zookeeper-backend/src/main/java/org/apache/kerby/kerberos/kdc/identitybackend/ZookeeperIdentityBackend.java
+++ b/kerby-backend/zookeeper-backend/src/main/java/org/apache/kerby/kerberos/kdc/identitybackend/ZookeeperIdentityBackend.java
@@ -94,10 +94,10 @@ public class ZookeeperIdentityBackend extends AbstractIdentityBackend
{
      * Init Zookeeper Server and connection service, used to initialize the backend.
      */
     private void init() throws KrbException {
-        zkHost = getConfig().getString(ZKConfKey.ZK_HOST);
-        zkPort = getConfig().getInt(ZKConfKey.ZK_PORT);
+        zkHost = getConfig().getString(ZKConfKey.ZK_HOST, true);
+        zkPort = getConfig().getInt(ZKConfKey.ZK_PORT, true);
 
-        String dataDirString = getConfig().getString(ZKConfKey.DATA_DIR);
+        String dataDirString = getConfig().getString(ZKConfKey.DATA_DIR, true);
         if (dataDirString == null || dataDirString.isEmpty()) {
             File zooKeeperDir = new File(getBackendConfig().getConfDir(), "zookeeper");
             dataDir = new File(zooKeeperDir, "data");
@@ -111,7 +111,7 @@ public class ZookeeperIdentityBackend extends AbstractIdentityBackend
{
 
         LOG.info("Data dir: " + dataDir);
 
-        String dataLogDirString = getConfig().getString(ZKConfKey.DATA_LOG_DIR);
+        String dataLogDirString = getConfig().getString(ZKConfKey.DATA_LOG_DIR, true);
         if (dataLogDirString == null || dataLogDirString.isEmpty()) {
             File zooKeeperDir = new File(getBackendConfig().getConfDir(), "zookeeper");
             dataLogDir = new File(zooKeeperDir, "datalog");

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/cf6e39b6/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 a207f52..ad47157 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
@@ -129,9 +129,9 @@ public class Conf implements Config {
     }
 
     @Override
-    public String getString(ConfigKey name) {
+    public String getString(ConfigKey name, boolean useDefault) {
         checkAndLoad();
-        return config.getString(name);
+        return config.getString(name, useDefault);
     }
 
     @Override
@@ -169,24 +169,24 @@ public class Conf implements Config {
     }
 
     @Override
-    public Boolean getBoolean(ConfigKey name) {
+    public Boolean getBoolean(ConfigKey name, boolean useDefault) {
         checkAndLoad();
-        return config.getBoolean(name);
+        return config.getBoolean(name, useDefault);
     }
 
     @Override
-    public Boolean getBoolean(String name, boolean defaultValue) {
+    public Boolean getBoolean(String name, Boolean defaultValue) {
         checkAndLoad();
         return config.getBoolean(name, defaultValue);
     }
 
     @Override
-    public void setBoolean(String name, boolean value) {
+    public void setBoolean(String name, Boolean value) {
         setString(name, String.valueOf(value));
     }
 
     @Override
-    public void setBoolean(ConfigKey name, boolean value) {
+    public void setBoolean(ConfigKey name, Boolean value) {
         setString(name.getPropertyKey(), String.valueOf(value));
     }
 
@@ -197,24 +197,24 @@ public class Conf implements Config {
     }
 
     @Override
-    public Integer getInt(ConfigKey name) {
+    public Integer getInt(ConfigKey name, boolean useDefault) {
         checkAndLoad();
-        return config.getInt(name);
+        return config.getInt(name, useDefault);
     }
 
     @Override
-    public Integer getInt(String name, int defaultValue) {
+    public Integer getInt(String name, Integer defaultValue) {
         checkAndLoad();
         return config.getInt(name, defaultValue);
     }
 
     @Override
-    public void setInt(String name, int value) {
+    public void setInt(String name, Integer value) {
         setString(name, String.valueOf(value));
     }
 
     @Override
-    public void setInt(ConfigKey name, int value) {
+    public void setInt(ConfigKey name, Integer value) {
         setString(name.getPropertyKey(), String.valueOf(value));
     }
 
@@ -231,18 +231,18 @@ public class Conf implements Config {
     }
 
     @Override
-    public Long getLong(String name, long defaultValue) {
+    public Long getLong(String name, Long defaultValue) {
         checkAndLoad();
         return config.getLong(name, defaultValue);
     }
 
     @Override
-    public void setLong(String name, long value) {
+    public void setLong(String name, Long value) {
         setString(name, String.valueOf(value));
     }
 
     @Override
-    public void setLong(ConfigKey name, long value) {
+    public void setLong(ConfigKey name, Long value) {
         setString(name.getPropertyKey(), String.valueOf(value));
     }
 
@@ -253,24 +253,24 @@ public class Conf implements Config {
     }
 
     @Override
-    public Float getFloat(ConfigKey name) {
+    public Float getFloat(ConfigKey name, boolean useDefault) {
         checkAndLoad();
-        return config.getFloat(name);
+        return config.getFloat(name, useDefault);
     }
 
     @Override
-    public Float getFloat(String name, float defaultValue) {
+    public Float getFloat(String name, Float defaultValue) {
         checkAndLoad();
         return config.getFloat(name, defaultValue);
     }
 
     @Override
-    public void setFloat(String name, float value) {
+    public void setFloat(String name, Float value) {
         setString(name, String.valueOf(value));
     }
 
     @Override
-    public void setFloat(ConfigKey name, float value) {
+    public void setFloat(ConfigKey name, Float value) {
         setString(name.getPropertyKey(), String.valueOf(value));
     }
 
@@ -311,15 +311,17 @@ public class Conf implements Config {
     }
 
     @Override
-    public Class<?> getClass(String name, Class<?> defaultValue) throws ClassNotFoundException
{
+    public Class<?> getClass(String name, Class<?> defaultValue)
+            throws ClassNotFoundException {
         checkAndLoad();
         return config.getClass(name, defaultValue);
     }
 
     @Override
-    public Class<?> getClass(ConfigKey name) throws ClassNotFoundException {
+    public Class<?> getClass(ConfigKey name, boolean useDefault)
+            throws ClassNotFoundException {
         checkAndLoad();
-        return config.getClass(name);
+        return config.getClass(name, useDefault);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/cf6e39b6/kerby-config/src/main/java/org/apache/kerby/config/Config.java
----------------------------------------------------------------------
diff --git a/kerby-config/src/main/java/org/apache/kerby/config/Config.java b/kerby-config/src/main/java/org/apache/kerby/config/Config.java
index 81fe9f3..1e8c32c 100644
--- a/kerby-config/src/main/java/org/apache/kerby/config/Config.java
+++ b/kerby-config/src/main/java/org/apache/kerby/config/Config.java
@@ -32,7 +32,7 @@ public interface Config {
     Set<String> getNames();
 
     String getString(String name);
-    String getString(ConfigKey name);
+    String getString(ConfigKey name, boolean useDefault);
     String getString(String name, String defaultValue);
 
     /**
@@ -52,77 +52,76 @@ public interface Config {
     String getTrimmed(String name);
     String getTrimmed(ConfigKey name);
     Boolean getBoolean(String name);
-    Boolean getBoolean(ConfigKey name);
-    Boolean getBoolean(String name, boolean defaultValue);
+    Boolean getBoolean(ConfigKey name, boolean useDefault);
+    Boolean getBoolean(String name, Boolean defaultValue);
 
     /**
      * Set a boolean value for the specified property
      * @param name The property name
      * @param value The boolean value
      */
-    void setBoolean(String name, boolean value);
+    void setBoolean(String name, Boolean value);
 
     /**
      * Set a boolean value for the specified property
      * @param name The config key name
      * @param value The boolean value
      */
-    void setBoolean(ConfigKey name, boolean value);
+    void setBoolean(ConfigKey name, Boolean value);
 
     Integer getInt(String name);
-    Integer getInt(ConfigKey name);
-    Integer getInt(String name, int defaultValue);
+    Integer getInt(ConfigKey name, boolean useDefault);
+    Integer getInt(String name, Integer defaultValue);
 
     /**
      * Set an int value for the specified property
      * @param name The property name
      * @param value The string value
      */
-    void setInt(String name, int value);
+    void setInt(String name, Integer value);
 
     /**
      * Set an int value for the specified property
      * @param name The config key name
      * @param value The int value
      */
-    void setInt(ConfigKey name, int value);
-
+    void setInt(ConfigKey name, Integer value);
 
     Long getLong(String name);
     Long getLong(ConfigKey name);
-    Long getLong(String name, long defaultValue);
+    Long getLong(String name, Long defaultValue);
 
     /**
      * Set a long value for the specified property
      * @param name The property name
      * @param value The long value
      */
-    void setLong(String name, long value);
+    void setLong(String name, Long value);
 
     /**
      * Set a long value for the specified property
      * @param name The config key name
      * @param value The long value
      */
-    void setLong(ConfigKey name, long value);
+    void setLong(ConfigKey name, Long value);
 
     Float getFloat(String name);
-    Float getFloat(ConfigKey name);
-    Float getFloat(String name, float defaultValue);
+    Float getFloat(ConfigKey name, boolean useDefault);
+    Float getFloat(String name, Float defaultValue);
 
     /**
      * Set a float value for the specified property
      * @param name The property name
      * @param value The float value
      */
-    void setFloat(String name, float value);
+    void setFloat(String name, Float value);
 
     /**
      * Set a float value for the specified property
      * @param name The config key name
      * @param value The float value
      */
-    void setFloat(ConfigKey name, float value);
+    void setFloat(ConfigKey name, Float value);
 
     List<String> getList(String name);
     List<String> getList(String name, String[] defaultValue);
@@ -132,7 +131,7 @@ public interface Config {
 
     Class<?> getClass(String name) throws ClassNotFoundException;
     Class<?> getClass(String name, Class<?> defaultValue) throws ClassNotFoundException;
-    Class<?> getClass(ConfigKey name) throws ClassNotFoundException;
+    Class<?> getClass(ConfigKey name, boolean useDefault) throws ClassNotFoundException;
     <T> T getInstance(String name) throws ClassNotFoundException;
     <T> T getInstance(ConfigKey name) throws ClassNotFoundException;
     <T> T getInstance(String name, Class<T> xface) throws ClassNotFoundException;

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/cf6e39b6/kerby-config/src/main/java/org/apache/kerby/config/ConfigImpl.java
----------------------------------------------------------------------
diff --git a/kerby-config/src/main/java/org/apache/kerby/config/ConfigImpl.java b/kerby-config/src/main/java/org/apache/kerby/config/ConfigImpl.java
index e8a5b50..b4cf2b6 100644
--- a/kerby-config/src/main/java/org/apache/kerby/config/ConfigImpl.java
+++ b/kerby-config/src/main/java/org/apache/kerby/config/ConfigImpl.java
@@ -82,9 +82,10 @@ public class ConfigImpl implements Config {
     }
 
     @Override
-    public String getString(ConfigKey name) {
-        if (name.getDefaultValue() != null) {
-            return getString(name.getPropertyKey(), (String) name.getDefaultValue());
+    public String getString(ConfigKey name, boolean useDefault) {
+        if (useDefault) {
+            return getString(name.getPropertyKey(),
+                    (String) name.getDefaultValue());
         }
         return getString(name.getPropertyKey());
     }
@@ -123,15 +124,24 @@ public class ConfigImpl implements Config {
     }
 
     @Override
-    public Integer getInt(ConfigKey name) {
-        if (name.getDefaultValue() != null) {
-            return getInt(name.getPropertyKey(), (Integer) name.getDefaultValue());
+    public Integer getInt(ConfigKey name, boolean useDefault) {
+        if (useDefault) {
+            return getInt(name.getPropertyKey(),
+                    getDefaultValueAs(name, Integer.class));
         }
         return getInt(name.getPropertyKey());
     }
 
+    private <T> T getDefaultValueAs(ConfigKey confKey, Class<T> cls) {
+        Object defValue = confKey.getDefaultValue();
+        if (defValue != null && cls != null) {
+            return (T) defValue;
+        }
+        return null;
+    }
+
     @Override
-    public Integer getInt(String name, int defaultValue) {
+    public Integer getInt(String name, Integer defaultValue) {
         Integer result = getInt(name);
         if (result == null) {
             result = defaultValue;
@@ -140,12 +150,12 @@ public class ConfigImpl implements Config {
     }
 
     @Override
-    public void setInt(String name, int value) {
+    public void setInt(String name, Integer value) {
         set(name, String.valueOf(value));
     }
 
     @Override
-    public void setInt(ConfigKey name, int value) {
+    public void setInt(ConfigKey name, Integer value) {
         set(name.getPropertyKey(), String.valueOf(value));
     }
 
@@ -162,13 +172,14 @@ public class ConfigImpl implements Config {
     @Override
     public Long getLong(ConfigKey name) {
         if (name.getDefaultValue() != null) {
-            return getLong(name.getPropertyKey(), (Long) name.getDefaultValue());
+            return getLong(name.getPropertyKey(),
+                    getDefaultValueAs(name, Long.class));
         }
         return getLong(name.getPropertyKey());
     }
 
     @Override
-    public Long getLong(String name, long defaultValue) {
+    public Long getLong(String name, Long defaultValue) {
         Long result = getLong(name);
         if (result == null) {
             result = defaultValue;
@@ -177,12 +188,12 @@ public class ConfigImpl implements Config {
     }
 
     @Override
-    public void setLong(String name, long value) {
+    public void setLong(String name, Long value) {
         set(name, String.valueOf(value));
     }
 
     @Override
-    public void setLong(ConfigKey name, long value) {
+    public void setLong(ConfigKey name, Long value) {
         set(name.getPropertyKey(), String.valueOf(value));
     }
 
@@ -197,15 +208,16 @@ public class ConfigImpl implements Config {
     }
 
     @Override
-    public Float getFloat(ConfigKey name) {
-        if (name.getDefaultValue() != null) {
-            return getFloat(name.getPropertyKey(), (Float) name.getDefaultValue());
+    public Float getFloat(ConfigKey name, boolean useDefault) {
+        if (useDefault) {
+            return getFloat(name.getPropertyKey(),
+                    getDefaultValueAs(name, Float.class));
         }
         return getFloat(name.getPropertyKey());
     }
 
     @Override
-    public Float getFloat(String name, float defaultValue) {
+    public Float getFloat(String name, Float defaultValue) {
         Float result = getFloat(name);
         if (result == null) {
             result = defaultValue;
@@ -214,12 +226,12 @@ public class ConfigImpl implements Config {
     }
 
     @Override
-    public void setFloat(String name, float value) {
+    public void setFloat(String name, Float value) {
         set(name, String.valueOf(value));
     }
 
     @Override
-    public void setFloat(ConfigKey name, float value) {
+    public void setFloat(ConfigKey name, Float value) {
         set(name.getPropertyKey(), String.valueOf(value));
     }
 
@@ -234,8 +246,8 @@ public class ConfigImpl implements Config {
     }
 
     @Override
-    public Boolean getBoolean(ConfigKey name) {
-        if (name.getDefaultValue() != null) {
+    public Boolean getBoolean(ConfigKey name, boolean useDefault) {
+        if (useDefault) {
             return getBoolean(name.getPropertyKey(),
                     (Boolean) name.getDefaultValue());
         }
@@ -243,7 +255,7 @@ public class ConfigImpl implements Config {
     }
 
     @Override
-    public Boolean getBoolean(String name, boolean defaultValue) {
+    public Boolean getBoolean(String name, Boolean defaultValue) {
         Boolean result = getBoolean(name);
         if (result == null) {
             result = defaultValue;
@@ -252,12 +264,12 @@ public class ConfigImpl implements Config {
     }
 
     @Override
-    public void setBoolean(String name, boolean value) {
+    public void setBoolean(String name, Boolean value) {
         set(name, String.valueOf(value));
     }
 
     @Override
-    public void setBoolean(ConfigKey name, boolean value) {
+    public void setBoolean(ConfigKey name, Boolean value) {
         set(name.getPropertyKey(), String.valueOf(value));
     }
 
@@ -332,7 +344,8 @@ public class ConfigImpl implements Config {
     }
 
     @Override
-    public Class<?> getClass(String name, Class<?> defaultValue) throws ClassNotFoundException
{
+    public Class<?> getClass(String name, Class<?> defaultValue)
+            throws ClassNotFoundException {
         Class<?> result = getClass(name);
         if (result == null) {
             result = defaultValue;
@@ -341,9 +354,11 @@ public class ConfigImpl implements Config {
     }
 
     @Override
-    public Class<?> getClass(ConfigKey name) throws ClassNotFoundException {
-        if (name.getDefaultValue() != null) {
-            return getClass(name.getPropertyKey(), (Class<?>) name.getDefaultValue());
+    public Class<?> getClass(ConfigKey name, boolean useDefault)
+            throws ClassNotFoundException {
+        if (useDefault) {
+            return getClass(name.getPropertyKey(),
+                    (Class<?>) name.getDefaultValue());
         }
         return getClass(name.getPropertyKey());
     }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/cf6e39b6/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 185fe3f..33bc532 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
@@ -119,13 +119,16 @@ public class ConfTest {
     @Test
     public void testConfKey() {
         Conf conf = new Conf();
-        assertThat(conf.getString(TestConfKey.ADDRESS)).isEqualTo(TestConfKey.ADDRESS.getDefaultValue());
+        assertThat(conf.getString(TestConfKey.ADDRESS, true)).isEqualTo(
+                TestConfKey.ADDRESS.getDefaultValue());
         Map<String, String> mapConfig = new HashMap<String, String>();
         String myAddress = "www.google.com";
         mapConfig.put(TestConfKey.ADDRESS.getPropertyKey(), myAddress);
         conf.addMapConfig(mapConfig);
-        assertThat(conf.getString(TestConfKey.ADDRESS)).isEqualTo(myAddress);
-        assertThat(conf.getInt(TestConfKey.PORT)).isEqualTo(TestConfKey.PORT.getDefaultValue());
-        assertThat(conf.getBoolean(TestConfKey.ENABLE)).isEqualTo(TestConfKey.ENABLE.getDefaultValue());
+        assertThat(conf.getString(TestConfKey.ADDRESS, true)).isEqualTo(myAddress);
+        assertThat(conf.getInt(TestConfKey.PORT, true)).isEqualTo(
+                TestConfKey.PORT.getDefaultValue());
+        assertThat(conf.getBoolean(TestConfKey.ENABLE, true)).isEqualTo(
+                TestConfKey.ENABLE.getDefaultValue());
     }
 }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/cf6e39b6/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbConfig.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbConfig.java
b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbConfig.java
index 4649e14..78706b1 100644
--- a/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbConfig.java
+++ b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbConfig.java
@@ -31,7 +31,7 @@ import java.util.List;
 public class KrbConfig extends Conf {
 
     public boolean enableDebug() {
-        return getBoolean(KrbConfigKey.KRB_DEBUG);
+        return getBoolean(KrbConfigKey.KRB_DEBUG, true);
     }
 
     /**
@@ -40,7 +40,8 @@ public class KrbConfig extends Conf {
      * @return The kdc host
      */
     public String getKdcHost() {
-        return KrbConfHelper.getStringUnderSection(this, KrbConfigKey.KDC_HOST);
+        return KrbConfHelper.getStringUnderSection(this,
+                KrbConfigKey.KDC_HOST, true);
     }
 
     /**
@@ -77,8 +78,9 @@ public class KrbConfig extends Conf {
      * @return true to allow UDP, false otherwise
      */
     public boolean allowKdcUdp() {
-        return getBoolean(KrbConfigKey.KDC_ALLOW_UDP) || KrbConfHelper.getIntUnderSection(this,
-                KrbConfigKey.KDC_UDP_PORT) != null;
+        return getBoolean(KrbConfigKey.KDC_ALLOW_UDP, true)
+                || KrbConfHelper.getIntUnderSection(this,
+                        KrbConfigKey.KDC_UDP_PORT) != null;
     }
 
     /**
@@ -87,7 +89,8 @@ public class KrbConfig extends Conf {
      * @return true to allow TCP, false otherwise
      */
     public boolean allowKdcTcp() {
-        return getBoolean(KrbConfigKey.KDC_ALLOW_TCP) || KrbConfHelper.getIntUnderSection(this,
+        return getBoolean(KrbConfigKey.KDC_ALLOW_TCP, true)
+                || KrbConfHelper.getIntUnderSection(this,
                 KrbConfigKey.KDC_TCP_PORT) != null;
     }
 
@@ -110,7 +113,18 @@ public class KrbConfig extends Conf {
      * @return The kdc realm
      */
     public String getKdcRealm() {
-        return KrbConfHelper.getStringUnderSection(this, KrbConfigKey.KDC_REALM);
+        String realm = KrbConfHelper.getStringUnderSection(this,
+                KrbConfigKey.KDC_REALM, false);
+        if (realm == null) {
+            realm = KrbConfHelper.getStringUnderSection(this,
+                    KrbConfigKey.DEFAULT_REALM, false);
+            if (realm == null) {
+                realm = KrbConfHelper.getStringUnderSection(this,
+                        KrbConfigKey.KDC_REALM, true);
+            }
+        }
+
+        return realm;
     }
 
     /**
@@ -118,7 +132,7 @@ public class KrbConfig extends Conf {
      * @return true if preauth required
      */
     public boolean isPreauthRequired() {
-        return getBoolean(KrbConfigKey.PREAUTH_REQUIRED);
+        return getBoolean(KrbConfigKey.PREAUTH_REQUIRED, true);
     }
 
     /**
@@ -126,7 +140,7 @@ public class KrbConfig extends Conf {
      * @return The tgs principal
      */
     public String getTgsPrincipal() {
-        return getString(KrbConfigKey.TGS_PRINCIPAL);
+        return getString(KrbConfigKey.TGS_PRINCIPAL, true);
     }
 
     /**
@@ -142,7 +156,7 @@ public class KrbConfig extends Conf {
      * @return true if empty address is allowed
      */
     public boolean isEmptyAddressesAllowed() {
-        return getBoolean(KrbConfigKey.EMPTY_ADDRESSES_ALLOWED);
+        return getBoolean(KrbConfigKey.EMPTY_ADDRESSES_ALLOWED, true);
     }
 
     /**
@@ -158,7 +172,7 @@ public class KrbConfig extends Conf {
      * @return true if post dated is allowed
      */
     public boolean isPostdatedAllowed() {
-        return getBoolean(KrbConfigKey.POSTDATED_ALLOWED);
+        return getBoolean(KrbConfigKey.POSTDATED_ALLOWED, true);
     }
 
     /**
@@ -174,7 +188,7 @@ public class KrbConfig extends Conf {
      * @return true if renew is allowed
      */
     public boolean isRenewableAllowed() {
-        return getBoolean(KrbConfigKey.RENEWABLE_ALLOWED);
+        return getBoolean(KrbConfigKey.RENEWABLE_ALLOWED, true);
     }
 
     /**
@@ -214,7 +228,7 @@ public class KrbConfig extends Conf {
      * @return true if pa encrypt time required
      */
     public boolean isPaEncTimestampRequired() {
-        return getBoolean(KrbConfigKey.PA_ENC_TIMESTAMP_REQUIRED);
+        return getBoolean(KrbConfigKey.PA_ENC_TIMESTAMP_REQUIRED, true);
     }
 
     /**
@@ -222,7 +236,7 @@ public class KrbConfig extends Conf {
      * @return true if body checksum verified
      */
     public boolean isBodyChecksumVerified() {
-        return getBoolean(KrbConfigKey.VERIFY_BODY_CHECKSUM);
+        return getBoolean(KrbConfigKey.VERIFY_BODY_CHECKSUM, true);
     }
 
     /**
@@ -230,7 +244,8 @@ public class KrbConfig extends Conf {
      * @return The default realm
      */
     public String getDefaultRealm() {
-        return KrbConfHelper.getStringUnderSection(this, KrbConfigKey.DEFAULT_REALM);
+        return KrbConfHelper.getStringUnderSection(this,
+                KrbConfigKey.DEFAULT_REALM, true);
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/cf6e39b6/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbConfigKey.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbConfigKey.java
b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbConfigKey.java
index 295aa64..4533b4e 100644
--- a/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbConfigKey.java
+++ b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbConfigKey.java
@@ -45,7 +45,7 @@ public enum KrbConfigKey implements SectionConfigKey {
     RENEWABLE_ALLOWED(true),
     VERIFY_BODY_CHECKSUM(true),
     PERMITTED_ENCTYPES("aes128-cts-hmac-sha1-96", "libdefaults"),
-    DEFAULT_REALM("EXAMPLE.COM", "libdefaults"),
+    DEFAULT_REALM(null, "libdefaults"),
     DNS_LOOKUP_KDC(false, "libdefaults"),
     DNS_LOOKUP_REALM(false, "libdefaults"),
     ALLOW_WEAK_CRYPTO(true, "libdefaults"),

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/cf6e39b6/kerby-kerb/kerb-client/src/test/java/org/apache/kerby/kerberos/kerb/client/TestKrbConfigLoadWithDefaultRealm.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-client/src/test/java/org/apache/kerby/kerberos/kerb/client/TestKrbConfigLoadWithDefaultRealm.java
b/kerby-kerb/kerb-client/src/test/java/org/apache/kerby/kerberos/kerb/client/TestKrbConfigLoadWithDefaultRealm.java
new file mode 100644
index 0000000..3835de4
--- /dev/null
+++ b/kerby-kerb/kerb-client/src/test/java/org/apache/kerby/kerberos/kerb/client/TestKrbConfigLoadWithDefaultRealm.java
@@ -0,0 +1,45 @@
+/**
+ *  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.kerby.kerberos.kerb.client;
+
+import org.junit.Test;
+
+import java.io.File;
+import java.net.URL;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * Test for loading configurations form krb5.conf with default kdc realm.
+ * krb5.conf is the configuration file in MIT Kerberos.
+ */
+public class TestKrbConfigLoadWithDefaultRealm {
+
+    @Test
+    public void test() throws Exception {
+        URL confFileUrl = TestKrbConfigLoadWithDefaultRealm.class.getResource(
+                        "/krb5-kdcrealm.conf");
+        File confFile = new File(confFileUrl.toURI());
+
+        KrbConfig krbConfig = new KrbConfig();
+        krbConfig.addIniConfig(confFile);
+        assertThat(krbConfig.getKdcRealm()).isEqualTo("KRB.COM");
+    }
+}

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/cf6e39b6/kerby-kerb/kerb-client/src/test/resources/krb5-kdcrealm.conf
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-client/src/test/resources/krb5-kdcrealm.conf b/kerby-kerb/kerb-client/src/test/resources/krb5-kdcrealm.conf
new file mode 100644
index 0000000..43e68a5
--- /dev/null
+++ b/kerby-kerb/kerb-client/src/test/resources/krb5-kdcrealm.conf
@@ -0,0 +1,19 @@
+#
+# 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.
+#
+[libdefaults]
+  default_realm = KRB.COM
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/cf6e39b6/kerby-kerb/kerb-common/src/main/java/org/apache/kerby/kerberos/kerb/common/KrbConfHelper.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-common/src/main/java/org/apache/kerby/kerberos/kerb/common/KrbConfHelper.java
b/kerby-kerb/kerb-common/src/main/java/org/apache/kerby/kerberos/kerb/common/KrbConfHelper.java
index 477dee1..27aab36 100644
--- a/kerby-kerb/kerb-common/src/main/java/org/apache/kerby/kerberos/kerb/common/KrbConfHelper.java
+++ b/kerby-kerb/kerb-common/src/main/java/org/apache/kerby/kerberos/kerb/common/KrbConfHelper.java
@@ -37,21 +37,22 @@ public class KrbConfHelper {
      */
     private static final String LIST_SPLITTER = " |,";
 
-    public static String getStringUnderSection(Conf conf, SectionConfigKey key) {
+    public static String getStringUnderSection(
+            Conf conf, SectionConfigKey key, boolean useDefault) {
         Config subConfig = conf.getConfig(key.getSectionName());
         if (subConfig != null) {
-            return subConfig.getString(key);
+            return subConfig.getString(key, useDefault);
         } else {
-            return conf.getString(key);
+            return conf.getString(key, useDefault);
         }
     }
 
     public static Boolean getBooleanUnderSection(Conf conf, SectionConfigKey key) {
         Config subConfig = conf.getConfig(key.getSectionName());
         if (subConfig != null) {
-            return subConfig.getBoolean(key);
+            return subConfig.getBoolean(key, true);
         } else {
-            return conf.getBoolean(key);
+            return conf.getBoolean(key, true);
         }
     }
 
@@ -67,15 +68,15 @@ public class KrbConfHelper {
     public static Integer getIntUnderSection(Conf conf, SectionConfigKey key) {
         Config subConfig = conf.getConfig(key.getSectionName());
         if (subConfig != null) {
-            return subConfig.getInt(key);
+            return subConfig.getInt(key, true);
         } else {
-            return conf.getInt(key);
+            return conf.getInt(key, true);
         }
     }
 
     public static String[] getStringArrayUnderSection(Conf conf,
                                                       SectionConfigKey key) {
-        String value = getStringUnderSection(conf, key);
+        String value = getStringUnderSection(conf, key, true);
         String[] values = value.split(LIST_SPLITTER);
         return values;
     }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/cf6e39b6/kerby-kerb/kerb-server/src/main/java/org/apache/kerby/kerberos/kerb/server/KdcConfig.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-server/src/main/java/org/apache/kerby/kerberos/kerb/server/KdcConfig.java
b/kerby-kerb/kerb-server/src/main/java/org/apache/kerby/kerberos/kerb/server/KdcConfig.java
index 82b8dfd..1f4bf8d 100644
--- a/kerby-kerb/kerb-server/src/main/java/org/apache/kerby/kerberos/kerb/server/KdcConfig.java
+++ b/kerby-kerb/kerb-server/src/main/java/org/apache/kerby/kerberos/kerb/server/KdcConfig.java
@@ -32,15 +32,16 @@ import java.util.List;
 public class KdcConfig extends Conf {
 
     public boolean enableDebug() {
-        return getBoolean(KdcConfigKey.KRB_DEBUG);
+        return getBoolean(KdcConfigKey.KRB_DEBUG, true);
     }
 
     public String getKdcServiceName() {
-        return getString(KdcConfigKey.KDC_SERVICE_NAME);
+        return getString(KdcConfigKey.KDC_SERVICE_NAME, true);
     }
 
     public String getKdcHost() {
-        return KrbConfHelper.getStringUnderSection(this, KdcConfigKey.KDC_HOST);
+        return KrbConfHelper.getStringUnderSection(this,
+                KdcConfigKey.KDC_HOST, true);
     }
 
     public int getKdcPort() {
@@ -66,7 +67,8 @@ public class KdcConfig extends Conf {
      * @return true to allow TCP, false otherwise
      */
     public Boolean allowTcp() {
-        return getBoolean(KdcConfigKey.KDC_ALLOW_TCP) || KrbConfHelper.getIntUnderSection(this,
+        return getBoolean(KdcConfigKey.KDC_ALLOW_TCP, true)
+                || KrbConfHelper.getIntUnderSection(this,
                 KdcConfigKey.KDC_TCP_PORT) != null;
     }
 
@@ -75,7 +77,8 @@ public class KdcConfig extends Conf {
      * @return true to allow UDP, false otherwise
      */
     public Boolean allowUdp() {
-        return getBoolean(KdcConfigKey.KDC_ALLOW_UDP) || KrbConfHelper.getIntUnderSection(this,
+        return getBoolean(KdcConfigKey.KDC_ALLOW_UDP, true)
+                || KrbConfHelper.getIntUnderSection(this,
                 KdcConfigKey.KDC_UDP_PORT) != null;
     }
 
@@ -90,19 +93,19 @@ public class KdcConfig extends Conf {
 
     public String getKdcRealm() {
         return KrbConfHelper.getStringUnderSection(this,
-                KdcConfigKey.KDC_REALM);
+                KdcConfigKey.KDC_REALM, true);
     }
 
     public String getKdcDomain() {
-        return getString(KdcConfigKey.KDC_DOMAIN);
+        return getString(KdcConfigKey.KDC_DOMAIN, true);
     }
 
     public boolean isPreauthRequired() {
-        return getBoolean(KdcConfigKey.PREAUTH_REQUIRED);
+        return getBoolean(KdcConfigKey.PREAUTH_REQUIRED, true);
     }
 
     public boolean isAllowTokenPreauth() {
-        return getBoolean(KdcConfigKey.ALLOW_TOKEN_PREAUTH);
+        return getBoolean(KdcConfigKey.ALLOW_TOKEN_PREAUTH, true);
     }
 
     public long getAllowableClockSkew() {
@@ -110,23 +113,23 @@ public class KdcConfig extends Conf {
     }
 
     public boolean isEmptyAddressesAllowed() {
-        return getBoolean(KdcConfigKey.EMPTY_ADDRESSES_ALLOWED);
+        return getBoolean(KdcConfigKey.EMPTY_ADDRESSES_ALLOWED, true);
     }
 
     public boolean isForwardableAllowed() {
-        return getBoolean(KdcConfigKey.FORWARDABLE_ALLOWED);
+        return getBoolean(KdcConfigKey.FORWARDABLE_ALLOWED, true);
     }
 
     public boolean isPostdatedAllowed() {
-        return getBoolean(KdcConfigKey.POSTDATED_ALLOWED);
+        return getBoolean(KdcConfigKey.POSTDATED_ALLOWED, true);
     }
 
     public boolean isProxiableAllowed() {
-        return getBoolean(KdcConfigKey.PROXIABLE_ALLOWED);
+        return getBoolean(KdcConfigKey.PROXIABLE_ALLOWED, true);
     }
 
     public boolean isRenewableAllowed() {
-        return getBoolean(KdcConfigKey.RENEWABLE_ALLOWED);
+        return getBoolean(KdcConfigKey.RENEWABLE_ALLOWED, true);
     }
 
     public long getMaximumRenewableLifetime() {
@@ -146,11 +149,11 @@ public class KdcConfig extends Conf {
     }
 
     public boolean isPaEncTimestampRequired() {
-        return getBoolean(KdcConfigKey.PA_ENC_TIMESTAMP_REQUIRED);
+        return getBoolean(KdcConfigKey.PA_ENC_TIMESTAMP_REQUIRED, true);
     }
 
     public boolean isBodyChecksumVerified() {
-        return getBoolean(KdcConfigKey.VERIFY_BODY_CHECKSUM);
+        return getBoolean(KdcConfigKey.VERIFY_BODY_CHECKSUM, true);
     }
 
     public boolean isRestrictAnonymousToTgt() {
@@ -164,14 +167,17 @@ public class KdcConfig extends Conf {
     }
 
     public String getVerifyKeyConfig() {
-        return KrbConfHelper.getStringUnderSection(this, KdcConfigKey.VERIFY_KEY);
+        return KrbConfHelper.getStringUnderSection(this,
+                KdcConfigKey.VERIFY_KEY, true);
     }
 
     public String getDecryptionKeyConfig() {
-        return KrbConfHelper.getStringUnderSection(this, KdcConfigKey.DECRYPTION_KEY);
+        return KrbConfHelper.getStringUnderSection(this,
+                KdcConfigKey.DECRYPTION_KEY, true);
     }
     
     public List<String> getIssuers() {
-        return Arrays.asList(KrbConfHelper.getStringArrayUnderSection(this, KdcConfigKey.ISSUERS));
+        return Arrays.asList(KrbConfHelper.getStringArrayUnderSection(this,
+                KdcConfigKey.ISSUERS));
     }
 }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/cf6e39b6/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 1b41a08..198a2f0 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
@@ -90,7 +90,7 @@ public final class KdcUtil {
     public static IdentityBackend getBackend(
             BackendConfig backendConfig) throws KrbException {
         String backendClassName = backendConfig.getString(
-                KdcConfigKey.KDC_IDENTITY_BACKEND);
+                KdcConfigKey.KDC_IDENTITY_BACKEND, true);
         if (backendClassName == null) {
             backendClassName = MemoryIdentityBackend.class.getCanonicalName();
         }


Mime
View raw message