From: James Moger Date: Fri, 14 Mar 2014 16:43:55 +0000 (-0400) Subject: Rename & simplify SshSession->SshDaemonClient X-Git-Tag: v1.5.0~68^2~92 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a8dd379bc357c64d1128bc6790e681e27387dbee;p=gitblit.git Rename & simplify SshSession->SshDaemonClient --- diff --git a/src/main/java/com/gitblit/git/GitblitReceivePackFactory.java b/src/main/java/com/gitblit/git/GitblitReceivePackFactory.java index b928d851..41e348ba 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.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 implements ReceivePackFactory { // 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 ad5dcf01..cc13144e 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.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 extends FileResolver { // 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 extends FileResolver { 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 bba6a402..188cb005 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 @@ public abstract class AbstractGitCommand extends BaseCommand { @Argument(index = 0, metaVar = "PROJECT.git", required = true, usage = "project name") protected String repository; - protected RepositoryResolver repositoryResolver; - protected ReceivePackFactory receivePackFactory; - protected UploadPackFactory uploadPackFactory; + protected RepositoryResolver repositoryResolver; + protected ReceivePackFactory receivePackFactory; + protected UploadPackFactory 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 repositoryResolver) { + RepositoryResolver repositoryResolver) { this.repositoryResolver = repositoryResolver; } public void setReceivePackFactory( - GitblitReceivePackFactory receivePackFactory) { + GitblitReceivePackFactory receivePackFactory) { this.receivePackFactory = receivePackFactory; } public void setUploadPackFactory( - GitblitUploadPackFactory uploadPackFactory) { + GitblitUploadPackFactory 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 0c8492f7..a52e62b8 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 @@ 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); diff --git a/src/main/java/com/gitblit/transport/ssh/SshContext.java b/src/main/java/com/gitblit/transport/ssh/SshContext.java index b137cb87..4c5786e4 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 @@ 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() { diff --git a/src/main/java/com/gitblit/transport/ssh/SshDaemon.java b/src/main/java/com/gitblit/transport/ssh/SshDaemon.java index 152b8263..81d78784 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 @@ public class SshDaemon { root.registerDispatcher("gitblit", gitblitCmd); root.registerDispatcher("git", gitCmd); - root.setRepositoryResolver(new RepositoryResolver(gitblit)); - root.setUploadPackFactory(new GitblitUploadPackFactory(gitblit)); - root.setReceivePackFactory(new GitblitReceivePackFactory(gitblit)); + root.setRepositoryResolver(new RepositoryResolver(gitblit)); + root.setUploadPackFactory(new GitblitUploadPackFactory(gitblit)); + root.setReceivePackFactory(new GitblitReceivePackFactory(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 00000000..4d8ea4b6 --- /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 KEY = new AttributeKey(); + + 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 044d2643..36319226 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 @@ public class SshKeyAuthenticator implements PublickeyAuthenticator { protected final Logger log = LoggerFactory.getLogger(getClass()); protected final IKeyManager keyManager; - + protected final IAuthenticationManager authManager; LoadingCache> 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; } } diff --git a/src/main/java/com/gitblit/transport/ssh/SshPasswordAuthenticator.java b/src/main/java/com/gitblit/transport/ssh/SshPasswordAuthenticator.java index 3baf985d..5ddc4a39 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 @@ 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 index ffff8af4..00000000 --- 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 KEY = - new AttributeKey(); - - 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 ae6da3fb..66fe057d 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 @@ 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() { + session.addCloseSessionListener(new SshFutureListener() { @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 31b718e0..dc963309 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.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 repositoryResolver; - public void setRepositoryResolver(RepositoryResolver repositoryResolver) { + private RepositoryResolver repositoryResolver; + public void setRepositoryResolver(RepositoryResolver repositoryResolver) { this.repositoryResolver = repositoryResolver; } - private GitblitUploadPackFactory gitblitUploadPackFactory; - public void setUploadPackFactory(GitblitUploadPackFactory gitblitUploadPackFactory) { + private GitblitUploadPackFactory gitblitUploadPackFactory; + public void setUploadPackFactory(GitblitUploadPackFactory gitblitUploadPackFactory) { this.gitblitUploadPackFactory = gitblitUploadPackFactory; } - private GitblitReceivePackFactory gitblitReceivePackFactory; - public void setReceivePackFactory(GitblitReceivePackFactory gitblitReceivePackFactory) { + private GitblitReceivePackFactory gitblitReceivePackFactory; + public void setReceivePackFactory(GitblitReceivePackFactory 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 dd1e8a06..f8c1334c 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 @@ 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); 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 44543f42..d1566596 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 @@ 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);