谷歌部署经理在项目中分配IAM政策

时间:2018-02-09 16:50:30

标签: google-deployment-manager

我用于使用IAM策略更新项目。在GCP部署管理器的模板中,他们使用的是python Jinja文件,但我想添加IAM策略(为用户/服务帐户分配一些角色)。有人可以修改Jinja / config文件并查明我可以修改的方式吗?

https://github.com/GoogleCloudPlatform/deploymentmanager-samples/blob/master/examples/v2/project_creation/config.yaml

https://github.com/GoogleCloudPlatform/deploymentmanager-samples/blob/master/examples/v2/project_creation/project.py

6 个答案:

答案 0 :(得分:5)

这是一个jinja代码段,用于创建新的服务帐户并将其作为所有者添加到现有项目中。这需要分配部署经理the proper access来管理项目的IAM。

{% set deployment = env['deployment'] %}
{% set project = env['project'] %}

resources:
- name: {{ deployment }}-svc-account
  type: iam.v1.serviceAccount
  properties:
    accountId: {{ deployment }}-svc-account
    displayName: {{ deployment }}-svc-account

- name: get-iam-policy
  action: gcp-types/cloudresourcemanager-v1:cloudresourcemanager.projects.getIamPolicy
  properties:
    resource: {{ project }}
  metadata:
    runtimePolicy:
    - 'UPDATE_ALWAYS'

- name: patch-iam-policy
  action: gcp-types/cloudresourcemanager-v1:cloudresourcemanager.projects.setIamPolicy
  properties:
    resource: {{ project }}
    policy: $(ref.get-iam-policy)
    gcpIamPolicyPatch:
      add:
      - role: roles/owner
        members:
        - serviceAccount:$(ref.{{ deployment }}-svc-account.email)

答案 1 :(得分:2)

请按照 Adam Ocsvari 的示例分配IAM策略。旧方法是获取所有IAM绑定策略,添加一些角色->成员绑定,然后设置所有绑定。他正在使用'type': 'gcp-types/cloudresourcemanager-v1:virtual.projects.iamMemberBinding'提供一种新方法。我使用了他提供的链接之一来找到分配了IAM策略绑定的the python template。那里的代码有一个嵌套循环。我只需要创建一个服务帐户并分配1个绑定:

service-accounts.py

def GenerateConfig(context):
    project_id = context.env['project']
    service_account = context.properties['service-account']

    resources = [
        {
            'name': service_account,
            'type': 'iam.v1.serviceAccount',
            'properties': {
                'accountId': service_account,
                'displayName': service_account,
                'projectId': project_id
            }
        },
        {
            'name': 'bind-iam-policy',
            'type': 'gcp-types/cloudresourcemanager-v1:virtual.projects.iamMemberBinding',
            'properties': {
                'resource': project_id,
                'role': 'roles/dataflow.admin',
                'member': 'serviceAccount:$(ref.' + service_account + '.email)'
            },
            'metadata': {
                'dependsOn': [service_account]
            }
        }
    ]

    return {'resources': resources}

service-accounts.yaml

imports:
  - path: service-accounts.py

resources:
  - name: service-accounts
    type: service-accounts.py
    properties:
      project: [*YOUR_PROJECT_ID*]
      service-account: k8s-service-account

此示例创建一个k8s-service-account并为其分配Dataflow管理员角色。开始之前,请确保您Grant Deployment Manager permission to set IAM policies

答案 2 :(得分:1)

您需要更改config.yaml文件的以下部分,并根据您在成员行下的需要添加用户或服务帐户。

 iam-policy:
      bindings:
      - role: roles/owner
        members:
        - serviceAccount:98765432111@cloudservices.gserviceaccount.com
        - serviceAccount:98765432100@cloudservices.gserviceaccount.com
      - role: roles/viewer
        members:
        - user:iamtester@deployment-manager.net

例如:您可以在相应部分的成员标签下添加-user:foo@bar.com,以使其成为项目的所有者或查看者。

答案 3 :(得分:1)

请避免使用以下解决方案:

i {font-family: serif}

它可能导致并发IAM策略更新错误。 Deployment Manager团队正在提供一种将这两个操作绑定在一起的新类型:

gcp-types/cloudresourcemanager-v1:cloudresourcemanager.projects.getIamPolicy
gcp-types/cloudresourcemanager-v1:cloudresourcemanager.projects.setIamPolicy

在Google Cloud提供的Cloud Foundation Toolkit中检查以下实现:

Cloud Foundation Toolkit NEW repo - IAM binding

Cloud Foundation Toolkit OLD repo - IAM binding

Cloud Foundation Toolkit NEW repo - Project Creation Factory

答案 4 :(得分:0)

我的用于向服务帐户添加权限的代码。

{% set deployment = env['deployment'] %}
{% set project = env['project'] %}

resources:
- name: get-iam-policy
  action: gcp-types/cloudresourcemanager-v1:cloudresourcemanager.projects.getIamPolicy
  properties:
    resource: {{ project }}
  metadata:
    runtimePolicy:
    - 'UPDATE_ALWAYS'
- name: patch-iam-policy
  action: gcp-types/cloudresourcemanager-v1:cloudresourcemanager.projects.setIamPolicy
  properties:
    resource: {{ project }}
    policy: $(ref.get-iam-policy)
    gcpIamPolicyPatch:
      add:
      - role: roles/bigquery.dataEditor
        members:
        - serviceAccount: <service account>

答案 5 :(得分:0)

根据Google的说法,首选方式是不使用操作。而是使用在Deployment Manager中引入状态的类型提供程序。有关可用类型的完整列表,请使用以下命令:

gcloud beta deployment-manager types list --project gcp-types

廖希il使用的示例是设置绑定的正确示例。