Mengelola resource Cloud Build dengan batasan kustom

Layanan Kebijakan Organisasi memberi Anda kontrol terpusat dan terprogram atas resource organisasi. Sebagai administrator kebijakan organisasi, Anda dapat menentukan kebijakan organisasi, yang merupakan serangkaian batasan yang disebut batasan yang berlaku untuk resource Cloud Build dan turunan dari resource tersebut dalam hierarki resourceGoogle Cloud . Anda dapat menerapkan kebijakan organisasi di level organisasi, folder, atau project.

Layanan Kebijakan Organisasi menyediakan batasan yang telah ditetapkan untuk berbagai layanan Cloud Build. Namun, jika menginginkan kontrol yang lebih terperinci dan dapat disesuaikan atas kolom tertentu yang dibatasi dalam kebijakan organisasi, Anda juga dapat membuat kebijakan organisasi kustom.

Manfaat

Anda dapat menggunakan kebijakan organisasi kustom untuk mengizinkan atau menolak resource Cloud Build tertentu. Misalnya, jika permintaan untuk membuat atau memperbarui pemicu build gagal memenuhi validasi batasan kustom sebagaimana ditetapkan oleh kebijakan organisasi Anda, permintaan akan gagal dan error akan ditampilkan kepada pemanggil.

Pewarisan kebijakan

Secara default, kebijakan organisasi diwarisi oleh turunan resource tempat Anda menerapkan kebijakan tersebut. Misalnya, jika Anda menerapkan kebijakan pada folder, Cloud Build akan menerapkan kebijakan tersebut pada semua project di folder tersebut. Untuk mempelajari lebih lanjut perilaku ini dan cara mengubahnya, lihat Aturan evaluasi hierarki.

Harga

Layanan Kebijakan Organisasi, termasuk kebijakan organisasi yang telah ditetapkan dan khusus, ditawarkan tanpa biaya.

Sebelum memulai

  1. Menyiapkan project Anda
    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.

      Go to project selector

    3. Make sure that billing is enabled for your Google Cloud project.

    4. Enable the Cloud Build API.

      Enable the API

    5. Install the Google Cloud CLI.

    6. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

    7. To initialize the gcloud CLI, run the following command:

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

      Go to project selector

    9. Make sure that billing is enabled for your Google Cloud project.

    10. Enable the Cloud Build API.

      Enable the API

    11. Install the Google Cloud CLI.

    12. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

    13. To initialize the gcloud CLI, run the following command:

      gcloud init
    14. Pastikan Anda mengetahui ID organisasi Anda.
    15. Peran yang diperlukan

      Untuk mendapatkan izin yang diperlukan untuk mengelola kebijakan organisasi, minta administrator untuk memberi Anda peran IAM Administrator kebijakan organisasi (roles/orgpolicy.policyAdmin) di resource organisasi. Untuk mengetahui informasi selengkapnya tentang cara memberikan peran, lihat Mengelola akses ke project, folder, dan organisasi.

      Anda mungkin juga bisa mendapatkan izin yang diperlukan melalui peran khusus atau peran bawaan lainnya.

      Anda juga perlu menambahkan peran yang diperlukan untuk membuat Cloud Build ke akun pengguna Anda. Lihat Mengonfigurasi akses ke resource Cloud Build. Untuk mempelajari lebih lanjut peran IAM yang terkait dengan Cloud Build, lihat Peran dan izin IAM.

      Membuat batasan khusus

      Anda dapat membuat batasan kustom menggunakan file YAML untuk menentukan resource, metode, kondisi, dan tindakan yang tunduk pada batasan. Kebijakan ini khusus untuk layanan tempat Anda menerapkan kebijakan organisasi. Kondisi untuk batasan kustom Anda harus ditentukan menggunakan Common Expression Language. Lihat halaman GitHub tentang Common Expression Language (CEL). Untuk mengetahui informasi selengkapnya tentang cara membangun kondisi dalam batasan khusus menggunakan CEL, lihat bagian CEL tentang Membuat dan mengelola batasan kustom.

      Gunakan template berikut untuk membuat file YAML untuk batasan kustom:

      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
      

      Ganti kode berikut:

      • ORGANIZATION_ID: ID organisasi Anda, seperti 123456789.

      • CONSTRAINT_NAME: nama yang Anda inginkan untuk batasan kustom baru. Batasan kustom harus dimulai dengan custom., dan hanya boleh menyertakan huruf besar, huruf kecil, atau angka, misalnya, custom.enableBuildTrigger. Panjang maksimum kolom ini adalah 70 karakter, tidak menghitung awalan, misalnya, organizations/123456789/customConstraints/custom.allowConstraint.

      • RESOURCE_NAME: nama (bukan URI) resource Cloud Build yang berisi objek dan kolom yang ingin Anda batasi. Contoh, BuildTrigger.

      • CONDITION: kondisi CEL yang ditulis berdasarkan representasi resource layanan yang didukung. Kolom ini memiliki panjang maksimal 1.000 karakter. Lihat Resource yang didukung untuk mengetahui informasi selengkapnya tentang resource yang tersedia untuk menulis kondisi. Contoh, "resource.github.name.contains('cloudbuild')"

      • ACTION: tindakan yang akan diambil jika condition terpenuhi. Ini dapat berupa ALLOW atau DENY.

      • DISPLAY_NAME: nama yang mudah dibaca manusia untuk batasan. Kolom ini memiliki panjang maksimal 200 karakter.

      • DESCRIPTION: deskripsi batasan yang mudah dipahami untuk ditampilkan sebagai pesan error saat kebijakan dilanggar. Kolom ini memiliki panjang maksimal 2.000 karakter.

      Untuk mengetahui informasi selengkapnya tentang cara membuat batasan kustom, lihat Membuat dan mengelola kebijakan organisasi kustom.

      Menyiapkan batasan kustom

      Setelah membuat file YAML untuk batasan khusus baru, Anda harus menyiapkannya agar tersedia untuk kebijakan organisasi di organisasi Anda. Untuk menyiapkan batasan kustom, gunakan perintah gcloud org-policies set-custom-constraint:
      gcloud org-policies set-custom-constraint CONSTRAINT_PATH
      Ganti CONSTRAINT_PATH dengan jalur lengkap ke file batasan kustom Anda. Contohnya, /home/user/customconstraint.yaml Setelah selesai, batasan khusus Anda tersedia sebagai kebijakan organisasi dalam daftar kebijakan organisasi. Google Cloud Untuk memverifikasi bahwa ada batasan kustom, gunakan perintah gcloud org-policies list-custom-constraints:
      gcloud org-policies list-custom-constraints --organization=ORGANIZATION_ID
      Ganti ORGANIZATION_ID dengan ID resource organisasi Anda. Untuk mengetahui informasi selengkapnya, lihat Melihat kebijakan organisasi.

      Menerapkan kebijakan organisasi kustom

      Anda dapat menerapkan batasan dengan membuat kebijakan organisasi yang mereferensikannya, lalu menerapkan kebijakan organisasi tersebut ke resource Google Cloud .

      Konsol

      1. Di konsol Google Cloud , buka halaman Kebijakan organisasi.

        Buka Organization policies

      2. Dari pemilih project, pilih project yang ingin Anda tetapkan kebijakan organisasinya.
      3. Dari daftar di halaman Kebijakan organisasi, pilih batasan Anda untuk melihat halaman Detail kebijakan untuk batasan tersebut.
      4. Untuk mengonfigurasi kebijakan organisasi untuk resource ini, klik Manage policy.
      5. Di halaman Edit kebijakan, pilih Ganti kebijakan induk.
      6. Klik Add a rule.
      7. Di bagian Penerapan, pilih apakah penerapan kebijakan organisasi ini diaktifkan atau dinonaktifkan.
      8. Opsional: Untuk membuat kebijakan organisasi bersyarat pada tag, klik Tambahkan kondisi. Perhatikan bahwa jika menambahkan aturan kondisional ke kebijakan organisasi, Anda harus menambahkan setidaknya satu aturan tanpa syarat atau kebijakan tidak dapat disimpan. Untuk mengetahui informasi selengkapnya, lihat Menetapkan kebijakan organisasi dengan tag.
      9. Klik Uji perubahan untuk menyimulasikan efek kebijakan organisasi. Simulasi kebijakan tidak tersedia untuk batasan terkelola lama. Untuk mengetahui informasi selengkapnya, lihat Menguji perubahan kebijakan organisasi dengan Policy Simulator.
      10. Untuk menyelesaikan dan menerapkan kebijakan organisasi, klik Set policy. Kebijakan ini memerlukan waktu hingga 15 menit untuk diterapkan.

      gcloud

      Untuk membuat kebijakan organisasi dengan aturan boolean, buat file YAML kebijakan yang merujuk batasan:

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

      Ganti kode berikut:

      • PROJECT_ID: project tempat Anda ingin menerapkan batasan.
      • CONSTRAINT_NAME: nama yang Anda tentukan untuk batasan kustom. Contoh, custom.enableBuildTrigger

      Untuk menerapkan kebijakan organisasi yang berisi batasan, jalankan perintah berikut:

          gcloud org-policies set-policy POLICY_PATH
          

      Ganti POLICY_PATH dengan jalur lengkap ke file YAML kebijakan organisasi Anda. Kebijakan ini memerlukan waktu hingga 15 menit untuk diterapkan.

      Menguji batasan kustom

      Untuk menguji batasan kustom, jalankan perintah gcloud yang mencoba menyelesaikan tugas yang diblokir oleh batasan Anda.

      Misalnya, asumsikan bahwa batasan mengharuskan nama pemicu GitHub berisi "cloudbuild". Anda dapat menguji batasan ini dengan menjalankan perintah gcloud builds triggers create github dengan nama pemicu lain seperti yang ditunjukkan dalam cuplikan berikut:

      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 \
      

      Outputnya mirip dengan hal berikut ini:

      Operation denied by custom org policies: ["customConstraints/custom.enableBuildTrigger": "GitHub trigger name must include "cloudbuild"."]
      

      Resource dan operasi yang didukung Cloud Build

      Kolom batasan kustom Cloud Build berikut tersedia untuk digunakan saat Anda membuat atau memperbarui resource Cloud Build.

      Perhatikan bahwa batasan pada Build tidak diterapkan pada Build yang dipicu oleh pemicu.

      • Konfigurasi Build Cloud Build
        • resource.source.storageSource.bucket
        • resource.source.storageSource.object
        • resource.source.storageSource.generation
        • resource.source.storageSource.sourceFetcher
        • resource.source.repoSource.projectId
        • resource.source.repoSource.repoName
        • resource.source.repoSource.branchName
        • resource.source.repoSource.tagName
        • resource.source.repoSource.commitSha
        • resource.source.repoSource.dir
        • resource.source.repoSource.invertRegex
        • resource.source.repoSource.substitutions
        • resource.source.gitSource.url
        • resource.source.gitSource.dir
        • resource.source.gitSource.revision
        • resource.source.storageSourceManifest.bucket
        • resource.source.storageSourceManifest.object
        • resource.source.storageSourceManifest.generation
        • resource.source.connectedRepository.repository
        • resource.source.connectedRepository.dir
        • resource.source.connectedRepository.revision
        • resource.source.developerConnectConfig.gitRepositoryLink
        • resource.source.developerConnectConfig.dir
        • resource.source.developerConnectConfig.revision
        • resource.steps.name
        • resource.steps.env
        • resource.steps.args
        • resource.steps.dir
        • resource.steps.id
        • resource.steps.waitFor
        • resource.steps.entryPoints
        • resource.steps.secretEnv
        • resource.steps.volumes.name
        • resource.steps.volumes.path
        • resource.steps.timeout
        • resource.steps.allowFailure
        • resource.steps.allowExitCodes
        • resource.steps.script
        • resource.steps.automapSubstitutions
        • resource.timeout
        • resource.images
        • resource.queueTtl
        • resource.artifacts.images
        • resource.artifacts.objects.location
        • resource.artifacts.objects.paths
        • resource.logsBucket
        • resource.options.sourceProvenanceHash
        • resource.options.requestedVerifyOption
        • resource.options.machineType
        • resource.options.diskSizeGb
        • resource.options.substitutionOption
        • resource.options.dynamicSubstitutions
        • resource.options.autoMapSubstitutions
        • resource.options.logStreamingOption
        • resource.options.pool.name
        • resource.options.logging
        • resource.options.env
        • resource.options.secretEnv
        • resource.options.volumes.name
        • resource.options.volumes.path
        • resource.options.defaultLogsBucketBehavior
        • resource.substitutions
        • resource.tags
        • resource.secrets.kmsKeyName
        • resource.secrets.secretEnv
        • resource.serviceAccount
        • resource.availableSecrets.secretManager.versionName
        • resource.availableSecrets.secretManager.env
        • resource.availableSecrets.inline.kmsKeyName
        • resource.availableSecrets.inline.envMap
        • resource.gitConfig.proxySecretVersionName
        • resource.gitConfig.proxySslCaInfo
        • resource.gitConfig.http
      • Cloud Build BitbucketServerConfig
        • resource.name
        • resource.hostUrl
        • resource.secrets.adminAccessTokenVersionName
        • resource.secrets.readAccessTokenVersionName
        • resource.secrets.webhookSecretVersionName
        • resource.username
        • resource.apiKey
        • resource.peeredNetwork
        • resource.sslCa
        • resource.peeredNetworkIpRange
      • Konfigurasi WorkerPool Cloud Build
        • resource.displayName
        • resource.annotations
        • resource.privatePoolV1Config.workerConfig.machineType
        • resource.privatePoolV1Config.workerConfig.diskSizeGb
        • resource.privatePoolV1Config.networkConfig.peeredNetwork
        • resource.privatePoolV1Config.networkConfig.egressOption
        • resource.privatePoolV1Config.networkConfig.peeredNetworkIpRange
      • Konfigurasi BuildTrigger Cloud Build
        • resource.tags
        • resource.resourceName
        • resource.description
        • resource.name
        • resource.tags
        • resource.triggerTemplate.projectId
        • resource.triggerTemplate.repoName
        • resource.triggerTemplate.branchName
        • resource.triggerTemplate.tagName
        • resource.triggerTemplate.commitSha
        • resource.triggerTemplate.dir
        • resource.triggerTemplate.invertRegex
        • resource.triggerTemplate.substitutions
        • resource.github.owner
        • resource.github.name
        • resource.github.enterpriseConfigResourceName
        • resource.pubsubConfig.topic
        • resource.pubsubConfig.serviceAccountEmail
        • resource.webhookConfig.secret
        • resource.bitbucketServerTriggerConfig.repoSlug
        • resource.bitbucketServerTriggerConfig.projectKey
        • resource.bitbucketServerTriggerConfig.pullRequest.branch
        • resource.bitbucketServerTriggerConfig.pullRequest.commentControl
        • resource.bitbucketServerTriggerConfig.pullRequest.invertRegex
        • resource.bitbucketServerTriggerConfig.push.branch
        • resource.bitbucketServerTriggerConfig.push.tag
        • resource.bitbucketServerTriggerConfig.push.invertRegex
        • resource.gitlabEnterpriseEventsConfig.projectNamespace
        • resource.gitlabEnterpriseEventsConfig.pullRequest.branch
        • resource.gitlabEnterpriseEventsConfig.pullRequest.commentControl
        • resource.gitlabEnterpriseEventsConfig.pullRequest.invertRegex
        • resource.gitlabEnterpriseEventsConfig.push.branch
        • resource.gitlabEnterpriseEventsConfig.push.tag
        • resource.gitlabEnterpriseEventsConfig.push.invertRegex
        • resource.gitlabEnterpriseEventsConfig.gitlabConfigResource
        • resource.disabled
        • resource.substitutions
        • resource.ignoredFiles
        • resource.includedFiles
        • resource.sourceToBuild.uri
        • resource.sourceToBuild.repository
        • resource.sourceToBuild.ref
        • resource.sourceToBuild.repoType
        • resource.sourceToBuild.githubEnterpriseConfig
        • resource.approvalConfig.approvalRequired
        • resource.filter
        • resource.serviceAccount
        • resource.eventType
        • resource.includeBuildLogs
        • resource.repositoryEventConfig.repository
        • resource.repositoryEventConfig.pullRequest.branch
        • resource.repositoryEventConfig.pullRequest.commentControl
        • resource.repositoryEventConfig.pullRequest.invertRegex
        • resource.repositoryEventConfig.push.branch
        • resource.repositoryEventConfig.push.tag
        • resource.repositoryEventConfig.push.invertRegex
      • Konfigurasi GitHubEnterpriseConfig Cloud Build
        • resource.name
        • resource.hostUrl
        • resource.appId
        • resource.name
        • resource.webhookKey
        • resource.peeredNetwork
        • resource.secrets.privateKeyVersionName
        • resource.secrets.webhookSecretVersionName
        • resource.secrets.oauthSecretVersionName
        • resource.secrets.oauthClientIdVersionName
        • resource.displayName
        • resource.sslCa
      • Konfigurasi Koneksi Cloud Build
        • resource.name
        • resource.githubConfig.authorizerCredential.oauthTokenSecretVersionName
        • resource.githubConfig.appInstallationId
        • resource.githubEnterpriseConfig.hostUrl
        • resource.githubEnterpriseConfig.apiKey
        • resource.githubEnterpriseConfig.appId
        • resource.githubEnterpriseConfig.appSlug
        • resource.githubEnterpriseConfig.privateKeySecretVersion
        • resource.githubEnterpriseConfig.webhookSecretSecretVersion
        • resource.githubEnterpriseConfig.oauthSecretSecretVersion
        • resource.githubEnterpriseConfig.oauthClientIdSecretVersion
        • resource.githubEnterpriseConfig.authorizerCredential.oauthTokenSecretVersionName
        • resource.githubEnterpriseConfig.appInstallationId
        • resource.githubEnterpriseConfig.serviceDirectoryConfig.service
        • resource.githubEnterpriseConfig.sslCa
        • resource.gitlabConfig.hostUri
        • resource.gitlabConfig.webhookSecretSecretVersion
        • resource.gitlabConfig.readAuthorizerCredential.oauthTokenSecretVersionName
        • resource.gitlabConfig.authorizerCredential.oauthTokenSecretVersionName
        • resource.gitlabConfig.serviceDirectoryConfig.service
        • resource.gitlabConfig.sslCa
        • resource.gitlabConfig.serviceDirectoryConfig.service
        • resource.bitbucketDataCenterConfig.hostUri
        • resource.bitbucketDataCenterConfig.webhookSecretSecretVersion
        • resource.bitbucketDataCenterConfig.readAuthorizerCredential.oauthTokenSecretVersionName
        • resource.bitbucketDataCenterConfig.authorizerCredential.oauthTokenSecretVersionName
        • resource.bitbucketDataCenterConfig.sslCa
        • resource.bitbucketDataCenterConfig.serviceDirectoryConfig.service
        • resource.bitbucketCloudConfig.workspace
        • resource.bitbucketCloudConfig.webhookSecretSecretVersion
        • resource.bitbucketCloudConfig.readAuthorizerCredential.oauthTokenSecretVersionName
        • resource.bitbucketCloudConfig.authorizerCredential.oauthTokenSecretVersionName
      • Konfigurasi Repositori Cloud Build
        • resource.name
        • resource.remoteUri
        • resource.annotations

      Contoh kebijakan organisasi kustom untuk kasus penggunaan umum

      Tabel berikut memberikan sintaksis beberapa kebijakan organisasi kustom yang mungkin berguna bagi Anda:

      Deskripsi Sintaksis batasan
      Hanya mengizinkan pembuatan pemicu Pub/Sub yang memproses topik yang berisi "cloud-builds"
          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".
      • ORGANIZATION_ID: ID organisasi Anda, seperti 123456789.

      Langkah berikutnya