Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ private void logHint(TaskListener listener) {
'Manage Jenkins' -> 'Security' -> 'Git Host Key Verification Configuration' \
and configure host key verification.\
"""));
LOGGER.log(
Level.FINEST,
"Known hosts file {0} not found, but verifying host keys with known hosts file",
new Object[] {SshHostKeyVerificationStrategy.KNOWN_HOSTS_DEFAULT});
LOGGER.finest("Verifying host keys with known hosts file, but known hosts file was not found");
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jenkinsci.plugins.gitclient.verifier;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.is;
import static org.jenkinsci.plugins.gitclient.verifier.KnownHostsTestUtil.nonGitHubHost;
import static org.jenkinsci.plugins.gitclient.verifier.KnownHostsTestUtil.runKnownHostsTests;
Expand All @@ -12,6 +13,12 @@
import hudson.model.TaskListener;
import java.io.File;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -39,6 +46,38 @@ void assignVerifiers() throws Exception {
fakeKnownHosts = knownHostsTestUtil.createFakeKnownHosts(FILE_CONTENT);
}

@Test
void missingKnownHostsShouldLogWarning() throws Exception {
assumeTrue(runKnownHostsTests());
File folder = new File(testFolder, "folder");
File missingKnownHosts = new File(folder, "non_existent_known_hosts");
KnownHostsFileVerifier knownHostsFileVerifier = spy(new KnownHostsFileVerifier());
when(knownHostsFileVerifier.getKnownHostsFile()).thenReturn(missingKnownHosts);

Logger logger = Logger.getLogger(KnownHostsFileVerifier.class.getName());
LogHandler handler = new LogHandler();
handler.setLevel(Level.ALL);
logger.setUseParentHandlers(false);
logger.addHandler(handler);
logger.setLevel(Level.ALL);
KnownHostsTestUtil.connectToHost(
nonGitHubHost(),
22,
missingKnownHosts,
knownHostsFileVerifier.forJGit(StreamBuildListener.fromStdout()),
"ssh-ed25519",
s -> {
assertThat(s.isOpen(), is(true));
Awaitility.await().atMost(Duration.ofSeconds(37)).until(() -> s.getServerKey() != null);
assertThat(KnownHostsTestUtil.checkKeys(s), is(false));
return true;
})
.close();
assertThat(
handler.getMessages(),
hasItem("Verifying host keys with known hosts file, but known hosts file was not found"));
}

@Test
void connectWhenHostKeyNotInKnownHostsFileForOtherHostNameThenShouldFail() throws Exception {
assumeTrue(runKnownHostsTests());
Expand Down Expand Up @@ -114,4 +153,26 @@ void testVerifyHostKeyOptionWithDefaultFile() throws Exception {
assertThat(
verifier.forCliGit(TaskListener.NULL).getVerifyHostKeyOption(null), is("-o StrictHostKeyChecking=yes"));
}

private static class LogHandler extends Handler {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(haven't tried myself modifying the code here) perhaps the LogRecorder utility added in jenkins-test-harness (relatively recent) could have worked ?
example - https://github.com/jenkinsci/jenkins-test-harness/blob/master/src/test/java/org/jvnet/hudson/test/LogRecorderTest.java


private List<String> messages = new ArrayList<>();

@Override
public void publish(LogRecord lr) {
messages.add(lr.getMessage());
}

@Override
public void flush() {}

@Override
public void close() throws SecurityException {
messages = new ArrayList<>();
}

List<String> getMessages() {
return messages;
}
}
}
Loading