coheigea commented on a change in pull request #38:
URL: https://github.com/apache/directory-server/pull/38#discussion_r446040492



##########
File path: 
protocol-kerberos/src/main/java/org/apache/directory/server/kerberos/kdc/KdcServer.java
##########
@@ -255,4 +252,36 @@ public String toString()
 
         return sb.toString();
     }
+
+    private void initReplayCache()
+    {
+        LOG.debug( "initializing the kerberos replay cache" );
+
+        Class<? extends ReplayCache> clazz = config.getReplayCacheType();
+        if ( clazz == null )
+        {
+            LOG.trace( "Kerberos replay cache is disabled" );
+            return;
+        }
+
+        LOG.debug( "Creating ReplayCache of type {}", clazz.getName() );
+        ReplayCache instance = null;
+        try
+        {
+            try
+            {
+                instance = clazz.getConstructor( Long.TYPE ).newInstance( 
config.getAllowableClockSkew() );
+            }
+            catch ( NoSuchMethodException e )
+            {
+                instance = clazz.newInstance();
+            }
+        }
+        catch ( Exception e )
+        {
+            LOG.error( "Failed creating ReplayCache of type {}. Replay cache 
will not be used.", clazz.getName() );

Review comment:
       Shouldn't we log the exception message at least here?

##########
File path: 
protocol-kerberos/src/test/java/org/apache/directory/server/kerberos/protocol/TGSReplayCacheTest.java
##########
@@ -0,0 +1,193 @@
+/*
+ *  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.directory.server.kerberos.protocol;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.text.ParseException;
+
+import javax.security.auth.kerberos.KerberosPrincipal;
+
+import org.apache.directory.server.kerberos.KerberosConfig;
+import org.apache.directory.server.kerberos.kdc.KdcServer;
+import 
org.apache.directory.server.kerberos.protocol.AbstractAuthenticationServiceTest.KrbDummySession;
+import 
org.apache.directory.server.kerberos.shared.crypto.encryption.CipherTextHandler;
+import org.apache.directory.server.kerberos.shared.replay.ReplayCache;
+import org.apache.directory.shared.kerberos.KerberosTime;
+import org.apache.directory.shared.kerberos.codec.options.KdcOptions;
+import org.apache.directory.shared.kerberos.components.EncTicketPart;
+import org.apache.directory.shared.kerberos.components.EncryptionKey;
+import org.apache.directory.shared.kerberos.components.KdcReq;
+import org.apache.directory.shared.kerberos.components.KdcReqBody;
+import org.apache.directory.shared.kerberos.exceptions.ErrorType;
+import org.apache.directory.shared.kerberos.exceptions.KerberosException;
+import org.apache.directory.shared.kerberos.messages.KrbError;
+import org.apache.directory.shared.kerberos.messages.TgsRep;
+import org.apache.directory.shared.kerberos.messages.Ticket;
+import org.junit.After;
+import org.junit.Test;
+
+/**
+ * Tests for configurable {@link ReplayCache}.
+ */
+public class TGSReplayCacheTest extends AbstractTicketGrantingServiceTest
+{
+    private KdcServer kdcServer;
+    private KerberosProtocolHandler handler;
+
+    /**
+     * Shutdown the Kerberos server
+     */
+    @After
+    public void shutDown()
+    {
+        kdcServer.stop();
+    }
+
+    /**
+     * Tests the replay cache is used by default.
+     */
+    @Test
+    public void testDefaultReplayCache() throws Exception
+    {
+        initKdcServer( new KerberosConfig() );
+
+        KdcReq message = createTgsRequest();
+
+        KrbDummySession session = new KrbDummySession();
+        handler.messageReceived( session, message );
+        assertEquals( "session.getMessage() instanceOf", TgsRep.class, 
session.getMessage().getClass() );
+
+        handler.messageReceived( session, message );
+        Object msg = session.getMessage();
+        assertEquals( "session.getMessage() instanceOf", KrbError.class, 
msg.getClass() );
+        KrbError error = (KrbError) msg;
+        assertEquals( "Replay not detected", ErrorType.KRB_AP_ERR_REPEAT, 
error.getErrorCode() );
+    }
+
+    /**
+     * Tests the replay cache is not used if the type is explicitly set to 
null.
+     */
+    @Test
+    public void testNullReplayCacheType() throws Exception
+    {
+        KerberosConfig config = new KerberosConfig();
+        config.setReplayCacheType( null );
+        initKdcServer( config );
+
+        KdcReq message = createTgsRequest();
+
+        KrbDummySession session = new KrbDummySession();
+        handler.messageReceived( session, message );
+        assertEquals( "session.getMessage() instanceOf", TgsRep.class, 
session.getMessage().getClass() );
+
+        handler.messageReceived( session, message );
+        assertEquals( "session.getMessage() instanceOf", TgsRep.class, 
session.getMessage().getClass() );
+    }
+
+    /**
+     * Tests that custom replay cache can be .

Review comment:
       can be set/configured




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to