683816f7f3aa303e6841a25d0e411dffb3d3ac95
[ccsdk/cds.git] /
1 /*
2  *  Copyright © 2019 IBM.
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
17 package org.onap.ccsdk.cds.blueprintsprocessor.ssh.service
18
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
40
41 @RunWith(SpringRunner::class)
42 @ContextConfiguration(
43     classes = [BluePrintSshLibConfiguration::class,
44         BluePrintPropertyConfiguration::class, BluePrintPropertiesService::class]
45 )
46 @TestPropertySource(
47     properties =
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"
53     ]
54 )
55 class BlueprintSshClientServiceTest {
56
57     @Autowired
58     lateinit var bluePrintSshLibPropertyService: BluePrintSshLibPropertyService
59
60     @Test
61     fun testBasicAuthSshClientService() {
62         runBlocking {
63             val sshServer = setupTestServer("localhost", 52815, "root", "dummyps")
64             sshServer.start()
65             println(sshServer)
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()
71             sshServer.stop(true)
72         }
73     }
74
75     private fun setupTestServer(host: String, port: Int, userName: String, password: String): SshServer {
76         val sshd = SshServer.setUpDefaultServer()
77         sshd.port = port
78         sshd.host = host
79         sshd.keyPairProvider = createTestHostKeyProvider()
80         sshd.passwordAuthenticator = BogusPasswordAuthenticator(userName, password)
81         sshd.publickeyAuthenticator = AcceptAllPublickeyAuthenticator.INSTANCE
82         // sshd.shellFactory = EchoShellFactory()
83         sshd.commandFactory = ProcessShellCommandFactory.INSTANCE
84         return sshd
85     }
86
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
91         return keyProvider
92     }
93 }
94
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")
99         return true
100     }
101 }