Skip to content

Commit 6eb1f90

Browse files
Split KeyWithCertificateSpec into HostKeyWithCertificateSpec and PublicKeyAuthWithCertificateSpec
Prevents from starting unnecessary SSHD containers, making the tests run a bit faster when they are launched separately.
1 parent 73e10d5 commit 6eb1f90

File tree

2 files changed

+84
-55
lines changed

2 files changed

+84
-55
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright (C)2009 - SSHJ Contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.hierynomus.sshj.signature
17+
18+
import com.hierynomus.sshj.SshdContainer
19+
import net.schmizz.sshj.DefaultConfig
20+
import net.schmizz.sshj.SSHClient
21+
import net.schmizz.sshj.transport.verification.OpenSSHKnownHosts
22+
import spock.lang.Specification
23+
import spock.lang.Unroll
24+
25+
import java.nio.file.Files
26+
27+
/**
28+
* This is a brief test for verifying connection to a server using keys with certificates.
29+
*
30+
* Also, take a look at the unit test {@link net.schmizz.sshj.transport.verification.KeyWithCertificateUnitSpec}.
31+
*/
32+
class HostKeyWithCertificateSpec extends Specification {
33+
@Unroll
34+
def "accepting a signed host public key #hostKey"() {
35+
given:
36+
SshdContainer sshd = new SshdContainer.Builder()
37+
.withSshdConfig("""
38+
PasswordAuthentication yes
39+
HostKey /etc/ssh/$hostKey
40+
HostCertificate /etc/ssh/${hostKey}-cert.pub
41+
""".stripMargin())
42+
.build()
43+
sshd.start()
44+
45+
and:
46+
File knownHosts = Files.createTempFile("known_hosts", "").toFile()
47+
knownHosts.deleteOnExit()
48+
49+
and:
50+
File caPubKey = new File("src/itest/resources/keyfiles/certificates/CA_rsa.pem.pub")
51+
def address = "127.0.0.1"
52+
String knownHostsFileContents = "" +
53+
"@cert-authority ${ address} ${caPubKey.text}" +
54+
"\n@cert-authority [${address}]:${sshd.firstMappedPort} ${caPubKey.text}"
55+
knownHosts.write(knownHostsFileContents)
56+
57+
and:
58+
SSHClient sshClient = new SSHClient(new DefaultConfig())
59+
sshClient.addHostKeyVerifier(new OpenSSHKnownHosts(knownHosts))
60+
sshClient.connect(address, sshd.firstMappedPort)
61+
62+
when:
63+
sshClient.authPassword("sshj", "ultrapassword")
64+
65+
then:
66+
sshClient.authenticated
67+
68+
and:
69+
knownHosts.getText() == knownHostsFileContents
70+
71+
cleanup:
72+
sshd.stop()
73+
74+
where:
75+
hostKey << [
76+
"ssh_host_ecdsa_256_key",
77+
"ssh_host_ecdsa_384_key",
78+
"ssh_host_ecdsa_521_key",
79+
"ssh_host_ed25519_384_key",
80+
"ssh_host_rsa_2048_key",
81+
]
82+
}
83+
}

src/itest/groovy/com/hierynomus/sshj/signature/KeyWithCertificateSpec.groovy renamed to src/itest/groovy/com/hierynomus/sshj/signature/PublicKeyAuthWithCertificateSpec.groovy

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,18 @@ package com.hierynomus.sshj.signature
1818
import com.hierynomus.sshj.SshdContainer
1919
import net.schmizz.sshj.DefaultConfig
2020
import net.schmizz.sshj.SSHClient
21-
import net.schmizz.sshj.transport.verification.OpenSSHKnownHosts
2221
import net.schmizz.sshj.transport.verification.PromiscuousVerifier
2322
import org.junit.ClassRule
2423
import spock.lang.Shared
2524
import spock.lang.Specification
2625
import spock.lang.Unroll
2726

28-
import java.nio.file.Files
29-
3027
/**
3128
* This is a brief test for verifying connection to a server using keys with certificates.
3229
*
3330
* Also, take a look at the unit test {@link net.schmizz.sshj.transport.verification.KeyWithCertificateUnitSpec}.
3431
*/
35-
class KeyWithCertificateSpec extends Specification {
32+
class PublicKeyAuthWithCertificateSpec extends Specification {
3633
@Shared
3734
@ClassRule
3835
SshdContainer sshd
@@ -81,55 +78,4 @@ class KeyWithCertificateSpec extends Specification {
8178
"id_ed25519_384_rfc4716_signed_by_rsa",
8279
]
8380
}
84-
85-
@Unroll
86-
def "accepting a signed host public key #hostKey"() {
87-
given:
88-
SshdContainer sshd = new SshdContainer.Builder()
89-
.withSshdConfig("""
90-
PasswordAuthentication yes
91-
HostKey /etc/ssh/$hostKey
92-
HostCertificate /etc/ssh/${hostKey}-cert.pub
93-
""".stripMargin())
94-
.build()
95-
sshd.start()
96-
97-
and:
98-
File knownHosts = Files.createTempFile("known_hosts", "").toFile()
99-
knownHosts.deleteOnExit()
100-
101-
and:
102-
File caPubKey = new File("src/itest/resources/keyfiles/certificates/CA_rsa.pem.pub")
103-
def address = "127.0.0.1"
104-
String knownHostsFileContents = "" +
105-
"@cert-authority ${ address} ${caPubKey.text}" +
106-
"\n@cert-authority [${address}]:${sshd.firstMappedPort} ${caPubKey.text}"
107-
knownHosts.write(knownHostsFileContents)
108-
109-
and:
110-
SSHClient sshClient = new SSHClient(new DefaultConfig())
111-
sshClient.addHostKeyVerifier(new OpenSSHKnownHosts(knownHosts))
112-
sshClient.connect(address, sshd.firstMappedPort)
113-
114-
when:
115-
sshClient.authPassword("sshj", "ultrapassword")
116-
117-
then:
118-
sshClient.authenticated
119-
120-
and:
121-
knownHosts.getText() == knownHostsFileContents
122-
123-
cleanup:
124-
sshd.stop()
125-
126-
where:
127-
hostKey << [
128-
"ssh_host_ecdsa_256_key",
129-
"ssh_host_ecdsa_384_key",
130-
"ssh_host_ecdsa_521_key",
131-
"ssh_host_ed25519_384_key",
132-
"ssh_host_rsa_2048_key",
133-
]
134-
}
13581
}

0 commit comments

Comments
 (0)