$npx -y skills add aws/agent-toolkit-for-aws --skill setup-security-agentConfigure AWS Security Agent for the current workspace — provision or reuse an agent space, IAM service role, and S3 bucket. Use when the user asks to "set up security agent", "configure security scanner", "is security agent configured", or on first-time use before any scan or pe
| 1 | # AWS Security Agent — Setup |
| 2 | |
| 3 | This skill handles ONE thing: making sure the workspace has a working agent space, IAM service role, and S3 bucket linked together. Scans and pentests live in separate skills and assume this is done. |
| 4 | |
| 5 | --- |
| 6 | |
| 7 | ## Local state convention |
| 8 | |
| 9 | All Security Agent skills share workspace-local state at `.security-agent/`: |
| 10 | |
| 11 | - `config.json` — `{ "agent_space_id": "as-...", "region": "us-east-1", "code_reviews": { "<abs_path>": "cr-..." } }`. Account ID, role ARN, and bucket name are derived by convention. The `code_reviews` map lets scans reuse the same CodeReview for a workspace. |
| 12 | - `scans.json` — array of `{ scan_id, code_review_id, job_id, agent_space_id, scan_type, title, started_at, status, path }` (keep last 50) |
| 13 | - `pentests.json` — same shape, for pentest jobs |
| 14 | - `.gitignore` — contents `*` so this directory stays untracked |
| 15 | - `findings-{scan_id}.md` — written by the scan skill after each scan completes |
| 16 | |
| 17 | This skill's job is to populate `config.json` and create `.gitignore`. |
| 18 | |
| 19 | ### Derived values (convention over config) |
| 20 | |
| 21 | Other skills compute these on each invocation rather than reading them from `config.json`: |
| 22 | |
| 23 | | Value | Convention | |
| 24 | |-------|------------| |
| 25 | | `ACCOUNT` | `aws sts get-caller-identity --query Account --output text` | |
| 26 | | `REGION` | `config.region` (default `us-east-1`) | |
| 27 | | `service_role_arn` | `arn:aws:iam::${ACCOUNT}:role/SecurityAgentScanRole` | |
| 28 | | `s3_bucket` | `security-agent-scans-${ACCOUNT}-${REGION}` | |
| 29 | |
| 30 | Why minimal config: the role name and bucket name are deterministic, so storing them adds drift risk (a user re-creating a role manually would silently use a stale path). Only `agent_space_id` is stored because users may have multiple agent spaces and we don't want to ask which one every session. |
| 31 | |
| 32 | --- |
| 33 | |
| 34 | ## Workflow |
| 35 | |
| 36 | 1. **Check existing state:** read `.security-agent/config.json` if it exists. |
| 37 | 2. **Caller identity + region:** |
| 38 | |
| 39 | ```bash |
| 40 | export ACCOUNT=$(aws sts get-caller-identity --query Account --output text) |
| 41 | export REGION="${AWS_REGION:-us-east-1}" |
| 42 | ``` |
| 43 | |
| 44 | 3. **Agent space:** |
| 45 | - If `config.agent_space_id` is set, verify with: |
| 46 | |
| 47 | ```bash |
| 48 | aws securityagent batch-get-agent-spaces --agent-space-ids <id> |
| 49 | ``` |
| 50 | |
| 51 | If the response shows it doesn't exist, treat as missing. |
| 52 | - If missing, list existing: |
| 53 | |
| 54 | ```bash |
| 55 | aws securityagent list-agent-spaces |
| 56 | ``` |
| 57 | |
| 58 | - If any exist → **show them to the user** with name + id and ask: "Would you like to reuse one of these, or should I create a new one?" Wait for the answer. **Do not auto-select.** |
| 59 | - If user picks one, use that `agentSpaceId`. |
| 60 | - If user wants new, or none exist: |
| 61 | |
| 62 | ```bash |
| 63 | aws securityagent create-agent-space --name security-scans |
| 64 | ``` |
| 65 | |
| 66 | Capture returned `agentSpaceId`. |
| 67 | 4. **Service role** (`SecurityAgentScanRole`, ARN `arn:aws:iam::$ACCOUNT:role/SecurityAgentScanRole`): |
| 68 | - Probe: |
| 69 | |
| 70 | ```bash |
| 71 | aws iam get-role --role-name SecurityAgentScanRole |
| 72 | ``` |
| 73 | |
| 74 | - If `NoSuchEntity` is returned, create the role. **Idempotency note:** `create-role` will fail with `EntityAlreadyExists` if the role already exists. If that happens, fall through to `update-assume-role-policy` to ensure the trust policy is correct. |
| 75 | |
| 76 | ```bash |
| 77 | # Trust policy — includes aws:SourceAccount confused-deputy guard |
| 78 | cat > /tmp/sa-trust.json <<EOF |
| 79 | {"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"securityagent.amazonaws.com"},"Action":"sts:AssumeRole","Condition":{"StringEquals":{"aws:SourceAccount":"${ACCOUNT}"}}}]} |
| 80 | EOF |
| 81 | # Permissions policy (S3 + CloudWatch Logs) |
| 82 | cat > /tmp/sa-perms.json <<EOF |
| 83 | {"Version":"2012-10-17","Statement":[ |
| 84 | {"Effect":"Allow","Action":["s3:GetObject","s3:GetObjectVersion","s3:ListBucket"],"Resource":["arn:aws:s3:::security-agent-scans-${ACCOUNT}-${REGION}","arn:aws:s3:::security-agent-scans-${ACCOUNT}-${REGION}/*"]}, |
| 85 | {"Effect":"Allow","Action":["logs:CreateLogGroup","logs:CreateLogStream","logs:PutLogEvents"],"Resource":"arn:aws:logs:*:${ACCOUNT}:log-group:/aws/securityagent/*"} |
| 86 | ]} |
| 87 | EOF |
| 88 | |
| 89 | aws iam create-role --role-name SecurityAgentScanRole --assume-role-policy-document file:///tmp/sa-trust.json |
| 90 | # if EntityAlreadyExists: |
| 91 | aws iam update-assume-role-policy --role-name SecurityAgentScanRole --policy-document file:///tmp/sa-trust.json |
| 92 | # always (re)apply permissions: |
| 93 | aws iam put-role-policy --role-name SecurityAgentScanRole --policy-name SecurityAgentCodeReviewAccess --policy-document file:///tmp/sa-perms.json |
| 94 | ``` |
| 95 | |
| 96 | 5. **S3 bucket** (`security-agent-scans-$ACCOUNT-$REGION`): |
| 97 | - Probe: |
| 98 | |
| 99 | ```bash |
| 100 | BUCKET="security-a |