Unfortunately, there is no way to list all users using the
gcloud iam . . .
command tree; however, we are able to list all accounts under a Google Cloud Platform (GCP) project ($GCP_PROJECT_NAME
) through the
gcloud projects get-iam-policy
command tree instead:
gcloud projects get-iam-policy $GCP_PROJECT_NAME \
--flatten="bindings[].members" \
--format="value(bindings.members)" \
--sort-by=bindings.members | uniq
#=>
. . .
serviceAccount:$SOME_SERVICE_ACCOUNT
. . .
user:$SOME_USER
. . .
which includes piping any duplicate results though uniq
.
Note: the above command is guaranteed to list all accounts associated with $GCP_PROJECT_NAME
because every account has to have at least one role:
gcloud projects add-iam-policy-binding $ANOTHER_USER \
--member="user:${ANOTHER_USER}"
#=>
ERROR: (gcloud.projects.add-iam-policy-binding) argument --role: Must be specified.
If necessary, we can make use of the .flatten()
, .slice()
and .split()
gcloud
projections to get rid of the serviceAccount:
and user:
prefixes:
gcloud projects get-iam-policy $GCP_PROJECT_NAME \
--flatten="bindings[].members" \
--format="value(bindings.members.split(':').slice(1:).flatten())" \
--sort-by=bindings.members | uinq
#=>
. . .
$SOME_SERVICE_ACCOUNT
. . .
$SOME_USER
. . .
More on gcloud
projections here.
We can also filter this result using the --filter
flag:
gcloud projects get-iam-policy $GCP_PROJECT_NAME \
--filter="user" \
--flatten="bindings[].members" \
--format="value(bindings.members.split(':').slice(1:).flatten())" \
--sort-by=bindings.members | uniq
#=>
. . .
$SOME_USER
. . .
and:
gcloud projects get-iam-policy $GCP_PROJECT_NAME \
--filter="serviceAccount" \
--flatten="bindings[].members" \
--format="value(bindings.members.split(':').slice(1:).flatten())" \
--sort-by=bindings.members | uniq
#=>
. . .
$SOME_SERVICE_ACCOUNT
. . .