2 * Copyright © 2019 IBM.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.blueprintsprocessor.ssh.service
19 import kotlinx.coroutines.runBlocking
20 import org.apache.sshd.common.config.keys.KeyUtils.RSA_ALGORITHM
21 import org.apache.sshd.common.keyprovider.KeyPairProvider
22 import org.apache.sshd.server.SshServer
23 import org.apache.sshd.server.auth.password.PasswordAuthenticator
24 import org.apache.sshd.server.auth.pubkey.AcceptAllPublickeyAuthenticator
25 import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider
26 import org.apache.sshd.server.session.ServerSession
27 import org.apache.sshd.server.shell.ProcessShellCommandFactory
28 import org.junit.runner.RunWith
29 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertiesService
30 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertyConfiguration
31 import org.onap.ccsdk.cds.blueprintsprocessor.ssh.BluePrintSshLibConfiguration
32 import org.springframework.beans.factory.annotation.Autowired
33 import org.springframework.test.context.ContextConfiguration
34 import org.springframework.test.context.TestPropertySource
35 import org.springframework.test.context.junit4.SpringRunner
36 import java.nio.file.Paths
37 import kotlin.test.Test
38 import kotlin.test.assertEquals
39 import kotlin.test.assertNotNull
42 @RunWith(SpringRunner::class)
43 @ContextConfiguration(classes = [BluePrintSshLibConfiguration::class,
44 BluePrintPropertyConfiguration::class, BluePrintPropertiesService::class])
45 @TestPropertySource(properties =
46 ["blueprintsprocessor.sshclient.sample.type=basic-auth",
47 "blueprintsprocessor.sshclient.sample.host=localhost",
48 "blueprintsprocessor.sshclient.sample.port=52815",
49 "blueprintsprocessor.sshclient.sample.username=root",
50 "blueprintsprocessor.sshclient.sample.password=dummyps"
52 class BlueprintSshClientServiceTest {
55 lateinit var bluePrintSshLibPropertyService: BluePrintSshLibPropertyService
58 fun testBasicAuthSshClientService() {
60 val sshServer = setupTestServer("localhost", 52815, "root", "dummyps")
63 val bluePrintSshLibPropertyService = bluePrintSshLibPropertyService.blueprintSshClientService("sample")
64 val sshSession = bluePrintSshLibPropertyService.startSession()
65 val response = bluePrintSshLibPropertyService.executeCommandsNB(arrayListOf("echo '1'", "echo '2'"), 2000)
66 assertNotNull(response, "failed to get command response")
67 bluePrintSshLibPropertyService.closeSession()
72 private fun setupTestServer(host: String, port: Int, userName: String, password: String): SshServer {
73 val sshd = SshServer.setUpDefaultServer()
76 sshd.keyPairProvider = createTestHostKeyProvider()
77 sshd.passwordAuthenticator = BogusPasswordAuthenticator(userName, password)
78 sshd.publickeyAuthenticator = AcceptAllPublickeyAuthenticator.INSTANCE
79 //sshd.shellFactory = EchoShellFactory()
80 sshd.commandFactory = ProcessShellCommandFactory.INSTANCE
84 private fun createTestHostKeyProvider(): KeyPairProvider {
85 val keyProvider = SimpleGeneratorHostKeyProvider()
86 keyProvider.path = Paths.get("target").resolve("hostkey." + RSA_ALGORITHM.toLowerCase())
87 keyProvider.algorithm = RSA_ALGORITHM
92 class BogusPasswordAuthenticator(userName: String, password: String) : PasswordAuthenticator {
93 override fun authenticate(username: String, password: String, serverSession: ServerSession): Boolean {
94 assertEquals(username, "root", "failed to match username")
95 assertEquals(password, "dummyps", "failed to match password")