public void visit()

in src/main/java/org/apache/accumulo/testing/randomwalk/security/ChangePass.java [36:98]


  public void visit(State state, RandWalkEnv env, Properties props) throws Exception {
    String target = props.getProperty("target");
    String source = props.getProperty("source");

    String principal;
    AuthenticationToken token;
    if (source.equals("system")) {
      principal = WalkingSecurity.get(state, env).getSysUserName();
      token = WalkingSecurity.get(state, env).getSysToken();
    } else {
      principal = WalkingSecurity.get(state, env).getTabUserName();
      token = WalkingSecurity.get(state, env).getTabToken();
    }
    try (AccumuloClient client = env.createClient(principal, token)) {

      boolean hasPerm;
      boolean targetExists;
      if (target.equals("table")) {
        target = WalkingSecurity.get(state, env).getTabUserName();
      } else
        target = WalkingSecurity.get(state, env).getSysUserName();

      targetExists = WalkingSecurity.get(state, env).userExists(target);

      hasPerm =
          client.securityOperations().hasSystemPermission(principal, SystemPermission.ALTER_USER)
              || principal.equals(target);

      byte[] newPassw = new byte[env.getRandom().nextInt(50) + 1];
      for (int i = 0; i < newPassw.length; i++)
        newPassw[i] = (byte) ((env.getRandom().nextInt(26) + 65) & 0xFF);

      PasswordToken newPass = new PasswordToken(newPassw);
      try {
        client.securityOperations().changeLocalUserPassword(target, newPass);
      } catch (AccumuloSecurityException ae) {
        switch (ae.getSecurityErrorCode()) {
          case PERMISSION_DENIED:
            if (hasPerm)
              throw new AccumuloException(
                  "Change failed when it should have succeeded to change " + target + "'s password",
                  ae);
            return;
          case USER_DOESNT_EXIST:
            if (targetExists)
              throw new AccumuloException("User " + target + " doesn't exist and they SHOULD.", ae);
            return;
          case BAD_CREDENTIALS:
            if (!WalkingSecurity.get(state, env).userPassTransient(client.whoami()))
              throw new AccumuloException("Bad credentials for user " + client.whoami());
            return;
          default:
            throw new AccumuloException("Got unexpected exception", ae);
        }
      }
      WalkingSecurity.get(state, env).changePassword(target, newPass);
      // Waiting 1 second for password to propogate through Zk
      Thread.sleep(1000);
      if (!hasPerm)
        throw new AccumuloException("Password change succeeded when it should have failed for "
            + source + " changing the password for " + target + ".");
    }
  }