チュートリアル: 高度なコネクタ ポリシーをプログラムで管理する

高度なコネクタ ポリシー (ACP) は、既定でコネクタをブロックする厳密な許可リストを使用してコネクタの使用を制御します。 Power Platform 管理センターのエクスペリエンスに加えて、Power Platform API と管理 (管理者) SDK を使用して、コードを使用して ACP を管理できます。 ACP の自動化は、多くの環境グループ間でガバナンスを標準化したり、グループ間でベースライン ポリシーをレプリケートしたり、デプロイ パイプラインの一部としてポリシーを管理したりする場合に便利です。

このチュートリアルで学習する内容は次のとおりです。

  1. Power Platform APIを使用して認証します
  2. ACP ポリシーの形状を理解します
  3. ポリシーを作成し、環境グループに追加します。
  4. 個々のコネクタ アクションを有効にします
  5. 1 つの環境でポリシーを適用または更新します。
  6. ある環境グループから別の環境グループにポリシーをコピーします
  7. 環境グループから ACP を削除します。

高度なコネクタ ポリシーは、Power Platform API の governance/ruleBasedPolicies 操作によって公開されます。 ポリシーに 1 つ以上のルール セットが含まれています。ACP コネクタの許可リストを保持ConnectorManagement ID を持つ規則セット。 この記事のすべての例では、API バージョンの 2024-10-01を使用します。

前提条件

ステップ 1. Power Platform API を使用して認証する

すべての例は、「認証」のガイダンスに従って、アプリ登録の クライアント ID認証します。 次の例では、現在のユーザーとして対話形式でサインインします。 サービス プリンシパルとして無人で実行するには、 認証 に関する記事の機密クライアント フローを参照し、サービス プリンシパルに RBAC ロールを割り当てます。

# Requires the MSAL.PS module: Install-Module MSAL.PS -Scope CurrentUser
Import-Module "MSAL.PS"

$clientId  = "<application (client) ID of your app registration>"
$apiBaseUrl = "https://api.powerplatform.com"
$apiVersion = "2024-10-01"

# Sign in interactively and request a token for the Power Platform API
$auth = Get-MsalToken -ClientId $clientId -Scope "https://api.powerplatform.com/.default" -Interactive
$headers = @{ Authorization = "Bearer $($auth.AccessToken)" }

ステップ 2. ACP ポリシーの形状を理解する

高度なコネクタ ポリシーは、ID ConnectorManagementを持つルール セットを含むルール ベースのポリシーです。 この規則セットには version が含まれており、その inputsAllowedConnectorListを保持します。各エントリではコネクタが許可され、アクションと接続の種類の管理方法が設定されます。

{
  "name": "Contoso ACP baseline",
  "ruleSets": [
    {
      "id": "ConnectorManagement",
      "version": "1.0",
      "inputs": {
        "AllowedConnectorList": [
          {
            "AllowedConnector": "/providers/Microsoft.PowerApps/apis/shared_office365",
            "AllowedActionsMode": "AllAllowed",
            "AllowedConnectionTypesMode": "AllAllowed"
          },
          {
            "AllowedConnector": "/providers/Microsoft.PowerApps/apis/shared_commondataserviceforapps",
            "AllowedActionsMode": "SomeAllowed",
            "AllowedActions": ["GetItem", "CreateRecord"],
            "AllowedConnectionTypesMode": "AllAllowed"
          }
        ]
      }
    }
  ]
}

次のセマンティクスに留意してください。

  • AllowedConnectorListに含まれていないコネクタはブロックされます (既定の拒否)。
  • 各エントリ は AllowedActionsModeを設定します。 AllAllowed は、コネクタのすべてのアクションを許可します。 SomeAllowed は、エントリの AllowedActions 配列にリストされているアクションにコネクタを制限します。 手順 4 では、アクションを追加してこのモードを設定する方法を示します。
  • AllowedConnectionTypesMode は、許可される接続の種類を制御し、同じ AllAllowed パターンに従います。
  • ポリシーを作成または更新するときに、ルール セットの version を含めます。 既存のポリシーからそれを読み取り、サービスが返す値を保持します。

ヒント

AllowedConnectorの正確な値は、コネクタのリソース識別子です。 テナントに既に存在するコネクタの形状を学習する最も信頼性の高い方法は、まず既存のポリシーを読み取るか (手順 4 では方法を示します)、コネクタ カタログ (次に説明) を使用してから、ポリシーを作成または更新するときにその図形をミラー化することです。

コネクタ カタログを使用してコネクタ ID とアクション ID を検索する

許可できるコネクタとアクションを検出するには、 コネクタ カタログ API を使用します。 環境で使用可能なコネクタと、 AllowedConnectorAllowedActionsに配置する識別子が一覧表示されます。

コネクタ カタログの操作には、パス内の環境 IDと、$filter環境 (など) を指定する OData $filter=environment eq '<environmentId>'が必要です。 どちらも必須です。

$environmentId = "<environment ID>"
$filter = [uri]::EscapeDataString("environment eq '$environmentId'")

# List connectors available in the environment
$connectors = Invoke-RestMethod -Method Get `
    -Uri "$apiBaseUrl/connectivity/environments/$environmentId/connectors?`$filter=$filter&api-version=$apiVersion" `
    -Headers $headers
$connectors.value | Select-Object name, @{ n = "displayName"; e = { $_.properties.displayName } }

# Get a single connector by ID (the connector's name, such as shared_office365)
$connectorId = "shared_office365"
$connector = Invoke-RestMethod -Method Get `
    -Uri "$apiBaseUrl/connectivity/environments/$environmentId/connectors/$connectorId?`$filter=$filter&api-version=$apiVersion" `
    -Headers $headers
$connector.id   # full resource path to use as AllowedConnector

コネクタの id ( /providers/Microsoft.PowerApps/apis/shared_office365 などの完全なリソース パス) を AllowedConnector 値として使用し、コネクタの操作 ID を AllowedActionsの値として使用します。 管理 SDK の connectivity 名前空間を使用して、同じカタログにアクセスできます。

手順 3. ポリシーを作成して環境グループに追加する

環境グループへの ACP の追加は、ポリシーを作成してからグループに割り当てるという 2 部構成の操作です。 create 呼び出しは、割り当て呼び出しで使用する新しいポリシー idを返します。

グループ全体にポリシーを割り当てるには、空の本文 ({}) で割り当て要求を送信します。 グループ内のすべての環境がポリシーを継承し、ポリシーと同期された状態を維持します。

$environmentGroupId = "<environment group ID>"

# 1. Create the policy with a ConnectorManagement rule set
$policyBody = @{
    name     = "Contoso ACP baseline"
    ruleSets = @(
        @{
            id      = "ConnectorManagement"
            version = "1.0"
            inputs  = @{
                AllowedConnectorList = @(
                    @{
                        AllowedConnector           = "/providers/Microsoft.PowerApps/apis/shared_office365"
                        AllowedActionsMode         = "AllAllowed"
                        AllowedConnectionTypesMode = "AllAllowed"
                    }
                )
            }
        }
    )
} | ConvertTo-Json -Depth 10

$policy = Invoke-RestMethod -Method Post `
    -Uri "$apiBaseUrl/governance/ruleBasedPolicies?api-version=$apiVersion" `
    -Headers $headers -ContentType "application/json" -Body $policyBody
Write-Host "Created policy $($policy.id)"

# 2. Assign the policy to the environment group (empty body = whole group)
Invoke-RestMethod -Method Post `
    -Uri "$apiBaseUrl/governance/ruleBasedPolicies/$($policy.id)/environmentGroups/$environmentGroupId/assignments?api-version=$apiVersion" `
    -Headers $headers -ContentType "application/json" -Body "{}"
Write-Host "Assigned policy $($policy.id) to group $environmentGroupId"

ステップ 4. 個々のコネクタ アクションを有効にする

コネクタに対して特定のアクションのみを許可するには、その AllowedActionsModeSomeAllowed に設定し、 AllowedActionsで許可されているアクションを一覧表示します。 次の使用例は、管理センターで選択できない非表示のアクションなどのアクションをコネクタの許可リストに追加し、コネクタを SomeAllowedに設定します。 ポリシーを読み取り、コネクタ エントリを更新し、更新された規則セットを パッチを使用して返送します。 パッチを適用すると、ID によってルール セットが更新され、ポリシーの他のルール セットはそのまま残されます。

$policyId     = "<policy ID>"
$connectorId  = "shared_commondataserviceforapps"   # last segment of AllowedConnector
$actionToAdd  = "aibuilderpredict_customprompt"

# 1. Read the current policy
$policy = Invoke-RestMethod -Method Get `
    -Uri "$apiBaseUrl/governance/ruleBasedPolicies/$policyId`?api-version=$apiVersion" `
    -Headers $headers

# 2. Find the ConnectorManagement rule set and the connector entry
$ruleSet = $policy.ruleSets | Where-Object { $_.id -eq "ConnectorManagement" }
$entry = $ruleSet.inputs.AllowedConnectorList |
    Where-Object { ($_.AllowedConnector -split "/")[-1] -eq $connectorId }

# 3. Restrict the connector to specific actions: add the action and set SomeAllowed
if ($entry) {
    $actions = @()
    if ($entry.PSObject.Properties.Name -contains "AllowedActions") { $actions = @($entry.AllowedActions) }
    if ($actions -notcontains $actionToAdd) { $actions += $actionToAdd }
    $entry | Add-Member -NotePropertyName AllowedActions -NotePropertyValue $actions -Force
    $entry.AllowedActionsMode = "SomeAllowed"

    # 4. Patch only the modified rule set back to the policy
    $patchBody = @{ name = $policy.name; ruleSets = @($ruleSet) } | ConvertTo-Json -Depth 10
    Invoke-RestMethod -Method Patch `
        -Uri "$apiBaseUrl/governance/ruleBasedPolicies/$policyId`?api-version=$apiVersion" `
        -Headers $headers -ContentType "application/json" -Body $patchBody
    Write-Host "Set '$connectorId' to SomeAllowed with '$actionToAdd' in policy $policyId"
}

ステップ 5: 1 つの環境でポリシーを適用または更新する

ポリシーは、環境グループではなく単一の環境でターゲットにすることができます。 このアプローチは、リスクの高い環境、パイロット環境、または規制された環境に役立ちます。 ポリシーを環境に割り当て、手順 4 の同じパッチ パターンを使用して後で変更します。 各環境では、1 つの有効な ACP ポリシーがサポートされます。

$policyId       = "<policy ID>"
$environmentId  = "<environment ID>"

# Assign the policy directly to the environment
Invoke-RestMethod -Method Post `
    -Uri "$apiBaseUrl/governance/ruleBasedPolicies/$policyId/environments/$environmentId/assignments?api-version=$apiVersion" `
    -Headers $headers -ContentType "application/json" -Body "{}"
Write-Host "Assigned policy $policyId to environment $environmentId"

ステップ 6. ある環境グループから別の環境グループにポリシーをコピーする

ガバナンス ベースラインを別のグループにレプリケートする場合は、 CopyAllRules フラグを使用してコピーする量を選択します。

  • CopyAllRules = true: ソース グループのすべての ルール セットから新しいポリシーを作成し、ターゲット グループに割り当てます。 ターゲット グループのガバナンスは、ソースの独立したコピーになります。
  • CopyAllRules = false: ソース ポリシーから ConnectorManagement ルール セットのみを抽出し、ターゲット グループの既存のポリシーにマージします。 パッチ操作では、ID によって設定された規則が追加または更新されるため、ターゲット グループはその他の規則を保持します。
$sourceGroupId = "<source environment group ID>"
$targetGroupId = "<target environment group ID>"
$CopyAllRules  = $true

# 1. Find and read the policy assigned to the source group
$sourceAssignments = Invoke-RestMethod -Method Get `
    -Uri "$apiBaseUrl/governance/ruleBasedPolicies/environmentGroups/$sourceGroupId/assignments?api-version=$apiVersion" `
    -Headers $headers
$sourcePolicyId = $sourceAssignments.value[0].policyId
$source = Invoke-RestMethod -Method Get `
    -Uri "$apiBaseUrl/governance/ruleBasedPolicies/$sourcePolicyId`?api-version=$apiVersion" `
    -Headers $headers

if ($CopyAllRules) {
    # 2a. Copy ALL rule sets into a new policy and assign it to the target group
    $copyBody = @{ name = "$($source.name) (copy)"; ruleSets = $source.ruleSets } | ConvertTo-Json -Depth 20
    $copy = Invoke-RestMethod -Method Post `
        -Uri "$apiBaseUrl/governance/ruleBasedPolicies?api-version=$apiVersion" `
        -Headers $headers -ContentType "application/json" -Body $copyBody
    Invoke-RestMethod -Method Post `
        -Uri "$apiBaseUrl/governance/ruleBasedPolicies/$($copy.id)/environmentGroups/$targetGroupId/assignments?api-version=$apiVersion" `
        -Headers $headers -ContentType "application/json" -Body "{}"
    Write-Host "Copied all rules to policy $($copy.id) and assigned it to group $targetGroupId"
}
else {
    # 2b. Merge ONLY the ConnectorManagement rule into the target group's existing policy
    $sourceCm = $source.ruleSets | Where-Object { $_.id -eq "ConnectorManagement" }

    $targetAssignments = Invoke-RestMethod -Method Get `
        -Uri "$apiBaseUrl/governance/ruleBasedPolicies/environmentGroups/$targetGroupId/assignments?api-version=$apiVersion" `
        -Headers $headers
    $targetPolicyId = $targetAssignments.value[0].policyId
    $targetPolicy = Invoke-RestMethod -Method Get `
        -Uri "$apiBaseUrl/governance/ruleBasedPolicies/$targetPolicyId`?api-version=$apiVersion" `
        -Headers $headers

    # Patch adds or updates the ConnectorManagement rule set by ID, keeping the target's other rules
    $patchBody = @{ name = $targetPolicy.name; ruleSets = @($sourceCm) } | ConvertTo-Json -Depth 20
    Invoke-RestMethod -Method Patch `
        -Uri "$apiBaseUrl/governance/ruleBasedPolicies/$targetPolicyId`?api-version=$apiVersion" `
        -Headers $headers -ContentType "application/json" -Body $patchBody
    Write-Host "Merged the ConnectorManagement rule into target policy $targetPolicyId"
}

ステップ 7. 環境グループから ACP を削除する

グループにはアクティブな ACP ルールが設定されていますが、グループ内のすべての環境がグループのポリシーと一致します。 強制を削除する方法は、これらの環境で現在の構成を維持するか、ACP を完全にクリアするかによって異なります。

  • グループのポリシーからルールを削除 して、グループが ACP を管理できないようにします。 removeRule操作を使用して、グループのポリシーからConnectorManagementルール セットを削除します。 環境は最後に適用された ACP 構成を保持しますが、グループとの同期は維持されなくなりました。 各環境を個別に管理し、それらを分岐させることができます。
  • グループとすべての環境から ACP を削除 して、ACP をどこでもオフにします。 グループのポリシーからルールを削除し、グループの環境をループ処理し、各環境のポリシーから ConnectorManagement ルール セットも削除します。

グループのポリシーからルールを削除しても、ACP を継承した環境から自動的にクリアされることはありません。 これらの環境では、適用ギャップを回避するために、最後に適用された構成が保持されます。 ループの例に示すように、ACP をすべての場所でクリアするには、各環境から ACP を削除します。 詳細については、「 高度なコネクタ ポリシー」を参照してください。

グループのポリシーからルールを削除する

次の例では、ConnectorManagement操作を使用して、ポリシーからremoveRuleルール セットを削除します。

$policyId = "<policy ID>"

# Read the policy, then send the rule set to remove
$policy = Invoke-RestMethod -Method Get `
    -Uri "$apiBaseUrl/governance/ruleBasedPolicies/$policyId`?api-version=$apiVersion" `
    -Headers $headers
$ruleSet = $policy.ruleSets | Where-Object { $_.id -eq "ConnectorManagement" }

$body = @{ name = $policy.name; ruleSets = @($ruleSet) } | ConvertTo-Json -Depth 10
Invoke-RestMethod -Method Patch `
    -Uri "$apiBaseUrl/governance/ruleBasedPolicies/$policyId/removeRule?api-version=$apiVersion" `
    -Headers $headers -ContentType "application/json" -Body $body
Write-Host "Removed the ConnectorManagement rule set from policy $policyId"

グループ内のすべての環境から ACP を削除する

グループ内のすべての環境で ACP をオフにするには、まずグループのポリシー (前の例) からルールを削除してから、各環境の独自のポリシーに対して削除を繰り返します。 各環境の割り当てられたポリシーを環境の割り当てから読み取り、そのポリシーの removeRule を呼び出します。 グループに属する環境 ID を指定するか、 環境管理 API を使用してそれらを列挙します。

# Environment IDs that belong to the group
$environmentIds = @("<environment ID 1>", "<environment ID 2>")

foreach ($environmentId in $environmentIds) {
    # Find the policy currently assigned to the environment
    $envAssignments = Invoke-RestMethod -Method Get `
        -Uri "$apiBaseUrl/governance/ruleBasedPolicies/environments/$environmentId/assignments?api-version=$apiVersion" `
        -Headers $headers
    if (-not $envAssignments.value) { continue }
    $envPolicyId = $envAssignments.value[0].policyId

    # Remove the ConnectorManagement rule set from that environment's policy
    $envPolicy = Invoke-RestMethod -Method Get `
        -Uri "$apiBaseUrl/governance/ruleBasedPolicies/$envPolicyId`?api-version=$apiVersion" `
        -Headers $headers
    $ruleSet = $envPolicy.ruleSets | Where-Object { $_.id -eq "ConnectorManagement" }
    if ($ruleSet) {
        $body = @{ name = $envPolicy.name; ruleSets = @($ruleSet) } | ConvertTo-Json -Depth 10
        Invoke-RestMethod -Method Patch `
            -Uri "$apiBaseUrl/governance/ruleBasedPolicies/$envPolicyId/removeRule?api-version=$apiVersion" `
            -Headers $headers -ContentType "application/json" -Body $body
        Write-Host "Removed ACP from environment $environmentId"
    }
}

環境ごとの同じremoveRule呼び出しは、前に示した C# SDK と Python SDK で動作します。 呼び出しを、グループの環境IDを反復処理するループで囲みます。

高度なコネクタ ポリシー
ルール ベースのポリシー - REST API リファレンス
認証
チュートリアル: サービス プリンシパルにロールを割り当てる
プログラミングと拡張性の概要