reduce CDS java security vulnerabilities
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / ssh-lib / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / ssh / service / echoShell / EchoShellFactory.kt
1 /*
2  *  Copyright © 2019 IBM. Bell Canada
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.echoShell
18
19 import org.apache.sshd.common.Factory
20 import org.apache.sshd.server.Environment
21 import org.apache.sshd.server.ExitCallback
22 import org.apache.sshd.server.channel.ChannelSession
23 import org.apache.sshd.server.command.Command
24 import org.apache.sshd.server.shell.ShellFactory
25 import java.io.BufferedReader
26 import java.io.IOException
27 import java.io.InputStream
28 import java.io.InputStreamReader
29 import java.io.InterruptedIOException
30 import java.io.OutputStream
31
32 class EchoShellFactory : Factory<Command>, ShellFactory {
33
34     override fun create(): Command {
35         return EchoShell()
36     }
37
38     override fun createShell(channel: ChannelSession?): Command {
39         return EchoShell()
40     }
41
42     companion object {
43
44         val INSTANCE = EchoShellFactory()
45     }
46 }
47
48 class EchoShell : Command, Runnable {
49
50     var `in`: InputStream? = null
51         private set
52     var out: OutputStream? = null
53         private set
54     var err: OutputStream? = null
55         private set
56     private var callback: ExitCallback? = null
57     var environment: Environment? = null
58         private set
59     private var thread: Thread? = null
60
61     override fun setInputStream(`in`: InputStream) {
62         this.`in` = `in`
63     }
64
65     override fun setOutputStream(out: OutputStream) {
66         this.out = out
67     }
68
69     override fun setErrorStream(err: OutputStream) {
70         this.err = err
71     }
72
73     override fun setExitCallback(callback: ExitCallback) {
74         this.callback = callback
75     }
76
77     @Throws(IOException::class)
78     override fun start(channel: ChannelSession?, env: Environment?) {
79         environment = env
80         thread = Thread(this, "EchoShell")
81         thread!!.isDaemon = true
82         thread!!.start()
83     }
84
85     override fun destroy(channel: ChannelSession?) {
86         thread!!.interrupt()
87     }
88
89     override fun run() {
90         val r = BufferedReader(InputStreamReader(`in`))
91         try {
92             while (true) {
93                 val s = r.readLine() ?: return
94                 out!!.write((s + "\n").toByteArray())
95                 out!!.write("#".toByteArray())
96                 out!!.flush()
97                 if ("exit" == s) {
98                     return
99                 }
100             }
101         } catch (e: InterruptedIOException) {
102             // Ignore
103         } catch (e: Exception) {
104             e.printStackTrace()
105         } finally {
106             callback!!.onExit(0)
107         }
108     }
109 }