com.google.api.services.cloudresourcemanager.model.GetIamPolicyRequest Java Examples

The following examples show how to use com.google.api.services.cloudresourcemanager.model.GetIamPolicyRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GCPProject.java    From policyscanner with Apache License 2.0 6 votes vote down vote up
/**
 * Get the IAM policy binding this project.
 * @return The GCPResourcePolicy object which represents the policy binding this project.
 * @throws IOException Thrown if there's an IO error reading the policy.
 * @throws GeneralSecurityException Thrown if there's a permissions error reading the policy.
 */
@Override
public GCPResourcePolicy getPolicy() throws IOException, GeneralSecurityException {
  Map<String, List<String>> bindings = new HashMap<>();
  Policy policy = null;

  rateLimiter.acquire();

  try {
    policy = getProjectsApiStub()
        .getIamPolicy(this.id, new GetIamPolicyRequest())
        .execute();
  } catch (GeneralSecurityException gse) {
    throw new GeneralSecurityException(
        "Cannot fetch IAM policy for project + " + id + "\nMessage: " + gse.getMessage()
    );
  }
  if (policy != null && policy.getBindings() != null) {
    for (Binding binding : policy.getBindings()) {
      bindings.put(binding.getRole(), binding.getMembers());
    }
    return new GCPResourcePolicy(this, bindings);
  }
  return null;
}
 
Example #2
Source File: FilePathToLiveStateTest.java    From policyscanner with Apache License 2.0 6 votes vote down vote up
@Test
public void testValidFile() throws IOException {
  String projectId = "sampleProject";
  List<String> filePath = Arrays.asList("sampleOrg", projectId, "POLICY");
  GCPProject project = new GCPProject(projectId);
  GCPResourcePolicy gcpResourcePolicy = getSampleGCPResourcePolicy(project);
  Policy policy = getSamplePolicy();
  GetIamPolicy correctRequest = mock(GetIamPolicy.class);
  GetIamPolicy wrongRequest = mock(GetIamPolicy.class);
  when(projects.getIamPolicy(anyString(), any(GetIamPolicyRequest.class)))
      .thenReturn(wrongRequest);
  when(projects.getIamPolicy(eq(projectId), any(GetIamPolicyRequest.class)))
      .thenReturn(correctRequest);
  when(correctRequest.execute()).thenReturn(policy);
  when(wrongRequest.execute()).thenThrow(new NoSuchElementException());
  try {
    assertEquals(tester.processBatch(filePath), Arrays.asList(KV.of(project, gcpResourcePolicy)));
  } catch (IllegalArgumentException ignored) {
    fail("Exception thrown on valid statefile name");
  } catch (NoSuchElementException nse) {
    fail("Tried accessing the wrong project ID");
  }
}
 
Example #3
Source File: ExtractStateTest.java    From policyscanner with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws IOException {
  this.projectsApiObject = mock(Projects.class);
  GCPProject.setProjectsApiStub(this.projectsApiObject);

  this.getIamPolicy = mock(Projects.GetIamPolicy.class);
  this.tester = DoFnTester.of(new ExtractState());
  when(this.projectsApiObject.getIamPolicy(anyString(), any(GetIamPolicyRequest.class)))
      .thenReturn(this.getIamPolicy);
}
 
Example #4
Source File: ListServiceAccountsTest.java    From policyscanner with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws IOException {
  GCPProject.setProjectsApiStub(this.projectsApiObject);
  this.projectsApiObject = mock(Projects.class);
  this.getIamPolicy = mock(GetIamPolicy.class);
  this.tester = DoFnTester.of(new ExtractState());
  when(this.projectsApiObject.getIamPolicy(anyString(), any(GetIamPolicyRequest.class)))
      .thenReturn(this.getIamPolicy);
}
 
Example #5
Source File: OnDemandLiveStateCheckerTest.java    From policyscanner with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws GeneralSecurityException, IOException {
  GCPProject.setProjectsApiStub(projectsObject);

  CloudResourceManager.Projects.List emptyList = mock(CloudResourceManager.Projects.List.class);
  ListProjectsResponse emptyListProjectResponse = new ListProjectsResponse();

  when(projectsObject.list()).thenReturn(listProjects);
  when(listProjects.setPageToken(anyString())).thenReturn(emptyList);
  when(listProjects.setPageToken(null)).thenReturn(listProjects);
  when(listProjects.setFilter(anyString())).thenReturn(listProjects);

  when(emptyList.setPageToken(null)).thenReturn(emptyList);
  when(emptyList.setPageToken(anyString())).thenReturn(emptyList);
  when(emptyList.setFilter(anyString())).thenReturn(emptyList);

  when(emptyList.execute()).thenReturn(emptyListProjectResponse
      .setNextPageToken("maybe halt?")
      .setProjects(new ArrayList<Project>(0)));

  when(objectList.setPageToken(anyString())).thenReturn(objectList);
  when(objectList.setPageToken(null)).thenReturn(objectList);
  when(objectList.setPrefix(anyString())).thenReturn(objectList);

  when(objects.list(anyString())).thenReturn(objectList);
  when(objects.get(anyString(), anyString())).thenReturn(objectGet);
  when(gcs.objects()).thenReturn(objects);

  when(buckets.get(anyString())).thenReturn(bucketGet);
  when(gcs.buckets()).thenReturn(buckets);

  when(this.projectsObject.getIamPolicy(anyString(), any(GetIamPolicyRequest.class)))
      .thenReturn(this.getIamPolicy);

  GCSFilesSource.setStorageApiStub(gcs);
  this.checkedSource = new GCSFilesSource(BUCKET, ORG_ID);
}
 
Example #6
Source File: DesiredStateEnforcerTest.java    From policyscanner with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws GeneralSecurityException, IOException {
  GCPProject.setProjectsApiStub(projectsObject);

  CloudResourceManager.Projects.List emptyList = mock(CloudResourceManager.Projects.List.class);
  ListProjectsResponse emptyListProjectResponse = new ListProjectsResponse();

  when(projectsObject.list()).thenReturn(listProjects);
  when(listProjects.setPageToken(anyString())).thenReturn(emptyList);
  when(listProjects.setPageToken(null)).thenReturn(listProjects);
  when(listProjects.setFilter(anyString())).thenReturn(listProjects);

  when(emptyList.setPageToken(null)).thenReturn(emptyList);
  when(emptyList.setPageToken(anyString())).thenReturn(emptyList);
  when(emptyList.setFilter(anyString())).thenReturn(emptyList);

  when(emptyList.execute()).thenReturn(emptyListProjectResponse
      .setNextPageToken("maybe halt?")
      .setProjects(new ArrayList<Project>(0)));

  when(objectList.setPageToken(anyString())).thenReturn(objectList);
  when(objectList.setPageToken(null)).thenReturn(objectList);
  when(objectList.setPrefix(anyString())).thenReturn(objectList);

  when(objects.list(anyString())).thenReturn(objectList);
  when(objects.get(anyString(), anyString())).thenReturn(objectGet);
  when(gcs.objects()).thenReturn(objects);

  when(buckets.get(anyString())).thenReturn(bucketGet);
  when(gcs.buckets()).thenReturn(buckets);

  when(this.projectsObject.getIamPolicy(anyString(), any(GetIamPolicyRequest.class)))
      .thenReturn(this.getIamPolicy);

  GCSFilesSource.setStorageApiStub(gcs);
  this.checkedSource = new GCSFilesSource(BUCKET, ORG_ID);
}
 
Example #7
Source File: LiveStateCheckerTest.java    From policyscanner with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws GeneralSecurityException, IOException {
  GCPProject.setProjectsApiStub(projectsObject);

  CloudResourceManager.Projects.List emptyList = mock(CloudResourceManager.Projects.List.class);
  ListProjectsResponse emptyListProjectResponse = new ListProjectsResponse();

  when(projectsObject.list()).thenReturn(listProjects);
  when(listProjects.setPageToken(anyString())).thenReturn(emptyList);
  when(listProjects.setPageToken(null)).thenReturn(listProjects);
  when(listProjects.setFilter(anyString())).thenReturn(listProjects);

  when(emptyList.setPageToken(null)).thenReturn(emptyList);
  when(emptyList.setPageToken(anyString())).thenReturn(emptyList);
  when(emptyList.setFilter(anyString())).thenReturn(emptyList);

  when(emptyList.execute()).thenReturn(emptyListProjectResponse
      .setNextPageToken("maybe halt?")
      .setProjects(new ArrayList<Project>(0)));

  when(objectList.setPageToken(anyString())).thenReturn(objectList);
  when(objectList.setPageToken(null)).thenReturn(objectList);
  when(objectList.setPrefix(anyString())).thenReturn(objectList);

  when(objects.list(anyString())).thenReturn(objectList);
  when(objects.get(anyString(), anyString())).thenReturn(objectGet);
  when(gcs.objects()).thenReturn(objects);

  when(buckets.get(anyString())).thenReturn(bucketGet);
  when(gcs.buckets()).thenReturn(buckets);

  when(this.projectsObject.getIamPolicy(anyString(), any(GetIamPolicyRequest.class)))
      .thenReturn(this.getIamPolicy);

  GCSFilesSource.setStorageApiStub(gcs);
  this.checkedSource = new GCSFilesSource(BUCKET, ORG_ID);
}
 
Example #8
Source File: QuickstartV2.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static Policy getPolicy(CloudResourceManager crmService, String projectId) {
  // Gets the project's policy by calling the
  // Cloud Resource Manager Projects API.
  Policy policy = null;
  try {
    GetIamPolicyRequest request = new GetIamPolicyRequest();
    policy = crmService.projects().getIamPolicy(projectId, request).execute();
  } catch (IOException e) {
    System.out.println("Unable to get policy: \n" + e.toString());
  }
  return policy;
}