use of com.google.cloud.Identity in project google-cloud-java by GoogleCloudPlatform.
the class ModifyPolicy method main.
public static void main(String... args) {
// Create Resource Manager service object
// By default, credentials are inferred from the runtime environment.
ResourceManager resourceManager = ResourceManagerOptions.getDefaultInstance().getService();
// Get a project from the server
// Use an existing project's ID
String projectId = "some-project-id";
Project project = resourceManager.get(projectId);
// Get the project's policy
Policy policy = project.getPolicy();
// Add a viewer
Policy.Builder modifiedPolicy = policy.toBuilder();
Identity newViewer = Identity.user("<insert user's email address here>");
modifiedPolicy.addIdentity(Role.viewer(), newViewer);
// Write policy
Policy updatedPolicy = project.replacePolicy(modifiedPolicy.build());
// Print policy
System.out.printf("Updated policy for %s: %n%s%n", projectId, updatedPolicy);
}
use of com.google.cloud.Identity in project google-cloud-java by GoogleCloudPlatform.
the class PolicyMarshaller method toPb.
@Override
protected com.google.api.services.cloudresourcemanager.model.Policy toPb(Policy policy) {
com.google.api.services.cloudresourcemanager.model.Policy policyPb = new com.google.api.services.cloudresourcemanager.model.Policy();
List<Binding> bindingPbList = new LinkedList<>();
for (Map.Entry<Role, Set<Identity>> binding : policy.getBindings().entrySet()) {
Binding bindingPb = new Binding();
bindingPb.setRole(binding.getKey().getValue());
bindingPb.setMembers(Lists.transform(new ArrayList<>(binding.getValue()), new Function<Identity, String>() {
@Override
public String apply(Identity identity) {
return IDENTITY_STR_VALUE_FUNCTION.apply(identity);
}
}));
bindingPbList.add(bindingPb);
}
policyPb.setBindings(bindingPbList);
policyPb.setEtag(policy.getEtag());
policyPb.setVersion(policy.getVersion());
return policyPb;
}
use of com.google.cloud.Identity in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testBucketPolicy.
@Test
public void testBucketPolicy() {
String projectId = remoteStorageHelper.getOptions().getProjectId();
Identity projectOwner = Identity.projectOwner(projectId);
Identity projectEditor = Identity.projectEditor(projectId);
Identity projectViewer = Identity.projectViewer(projectId);
Map<com.google.cloud.Role, Set<Identity>> bindingsWithoutPublicRead = ImmutableMap.of(StorageRoles.legacyBucketOwner(), (Set<Identity>) newHashSet(projectOwner, projectEditor), StorageRoles.legacyBucketReader(), newHashSet(projectViewer));
Map<com.google.cloud.Role, Set<Identity>> bindingsWithPublicRead = ImmutableMap.of(StorageRoles.legacyBucketOwner(), (Set<Identity>) newHashSet(projectOwner, projectEditor), StorageRoles.legacyBucketReader(), newHashSet(projectViewer), StorageRoles.legacyObjectReader(), newHashSet(Identity.allUsers()));
// Validate getting policy.
Policy currentPolicy = storage.getIamPolicy(BUCKET);
assertEquals(bindingsWithoutPublicRead, currentPolicy.getBindings());
// Validate updating policy.
Policy updatedPolicy = storage.setIamPolicy(BUCKET, currentPolicy.toBuilder().addIdentity(StorageRoles.legacyObjectReader(), Identity.allUsers()).build());
assertEquals(bindingsWithPublicRead, updatedPolicy.getBindings());
Policy revertedPolicy = storage.setIamPolicy(BUCKET, updatedPolicy.toBuilder().removeIdentity(StorageRoles.legacyObjectReader(), Identity.allUsers()).build());
assertEquals(bindingsWithoutPublicRead, revertedPolicy.getBindings());
// Validate testing permissions.
List<Boolean> expectedPermissions = ImmutableList.of(true, true);
assertEquals(expectedPermissions, storage.testIamPermissions(BUCKET, ImmutableList.of("storage.buckets.getIamPolicy", "storage.buckets.setIamPolicy")));
}
use of com.google.cloud.Identity in project google-cloud-java by GoogleCloudPlatform.
the class PolicyHelper method convertToApiPolicy.
static com.google.api.services.storage.model.Policy convertToApiPolicy(Policy policy) {
List<Bindings> bindings = new ArrayList<>(policy.getBindings().size());
for (Map.Entry<Role, Set<Identity>> entry : policy.getBindings().entrySet()) {
List<String> members = new ArrayList<>(entry.getValue().size());
for (Identity identity : entry.getValue()) {
members.add(identity.strValue());
}
bindings.add(new Bindings().setMembers(members).setRole(entry.getKey().getValue()));
}
return new com.google.api.services.storage.model.Policy().setBindings(bindings).setEtag(policy.getEtag());
}
Aggregations