public void visit()

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


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

    String targetUser = props.getProperty("system");
    String target;
    String authPrincipal;
    AuthenticationToken authToken;
    if ("table".equals(targetUser)) {
      target = WalkingSecurity.get(state, env).getTabUserName();
      authPrincipal = WalkingSecurity.get(state, env).getSysUserName();
      authToken = WalkingSecurity.get(state, env).getSysToken();
    } else {
      target = WalkingSecurity.get(state, env).getSysUserName();
      authPrincipal = env.getAccumuloUserName();
      authToken = env.getToken();
    }
    try (AccumuloClient client = env.createClient(authPrincipal, authToken)) {

      boolean exists = WalkingSecurity.get(state, env).userExists(target);
      boolean hasPermission = client.securityOperations().hasSystemPermission(authPrincipal,
          SystemPermission.ALTER_USER);

      Authorizations auths;
      if (authsString.equals("_random")) {
        String[] possibleAuths = WalkingSecurity.get(state, env).getAuthsArray();

        int i = env.getRandom().nextInt(possibleAuths.length);
        String[] authSet = new String[i];
        int length = possibleAuths.length;
        for (int j = 0; j < i; j++) {
          int nextRand = env.getRandom().nextInt(length);
          authSet[j] = possibleAuths[nextRand];
          length--;
          possibleAuths[nextRand] = possibleAuths[length];
          possibleAuths[length] = authSet[j];
        }
        auths = new Authorizations(authSet);
      } else {
        auths = new Authorizations(authsString.split(","));
      }

      try {
        client.securityOperations().changeUserAuthorizations(target, auths);
      } catch (AccumuloSecurityException ae) {
        switch (ae.getSecurityErrorCode()) {
          case PERMISSION_DENIED:
            if (hasPermission)
              throw new AccumuloException(
                  "Got a security exception when I should have had permission.", ae);
            else
              return;
          case USER_DOESNT_EXIST:
            if (exists)
              throw new AccumuloException(
                  "Got security exception when the user should have existed", ae);
            else
              return;
          default:
            throw new AccumuloException("Got unexpected exception", ae);
        }
      }
      WalkingSecurity.get(state, env).changeAuthorizations(target, auths);
      if (!hasPermission)
        throw new AccumuloException("Didn't get Security Exception when we should have");
    }
  }