Remove SshInfo from email templates and move to sysInjector
SshInfo in the web context comes from SshDaemon and thus must be bound in a child of sysInjector. This doesn't work for REST API endpoints, so email template classes cannot depend on SshInfo. The only thing they really need, though, is the advertised SSH address, which comes directly from the server config. Move the server config parsing logic into a module in gerrit-server and inject the lists of listening and advertised addresses into SshDaemon as well as EmailArguments. Change-Id: Iee393e625898c00c3dac02e6a407ecb866a939ff
This commit is contained in:
committed by
Edwin Kempin
parent
386add71e3
commit
107c1bbb3f
@@ -75,9 +75,16 @@ import com.google.gerrit.server.git.TagCache;
|
||||
import com.google.gerrit.server.git.TransferConfig;
|
||||
import com.google.gerrit.server.git.validators.CommitValidationListener;
|
||||
import com.google.gerrit.server.git.validators.CommitValidators;
|
||||
import com.google.gerrit.server.mail.AddReviewerSender;
|
||||
import com.google.gerrit.server.mail.CommitMessageEditedSender;
|
||||
import com.google.gerrit.server.mail.CreateChangeSender;
|
||||
import com.google.gerrit.server.mail.EmailModule;
|
||||
import com.google.gerrit.server.mail.FromAddressGenerator;
|
||||
import com.google.gerrit.server.mail.FromAddressGeneratorProvider;
|
||||
import com.google.gerrit.server.mail.MergeFailSender;
|
||||
import com.google.gerrit.server.mail.MergedSender;
|
||||
import com.google.gerrit.server.mail.RebasedPatchSetSender;
|
||||
import com.google.gerrit.server.mail.ReplacePatchSetSender;
|
||||
import com.google.gerrit.server.mail.VelocityRuntimeProvider;
|
||||
import com.google.gerrit.server.patch.PatchListCacheImpl;
|
||||
import com.google.gerrit.server.patch.PatchSetInfoFactory;
|
||||
@@ -91,6 +98,7 @@ import com.google.gerrit.server.project.ProjectState;
|
||||
import com.google.gerrit.server.project.SectionSortCache;
|
||||
import com.google.gerrit.server.query.change.ChangeQueryBuilder;
|
||||
import com.google.gerrit.server.query.change.ChangeQueryRewriter;
|
||||
import com.google.gerrit.server.ssh.SshAddressesModule;
|
||||
import com.google.gerrit.server.tools.ToolsCatalog;
|
||||
import com.google.gerrit.server.util.IdGenerator;
|
||||
import com.google.gerrit.server.util.ThreadLocalRequestContext;
|
||||
@@ -152,21 +160,29 @@ public class GerritGlobalModule extends FactoryModule {
|
||||
install(new EmailModule());
|
||||
install(new GitModule());
|
||||
install(new PrologModule());
|
||||
install(new SshAddressesModule());
|
||||
install(ThreadLocalRequestContext.module());
|
||||
|
||||
bind(AccountResolver.class);
|
||||
bind(ChangeQueryRewriter.class);
|
||||
|
||||
factory(AccountInfoCacheFactory.Factory.class);
|
||||
factory(AddReviewerSender.Factory.class);
|
||||
factory(CapabilityControl.Factory.class);
|
||||
factory(ChangeQueryBuilder.Factory.class);
|
||||
factory(GroupInfoCacheFactory.Factory.class);
|
||||
factory(CommitMessageEditedSender.Factory.class);
|
||||
factory(CreateChangeSender.Factory.class);
|
||||
factory(GroupDetailFactory.Factory.class);
|
||||
factory(GroupInfoCacheFactory.Factory.class);
|
||||
factory(InternalUser.Factory.class);
|
||||
factory(MergedSender.Factory.class);
|
||||
factory(MergeFailSender.Factory.class);
|
||||
factory(PerformCreateGroup.Factory.class);
|
||||
factory(PerformRenameGroup.Factory.class);
|
||||
factory(InternalUser.Factory.class);
|
||||
factory(ProjectNode.Factory.class);
|
||||
factory(ProjectState.Factory.class);
|
||||
factory(RebasedPatchSetSender.Factory.class);
|
||||
factory(ReplacePatchSetSender.Factory.class);
|
||||
bind(PermissionCollection.Factory.class);
|
||||
bind(AccountVisibility.class)
|
||||
.toProvider(AccountVisibilityProvider.class)
|
||||
|
||||
@@ -27,13 +27,6 @@ import com.google.gerrit.server.git.BanCommit;
|
||||
import com.google.gerrit.server.git.MergeOp;
|
||||
import com.google.gerrit.server.git.MetaDataUpdate;
|
||||
import com.google.gerrit.server.git.SubmoduleOp;
|
||||
import com.google.gerrit.server.mail.AddReviewerSender;
|
||||
import com.google.gerrit.server.mail.CommitMessageEditedSender;
|
||||
import com.google.gerrit.server.mail.CreateChangeSender;
|
||||
import com.google.gerrit.server.mail.MergeFailSender;
|
||||
import com.google.gerrit.server.mail.MergedSender;
|
||||
import com.google.gerrit.server.mail.RebasedPatchSetSender;
|
||||
import com.google.gerrit.server.mail.ReplacePatchSetSender;
|
||||
import com.google.gerrit.server.patch.AddReviewer;
|
||||
import com.google.gerrit.server.patch.RemoveReviewer;
|
||||
import com.google.gerrit.server.project.ChangeControl;
|
||||
@@ -65,16 +58,9 @@ public class GerritRequestModule extends FactoryModule {
|
||||
// easily park this stuff.
|
||||
//
|
||||
factory(AddReviewer.Factory.class);
|
||||
factory(AddReviewerSender.Factory.class);
|
||||
factory(CommitMessageEditedSender.Factory.class);
|
||||
factory(CreateChangeSender.Factory.class);
|
||||
factory(DeleteDraftPatchSet.Factory.class);
|
||||
factory(PublishDraft.Factory.class);
|
||||
factory(ReplacePatchSetSender.Factory.class);
|
||||
factory(RebasedPatchSetSender.Factory.class);
|
||||
factory(RemoveReviewer.Factory.class);
|
||||
factory(MergedSender.Factory.class);
|
||||
factory(MergeFailSender.Factory.class);
|
||||
factory(GroupMembers.Factory.class);
|
||||
factory(CreateProject.Factory.class);
|
||||
factory(SuggestParentCandidates.Factory.class);
|
||||
|
||||
@@ -16,7 +16,6 @@ package com.google.gerrit.server.mail;
|
||||
|
||||
import com.google.gerrit.common.errors.EmailException;
|
||||
import com.google.gerrit.reviewdb.client.Change;
|
||||
import com.google.gerrit.server.ssh.SshInfo;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.assistedinject.Assisted;
|
||||
|
||||
@@ -27,9 +26,8 @@ public class AddReviewerSender extends NewChangeSender {
|
||||
}
|
||||
|
||||
@Inject
|
||||
public AddReviewerSender(EmailArguments ea, SshInfo si, @Assisted Change c) {
|
||||
public AddReviewerSender(EmailArguments ea, @Assisted Change c) {
|
||||
super(ea, c);
|
||||
setSshInfo(si);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -16,7 +16,6 @@ package com.google.gerrit.server.mail;
|
||||
|
||||
import com.google.gerrit.common.errors.EmailException;
|
||||
import com.google.gerrit.reviewdb.client.Change;
|
||||
import com.google.gerrit.server.ssh.SshInfo;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.assistedinject.Assisted;
|
||||
|
||||
@@ -26,9 +25,8 @@ public class CommitMessageEditedSender extends ReplacePatchSetSender {
|
||||
}
|
||||
|
||||
@Inject
|
||||
public CommitMessageEditedSender(EmailArguments ea, SshInfo si,
|
||||
@Assisted Change c) {
|
||||
super(ea, si, c);
|
||||
public CommitMessageEditedSender(EmailArguments ea, @Assisted Change c) {
|
||||
super(ea, c);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -20,7 +20,6 @@ import com.google.gerrit.reviewdb.client.Account;
|
||||
import com.google.gerrit.reviewdb.client.AccountProjectWatch.NotifyType;
|
||||
import com.google.gerrit.reviewdb.client.Change;
|
||||
import com.google.gerrit.server.mail.ProjectWatch.Watchers;
|
||||
import com.google.gerrit.server.ssh.SshInfo;
|
||||
import com.google.gwtorm.server.OrmException;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.assistedinject.Assisted;
|
||||
@@ -38,9 +37,8 @@ public class CreateChangeSender extends NewChangeSender {
|
||||
}
|
||||
|
||||
@Inject
|
||||
public CreateChangeSender(EmailArguments ea, SshInfo si, @Assisted Change c) {
|
||||
public CreateChangeSender(EmailArguments ea, @Assisted Change c) {
|
||||
super(ea, c);
|
||||
setSshInfo(si);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -31,11 +31,14 @@ import com.google.gerrit.server.patch.PatchSetInfoFactory;
|
||||
import com.google.gerrit.server.project.ProjectCache;
|
||||
import com.google.gerrit.server.query.change.ChangeQueryBuilder;
|
||||
import com.google.gerrit.server.query.change.ChangeQueryRewriter;
|
||||
import com.google.gerrit.server.ssh.SshAdvertisedAddresses;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
|
||||
import org.apache.velocity.runtime.RuntimeInstance;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
class EmailArguments {
|
||||
@@ -54,6 +57,7 @@ class EmailArguments {
|
||||
final String anonymousCowardName;
|
||||
final Provider<String> urlProvider;
|
||||
final AllProjectsName allProjectsName;
|
||||
final List<String> sshAddresses;
|
||||
|
||||
final ChangeQueryBuilder.Factory queryBuilder;
|
||||
final Provider<ChangeQueryRewriter> queryRewriter;
|
||||
@@ -76,7 +80,8 @@ class EmailArguments {
|
||||
ChangeQueryBuilder.Factory queryBuilder,
|
||||
Provider<ChangeQueryRewriter> queryRewriter, Provider<ReviewDb> db,
|
||||
RuntimeInstance velocityRuntime,
|
||||
EmailSettings settings) {
|
||||
EmailSettings settings,
|
||||
@SshAdvertisedAddresses List<String> sshAddresses) {
|
||||
this.server = server;
|
||||
this.projectCache = projectCache;
|
||||
this.groupBackend = groupBackend;
|
||||
@@ -97,5 +102,6 @@ class EmailArguments {
|
||||
this.db = db;
|
||||
this.velocityRuntime = velocityRuntime;
|
||||
this.settings = settings;
|
||||
this.sshAddresses = sshAddresses;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,22 +14,18 @@
|
||||
|
||||
package com.google.gerrit.server.mail;
|
||||
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.gerrit.common.errors.EmailException;
|
||||
import com.google.gerrit.reviewdb.client.Account;
|
||||
import com.google.gerrit.reviewdb.client.AccountProjectWatch.NotifyType;
|
||||
import com.google.gerrit.reviewdb.client.Branch;
|
||||
import com.google.gerrit.reviewdb.client.Project;
|
||||
import com.google.gerrit.reviewdb.client.AccountProjectWatch.NotifyType;
|
||||
import com.google.gerrit.server.mail.ProjectWatch.Watchers;
|
||||
import com.google.gerrit.server.ssh.SshInfo;
|
||||
import com.google.gwtorm.server.OrmException;
|
||||
|
||||
import com.jcraft.jsch.HostKey;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Common class for notifications that are related to a project and branch
|
||||
*/
|
||||
@@ -39,7 +35,6 @@ public abstract class NotificationEmail extends OutgoingEmail {
|
||||
|
||||
protected Project.NameKey project;
|
||||
protected Branch.NameKey branch;
|
||||
private SshInfo sshInfo;
|
||||
|
||||
protected NotificationEmail(EmailArguments ea,
|
||||
String mc, Project.NameKey project, Branch.NameKey branch) {
|
||||
@@ -95,20 +90,11 @@ public abstract class NotificationEmail extends OutgoingEmail {
|
||||
}
|
||||
}
|
||||
|
||||
protected void setSshInfo(SshInfo si) {
|
||||
this.sshInfo = si;
|
||||
}
|
||||
|
||||
public String getSshHost() {
|
||||
if (sshInfo == null) {
|
||||
String host = Iterables.getFirst(args.sshAddresses, null);
|
||||
if (host == null) {
|
||||
return null;
|
||||
}
|
||||
final List<HostKey> hostKeys = sshInfo.getHostKeys();
|
||||
if (hostKeys.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final String host = hostKeys.get(0).getHost();
|
||||
if (host.startsWith("*:")) {
|
||||
return getGerritHost() + host.substring(1);
|
||||
}
|
||||
|
||||
@@ -16,7 +16,6 @@ package com.google.gerrit.server.mail;
|
||||
|
||||
import com.google.gerrit.common.errors.EmailException;
|
||||
import com.google.gerrit.reviewdb.client.Change;
|
||||
import com.google.gerrit.server.ssh.SshInfo;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.assistedinject.Assisted;
|
||||
|
||||
@@ -27,8 +26,8 @@ public class RebasedPatchSetSender extends ReplacePatchSetSender {
|
||||
}
|
||||
|
||||
@Inject
|
||||
public RebasedPatchSetSender(EmailArguments ea, SshInfo si, @Assisted Change c) {
|
||||
super(ea, si, c);
|
||||
public RebasedPatchSetSender(EmailArguments ea, @Assisted Change c) {
|
||||
super(ea, c);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -18,7 +18,6 @@ import com.google.gerrit.common.errors.EmailException;
|
||||
import com.google.gerrit.reviewdb.client.Account;
|
||||
import com.google.gerrit.reviewdb.client.AccountProjectWatch.NotifyType;
|
||||
import com.google.gerrit.reviewdb.client.Change;
|
||||
import com.google.gerrit.server.ssh.SshInfo;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.assistedinject.Assisted;
|
||||
|
||||
@@ -38,9 +37,8 @@ public class ReplacePatchSetSender extends ReplyToChangeSender {
|
||||
private final Set<Account.Id> extraCC = new HashSet<Account.Id>();
|
||||
|
||||
@Inject
|
||||
public ReplacePatchSetSender(EmailArguments ea, SshInfo si, @Assisted Change c) {
|
||||
public ReplacePatchSetSender(EmailArguments ea, @Assisted Change c) {
|
||||
super(ea, c, "newpatchset");
|
||||
setSshInfo(si);
|
||||
}
|
||||
|
||||
public void addReviewers(final Collection<Account.Id> cc) {
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
// Copyright (C) 2013 The Android Open Source Project
|
||||
//
|
||||
// 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.google.gerrit.server.ssh;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.gerrit.server.config.GerritServerConfig;
|
||||
import com.google.gerrit.server.util.SocketUtil;
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Provides;
|
||||
import com.google.inject.Singleton;
|
||||
|
||||
import org.eclipse.jgit.lib.Config;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.SocketAddress;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class SshAddressesModule extends AbstractModule {
|
||||
private static final Logger log =
|
||||
LoggerFactory.getLogger(SshAddressesModule.class);
|
||||
|
||||
public static final int DEFAULT_PORT = 29418;
|
||||
public static final int IANA_SSH_PORT = 22;
|
||||
|
||||
@Override
|
||||
protected void configure() {
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
@SshListenAddresses
|
||||
List<SocketAddress> getListenAddresses(@GerritServerConfig Config cfg) {
|
||||
List<SocketAddress> listen = Lists.newArrayListWithExpectedSize(2);
|
||||
String[] want = cfg.getStringList("sshd", null, "listenaddress");
|
||||
if (want == null || want.length == 0) {
|
||||
listen.add(new InetSocketAddress(DEFAULT_PORT));
|
||||
return listen;
|
||||
}
|
||||
|
||||
if (want.length == 1 && isOff(want[0])) {
|
||||
return listen;
|
||||
}
|
||||
|
||||
for (final String desc : want) {
|
||||
try {
|
||||
listen.add(SocketUtil.resolve(desc, DEFAULT_PORT));
|
||||
} catch (IllegalArgumentException e) {
|
||||
log.error("Bad sshd.listenaddress: " + desc + ": " + e.getMessage());
|
||||
}
|
||||
}
|
||||
return listen;
|
||||
}
|
||||
|
||||
private static boolean isOff(String listenHostname) {
|
||||
return "off".equalsIgnoreCase(listenHostname)
|
||||
|| "none".equalsIgnoreCase(listenHostname)
|
||||
|| "no".equalsIgnoreCase(listenHostname);
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
@SshAdvertisedAddresses
|
||||
List<String> getAdvertisedAddresses(@GerritServerConfig Config cfg,
|
||||
@SshListenAddresses List<SocketAddress> listen) {
|
||||
String[] want = cfg.getStringList("sshd", null, "advertisedaddress");
|
||||
if (want.length > 0) {
|
||||
return Arrays.asList(want);
|
||||
}
|
||||
List<InetSocketAddress> pub = Lists.newArrayList();
|
||||
List<InetSocketAddress> local = Lists.newArrayList();
|
||||
|
||||
for (SocketAddress addr : listen) {
|
||||
if (addr instanceof InetSocketAddress) {
|
||||
InetSocketAddress inetAddr = (InetSocketAddress) addr;
|
||||
if (inetAddr.getAddress().isLoopbackAddress()) {
|
||||
local.add(inetAddr);
|
||||
} else {
|
||||
pub.add(inetAddr);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (pub.isEmpty()) {
|
||||
pub = local;
|
||||
}
|
||||
List<String> adv = Lists.newArrayListWithCapacity(pub.size());
|
||||
for (InetSocketAddress addr : pub) {
|
||||
adv.add(SocketUtil.format(addr, IANA_SSH_PORT));
|
||||
}
|
||||
return adv;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
// Copyright (C) 2013 The Android Open Source Project
|
||||
//
|
||||
// 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.google.gerrit.server.ssh;
|
||||
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
import com.google.inject.BindingAnnotation;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
|
||||
/**
|
||||
* Marker on the list of {@link SocketAddress}es configured to be advertised by
|
||||
* the server.
|
||||
*/
|
||||
@Retention(RUNTIME)
|
||||
@BindingAnnotation
|
||||
public @interface SshAdvertisedAddresses {
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
// Copyright (C) 2013 The Android Open Source Project
|
||||
//
|
||||
// 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.google.gerrit.server.ssh;
|
||||
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
import com.google.inject.BindingAnnotation;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
|
||||
/**
|
||||
* Marker on the list of {@link SocketAddress}es on which the SSH daemon is
|
||||
* configured to listen.
|
||||
*/
|
||||
@Retention(RUNTIME)
|
||||
@BindingAnnotation
|
||||
public @interface SshListenAddresses {
|
||||
}
|
||||
@@ -14,6 +14,8 @@
|
||||
|
||||
package com.google.gerrit.sshd;
|
||||
|
||||
import static com.google.gerrit.server.ssh.SshAddressesModule.IANA_SSH_PORT;
|
||||
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
||||
|
||||
@@ -21,7 +23,9 @@ import com.google.gerrit.common.Version;
|
||||
import com.google.gerrit.extensions.events.LifecycleListener;
|
||||
import com.google.gerrit.server.config.ConfigUtil;
|
||||
import com.google.gerrit.server.config.GerritServerConfig;
|
||||
import com.google.gerrit.server.ssh.SshAdvertisedAddresses;
|
||||
import com.google.gerrit.server.ssh.SshInfo;
|
||||
import com.google.gerrit.server.ssh.SshListenAddresses;
|
||||
import com.google.gerrit.server.util.IdGenerator;
|
||||
import com.google.gerrit.server.util.SocketUtil;
|
||||
import com.google.inject.Inject;
|
||||
@@ -114,13 +118,10 @@ import java.util.List;
|
||||
*/
|
||||
@Singleton
|
||||
public class SshDaemon extends SshServer implements SshInfo, LifecycleListener {
|
||||
private static final int IANA_SSH_PORT = 22;
|
||||
public static final int DEFAULT_PORT = 29418;
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(SshDaemon.class);
|
||||
|
||||
private final List<SocketAddress> listen;
|
||||
private final List<String> advertisedAddress;
|
||||
private final List<String> advertised;
|
||||
private final boolean keepAlive;
|
||||
private final List<HostKey> hostKeys;
|
||||
private volatile IoAcceptor acceptor;
|
||||
@@ -129,11 +130,13 @@ public class SshDaemon extends SshServer implements SshInfo, LifecycleListener {
|
||||
SshDaemon(final CommandFactory commandFactory, final NoShell noShell,
|
||||
final PublickeyAuthenticator userAuth,
|
||||
final KeyPairProvider hostKeyProvider, final IdGenerator idGenerator,
|
||||
@GerritServerConfig final Config cfg, final SshLog sshLog) {
|
||||
@GerritServerConfig final Config cfg, final SshLog sshLog,
|
||||
@SshListenAddresses final List<SocketAddress> listen,
|
||||
@SshAdvertisedAddresses final List<String> advertised) {
|
||||
setPort(IANA_SSH_PORT /* never used */);
|
||||
|
||||
listen = parseListen(cfg);
|
||||
advertisedAddress = parseAdvertisedAddress(cfg);
|
||||
this.listen = listen;
|
||||
this.advertised = advertised;
|
||||
reuseAddress = cfg.getBoolean("sshd", "reuseaddress", true);
|
||||
keepAlive = cfg.getBoolean("sshd", "tcpkeepalive", true);
|
||||
|
||||
@@ -268,7 +271,7 @@ public class SshDaemon extends SshServer implements SshInfo, LifecycleListener {
|
||||
buf.putRawPublicKey(pub);
|
||||
final byte[] keyBin = buf.getCompactData();
|
||||
|
||||
for (final String addr : myAdvertisedAddresses()) {
|
||||
for (final String addr : advertised) {
|
||||
try {
|
||||
r.add(new HostKey(addr, keyBin));
|
||||
} catch (JSchException e) {
|
||||
@@ -279,36 +282,6 @@ public class SshDaemon extends SshServer implements SshInfo, LifecycleListener {
|
||||
return Collections.unmodifiableList(r);
|
||||
}
|
||||
|
||||
private List<String> myAdvertisedAddresses() {
|
||||
if (advertisedAddress != null) {
|
||||
return advertisedAddress;
|
||||
} else {
|
||||
List<InetSocketAddress> addrs = myAddresses();
|
||||
List<String> strAddrs = new ArrayList<String>(addrs.size());
|
||||
for (final InetSocketAddress addr : addrs) {
|
||||
strAddrs.add(SocketUtil.format(addr, IANA_SSH_PORT));
|
||||
}
|
||||
return strAddrs;
|
||||
}
|
||||
}
|
||||
|
||||
private List<InetSocketAddress> myAddresses() {
|
||||
ArrayList<InetSocketAddress> pub = new ArrayList<InetSocketAddress>();
|
||||
ArrayList<InetSocketAddress> local = new ArrayList<InetSocketAddress>();
|
||||
|
||||
for (final SocketAddress addr : listen) {
|
||||
if (addr instanceof InetSocketAddress) {
|
||||
final InetSocketAddress inetAddr = (InetSocketAddress) addr;
|
||||
if (inetAddr.getAddress().isLoopbackAddress()) {
|
||||
local.add(inetAddr);
|
||||
} else {
|
||||
pub.add(inetAddr);
|
||||
}
|
||||
}
|
||||
}
|
||||
return pub.isEmpty() ? local : pub;
|
||||
}
|
||||
|
||||
private List<PublicKey> myHostKeys() {
|
||||
final KeyPairProvider p = getKeyPairProvider();
|
||||
final List<PublicKey> keys = new ArrayList<PublicKey>(2);
|
||||
@@ -336,42 +309,6 @@ public class SshDaemon extends SshServer implements SshInfo, LifecycleListener {
|
||||
return r.toString();
|
||||
}
|
||||
|
||||
private List<String> parseAdvertisedAddress(final Config cfg) {
|
||||
final String[] want = cfg.getStringList("sshd", null, "advertisedaddress");
|
||||
if (want.length == 0) {
|
||||
return null;
|
||||
}
|
||||
return Arrays.asList(want);
|
||||
}
|
||||
|
||||
private List<SocketAddress> parseListen(final Config cfg) {
|
||||
final ArrayList<SocketAddress> bind = new ArrayList<SocketAddress>(2);
|
||||
final String[] want = cfg.getStringList("sshd", null, "listenaddress");
|
||||
if (want == null || want.length == 0) {
|
||||
bind.add(new InetSocketAddress(DEFAULT_PORT));
|
||||
return bind;
|
||||
}
|
||||
|
||||
if (want.length == 1 && isOff(want[0])) {
|
||||
return bind;
|
||||
}
|
||||
|
||||
for (final String desc : want) {
|
||||
try {
|
||||
bind.add(SocketUtil.resolve(desc, DEFAULT_PORT));
|
||||
} catch (IllegalArgumentException e) {
|
||||
log.error("Bad sshd.listenaddress: " + desc + ": " + e.getMessage());
|
||||
}
|
||||
}
|
||||
return bind;
|
||||
}
|
||||
|
||||
private static boolean isOff(String listenHostname) {
|
||||
return "off".equalsIgnoreCase(listenHostname)
|
||||
|| "none".equalsIgnoreCase(listenHostname)
|
||||
|| "no".equalsIgnoreCase(listenHostname);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void initProviderBouncyCastle() {
|
||||
setKeyExchangeFactories(Arrays.<NamedFactory<KeyExchange>> asList(
|
||||
|
||||
Reference in New Issue
Block a user