Skip to content

[Storage] Bucket-level IAM Samples#2008

Merged
lesv merged 6 commits intogoogleapis:masterfrom
frankyn:storage-iam-snippets
Apr 29, 2017
Merged

[Storage] Bucket-level IAM Samples#2008
lesv merged 6 commits intogoogleapis:masterfrom
frankyn:storage-iam-snippets

Conversation

@frankyn
Copy link
Copy Markdown
Contributor

@frankyn frankyn commented Apr 26, 2017

Hi Garrett,

PR Summary:
This pull request will add the following samples to google-cloud-examples:
view_bucket_iam_members -- list bucket-level iam roles and their members
add_bucket_iam_member -- add a bucket-level iam member
remove_bucket_iam_member -- remove a bucket-level iam member

I copied the existing file BucketSnippets.java to keep consistency, and because ACL and Bucket-level IAM have a conflicting class name com.google.cloud.storage.Acl.Role and com.google.cloud.Role.

What additional work do I need to include in this PR to satisfy this line "This file is referenced in Storage's javadoc. Any change to this file should be reflected in Storage's javadoc.".?

Thank you!

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Apr 26, 2017
@frankyn frankyn changed the title Storage iam snippets [Storage] Bucket-level IAM Samples Apr 26, 2017
@frankyn frankyn added api: storage Issues related to the Cloud Storage API. and removed android labels Apr 26, 2017
@coveralls
Copy link
Copy Markdown

Coverage Status

Changes Unknown when pulling 6ec2d3c on frankyn:storage-iam-snippets into ** on GoogleCloudPlatform:master**.

/**
* Example of listing the Bucket-Level IAM Roles and Members
*/

This comment was marked as spam.

Map<Role, Set<Identity>> policyBindings = policy.getBindings();
for(Map.Entry<Role, Set<Identity>> entry : policyBindings.entrySet()) {
System.out.printf("Role: %s", entry.getKey());
System.out.printf(" Identities: %s\n", entry.getValue());

This comment was marked as spam.

// Update the bucket IAM Policy
storage.setIamPolicy(bucketName, updatedPolicy);

System.out.printf("Added %s with role %s to %s\n", identity, role, bucketName);

This comment was marked as spam.

/*
* EDITING INSTRUCTIONS
* This file is referenced in Storage's javadoc. Any change to this file should be reflected in
* Storage's javadoc.

This comment was marked as spam.

@frankyn
Copy link
Copy Markdown
Contributor Author

frankyn commented Apr 27, 2017

Thanks @shinfan! I have updated my PR to reflect your comments. PTAL

@coveralls
Copy link
Copy Markdown

Coverage Status

Changes Unknown when pulling b21740b on frankyn:storage-iam-snippets into ** on GoogleCloudPlatform:master**.

@frankyn frankyn force-pushed the storage-iam-snippets branch from b3753a3 to 873b8ff Compare April 27, 2017 16:40
@coveralls
Copy link
Copy Markdown

Coverage Status

Changes Unknown when pulling 873b8ff on frankyn:storage-iam-snippets into ** on GoogleCloudPlatform:master**.

@coveralls
Copy link
Copy Markdown

Coverage Status

Changes Unknown when pulling 8b49ff9 on frankyn:storage-iam-snippets into ** on GoogleCloudPlatform:master**.

Copy link
Copy Markdown
Contributor

@shinfan shinfan left a comment

Choose a reason for hiding this comment

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

LGTM

@frankyn
Copy link
Copy Markdown
Contributor Author

frankyn commented Apr 27, 2017

@shinfan do I need to wait for Garrett's LGTM as well or can I merge?

@shinfan
Copy link
Copy Markdown
Contributor

shinfan commented Apr 27, 2017

@garrettjonesgoogle Do you wanna take another look?

@garrettjonesgoogle garrettjonesgoogle requested a review from lesv April 27, 2017 23:09
@garrettjonesgoogle
Copy link
Copy Markdown
Contributor

Adding @lesv for final review

Copy link
Copy Markdown
Contributor

@lesv lesv left a comment

Choose a reason for hiding this comment

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

The snippets are good, but you might wish to return updatedPolicy for the last two.

You might run into propagation issues with this, so your tests should be a bit less brittle.

System.out.printf("Added %s with role %s to %s\n", identity, role, bucketName);
}
// [END add_bucket_iam_member]
}

This comment was marked as spam.

System.out.printf("Removed %s with role %s from %s\n", identity, role, bucketName);
}
// [END remove_bucket_iam_member]
}

This comment was marked as spam.

@Test
public void testAddBucketIamMemeber() {
// Test a member is added to Bucket-level IAM
Policy policy = storage.getIamPolicy(BUCKET);

This comment was marked as spam.


@Test
public void testRemoveBucketIamMember() {
// Test a member is removed from Bucket-level IAM

This comment was marked as spam.

@Test
public void testListBucketIamMembers() {
// Test an added Bucket-level IAM member is listed
Policy policy = storage.getIamPolicy(BUCKET);

This comment was marked as spam.

assertNull(policy.getBindings().get(StorageRoles.admin()));
storage.setIamPolicy(BUCKET, policy.toBuilder().addIdentity(StorageRoles.admin(),
Identity.user(USER_EMAIL)).build());
policy = storage.getIamPolicy(BUCKET);

This comment was marked as spam.

This comment was marked as spam.

@coveralls
Copy link
Copy Markdown

Coverage Status

Changes Unknown when pulling 00555f3 on frankyn:storage-iam-snippets into ** on GoogleCloudPlatform:master**.

@frankyn
Copy link
Copy Markdown
Contributor Author

frankyn commented Apr 28, 2017

Thanks @lesv, I have applied changes. PTAL

Copy link
Copy Markdown
Contributor

@lesv lesv left a comment

Choose a reason for hiding this comment

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

Sorry I wasn't thinking expressively yesterday.

What I would do is:

  1. In your create bucket, I would add several policies, so all of them are done.
  2. I would test myAddBucketIAM first
  3. Then I run myRemoveBucketIAMMember final.
  4. I would test myListBucketIAM.

Use lots of email addresses, not just one. Deal with the fact that you might not see yours.

You have a good chance to see the initial add's (setup) by the time you do your list. (but no guarantee).

The key point is Eventual Consistency -- I think it's a part of everything we deal with.

Identity.user(USER_EMAIL)).build());
assertTrue(policy.getBindings().get(StorageRoles.admin()).contains(Identity.user(USER_EMAIL)));
Policy snippetPolicy = bucketIamSnippets.listBucketIamMembers(BUCKET);
assertTrue(snippetPolicy.getBindings().get(StorageRoles.admin()).

This comment was marked as spam.

Policy policy = storage.getIamPolicy(BUCKET);
policy = storage.setIamPolicy(BUCKET,
policy.toBuilder().removeRole(StorageRoles.admin()).build());
assertNull(policy.getBindings().get(StorageRoles.admin()));

This comment was marked as spam.

assertNull(policy.getBindings().get(StorageRoles.admin()));
policy = storage.setIamPolicy(BUCKET, policy.toBuilder().addIdentity(StorageRoles.admin(),
Identity.user(USER_EMAIL)).build());
assertTrue(policy.getBindings().get(StorageRoles.admin()).contains(Identity.user(USER_EMAIL)));

This comment was marked as spam.

Policy policy = storage.getIamPolicy(BUCKET);
policy = storage.setIamPolicy(BUCKET,
policy.toBuilder().removeRole(StorageRoles.admin()).build());
assertNull(policy.getBindings().get(StorageRoles.admin()));

This comment was marked as spam.

Policy policy = storage.getIamPolicy(BUCKET);
policy = storage.setIamPolicy(BUCKET,
policy.toBuilder().removeRole(StorageRoles.admin()).build());
assertNull(policy.getBindings().get(StorageRoles.admin()));

This comment was marked as spam.

assertNull(policy.getBindings().get(StorageRoles.admin()));
policy = storage.setIamPolicy(BUCKET, policy.toBuilder().addIdentity(StorageRoles.admin(),
Identity.user(USER_EMAIL)).build());
assertTrue(policy.getBindings().get(StorageRoles.admin()).contains(Identity.user(USER_EMAIL)));

This comment was marked as spam.

@lesv
Copy link
Copy Markdown
Contributor

lesv commented Apr 29, 2017

I'm going to improve several of the tests by hand, then approve this PR as Frank had something come up and this needs to be published on Monday.

Copy link
Copy Markdown
Contributor

@lesv lesv left a comment

Choose a reason for hiding this comment

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

I think these might be more brittle than I'd like, but for now, it's working. I'll ask Frank to update later.

@lesv lesv merged commit f44ac0d into googleapis:master Apr 29, 2017
meltsufin pushed a commit that referenced this pull request Apr 29, 2026
🤖 I have created a release *beep* *boop*
---


### Updating meta-information for bleeding-edge SNAPSHOT release.

---
This PR was generated with [Release Please](https://togithub.com/googleapis/release-please). See [documentation](https://togithub.com/googleapis/release-please#release-please).
meltsufin pushed a commit that referenced this pull request May 1, 2026
🤖 I have created a release *beep* *boop*
---


### Updating meta-information for bleeding-edge SNAPSHOT release.

---
This PR was generated with [Release Please](https://togithub.com/googleapis/release-please). See [documentation](https://togithub.com/googleapis/release-please#release-please).
meltsufin pushed a commit that referenced this pull request May 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: storage Issues related to the Cloud Storage API. cla: yes This human has signed the Contributor License Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants