n-y-kim / azure-policy-tf

Deploy Azure Policy with Terraform

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

azure-policy-tf

개요

Azure Policy파일은 json으로 되어 있으므로 json파일을 별도의 디렉토리에 리소스 별로 관리함. 테라폼은 이 디렉토리에 있는 모든 json들을 반영함.

Quick Start

본 Repo를 Clone

$ git clone <this-repository>

Policy 작성

  • 적용할 built-in policy들은 공식 repo에서 받아 builtin-policies에 넣어 놓음
  • 커스텀 policy들은 여기를를 참고하여 직접 json으로 작성하며 custom-policies에 넣어 놓음.

Policy definition 생성

$ terraform plan -out tfplan
$ terraform apply tfplan

폴더 구조

.
├── README.md
├── builtin-policies
│   ├── compute
│   │   └── ClassicCompute_Audit.json
│   │   └── Builtin_Policy_Sample.json
│   │   └── ....
│   │    
│   ├── general
│   │   └── NotAllowM365_Deny.json
│   │   └── ....
│   │
│   └── 카테고리 별 폴더 이름 ...
│   
├── custom-policies
│   ├── network
│   │   └── subnet-need-nsg-assigned-to-be-created.json
│   │   └── ....
│   │
│   └── 카테고리 별 폴더 이름 ....
│   
├── main.tf
├── modules
│   └── policy
│       └── policy_apply.tf
├── providers.tf
  • builtin-policies: Azure에서 제공하는 기본 정책들을 카테고리별로 복사해서 정리한 폴더. Allowed values, Default Value, Name 등을 수정한 것들도 여기에 포함.
  • custom-policies: Built-in base가 아닌 완전히 새로운 정책들을 정리한 폴더.

    해당 폴더에 있는 json은 예시입니다

  • policy 모듈: policy_directory 변수에 따라 builtin-policies 또는 custom-policies 폴더를 참조하여 정책을 적용하는 모듈.
  • main.tf: 정책을 적용하는 메인 파일.

built-in policy json 파일 작성 시 유의사항

1. name 값은 수정하지 않아도 됨.

Azure policy github에서 json 파일을 다운 받아보면 name 값이 정해져 있음.

예시)

{
  "properties": {
    "displayName": "Container registries should have local admin account disabled.",
    "description": "Disable admin account for your registry so that it is not accessible by local admin. Disabling local authentication methods like admin user, repository scoped access tokens and anonymous pull improves security by ensuring that container registries exclusively require Azure Active Directory identities for authentication. Learn more at: https://aka.ms/acr/authentication.",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "metadata": {
      "version": "1.0.1",
      "category": "Container Registry"
    },
    "version": "1.0.1",
    "parameters": {
      "effect": {
        "type": "String",
        "defaultValue": "Audit",
        "allowedValues": [
          "Audit",
          "Deny",
          "Disabled"
        ],
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        }
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.ContainerRegistry/registries"
          },
          {
            "field": "Microsoft.ContainerRegistry/registries/adminUserEnabled",
            "equals": true
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    }
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/dc921057-6b28-4fbe-9b83-f7bec05db6c2", 
  "name": "dc921057-6b28-4fbe-9b83-f7bec05db6c2" # 이 부분
}

이 Built in policy를 식별하게 하는 문자열이므로, 이미 기본 Built in으로 배포되어 있기 때문에 같은 name으로 정책을 만들 수 없음.

따라서 modules/policy/policy_apply.tf 에서 모든 정책의 nameDisplayName으로 변경하여 배포. (해당 파일 L14) 사용자가 따로 해당 파일의 name을 변경할 필요가 없음.

2. displayName 값이나 description 값은 수정 권고.

이미 존재하는 Built-in policy와 이름이 겹치지 않게 하기 위해 displayName 값을 수정하는 것을 권고. 가독성을 위해 한글로 작성하는 것도 좋음.

About

Deploy Azure Policy with Terraform


Languages

Language:HCL 100.0%