Share via


掃描已連線的 GitHub 存放庫或 Azure DevOps 專案

您可以設定 Microsoft Security DevOps 來掃描已連線的 GitHub 存放庫或 Azure DevOps 專案。 使用 GitHub 動作或 Azure DevOps 延伸模組,只在基礎結構即程式碼 (IaC) 原始程式碼上執行 Microsoft Security DevOps,並協助縮短您的管線執行階段。

本文說明如何使用 Microsoft Security DevOps 規則,套用範本 YAML 組態檔,以掃描連線的存放庫或專案 (特別針對 IaC 安全性問題)。

必要條件

設定並執行 GitHub 動作以掃描連線的 IaC 原始程式碼

若要在 GitHub 中設定動作和檢視掃描結果:

  1. 登入 GitHub

  2. 移至存放庫的主頁面。

  3. 在檔案目錄中,選取 .github>workflows>msdevopssec.yml

    如需在 GitHub 中使用動作的詳細資訊,請參閱必要條件

  4. 選取 [編輯此檔案] (鉛筆) 圖示。

    Screenshot that highlights the Edit this file icon for the msdevopssec.yml file.

  5. 在 YAML 檔案的 [執行分析器] 區段中,新增以下程式碼:

    with:
        categories: 'IaC'
    

    注意

    值會區分大小寫。

    以下是範例:

    Screenshot that shows the information to add to the YAML file.

  6. 選取 [認可變更...]

  7. 選取 [認可變更]

    Screenshot that shows where to select Commit changes on the GitHub page.

  8. (選擇性) 將 IaC 範本新增至您的存放庫。 如果您的存放庫中已經有 IaC 範本,請略過此步驟。

    例如,認可您可用於部署基本 Linux Web 應用程式的 IaC 範本。

    1. 選取 azuredeploy.json 檔案。

      Screenshot that shows where the azuredeploy.json file is located.

    2. 選取 [Raw]。

    3. 複製檔案中的所有資訊,如下列範例所示:

      {
        "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
        "contentVersion": "1.0.0.0",
        "parameters": {
          "webAppName": {
            "type": "string",
            "defaultValue": "AzureLinuxApp",
            "metadata": {
              "description": "The base name of the resource, such as the web app name or the App Service plan."
            },
            "minLength": 2
          },
          "sku": {
            "type": "string",
            "defaultValue": "S1",
            "metadata": {
              "description": "The SKU of the App Service plan."
            }
          },
          "linuxFxVersion": {
            "type": "string",
            "defaultValue": "php|7.4",
            "metadata": {
              "description": "The runtime stack of the current web app."
            }
          },
          "location": {
            "type": "string",
            "defaultValue": "[resourceGroup().location]",
            "metadata": {
              "description": "The location for all resources."
            }
          }
        },
        "variables": {
          "webAppPortalName": "[concat(parameters('webAppName'), '-webapp')]",
          "appServicePlanName": "[concat('AppServicePlan-', parameters('webAppName'))]"
        },
        "resources": [
          {
            "type": "Microsoft.Web/serverfarms",
            "apiVersion": "2020-06-01",
            "name": "[variables('appServicePlanName')]",
            "location": "[parameters('location')]",
            "sku": {
              "name": "[parameters('sku')]"
            },
            "kind": "linux",
            "properties": {
              "reserved": true
            }
          },
          {
            "type": "Microsoft.Web/sites",
            "apiVersion": "2020-06-01",
            "name": "[variables('webAppPortalName')]",
            "location": "[parameters('location')]",
            "kind": "app",
            "dependsOn": [
              "[resourceId('Microsoft.Web/serverfarms', variables('appServicePlanName'))]"
            ],
            "properties": {
              "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('appServicePlanName'))]",
              "siteConfig": {
                "linuxFxVersion": "[parameters('linuxFxVersion')]"
              }
            }
          }
        ]
      }
      
    4. 在您的 GitHub 存放庫中,移至 .github/workflows 資料夾。

    5. 選取 [新增檔案]>[建立新檔案]

      Screenshot that shows you how to create a new file.

    6. 輸入檔案的名稱。

    7. 將所複製的資訊貼到檔案中。

    8. 選取 [認可新檔案]

    範本檔案會新增至您的存放庫。

    Screenshot that shows that the new file you created is added to your repository.

  9. 確認 Microsoft Security DevOps 掃描已完成:

    1. 針對存放庫,選取 [動作]

    2. 選取工作流程以查看動作狀態。

  10. 若要檢視掃描的結果,請移至 [安全性]>[程式碼掃描警示]

    您可以依工具進行篩選,只查看 IaC 結果。

設定並執行 Azure DevOps 延伸模組以掃描連線的 IaC 原始程式碼

若要在 Azure DevOps 中設定延伸模組並檢視掃描結果:

  1. 登入 Azure DevOps

  2. 選取您的專案

  3. 選取 [管線]

  4. 選取已針對 Microsoft Security DevOps 設定 Azure DevOps 延伸模組的管線。

  5. 選取 [編輯管線]

  6. 在管線 YAML 組態檔中,於 MicrosoftSecurityDevOps@1 工作的 displayName 行下方,新增以下程式碼:

    inputs:
        categories: 'IaC'
    

    以下是範例:

    Screenshot that shows where to add the IaC categories line in the pipeline configuration YAML file.

  7. 選取 [儲存]。

  8. (選擇性) 將 IaC 範本新增至您的 Azure DevOps 專案。 如果您的專案中已經有 IaC 範本,請略過此步驟。

  9. 選擇要直接認可至主要分支,還是建立認可的新分支,然後選取 [儲存]

  10. 若要檢視 IaC 掃描的結果,請選取 [Pipelines],然後選取您修改的管線。

  11. 請參閱更多詳細資料,選取特定的管線執行。

檢視已套用 IaC 規則的詳細資料和補救資訊

Microsoft Security DevOps 隨附的 IaC 掃描工具有範本分析器 (PSRule 包含在範本分析器中) 和 Terrascan

範本分析器會在 Azure Resource Manager 範本 (ARM 範本) 和 Bicep 樣本上執行規則。 如需詳細資訊,請參閱範本分析器規則和補救詳細資料

Terrascan 會在 ARM 範本和適用於 CloudFormation、Docker、Helm、Kubernetes、Kustomize 和 Terraform 的範本上執行規則。 如需詳細資訊,請參閱 Terrascan 規則

若要深入了解 Microsoft Security DevOps 隨附的 IaC 掃描工具,請參閱:

在本文中,您已了解如何設定 GitHub 動作和適用於 Microsoft Security DevOps 的 Azure DevOps 延伸模組以掃描 IaC 安全性設定錯誤,以及如何檢視結果。

若要取得詳細資訊: