All Products
Search
Document Center

Alibaba Cloud Service Mesh:Configure authorization policies for HTTP requests

Last Updated:Jan 30, 2024

To implement fine-grained control over service-to-service HTTP requests, you can configure authorization policies for HTTP requests to manage service interaction permissions. This guarantees that only authorized requests can access a specific service and improves service security and reliability.

Prerequisites

Step 1: Configure a blank authorization policy

The following example shows you how to configure authorization policies to obtain access permissions on a workload based on a blank authorization policy. The blank authorization policy is a basic deny policy that denies all requests initiated to access the workload.

  1. Log on to the ASM console. In the left-side navigation pane, choose Service Mesh > Mesh Management.

  2. On the Mesh Management page, click the name of the ASM instance. In the left-side navigation pane, choose Mesh Security Center > AuthorizationPolicy. On the page that appears, click Create from YAML.

  3. On the Create page, select default from the Namespace drop-down list, select a template, copy the following content to the YAML code editor, and then click Create.

    apiVersion: security.istio.io/v1beta1
    kind: AuthorizationPolicy
    metadata:
      name: allow-nothing
      namespace: default
    spec:
      {}

    Then, you can view the allow-nothing authorization policy on the AuthorizationPolicy page.

  4. In the address bar of your browser, enter http://{IP address of your ASM gateway}/productpage to access the Bookinfo application.

    The RBAC: access denied message is returned, which indicates that you have no access permissions. For more information about how to obtain the IP address of your gateway, see Use Istio resources to route traffic to different versions of a service.

Step 2: Configure an authorization policy for the productpage service

  1. Log on to the ASM console. In the left-side navigation pane, choose Service Mesh > Mesh Management.

  2. On the Mesh Management page, click the name of the ASM instance. In the left-side navigation pane, choose Mesh Security Center > AuthorizationPolicy.

  3. Use one of the following methods to configure an authorization policy to allow access to the Productpage service:

    Method 1: Use YAML to create an authorization policy

    1. On the AuthorizationPolicy page, click Create from YAML.

    2. On the Create page, select default from the Namespace drop-down list, select a template, copy the following content to the YAML code editor, and then click Create.

      apiVersion: security.istio.io/v1beta1
      kind: AuthorizationPolicy
      metadata:
        name: "productpage-viewer"
        namespace: default
      spec:
        selector:
          matchLabels:
            app: productpage
        action: ALLOW
        rules:
        - to:
          - operation:
              methods: ["GET"]

    Method 2: Use the graphical user interface (GUI) to create an authorization policy

    1. On the AuthorizationPolicy page, click Create.

    2. On the Create page, configure the following parameters and click Create.

      Parameter

      Description

      Name

      The name of the authorization policy. In this example, the value is set to productpage-viewer.

      Policy Type

      The authorization action. In this example, the value is set to ALLOW.

      Namespace

      The namespace in which you want to create the authorization policy. In this example, the Namespace parameter on the Workload Scope tab is set to default.

      Effective Scope

      The effective scope of the authorization policy. In this example, the value is set to Service.

      Workload

      The workload on which the authorization policy takes effect. In this example, the value is set to productpage.

      Request Matching Rules

      The method for processing HTTP requests. In this example, Methods is turned on in the Add Request Target section and the value is set to GET.

    Then, the productpage-viewer authorization policy is displayed on the AuthorizationPolicy page.

  4. In the address bar of your browser, enter http://{IP address of your ASM gateway}/productpage to access the Bookinfo application.

    The result in the following figure indicates that you can access the productpage service, but not the details or reviews service. This is because the authorization policy that you configured for the productpage service allows HTTP GET requests to access the productpage service.访问Productpage页面

Step 3: Configure an authorization policy for the details service

You can configure an authorization policy for the details service to allow the requests that are sent by the service account of the productpage service to access the details service.

  1. Obtain the service account of the productpage service.

    1. Log on to the ACK console. In the left-side navigation pane, click Clusters.

    2. On the Clusters page, click the name of the cluster that you want to manage and choose Workloads > Deployments in the left-side navigation pane.

    3. On the Deployments page, find the productpage-v1 service and choose More > View in YAML in the Actions column. In the Edit YAML dialog box, obtain the value of the serviceAccount parameter.

      As shown in the following figure, the value of the serviceAccount parameter is bookinfo-productpage.获取Productpage的serviceAccount

  2. Create an authorization policy.

    1. Log on to the ASM console. In the left-side navigation pane, choose Service Mesh > Mesh Management.

    2. On the Mesh Management page, click the name of the ASM instance. In the left-side navigation pane, choose Mesh Security Center > AuthorizationPolicy.

    3. Use one of the following methods to create an authorization policy.

      Method 1: Use YAML to create an authorization policy

      1. On the AuthorizationPolicy page, click Create from YAML.

      2. On the Create page, select default from the Namespace drop-down list, select a template, copy the following content to the YAML code editor, and then click Create.

        kind: AuthorizationPolicy
        apiVersion: security.istio.io/v1beta1
        metadata:
          name: details-viewer
          namespace: default
        spec:
          action: ALLOW
          rules:
            - to:
                - operation:
                    methods:
                      - GET
            - from:
                - source:
                    principals:
                      - cluster.local/ns/default/sa/bookinfo-productpage # bookinfo-productpage is the value of the serviceAccount parameter, which is obtained in the previous step. 
          selector:
            matchLabels:
              app: details

      Method 2: Use the GUI to create an authorization policy

      1. On the AuthorizationPolicy page, click Create.

      2. On the Create page, configure the following parameters and click Create.

        Parameter

        Description

        Name

        The name of the authorization policy. In this example, the value is set to details-viewer.

        Policy Type

        The authorization action. In this example, the value is set to ALLOW.

        Namespace

        The namespace in which you want to create the authorization policy. In this example, the Namespace parameter on the Workload Scope tab is set to default.

        Effective Scope

        The effective scope of the authorization policy. In this example, the value is set to Service.

        Workload

        The workload on which the authorization policy takes effect. In this example, the value is set to details.

        Request Matching Rules

        1. The source of requests. In this example, Principals is turned on in the Add Request Source section and the value is set to cluster.local/ns/default/sa/bookinfo-productpage.

          Note

          bookinfo-productpage is the value of the serviceAccount parameter, which is obtained in Substep 1.

        2. The method for processing HTTP requests. In this example, Methods is turned on in the Add Request Target section and the value is set to GET.

      Then, the details-viewer authorization policy is displayed on the AuthorizationPolicy page.

  3. In the address bar of your browser, enter http://{IP address of your ASM gateway}/productpage to access the Bookinfo application.

    The result in the following figure indicates that you can access the details service on the left, but not the reviews service on the right.Details服务

Step 4: Configure an authorization policy for the reviews service

  1. Log on to the ASM console. In the left-side navigation pane, choose Service Mesh > Mesh Management.

  2. On the Mesh Management page, click the name of the ASM instance. In the left-side navigation pane, choose Mesh Security Center > AuthorizationPolicy.

  3. Use one of the following methods to create an authorization policy.

    Method 1: Use YAML to create an authorization policy

    1. On the AuthorizationPolicy page, click Create from YAML.

    2. On the Create page, select default from the Namespace drop-down list, select a template, copy the following content to the YAML code editor, and then click Create.

      kind: AuthorizationPolicy
      apiVersion: security.istio.io/v1beta1
      metadata:
        name: reviews-viewer
        namespace: default
      spec:
        action: ALLOW
        rules:
          - to:
              - operation:
                  methods:
                    - GET
          - from:
              - source:
                  principals:
                    - cluster.local/ns/default/sa/bookinfo-productpage
        selector:
          matchLabels:
            app: reviews

    Method 2: Use the GUI to create an authorization policy.

    1. On the AuthorizationPolicy page, click Create.

    2. On the Create page, configure the following parameters and click Create.

      Parameter

      Description

      Name

      The name of the authorization policy. In this example, the value is set to reviews-viewer.

      Policy Type

      The authorization action. In this example, the value is set to ALLOW.

      Namespace

      The namespace in which you want to create the authorization policy. In this example, the Namespace parameter on the Workload Scope tab is set to default.

      Effective Scope

      The effective scope of the authorization policy. In this example, the value is set to Service.

      Workload

      The workload on which the authorization policy takes effect. In this example, the value is set to reviews.

      Request Matching Rules

      1. The source of requests. In this example, Principals is turned on in the Add Request Source section and the value is set to cluster.local/ns/default/sa/bookinfo-productpage.

        Note

        bookinfo-productpage is the value of the serviceAccount parameter, which is obtained in Substep 1.

      2. The method for processing HTTP requests. In this example, Methods is turned on in the Add Request Target section and the value is set to GET.

    Then, the reviews-viewer authorization policy is displayed on the AuthorizationPolicy page.

  4. In the address bar of your browser, enter http://{IP address of your ASM gateway}/productpage to access the Bookinfo application.

    The result in the following figure indicates that you can access the details service on the left and the reviews service on the right.Reviews服务

References