無料でビルドを開始
CircleCI.comアカデミーブログコミュニティサポート

設定ファイルのポリシーの作成と管理

8 months ago1 min read
クラウド
このページの内容

このページでは、設定ファイルのポリシーに関するテストを作成、セットアップする方法をガイド形式で説明します。

組織のポリシー評価を有効・無効にする

組織のパイプライン設定にポリシーの評価を適用するかどうかは、--enabled フラグを使って制御できます。

  • ポリシーの評価を有効化するには、以下のコマンドを実行します。 これにより、--enabledtrue になります。パイプラインのトリガー時に、組織のポリシーに対してプロジェクトの設定ファイルが評価されます

    circleci policy settings --enabled=true --owner-id <your-organization-ID>

    以下のように出力されます。

    {
      "enabled": true
    }
  • ポリシーの評価を無効化するには、以下のコマンドを実行します。 これにより、--enabledfalse になります。パイプラインのトリガー時には、組織のポリシーに対するプロジェクトの設定ファイルの評価は行われません

    circleci policy settings --enabled=false --owner-id <your-organization-ID>

    以下のように出力されます。

    {
      "enabled": false
    }

組織のポリシー評価を有効・無効にする

以下の手順に従って、CircleCI 設定ファイルの version2.1 以上であることを確認するポリシーを作成します。

  1. 組織の設定ファイルのポリシー管理機能を有効にします。

    circleci policy settings --enabled=true --owner-id <your-organization-ID>

    以下のように出力されます。

    {
      "enabled": true
    }
  2. ポリシーの保存用に空のディレクトリを作成します。 たとえば以下のようになります。

    mkdir ./config-policies
  3. 新しいディレクトリ内に、新しいポリシー用の Rego ファイルを作成します。 今回は version.rego という名前にします。

  4. 以下の内容を version.rego に追加します。

    # All policies start with the org package definition
    package org
    
    policy_name["example"]
    
    # signal to circleci that check_version is enabled and must be included when making a decision
    # Also, signal to circleci that check_version is a hard_failure condition and that builds should be
    # stopped if this rule is not satisfied.
    enable_hard["check_version"]
    
    # define check version
    check_version = reason {
        not input.version # check the case where version is not in the input
        reason := "version must be defined"
    } {
        not is_number(input.version) # check that version is number
        reason := "version must be a number"
    } {
        not input.version >= 2.1 # check that version is at least 2.1
        reason := sprintf("version must be at least 2.1 but got %v", [input.version])
    }
  5. 組織にポリシーをアップロードします。

    circleci policy push ./config-policies --owner-id <your-organization-ID>

    これで、組織内でパイプラインがトリガーされる際、このポリシーに対してプロジェクトの .circleci/config.yml が検証されるようになりました。

ポリシーの更新

既存のポリシーに変更を加える方法を説明するために、上記のポリシーの作成中に間違ってしまったとします。 たとえば、組織の一部のプロジェクト設定ファイルで CircieCI のバージョン 2.0 が使用されていたので、これをポリシーに反映してみましょう。

  1. version.rego ファイルのルール定義のうち、最後のチェックを以下のように変更します。

    {
        not input.version >= 2.0 # check that version is at least 2.0
        reason := sprintf("version must be at least 2.0 but got %v", [input.version])
    }
  2. この更新されたポリシーファイルを含むポリシーディレクトリを CLI を使ってプッシュします (差分を検証し、プロンプトが表示されたら yes を選択します)。

    circleci policy push ./config-policies --owner-id <your-organization-ID>

ポリシーを作成する

CircleCI のポリシーは、ポリシーのディレクトリを CLI を介して CircleCI にプッシュして管理します。 推奨されるポリシーディレクトリの管理方法は、組織の VCS のレポジトリにポリシーディレクトリを保存することです。 これは、CircleCI の内部でポリシーを管理する方法です。 ポリシーバンドルのプッシュは、CircleCI パイプラインをトリガーすることで実行できます。

以下の手順に従って、CircleCI 設定ファイルの version2.1 以上であることを確認するポリシーを作成します。 最大限のセキュリティを確保するには、トークンを環境変数としてコンテキスト内に保存し、そのコンテキストをポリシーの管理を担当するグループに制限する必要があります 詳細については、 コンテキストの使用のページを参照してください。

設定ファイルポリシーCI/CDパイプラインの設定

  1. VCS にポリシーを管理するためのレポジトリをセットアップします。

  2. 新しいリポジトリに Rego ポリシーファイル用のディレクトリを作成します。

    mkdir ./config-policies
  3. 新しいポリシーのリポジトリ用の .circleci/config.yml ファイルを作成し、以下の設定サンプルをコピー & ペーストします。 このサンプルでは、main ブランチのコミット時に CircleCI にポリシーをプッシュし、他のすべてのブランチへのコミット時のポリシーバンドルにおける差分を表示します。

    version: 2.1
    
    orbs:
      circleci-cli: circleci/circleci-cli@0.1.9 # Use orb to make the `circleci-cli/default` executor available for running jobs
    
    workflows:
      main-workflow:
        jobs:
          - diff-policy-bundle:
              context: <my-context>
              filters:
                branches:
                  ignore: main # on all branches other than main
          - push-policy-bundle:
              context: <my-context>
              filters:
                branches:
                  only: main # only on the main branch
    
    jobs:
      diff-policy-bundle:
        executor: circleci-cli/default
        resource_class: small
        steps:
          - checkout
          - run:
              name: Diff policy bundle
              command: circleci policy diff ./config --owner-id $ORG_ID # show a diff of the policy bundle
    
      push-policy-bundle:
        executor: circleci-cli/default
        resource_class: small
        steps:
          - checkout
          - run:
              name: Push policy bundle
              command: circleci policy push ./config --no-prompt --owner-id $ORG_ID # push the policy bundle to CircleCI

Suggest an edit to this page

Make a contribution
Learn how to contribute