Tüm Ürünler
Search
Belge Merkezi

Alibaba Cloud Service Mesh:Özel bir HTTP yetkilendirme hizmetini entegre etme

Son Güncelleme:Oct 07, 2026

Özel yetkilendirme hizmeti özelliği, HTTP üzerinden iletişim kuran hizmetler için ayrıntılı erişim kontrolü sağlar. Hizmetten hizmete iletişime özel bir yetkilendirme denetimi ekleyerek yalnızca kimliği doğrulanmış ve yetkilendirilmiş isteklerin hizmet kaynaklarınıza erişmesini sağlayabilir, böylece güvenliği artırabilirsiniz. Bu konuda, özel bir HTTP yetkilendirme hizmetinin nasıl entegre edileceği sleep ve httpbin uygulamaları kullanılarak gösterilmektedir.

Ön koşullar

Kümenin ASM örneğine eklenmiş olması gerekir..

1. Adım: Özel bir yetkilendirme hizmeti dağıtma

ACK kümenizde özel bir yetkilendirme hizmeti dağıtın. Bu hizmet, özel yetkilendirme hizmetleri için Istio API teknik özelliklerine uygun olmalı ve özel yetkilendirme mantığınızı uygulamak üzere hem HTTP hem de gRPC protokollerini desteklemelidir. Bu konuda kullanılan örnek hizmet, bir isteğin yetkilendirme denetiminden geçmesi için x-ext-authz: allow istek üst bilgisini içermesini gerektirir.

Not

Kendi özel yetkilendirme hizmetinizi oluşturmak için bu örnek uygulamanın kodunu referans olarak da kullanabilirsiniz. Daha fazla bilgi için bkz. Özel yetkilendirme.

  1. kubectl kullanarak kümenize bağlanın ve aşağıdaki içeriği içeren ext-authz.yaml adlı bir dosya oluşturun.

    kubectl kullanarak bir kümeye bağlanma hakkında daha fazla bilgi için bkz. Bir kümenin KubeConfig dosyasını alma ve kubectl ile kümeye bağlanma.

    ext-authz.yaml

    # Copyright Istio Authors
    #
    #   Licensed under the Apache License, Version 2.0 (the "License");
    #   you may not use this file except in compliance with the License.
    #   You may obtain a copy of the License at
    #
    #       http://www.apache.org/licenses/LICENSE-2.0
    #
    #   Unless required by applicable law or agreed to in writing, software
    #   distributed under the License is distributed on an "AS IS" BASIS,
    #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    #   See the License for the specific language governing permissions and
    #   limitations under the License.
    
    # Example configurations for deploying ext-authz server separately in the mesh.
    
    apiVersion: v1
    kind: Service
    metadata:
      name: ext-authz
      labels:
        app: ext-authz
    spec:
      ports:
      - name: http
        port: 8000
        targetPort: 8000
      - name: grpc
        port: 9000
        targetPort: 9000
      selector:
        app: ext-authz
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: ext-authz
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: ext-authz
      template:
        metadata:
          labels:
            app: ext-authz
        spec:
          containers:
          - image: istio/ext-authz:0.6
            imagePullPolicy: IfNotPresent
            name: ext-authz
            ports:
            - containerPort: 8000
            - containerPort: 9000
    ---
  2. Özel yetkilendirme hizmetini dağıtmak için aşağıdaki komutu çalıştırın.

    kubectl apply -f ext-authz.yaml
  3. Pod dağıtım durumunu kontrol etmek için aşağıdaki komutu çalıştırın.

    kubectl get pod

    Beklenen çıktı:

    NAME                         READY   STATUS    RESTARTS   AGE
    ext-authz-6b5db88f86-2m7c6   2/2     Running   0          79m
  4. Uygulamanın çalıştığını doğrulamak için aşağıdaki komutu çalıştırın.

    kubectl logs "$(kubectl get pod -l app=ext-authz -n default -o jsonpath={.items..metadata.name})" -n default -c ext-authz

    Beklenen çıktı:

    2023/12/20 08:15:39 Starting gRPC server at [::]:9000
    2023/12/20 08:15:39 Starting HTTP server at [::]:8000

    Bu çıktı, uygulamanın çalıştığını ve özel yetkilendirme hizmetinin dağıtıldığını gösterir.

  5. ext-authz hizmetinin HTTP bağlantı noktasını alın.

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

    2. On the Clusters page, click the name of your cluster. In the left navigation pane, click Network > Services.

    3. On the Services page, click ext-authz.

      In the Endpoint section, note that the HTTP port is 8000. The access address for this service is ext-authz.default.svc.cluster.local:8000.

2. Adım: Örnek uygulamaları dağıtma

  1. Create a file named httpbin.yaml with the following content.

    httpbin.yaml

    # Copyright Istio Authors
    #
    #   Licensed under the Apache License, Version 2.0 (the "License");
    #   you may not use this file except in compliance with the License.
    #   You may obtain a copy of the License at
    #
    #       http://www.apache.org/licenses/LICENSE-2.0
    #
    #   Unless required by applicable law or agreed to in writing, software
    #   distributed under the License is distributed on an "AS IS" BASIS,
    #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    #   See the License for the specific language governing permissions and
    #   limitations under the License.
    ##################################################################################################
    # httpbin service
    ##################################################################################################
    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: httpbin
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: httpbin
      labels:
        app: httpbin
        service: httpbin
    spec:
      ports:
      - name: http
        port: 8000
        targetPort: 80
      selector:
        app: httpbin
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: httpbin
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: httpbin
          version: v1
      template:
        metadata:
          labels:
            app: httpbin
            version: v1
        spec:
          serviceAccountName: httpbin
          containers:
          - image: docker.io/kennethreitz/httpbin
            imagePullPolicy: IfNotPresent
            name: httpbin
            ports:
            - containerPort: 80
                            
  2. Run the following command to deploy the httpbin application.

    kubectl apply -f httpbin.yaml
  3. Create a file named sleep.yaml with the following content.

    sleep.yaml

    # Copyright Istio Authors
    #
    #   Licensed under the Apache License, Version 2.0 (the "License");
    #   you may not use this file except in compliance with the License.
    #   You may obtain a copy of the License at
    #
    #       http://www.apache.org/licenses/LICENSE-2.0
    #
    #   Unless required by applicable law or agreed to in writing, software
    #   distributed under the License is distributed on an "AS IS" BASIS,
    #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    #   See the License for the specific language governing permissions and
    #   limitations under the License.
    ##################################################################################################
    # Sleep service
    ##################################################################################################
    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: sleep
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: sleep
      labels:
        app: sleep
        service: sleep
    spec:
      ports:
      - port: 80
        name: http
      selector:
        app: sleep
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: sleep
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: sleep
      template:
        metadata:
          labels:
            app: sleep
        spec:
          terminationGracePeriodSeconds: 0
          serviceAccountName: sleep
          containers:
          - name: sleep
            image: curlimages/curl
            command: ["/bin/sleep", "3650d"]
            imagePullPolicy: IfNotPresent
            volumeMounts:
            - mountPath: /etc/sleep/tls
              name: secret-volume
          volumes:
          - name: secret-volume
            secret:
              secretName: sleep-secret
              optional: true
    ---
                            
  4. Run the following command to deploy the sleep application.

    kubectl apply -f sleep.yaml

3. Adım: Özel yetkilendirme hizmetini kaydetme

Declare the service deployed in Step 1 to the Service Mesh so that the Service Mesh can use the service for authentication.

  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 > External Authorization Service. On the page that appears, click Define External Authorization Service.

  3. On the Register External Authorization Service page, click the Custom authorization service (HTTP or gRPC protocol) implemented based on envoy.ext_authz tab, configure the parameters, and then click Create.

    Tür

    Parametre

    Açıklama

    Gerekli parametreler

    Protocol

    Özel yetkilendirme hizmeti için protokolü seçin. Bu örnekte HTTP seçeneğini belirleyin.

    Name

    Özel yetkilendirme hizmetinin adıdır. Bu örnekte test4http değerini girin.

    Service Address

    Özel yetkilendirme uygulamasının hizmet adresini girin. Adres, <Application Name>.<Namespace>.svc.<Cluster Domain> biçiminde tam nitelikli bir alan adı olmalıdır. Bu örnek için ext-authz.default.svc.cluster.local değerini girin.

    Port(1 - 65535)

    Özel yetkilendirme uygulamasının hizmet bağlantı noktasını girin. Bu örnekte 8000 değerini girin.

    Timeout(second)

    Yetkilendirme uygulaması bu süre içinde yanıt vermezse hizmet kullanılamaz durumda kabul edilir. Bu örnekte zaman aşımını 10 saniye olarak ayarlayın.

    İsteğe bağlı parametreler

    Skip authentication while authorization service is unavailable

    Etkinleştirilirse yetkilendirme hizmeti kullanılamaz durumda olduğunda isteklere izin verilir. Bu örnekte bu seçenek devre dışı bırakılmıştır.

    Error code returned by asm proxy while Auth-Service is not available

    Bu seçenek yalnızca Skip authentication while authorization service is unavailable seçeneği devre dışı bırakıldığında kullanılabilir. Bu seçeneği etkinleştirirseniz bir hata kodu girmeniz gerekir. Yetkilendirme hizmeti kullanılamaz durumda olduğunda bu kod istemciye döndürülür. Bu örnekte bu seçenek devre dışı bırakılmıştır.

    Carry origin header within auth request [includeRequestHeadersInCheck]

    Etkinleştirildiğinde dahil edilecek üst bilgi anahtarlarını belirtmeniz gerekir. Eşleşen üst bilgiler özel yetkilendirme hizmetine gönderilen isteğe dahil edilir. Bu örnek için bkz. Yetkilendirme isteğine üst bilgileri dahil etme.

    Not

    Bu parametre yalnızca Protocol seçeneği HTTP olarak ayarlandığında yapılandırılabilir.

    Add a header in the authentication request (if a header with the same name already exists, the original value will be overwritten) [includeAdditionalHeadersInCheck]

    Etkinleştirildiğinde yetkilendirme isteğine eklenecek üst bilgi anahtarlarını ve değerlerini belirtmeniz gerekir.

    Aynı ada sahip bir üst bilgi zaten varsa değeri üzerine yazılır. Bu örnekte bu seçenek devre dışı bırakılmıştır.

    Not

    Bu parametre yalnızca Protocol seçeneği HTTP olarak ayarlandığında yapılandırılabilir.

    Overwrite Header when authentication passes (overwrite Header in the request to the target service by using Header in the authentication request Response) [headersToUpstreamOnAllow]

    Etkinleştirilirse üzerine yazılacak üst bilgi anahtarlarını belirtin. Yetkilendirme başarılı olduğunda yetkilendirme yanıtındaki eşleşen üst bilgiler hedef hizmete yapılan istekteki karşılık gelen üst bilgilerin üzerine yazar. Bu örnek için bkz. Başarılı yetkilendirmede üst bilgileri üzerine yazma.

    Not

    Bu parametre yalnızca Protocol seçeneği HTTP olarak ayarlandığında yapılandırılabilir.

    Overwrite Header when authentication fails (overwrite Header in Response using Header in authentication request Response) [headersToDownstreamOnDeny]

    Etkinleştirilirse üzerine yazılacak üst bilgi anahtarlarını belirtin. Yetkilendirme başarısız olduğunda yetkilendirme yanıtındaki eşleşen üst bilgiler çağrı yapan hizmete verilen yanıtta karşılık gelen üst bilgilerin üzerine yazar. Bu örnek için bkz. Başarısız yetkilendirmede üst bilgileri üzerine yazma.

    Not

    Bu parametre yalnızca Protocol seçeneği HTTP olarak ayarlandığında yapılandırılabilir.

    Carry origin request body within auth request

    Etkinleştirilirse yetkilendirme denetimine dahil edilecek istek gövdesinin maksimum boyutunu belirtin. Ayrıca Allow send incomplete message to Auth-Service(HTTP 413 will be returned if request body size beyond limitation and this option is disabled) seçeneğini etkinleştirirseniz sınırı aşan gövdeler kırpılarak gönderilir. Aksi takdirde aşırı büyük gövdeli istekler HTTP 413 hatasıyla reddedilir.

    Şekil 1. Yetkilendirme isteğine üst bilgileri dahil etme

    Not

    Son giriş, yeni eklenen x-ext-authz öğesidir.

    The headers to be included are: cookie, x-forwarded-access-token, x-forwarded-user, x-forwarded-email, authorization, x-forwarded-proto, proxy-authorization, user-agent, x-forwarded-host, from, x-forwarded-for, accept, and x-ext-authz.

    Şekil 2. Başarılı yetkilendirmede üst bilgileri üzerine yazma

    Not

    Son giriş, yeni eklenen x-ext-authz-check-result öğesidir.

    This list also includes the five existing fields: authorization, cookie, path, x-auth-request-access-token, and x-forwarded-access-token.

    Şekil 3. Başarısız yetkilendirmede üst bilgileri üzerine yazma

    Not

    Son giriş, yeni eklenen x-ext-authz-check-result öğesidir.

    The list of headers to overwrite already contains content-type and set-cookie. The new x-ext-authz-check-result is added at the end of the list.

4. Adım: Bir yetkilendirme ilkesi tanımlama

Hangi işlemlerin yetkilendirme gerektirdiğini belirtmek için bir yetkilendirme ilkesi oluşturun.

  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.

  3. On the Create page, configure the parameters, and then click Create.


    Parameter


    Description


    Name


    Özel yetkilendirme ilkesinin adıdır. Bu örnekte test1 değerini girin.


    Policy Type


    Select Custom Authorization Service.


    Custom Authorization Service


    httpextauth-test4http(HTTP) seçeneğini belirleyin.


    Namespace


    On the Workload Scope tab, select the default Namespace.


    Effective Scope


    Service seçeneğini belirleyin.


    Workload


    httpbin seçeneğini belirleyin.


    Request Matching Rules


    In the Add Request Target section, enable Paths and set the value to /headers.

5. Adım: Özel yetkilendirmeyi doğrulama

  1. Run the following command to access httpbin.default:8000/ip.

    kubectl exec "$(kubectl get pod -l app=sleep -n default -o jsonpath={.items..metadata.name})" -c sleep -n default -- curl "http://httpbin.default:8000/ip" -s -o /dev/null -w "%{http_code}\n"

    A 200 status code is returned, which indicates that authorization was not triggered. Because the request path /ip does not match the /headers path in the authorization policy, the policy did not apply to this request.

  2. Run the following command to access httpbin.default:8000/headers with the x-ext-authz: deny request header.

    kubectl exec "$(kubectl get pod -l app=sleep -n default -o jsonpath={.items..metadata.name})" -c sleep -ndefault -- curl "http://httpbin.default:8000/headers" -H "x-ext-authz: deny" -s -i

    Beklenen çıktı:

    HTTP/1.1 403 Forbidden
    x-ext-authz-check-result: denied
    content-length: 76
    content-type: text/plain; charset=utf-8
    date: Wed, 20 Dec 2023 09:53:28 GMT
    server: envoy
    x-envoy-upstream-service-time: 10
    denied by ext_authz for not found header `x-ext-authz: allow` in the request

    The output shows that the request was denied because it triggered the authorization policy. The response includes the x-ext-authz-check-result: denied header, as configured for authorization failures. The policy was applied because the request path is /headers.

  3. Run the following command to access httpbin.default:8000/headers with the x-ext-authz: allow request header.

    kubectl exec "$(kubectl get pod -l app=sleep -n default -o jsonpath={.items..metadata.name})" -c sleep -n default -- curl "http://httpbin.default:8000/headers" -H "x-ext-authz: allow" -s

    Beklenen çıktı:

    {
      "headers": {
        "Accept": "*/*",
        "Host": "httpbin.default:8000",
        "User-Agent": "curl/8.5.0",
        "X-Envoy-Attempt-Count": "1",
        "X-Ext-Authz": "allow",
        "X-Ext-Authz-Check-Result": "allowed",
        "X-Forwarded-Client-Cert": "By=spiffe://cluster.local/ns/default/sa/httpbin;Hash=c3e5364e87add0f4f69e6b0d029f5961b404c8f209bf9004b3d21a82cf67****;Subject=\"\";URI=spiffe://cluster.local/ns/default/sa/sleep"
      }
    }

    The output shows that the request was allowed because it triggered the authorization policy and passed the check. The response includes the "X-Ext-Authz-Check-Result": "allowed" header, as configured for successful authorization. The policy was applied because the request path is /headers.

İlgili işlemler