你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

扫描连接的 GitHub 存储库或 Azure DevOps 项目

可以设置 Microsoft Security DevOps 来扫描连接的 GitHub 存储库或 Azure DevOps 项目。 使用 GitHub 操作或 Azure DevOps 扩展仅在基础结构即代码 (IaC) 源代码上运行 Microsoft Security DevOps,并帮助减少管道运行时。

本文介绍如何应用模板 YAML 配置文件,以使用 Microsoft 安全 DevOps 规则扫描连接的存储库或项目,以专门查找 IaC 安全问题。

先决条件

设置并运行 GitHub 操作以扫描连接的 IaC 源代码

若要在 GitHub 中设置操作并查看扫描结果,请执行以下操作:

  1. 登录到 GitHub

  2. 转到存储库的主页。

  3. 在文件目录中,选择 .github>工作流>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. 选择“Pipelines”。

  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 扫描的结果,请选择“管道”,然后选择所修改的管道。

  11. 请参阅更多详细信息,选择特定的管道运行。

查看已应用 IaC 规则的详细信息和修正信息

Microsoft Security DevOps 附带的 IaC 扫描工具包括模板分析器PSRule 包含在模板分析器中)和 Terrascan

模板分析器在 Azure 资源管理器模板(ARM 模板)和 Bicep 模板上运行规则。 有关详细信息,请参阅“模板分析器规则和修正详细信息”。

Terrascan 在适用于 CloudFormation、Docker、Helm、Kubernetes、Kustomize 和 Terraform 的 ARM 模板和模板上运行规则。 有关详细信息,请参阅“Terrascan 规则”。

若要详细了解 Microsoft Security DevOps 随附的 IaC 扫描工具,请参阅:

本文介绍了如何为 Microsoft Security DevOps 设置 GitHub 操作和 Azure DevOps 扩展以扫描 IaC 安全配置错误,以及如何查看结果。

若要获取详细信息,请参阅: