AWS Big Data Blog
Single sign-on SSO for Amazon OpenSearch Service using SAML and Keycloak
A standard use case for customers is to integrate existing identity providers (IdPs) with Amazon OpenSearch Service. OpenSearch Service offers built-in support for single sign-on (SSO) authentication for OpenSearch Dashboards, and uses SAML protocol. The SAML authentication for OpenSearch Service lets you integrate your existing third-party IdPs, such as Okta, Ping Identity, OneLogin, Auth0, ADFS, Azure Active Directory, and Keycloak, with OpenSearch Service dashboards.
In this post, we walk you through how to configure service provider-initiated authentication for OpenSearch Dashboards by using OpenSearch Service and Keycloak. We also discuss how to set up users, groups, and roles in Keycloak and configure their access to OpenSearch Dashboards.
Solution overview
The following diagram illustrates the SAML authentication flow for this solution.
The sign-in flow consists of the following steps.
- The user opens a browser to navigate to the OpenSearch Dashboards endpoint of OpenSearch Service in a virtual private cloud (VPC), for example https://vpc-abc123.us-east-1.es.amazonaws.com/_dashboards.
- The service provider (OpenSearch Service) uses the information about the IdP (Keycloak) to generate a SAML authentication request. The service provider redirects SAML authentication requests back to the browser.
- The browser relays the SAML authentication request to Keycloak. Keycloak parses the SAML authentication request and asks for the user to insert their login and password to authenticate.
- After a successful authentication, Keycloak generates a SAML authentication response that includes authenticated user details from Keycloak and sends the encoded SAML response to the browser.
- The browser relays the SAML response to OpenSearch Service Assertion Consumer Service (ACS) URL.
- OpenSearch Service validates the SAML response. If the validation checks are passed, the user is redirected to the front page of OpenSearch Dashboards. The authorization is performed according to the roles mapped to the user.
Prerequisites
To complete this walkthrough, you should have the following set up:
- An OpenSearch Service domain running OpenSearch or Elasticsearch version 6.7 or later with fine-grained access control enabled within a VPC.
- Keycloak installed and configured. In this post, we created the IdP in the same VPC of the OpenSearch domain. There is no need for a direct connection between the IdP and the service provider, so you can have the IdP in a different network as well.
- A properly configured security group for OpenSearch Service and Keycloak IdP server to receive inbound traffic from users.
- A browser with network connectivity to both Keycloak and OpenSearch Dashboards.
Enable SAML authentication for OpenSearch Service
The first step is to enable SAML authentication for OpenSearch Service. Complete the following steps:
- On the OpenSearch Service console, open the details page for your OpenSearch Service domain.
- On the Security configuration tab, choose Edit.
- Select Enable SAML authentication.
Enabling this option automatically populates different IdP URLs, which is required to configure SAML support in the Keycloak IdP. Note down the values under Service provider entity ID and SP-initiated SSO URL. The OpenSearch Dashboards login flow can be configured either as service provider-initiated or IdP-initiated. The service provider-initiated login flow is initiated by OpenSearch Service, and the IdP-initiated login flow is initiated by the IdP (for example, Keycloak). In this post, we use a service provider-initiated login flow.
Configure Keycloak as IdP
During the SAML authentication process, when the user is authenticated, the browser receives a SAML assertion token from Keycloak and forwards it to OpenSearch Service. The OpenSearch Service domain authorizes the user with backend roles according to the attributes presented in the token.
To configure Keycloak as IdP, complete the following steps:
- Log in to the Keycloak IdP admin console with admin user privileges (for example,
https://<Keycloak server>:8081/admin/
). - Choose Create Realm.
- For Realm name, enter a name (for example,
Amazon_OpenSearch
) and choose Create.
For managing OpenSearch Service specific roles, users, and groups, you first create a separate client realm that provides a logical space to manage objects.
- In the navigation pane, choose your realm, then choose Clients.
- Choose Create client.
- In the General Settings window, for Client type, choose SAML
- For Client ID, use the service provider entity ID you copied earlier, then choose Next
- Under Login settings, enter the service provider-initiated SSO URL copied from earlier (for example,
https://vpc-abc123.us-east-1.es.amazonaws.com/_dashboards/_opendistro/_security/saml/acs
) and choose Save. - On the client settings tab, under Signature and Encryption, turn on Sign Assertions and keep all other options as default, then choose Save.
- On the Keys tab, under Signing keys config, turn Client signature required off.
Configure Keycloak users, roles, and groups
After you have configured the Keycloak IdP client for OpenSearch Service, you can create roles, groups, and users on the IdP side. For this post, we create two roles, two groups, and two users, as listed in the following table.
Users | Groups | Roles |
super_user_1 |
super_user_group |
super_user_role |
readonly_user_1 |
readonly_user_group |
readonly_user_role |
Complete the following steps:
- In the navigation pane for your realm, choose Realm roles.
- Choose Create role.
- For Role name, enter a name (for this post,
super_user_role
) and choose Save. - Repeat these steps to create a second role,
readonly_user_role
.
Now let’s create groups, assign the roles to the groups, and map the users to the groups.
- Under your realm, choose Groups in the navigation pane.
- Choose Create group.
- For Name, enter a group name (for example,
super_user_group
) and choose Save. - Repeat these steps to create a second group,
readonly_user_group
.
When the new groups are created, they will be listed on the Groups page.
- On the details page for each group, on the Role mapping tab, choose Assign role.
- For the group
super_user_group
, select the rolesuper_user_role
and choose Assign.
- Repeat these steps to assign the role
readonly_user_role
to the groupreadonly_user_group
.
The last step is to create users and assign them to groups so they automatically inherit group privileges. For this post, we create two users, super_user_1
and readonly_user_1
, with dashboard admin and dashboard read-only privileges, respectively.
- Under your realm, choose Users in the navigation pane.
- Choose Create new user.
- Under General, configure the user details, including user name, first name, last name, and email, then choose Create.
- Set a temporary password on the Credentials tab after you create the user.
- Choose Add user and repeat these steps to add your second user,
readonly_user_1
. - To join a user to a specific group, choose Join Group on the Groups tab of the respective user.
- Select the group the user is joining and choose Join. For example, the user
super_user_1
is joining the groupsuper_user_group
.
- Repeat these steps for the user
readonly_user_1
to join the groupreadonly_user_group
.
Next, you can remove the default role mapping for the users because you already assigned the roles to their respective groups.
- On the Role Mapping tab, select the default role.
- Unassign the default role for the user by choosing Unassign and then Remove.
- Repeat these steps for the other user.
- Choose Client scopes in the navigation pane.
- In the Name column, choose
role_list
.
- On the Mappers tab, choose role list.
- Turn on Single Role Attribute and choose Save.
Download SAML metadata from Keycloak
The configuration of Keycloak is now complete, so you can download the SAML metadata file from Keycloak. The SAML metadata is in XML format and is needed to configure SAML in the OpenSearch Service domain.
- Under your realm, choose Realm settings in the navigation pane.
- On the General tab, choose SAML 2.0 Identify Provider Metadata under Endpoints.
This will generate an IdP metadata file in another window. This XML file contains information on the provider, such as a TLS certificate, SSO endpoints, and the IdP entity ID.
- Download this XML file locally so you can upload this file on the OpenSearch Service console in later steps.
Integrate OpenSearch Service SAML with Keycloak
To integrate OpenSearch Service with the Keycloak IDP, you need to upload the IdP metadata XML file on the OpenSearch Service console.
- On the OpenSearch Service console, navigate to your domain.
- Choose Security configuration, then choose Edit.
- Under Metadata from IdP, choose Import from XML file to import the file and auto-populate the IdP entity ID.
Alternatively, you can copy and paste the contents of the entity ID property from the metadata file.
- For SAML master backend role, enter
super_user_role
.
This means that a user with this role is provided manager user privileges to the cluster, but can only use permissions within OpenSearch Dashboards.
- Expand the Additional settings section
- For Roles key, enter an attribute from the assertion (in our case, Role) and choose Save Changes.
Test the OpenSearch Dashboards SAML authentication with Keycloak
You’re now ready to test the SAML integration with Keycloak as an IdP.
- Choose the OpenSearch Dashboards URL provided on OpenSearch Service console.
It will automatically redirect you to the Keycloak sign-in page for authentication.
- Enter the admin user name (
super_user_1
) and password and choose Sign In.
Upon successful authentication, it will redirect you to the home page of OpenSearch Dashboards. If you encounter issues at this step, refer to SAML troubleshooting for common issues.
Internally, the security plugin maps the backend role super_user_role
to the reserved security roles all_access
and security_manager
. Therefore, Keycloak users with the backend role super_user_role
are authorized with the privileges of the manager user in the domain. To grant read-only dashboard access to user readonly_user_1
, log in to OpenSearch Dashboards as the user super_user_1
. Then map the role readonly_user_role
as a backend role for the reserved security role opensearch_dashboards_read_only
.
When establishing access control for the cluster, it’s crucial to carefully manage the permissions granted to users, adhering to the principle of least privilege. By having both super_user_role
with administrative capabilities and read-only readonly_user_role
, you can strike a balance. This approach allows a small number of trusted users to have full administrative access within OpenSearch Dashboards, while also enabling read-only access for other stakeholders who require visibility but don’t need more access.
At the time of writing, if you specify the <SingleLogoutService />
details in the Keycloak metadata XML, when you sign out from OpenSearch Dashboards, it will call Keycloak directly and try to sign the user out. This doesn’t work currently with some of the versions of OpenSearch Service, because Keycloak expects the sign-out request to be signed with a certificate that OpenSearch Service doesn’t currently support. If you remove <SingleLogoutService />
from the metadata XML file, OpenSearch Service will use its own internal sign-out mechanism and sign the user out on the OpenSearch Service side. No calls will be made to Keycloak for signing out.
Clean up
If you don’t want to continue using the solution, delete the resources you created:
- OpenSearch Service domain
- VPN and Keycloak instance
Conclusion
In this post, you learned how to configure Keycloak as an IdP to access OpenSearch Dashboards using SAML. To learn more about OpenSearch Service and SAML integration, refer to SAML authentication for OpenSearch Dashboards. Stay tuned for a series of posts focusing on SAML integrations with OpenSearch Service and Amazon OpenSearch Serverless.
About the Author
Sajeev is a Senior Cloud Engineer (Big Data & Analytics) and a Subject Matter Expert for Amazon OpenSearch Service. He works closely with AWS customers to provide them architectural and engineering assistance and guidance. He dives deep into big data technologies and streaming solutions and leads onsite and online sessions for customers to design the best solutions for their use cases.