Search in sources :

Example 26 with ActionContext

use of org.apache.activemq.artemis.cli.commands.ActionContext in project activemq-artemis by apache.

the class ArtemisTest method testUserCommandReset.

@Test
public void testUserCommandReset() throws Exception {
    Run.setEmbedded(true);
    File instance1 = new File(temporaryFolder.getRoot(), "instance_user");
    System.setProperty("java.security.auth.login.config", instance1.getAbsolutePath() + "/etc/login.config");
    Artemis.main("create", instance1.getAbsolutePath(), "--silent", "--no-autotune");
    System.setProperty("artemis.instance", instance1.getAbsolutePath());
    File userFile = new File(instance1.getAbsolutePath() + "/etc/artemis-users.properties");
    File roleFile = new File(instance1.getAbsolutePath() + "/etc/artemis-roles.properties");
    ListUser listCmd = new ListUser();
    TestActionContext context = new TestActionContext();
    listCmd.execute(context);
    String result = context.getStdout();
    System.out.println("output1:\n" + result);
    // default only one user admin with role amq
    assertTrue(result.contains("\"admin\"(amq)"));
    // remove a user
    RemoveUser rmCmd = new RemoveUser();
    rmCmd.setUsername("admin");
    rmCmd.execute(ActionContext.system());
    // check
    context = new TestActionContext();
    listCmd.execute(context);
    result = context.getStdout();
    System.out.println("output8:\n" + result);
    assertTrue(result.contains("Total: 0"));
    // add some users
    AddUser addCmd = new AddUser();
    addCmd.setUsername("guest");
    addCmd.setPassword("guest123");
    addCmd.setRole("admin");
    addCmd.execute(new TestActionContext());
    addCmd.setUsername("user1");
    addCmd.setPassword("password1");
    addCmd.setRole("admin,manager");
    addCmd.execute(new TestActionContext());
    assertTrue(checkPassword("user1", "password1", userFile));
    addCmd.setUsername("user2");
    addCmd.setPassword("password2");
    addCmd.setRole("admin,manager,master");
    addCmd.execute(new TestActionContext());
    addCmd.setUsername("user3");
    addCmd.setPassword("password3");
    addCmd.setRole("system,master");
    addCmd.execute(new TestActionContext());
    // verify use list cmd
    context = new TestActionContext();
    listCmd.execute(context);
    result = context.getStdout();
    System.out.println("output2:\n" + result);
    assertTrue(result.contains("Total: 4"));
    assertTrue(result.contains("\"guest\"(admin)"));
    assertTrue(result.contains("\"user1\"(admin,manager)"));
    assertTrue(result.contains("\"user2\"(admin,manager,master)"));
    assertTrue(result.contains("\"user3\"(master,system)"));
    checkRole("user1", roleFile, "admin", "manager");
    // reset password
    context = new TestActionContext();
    ResetUser resetCommand = new ResetUser();
    resetCommand.setUsername("user1");
    resetCommand.setPassword("newpassword1");
    resetCommand.execute(context);
    checkRole("user1", roleFile, "admin", "manager");
    assertFalse(checkPassword("user1", "password1", userFile));
    assertTrue(checkPassword("user1", "newpassword1", userFile));
    // reset role
    resetCommand.setUsername("user2");
    resetCommand.setRole("manager,master,operator");
    resetCommand.execute(new TestActionContext());
    checkRole("user2", roleFile, "manager", "master", "operator");
    // reset both
    resetCommand.setUsername("user3");
    resetCommand.setPassword("newpassword3");
    resetCommand.setRole("admin,system");
    resetCommand.execute(new ActionContext());
    checkRole("user3", roleFile, "admin", "system");
    assertTrue(checkPassword("user3", "newpassword3", userFile));
}
Also used : RemoveUser(org.apache.activemq.artemis.cli.commands.user.RemoveUser) ListUser(org.apache.activemq.artemis.cli.commands.user.ListUser) ResetUser(org.apache.activemq.artemis.cli.commands.user.ResetUser) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ActionContext(org.apache.activemq.artemis.cli.commands.ActionContext) File(java.io.File) AddUser(org.apache.activemq.artemis.cli.commands.user.AddUser) Test(org.junit.Test)

Example 27 with ActionContext

use of org.apache.activemq.artemis.cli.commands.ActionContext in project activemq-artemis by apache.

the class OptionsValidationTest method testCommand.

@Test
public void testCommand() throws Exception {
    ActionContext context = new TestActionContext();
    String[] invalidArgs = null;
    if (group == null) {
        invalidArgs = new String[] { command, "--blahblah-" + command, "--rubbish-" + command + "=" + "more-rubbish", "--input=blahblah" };
    } else {
        invalidArgs = new String[] { group, command, "--blahblah-" + command, "--rubbish-" + command + "=" + "more-rubbish", "--input=blahblah" };
    }
    try {
        Artemis.internalExecute(null, needInstance ? this.artemisInstance : null, invalidArgs, context);
        fail("cannot detect invalid options");
    } catch (InvalidOptionsError e) {
        assertTrue(e.getMessage().contains("Found unexpected parameters"));
    } catch (ParseArgumentsUnexpectedException e) {
    // airline can detect some invalid args during parsing
    // which is fine.
    }
}
Also used : InvalidOptionsError(org.apache.activemq.artemis.cli.commands.InvalidOptionsError) ParseArgumentsUnexpectedException(io.airlift.airline.ParseArgumentsUnexpectedException) ActionContext(org.apache.activemq.artemis.cli.commands.ActionContext) Test(org.junit.Test)

Aggregations

ActionContext (org.apache.activemq.artemis.cli.commands.ActionContext)27 Test (org.junit.Test)27 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)26 PrintStream (java.io.PrintStream)25 CreateQueue (org.apache.activemq.artemis.cli.commands.queue.CreateQueue)9 AddressInfo (org.apache.activemq.artemis.core.server.impl.AddressInfo)9 DeleteQueue (org.apache.activemq.artemis.cli.commands.queue.DeleteQueue)8 UpdateQueue (org.apache.activemq.artemis.cli.commands.queue.UpdateQueue)7 CreateAddress (org.apache.activemq.artemis.cli.commands.address.CreateAddress)4 RoutingType (org.apache.activemq.artemis.api.core.RoutingType)3 DeleteAddress (org.apache.activemq.artemis.cli.commands.address.DeleteAddress)3 ShowAddress (org.apache.activemq.artemis.cli.commands.address.ShowAddress)3 UpdateAddress (org.apache.activemq.artemis.cli.commands.address.UpdateAddress)3 Queue (org.apache.activemq.artemis.core.server.Queue)3 QueueQueryResult (org.apache.activemq.artemis.core.server.QueueQueryResult)3 ParseArgumentsUnexpectedException (io.airlift.airline.ParseArgumentsUnexpectedException)1 File (java.io.File)1 InvalidOptionsError (org.apache.activemq.artemis.cli.commands.InvalidOptionsError)1 AddUser (org.apache.activemq.artemis.cli.commands.user.AddUser)1 ListUser (org.apache.activemq.artemis.cli.commands.user.ListUser)1