# Implementing GCP Organization Policy Constraints

> 实施 GCP 组织策略约束，在整个资源层次结构中强制执行安全防护栏，限制危险配置并在组织、文件夹和项目级别确保合规性。

- Skill: `killvxk/implementing-gcp-organization-policy-constraints` (Agent Skill, multi-file: 8 files)
- Install (CLI): `npx skillmds@latest add killvxk/implementing-gcp-organization-policy-constraints`
- Raw SKILL.md: https://api.skillmd.com/api/skills/killvxk/implementing-gcp-organization-policy-constraints/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: DevOps & Infra
- License: Apache-2.0
- Author: killvxk (https://skillmd.com/u/killvxk)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/killvxk/implementing-gcp-organization-policy-constraints

---


# 实施 GCP 组织策略约束

## 概述

GCP 组织策略服务（Organization Policy Service）对云资源提供集中化和程序化的控制。组织策略通过配置约束来限制一个或多个 Google Cloud 服务，可在组织、文件夹或项目级别强制执行。它们通过阻止外部 IP、要求加密和最小化未授权访问来提升安全性。策略变更可能需要最多 15 分钟才能生效。

## 前置条件

- 拥有 Organization Administrator 角色的 GCP 组织
- 已配置并完成身份验证的 `gcloud` CLI
- 用于策略管理的 Terraform 或 gcloud
- 组织策略管理员 IAM 角色（`roles/orgpolicy.policyAdmin`）

## 核心概念

### 约束类型

1. **列表约束（List Constraints）**：允许或拒绝特定值（如允许的区域）
2. **布尔约束（Boolean Constraints）**：启用或禁用某项功能（如禁用串行端口访问）
3. **自定义约束（Custom Constraints）**：针对特定资源字段的用户自定义规则（预览版）

### 策略继承

策略从具有强制执行策略的最低祖先节点继承。如果没有祖先节点配置策略，则应用 Google 的托管默认行为。

## 核心安全约束

### 限制 VM 外部 IP 地址

```bash
# 拒绝所有 VM 使用外部 IP 地址
gcloud resource-manager org-policies set-policy \
  --organization=ORGANIZATION_ID \
  policy.yaml
```

policy.yaml：
```yaml
constraint: constraints/compute.vmExternalIpAccess
listPolicy:
  allValues: DENY
```

### 限制资源位置

```bash
gcloud org-policies set-policy \
  --organization=ORGANIZATION_ID \
  location-policy.yaml
```

location-policy.yaml：
```yaml
constraint: constraints/gcp.resourceLocations
listPolicy:
  allowedValues:
    - "in:us-locations"
    - "in:eu-locations"
```

### 禁用默认服务账号创建

```yaml
constraint: constraints/iam.automaticIamGrantsForDefaultServiceAccounts
booleanPolicy:
  enforced: true
```

### 要求 SSH 使用 OS Login

```yaml
constraint: constraints/compute.requireOsLogin
booleanPolicy:
  enforced: true
```

### 禁用串行端口访问

```yaml
constraint: constraints/compute.disableSerialPortAccess
booleanPolicy:
  enforced: true
```

### 强制统一存储桶级访问

```yaml
constraint: constraints/storage.uniformBucketLevelAccess
booleanPolicy:
  enforced: true
```

### 限制 Cloud SQL 公共 IP

```yaml
constraint: constraints/sql.restrictPublicIp
booleanPolicy:
  enforced: true
```

### 禁用服务账号密钥创建

```yaml
constraint: constraints/iam.disableServiceAccountKeyCreation
booleanPolicy:
  enforced: true
```

## Terraform 实现

```hcl
resource "google_organization_policy" "restrict_vm_external_ip" {
  org_id     = var.org_id
  constraint = "constraints/compute.vmExternalIpAccess"

  list_policy {
    deny {
      all = true
    }
  }
}

resource "google_organization_policy" "restrict_locations" {
  org_id     = var.org_id
  constraint = "constraints/gcp.resourceLocations"

  list_policy {
    allow {
      values = ["in:us-locations", "in:eu-locations"]
    }
  }
}

resource "google_organization_policy" "require_os_login" {
  org_id     = var.org_id
  constraint = "constraints/compute.requireOsLogin"

  boolean_policy {
    enforced = true
  }
}

resource "google_folder_organization_policy" "dev_folder_external_ip" {
  folder     = google_folder.dev.name
  constraint = "constraints/compute.vmExternalIpAccess"

  list_policy {
    allow {
      values = ["projects/dev-project/zones/us-central1-a/instances/bastion-host"]
    }
  }
}
```

## 试运行测试

在强制执行前，使用 Policy Intelligence 工具测试变更影响：

```bash
# 创建试运行策略以监控影响
gcloud org-policies set-policy \
  --organization=ORGANIZATION_ID \
  dry-run-policy.yaml
```

dry-run-policy.yaml：
```yaml
constraint: constraints/compute.vmExternalIpAccess
listPolicy:
  allValues: DENY
dryRunSpec: true
```

```bash
# 检查试运行策略的违规情况
gcloud org-policies list-custom-constraints \
  --organization=ORGANIZATION_ID
```

## 自定义约束

```yaml
# custom-constraint.yaml
name: organizations/ORGANIZATION_ID/customConstraints/custom.disableGKEAutoUpgrade
resourceTypes:
  - container.googleapis.com/NodePool
methodTypes:
  - CREATE
  - UPDATE
condition: "resource.management.autoUpgrade == true"
actionType: DENY
displayName: 拒绝 GKE 节点池自动升级
description: 防止在 GKE 节点池上启用自动升级，以实现受控升级
```

```bash
gcloud org-policies set-custom-constraint custom-constraint.yaml
```

## 监控与合规

### 列出活跃策略

```bash
gcloud org-policies list --organization=ORGANIZATION_ID
```

### 描述特定策略

```bash
gcloud org-policies describe constraints/compute.vmExternalIpAccess \
  --organization=ORGANIZATION_ID
```

### 使用 Cloud Asset Inventory 审计策略违规

```bash
gcloud asset search-all-resources \
  --scope=organizations/ORGANIZATION_ID \
  --query="policy:constraints/compute.vmExternalIpAccess"
```

## 推荐基线策略

| 约束 | 类型 | 范围 | 用途 |
|------|------|------|------|
| compute.vmExternalIpAccess | 列表/拒绝 | 组织 | 防止 VM 使用公网 IP |
| gcp.resourceLocations | 列表/允许 | 组织 | 限制为已批准区域 |
| iam.disableServiceAccountKeyCreation | 布尔 | 组织 | 强制使用 Workload Identity |
| compute.requireOsLogin | 布尔 | 组织 | 强制 SSH 使用 OS Login |
| storage.uniformBucketLevelAccess | 布尔 | 组织 | 强制统一存储桶访问 |
| sql.restrictPublicIp | 布尔 | 组织 | 禁止 Cloud SQL 公网访问 |
| compute.disableSerialPortAccess | 布尔 | 组织 | 禁用串行端口 |
| compute.disableNestedVirtualization | 布尔 | 组织 | 禁止嵌套虚拟化 |

## 参考资料

- GCP 组织策略约束: https://docs.google.com/resource-manager/docs/organization-policy/org-policy-constraints
- GCP Policy Intelligence: https://cloud.google.com/policy-intelligence
- CIS GCP 基础基准

