

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用带有 Kiro 和其他编码助手的 MCP 服务器部署实时编码安全验证
<a name="deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants"></a>

*Ivan Girardi 和 Iker Reina Fuente，Amazon Web Services*

## Summary
<a name="deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants-summary"></a>

此模式描述了如何实现模型上下文协议 (MCP) 服务器，该服务器集成了三个行业标准的安全扫描工具，以提供全面的代码安全分析。该服务器使人工智能编码助手（例如 Kiro、Amazon Q Developer 和 Cline）能够自动扫描代码片段和基础设施即代码 (IaC) 配置。通过这些扫描，编码助手可以帮助识别安全漏洞、配置错误和合规违规行为。

在数百万个代码片段上训练的 AI 代码生成器会造成安全盲点——这些训练数据有多安全？ 此模式可在代码生成期间提供实时安全验证，帮助开发人员在编码时识别和了解潜在的安全问题。这种方法可以帮助开发人员解决直接漏洞和依赖关系中继承的风险。通过弥合 AI 效率和安全合规性之间的差距，这种模式有助于安全采用 AI 驱动的开发工具。

这种模式通过人工智能辅助的编码工具帮助组织增强其开发安全实践，提供跨多种编程语言和基础架构定义的持续安全扫描功能。该解决方案结合了以下工具的功能：
+ 用于扫描 IaC 文件（包括 Terraform 和 Kubernetes 清单）的 Checkov AWS CloudFormation
+ Semgrep 用于分析多种编程语言，例如 Python、 JavaScript、Java 等
+ Bandit 用于专门的 Python 安全扫描 

该解决方案的主要功能包括：
+ 对新代码段进行增量扫描，减少计算开销
+ 隔离的安全工具环境，防止交叉工具污染
+ 与人工智能编码助手（Kiro、Amazon Q Developer、Cline 等）无缝集成
+ 代码生成期间的实时安全反馈
+ 可自定义的扫描规则以实现组织合规性

该模式为具有标准化响应格式的安全扫描提供了统一的接口，从而可以更轻松地将安全检查集成到开发工作流程中。该模式使用 Python 和 MCP 框架来提供自动安全反馈。这种方法可以帮助开发人员在开发过程的早期发现和解决安全问题，同时通过详细的发现来学习安全最佳实践。

## 先决条件和限制
<a name="deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants-prereqs"></a>

**先决条件**
+ 如果您想使用 AWS 账户 其中任何一个编码助手，则可以使用 Kiro 或 Amazon Q Developer
+ [已安装 Python 版本 3.10 或更高版本](https://www.python.org/downloads/)
+ `uv`软件包管理器[已安装](https://docs.astral.sh/uv/getting-started/installation/)
+ 熟悉安全扫描工具和概念
+ 对 IaC 和应用程序安全的基本了解

**限制**
+ 强盗扫描仅限于 Python 文件。
+ 实时扫描可能会影响大型代码库的性能。
+ 特定于工具的限制取决于支持的文件格式和语言。
+ 需要手动审查才能验证安全调查结果。
+ 安全扫描结果需要安全专业知识才能正确解释。
+ 有些 AWS 服务 并非全部可用 AWS 区域。有关区域可用性，请参阅[按区域划分的AWS 服务](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/)。有关特定端点，请参阅[服务端点和配额](https://docs.aws.amazon.com/general/latest/gr/aws-service-information.html)，然后选择相应服务的链接。

**产品版本**
+ Python 版本 3.10 或更高版本
+ Checkov 版本 3.0.0 或更高版本
+ Semgrep 版本 1.45.0 或更高版本
+ Bandit 版本 1.7.5 或更高版本
+ MCP [cli] 版本 1.11.0 或更高版本
+ Pydantic 版本 1.10.0 或更高版本
+ Loguru 版本 0.6.0 或更高版本

## 架构
<a name="deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants-architecture"></a>

下图显示了该解决方案的架构。

![\[AI 助手将代码发送到 MCP 安全扫描器服务器以路由到专门的扫描仪；扫描结果将发送给开发人员。\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/images/pattern-img/fa623544-4d54-48af-a4e4-9b6a0624e776/images/9c881f95-76d0-40f6-983e-d987fd2097b8.png)


下图显示了如下工作流：

1. 开发者使用 AI 助手（例如 Kiro、Cline、Amazon Q Developer 或 RooCode）来生成或分析代码。AI 助手发送代码进行安全扫描。

1. MCP 安全扫描器服务器通过将请求路由到相应的专业扫描程序来处理请求：Checkov 用于 IaC 文件，Semgrep 用于多种编程语言分析，Bandit 用于特定于 Python 的安全扫描。

1. 扫描器结果以及安全发现、严重性级别、详细描述和建议的修复方法将通过 AI 助手发送给开发人员。

1. 建立了持续的反馈循环，开发人员可以在其中获得实时安全验证，通过人工智能助手实现自动修复，并在开发过程中推广安全最佳实践。

该架构可降低以下常见安全风险：
+ 命令注入
+ 及时注射
+ 路径遍历
+ 依赖攻击
+ 资源枯竭 

该架构通过实施以下最佳实践来降低这些常见的安全风险：
+ 所有用户和 AI 模型输入都写入临时文件。
+ 不向命令行界面 (CLI) 命令提供直接输入。
+ 文件系统访问权限仅限于临时目录和文件。
+ 临时文件会自动清理。
+ 扫描响应已被清理。
+ 强制执行限制进程能力的进程隔离。
+ 所有扫描活动都将被记录下来。

**自动化和扩展**

该模式通过以下功能支持自动化：
+ 与 AI 编码助手集成，实现自动扫码
+ 用于自动处理的标准化 API 响应
+ 通过 MCP 配置文件进行配置
+ Support 支持批处理多个文件
+ 跨多种编程语言和 IaC 格式的可扩展扫描

扫描过程可以通过提供的 API 端点实现自动化：
+ `scan_with_checkov`用于 IaC 扫描
+ `scan_with_semgrep`用于多语言代码扫描
+ `scan_with_bandit`用于特定于 Python 的扫描
+ `get_supported_formats`用于格式验证

扩展扫描工具时，请遵循本节前面描述的设计原则和最佳实践。另请参阅[最佳实践](#deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants-best-practices)。

## 工具
<a name="deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants-tools"></a>

**AWS 服务**
+ [Kiro](https://aws.amazon.com/documentation-overview/kiro/) 是一项代理编码服务，它与开发人员合作，将提示转化为详细规范，然后转化为工作代码、文档和测试。Kiro 代理可帮助开发人员解决具有挑战性的问题并自动执行生成文档和单元测试等任务。
+ [Amazon Q](https://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/what-is.html) Developer 是一款基于人工智能的生成式对话助手，可以帮助您理解、构建、扩展和操作 AWS 应用程序。

**其他工具**
+ [Bandit](https://bandit.readthedocs.io/en/latest/) 是一款专门的 Python 安全扫描工具。它可以检测常见的 Python 安全问题，例如不安全的函数、硬编码的机密和注入漏洞。Bandit 提供了详细的置信度和严重性评级。
+ [Checkov](https://github.com/bridgecrewio/checkov) 是一种静态代码分析工具，用于检查 IaC 是否存在安全性和合规性错误配置。此外，Checkov还可以检测违规行为和安全最佳实践。
+ [Cline](https://cline.bot/) 是一款由 AI 驱动的编码助手，在 VS Code 中运行。
+ [Loguru](https://loguru.readthedocs.io/en/stable/) 是一个适用于 Python 的数据验证库。
+ [模型上下文协议 (MCP)](https://modelcontextprotocol.io/docs/getting-started/intro) 是一个用于构建 AI 辅助开发工具的开源框架。
+ [Pydantic](https://docs.pydantic.dev/latest/) 是一个适用于 Python 的数据验证库。
+ [Semgrep](https://semgrep.dev/docs/introduction) 分析源代码中是否存在安全漏洞和错误。它支持多种编程语言。Semgrep 使用以安全为重点的规则集进行全面分析。它提供了详细的置信度和严重性评级。

**代码存储库**

此模式的代码可在 GitHub [MCP 安全扫描器：AI 代码助手的实时保护](https://github.com/aws-samples/sample-mcp-security-scanner)存储库中找到。该存储库包括 MCP 服务器实现、Kiro、Amazon Q Developer、Cline 等的 MCP 配置的详细信息、配置示例和测试实用程序。

存储库结构包括：
+ `security_scanner_mcp_server/`-主服务器实现
+ `docs/`-文档和演示材料
+ `tests/`-测试文件
+ `mcp-config-example.json`-MCP 配置示例
+ `requirements.txt`-项目依赖关系

## 最佳实践
<a name="deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants-best-practices"></a>

**安全扫描实施**
+ 查看安全发现以验证问题并确定问题优先级。
+ 将扫描工具（Checkov、Semgrep 和 Bandit）更新到最新版本。
+ 将此模式的 MCP 安全工具与其他安全措施和工具结合使用。
+ 定期更新安全规则集和策略。

**配置管理**
+ 使用官方版本控制源中的 MCP 配置文件。
+ 记录自定义规则和配置。

**集成**
+ 在开发周期的早期就集成安全扫描。
+ 在预提交挂钩或持续集成和持续部署 (CI/CD) 管道中设置自动扫描。
+ 为您的环境配置适当的严重性阈值。
+ 制定处理安全发现的明确程序。

**运营注意事项**
+ 监控扫描性能和资源使用情况。
+ 实现正确的错误处理和日志记录。
+ 维护自定义配置的文档。
+ 建立审查和更新安全规则的流程。

此外，请记住以下最佳实践：
+ 务必在您的特定环境中验证安全发现。
+ 使安全工具和依赖项保持最新。
+ 使用多种安全工具实现全面覆盖。
+ 在开发过程中遵循安全最佳实践。

## 操作说明
<a name="deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants-epics"></a>

### （Kiro 用户）设置 MCP 安全扫描器服务器
<a name="kiro-users-set-up-the-mcp-security-scanner-server"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 配置 MCP 设置。 | 您可以通过（选项 1）手动查找配置文件或（选项 2）使用 Kiro IDE 在 Kiro 中编辑配置文件。[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants.html)<pre>{<br />  "mcpServers": {<br />    "security-scanner": {<br />      "command": "uvx",<br />      "args": [<br />        "--from",<br />        "git+https://github.com/aws-samples/sample-mcp-security-scanner.git@main",<br />        "security_scanner_mcp_server"<br />      ],<br />      "env": {<br />        "FASTMCP_LOG_LEVEL": "ERROR"<br />      },<br />      "disabled": false,<br />      "autoApprove": []<br />    }<br />  }<br />}</pre> | 应用程序开发人员 | 

### （Amazon Q 开发者用户）设置 MCP 安全扫描器服务器
<a name="qdevlong-users-set-up-the-mcp-security-scanner-server"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 配置 MCP 设置。 | 要手动配置 MCP 设置，请使用以下步骤：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants.html)<pre>{<br />  "mcpServers": {<br />    "security-scanner": {<br />      "command": "uvx",<br />      "args": [<br />        "--from",<br />        "git+https://github.com/aws-samples/sample-mcp-security-scanner.git@main",<br />        "security_scanner_mcp_server"<br />      ],<br />      "env": {<br />        "FASTMCP_LOG_LEVEL": "ERROR"<br />      }<br />    }<br />  }<br />}</pre> | 应用程序开发人员 | 

### （Cline 用户）设置 MCP 安全扫描器服务器
<a name="cline-users-set-up-the-mcp-security-scanner-server"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 配置 MCP 设置。 | 要手动配置 MCP 设置，请使用以下步骤：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants.html)<pre>{<br />  "mcpServers": {<br />    "security-scanner": {<br />      "command": "uvx",<br />      "args": [<br />        "--from",<br />        "git+https://github.com/aws-samples/sample-mcp-security-scanner.git@main",<br />        "security_scanner_mcp_server"<br />      ],<br />      "env": {<br />        "FASTMCP_LOG_LEVEL": "ERROR"<br />      },<br />      "disabled": false,<br />      "autoApprove": []<br />    }<br />  }<br />}</pre> | 应用程序开发人员 | 

### 使用 Python 和 Bandit 进行代码分析的示例
<a name="example-of-code-analysis-using-python-and-bandit"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 执行代码分析。 | 要使用 Python 和 Bandit 执行代码分析，请按以下步骤操作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants.html) | 应用程序开发人员 | 

### 使用 Terraform 和 Checkov 进行代码分析的示例
<a name="example-of-code-analysis-using-terraform-and-checkov"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 执行代码分析。 | 要使用 Terraform 和 Checkov 执行代码分析，请使用以下步骤：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants.html) | 应用程序开发人员 | 

### 高级扫描功能示例
<a name="example-of-advanced-scanning-capabilities"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 执行有针对性的扫描。 | 以下是可用于执行定向扫描的请求示例：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants.html) | 应用程序开发人员 | 
| 使用安全扫描和代码生成。 | 要使用代码生成循环来解决安全问题，请使用以下步骤（此示例使用 Kiro 作为编码助手）：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants.html) | 应用程序开发人员 | 

## 问题排查
<a name="deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants-troubleshooting"></a>


| 问题 | 解决方案 | 
| --- | --- | 
| 环境设置问题 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants.html) | 
| 扫描仪问题 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants.html) | 
| 集成问题 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants.html) | 
| 其他支持 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants.html) | 

## 相关资源
<a name="deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants-resources"></a>

**AWS 文档**
+ [基础架构即代码](https://docs.aws.amazon.com/whitepapers/latest/introduction-devops-aws/infrastructure-as-code.html)（AWS 白皮书*简介*） DevOps AWS

**其他 AWS 资源**
+ [安全性、身份和合规性最佳实践](https://aws.amazon.com/architecture/security-identity-compliance/)

**其他资源**
+ [Bandit 文档](https://aws.amazon.com/architecture/security-identity-compliance/)
+ [Checkov 文档](https://aws.amazon.com/architecture/security-identity-compliance/)
+ [模型上下文协议 (MCP) 文档](https://aws.amazon.com/architecture/security-identity-compliance/)
+ [OWASP 安全编码实践](https://owasp.org/www-project-secure-coding-practices-quick-reference-guide/)（OWASP 基金会网站）
+ [Semgrep 文档](https://aws.amazon.com/architecture/security-identity-compliance/)

## 附加信息
<a name="deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants-additional"></a>

**启用了 auto 批准的 MCP 配置示例**

如果未`autoApprove`进行配置，则用户必须批准才能将代码发送到 MCP 安全服务器进行扫描。配置完成后`autoApprove`，允许代码助手在未经用户批准的情况下调用这些工具。这些工具在计算机上本地运行，不发送任何数据，只执行代码扫描。

以下配置允许自动执行所有安全扫描功能：

```
{
  "mcpServers": {
    "security-scanner": {
      "command": "uvx",
      "args": [
        "--from",
        "git+https://github.com/aws-samples/sample-mcp-security-scanner.git@main",
        "security_scanner_mcp_server"
      ],
      "env": {
        "FASTMCP_LOG_LEVEL": "ERROR"
      },
      "disabled": false,
      "autoApprove": [
        "scan_with_checkov",
        "scan_with_semgrep", 
        "scan_with_bandit",
        "get_supported_formats"
      ]
    }
  }
}
```

要启用调试日志记录，`"FASTMCP_LOG_LEVEL"`请将设置为`"DEBUG"`。

**安全扫描工具支持的文件格式**

此解决方案中的每种安全扫描工具都支持以下文件格式：

*Checkov (IaC)*
+ Terraform — .tf、.tfvars、.tfstate
+ CloudFormation — .yaml、.yml、.json、.template
+ Kubernetes — .yaml、.yml
+ Dockerfile — Dockerfile
+ ARM — .json（Azure 资源管理器）
+ 二头肌 — .bicep
+ 无服务器 — .yml、.yaml
+ Helm — .yaml、.yml、.tpl
+ GitHub 操作 — .yml、.yaml
+ GitLab\$1ci — .yml、.yaml
+ Ansible — .yml、.yaml

*Semgrep（源代码）*
+ Python — .py
+ JavaScript — .js
+ TypeScript — .ts
+ Java — .java
+ 走 — .go
+ C — .c.
+ C\$1\$1 — .cpp
+ C\$1 — .cs
+ Ruby — .rb
+ PHP — .php
+ Scala — .scala
+ Kotlin — .kt
+ Rust — .rs

*强盗（仅限 Python）*
+ Python — .py

**演示**

要进行代码扫描，请使用 AI 助手尝试以下示例提示：
+ “扫描当前脚本并告诉我结果。”
+ “扫描 20-60 行，然后告诉我结果。”
+ “扫描这个 Amazon DynamoDB 表资源并告诉我结果。”

有关更多信息，请参阅此模式 GitHub 存储库中的此[代码扫描演示](https://github.com/aws-samples/sample-mcp-security-scanner/blob/main/docs/demo_code_scan.gif)。

要生成安全代码，请尝试以下示例提示：
+ “生成 Terraform 配置以创建启用加密的 DynamoDB 表，然后对其进行扫描以发现安全问题。”
+ “创建一个写入 DynamoDB 的 Python Lambda 函数并对其进行漏洞扫描。”
+ “为具有正确安全设置的 S3 存储桶生成 CloudFormation 模板并验证其是否通过安全检查。”
+ “编写一个 Python 脚本，通过分页查询 DynamoDB，并扫描安全最佳实践。”
+ “为具有安全强化功能的微服务创建 Kubernetes 部署清单并对其进行验证。”

有关更多信息，请参阅此模式 GitHub 存储库中的此[代码生成和安全扫描](https://github.com/aws-samples/sample-mcp-security-scanner/blob/main/docs/demo_code_generation.gif)演示。