This topic describes how to use a self-hosted Keycloak instance as an identity provider (IdP) to enable single sign-on for applications in your service mesh. By configuring a Custom Authorization Service in ASM, you can use the OIDC protocol to delegate authentication and authorization to Keycloak. This eliminates the need for individual applications to implement their own security logic. After successful authorization, requests are forwarded to the application with the user's information from Keycloak.
Prerequisites
An ASM instance is created. For more information, see Create an ASM instance.
An ACK managed cluster is created. For more information, see Create an ACK managed cluster.
You have enabled automatic sidecar injection for the
defaultnamespace. For more information, see Enable automatic sidecar injection.
Concepts
|
Concept |
Description |
|
IdP |
An identity provider (IdP) is a service that creates, maintains, and manages user identities and provides authentication services. For example, when you use a Google account to sign in to a third-party application, Google acts as the IdP. |
|
OIDC |
OpenID Connect (OIDC) is an authentication protocol built on top of the OAuth 2.0 framework. For more information, see OpenID Connect. |
|
Scope |
In OIDC, scopes specify which user attributes (such as email and profile information) an application is allowed to access. During authentication, the IdP may ask the user to grant permission for the application to access specific scopes of their user data. |
Procedure
Step 1: Deploy the demo application and Keycloak
Use the following YAML manifest to deploy the httpbin application to the
defaultnamespace in your ACK managed cluster. Run thekubectl apply -n default -f xxx.yamlcommand.Use the following YAML manifest to deploy the Keycloak application to the
defaultnamespace in your ACK managed cluster. Run thekubectl apply -n default -f xxx.yamlcommand.
Step 2: Expose the application and Keycloak via gateway
In this example, you access the demo application over HTTPS (port 443) and the Keycloak console over HTTP (port 80).
Create a certificate for the HTTPS service and name it
myexample-credential. For more information, see Enable Secure HTTPS Services by Using an ASM Ingress Gateway.Use the following YAML to create a Gateway and a VirtualService in the ASM cluster to expose Keycloak to the public network.
In the ASM console, configure the Gateway for the corresponding ASM instance. For more information, see Manage gateway rules.
Apply the following VirtualService to the ASM instance. For more information, see Manage virtual services.
In your browser, navigate to http://${ASM_INGRESS_GATEWAY_ADDRESS} to access the Keycloak application.
Click Administration Console and log on with the admin credentials you configured during the Keycloak deployment.
Step 3: Configure Keycloak
In the left-side navigation pane of Keycloak, select Master from the dropdown list and click Create Realm.
On the new realm's configuration page, create a client. On the General Settings tab, set Client type to OpenID Connect and Client ID to oauth2proxy. On the Capability config tab, enable Client authentication, disable Authorization, and under Authentication flow, select only the Standard flow and Direct access grants checkboxes.
Create a user in the realm. Set Username to asm-test and enable Email verified. Then, click Save.
On the User details page, click the Credentials tab.
Set a password for the user. Set Temporary to Off and click Save.
Create a realm role. Set Role name to test-role and click Save.
On the User details page, select the Role mapping tab.
On the Role mapping page, click Assign role. Assign the
test-roleto the user.Create a client scope. Set Name to test_scope and Protocol to OpenID Connect. Enable Display on consent screen and Include in token scope. Then, click Save.
On the Client Scope page, click the Mappers tab and add a mapper. Set Mapper type to Audience and Name to test-mapper. For Included Client Audience, select oauth2proxy, and enable Add to access token. Then, click Save.
Click the Scope tab, select the checkbox next to the
test-rolerole, and then click Assign.On the client settings page, click the Client scopes tab and add the
test_scopeclient scope as a default scope.Add the Valid redirect URIs for the cluster's oauth2-proxy. On the Settings tab, in the Access settings section, enter
https://${ASM ingress gateway address}/oauth2/callbackin the Valid redirect URIs field and click Save.
In this example, only the Keycloak service uses the HTTP protocol. All other services use HTTPS. Therefore, the redirect URI format is https://${ASM_INGRESS_GATEWAY_ADDRESS}/oauth2/callback.
Keycloak is now configured. Record the following information:
The ID of the new realm: Test-oidc.
The client ID created in the realm: oauth2proxy.
The client secret on the Credentials subtab in the client settings.
Step 4: Enable custom authorization and OIDC SSO
-
Log on to the ASM console. In the left-side navigation pane, choose .
-
On the Mesh Management page, click the name of the ASM instance. In the left-side navigation pane, choose .
On the External Authorization Service page, click Register External Authorization Service and select OIDC Authz and Authn Service. Set the parameters for the OIDC Identity Authentication and Authorization Service and click Create.
In the Associate Custom Authorization Service panel, select OIDC Identity Authentication and Authorization Service as the type and configure the parameters.
Enter the information from the OIDC client that you created in Keycloak. You can use the ASM ingress gateway as the redirect URI for login. For more information about the cookie secret, see Generating a Cookie Secret.
IdP OIDC Issuer URL: http://${ASM_INGRESS_GATEWAY_ADDRESS}/realms/${REALM_NAME_IN_KEYCLOAK}.
ClientID and Client Secret: Use the values that you recorded in the previous step.
Scopes:
OpenIDis a required scope. You can specify other scopes.
Create a VirtualService by using the following YAML.
apiVersion: networking.istio.io/v1beta1 kind: VirtualService metadata: name: oauth2-vs namespace: istio-system spec: gateways: - ingressgateway hosts: - '*' http: - match: - uri: prefix: /oauth2 name: oauth2 route: - destination: host: asm-oauth2proxy-httpextauth-oidc.istio-system.svc.cluster.local port: number: 4180NoteReplace the value of the
hostfield in theroutesection with the service name of the oauth2-proxy in theistio-systemnamespace of your ACK cluster.To prevent VirtualService conflicts, avoid having other VirtualServices match paths with the prefix /oauth2.
Step 5: Create an authorization policy
-
On the Mesh Management page, click the name of the ASM instance. In the left-side navigation pane, choose .
On the AuthorizationPolicy page, click Create from YAML.
On the Create page, select a Namespaces and a Scenario Template, configure the following YAML manifest, and then click Create.
apiVersion: security.istio.io/v1beta1 kind: AuthorizationPolicy metadata: name: oidc namespace: istio-system spec: action: CUSTOM provider: name: httpextauth-oidc rules: - to: - operation: notPorts: - '80' selector: matchLabels: istio: ingressgatewayNoteThis AuthorizationPolicy specifies that all requests to ports other than port 80 require authorization.
For
provider.name, enter the name of your associated custom authorization service. You can find this name in the list on the Custom Authorization Service page.
Step 6: Verify the integration
In your browser, navigate to http://${ASM_INGRESS_GATEWAY_ADDRESS}:80.
Expected result: The OAuth2 Proxy login page appears with a Sign in with OpenID Connect button, indicating that single sign-on is enabled.
Click Sign in with OpenID Connect. On the Keycloak login page, enter the credentials for the test user created in Step 3 and click Log In.
Expected result: After you log in, the httpbin.org page is displayed. It includes API categories such as HTTP Methods, Auth, Status codes, and Request Inspection.
Click Request inspection, and then select .
Expected result: In the Server response section, the response body contains the request header information, including an Authorization: Bearer {JWT} field. This indicates that the request carries the JWT token issued by Keycloak.
Decode the JWT token from the previous step (the string that follows
Bearer) by using a JWT debugger. For more information about JWT debuggers, see JWT debugger.Expected result: After the token is successfully decoded, the decoded token shows that the algorithm in the HEADER is RS256 and the PAYLOAD contains claims such as realm_access (role information), preferred_username (username), and client_id (oauth2proxy). This confirms that the JWT has been verified by ASM and includes the user information stored in Keycloak.