|
30 | 30 | import com.google.api.services.cloudiot.v1.model.DeviceRegistry;
|
31 | 31 | import com.google.api.services.cloudiot.v1.model.DeviceState;
|
32 | 32 | import com.google.api.services.cloudiot.v1.model.EventNotificationConfig;
|
| 33 | +import com.google.api.services.cloudiot.v1.model.GetIamPolicyRequest; |
33 | 34 | import com.google.api.services.cloudiot.v1.model.ListDeviceStatesResponse;
|
34 | 35 | import com.google.api.services.cloudiot.v1.model.ModifyCloudToDeviceConfigRequest;
|
35 | 36 | import com.google.api.services.cloudiot.v1.model.PublicKeyCredential;
|
| 37 | +import com.google.api.services.cloudiot.v1.model.SetIamPolicyRequest; |
36 | 38 | import com.google.cloud.Role;
|
37 | 39 | import com.google.cloud.pubsub.v1.TopicAdminClient;
|
38 | 40 | import com.google.common.io.Files;
|
39 | 41 | import com.google.iam.v1.Binding;
|
40 |
| -import com.google.iam.v1.Policy; |
41 | 42 | import com.google.pubsub.v1.Topic;
|
42 | 43 | import com.google.pubsub.v1.TopicName;
|
43 | 44 |
|
|
48 | 49 | import java.util.Arrays;
|
49 | 50 | import java.util.Base64;
|
50 | 51 | import java.util.List;
|
51 |
| -import javax.xml.bind.DatatypeConverter; |
52 | 52 | import org.apache.commons.cli.HelpFormatter;
|
53 | 53 |
|
54 | 54 | /**
|
@@ -93,15 +93,16 @@ public static Topic createIotTopic(String projectId, String topicId) throws Exce
|
93 | 93 |
|
94 | 94 | try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
|
95 | 95 | final Topic topic = topicAdminClient.createTopic(topicName);
|
96 |
| - Policy policy = topicAdminClient.getIamPolicy(topicName.toString()); |
| 96 | + com.google.iam.v1.Policy policy = topicAdminClient.getIamPolicy(topicName.toString()); |
97 | 97 | // add role -> members binding
|
98 | 98 | Binding binding =
|
99 | 99 | Binding.newBuilder()
|
100 | 100 | . addMembers( "serviceAccount:[email protected]")
|
101 | 101 | .setRole(Role.owner().toString())
|
102 | 102 | .build();
|
103 | 103 | // create updated policy
|
104 |
| - Policy updatedPolicy = Policy.newBuilder(policy).addBindings(binding).build(); |
| 104 | + com.google.iam.v1.Policy updatedPolicy = |
| 105 | + com.google.iam.v1.Policy.newBuilder(policy).addBindings(binding).build(); |
105 | 106 | topicAdminClient.setIamPolicy(topicName.toString(), updatedPolicy);
|
106 | 107 |
|
107 | 108 | System.out.println("Setup topic / policy for: " + topic.getName());
|
@@ -578,6 +579,114 @@ public static void setDeviceConfiguration(
|
578 | 579 | System.out.println("Updated: " + config.getVersion());
|
579 | 580 | }
|
580 | 581 |
|
| 582 | + /** Retrieves IAM permissions for the given registry. */ |
| 583 | + public static void getIamPermissions( |
| 584 | + String projectId, String cloudRegion, String registryName) |
| 585 | + throws GeneralSecurityException, IOException { |
| 586 | + GoogleCredential credential = |
| 587 | + GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all()); |
| 588 | + JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); |
| 589 | + HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential); |
| 590 | + final CloudIot service = new CloudIot.Builder( |
| 591 | + GoogleNetHttpTransport.newTrustedTransport(),jsonFactory, init) |
| 592 | + .setApplicationName(APP_NAME).build(); |
| 593 | + |
| 594 | + final String registryPath = String.format("projects/%s/locations/%s/registries/%s", |
| 595 | + projectId, cloudRegion, registryName); |
| 596 | + |
| 597 | + com.google.api.services.cloudiot.v1.model.Policy policy = |
| 598 | + service |
| 599 | + .projects() |
| 600 | + .locations() |
| 601 | + .registries() |
| 602 | + .getIamPolicy(registryPath, new GetIamPolicyRequest()).execute(); |
| 603 | + |
| 604 | + System.out.println("Policy ETAG: " + policy.getEtag()); |
| 605 | + |
| 606 | + if (policy.getBindings() != null) { |
| 607 | + for (com.google.api.services.cloudiot.v1.model.Binding binding : policy.getBindings()) { |
| 608 | + System.out.println(String.format("Role: %s", binding.getRole())); |
| 609 | + System.out.println("Binding members: "); |
| 610 | + for (String member : binding.getMembers()) { |
| 611 | + System.out.println(String.format("\t%s", member)); |
| 612 | + } |
| 613 | + } |
| 614 | + } else { |
| 615 | + System.out.println(String.format("No policy bindings for %s", registryName)); |
| 616 | + } |
| 617 | + } |
| 618 | + |
| 619 | + /** Sets IAM permissions for the given registry. */ |
| 620 | + public static void setIamPermissions( |
| 621 | + String projectId, String cloudRegion, String registryName, |
| 622 | + String member, String role) |
| 623 | + throws GeneralSecurityException, IOException { |
| 624 | + GoogleCredential credential = |
| 625 | + GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all()); |
| 626 | + JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); |
| 627 | + HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential); |
| 628 | + final CloudIot service = new CloudIot.Builder( |
| 629 | + GoogleNetHttpTransport.newTrustedTransport(),jsonFactory, init) |
| 630 | + .setApplicationName(APP_NAME).build(); |
| 631 | + |
| 632 | + final String registryPath = String.format("projects/%s/locations/%s/registries/%s", |
| 633 | + projectId, cloudRegion, registryName); |
| 634 | + |
| 635 | + com.google.api.services.cloudiot.v1.model.Policy policy = |
| 636 | + service |
| 637 | + .projects() |
| 638 | + .locations() |
| 639 | + .registries() |
| 640 | + .getIamPolicy(registryPath, new GetIamPolicyRequest()).execute(); |
| 641 | + |
| 642 | + List<com.google.api.services.cloudiot.v1.model.Binding> bindings = |
| 643 | + policy.getBindings(); |
| 644 | + |
| 645 | + boolean addNewRole = true; |
| 646 | + if (bindings != null) { |
| 647 | + for (com.google.api.services.cloudiot.v1.model.Binding binding : bindings) { |
| 648 | + if (binding.getRole().equals(role)) { |
| 649 | + List<String> members = binding.getMembers(); |
| 650 | + members.add(member); |
| 651 | + binding.setMembers(members); |
| 652 | + addNewRole = false; |
| 653 | + } |
| 654 | + } |
| 655 | + } else { |
| 656 | + bindings = new ArrayList<>(); |
| 657 | + } |
| 658 | + |
| 659 | + if (addNewRole) { |
| 660 | + com.google.api.services.cloudiot.v1.model.Binding bind = |
| 661 | + new com.google.api.services.cloudiot.v1.model.Binding(); |
| 662 | + bind.setRole(role); |
| 663 | + List<String> members = new ArrayList<>(); |
| 664 | + members.add(member); |
| 665 | + bind.setMembers(members); |
| 666 | + |
| 667 | + bindings.add(bind); |
| 668 | + } |
| 669 | + |
| 670 | + policy.setBindings(bindings); |
| 671 | + SetIamPolicyRequest req = new SetIamPolicyRequest().setPolicy(policy); |
| 672 | + |
| 673 | + policy = |
| 674 | + service |
| 675 | + .projects() |
| 676 | + .locations() |
| 677 | + .registries() |
| 678 | + .setIamPolicy(registryPath, req).execute(); |
| 679 | + |
| 680 | + System.out.println("Policy ETAG: " + policy.getEtag()); |
| 681 | + for (com.google.api.services.cloudiot.v1.model.Binding binding: policy.getBindings()) { |
| 682 | + System.out.println(String.format("Role: %s", binding.getRole())); |
| 683 | + System.out.println("Binding members: "); |
| 684 | + for (String mem : binding.getMembers()) { |
| 685 | + System.out.println(String.format("\t%s", mem)); |
| 686 | + } |
| 687 | + } |
| 688 | + } |
| 689 | + |
581 | 690 | /** Entry poit for CLI. */
|
582 | 691 | public static void main(String[] args) throws Exception {
|
583 | 692 | DeviceRegistryExampleOptions options = DeviceRegistryExampleOptions.fromFlags(args);
|
@@ -626,6 +735,10 @@ public static void main(String[] args) throws Exception {
|
626 | 735 | options.registryName)
|
627 | 736 | .toPrettyString());
|
628 | 737 | break;
|
| 738 | + case "get-iam-permissions": |
| 739 | + System.out.println("Get iam permissions"); |
| 740 | + getIamPermissions(options.projectId, options.cloudRegion, options.registryName); |
| 741 | + break; |
629 | 742 | case "get-device-state":
|
630 | 743 | System.out.println("Get device state");
|
631 | 744 | List<DeviceState> states = getDeviceStates(options.deviceId, options.projectId,
|
@@ -666,6 +779,15 @@ public static void main(String[] args) throws Exception {
|
666 | 779 | options.registryName, options.configuration, options.version);
|
667 | 780 | }
|
668 | 781 | break;
|
| 782 | + case "set-iam-permissions": |
| 783 | + if (options.member == null || options.role == null) { |
| 784 | + System.out.println("Specify member and role for the policy you are updating."); |
| 785 | + } else { |
| 786 | + System.out.println("Setting iam permissions"); |
| 787 | + setIamPermissions(options.projectId, options.cloudRegion, options.registryName, |
| 788 | + options.member, options.role); |
| 789 | + } |
| 790 | + break; |
669 | 791 | default:
|
670 | 792 | String header = "Cloud IoT Core Commandline Example (Device / Registry management): \n\n";
|
671 | 793 | String footer = "\nhttps://cloud.google.com/iot-core";
|
|
0 commit comments