2 * Copyright © 2019 IBM.
4 * Modifications Copyright © 2018-2019 IBM, Bell Canada
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 package org.onap.ccsdk.cds.blueprintsprocessor.ssh.service
21 import kotlinx.coroutines.runBlocking
22 import org.apache.sshd.common.config.keys.KeyUtils.RSA_ALGORITHM
23 import org.apache.sshd.common.keyprovider.KeyPairProvider
24 import org.apache.sshd.server.SshServer
25 import org.apache.sshd.server.auth.password.PasswordAuthenticator
26 import org.apache.sshd.server.auth.pubkey.AcceptAllPublickeyAuthenticator
27 import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider
28 import org.apache.sshd.server.session.ServerSession
29 import org.apache.sshd.server.shell.ProcessShellCommandFactory
30 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertiesService
31 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertyConfiguration
32 import org.onap.ccsdk.cds.blueprintsprocessor.ssh.BluePrintSshLibConfiguration
33 import org.onap.ccsdk.cds.blueprintsprocessor.ssh.service.echoShell.EchoShellFactory
34 import org.springframework.beans.factory.annotation.Autowired
35 import org.springframework.test.context.ContextConfiguration
36 import org.springframework.test.context.TestPropertySource
37 import org.springframework.test.context.junit4.SpringRunner
38 import java.nio.file.Paths
39 import org.junit.runner.RunWith
40 import kotlin.test.BeforeTest
41 import kotlin.test.AfterTest
42 import kotlin.test.Test
43 import kotlin.test.assertTrue
44 import kotlin.test.assertNotNull
45 import kotlin.test.assertEquals
47 @RunWith(SpringRunner::class)
48 @ContextConfiguration(
49 classes = [BluePrintSshLibConfiguration::class,
50 BluePrintPropertyConfiguration::class, BluePrintPropertiesService::class]
54 ["blueprintsprocessor.sshclient.sample.type=basic-auth",
55 "blueprintsprocessor.sshclient.sample.host=localhost",
56 "blueprintsprocessor.sshclient.sample.port=52815",
57 "blueprintsprocessor.sshclient.sample.username=root",
58 "blueprintsprocessor.sshclient.sample.password=dummyps"
61 class BlueprintSshClientServiceTest {
64 lateinit var bluePrintSshLibPropertyService: BluePrintSshLibPropertyService
66 lateinit var bluePrintSshLibPropertyServiceMock: BluePrintSshLibPropertyService
68 private lateinit var sshServer: SshServer
71 fun startShellServer() {
73 println("Start local Shell server")
74 sshServer = setupTestServer("localhost", 52815, "root", "dummyps")
81 fun stopShellServer() {
82 println("End the Shell server")
87 fun testStartSessionNB() {
88 val clientSession = getSshClientService().startSession()
89 assertNotNull(clientSession, "Failed to start ssh session with server")
93 fun testBasicAuthSshClientService() {
95 val blueprintSshClientService = getSshClientService()
96 blueprintSshClientService.startSession()
98 val commandResults = arrayListOf<CommandResult>()
99 commandResults.add(CommandResult("echo 1", "echo 1\n#", true))
100 commandResults.add(CommandResult("echo 2", "echo 1\n#echo 2\n#", true))
101 val response = blueprintSshClientService.executeCommands(arrayListOf("echo 1", "echo 2"), 2000)
102 blueprintSshClientService.closeSession()
104 assertEquals(response, commandResults, "failed to get command responses")
109 fun `testBasicAuthSshClientService single execution command`() {
111 val blueprintSshClientService = getSshClientService()
112 blueprintSshClientService.startSession()
113 val response = blueprintSshClientService.executeCommand("echo 1", 2000)
114 blueprintSshClientService.closeSession()
116 assertEquals(response, CommandResult("echo 1", "echo 1\n#", true), "failed to get command response")
121 fun testCloseSessionNB() {
122 val bluePrintSshLibPropertyService = bluePrintSshLibPropertyService.blueprintSshClientService("sample")
123 val clientSession = bluePrintSshLibPropertyService.startSession()
124 bluePrintSshLibPropertyService.closeSession()
125 assertTrue(clientSession.isClosed, "Failed to close ssh session with server")
128 private fun setupTestServer(host: String, port: Int, username: String, password: String): SshServer {
129 val sshd = SshServer.setUpDefaultServer()
132 sshd.keyPairProvider = createTestHostKeyProvider()
133 sshd.passwordAuthenticator = BogusPasswordAuthenticator(username, password)
134 sshd.publickeyAuthenticator = AcceptAllPublickeyAuthenticator.INSTANCE
135 sshd.shellFactory = EchoShellFactory.INSTANCE
136 sshd.commandFactory = ProcessShellCommandFactory.INSTANCE
140 private fun createTestHostKeyProvider(): KeyPairProvider {
141 val keyProvider = SimpleGeneratorHostKeyProvider()
142 keyProvider.path = Paths.get("target").resolve("hostkey." + RSA_ALGORITHM.toLowerCase())
143 keyProvider.algorithm = RSA_ALGORITHM
147 private fun getSshClientService(): BlueprintSshClientService {
148 return bluePrintSshLibPropertyService.blueprintSshClientService("sample")
152 class BogusPasswordAuthenticator(private val usr: String, private val pwd: String) : PasswordAuthenticator {
154 override fun authenticate(username: String, password: String, serverSession: ServerSession): Boolean {
155 assertEquals(username, usr, "failed to match username")
156 assertEquals(password, pwd, "failed to match password")