How to add cloudsql role to a service account via cloud-deployment-manager

Viewed 254

I'm trying to add the cloudsq.client role to a service account via the cloud deployment manager. How do i do this?

I figured out that i can add primitive roles like role/owner with the pubsub.v1.topic resourceType. See the official google example:

resources:
- name: {{ env['name'] }}
  type: pubsub.v1.topic
  properties:
    topic: {{ env['name'] }}
  accessControl:
    gcpIamPolicy:
      bindings:
      - role: roles/pubsub.subscriber
        members:
        - "serviceAccount:$(ref.{{ properties['serviceAccountId'] }}.email)"

But it seems like that this doesn't work with role/cloudsql.client: It says:

"message":"Role roles/cloudsql.client is not supported for this resource."

I figured out through Can't create cloudsql role for Service Account via api that i most likely have to use the cloudresourcemanager.v1.project resource. But just replacing the resources doesn't work either:

resources:
- name: {{ env['name'] }}
  type: cloudresourcemanager.v1.project
  properties:
    projectId: {{ env['project'] }}
  accessControl:
    gcpIamPolicy:
      bindings:
      - role: roles/cloudsql.client
        members:
        - "serviceAccount:$(ref.{{ properties['serviceAccountId'] }}.email)"

Error:

- code: RESOURCE_ERROR
  location: /deployments/test-cluster/resources/cloudsql-client-role
  message: '{"ResourceType":"cloudresourcemanager.v1.project","ResourceErrorCode":"403","ResourceErrorMessage":{"code":403,"message":"Service
    accounts cannot create projects without a parent.","status":"PERMISSION_DENIED","statusMessage":"Forbidden","requestPath":"https://cloudresourcemanager.googleapis.com/v1/projects","httpMethod":"POST"}}'

Im kinda stuck so i appreciate every help i can get!

1 Answers

To fix this you need to use the following type in your "resources": https://github.com/GoogleCloudPlatform/deploymentmanager-samples/blob/ae293a455f90746fb2e25142dbc11250cc51aad3/community/cloud-foundation/templates/iam_member/iam_member.py#L30

The correct way to add a role to a service account is: getIamPolicy > setIamPolicy

First you need to get the policies then you will be able to set the policies, this process is called "binding".

Please use the following template to add roles to service accounts:

https://github.com/GoogleCloudPlatform/deploymentmanager-samples/tree/master/community/cloud-foundation/templates/iam_member

If you have further questions I would be glad to help you.

Related