Search in sources :

Example 1 with Directory

use of com.google.api.services.directory.Directory in project styx by spotify.

the class ServiceAccountUsageAuthorizer method create.

static ServiceAccountUsageAuthorizer create(Closer closer, String serviceAccountUserRole, AuthorizationPolicy authorizationPolicy, GoogleCredentials credentials, String gsuiteUserEmail, String serviceName, String message, List<String> administrators, List<String> blacklist) {
    final HttpTransport httpTransport;
    try {
        httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    } catch (GeneralSecurityException | IOException e) {
        throw new RuntimeException(e);
    }
    final JsonFactory jsonFactory = Utils.getDefaultJsonFactory();
    final CloudResourceManager crm = new CloudResourceManager.Builder(httpTransport, jsonFactory, new HttpCredentialsAdapter(credentials.createScoped(IamScopes.all()))).setApplicationName(serviceName).build();
    final Iam iam = new Iam.Builder(httpTransport, jsonFactory, new HttpCredentialsAdapter(credentials.createScoped(IamScopes.all()))).setApplicationName(serviceName).build();
    final IamCredentialsClient iamCredentialsClient;
    try {
        iamCredentialsClient = IamCredentialsClient.create();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    closer.register(iamCredentialsClient::close);
    final GoogleCredential directoryCredential = new ManagedServiceAccountKeyCredential.Builder(iamCredentialsClient).setServiceAccountId(ServiceAccounts.serviceAccountEmail(credentials)).setServiceAccountUser(gsuiteUserEmail).setServiceAccountScopes(Set.of(ADMIN_DIRECTORY_GROUP_MEMBER_READONLY)).build();
    final Directory directory = new Directory.Builder(httpTransport, jsonFactory, directoryCredential).setApplicationName(serviceName).build();
    return new Impl(iam, crm, directory, serviceAccountUserRole, authorizationPolicy, Impl.DEFAULT_WAIT_STRATEGY, Impl.DEFAULT_RETRY_STOP_STRATEGY, message, administrators, blacklist);
}
Also used : HttpCredentialsAdapter(com.google.auth.http.HttpCredentialsAdapter) CloudResourceManager(com.google.api.services.cloudresourcemanager.CloudResourceManager) GeneralSecurityException(java.security.GeneralSecurityException) RetryerBuilder(com.github.rholder.retry.RetryerBuilder) CacheBuilder(com.google.common.cache.CacheBuilder) JsonFactory(com.google.api.client.json.JsonFactory) IOException(java.io.IOException) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) HttpTransport(com.google.api.client.http.HttpTransport) GoogleNetHttpTransport(com.google.api.client.googleapis.javanet.GoogleNetHttpTransport) Iam(com.google.api.services.iam.v1.Iam) IamCredentialsClient(com.google.cloud.iam.credentials.v1.IamCredentialsClient) Directory(com.google.api.services.directory.Directory)

Example 2 with Directory

use of com.google.api.services.directory.Directory in project perun by CESNET.

the class ExtSourceGoogleTest method setUp.

@Before
public void setUp() throws Exception {
    extSourceGoogle = new ExtSourceGoogle();
    Members members = fillInMemberList();
    domainName = "parker.sm";
    groupName = "spectacular";
    MockitoAnnotations.initMocks(this);
    // mock google connection
    Directory directory = mock(Directory.class, RETURNS_DEEP_STUBS);
    doReturn(directory).when(extSourceGoogle).getDirectoryService();
    when(directory.members().list(groupName).execute()).thenReturn(members);
}
Also used : Members(com.google.api.services.directory.model.Members) Directory(com.google.api.services.directory.Directory) Before(org.junit.Before)

Example 3 with Directory

use of com.google.api.services.directory.Directory in project workbench by all-of-us.

the class DirectoryServiceImpl method countUsersInDomain.

private long countUsersInDomain(String gSuiteDomain) {
    long result = 0;
    try {
        final Directory directoryService = getGoogleDirectoryService();
        Optional<String> nextPageToken = Optional.empty();
        do {
            final Directory.Users.List listQuery = directoryService.users().list().setDomain(gSuiteDomain).setViewType(USER_VIEW_TYPE).setCustomFieldMask("email").setMaxResults(MAX_USERS_LIST_PAGE_SIZE).setOrderBy(EMAIL_USER_FIELD);
            nextPageToken.ifPresent(listQuery::setPageToken);
            final Users usersQueryResult = listQuery.execute();
            result += usersQueryResult.getUsers().size();
            nextPageToken = Optional.ofNullable(usersQueryResult.getNextPageToken());
        } while (nextPageToken.isPresent());
        return result;
    } catch (IOException e) {
        log.warn("Failed to retrieve GSuite User List.", e);
        return 0;
    }
}
Also used : Users(com.google.api.services.directory.model.Users) IOException(java.io.IOException) Directory(com.google.api.services.directory.Directory)

Aggregations

Directory (com.google.api.services.directory.Directory)3 IOException (java.io.IOException)2 RetryerBuilder (com.github.rholder.retry.RetryerBuilder)1 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)1 GoogleNetHttpTransport (com.google.api.client.googleapis.javanet.GoogleNetHttpTransport)1 HttpTransport (com.google.api.client.http.HttpTransport)1 JsonFactory (com.google.api.client.json.JsonFactory)1 CloudResourceManager (com.google.api.services.cloudresourcemanager.CloudResourceManager)1 Members (com.google.api.services.directory.model.Members)1 Users (com.google.api.services.directory.model.Users)1 Iam (com.google.api.services.iam.v1.Iam)1 HttpCredentialsAdapter (com.google.auth.http.HttpCredentialsAdapter)1 IamCredentialsClient (com.google.cloud.iam.credentials.v1.IamCredentialsClient)1 CacheBuilder (com.google.common.cache.CacheBuilder)1 GeneralSecurityException (java.security.GeneralSecurityException)1 Before (org.junit.Before)1