AWS Security Agent — Setup
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.
Local state convention
All Security Agent skills share workspace-local state at .security-agent/:
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.
scans.json — array of { scan_id, code_review_id, job_id, agent_space_id, scan_type, title, started_at, status, path } (keep last 50)
pentests.json — same shape, for pentest jobs
.gitignore — contents * so this directory stays untracked
findings-{scan_id}.md — written by the scan skill after each scan completes
This skill's job is to populate config.json and create .gitignore.
Derived values (convention over config)
Other skills compute these on each invocation rather than reading them from config.json:
| Value |
Convention |
ACCOUNT |
aws sts get-caller-identity --query Account --output text |
REGION |
config.region (default us-east-1) |
service_role_arn |
arn:aws:iam::${ACCOUNT}:role/SecurityAgentScanRole |
s3_bucket |
security-agent-scans-${ACCOUNT}-${REGION} |
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.
Workflow
Check existing state: read .security-agent/config.json if it exists.
Caller identity + region:
export ACCOUNT=$(aws sts get-caller-identity --query Account --output text)
export REGION="${AWS_REGION:-us-east-1}"
Agent space:
If config.agent_space_id is set, verify with:
aws securityagent batch-get-agent-spaces --agent-space-ids <id>
If the response shows it doesn't exist, treat as missing.
If missing, list existing:
aws securityagent list-agent-spaces
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.
If user picks one, use that agentSpaceId.
If user wants new, or none exist:
aws securityagent create-agent-space --name security-scans
Capture returned agentSpaceId.
Service role (SecurityAgentScanRole, ARN arn:aws:iam::$ACCOUNT:role/SecurityAgentScanRole):
Probe:
aws iam get-role --role-name SecurityAgentScanRole
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.
# Trust policy — includes aws:SourceAccount confused-deputy guard
cat > /tmp/sa-trust.json <<EOF
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"securityagent.amazonaws.com"},"Action":"sts:AssumeRole","Condition":{"StringEquals":{"aws:SourceAccount":"${ACCOUNT}"}}}]}
EOF
# Permissions policy (S3 + CloudWatch Logs)
cat > /tmp/sa-perms.json <<EOF
{"Version":"2012-10-17","Statement":[
{"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}/*"]},
{"Effect":"Allow","Action":["logs:CreateLogGroup","logs:CreateLogStream","logs:PutLogEvents"],"Resource":"arn:aws:logs:*:${ACCOUNT}:log-group:/aws/securityagent/*"}
]}
EOF
aws iam create-role --role-name SecurityAgentScanRole --assume-role-policy-document file:///tmp/sa-trust.json
# if EntityAlreadyExists:
aws iam update-assume-role-policy --role-name SecurityAgentScanRole --policy-document file:///tmp/sa-trust.json
# always (re)apply permissions:
aws iam put-role-policy --role-name SecurityAgentScanRole --policy-name SecurityAgentCodeReviewAccess --policy-document file:///tmp/sa-perms.json
S3 bucket (security-agent-scans-$ACCOUNT-$REGION):
Probe:
BUCKET="security-agent-scans-${ACCOUNT}-${REGION}"
aws s3api head-bucket --bucket "$BUCKET"
If 404, create:
# us-east-1: no LocationConstraint
aws s3api create-bucket --bucket "$BUCKET"
# other regions:
aws s3api create-bucket --bucket "$BUCKET" --create-bucket-configuration LocationConstraint="$REGION"
Always (re)apply public access block + 30-day lifecycle:
aws s3api put-public-access-block --bucket "$BUCKET" \
--public-access-block-configuration BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=true
cat > /tmp/sa-lifecycle.json <<'EOF'
{"Rules":[{"ID":"AutoDeleteUploads","Status":"Enabled","Filter":{"Prefix":""},"Expiration":{"Days":30}}]}
EOF
aws s3api put-bucket-lifecycle-configuration --bucket "$BUCKET" --lifecycle-configuration file:///tmp/sa-lifecycle.json
Register role + bucket on the agent space (idempotent):
Read existing resources:
aws securityagent batch-get-agent-spaces --agent-space-ids <id>
Look at agentSpaces[0].awsResources.iamRoles and awsResources.s3Buckets.
If the role ARN or the bucket name is missing from those lists, merge and update:
aws securityagent update-agent-space --agent-space-id <id> --name <existing-name> \
--aws-resources iamRoles=[<arn1>,<arn2>...],s3Buckets=[<bucket1>,<bucket2>...]
Persist to .security-agent/config.json (minimal — account/role/bucket are derived):
{
"agent_space_id": "as-xxxxx",
"region": "us-east-1"
}
Create gitignore if missing:
mkdir -p .security-agent
echo '*' > .security-agent/.gitignore
Confirm to user: "Setup complete. You can run security scans or pentests now."
Rules
- Never auto-select an agent space when multiple exist — always ask the user
- Never disable safety protections (the public-access-block stays on)
- Trust policy must allow
securityagent.amazonaws.com (production service principal) and include the aws:SourceAccount confused-deputy guard
- If the user provides their own role name or bucket name (different from the conventional defaults), tell them: this plugin uses convention-based defaults (
SecurityAgentScanRole / security-agent-scans-${ACCOUNT}-${REGION}). Either accept those defaults or extend the skill — the other skills derive these names rather than reading them from config.
- The scan and pentest skills can call this skill inline if
config.json is missing — first-time users don't need to run setup separately.
Troubleshooting
AccessDenied calling iam:CreateRole → user lacks IAM permissions. Ask them to run setup with their own role ARN, or to grant iam:CreateRole + iam:PutRolePolicy.
AccessDenied on s3api create-bucket → either the bucket name is taken globally, or the user lacks s3:CreateBucket. Suggest using an existing bucket they own and pass it explicitly.
- Role exists but trust policy is wrong →
update-assume-role-policy (step 4 fallback). If they don't want that role updated, ask them for a different role ARN.
- Agent space exists but in a different region → tell the user; suggest using the right region or creating a new space in the current region.
1---2name: setup-security-agent3description: Configure 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 pentest.4---5
6# AWS Security Agent — Setup
7
8This 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.
9
10---
11
12## Local state convention
13
14All Security Agent skills share workspace-local state at `.security-agent/`:
15
16- `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.
17- `scans.json` — array of `{ scan_id, code_review_id, job_id, agent_space_id, scan_type, title, started_at, status, path }` (keep last 50)
18- `pentests.json` — same shape, for pentest jobs
19- `.gitignore` — contents `*` so this directory stays untracked
20- `findings-{scan_id}.md` — written by the scan skill after each scan completes
21
22This skill's job is to populate `config.json` and create `.gitignore`.
23
24### Derived values (convention over config)
25
26Other skills compute these on each invocation rather than reading them from `config.json`:
27
28| Value | Convention |
29|-------|------------|
30| `ACCOUNT` | `aws sts get-caller-identity --query Account --output text` |
31| `REGION` | `config.region` (default `us-east-1`) |
32| `service_role_arn` | `arn:aws:iam::${ACCOUNT}:role/SecurityAgentScanRole` |
33| `s3_bucket` | `security-agent-scans-${ACCOUNT}-${REGION}` |
34
35Why 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.
36
37---
38
39## Workflow
40
411. **Check existing state:** read `.security-agent/config.json` if it exists.
422. **Caller identity + region:**
43
44 ```bash
45 export ACCOUNT=$(aws sts get-caller-identity --query Account --output text)
46 export REGION="${AWS_REGION:-us-east-1}"
47 ```
48
493. **Agent space:**
50 - If `config.agent_space_id` is set, verify with:
51
52 ```bash
53 aws securityagent batch-get-agent-spaces --agent-space-ids <id>
54 ```
55
56 If the response shows it doesn't exist, treat as missing.
57 - If missing, list existing:
58
59 ```bash
60 aws securityagent list-agent-spaces
61 ```
62
63 - 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.**
64 - If user picks one, use that `agentSpaceId`.
65 - If user wants new, or none exist:
66
67 ```bash
68 aws securityagent create-agent-space --name security-scans
69 ```
70
71 Capture returned `agentSpaceId`.
724. **Service role** (`SecurityAgentScanRole`, ARN `arn:aws:iam::$ACCOUNT:role/SecurityAgentScanRole`):
73 - Probe:
74
75 ```bash
76 aws iam get-role --role-name SecurityAgentScanRole
77 ```
78
79 - 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.
80
81 ```bash
82 # Trust policy — includes aws:SourceAccount confused-deputy guard
83 cat > /tmp/sa-trust.json <<EOF
84 {"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"securityagent.amazonaws.com"},"Action":"sts:AssumeRole","Condition":{"StringEquals":{"aws:SourceAccount":"${ACCOUNT}"}}}]}
85 EOF
86 # Permissions policy (S3 + CloudWatch Logs)
87 cat > /tmp/sa-perms.json <<EOF
88 {"Version":"2012-10-17","Statement":[
89 {"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}/*"]},
90 {"Effect":"Allow","Action":["logs:CreateLogGroup","logs:CreateLogStream","logs:PutLogEvents"],"Resource":"arn:aws:logs:*:${ACCOUNT}:log-group:/aws/securityagent/*"}
91 ]}
92 EOF
93
94 aws iam create-role --role-name SecurityAgentScanRole --assume-role-policy-document file:///tmp/sa-trust.json
95 # if EntityAlreadyExists:
96 aws iam update-assume-role-policy --role-name SecurityAgentScanRole --policy-document file:///tmp/sa-trust.json
97 # always (re)apply permissions:
98 aws iam put-role-policy --role-name SecurityAgentScanRole --policy-name SecurityAgentCodeReviewAccess --policy-document file:///tmp/sa-perms.json
99 ```
100
1015. **S3 bucket** (`security-agent-scans-$ACCOUNT-$REGION`):
102 - Probe:
103
104 ```bash
105 BUCKET="security-agent-scans-${ACCOUNT}-${REGION}"
106 aws s3api head-bucket --bucket "$BUCKET"
107 ```
108
109 - If 404, create:
110
111 ```bash
112 # us-east-1: no LocationConstraint
113 aws s3api create-bucket --bucket "$BUCKET"
114 # other regions:
115 aws s3api create-bucket --bucket "$BUCKET" --create-bucket-configuration LocationConstraint="$REGION"
116 ```
117
118 - Always (re)apply public access block + 30-day lifecycle:
119
120 ```bash
121 aws s3api put-public-access-block --bucket "$BUCKET" \
122 --public-access-block-configuration BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=true
123
124 cat > /tmp/sa-lifecycle.json <<'EOF'
125 {"Rules":[{"ID":"AutoDeleteUploads","Status":"Enabled","Filter":{"Prefix":""},"Expiration":{"Days":30}}]}
126 EOF
127 aws s3api put-bucket-lifecycle-configuration --bucket "$BUCKET" --lifecycle-configuration file:///tmp/sa-lifecycle.json
128 ```
129
1306. **Register role + bucket on the agent space (idempotent):**
131 - Read existing resources:
132
133 ```bash
134 aws securityagent batch-get-agent-spaces --agent-space-ids <id>
135 ```
136
137 Look at `agentSpaces[0].awsResources.iamRoles` and `awsResources.s3Buckets`.
138 - If the role ARN or the bucket name is missing from those lists, merge and update:
139
140 ```bash
141 aws securityagent update-agent-space --agent-space-id <id> --name <existing-name> \
142 --aws-resources iamRoles=[<arn1>,<arn2>...],s3Buckets=[<bucket1>,<bucket2>...]
143 ```
144
1457. **Persist** to `.security-agent/config.json` (minimal — account/role/bucket are derived):
146
147 ```json
148 {
149 "agent_space_id": "as-xxxxx",
150 "region": "us-east-1"
151 }
152 ```
153
1548. **Create gitignore** if missing:
155
156 ```bash
157 mkdir -p .security-agent
158 echo '*' > .security-agent/.gitignore
159 ```
160
1619. Confirm to user: "Setup complete. You can run security scans or pentests now."
162
163---
164
165## Rules
166
167- Never auto-select an agent space when multiple exist — always ask the user
168- Never disable safety protections (the public-access-block stays on)
169- Trust policy must allow `securityagent.amazonaws.com` (production service principal) and include the `aws:SourceAccount` confused-deputy guard
170- If the user provides their own role name or bucket name (different from the conventional defaults), tell them: this plugin uses convention-based defaults (`SecurityAgentScanRole` / `security-agent-scans-${ACCOUNT}-${REGION}`). Either accept those defaults or extend the skill — the other skills derive these names rather than reading them from config.
171- The scan and pentest skills can call this skill inline if `config.json` is missing — first-time users don't need to run setup separately.
172
173---
174
175## Troubleshooting
176
177- **`AccessDenied` calling `iam:CreateRole`** → user lacks IAM permissions. Ask them to run setup with their own role ARN, or to grant `iam:CreateRole` + `iam:PutRolePolicy`.
178- **`AccessDenied` on `s3api create-bucket`** → either the bucket name is taken globally, or the user lacks `s3:CreateBucket`. Suggest using an existing bucket they own and pass it explicitly.
179- **Role exists but trust policy is wrong** → `update-assume-role-policy` (step 4 fallback). If they don't want that role updated, ask them for a different role ARN.
180- **Agent space exists but in a different region** → tell the user; suggest using the right region or creating a new space in the current region.