Search in sources :

Example 1 with ListServiceAccountsResponse

use of com.google.api.services.iam.v1.model.ListServiceAccountsResponse in project cloudbreak by hortonworks.

the class GcpPlatformResources method accessConfigs.

@Override
public CloudAccessConfigs accessConfigs(ExtendedCloudCredential cloudCredential, Region region, Map<String, String> filters) {
    Iam iam = gcpIamFactory.buildIam(cloudCredential);
    String projectId = gcpStackUtil.getProjectId(cloudCredential);
    Set<CloudAccessConfig> collect = new HashSet<>();
    try {
        Iam.Projects.ServiceAccounts.List listServiceAccountEmailsRequest = iam.projects().serviceAccounts().list("projects/" + projectId).setPageSize(DEFAULT_PAGE_SIZE);
        ListServiceAccountsResponse response;
        do {
            response = listServiceAccountEmailsRequest.execute();
            Set<CloudAccessConfig> accessConfigs = response.getAccounts().stream().map(e -> new CloudAccessConfig(e.getName(), e.getEmail(), new HashMap<>())).collect(Collectors.toSet());
            collect.addAll(accessConfigs);
            listServiceAccountEmailsRequest.setPageToken(response.getNextPageToken());
        } while (response.getNextPageToken() != null);
        return new CloudAccessConfigs(collect);
    } catch (Exception ex) {
        return new CloudAccessConfigs(collect);
    }
}
Also used : ListServiceAccountsResponse(com.google.api.services.iam.v1.model.ListServiceAccountsResponse) Arrays(java.util.Arrays) CloudVmTypes(com.sequenceiq.cloudbreak.cloud.model.CloudVmTypes) ExtendedCloudCredential(com.sequenceiq.cloudbreak.cloud.model.ExtendedCloudCredential) MinimalHardwareFilter(com.sequenceiq.cloudbreak.filter.MinimalHardwareFilter) Cacheable(org.springframework.cache.annotation.Cacheable) LoggerFactory(org.slf4j.LoggerFactory) JsonUtil(com.sequenceiq.cloudbreak.common.json.JsonUtil) Network(com.google.api.services.compute.model.Network) StringUtils(org.apache.commons.lang3.StringUtils) Matcher(java.util.regex.Matcher) CloudAccessConfig(com.sequenceiq.cloudbreak.cloud.model.CloudAccessConfig) CloudSshKeys(com.sequenceiq.cloudbreak.cloud.model.CloudSshKeys) GcpComputeFactory(com.sequenceiq.cloudbreak.cloud.gcp.client.GcpComputeFactory) Map(java.util.Map) RegionList(com.google.api.services.compute.model.RegionList) CloudEncryptionKey(com.sequenceiq.cloudbreak.cloud.model.CloudEncryptionKey) GcpStackUtil(com.sequenceiq.cloudbreak.cloud.gcp.util.GcpStackUtil) VmTypeMeta(com.sequenceiq.cloudbreak.cloud.model.VmTypeMeta) Predicate(java.util.function.Predicate) ListCryptoKeysResponse(com.google.api.services.cloudkms.v1.model.ListCryptoKeysResponse) CloudEncryptionKeys(com.sequenceiq.cloudbreak.cloud.model.CloudEncryptionKeys) CloudResourceGroups(com.sequenceiq.cloudbreak.cloud.model.resourcegroup.CloudResourceGroups) Set(java.util.Set) CryptoKey(com.google.api.services.cloudkms.v1.model.CryptoKey) Collectors(java.util.stream.Collectors) VmType(com.sequenceiq.cloudbreak.cloud.model.VmType) CloudNoSqlTables(com.sequenceiq.cloudbreak.cloud.model.nosql.CloudNoSqlTables) RegionCoordinateSpecifications(com.sequenceiq.cloudbreak.cloud.model.RegionCoordinateSpecifications) List(java.util.List) MachineTypeList(com.google.api.services.compute.model.MachineTypeList) CloudKMS(com.google.api.services.cloudkms.v1.CloudKMS) SubnetworkList(com.google.api.services.compute.model.SubnetworkList) CloudPrivateDnsZones(com.sequenceiq.cloudbreak.cloud.model.dns.CloudPrivateDnsZones) Coordinate.coordinate(com.sequenceiq.cloudbreak.cloud.model.Coordinate.coordinate) GcpCloudKMSFactory(com.sequenceiq.cloudbreak.cloud.gcp.client.GcpCloudKMSFactory) PostConstruct(javax.annotation.PostConstruct) FirewallList(com.google.api.services.compute.model.FirewallList) Optional(java.util.Optional) Coordinate(com.sequenceiq.cloudbreak.cloud.model.Coordinate) Pattern(java.util.regex.Pattern) Compute(com.google.api.services.compute.Compute) ListKeyRingsResponse(com.google.api.services.cloudkms.v1.model.ListKeyRingsResponse) CloudSecurityGroup(com.sequenceiq.cloudbreak.cloud.model.CloudSecurityGroup) CloudbreakResourceReaderService(com.sequenceiq.cloudbreak.service.CloudbreakResourceReaderService) CloudIpPools(com.sequenceiq.cloudbreak.cloud.model.CloudIpPools) Region(com.sequenceiq.cloudbreak.cloud.model.Region) HashMap(java.util.HashMap) Function(java.util.function.Function) CloudGateWays(com.sequenceiq.cloudbreak.cloud.model.CloudGateWays) CloudNetworks(com.sequenceiq.cloudbreak.cloud.model.CloudNetworks) CloudRegions(com.sequenceiq.cloudbreak.cloud.model.CloudRegions) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) Value(org.springframework.beans.factory.annotation.Value) AvailabilityZone(com.sequenceiq.cloudbreak.cloud.model.AvailabilityZone) Strings(com.google.common.base.Strings) PUBLIC(com.sequenceiq.cloudbreak.cloud.model.network.SubnetType.PUBLIC) NetworkList(com.google.api.services.compute.model.NetworkList) Firewall(com.google.api.services.compute.model.Firewall) Service(org.springframework.stereotype.Service) GcpIamFactory(com.sequenceiq.cloudbreak.cloud.gcp.client.GcpIamFactory) KeyRing(com.google.api.services.cloudkms.v1.model.KeyRing) PRIVATE(com.sequenceiq.cloudbreak.cloud.model.network.SubnetType.PRIVATE) RegionCoordinateSpecification(com.sequenceiq.cloudbreak.cloud.model.RegionCoordinateSpecification) CloudSubnet(com.sequenceiq.cloudbreak.cloud.model.CloudSubnet) Logger(org.slf4j.Logger) ListServiceAccountsResponse(com.google.api.services.iam.v1.model.ListServiceAccountsResponse) Subnetwork(com.google.api.services.compute.model.Subnetwork) PlatformResources(com.sequenceiq.cloudbreak.cloud.PlatformResources) VmTypeMetaBuilder(com.sequenceiq.cloudbreak.cloud.model.VmTypeMeta.VmTypeMetaBuilder) Region.region(com.sequenceiq.cloudbreak.cloud.model.Region.region) IOException(java.io.IOException) CloudAccessConfigs(com.sequenceiq.cloudbreak.cloud.model.CloudAccessConfigs) MachineType(com.google.api.services.compute.model.MachineType) TokenResponseException(com.google.api.client.auth.oauth2.TokenResponseException) CloudSecurityGroups(com.sequenceiq.cloudbreak.cloud.model.CloudSecurityGroups) Iam(com.google.api.services.iam.v1.Iam) Collections(java.util.Collections) CloudNetwork(com.sequenceiq.cloudbreak.cloud.model.CloudNetwork) SHARED_PROJECT_ID(com.sequenceiq.cloudbreak.cloud.gcp.util.GcpStackUtil.SHARED_PROJECT_ID) CloudAccessConfig(com.sequenceiq.cloudbreak.cloud.model.CloudAccessConfig) Iam(com.google.api.services.iam.v1.Iam) CloudAccessConfigs(com.sequenceiq.cloudbreak.cloud.model.CloudAccessConfigs) IOException(java.io.IOException) TokenResponseException(com.google.api.client.auth.oauth2.TokenResponseException) HashSet(java.util.HashSet)

Example 2 with ListServiceAccountsResponse

use of com.google.api.services.iam.v1.model.ListServiceAccountsResponse in project platinum by hartwigmedical.

the class EphemeralPipelineServiceAccountTest method setUp.

@Before
public void setUp() throws Exception {
    final Iam iam = mock(Iam.class);
    Iam.Projects projects = mock(Iam.Projects.class);
    serviceAccounts = mock(Iam.Projects.ServiceAccounts.class);
    Iam.Projects.ServiceAccounts.List listServiceAccounts = mock(Iam.Projects.ServiceAccounts.List.class);
    listServiceAccountsResponse = mock(ListServiceAccountsResponse.class);
    when(iam.projects()).thenReturn(projects);
    when(projects.serviceAccounts()).thenReturn(serviceAccounts);
    when(serviceAccounts.list(PROJECT_RESOURCE_NAME)).thenReturn(listServiceAccounts);
    when(listServiceAccounts.execute()).thenReturn(listServiceAccountsResponse);
    Iam.Projects.ServiceAccounts.Create create = mock(Iam.Projects.ServiceAccounts.Create.class);
    serviceAccount = mock(ServiceAccount.class);
    projectArgumentCaptor = ArgumentCaptor.forClass(String.class);
    createServiceAccountRequestArgumentCaptor = ArgumentCaptor.forClass(CreateServiceAccountRequest.class);
    when(create.execute()).thenReturn(serviceAccount);
    when(serviceAccounts.create(projectArgumentCaptor.capture(), createServiceAccountRequestArgumentCaptor.capture())).thenReturn(create);
    when(serviceAccount.getEmail()).thenReturn(EMAIL);
    iamPolicy = mock(PipelineIamPolicy.class);
    victim = new EphemeralPipelineServiceAccount(iam, iamPolicy, RUN_NAME, PROJECT);
}
Also used : ListServiceAccountsResponse(com.google.api.services.iam.v1.model.ListServiceAccountsResponse) ServiceAccount(com.google.api.services.iam.v1.model.ServiceAccount) Iam(com.google.api.services.iam.v1.Iam) CreateServiceAccountRequest(com.google.api.services.iam.v1.model.CreateServiceAccountRequest) Before(org.junit.Before)

Example 3 with ListServiceAccountsResponse

use of com.google.api.services.iam.v1.model.ListServiceAccountsResponse in project java-docs-samples by GoogleCloudPlatform.

the class ListServiceAccounts method listServiceAccounts.

// Lists all service accounts for the current project.
public static void listServiceAccounts(String projectId) {
    // String projectId = "my-project-id"
    Iam service = null;
    try {
        service = initService();
    } catch (IOException | GeneralSecurityException e) {
        System.out.println("Unable to initialize service: \n" + e.toString());
        return;
    }
    try {
        ListServiceAccountsResponse response = service.projects().serviceAccounts().list("projects/" + projectId).execute();
        List<ServiceAccount> serviceAccounts = response.getAccounts();
        for (ServiceAccount account : serviceAccounts) {
            System.out.println("Name: " + account.getName());
            System.out.println("Display Name: " + account.getDisplayName());
            System.out.println("Email: " + account.getEmail());
            System.out.println();
        }
    } catch (IOException e) {
        System.out.println("Unable to list service accounts: \n" + e.toString());
    }
}
Also used : ListServiceAccountsResponse(com.google.api.services.iam.v1.model.ListServiceAccountsResponse) ServiceAccount(com.google.api.services.iam.v1.model.ServiceAccount) Iam(com.google.api.services.iam.v1.Iam) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException)

Example 4 with ListServiceAccountsResponse

use of com.google.api.services.iam.v1.model.ListServiceAccountsResponse in project cloudbreak by hortonworks.

the class GcpServiceAccountObjectStorageValidator method validateObjectStorage.

public ValidationResultBuilder validateObjectStorage(CloudCredential cloudCredential, SpiFileSystem spiFileSystem, ValidationResultBuilder resultBuilder) throws IOException {
    LOGGER.info("Validating Gcp identities...");
    Iam iam = gcpIamFactory.buildIam(cloudCredential);
    List<CloudFileSystemView> cloudFileSystems = spiFileSystem.getCloudFileSystems();
    if (Objects.nonNull(cloudFileSystems) && cloudFileSystems.size() > 0) {
        String projectId = gcpStackUtil.getProjectId(cloudCredential);
        Set<String> serviceAccountEmailsToFind = cloudFileSystems.stream().map(cloudFileSystemView -> ((CloudGcsView) cloudFileSystemView).getServiceAccountEmail()).collect(Collectors.toSet());
        Iam.Projects.ServiceAccounts.List listServiceAccountEmailsRequest = iam.projects().serviceAccounts().list("projects/" + projectId).setPageSize(DEFAULT_PAGE_SIZE);
        ListServiceAccountsResponse response;
        do {
            response = listServiceAccountEmailsRequest.execute();
            response.getAccounts().forEach(serviceAccount -> serviceAccountEmailsToFind.remove(serviceAccount.getEmail()));
            listServiceAccountEmailsRequest.setPageToken(response.getNextPageToken());
        } while (response.getNextPageToken() != null && !serviceAccountEmailsToFind.isEmpty());
        if (!serviceAccountEmailsToFind.isEmpty()) {
            addError(resultBuilder, String.format("Service Account with email(s) '%s' could not be found in the configured Google Cloud project '%s'.", String.join(", ", serviceAccountEmailsToFind), projectId));
        }
    }
    return resultBuilder;
}
Also used : Logger(org.slf4j.Logger) ListServiceAccountsResponse(com.google.api.services.iam.v1.model.ListServiceAccountsResponse) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) SpiFileSystem(com.sequenceiq.cloudbreak.cloud.model.SpiFileSystem) IOException(java.io.IOException) CloudFileSystemView(com.sequenceiq.cloudbreak.cloud.model.filesystem.CloudFileSystemView) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Inject(javax.inject.Inject) List(java.util.List) Component(org.springframework.stereotype.Component) ValidationResultBuilder(com.sequenceiq.cloudbreak.validation.ValidationResult.ValidationResultBuilder) Iam(com.google.api.services.iam.v1.Iam) GcpIamFactory(com.sequenceiq.cloudbreak.cloud.gcp.client.GcpIamFactory) CloudGcsView(com.sequenceiq.cloudbreak.cloud.model.filesystem.CloudGcsView) GcpStackUtil(com.sequenceiq.cloudbreak.cloud.gcp.util.GcpStackUtil) ListServiceAccountsResponse(com.google.api.services.iam.v1.model.ListServiceAccountsResponse) CloudGcsView(com.sequenceiq.cloudbreak.cloud.model.filesystem.CloudGcsView) Iam(com.google.api.services.iam.v1.Iam) CloudFileSystemView(com.sequenceiq.cloudbreak.cloud.model.filesystem.CloudFileSystemView)

Example 5 with ListServiceAccountsResponse

use of com.google.api.services.iam.v1.model.ListServiceAccountsResponse in project platinum by hartwigmedical.

the class EphemeralPipelineServiceAccount method projectServiceAccounts.

private static List<ServiceAccount> projectServiceAccounts(final Iam iam, final String projectResourceName) throws IOException {
    ListServiceAccountsResponse response = iam.projects().serviceAccounts().list(projectResourceName).execute();
    List<ServiceAccount> accounts = response.getAccounts();
    while (response.getNextPageToken() != null) {
        response = iam.projects().serviceAccounts().list(projectResourceName).setPageSize(100).setPageToken(response.getNextPageToken()).execute();
        if (response.getAccounts() != null) {
            accounts.addAll(response.getAccounts());
        }
    }
    return accounts;
}
Also used : ListServiceAccountsResponse(com.google.api.services.iam.v1.model.ListServiceAccountsResponse) ServiceAccount(com.google.api.services.iam.v1.model.ServiceAccount)

Aggregations

ListServiceAccountsResponse (com.google.api.services.iam.v1.model.ListServiceAccountsResponse)5 Iam (com.google.api.services.iam.v1.Iam)4 ServiceAccount (com.google.api.services.iam.v1.model.ServiceAccount)3 GcpIamFactory (com.sequenceiq.cloudbreak.cloud.gcp.client.GcpIamFactory)2 GcpStackUtil (com.sequenceiq.cloudbreak.cloud.gcp.util.GcpStackUtil)2 IOException (java.io.IOException)2 TokenResponseException (com.google.api.client.auth.oauth2.TokenResponseException)1 CloudKMS (com.google.api.services.cloudkms.v1.CloudKMS)1 CryptoKey (com.google.api.services.cloudkms.v1.model.CryptoKey)1 KeyRing (com.google.api.services.cloudkms.v1.model.KeyRing)1 ListCryptoKeysResponse (com.google.api.services.cloudkms.v1.model.ListCryptoKeysResponse)1 ListKeyRingsResponse (com.google.api.services.cloudkms.v1.model.ListKeyRingsResponse)1 Compute (com.google.api.services.compute.Compute)1 Firewall (com.google.api.services.compute.model.Firewall)1 FirewallList (com.google.api.services.compute.model.FirewallList)1 MachineType (com.google.api.services.compute.model.MachineType)1 MachineTypeList (com.google.api.services.compute.model.MachineTypeList)1 Network (com.google.api.services.compute.model.Network)1 NetworkList (com.google.api.services.compute.model.NetworkList)1 RegionList (com.google.api.services.compute.model.RegionList)1