11package fr .techad .sonar .gerrit ;
22
3+ import java .io .File ;
34import java .io .IOException ;
45import java .nio .ByteBuffer ;
6+ import java .nio .file .Files ;
7+ import java .nio .file .LinkOption ;
8+ import java .nio .file .Paths ;
59
610import fi .jpalomaki .ssh .Result ;
711import fi .jpalomaki .ssh .SshClient ;
812import fi .jpalomaki .ssh .UserAtHost ;
913import fi .jpalomaki .ssh .jsch .JschSshClient ;
14+ import fi .jpalomaki .ssh .jsch .JschSshClient .Options ;
1015
1116import org .jetbrains .annotations .NotNull ;
1217import 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