A cloud engineer needs to configure access for an application running on a Compute Engine VM in project `prod-app-project`. The application must publish messages to a Cloud Pub/Sub topic in the same project. Following Google Cloud security best practices, access must follow the principle of least privilege without generating long-lived service account keys. Which set of `gcloud` commands correctly creates the service account and grants the minimum required access?
- Execute `gcloud iam service-accounts create app-runner-sa --display-name="App Runner SA"` and then run `gcloud projects add-iam-policy-binding prod-app-project --member="serviceAccount:[email protected]" --role="roles/pubsub.publisher"`.Answer
- BExecute `gcloud iam service-accounts create app-runner-sa --display-name="App Runner SA"` and then run `gcloud projects add-iam-policy-binding prod-app-project --member="serviceAccount:[email protected]" --role="roles/editor"`.
- CExecute `gcloud iam service-accounts create app-runner-sa --display-name="App Runner SA"` and then run `gcloud iam service-accounts keys create key.json --iam-account=app-runner-sa@prod-app-project.iam.gserviceaccount.com` to download and attach service account credentials to the VM.
- DExecute `gcloud organizations add-iam-policy-binding MY_ORG_ID --member="serviceAccount:[email protected]" --role="roles/pubsub.admin"` to ensure cross-resource inheritance.
Answer
Create the custom service account using `gcloud iam service-accounts create` and grant the specific predefined role `roles/pubsub.publisher` using `gcloud projects add-iam-policy-binding`.
Creating a dedicated service account and binding the `roles/pubsub.publisher` predefined role directly at the project level complies with security mandates by adhering to the principle of least privilege. Furthermore, attaching this service account directly to the VM eliminates the security risks associated with exported service account key files.
Step-by-Step Solution
Key Concept
Creating User-Managed Service Accounts and Assigning Predefined Least-Privilege Roles via gcloud
Estimated Time:1m 30s