코드 보안 작업 분석을 사용하여 GitHub Actions 워크플로의 일부인 코드형 인프라(IaC)를 검증할 수 있습니다. IaC 검증을 통해 Terraform 리소스 정의가 Google Cloud 리소스에 적용된 기존 조직 정책과 Security Health Analytics 감지기를 위반하는지 여부를 확인할 수 있습니다.
GitHub ID 공급업체로 워크로드 아이덴티티 제휴를 구성합니다. 자세한 내용은 워크로드 아이덴티티 제휴를 참조하세요.
워크로드 아이덴티티 제휴 ID 토큰의 URL을 가져옵니다. 예를 들면 https://iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/POOL_ID/providers/PROVIDER_ID입니다.
다음 사항을 고려하세요.
PROJECT_NUMBER는 워크로드 아이덴티티 제휴를 설정한 Google Cloud 프로젝트의 프로젝트 번호입니다.
- name: Create Terraform Plan
id: plan
run: terraform plan -out=TF_PLAN_FILE
TF_PLAN_FILE을 Terraform 계획 파일의 이름으로 바꿉니다. 예를 들면 myplan.tfplan입니다.
계획 파일을 JSON 형식으로 변환합니다.
- name: Convert Terraform Plan to JSON
id: convert
run: terraform show -no-color -json TF_PLAN_FILE > TF_PLAN_JSON_FILE
TF_PLAN_JSON_FILE을 JSON 형식의 Terraform 계획 파일 이름으로 바꿉니다. 예를 들면 mytfplan.json입니다.
GitHub Actions 워크플로에 작업 추가
GitHub 저장소에서 워크플로를 찾습니다.
워크플로 편집기를 엽니다.
GitHub Marketplace 사이드바에서 코드 보안 분석을 검색합니다.
설치 섹션에서 구문을 복사합니다.
구문을 워크플로에 새 단계로 붙여넣습니다.
다음 값을 바꿉니다.
workload_identity_provider를 워크로드 아이덴티티 제휴 ID 토큰의 URL 링크로 바꿉니다.
service_account를 작업에 만든 서비스 계정의 이메일 주소로 바꿉니다.
organization_id를 Google Cloud 조직 ID로 바꿉니다.
scan_file_ref를 JSON 형식의 Terraform 계획 파일 경로로 바꿉니다.
failure_criteria를 작업이 실패하는 시기를 결정하는 실패 임곗값 기준으로 바꿉니다. 임곗값 기준은 IaC 검증 스캔에서 발생하는 매우 심각한 문제, 심각도가 높은 문제, 심각도가 중간인 문제, 심각도가 낮은 문제 수를 기반으로 합니다. failure_criteria는 허용되는 각 심각도의 문제 수와 문제가 합산되는 방식(AND 또는 OR)을 지정합니다. 예를 들어 심각한 문제 하나 또는 심각도가 높은 문제 하나가 발생할 경우 작업이 실패하도록 하려면 failure_criteria를 Critical:1,High:1,Operator:OR로 설정합니다. 기본값은 Critical:1,High:1,Medium:1,Low:1,Operator:OR입니다. 즉, IaC 검증 스캔에서 문제가 발생하면 작업이 실패해야 합니다.
이제 워크플로를 실행하여 Terraform 계획 파일을 검증할 수 있습니다. 워크플로를 수동으로 실행하려면 워크플로 수동 실행을 참조하세요.
IaC 위반 보고서 보기
GitHub 저장소에서 작업을 클릭하고 워크플로를 선택합니다.
워크플로의 가장 최근 실행을 클릭합니다.
아티팩트 섹션에서 위반 보고서(ias-scan-sarif.json)를 ZIP 파일로 사용할 수 있습니다. 보고서에는 다음 필드가 포함됩니다.
Terraform 계획에서 위반한 정책을 설명하는 rules 필드. 각 규칙에는 보고서에 포함된 결과와 일치할 수 있는 ruleID가 포함됩니다.
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["이해하기 어려움","hardToUnderstand","thumb-down"],["잘못된 정보 또는 샘플 코드","incorrectInformationOrSampleCode","thumb-down"],["필요한 정보/샘플이 없음","missingTheInformationSamplesINeed","thumb-down"],["번역 문제","translationIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-09-04(UTC)"],[],[],null,["| Premium and Enterprise [service tiers](/security-command-center/docs/service-tiers) (requires [organization-level activation](/security-command-center/docs/activate-scc-overview#overview_of_organization-level_activation))\n\nYou can use the [Analyze Code Security\naction](https://github.com/marketplace/actions/analyze-code-security) to\nvalidate the infrastructure as code (IaC) that is part of your GitHub Actions\nworkflow. Validating IaC lets you determine whether your Terraform resource\ndefinitions violate the existing organization policies and\nSecurity Health Analytics detectors that are applied to your Google Cloud resources.\n\nFor more information about IaC validation, see\n[Validate your IaC against your Google Cloud organization's policies](/security-command-center/docs/validate-iac).\n\nBefore you begin\n\nComplete these tasks to get started with IaC validation with GitHub Actions.\n\nActivate the Security Command Center Premium tier or Enterprise tier\n\nVerify that the\n[Security Command Center Premium tier or Enterprise tier](/security-command-center/docs/activate-scc-overview)\nis activated at the organization level.\n\nActivating Security Command Center enables the `securityposture.googleapis.com` and\n`securitycentermanagement.googleapis.com` APIs.\n\nCreate a service account\n\nCreate a service account that you can use for the Analyze Code Security\naction.\n\n1.\n In the Google Cloud console, go to the **Create service account** page.\n\n [Go to Create service account](https://console.cloud.google.com/projectselector/iam-admin/serviceaccounts/create?supportedpurview=project)\n2. Select your project.\n3.\n In the **Service account name** field, enter a name. The Google Cloud console fills\n in the **Service account ID** field based on this name.\n\n\n In the **Service account description** field, enter a description. For example,\n `Service account for quickstart`.\n4. Click **Create and continue**.\n5.\n Grant the **Security Posture Shift-Left Validator** role to the service account.\n\n\n To grant the role, find the **Select a role** list, then select\n **Security Posture Shift-Left Validator**.\n | **Note** : The **Role** field affects which resources the service account can access in your project. You can revoke these roles or grant additional roles later.\n6. Click **Continue**.\n7.\n Click **Done** to finish creating the service account.\n\n\u003cbr /\u003e\n\nFor more information about IaC validation permissions, see\n[IAM for organization-level activations](/security-command-center/docs/access-control-org).\n\nSet up authentication\n\n1. Configure Workload Identity Federation with your GitHub identity provider. For\n instructions, see\n [Workload Identity Federation](/iam/docs/workload-identity-federation).\n\n2. Obtain the URL for your Workload Identity Federation ID token. For example,\n `https://iam.googleapis.com/projects/`\u003cvar translate=\"no\"\u003ePROJECT_NUMBER\u003c/var\u003e`/locations/global/workloadIdentityPools/`\u003cvar translate=\"no\"\u003ePOOL_ID\u003c/var\u003e`/providers/`\u003cvar translate=\"no\"\u003ePROVIDER_ID\u003c/var\u003e.\n\n Consider the following:\n - \u003cvar translate=\"no\"\u003ePROJECT_NUMBER\u003c/var\u003e is the project number for the Google Cloud project that you set up Workload Identity Federation in.\n - \u003cvar translate=\"no\"\u003ePOOL_ID\u003c/var\u003e is the pool name.\n - \u003cvar translate=\"no\"\u003ePROVIDER_ID\u003c/var\u003e is the name of your identity provider.\n3. Add the [Authenticate to Google Cloud\n action](https://github.com/marketplace/actions/authenticate-to-google-cloud)\n to your workflow to authenticate the IaC validation action.\n\nDefine your policies\n\nDefine your\n[organization policies](/resource-manager/docs/organization-policy/creating-managing-policies)\nand\n[Security Health Analytics detectors](/security-command-center/docs/concepts-security-health-analytics).\nTo define these policies using a security posture, complete the tasks in\n[Create and deploy a posture](/security-command-center/docs/how-to-use-security-posture#create_and_deploy_a_posture).\n\nCreate your Terraform plan JSON file\n\n1. Create your Terraform code. For instructions, see [Create your Terraform\n code](/security-command-center/docs/validate-iac#create_your_terraform_code).\n\n2. In your GitHub Actions, initialize Terraform. For example, if you're using\n the [HashiCorp - Setup Terraform action](https://github.com/marketplace/actions/hashicorp-setup-terraform), run the following command:\n\n - name: Terraform Init\n id: init\n run: terraform init\n\n3. Create a Terraform plan file:\n\n - name: Create Terraform Plan\n id: plan\n run: terraform plan -out=\u003cvar translate=\"no\"\u003eTF_PLAN_FILE\u003c/var\u003e\n\n Replace \u003cvar translate=\"no\"\u003eTF_PLAN_FILE\u003c/var\u003e with the name for the Terraform plan\n file. For example, `myplan.tfplan`.\n4. Convert your plan file into JSON format:\n\n - name: Convert Terraform Plan to JSON\n id: convert\n run: terraform show -no-color -json \u003cvar translate=\"no\"\u003eTF_PLAN_FILE\u003c/var\u003e \u003e \u003cvar translate=\"no\"\u003eTF_PLAN_JSON_FILE\u003c/var\u003e\n\n Replace \u003cvar translate=\"no\"\u003eTF_PLAN_JSON_FILE\u003c/var\u003e with the name for the Terraform\n plan file, in JSON format. For example, `mytfplan.json`.\n\nAdd the action to your GitHub Actions workflow\n\n1. In the GitHub repository, browse to your workflow.\n2. Open the workflow editor.\n3. In the GitHub Marketplace sidebar, search for **Analyze Code Security**.\n4. In the **Installation** section, copy the syntax.\n5. Paste the syntax as a new step into your workflow.\n6. Replace the following values:\n\n - `workload_identity_provider` with the link to the URL for your Workload Identity Federation ID token.\n - `service_account` with the email address of the service account that you created for the action.\n - `organization_id` with your Google Cloud organization ID.\n - `scan_file_ref` with the path to your Terraform plan file, in JSON format.\n - `failure_criteria` with the failure threshold criteria that determines when the action fails. The threshold criteria is based on the number of critical, high, medium, and low severity issues that the IaC validation scan encounters. `failure_criteria` specifies how many issues of each severity are permitted and how the issues are aggregated (either `AND` or `OR`). For example, if you want the action to fail if it encounters one critical issue *or* one high severity issue, set the `failure_criteria` to `Critical:1,High:1,Operator:OR`. The default is `Critical:1,High:1,Medium:1,Low:1,Operator:OR`, which means that if the IaC validation scan encounters any issue, the action must fail.\n\nYou can now run the workflow to validate your Terraform plan file. To run the\nworkflow manually, see [Manually running a\nworkflow](https://docs.github.com/en/actions/using-workflows/manually-running-a-workflow).\n\nView the IaC violation report\n\n1. In your GitHub repository, click **Actions** and select your workflow.\n\n2. Click the most recent run for your workflow.\n\n In the **Artifacts** section, the violation report (`ias-scan-sarif.json`) is available in a zip file. The report includes the\n following fields:\n - A `rules` field that describes which policies were violated by the Terraform plan. Each rule includes a `ruleID` that you can match with the results that are included in the report.\n - A `results` field that describes the proposed asset modifications that violate a specific rule.\n3. Resolve any violations within your Terraform code before applying it.\n\nWhat's next\n\n- View the [analyze-code-security-scc action\n source code](https://github.com/google-github-actions/analyze-code-security-scc/) in GitHub."]]