]> source.dussan.org Git - gitblit.git/commitdiff
Rename & simplify SshSession->SshDaemonClient
authorJames Moger <james.moger@gitblit.com>
Fri, 14 Mar 2014 16:43:55 +0000 (12:43 -0400)
committerJames Moger <james.moger@gitblit.com>
Thu, 10 Apr 2014 22:58:08 +0000 (18:58 -0400)
14 files changed:
src/main/java/com/gitblit/git/GitblitReceivePackFactory.java
src/main/java/com/gitblit/git/RepositoryResolver.java
src/main/java/com/gitblit/transport/ssh/AbstractGitCommand.java
src/main/java/com/gitblit/transport/ssh/SshCommandFactory.java
src/main/java/com/gitblit/transport/ssh/SshContext.java
src/main/java/com/gitblit/transport/ssh/SshDaemon.java
src/main/java/com/gitblit/transport/ssh/SshDaemonClient.java [new file with mode: 0644]
src/main/java/com/gitblit/transport/ssh/SshKeyAuthenticator.java
src/main/java/com/gitblit/transport/ssh/SshPasswordAuthenticator.java
src/main/java/com/gitblit/transport/ssh/SshSession.java [deleted file]
src/main/java/com/gitblit/transport/ssh/SshSessionFactory.java
src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java
src/main/java/com/gitblit/transport/ssh/commands/Receive.java
src/main/java/com/gitblit/transport/ssh/commands/Upload.java

index b928d85197b08985bca80b25ce5de67c1e410374..41e348baf226707a123a6704df02b202e0793695 100644 (file)
@@ -32,7 +32,7 @@ import com.gitblit.manager.IGitblit;
 import com.gitblit.models.RepositoryModel;
 import com.gitblit.models.UserModel;
 import com.gitblit.transport.git.GitDaemonClient;
-import com.gitblit.transport.ssh.SshSession;
+import com.gitblit.transport.ssh.SshDaemonClient;
 import com.gitblit.utils.HttpUtils;
 import com.gitblit.utils.StringUtils;
 
@@ -90,13 +90,12 @@ public class GitblitReceivePackFactory<X> implements ReceivePackFactory<X> {
 
                        // set timeout from Git daemon
                        timeout = client.getDaemon().getTimeout();
-               } else if (req instanceof SshSession) {
+               } else if (req instanceof SshDaemonClient) {
                        // SSH request is always authenticated
-                       SshSession client = (SshSession) req;
+                       SshDaemonClient client = (SshDaemonClient) req;
                        repositoryName = client.getRepositoryName();
                        origin = client.getRemoteAddress().toString();
-                       String username = client.getRemoteUser();
-                       user = gitblit.getUserModel(username);
+                       user = client.getUser();
                }
 
                boolean allowAnonymousPushes = settings.getBoolean(Keys.git.allowAnonymousPushes, false);
index ad5dcf010c65c103164cc21009f170a87ee27d48..cc13144ec1c5520660fd6cf58ccf6982c43e4b98 100644 (file)
@@ -31,7 +31,7 @@ import com.gitblit.manager.IGitblit;
 import com.gitblit.models.RepositoryModel;
 import com.gitblit.models.UserModel;
 import com.gitblit.transport.git.GitDaemonClient;
-import com.gitblit.transport.ssh.SshSession;
+import com.gitblit.transport.ssh.SshDaemonClient;
 
 /**
  * Resolves repositories and grants export access.
@@ -69,9 +69,9 @@ public class RepositoryResolver<X> extends FileResolver<X> {
                        // git request
                        GitDaemonClient client = (GitDaemonClient) req;
                        client.setRepositoryName(name);
-               } else if (req instanceof SshSession) {
-                       SshSession s = (SshSession)req;
-                       s.setRepositoryName(name);
+               } else if (req instanceof SshDaemonClient) {
+                       SshDaemonClient client = (SshDaemonClient) req;
+                       client.setRepositoryName(name);
                }
                return repo;
        }
@@ -96,17 +96,17 @@ public class RepositoryResolver<X> extends FileResolver<X> {
                        user = UserModel.ANONYMOUS;
                } else if (req instanceof HttpServletRequest) {
                        // http/https request
-                       HttpServletRequest httpRequest = (HttpServletRequest) req;
-                       scheme = httpRequest.getScheme();
-                       origin = httpRequest.getRemoteAddr();
-                       user = gitblit.authenticate(httpRequest);
+                       HttpServletRequest client = (HttpServletRequest) req;
+                       scheme = client.getScheme();
+                       origin = client.getRemoteAddr();
+                       user = gitblit.authenticate(client);
                        if (user == null) {
                                user = UserModel.ANONYMOUS;
                        }
-               } else if (req instanceof SshSession) {
+               } else if (req instanceof SshDaemonClient) {
                        // ssh is always authenticated
-                       SshSession s = (SshSession) req;
-                       user = gitblit.getUserModel(s.getRemoteUser());
+                       SshDaemonClient client = (SshDaemonClient) req;
+                       user = client.getUser();
                }
 
                if (user.canClone(model)) {
index bba6a402d0ac2b7909fb8b70d40340f54eadc229..188cb005c8e60fb2d09becfd57a32b6ac0d4552a 100644 (file)
@@ -36,9 +36,9 @@ public abstract class AbstractGitCommand extends BaseCommand {
        @Argument(index = 0, metaVar = "PROJECT.git", required = true, usage = "project name")
        protected String repository;
 
-       protected RepositoryResolver<SshSession> repositoryResolver;
-       protected ReceivePackFactory<SshSession> receivePackFactory;
-       protected UploadPackFactory<SshSession> uploadPackFactory;
+       protected RepositoryResolver<SshDaemonClient> repositoryResolver;
+       protected ReceivePackFactory<SshDaemonClient> receivePackFactory;
+       protected UploadPackFactory<SshDaemonClient> uploadPackFactory;
 
        protected Repository repo;
 
@@ -84,7 +84,7 @@ public abstract class AbstractGitCommand extends BaseCommand {
                }
                repository = repository.substring(1);
                try {
-                       return repositoryResolver.open(ctx.getSession(), repository);
+                       return repositoryResolver.open(ctx.getClient(), repository);
                } catch (Exception e) {
                        throw new Failure(1, "fatal: '" + repository
                                        + "': not a git archive", e);
@@ -92,17 +92,17 @@ public abstract class AbstractGitCommand extends BaseCommand {
        }
 
        public void setRepositoryResolver(
-                       RepositoryResolver<SshSession> repositoryResolver) {
+                       RepositoryResolver<SshDaemonClient> repositoryResolver) {
                this.repositoryResolver = repositoryResolver;
        }
 
        public void setReceivePackFactory(
-                       GitblitReceivePackFactory<SshSession> receivePackFactory) {
+                       GitblitReceivePackFactory<SshDaemonClient> receivePackFactory) {
                this.receivePackFactory = receivePackFactory;
        }
 
        public void setUploadPackFactory(
-                       GitblitUploadPackFactory<SshSession> uploadPackFactory) {
+                       GitblitUploadPackFactory<SshDaemonClient> uploadPackFactory) {
                this.uploadPackFactory = uploadPackFactory;
        }
 }
\ No newline at end of file
index 0c8492f7f8616700c41c1bba93c29cd068821386..a52e62b89a8f223d1c81b77b654a70cc5d0ed394 100644 (file)
@@ -133,7 +133,7 @@ public class SshCommandFactory implements CommandFactory {
 
            private void onStart() throws IOException {
              synchronized (this) {
-                   SshContext ctx = new SshContext(session.getAttribute(SshSession.KEY), cmdLine);
+                   SshContext ctx = new SshContext(session.getAttribute(SshDaemonClient.KEY), cmdLine);
                try {
                  cmd = dispatcher;
                  cmd.setArguments(argv);
index b137cb87c5dabe255e179928a80a484df82e4bb8..4c5786e479ab5bb9321588eadb5ba6867550bf1d 100644 (file)
@@ -17,16 +17,16 @@ package com.gitblit.transport.ssh;
 
 public class SshContext {
 
-       private final SshSession session;
+       private final SshDaemonClient client;
        private final String commandLine;
 
-       public SshContext(SshSession session, String commandLine) {
-               this.session = session;
+       public SshContext(SshDaemonClient client, String commandLine) {
+               this.client = client;
                this.commandLine = commandLine;
        }
 
-       public SshSession getSession() {
-               return session;
+       public SshDaemonClient getClient() {
+               return client;
        }
 
        public String getCommandLine() {
index 152b82638f07db4d850d02482565c2c03a7ca58d..81d7878409bb5f0eca582c6560e8a1b49d7358c5 100644 (file)
@@ -142,9 +142,9 @@ public class SshDaemon {
                root.registerDispatcher("gitblit", gitblitCmd);
                root.registerDispatcher("git", gitCmd);
 
-               root.setRepositoryResolver(new RepositoryResolver<SshSession>(gitblit));
-               root.setUploadPackFactory(new GitblitUploadPackFactory<SshSession>(gitblit));
-               root.setReceivePackFactory(new GitblitReceivePackFactory<SshSession>(gitblit));
+               root.setRepositoryResolver(new RepositoryResolver<SshDaemonClient>(gitblit));
+               root.setUploadPackFactory(new GitblitUploadPackFactory<SshDaemonClient>(gitblit));
+               root.setReceivePackFactory(new GitblitReceivePackFactory<SshDaemonClient>(gitblit));
                root.setAuthenticator(publickeyAuthenticator);
 
                SshCommandFactory commandFactory = new SshCommandFactory(
diff --git a/src/main/java/com/gitblit/transport/ssh/SshDaemonClient.java b/src/main/java/com/gitblit/transport/ssh/SshDaemonClient.java
new file mode 100644 (file)
index 0000000..4d8ea4b
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * 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.net.SocketAddress;
+
+import org.apache.sshd.common.Session.AttributeKey;
+
+import com.gitblit.models.UserModel;
+
+/**
+ *
+ * @author Eric Myrhe
+ *
+ */
+public class SshDaemonClient {
+       public static final AttributeKey<SshDaemonClient> KEY = new AttributeKey<SshDaemonClient>();
+
+       private final SocketAddress remoteAddress;
+
+       private volatile UserModel user;
+       private volatile String repositoryName;
+
+       SshDaemonClient(SocketAddress peer) {
+               this.remoteAddress = peer;
+       }
+
+       public SocketAddress getRemoteAddress() {
+               return remoteAddress;
+       }
+
+       public UserModel getUser() {
+               return user;
+       }
+
+       public void setUser(UserModel user) {
+               this.user = user;
+       }
+
+       public String getUsername() {
+               return user == null ? null : user.username;
+       }
+
+       public void setRepositoryName(String repositoryName) {
+               this.repositoryName = repositoryName;
+       }
+
+       public String getRepositoryName() {
+               return repositoryName;
+       }
+}
index 044d264385b8aafc57c0872c2c039c876d52323d..36319226fb97ba43a420a582c85d591c2717ace2 100644 (file)
@@ -43,7 +43,7 @@ public class SshKeyAuthenticator implements PublickeyAuthenticator {
        protected final Logger log = LoggerFactory.getLogger(getClass());
 
        protected final IKeyManager keyManager;
-       
+
        protected final IAuthenticationManager authManager;
 
        LoadingCache<String, List<PublicKey>> sshKeyCache = CacheBuilder
@@ -65,9 +65,9 @@ public class SshKeyAuthenticator implements PublickeyAuthenticator {
        @Override
        public boolean authenticate(String username, final PublicKey suppliedKey,
                        final ServerSession session) {
-               final SshSession client = session.getAttribute(SshSession.KEY);
+               final SshDaemonClient client = session.getAttribute(SshDaemonClient.KEY);
 
-               if (client.getRemoteUser() != null) {
+               if (client.getUser() != null) {
                        // TODO why do we re-authenticate?
                        log.info("{} has already authenticated!", username);
                        return true;
@@ -85,7 +85,7 @@ public class SshKeyAuthenticator implements PublickeyAuthenticator {
                                if (key.equals(suppliedKey)) {
                                        UserModel user = authManager.authenticate(username, key);
                                        if (user != null) {
-                                               client.authenticationSuccess(username);
+                                               client.setUser(user);
                                                return true;
                                        }
                                }
index 3baf985d6ec3d4e90e2311934e11bfb2e01a9973..5ddc4a3983ec6752770e77e09e439190f7ed888d 100644 (file)
@@ -42,8 +42,8 @@ public class SshPasswordAuthenticator implements PasswordAuthenticator {
 
        @Override
        public boolean authenticate(String username, String password, ServerSession session) {
-               SshSession client = session.getAttribute(SshSession.KEY);
-               if (client.getRemoteUser() != null) {
+               SshDaemonClient client = session.getAttribute(SshDaemonClient.KEY);
+               if (client.getUser() != null) {
                        log.info("{} has already authenticated!", username);
                        return true;
                }
@@ -51,7 +51,7 @@ public class SshPasswordAuthenticator implements PasswordAuthenticator {
                username = username.toLowerCase(Locale.US);
                UserModel user = authManager.authenticate(username, password.toCharArray());
                if (user != null) {
-                       client.authenticationSuccess(username);
+                       client.setUser(user);
                        return true;
                }
 
diff --git a/src/main/java/com/gitblit/transport/ssh/SshSession.java b/src/main/java/com/gitblit/transport/ssh/SshSession.java
deleted file mode 100644 (file)
index ffff8af..0000000
+++ /dev/null
@@ -1,111 +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.net.InetAddress;
-import java.net.InetSocketAddress;
-import java.net.SocketAddress;
-
-import org.apache.sshd.common.Session.AttributeKey;
-
-/**
- *
- * @author Eric Myrhe
- *
- */
-public class SshSession {
-  public static final AttributeKey<SshSession> KEY =
-      new AttributeKey<SshSession>();
-
-  private final int sessionId;
-  private final SocketAddress remoteAddress;
-  private final String remoteAsString;
-
-  private volatile String username;
-  private volatile String authError;
-  private volatile String repositoryName;
-
-  SshSession(int sessionId, SocketAddress peer) {
-    this.sessionId = sessionId;
-    this.remoteAddress = peer;
-    this.remoteAsString = format(remoteAddress);
-  }
-
-  public SocketAddress getRemoteAddress() {
-    return remoteAddress;
-  }
-
-  String getRemoteAddressAsString() {
-    return remoteAsString;
-  }
-
-  public String getRemoteUser() {
-    return username;
-  }
-
-  /** Unique session number, assigned during connect. */
-  public int getSessionId() {
-    return sessionId;
-  }
-
-  String getUsername() {
-    return username;
-  }
-
-  String getAuthenticationError() {
-    return authError;
-  }
-
-  void authenticationSuccess(String user) {
-    username = user;
-    authError = null;
-  }
-
-  void authenticationError(String user, String error) {
-    username = user;
-    authError = error;
-  }
-
-  public void setRepositoryName(String repositoryName) {
-       this.repositoryName = repositoryName;
-  }
-
-  public String getRepositoryName() {
-       return repositoryName;
-  }
-
-  /** @return {@code true} if the authentication did not succeed. */
-  boolean isAuthenticationError() {
-    return authError != null;
-  }
-
-  private static String format(final SocketAddress remote) {
-    if (remote instanceof InetSocketAddress) {
-      final InetSocketAddress sa = (InetSocketAddress) remote;
-
-      final InetAddress in = sa.getAddress();
-      if (in != null) {
-        return in.getHostAddress();
-      }
-
-      final String hostName = sa.getHostName();
-      if (hostName != null) {
-        return hostName;
-      }
-    }
-    return remote.toString();
-  }
-}
index ae6da3fb72571329ae25f85494a9f22aad4a0ada..66fe057d58f449d6580118588c69d10a46ccb2f0 100644 (file)
@@ -52,21 +52,21 @@ public class SshSessionFactory extends SessionFactory {
                        }
                }
 
-               final GitblitServerSession s = (GitblitServerSession) super
+               final GitblitServerSession session = (GitblitServerSession) super
                                .createSession(io);
                SocketAddress peer = io.getRemoteAddress();
-               SshSession session = new SshSession(idGenerator.next(), peer);
-               s.setAttribute(SshSession.KEY, session);
+               SshDaemonClient client = new SshDaemonClient(peer);
+               session.setAttribute(SshDaemonClient.KEY, client);
 
                // TODO(davido): Log a session close without authentication as a
                // failure.
-               s.addCloseSessionListener(new SshFutureListener<CloseFuture>() {
+               session.addCloseSessionListener(new SshFutureListener<CloseFuture>() {
                        @Override
                        public void operationComplete(CloseFuture future) {
                                log.info("connection closed on " + io);
                        }
                });
-               return s;
+               return session;
        }
 
        @Override
index 31b718e017e8c56074c147d409af7f61b2a73ee5..dc963309afb6d846c6fcf36331aa62ce714ef246 100644 (file)
@@ -33,7 +33,7 @@ import com.gitblit.git.RepositoryResolver;
 import com.gitblit.transport.ssh.AbstractGitCommand;
 import com.gitblit.transport.ssh.CommandMetaData;
 import com.gitblit.transport.ssh.SshKeyAuthenticator;
-import com.gitblit.transport.ssh.SshSession;
+import com.gitblit.transport.ssh.SshDaemonClient;
 import com.gitblit.utils.cli.SubcommandHandler;
 import com.google.common.base.Charsets;
 import com.google.common.base.Strings;
@@ -204,18 +204,18 @@ public class DispatchCommand extends BaseCommand {
          }
   }
 
-  private RepositoryResolver<SshSession> repositoryResolver;
-  public void setRepositoryResolver(RepositoryResolver<SshSession> repositoryResolver) {
+  private RepositoryResolver<SshDaemonClient> repositoryResolver;
+  public void setRepositoryResolver(RepositoryResolver<SshDaemonClient> repositoryResolver) {
          this.repositoryResolver = repositoryResolver;
   }
 
-  private GitblitUploadPackFactory<SshSession> gitblitUploadPackFactory;
-  public void setUploadPackFactory(GitblitUploadPackFactory<SshSession> gitblitUploadPackFactory) {
+  private GitblitUploadPackFactory<SshDaemonClient> gitblitUploadPackFactory;
+  public void setUploadPackFactory(GitblitUploadPackFactory<SshDaemonClient> gitblitUploadPackFactory) {
          this.gitblitUploadPackFactory = gitblitUploadPackFactory;
   }
 
-  private GitblitReceivePackFactory<SshSession> gitblitReceivePackFactory;
-  public void setReceivePackFactory(GitblitReceivePackFactory<SshSession> gitblitReceivePackFactory) {
+  private GitblitReceivePackFactory<SshDaemonClient> gitblitReceivePackFactory;
+  public void setReceivePackFactory(GitblitReceivePackFactory<SshDaemonClient> gitblitReceivePackFactory) {
          this.gitblitReceivePackFactory = gitblitReceivePackFactory;
   }
 
index dd1e8a063410bf072e56108ba0847e0df94556c8..f8c1334c2453e4191cfafb824c8cf141d331022d 100644 (file)
@@ -25,7 +25,7 @@ public class Receive extends AbstractGitCommand {
        @Override
        protected void runImpl() throws Failure {
                try {
-                       ReceivePack rp = receivePackFactory.create(ctx.getSession(), repo);
+                       ReceivePack rp = receivePackFactory.create(ctx.getClient(), repo);
                        rp.receive(in, out, null);
                } catch (Exception e) {
                        throw new Failure(1, "fatal: Cannot receive pack: ", e);
index 44543f4254b23b8d0014936250f5814d4376644c..d1566596c093cc30eb0d931212483b9759343f50 100644 (file)
@@ -25,7 +25,7 @@ public class Upload extends AbstractGitCommand {
        @Override
        protected void runImpl() throws Failure {
                try {
-                       UploadPack up = uploadPackFactory.create(ctx.getSession(), repo);
+                       UploadPack up = uploadPackFactory.create(ctx.getClient(), repo);
                        up.upload(in, out, null);
                } catch (Exception e) {
                        throw new Failure(1, "fatal: Cannot upload pack: ", e);