組織のポリシー サービスを使用すると、組織のリソースをプログラマティックに一元管理できます。組織のポリシー管理者は組織のポリシーを定義できます。組織のポリシーは、Google Cloud のリソース階層内の Cloud Build リソースやそれらのリソースの子孫に適用される、制約と呼ばれる一連の制限です。組織のポリシーは、組織レベル、フォルダレベル、またはプロジェクト レベルで適用できます。
組織のポリシー サービスには、さまざまな Cloud Build サービスに対する事前定義された制約が存在します。ただし、組織のポリシーで制限されている特定のフィールドをカスタマイズ可能な方法でより詳細に制御する必要がある場合は、カスタムの組織のポリシーを作成することもできます。
利点
カスタムの組織のポリシーを使用して、特定の Cloud Build リソースを許可または拒否できます。たとえば、ビルドトリガーの作成または更新を行うリクエストが、組織のポリシーによって設定されたカスタム制約検証の要件を満たしていない場合、リクエストは失敗し、エラーが呼び出し元に返されます。
ポリシーの継承
デフォルトでは、組織のポリシーは、そのポリシーを適用したリソースの子孫に継承されます。たとえば、フォルダにポリシーを適用した場合は、Cloud Build によってフォルダ内のすべてのプロジェクトにポリシーが適用されます。この動作の詳細と変更方法については、階層評価ルールをご覧ください。
料金
事前定義の組織のポリシーやカスタムの組織のポリシーを含め、組織のポリシー サービスは無料です。
始める前に
- プロジェクトを設定する
- 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.
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Build API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
Install the Google Cloud CLI.
-
外部 ID プロバイダ(IdP)を使用している場合は、まず連携 ID を使用して gcloud CLI にログインする必要があります。
-
gcloud CLI を初期化するには、次のコマンドを実行します。
gcloud init -
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Build API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
Install the Google Cloud CLI.
-
外部 ID プロバイダ(IdP)を使用している場合は、まず連携 ID を使用して gcloud CLI にログインする必要があります。
-
gcloud CLI を初期化するには、次のコマンドを実行します。
gcloud init - 組織 ID を確認します。
ORGANIZATION_ID: 組織 ID(123456789など)。CONSTRAINT_NAME: 新しいカスタム制約に付ける名前。カスタム制約はcustom.で始まる必要があり、大文字、小文字、数字のみを含めることができます(例:custom.enableBuildTrigger)。このフィールドの最大長は 70 文字です。接頭辞(例:organizations/123456789/customConstraints/custom.allowConstraint)はカウントされません。RESOURCE_NAME: 制限するオブジェクトとフィールドを含む Cloud Build リソースの名前(URI ではありません)。例:BuildTriggerCONDITION: サポート対象のサービス リソースの表現に対して書き込まれる CEL 条件。このフィールドの最大長は 1,000 文字です。条件の書き込み先として使用できるリソースの詳細については、サポート対象のリソースをご覧ください。例:"resource.github.name.contains('cloudbuild')"ACTION:conditionが満たされている場合に実行するアクション。ALLOWまたはDENYになります。DISPLAY_NAME: 制約の名前。わかりやすい名前を入力してください。このフィールドの最大長は 200 文字です。DESCRIPTION: ポリシー違反時にエラー メッセージとして表示される制約の説明。わかりやすい説明を入力してください。このフィールドの最大長は 2000 文字です。- Google Cloud コンソールで [組織のポリシー] ページに移動します。
- プロジェクト選択ツールで、組織のポリシーを設定するプロジェクトを選択します。
- [組織のポリシー] ページのリストで制約を選択して、その制約の [ポリシーの詳細] ページを表示します。
- このリソースの組織のポリシーを構成するには、[ポリシーを管理] をクリックします。
- [ポリシーの編集] ページで、[Override parent's policy] を選択します。
- [ルールを追加] をクリックします。
- [適用] セクションで、この組織のポリシーの適用を有効にするかどうかを選択します。
- 省略可: タグで組織のポリシーに条件を設定するには、[条件を追加] をクリックします。組織のポリシーに条件付きルールを追加する場合は、少なくとも 1 つは無条件のルールを追加する必要があります。そうしないとポリシーを保存できないのでご注意ください。詳細については、タグ付きの組織のポリシーの設定をご覧ください。
- [変更内容をテスト] をクリックして、組織のポリシーの効果をシミュレートします。以前のマネージド制約ではポリシー シミュレーションを使用できません。詳細については、Policy Simulator で組織のポリシーの変更をテストするをご覧ください。
- 組織のポリシーを完成させて適用するには、[ポリシーを設定] をクリックします。ポリシーが有効になるまでに最大 15 分かかります。
-
PROJECT_ID: 制約を適用するプロジェクト。 -
CONSTRAINT_NAME: カスタム制約に定義した名前。たとえば、custom.enableBuildTriggerのようにします。 - Cloud Build のビルド構成
resource.source.storageSource.bucketresource.source.storageSource.objectresource.source.storageSource.generationresource.source.storageSource.sourceFetcherresource.source.repoSource.projectIdresource.source.repoSource.repoNameresource.source.repoSource.branchNameresource.source.repoSource.tagNameresource.source.repoSource.commitSharesource.source.repoSource.dirresource.source.repoSource.invertRegexresource.source.repoSource.substitutionsresource.source.gitSource.urlresource.source.gitSource.dirresource.source.gitSource.revisionresource.source.storageSourceManifest.bucketresource.source.storageSourceManifest.objectresource.source.storageSourceManifest.generationresource.source.connectedRepository.repositoryresource.source.connectedRepository.dirresource.source.connectedRepository.revisionresource.source.developerConnectConfig.gitRepositoryLinkresource.source.developerConnectConfig.dirresource.source.developerConnectConfig.revisionresource.steps.nameresource.steps.envresource.steps.argsresource.steps.dirresource.steps.idresource.steps.waitForresource.steps.entryPointsresource.steps.secretEnvresource.steps.volumes.nameresource.steps.volumes.pathresource.steps.timeoutresource.steps.allowFailureresource.steps.allowExitCodesresource.steps.scriptresource.steps.automapSubstitutionsresource.timeoutresource.imagesresource.queueTtlresource.artifacts.imagesresource.artifacts.objects.locationresource.artifacts.objects.pathsresource.logsBucketresource.options.sourceProvenanceHashresource.options.requestedVerifyOptionresource.options.machineTyperesource.options.diskSizeGbresource.options.substitutionOptionresource.options.dynamicSubstitutionsresource.options.autoMapSubstitutionsresource.options.logStreamingOptionresource.options.pool.nameresource.options.loggingresource.options.envresource.options.secretEnvresource.options.volumes.nameresource.options.volumes.pathresource.options.defaultLogsBucketBehaviorresource.substitutionsresource.tagsresource.secrets.kmsKeyNameresource.secrets.secretEnvresource.serviceAccountresource.availableSecrets.secretManager.versionNameresource.availableSecrets.secretManager.envresource.availableSecrets.inline.kmsKeyNameresource.availableSecrets.inline.envMapresource.gitConfig.proxySecretVersionNameresource.gitConfig.proxySslCaInforesource.gitConfig.http
- Cloud Build BitbucketServerConfig
resource.nameresource.hostUrlresource.secrets.adminAccessTokenVersionNameresource.secrets.readAccessTokenVersionNameresource.secrets.webhookSecretVersionNameresource.usernameresource.apiKeyresource.peeredNetworkresource.sslCaresource.peeredNetworkIpRange
- Cloud Build WorkerPool の構成
resource.displayNameresource.annotationsresource.privatePoolV1Config.workerConfig.machineTyperesource.privatePoolV1Config.workerConfig.diskSizeGbresource.privatePoolV1Config.networkConfig.peeredNetworkresource.privatePoolV1Config.networkConfig.egressOptionresource.privatePoolV1Config.networkConfig.peeredNetworkIpRange
- Cloud Build BuildTrigger 構成
resource.tagsresource.resourceNameresource.descriptionresource.nameresource.tagsresource.triggerTemplate.projectIdresource.triggerTemplate.repoNameresource.triggerTemplate.branchNameresource.triggerTemplate.tagNameresource.triggerTemplate.commitSharesource.triggerTemplate.dirresource.triggerTemplate.invertRegexresource.triggerTemplate.substitutionsresource.github.ownerresource.github.nameresource.github.enterpriseConfigResourceNameresource.pubsubConfig.topicresource.pubsubConfig.serviceAccountEmailresource.webhookConfig.secretresource.bitbucketServerTriggerConfig.repoSlugresource.bitbucketServerTriggerConfig.projectKeyresource.bitbucketServerTriggerConfig.pullRequest.branchresource.bitbucketServerTriggerConfig.pullRequest.commentControlresource.bitbucketServerTriggerConfig.pullRequest.invertRegexresource.bitbucketServerTriggerConfig.push.branchresource.bitbucketServerTriggerConfig.push.tagresource.bitbucketServerTriggerConfig.push.invertRegexresource.gitlabEnterpriseEventsConfig.projectNamespaceresource.gitlabEnterpriseEventsConfig.pullRequest.branchresource.gitlabEnterpriseEventsConfig.pullRequest.commentControlresource.gitlabEnterpriseEventsConfig.pullRequest.invertRegexresource.gitlabEnterpriseEventsConfig.push.branchresource.gitlabEnterpriseEventsConfig.push.tagresource.gitlabEnterpriseEventsConfig.push.invertRegexresource.gitlabEnterpriseEventsConfig.gitlabConfigResourceresource.disabledresource.substitutionsresource.ignoredFilesresource.includedFilesresource.sourceToBuild.uriresource.sourceToBuild.repositoryresource.sourceToBuild.refresource.sourceToBuild.repoTyperesource.sourceToBuild.githubEnterpriseConfigresource.approvalConfig.approvalRequiredresource.filterresource.serviceAccountresource.eventTyperesource.includeBuildLogsresource.repositoryEventConfig.repositoryresource.repositoryEventConfig.pullRequest.branchresource.repositoryEventConfig.pullRequest.commentControlresource.repositoryEventConfig.pullRequest.invertRegexresource.repositoryEventConfig.push.branchresource.repositoryEventConfig.push.tagresource.repositoryEventConfig.push.invertRegex
- Cloud Build GitHubEnterpriseConfig の構成
resource.nameresource.hostUrlresource.appIdresource.nameresource.webhookKeyresource.peeredNetworkresource.secrets.privateKeyVersionNameresource.secrets.webhookSecretVersionNameresource.secrets.oauthSecretVersionNameresource.secrets.oauthClientIdVersionNameresource.displayNameresource.sslCa
- Cloud Build 接続の構成
resource.nameresource.githubConfig.authorizerCredential.oauthTokenSecretVersionNameresource.githubConfig.appInstallationIdresource.githubEnterpriseConfig.hostUriresource.githubEnterpriseConfig.apiKeyresource.githubEnterpriseConfig.appIdresource.githubEnterpriseConfig.appSlugresource.githubEnterpriseConfig.privateKeySecretVersionresource.githubEnterpriseConfig.webhookSecretSecretVersionresource.githubEnterpriseConfig.oauthSecretSecretVersionresource.githubEnterpriseConfig.oauthClientIdSecretVersionresource.githubEnterpriseConfig.authorizerCredential.oauthTokenSecretVersionNameresource.githubEnterpriseConfig.appInstallationIdresource.githubEnterpriseConfig.serviceDirectoryConfig.serviceresource.githubEnterpriseConfig.sslCaresource.gitlabConfig.hostUriresource.gitlabConfig.webhookSecretSecretVersionresource.gitlabConfig.readAuthorizerCredential.oauthTokenSecretVersionNameresource.gitlabConfig.authorizerCredential.oauthTokenSecretVersionNameresource.gitlabConfig.serviceDirectoryConfig.serviceresource.gitlabConfig.sslCaresource.gitlabConfig.serviceDirectoryConfig.serviceresource.bitbucketDataCenterConfig.hostUriresource.bitbucketDataCenterConfig.webhookSecretSecretVersionresource.bitbucketDataCenterConfig.readAuthorizerCredential.oauthTokenSecretVersionNameresource.bitbucketDataCenterConfig.authorizerCredential.oauthTokenSecretVersionNameresource.bitbucketDataCenterConfig.sslCaresource.bitbucketDataCenterConfig.serviceDirectoryConfig.serviceresource.bitbucketCloudConfig.workspaceresource.bitbucketCloudConfig.webhookSecretSecretVersionresource.bitbucketCloudConfig.readAuthorizerCredential.oauthTokenSecretVersionNameresource.bitbucketCloudConfig.authorizerCredential.oauthTokenSecretVersionName
- Cloud Build リポジトリの構成
resource.nameresource.remoteUriresource.annotations
ORGANIZATION_ID: 組織 ID(123456789など)。- 組織のポリシーの詳細について、組織のポリシー サービスの概要を確認する。
- 組織のポリシーの作成と管理の方法について学習する。
- 事前定義された組織のポリシーの制約の完全なリストを確認する。
必要なロール
組織のポリシーを管理するために必要な権限を取得するには、組織のリソースに対する組織のポリシー管理者(
roles/orgpolicy.policyAdmin)の IAM ロールを付与するよう管理者に依頼してください。ロールの付与については、プロジェクト、フォルダ、組織に対するアクセス権の管理をご覧ください。必要な権限は、カスタムロールや他の事前定義ロールから取得することもできます。
Cloud Build を作成するために必要なロールをユーザー アカウントに追加する必要もあります。Cloud Build リソースへのアクセスの構成をご覧ください。Cloud Build に関係する IAM ロールの詳細については、IAM ロールと権限をご覧ください。
カスタム制約を作成する
カスタム制約を作成するには、YAML ファイルを使用して、制約の対象となるリソース、メソッド、条件、アクションを定義します。これらは、組織のポリシーを適用するサービスに固有のものです。カスタム制約の条件は、Common Expression Language を使用して定義する必要があります。Common Expression Language(CEL)に関する GitHub ページをご覧ください。CEL を使用してカスタム制約で条件を作成する方法については、カスタム制約の作成と管理の CEL セクションをご覧ください。
次のテンプレートを使用して、カスタム制約の YAML ファイルを作成します。
name: organizations/ORGANIZATION_ID/customConstraints/CONSTRAINT_NAME resourceTypes: - cloudbuild.googleapis.com/RESOURCE_NAME methodTypes: - CREATE - UPDATE condition: "CONDITION" actionType: ACTION displayName: DISPLAY_NAME description: DESCRIPTION以下を置き換えます。
カスタム制約の作成方法の詳細については、カスタム組織のポリシーの作成と管理をご覧ください。
カスタム制約を設定する
新しいカスタム制約の 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 リソースに適用します。コンソール
gcloud
ブール型ルールを含む組織のポリシーを作成するには、制約を参照するポリシー YAML ファイルを作成します。
name: projects/PROJECT_ID/policies/CONSTRAINT_NAME spec: rules: - enforce: true
次のように置き換えます。
制約を含む組織のポリシーを適用するには、次のコマンドを実行します。
gcloud org-policies set-policy POLICY_PATH
POLICY_PATHは、組織のポリシーの YAML ファイルのパスに置き換えます。ポリシーが有効になるまでに最大 15 分かかります。カスタム制約をテストする
カスタム制約をテストするには、制約によってブロックされたタスクの完了を試みる gcloud コマンドを実行します。
たとえば、制約で GitHub トリガー名に「cloudbuild」が含まれている必要があるとします。この制約をテストするには、次のスニペットに示すように、別のトリガー名で
gcloud builds triggers create githubコマンドを実行します。gcloud builds triggers create github --name=github-trigger \ --repo-owner=some-owner \ --repo-name=some-repo \ --branch-pattern=main \ --build-config=cloudbuild.yaml \ --project=my-project \出力は次のようになります。
Operation denied by custom org policies: ["customConstraints/custom.enableBuildTrigger": "GitHub trigger name must include "cloudbuild"."]Cloud Build でサポートされているリソースとオペレーション
次の Cloud Build カスタム制約フィールドは、Cloud Build リソースを作成または更新する際に使用できます。
トリガーによってトリガーされたビルドには、ビルドの制約は適用されません。
一般的なユースケースのカスタム組織ポリシーの例
次の表では、実際に役に立つ可能性のあるカスタム組織ポリシーの構文を示します。
説明 制約の構文 「cloud-builds」を含むトピックをリッスンする Pub/Sub トリガーの作成のみを許可する name: organizations/ORGANIZATION_ID/customConstraints/custom.enableCloudBuildPubsubTrigger resourceTypes: - cloudbuild.googleapis.com/BuildTrigger methodTypes: - CREATE condition: "resource.pubsubConfig != null && resource.pubsubConfig.topic.contains('cloud-builds')" actionType: ALLOW displayName: Enable creating Pub/Sub trigger that listens on topics that contain "cloud-builds". description: Only allow creating Pub/Sub trigger that listens on topics that contain "cloud-builds".
次のステップ