public void visit()

in src/main/java/org/apache/accumulo/testing/randomwalk/security/AlterSystemPerm.java [34:99]


  public void visit(State state, RandWalkEnv env, Properties props) throws Exception {
    AccumuloClient client = env.getAccumuloClient();
    WalkingSecurity ws = new WalkingSecurity(state, env);

    String action = props.getProperty("task", "toggle");
    String perm = props.getProperty("perm", "random");

    String targetUser = WalkingSecurity.get(state, env).getSysUserName();

    SystemPermission sysPerm;
    if (perm.equals("random")) {
      int i = env.getRandom().nextInt(SystemPermission.values().length);
      sysPerm = SystemPermission.values()[i];
    } else
      sysPerm = SystemPermission.valueOf(perm);

    boolean hasPerm = ws.hasSystemPermission(targetUser, sysPerm);

    // toggle
    if (!"take".equals(action) && !"give".equals(action)) {
      if (hasPerm != client.securityOperations().hasSystemPermission(targetUser, sysPerm))
        throw new AccumuloException("Test framework and accumulo are out of sync!");
      if (hasPerm)
        action = "take";
      else
        action = "give";
    }

    if ("take".equals(action)) {
      try {
        client.securityOperations().revokeSystemPermission(targetUser, sysPerm);
      } catch (AccumuloSecurityException ae) {
        switch (ae.getSecurityErrorCode()) {
          case GRANT_INVALID:
            if (sysPerm.equals(SystemPermission.GRANT))
              return;
            throw new AccumuloException("Got GRANT_INVALID when not dealing with GRANT", ae);
          case PERMISSION_DENIED:
            throw new AccumuloException("Test user doesn't have root", ae);
          case USER_DOESNT_EXIST:
            throw new AccumuloException("System user doesn't exist and they SHOULD.", ae);
          default:
            throw new AccumuloException("Got unexpected exception", ae);
        }
      }
      ws.revokeSystemPermission(targetUser, sysPerm);
    } else if ("give".equals(action)) {
      try {
        client.securityOperations().grantSystemPermission(targetUser, sysPerm);
      } catch (AccumuloSecurityException ae) {
        switch (ae.getSecurityErrorCode()) {
          case GRANT_INVALID:
            if (sysPerm.equals(SystemPermission.GRANT))
              return;
            throw new AccumuloException("Got GRANT_INVALID when not dealing with GRANT", ae);
          case PERMISSION_DENIED:
            throw new AccumuloException("Test user doesn't have root", ae);
          case USER_DOESNT_EXIST:
            throw new AccumuloException("System user doesn't exist and they SHOULD.", ae);
          default:
            throw new AccumuloException("Got unexpected exception", ae);
        }
      }
      ws.grantSystemPermission(targetUser, sysPerm);
    }
  }