Tutorial: Creating a Custom Spring Security Profile
Create a custom Spring Security profile to define a new user role with specific access restrictions to Data Hub REST endpoints.
Context
Data Hub ships with a default Spring security profile that includes definitions for an admin user, and a read only user. If you want to define an additional role to gain more granular control over Data Hub access, you can do so in a custom security profile. With a custom security profile, you can, for example:
- allow a user to load and compose data, but not publish
- restrict a user to only using the GET and POST methods, but not PUT or DELETE
Both of these examples are included in this tutorial. Many more possibilities exist for your own custom security profile.
Procedure
- Create a new Maven project with at least the following structure:
In our example, the project is called new-security-basic.
- Edit the pom.xml file to include your artifact ID, version, and packaging.
<groupId>com.example</groupId> <artifactId>new-security-basic</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging>
- Create a new Data Hub extension XML file in the src/main/resources/META-INF directory. Use the naming schema <artifactId>-datahub-extension.xml, and add the following content:
Ensure that you provide your extension name. Once again, we've called ours new-security-basic.
<extension xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.hybris.com/schema/" xsi:schemaLocation="http://www.hybris.com/schema/ http://www.hybris.com/schema/datahub-metadata-schema-1.3.0.xsd" name="new-security-basic"> </extension>
- Create a second Spring XML file in the same directory called <artifactId>-datahub-extension-spring.xml, with the following content.
<?xml version="1.0" encoding="UTF-8"?> <!-- [y] hybris Platform Copyright (c) 2017 SAP SE or an SAP affiliate company. All rights reserved. This software is the confidential and proprietary information of SAP ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in accordance with the terms of the license agreement you entered into with SAP. --> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:security="http://www.springframework.org/schema/security" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd"> <bean id="channelProtocol" class="java.lang.String"> <constructor-arg value="#{${datahub.security.https.enabled} ? 'https' : 'http'}"/> </bean> <beans profile="default"> <security:authentication-manager alias="datahubAuthenticationManager"> <security:authentication-provider user-service-ref="userDetailsService" /> </security:authentication-manager> <!-- Secured URLs --> <security:http use-expressions="true" authentication-manager-ref="datahubAuthenticationManager"> <security:custom-filter ref="requestContextFilter" before="BASIC_AUTH_FILTER" /> <security:intercept-url pattern="/**" requires-channel="#{channelProtocol}" /> <security:intercept-url pattern="/**" method="GET" access="hasAnyRole('ROLE_DATAHUB_ADMIN','ROLE_DATAHUB_RO')"/> <security:intercept-url pattern="/**" method="POST" access="hasRole('ROLE_DATAHUB_INBOUND')" /> <security:intercept-url pattern="/**" method="DELETE" access="hasRole('ROLE_DATAHUB_ADMIN')" /> <security:intercept-url pattern="/**" method="PUT" access="hasRole('ROLE_DATAHUB_INBOUND')" /> <security:intercept-url pattern="/**" access="denyAll" /> <security:http-basic /> <security:csrf disabled="true" /> <security:port-mappings> <security:port-mapping http="${datahub.tomcat.http.port}" https="${datahub.tomcat.https.port}"/> <security:port-mapping http="80" https="443"/> </security:port-mappings> </security:http> <bean id="requestContextFilter" class="org.springframework.web.filter.RequestContextFilter" /> <bean class="com.hybris.datahub.spring.security.AuthenticationFailureApplicationListener" /> <security:user-service id="userDetailsService"> <security:user name="${datahub.security.basic.admin.user}" password="${datahub.security.basic.admin.password}" authorities="ROLE_DATAHUB_ADMIN,ROLE_DATAHUB_RO,ROLE_DATAHUB_CONTENT_OWNER,ROLE_DATAHUB_INBOUND" /> <security:user name="${datahub.security.basic.developer.user}" password="${datahub.security.basic.developer.password}" authorities="ROLE_DATAHUB_ADMIN,ROLE_DATAHUB_RO,ROLE_DATAHUB_INBOUND" /> <security:user name="${datahub.security.basic.read_only.user}" password="${datahub.security.basic.read_only.password}" authorities="ROLE_DATAHUB_RO" /> <security:user name="${datahub.security.basic.content_owner.user}" password="${datahub.security.basic.content_owner.password}" authorities="ROLE_DATAHUB_RO,ROLE_DATAHUB_CONTENT_OWNER" /> <security:user name="${datahub.security.basic.inbound.user}" password="${datahub.security.basic.inbound.password}" authorities="ROLE_DATAHUB_INBOUND" /> </security:user-service> </beans> <beans profile="no_auth"> <bean id="noAuthProvider" class="com.hybris.datahub.spring.security.NoAuthProfileAuthenticationProvider" /> <security:authentication-manager alias="noAuthenticationManager"> <security:authentication-provider ref="noAuthProvider" /> </security:authentication-manager> <security:http use-expressions="false" authentication-manager-ref="noAuthenticationManager"> <security:intercept-url pattern="/**" requires-channel="#{channelProtocol}"/> <security:http-basic /> <security:csrf disabled="true" /> <security:port-mappings> <security:port-mapping http="${datahub.tomcat.http.port}" https="${datahub.tomcat.https.port}"/> <security:port-mapping http="80" https="443"/> </security:port-mappings> </security:http> <bean id="springSecurityFilterChain" class="com.hybris.datahub.spring.security.NoSecurityFilterChain" /> </beans> </beans>This file duplicates the content of the default Data Hub security profile. In the rest of this tutorial, we rename and modify the profile to create a custom Spring security profile. In the steps that follow, you add a third user role with specific restrictions.
- Give your custom profile a new name by editing the profile name in the <beans> element.
In this example, we want to use the profile to define a new user role with access only for loading new data. We'll therefore call the new profile loaduser.
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ... profile="loaduser">
- Add a new user to the security:user-service element of your profile.
We'll add a user role called ROLE_DATAHUB_LOAD, with name and password variables as follows:
<!-- User Details --> <security:user-service id="userDetailsService"> <security:user name="${datahub.security.basic.admin.user}" password="${datahub.security.basic.admin.password}" authorities="ROLE_DATAHUB_ADMIN, ROLE_DATAHUB_RO" /> <security:user name="${datahub.security.basic.load.user}" password="${datahub.security.basic.load.password}" authorities="ROLE_DATAHUB_LOAD" /> <security:user name="${datahub.security.basic.read_only.user}" password="${datahub.security.basic.read_only.password}" authorities="ROLE_DATAHUB_RO" /> </security:user-service> - Add access definitions for the new load user in the security:http element. This element is the section under the <!-- Secured URLs --> comment.
In our example, we want to give the load user GET access to all endpoints, but access to the POST method only for item loading. We therefore modify our file as follows:
- Add the new ROLE_DATAHUB_LOAD user role to the list of roles allowed to access all endpoints with the GET method on line 20.
<!-- Secured URLs --> <security:http use-expressions="true" authentication-manager-ref="datahubAuthenticationManager"> <security:custom-filter ref="requestContextFilter" before="BASIC_AUTH_FILTER"/> <security:intercept-url pattern="/**" method="GET" access="hasAnyRole('ROLE_DATAHUB_ADMIN','ROLE_DATAHUB_RO','ROLE_DATAHUB_LOAD')" /> <security:intercept-url pattern="/**" method="POST" access="hasRole('ROLE_DATAHUB_ADMIN')" /> <security:intercept-url pattern="/**" method="DELETE" access="hasRole('ROLE_DATAHUB_ADMIN')" /> <security:intercept-url pattern="/**" method="PUT" access="hasRole('ROLE_DATAHUB_ADMIN')" /> <security:intercept-url pattern="/**" access="denyAll" /> <security:http-basic /> <security:csrf disabled="true"/> </security:http> - Add a new line to allow both the admin user and load user roles to perform data loading actions, as follows:
<!-- Secured URLs --> <security:http use-expressions="true" authentication-manager-ref="datahubAuthenticationManager"> <security:custom-filter ref="requestContextFilter" before="BASIC_AUTH_FILTER"/> <security:intercept-url pattern="/v1/data-feeds/*/items/*" method="POST" access="hasAnyRole('ROLE_DATAHUB_ADMIN','ROLE_DATAHUB_LOAD')" /> <security:intercept-url pattern="/**" method="GET" access="hasAnyRole('ROLE_DATAHUB_ADMIN','ROLE_DATAHUB_RO','ROLE_DATAHUB_LOAD')" /> <security:intercept-url pattern="/**" method="POST" access="hasRole('ROLE_DATAHUB_ADMIN')" /> <security:intercept-url pattern="/**" method="DELETE" access="hasRole('ROLE_DATAHUB_ADMIN')" /> <security:intercept-url pattern="/**" method="PUT" access="hasRole('ROLE_DATAHUB_ADMIN')" /> <security:intercept-url pattern="/**" access="denyAll" /> <security:http-basic /> <security:csrf disabled="true"/> </security:http>Note that this line must come first. In order for the restriction to work, more restrictive patterns must precede more general, catch-all patterns such as "/**".
- Add the new ROLE_DATAHUB_LOAD user role to the list of roles allowed to access all endpoints with the GET method on line 20.
- Save your project files once you are happy with the patterns you've defined.
Your completed project should now look like the following:
- Build the project using maven install, and copy the resulting JAR file to your extensions directory.
To run the Maven target in Eclipse, right click on the project pom.xml file, then choose
Run as ...
Maven install
. - Edit your local.properties file to add credentials for the new user role.
# The username and password for basic authentication of the load only user role datahub.security.basic.load.user=<loaduser> datahub.security.basic.load.password=<loadpass>
- Add the following to your local.properties file to ensure the value of your new username and password properties are not visible in log file output.
datahub.logging.additional.masked.properties=datahub.security.basic.load.user,datahub.security.basic.load.password
If you have defined more than one user role, simply add the related properties to this comma separated list.
- Edit your setenv.sh or setenv.bat file to specify your new security profile as the active profile when starting Data Hub.
The following example shows the setenv.sh CATALINA_OPTS for a stand-alone Data Hub instance. You can also specify the active profile to use on the command line with the -D option when starting up Data Hub.
# Example CATALINA_OPTS configuration for a stand-alone Data Hub CATALINA_OPTS="$CATALINA_OPTS -Dspring.profiles.active=loaduser -Ddatahub.cluster.seed.hostname=127.0.0.1 -Ddatahub.cluster.node.hostname=127.0.0.1 -Ddatahub.jmx.serviceurl.port=1110 -Dconfig.resource=datahub_cluster_seed.conf -Ddatahub.akka.ssl.workingDirectory=/usr/local/apache-tomcat-8.5.1/webapps/datahub-webapp/WEB-INF/classes" - Start Data Hub, and ensure that your new extension is loaded successfully.
You should see the following lines in your log output:
2017-01-04 15:01:24,631 [INFO] [o.s.b.f.x.XmlBeanDefinitionReader] Loading XML bean definitions from URL [jar:file:/opt/datahub/extensions/new-security-basic-0.0.1-SNAPSHOT.jar!/META-INF/new-security-basic-datahub-extension-spring.xml] 2017-01-04 15:01:24,646 [INFO] [o.s.s.c.SpringSecurityCoreVersion] You are running with Spring Security Core 4.1.3.RELEASE 2017-01-04 15:01:24,648 [INFO] [o.s.s.c.SecurityNamespaceHandler] Spring Security 'config' module version is 4.1.3.RELEASE 2017-01-04 15:01:24,683 [INFO] [o.s.s.c.h.FilterInvocationSecurityMetadataSourceParser] Creating access control expression attribute 'hasAnyRole('ROLE_DATAHUB_ADMIN','ROLE_DATAHUB_RO','ROLE_DATAHUB_LOAD')' for /** 2017-01-04 15:01:24,683 [INFO] [o.s.s.c.h.FilterInvocationSecurityMetadataSourceParser] Creating access control expression attribute 'hasAnyRole('ROLE_DATAHUB_ADMIN','ROLE_DATAHUB_LOAD')' for /v1/data-feeds/*/items/* 2017-01-04 15:01:24,683 [INFO] [o.s.s.c.h.FilterInvocationSecurityMetadataSourceParser] Creating access control expression attribute 'hasRole('ROLE_DATAHUB_ADMIN')' for /** 2017-01-04 15:01:24,683 [INFO] [o.s.s.c.h.FilterInvocationSecurityMetadataSourceParser] Creating access control expression attribute 'hasRole('ROLE_DATAHUB_ADMIN')' for /** 2017-01-04 15:01:24,683 [INFO] [o.s.s.c.h.FilterInvocationSecurityMetadataSourceParser] Creating access control expression attribute 'hasRole('ROLE_DATAHUB_ADMIN')' for /** 2017-01-04 15:01:24,683 [INFO] [o.s.s.c.h.FilterInvocationSecurityMetadataSourceParser] Creating access control expression attribute 'denyAll' for /** - Test your newly defined access parameters to ensure that they work as intended.
You can test the access to various REST endpoints using either a REST client, such as Postman, or a command-line tool such as cURL. Here we attempt to load data using the credentials we've provided for the new ROLE_DATAHUB_LOAD user. Given the access rules provided in the previous steps, the following command should result in a successful load.
curl -k -u loaduser:loadpass --data-binary @base_unique_items.csv -H "Content-Type:application/octet-stream" http://localhost:8080/datahub-webapp/v1/data-feeds/DEFAULT_FEED/items/RawProduct --verbose
The following command should return a 403 Unauthorized error. The load user role does not have access to the POST method for any other REST endpoints.
curl -k -u loaduser:loadpass --request POST http://localhost:8080/datahub-webapp/v1/pools/GLOBAL/compositions
Testing that same command with the admin user credentials should result in a successful composition. You can also test that the load user has access to all endpoints using the GET method.
Results
You now have a new security profile that you can use in place of the existing default profile. It can include any number of new user roles, allowing fine control over access to the various Data Hub REST endpoints, and available methods.