]> source.dussan.org Git - gitblit.git/commitdiff
Elevate the public key manager to a top-level manager
authorJames Moger <james.moger@gitblit.com>
Tue, 18 Mar 2014 01:30:46 +0000 (21:30 -0400)
committerJames Moger <james.moger@gitblit.com>
Thu, 10 Apr 2014 22:58:09 +0000 (18:58 -0400)
22 files changed:
src/main/distrib/data/gitblit.properties
src/main/java/com/gitblit/DaggerModule.java
src/main/java/com/gitblit/FederationClient.java
src/main/java/com/gitblit/GitBlit.java
src/main/java/com/gitblit/manager/GitblitManager.java
src/main/java/com/gitblit/manager/IGitblit.java
src/main/java/com/gitblit/servlet/GitblitContext.java
src/main/java/com/gitblit/transport/ssh/CachingPublicKeyAuthenticator.java
src/main/java/com/gitblit/transport/ssh/FileKeyManager.java
src/main/java/com/gitblit/transport/ssh/IKeyManager.java [deleted file]
src/main/java/com/gitblit/transport/ssh/IPublicKeyManager.java [new file with mode: 0644]
src/main/java/com/gitblit/transport/ssh/MemoryKeyManager.java [new file with mode: 0644]
src/main/java/com/gitblit/transport/ssh/NullKeyManager.java
src/main/java/com/gitblit/transport/ssh/SshCommandFactory.java
src/main/java/com/gitblit/transport/ssh/SshDaemon.java
src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java
src/main/java/com/gitblit/transport/ssh/gitblit/BaseKeyCommand.java
src/main/java/com/gitblit/wicket/GitBlitWebApp.java
src/test/config/test-gitblit.properties
src/test/java/com/gitblit/tests/BogusPublicKeyAuthenticator.java [deleted file]
src/test/java/com/gitblit/tests/GitBlitSuite.java
src/test/java/com/gitblit/tests/SshDaemonTest.java

index 52bb252b15ee6a9016d63473c9d9b5d5c605085b..64a52f5cc548da1898e8438b2be4a0b13e0ecb43 100644 (file)
@@ -129,11 +129,6 @@ git.sshKeysFolder= ${baseFolder}/ssh
 # SINCE 1.5.0\r
 git.sshBackend = NIO2\r
 \r
-# SSH public key authenticator\r
-#\r
-# SINCE 1.5.0\r
-git.sshPublicKeyAuthenticator = com.gitblit.transport.ssh.CachingPublicKeyAuthenticator\r
-\r
 # Allow push/pull over http/https with JGit servlet.\r
 # If you do NOT want to allow Git clients to clone/push to Gitblit set this\r
 # to false.  You might want to do this if you are only using ssh:// or git://.\r
index 5ae8b253b17de4e572ab7c5f9eb02275f74d344a..b109f1db1bf828c3e410d72bea4c013eb6c33b9d 100644 (file)
@@ -32,6 +32,11 @@ import com.gitblit.manager.ProjectManager;
 import com.gitblit.manager.RepositoryManager;
 import com.gitblit.manager.RuntimeManager;
 import com.gitblit.manager.UserManager;
+import com.gitblit.transport.ssh.FileKeyManager;
+import com.gitblit.transport.ssh.IPublicKeyManager;
+import com.gitblit.transport.ssh.MemoryKeyManager;
+import com.gitblit.transport.ssh.NullKeyManager;
+import com.gitblit.utils.StringUtils;
 import com.gitblit.wicket.GitBlitWebApp;
 
 import dagger.Module;
@@ -53,6 +58,7 @@ import dagger.Provides;
                        INotificationManager.class,
                        IUserManager.class,
                        IAuthenticationManager.class,
+                       IPublicKeyManager.class,
                        IRepositoryManager.class,
                        IProjectManager.class,
                        IFederationManager.class,
@@ -62,7 +68,7 @@ import dagger.Provides;
 
                        // the Gitblit Wicket app
                        GitBlitWebApp.class
-       }
+               }
 )
 public class DaggerModule {
 
@@ -91,6 +97,31 @@ public class DaggerModule {
                                userManager);
        }
 
+       @Provides @Singleton IPublicKeyManager providePublicKeyManager(
+                       IStoredSettings settings,
+                       IRuntimeManager runtimeManager) {
+
+               String clazz = settings.getString(Keys.git.sshKeysManager, FileKeyManager.class.getName());
+               if (StringUtils.isEmpty(clazz)) {
+                       clazz = FileKeyManager.class.getName();
+               }
+               if (FileKeyManager.class.getName().equals(clazz)) {
+                       return new FileKeyManager(runtimeManager);
+               } else if (NullKeyManager.class.getName().equals(clazz)) {
+                       return new NullKeyManager();
+               } else if (MemoryKeyManager.class.getName().equals(clazz)) {
+                       return new MemoryKeyManager();
+               } else {
+                       try {
+                               Class<?> mgrClass = Class.forName(clazz);
+                               return (IPublicKeyManager) mgrClass.newInstance();
+                       } catch (Exception e) {
+
+                       }
+                       return null;
+               }
+       }
+
        @Provides @Singleton IRepositoryManager provideRepositoryManager(
                        IRuntimeManager runtimeManager,
                        IUserManager userManager) {
@@ -127,6 +158,7 @@ public class DaggerModule {
                        INotificationManager notificationManager,
                        IUserManager userManager,
                        IAuthenticationManager authenticationManager,
+                       IPublicKeyManager publicKeyManager,
                        IRepositoryManager repositoryManager,
                        IProjectManager projectManager,
                        IFederationManager federationManager) {
@@ -136,6 +168,7 @@ public class DaggerModule {
                                notificationManager,
                                userManager,
                                authenticationManager,
+                               publicKeyManager,
                                repositoryManager,
                                projectManager,
                                federationManager);
@@ -146,6 +179,7 @@ public class DaggerModule {
                        INotificationManager notificationManager,
                        IUserManager userManager,
                        IAuthenticationManager authenticationManager,
+                       IPublicKeyManager publicKeyManager,
                        IRepositoryManager repositoryManager,
                        IProjectManager projectManager,
                        IFederationManager federationManager,
@@ -156,6 +190,7 @@ public class DaggerModule {
                                notificationManager,
                                userManager,
                                authenticationManager,
+                               publicKeyManager,
                                repositoryManager,
                                projectManager,
                                federationManager,
index 792a63822c013e2b259e29656992332ef8412264..d20025f09a9104527aecee884f3a669dd0ed4e15 100644 (file)
@@ -97,7 +97,7 @@ public class FederationClient {
                UserManager users = new UserManager(runtime).start();\r
                RepositoryManager repositories = new RepositoryManager(runtime, users).start();\r
                FederationManager federation = new FederationManager(runtime, notifications, repositories).start();\r
-               IGitblit gitblit = new GitblitManager(runtime, notifications, users, null, repositories, null, federation);\r
+               IGitblit gitblit = new GitblitManager(runtime, notifications, users, null, null, repositories, null, federation);\r
 \r
                FederationPullService puller = new FederationPullService(gitblit, federation.getFederationRegistrations()) {\r
                        @Override\r
index 817d18cbeb48e8b0b44b1a006c7d4b59b2caf1a9..b223d03cc59c354c692b5190516d5d6a5c4db3bd 100644 (file)
@@ -41,6 +41,7 @@ import com.gitblit.tickets.FileTicketService;
 import com.gitblit.tickets.ITicketService;
 import com.gitblit.tickets.NullTicketService;
 import com.gitblit.tickets.RedisTicketService;
+import com.gitblit.transport.ssh.IPublicKeyManager;
 import com.gitblit.utils.StringUtils;
 
 import dagger.Module;
@@ -67,6 +68,7 @@ public class GitBlit extends GitblitManager {
                        INotificationManager notificationManager,
                        IUserManager userManager,
                        IAuthenticationManager authenticationManager,
+                       IPublicKeyManager publicKeyManager,
                        IRepositoryManager repositoryManager,
                        IProjectManager projectManager,
                        IFederationManager federationManager) {
@@ -75,6 +77,7 @@ public class GitBlit extends GitblitManager {
                                notificationManager,
                                userManager,
                                authenticationManager,
+                               publicKeyManager,
                                repositoryManager,
                                projectManager,
                                federationManager);
@@ -262,7 +265,7 @@ public class GitBlit extends GitblitManager {
                                        FileTicketService.class,
                                        BranchTicketService.class,
                                        RedisTicketService.class
-                       }
+                               }
                        )
        class GitBlitModule {
 
index 97e8efc9eef05efc4ea95ab5ec180e8fa3719abe..8856715ada4f040ff7d41c0d2871d586fee36d80 100644 (file)
@@ -69,6 +69,7 @@ import com.gitblit.models.SettingModel;
 import com.gitblit.models.TeamModel;
 import com.gitblit.models.UserModel;
 import com.gitblit.tickets.ITicketService;
+import com.gitblit.transport.ssh.IPublicKeyManager;
 import com.gitblit.utils.ArrayUtils;
 import com.gitblit.utils.HttpUtils;
 import com.gitblit.utils.JsonUtils;
@@ -107,6 +108,8 @@ public class GitblitManager implements IGitblit {
 
        protected final IAuthenticationManager authenticationManager;
 
+       protected final IPublicKeyManager publicKeyManager;
+
        protected final IRepositoryManager repositoryManager;
 
        protected final IProjectManager projectManager;
@@ -118,6 +121,7 @@ public class GitblitManager implements IGitblit {
                        INotificationManager notificationManager,
                        IUserManager userManager,
                        IAuthenticationManager authenticationManager,
+                       IPublicKeyManager publicKeyManager,
                        IRepositoryManager repositoryManager,
                        IProjectManager projectManager,
                        IFederationManager federationManager) {
@@ -127,6 +131,7 @@ public class GitblitManager implements IGitblit {
                this.notificationManager = notificationManager;
                this.userManager = userManager;
                this.authenticationManager = authenticationManager;
+               this.publicKeyManager = publicKeyManager;
                this.repositoryManager = repositoryManager;
                this.projectManager = projectManager;
                this.federationManager = federationManager;
@@ -524,6 +529,11 @@ public class GitblitManager implements IGitblit {
                throw new RuntimeException("This class does not have a ticket service!");
        }
 
+       @Override
+       public IPublicKeyManager getPublicKeyManager() {
+               return publicKeyManager;
+       }
+
        /*
         * ISTOREDSETTINGS
         *
index 50210e9d0ce7a8bb842ea8824a836d517f6f2226..f4221cf9b51685a4d880c9460ccc9045bf9780c0 100644 (file)
@@ -27,6 +27,7 @@ import com.gitblit.models.RepositoryUrl;
 import com.gitblit.models.TeamModel;
 import com.gitblit.models.UserModel;
 import com.gitblit.tickets.ITicketService;
+import com.gitblit.transport.ssh.IPublicKeyManager;
 
 public interface IGitblit extends IManager,
                                                                        IRuntimeManager,
@@ -109,4 +110,11 @@ public interface IGitblit extends IManager,
         */
        ITicketService getTicketService();
 
+       /**
+        * Returns the SSH public key manager.
+        *
+        * @return the SSH public key manager
+        */
+       IPublicKeyManager getPublicKeyManager();
+
 }
\ No newline at end of file
index d4ec9671e9ade1e58531849db060ea408293c617..cf8bba0123965c38be901216e5515c2a996f7c71 100644 (file)
@@ -47,6 +47,7 @@ import com.gitblit.manager.IProjectManager;
 import com.gitblit.manager.IRepositoryManager;
 import com.gitblit.manager.IRuntimeManager;
 import com.gitblit.manager.IUserManager;
+import com.gitblit.transport.ssh.IPublicKeyManager;
 import com.gitblit.utils.ContainerUtils;
 import com.gitblit.utils.StringUtils;
 
@@ -149,7 +150,7 @@ public class GitblitContext extends DaggerContext {
                        String contextRealPath = context.getRealPath("/");
                        File contextFolder = (contextRealPath != null) ? new File(contextRealPath) : null;
 
-                       // if the base folder dosen't match the default assume they don't want to use express, 
+                       // if the base folder dosen't match the default assume they don't want to use express,
                        // this allows for other containers to customise the basefolder per context.
                        String defaultBase = Constants.contextFolder$ + "/WEB-INF/data";
                        String base = lookupBaseFolderFromJndi();
@@ -178,6 +179,7 @@ public class GitblitContext extends DaggerContext {
                startManager(injector, INotificationManager.class);
                startManager(injector, IUserManager.class);
                startManager(injector, IAuthenticationManager.class);
+               startManager(injector, IPublicKeyManager.class);
                startManager(injector, IRepositoryManager.class);
                startManager(injector, IProjectManager.class);
                startManager(injector, IFederationManager.class);
index 7d6066c794c97f89df5199312d2a6c43aee97a3d..0120fa65bd5cacee44f38968667a8640682e847e 100644 (file)
@@ -34,23 +34,23 @@ import com.gitblit.models.UserModel;
 import com.google.common.base.Preconditions;
 
 /**
- * 
+ *
  * @author Eric Myrhe
- * 
+ *
  */
 public class CachingPublicKeyAuthenticator implements PublickeyAuthenticator,
                SessionListener {
 
        protected final Logger log = LoggerFactory.getLogger(getClass());
 
-       protected final IKeyManager keyManager;
+       protected final IPublicKeyManager keyManager;
 
        protected final IAuthenticationManager authManager;
 
        private final Map<ServerSession, Map<PublicKey, Boolean>> cache =
                        new ConcurrentHashMap<ServerSession, Map<PublicKey, Boolean>>();
 
-       public CachingPublicKeyAuthenticator(IKeyManager keyManager,
+       public CachingPublicKeyAuthenticator(IPublicKeyManager keyManager,
                        IAuthenticationManager authManager) {
                this.keyManager = keyManager;
                this.authManager = authManager;
@@ -101,16 +101,15 @@ public class CachingPublicKeyAuthenticator implements PublickeyAuthenticator,
                return false;
        }
 
-       public IKeyManager getKeyManager() {
-               return keyManager;
-       }
-
+       @Override
        public void sessionCreated(Session session) {
        }
 
+       @Override
        public void sessionEvent(Session sesssion, Event event) {
        }
 
+       @Override
        public void sessionClosed(Session session) {
                cache.remove(session);
        }
index ae0bc9cfc728fb77bc1d11954ab5f894b3133032..defb4a3e6041341681c27d3516d652ecfe9cd9db 100644 (file)
@@ -35,12 +35,12 @@ import com.google.common.base.Joiner;
 import com.google.common.io.Files;
 
 /**
- * Manages SSH keys on the filesystem.
+ * Manages public keys on the filesystem.
  *
  * @author James Moger
  *
  */
-public class FileKeyManager extends IKeyManager {
+public class FileKeyManager extends IPublicKeyManager {
 
        protected final IRuntimeManager runtimeManager;
 
@@ -59,6 +59,7 @@ public class FileKeyManager extends IKeyManager {
 
        @Override
        public FileKeyManager start() {
+               log.info(toString());
                return this;
        }
 
diff --git a/src/main/java/com/gitblit/transport/ssh/IKeyManager.java b/src/main/java/com/gitblit/transport/ssh/IKeyManager.java
deleted file mode 100644 (file)
index 12fce3d..0000000
+++ /dev/null
@@ -1,78 +0,0 @@
-/*
- * Copyright 2014 gitblit.com.
- *
- * Licensed 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 com.gitblit.transport.ssh;
-
-import java.security.PublicKey;
-import java.text.MessageFormat;
-import java.util.List;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.TimeUnit;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.google.common.cache.CacheBuilder;
-import com.google.common.cache.CacheLoader;
-import com.google.common.cache.LoadingCache;
-
-/**
- *
- * @author James Moger
- *
- */
-public abstract class IKeyManager {
-
-       protected final Logger log = LoggerFactory.getLogger(getClass());
-
-       protected final LoadingCache<String, List<PublicKey>> keyCache = CacheBuilder
-                       .newBuilder().
-                       expireAfterAccess(15, TimeUnit.MINUTES).
-                       maximumSize(100)
-                       .build(new CacheLoader<String, List<PublicKey>>() {
-                               @Override
-                               public List<PublicKey> load(String username) {
-                                       return getKeysImpl(username);
-                               }
-                       });
-
-       public abstract IKeyManager start();
-
-       public abstract boolean isReady();
-
-       public abstract IKeyManager stop();
-
-       public final List<PublicKey> getKeys(String username) {
-               try {
-                       if (isStale(username)) {
-                               keyCache.invalidate(username);
-                       }
-                       return keyCache.get(username);
-               } catch (ExecutionException e) {
-                       log.error(MessageFormat.format("failed to retrieve keys for {0}", username), e);
-               }
-               return null;
-       }
-
-       protected abstract boolean isStale(String username);
-
-       protected abstract List<PublicKey> getKeysImpl(String username);
-
-       public abstract boolean addKey(String username, String data);
-
-       public abstract boolean removeKey(String username, String data);
-
-       public abstract boolean removeAllKeys(String username);
-}
diff --git a/src/main/java/com/gitblit/transport/ssh/IPublicKeyManager.java b/src/main/java/com/gitblit/transport/ssh/IPublicKeyManager.java
new file mode 100644 (file)
index 0000000..5857a59
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2014 gitblit.com.
+ *
+ * Licensed 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 com.gitblit.transport.ssh;
+
+import java.security.PublicKey;
+import java.text.MessageFormat;
+import java.util.List;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.gitblit.manager.IManager;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
+
+/**
+ * Parent class for public key managers.
+ *
+ * @author James Moger
+ *
+ */
+public abstract class IPublicKeyManager implements IManager {
+
+       protected final Logger log = LoggerFactory.getLogger(getClass());
+
+       protected final LoadingCache<String, List<PublicKey>> keyCache = CacheBuilder
+                       .newBuilder().
+                       expireAfterAccess(15, TimeUnit.MINUTES).
+                       maximumSize(100)
+                       .build(new CacheLoader<String, List<PublicKey>>() {
+                               @Override
+                               public List<PublicKey> load(String username) {
+                                       return getKeysImpl(username);
+                               }
+                       });
+
+       @Override
+       public abstract IPublicKeyManager start();
+
+       public abstract boolean isReady();
+
+       @Override
+       public abstract IPublicKeyManager stop();
+
+       public final List<PublicKey> getKeys(String username) {
+               try {
+                       if (isStale(username)) {
+                               keyCache.invalidate(username);
+                       }
+                       return keyCache.get(username);
+               } catch (ExecutionException e) {
+                       log.error(MessageFormat.format("failed to retrieve keys for {0}", username), e);
+               }
+               return null;
+       }
+
+       protected abstract boolean isStale(String username);
+
+       protected abstract List<PublicKey> getKeysImpl(String username);
+
+       public abstract boolean addKey(String username, String data);
+
+       public abstract boolean removeKey(String username, String data);
+
+       public abstract boolean removeAllKeys(String username);
+}
diff --git a/src/main/java/com/gitblit/transport/ssh/MemoryKeyManager.java b/src/main/java/com/gitblit/transport/ssh/MemoryKeyManager.java
new file mode 100644 (file)
index 0000000..26bd021
--- /dev/null
@@ -0,0 +1,98 @@
+/*
+ * Copyright 2014 gitblit.com.
+ *
+ * Licensed 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 com.gitblit.transport.ssh;
+
+import java.security.PublicKey;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Memory public key manager.
+ *
+ * @author James Moger
+ *
+ */
+public class MemoryKeyManager extends IPublicKeyManager {
+
+       Map<String, List<PublicKey>> keys;
+
+       public MemoryKeyManager() {
+               keys = new HashMap<String, List<PublicKey>>();
+       }
+
+       @Override
+       public String toString() {
+               return getClass().getSimpleName();
+       }
+
+       @Override
+       public MemoryKeyManager start() {
+               log.info(toString());
+               return this;
+       }
+
+       @Override
+       public boolean isReady() {
+               return true;
+       }
+
+       @Override
+       public MemoryKeyManager stop() {
+               return this;
+       }
+
+       @Override
+       protected boolean isStale(String username) {
+               return false;
+       }
+
+       @Override
+       protected List<PublicKey> getKeysImpl(String username) {
+               String id = username.toLowerCase();
+               if (keys.containsKey(id)) {
+                       return keys.get(id);
+               }
+               return null;
+       }
+
+       @Override
+       public boolean addKey(String username, String data) {
+               return false;
+       }
+
+       @Override
+       public boolean removeKey(String username, String data) {
+               return false;
+       }
+
+       @Override
+       public boolean removeAllKeys(String username) {
+               String id = username.toLowerCase();
+               keys.remove(id.toLowerCase());
+               return true;
+       }
+
+       /* Test method for populating the memory key manager */
+       public void addKey(String username, PublicKey key) {
+               String id = username.toLowerCase();
+               if (!keys.containsKey(id)) {
+                       keys.put(id, new ArrayList<PublicKey>());
+               }
+               keys.get(id).add(key);
+       }
+}
index c76728d890f95a7e662aab81f49a3504f62ecce8..25860d6cfbabcb7a7ef71563de4663a2af473c10 100644 (file)
@@ -19,12 +19,12 @@ import java.security.PublicKey;
 import java.util.List;
 
 /**
- * Rejects all SSH key management requests.
+ * Rejects all public key management requests.
  *
  * @author James Moger
  *
  */
-public class NullKeyManager extends IKeyManager {
+public class NullKeyManager extends IPublicKeyManager {
 
        public NullKeyManager() {
        }
@@ -36,6 +36,7 @@ public class NullKeyManager extends IKeyManager {
 
        @Override
        public NullKeyManager start() {
+               log.info(toString());
                return this;
        }
 
index de7aad1f9b3aa6e7780e5c88a23314e8a3ab09bd..2b2093ead6c31d7653a4b5d1de52923c5aaeebef 100644 (file)
@@ -52,14 +52,10 @@ public class SshCommandFactory implements CommandFactory {
        private static final Logger logger = LoggerFactory.getLogger(SshCommandFactory.class);
 
        private final IGitblit gitblit;
-       private final CachingPublicKeyAuthenticator keyAuthenticator;
        private final ScheduledExecutorService startExecutor;
 
-       public SshCommandFactory(IGitblit gitblit,
-                       CachingPublicKeyAuthenticator keyAuthenticator,
-                       IdGenerator idGenerator) {
+       public SshCommandFactory(IGitblit gitblit, IdGenerator idGenerator) {
                this.gitblit = gitblit;
-               this.keyAuthenticator = keyAuthenticator;
 
                int threads = 2;// cfg.getInt("sshd","commandStartThreads", 2);
                WorkQueue workQueue = new WorkQueue(idGenerator);
@@ -84,8 +80,6 @@ public class SshCommandFactory implements CommandFactory {
                root.registerDispatcher(user, GitblitDispatchCommand.class);
                root.registerDispatcher(user, GitDispatchCommand.class);
 
-               root.setAuthenticator(keyAuthenticator);
-
                return root;
        }
 
index b6c5d680048952c1e4da27f4cc46876f48874fb8..da9a37266c8da6e6bd11a3a0c2523f48c28aa0d4 100644 (file)
@@ -21,8 +21,6 @@ import java.net.InetSocketAddress;
 import java.text.MessageFormat;
 import java.util.concurrent.atomic.AtomicBoolean;
 
-import javax.inject.Singleton;
-
 import org.apache.sshd.SshServer;
 import org.apache.sshd.common.io.IoServiceFactoryFactory;
 import org.apache.sshd.common.io.mina.MinaServiceFactoryFactory;
@@ -35,15 +33,10 @@ import org.slf4j.LoggerFactory;
 import com.gitblit.Constants;
 import com.gitblit.IStoredSettings;
 import com.gitblit.Keys;
-import com.gitblit.manager.IAuthenticationManager;
 import com.gitblit.manager.IGitblit;
 import com.gitblit.utils.IdGenerator;
 import com.gitblit.utils.StringUtils;
 
-import dagger.Module;
-import dagger.ObjectGraph;
-import dagger.Provides;
-
 /**
  * Manager for the ssh transport. Roughly analogous to the
  * {@link com.gitblit.transport.git.GitDaemon} class.
@@ -73,7 +66,6 @@ public class SshDaemon {
 
        private final IGitblit gitblit;
        private final SshServer sshd;
-       private final ObjectGraph injector;
 
        /**
         * Construct the Gitblit SSH daemon.
@@ -82,15 +74,12 @@ public class SshDaemon {
         */
        public SshDaemon(IGitblit gitblit, IdGenerator idGenerator) {
                this.gitblit = gitblit;
-               this.injector = ObjectGraph.create(new SshModule());
 
                IStoredSettings settings = gitblit.getSettings();
                int port = settings.getInteger(Keys.git.sshPort, 0);
                String bindInterface = settings.getString(Keys.git.sshBindInterface,
                                "localhost");
 
-               IKeyManager keyManager = getKeyManager();
-
                String sshBackendStr = settings.getString(Keys.git.sshBackend,
                                SshSessionBackend.NIO2.name());
                SshSessionBackend backend = SshSessionBackend.valueOf(sshBackendStr);
@@ -108,7 +97,7 @@ public class SshDaemon {
 
                File hostKeyStore = new File(gitblit.getBaseFolder(), HOST_KEY_STORE);
                CachingPublicKeyAuthenticator keyAuthenticator =
-                               getPublicKeyAuthenticator(keyManager, gitblit);
+                               new CachingPublicKeyAuthenticator(gitblit.getPublicKeyManager(), gitblit);
 
                sshd = SshServer.setUpDefaultServer();
                sshd.setPort(addr.getPort());
@@ -119,7 +108,7 @@ public class SshDaemon {
                sshd.setSessionFactory(new SshServerSessionFactory());
                sshd.setFileSystemFactory(new DisabledFilesystemFactory());
                sshd.setTcpipForwardingFilter(new NonForwardingFilter());
-               sshd.setCommandFactory(new SshCommandFactory(gitblit, keyAuthenticator, idGenerator));
+               sshd.setCommandFactory(new SshCommandFactory(gitblit, idGenerator));
                sshd.setShellFactory(new WelcomeShell(settings));
 
                String version = Constants.getGitBlitVersion() + " (" + sshd.getVersion() + ")";
@@ -128,27 +117,6 @@ public class SshDaemon {
                run = new AtomicBoolean(false);
        }
 
-       private CachingPublicKeyAuthenticator getPublicKeyAuthenticator(
-                       IKeyManager keyManager, IGitblit gitblit) {
-               IStoredSettings settings = gitblit.getSettings();
-               String clazz = settings.getString(Keys.git.sshPublicKeyAuthenticator,
-                               CachingPublicKeyAuthenticator.class.getName());
-               if (StringUtils.isEmpty(clazz)) {
-                       clazz = CachingPublicKeyAuthenticator.class.getName();
-               }
-               try {
-                       Class<CachingPublicKeyAuthenticator> authClass =
-                                       (Class<CachingPublicKeyAuthenticator>) Class.forName(clazz);
-                       return authClass.getConstructor(
-                                       new Class[] { IKeyManager.class,
-                                                       IAuthenticationManager.class }).newInstance(
-                                       keyManager, gitblit);
-               } catch (Exception e) {
-                       log.error("failed to create ssh auth manager " + clazz, e);
-               }
-               return null;
-       }
-
        public String formatUrl(String gituser, String servername, String repository) {
                if (sshd.getPort() == DEFAULT_PORT) {
                        // standard port
@@ -203,75 +171,4 @@ public class SshDaemon {
                        }
                }
        }
-
-       @SuppressWarnings("unchecked")
-       protected IKeyManager getKeyManager() {
-               IKeyManager keyManager = null;
-               IStoredSettings settings = gitblit.getSettings();
-               String clazz = settings.getString(Keys.git.sshKeysManager, FileKeyManager.class.getName());
-               if (StringUtils.isEmpty(clazz)) {
-                       clazz = FileKeyManager.class.getName();
-               }
-               try {
-                       Class<? extends IKeyManager> managerClass = (Class<? extends IKeyManager>) Class.forName(clazz);
-                       keyManager = injector.get(managerClass).start();
-                       if (keyManager.isReady()) {
-                               log.info("{} is ready.", keyManager);
-                       } else {
-                               log.warn("{} is disabled.", keyManager);
-                       }
-               } catch (Exception e) {
-                       log.error("failed to create ssh key manager " + clazz, e);
-                       keyManager = injector.get(NullKeyManager.class).start();
-               }
-               return keyManager;
-       }
-
-       @SuppressWarnings("unchecked")
-       protected IKeyManager getKeyAuthenticator() {
-               IKeyManager keyManager = null;
-               IStoredSettings settings = gitblit.getSettings();
-               String clazz = settings.getString(Keys.git.sshKeysManager, FileKeyManager.class.getName());
-               if (StringUtils.isEmpty(clazz)) {
-                       clazz = FileKeyManager.class.getName();
-               }
-               try {
-                       Class<? extends IKeyManager> managerClass = (Class<? extends IKeyManager>) Class.forName(clazz);
-                       keyManager = injector.get(managerClass).start();
-                       if (keyManager.isReady()) {
-                               log.info("{} is ready.", keyManager);
-                       } else {
-                               log.warn("{} is disabled.", keyManager);
-                       }
-               } catch (Exception e) {
-                       log.error("failed to create ssh key manager " + clazz, e);
-                       keyManager = injector.get(NullKeyManager.class).start();
-               }
-               return keyManager;
-       }
-
-       /**
-        * A nested Dagger graph is used for constructor dependency injection of
-        * complex classes.
-        *
-        * @author James Moger
-        *
-        */
-       @Module(
-                       library = true,
-                       injects = {
-                                       NullKeyManager.class,
-                                       FileKeyManager.class
-                       }
-                       )
-       class SshModule {
-
-               @Provides @Singleton NullKeyManager provideNullKeyManager() {
-                       return new NullKeyManager();
-               }
-
-               @Provides @Singleton FileKeyManager provideFileKeyManager() {
-                       return new FileKeyManager(SshDaemon.this.gitblit);
-               }
-       }
 }
index 38f1a48f44e2b55f1d9183fdcb20324d227b8c8a..dd581f4dc46d1aafe130459ebeb8cae54117daa4 100644 (file)
@@ -32,8 +32,6 @@ import org.slf4j.LoggerFactory;
 
 import com.gitblit.models.UserModel;
 import com.gitblit.transport.ssh.CommandMetaData;
-import com.gitblit.transport.ssh.CachingPublicKeyAuthenticator;
-import com.gitblit.transport.ssh.gitblit.BaseKeyCommand;
 import com.gitblit.utils.StringUtils;
 import com.gitblit.utils.cli.SubcommandHandler;
 import com.google.common.base.Charsets;
@@ -252,16 +250,5 @@ public abstract class DispatchCommand extends BaseCommand {
                cmd.setOutputStream(out);
                cmd.setErrorStream(err);
                cmd.setExitCallback(exit);
-
-               if (cmd instanceof BaseKeyCommand) {
-                       BaseKeyCommand k = (BaseKeyCommand) cmd;
-                       k.setAuthenticator(authenticator);
-               }
-       }
-
-       private CachingPublicKeyAuthenticator authenticator;
-
-       public void setAuthenticator(CachingPublicKeyAuthenticator authenticator) {
-               this.authenticator = authenticator;
        }
 }
index 090995786533e0dc73f03683efb84f16d7e576c9..1b7bac11f0c4f95465c6f251e7ad69cd59790a1e 100644 (file)
@@ -21,8 +21,7 @@ import java.io.InputStreamReader;
 import java.io.UnsupportedEncodingException;
 import java.util.List;
 
-import com.gitblit.transport.ssh.IKeyManager;
-import com.gitblit.transport.ssh.CachingPublicKeyAuthenticator;
+import com.gitblit.transport.ssh.IPublicKeyManager;
 import com.gitblit.transport.ssh.commands.SshCommand;
 import com.google.common.base.Charsets;
 
@@ -53,12 +52,7 @@ abstract class BaseKeyCommand extends SshCommand {
                return sshKeys;
        }
 
-       protected CachingPublicKeyAuthenticator authenticator;
-       public void setAuthenticator(CachingPublicKeyAuthenticator authenticator) {
-               this.authenticator = authenticator;
-       }
-       
-       protected IKeyManager getKeyManager() {
-               return authenticator.getKeyManager();
+       protected IPublicKeyManager getKeyManager() {
+               return ctx.getGitblit().getPublicKeyManager();
        }
 }
index 445335ffec1e9ef0267560c7bbbd8010779aebf9..6e8aa05fd2e70056be609f86e92306b2af03779c 100644 (file)
@@ -39,6 +39,7 @@ import com.gitblit.manager.IRepositoryManager;
 import com.gitblit.manager.IRuntimeManager;
 import com.gitblit.manager.IUserManager;
 import com.gitblit.tickets.ITicketService;
+import com.gitblit.transport.ssh.IPublicKeyManager;
 import com.gitblit.utils.StringUtils;
 import com.gitblit.wicket.pages.ActivityPage;
 import com.gitblit.wicket.pages.BlamePage;
@@ -95,6 +96,8 @@ public class GitBlitWebApp extends WebApplication {
 
        private final IAuthenticationManager authenticationManager;
 
+       private final IPublicKeyManager publicKeyManager;
+
        private final IRepositoryManager repositoryManager;
 
        private final IProjectManager projectManager;
@@ -108,6 +111,7 @@ public class GitBlitWebApp extends WebApplication {
                        INotificationManager notificationManager,
                        IUserManager userManager,
                        IAuthenticationManager authenticationManager,
+                       IPublicKeyManager publicKeyManager,
                        IRepositoryManager repositoryManager,
                        IProjectManager projectManager,
                        IFederationManager federationManager,
@@ -119,6 +123,7 @@ public class GitBlitWebApp extends WebApplication {
                this.notificationManager = notificationManager;
                this.userManager = userManager;
                this.authenticationManager = authenticationManager;
+               this.publicKeyManager = publicKeyManager;
                this.repositoryManager = repositoryManager;
                this.projectManager = projectManager;
                this.federationManager = federationManager;
@@ -280,6 +285,10 @@ public class GitBlitWebApp extends WebApplication {
                return authenticationManager;
        }
 
+       public IPublicKeyManager keys() {
+               return publicKeyManager;
+       }
+
        public IRepositoryManager repositories() {
                return repositoryManager;
        }
index 7d8e9a79a81d44c7df7a6f93daaa234aa66e3616..1a52eaf48dbc7fa59f965654bd65893feb6a3365 100644 (file)
@@ -8,7 +8,7 @@ git.searchRepositoriesSubfolders = true
 git.enableGitServlet = true
 git.daemonPort = 8300
 git.sshPort = 29418
-git.sshPublicKeyAuthenticator = com.gitblit.tests.BogusPublicKeyAuthenticator
+git.sshKeysManager = com.gitblit.transport.ssh.MemoryKeyManager
 groovy.scriptsFolder = src/main/distrib/data/groovy
 groovy.preReceiveScripts = blockpush
 groovy.postReceiveScripts = sendmail
diff --git a/src/test/java/com/gitblit/tests/BogusPublicKeyAuthenticator.java b/src/test/java/com/gitblit/tests/BogusPublicKeyAuthenticator.java
deleted file mode 100644 (file)
index 80be1a0..0000000
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- * Copyright 2014 gitblit.com.
- *
- * Licensed 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 com.gitblit.tests;
-
-import java.security.PublicKey;
-
-import org.apache.sshd.server.session.ServerSession;
-
-import com.gitblit.manager.IAuthenticationManager;
-import com.gitblit.transport.ssh.CachingPublicKeyAuthenticator;
-import com.gitblit.transport.ssh.IKeyManager;
-
-public class BogusPublicKeyAuthenticator extends CachingPublicKeyAuthenticator {
-
-       public BogusPublicKeyAuthenticator(IKeyManager keyManager,
-                       IAuthenticationManager authManager) {
-               super(keyManager, authManager);
-       }
-
-       @Override
-       protected boolean doAuthenticate(String username, PublicKey suppliedKey,
-                       ServerSession session) {
-               // TODO(davido): put authenticated user in session
-               return true;
-       }
-}
index 17d609e75aceafa5528cf52841c916cd382446e9..b8d3b181979a20fe3f56321f46957a123fc1a0d6 100644 (file)
@@ -61,7 +61,7 @@ import com.gitblit.utils.JGitUtils;
                MarkdownUtilsTest.class, JGitUtilsTest.class, SyndicationUtilsTest.class,\r
                DiffUtilsTest.class, MetricUtilsTest.class, X509UtilsTest.class,\r
                GitBlitTest.class, FederationTests.class, RpcTests.class, GitServletTest.class, GitDaemonTest.class,\r
-               GroovyScriptTest.class, LuceneExecutorTest.class, RepositoryModelTest.class, SshDaemonTest.class,\r
+               SshDaemonTest.class, GroovyScriptTest.class, LuceneExecutorTest.class, RepositoryModelTest.class,\r
                FanoutServiceTest.class, Issue0259Test.class, Issue0271Test.class, HtpasswdAuthenticationTest.class,\r
                ModelUtilsTest.class, JnaUtilsTest.class, LdapSyncServiceTest.class, FileTicketServiceTest.class,
                BranchTicketServiceTest.class, RedisTicketServiceTest.class, AuthenticationManagerTest.class })
@@ -78,20 +78,12 @@ public class GitBlitSuite {
        static int port = 8280;\r
        static int gitPort = 8300;\r
        static int shutdownPort = 8281;\r
-       static int sshPort = 29418;\r
-\r
-// Overriding of keys doesn't seem to work\r
-//     static {\r
-//             try {\r
-//                     sshPort = SshUtils.getFreePort();\r
-//             } catch (Exception e) {\r
-//                     e.printStackTrace();\r
-//             }\r
-//     }\r
+       static int sshPort = 39418;\r
 \r
        public static String url = "http://localhost:" + port;\r
        public static String gitServletUrl = "http://localhost:" + port + "/git";\r
        public static String gitDaemonUrl = "git://localhost:" + gitPort;\r
+       public static String sshDaemonUrl = "ssh://admin@localhost:" + sshPort;\r
        public static String account = "admin";\r
        public static String password = "admin";\r
 \r
@@ -149,9 +141,7 @@ public class GitBlitSuite {
                                                "" + shutdownPort, "--gitPort", "" + gitPort, "--repositoriesFolder",\r
                                                "\"" + GitBlitSuite.REPOSITORIES.getAbsolutePath() + "\"", "--userService",\r
                                                GitBlitSuite.USERSCONF.getAbsolutePath(), "--settings", GitBlitSuite.SETTINGS.getAbsolutePath(),\r
-                                               "--baseFolder", "data");\r
-                               // doesn't work\r
-                               //, "--sshPort", "" + sshPort);\r
+                                               "--baseFolder", "data", "--sshPort", "" + sshPort);\r
                        }\r
                });\r
 \r
index 5294f691cccd1e96257400e8149787d818494abd..45d31c29db16522d8a8c586f4132ddf54c5b909d 100644 (file)
@@ -26,11 +26,15 @@ import org.apache.sshd.ClientChannel;
 import org.apache.sshd.ClientSession;
 import org.apache.sshd.SshClient;
 import org.apache.sshd.common.KeyPairProvider;
+import org.junit.After;
 import org.junit.AfterClass;
+import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
 
 import com.gitblit.Constants;
+import com.gitblit.transport.ssh.IPublicKeyManager;
+import com.gitblit.transport.ssh.MemoryKeyManager;
 
 public class SshDaemonTest extends GitblitUnitTest {
 
@@ -50,6 +54,27 @@ public class SshDaemonTest extends GitblitUnitTest {
                }
        }
 
+       protected MemoryKeyManager getKeyManager() {
+               IPublicKeyManager mgr = gitblit().getPublicKeyManager();
+               if (mgr instanceof MemoryKeyManager) {
+                       return (MemoryKeyManager) gitblit().getPublicKeyManager();
+               } else {
+                       throw new RuntimeException("unexpected key manager type " + mgr.getClass().getName());
+               }
+       }
+
+       @Before
+       public void prepare() {
+               MemoryKeyManager keyMgr = getKeyManager();
+               keyMgr.addKey("admin", pair.getPublic());
+       }
+
+       @After
+       public void tearDown() {
+               MemoryKeyManager keyMgr = getKeyManager();
+               keyMgr.removeAllKeys("admin");
+       }
+
        @Test
        public void testPublicKeyAuthentication() throws Exception {
                SshClient client = SshClient.setUpDefaultClient();