James Moger
2014-03-14 a8dd379bc357c64d1128bc6790e681e27387dbee
Rename & simplify SshSession->SshDaemonClient
1 files deleted
1 files added
12 files modified
280 ■■■■■ changed files
src/main/java/com/gitblit/git/GitblitReceivePackFactory.java 9 ●●●●● patch | view | raw | blame | history
src/main/java/com/gitblit/git/RepositoryResolver.java 22 ●●●● patch | view | raw | blame | history
src/main/java/com/gitblit/transport/ssh/AbstractGitCommand.java 14 ●●●● patch | view | raw | blame | history
src/main/java/com/gitblit/transport/ssh/SshCommandFactory.java 2 ●●● patch | view | raw | blame | history
src/main/java/com/gitblit/transport/ssh/SshContext.java 10 ●●●● patch | view | raw | blame | history
src/main/java/com/gitblit/transport/ssh/SshDaemon.java 6 ●●●● patch | view | raw | blame | history
src/main/java/com/gitblit/transport/ssh/SshDaemonClient.java 64 ●●●●● patch | view | raw | blame | history
src/main/java/com/gitblit/transport/ssh/SshKeyAuthenticator.java 8 ●●●● patch | view | raw | blame | history
src/main/java/com/gitblit/transport/ssh/SshPasswordAuthenticator.java 6 ●●●● patch | view | raw | blame | history
src/main/java/com/gitblit/transport/ssh/SshSession.java 111 ●●●●● patch | view | raw | blame | history
src/main/java/com/gitblit/transport/ssh/SshSessionFactory.java 10 ●●●● patch | view | raw | blame | history
src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java 14 ●●●● patch | view | raw | blame | history
src/main/java/com/gitblit/transport/ssh/commands/Receive.java 2 ●●● patch | view | raw | blame | history
src/main/java/com/gitblit/transport/ssh/commands/Upload.java 2 ●●● patch | view | raw | blame | history
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);
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)) {
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;
    }
}
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);
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() {
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(
src/main/java/com/gitblit/transport/ssh/SshDaemonClient.java
New file
@@ -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;
    }
}
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;
                    }
                }
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;
        }
src/main/java/com/gitblit/transport/ssh/SshSession.java
File was deleted
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
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;
  }
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);
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);