Skip to content

Commit ca3ad32

Browse files
nupurg-ibmNupur Goyal
andauthored
Updating permissions script (#290)
* fixing permissions for scc wp * removing scale * removing scale * fixing pre-commit * fixing pre-commit --------- Co-authored-by: Nupur Goyal <nupurgoyal@Nupurs-MacBook-Pro.local>
1 parent cd28cb7 commit ca3ad32

File tree

2 files changed

+87
-16
lines changed

2 files changed

+87
-16
lines changed

.secrets.baseline

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"files": "^.secrets.baseline$",
44
"lines": null
55
},
6-
"generated_at": "2025-10-29T15:24:27Z",
6+
"generated_at": "2025-11-07T08:08:33Z",
77
"plugins_used": [
88
{
99
"name": "AWSKeyDetector"

tools/access-management/permissions.sh

Lines changed: 86 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ check_policies() {
5555
select(any(.resources[].attributes[]?; .name == "serviceType" and .value == "service"))
5656
' >/dev/null 2>&1 && echo "true" || echo "false")
5757

58-
# Check role for serviceType=platform_service (Viewer, Editor, or Administrator)
58+
# Check role for serviceType=platform_service (Administrator)
5959
local has_platform_role
6060
has_platform_role=$(echo "$policies" | jq -e '
6161
.[] |
6262
select(.roles? != null) |
63-
select(any(.roles[]?.display_name; . == "Viewer" or . == "Editor" or . == "Administrator")) |
63+
select(any(.roles[]?.display_name; . == "Administrator")) |
6464
select(any(.resources[].attributes[]?; .name == "accountId")) |
6565
select(any(.resources[].attributes[]?; .name == "serviceType" and .value == "platform_service"))
6666
' >/dev/null 2>&1 && echo "true" || echo "false")
@@ -89,15 +89,16 @@ check_policies() {
8989
fi
9090

9191
if [ "$has_platform_role" = "true" ]; then
92-
echo "✅ At $scope policy level: Has Viewer/Editor/Administrator for All Account Management services"
92+
echo "✅ At $scope policy level: Has Administrator for All Account Management services"
9393
else
94-
echo "❌ At $scope policy level: Missing Viewer/Editor/Administrator for All Account Management services"
94+
echo "❌ At $scope policy level: Missing Administrator for All Account Management services"
9595
fi
9696

9797
[[ "$has_admin" == "true" && "$has_platform_role" == "true" && "$has_identity_role" == "true" ]]
9898
}
9999

100100
USER_POLICIES=$(ibmcloud iam user-policies "$ADMIN_EMAIL" --output json 2>/dev/null || echo "[]")
101+
101102
if echo "$USER_POLICIES" | jq empty 2>/dev/null; then
102103
if check_policies "$USER_POLICIES" "User"; then
103104
has_permission=true
@@ -136,10 +137,7 @@ kms|Service Configuration Reader|Manager
136137
secrets-manager|Administrator|Manager
137138
sysdig-secure|Administrator|
138139
is|Editor|
139-
iam-identity|Administrator|
140-
atracker|Administrator|
141-
logs-router|Administrator|
142-
metrics-router|Administrator|"
140+
iam-identity|Administrator|"
143141

144142
FRIENDLY_NAMES="apprapp|App Configuration
145143
cloud-object-storage|Cloud Object Storage
@@ -149,10 +147,7 @@ kms|Key Protect
149147
secrets-manager|Secrets Manager
150148
sysdig-secure|Security and Compliance Center Workload Protection
151149
is|VPC Infrastructure Services
152-
iam-identity|IAM Identity
153-
atracker|Activity tracker event routing
154-
logs-router|Cloud logs routing
155-
metrics-router|Metrics routing"
150+
iam-identity|IAM Identity"
156151

157152
get_friendly_name() {
158153
local service="$1"
@@ -185,8 +180,10 @@ if [ -n "$ACCESS_GROUP" ] && [ -z "$USER_EMAIL" ]; then
185180

186181
POLICY_ID=$(echo "$existing_policies" | jq -r \
187182
--arg service "$SERVICE_NAME" '
188-
.[] | select(any(.resources[].attributes[]?;
189-
.name == "serviceName" and .value == $service)) |
183+
.[] |
184+
select(any(.resources[].attributes[]?;
185+
.name == "serviceName" and .value == $service)) |
186+
select(all(.resources[].attributes[]?.name; . != "resourceGroupId")) |
190187
.id' | head -n1)
191188

192189
if [ -n "$POLICY_ID" ] && [ "$POLICY_ID" != "null" ]; then
@@ -224,8 +221,8 @@ if [ -n "$ACCESS_GROUP" ] && [ -z "$USER_EMAIL" ]; then
224221
POLICY_ID=$(echo "$existing_policies" | jq -r '
225222
.[] |
226223
select(any(.resources[].attributes[]?; .name == "serviceType" and .value == "service")) |
224+
select(all(.resources[].attributes[]?.name; . != "resourceGroupId")) |
227225
.id' | head -n1)
228-
229226
if [ -n "$POLICY_ID" ] && [ "$POLICY_ID" != "null" ]; then
230227
EXISTING_ROLES=$(echo "$existing_policies" | jq -r --arg id "$POLICY_ID" '
231228
.[] | select(.id == $id) | [.roles[].display_name] | join(",")')
@@ -253,6 +250,43 @@ if [ -n "$ACCESS_GROUP" ] && [ -z "$USER_EMAIL" ]; then
253250
--roles "Administrator,Manager" || echo "⚠️ Failed to assign Administrator,Manager roles for All Identity and Access enabled services to access group: $ACCESS_GROUP"
254251
fi
255252

253+
echo "🔍 Checking All Account Management Administrator policy for access group: $ACCESS_GROUP"
254+
existing_policies=$(ibmcloud iam access-group-policies "$ACCESS_GROUP" --output json 2>/dev/null || echo "[]")
255+
POLICY_ID=$(echo "$existing_policies" | jq -r '
256+
.[] |
257+
select(any(.resources[].attributes[]?; .name == "serviceType" and .value == "platform_service")) |
258+
select(all(.resources[].attributes[]?.name; . != "resourceGroupId")) |
259+
.id' | head -n1)
260+
261+
if [ -n "$POLICY_ID" ] && [ "$POLICY_ID" != "null" ]; then
262+
EXISTING_ROLES=$(echo "$existing_policies" | jq -r --arg id "$POLICY_ID" '
263+
.[] | select(.id == $id) | [.roles[].display_name] | join(",")')
264+
265+
EXISTING_SORTED=$(normalize_roles "$EXISTING_ROLES")
266+
MERGED_SORTED=$(normalize_roles "$EXISTING_ROLES,Administrator")
267+
268+
if [ "$MERGED_SORTED" = "$EXISTING_SORTED" ]; then
269+
echo "✅ All Account Management Administrator policy already present with required roles for access group: $ACCESS_GROUP"
270+
else
271+
NEW_ROLES=$(comm -13 \
272+
<(echo "$EXISTING_SORTED" | tr ',' '\n' | sort) \
273+
<(echo "$MERGED_SORTED" | tr ',' '\n' | sort) | paste -sd, -)
274+
275+
echo "🔄 Updating account management policy $POLICY_ID for access group: $ACCESS_GROUP"
276+
echo " • Current roles : $EXISTING_SORTED"
277+
echo " • Adding roles : $NEW_ROLES"
278+
279+
ibmcloud iam access-group-policy-update "$ACCESS_GROUP" "$POLICY_ID" \
280+
--account-management \
281+
--roles "$MERGED_SORTED" || echo "⚠️ Failed to update Administrator roles for All Account Management Administrator services to access group: $ACCESS_GROUP"
282+
fi
283+
else
284+
echo "➕ Creating new all account management Administrator policy for access group: $ACCESS_GROUP"
285+
ibmcloud iam access-group-policy-create "$ACCESS_GROUP" \
286+
--account-management \
287+
--roles "Administrator" || echo "⚠️ Failed to assign Administrator roles for All Account Management Administrator services to access group: $ACCESS_GROUP"
288+
fi
289+
256290
elif [ -z "$ACCESS_GROUP" ] && [ -n "$USER_EMAIL" ]; then
257291
echo "👤 Assigning roles to user: $USER_EMAIL"
258292
echo "$PERMISSIONS_LIST" | while IFS='|' read -r SERVICE_NAME PLATFORM_ROLE SERVICE_ROLE; do
@@ -267,6 +301,7 @@ elif [ -z "$ACCESS_GROUP" ] && [ -n "$USER_EMAIL" ]; then
267301
.[]
268302
| select(any(.resources[].attributes[]?;
269303
.name == "serviceName" and .value == $service))
304+
| select(all(.resources[].attributes[]?.name; . != "resourceGroupId"))
270305
| .id' | head -n1)
271306

272307
if [ -n "$POLICY_ID" ] && [ "$POLICY_ID" != "null" ]; then
@@ -304,6 +339,7 @@ elif [ -z "$ACCESS_GROUP" ] && [ -n "$USER_EMAIL" ]; then
304339
POLICY_ID=$(echo "$existing_policies" | jq -r '
305340
.[] |
306341
select(any(.resources[].attributes[]?; .name == "serviceType" and .value == "service")) |
342+
select(all(.resources[].attributes[]?.name; . != "resourceGroupId")) |
307343
.id' | head -n1)
308344
if [ -n "$POLICY_ID" ] && [ "$POLICY_ID" != "null" ]; then
309345
EXISTING_ROLES=$(echo "$existing_policies" | jq -r --arg id "$POLICY_ID" '
@@ -332,6 +368,41 @@ elif [ -z "$ACCESS_GROUP" ] && [ -n "$USER_EMAIL" ]; then
332368
--roles "Administrator,Manager" || echo "⚠️ Failed to assign Administrator,Manager roles for All Identity and Access enabled services to user: $USER_EMAIL"
333369
fi
334370

371+
echo "🔍 Checking All Account Management Administrator policy for $USER_EMAIL"
372+
existing_policies=$(ibmcloud iam user-policies "$USER_EMAIL" --output json 2>/dev/null || echo "[]")
373+
POLICY_ID=$(echo "$existing_policies" | jq -r '
374+
.[] |
375+
select(any(.resources[].attributes[]?; .name == "serviceType" and .value == "platform_service")) |
376+
select(all(.resources[].attributes[]?.name; . != "resourceGroupId")) |
377+
.id' | head -n1)
378+
if [ -n "$POLICY_ID" ] && [ "$POLICY_ID" != "null" ]; then
379+
EXISTING_ROLES=$(echo "$existing_policies" | jq -r --arg id "$POLICY_ID" '
380+
.[] | select(.id == $id) | [.roles[].display_name] | join(",")')
381+
382+
EXISTING_SORTED=$(normalize_roles "$EXISTING_ROLES")
383+
MERGED_SORTED=$(normalize_roles "$EXISTING_ROLES,Administrator")
384+
385+
if [ "$MERGED_SORTED" = "$EXISTING_SORTED" ]; then
386+
echo "✅ All Account Management Administrator policy already present with required roles for $USER_EMAIL"
387+
else
388+
NEW_ROLES=$(comm -13 \
389+
<(echo "$EXISTING_SORTED" | tr ',' '\n' | sort) \
390+
<(echo "$MERGED_SORTED" | tr ',' '\n' | sort) | paste -sd, -)
391+
392+
echo "🔄 Updating account management policy $POLICY_ID for $USER_EMAIL"
393+
echo " • Current roles : $EXISTING_SORTED"
394+
echo " • Adding roles : $NEW_ROLES"
395+
396+
ibmcloud iam user-policy-update "$USER_EMAIL" "$POLICY_ID" \
397+
--roles "$MERGED_SORTED" || echo "⚠️ Failed to update Administrator roles for All Account Management services to user: $USER_EMAIL"
398+
fi
399+
else
400+
echo "➕ Creating new All Account Management Administrator policy for $USER_EMAIL"
401+
ibmcloud iam user-policy-create "$USER_EMAIL" \
402+
--account-management \
403+
--roles "Administrator" || echo "⚠️ Failed to assign Administrator roles for All Account Management services to user: $USER_EMAIL"
404+
fi
405+
335406
else
336407
echo "❗ Please choose either Access Group or User."
337408
exit 1

0 commit comments

Comments
 (0)