Java Code Examples for org.apache.nifi.authorization.Authorizer#authorize()
The following examples show how to use
org.apache.nifi.authorization.Authorizer#authorize() .
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: Authorizable.java From localization_nifi with Apache License 2.0 | 4 votes |
/** * Returns the result of an authorization request for the specified user for the specified action on the specified * resource. This method does not imply the user is directly attempting to access the specified resource. If the user is * attempting a direct access use Authorizable.authorize(). * * @param authorizer authorizer * @param action action * @param user user * @return is authorized */ default AuthorizationResult checkAuthorization(Authorizer authorizer, RequestAction action, NiFiUser user, Map<String, String> resourceContext) { if (user == null) { return AuthorizationResult.denied("Unknown user."); } final Map<String,String> userContext; if (user.getClientAddress() != null && !user.getClientAddress().trim().isEmpty()) { userContext = new HashMap<>(); userContext.put(UserContextKeys.CLIENT_ADDRESS.name(), user.getClientAddress()); } else { userContext = null; } final Resource resource = getResource(); final AuthorizationRequest request = new AuthorizationRequest.Builder() .identity(user.getIdentity()) .anonymous(user.isAnonymous()) .accessAttempt(false) .action(action) .resource(resource) .resourceContext(resourceContext) .userContext(userContext) .explanationSupplier(() -> { // build the safe explanation final StringBuilder safeDescription = new StringBuilder("Unable to "); if (RequestAction.READ.equals(action)) { safeDescription.append("view "); } else { safeDescription.append("modify "); } safeDescription.append(resource.getSafeDescription()).append("."); return safeDescription.toString(); }) .build(); // perform the authorization final AuthorizationResult result = authorizer.authorize(request); // verify the results if (Result.ResourceNotFound.equals(result.getResult())) { final Authorizable parent = getParentAuthorizable(); if (parent == null) { return AuthorizationResult.denied("No applicable policies could be found."); } else { // create a custom authorizable to override the safe description but still defer to the parent authorizable final Authorizable parentProxy = new Authorizable() { @Override public Authorizable getParentAuthorizable() { return parent.getParentAuthorizable(); } @Override public Resource getResource() { final Resource parentResource = parent.getResource(); return new Resource() { @Override public String getIdentifier() { return parentResource.getIdentifier(); } @Override public String getName() { return parentResource.getName(); } @Override public String getSafeDescription() { return resource.getSafeDescription(); } }; } }; return parentProxy.checkAuthorization(authorizer, action, user, resourceContext); } } else { return result; } }
Example 2
Source File: Authorizable.java From localization_nifi with Apache License 2.0 | 4 votes |
/** * Authorizes the current user for the specified action on the specified resource. This method does imply the user is * directly accessing the specified resource. * * @param authorizer authorizer * @param action action * @param user user * @param resourceContext resource context */ default void authorize(Authorizer authorizer, RequestAction action, NiFiUser user, Map<String, String> resourceContext) throws AccessDeniedException { if (user == null) { throw new AccessDeniedException("Unknown user."); } final Map<String,String> userContext; if (user.getClientAddress() != null && !user.getClientAddress().trim().isEmpty()) { userContext = new HashMap<>(); userContext.put(UserContextKeys.CLIENT_ADDRESS.name(), user.getClientAddress()); } else { userContext = null; } final Resource resource = getResource(); final AuthorizationRequest request = new AuthorizationRequest.Builder() .identity(user.getIdentity()) .anonymous(user.isAnonymous()) .accessAttempt(true) .action(action) .resource(resource) .resourceContext(resourceContext) .userContext(userContext) .explanationSupplier(() -> { // build the safe explanation final StringBuilder safeDescription = new StringBuilder("Unable to "); if (RequestAction.READ.equals(action)) { safeDescription.append("view "); } else { safeDescription.append("modify "); } safeDescription.append(resource.getSafeDescription()).append("."); return safeDescription.toString(); }) .build(); final AuthorizationResult result = authorizer.authorize(request); if (Result.ResourceNotFound.equals(result.getResult())) { final Authorizable parent = getParentAuthorizable(); if (parent == null) { throw new AccessDeniedException("No applicable policies could be found."); } else { // create a custom authorizable to override the safe description but still defer to the parent authorizable final Authorizable parentProxy = new Authorizable() { @Override public Authorizable getParentAuthorizable() { return parent.getParentAuthorizable(); } @Override public Resource getResource() { final Resource parentResource = parent.getResource(); return new Resource() { @Override public String getIdentifier() { return parentResource.getIdentifier(); } @Override public String getName() { return parentResource.getName(); } @Override public String getSafeDescription() { return resource.getSafeDescription(); } }; } }; parentProxy.authorize(authorizer, action, user, resourceContext); } } else if (Result.Denied.equals(result.getResult())) { throw new AccessDeniedException(result.getExplanation()); } }
Example 3
Source File: TestRangerNiFiAuthorizer.java From localization_nifi with Apache License 2.0 | 4 votes |
@Test @Ignore public void testIntegration() { final AuthorizerInitializationContext initializationContext = Mockito.mock(AuthorizerInitializationContext.class); final AuthorizerConfigurationContext configurationContext = Mockito.mock(AuthorizerConfigurationContext.class); when(configurationContext.getProperty(eq(RangerNiFiAuthorizer.RANGER_SECURITY_PATH_PROP))) .thenReturn(new MockPropertyValue("src/test/resources/ranger/ranger-nifi-security.xml")); when(configurationContext.getProperty(eq(RangerNiFiAuthorizer.RANGER_AUDIT_PATH_PROP))) .thenReturn(new MockPropertyValue("src/test/resources/ranger/ranger-nifi-audit.xml")); Authorizer authorizer = new RangerNiFiAuthorizer(); try { authorizer.initialize(initializationContext); authorizer.onConfigured(configurationContext); final AuthorizationRequest request = new AuthorizationRequest.Builder() .resource(new Resource() { @Override public String getIdentifier() { return "/system"; } @Override public String getName() { return "/system"; } @Override public String getSafeDescription() { return "system"; } }) .action(RequestAction.WRITE) .identity("admin") .resourceContext(new HashMap<>()) .accessAttempt(true) .anonymous(false) .build(); final AuthorizationResult result = authorizer.authorize(request); Assert.assertEquals(AuthorizationResult.denied().getResult(), result.getResult()); } finally { authorizer.preDestruction(); } }
Example 4
Source File: Authorizable.java From nifi with Apache License 2.0 | 4 votes |
/** * Returns the result of an authorization request for the specified user for the specified action on the specified * resource. This method does not imply the user is directly attempting to access the specified resource. If the user is * attempting a direct access use Authorizable.authorize(). * * @param authorizer authorizer * @param action action * @param user user * @return is authorized */ default AuthorizationResult checkAuthorization(Authorizer authorizer, RequestAction action, NiFiUser user, Map<String, String> resourceContext) { if (user == null) { return AuthorizationResult.denied("Unknown user."); } final Map<String,String> userContext; if (user.getClientAddress() != null && !user.getClientAddress().trim().isEmpty()) { userContext = new HashMap<>(); userContext.put(UserContextKeys.CLIENT_ADDRESS.name(), user.getClientAddress()); } else { userContext = null; } final Resource resource = getResource(); final Resource requestedResource = getRequestedResource(); final AuthorizationRequest request = new AuthorizationRequest.Builder() .identity(user.getIdentity()) .groups(user.getGroups()) .anonymous(user.isAnonymous()) .accessAttempt(false) .action(action) .resource(resource) .requestedResource(requestedResource) .resourceContext(resourceContext) .userContext(userContext) .explanationSupplier(() -> { // build the safe explanation final StringBuilder safeDescription = new StringBuilder("Unable to "); if (RequestAction.READ.equals(action)) { safeDescription.append("view "); } else { safeDescription.append("modify "); } safeDescription.append(resource.getSafeDescription()).append("."); return safeDescription.toString(); }) .build(); // perform the authorization final AuthorizationResult result = authorizer.authorize(request); // verify the results if (Result.ResourceNotFound.equals(result.getResult())) { final Authorizable parent = getParentAuthorizable(); if (parent == null) { return AuthorizationResult.denied("No applicable policies could be found."); } else { // create a custom authorizable to override the safe description but still defer to the parent authorizable final Authorizable parentProxy = new Authorizable() { @Override public Authorizable getParentAuthorizable() { return parent.getParentAuthorizable(); } @Override public Resource getRequestedResource() { return requestedResource; } @Override public Resource getResource() { final Resource parentResource = parent.getResource(); return new Resource() { @Override public String getIdentifier() { return parentResource.getIdentifier(); } @Override public String getName() { return parentResource.getName(); } @Override public String getSafeDescription() { return resource.getSafeDescription(); } }; } }; return parentProxy.checkAuthorization(authorizer, action, user, resourceContext); } } else { return result; } }
Example 5
Source File: TestRangerNiFiAuthorizer.java From nifi with Apache License 2.0 | 4 votes |
@Test @Ignore public void testIntegration() { final AuthorizerInitializationContext initializationContext = Mockito.mock(AuthorizerInitializationContext.class); final AuthorizerConfigurationContext configurationContext = Mockito.mock(AuthorizerConfigurationContext.class); when(configurationContext.getProperty(eq(RangerNiFiAuthorizer.RANGER_SECURITY_PATH_PROP))) .thenReturn(new MockPropertyValue("src/test/resources/ranger/ranger-nifi-security.xml")); when(configurationContext.getProperty(eq(RangerNiFiAuthorizer.RANGER_AUDIT_PATH_PROP))) .thenReturn(new MockPropertyValue("src/test/resources/ranger/ranger-nifi-audit.xml")); Authorizer authorizer = new RangerNiFiAuthorizer(); try { authorizer.initialize(initializationContext); authorizer.onConfigured(configurationContext); final AuthorizationRequest request = new AuthorizationRequest.Builder() .resource(new Resource() { @Override public String getIdentifier() { return "/system"; } @Override public String getName() { return "/system"; } @Override public String getSafeDescription() { return "system"; } }) .action(RequestAction.WRITE) .identity("admin") .resourceContext(new HashMap<>()) .accessAttempt(true) .anonymous(false) .build(); final AuthorizationResult result = authorizer.authorize(request); Assert.assertEquals(AuthorizationResult.denied().getResult(), result.getResult()); } finally { authorizer.preDestruction(); } }