All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.cloudslang.content.services.PowerShellScriptServiceImpl Maven / Gradle / Ivy

The newest version!
package io.cloudslang.content.services;

import com.xebialabs.overthere.CmdLine;
import com.xebialabs.overthere.ConnectionOptions;
import com.xebialabs.overthere.Overthere;
import com.xebialabs.overthere.OverthereConnection;
import com.xebialabs.overthere.cifs.CifsConnectionBuilder;
import com.xebialabs.overthere.cifs.CifsConnectionType;
import com.xebialabs.overthere.cifs.WinrmHttpsCertificateTrustStrategy;
import com.xebialabs.overthere.cifs.WinrmHttpsHostnameVerificationStrategy;
import com.xebialabs.overthere.util.CapturingOverthereExecutionOutputHandler;
import io.cloudslang.content.entities.PowerShellActionInputs;
import io.cloudslang.content.utils.Constants;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

import static com.xebialabs.overthere.ConnectionOptions.*;
import static com.xebialabs.overthere.OperatingSystemFamily.WINDOWS;
import static com.xebialabs.overthere.cifs.CifsConnectionBuilder.*;
import static com.xebialabs.overthere.util.CapturingOverthereExecutionOutputHandler.capturingHandler;

/**
 * User: bancl
 * Date: 10/9/2015
 */
public class PowerShellScriptServiceImpl implements PowerShellScriptService {

    @Override
    public Map execute(PowerShellActionInputs inputs) {
        try {
            return executePowerShellScript(inputs);
        } catch (Exception e) {
            return getExceptionReturnResult(e);
        }
    }

    private Map executePowerShellScript(PowerShellActionInputs inputs) {
        ConnectionOptions options = getConnectionOptions(inputs);
        try(OverthereConnection connection =
                    Overthere.getConnection(CifsConnectionBuilder.CIFS_PROTOCOL, options)) {
            String encodeBase64 = getBase64EncodedScript(inputs.getScript());

            CapturingOverthereExecutionOutputHandler outHandler = capturingHandler();
            CapturingOverthereExecutionOutputHandler errHandler = capturingHandler();
            CmdLine cmdLine = CmdLine.build("powershell",
                    "-NoProfile", "-NonInteractive", "-EncodedCommand", encodeBase64);
            connection.execute(outHandler, errHandler, cmdLine);

            return getReturnResult(outHandler, errHandler);
        }
    }

    private Map getExceptionReturnResult(Exception e) {
        Map returnResult = new HashMap<>();
        returnResult.put(Constants.OutputNames.RETURN_RESULT, e.getMessage());
        returnResult.put(Constants.OutputNames.EXCEPTION, ExceptionUtils.getStackTrace(e));
        returnResult.put(Constants.OutputNames.RETURN_CODE, Constants.ReturnCodes.RETURN_CODE_FAILURE);
        return returnResult;
    }

    private Map getReturnResult(CapturingOverthereExecutionOutputHandler outHandler,
                                                CapturingOverthereExecutionOutputHandler errHandler) {
        Map returnResult = new HashMap<>();
        returnResult.put(Constants.OutputNames.RETURN_RESULT, outHandler.getOutput());
        returnResult.put(Constants.OutputNames.EXCEPTION, errHandler.getOutput());
        returnResult.put(Constants.OutputNames.RETURN_CODE, Constants.ReturnCodes.RETURN_CODE_SUCCESS);
        return returnResult;
    }

    private String getBase64EncodedScript(String script) {
        byte[] utf8Bytes = script.getBytes(StandardCharsets.UTF_16LE);
        return Base64.encodeBase64String(utf8Bytes);
    }

    private ConnectionOptions getConnectionOptions(PowerShellActionInputs inputs) {
        ConnectionOptions options = new ConnectionOptions();
        options.set(ADDRESS, inputs.getHost());
        options.set(USERNAME, inputs.getUsername());
        options.set(PASSWORD, inputs.getPassword());
        options.set(OPERATING_SYSTEM, WINDOWS);
        options.set(CONNECTION_TYPE, CifsConnectionType.valueOf(inputs.getConnectionType()));

        setOptionalInput(options, WINRM_CONTEXT, inputs.getWinrmContext());
        setOptionalInput(options, WINRM_ENABLE_HTTPS, inputs.getWinrmEnableHTTPS());
        setOptionalInput(options, WINRM_ENVELOP_SIZE, inputs.getWinrmEnvelopSize());
        setOptionalInput(options, WINRM_HTTPS_CERTIFICATE_TRUST_STRATEGY,
                inputs.getWinrmHttpsCertificateTrustStrategy());
        setOptionalInput(options, WINRM_HTTPS_HOSTNAME_VERIFICATION_STRATEGY,
                inputs.getWinrmHttpsHostnameVerificationStrategy());
        setOptionalInput(options, WINRM_KERBEROS_ADD_PORT_TO_SPN, inputs.getWinrmKerberosAddPortToSpn());
        setOptionalInput(options, WINRM_KERBEROS_TICKET_CACHE, inputs.getWinrmKerberosTicketCache());
        setOptionalInput(options, WINRM_KERBEROS_USE_HTTP_SPN, inputs.getWinrmKerberosUseHttpSpn());
        setOptionalInput(options, WINRM_LOCALE, inputs.getWinrmLocale());
        setOptionalInput(options, WINRM_TIMEMOUT, inputs.getWinrmTimeout());
        return options;
    }

    private void setOptionalInput(ConnectionOptions options, String key, String value) {
        if (StringUtils.isNotBlank(value))
            switch (key) {
                case WINRM_ENVELOP_SIZE:
                    options.set(key, Integer.parseInt(value));
                    break;
                case WINRM_HTTPS_CERTIFICATE_TRUST_STRATEGY:
                    options.set(key, WinrmHttpsCertificateTrustStrategy.valueOf(value));
                    break;
                case WINRM_HTTPS_HOSTNAME_VERIFICATION_STRATEGY:
                    options.set(key, WinrmHttpsHostnameVerificationStrategy.valueOf(value));
                    break;
                case WINRM_ENABLE_HTTPS:
                case WINRM_KERBEROS_ADD_PORT_TO_SPN:
                case WINRM_KERBEROS_TICKET_CACHE:
                case WINRM_KERBEROS_USE_HTTP_SPN:
                    options.set(key, Boolean.parseBoolean(value));
                    break;
                default:
                    options.set(key, value);
            }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy