Search in sources :

Example 11 with ErrorResultData

use of org.apache.geode.management.internal.cli.result.ErrorResultData in project geode by apache.

the class ConfigCommands method describeConfig.

@CliCommand(value = { CliStrings.DESCRIBE_CONFIG }, help = CliStrings.DESCRIBE_CONFIG__HELP)
@CliMetaData(relatedTopic = { CliStrings.TOPIC_GEODE_CONFIG })
@ResourceOperation(resource = Resource.CLUSTER, operation = Operation.READ)
public Result describeConfig(@CliOption(key = CliStrings.DESCRIBE_CONFIG__MEMBER, optionContext = ConverterHint.ALL_MEMBER_IDNAME, help = CliStrings.DESCRIBE_CONFIG__MEMBER__HELP, mandatory = true) String memberNameOrId, @CliOption(key = CliStrings.DESCRIBE_CONFIG__HIDE__DEFAULTS, help = CliStrings.DESCRIBE_CONFIG__HIDE__DEFAULTS__HELP, unspecifiedDefaultValue = "true", specifiedDefaultValue = "true") boolean hideDefaults) {
    Result result = null;
    try {
        DistributedMember targetMember = null;
        if (memberNameOrId != null && !memberNameOrId.isEmpty()) {
            targetMember = CliUtil.getDistributedMemberByNameOrId(memberNameOrId);
        }
        if (targetMember != null) {
            ResultCollector<?, ?> rc = CliUtil.executeFunction(getMemberConfigFunction, new Boolean(hideDefaults), targetMember);
            ArrayList<?> output = (ArrayList<?>) rc.getResult();
            Object obj = output.get(0);
            if (obj != null && obj instanceof MemberConfigurationInfo) {
                MemberConfigurationInfo memberConfigInfo = (MemberConfigurationInfo) obj;
                CompositeResultData crd = ResultBuilder.createCompositeResultData();
                crd.setHeader(CliStrings.format(CliStrings.DESCRIBE_CONFIG__HEADER__TEXT, memberNameOrId));
                List<String> jvmArgsList = memberConfigInfo.getJvmInputArguments();
                TabularResultData jvmInputArgs = crd.addSection().addSection().addTable();
                for (String jvmArg : jvmArgsList) {
                    jvmInputArgs.accumulate("JVM command line arguments", jvmArg);
                }
                addSection(crd, memberConfigInfo.getGfePropsSetUsingApi(), "GemFire properties defined using the API");
                addSection(crd, memberConfigInfo.getGfePropsRuntime(), "GemFire properties defined at the runtime");
                addSection(crd, memberConfigInfo.getGfePropsSetFromFile(), "GemFire properties defined with the property file");
                addSection(crd, memberConfigInfo.getGfePropsSetWithDefaults(), "GemFire properties using default values");
                addSection(crd, memberConfigInfo.getCacheAttributes(), "Cache attributes");
                List<Map<String, String>> cacheServerAttributesList = memberConfigInfo.getCacheServerAttributes();
                if (cacheServerAttributesList != null && !cacheServerAttributesList.isEmpty()) {
                    SectionResultData cacheServerSection = crd.addSection();
                    cacheServerSection.setHeader("Cache-server attributes");
                    for (Map<String, String> cacheServerAttributes : cacheServerAttributesList) {
                        addSubSection(cacheServerSection, cacheServerAttributes, "");
                    }
                }
                result = ResultBuilder.buildResult(crd);
            }
        } else {
            ErrorResultData erd = ResultBuilder.createErrorResultData();
            erd.addLine(CliStrings.format(CliStrings.DESCRIBE_CONFIG__MEMBER__NOT__FOUND, new Object[] { memberNameOrId }));
            result = ResultBuilder.buildResult(erd);
        }
    } catch (FunctionInvocationTargetException e) {
        result = ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.COULD_NOT_EXECUTE_COMMAND_TRY_AGAIN, CliStrings.DESCRIBE_CONFIG));
    } catch (Exception e) {
        ErrorResultData erd = ResultBuilder.createErrorResultData();
        erd.addLine(e.getMessage());
        result = ResultBuilder.buildResult(erd);
    }
    return result;
}
Also used : CompositeResultData(org.apache.geode.management.internal.cli.result.CompositeResultData) TabularResultData(org.apache.geode.management.internal.cli.result.TabularResultData) ArrayList(java.util.ArrayList) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) CommandResultException(org.apache.geode.management.internal.cli.result.CommandResultException) IOException(java.io.IOException) GfshParseResult(org.apache.geode.management.internal.cli.GfshParseResult) Result(org.apache.geode.management.cli.Result) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) MemberConfigurationInfo(org.apache.geode.management.internal.cli.domain.MemberConfigurationInfo) DistributedMember(org.apache.geode.distributed.DistributedMember) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) SectionResultData(org.apache.geode.management.internal.cli.result.CompositeResultData.SectionResultData) ErrorResultData(org.apache.geode.management.internal.cli.result.ErrorResultData) HashMap(java.util.HashMap) Map(java.util.Map) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData) ResourceOperation(org.apache.geode.management.internal.security.ResourceOperation)

Example 12 with ErrorResultData

use of org.apache.geode.management.internal.cli.result.ErrorResultData in project geode by apache.

the class GfshCommandsSecurityTest method runCommandsWithAndWithout.

private void runCommandsWithAndWithout(String permission) throws Exception {
    List<TestCommand> allPermitted = TestCommand.getPermittedCommands(new WildcardPermission(permission, true));
    for (TestCommand permitted : allPermitted) {
        System.out.println("Processing authorized command: " + permitted.getCommand());
        CommandResult result = gfshConnection.executeCommand(permitted.getCommand());
        assertNotNull(result);
        if (result.getResultData() instanceof ErrorResultData) {
            assertNotEquals(ResultBuilder.ERRORCODE_UNAUTHORIZED, ((ErrorResultData) result.getResultData()).getErrorCode());
        } else {
            assertEquals(Result.Status.OK, result.getStatus());
        }
    }
    List<TestCommand> others = TestCommand.getCommands();
    others.removeAll(allPermitted);
    for (TestCommand other : others) {
        // skip no permission commands
        if (other.getPermission() == null)
            continue;
        System.out.println("Processing unauthorized command: " + other.getCommand());
        CommandResult result = (CommandResult) gfshConnection.executeCommand(other.getCommand());
        int errorCode = ((ErrorResultData) result.getResultData()).getErrorCode();
        // those commands
        if (errorCode == ResultBuilder.ERRORCODE_USER_ERROR) {
            LogService.getLogger().info("Skip user error: " + result.getContent());
            continue;
        }
        assertEquals(ResultBuilder.ERRORCODE_UNAUTHORIZED, ((ErrorResultData) result.getResultData()).getErrorCode());
        String resultMessage = result.getContent().toString();
        String permString = other.getPermission().toString();
        assertTrue(resultMessage + " does not contain " + permString, resultMessage.contains(permString));
    }
}
Also used : WildcardPermission(org.apache.shiro.authz.permission.WildcardPermission) ErrorResultData(org.apache.geode.management.internal.cli.result.ErrorResultData) CommandResult(org.apache.geode.management.internal.cli.result.CommandResult)

Example 13 with ErrorResultData

use of org.apache.geode.management.internal.cli.result.ErrorResultData in project geode by apache.

the class MultiUserDUnitTest method testMultiUser.

// GEODE-1579
@Category(FlakyTest.class)
@Test
public void testMultiUser() throws IOException, JSONException, InterruptedException {
    IgnoredException.addIgnoredException("java.util.zip.ZipException: zip file is empty");
    Properties properties = new Properties();
    properties.put(NAME, MultiUserDUnitTest.class.getSimpleName());
    properties.put(SECURITY_MANAGER, TestSecurityManager.class.getName());
    // set up vm_0 the secure jmx manager
    Object[] results = setUpJMXManagerOnVM(0, properties, "org/apache/geode/management/internal/security/cacheServer.json");
    String gfshDir = this.gfshDir;
    // set up vm_1 as a gfsh vm, data-reader will login and log out constantly in this vm until the
    // test is done.
    VM vm1 = Host.getHost(0).getVM(1);
    AsyncInvocation vm1Invoke = vm1.invokeAsync("run as data-reader", () -> {
        String shellId = getClass().getSimpleName() + "_vm1";
        HeadlessGfsh shell = new HeadlessGfsh(shellId, 30, gfshDir);
        while (true) {
            connect((String) results[0], (Integer) results[1], (Integer) results[2], shell, "data-reader", "1234567");
            Awaitility.waitAtMost(5, TimeUnit.MILLISECONDS);
            shell.executeCommand("disconnect");
        }
    });
    VM vm2 = Host.getHost(0).getVM(2);
    // set up vm_2 as a gfsh vm, and then connect as "stranger" and try to execute the commands and
    // assert errors comes back are NotAuthorized
    AsyncInvocation vm2Invoke = vm2.invokeAsync("run as guest", () -> {
        String shellId = getClass().getSimpleName() + "_vm2";
        HeadlessGfsh shell = new HeadlessGfsh(shellId, 30, gfshDir);
        connect((String) results[0], (Integer) results[1], (Integer) results[2], shell, "stranger", "1234567");
        List<TestCommand> allCommands = TestCommand.getCommands();
        for (TestCommand command : allCommands) {
            LogService.getLogger().info("executing: " + command.getCommand());
            if (command.getPermission() == null) {
                continue;
            }
            CommandResult result = executeCommand(shell, command.getCommand());
            int errorCode = ((ErrorResultData) result.getResultData()).getErrorCode();
            // those commands
            if (errorCode == ResultBuilder.ERRORCODE_USER_ERROR) {
                LogService.getLogger().info("Skip user error: " + result.getContent());
                continue;
            }
            assertEquals("Not an expected result: " + result.toString(), ResultBuilder.ERRORCODE_UNAUTHORIZED, ((ErrorResultData) result.getResultData()).getErrorCode());
            String resultMessage = result.getContent().toString();
            String permString = command.getPermission().toString();
            assertTrue(resultMessage + " does not contain " + permString, resultMessage.contains(permString));
        }
        LogService.getLogger().info("vm 2 done!");
    });
    VM vm3 = Host.getHost(0).getVM(3);
    IgnoredException.addIgnoredException("java.lang.IllegalArgumentException: Region doesnt exist: {0}", vm3);
    IgnoredException.addIgnoredException("java.lang.ClassNotFoundException: myApp.myListener", vm3);
    // set up vm_3 as another gfsh vm, and then connect as "super-user" and try to execute the
    // commands and assert we don't get a NotAuthorized Exception
    AsyncInvocation vm3Invoke = vm3.invokeAsync("run as superUser", () -> {
        String shellId = getClass().getSimpleName() + "_vm3";
        HeadlessGfsh shell = new HeadlessGfsh(shellId, 30, gfshDir);
        connect((String) results[0], (Integer) results[1], (Integer) results[2], shell, "super-user", "1234567");
        List<TestCommand> allCommands = TestCommand.getCommands();
        for (TestCommand command : allCommands) {
            LogService.getLogger().info("executing: " + command.getCommand());
            if (command.getPermission() == null) {
                continue;
            }
            CommandResult result = executeCommand(shell, command.getCommand());
            if (result.getResultData().getStatus() == Status.OK) {
                continue;
            }
            assertNotEquals("Did not expect an Unauthorized exception: " + result.toString(), ResultBuilder.ERRORCODE_UNAUTHORIZED, ((ErrorResultData) result.getResultData()).getErrorCode());
        }
        LogService.getLogger().info("vm 3 done!");
    });
    // only wait until vm2 and vm3 are done. vm1 will close when test finishes
    vm2Invoke.join();
    vm3Invoke.join();
    vm2Invoke.checkException();
    vm3Invoke.checkException();
    IgnoredException.removeAllExpectedExceptions();
}
Also used : HeadlessGfsh(org.apache.geode.management.internal.cli.HeadlessGfsh) Properties(java.util.Properties) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) CommandResult(org.apache.geode.management.internal.cli.result.CommandResult) VM(org.apache.geode.test.dunit.VM) ErrorResultData(org.apache.geode.management.internal.cli.result.ErrorResultData) TestSecurityManager(org.apache.geode.security.TestSecurityManager) Category(org.junit.experimental.categories.Category) SecurityTest(org.apache.geode.test.junit.categories.SecurityTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 14 with ErrorResultData

use of org.apache.geode.management.internal.cli.result.ErrorResultData in project geode by apache.

the class DurableClientCommands method buildFailureData.

private ErrorResultData buildFailureData(Map<String, List<String>> successMap, Map<String, List<String>> exceptionMap, Map<String, List<String>> errorMap, String errorHeader) {
    ErrorResultData erd = ResultBuilder.createErrorResultData();
    buildErrorResult(erd, successMap);
    erd.addLine("\n");
    erd.addLine(errorHeader);
    buildErrorResult(erd, exceptionMap);
    buildErrorResult(erd, errorMap);
    return erd;
}
Also used : ErrorResultData(org.apache.geode.management.internal.cli.result.ErrorResultData)

Example 15 with ErrorResultData

use of org.apache.geode.management.internal.cli.result.ErrorResultData in project geode by apache.

the class FunctionCommands method executeFunction.

Result executeFunction(InternalCache cache, Set<DistributedMember> DsMembers, String functionId) {
    // unregister on a set of of members
    Function unregisterFunction = new UnregisterFunction();
    FunctionService.registerFunction(unregisterFunction);
    List resultList = null;
    if (DsMembers.isEmpty()) {
        return ResultBuilder.createInfoResult("No members for execution");
    }
    Object[] obj = new Object[1];
    obj[0] = functionId;
    Execution execution = FunctionService.onMembers(DsMembers).setArguments(obj);
    if (execution == null) {
        cache.getLogger().error("executeUnregister execution is null");
        ErrorResultData errorResultData = ResultBuilder.createErrorResultData().setErrorCode(ResultBuilder.ERRORCODE_DEFAULT).addLine(CliStrings.DESTROY_FUNCTION__MSG__CANNOT_EXECUTE);
        return (ResultBuilder.buildResult(errorResultData));
    }
    try {
        resultList = (ArrayList) execution.execute(unregisterFunction).getResult();
    } catch (FunctionException ex) {
        ErrorResultData errorResultData = ResultBuilder.createErrorResultData().setErrorCode(ResultBuilder.ERRORCODE_DEFAULT).addLine(ex.getMessage());
        return (ResultBuilder.buildResult(errorResultData));
    }
    String resultStr = ((String) resultList.get(0));
    if (resultStr.equals("Succeeded in unregistering")) {
        StringBuilder members = new StringBuilder();
        for (DistributedMember member : DsMembers) {
            members.append(member.getId());
            members.append(",");
        }
        return ResultBuilder.createInfoResult("Destroyed " + functionId + " Successfully on " + members.toString().substring(0, members.toString().length() - 1));
    } else {
        return ResultBuilder.createInfoResult("Failed in unregistering");
    }
}
Also used : Function(org.apache.geode.cache.execute.Function) ListFunctionFunction(org.apache.geode.management.internal.cli.functions.ListFunctionFunction) UnregisterFunction(org.apache.geode.management.internal.cli.functions.UnregisterFunction) UserFunctionExecution(org.apache.geode.management.internal.cli.functions.UserFunctionExecution) Execution(org.apache.geode.cache.execute.Execution) FunctionException(org.apache.geode.cache.execute.FunctionException) DistributedMember(org.apache.geode.distributed.DistributedMember) ArrayList(java.util.ArrayList) List(java.util.List) UnregisterFunction(org.apache.geode.management.internal.cli.functions.UnregisterFunction) ErrorResultData(org.apache.geode.management.internal.cli.result.ErrorResultData)

Aggregations

ErrorResultData (org.apache.geode.management.internal.cli.result.ErrorResultData)23 Result (org.apache.geode.management.cli.Result)14 CliMetaData (org.apache.geode.management.cli.CliMetaData)13 CliCommand (org.springframework.shell.core.annotation.CliCommand)13 DistributedMember (org.apache.geode.distributed.DistributedMember)11 InternalCache (org.apache.geode.internal.cache.InternalCache)10 HashSet (java.util.HashSet)9 CliFunctionResult (org.apache.geode.management.internal.cli.functions.CliFunctionResult)8 ResourceOperation (org.apache.geode.management.internal.security.ResourceOperation)8 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 List (java.util.List)7 CommandResultException (org.apache.geode.management.internal.cli.result.CommandResultException)7 TabularResultData (org.apache.geode.management.internal.cli.result.TabularResultData)7 ConverterHint (org.apache.geode.management.cli.ConverterHint)6 GfshParseResult (org.apache.geode.management.internal.cli.GfshParseResult)6 CompositeResultData (org.apache.geode.management.internal.cli.result.CompositeResultData)6 InfoResultData (org.apache.geode.management.internal.cli.result.InfoResultData)6 HashMap (java.util.HashMap)5 FunctionException (org.apache.geode.cache.execute.FunctionException)5