網路政策的程式碼範例

下列要求會說明如何使用 Policy API 管理政策,並以網路政策為例。在開始之前,請務必詳閱 Chrome Policy API 總覽政策結構定義指南

下方顯示的所有要求都使用下列變數:

  • $TOKEN:OAuth 2 權杖
  • $CUSTOMER - 客戶 ID 或常值my_customer
  • $ORG_UNIT:目標機構單位的 ID
  • $NETWORK_ID - 要互動的物件專屬 ID

政策網路服務

Policy Networks 服務是一項 API,可協助 Chrome Policy API 管理網路設定。

API 由四個端點組成:

定義網路

定義網路端點會用來建立新的網路。這個端點用於 Wi-Fi、乙太網路和 VPN 網路。

在這個範例中,我們定義了一個簡單的 Wi-Fi 網路。如要定義較複雜的網路,請檢查 chrome.networks.wifi 命名空間提供哪些欄位。

所有類型的網路都必須提供詳細資料 policy_schema。

curl -H "Content-Type: application/json" -H "Authorization:Bearer $TOKEN"   -d "{target_resource: 'orgunits/$ORG_UNIT', \
    name: 'Network Name', \
    settings: [
      {policy_schema: 'chrome.networks.wifi.AllowForChromeUsers', value: {'allowForChromeUsers': true}}, \
      {policy_schema: 'chrome.networks.wifi.Details',value: {'details': {'security': 'None', 'ssid': 'ssid'}}}
    ]}" \
    "https://chromepolicy.googleapis.com/v1/customers/$CUSTOMER/policies/networks:defineNetwork"   

成功的回應會包含已建立的網路,包括參照該網路的 NetworkId。

{
  "networkId": "Network Name-Wifi",
  "targetResource": "orgunits/$ORG_UNIT",
  "settings": [
    {
      "policySchema": "chrome.networks.wifi.Details",
      "value": {
        "details": {
          "ssid": "ssid",
          "security": "None",
          "proxySettings": {
            "type": "Direct"
          },
          "allowIpConfiguration": false,
          "allowNameServersConfiguration": false,
          "nameServerSelection": "NAME_SERVERS_ENUM_AUTOMATIC"
        }
      }
    },
    {
      "policySchema": "chrome.networks.wifi.AllowForChromeDevices",
      "value": {
        "allowForChromeDevices": false
      }
    },
    {
      "policySchema": "chrome.networks.wifi.AllowForChromeUsers",
      "value": {
        "allowForChromeUsers": true
      }
    }
  ]
}

移除網路

「移除網路」端點可用來刪除網路。 這個端點用於 Wi-Fi、乙太網路和 VPN 網路。

在本例中,我們會移除 Wi-Fi 網路。

curl -H "Content-Type: application/json" -H "Authorization:Bearer $TOKEN" -d "{target_resource: 'orgunits/$ORG_UNIT', network_id: '$NETWORK_ID'}" \
"https://chromepolicy.googleapis.com/v1/customers/$CUSTOMER/policies/networks:removeNetwork"

成功的回應沒有任何內容。

{}

定義憑證

定義憑證端點會用來建立新的憑證。

在這個範例中,我們定義一個憑證,並允許 Chrome 裝置使用這個憑證。

curl -H "Content-Type: application/json" -H "Authorization:Bearer $TOKEN" -d " \
{
  target_resource: 'orgunits/$ORG_UNIT', 
  certificate: 'raw string representation of a .pem or .crt certificate file.', 
  settings: [{
      policy_schema: 'chrome.networks.certificates.AllowForChromeDevices', 
      value: {'allowForChromeDevices': true} 
  }]
}" "https://chromepolicy.googleapis.com/v1/customers/$CUSTOMER/policies/networks:defineCertificate"

成功的回應將包含已建立憑證 (networkId) 的參照。

{
  "networkId": "{c045f8df-79f1-49d3-92b9-0e61516e6a6b}",
  "targetResource": "orgunits/$ORG_UNIT"
}

移除憑證

「移除憑證」端點可用來移除憑證定義。

在此範例中,我們會移除憑證。

curl -H "Content-Type: application/json" -H "Authorization:Bearer $TOKEN" -d "{target_resource: 'orgunits/$ORG_UNIT', network_id: '$NETWORK_ID'}" \
"https://chromepolicy.googleapis.com/v1/customers/$CUSTOMER/policies/networks:removeCertificate"

成功的回應沒有任何內容。

{}

與已儲存的網路互動

如要與憑證或網路互動,請使用 Policy API。要求必須包含額外的目標鍵,代表您想要與其互動的資源。

只有在解決要求中省略其他目標鍵是可接受的。這樣會導致系統傳回與要求命名空間相符的所有網路。

您可以使用篩選器,透過結構定義服務取得完整的網路結構定義。
如要查看所有 VPN 設定,請嘗試以下做法:

curl -H "Authorization:Bearer $TOKEN" \
  "https://chromepolicy.googleapis.com/v1/customers/$CUSTOMER/policySchemas?filter=chrome.networks.vpn"

以下是將 Imprivata 新增為憑證授權單位的範例。

curl -X POST \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{
        requests: [{
                policyTargetKey: {
                        targetResource: "orgunits/$ORG_UNIT",
                        additionalTargetKeys: {"network_id": "$NETWORK_ID"}
                        },
                policyValue: {
                        policySchema: "chrome.networks.certificates.AllowForChromeImprivata",
                        value: {allowForChromeImprivata: true}
                        },
                updateMask: {paths: "allowForChromeImprivata"}
       }]
      }' \
  "https://chromepolicy.googleapis.com/v1/customers/$CUSTOMER/policies/orgunits:batchModify"

以下是變更網路密碼的範例。

curl -X POST \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{
        requests: [{
                policyTargetKey: {
                        targetResource: "orgunits/$ORG_UNIT",
                        additionalTargetKeys: {"network_id": "$NETWORK_ID"}
                        },
                policyValue: {
                        policySchema: "chrome.networks.wifi.Details",
                        value: {details: {
                                  ssid: 'ssid',
                                  security: 'WEP-PSK'
                                  passphrase: 'Your passphrase.'
                               }
                        }
                },
                updateMask: {paths: "details"}
        }]
      }' \
  "https://chromepolicy.googleapis.com/v1/customers/$CUSTOMER/policies/orgunits:batchModify"