You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ranger.apache.org by me...@apache.org on 2018/04/30 14:05:02 UTC
ranger git commit: RANGER-2085:- Add resource lookup for entity-id in
Atlas service
Repository: ranger
Updated Branches:
refs/heads/master 6c6ec75db -> f076f5216
RANGER-2085:- Add resource lookup for entity-id in Atlas service
Signed-off-by: Mehul Parikh <me...@apache.org>
Project: http://git-wip-us.apache.org/repos/asf/ranger/repo
Commit: http://git-wip-us.apache.org/repos/asf/ranger/commit/f076f521
Tree: http://git-wip-us.apache.org/repos/asf/ranger/tree/f076f521
Diff: http://git-wip-us.apache.org/repos/asf/ranger/diff/f076f521
Branch: refs/heads/master
Commit: f076f521655c62b8859de12f57dede1b1fa4aa0a
Parents: 6c6ec75
Author: nixonrodrigues <ni...@apache.org>
Authored: Thu Apr 26 17:40:45 2018 +0530
Committer: Mehul Parikh <me...@apache.org>
Committed: Mon Apr 30 19:34:42 2018 +0530
----------------------------------------------------------------------
.../services/atlas/RangerServiceAtlas.java | 93 +++++++++++++++++++-
src/main/assembly/admin-web.xml | 7 ++
2 files changed, 99 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/ranger/blob/f076f521/plugin-atlas/src/main/java/org/apache/ranger/services/atlas/RangerServiceAtlas.java
----------------------------------------------------------------------
diff --git a/plugin-atlas/src/main/java/org/apache/ranger/services/atlas/RangerServiceAtlas.java b/plugin-atlas/src/main/java/org/apache/ranger/services/atlas/RangerServiceAtlas.java
index 671d2d1..fb3688b 100644
--- a/plugin-atlas/src/main/java/org/apache/ranger/services/atlas/RangerServiceAtlas.java
+++ b/plugin-atlas/src/main/java/org/apache/ranger/services/atlas/RangerServiceAtlas.java
@@ -29,6 +29,8 @@ import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.core.util.MultivaluedMapImpl;
+import org.apache.atlas.model.instance.AtlasEntityHeader;
+import org.apache.atlas.model.discovery.AtlasSearchResult;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOCase;
import org.apache.commons.lang.StringUtils;
@@ -42,7 +44,6 @@ import org.apache.ranger.plugin.model.RangerServiceDef;
import org.apache.ranger.plugin.service.RangerBaseService;
import org.apache.ranger.plugin.service.ResourceLookupContext;
import org.apache.ranger.plugin.util.PasswordUtils;
-
import javax.security.auth.Subject;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.NewCookie;
@@ -68,6 +69,7 @@ public class RangerServiceAtlas extends RangerBaseService {
private static final String URL_LOGIN = "/j_spring_security_check";
private static final String URL_GET_TYPESDEF_HEADERS = "/api/atlas/v2/types/typedefs/headers";
+ private static final String URl_ENTITY_SEARCH = "v2/search/attribute?attrName=qualifiedName";
private static final String WEB_RESOURCE_CONTENT_TYPE = "application/x-www-form-urlencoded";
private static final String CONNECTION_ERROR_MSG = " You can still save the repository and start creating"
@@ -169,6 +171,8 @@ public class RangerServiceAtlas extends RangerBaseService {
final String userInput = lookupContext.getUserInput();
final List<String> currentValues = lookupContext.getResources().get(lookupContext.getResourceName());
+
+
switch(lookupContext.getResourceName()) {
case RESOURCE_TYPE_CATEGORY: {
for (String typeCategory : TYPE_CATEGORIES) {
@@ -216,6 +220,14 @@ public class RangerServiceAtlas extends RangerBaseService {
addIfStartsWithAndNotExcluded(ret, typesDef.get(TYPE_CLASSIFICATION), userInput, currentValues);
}
+ break;
+
+ case RESOURCE_ENTITY_ID: {
+ List<String> searchTypes = lookupContext.getResources().get("entity-type");
+ List<String> values = searchEntities(userInput , searchTypes);
+ addIfStartsWithAndNotExcluded(ret, values, userInput, currentValues);
+
+ }
break;
default: {
@@ -368,6 +380,85 @@ public class RangerServiceAtlas extends RangerBaseService {
return ret;
}
+ private List<String> searchEntities(String userInput, List<String> types) {
+
+ if( LOG.isDebugEnabled()) {
+ LOG.info(" RangerServiceAtlas.searchEntities ==>> userInput" + userInput +" types "+ types);
+ }
+ List<String> list = null;
+
+ Subject subj = getLoginSubject();
+
+ if (subj == null) {
+ return null;
+ }
+
+ list = Subject.doAs(subj, new PrivilegedAction<List<String>>() {
+ @Override
+ public List<String> run() {
+ List<String> ret = null;
+ String entityType = "";
+ if (types != null && types.size() == 1) {
+ entityType = types.get(0);
+ } else {
+ return null;
+ }
+ for (String atlasUrl : getAtlasUrls()) {
+ Client client = null;
+
+ try {
+ client = Client.create();
+
+ ClientResponse loginResponse = loginToAtlas(client);
+ String entitySearcApiUrl = atlasUrl + "/api/atlas/" + URl_ENTITY_SEARCH;
+ StringBuilder searchUrl = new StringBuilder();
+
+ searchUrl.append(entitySearcApiUrl)
+ .append("&typeName=")
+ .append(entityType)
+ .append("&attrValuePrefix=" + userInput + "&limit=25");
+
+
+ WebResource webResource = client.resource(searchUrl.toString());
+ WebResource.Builder builder = webResource.getRequestBuilder();
+
+ for (NewCookie cook : loginResponse.getCookies()) {
+ builder = builder.cookie(cook);
+ }
+
+ ClientResponse response = builder.get(ClientResponse.class);
+
+ if (response != null) {
+ String jsonString = response.getEntity(String.class);
+ Gson gson = new Gson();
+ AtlasSearchResult searchResult = gson.fromJson(jsonString, AtlasSearchResult.class);
+
+ ret = new ArrayList<>();
+
+ if (searchResult != null) {
+ List<AtlasEntityHeader> entityHeaderList = searchResult.getEntities();
+ for (AtlasEntityHeader entity : entityHeaderList) {
+ ret.add((String) entity.getAttribute("qualifiedName"));
+ }
+ }
+ }
+ } catch (Throwable t) {
+ String msgDesc = "Exception while getting Atlas Entity Resource List.";
+ LOG.error(msgDesc, t);
+ } finally {
+ if (client != null) {
+ client.destroy();
+ }
+ }
+ }
+
+ return ret;
+ }
+ });
+
+ return list;
+ }
+
String[] getAtlasUrls() {
String urlString = connectionProperties.get(CONFIG_REST_ADDRESS);
http://git-wip-us.apache.org/repos/asf/ranger/blob/f076f521/src/main/assembly/admin-web.xml
----------------------------------------------------------------------
diff --git a/src/main/assembly/admin-web.xml b/src/main/assembly/admin-web.xml
index 73bf8c0..d0f3545 100644
--- a/src/main/assembly/admin-web.xml
+++ b/src/main/assembly/admin-web.xml
@@ -324,6 +324,13 @@
<unpack>false</unpack>
<directoryMode>755</directoryMode>
<fileMode>644</fileMode>
+ <dependencySets>
+ <dependencySet>
+ <includes>
+ <include>org.apache.atlas:atlas-intg:jar:${atlas.version}</include>
+ </includes>
+ </dependencySet>
+ </dependencySets>
</binaries>
<includes>
<include>org.apache.ranger:ranger-atlas-plugin</include>