-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy path13-mcp-policy.sh
More file actions
executable file
·191 lines (161 loc) · 6.36 KB
/
13-mcp-policy.sh
File metadata and controls
executable file
·191 lines (161 loc) · 6.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/bin/bash
# Deploy Cedar policies to MCP Gateway
# Creates: Policy Engine, Permit all, Forbid cancelTrip, IAM permissions, Gateway attachment
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Load environment
if [ -f "$SCRIPT_DIR/.env" ]; then
source "$SCRIPT_DIR/.env"
fi
# Set defaults if not in .env
AWS_REGION=${AWS_REGION:-$(aws configure get region)}
ACCOUNT_ID=${ACCOUNT_ID:-$(aws sts get-caller-identity --query Account --output text)}
# Get gateway ID if not set
if [ -z "$MCP_GATEWAY_ID" ]; then
MCP_GATEWAY_ID=$(aws bedrock-agentcore-control list-gateways --query 'items[0].gatewayId' --output text)
fi
GATEWAY_ARN="arn:aws:bedrock-agentcore:${AWS_REGION}:${ACCOUNT_ID}:gateway/${MCP_GATEWAY_ID}"
echo "Deploying Cedar policies..."
echo " Region: ${AWS_REGION}"
echo " Gateway: ${MCP_GATEWAY_ID}"
echo ""
# Step 1: Create or get Policy Engine
echo "1. Setting up Policy Engine..."
POLICY_ENGINE_ID=$(aws bedrock-agentcore-control list-policy-engines \
--query 'policyEngines[?name==`BackofficePolicyEngine`].policyEngineId' --output text 2>/dev/null || true)
if [ -z "$POLICY_ENGINE_ID" ]; then
echo " Creating new Policy Engine..."
POLICY_ENGINE_ID=$(aws bedrock-agentcore-control create-policy-engine \
--name "BackofficePolicyEngine" \
--query "policyEngineId" --output text)
echo -n " Waiting for ACTIVE"
while [ "$(aws bedrock-agentcore-control get-policy-engine --policy-engine-id "${POLICY_ENGINE_ID}" \
--query 'status' --output text)" != "ACTIVE" ]; do
echo -n "."; sleep 3
done
echo " Done"
else
echo " Using existing Policy Engine: ${POLICY_ENGINE_ID}"
fi
POLICY_ENGINE_ARN="arn:aws:bedrock-agentcore:${AWS_REGION}:${ACCOUNT_ID}:policy-engine/${POLICY_ENGINE_ID}"
# Step 2: Delete existing policies
echo ""
echo "2. Cleaning up existing policies..."
EXISTING=$(aws bedrock-agentcore-control list-policies \
--policy-engine-id "${POLICY_ENGINE_ID}" \
--query 'policies[].policyId' --output text 2>/dev/null || true)
for PID in $EXISTING; do
echo " Deleting: $PID"
aws bedrock-agentcore-control delete-policy \
--policy-engine-id "${POLICY_ENGINE_ID}" \
--policy-id "$PID" > /dev/null
while true; do
STATUS=$(aws bedrock-agentcore-control get-policy \
--policy-engine-id "${POLICY_ENGINE_ID}" \
--policy-id "$PID" \
--query 'status' --output text 2>/dev/null || echo "DELETED")
[ "$STATUS" = "DELETED" ] || [ -z "$STATUS" ] && break
sleep 2
done
done
echo " Done"
# Step 3: Create permit policy (Cedar is deny-by-default)
echo ""
echo "3. Creating permit policy..."
PERMIT_STATEMENT="permit (principal, action, resource == AgentCore::Gateway::\"${GATEWAY_ARN}\");"
PERMIT_ID=$(aws bedrock-agentcore-control create-policy \
--policy-engine-id "${POLICY_ENGINE_ID}" \
--name "PermitAllActions" \
--validation-mode "IGNORE_ALL_FINDINGS" \
--definition "{\"cedar\":{\"statement\":$(echo "$PERMIT_STATEMENT" | jq -Rs .)}}" \
--query 'policyId' --output text)
echo " Policy ID: $PERMIT_ID"
# Step 4: Create forbid policy
echo ""
echo "4. Creating forbid policy..."
FORBID_STATEMENT="forbid (principal, action == AgentCore::Action::\"backoffice___cancelTrip\", resource == AgentCore::Gateway::\"${GATEWAY_ARN}\");"
FORBID_ID=$(aws bedrock-agentcore-control create-policy \
--policy-engine-id "${POLICY_ENGINE_ID}" \
--name "ForbidCancelTrip" \
--validation-mode "IGNORE_ALL_FINDINGS" \
--definition "{\"cedar\":{\"statement\":$(echo "$FORBID_STATEMENT" | jq -Rs .)}}" \
--query 'policyId' --output text)
echo " Policy ID: $FORBID_ID"
# Step 5: Wait for policies to become ACTIVE
echo ""
echo "5. Waiting for policies to become ACTIVE..."
for PID in "$PERMIT_ID" "$FORBID_ID"; do
for i in {1..30}; do
STATUS=$(aws bedrock-agentcore-control get-policy \
--policy-engine-id "${POLICY_ENGINE_ID}" \
--policy-id "$PID" \
--query 'status' --output text)
if [ "$STATUS" = "ACTIVE" ]; then
echo " $PID: ACTIVE"
break
elif [[ "$STATUS" == *"FAILED"* ]]; then
echo "❌ Policy $PID failed"
exit 1
fi
sleep 2
done
done
# Step 6: Add IAM permissions for gateway role
echo ""
echo "6. Adding IAM permissions..."
cat > /tmp/policy-permissions.json << EOF
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": "bedrock-agentcore:*",
"Resource": [
"${GATEWAY_ARN}",
"${POLICY_ENGINE_ARN}",
"${POLICY_ENGINE_ARN}/*"
]
}]
}
EOF
aws iam put-role-policy \
--role-name mcp-gateway-role \
--policy-name PolicyEngineAccess \
--policy-document file:///tmp/policy-permissions.json
echo " Waiting for IAM propagation..."
sleep 10
# Step 7: Attach policy engine to gateway
echo ""
echo "7. Attaching policy engine to gateway..."
MCP_GATEWAY_ROLE_ARN=$(aws bedrock-agentcore-control get-gateway \
--gateway-id "${MCP_GATEWAY_ID}" --query 'roleArn' --output text)
aws bedrock-agentcore-control update-gateway \
--gateway-identifier "${MCP_GATEWAY_ID}" \
--name "mcp-gateway" \
--role-arn "${MCP_GATEWAY_ROLE_ARN}" \
--protocol-type "MCP" \
--authorizer-type "AWS_IAM" \
--policy-engine-configuration "{\"arn\":\"${POLICY_ENGINE_ARN}\",\"mode\":\"ENFORCE\"}" \
--no-cli-pager > /dev/null
echo -n " Waiting for gateway READY"
while [ "$(aws bedrock-agentcore-control get-gateway --gateway-id "${MCP_GATEWAY_ID}" \
--query 'status' --output text)" != "READY" ]; do
echo -n "."; sleep 3
done
echo " Done"
# Save to .env for cleanup script
if ! grep -q "MCP_POLICY_ENGINE_ID" "$SCRIPT_DIR/.env" 2>/dev/null; then
echo "" >> "$SCRIPT_DIR/.env"
echo "# Policy Engine" >> "$SCRIPT_DIR/.env"
echo "MCP_POLICY_ENGINE_ID=${POLICY_ENGINE_ID}" >> "$SCRIPT_DIR/.env"
fi
echo ""
echo "✅ Policies deployed successfully!"
echo ""
echo "Policies:"
aws bedrock-agentcore-control list-policies \
--policy-engine-id "${POLICY_ENGINE_ID}" \
--query 'policies[].{Name:name,Status:status}' --output table --no-cli-pager
echo ""
echo "Gateway:"
aws bedrock-agentcore-control get-gateway --gateway-id "${MCP_GATEWAY_ID}" \
--query '{PolicyEngine:policyEngineConfiguration.arn,Mode:policyEngineConfiguration.mode}' --output table --no-cli-pager