Skip to content

Commit

Permalink
Skip ssh host tests on ci.jenkins.io (#1255)
Browse files Browse the repository at this point in the history
The GitHub.com SSH abuse prevention system rejects connections from
the tests when they are run on ci.jenkins.io.  These tests should only
be run as end to end acceptance tests but they are not too disruptive
(for now) to continue to be included in the default tests.

Additional tests should be created that use test containers so that they
can use the ssh connection on the test container instead of using the
GitHub.com SSH servers.

Unfortunately, I can't prioritize that test rewrite now, so these tests
are disabled on ci.jenkins.io.

(cherry picked from commit 2d90e37)
  • Loading branch information
MarkEWaite committed Feb 14, 2025
1 parent cf16f3d commit 27cd7c0
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.io.FileMatchers.anExistingFile;
import static org.jenkinsci.plugins.gitclient.verifier.KnownHostsTestUtil.runKnownHostsTests;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

Expand All @@ -17,6 +18,7 @@
import java.util.Collections;
import java.util.List;
import org.awaitility.Awaitility;
import org.junit.Assume;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -46,13 +48,15 @@ public class AcceptFirstConnectionVerifierTest {

@Test
public void testVerifyHostKeyOption() throws IOException {
Assume.assumeTrue(runKnownHostsTests());
assertThat(
new AcceptFirstConnectionVerifier().forCliGit(TaskListener.NULL).getVerifyHostKeyOption(null),
is("-o StrictHostKeyChecking=accept-new -o HashKnownHosts=yes"));
}

@Test
public void testVerifyServerHostKeyWhenFirstConnection() throws Exception {
Assume.assumeTrue(runKnownHostsTests());
File file = new File(testFolder.getRoot() + "path/to/file");
AcceptFirstConnectionVerifier acceptFirstConnectionVerifier = spy(new AcceptFirstConnectionVerifier());
when(acceptFirstConnectionVerifier.getKnownHostsFile()).thenReturn(file);
Expand All @@ -77,6 +81,7 @@ public void testVerifyServerHostKeyWhenFirstConnection() throws Exception {

@Test
public void testVerifyServerHostKeyWhenSecondConnectionWithEqualKeys() throws Exception {
Assume.assumeTrue(runKnownHostsTests());
String hostKeyEntry =
"|1|FJGXVAi7jMQIsl1J6uE6KnCiteM=|xlH92KQ91GuBgRxvRbU/sBo60Bo= ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBEmKSENjQEezOmxkZMy7opKgwFB9nkt5YRrYMjNuG5N87uRgg6CLrbo5wAdT/y6v0mKV0U2w0WZ2YB/++Tpockg=";

Expand Down Expand Up @@ -105,6 +110,7 @@ public void testVerifyServerHostKeyWhenSecondConnectionWithEqualKeys() throws Ex

@Test
public void testVerifyServerHostKeyWhenHostnameWithoutPort() throws Exception {
Assume.assumeTrue(runKnownHostsTests());
String hostKeyEntry =
"github.com ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBEmKSENjQEezOmxkZMy7opKgwFB9nkt5YRrYMjNuG5N87uRgg6CLrbo5wAdT/y6v0mKV0U2w0WZ2YB/++Tpockg=";
File mockedKnownHosts = knownHostsTestUtil.createFakeKnownHosts(hostKeyEntry);
Expand All @@ -128,6 +134,7 @@ public void testVerifyServerHostKeyWhenHostnameWithoutPort() throws Exception {

@Test
public void testVerifyServerHostKeyWhenSecondConnectionWhenNotDefaultAlgorithm() throws Exception {
Assume.assumeTrue(runKnownHostsTests());
String fileContent =
"""
github.com,140.82.121.4\
Expand Down Expand Up @@ -158,6 +165,7 @@ public void testVerifyServerHostKeyWhenSecondConnectionWhenNotDefaultAlgorithm()
@Test
@Ignore("FIXME not sure what is the test here")

Check warning on line 166 in src/test/java/org/jenkinsci/plugins/gitclient/verifier/AcceptFirstConnectionVerifierTest.java

View check run for this annotation

ci.jenkins.io / Open Tasks Scanner

FIXME

HIGH: not sure what is the test here")
public void testVerifyServerHostKeyWhenSecondConnectionWithNonEqualKeys() throws Exception {
Assume.assumeTrue(runKnownHostsTests());
String fileContent =
"""
|1|f7esvmtaiBk+EMHjPzWbRYRpBPY=|T7Qe4QAksYPZPwYEx5QxQykSjfc=\
Expand Down Expand Up @@ -188,6 +196,7 @@ public void testVerifyServerHostKeyWhenSecondConnectionWithNonEqualKeys() throws

@Test
public void testVerifyServerHostKeyWhenConnectionWithAnotherHost() throws Exception {
Assume.assumeTrue(runKnownHostsTests());
String bitbucketFileContent =
"""
|1|HnmPCP38pBhCY0NUtBXSraOg9pM=|L6YZ9asEeb2xplTDEThGOxRq7ZY=\
Expand Down Expand Up @@ -220,6 +229,7 @@ public void testVerifyServerHostKeyWhenConnectionWithAnotherHost() throws Except

@Test
public void testVerifyServerHostKeyWhenHostnamePortProvided() throws Exception {
Assume.assumeTrue(runKnownHostsTests());
String fileContent =
"""
|1|6uMj3M7sLgZpn54vQbGqgPNTCVM=|OkV9Lu9REJZR5QCVrITAIY34I1M=\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public void connectWhenHostKeyNotInKnownHostsFileForOtherHostNameThenShouldFail(

@Test
public void connectWhenHostKeyProvidedThenShouldNotFail() throws IOException {
Assume.assumeTrue(runKnownHostsTests());
KnownHostsFileVerifier knownHostsFileVerifier = spy(new KnownHostsFileVerifier());
when(knownHostsFileVerifier.getKnownHostsFile()).thenReturn(fakeKnownHosts);

Expand All @@ -84,6 +85,7 @@ public void connectWhenHostKeyProvidedThenShouldNotFail() throws IOException {

@Test
public void connectWhenHostKeyInKnownHostsFileWithNotDefaultAlgorithmThenShouldNotFail() throws IOException {
Assume.assumeTrue(runKnownHostsTests());
fakeKnownHosts = knownHostsTestUtil.createFakeKnownHosts(
"fake2.ssh",
"known_hosts_fake2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public void connectWhenHostKeyProvidedForOtherHostNameThenShouldFail() throws Ex

@Test
public void connectWhenHostKeyProvidedThenShouldNotFail() throws Exception {
Assume.assumeTrue(runKnownHostsTests());
ManuallyProvidedKeyVerifier verifier = new ManuallyProvidedKeyVerifier(hostKey);
ManuallyProvidedKeyVerifier.ManuallyProvidedKeyJGitHostKeyVerifier jGitHostKeyVerifier =
(ManuallyProvidedKeyVerifier.ManuallyProvidedKeyJGitHostKeyVerifier)
Expand All @@ -78,6 +79,7 @@ public void connectWhenHostKeyProvidedThenShouldNotFail() throws Exception {

@Test
public void connectWhenWrongHostKeyProvidedThenShouldFail() throws Exception {
Assume.assumeTrue(runKnownHostsTests());
String key = "github.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9OOOO";
HostKeyVerifierFactory verifier = new ManuallyProvidedKeyVerifier(key);

Expand All @@ -97,6 +99,7 @@ public void connectWhenWrongHostKeyProvidedThenShouldFail() throws Exception {

@Test
public void connectWhenHostKeyProvidedWithPortThenShouldNotFail() throws Exception {
Assume.assumeTrue(runKnownHostsTests());
String key =
"github.com:22 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBEmKSENjQEezOmxkZMy7opKgwFB9nkt5YRrYMjNuG5N87uRgg6CLrbo5wAdT/y6v0mKV0U2w0WZ2YB/++Tpockg=";
HostKeyVerifierFactory verifier = new ManuallyProvidedKeyVerifier(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.jenkinsci.plugins.gitclient.verifier.KnownHostsTestUtil.runKnownHostsTests;

import hudson.model.StreamBuildListener;
import hudson.model.TaskListener;
import java.io.IOException;
import java.nio.file.Path;
import java.time.Duration;
import org.awaitility.Awaitility;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;

Expand All @@ -23,6 +25,7 @@ public void assignVerifier() {

@Test
public void verifyServerHostKey() throws IOException {
Assume.assumeTrue(runKnownHostsTests());
NoHostKeyVerifier acceptFirstConnectionVerifier = new NoHostKeyVerifier();

KnownHostsTestUtil.connectToHost(
Expand Down

0 comments on commit 27cd7c0

Please sign in to comment.