directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zengl...@apache.org
Subject directory-kerby git commit: DIRKRB-661 Add duration time parse support when parsing krb5.conf.
Date Mon, 16 Oct 2017 08:45:09 GMT
Repository: directory-kerby
Updated Branches:
  refs/heads/cross-realm 9eb294fcc -> 73a612b31


DIRKRB-661 Add duration time parse support when parsing krb5.conf.


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

Branch: refs/heads/cross-realm
Commit: 73a612b31e152b6c756bafba57ee9e06393f3bb7
Parents: 9eb294f
Author: zenglinx <frank.zeng@intel.com>
Authored: Mon Oct 16 16:44:54 2017 +0800
Committer: zenglinx <frank.zeng@intel.com>
Committed: Mon Oct 16 16:44:54 2017 +0800

----------------------------------------------------------------------
 .../main/java/org/apache/kerby/KOptions.java    | 60 +++++++++++---------
 .../kerby/kerberos/kerb/client/KrbConfig.java   | 16 ++++--
 .../kerby/kerberos/kerb/client/KrbContext.java  |  5 +-
 .../kerb/client/request/KdcRequest.java         |  3 +-
 .../kerberos/kerb/client/KrbConfigLoadTest.java |  4 +-
 5 files changed, 52 insertions(+), 36 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/73a612b3/kerby-common/kerby-util/src/main/java/org/apache/kerby/KOptions.java
----------------------------------------------------------------------
diff --git a/kerby-common/kerby-util/src/main/java/org/apache/kerby/KOptions.java b/kerby-common/kerby-util/src/main/java/org/apache/kerby/KOptions.java
index 24c6a9d..ce27e37 100644
--- a/kerby-common/kerby-util/src/main/java/org/apache/kerby/KOptions.java
+++ b/kerby-common/kerby-util/src/main/java/org/apache/kerby/KOptions.java
@@ -81,34 +81,7 @@ public class KOptions {
                 throw new IllegalArgumentException("Fail to parse the date: " + strValue);
             }
         } else if (kt == KOptionType.DURATION) {
-            int duration;
-            Matcher matcherColon = Pattern.compile("\\d+(?::\\d+){0,2}").matcher(strValue);
-            Matcher matcherWord = Pattern.compile("(?:(\\d+)D)?(?:(\\d+)H)?(?:(\\d+)M)?(?:(\\d+)S)?",
-                Pattern.CASE_INSENSITIVE).matcher(strValue);
-            if (matcherColon.matches()) {
-                String[] durations = strValue.split(":");
-                if (durations.length == 1) {
-                    duration = Integer.valueOf(durations[0]);
-                } else if (durations.length == 2) {
-                    duration = Integer.valueOf(durations[0]) * 3600 + Integer.valueOf(durations[1])
* 60;
-                } else {
-                    duration = Integer.valueOf(durations[0]) * 3600 + Integer.valueOf(durations[1])
* 60;
-                    duration += Integer.valueOf(durations[2]);
-                }
-                kopt.setValue(duration);
-            } else if (matcherWord.matches()) {
-                int[] durations = new int[4];
-                for (int i = 0; i < 4; i++) {
-                    String durationMatch = matcherWord.group(i + 1);
-                    if (durationMatch != null) {
-                        durations[i] = Integer.valueOf(durationMatch);
-                    }
-                }
-                duration = durations[0] * 86400 + durations[1] * 3600 + durations[2] * 60
+ durations[3];
-                kopt.setValue(duration);
-            } else {
-                throw new IllegalArgumentException("Text can't be parsed to a Duration: "
+ strValue);
-            }
+            kopt.setValue(parseDuration(strValue));
         } else if (kt == KOptionType.BOOL) {
             kopt.setValue(Boolean.valueOf(strValue));
         } else {
@@ -117,6 +90,37 @@ public class KOptions {
         return true;
     }
 
+  public static int parseDuration(String strValue) {
+    int duration;
+    Matcher matcherColon = Pattern.compile("\\d+(?::\\d+){0,2}").matcher(strValue);
+    Matcher matcherWord = Pattern.compile("(?:(\\d+)D)?(?:(\\d+)H)?(?:(\\d+)M)?(?:(\\d+)S)?",
+        Pattern.CASE_INSENSITIVE).matcher(strValue);
+    if (matcherColon.matches()) {
+      String[] durations = strValue.split(":");
+      if (durations.length == 1) {
+          duration = Integer.valueOf(durations[0]);
+      } else if (durations.length == 2) {
+          duration = Integer.valueOf(durations[0]) * 3600 + Integer.valueOf(durations[1])
* 60;
+      } else {
+          duration = Integer.valueOf(durations[0]) * 3600 + Integer.valueOf(durations[1])
* 60;
+          duration += Integer.valueOf(durations[2]);
+      }
+    } else if (matcherWord.matches()) {
+        int[] durations = new int[4];
+        for (int i = 0; i < 4; i++) {
+          String durationMatch = matcherWord.group(i + 1);
+          if (durationMatch != null) {
+            durations[i] = Integer.valueOf(durationMatch);
+          }
+        }
+        duration = durations[0] * 86400 + durations[1] * 3600 + durations[2] * 60 + durations[3];
+    } else {
+        throw new IllegalArgumentException("Text can't be parsed to a Duration: " + strValue);
+    }
+
+    return duration;
+  }
+
     public void add(KOption option) {
         if (option != null) {
             options.put(option, option);

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/73a612b3/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 9ffd6bd..3e0d046 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
@@ -277,16 +277,24 @@ public class KrbConfig extends Krb5Conf {
      * Get ticket life time.
      * @return The ticket life time
      */
-    public long getTicketLifetime() {
-        return getLong(KrbConfigKey.TICKET_LIFETIME, true, LIBDEFAULT);
+    public String getTicketLifetime() {
+        try {
+            return Long.toString(getLong(KrbConfigKey.TICKET_LIFETIME, true, LIBDEFAULT));
+        } catch (Exception e) {
+            return getString(KrbConfigKey.TICKET_LIFETIME, true, LIBDEFAULT);
+        }
     }
 
     /**
      * Get renew life time.
      * @return The renew life time
      */
-    public long getRenewLifetime() {
-        return getLong(KrbConfigKey.RENEW_LIFETIME, true, LIBDEFAULT);
+    public String getRenewLifetime() {
+        try {
+            return Long.toString(getLong(KrbConfigKey.RENEW_LIFETIME, true, LIBDEFAULT));
+        } catch (Exception e) {
+            return getString(KrbConfigKey.RENEW_LIFETIME, true, LIBDEFAULT);
+        }
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/73a612b3/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbContext.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbContext.java
b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbContext.java
index 1534f90..eb60aaa 100644
--- a/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbContext.java
+++ b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbContext.java
@@ -19,6 +19,7 @@
  */
 package org.apache.kerby.kerberos.kerb.client;
 
+import org.apache.kerby.KOptions;
 import org.apache.kerby.kerberos.kerb.client.preauth.PreauthHandler;
 import org.apache.kerby.kerberos.kerb.crypto.util.Nonce;
 
@@ -66,7 +67,9 @@ public class KrbContext {
      * @return The ticket valid time
      */
     public long getTicketValidTime() {
-        return getConfig().getTicketLifetime() * 1000;
+        String ticketValidTimeStr = getConfig().getTicketLifetime();
+        long ticketValidTime = KOptions.parseDuration(ticketValidTimeStr);
+        return ticketValidTime * 1000;
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/73a612b3/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/request/KdcRequest.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/request/KdcRequest.java
b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/request/KdcRequest.java
index be5efc4..6d26341 100644
--- a/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/request/KdcRequest.java
+++ b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/request/KdcRequest.java
@@ -203,7 +203,8 @@ public abstract class KdcRequest {
             if (getRequestOptions().contains(KrbOption.RENEWABLE_TIME)) {
                 renewLifetime = getRequestOptions().getIntegerOption(KrbOption.RENEWABLE_TIME);
             } else {
-                renewLifetime = getContext().getKrbSetting().getKrbConfig().getRenewLifetime();
+                String renewLifetimeStr = getContext().getKrbSetting().getKrbConfig().getRenewLifetime();
+                renewLifetime = KOptions.parseDuration(renewLifetimeStr);
             }
             rtime = new KerberosTime(startTime + renewLifetime * 1000);
         }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/73a612b3/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 ac9b3da..eb792d1 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
@@ -46,8 +46,8 @@ public class KrbConfigLoadTest {
         assertThat(krbConfig.getDnsLookUpKdc()).isFalse();
         assertThat(krbConfig.getDnsLookUpRealm()).isFalse();
         assertThat(krbConfig.getAllowWeakCrypto()).isTrue();
-        assertThat(krbConfig.getTicketLifetime()).isEqualTo(24 * 3600);
-        assertThat(krbConfig.getRenewLifetime()).isEqualTo(7 * 24 * 3600);
+        assertThat(krbConfig.getTicketLifetime()).isEqualTo("86400");
+        assertThat(krbConfig.getRenewLifetime()).isEqualTo("604800");
         assertThat(krbConfig.isForwardableAllowed()).isTrue();
         assertThat(krbConfig.getEncryptionTypes()).hasSize(2)
                 .contains(EncryptionType.DES_CBC_CRC,


Mime
View raw message