Skip to content

Commit e2865ea

Browse files
authored
fix: add organization-level IAM permissions for GCP deployments (#3930)
Fixes GCP Infrastructure Manager deployments for organization-level CSPM by adding support for granting organization-scoped IAM permissions. The setup scripts for both gcp-credentials-json and gcp-elastic-agent now accept an optional organization ID parameter, and when provided, grant the necessary roles/iam.securityAdmin role at the organization level. This enables proper organization-wide security scanning capabilities when deploying Elastic Agent or service account credentials for GCP CSPM.
1 parent d29e5e1 commit e2865ea

File tree

6 files changed

+60
-4
lines changed

6 files changed

+60
-4
lines changed

deploy/infrastructure-manager/gcp-credentials-json/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@ After successful deployment, the script saves the service account credentials to
6868

6969
> **Note:** The key is also stored in Secret Manager for future access. The script outputs the `gcloud` command to retrieve it if needed.
7070
71+
### Required Permissions
72+
73+
The deployment service account needs these roles:
74+
- `roles/iam.serviceAccountAdmin` - Create and manage service accounts
75+
- `roles/iam.serviceAccountKeyAdmin` - Create service account keys
76+
- `roles/resourcemanager.projectIamAdmin` - Manage project-level IAM bindings
77+
- `roles/config.admin` - Infrastructure Manager operations
78+
- `roles/storage.admin` - Store Terraform state
79+
- `roles/secretmanager.admin` - Create and manage secrets
80+
81+
For organization-level deployments (when `ORG_ID` is set), you also need:
82+
- `roles/iam.securityAdmin` - Manage organization IAM bindings (granted at organization level)
83+
7184
### Management
7285

7386
**View deployment:**

deploy/infrastructure-manager/gcp-credentials-json/deploy.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ PROJECT_ID=$(gcloud config get-value core/project)
1515
SERVICE_ACCOUNT="infra-manager-deployer"
1616

1717
# Ensure prerequisites are configured
18-
"${SCRIPT_DIR}/setup.sh" "${PROJECT_ID}" "${SERVICE_ACCOUNT}"
18+
"${SCRIPT_DIR}/setup.sh" "${PROJECT_ID}" "${SERVICE_ACCOUNT}" "${ORG_ID}"
1919

2020
# Optional environment variables (defaults are in variables.tf or below)
2121
# ORG_ID - Set for org-level monitoring

deploy/infrastructure-manager/gcp-credentials-json/setup.sh

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ set -e
44
# Accept parameters
55
PROJECT_ID="$1"
66
SERVICE_ACCOUNT="$2"
7+
ORG_ID="$3" # Optional: required for organization-scope deployments
78
SERVICE_ACCOUNT_EMAIL="${SERVICE_ACCOUNT}@${PROJECT_ID}.iam.gserviceaccount.com"
89

910
REQUIRED_APIS=(
@@ -23,6 +24,10 @@ REQUIRED_ROLES=(
2324
roles/secretmanager.admin
2425
)
2526

27+
ORG_LEVEL_ROLES=(
28+
roles/iam.securityAdmin
29+
)
30+
2631
echo "Setting up GCP Infrastructure Manager prerequisites..."
2732

2833
# Enable APIs
@@ -34,11 +39,21 @@ if ! gcloud iam service-accounts describe "${SERVICE_ACCOUNT_EMAIL}" >/dev/null
3439
--display-name="Infra Manager Deployment Account" --quiet
3540
fi
3641

37-
# Grant permissions
42+
# Grant project-level permissions
3843
for role in "${REQUIRED_ROLES[@]}"; do
3944
gcloud projects add-iam-policy-binding "${PROJECT_ID}" \
4045
--member="serviceAccount:${SERVICE_ACCOUNT_EMAIL}" \
4146
--role="${role}" --condition=None --quiet >/dev/null
4247
done
4348

49+
# Grant organization-level permissions if ORG_ID is provided
50+
if [ -n "${ORG_ID}" ]; then
51+
echo "Granting organization-level permissions for org ${ORG_ID}..."
52+
for role in "${ORG_LEVEL_ROLES[@]}"; do
53+
gcloud organizations add-iam-policy-binding "${ORG_ID}" \
54+
--member="serviceAccount:${SERVICE_ACCOUNT_EMAIL}" \
55+
--role="${role}" --condition=None --quiet >/dev/null
56+
done
57+
fi
58+
4459
echo "✓ Setup complete"

deploy/infrastructure-manager/gcp-elastic-agent/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,19 @@ gcloud compute instances get-guest-attributes ${INSTANCE_NAME} \
9797
--query-path=elastic-agent/
9898
```
9999

100+
### Required Permissions
101+
102+
The deployment service account needs these roles:
103+
- `roles/compute.admin` - Create and manage compute instances
104+
- `roles/iam.serviceAccountAdmin` - Create and manage service accounts
105+
- `roles/iam.serviceAccountUser` - Attach service accounts to instances
106+
- `roles/resourcemanager.projectIamAdmin` - Manage project-level IAM bindings
107+
- `roles/config.admin` - Infrastructure Manager operations
108+
- `roles/storage.admin` - Store Terraform state
109+
110+
For organization-level deployments (when `ORG_ID` is set), you also need:
111+
- `roles/iam.securityAdmin` - Manage organization IAM bindings (granted at organization level)
112+
100113
### Management
101114

102115
**View deployment:**

deploy/infrastructure-manager/gcp-elastic-agent/deploy.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ PROJECT_ID=$(gcloud config get-value core/project)
99
SERVICE_ACCOUNT="infra-manager-deployer"
1010

1111
# Ensure prerequisites are configured
12-
"${SCRIPT_DIR}/setup.sh" "${PROJECT_ID}" "${SERVICE_ACCOUNT}"
12+
"${SCRIPT_DIR}/setup.sh" "${PROJECT_ID}" "${SERVICE_ACCOUNT}" "${ORG_ID}"
1313

1414
# Required environment variables (no defaults - must be provided)
1515
# FLEET_URL, ENROLLMENT_TOKEN, STACK_VERSION

deploy/infrastructure-manager/gcp-elastic-agent/setup.sh

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ set -e
44
# Accept parameters
55
PROJECT_ID="$1"
66
SERVICE_ACCOUNT="$2"
7+
ORG_ID="$3" # Optional: required for organization-scope deployments
78
SERVICE_ACCOUNT_EMAIL="${SERVICE_ACCOUNT}@${PROJECT_ID}.iam.gserviceaccount.com"
89

910
REQUIRED_APIS=(
@@ -23,6 +24,10 @@ REQUIRED_ROLES=(
2324
roles/storage.admin
2425
)
2526

27+
ORG_LEVEL_ROLES=(
28+
roles/iam.securityAdmin
29+
)
30+
2631
echo "Setting up GCP Infrastructure Manager prerequisites..."
2732

2833
# Enable APIs
@@ -34,11 +39,21 @@ if ! gcloud iam service-accounts describe "${SERVICE_ACCOUNT_EMAIL}" >/dev/null
3439
--display-name="Infra Manager Deployment Account" --quiet
3540
fi
3641

37-
# Grant permissions
42+
# Grant project-level permissions
3843
for role in "${REQUIRED_ROLES[@]}"; do
3944
gcloud projects add-iam-policy-binding "${PROJECT_ID}" \
4045
--member="serviceAccount:${SERVICE_ACCOUNT_EMAIL}" \
4146
--role="${role}" --condition=None --quiet >/dev/null
4247
done
4348

49+
# Grant organization-level permissions if ORG_ID is provided
50+
if [ -n "${ORG_ID}" ]; then
51+
echo "Granting organization-level permissions for org ${ORG_ID}..."
52+
for role in "${ORG_LEVEL_ROLES[@]}"; do
53+
gcloud organizations add-iam-policy-binding "${ORG_ID}" \
54+
--member="serviceAccount:${SERVICE_ACCOUNT_EMAIL}" \
55+
--role="${role}" --condition=None --quiet >/dev/null
56+
done
57+
fi
58+
4459
echo "✓ Setup complete"

0 commit comments

Comments
 (0)