You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Terraform禁用GCP漏洞扫描?Azure DevOps场景问题

问题描述

我在Azure DevOps流水线中通过服务连接创建GCP Artifact Registry,需要禁用漏洞扫描。在main.tf里创建完Artifact Registry后,我加了一个带local-exec的null_resource:

provisioner "local-exec" {
   command = "gcloud services disable containerscanning.googleapis.com --project=test --force"
}

Artifact Registry创建成功了,但运行这个null_resource时抛出错误:

Error running command 'gcloud services disable containerscanning.googleapis.com --project=test --force': exit status 1. Output: ERROR: (gcloud.services.disable) You do not currently have an active account selected.

我临时解决方法是在null_resource里先执行gcloud auth activate-service-account命令,但有两个疑问:

  1. 已经通过服务连接创建了Artifact Registry,为什么null_resource的gcloud命令还要求凭据?
  2. 能不能不用null_resource,直接通过Terraform禁用漏洞扫描?

解答

1. 为什么null_resource仍要求凭据?

Terraform创建Artifact Registry用的是GCP Provider层面的凭据——也就是你Azure DevOps服务连接传递给Terraform的身份信息,这个凭据只在Terraform的资源调用流程里生效。
而local-exec是在流水线的代理机器上直接执行本地的gcloud命令,这个命令的运行上下文和Terraform Provider完全独立:代理机器上的gcloud没有自动继承Terraform的身份凭证,也不知道你用服务连接做过认证,所以会提示没有活跃账户。

2. 不用null_resource的替代方案

完全可以不用null_resource,有两种更规范的Terraform实现方式:

方式一:直接禁用Containerscanning服务(和你原本的需求一致)

用Terraform的google_project_service资源直接禁用containerscanning.googleapis.com服务,不需要依赖gcloud命令:

resource "google_project_service" "disable_container_scanning" {
  project = "test"
  service = "containerscanning.googleapis.com"
  disable_dependent_services = true # 如果有依赖服务需要一并禁用,设为true
  disable_on_destroy = false # 可选:销毁资源时是否重新启用服务,按需设置
}

方式二:仅禁用特定Artifact Registry的镜像扫描(更精准)

如果你不想禁用整个项目的容器扫描服务,只是想让当前创建的Artifact Registry不做漏洞扫描,可以在google_artifact_registry_repository资源里配置image_scanning_config块:

resource "google_artifact_registry_repository" "my_repo" {
  project      = "test"
  location     = "us-central1"
  repository_id = "my-repo"
  format       = "DOCKER"

  # 禁用该仓库的镜像漏洞扫描
  image_scanning_config {
    enabled = false
  }
}

内容的提问来源于stack exchange,提问作者reddy malathi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 14:47:02