target = SecurityHelper.getSysUserName(state);
boolean exists = SecurityHelper.getTabUserExists(state);
boolean tableExists = SecurityHelper.getTableExists(state);
TablePermission tabPerm;
if (perm.equals("random")) {
Random r = new Random();
int i = r.nextInt(TablePermission.values().length);
tabPerm = TablePermission.values()[i];
} else
tabPerm = TablePermission.valueOf(perm);
boolean hasPerm = SecurityHelper.getTabPerm(state, target, tabPerm);
boolean canGive;
if ("system".equals(sourceUser)) {
conn = SecurityHelper.getSystemConnector(state);
canGive = SecurityHelper.getSysPerm(state, SecurityHelper.getSysUserName(state), SystemPermission.ALTER_USER)
|| SecurityHelper.getTabPerm(state, SecurityHelper.getSysUserName(state), TablePermission.GRANT);
} else if ("table".equals(sourceUser)) {
conn = state.getInstance().getConnector(SecurityHelper.getTabUserName(state), SecurityHelper.getTabUserPass(state));
canGive = SecurityHelper.getTabPerm(state, SecurityHelper.getTabUserName(state), TablePermission.GRANT);
} else {
conn = state.getConnector();
canGive = true;
}
// toggle
if (!"take".equals(action) && !"give".equals(action)) {
try {
boolean res;
if (hasPerm != (res = state.getConnector().securityOperations().hasTablePermission(target, SecurityHelper.getTableName(state), tabPerm)))
throw new AccumuloException("Test framework and accumulo are out of sync for user " + conn.whoami() + " for perm " + tabPerm.name()
+ " with local vs. accumulo being " + hasPerm + " " + res);
if (hasPerm)
action = "take";
else
action = "give";
} catch (AccumuloSecurityException ae) {
switch (ae.getErrorCode()) {
case USER_DOESNT_EXIST:
if (exists)
throw new AccumuloException("Framework and Accumulo are out of sync, we think user exists", ae);
else
return;
case TABLE_DOESNT_EXIST:
if (tabExists)
throw new AccumuloException(conn.whoami(), ae);
else
return;
default:
throw ae;
}
}
}
if ("take".equals(action)) {
try {
conn.securityOperations().revokeTablePermission(target, SecurityHelper.getTableName(state), tabPerm);
} catch (AccumuloSecurityException ae) {
switch (ae.getErrorCode()) {
case GRANT_INVALID:
if (tabPerm.equals(SystemPermission.GRANT))
return;
case PERMISSION_DENIED:
if (canGive)
throw new AccumuloException("Test user failed to give permission when it should have worked", ae);
return;
case USER_DOESNT_EXIST:
if (exists)
throw new AccumuloException("Table user doesn't exist and they SHOULD.", ae);
return;
case TABLE_DOESNT_EXIST:
if (tableExists)
throw new AccumuloException("Table doesn't exist but it should", ae);
return;
default:
throw new AccumuloException("Got unexpected exception", ae);
}
}
SecurityHelper.setTabPerm(state, target, tabPerm, false);
} else if ("give".equals(action)) {
try {
conn.securityOperations().grantTablePermission(target, SecurityHelper.getTableName(state), tabPerm);
} catch (AccumuloSecurityException ae) {
switch (ae.getErrorCode()) {
case GRANT_INVALID:
if (tabPerm.equals(SystemPermission.GRANT))
return;
throw new AccumuloException("Got a grant invalid on non-System.GRANT option", ae);
case PERMISSION_DENIED:
if (canGive)
throw new AccumuloException("Test user failed to give permission when it should have worked", ae);