Search in sources :

Example 1 with AddUser

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

the class ArtemisTest method testUserCommand.

@Test
public void testUserCommand() 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)"));
    checkRole("admin", roleFile, "amq");
    // add a simple user
    AddUser addCmd = new AddUser();
    addCmd.setUsername("guest");
    addCmd.setPassword("guest123");
    addCmd.setRole("admin");
    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("\"admin\"(amq)"));
    assertTrue(result.contains("\"guest\"(admin)"));
    checkRole("guest", roleFile, "admin");
    assertTrue(checkPassword("guest", "guest123", userFile));
    // add a user with 2 roles
    addCmd = new AddUser();
    addCmd.setUsername("scott");
    addCmd.setPassword("tiger");
    addCmd.setRole("admin,operator");
    addCmd.execute(ActionContext.system());
    // verify
    context = new TestActionContext();
    listCmd.execute(context);
    result = context.getStdout();
    System.out.println("output3:\n" + result);
    assertTrue(result.contains("\"admin\"(amq)"));
    assertTrue(result.contains("\"guest\"(admin)"));
    assertTrue(result.contains("\"scott\"(admin,operator)"));
    checkRole("scott", roleFile, "admin", "operator");
    assertTrue(checkPassword("scott", "tiger", userFile));
    // add an existing user
    addCmd = new AddUser();
    addCmd.setUsername("scott");
    addCmd.setPassword("password");
    addCmd.setRole("visitor");
    try {
        addCmd.execute(ActionContext.system());
        fail("should throw an exception if adding a existing user");
    } catch (IllegalArgumentException expected) {
    }
    // check existing users are intact
    context = new TestActionContext();
    listCmd.execute(context);
    result = context.getStdout();
    System.out.println("output4:\n" + result);
    assertTrue(result.contains("\"admin\"(amq)"));
    assertTrue(result.contains("\"guest\"(admin)"));
    assertTrue(result.contains("\"scott\"(admin,operator)"));
    // remove a user
    RemoveUser rmCmd = new RemoveUser();
    rmCmd.setUsername("guest");
    rmCmd.execute(ActionContext.system());
    // check
    context = new TestActionContext();
    listCmd.execute(context);
    result = context.getStdout();
    System.out.println("output5:\n" + result);
    assertTrue(result.contains("\"admin\"(amq)"));
    assertFalse(result.contains("\"guest\"(admin)"));
    assertTrue(result.contains("\"scott\"(admin,operator)") || result.contains("\"scott\"(operator,admin)"));
    assertTrue(result.contains("Total: 2"));
    // remove another
    rmCmd = new RemoveUser();
    rmCmd.setUsername("scott");
    rmCmd.execute(ActionContext.system());
    // check
    context = new TestActionContext();
    listCmd.execute(context);
    result = context.getStdout();
    System.out.println("output6:\n" + result);
    assertTrue(result.contains("\"admin\"(amq)"));
    assertFalse(result.contains("\"guest\"(admin)"));
    assertFalse(result.contains("\"scott\"(admin,operator)") || result.contains("\"scott\"(operator,admin)"));
    assertTrue(result.contains("Total: 1"));
    // remove non-exist
    rmCmd = new RemoveUser();
    rmCmd.setUsername("alien");
    try {
        rmCmd.execute(ActionContext.system());
        fail("should throw exception when removing a non-existing user");
    } catch (IllegalArgumentException expected) {
    }
    // check
    context = new TestActionContext();
    listCmd.execute(context);
    result = context.getStdout();
    System.out.println("output7:\n" + result);
    assertTrue(result.contains("\"admin\"(amq)"));
    assertTrue(result.contains("Total: 1"));
    // now remove last
    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"));
}
Also used : RemoveUser(org.apache.activemq.artemis.cli.commands.user.RemoveUser) ListUser(org.apache.activemq.artemis.cli.commands.user.ListUser) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) File(java.io.File) AddUser(org.apache.activemq.artemis.cli.commands.user.AddUser) Test(org.junit.Test)

Example 2 with AddUser

use of org.apache.activemq.artemis.cli.commands.user.AddUser 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)

Aggregations

File (java.io.File)2 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)2 AddUser (org.apache.activemq.artemis.cli.commands.user.AddUser)2 ListUser (org.apache.activemq.artemis.cli.commands.user.ListUser)2 RemoveUser (org.apache.activemq.artemis.cli.commands.user.RemoveUser)2 Test (org.junit.Test)2 ActionContext (org.apache.activemq.artemis.cli.commands.ActionContext)1 ResetUser (org.apache.activemq.artemis.cli.commands.user.ResetUser)1