共用方式為


如何尋找陰影 API

您可以使用 Azure API 中心來編錄組織中所使用的 API。 您可以隨時判斷組織中所使用的 API。 您也可以告知 API 在其生命週期中的位置,以及是否有問題要連絡的人員。 簡言之,有最新的 API 目錄可協助您改善治理、合規性和安全性狀態。

建置應用程式時,特別是當您整合新的案例時,您可能會使用未在 Azure API 中心註冊的 API。 這些 API 稱為陰影 API。 影子 API 是未在您的組織中註冊的 API。 這些 API 可能是尚未註冊的 API,或可能是貴組織中未使用的 API。

若要檢查您的應用程式是否使用陰影 API,您可以使用 ApiCenterOnboardingPlugin 外掛程式。 此外掛程式會分析來自您應用程式的 API 要求,以及針對未在 API 中心註冊的任何 API 報告。 此外,它可以將新的 API 直接上線至 Azure API 中心。

命令提示字元的螢幕快照,其中顯示開發人員 Proxy 檢查記錄的 API 要求是否已在 Azure API 中心註冊。

在您開始使用 Intune 之前

若要偵測影子 API,您需要有 Azure API 中心 實例,其中包含您在組織中所使用的 API 相關信息。

建立 Azure API 中心實例

開始之前,請先建立 Azure API 中心實例,並註冊您在組織中使用的 API。

Azure API 中心的螢幕快照,其中包含組織中使用的數個 API。

ApiCenterOnboardingPlugin 使用這項資訊來檢查您的應用程式是否在組織中註冊 API。

複製 API 中心資訊

從 [Azure API 中心實例概觀] 頁面,複製 API 中心實例的名稱、資源群組的名稱訂用帳戶標識碼。 您需要這項資訊來設定 ApiCenterOnboardingPlugin ,使其可以連線到您的 Azure API 中心實例。

Azure API 中心概觀頁面的螢幕快照,其中已醒目提示數個屬性。

設定開發 Proxy

若要檢查您的應用程式是否使用陰影 API,您需要在開發 Proxy 設定檔案中啟用 ApiCenterOnboardingPlugin 。 若要建立應用程式所使用的 API 報告,請新增記者。

啟用 ApiCenterOnboardingPlugin

在 檔案中 devproxyrc.json ,新增下列組態:

{
  "$schema": "https://raw.githubusercontent.com/microsoft/dev-proxy/main/schemas/v0.19.0/rc.schema.json",
  "plugins": [
    {
      "name": "ApiCenterOnboardingPlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll",
      "configSection": "apiCenterOnboardingPlugin"
    }
  ],
  "urlsToWatch": [
    "https://jsonplaceholder.typicode.com/*"
  ],
  "apiCenterOnboardingPlugin": {
    "subscriptionId": "00000000-0000-0000-0000-000000000000",
    "resourceGroupName": "demo",
    "serviceName": "contoso-api-center",
    "workspaceName": "default",
    "createApicEntryForNewApis": false
  }
}

subscriptionIdresourceGroupNameserviceName 屬性中,提供 Azure API 中心實例的相關信息。

在屬性中 urlsToWatch ,指定您的應用程式所使用的URL。

提示

使用 Dev Proxy 工具組 Visual Studio Code 擴充功能輕鬆管理開發 Proxy 設定。

新增記者

會產生 ApiCenterOnboardingPlugin 應用程式正在使用的 API 報告。 若要檢視此報告,請將記者新增至您的 Dev Proxy 組態檔。 開發人員 Proxy 提供數 記者。 在此範例中,您會使用 純文本記者

使用純文字記者的參考來更新您的 devproxyrc.json 檔案:

{
  "$schema": "https://raw.githubusercontent.com/microsoft/dev-proxy/main/schemas/v0.19.0/rc.schema.json",
  "plugins": [
    {
      "name": "ApiCenterOnboardingPlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll",
      "configSection": "apiCenterOnboardingPlugin"
    },
    {
      "name": "PlainTextReporter",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll"
    }
  ],
  "urlsToWatch": [
    "https://jsonplaceholder.typicode.com/*"
  ],
  "apiCenterOnboardingPlugin": {
    "subscriptionId": "00000000-0000-0000-0000-000000000000",
    "resourceGroupName": "demo",
    "serviceName": "contoso-api-center",
    "workspaceName": "default",
    "createApicEntryForNewApis": false
  }
}

檢查您的應用程式是否使用陰影 API

若要檢查您的應用程式是否使用影子 API,請連線到您的 Azure 訂用帳戶、執行 Dev Proxy,並讓它攔截來自您應用程式的 API 要求。 然後,開發人員 Proxy 會比較 API 要求的相關信息與 Azure API 中心的資訊,以及未在 API 中心註冊的任何 API 報告。

連接到 Azure 訂用帳戶

開發人員 Proxy 會使用來自 Azure API 中心的信息來判斷您的應用程式是否使用陰影 API。 若要取得這項資訊,它需要連線到您的 Azure 訂用帳戶。 您可以透過數種方式連線到您的 Azure 訂用帳戶。

執行開發 Proxy

連線到您的 Azure 訂用帳戶之後,請啟動 Dev Proxy。 如果您從檔案 devproxyrc.json 所在的相同資料夾啟動 Dev Proxy,它會自動載入組態。 否則,請使用 --config-file 選項指定組態檔的路徑。

當開發 Proxy 啟動時,它會檢查它是否可以連線到您的 Azure 訂用帳戶。 線上成功時,您會看到類似下列的訊息:

 info    Plugin ApiCenterOnboardingPlugin connecting to Azure...
 info    Listening on 127.0.0.1:8000...

Hotkeys: issue (w)eb request, (r)ecord, (s)top recording, (c)lear screen
Press CTRL+C to stop Dev Proxy

r 以開始錄製來自您應用程式的 API 要求。

使用您的應用程式

像平常一樣使用您的應用程式。 開發 Proxy 會攔截 API 要求,並將相關信息儲存在記憶體中。 在 Dev Proxy 執行的命令行中,您應該會看到應用程式提出的 API 要求相關信息。

 info    Plugin ApiCenterOnboardingPlugin connecting to Azure...
 info    Listening on 127.0.0.1:8000...

Hotkeys: issue (w)eb request, (r)ecord, (s)top recording, (c)lear screen
Press CTRL+C to stop Dev Proxy

◉ Recording... 

 req   ╭ GET https://jsonplaceholder.typicode.com/posts
 api   ╰ Passed through

 req   ╭ DELETE https://jsonplaceholder.typicode.com/posts/1
 api   ╰ Passed through

檢查陰影 API

按 s 停止錄製。 開發人員 Proxy 會連線到 API 中心實例,並將要求的相關信息與 API 中心的信息進行比較。

 info    Plugin ApiCenterOnboardingPlugin connecting to Azure...
 info    Listening on 127.0.0.1:8000...

Hotkeys: issue (w)eb request, (r)ecord, (s)top recording, (c)lear screen
Press CTRL+C to stop Dev Proxy

◉ Recording... 

 req   ╭ GET https://jsonplaceholder.typicode.com/posts
 api   ╰ Passed through

 req   ╭ DELETE https://jsonplaceholder.typicode.com/posts/1
 api   ╰ Passed through
○ Stopped recording
 info    Checking if recorded API requests belong to APIs in API Center...
 info    Loading APIs from API Center...
 info    Loading API definitions from API Center...

當 Dev Proxy 完成分析時,它會在名為 ApiCenterOnboardingPlugin_PlainTextReporter.txt 且具有下列內容的檔案中建立報表:

New APIs that aren't registered in Azure API Center:

https://jsonplaceholder.typicode.com:
  DELETE https://jsonplaceholder.typicode.com/posts/1

APIs that are already registered in Azure API Center:

GET https://jsonplaceholder.typicode.com/posts

自動上線陰影 API

ApiCenterOnboardingPlugin不僅可以偵測陰影 API,還可以自動將它們上線至 API 中心。 若要自動將陰影 API 上線,請在開發人員 Proxy 設定檔中,將更新 createApicEntryForNewApistrue

{
  "$schema": "https://raw.githubusercontent.com/microsoft/dev-proxy/main/schemas/v0.19.0/rc.schema.json",
  "plugins": [
    {
      "name": "ApiCenterOnboardingPlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll",
      "configSection": "apiCenterOnboardingPlugin"
    },
    {
      "name": "PlainTextReporter",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll"
    }
  ],
  "urlsToWatch": [
    "https://jsonplaceholder.typicode.com/*"
  ],
  "apiCenterOnboardingPlugin": {
    "subscriptionId": "00000000-0000-0000-0000-000000000000",
    "resourceGroupName": "demo",
    "serviceName": "contoso-api-center",
    "workspaceName": "default",
    "createApicEntryForNewApis": true
  }
}

當您執行設定為true的 Dev Proxy createApicEntryForNewApis 時,它會自動在 Azure API 中心中為偵測到的陰影 API 建立新的 API 專案。

顯示新上線 API 的 API 中心螢幕快照。

使用 OpenAPI 規格自動上線陰影 API

當您選擇自動上線、陰影 API 至 API 中心時,您可以讓開發人員 Proxy 產生 API 的 OpenAPI 規格。 使用 OpenAPI 規格將 API 上線可加快遺失端點的上線速度,並提供 API 的必要資訊。 ApiCenterOnboardingPlugin當 偵測到時,開發 Proxy 會建立新的 OpenAPI 規格,它會將它與 API 中心中對應的上線 API 產生關聯。

若要自動產生上線 API 的 OpenAPI 規格,請更新 Dev Proxy 組態以包含 OpenApiSpecGeneratorPlugin

{
  "$schema": "https://raw.githubusercontent.com/microsoft/dev-proxy/main/schemas/v0.19.0/rc.schema.json",
  "plugins": [
    {
      "name": "OpenApiSpecGeneratorPlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll"
    },
    {
      "name": "ApiCenterOnboardingPlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll",
      "configSection": "apiCenterOnboardingPlugin"
    },
    {
      "name": "PlainTextReporter",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll"
    }
  ],
  "urlsToWatch": [
    "https://jsonplaceholder.typicode.com/*"
  ],
  "apiCenterOnboardingPlugin": {
    "subscriptionId": "00000000-0000-0000-0000-000000000000",
    "resourceGroupName": "demo",
    "serviceName": "contoso-api-center",
    "workspaceName": "default",
    "createApicEntryForNewApis": true
  }
}

重要

開發 Proxy 會依在組態中註冊的順序執行外掛程式。 您需要先註冊, OpenApiSpecGeneratorPlugin 才能在上線新的 API 之前 ApiCenterOnboardingPlugin 建立 OpenAPI 規格。

當您使用此組態執行 Dev Proxy 時,它會在 Azure API 中心自動為偵測到的陰影 API 建立新的 API 專案。 針對每個新的 API,開發 Proxy 會產生 OpenAPI 規格,並將它與 API 中心中對應的上線 API 產生關聯。

 info    Plugin ApiCenterOnboardingPlugin connecting to Azure...
 info    Listening on 127.0.0.1:8000...

Hotkeys: issue (w)eb request, (r)ecord, (s)top recording, (c)lear screen
Press CTRL+C to stop Dev Proxy

◉ Recording... 

 req   ╭ GET https://jsonplaceholder.typicode.com/posts
 api   ╰ Passed through

 req   ╭ DELETE https://jsonplaceholder.typicode.com/posts/1
 api   ╰ Passed through
○ Stopped recording
 info    Creating OpenAPI spec from recorded requests...
 info    Created OpenAPI spec file jsonplaceholder.typicode.com-20240614104931.json
 info    Checking if recorded API requests belong to APIs in API Center...
 info    Loading APIs from API Center...
 info    Loading API definitions from API Center...
 info    New APIs that aren't registered in Azure API Center:

https://jsonplaceholder.typicode.com:
  DELETE https://jsonplaceholder.typicode.com/posts/1
 info    Creating new API entries in API Center...
 info      Creating API new-jsonplaceholder-typicode-com-1718354977 for https://jsonplaceholder.typicode.com...
 info    DONE

Azure API 中心的螢幕快照,其中顯示具有OpenAPI規格的新上線API。

摘要

ApiCenterOnboardingPlugin使用 ,您可以檢查您的應用程式是否使用陰影 API。 外掛程式會分析來自您應用程式的 API 要求,並報告任何未在 Azure API 中心註冊的 API 要求。 外掛程式可讓您輕鬆地將遺漏的 API 上線至 API 中心。 藉由結合 ApiCenterOnboardingPlugin 外掛程式與 OpenApiSpecGeneratorPlugin,您可以為新上線的 API 自動產生 OpenAPI 規格。 您可以手動執行這項檢查,或與您的 CI/CD 管線整合,以確保您的應用程式在發行至生產環境之前使用已註冊的 API。

其他相關資訊