Search in sources :

Example 1 with SecurityService

use of org.apache.geode.internal.security.SecurityService in project geode by apache.

the class DeployCommands method deploy.

/**
   * Deploy one or more JAR files to members of a group or all members.
   * 
   * @param groups Group(s) to deploy the JAR to or null for all members
   * @param jar JAR file to deploy
   * @param dir Directory of JAR files to deploy
   * @return The result of the attempt to deploy
   */
@CliCommand(value = { CliStrings.DEPLOY }, help = CliStrings.DEPLOY__HELP)
@CliMetaData(interceptor = "org.apache.geode.management.internal.cli.commands.DeployCommands$Interceptor", relatedTopic = { CliStrings.TOPIC_GEODE_CONFIG })
public Result deploy(@CliOption(key = { CliStrings.DEPLOY__GROUP }, help = CliStrings.DEPLOY__GROUP__HELP, optionContext = ConverterHint.MEMBERGROUP) String[] groups, @CliOption(key = { CliStrings.DEPLOY__JAR }, help = CliStrings.DEPLOY__JAR__HELP) String jar, @CliOption(key = { CliStrings.DEPLOY__DIR }, help = CliStrings.DEPLOY__DIR__HELP) String dir) {
    try {
        // since deploy function can potentially do a lot of damage to security, this action should
        // require these following privileges
        SecurityService securityService = SecurityService.getSecurityService();
        securityService.authorizeClusterManage();
        securityService.authorizeClusterWrite();
        securityService.authorizeDataManage();
        securityService.authorizeDataWrite();
        TabularResultData tabularData = ResultBuilder.createTabularResultData();
        byte[][] shellBytesData = CommandExecutionContext.getBytesFromShell();
        String[] jarNames = CliUtil.bytesToNames(shellBytesData);
        byte[][] jarBytes = CliUtil.bytesToData(shellBytesData);
        Set<DistributedMember> targetMembers;
        targetMembers = CliUtil.findMembers(groups, null);
        if (targetMembers.size() > 0) {
            // this deploys the jars to all the matching servers
            ResultCollector<?, ?> resultCollector = CliUtil.executeFunction(this.deployFunction, new Object[] { jarNames, jarBytes }, targetMembers);
            List<CliFunctionResult> results = CliFunctionResult.cleanResults((List<?>) resultCollector.getResult());
            for (CliFunctionResult result : results) {
                if (result.getThrowable() != null) {
                    tabularData.accumulate("Member", result.getMemberIdOrName());
                    tabularData.accumulate("Deployed JAR", "");
                    tabularData.accumulate("Deployed JAR Location", "ERROR: " + result.getThrowable().getClass().getName() + ": " + result.getThrowable().getMessage());
                    tabularData.setStatus(Status.ERROR);
                } else {
                    String[] strings = (String[]) result.getSerializables();
                    for (int i = 0; i < strings.length; i += 2) {
                        tabularData.accumulate("Member", result.getMemberIdOrName());
                        tabularData.accumulate("Deployed JAR", strings[i]);
                        tabularData.accumulate("Deployed JAR Location", strings[i + 1]);
                    }
                }
            }
        }
        Result result = ResultBuilder.buildResult(tabularData);
        persistClusterConfiguration(result, () -> getSharedConfiguration().addJarsToThisLocator(jarNames, jarBytes, groups));
        return result;
    } catch (NotAuthorizedException e) {
        // for NotAuthorizedException, will catch this later in the code
        throw e;
    } catch (VirtualMachineError e) {
        SystemFailure.initiateFailure(e);
        throw e;
    } catch (Throwable t) {
        SystemFailure.checkFailure();
        return ResultBuilder.createGemFireErrorResult(String.format("Exception while attempting to deploy: (%1$s)", toString(t, isDebugging())));
    }
}
Also used : TabularResultData(org.apache.geode.management.internal.cli.result.TabularResultData) NotAuthorizedException(org.apache.geode.security.NotAuthorizedException) ConverterHint(org.apache.geode.management.cli.ConverterHint) GfshParseResult(org.apache.geode.management.internal.cli.GfshParseResult) Result(org.apache.geode.management.cli.Result) FileResult(org.apache.geode.management.internal.cli.result.FileResult) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) SecurityService(org.apache.geode.internal.security.SecurityService) DistributedMember(org.apache.geode.distributed.DistributedMember) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData)

Aggregations

DistributedMember (org.apache.geode.distributed.DistributedMember)1 SecurityService (org.apache.geode.internal.security.SecurityService)1 CliMetaData (org.apache.geode.management.cli.CliMetaData)1 ConverterHint (org.apache.geode.management.cli.ConverterHint)1 Result (org.apache.geode.management.cli.Result)1 GfshParseResult (org.apache.geode.management.internal.cli.GfshParseResult)1 CliFunctionResult (org.apache.geode.management.internal.cli.functions.CliFunctionResult)1 FileResult (org.apache.geode.management.internal.cli.result.FileResult)1 TabularResultData (org.apache.geode.management.internal.cli.result.TabularResultData)1 NotAuthorizedException (org.apache.geode.security.NotAuthorizedException)1 CliCommand (org.springframework.shell.core.annotation.CliCommand)1