Skip to content

Commit a7ea0d1

Browse files
authored
Merge pull request #60 from tech-advantage/ssh-backport
backport StrictHostKeyChecking to 2.3
2 parents 1c5a48e + dcaa9ba commit a7ea0d1

File tree

6 files changed

+63
-8
lines changed

6 files changed

+63
-8
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>fr.techad</groupId>
66
<artifactId>sonar-gerrit-plugin</artifactId>
7-
<version>2.3.0</version>
7+
<version>2.3.1</version>
88
<packaging>sonar-plugin</packaging>
99
<name>Sonar Gerrit Plugin</name>
1010
<description>Sonar will rate your gerrit patch set and comment on found alerts.</description>

src/main/java/fr/techad/sonar/GerritConfiguration.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class GerritConfiguration {
1818
private boolean valid;
1919
private boolean anonymous;
2020
private boolean commentNewIssuesOnly;
21+
private boolean strictHostkey;
2122

2223
private String host;
2324

@@ -48,6 +49,7 @@ public GerritConfiguration(Settings settings) {
4849

4950
this.enable(settings.getBoolean(PropertyKey.GERRIT_ENABLED));
5051
this.commentNewIssuesOnly(settings.getBoolean(PropertyKey.GERRIT_COMMENT_NEW_ISSUES_ONLY));
52+
this.strictlyCheckHostkey(settings.getBoolean(PropertyKey.GERRIT_STRICT_HOSTKEY));
5153

5254
this.setScheme(settings.getString(PropertyKey.GERRIT_SCHEME));
5355
this.setHost(settings.getString(PropertyKey.GERRIT_HOST));
@@ -109,7 +111,15 @@ public boolean shouldCommentNewIssuesOnly() {
109111
return commentNewIssuesOnly;
110112
}
111113

112-
@NotNull
114+
public GerritConfiguration strictlyCheckHostkey(boolean strictHostkey) {
115+
this.strictHostkey = strictHostkey;
116+
return this;
117+
}
118+
119+
public boolean shouldStrictlyCheckHostKey() {
120+
return strictHostkey;
121+
}
122+
113123
public String getScheme() {
114124
return scheme;
115125
}

src/main/java/fr/techad/sonar/GerritConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public final class GerritConstants {
1313
public static final String AUTH_BASIC = "basic";
1414
public static final String AUTH_DIGEST = "digest";
1515
public static final String GERRIT_COMMENT_NEW_ISSUES_ONLY = "false";
16+
public static final String GERRIT_STRICT_HOSTKEY_DEFAULT = "true";
1617
public static final String GERRIT_VOTE_NO_ISSUE_DEFAULT = "+1";
1718
public static final String GERRIT_VOTE_ISSUE_BELOW_THRESHOLD_DEFAULT = "+1";
1819
public static final String GERRIT_VOTE_ISSUE_ABOVE_THRESHOLD_DEFAULT = "-1";

src/main/java/fr/techad/sonar/GerritPlugin.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,13 @@ public void define(Context context) {
4545
.type(PropertyType.PASSWORD).index(serverBaseIndex++).build();
4646

4747
PropertyDefinition sshKeyPath = PropertyDefinition.builder(PropertyKey.GERRIT_SSH_KEY_PATH)
48-
.category(GerritConstants.GERRIT_CATEGORY).subCategory(GerritConstants.GERRIT_SUBCATEGORY_SERVER)
49-
.type(PropertyType.STRING).index(serverBaseIndex++).build();
48+
.category(GerritConstants.GERRIT_CATEGORY).subCategory(GerritConstants.GERRIT_SUBCATEGORY_SERVER)
49+
.type(PropertyType.STRING).index(serverBaseIndex++).build();
50+
51+
PropertyDefinition strictHostkey = PropertyDefinition.builder(PropertyKey.GERRIT_STRICT_HOSTKEY)
52+
.category(GerritConstants.GERRIT_CATEGORY).subCategory(GerritConstants.GERRIT_SUBCATEGORY_SERVER)
53+
.type(PropertyType.BOOLEAN).defaultValue(GerritConstants.GERRIT_STRICT_HOSTKEY_DEFAULT)
54+
.index(serverBaseIndex++).build();
5055

5156
PropertyDefinition authScheme = PropertyDefinition.builder(PropertyKey.GERRIT_HTTP_AUTH_SCHEME)
5257
.category(GerritConstants.GERRIT_CATEGORY).subCategory(GerritConstants.GERRIT_SUBCATEGORY_SERVER)
@@ -102,7 +107,7 @@ public void define(Context context) {
102107

103108
context.addExtensions(Arrays.asList(GerritConfiguration.class, GerritConnectorFactory.class,
104109
GerritFacadeFactory.class, GerritInitializer.class, GerritProjectBuilder.class, GerritPostJob.class,
105-
enabled, scheme, host, port, username, password, authScheme, basePath, sshKeyPath, label, message,
106-
newIssuesOnly, threshold, voteNoIssue, voteIssueBelowThreshold, voteIssueAboveThreshold, issueComment));
110+
enabled, scheme, host, port, username, password, authScheme, basePath, sshKeyPath, strictHostkey, label,
111+
message, newIssuesOnly, threshold, voteNoIssue, voteIssueBelowThreshold, voteIssueAboveThreshold, issueComment));
107112
}
108113
}

src/main/java/fr/techad/sonar/PropertyKey.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public final class PropertyKey {
2323
public static final String GERRIT_VOTE_ISSUE_BELOW_THRESHOLD = "GERRIT_VOTE_ISSUE_BELOW_THRESHOLD";
2424
public static final String GERRIT_VOTE_ISSUE_ABOVE_THRESHOLD = "GERRIT_VOTE_ISSUE_ABOVE_THRESHOLD";
2525
public static final String GERRIT_ISSUE_COMMENT = "GERRIT_ISSUE_COMMENT";
26+
public static final String GERRIT_STRICT_HOSTKEY = "GERRIT_STRICT_HOSTKEY";
2627

2728
private PropertyKey() {
2829
}

src/main/java/fr/techad/sonar/gerrit/GerritSshConnector.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package fr.techad.sonar.gerrit;
22

3+
import java.io.File;
34
import java.io.IOException;
45
import java.nio.ByteBuffer;
6+
import java.nio.file.Files;
7+
import java.nio.file.LinkOption;
8+
import java.nio.file.Paths;
59

610
import fi.jpalomaki.ssh.Result;
711
import fi.jpalomaki.ssh.SshClient;
812
import fi.jpalomaki.ssh.UserAtHost;
913
import fi.jpalomaki.ssh.jsch.JschSshClient;
14+
import fi.jpalomaki.ssh.jsch.JschSshClient.Options;
1015

1116
import org.jetbrains.annotations.NotNull;
1217
import org.sonar.api.utils.log.Logger;
@@ -18,6 +23,9 @@ public class GerritSshConnector implements GerritConnector {
1823
private static final Logger LOG = Loggers.get(GerritSshConnector.class);
1924
private static final String CMD_LIST_FILES = "gerrit query --format=JSON --files --current-patch-set status:open change:%s limit:1";
2025
private static final String CMD_SET_REVIEW = "gerrit review %s -j";
26+
private static final String SSH_KNOWN_HOSTS = ".ssh/known_hosts";
27+
private static final String SSH_STRICT_NO = "StrictHostKeyChecking=no";
28+
2129
private final GerritConfiguration gerritConfiguration;
2230
private final UserAtHost userAtHost;
2331

@@ -31,7 +39,7 @@ public GerritSshConnector(GerritConfiguration gerritConfiguration) {
3139
@NotNull
3240
@Override
3341
public String listFiles() throws IOException {
34-
SshClient sshClient = new JschSshClient(gerritConfiguration.getSshKeyPath(), gerritConfiguration.getPassword());
42+
SshClient sshClient = getSshClient();
3543

3644
LOG.debug("[GERRIT PLUGIN] Execute command SSH {}",
3745
String.format(CMD_LIST_FILES, gerritConfiguration.getChangeId()));
@@ -48,7 +56,7 @@ public String setReview(String reviewInputAsJson) throws IOException {
4856
LOG.info("[GERRIT PLUGIN] Setting review {}", reviewInputAsJson);
4957

5058
ByteBuffer stdin = ByteBuffer.wrap(reviewInputAsJson.getBytes("UTF-8"));
51-
SshClient sshClient = new JschSshClient(gerritConfiguration.getSshKeyPath(), gerritConfiguration.getPassword());
59+
SshClient sshClient = getSshClient();
5260

5361
LOG.debug("[GERRIT PLUGIN] Execute command SSH {}",
5462
String.format(CMD_SET_REVIEW, gerritConfiguration.getRevisionId()));
@@ -58,4 +66,34 @@ public String setReview(String reviewInputAsJson) throws IOException {
5866

5967
return cmdResult.stdoutAsText();
6068
}
69+
70+
private SshClient getSshClient() {
71+
SshClient sc = null;
72+
73+
if (gerritConfiguration.shouldStrictlyCheckHostKey()) {
74+
LOG.debug("[GERRIT PLUGIN] SSH will check host key.");
75+
sc = new JschSshClient(gerritConfiguration.getSshKeyPath(), gerritConfiguration.getPassword());
76+
} else {
77+
LOG.debug("[GERRIT PLUGIN] SSH will not check host key.");
78+
String userKnownHosts = System.getProperty("user.home") + File.separator + SSH_KNOWN_HOSTS;
79+
Boolean knownHostsExists = Files.exists(Paths.get(userKnownHosts), LinkOption.NOFOLLOW_LINKS);
80+
81+
if (!knownHostsExists) {
82+
LOG.debug("[GERRIT PLUGIN] {} does not exist. Creating.", userKnownHosts);
83+
// known_hosts DOES NOT exists => create it
84+
try {
85+
Files.createFile(Paths.get(userKnownHosts));
86+
} catch (IOException e) {
87+
LOG.warn("[GERRIT PLUGIN] Could not create known_hosts", e);
88+
}
89+
LOG.debug("[GERRIT PLUGIN] {} created.", userKnownHosts);
90+
}
91+
92+
sc = new JschSshClient(gerritConfiguration.getSshKeyPath(), gerritConfiguration.getPassword(),
93+
userKnownHosts, new Options("5s", "0s", "1M", "1M", SSH_STRICT_NO, false));
94+
}
95+
96+
return sc;
97+
}
98+
6199
}

0 commit comments

Comments
 (0)