커스텀 제약조건을 사용하여 Cloud Storage 리소스 관리

이 페이지에서는 조직 정책 서비스 커스텀 제약조건을 사용하여 다음 Google Cloud 리소스에 대한 특정 작업을 제한하는 방법을 보여줍니다.

  • storage.googleapis.com/Bucket

조직 정책에 대한 자세한 내용은 커스텀 조직 정책을 참조하세요.

조직 정책 및 제약조건 정보

Google Cloud 조직 정책 서비스를 사용하면 조직 리소스를 중앙에서 프로그래매틱 방식으로 제어할 수 있습니다. 조직 정책 관리자Google Cloud 리소스 계층 구조에서Google Cloud 리소스 및 이러한 리소스의 하위 요소에 적용되는 제약조건이라는 제한사항 집합인 조직 정책을 정의할 수 있습니다. 조직, 폴더 또는 프로젝트 수준에서 조직 정책을 적용할 수 있습니다.

조직 정책은 다양한 Google Cloud 서비스에 내장된 관리형 제약조건을 제공합니다. 그러나 조직 정책에서 제한되는 특정 필드를 보다 세부적으로 맞춤설정 가능한 방식으로 제어하려면 커스텀 제약조건을 만들고 조직 정책에 이러한 커스텀 제약조건을 사용할 수 있습니다.

정책 상속

기본적으로 조직 정책은 정책을 적용하는 리소스의 하위 요소에 상속됩니다. 예를 들어 폴더에 정책을 적용하면 Google Cloud 가 폴더의 모든 프로젝트에 정책을 적용합니다. 이 동작 및 이를 변경하는 방법에 대한 자세한 내용은 계층 구조 평가 규칙을 참조하세요.

제한사항

  • 커스텀 제약 조건 조건에서는 버킷 라벨을 사용하지 않는 것이 좋습니다. 대신 필요한 ID 및 액세스 관리 (IAM) 역할을 가진 개인만 설정할 수 있으며 라벨보다 더욱 엄격하게 제어되는 태그를 사용하세요.

  • 새로 적용된 커스텀 제약조건은 기존 리소스에 적용되지 않습니다. 제약 조건을 적용하려면 기존 리소스를 업데이트해야 합니다.

    업데이트해야 하는 기존 리소스를 찾으려면 테스트 실행 조직 정책을 시행하면 됩니다.

  • 커스텀 제약 조건은 객체 또는 버킷에 ACL 또는 IAM 정책을 제한하는 데 사용될 수 없습니다.

시작하기 전에

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. 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 (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Install the Google Cloud CLI.

  5. 외부 ID 공급업체(IdP)를 사용하는 경우 먼저 제휴 ID로 gcloud CLI에 로그인해야 합니다.

  6. gcloud CLI를 초기화하려면, 다음 명령어를 실행합니다.

    gcloud init
  7. 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 (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  8. Verify that billing is enabled for your Google Cloud project.

  9. Install the Google Cloud CLI.

  10. 외부 ID 공급업체(IdP)를 사용하는 경우 먼저 제휴 ID로 gcloud CLI에 로그인해야 합니다.

  11. gcloud CLI를 초기화하려면, 다음 명령어를 실행합니다.

    gcloud init
  12. 조직 ID를 알고 있어야 합니다.
  13. 필요한 역할

    조직 정책을 관리하는 데 필요한 권한을 얻으려면 관리자에게 다음의 IAM 역할을 부여해 달라고 요청합니다.

    역할 부여에 대한 자세한 내용은 프로젝트, 폴더, 조직에 대한 액세스 관리를 참조하세요.

    이러한 사전 정의된 역할에는 조직 정책 관리에 필요한 권한이 포함되어 있습니다. 필요한 정확한 권한을 보려면 필수 권한 섹션을 펼치세요.

    필수 권한

    조직 정책을 관리하려면 다음 권한이 필요합니다.

    • 조직에 대한 orgpolicy.* 권한
    • 이 페이지의 조직 정책 예시를 테스트하려면 프로젝트에 버킷을 만드세요. storage.buckets.create on the project

    커스텀 역할이나 다른 사전 정의된 역할을 사용하여 이 권한을 부여받을 수도 있습니다.

    커스텀 제약조건 만들기

    커스텀 제약조건은 조직 정책을 적용하는 서비스에서 지원되는 리소스, 메서드, 조건, 작업을 사용하여 YAML 파일에서 정의됩니다. 커스텀 제약조건의 조건은 Common Expression Language(CEL)를 사용하여 정의됩니다. CEL을 사용해서 커스텀 제약조건에서 조건을 빌드하는 방법은 커스텀 제약조건 만들기 및 관리의 CEL 섹션을 참조하세요.

    커스텀 제약조건을 만들려면 다음 형식을 사용하여 YAML 파일을 만듭니다.

    name: organizations/ORGANIZATION_ID/customConstraints/CONSTRAINT_NAME
    resourceTypes:
    - RESOURCE_NAME
    methodTypes:
    - CREATE
    - UPDATE
    condition: "CONDITION"
    actionType: ACTION
    displayName: DISPLAY_NAME
    description: DESCRIPTION
    

    다음을 바꿉니다.

    • ORGANIZATION_ID: 조직 ID(예: 123456789)

    • CONSTRAINT_NAME: 새 커스텀 제약조건에 사용하려는 이름. 커스텀 제약조건은 custom.으로 시작해야 하며 대문자, 소문자 또는 숫자만 포함할 수 있습니다. 예를 들면 custom.bucketNamingRequirement입니다. 이 필드의 최대 길이는 70자입니다.

    • RESOURCE_NAME: 제한하려는 객체 및 필드가 포함된Google Cloud 리소스의 정규화된 이름. 예를 들면 storage.googleapis.com/Bucket입니다.

    • CONDITION: 지원되는 서비스 리소스의 표현에 대해 작성된 CEL 조건. 이 필드의 최대 길이는 1000자(영문 기준)입니다. 조건을 작성하는 데 사용할 수 있는 리소스에 대한 자세한 내용은 지원되는 리소스를 참조하세요. 예를 들면 "resource.name.matches('^[a-zA-Z]+$')"입니다.

    • ACTION: condition이 충족될 때 수행할 작업. 가능한 값은 ALLOWDENY입니다.

    • DISPLAY_NAME: 제약조건에 대한 사용자 친화적인 이름. 이 필드의 최대 길이는 200자(영문 기준)입니다.

    • DESCRIPTION: 정책을 위반할 때 오류 메시지로 표시할 제약조건에 대한 사용자 친화적인 설명. 이 필드의 최대 길이는 2,000자(영문 기준)입니다.

    커스텀 제약조건을 만드는 방법에 대한 자세한 내용은 커스텀 제약조건 정의를 참조하세요.

    커스텀 제약조건 설정

    새 커스텀 제약조건의 YAML 파일을 만든 후에는 조직에서 조직 정책에 사용할 수 있도록 설정해야 합니다. 커스텀 제약조건을 설정하려면 gcloud org-policies set-custom-constraint 명령어를 사용합니다.
    gcloud org-policies set-custom-constraint CONSTRAINT_PATH
    CONSTRAINT_PATH를 커스텀 제약조건 파일의 전체 경로로 바꿉니다. 예를 들면 /home/user/customconstraint.yaml입니다. 완료되면 Google Cloud 조직 정책 목록에서 조직 정책으로 커스텀 제약조건을 사용할 수 있습니다. 커스텀 제약조건이 존재하는지 확인하려면 gcloud org-policies list-custom-constraints 명령어를 사용합니다.
    gcloud org-policies list-custom-constraints --organization=ORGANIZATION_ID
    ORGANIZATION_ID를 조직 리소스 ID로 바꿉니다. 자세한 내용은 조직 정책 보기를 참조하세요.

    커스텀 조직 정책 적용

    제약조건을 참조하는 조직 정책을 만들고 해당 조직 정책을 Google Cloud 리소스에 적용하여 제약조건을 적용할 수 있습니다.

    콘솔

    1. Google Cloud 콘솔에서 조직 정책 페이지로 이동합니다.

      조직 정책으로 이동

    2. 프로젝트 선택 도구에서 조직 정책을 설정할 프로젝트를 선택합니다.
    3. 조직 정책 페이지의 목록에서 제약조건을 선택하여 해당 제약조건의 정책 세부정보 페이지를 봅니다.
    4. 이 리소스의 조직 정책을 구성하려면 정책 관리를 클릭합니다.
    5. 정책 수정 페이지에서 상위 정책 재정의를 선택합니다.
    6. 규칙 추가를 클릭합니다.
    7. 적용 섹션에서 이 조직 정책 적용을 사용 설정할지 여부를 선택합니다.
    8. (선택사항) 태그로 조직 정책을 조건부로 만들려면 조건 추가를 클릭합니다. 조건부 규칙을 조직 정책에 추가하면 비조건부 규칙을 최소 하나 이상 추가해야 합니다. 그렇지 않으면 정책을 저장할 수 없습니다. 자세한 내용은 태그를 사용하여 조직 정책 설정을 참조하세요.
    9. 변경사항 테스트를 클릭하여 조직 정책의 효과를 시뮬레이션합니다. 기존 관리형 제약조건에는 정책 시뮬레이션을 사용할 수 없습니다. 자세한 내용은 정책 시뮬레이터로 조직 정책 변경사항 테스트를 참조하세요.
    10. 조직 정책을 완료하고 적용하려면 정책 설정을 클릭합니다. 정책이 적용되는 데 최대 15분이 소요됩니다.

    gcloud

    불리언 규칙이 있는 조직 정책을 만들려면 제약조건을 참조하는 정책 YAML 파일을 만듭니다.

          name: projects/PROJECT_ID/policies/CONSTRAINT_NAME
          spec:
            rules:
            - enforce: true
        

    다음을 바꿉니다.

    • PROJECT_ID: 제약조건을 적용할 프로젝트
    • CONSTRAINT_NAME: 커스텀 제약조건에 대해 정의된 이름. 예를 들면 custom.bucketNamingRequirement입니다.

    제약조건이 포함된 조직 정책을 적용하려면 다음 명령어를 실행합니다.

        gcloud org-policies set-policy POLICY_PATH
        

    POLICY_PATH를 조직 정책 YAML 파일의 전체 경로로 바꿉니다. 정책이 적용되는 데 최대 15분이 소요됩니다.

    커스텀 조직 정책 테스트

    다음 예시에서는 새로 생성된 모든 버킷의 이름에 문자만 포함되어야 하는 커스텀 제약 조건 및 정책을 만듭니다. 정책에서 방지해야 하는 작업을 시도하여 정책을 테스트할 수 있습니다.

    제약조건 만들기

    1. 다음 파일을 constraint-bucket-names.yaml로 저장합니다.

      name: organizations/ORGANIZATION_ID/customConstraints/custom.bucketNamingRequirement
      resource_types: storage.googleapis.com/Bucket
      method_types:
      - CREATE
      condition: "resource.name.matches('^[a-zA-Z]+$')"
      action_type: ALLOW
      display_name: Bucket names must match the specified regular expression
      description: Newly created buckets must have a name that matches the specified regular expression. Only letters are allowed in the bucket name.
      

      ORGANIZATION_ID를 조직 ID로 바꿉니다.

    2. 제약조건을 적용합니다.

      gcloud org-policies set-custom-constraint ~/constraint-bucket-names.yaml
      
    3. 제약조건이 있는지 확인합니다.

      gcloud org-policies list-custom-constraints --organization=ORGANIZATION_ID
      

      출력은 다음과 비슷합니다.

      CUSTOM_CONSTRAINT                 ACTION_TYPE    METHOD_TYPES    RESOURCE_TYPES                  DISPLAY_NAME
      custom.bucketNamingRequirement    ALLOW          CREATE          storage.googleapis.com/Bucket   Bucket names must match the specified regular expression
      ...
      

    정책 만들기

    1. 다음 파일을 policy-bucket-names.yaml로 저장합니다.

      name: projects/PROJECT_ID/policies/custom.bucketNamingRequirement
      spec:
        rules:
        - enforce: true
      

      PROJECT_ID를 프로젝트 ID로 바꿉니다.

      이 예시에서는 프로젝트 수준에서 이 제약조건을 적용하지만 조직 또는 폴더 수준에서 이를 설정할 수도 있습니다.

    2. 정책을 적용합니다.

      gcloud org-policies set-policy ~/policy-bucket-names.yaml
      
    3. 정책이 있는지 확인합니다.

      gcloud org-policies list --project=PROJECT_ID
      

      출력은 다음과 비슷합니다.

      CONSTRAINT                        LIST_POLICY    BOOLEAN_POLICY    ETAG
      custom.bucketNamingRequirement    -              SET               CIqktscGELiZn6cC-
      

    정책을 적용한 후 Google Cloud가 정책 시행을 시작할 때까지 2분 정도 기다립니다.

    정책 테스트

    • 문자가 아닌 문자가 포함된 이름으로 버킷을 만들어 보세요.

      gcloud storage buckets create gs://example-bucket --location=BUCKET_LOCATION
      

      BUCKET_LOCATION을 버킷의 위치로 바꿉니다. 예를 들면 US입니다.

      다음과 유사한 오류가 표시되면서 요청이 실패합니다.

      ERROR: (gcloud.storage.buckets.create) HTTPError 412: orgpolicy:projects/_/buckets/example-bucket violates customConstraints/custom.bucketNamingRequirement. Details: Newly created buckets must have a name that matches the specified regular expression. Only letters are allowed in the bucket name.
      

    일반적인 사용 사례의 커스텀 조직 정책 예시

    다음 표에서는 몇 가지 일반적인 맞춤 제약 조건에 대한 구문 예시를 제공합니다. Cloud Storage에서 사용할 수 있는 사전 정의된 제약 조건 목록은 Cloud Storage의 조직 정책 제약 조건을 참고하세요.

    설명 제약조건 구문
    버킷에 객체 버전 관리가 사용 설정되어 있어야 합니다.
      name: organizations/ORGANIZATION_ID/customConstraints/custom.enforceBucketVersioning
      method_types:
      - CREATE
      - UPDATE
      resource_types: storage.googleapis.com/Bucket
      condition: "resource.versioning.enabled == true"
      action_type: ALLOW
      display_name: Buckets must have Object Versioning enabled
      description: Newly created buckets and newly updated buckets must have Object Versioning enabled.
    특정 정규 표현식을 사용하여 버킷의 이름을 지정해야 합니다.
      name: organizations/ORGANIZATION_ID/customConstraints/custom.bucketNamingRequirement
      method_types:
      - CREATE
      resource_types: storage.googleapis.com/Bucket
      condition: "resource.name.matches('^[a-zA-Z]+$')"
      action_type: ALLOW
      display_name: Bucket names must match the specified regular expression
      description: Newly created buckets must have a name that matches the specified regular expression. Only letters are allowed in the bucket name.
    버킷에서 버킷 잠금을 사용 설정할 수 없습니다.
      name: organizations/ORGANIZATION_ID/customConstraints/custom.prohibitBucketLock
      method_types:
      - CREATE
      - UPDATE
      resource_types: storage.googleapis.com/Bucket
      condition: "resource.retentionPolicy.isLocked == true"
      action_type: DENY
      display_name: Prohibit the use of Bucket Lock
      description: Newly created buckets and newly updated buckets cannot have Bucket Lock enabled.
    버킷에서 객체 보관 잠금을 사용 설정할 수 없습니다.
      name: organizations/ORGANIZATION_ID/customConstraints/custom.prohibitObjectRetentionLock
      method_types:
      - CREATE
      - UPDATE
      resource_types: storage.googleapis.com/Bucket
      condition: "resource.objectRetention.mode == 'Enabled'"
      action_type: DENY
      display_name: Objects cannot have retention configurations
      description: Newly created buckets and newly updated buckets cannot have Object Retention Lock enabled.
    US 또는 EU 멀티 리전에 있는 버킷의 보관 기간은 86,400초여야 합니다.
      name: organizations/ORGANIZATION_ID/customConstraints/custom.locationRetentionPolicy
      method_types:
      - CREATE
      - UPDATE
      resource_types: storage.googleapis.com/Bucket
      condition: "(resource.location.startsWith('US') || resource.location.startsWith('EU')) && resource.retentionPolicy.retentionPeriod != 86400"
      action_type: DENY
      display_name: All buckets in US and EU must have a retention policy of 86,400 seconds
      description: Newly created buckets and newly updated buckets located in US and EU regions must have a retention policy of 86,400 seconds.
    버킷에 라벨이 있어야 합니다.1
      name: organizations/ORGANIZATION_ID/customConstraints/custom.labels
      method_types:
      - CREATE
      - UPDATE
      resource_types: storage.googleapis.com/Bucket
      condition: "'my_annotations.data.source' in resource.labels && resource.labels['my_annotations.data.source'] in ['SOURCE_IMAGES','SOURCE_TEXT','SOURCE_VIDEOS']"
      action_type: ALLOW
      display_name: Buckets must have a label classifying the contents of the bucket
      description: Newly created buckets and newly updated buckets must have the label my_annotations.data.source with the SOURCE_IMAGES, SOURCE_TEXT, or SOURCE_VIDEOS key.
    버킷은 이중 리전에 있어야 합니다.
      name: organizations/ORGANIZATION_ID/customConstraints/custom.dualRegionUS
      method_types:
      - CREATE
      - UPDATE
      resource_types: storage.googleapis.com/Bucket
      condition: "'US-EAST1' in resource.customPlacementConfig.dataLocations && 'US-EAST4' in resource.customPlacementConfig.dataLocations"
      action_type: ALLOW
      display_name: Buckets must be located in a dual-region
      description: Newly created buckets and newly updated buckets must be located in a dual-region composed of the us-east1 and us-east4 regions.
    버킷에서 기존 스토리지 클래스를 사용할 수 없습니다.
      name: organizations/ORGANIZATION_ID/customConstraints/custom.disableLegacyStorageClass
      method_types:
      - CREATE
      - UPDATE
      resource_types: storage.googleapis.com/Bucket
      condition: "resource.storageClass in ['STANDARD', 'NEARLINE', 'COLDLINE', 'ARCHIVE']"
      action_type: ALLOW
      display_name: Buckets cannot use legacy storage classes
      description: Newly created buckets and newly updated buckets must use Standard storage, Nearline storage, Coldline storage, or Archive storage.
    버킷 IP 필터링은 모든 공개 인터넷의 요청을 제한해야 합니다.
      name: organizations/ORGANIZATION_ID/customConstraints/custom.IpFilter
      method_types:
      - CREATE
      resource_types: storage.googleapis.com/Bucket
      condition: "!has(resource.ipFilter) || (resource.ipFilter.mode == 'Disabled' || resource.ipFilter.publicNetworkSource.allowedIpCidrRanges.size() > 0)"
      action_type: DENY
      display_name: Bucket IP filter rules must restrict all the public network
      description: Newly created buckets must have IP filtering and IP filtering rules must restrict all public network resources. 

    1 존재하지 않는 버킷 라벨 키를 지정하면 BAD_CONDITION 오류가 반환됩니다. 이 오류는 CREATEUPDATE 메서드가 버킷에서 실행되지 않도록 차단합니다. 이 오류를 방지하려면 먼저 항상 "my_annotations.data.source" in resource.labels를 사용하여 라벨 키가 존재하는지 확인합니다.

    조건부 조직 정책

    태그를 사용하여 커스텀 조직 정책을 조건부로 만들 수 있습니다. 자세한 내용은 태그를 사용하여 조직 정책 설정을 참고하세요.

    Cloud Storage 지원 리소스

    다음 표에는 커스텀 제약 조건에서 참조할 수 있는 Cloud Storage 리소스가 나와 있습니다.

    리소스 필드
    storage.googleapis.com/Bucket resource.billing.requesterPays
    resource.cors.maxAgeSeconds
    resource.cors.method
    resource.cors.origin
    resource.cors.responseHeader
    resource.customPlacementConfig.dataLocations
    resource.defaultEventBasedHold
    resource.encryption.defaultKmsKeyName
    resource.iamConfiguration.publicAccessPrevention
    resource.iamConfiguration.uniformBucketLevelAccess.enabled
    resource.ipFilter.mode
    resource.ipFilter.publicNetworkSource.allowedIpCidrRanges
    resource.ipFilter.vpcNetworkSources.allowedIpCidrRanges
    resource.ipFilter.vpcNetworkSources.network
    resource.labels
    resource.lifecycle.rule.action.storageClass
    resource.lifecycle.rule.action.type
    resource.lifecycle.rule.condition.age
    resource.lifecycle.rule.condition.createdBefore
    resource.lifecycle.rule.condition.customTimeBefore
    resource.lifecycle.rule.condition.daysSinceCustomTime
    resource.lifecycle.rule.condition.daysSinceNoncurrentTime
    resource.lifecycle.rule.condition.isLive
    resource.lifecycle.rule.condition.matchesPrefix
    resource.lifecycle.rule.condition.matchesStorageClass
    resource.lifecycle.rule.condition.matchesSuffix
    resource.lifecycle.rule.condition.noncurrentTimeBefore
    resource.lifecycle.rule.condition.numNewerVersions
    resource.location
    resource.locationType
    resource.logging.logBucket
    resource.logging.logObjectPrefix
    resource.name
    resource.objectRetention.mode
    resource.retentionPolicy.isLocked
    resource.retentionPolicy.retentionPeriod
    resource.rpo
    resource.softDeletePolicy.retentionDurationSeconds
    resource.storageClass
    resource.versioning.enabled
    resource.website.mainPageSuffix
    resource.website.notFoundPage

    다음에 유의하세요.

    • 다음 필드의 값을 지정할 때는 대문자를 사용해야 합니다.
      • resource.customPlacementConfig.dataLocations
      • resource.lifecycle.rule.action.storageClass
      • resource.location
      • resource.storageClass
    • resource.retentionPolicy.isLocked 필드는 버킷 잠금을 강제 적용하는 것이 아니라 버킷 잠금 사용을 금지하는 데만 사용할 수 있습니다.

    다음 단계