커스텀 제약조건 사용

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

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

혜택

커스텀 조직 정책을 사용하여 서버리스 Dataproc 배치에서 특정 작업을 허용하거나 거부할 수 있습니다. 예를 들어 배치 워크로드 생성 요청이 조직 정책에 설정된 대로 커스텀 제약조건 검증을 충족하지 못하면 요청이 실패하고 호출자에게 오류가 반환됩니다.

정책 상속

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

가격 책정

사전 정의된 제약조건 및 커스텀 제약조건을 포함하여 조직 정책 서비스는 무료로 제공됩니다.

시작하기 전에

  1. 프로젝트 설정
    1. Google Cloud 계정에 로그인합니다. Google Cloud를 처음 사용하는 경우 계정을 만들고 Google 제품의 실제 성능을 평가해 보세요. 신규 고객에게는 워크로드를 실행, 테스트, 배포하는 데 사용할 수 있는 $300의 무료 크레딧이 제공됩니다.
    2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

      Go to project selector

    3. Google Cloud 프로젝트에 결제가 사용 설정되어 있는지 확인합니다.

    4. Dataproc Serverless API 사용 설정

      API 사용 설정

    5. Google Cloud CLI를 설치합니다.
    6. gcloud CLI를 초기화하려면 다음 명령어를 실행합니다.

      gcloud init
    7. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

      Go to project selector

    8. Google Cloud 프로젝트에 결제가 사용 설정되어 있는지 확인합니다.

    9. Dataproc Serverless API 사용 설정

      API 사용 설정

    10. Google Cloud CLI를 설치합니다.
    11. gcloud CLI를 초기화하려면 다음 명령어를 실행합니다.

      gcloud init
    12. 조직 ID를 알고 있어야 합니다.

필요한 역할

조직 정책을 관리하는 데 필요한 권한을 얻으려면 관리자에게 조직 리소스에 대한 조직 정책 관리자(roles/orgpolicy.policyAdmin) IAM 역할을 요청하세요. 역할 부여에 대한 자세한 내용은 액세스 관리를 참조하세요.

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

필수 권한

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

  • orgpolicy.constraints.list
  • orgpolicy.policies.create
  • orgpolicy.policies.delete
  • orgpolicy.policies.list
  • orgpolicy.policies.update
  • orgpolicy.policy.get
  • orgpolicy.policy.set

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

커스텀 제약조건 만들기

커스텀 제약조건은 YAML 파일에서 적용되는 리소스, 메서드, 조건, 작업으로 정의됩니다. Dataproc 서버리스는 BATCH 리소스의 CREATE 메서드에 적용되는 커스텀 제약조건을 지원합니다(리소스 및 작업에 대한 Dataproc 서버리스 제약조건 참조).

Dataproc 서버리스 커스텀 제약조건에 대해 YAML 파일을 만들려면 다음 안내를 따르세요.

name: organizations/ORGANIZATION_ID/customConstraints/CONSTRAINT_NAME
resourceTypes:
- dataproc.googleapis.com/Batch
methodTypes:
- CREATE
condition: CONDITION
actionType: ACTION
displayName: DISPLAY_NAME
description: DESCRIPTION

다음을 바꿉니다.

  • ORGANIZATION_ID: 조직 ID입니다(예: 123456789).

  • CONSTRAINT_NAME: 새 커스텀 제약조건에 사용하려는 이름입니다. 커스텀 제약조건은 custom.으로 시작해야 하며 대문자, 소문자 또는 숫자만 포함할 수 있습니다(예: custom.batchMustHaveSpecifiedCategoryLabel). 이 필드의 최대 길이는 프리픽스를 제외하고 70자입니다(예: organizations/123456789/customConstraints/custom).

  • CONDITION: 지원되는 서비스 리소스의 표현에 대해 작성된 CEL 조건입니다. 이 필드의 최대 길이는 1000자(영문 기준)입니다. 조건을 작성하는 데 사용할 수 있는 리소스에 대한 자세한 내용은 지원되는 리소스를 참조하세요. 샘플 조건: ("category" in resource.labels) && (resource.labels['category'] in ['retail', 'ads', 'service'])

  • ACTION: 조건이 충족될 때 수행할 작업. ALLOW 또는 DENY일 수 있습니다.

  • DISPLAY_NAME: 제약조건에 대한 사용자 친화적인 이름. 샘플 표시 이름: "배치 '카테고리' 라벨 요구 조건 적용". 이 필드의 최대 길이는 200자(영문 기준)입니다.

  • DESCRIPTION: 정책을 위반할 때 오류 메시지로 표시할 제약조건에 대한 사용자 친화적인 설명입니다. 이 필드의 최대 길이는 2,000자(영문 기준)입니다. 샘플 설명: '소매업체', '광고' 또는 '서비스' 값이 있는 '카테고리' 라벨이 있는 경우에만 Dataproc 배치 만들기 허용'.

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

커스텀 제약조건 설정

Google Cloud CLI를 사용하여 새 커스텀 제약조건을 만든 다음에는 조직에서 조직 정책에 사용할 수 있도록 설정해야 합니다. 커스텀 제약조건을 설정하려면 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. 커스텀 제약조건인 경우 변경사항 테스트를 클릭하여 이 조직 정책의 효과를 시뮬레이션할 수 있습니다. 자세한 내용은 정책 시뮬레이터로 조직 정책 변경사항 테스트를 참조하세요.
  11. 조직 정책을 완료하고 적용하려면 정책 설정을 클릭합니다. 정책이 적용되는 데 최대 15분이 소요됩니다.

gcloud

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

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

다음을 바꿉니다.

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

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

    gcloud org-policies set-policy POLICY_PATH
    

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

커스텀 제약조건 테스트

다음 배치 만들기 예시에서는 커스텀 제약조건이 생성되었으며 배치 생성 시 배치에 '소매업', '광고' 또는 '서비스' 값이 연결된 '카테고리' 라벨을 포함하게 되어 있다고 가정합니다: ("category" in resource.labels) && (resource.labels['category'] in ['retail', 'ads', 'service']). 예제의 'category' 라벨에는 필수 값 중 하나가 없습니다.

gcloud dataproc batches submit spark \
  --region us-west1
  --jars file:///usr/lib/spark/examples/jars/spark-examples.jar \
  --class org.apache.spark.examples.SparkPi  \
  --network default \
  --labels category=foo \
  -- 100

샘플 출력:

Operation denied by custom org policies: ["customConstraints/custom.batchMustHaveSpecifiedCategoryLabel": ""Only allow Dataproc batch creation if it has a 'category' label with
  a 'retail', 'ads', or 'service' value""]

리소스 및 작업에 대한 Dataproc 서버리스 제약조건

다음 서버리스 Dataproc 커스텀 제약조건은 배치 워크로드를 생성(제출)할 때 사용할 수 있습니다.

일반

  • resource.labels

RuntimeConfig

  • resource.runtimeConfig.version
  • resource.runtimeConfig.containerImage
  • resource.runtimeConfig.properties

ExecutionConfig

  • resource.environmentConfig.executionConfig.serviceAccount
  • resource.environmentConfig.executionConfig.networkTags
  • resource.environmentConfig.executionConfig.kmsKey
  • resource.environmentConfig.executionConfig.stagingBucket

PeripheralsConfig

  • resource.environmentConfig.peripheralsConfig.metastoreService
  • resource.environmentConfig.peripheralsConfig.sparkHistoryServerConfig.dataprocCluster

일반적인 사용 사례의 커스텀 제약조건 예시

다음 표에서는 Dataproc 서버리스 배치 커스텀 제약조건의 예시를 제공합니다.

설명 제약조건 문법
배치는 '카테고리' 라벨을 허용되는 값으로 연결해야 합니다.

    name: organizations/ORGANIZATION_ID/customConstraints/custom.batchMustHaveSpecifiedCategoryLabel
    resourceTypes:
    - dataproc.googleapis.com/Batch
    methodTypes:
    - CREATE
    condition: ("category" in resource.labels) && (resource.labels['category'] in ['retail', 'ads', 'service'])
    actionType: ALLOW
    displayName: Enforce batch "category" label requirement.
    description: Only allow batch creation if it attaches a "category" label with an allowable value.
배치는 허용되는 런타임 버전을 설정해야 합니다.

    name: organizations/ORGANIZATION_ID/customConstraints/custom.batchMustUseAllowedVersion
    resourceTypes:
    - dataproc.googleapis.com/Batch
    methodTypes:
    - CREATE
    condition:  (has(resource.runtimeConfig.version)) && (resource.runtimeConfig.version in ["2.0.45", "2.0.48"])
    actionType: ALLOW
    displayName: Enforce batch runtime version.
    description: Only allow batch creation if it sets an allowable runtime version.
Spark 초기 실행자는 20개를 초과하여 배치를 설정할 수 없습니다.

    name: organizations/ORGANIZATION_ID/customConstraints/custom.batchInitialExecutorMax20
    resourceTypes:
    - dataproc.googleapis.com/Batch
    methodTypes:
    - CREATE
    condition: (has(resource.runtimeConfig.properties)) && ('spark.executor.instances' in resource.runtimeConfig.properties)
     && (int(resource.runtimeConfig.properties['spark.executor.instances'])>20)
    actionType: DENY
    displayName: Enforce maximum number of batch Spark executor instances.
    description: Deny batch creation if it specifies more than 20 Spark executor instances.
배치 처리에서는 Spark 동적 할당 초기 실행자를 20개 넘게 설정할 수 없습니다.

    name: organizations/ORGANIZATION_ID/customConstraints/custom.batchDynamicAllocationInitialExecutorMax20
    resourceTypes:
    - dataproc.googleapis.com/Batch
    methodTypes:
    - CREATE
    condition: (has(resource.runtimeConfig.properties)) && ('spark.dynamicAllocation.initialExecutors' in resource.runtimeConfig.properties)
     && (int(resource.runtimeConfig.properties['spark.dynamicAllocation.initialExecutors'])>20)
    actionType: DENY
    displayName: Enforce maximum number of batch dynamic allocation initial executors.
    description: Deny batch creation if it specifies more than 20 Spark dynamic allocation initial executors.
배치에서 동적 할당 실행자를 20개 넘게 허용하지 않아야 합니다.

    name: organizations/ORGANIZATION_ID/customConstraints/custom.batchDynamicAllocationMaxExecutorMax20
    resourceTypes:
    - dataproc.googleapis.com/Batch
    methodTypes:
    - CREATE
    condition: (resource.runtimeConfig.properties['spark.dynamicAllocation.enabled']=='false') || (('spark.dynamicAllocation.maxExecutors' in resource.runtimeConfig.properties) && (int(resource.runtimeConfig.properties['spark.dynamicAllocation.maxExecutors'])<=20))
    actionType: ALLOW
    displayName: Enforce batch maximum number of dynamic allocation executors.
    description:  Only allow batch creation if dynamic allocation is disabled or
    the maximum number of dynamic allocation executors is set to less than or equal to 20.
배치는 허용되는 패턴으로 KMS 키를 설정해야 합니다.

    name: organizations/ORGANIZATION_ID/custom.batchKmsPattern
    resourceTypes:
    - dataproc.googleapis.com/Batch
    methodTypes:
    - CREATE
    condition:  matches(resource.environmentConfig.executionConfig.kmsKey, '^keypattern[a-z]$')
    actionType: ALLOW
    displayName: Enforce batch KMS Key pattern.
    description: Only allow batch creation if it sets the KMS key to an allowable pattern.
배치는 스테이징 버킷 프리픽스를 허용되는 값으로 설정해야 합니다.

    name: organizations/ORGANIZATION_ID/customConstraints/custom.batchStagingBucketPrefix
    resourceTypes:
    - dataproc.googleapis.com/Batch
    methodTypes:
    - CREATE
    condition:  resource.environmentConfig.executionConfig.stagingBucket.startsWith(ALLOWED_PREFIX)
    actionType: ALLOW
    displayName: Enforce batch staging bucket prefix.
    description: Only allow batch creation if it sets the staging bucket prefix to ALLOWED_PREFIX.

다음 단계