Class AuthorizationUtils
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final com.google.common.base.Function<String,ResourceAction> Function for the common pattern of generating a resource-action for reading from a datasource, using the datasource name.static final com.google.common.base.Function<String,ResourceAction> Function for the common pattern of generating a resource-action for reading from a datasource, using the datasource name. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic AuthenticationResultauthenticationResultFromRequest(javax.servlet.http.HttpServletRequest request) Returns the authentication information for a request.static AuthorizationResultauthorizeAllResourceActions(javax.servlet.http.HttpServletRequest request, Iterable<ResourceAction> resourceActions, AuthorizerMapper authorizerMapper) Performs authorization check on a list of resource-actions based on the authentication fields from the request.static AuthorizationResultauthorizeAllResourceActions(AuthenticationResult authenticationResult, Iterable<ResourceAction> resourceActions, AuthorizerMapper authorizerMapper) Performs authorization check on a list of resource-actions based on the authenticationResult.static AuthorizationResultauthorizeResourceAction(javax.servlet.http.HttpServletRequest request, ResourceAction resourceAction, AuthorizerMapper authorizerMapper) Performs authorization check on a single resource-action based on the authentication fields from the request.static AuditInfobuildAuditInfo(javax.servlet.http.HttpServletRequest request) Builds an AuditInfo for the given request by extracting the following from it: HeaderAuditManager.X_DRUID_AUTHORHeaderAuditManager.X_DRUID_COMMENTAttributeAuthConfig.DRUID_AUTHENTICATION_RESULTIP address usingServletRequest.getRemoteAddr()static RequestInfobuildRequestInfo(String service, javax.servlet.http.HttpServletRequest request) Builds a RequestInfo object that can be used for auditing purposes.static ResourceActioncreateExternalResourceReadAction(String resourceName) static <ResType> Iterable<ResType>filterAuthorizedResources(javax.servlet.http.HttpServletRequest request, Iterable<ResType> resources, com.google.common.base.Function<? super ResType, Iterable<ResourceAction>> resourceActionGenerator, AuthorizerMapper authorizerMapper) Return an iterable of authorized resources, by filtering the input resources with authorization checks based on the authentication fields from the request.filterAuthorizedResources(javax.servlet.http.HttpServletRequest request, Map<KeyType, List<ResType>> unfilteredResources, com.google.common.base.Function<? super ResType, Iterable<ResourceAction>> resourceActionGenerator, AuthorizerMapper authorizerMapper) Return a map of authorized resources, by filtering the input resources with authorization checks based on the authentication fields from the request.static <ResType> Iterable<ResType>filterAuthorizedResources(AuthenticationResult authenticationResult, Iterable<ResType> resources, com.google.common.base.Function<? super ResType, Iterable<ResourceAction>> resourceActionGenerator, AuthorizerMapper authorizerMapper) Return an iterable of authorized resources, by filtering the input resources with authorization checks based on authenticationResult.static <T> List<T>filterByAuthorizedDatasources(javax.servlet.http.HttpServletRequest request, List<T> resources, com.google.common.base.Function<T, String> getDatasource, AuthorizerMapper authorizerMapper) Filters the given datasource-related resources on the basis of datasource permissions.static StringgetAuthenticatedIdentity(javax.servlet.http.HttpServletRequest request) Extracts the identity from the authentication result if set as an atrribute of this request.static List<ResourceAction>This method constructs a 'superuser' set of permissions composed ofAction.READandAction.WRITEpermissions for all knownResourceType.knownTypes()for anyAuthorizerimplementation which is built on pattern matching with a regex.static voidsetRequestAuthorizationAttributeIfNeeded(javax.servlet.http.HttpServletRequest request) Sets theAuthConfig.DRUID_AUTHORIZATION_CHECKEDattribute in theHttpServletRequestto true.static voidverifyUnrestrictedAccessToDatasource(javax.servlet.http.HttpServletRequest req, String datasource, AuthorizerMapper authorizerMapper) Verifies that the user has unrestricted access to perform the required action on the given datasource.
-
Field Details
-
DATASOURCE_READ_RA_GENERATOR
public static final com.google.common.base.Function<String,ResourceAction> DATASOURCE_READ_RA_GENERATORFunction for the common pattern of generating a resource-action for reading from a datasource, using the datasource name. -
DATASOURCE_WRITE_RA_GENERATOR
public static final com.google.common.base.Function<String,ResourceAction> DATASOURCE_WRITE_RA_GENERATORFunction for the common pattern of generating a resource-action for reading from a datasource, using the datasource name.
-
-
Constructor Details
-
AuthorizationUtils
public AuthorizationUtils()
-
-
Method Details
-
authorizeResourceAction
public static AuthorizationResult authorizeResourceAction(javax.servlet.http.HttpServletRequest request, ResourceAction resourceAction, AuthorizerMapper authorizerMapper) Performs authorization check on a single resource-action based on the authentication fields from the request.This function will set the DRUID_AUTHORIZATION_CHECKED attribute in the request. If this attribute is already set when this function is called, an exception is thrown.
- Parameters:
request- HTTP request to be authorizedresourceAction- A resource identifier and the action to be taken the resource.authorizerMapper- The singleton AuthorizerMapper instance- Returns:
- AuthorizationResult containing allow/deny access to the resource action, along with policy restrictions.
-
verifyUnrestrictedAccessToDatasource
public static void verifyUnrestrictedAccessToDatasource(javax.servlet.http.HttpServletRequest req, String datasource, AuthorizerMapper authorizerMapper) Verifies that the user has unrestricted access to perform the required action on the given datasource.- Throws:
ForbiddenException- if the user does not have unrestricted access to perform the required action on the given datasource.
-
authenticationResultFromRequest
public static AuthenticationResult authenticationResultFromRequest(javax.servlet.http.HttpServletRequest request) Returns the authentication information for a request.- Parameters:
request- http request- Returns:
- authentication result
- Throws:
IllegalStateException- if the request was not authenticated
-
getAuthenticatedIdentity
@Nullable public static String getAuthenticatedIdentity(javax.servlet.http.HttpServletRequest request) Extracts the identity from the authentication result if set as an atrribute of this request. -
buildAuditInfo
Builds an AuditInfo for the given request by extracting the following from it:- Header
AuditManager.X_DRUID_AUTHOR - Header
AuditManager.X_DRUID_COMMENT - Attribute
AuthConfig.DRUID_AUTHENTICATION_RESULT - IP address using
ServletRequest.getRemoteAddr()
- Header
-
buildRequestInfo
public static RequestInfo buildRequestInfo(String service, javax.servlet.http.HttpServletRequest request) Builds a RequestInfo object that can be used for auditing purposes. -
authorizeAllResourceActions
public static AuthorizationResult authorizeAllResourceActions(AuthenticationResult authenticationResult, Iterable<ResourceAction> resourceActions, AuthorizerMapper authorizerMapper) Performs authorization check on a list of resource-actions based on the authenticationResult.If one of the resource-actions denys access, returns deny access immediately.
- Parameters:
authenticationResult- Authentication result representing identity of requesterresourceActions- An Iterable of resource-actions to authorize- Returns:
- AuthorizationResult containing allow/deny access to the resource actions, along with policy restrictions.
-
authorizeAllResourceActions
public static AuthorizationResult authorizeAllResourceActions(javax.servlet.http.HttpServletRequest request, Iterable<ResourceAction> resourceActions, AuthorizerMapper authorizerMapper) Performs authorization check on a list of resource-actions based on the authentication fields from the request.If one of the resource-actions denys access, returns deny access immediately.
This function will set the DRUID_AUTHORIZATION_CHECKED attribute in the request. If this attribute is already set when this function is called, an exception is thrown.
- Parameters:
request- HTTP request to be authorizedresourceActions- An Iterable of resource-actions to authorize- Returns:
- AuthorizationResult containing allow/deny access to the resource actions, along with policy restrictions.
-
setRequestAuthorizationAttributeIfNeeded
public static void setRequestAuthorizationAttributeIfNeeded(javax.servlet.http.HttpServletRequest request) Sets theAuthConfig.DRUID_AUTHORIZATION_CHECKEDattribute in theHttpServletRequestto true. This method is generally used when noResourceActionneed to be checked for the API. If resources are present, users should callauthorizeAllResourceActions(HttpServletRequest, Iterable, AuthorizerMapper) -
filterAuthorizedResources
public static <ResType> Iterable<ResType> filterAuthorizedResources(javax.servlet.http.HttpServletRequest request, Iterable<ResType> resources, com.google.common.base.Function<? super ResType, Iterable<ResourceAction>> resourceActionGenerator, AuthorizerMapper authorizerMapper) Return an iterable of authorized resources, by filtering the input resources with authorization checks based on the authentication fields from the request. This method does:- For every resource, resourceActionGenerator generates an Iterable of ResourceAction or null.
- If null, continue with next resource. If any resource-action in the iterable has deny-access, continue with next resource. Only when every resource-action has allow-access, add the resource to the result.
This function will set the DRUID_AUTHORIZATION_CHECKED attribute in the request. If this attribute is already set when this function is called, an exception is thrown.
- Parameters:
request- HTTP request to be authorizedresources- resources to be processed into resource-actionsresourceActionGenerator- Function that creates an iterable of resource-actions from a resourceauthorizerMapper- authorizer mapper- Returns:
- Iterable containing resources that were authorized
-
filterAuthorizedResources
public static <ResType> Iterable<ResType> filterAuthorizedResources(AuthenticationResult authenticationResult, Iterable<ResType> resources, com.google.common.base.Function<? super ResType, Iterable<ResourceAction>> resourceActionGenerator, AuthorizerMapper authorizerMapper) Return an iterable of authorized resources, by filtering the input resources with authorization checks based on authenticationResult. This method does:- For every resource, resourceActionGenerator generates an Iterable of ResourceAction or null.
- If null, continue with next resource. If any resource-action in the iterable has deny-access, continue with next resource. Only when every resource-action has allow-access, add the resource to the result.
- Parameters:
authenticationResult- Authentication result representing identity of requesterresources- resources to be processed into resource-actionsresourceActionGenerator- Function that creates an iterable of resource-actions from a resourceauthorizerMapper- authorizer mapper- Returns:
- Iterable containing resources that were authorized
-
filterAuthorizedResources
public static <KeyType,ResType> Map<KeyType,List<ResType>> filterAuthorizedResources(javax.servlet.http.HttpServletRequest request, Map<KeyType, List<ResType>> unfilteredResources, com.google.common.base.Function<? super ResType, Iterable<ResourceAction>> resourceActionGenerator, AuthorizerMapper authorizerMapper) Return a map of authorized resources, by filtering the input resources with authorization checks based on the authentication fields from the request. This method does:- For every resource, resourceActionGenerator generates an Iterable of ResourceAction or null.
- If null, continue with next resource. If any resource-action in the iterable has deny-access, continue with next resource. Only when every resource-action has allow-access, add the resource to the result.
This function will set the DRUID_AUTHORIZATION_CHECKED attribute in the request. If this attribute is already set when this function is called, an exception is thrown.
- Parameters:
request- HTTP request to be authorizedunfilteredResources- Map of resource lists to be filteredresourceActionGenerator- Function that creates an iterable of resource-actions from a resourceauthorizerMapper- authorizer mapper- Returns:
- Map containing lists of resources that were authorized
-
filterByAuthorizedDatasources
public static <T> List<T> filterByAuthorizedDatasources(javax.servlet.http.HttpServletRequest request, List<T> resources, com.google.common.base.Function<T, String> getDatasource, AuthorizerMapper authorizerMapper) Filters the given datasource-related resources on the basis of datasource permissions.- Returns:
- List of resources to which the user has access, based on whether the user has access to the underlying datasource or not.
-
createExternalResourceReadAction
-
makeSuperUserPermissions
This method constructs a 'superuser' set of permissions composed ofAction.READandAction.WRITEpermissions for all knownResourceType.knownTypes()for anyAuthorizerimplementation which is built on pattern matching with a regex.Note that if any
Resourceexist that use custom types not registered withResourceType.registerResourceType(java.lang.String), those permissions will not be included in this list and will need to be added manually.
-