Search in sources :

Example 1 with ResetUser

use of org.apache.activemq.artemis.cli.commands.user.ResetUser 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)1 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)1 ActionContext (org.apache.activemq.artemis.cli.commands.ActionContext)1 AddUser (org.apache.activemq.artemis.cli.commands.user.AddUser)1 ListUser (org.apache.activemq.artemis.cli.commands.user.ListUser)1 RemoveUser (org.apache.activemq.artemis.cli.commands.user.RemoveUser)1 ResetUser (org.apache.activemq.artemis.cli.commands.user.ResetUser)1 Test (org.junit.Test)1