Skip to content

[IAM] Update quickstart samples #2829

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 8 commits into from
May 12, 2020
Merged

[IAM] Update quickstart samples #2829

merged 8 commits into from
May 12, 2020

Conversation

melaniedejong
Copy link
Contributor

Adding new quickstart samples to support the new quickstart flow (staged: https://cloud.devsite.corp.google.com/iam/docs/quickstart-client-libraries#iam_quickstart-csharp). The new flow better demonstrates the core functionality of IAM.

The old samples will need to stay around until the doc update is completed. After that, I can put in a separate PR to remove them.

@melaniedejong melaniedejong requested a review from a team May 1, 2020 19:04
@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label May 1, 2020
@lesv lesv added the kokoro:run Add this label to force Kokoro to re-run the tests. label May 1, 2020
@kokoro-team kokoro-team removed the kokoro:run Add this label to force Kokoro to re-run the tests. label May 1, 2020
@melaniedejong
Copy link
Contributor Author

I just realized that I needed to change this flow--the testIamPermissions method tests the permissions of the caller (in this case, whatever service account the user has created) and not the member.

I updated the sample to instruct the user to grant the role to the service account used to run the quickstart. This adds a complication in that the service account used in quickstarts by default is usually granted the owner role, so the testPermissions call would always succeed, regardless of whether or not the role grant was successful. We're hoping to downscope the permissions granted to quickstart service accounts in the future, so this may become a non-issue, though the date of that work is uncertain.

If you have a better solution, please let me know.

@melaniedejong
Copy link
Contributor Author

Update: I decided to try and refactor the code to remove the testIamPermissions call, instead just printing out the binding that was added to the policy. I'll comment here when I've updated the code.

@melaniedejong
Copy link
Contributor Author

The thread isn't updating with the new commit for some reason, but I did push the new version to the quickstart branch. Hopefully github will catch up soon?

@melaniedejong
Copy link
Contributor Author

Looks like it's updated now.
Also, as a note, I took this chance to fix the AddMember and RemoveMember methods, which need to use .equals rather than =. And, the RemoveMember method was throwing a ConcurrentModificationException, so I refactored the code to avoid that as well.

@lesv lesv added kokoro:run Add this label to force Kokoro to re-run the tests. automerge Merge the pull request once unit tests and other checks pass. labels May 6, 2020
@kokoro-team kokoro-team removed the kokoro:run Add this label to force Kokoro to re-run the tests. label May 6, 2020
@lesv
Copy link
Contributor

lesv commented May 6, 2020

Melanie, please join the org at github/ (internal site)

@melaniedejong
Copy link
Contributor Author

Done

@lesv
Copy link
Contributor

lesv commented May 6, 2020

Hi Melanie,

You have one checkstyle violation which I've posted below, if you could fix your PR, that's all that's needed.

There are also a bunch of suggestions from the linter that are optional, but it would be good if you at least took a look at. I've posted them below the checkstyle issue.

Java 8

------------------------------------------------------------
- testing iam/api-client
------------------------------------------------------------
[ERROR] src/main/java/com/google/iam/snippets/QuickstartV2.java:[24,58] (imports) AvoidStarImport: Using the '.*' form of import should be avoided - com.google.api.services.cloudresourcemanager.model.*.
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-checkstyle-plugin:3.1.1:check (default) on project iam-snippets: You have 1 Checkstyle violation. -> [Help 1]

Linter issues:

[ERROR] Method com.google.iam.snippets.AddBinding.addBinding(Policy) concatenates the result of a toString() call [com.google.iam.snippets.AddBinding] At AddBinding.java:[line 39] ISB_TOSTRING_APPENDING
[ERROR] Method com.google.iam.snippets.AddMember.addMember(Policy) makes literal string comparisons passing the literal as an argument [com.google.iam.snippets.AddMember] At AddMember.java:[line 35] LSC_LITERAL_STRING_COMPARISON
[ERROR] Method com.google.iam.snippets.CreateServiceAccount.createServiceAccount(String) concatenates the result of a toString() call [com.google.iam.snippets.CreateServiceAccount] At CreateServiceAccount.java:[line 40] ISB_TOSTRING_APPENDING
[ERROR] Method com.google.iam.snippets.CreateServiceAccount.createServiceAccount(String) concatenates the result of a toString() call [com.google.iam.snippets.CreateServiceAccount] At CreateServiceAccount.java:[line 56] ISB_TOSTRING_APPENDING
[ERROR] Method com.google.iam.snippets.CreateServiceAccount.initService() stores return result in local before immediately returning it [com.google.iam.snippets.CreateServiceAccount] At CreateServiceAccount.java:[line 74] USBR_UNNECESSARY_STORE_BEFORE_RETURN
[ERROR] Method com.google.iam.snippets.CreateServiceAccountKey.createKey(String) concatenates the result of a toString() call [com.google.iam.snippets.CreateServiceAccountKey] At CreateServiceAccountKey.java:[line 40] ISB_TOSTRING_APPENDING
[ERROR] Method com.google.iam.snippets.CreateServiceAccountKey.createKey(String) concatenates the result of a toString() call [com.google.iam.snippets.CreateServiceAccountKey] At CreateServiceAccountKey.java:[line 59] ISB_TOSTRING_APPENDING
[ERROR] Method com.google.iam.snippets.CreateServiceAccountKey.initService() stores return result in local before immediately returning it [com.google.iam.snippets.CreateServiceAccountKey] At CreateServiceAccountKey.java:[line 77] USBR_UNNECESSARY_STORE_BEFORE_RETURN
[ERROR] Method com.google.iam.snippets.DeleteServiceAccount.deleteServiceAccount(String) concatenates the result of a toString() call [com.google.iam.snippets.DeleteServiceAccount] At DeleteServiceAccount.java:[line 38] ISB_TOSTRING_APPENDING
[ERROR] Method com.google.iam.snippets.DeleteServiceAccount.deleteServiceAccount(String) concatenates the result of a toString() call [com.google.iam.snippets.DeleteServiceAccount] At DeleteServiceAccount.java:[line 59] ISB_TOSTRING_APPENDING
[ERROR] Method com.google.iam.snippets.DeleteServiceAccount.initService() stores return result in local before immediately returning it [com.google.iam.snippets.DeleteServiceAccount] At DeleteServiceAccount.java:[line 77] USBR_UNNECESSARY_STORE_BEFORE_RETURN
[ERROR] Method com.google.iam.snippets.DeleteServiceAccountKey.deleteKey(String) concatenates the result of a toString() call [com.google.iam.snippets.DeleteServiceAccountKey] At DeleteServiceAccountKey.java:[line 40] ISB_TOSTRING_APPENDING
[ERROR] Method com.google.iam.snippets.DeleteServiceAccountKey.deleteKey(String) concatenates the result of a toString() call [com.google.iam.snippets.DeleteServiceAccountKey] At DeleteServiceAccountKey.java:[line 65] ISB_TOSTRING_APPENDING
[ERROR] Method com.google.iam.snippets.DeleteServiceAccountKey.deleteKey(String) appears to call the same method on the same object redundantly [com.google.iam.snippets.DeleteServiceAccountKey] At DeleteServiceAccountKey.java:[line 61] PRMC_POSSIBLY_REDUNDANT_METHOD_CALLS
[ERROR] Method com.google.iam.snippets.DeleteServiceAccountKey.initService() stores return result in local before immediately returning it [com.google.iam.snippets.DeleteServiceAccountKey] At DeleteServiceAccountKey.java:[line 83] USBR_UNNECESSARY_STORE_BEFORE_RETURN
[ERROR] Method com.google.iam.snippets.DisableServiceAccount.disableServiceAccount(String) concatenates the result of a toString() call [com.google.iam.snippets.DisableServiceAccount] At DisableServiceAccount.java:[line 39] ISB_TOSTRING_APPENDING
[ERROR] Method com.google.iam.snippets.DisableServiceAccount.disableServiceAccount(String) concatenates the result of a toString() call [com.google.iam.snippets.DisableServiceAccount] At DisableServiceAccount.java:[line 62] ISB_TOSTRING_APPENDING
[ERROR] Method com.google.iam.snippets.DisableServiceAccount.initService() stores return result in local before immediately returning it [com.google.iam.snippets.DisableServiceAccount] At DisableServiceAccount.java:[line 80] USBR_UNNECESSARY_STORE_BEFORE_RETURN
[ERROR] Method com.google.iam.snippets.EnableServiceAccount.enableServiceAccount(String) concatenates the result of a toString() call [com.google.iam.snippets.EnableServiceAccount] At EnableServiceAccount.java:[line 39] ISB_TOSTRING_APPENDING
[ERROR] Method com.google.iam.snippets.EnableServiceAccount.enableServiceAccount(String) concatenates the result of a toString() call [com.google.iam.snippets.EnableServiceAccount] At EnableServiceAccount.java:[line 62] ISB_TOSTRING_APPENDING
[ERROR] Method com.google.iam.snippets.EnableServiceAccount.initService() stores return result in local before immediately returning it [com.google.iam.snippets.EnableServiceAccount] At EnableServiceAccount.java:[line 80] USBR_UNNECESSARY_STORE_BEFORE_RETURN
[ERROR] Method com.google.iam.snippets.GetPolicy.getPolicy(String) that can return null, is missing a @Nullable annotation [com.google.iam.snippets.GetPolicy] At GetPolicy.java:[lines 36-53] AI_ANNOTATION_ISSUES_NEEDS_NULLABLE
[ERROR] Method com.google.iam.snippets.GetPolicy.getPolicy(String) concatenates the result of a toString() call [com.google.iam.snippets.GetPolicy] At GetPolicy.java:[line 42] ISB_TOSTRING_APPENDING
[ERROR] Method com.google.iam.snippets.GetPolicy.getPolicy(String) concatenates the result of a toString() call [com.google.iam.snippets.GetPolicy] At GetPolicy.java:[line 49] ISB_TOSTRING_APPENDING
[ERROR] Method com.google.iam.snippets.GetPolicy.getPolicy(String) concatenates the result of a toString() call [com.google.iam.snippets.GetPolicy] At GetPolicy.java:[line 52] ISB_TOSTRING_APPENDING
[ERROR] This method com.google.iam.snippets.GetPolicy.createCloudResourceManagerService() is declared more permissively than is used in the code base [com.google.iam.snippets.GetPolicy] At GetPolicy.java:[lines 62-72] OPM_OVERLY_PERMISSIVE_METHOD
[ERROR] Method com.google.iam.snippets.GetPolicy.createCloudResourceManagerService() stores return result in local before immediately returning it [com.google.iam.snippets.GetPolicy] At GetPolicy.java:[line 72] USBR_UNNECESSARY_STORE_BEFORE_RETURN
[ERROR] Method com.google.iam.snippets.ListServiceAccountKeys.listKeys(String) concatenates the result of a toString() call [com.google.iam.snippets.ListServiceAccountKeys] At ListServiceAccountKeys.java:[line 40] ISB_TOSTRING_APPENDING
[ERROR] Method com.google.iam.snippets.ListServiceAccountKeys.listKeys(String) concatenates the result of a toString() call [com.google.iam.snippets.ListServiceAccountKeys] At ListServiceAccountKeys.java:[line 62] ISB_TOSTRING_APPENDING
[ERROR] Method com.google.iam.snippets.ListServiceAccountKeys.initService() stores return result in local before immediately returning it [com.google.iam.snippets.ListServiceAccountKeys] At ListServiceAccountKeys.java:[line 80] USBR_UNNECESSARY_STORE_BEFORE_RETURN
[ERROR] Method com.google.iam.snippets.ListServiceAccounts.listServiceAccounts(String) concatenates the result of a toString() call [com.google.iam.snippets.ListServiceAccounts] At ListServiceAccounts.java:[line 41] ISB_TOSTRING_APPENDING
[ERROR] Method com.google.iam.snippets.ListServiceAccounts.listServiceAccounts(String) concatenates the result of a toString() call [com.google.iam.snippets.ListServiceAccounts] At ListServiceAccounts.java:[line 57] ISB_TOSTRING_APPENDING
[ERROR] Method com.google.iam.snippets.ListServiceAccounts.initService() stores return result in local before immediately returning it [com.google.iam.snippets.ListServiceAccounts] At ListServiceAccounts.java:[line 75] USBR_UNNECESSARY_STORE_BEFORE_RETURN
[ERROR] Method com.google.iam.snippets.QuickstartV2.getPolicy(CloudResourceManager, String) concatenates the result of a toString() call [com.google.iam.snippets.QuickstartV2] At QuickstartV2.java:[line 100] ISB_TOSTRING_APPENDING
[ERROR] Method com.google.iam.snippets.QuickstartV2.main(String[]) concatenates the result of a toString() call [com.google.iam.snippets.QuickstartV2] At QuickstartV2.java:[line 47] ISB_TOSTRING_APPENDING
[ERROR] Method com.google.iam.snippets.QuickstartV2.setPolicy(CloudResourceManager, String, Policy) concatenates the result of a toString() call [com.google.iam.snippets.QuickstartV2] At QuickstartV2.java:[line 113] ISB_TOSTRING_APPENDING
[ERROR] Method com.google.iam.snippets.QuickstartV2.main(String[]) makes literal string comparisons passing the literal as an argument [com.google.iam.snippets.QuickstartV2] At QuickstartV2.java:[line 58] LSC_LITERAL_STRING_COMPARISON
[ERROR] Method com.google.iam.snippets.QuickstartV2.addBinding(CloudResourceManager, String, String, String) builds a list from one element using Arrays.asList rather than Collections.singletonList [com.google.iam.snippets.QuickstartV2] At QuickstartV2.java:[line 135] LUI_USE_SINGLETON_LIST
[ERROR] Null passed for non-null parameter of addBinding(CloudResourceManager, String, String, String) in com.google.iam.snippets.QuickstartV2.main(String[]) [com.google.iam.snippets.QuickstartV2, com.google.iam.snippets.QuickstartV2] Method invoked at QuickstartV2.java:[line 51]Known null at QuickstartV2.java:[line 43] NP_NULL_PARAM_DEREF
[ERROR] This method com.google.iam.snippets.QuickstartV2.addBinding(CloudResourceManager, String, String, String) is declared more permissively than is used in the code base [com.google.iam.snippets.QuickstartV2] At QuickstartV2.java:[lines 121-140] OPM_OVERLY_PERMISSIVE_METHOD
[ERROR] This method com.google.iam.snippets.QuickstartV2.getPolicy(CloudResourceManager, String) is declared more permissively than is used in the code base [com.google.iam.snippets.QuickstartV2] At QuickstartV2.java:[lines 95-102] OPM_OVERLY_PERMISSIVE_METHOD
[ERROR] This method com.google.iam.snippets.QuickstartV2.initializeService() is declared more permissively than is used in the code base [com.google.iam.snippets.QuickstartV2] At QuickstartV2.java:[lines 78-89] OPM_OVERLY_PERMISSIVE_METHOD
[ERROR] This method com.google.iam.snippets.QuickstartV2.removeMember(CloudResourceManager, String, String, String) is declared more permissively than is used in the code base [com.google.iam.snippets.QuickstartV2] At QuickstartV2.java:[lines 145-164] OPM_OVERLY_PERMISSIVE_METHOD
[ERROR] This method com.google.iam.snippets.QuickstartV2.main(String[]) continues a loop after finding an equality condition [com.google.iam.snippets.QuickstartV2] At QuickstartV2.java:[line 59] SLS_SUSPICIOUS_LOOP_SEARCH
[ERROR] This method com.google.iam.snippets.QuickstartV2.removeMember(CloudResourceManager, String, String, String) continues a loop after finding an equality condition [com.google.iam.snippets.QuickstartV2] At QuickstartV2.java:[line 152] SLS_SUSPICIOUS_LOOP_SEARCH
[ERROR] Method com.google.iam.snippets.QuickstartV2.removeMember(CloudResourceManager, String, String, String) checks the size of a collection against zero rather than using isEmpty() [com.google.iam.snippets.QuickstartV2] At QuickstartV2.java:[line 157] SPP_USE_ISEMPTY
[ERROR] Method com.google.iam.snippets.QuickstartV2.initializeService() stores return result in local before immediately returning it [com.google.iam.snippets.QuickstartV2] At QuickstartV2.java:[line 89] USBR_UNNECESSARY_STORE_BEFORE_RETURN
[ERROR] Method com.google.iam.snippets.RemoveMember.removeMember(Policy) makes literal string comparisons passing the literal as an argument [com.google.iam.snippets.RemoveMember] At RemoveMember.java:[line 35] LSC_LITERAL_STRING_COMPARISON
[ERROR] This method com.google.iam.snippets.RemoveMember.removeMember(Policy) continues a loop after finding an equality condition [com.google.iam.snippets.RemoveMember] At RemoveMember.java:[line 36] SLS_SUSPICIOUS_LOOP_SEARCH
[ERROR] Method com.google.iam.snippets.RemoveMember.removeMember(Policy) checks the size of a collection against zero rather than using isEmpty() [com.google.iam.snippets.RemoveMember] At RemoveMember.java:[line 42] SPP_USE_ISEMPTY
[ERROR] Method com.google.iam.snippets.RenameServiceAccount.renameServiceAccount(String) concatenates the result of a toString() call [com.google.iam.snippets.RenameServiceAccount] At RenameServiceAccount.java:[line 39] ISB_TOSTRING_APPENDING
[ERROR] Method com.google.iam.snippets.RenameServiceAccount.renameServiceAccount(String) concatenates the result of a toString() call [com.google.iam.snippets.RenameServiceAccount] At RenameServiceAccount.java:[line 71] ISB_TOSTRING_APPENDING
[ERROR] Method com.google.iam.snippets.RenameServiceAccount.renameServiceAccount(String) appears to call the same method on the same object redundantly [com.google.iam.snippets.RenameServiceAccount] At RenameServiceAccount.java:[line 60] PRMC_POSSIBLY_REDUNDANT_METHOD_CALLS
[ERROR] Method com.google.iam.snippets.RenameServiceAccount.initService() stores return result in local before immediately returning it [com.google.iam.snippets.RenameServiceAccount] At RenameServiceAccount.java:[line 89] USBR_UNNECESSARY_STORE_BEFORE_RETURN
[ERROR] Method com.google.iam.snippets.SetPolicy.setPolicy(Policy, String) concatenates the result of a toString() call [com.google.iam.snippets.SetPolicy] At SetPolicy.java:[line 41] ISB_TOSTRING_APPENDING
[ERROR] Method com.google.iam.snippets.SetPolicy.setPolicy(Policy, String) concatenates the result of a toString() call [com.google.iam.snippets.SetPolicy] At SetPolicy.java:[line 49] ISB_TOSTRING_APPENDING
[ERROR] Method com.google.iam.snippets.SetPolicy.setPolicy(Policy, String) concatenates the result of a toString() call [com.google.iam.snippets.SetPolicy] At SetPolicy.java:[line 51] ISB_TOSTRING_APPENDING
[ERROR] This method com.google.iam.snippets.SetPolicy.createCloudResourceManagerService() is declared more permissively than is used in the code base [com.google.iam.snippets.SetPolicy] At SetPolicy.java:[lines 60-70] OPM_OVERLY_PERMISSIVE_METHOD
[ERROR] Method com.google.iam.snippets.SetPolicy.createCloudResourceManagerService() stores return result in local before immediately returning it [com.google.iam.snippets.SetPolicy] At SetPolicy.java:[line 70] USBR_UNNECESSARY_STORE_BEFORE_RETURN
[ERROR] Method com.google.iam.snippets.TestPermissions.testPermissions(String) concatenates the result of a toString() call [com.google.iam.snippets.TestPermissions] At TestPermissions.java:[line 42] ISB_TOSTRING_APPENDING
[ERROR] Method com.google.iam.snippets.TestPermissions.testPermissions(String) concatenates the result of a toString() call [com.google.iam.snippets.TestPermissions] At TestPermissions.java:[line 57] ISB_TOSTRING_APPENDING
[ERROR] Method com.google.iam.snippets.TestPermissions.testPermissions(String) concatenates the result of a toString() call [com.google.iam.snippets.TestPermissions] At TestPermissions.java:[line 59] ISB_TOSTRING_APPENDING
[ERROR] This method com.google.iam.snippets.TestPermissions.createCloudResourceManagerService() is declared more permissively than is used in the code base [com.google.iam.snippets.TestPermissions] At TestPermissions.java:[lines 68-78] OPM_OVERLY_PERMISSIVE_METHOD
[ERROR] Method com.google.iam.snippets.TestPermissions.createCloudResourceManagerService() stores return result in local before immediately returning it [com.google.iam.snippets.TestPermissions] At TestPermissions.java:[line 78] USBR_UNNECESSARY_STORE_BEFORE_RETURN

@lesv lesv removed the automerge Merge the pull request once unit tests and other checks pass. label May 6, 2020
@gcf-merge-on-green
Copy link
Contributor

Your PR has attempted to merge for 3 hours. Please check that all required checks have passed, you have an automerge label, and that all your reviewers have approved the PR

@gcf-merge-on-green
Copy link
Contributor

Merge-on-green attempted to merge your PR for 6 hours, but it was not mergeable because either one of your required status checks failed, or one of your required reviews was not approved. Learn more about your required status checks here: https://help.github.com/en/github/administering-a-repository/enabling-required-status-checks. You can remove and reapply the label to re-run the bot.

@lesv lesv added the kokoro:run Add this label to force Kokoro to re-run the tests. label May 7, 2020
Copy link
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.

Please rename the package to iam.snippets

@lesv lesv added kokoro:force-run Add this label to force Kokoro to re-run the tests. and removed kokoro:run Add this label to force Kokoro to re-run the tests. labels May 7, 2020
@kokoro-team kokoro-team removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label May 7, 2020
@melaniedejong
Copy link
Contributor Author

Please rename the package to iam.snippets

Can you explain why? All of the other samples have com.google.iam.snippets.

@lesv
Copy link
Contributor

lesv commented May 8, 2020

Someone didn't catch it. They aren't supposed to ever use com.google for samples. com.google is reserved for libraries we publish, not for sample code which users might grab and accidently publish.

Please read our Sample format guidelines

@melaniedejong
Copy link
Contributor Author

Gotcha. Done.

Copy link
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.

LGTM - consider the comment on the test before merging. (you may wish to fix it, we typically use a UUID instead)

Thank you for changing the package everywhere. That will change the path in the docs you'll wan't let a TW know or prep a CL yourself to fix the docs for the updated paths, before merging.

@lesv lesv added the kokoro:run Add this label to force Kokoro to re-run the tests. label May 8, 2020
@kokoro-team kokoro-team removed the kokoro:run Add this label to force Kokoro to re-run the tests. label May 8, 2020
@lesv
Copy link
Contributor

lesv commented May 8, 2020

Ah, didn't realize you are a TW, then you'll want to do a code search for include code usage. Feel free to IM me for more info. Also, feel free to ping one of us when you want to merge.

@lesv lesv added the kokoro:run Add this label to force Kokoro to re-run the tests. label May 12, 2020
@kokoro-team kokoro-team removed the kokoro:run Add this label to force Kokoro to re-run the tests. label May 12, 2020
@lesv lesv added the kokoro:run Add this label to force Kokoro to re-run the tests. label May 12, 2020
@kokoro-team kokoro-team removed the kokoro:run Add this label to force Kokoro to re-run the tests. label May 12, 2020
@lesv lesv merged commit e87c444 into GoogleCloudPlatform:master May 12, 2020
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.

5 participants