3785a21b2efa44119ee37ed093a8540c50e42302
[ccsdk/cds.git] /
1 /*
2  *  Copyright © 2019 IBM.
3  *
4  *  Modifications Copyright © 2018-2019 IBM, Bell Canada
5  *
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 package org.onap.ccsdk.cds.blueprintsprocessor.ssh.service
20
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
46
47 @RunWith(SpringRunner::class)
48 @ContextConfiguration(
49     classes = [BluePrintSshLibConfiguration::class,
50         BluePrintPropertyConfiguration::class, BluePrintPropertiesService::class]
51 )
52 @TestPropertySource(
53     properties =
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"
59     ]
60 )
61 class BlueprintSshClientServiceTest {
62
63     @Autowired
64     lateinit var bluePrintSshLibPropertyService: BluePrintSshLibPropertyService
65
66     lateinit var bluePrintSshLibPropertyServiceMock: BluePrintSshLibPropertyService
67
68     private lateinit var sshServer: SshServer
69
70     @BeforeTest
71     fun startShellServer() {
72         runBlocking {
73             println("Start local Shell server")
74             sshServer = setupTestServer("localhost", 52815, "root", "dummyps")
75             sshServer.start()
76             println(sshServer)
77         }
78     }
79
80     @AfterTest
81     fun stopShellServer() {
82         println("End the Shell server")
83         sshServer.stop(true)
84     }
85
86     @Test
87     fun testStartSessionNB() {
88         val clientSession = getSshClientService().startSession()
89         assertNotNull(clientSession, "Failed to start ssh session with server")
90     }
91
92     @Test
93     fun testBasicAuthSshClientService() {
94         runBlocking {
95             val blueprintSshClientService = getSshClientService()
96             blueprintSshClientService.startSession()
97             // Preparing response
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()
103
104             assertEquals(response, commandResults, "failed to get command responses")
105         }
106     }
107
108     @Test
109     fun `testBasicAuthSshClientService single execution command`() {
110         runBlocking {
111             val blueprintSshClientService = getSshClientService()
112             blueprintSshClientService.startSession()
113             val response = blueprintSshClientService.executeCommand("echo 1", 2000)
114             blueprintSshClientService.closeSession()
115
116             assertEquals(response, CommandResult("echo 1", "echo 1\n#", true), "failed to get command response")
117         }
118     }
119
120     @Test
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")
126     }
127
128     private fun setupTestServer(host: String, port: Int, username: String, password: String): SshServer {
129         val sshd = SshServer.setUpDefaultServer()
130         sshd.port = port
131         sshd.host = host
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
137         return sshd
138     }
139
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
144         return keyProvider
145     }
146
147     private fun getSshClientService(): BlueprintSshClientService {
148         return bluePrintSshLibPropertyService.blueprintSshClientService("sample")
149     }
150 }
151
152 class BogusPasswordAuthenticator(private val usr: String, private val pwd: String) : PasswordAuthenticator {
153
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")
157         return true
158     }
159 }