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
41 @RunWith(SpringRunner::class)
42 @ContextConfiguration(
43 classes = [BluePrintSshLibConfiguration::class,
44 BluePrintPropertyConfiguration::class, BluePrintPropertiesService::class]
48 ["blueprintsprocessor.sshclient.sample.type=basic-auth",
49 "blueprintsprocessor.sshclient.sample.host=localhost",
50 "blueprintsprocessor.sshclient.sample.port=52815",
51 "blueprintsprocessor.sshclient.sample.username=root",
52 "blueprintsprocessor.sshclient.sample.password=dummyps"
55 class BlueprintSshClientServiceTest {
58 lateinit var bluePrintSshLibPropertyService: BluePrintSshLibPropertyService
61 fun testBasicAuthSshClientService() {
63 val sshServer = setupTestServer("localhost", 52815, "root", "dummyps")
66 val bluePrintSshLibPropertyService = bluePrintSshLibPropertyService.blueprintSshClientService("sample")
67 val sshSession = bluePrintSshLibPropertyService.startSession()
68 val response = bluePrintSshLibPropertyService.executeCommandsNB(arrayListOf("echo '1'", "echo '2'"), 2000)
69 assertNotNull(response, "failed to get command response")
70 bluePrintSshLibPropertyService.closeSession()
75 private fun setupTestServer(host: String, port: Int, userName: String, password: String): SshServer {
76 val sshd = SshServer.setUpDefaultServer()
79 sshd.keyPairProvider = createTestHostKeyProvider()
80 sshd.passwordAuthenticator = BogusPasswordAuthenticator(userName, password)
81 sshd.publickeyAuthenticator = AcceptAllPublickeyAuthenticator.INSTANCE
82 // sshd.shellFactory = EchoShellFactory()
83 sshd.commandFactory = ProcessShellCommandFactory.INSTANCE
87 private fun createTestHostKeyProvider(): KeyPairProvider {
88 val keyProvider = SimpleGeneratorHostKeyProvider()
89 keyProvider.path = Paths.get("target").resolve("hostkey." + RSA_ALGORITHM.toLowerCase())
90 keyProvider.algorithm = RSA_ALGORITHM
95 class BogusPasswordAuthenticator(userName: String, password: String) : PasswordAuthenticator {
96 override fun authenticate(username: String, password: String, serverSession: ServerSession): Boolean {
97 assertEquals(username, "root", "failed to match username")
98 assertEquals(password, "dummyps", "failed to match password")