GKE의 사이드카에 승인 정책 설정
이 페이지에서는 GKE의 Cloud Service Mesh 사이드카에서 다양한 종류의 승인 정책을 설정하는 방법을 설명합니다.
시작하기 전에
Gateway API 및 승인 확장 프로그램을 숙지해야 합니다.
승인 정책을 만들기 전에 다음 단계를 수행해야 합니다.
- 계정에 로그인합니다. Google Cloud 를 처음 사용하는 경우 계정을 만들어 실제 시나리오에서 제품의 성능을 평가하세요. Google Cloud신규 고객에게는 워크로드를 실행, 테스트, 배포하는 데 사용할 수 있는 $300의 무료 크레딧이 제공됩니다.
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
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.enablepermission. 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.-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
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.enablepermission. 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.
네임스페이스 전체 AuthzPolicy
전체 네임스페이스에 예상되는 트래픽 패턴을 거부하려면 네임스페이스의 모든 워크로드에 수신되는 모든 HTTP 요청을 거부하도록 승인 정책을 구성합니다.
cat >ns-authz-policy-deny.yaml <<EOF
apiVersion: networking.gke.io/v1
kind: GCPAuthzPolicy
metadata:
name: ns-authz
namespace: NAMESPACE
spec:
targetRefs:
- kind: Pod
selector: {}
rules:
- to:
operations:
- paths:
- type: Prefix
value: "/bad_token"
action: DENY
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:
- kind: Pod
selector: {}
rules:
- to:
operations:
- paths:
- type: Prefix
value: "/headers"
methods: ["GET"]
action: ALLOW
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:
- kind: Pod
selector:
matchLabels:
app: EXAMPLE_APP
rules:
- to:
operations:
- paths:
- type: Prefix
value: "/deny_path"
action: DENY
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 Forbidden 응답 코드가 수신됩니다.
워크로드에 대한 특정 인바운드 요청 허용
특정 기준과 일치하는 요청만 허용하고 나머지는 거부하는 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:
- kind: Pod
selector:
matchLabels:
app: EXAMPLE_APP
rules:
- from:
sources:
- principals:
- principal:
exact: "spiffe://cluster.local/NAMESPACE/pod1"
action: ALLOW
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 Forbidden으로 프록시에 의해 거부됩니다.
외부 승인 엔진에 위임
자체 승인 엔진을 가져와서 워크로드에 대한 모든 승인 결정을 구성된 엔진에 위임하도록 Cloud Service Mesh를 구성할 수 있습니다.
GCPAuthzExtensionCRD가 아직 설치되어 있지 않으면 설치합니다.kubectl apply -f https://raw.githubusercontent.com/GoogleCloudPlatform/gke-gateway-api/main/config/crd/networking.gke.io_gcpauthzextensions.yamlext_proc gRPC프로토콜을 지원하는 Kubernetes에서 콜아웃 기반 승인 확장 프로그램 워크로드를 배포하고 승인 확장 프로그램 리소스를 구성합니다.cat >authz-extension.yaml <<EOF apiVersion: networking.gke.io/v1 kind: GCPAuthzExtension metadata: name: my-authz-ext namespace: ns1 spec: backendRef: kind: Service name: authz-service loadBalancingScheme: INTERNAL_SELF_MANAGED forwardHeaders: - Authorization failOpen: false timeout: "0.1s" wireFormat: EXT_PROC_GRPC EOF kubectl apply -f authz-extension.yaml이 명령어는 Kubernetes 서비스
authz-service로 실행되는 콜아웃 기반 승인 확장 프로그램을 만듭니다.출력은 다음과 비슷합니다.
gcpauthzextension.networking.gke.io/my-authz-ext created워크로드에 대한 승인 결정을 이전에 구성된 승인 확장 프로그램에 위임하도록 승인 정책을 설정합니다.
cat >authz-policy.yaml <<EOF apiVersion: networking.gke.io/v1 kind: GCPAuthzPolicy metadata: name: my-workload-authz namespace: NAMESPACE spec: targetRefs: - kind: Pod selector: matchLabels: app: EXAMPLE_APP rules: - from: sources: - principals: - principal: exact: "spiffe://cluster.local/NAMESPACE/pod1" to: operations: - paths: - type: Exact value: "/api/payments-schedule" action: CUSTOM customProvider: authzExtension: targetRefs: - kind: GCPAuthzExtension name: my-authz-ext EOF kubectl apply -f authz-policy.yaml출력은 다음과 비슷합니다.
gcpauthzpolicy.networking.gke.io/my-workload-authz created프록시는 요청을 애플리케이션에 전달하거나 호출자에게 HTTP 403을 반환하기 전에 커스텀 서비스가
OK또는DENY응답을 반환할 때까지 기다립니다.