mcpField
D
diff-scanning-with-aws-security-agent

diff-scanning-with-aws-security-agent

github.com/aws安全3.3k1.5k 次安装今天更新
代码仓库

关于 diff-scanning-with-aws-security-agent

用于指导 AWS Security Agent 扫描相对指定 Git ref 的代码变更。它涵盖生成差异、提交扫描、轮询任务和整理发现结果,无需先做完整扫描。

主要功能

  1. 01按 Git ref 扫描变更
  2. 02生成并上传差异补丁
  3. 03提交并轮询扫描任务
  4. 04记录扫描元数据和发现

适用场景

  1. 01提交前检查未提交代码
  2. 02审查分支相对 main 的变更
  3. 03按自定义 ref 扫描变更

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/aws/agent-toolkit-for-aws --skill diff-scanning-with-aws-security-agent

安装后在 Claude Code 中输入/diff-scanning-with-aws-security-agent确认已加载。

详细介绍

AWS Security Agent — 差异扫描

仅扫描自某个 Git 引用以来发生更改的代码。比完整扫描更快,发现结果仅针对差异部分。无需先进行完整扫描。

本地状态

读取 .security-agent/config.json 中的 agent_space_id 和 region。如果缺少这些值,请先内联运行 setup-security-agent 工作流。

在 .security-agent/scans.json 中跟踪扫描记录。

解析所需的值
占位符解析方式
<id>(Agent 空间)config.agent_space_id
<region>config.region(默认值为 us-east-1)
<account>aws sts get-caller-identity --query Account --output text
<role-arn>arn:aws:iam::<account>:role/SecurityAgentScanRole
<bucket>security-agent-scans-<account>-<region>
<WORKSPACE_ID>printf '%s' "$(pwd)" | md5sum | cut -c1-12

工作流

  1. 扫描前检查。 与完整扫描相同:读取配置,验证 Agent 空间,解析值并生成工作区 ID。

  2. 询问要以什么为基准进行扫描:

    • 未提交的更改 → BASE_REF=HEAD(默认值)
    • 分支与 main 比较 → BASE_REF=main
    • 自定义引用 → 由用户提供
  3. 生成差异(如果为空则快速失败):

    cd <absolute-workspace-path>
    if [ "$BASE_REF" = "HEAD" ]; then
      git diff HEAD > /tmp/diff.patch
    else
      git diff "$BASE_REF..HEAD" > /tmp/diff.patch
    fi
    [ -s /tmp/diff.patch ] || { echo "No changes vs $BASE_REF"; exit 1; }
    
  4. 压缩工作区(排除项与完整扫描相同,限制为 2 GB):

    cd <absolute-workspace-path>
    zip -r /tmp/source.zip . \
      -x ".git/*" -x ".security-agent/*" -x "node_modules/*" \
      -x "__pycache__/*" -x ".venv/*" -x "venv/*" \
      -x "dist/*" -x "build/*" -x "target/*" \
      -x ".mypy_cache/*" -x ".pytest_cache/*" -x ".tox/*" \
      -x ".next/*" -x "cdk.out/*" -x ".DS_Store" -x "*.pyc"
    
  5. 同时上传源代码压缩包和差异补丁:

    SCAN_ID="diff-$(date +%s)-$(openssl rand -hex 3)"
    aws s3 cp /tmp/source.zip s3://<bucket>/security-scans/source/<WORKSPACE_ID>/source.zip --expected-bucket-owner <account>
    aws s3 cp /tmp/diff.patch s3://<bucket>/security-scans/diffs/${SCAN_ID}/diff.patch --expected-bucket-owner <account>
    
  6. 获取或创建每个工作区对应的 CodeReview(逻辑与完整扫描相同:查找 config.json → code_reviews[<abs_path>],如果不存在则创建):

    aws securityagent create-code-review --agent-space-id <id> --title <title> \
      --service-role <role-arn> \
      --assets sourceCode=[{s3Location=s3://<bucket>/security-scans/source/<WORKSPACE_ID>/source.zip}]
    
  7. 启动差异扫描任务:

    aws securityagent start-code-review-job --agent-space-id <id> --code-review-id <cr-id> \
      --diff-source s3Uri=s3://<bucket>/security-scans/diffs/${SCAN_ID}/diff.patch
    

    如果出现 ResourceNotFoundException:重新创建 CodeReview 并重试。

  8. 捕获 codeReviewJobId。将 scan_type: "DIFF" 和 base_ref 保存到 scans.json。

  9. 告知用户:“差异扫描已启动。需要几分钟。我会每 2 分钟检查一次——如要退出轮询,请说‘停止轮询’。”

  10. 每 2 分钟轮询一次:

    aws securityagent batch-get-code-review-jobs --agent-space-id <id> --code-review-job-ids <job_id>
    

    仅在状态发生变化时回复。状态变为 COMPLETED 时 → 获取发现结果。

  11. 发现结果: 展示方式与完整扫描相同——按严重程度分组,报告写入 .security-agent/findings-{scan_id}.md。


规则

  • 差异扫描可独立运行——无需先进行完整扫描
  • 每 2 分钟轮询一次,不要更频繁
  • 如果用户未指定,则默认使用 BASE_REF=HEAD
  • 标题:diff-<git-branch>-<timestamp>(不含空格)
  • 如果差异为空,告知用户并停止——不要启动扫描

评论

支持 Markdown