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 org.apache.sshd.client.SshClient
20 import org.apache.sshd.client.channel.ChannelExec
21 import org.apache.sshd.client.channel.ClientChannel
22 import org.apache.sshd.client.channel.ClientChannelEvent
23 import org.apache.sshd.client.keyverifier.AcceptAllServerKeyVerifier
24 import org.apache.sshd.client.session.ClientSession
25 import org.onap.ccsdk.cds.blueprintsprocessor.ssh.BasicAuthSshClientProperties
26 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
27 import org.slf4j.LoggerFactory
28 import java.io.ByteArrayOutputStream
29 import java.util.Collections
30 import java.util.EnumSet
32 open class BasicAuthSshClientService(private val basicAuthSshClientProperties: BasicAuthSshClientProperties) :
33 BlueprintSshClientService {
35 private val log = LoggerFactory.getLogger(BasicAuthSshClientService::class.java)!!
37 private lateinit var sshClient: SshClient
38 private lateinit var clientSession: ClientSession
39 var channel: ChannelExec? = null
41 override suspend fun startSessionNB(): ClientSession {
42 sshClient = SshClient.setUpDefaultClient()
43 sshClient.serverKeyVerifier = AcceptAllServerKeyVerifier.INSTANCE
45 log.debug("SSH Client Service started successfully")
46 clientSession = sshClient.connect(
47 basicAuthSshClientProperties.username, basicAuthSshClientProperties.host,
48 basicAuthSshClientProperties.port
50 .verify(basicAuthSshClientProperties.connectionTimeOut)
53 clientSession.addPasswordIdentity(basicAuthSshClientProperties.password)
54 clientSession.auth().verify(basicAuthSshClientProperties.connectionTimeOut)
55 log.info("SSH client session($clientSession) created")
59 override suspend fun executeCommandsNB(commands: List<String>, timeOut: Long): String {
60 val buffer = StringBuffer()
62 commands.forEach { command ->
63 buffer.append("\nCommand : $command")
64 buffer.append("\n" + executeCommandNB(command, timeOut))
66 } catch (e: Exception) {
67 throw BluePrintProcessorException("Failed to execute commands, below the output : $buffer")
69 return buffer.toString()
72 override suspend fun executeCommandNB(command: String, timeOut: Long): String {
73 log.debug("Executing host($clientSession) command($command)")
75 channel = clientSession.createExecChannel(command)
76 checkNotNull(channel) { "failed to create Channel for the command : $command" }
78 // TODO("Convert to streaming ")
79 val outputStream = ByteArrayOutputStream()
80 channel!!.out = outputStream
81 channel!!.err = outputStream
82 channel!!.open().await()
83 val waitMask = channel!!.waitFor(Collections.unmodifiableSet(EnumSet.of(ClientChannelEvent.CLOSED)), timeOut)
84 if (waitMask.contains(ClientChannelEvent.TIMEOUT)) {
85 throw BluePrintProcessorException("Failed to retrieve command result in time: $command")
87 val exitStatus = channel!!.exitStatus
88 ClientChannel.validateCommandExitStatusCode(command, exitStatus!!)
89 return outputStream.toString()
92 override suspend fun closeSessionNB() {
95 if (sshClient.isStarted) {
98 log.debug("SSH Client Service stopped successfully")