This guide explains how to set up single sign-on (SSO) for applications in an Alibaba Cloud Service Mesh (ASM) instance.
Prerequisites
-
You have created an ASM Enterprise Edition instance. 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 default namespace. For more information, see Enable automatic sidecar injection.
Background information
Many applications require access control, granting access only to authenticated users. As systems become more complex, single sign-on (SSO) is an effective solution that allows users to log in to multiple systems with a single account. The Custom Authorization Service in ASM allows you to implement SSO for your mesh applications with no code changes.
Using ASM to implement SSO can reduce application refactoring and O&M costs. This guide uses an Alibaba Cloud IDaaS instance as the identity provider (IdP) to enable SSO for services within the mesh. Instead of building authentication and authorization logic into your applications, you can configure ASM to use a custom authorization service. This service integrates with Alibaba Cloud IDaaS or any OIDC-compliant IdP to handle the authorization and login flow. After a user authenticates successfully, the request and the user's identity information are forwarded to the application, which can then perform business logic based on that identity.
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. |
Step 1: Create an IDaaS instance and a test user
-
Log on to the EIAM console and create an Alibaba Cloud IDaaS instance.
NoteAn Alibaba Cloud IDaaS instance acts as a self-contained account system. Creating an instance is equivalent to creating an independent identity management system.
-
On the EIAM page, click the name of the Alibaba Cloud IDaaS instance.
-
In the left-side navigation pane, choose .
-
On the Account page, click the Account tab, and then click Create User.
-
In the Create User panel, enter the user information and click OK.
Step 2: Add and configure an OIDC application
Applications in your Alibaba Cloud IDaaS instance act as interfaces. The IDaaS instance can expose these interfaces in various ways to enable SSO. This guide uses an OIDC application to demonstrate the process.
-
In the left-side navigation pane of the EIAM console, click Application Management.
-
On the Application Management page, click Add Application.
-
On the Add Application page, click the Standard Protocols tab. In the OIDC card, click Add Application.
-
In the Add Application-OIDC dialog box, enter an Application Name and click Add.
-
On the OIDC Application details page, select .
-
On the SSO configuration tab, set the Redirect URIs. Click Show Advanced Settings, select the user information to obtain in the scopes section, and then click Save.
In the left-side navigation pane, click Applications. Select your target application, click the Sign-in tab, and go to the SSO subtab. Available scopes include
openid,email,phone, andprofile.NoteIn this example, the Redirect URI is set to
http://{IP_address_of_the_ingress_gateway}/oauth2/callback. Replace{IP_address_of_the_ingress_gateway}with the actual IP address of your ingress gateway. -
On the OIDC Application details page, select , and click Authorize.
-
In the Authorize dialog box, grant permissions to the user that you created in Step 1, and then click Confirm.
After you configure the OIDC application, record the Issuer, client_id, and client_secret. You will need this information to configure SSO in ASM.
-
Issuer: In your OIDC application, you can obtain this value from . Issuer: Obtain this value from the Application Configuration Information section at the bottom of the Login Access > Single Sign-On page in your OIDC application. This field is used to verify the source of token issuance and is also the
baseUrlfor endpoint URLs. -
client_id and client_secret: On the details page of the OIDC application, find these values in the General section.
Step 3: Deploy and expose the test application
This guide uses the httpbin application for testing. This application helps inspect request details, which simplifies verifying the information obtained from the IdP after a successful login.
-
Use the following YAML to deploy the httpbin application to the
defaultnamespace of your ACK cluster. -
Use the following YAML to configure a Gateway resource for your ASM instance. For more information, see Manage Istio gateways.
apiVersion: networking.istio.io/v1beta1 kind: Gateway metadata: name: ingressgateway namespace: istio-system spec: selector: app: istio-ingressgateway servers: - hosts: - '*' port: name: http number: 80 protocol: HTTP -
Use the following YAML to apply a virtual service to your ASM instance. For more information, see Manage virtual services.
This virtual service defines a default route to the httpbin application.
apiVersion: networking.istio.io/v1beta1 kind: VirtualService metadata: name: ingressgateway-vs namespace: istio-system spec: gateways: - ingressgateway hosts: - '*' http: - name: default route: - destination: host: httpbin.default.svc.cluster.local port: number: 8000 -
After you configure the Gateway and virtual service, you can access the httpbin service by running the
curl -I http://{IP_address_of_the_ingress_gateway}:80command.Sample output:
curl -I http://{IP_address_of_the_ingress_gateway}:80 HTTP/1.1 200 OK server: istio-envoy date: Fri, 19 Aug 2022 07:39:58 GMT content-type: text/html; charset=utf-8 content-length: 9593 access-control-allow-origin: * access-control-allow-credentials: true x-envoy-upstream-service-time: 3
Step 4: Enable custom authorization and configure 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 page that appears, click Define Custom Authorization Service.
-
On the Define External Authorization Service page, click the OIDC Authz and Authn Service tab, configure the parameters, and then click Create.
NoteUse the OIDC application information that you obtained in Step 2. You can use the IP address of the ingress gateway as the redirect URI for logins. For more information about cookie secrets, see Generating a Cookie Secret.
-
Use the KubeConfig of the data plane cluster to run the following command to obtain the domain name of the OIDC external authorization service.
kubectl get svc -n istio-system|grep oauth2proxy|awk -F' ' '{print $1}' -
Use the following YAML to create a virtual service that receives authentication requests from the ingress gateway.
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: # The domain name of the external authorization service obtained in the previous step. port: number: 4180ImportantTo prevent conflicts, do not create other virtual services that match the
/oauth2path prefix.
Step 5: Create an authorization policy
-
On the details page of the ASM instance, choose in the left-side navigation pane. On the page that appears, click Create from YAML.
-
On the Create page, select a Namespaces and a Scenario Template. Enter the following YAML, and then click Create.
apiVersion: security.istio.io/v1beta1 kind: AuthorizationPolicy metadata: name: oidc namespace: istio-system spec: action: CUSTOM provider: name: httpextauth-oidc # The name of the custom authorization service created in the previous step. rules: - {} selector: matchLabels: istio: ingressgatewayNoteThis authorization policy applies to all requests sent to the ingress gateway.
Step 6: Verify the result
-
In a web browser, go to http://{IP_address_of_the_ingress_gateway}:80.
The OAuth2 Proxy login page appears, containing a Sign in with OpenID Connect button. This indicates that OAuth2 Proxy deployed and integrated successfully with OpenID Connect, and that SSO is active.
-
Click Sign in with OpenID Connect.
Expected result: The Alibaba Cloud IDaaS login page appears. The page contains input fields for Account name, mobile number, or email and Password, and a Log On button. This confirms that the OIDC SSO configuration is working.
-
On the Alibaba Cloud IDaaS login page, enter the credentials for the test user that you created in Step 1 and click Log On.
Expected result: After you log in successfully, the httpbin.org Swagger API documentation page appears for version 0.9.2. The page displays the service description, "A simple HTTP Request & Response Service," and lists API groups such as HTTP Methods, Auth, Status codes, and Request inspection. This confirms that the Alibaba Cloud IDaaS SSO configuration is valid.
-
Click Request inspection, then choose /headers > Try it out > Execute.
Expected result: After you execute the request, the server returns a
200 OKstatus code. The response body includes anAuthorizationfield that contains a Bearer JWT, which indicates that JWT authorization is working.{ "headers": { "Accept": "application/json", "Accept-Encoding": "gzip, deflate", "Accept-Language": "zh-CN,zh;q=0.9", "Authorization": "Bearer eyJraWQiOiJLRVkxM3d0ekhLSmdEbkFmd1lQR0RqemM5RFUzTkwSNXd1S0wiLC3hbGciOiJSUzI1NiJ9.eyJzdWIiOiJlc2VyVyX29xemw3Z3FQb2lrb2xtNG9xY29mMndlcGhtIiwinRpi-joiand0X2FhYWFkYXZ2aG12Y2JpdDRnbDVsNGNudDR5d2FwZDI3cnFkd2Z0eDI3cnFkZTByc2FtcnFkd2Z0ZDI3cnFkZTByc2FtIixxx.xxxb51hcGktY24taGFuZ3pobxxx.xxxMuY29xxx.3yiL2xxx.Fx3R1bjI1b3NkbG16Nmd0cWZxM2oycHpwZWZx-L2FwcF9tbDV0emFwc2w3em1mbzUzd2xxx.xxxaGFzaCI6IkUxcm9oRnlpR2FZYmJnTS.xxxtudHZpIiwiYXRf-IrODYgMTg2IDkxNzEgMjQ5MS.xxx.xxx.BanFbeM28p\nYp-aiEymtvy-QC-6VjohuSSi.xxx\nE9c3RTMntC66D7fxTGME2x.xxx.Et\nrwPRUTM-2AF74y2NVFtpNdkxbLenyz1duwTpeGANmgLGxxxJrZvKyNxBjAV6TZjGrUVVNICbU1_sg", "...": "..." } } -
Copy the JWT from the Bearer token in the previous step and parse it with a JWT debugger.
For more information, see JWT.io.
Expected result: The parsed JWT shows that the
algin the HEADER isRS256. The PAYLOAD contains standard OIDC ID Token claims, such assub,jti,iss(which is the OIDC endpoint of your EIAM instance),iat,nbf,exp,aud,at_hash,c_hash,email,email_verified,phone_number,phone_number_verified,name,preferred_username, andupdated_at. This confirms that the JWT contains the user information from Alibaba Cloud IDaaS and that ASM has successfully validated it.