Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add IAM custom roles and access snippets #1692

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 2, 2018

Conversation

awkoren
Copy link
Contributor

@awkoren awkoren commented Sep 7, 2018

No description provided.

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Sep 7, 2018
@@ -0,0 +1,131 @@
# !/usr/bin/env python
#
# Copyright 2018 Google Inc. All Rights Reserved.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Google LLC

Drop the "all rights reserved". See go/releasing/preparing#headers

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

import googleapiclient.discovery

credentials = service_account.Credentials.from_service_account_file(
filename=os.environ['GOOGLE_APPLICATION_CREDENTIALS'],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why read from the environment variable directly instead of using google.auth.default(scopes)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the documented approach: https://developers.google.com/api-client-library/python/auth/service-accounts
google.auth.default() does not seem to work.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Odd. It should do the same thing if the environment variable is set.

Note that its signature is slightly different: google.auth.default() returns a tuple with the credentials and a default project ID.

# pylint: disable=no-member
policy = service.projects().getIamPolicy(
resource=project_id, body={}).execute()
print(policy)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please introspect in the objects so that we see the relevant fields in the actual code. e.g.

print(policy['some_relevant_field_name'])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

print(policy) dumps the policy object as JSON. This is good since users need to understand and manipulate this object to edit IAM policy. This is explained in the companion article: https://cloud.google.com/iam/docs/granting-changing-revoking-access#overview_of_cloud_iam_policy

"""Adds a new member to a role binding."""
binding = next(b for b in policy['bindings'] if b['role'] == role)
binding['members'].append(member)
print(binding)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this missing an API request to actually update the policy?

Copy link
Contributor Author

@awkoren awkoren Nov 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The set_policy() method on line 72 demonstrates that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find that a bit strange to separate them. Most how-to guides that I've seen try to keep each code snippet self-contained, but you're right that this matches the docs.



# [START iam_create_role]
def create_role(name, project, title, description, permissions, stage):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd like to avoid general samples if at all possible. Could you remove the arguments and use specific values?

Otherwise, provide example values as comments if not possible to hardcode values for these.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't a general sample, it's a series of companion snippets that map to this article: https://cloud.google.com/iam/docs/creating-custom-roles.
Each snippet corresponds to a section in the article that explains its parameters.
Using hardcoded values like "my-project" is generally considered more confusing.

import googleapiclient.discovery

credentials = service_account.Credentials.from_service_account_file(
filename=os.environ['GOOGLE_APPLICATION_CREDENTIALS'],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Odd. It should do the same thing if the environment variable is set.

Note that its signature is slightly different: google.auth.default() returns a tuple with the credentials and a default project ID.

"""Adds a new member to a role binding."""
binding = next(b for b in policy['bindings'] if b['role'] == role)
binding['members'].append(member)
print(binding)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find that a bit strange to separate them. Most how-to guides that I've seen try to keep each code snippet self-contained, but you're right that this matches the docs.

@tswast
Copy link
Contributor

tswast commented Nov 1, 2018

@awkoren Travis is showing some lint errors. Please fix.

nox > flake8 --show-source --builtin gettext --max-complexity 20 --import-order-style google --exclude .nox,.cache,env,lib,generated_pb2,*_pb2.py,*_pb2_grpc.py --ignore=E121,E123,E126,E226,E24,E704,W503,W504,I100,I201,I202 --application-import-names grantable_roles,grantable_roles_test,quickstart,quickstart_test,service_account_keys,service_accounts,service_accounts_test,access,access_test,custom_roles,custom_roles_test .
./custom_roles.py:4:2: W291 trailing whitespace
# 
 ^
./custom_roles_test.py:2:2: W291 trailing whitespace
# 
 ^

@awkoren
Copy link
Contributor Author

awkoren commented Nov 2, 2018

Travis errors fixed.
Also I cannot merge so someone with access will need to.

@tswast tswast merged commit 9473909 into GoogleCloudPlatform:master Nov 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants