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;
// 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);
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.
// 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;
}
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)) {
@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;
}
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);
}
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
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);
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() {
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(
--- /dev/null
+/*
+ * 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;
+ }
+}
protected final Logger log = LoggerFactory.getLogger(getClass());
protected final IKeyManager keyManager;
-
+
protected final IAuthenticationManager authManager;
LoadingCache<String, List<PublicKey>> sshKeyCache = CacheBuilder
@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;
if (key.equals(suppliedKey)) {
UserModel user = authManager.authenticate(username, key);
if (user != null) {
- client.authenticationSuccess(username);
+ client.setUser(user);
return true;
}
}
@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;
}
username = username.toLowerCase(Locale.US);
UserModel user = authManager.authenticate(username, password.toCharArray());
if (user != null) {
- client.authenticationSuccess(username);
+ client.setUser(user);
return true;
}
+++ /dev/null
-/*
- * 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();
- }
-}
}
}
- 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
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;
}
}
- 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;
}
@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);
@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);