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.junit.runner.RunWith
31 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertiesService
32 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertyConfiguration
33 import org.onap.ccsdk.cds.blueprintsprocessor.ssh.BluePrintSshLibConfiguration
34 import org.onap.ccsdk.cds.blueprintsprocessor.ssh.service.echoShell.EchoShellFactory
35 import org.springframework.beans.factory.annotation.Autowired
36 import org.springframework.test.context.ContextConfiguration
37 import org.springframework.test.context.TestPropertySource
38 import org.springframework.test.context.junit4.SpringRunner
39 import java.nio.file.Paths
40 import kotlin.test.AfterTest
41 import kotlin.test.BeforeTest
42 import kotlin.test.Test
43 import kotlin.test.assertEquals
44 import kotlin.test.assertNotNull
45 import kotlin.test.assertTrue
47 @RunWith(SpringRunner::class)
48 @ContextConfiguration(
50 BluePrintSshLibConfiguration::class,
51 BluePrintPropertyConfiguration::class, BluePrintPropertiesService::class
57 "blueprintsprocessor.sshclient.sample.type=basic-auth",
58 "blueprintsprocessor.sshclient.sample.host=localhost",
59 "blueprintsprocessor.sshclient.sample.port=52815",
60 "blueprintsprocessor.sshclient.sample.username=root",
61 "blueprintsprocessor.sshclient.sample.password=dummyps"
64 class BlueprintSshClientServiceTest {
67 lateinit var bluePrintSshLibPropertyService: BluePrintSshLibPropertyService
69 lateinit var bluePrintSshLibPropertyServiceMock: BluePrintSshLibPropertyService
71 private lateinit var sshServer: SshServer
74 fun startShellServer() {
76 println("Start local Shell server")
77 sshServer = setupTestServer("localhost", 52815, "root", "dummyps")
84 fun stopShellServer() {
85 println("End the Shell server")
90 fun testStartSessionNB() {
91 val clientSession = getSshClientService().startSession()
92 assertNotNull(clientSession, "Failed to start ssh session with server")
96 fun testBasicAuthSshClientService() {
98 val blueprintSshClientService = getSshClientService()
99 blueprintSshClientService.startSession()
100 // Preparing response
101 val commandResults = arrayListOf<CommandResult>()
102 commandResults.add(CommandResult("echo 1", "echo 1\n#", true))
103 commandResults.add(CommandResult("echo 2", "echo 1\n#echo 2\n#", true))
104 val response = blueprintSshClientService.executeCommands(arrayListOf("echo 1", "echo 2"), 2000)
105 blueprintSshClientService.closeSession()
107 assertEquals(response, commandResults, "failed to get command responses")
112 fun `testBasicAuthSshClientService single execution command`() {
114 val blueprintSshClientService = getSshClientService()
115 blueprintSshClientService.startSession()
116 val response = blueprintSshClientService.executeCommand("echo 1", 2000)
117 blueprintSshClientService.closeSession()
119 assertEquals(response, CommandResult("echo 1", "echo 1\n#", true), "failed to get command response")
124 fun testCloseSessionNB() {
125 val bluePrintSshLibPropertyService = bluePrintSshLibPropertyService.blueprintSshClientService("sample")
126 val clientSession = bluePrintSshLibPropertyService.startSession()
127 bluePrintSshLibPropertyService.closeSession()
128 assertTrue(clientSession.isClosed, "Failed to close ssh session with server")
131 private fun setupTestServer(host: String, port: Int, username: String, password: String): SshServer {
132 val sshd = SshServer.setUpDefaultServer()
135 sshd.keyPairProvider = createTestHostKeyProvider()
136 sshd.passwordAuthenticator = BogusPasswordAuthenticator(username, password)
137 sshd.publickeyAuthenticator = AcceptAllPublickeyAuthenticator.INSTANCE
138 sshd.shellFactory = EchoShellFactory.INSTANCE
139 sshd.commandFactory = ProcessShellCommandFactory.INSTANCE
143 private fun createTestHostKeyProvider(): KeyPairProvider {
144 val keyProvider = SimpleGeneratorHostKeyProvider()
145 keyProvider.path = Paths.get("target").resolve("hostkey." + RSA_ALGORITHM.toLowerCase())
146 keyProvider.algorithm = RSA_ALGORITHM
150 private fun getSshClientService(): BlueprintSshClientService {
151 return bluePrintSshLibPropertyService.blueprintSshClientService("sample")
155 class BogusPasswordAuthenticator(private val usr: String, private val pwd: String) : PasswordAuthenticator {
157 override fun authenticate(username: String, password: String, serverSession: ServerSession): Boolean {
158 assertEquals(username, usr, "failed to match username")
159 assertEquals(password, pwd, "failed to match password")