1
1
package fr .techad .sonar .gerrit ;
2
2
3
+ import java .io .File ;
3
4
import java .io .IOException ;
4
5
import java .nio .ByteBuffer ;
6
+ import java .nio .file .Files ;
7
+ import java .nio .file .LinkOption ;
8
+ import java .nio .file .Paths ;
5
9
6
10
import fi .jpalomaki .ssh .Result ;
7
11
import fi .jpalomaki .ssh .SshClient ;
8
12
import fi .jpalomaki .ssh .UserAtHost ;
9
13
import fi .jpalomaki .ssh .jsch .JschSshClient ;
14
+ import fi .jpalomaki .ssh .jsch .JschSshClient .Options ;
10
15
11
16
import org .jetbrains .annotations .NotNull ;
12
17
import org .sonar .api .utils .log .Logger ;
@@ -18,6 +23,9 @@ public class GerritSshConnector implements GerritConnector {
18
23
private static final Logger LOG = Loggers .get (GerritSshConnector .class );
19
24
private static final String CMD_LIST_FILES = "gerrit query --format=JSON --files --current-patch-set status:open change:%s limit:1" ;
20
25
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
+
21
29
private final GerritConfiguration gerritConfiguration ;
22
30
private final UserAtHost userAtHost ;
23
31
@@ -31,7 +39,7 @@ public GerritSshConnector(GerritConfiguration gerritConfiguration) {
31
39
@ NotNull
32
40
@ Override
33
41
public String listFiles () throws IOException {
34
- SshClient sshClient = new JschSshClient ( gerritConfiguration . getSshKeyPath (), gerritConfiguration . getPassword () );
42
+ SshClient sshClient = getSshClient ( );
35
43
36
44
LOG .debug ("[GERRIT PLUGIN] Execute command SSH {}" ,
37
45
String .format (CMD_LIST_FILES , gerritConfiguration .getChangeId ()));
@@ -48,7 +56,7 @@ public String setReview(String reviewInputAsJson) throws IOException {
48
56
LOG .info ("[GERRIT PLUGIN] Setting review {}" , reviewInputAsJson );
49
57
50
58
ByteBuffer stdin = ByteBuffer .wrap (reviewInputAsJson .getBytes ("UTF-8" ));
51
- SshClient sshClient = new JschSshClient ( gerritConfiguration . getSshKeyPath (), gerritConfiguration . getPassword () );
59
+ SshClient sshClient = getSshClient ( );
52
60
53
61
LOG .debug ("[GERRIT PLUGIN] Execute command SSH {}" ,
54
62
String .format (CMD_SET_REVIEW , gerritConfiguration .getRevisionId ()));
@@ -58,4 +66,34 @@ public String setReview(String reviewInputAsJson) throws IOException {
58
66
59
67
return cmdResult .stdoutAsText ();
60
68
}
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
+
61
99
}
0 commit comments