mirror of
https://github.com/apache/sqoop.git
synced 2025-05-19 02:10:54 +08:00
SQOOP-2774: Sqoop2: Remove the notion of RolesBean
(Jarek Jarcec Cecho via Colin Ma)
This commit is contained in:
parent
67214a48c5
commit
63002273d0
@ -52,10 +52,10 @@ public AuthorizationResourceRequest(DelegationTokenAuthenticatedURL.Token token)
|
||||
super(token);
|
||||
}
|
||||
|
||||
public RolesBean readRoles(String serverUrl) {
|
||||
public RoleBean readRoles(String serverUrl) {
|
||||
String response = super.get(serverUrl + RESOURCE + ROLES);
|
||||
JSONObject jsonObject = JSONUtils.parse(response);
|
||||
RolesBean bean = new RolesBean();
|
||||
RoleBean bean = new RoleBean();
|
||||
bean.restore(jsonObject);
|
||||
return bean;
|
||||
}
|
||||
@ -72,7 +72,7 @@ public void dropRole(String serverUrl, MRole role) {
|
||||
}
|
||||
|
||||
public void grantRevokeRole(String serverUrl, List<MRole> roles, List<MPrincipal> principals, boolean isGrant) {
|
||||
RolesBean rolesBean = new RolesBean(roles);
|
||||
RoleBean rolesBean = new RoleBean(roles);
|
||||
PrincipalBean principalsBean = new PrincipalBean(principals);
|
||||
// Extract all config inputs including sensitive inputs
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
@ -85,12 +85,12 @@ public void grantRevokeRole(String serverUrl, List<MRole> roles, List<MPrincipal
|
||||
}
|
||||
}
|
||||
|
||||
public RolesBean readRolesByPrincipal(String serverUrl, MPrincipal principal) {
|
||||
public RoleBean readRolesByPrincipal(String serverUrl, MPrincipal principal) {
|
||||
String response = super.get(serverUrl + RESOURCE + ROLES
|
||||
+ "?principal_name=" + UrlSafeUtils.urlEncode(principal.getName())
|
||||
+ "&principal_type=" + UrlSafeUtils.urlEncode(principal.getType()));
|
||||
JSONObject jsonObject = JSONUtils.parse(response);
|
||||
RolesBean bean = new RolesBean();
|
||||
RoleBean bean = new RoleBean();
|
||||
bean.restore(jsonObject);
|
||||
return bean;
|
||||
}
|
||||
|
@ -178,7 +178,7 @@ public SubmissionsBean readSubmission(String jArg) {
|
||||
return getSubmissionResourceRequest().read(serverUrl, jArg);
|
||||
}
|
||||
|
||||
public RolesBean readRoles() {
|
||||
public RoleBean readRoles() {
|
||||
return getAuthorizationRequest().readRoles(serverUrl);
|
||||
}
|
||||
|
||||
@ -198,7 +198,7 @@ public void revokeRole(List<MRole> roles, List<MPrincipal> principals) {
|
||||
getAuthorizationRequest().grantRevokeRole(serverUrl, roles, principals, false);
|
||||
}
|
||||
|
||||
public RolesBean readRolesByPrincipal(MPrincipal principal) {
|
||||
public RoleBean readRolesByPrincipal(MPrincipal principal) {
|
||||
return getAuthorizationRequest().readRolesByPrincipal(serverUrl, principal);
|
||||
}
|
||||
|
||||
|
@ -33,7 +33,7 @@
|
||||
@InterfaceStability.Unstable
|
||||
public class RoleBean implements JsonBean {
|
||||
|
||||
public static final String ROLE = "role";
|
||||
private static final String ROLES = "roles";
|
||||
private static final String NAME = "name";
|
||||
|
||||
private List<MRole> roles;
|
||||
@ -45,7 +45,7 @@ public List<MRole> getRoles() {
|
||||
// For "extract"
|
||||
public RoleBean(MRole role) {
|
||||
this();
|
||||
this.roles = new ArrayList<MRole>();
|
||||
this.roles = new ArrayList<>();
|
||||
this.roles.add(role);
|
||||
}
|
||||
|
||||
@ -61,9 +61,16 @@ public RoleBean() {
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public JSONObject extract(boolean skipSensitive) {
|
||||
JSONObject role = new JSONObject();
|
||||
role.put(ROLE, extractRole(roles.get(0)));
|
||||
return role;
|
||||
JSONArray rolesArray = extractRoles();
|
||||
JSONObject roles = new JSONObject();
|
||||
roles.put(ROLES, rolesArray);
|
||||
return roles;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void restore(JSONObject json) {
|
||||
JSONArray rolesArray = JSONUtils.getJSONArray(json, ROLES);
|
||||
restoreRoles(rolesArray);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@ -84,15 +91,8 @@ private JSONObject extractRole(MRole role) {
|
||||
return object;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void restore(JSONObject json) {
|
||||
roles = new ArrayList<MRole>();
|
||||
JSONObject obj = JSONUtils.getJSONObject(json, ROLE);
|
||||
roles.add(restoreRole(obj));
|
||||
}
|
||||
|
||||
protected void restoreRoles(JSONArray array) {
|
||||
roles = new ArrayList<MRole>();
|
||||
roles = new ArrayList<>();
|
||||
for (Object obj : array) {
|
||||
roles.add(restoreRole(obj));
|
||||
}
|
||||
|
@ -17,47 +17,41 @@
|
||||
*/
|
||||
package org.apache.sqoop.json;
|
||||
|
||||
import org.apache.sqoop.classification.InterfaceAudience;
|
||||
import org.apache.sqoop.classification.InterfaceStability;
|
||||
import org.apache.sqoop.model.MRole;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
@InterfaceAudience.Private
|
||||
@InterfaceStability.Unstable
|
||||
public class RolesBean extends RoleBean {
|
||||
import static org.testng.AssertJUnit.assertEquals;
|
||||
|
||||
private static final String ROLES = "roles";
|
||||
public class TestRoleBean {
|
||||
|
||||
// For "extract"
|
||||
public RolesBean(MRole role) {
|
||||
super(role);
|
||||
}
|
||||
@Test
|
||||
public void testLinkSerialization() {
|
||||
MRole godRole = new MRole("god");
|
||||
MRole janitorRole = new MRole("janitor");
|
||||
List<MRole> roles = new LinkedList<>();
|
||||
roles.add(godRole);
|
||||
roles.add(janitorRole);
|
||||
|
||||
public RolesBean(List<MRole> roles) {
|
||||
super(roles);
|
||||
// Serialize it to JSON object
|
||||
RoleBean outputBean = new RoleBean(roles);
|
||||
JSONObject json = outputBean.extract(false);
|
||||
|
||||
}
|
||||
// "Move" it across network in text form
|
||||
String jsonString = json.toJSONString();
|
||||
|
||||
// For "restore"
|
||||
public RolesBean() {
|
||||
}
|
||||
// Retrieved transferred object
|
||||
JSONObject parsedJson = JSONUtils.parse(jsonString);
|
||||
RoleBean inputBean = new RoleBean(roles);
|
||||
inputBean.restore(parsedJson);
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public JSONObject extract(boolean skipSensitive) {
|
||||
JSONArray rolesArray = super.extractRoles();
|
||||
JSONObject roles = new JSONObject();
|
||||
roles.put(ROLES, rolesArray);
|
||||
return roles;
|
||||
}
|
||||
assertEquals(inputBean.getRoles().size(), 2);
|
||||
assertEquals(inputBean.getRoles().get(0).getName(), "god");
|
||||
assertEquals(inputBean.getRoles().get(1).getName(), "janitor");
|
||||
|
||||
@Override
|
||||
public void restore(JSONObject json) {
|
||||
JSONArray rolesArray = JSONUtils.getJSONArray(json, ROLES);
|
||||
restoreRoles(rolesArray);
|
||||
}
|
||||
|
||||
}
|
@ -145,12 +145,12 @@ private JsonBean getRoles(RequestContext ctx) {
|
||||
MPrincipal principal = new MPrincipal(principal_name, principal_type);
|
||||
manager.logAuditEvent(ctx.getUserName(),
|
||||
ctx.getRequest().getRemoteAddr(), "get", "roles by principal", principal.toString());
|
||||
return new RolesBean(handler.getRolesByPrincipal(principal));
|
||||
return new RoleBean(handler.getRolesByPrincipal(principal));
|
||||
} else {
|
||||
// get all roles in the system
|
||||
manager.logAuditEvent(ctx.getUserName(),
|
||||
ctx.getRequest().getRemoteAddr(), "get", "roles", "all");
|
||||
return new RolesBean(handler.getAllRoles());
|
||||
return new RoleBean(handler.getAllRoles());
|
||||
}
|
||||
}
|
||||
|
||||
@ -258,7 +258,7 @@ private JsonBean grantRevokeRole(RequestContext ctx, boolean isGrant) {
|
||||
AuthorizationHandler handler = AuthorizationManager.getInstance().getAuthorizationHandler();
|
||||
AuditLoggerManager manager = AuditLoggerManager.getInstance();
|
||||
|
||||
RolesBean rolesBean = new RolesBean();
|
||||
RoleBean rolesBean = new RoleBean();
|
||||
PrincipalBean principalsBean = new PrincipalBean();
|
||||
|
||||
try {
|
||||
|
Loading…
Reference in New Issue
Block a user