Skip to content

Commit

Permalink
[GOOGLEAPPS-19] #resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
ilgrosso committed May 24, 2024
1 parent 1267bec commit bfd09dc
Show file tree
Hide file tree
Showing 15 changed files with 2,620 additions and 1,874 deletions.
28 changes: 6 additions & 22 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,6 @@

<properties>
<connid.version>1.5.2.0</connid.version>
<google-api-client.version>2.5.1</google-api-client.version>
<google-oauth-client.version>1.36.0</google-oauth-client.version>
<google-api-services.version>1.25.0</google-api-services.version>
<google-auth-library-oauth2-http.version>1.23.0</google-auth-library-oauth2-http.version>
<jackson.version>2.17.1</jackson.version>

<spring-boot.version>2.7.18</spring-boot.version>

Expand All @@ -112,46 +107,35 @@
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client</artifactId>
<version>${google-oauth-client.version}</version>
<version>1.36.0</version>
</dependency>
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-oauth2-http</artifactId>
<version>${google-auth-library-oauth2-http.version}</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-java6</artifactId>
<version>${google-oauth-client.version}</version>
<version>1.23.0</version>
</dependency>
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>${google-api-client.version}</version>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-admin-directory</artifactId>
<version>directory_v1-rev118-${google-api-services.version}</version>
<version>directory_v1-rev20240509-2.0.0</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-licensing</artifactId>
<version>v1-rev62-${google-api-services.version}</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
<version>v1-rev20220430-2.0.0</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
</dependency>

<!-- TEST -->
<dependency>
<groupId>net.tirasa.connid</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* ====================
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2024 ConnId. All Rights Reserved
*
* The contents of this file are subject to the terms of the Common Development
* and Distribution License("CDDL") (the "License"). You may not use this file
* except in compliance with the License.
*
* You can obtain a copy of the License at
* http://opensource.org/licenses/cddl1.php
* See the License for the specific language governing permissions and limitations
* under the License.
*
* When distributing the Covered Code, include this CDDL Header Notice in each file
* and include the License file at http://opensource.org/licenses/cddl1.php.
* If applicable, add the following below this CDDL Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
* ====================
*/
package net.tirasa.connid.bundles.googleapps;

import com.google.api.client.googleapis.json.GoogleJsonError;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest;
import com.google.api.client.http.HttpStatusCodes;
import java.io.IOException;
import java.security.SecureRandom;
import org.identityconnectors.common.Assertions;
import org.identityconnectors.common.logging.Log;
import org.identityconnectors.framework.common.exceptions.ConnectorException;
import org.identityconnectors.framework.common.exceptions.RetryableException;

public final class GoogleApiExecutor {

private static final Log LOG = Log.getLog(GoogleApiExecutor.class);

private static final SecureRandom RANDOM = new SecureRandom();

public static <G extends AbstractGoogleJsonClientRequest<T>, T, R> R execute(
final G request, final RequestResultHandler<G, T, R> handler) {

return execute(
Assertions.nullChecked(request, "Google Json ClientRequest"),
Assertions.nullChecked(handler, "handler"), -1);
}

public static <G extends AbstractGoogleJsonClientRequest<T>, T, R> R execute(
final G request, final RequestResultHandler<G, T, R> handler, final int retry) {

try {
if (retry >= 0) {
long sleep = (long) ((1000 * Math.pow(2, retry)) + nextLong(1000));
try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
throw ConnectorException.wrap(e);
}
}
return handler.handleResult(request, request.execute());
} catch (GoogleJsonResponseException e) {
GoogleJsonError details = e.getDetails();
if (null != details && null != details.getErrors()) {
GoogleJsonError.ErrorInfo errorInfo = details.getErrors().get(0);
// error: 403
LOG.error("Unable to execute request {0} - {1} - {2}",
e.getStatusCode(), e.getStatusMessage(), errorInfo.getReason());
switch (e.getStatusCode()) {
case HttpStatusCodes.STATUS_CODE_FORBIDDEN:
if ("userRateLimitExceeded".equalsIgnoreCase(errorInfo.getReason())
|| "rateLimitExceeded".equalsIgnoreCase(errorInfo.getReason())) {
return handler.handleError(e);
}
break;
case HttpStatusCodes.STATUS_CODE_NOT_FOUND:
if ("notFound".equalsIgnoreCase(errorInfo.getReason())) {
return handler.handleNotFound(e);
}
break;
case 409:
if ("duplicate".equalsIgnoreCase(errorInfo.getReason())) {
// Already Exists
handler.handleDuplicate(e);
}
break;
case 400:
if ("invalid".equalsIgnoreCase(errorInfo.getReason())) {
// Already Exists "Invalid Ou Id"
}
break;
case HttpStatusCodes.STATUS_CODE_SERVICE_UNAVAILABLE:
if ("backendError".equalsIgnoreCase(errorInfo.getReason())) {
throw RetryableException.wrap(e.getMessage(), e);
}
break;
default:
break;
}
}

if (e.getStatusCode() == HttpStatusCodes.STATUS_CODE_FORBIDDEN) {
LOG.error("Forbidden request");
handler.handleError(e);
} else if (e.getStatusCode() == HttpStatusCodes.STATUS_CODE_NOT_FOUND) {
LOG.error("Endpoint not found for request");
return handler.handleNotFound(e);
}
throw ConnectorException.wrap(e);
} catch (IOException e) {
// https://developers.google.com/admin-sdk/directory/v1/limits
// rateLimitExceeded or userRateLimitExceeded
if (retry < 5) {
return execute(request, handler, retry + 1);
} else {
return handler.handleError(e);
}
}
}

private static long nextLong(final long n) {
long bits, val;
do {
bits = (RANDOM.nextLong() << 1) >>> 1;
val = bits % n;
} while (bits - val + (n - 1) < 0L);
return val;
}

private GoogleApiExecutor() {
// private constructor for static utility class
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.admin.directory.Directory;
import com.google.api.services.admin.directory.DirectoryScopes;
import com.google.api.services.directory.Directory;
import com.google.api.services.directory.DirectoryScopes;
import com.google.api.services.licensing.Licensing;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;
Expand Down Expand Up @@ -240,7 +240,6 @@ private void initGoogleCredentials() {
DirectoryScopes.ADMIN_DIRECTORY_USERSCHEMA,
DirectoryScopes.ADMIN_DIRECTORY_ORGUNIT,
DirectoryScopes.ADMIN_DIRECTORY_DOMAIN,
DirectoryScopes.ADMIN_DIRECTORY_NOTIFICATIONS,
DirectoryScopes.ADMIN_DIRECTORY_GROUP,
DirectoryScopes.ADMIN_DIRECTORY_GROUP_MEMBER));

Expand Down
Loading

0 comments on commit bfd09dc

Please sign in to comment.