Line data Source code
1 : // Copyright (C) 2009 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.sshd; 16 : 17 : import static com.google.gerrit.extensions.registration.PrivateInternals_DynamicTypes.registerInParentInjectors; 18 : import static com.google.inject.Scopes.SINGLETON; 19 : 20 : import com.google.common.base.CharMatcher; 21 : import com.google.common.base.Splitter; 22 : import com.google.gerrit.extensions.events.AccountActivationListener; 23 : import com.google.gerrit.extensions.registration.DynamicItem; 24 : import com.google.gerrit.extensions.registration.DynamicSet; 25 : import com.google.gerrit.lifecycle.LifecycleModule; 26 : import com.google.gerrit.server.PeerDaemonUser; 27 : import com.google.gerrit.server.RemotePeer; 28 : import com.google.gerrit.server.config.GerritConfigListener; 29 : import com.google.gerrit.server.config.GerritRequestModule; 30 : import com.google.gerrit.server.config.GerritServerConfig; 31 : import com.google.gerrit.server.git.QueueProvider; 32 : import com.google.gerrit.server.git.receive.AsyncReceiveCommits.AsyncReceiveCommitsModule; 33 : import com.google.gerrit.server.plugins.ModuleGenerator; 34 : import com.google.gerrit.server.plugins.ReloadPluginListener; 35 : import com.google.gerrit.server.plugins.StartPluginListener; 36 : import com.google.gerrit.server.ssh.SshInfo; 37 : import com.google.gerrit.server.util.RequestScopePropagator; 38 : import com.google.inject.Inject; 39 : import com.google.inject.internal.UniqueAnnotations; 40 : import com.google.inject.servlet.RequestScoped; 41 : import java.net.SocketAddress; 42 : import java.util.HashMap; 43 : import java.util.List; 44 : import java.util.Map; 45 : import java.util.concurrent.ScheduledThreadPoolExecutor; 46 : import org.apache.sshd.server.auth.gss.GSSAuthenticator; 47 : import org.apache.sshd.server.auth.pubkey.PublickeyAuthenticator; 48 : import org.apache.sshd.server.command.CommandFactory; 49 : import org.eclipse.jgit.lib.Config; 50 : 51 : /** Configures standard dependencies for {@link SshDaemon}. */ 52 : public class SshModule extends LifecycleModule { 53 : private final Map<String, String> aliases; 54 : 55 : @Inject 56 17 : SshModule(@GerritServerConfig Config cfg) { 57 17 : aliases = new HashMap<>(); 58 17 : for (String name : cfg.getNames("ssh-alias", true)) { 59 0 : aliases.put(name, cfg.getString("ssh-alias", null, name)); 60 0 : } 61 17 : } 62 : 63 : @Override 64 : protected void configure() { 65 17 : bindScope(RequestScoped.class, SshScope.REQUEST); 66 17 : bind(RequestScopePropagator.class).to(SshScope.Propagator.class); 67 17 : bind(SshScope.class).in(SINGLETON); 68 : 69 17 : configureRequestScope(); 70 17 : install(new AsyncReceiveCommitsModule()); 71 17 : configureAliases(); 72 : 73 17 : bind(SshLog.class); 74 17 : DynamicSet.bind(binder(), GerritConfigListener.class).to(SshLog.class); 75 : 76 17 : bind(SshInfo.class).to(SshDaemon.class).in(SINGLETON); 77 17 : factory(DispatchCommand.Factory.class); 78 17 : factory(PeerDaemonUser.Factory.class); 79 : 80 17 : bind(DispatchCommandProvider.class) 81 17 : .annotatedWith(Commands.CMD_ROOT) 82 17 : .toInstance(new DispatchCommandProvider(Commands.CMD_ROOT)); 83 17 : bind(CommandFactoryProvider.class); 84 17 : bind(CommandFactory.class).toProvider(CommandFactoryProvider.class); 85 17 : bind(ScheduledThreadPoolExecutor.class) 86 17 : .annotatedWith(StreamCommandExecutor.class) 87 17 : .toProvider(StreamCommandExecutorProvider.class) 88 17 : .in(SINGLETON); 89 17 : bind(QueueProvider.class).to(CommandExecutorQueueProvider.class); 90 : 91 17 : bind(GSSAuthenticator.class).to(GerritGSSAuthenticator.class); 92 17 : bind(PublickeyAuthenticator.class).to(CachingPublicKeyAuthenticator.class); 93 : 94 17 : bind(ModuleGenerator.class).to(SshAutoRegisterModuleGenerator.class); 95 17 : bind(SshPluginStarterCallback.class); 96 17 : bind(StartPluginListener.class) 97 17 : .annotatedWith(UniqueAnnotations.create()) 98 17 : .to(SshPluginStarterCallback.class); 99 : 100 17 : bind(ReloadPluginListener.class) 101 17 : .annotatedWith(UniqueAnnotations.create()) 102 17 : .to(SshPluginStarterCallback.class); 103 : 104 17 : DynamicItem.itemOf(binder(), SshCreateCommandInterceptor.class); 105 17 : DynamicSet.setOf(binder(), SshExecuteCommandInterceptor.class); 106 : 107 17 : DynamicSet.bind(binder(), AccountActivationListener.class) 108 17 : .to(InactiveAccountDisconnector.class); 109 : 110 17 : listener().toInstance(registerInParentInjectors()); 111 17 : listener().to(SshLog.class); 112 17 : listener().to(SshDaemon.class); 113 17 : listener().to(CommandFactoryProvider.class); 114 17 : } 115 : 116 : private void configureAliases() { 117 17 : CommandName gerrit = Commands.named("gerrit"); 118 17 : for (Map.Entry<String, String> e : aliases.entrySet()) { 119 0 : String name = e.getKey(); 120 0 : List<String> dest = Splitter.on(CharMatcher.whitespace()).splitToList(e.getValue()); 121 0 : CommandName cmd = Commands.named(dest.get(0)); 122 0 : for (int i = 1; i < dest.size(); i++) { 123 0 : cmd = Commands.named(cmd, dest.get(i)); 124 : } 125 0 : bind(Commands.key(gerrit, name)).toProvider(new AliasCommandProvider(cmd)); 126 0 : } 127 17 : } 128 : 129 : private void configureRequestScope() { 130 17 : bind(SshScope.Context.class).toProvider(SshScope.ContextProvider.class); 131 : 132 17 : bind(SshSession.class).toProvider(SshScope.SshSessionProvider.class).in(SshScope.REQUEST); 133 17 : bind(SocketAddress.class) 134 17 : .annotatedWith(RemotePeer.class) 135 17 : .toProvider(SshRemotePeerProvider.class) 136 17 : .in(SshScope.REQUEST); 137 : 138 17 : bind(ScheduledThreadPoolExecutor.class) 139 17 : .annotatedWith(CommandExecutor.class) 140 17 : .toProvider(CommandExecutorProvider.class) 141 17 : .in(SshScope.REQUEST); 142 : 143 17 : install(new GerritRequestModule()); 144 17 : } 145 : }