From a8dd379bc357c64d1128bc6790e681e27387dbee Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Thu, 10 Apr 2014 18:58:08 -0400
Subject: [PATCH] Rename & simplify SshSession->SshDaemonClient

---
 src/main/java/com/gitblit/transport/ssh/AbstractGitCommand.java       |   14 +-
 src/main/java/com/gitblit/git/RepositoryResolver.java                 |   22 ++--
 src/main/java/com/gitblit/transport/ssh/commands/Receive.java         |    2 
 src/main/java/com/gitblit/transport/ssh/SshPasswordAuthenticator.java |    6 
 src/main/java/com/gitblit/git/GitblitReceivePackFactory.java          |    9 -
 /dev/null                                                             |  111 ----------------------
 src/main/java/com/gitblit/transport/ssh/SshContext.java               |   10 +-
 src/main/java/com/gitblit/transport/ssh/SshDaemon.java                |    6 
 src/main/java/com/gitblit/transport/ssh/commands/Upload.java          |    2 
 src/main/java/com/gitblit/transport/ssh/SshSessionFactory.java        |   10 +-
 src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java |   14 +-
 src/main/java/com/gitblit/transport/ssh/SshKeyAuthenticator.java      |    8 
 src/main/java/com/gitblit/transport/ssh/SshDaemonClient.java          |   64 ++++++++++++
 src/main/java/com/gitblit/transport/ssh/SshCommandFactory.java        |    2 
 14 files changed, 116 insertions(+), 164 deletions(-)

diff --git a/src/main/java/com/gitblit/git/GitblitReceivePackFactory.java b/src/main/java/com/gitblit/git/GitblitReceivePackFactory.java
index b928d85..41e348b 100644
--- a/src/main/java/com/gitblit/git/GitblitReceivePackFactory.java
+++ b/src/main/java/com/gitblit/git/GitblitReceivePackFactory.java
@@ -32,7 +32,7 @@
 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 @@
 
 			// 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);
diff --git a/src/main/java/com/gitblit/git/RepositoryResolver.java b/src/main/java/com/gitblit/git/RepositoryResolver.java
index ad5dcf0..cc13144 100644
--- a/src/main/java/com/gitblit/git/RepositoryResolver.java
+++ b/src/main/java/com/gitblit/git/RepositoryResolver.java
@@ -31,7 +31,7 @@
 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 @@
 			// 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 @@
 			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)) {
diff --git a/src/main/java/com/gitblit/transport/ssh/AbstractGitCommand.java b/src/main/java/com/gitblit/transport/ssh/AbstractGitCommand.java
index bba6a40..188cb00 100644
--- a/src/main/java/com/gitblit/transport/ssh/AbstractGitCommand.java
+++ b/src/main/java/com/gitblit/transport/ssh/AbstractGitCommand.java
@@ -36,9 +36,9 @@
 	@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 @@
 		}
 		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 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
diff --git a/src/main/java/com/gitblit/transport/ssh/SshCommandFactory.java b/src/main/java/com/gitblit/transport/ssh/SshCommandFactory.java
index 0c8492f..a52e62b 100644
--- a/src/main/java/com/gitblit/transport/ssh/SshCommandFactory.java
+++ b/src/main/java/com/gitblit/transport/ssh/SshCommandFactory.java
@@ -133,7 +133,7 @@
 
 	    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);
diff --git a/src/main/java/com/gitblit/transport/ssh/SshContext.java b/src/main/java/com/gitblit/transport/ssh/SshContext.java
index b137cb8..4c5786e 100644
--- a/src/main/java/com/gitblit/transport/ssh/SshContext.java
+++ b/src/main/java/com/gitblit/transport/ssh/SshContext.java
@@ -17,16 +17,16 @@
 
 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() {
diff --git a/src/main/java/com/gitblit/transport/ssh/SshDaemon.java b/src/main/java/com/gitblit/transport/ssh/SshDaemon.java
index 152b826..81d7878 100644
--- a/src/main/java/com/gitblit/transport/ssh/SshDaemon.java
+++ b/src/main/java/com/gitblit/transport/ssh/SshDaemon.java
@@ -142,9 +142,9 @@
 		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
index 0000000..4d8ea4b
--- /dev/null
+++ b/src/main/java/com/gitblit/transport/ssh/SshDaemonClient.java
@@ -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;
+	}
+}
diff --git a/src/main/java/com/gitblit/transport/ssh/SshKeyAuthenticator.java b/src/main/java/com/gitblit/transport/ssh/SshKeyAuthenticator.java
index 044d264..3631922 100644
--- a/src/main/java/com/gitblit/transport/ssh/SshKeyAuthenticator.java
+++ b/src/main/java/com/gitblit/transport/ssh/SshKeyAuthenticator.java
@@ -43,7 +43,7 @@
 	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 @@
 	@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 @@
 				if (key.equals(suppliedKey)) {
 					UserModel user = authManager.authenticate(username, key);
 					if (user != null) {
-						client.authenticationSuccess(username);
+						client.setUser(user);
 						return true;
 					}
 				}
diff --git a/src/main/java/com/gitblit/transport/ssh/SshPasswordAuthenticator.java b/src/main/java/com/gitblit/transport/ssh/SshPasswordAuthenticator.java
index 3baf985..5ddc4a3 100644
--- a/src/main/java/com/gitblit/transport/ssh/SshPasswordAuthenticator.java
+++ b/src/main/java/com/gitblit/transport/ssh/SshPasswordAuthenticator.java
@@ -42,8 +42,8 @@
 
 	@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 @@
 		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
index ffff8af..0000000
--- a/src/main/java/com/gitblit/transport/ssh/SshSession.java
+++ /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();
-  }
-}
diff --git a/src/main/java/com/gitblit/transport/ssh/SshSessionFactory.java b/src/main/java/com/gitblit/transport/ssh/SshSessionFactory.java
index ae6da3f..66fe057 100644
--- a/src/main/java/com/gitblit/transport/ssh/SshSessionFactory.java
+++ b/src/main/java/com/gitblit/transport/ssh/SshSessionFactory.java
@@ -52,21 +52,21 @@
 			}
 		}
 
-		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
diff --git a/src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java b/src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java
index 31b718e..dc96330 100644
--- a/src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java
+++ b/src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java
@@ -33,7 +33,7 @@
 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 @@
 	  }
   }
 
-  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;
   }
 
diff --git a/src/main/java/com/gitblit/transport/ssh/commands/Receive.java b/src/main/java/com/gitblit/transport/ssh/commands/Receive.java
index dd1e8a0..f8c1334 100644
--- a/src/main/java/com/gitblit/transport/ssh/commands/Receive.java
+++ b/src/main/java/com/gitblit/transport/ssh/commands/Receive.java
@@ -25,7 +25,7 @@
 	@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);
diff --git a/src/main/java/com/gitblit/transport/ssh/commands/Upload.java b/src/main/java/com/gitblit/transport/ssh/commands/Upload.java
index 44543f4..d156659 100644
--- a/src/main/java/com/gitblit/transport/ssh/commands/Upload.java
+++ b/src/main/java/com/gitblit/transport/ssh/commands/Upload.java
@@ -25,7 +25,7 @@
 	@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);

--
Gitblit v1.9.1