Java Code Examples for org.apache.ranger.plugin.policyengine.RangerAccessRequestImpl#setUser()
The following examples show how to use
org.apache.ranger.plugin.policyengine.RangerAccessRequestImpl#setUser() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StormRangerPlugin.java From ranger with Apache License 2.0 | 6 votes |
public RangerAccessRequest buildAccessRequest(String _user, String[] _groups, String _clientIp, String _topology, String _operation) { RangerAccessRequestImpl request = new RangerAccessRequestImpl(); request.setUser(_user); if (_groups != null && _groups.length > 0) { Set<String> groups = Sets.newHashSet(_groups); request.setUserGroups(groups); } request.setAccessType(getAccessType(_operation)); request.setClientIPAddress(_clientIp); request.setAction(_operation); // build resource and connect stuff into request RangerAccessResourceImpl resource = new RangerAccessResourceImpl(); resource.setValue(ResourceName.Topology, _topology); request.setResource(resource); if (LOG.isDebugEnabled()) { LOG.debug("Returning request: " + request.toString()); } return request; }
Example 2
Source File: KnoxRangerPlugin.java From ranger with Apache License 2.0 | 6 votes |
RangerAccessRequest build() { // build resource RangerAccessResourceImpl resource = new RangerAccessResourceImpl(); resource.setValue(ResourceName.Service, _service); resource.setValue(ResourceName.Topology, _topology); // build request RangerAccessRequestImpl request = new RangerAccessRequestImpl(); request.setAction(AccessType.Allow); request.setAccessType(AccessType.Allow); request.setClientIPAddress(_clientIp); request.setUser(_user); request.setUserGroups(_groups); request.setResource(resource); request.setRemoteIPAddress(_remoteIp); request.setForwardedAddresses(_forwardedAddresses); return request; }
Example 3
Source File: RangerSolrAuthorizer.java From ranger with Apache License 2.0 | 5 votes |
private RangerAccessRequestImpl createBaseRequest(String userName, Set<String> userGroups, String ip, Date eventTime) { RangerAccessRequestImpl rangerRequest = new RangerAccessRequestImpl(); if (userName != null && !userName.isEmpty()) { rangerRequest.setUser(userName); } if (userGroups != null && userGroups.size() > 0) { rangerRequest.setUserGroups(userGroups); } if (ip != null && !ip.isEmpty()) { rangerRequest.setClientIPAddress(ip); } rangerRequest.setAccessTime(eventTime); return rangerRequest; }
Example 4
Source File: RangerPolicyFactory.java From ranger with Apache License 2.0 | 5 votes |
private static RangerAccessRequest mutate(RangerAccessRequest template, boolean shouldEvaluateToTrue) { RangerAccessRequestImpl accessRequest = (RangerAccessRequestImpl) template; accessRequest.setResource(new RangerAccessResourceImpl(createResourceElements(shouldEvaluateToTrue))); accessRequest.setAccessType(pickOneRandomly(ALWAYS_ALLOWED_ACCESS_TYPES )); accessRequest.setRequestData(null); accessRequest.setUser(pickOneRandomly(KNOWN_USERS)); return accessRequest; }
Example 5
Source File: RangerBasePlugin.java From ranger with Apache License 2.0 | 5 votes |
private void auditGrantRevoke(GrantRevokeRequest request, String action, boolean isSuccess, RangerAccessResultProcessor resultProcessor) { if(request != null && resultProcessor != null) { RangerAccessRequestImpl accessRequest = new RangerAccessRequestImpl(); accessRequest.setResource(new RangerAccessResourceImpl(StringUtil.toStringObjectMap(request.getResource()))); accessRequest.setUser(request.getGrantor()); accessRequest.setAccessType(RangerPolicyEngine.ADMIN_ACCESS); accessRequest.setAction(action); accessRequest.setClientIPAddress(request.getClientIPAddress()); accessRequest.setClientType(request.getClientType()); accessRequest.setRequestData(request.getRequestData()); accessRequest.setSessionId(request.getSessionId()); // call isAccessAllowed() to determine if audit is enabled or not RangerAccessResult accessResult = isAccessAllowed(accessRequest, null); if(accessResult != null && accessResult.getIsAudited()) { accessRequest.setAccessType(action); accessResult.setIsAllowed(isSuccess); if(! isSuccess) { accessResult.setPolicyId(-1); } resultProcessor.processResult(accessResult); } } }
Example 6
Source File: RangerNiFiAuthorizer.java From localization_nifi with Apache License 2.0 | 4 votes |
@Override public AuthorizationResult authorize(final AuthorizationRequest request) throws AuthorizationAccessException { final String identity = request.getIdentity(); final String resourceIdentifier = request.getResource().getIdentifier(); // if a ranger admin identity was provided, and it equals the identity making the request, // and the request is to retrieve the resources, then allow it through if (StringUtils.isNotBlank(rangerAdminIdentity) && rangerAdminIdentity.equals(identity) && resourceIdentifier.equals(RESOURCES_RESOURCE)) { return AuthorizationResult.approved(); } final String clientIp; if (request.getUserContext() != null) { clientIp = request.getUserContext().get(UserContextKeys.CLIENT_ADDRESS.name()); } else { clientIp = null; } final RangerAccessResourceImpl resource = new RangerAccessResourceImpl(); resource.setValue(RANGER_NIFI_RESOURCE_NAME, resourceIdentifier); final RangerAccessRequestImpl rangerRequest = new RangerAccessRequestImpl(); rangerRequest.setResource(resource); rangerRequest.setAction(request.getAction().name()); rangerRequest.setAccessType(request.getAction().name()); rangerRequest.setUser(identity); rangerRequest.setAccessTime(new Date()); if (!StringUtils.isBlank(clientIp)) { rangerRequest.setClientIPAddress(clientIp); } // for a direct access request use the default audit handler so we generate audit logs // for non-direct access provide a null result processor so no audit logs get generated final RangerAccessResultProcessor resultProcessor = request.isAccessAttempt() ? defaultAuditHandler : null; final RangerAccessResult result = nifiPlugin.isAccessAllowed(rangerRequest, resultProcessor); if (result != null && result.getIsAllowed()) { return AuthorizationResult.approved(); } else { // if result.getIsAllowed() is false, then we need to determine if it was because no policy exists for the // given resource, or if it was because a policy exists but not for the given user or action final boolean doesPolicyExist = nifiPlugin.doesPolicyExist(request.getResource().getIdentifier()); if (doesPolicyExist) { final String reason = result == null ? null : result.getReason(); if (reason != null) { logger.debug(String.format("Unable to authorize %s due to %s", identity, reason)); } // a policy does exist for the resource so we were really denied access here return AuthorizationResult.denied(request.getExplanationSupplier().get()); } else { // a policy doesn't exist so return resource not found so NiFi can work back up the resource hierarchy return AuthorizationResult.resourceNotFound(); } } }
Example 7
Source File: RangerAuthorizer.java From nifi-registry with Apache License 2.0 | 4 votes |
@Override public AuthorizationResult authorize(final AuthorizationRequest request) throws SecurityProviderCreationException { final String identity = request.getIdentity(); final Set<String> userGroups = request.getGroups(); final String resourceIdentifier = request.getResource().getIdentifier(); // if a ranger admin identity was provided, and it equals the identity making the request, // and the request is to retrieve the resources, then allow it through if (StringUtils.isNotBlank(rangerAdminIdentity) && rangerAdminIdentity.equals(identity) && resourceIdentifier.equals(RESOURCES_RESOURCE)) { return AuthorizationResult.approved(); } final String clientIp; if (request.getUserContext() != null) { clientIp = request.getUserContext().get(UserContextKeys.CLIENT_ADDRESS.name()); } else { clientIp = null; } final RangerAccessResourceImpl resource = new RangerAccessResourceImpl(); resource.setValue(RANGER_NIFI_REG_RESOURCE_NAME, resourceIdentifier); final RangerAccessRequestImpl rangerRequest = new RangerAccessRequestImpl(); rangerRequest.setResource(resource); rangerRequest.setAction(request.getAction().name()); rangerRequest.setAccessType(request.getAction().name()); rangerRequest.setUser(identity); rangerRequest.setUserGroups(userGroups); rangerRequest.setAccessTime(new Date()); if (!StringUtils.isBlank(clientIp)) { rangerRequest.setClientIPAddress(clientIp); } final RangerAccessResult result = rangerPlugin.isAccessAllowed(rangerRequest); // store the result for auditing purposes later if appropriate if (request.isAccessAttempt()) { synchronized (resultLookup) { resultLookup.put(request, result); } } if (result != null && result.getIsAllowed()) { // return approved return AuthorizationResult.approved(); } else { // if result.getIsAllowed() is false, then we need to determine if it was because no policy exists for the // given resource, or if it was because a policy exists but not for the given user or action final boolean doesPolicyExist = rangerPlugin.doesPolicyExist(request.getResource().getIdentifier(), request.getAction()); if (doesPolicyExist) { final String reason = result == null ? null : result.getReason(); if (reason != null) { logger.debug(String.format("Unable to authorize %s due to %s", identity, reason)); } // a policy does exist for the resource so we were really denied access here return AuthorizationResult.denied(request.getExplanationSupplier().get()); } else { // a policy doesn't exist so return resource not found so NiFi Registry can work back up the resource hierarchy return AuthorizationResult.resourceNotFound(); } } }
Example 8
Source File: RangerKafkaAuthorizer.java From ranger with Apache License 2.0 | 4 votes |
@Override public boolean authorize(Session session, Operation operation, Resource resource) { if (rangerPlugin == null) { MiscUtil.logErrorMessageByInterval(logger, "Authorizer is still not initialized"); return false; } RangerPerfTracer perf = null; if(RangerPerfTracer.isPerfTraceEnabled(PERF_KAFKAAUTH_REQUEST_LOG)) { perf = RangerPerfTracer.getPerfTracer(PERF_KAFKAAUTH_REQUEST_LOG, "RangerKafkaAuthorizer.authorize(resource=" + resource + ")"); } String userName = null; if (session.principal() != null) { userName = session.principal().getName(); } java.util.Set<String> userGroups = MiscUtil .getGroupsForRequestUser(userName); String ip = session.clientAddress().getHostAddress(); // skip leading slash if (StringUtils.isNotEmpty(ip) && ip.charAt(0) == '/') { ip = ip.substring(1); } Date eventTime = new Date(); String accessType = mapToRangerAccessType(operation); boolean validationFailed = false; String validationStr = ""; if (accessType == null) { if (MiscUtil.logErrorMessageByInterval(logger, "Unsupported access type. operation=" + operation)) { logger.fatal("Unsupported access type. session=" + session + ", operation=" + operation + ", resource=" + resource); } validationFailed = true; validationStr += "Unsupported access type. operation=" + operation; } String action = accessType; RangerAccessRequestImpl rangerRequest = new RangerAccessRequestImpl(); rangerRequest.setUser(userName); rangerRequest.setUserGroups(userGroups); rangerRequest.setClientIPAddress(ip); rangerRequest.setAccessTime(eventTime); RangerAccessResourceImpl rangerResource = new RangerAccessResourceImpl(); rangerRequest.setResource(rangerResource); rangerRequest.setAccessType(accessType); rangerRequest.setAction(action); rangerRequest.setRequestData(resource.name()); if (resource.resourceType().equals(Topic$.MODULE$)) { rangerResource.setValue(KEY_TOPIC, resource.name()); } else if (resource.resourceType().equals(Cluster$.MODULE$)) { rangerResource.setValue(KEY_CLUSTER, resource.name()); } else if (resource.resourceType().equals(Group$.MODULE$)) { rangerResource.setValue(KEY_CONSUMER_GROUP, resource.name()); } else if (resource.resourceType().equals(TransactionalId$.MODULE$)) { rangerResource.setValue(KEY_TRANSACTIONALID, resource.name()); } else if (resource.resourceType().equals(DelegationToken$.MODULE$)) { rangerResource.setValue(KEY_DELEGATIONTOKEN, resource.name()); } else { logger.fatal("Unsupported resourceType=" + resource.resourceType()); validationFailed = true; } boolean returnValue = false; if (validationFailed) { MiscUtil.logErrorMessageByInterval(logger, validationStr + ", request=" + rangerRequest); } else { try { RangerAccessResult result = rangerPlugin .isAccessAllowed(rangerRequest); if (result == null) { logger.error("Ranger Plugin returned null. Returning false"); } else { returnValue = result.getIsAllowed(); } } catch (Throwable t) { logger.error("Error while calling isAccessAllowed(). request=" + rangerRequest, t); } finally { auditHandler.flushAudit(); } } RangerPerfTracer.log(perf); if (logger.isDebugEnabled()) { logger.debug("rangerRequest=" + rangerRequest + ", return=" + returnValue); } return returnValue; }
Example 9
Source File: RangerAtlasAuthorizer.java From ranger with Apache License 2.0 | 4 votes |
private boolean isAccessAllowed(AtlasEntityAccessRequest request, RangerAtlasAuditHandler auditHandler) throws AtlasAuthorizationException { if (LOG.isDebugEnabled()) { LOG.debug("==> isAccessAllowed(" + request + ")"); } boolean ret = true; try { final String action = request.getAction() != null ? request.getAction().getType() : null; final Set<String> entityTypes = request.getEntityTypeAndAllSuperTypes(); final String entityId = request.getEntityId(); final String classification = request.getClassification() != null ? request.getClassification().getTypeName() : null; final RangerAccessRequestImpl rangerRequest = new RangerAccessRequestImpl(); final RangerAccessResourceImpl rangerResource = new RangerAccessResourceImpl(); final String ownerUser = request.getEntity() != null ? (String) request.getEntity().getAttribute(RESOURCE_ENTITY_OWNER) : null; rangerResource.setValue(RESOURCE_ENTITY_TYPE, entityTypes); rangerResource.setValue(RESOURCE_ENTITY_ID, entityId); rangerResource.setOwnerUser(ownerUser); rangerRequest.setAccessType(action); rangerRequest.setAction(action); rangerRequest.setUser(request.getUser()); rangerRequest.setUserGroups(request.getUserGroups()); rangerRequest.setClientIPAddress(request.getClientIPAddress()); rangerRequest.setAccessTime(request.getAccessTime()); rangerRequest.setResource(rangerResource); rangerRequest.setForwardedAddresses(request.getForwardedAddresses()); rangerRequest.setRemoteIPAddress(request.getRemoteIPAddress()); if (AtlasPrivilege.ENTITY_ADD_LABEL.equals(request.getAction()) || AtlasPrivilege.ENTITY_REMOVE_LABEL.equals(request.getAction())) { rangerResource.setValue(RESOURCE_ENTITY_LABEL, request.getLabel()); } else if (AtlasPrivilege.ENTITY_UPDATE_BUSINESS_METADATA.equals(request.getAction())) { rangerResource.setValue(RESOURCE_ENTITY_BUSINESS_METADATA, request.getBusinessMetadata()); } if (StringUtils.isNotEmpty(classification)) { rangerResource.setValue(RESOURCE_ENTITY_CLASSIFICATION, request.getClassificationTypeAndAllSuperTypes(classification)); ret = checkAccess(rangerRequest, auditHandler); } if (ret) { if (CollectionUtils.isNotEmpty(request.getEntityClassifications())) { // check authorization for each classification for (String classificationToAuthorize : request.getEntityClassifications()) { rangerResource.setValue(RESOURCE_ENTITY_CLASSIFICATION, request.getClassificationTypeAndAllSuperTypes(classificationToAuthorize)); ret = checkAccess(rangerRequest, auditHandler); if (!ret) { break; } } } else { rangerResource.setValue(RESOURCE_ENTITY_CLASSIFICATION, ENTITY_NOT_CLASSIFIED); ret = checkAccess(rangerRequest, auditHandler); } } } finally { if(auditHandler != null) { auditHandler.flushAudit(); } } if (LOG.isDebugEnabled()) { LOG.debug("<== isAccessAllowed(" + request + "): " + ret); } return ret; }
Example 10
Source File: RangerNiFiAuthorizer.java From nifi with Apache License 2.0 | 4 votes |
@Override public AuthorizationResult authorize(final AuthorizationRequest request) throws AuthorizationAccessException { final String identity = request.getIdentity(); final Set<String> userGroups = request.getGroups(); final String resourceIdentifier = request.getResource().getIdentifier(); // if a ranger admin identity was provided, and it equals the identity making the request, // and the request is to retrieve the resources, then allow it through if (StringUtils.isNotBlank(rangerAdminIdentity) && rangerAdminIdentity.equals(identity) && resourceIdentifier.equals(RESOURCES_RESOURCE)) { return AuthorizationResult.approved(); } final String clientIp; if (request.getUserContext() != null) { clientIp = request.getUserContext().get(UserContextKeys.CLIENT_ADDRESS.name()); } else { clientIp = null; } final RangerAccessResourceImpl resource = new RangerAccessResourceImpl(); resource.setValue(RANGER_NIFI_RESOURCE_NAME, resourceIdentifier); final RangerAccessRequestImpl rangerRequest = new RangerAccessRequestImpl(); rangerRequest.setResource(resource); rangerRequest.setAction(request.getAction().name()); rangerRequest.setAccessType(request.getAction().name()); rangerRequest.setUser(identity); rangerRequest.setUserGroups(userGroups); rangerRequest.setAccessTime(new Date()); if (!StringUtils.isBlank(clientIp)) { rangerRequest.setClientIPAddress(clientIp); } final RangerAccessResult result = nifiPlugin.isAccessAllowed(rangerRequest); // store the result for auditing purposes later if appropriate if (request.isAccessAttempt()) { synchronized (resultLookup) { resultLookup.put(request, result); } } if (result != null && result.getIsAllowed()) { // return approved return AuthorizationResult.approved(); } else { // if result.getIsAllowed() is false, then we need to determine if it was because no policy exists for the // given resource, or if it was because a policy exists but not for the given user or action final boolean doesPolicyExist = nifiPlugin.doesPolicyExist(request.getResource().getIdentifier(), request.getAction()); if (doesPolicyExist) { final String reason = result == null ? null : result.getReason(); if (reason != null) { logger.debug(String.format("Unable to authorize %s due to %s", identity, reason)); } // a policy does exist for the resource so we were really denied access here return AuthorizationResult.denied(request.getExplanationSupplier().get()); } else { // a policy doesn't exist so return resource not found so NiFi can work back up the resource hierarchy return AuthorizationResult.resourceNotFound(); } } }