GKE의 사이드카에 승인 정책 설정

이 페이지에서는 GKE의 Cloud Service Mesh 사이드카에서 다양한 종류의 승인 정책을 설정하는 방법을 설명합니다.

시작하기 전에

Gateway API 및 승인 확장 프로그램에 익숙해야 합니다.

승인 정책을 만들려면 먼저 다음 단계를 수행해야 합니다.

  1. Google Cloud 계정에 로그인합니다. Google Cloud를 처음 사용하는 경우 계정을 만들고 Google 제품의 실제 성능을 평가해 보세요. 신규 고객에게는 워크로드를 실행, 테스트, 배포하는 데 사용할 수 있는 $300의 무료 크레딧이 제공됩니다.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Network Security, Network Services APIs, if any are not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the APIs

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Network Security, Network Services APIs, if any are not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the APIs

네임스페이스 전체 AuthzPolicy

전체 네임스페이스에 대해 예상되는 트래픽 패턴을 거부하려면 네임스페이스의 모든 워크로드에 대한 수신 HTTP 요청을 거부하도록 승인 정책을 구성합니다.

cat >ns-authz-policy-deny.yaml <<EOF
apiVersion: networking.gke.io/v1
kind: GCPAuthzPolicy
metadata:
  name: ns-authz
  namespace: NAMESPACE
spec:
  targetRefs:
  - group: ""
    kind: Pod
    selector: {}
  rules:
  - to:
      operations:
      - paths:
        - type: Prefix
          value: "/bad_token"
  action: DENY
  enforcementLevel: L7
EOF
kubectl apply -f ns-authz-policy-deny.yaml

비어 있는 selector: {}는 네임스페이스의 모든 포드를 타겟팅합니다.

전체 네임스페이스에 대해 예상되는 트래픽 패턴을 허용하려면 네임스페이스의 모든 워크로드에 수신되는 모든 HTTP 요청을 허용하도록 승인 정책을 구성합니다.

cat >ns-authz-policy-allow.yaml <<EOF
apiVersion: networking.gke.io/v1
kind: GCPAuthzPolicy
metadata:
  name: ns-authz
  namespace: NAMESPACE
spec:
  targetRefs:
  - group: ""
    kind: Pod
    selector: {}
  rules:
  - to:
      operations:
      - paths:
        - type: Prefix
          value: "/headers"
        methods: ["GET"]
  action: ALLOW
  enforcementLevel: L7
EOF
kubectl apply -f ns-authz-policy-allow.yaml

비어 있는 selector: {}는 네임스페이스의 모든 포드를 타겟팅합니다.

워크로드에 대한 인바운드 요청 거부

크론 작업과 같이 아웃바운드 호출만 실행해야 하는 워크로드가 있는 경우 워크로드에 수신되는 모든 HTTP 요청을 거부하도록 승인 정책을 구성합니다.

cat >deny-path-authz-policy.yaml <<EOF
apiVersion: networking.gke.io/v1
kind: GCPAuthzPolicy
metadata:
  name: my-workload-authz
  namespace: NAMESPACE
spec:
  targetRefs:
  - group: ""
    kind: Pod
    selector:
      matchLabels:
        app: EXAMPLE_APP
  rules:
  - to:
      operations:
      - paths:
        - type: Prefix
          value: "/deny_path"
  action: DENY
  enforcementLevel: L7
EOF
kubectl apply -f deny-path-authz-policy.yaml

출력은 다음과 비슷합니다.

gcpauthzpolicy.networking.gke.io/my-workload-authz created

이 정책이 적용되면 app: EXAMPLE_APP과 일치하는 포드의 /deny_path 경로에 대한 수신 HTTP 요청이 거부되고 호출자는 HTTP 403 금지 응답 코드를 수신합니다.

워크로드에 대한 특정 인바운드 요청 허용

특정 기준과 일치하는 요청만 허용하고 나머지는 거부하는 ALLOW 정책을 구성할 수도 있습니다.

다음 예시에서는 ID가 spiffe://cluster.local/NAMESPACE/pod1인 포드의 mTLS 요청만 허용하도록 example-app 배포에서 승인 정책을 구성합니다.

cat >allow-authz-policy.yaml <<EOF
apiVersion: networking.gke.io/v1
kind: GCPAuthzPolicy
metadata:
  name: my-workload-authz
  namespace: NAMESPACE
spec:
  targetRefs:
  - group: ""
    kind: Pod
    selector:
      matchLabels:
        app: EXAMPLE_APP
  rules:
  - from:
      sources:
      - principals:
        - principal:
            type: Exact
            value: "spiffe://cluster.local/NAMESPACE/pod1"
  action: ALLOW
  enforcementLevel: L7
EOF
kubectl apply -f allow-authz-policy.yaml

출력은 다음과 비슷합니다.

gcpauthzpolicy.networking.gke.io/my-workload-authz created

정확한 SPIFFE ID spiffe://cluster.local/NAMESPACE/pod1를 제공하는 mTLS를 사용하여 인증된 수신 요청만 HTTP 200 OK를 수신합니다. 다른 요청은 HTTP 403 금지와 함께 프록시에 의해 거부됩니다.

외부 승인 엔진에 위임

자체 승인 엔진을 가져오고 워크로드의 모든 승인 결정을 구성된 엔진에 위임하도록 Cloud Service Mesh를 구성할 수 있습니다.

  1. GCPAuthzExtension CRD가 아직 설치되어 있지 않으면 설치합니다.

    kubectl apply -f https://raw.githubusercontent.com/GoogleCloudPlatform/gke-gateway-api/main/config/crd/networking.gke.io_gcpauthzextensions.yaml
    
  2. ext_proc gRPC 프로토콜을 지원하는 Kubernetes에 콜아웃 기반 승인 확장 프로그램 워크로드를 배포하고 승인 확장 프로그램 리소스를 구성합니다.

    cat >authz-extension.yaml <<EOF
    apiVersion: networking.gke.io/v1
    kind: GCPAuthzExtension
    metadata:
      name: my-authz-ext
      namespace: NAMESPACE
    spec:
      backendRef:
        group: ""
        kind: Service
        name: authz-service
        port: PORT
      authority: "authz-service.NAMESPACE.svc.cluster.local"
      forwardHeaders:
      - Authorization
      failOpen: false
      timeout: "100ms"
      wireFormat: ExtProcGRPC
    EOF
    kubectl apply -f authz-extension.yaml
    

    다음을 바꿉니다.

    • NAMESPACE: authz-service 서비스 및 워크로드의 네임스페이스입니다.
    • PORT: ext_proc gRPC API를 제공하는 authz-service 서비스의 포트입니다.

    이 명령어는 Kubernetes 서비스 authz-service로 실행되는 콜아웃 기반 승인 확장 프로그램을 만듭니다.

    출력은 다음과 비슷합니다.

    gcpauthzextension.networking.gke.io/my-authz-ext created
    
  3. 워크로드의 승인 결정을 이전에 구성한 승인 확장 프로그램에 위임하도록 승인 정책을 설정합니다.

    cat >authz-policy.yaml <<EOF
    apiVersion: networking.gke.io/v1
    kind: GCPAuthzPolicy
    metadata:
      name: my-workload-authz
      namespace: NAMESPACE
    spec:
      targetRefs:
      - group: ""
        kind: Pod
        selector:
          matchLabels:
            app: EXAMPLE_APP
      rules:
      - from:
          sources:
          - principals:
            - principal:
                type: Exact
                value: "spiffe://cluster.local/NAMESPACE/pod1"
        to:
          operations:
          - paths:
            - type: Exact
              value: "/api/payments-schedule"
      action: CUSTOM
      enforcementLevel: L7
      customProviders:
        extensionRefs:
        - group: networking.gke.io
          kind: GCPAuthzExtension
          name: my-authz-ext
    EOF
    kubectl apply -f authz-policy.yaml
    

    출력은 다음과 비슷합니다.

    gcpauthzpolicy.networking.gke.io/my-workload-authz created
    

    프록시는 승인 확장 프로그램이 응답할 때까지 기다린 후 요청을 애플리케이션에 전달합니다. 확장 프로그램이 오류와 함께 호출을 종료하여 요청을 거부하거나 failOpen가 false인 동안 호출이 실패하거나 타임아웃되면 호출자가 HTTP 500 응답을 수신합니다. 확장 프로그램이 즉시 응답을 전송하면 호출자는 해당 응답에서 상태 코드를 수신합니다.