Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified so that connection timeout and read timeout can be set when … #1053

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions oauth2_http/java/com/google/auth/http/TimeoutInitializer.java
@@ -0,0 +1,21 @@
package com.google.auth.http;

import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;

public class TimeoutInitializer implements HttpRequestInitializer {

private final int connectTimeoutMillis;
private final int readTimeoutMillis;

public TimeoutInitializer(int connectTimeoutMillis, int readTimeoutMillis) {
this.connectTimeoutMillis = connectTimeoutMillis;
this.readTimeoutMillis = readTimeoutMillis;
}

@Override
public void initialize(HttpRequest request) {
request.setConnectTimeout(connectTimeoutMillis);
request.setReadTimeout(readTimeoutMillis);
}
}
Expand Up @@ -31,6 +31,7 @@

package com.google.auth.oauth2;

import com.google.api.client.http.HttpRequestInitializer;
import com.google.auth.http.HttpTransportFactory;
import java.io.File;
import java.io.FileInputStream;
Expand Down Expand Up @@ -119,11 +120,11 @@ class DefaultCredentialsProvider {
* @return the credentials instance.
* @throws IOException if the credentials cannot be created in the current environment.
*/
final GoogleCredentials getDefaultCredentials(HttpTransportFactory transportFactory)
final GoogleCredentials getDefaultCredentials(HttpTransportFactory transportFactory, HttpRequestInitializer httpRequestInitializer)
throws IOException {
synchronized (this) {
if (cachedCredentials == null) {
cachedCredentials = getDefaultCredentialsUnsynchronized(transportFactory);
cachedCredentials = getDefaultCredentialsUnsynchronized(transportFactory, httpRequestInitializer);
}
if (cachedCredentials != null) {
return cachedCredentials;
Expand All @@ -139,7 +140,7 @@ final GoogleCredentials getDefaultCredentials(HttpTransportFactory transportFact
}

private final GoogleCredentials getDefaultCredentialsUnsynchronized(
HttpTransportFactory transportFactory) throws IOException {
HttpTransportFactory transportFactory, HttpRequestInitializer httpRequestInitializer) throws IOException {

// First try the environment variable
GoogleCredentials credentials = null;
Expand All @@ -156,7 +157,7 @@ private final GoogleCredentials getDefaultCredentialsUnsynchronized(
throw new IOException("File does not exist.");
}
credentialsStream = readStream(credentialsFile);
credentials = GoogleCredentials.fromStream(credentialsStream, transportFactory);
credentials = GoogleCredentials.fromStream(credentialsStream, transportFactory, httpRequestInitializer);
} catch (IOException e) {
// Although it is also the cause, the message of the caught exception can have very
// important information for diagnosing errors, so include its message in the
Expand Down Expand Up @@ -187,7 +188,7 @@ private final GoogleCredentials getDefaultCredentialsUnsynchronized(
"Attempting to load credentials from well known file: %s",
wellKnownFileLocation.getCanonicalPath()));
credentialsStream = readStream(wellKnownFileLocation);
credentials = GoogleCredentials.fromStream(credentialsStream, transportFactory);
credentials = GoogleCredentials.fromStream(credentialsStream, transportFactory, httpRequestInitializer);
}
} catch (IOException e) {
throw new IOException(
Expand Down Expand Up @@ -315,8 +316,8 @@ private final GoogleCredentials tryGetComputeCredentials(HttpTransportFactory tr
checkedComputeEngine = true;
if (runningOnComputeEngine) {
return ComputeEngineCredentials.newBuilder()
.setHttpTransportFactory(transportFactory)
.build();
.setHttpTransportFactory(transportFactory)
.build();
}
return null;
}
Expand Down
15 changes: 8 additions & 7 deletions oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java
Expand Up @@ -31,6 +31,7 @@

package com.google.auth.oauth2;

import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.json.GenericJson;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.JsonObjectParser;
Expand Down Expand Up @@ -94,7 +95,7 @@ public static GoogleCredentials create(AccessToken accessToken) {
* @throws IOException if the credentials cannot be created in the current environment.
*/
public static GoogleCredentials getApplicationDefault() throws IOException {
return getApplicationDefault(OAuth2Utils.HTTP_TRANSPORT_FACTORY);
return getApplicationDefault(OAuth2Utils.HTTP_TRANSPORT_FACTORY, null);
}

/**
Expand All @@ -119,10 +120,10 @@ public static GoogleCredentials getApplicationDefault() throws IOException {
* @return the credentials instance.
* @throws IOException if the credentials cannot be created in the current environment.
*/
public static GoogleCredentials getApplicationDefault(HttpTransportFactory transportFactory)
public static GoogleCredentials getApplicationDefault(HttpTransportFactory transportFactory, HttpRequestInitializer httpRequestInitializer)
throws IOException {
Preconditions.checkNotNull(transportFactory);
return defaultCredentialsProvider.getDefaultCredentials(transportFactory);
return defaultCredentialsProvider.getDefaultCredentials(transportFactory, httpRequestInitializer);
}

/**
Expand All @@ -136,7 +137,7 @@ public static GoogleCredentials getApplicationDefault(HttpTransportFactory trans
* @throws IOException if the credential cannot be created from the stream.
*/
public static GoogleCredentials fromStream(InputStream credentialsStream) throws IOException {
return fromStream(credentialsStream, OAuth2Utils.HTTP_TRANSPORT_FACTORY);
return fromStream(credentialsStream, OAuth2Utils.HTTP_TRANSPORT_FACTORY, null);
}

/**
Expand All @@ -152,7 +153,7 @@ public static GoogleCredentials fromStream(InputStream credentialsStream) throws
* @throws IOException if the credential cannot be created from the stream.
*/
public static GoogleCredentials fromStream(
InputStream credentialsStream, HttpTransportFactory transportFactory) throws IOException {
InputStream credentialsStream, HttpTransportFactory transportFactory, HttpRequestInitializer httpRequestInitializer) throws IOException {
Preconditions.checkNotNull(credentialsStream);
Preconditions.checkNotNull(transportFactory);

Expand All @@ -169,13 +170,13 @@ public static GoogleCredentials fromStream(
return UserCredentials.fromJson(fileContents, transportFactory);
}
if (SERVICE_ACCOUNT_FILE_TYPE.equals(fileType)) {
return ServiceAccountCredentials.fromJson(fileContents, transportFactory);
return ServiceAccountCredentials.fromJson(fileContents, transportFactory, httpRequestInitializer);
}
if (ExternalAccountCredentials.EXTERNAL_ACCOUNT_FILE_TYPE.equals(fileType)) {
return ExternalAccountCredentials.fromJson(fileContents, transportFactory);
}
if ("impersonated_service_account".equals(fileType)) {
return ImpersonatedCredentials.fromJson(fileContents, transportFactory);
return ImpersonatedCredentials.fromJson(fileContents, transportFactory, httpRequestInitializer);
}
throw new IOException(
String.format(
Expand Down
101 changes: 51 additions & 50 deletions oauth2_http/java/com/google/auth/oauth2/ImpersonatedCredentials.java
Expand Up @@ -38,6 +38,7 @@
import com.google.api.client.http.HttpContent;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.json.JsonHttpContent;
Expand Down Expand Up @@ -143,13 +144,13 @@ public static ImpersonatedCredentials create(
int lifetime,
HttpTransportFactory transportFactory) {
return ImpersonatedCredentials.newBuilder()
.setSourceCredentials(sourceCredentials)
.setTargetPrincipal(targetPrincipal)
.setDelegates(delegates)
.setScopes(scopes)
.setLifetime(lifetime)
.setHttpTransportFactory(transportFactory)
.build();
.setSourceCredentials(sourceCredentials)
.setTargetPrincipal(targetPrincipal)
.setDelegates(delegates)
.setScopes(scopes)
.setLifetime(lifetime)
.setHttpTransportFactory(transportFactory)
.build();
}

/**
Expand Down Expand Up @@ -186,14 +187,14 @@ public static ImpersonatedCredentials create(
HttpTransportFactory transportFactory,
String quotaProjectId) {
return ImpersonatedCredentials.newBuilder()
.setSourceCredentials(sourceCredentials)
.setTargetPrincipal(targetPrincipal)
.setDelegates(delegates)
.setScopes(scopes)
.setLifetime(lifetime)
.setHttpTransportFactory(transportFactory)
.setQuotaProjectId(quotaProjectId)
.build();
.setSourceCredentials(sourceCredentials)
.setTargetPrincipal(targetPrincipal)
.setDelegates(delegates)
.setScopes(scopes)
.setLifetime(lifetime)
.setHttpTransportFactory(transportFactory)
.setQuotaProjectId(quotaProjectId)
.build();
}

/**
Expand Down Expand Up @@ -233,15 +234,15 @@ public static ImpersonatedCredentials create(
String quotaProjectId,
String iamEndpointOverride) {
return ImpersonatedCredentials.newBuilder()
.setSourceCredentials(sourceCredentials)
.setTargetPrincipal(targetPrincipal)
.setDelegates(delegates)
.setScopes(scopes)
.setLifetime(lifetime)
.setHttpTransportFactory(transportFactory)
.setQuotaProjectId(quotaProjectId)
.setIamEndpointOverride(iamEndpointOverride)
.build();
.setSourceCredentials(sourceCredentials)
.setTargetPrincipal(targetPrincipal)
.setDelegates(delegates)
.setScopes(scopes)
.setLifetime(lifetime)
.setHttpTransportFactory(transportFactory)
.setQuotaProjectId(quotaProjectId)
.setIamEndpointOverride(iamEndpointOverride)
.build();
}

/**
Expand Down Expand Up @@ -273,12 +274,12 @@ public static ImpersonatedCredentials create(
List<String> scopes,
int lifetime) {
return ImpersonatedCredentials.newBuilder()
.setSourceCredentials(sourceCredentials)
.setTargetPrincipal(targetPrincipal)
.setDelegates(delegates)
.setScopes(scopes)
.setLifetime(lifetime)
.build();
.setSourceCredentials(sourceCredentials)
.setTargetPrincipal(targetPrincipal)
.setDelegates(delegates)
.setScopes(scopes)
.setLifetime(lifetime)
.build();
}

static String extractTargetPrincipal(String serviceAccountImpersonationUrl) {
Expand Down Expand Up @@ -367,7 +368,7 @@ public byte[] sign(byte[] toSign) {
* @throws IOException if the credential cannot be created from the JSON.
*/
static ImpersonatedCredentials fromJson(
Map<String, Object> json, HttpTransportFactory transportFactory) throws IOException {
Map<String, Object> json, HttpTransportFactory transportFactory, HttpRequestInitializer httpRequestInitializer) throws IOException {

checkNotNull(json);
checkNotNull(transportFactory);
Expand Down Expand Up @@ -396,23 +397,23 @@ static ImpersonatedCredentials fromJson(
sourceCredentials = UserCredentials.fromJson(sourceCredentialsJson, transportFactory);
} else if (GoogleCredentials.SERVICE_ACCOUNT_FILE_TYPE.equals(sourceCredentialsType)) {
sourceCredentials =
ServiceAccountCredentials.fromJson(sourceCredentialsJson, transportFactory);
ServiceAccountCredentials.fromJson(sourceCredentialsJson, transportFactory, httpRequestInitializer);
} else {
throw new IOException(
String.format(
"A credential of type %s is not supported as source credential for impersonation.",
sourceCredentialsType));
}
return ImpersonatedCredentials.newBuilder()
.setSourceCredentials(sourceCredentials)
.setTargetPrincipal(targetPrincipal)
.setDelegates(delegates)
.setScopes(new ArrayList<String>())
.setLifetime(DEFAULT_LIFETIME_IN_SECONDS)
.setHttpTransportFactory(transportFactory)
.setQuotaProjectId(quotaProjectId)
.setIamEndpointOverride(serviceAccountImpersonationUrl)
.build();
.setSourceCredentials(sourceCredentials)
.setTargetPrincipal(targetPrincipal)
.setDelegates(delegates)
.setScopes(new ArrayList<String>())
.setLifetime(DEFAULT_LIFETIME_IN_SECONDS)
.setHttpTransportFactory(transportFactory)
.setQuotaProjectId(quotaProjectId)
.setIamEndpointOverride(serviceAccountImpersonationUrl)
.build();
}

@Override
Expand Down Expand Up @@ -585,15 +586,15 @@ public int hashCode() {
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("sourceCredentials", sourceCredentials)
.add("targetPrincipal", targetPrincipal)
.add("delegates", delegates)
.add("scopes", scopes)
.add("lifetime", lifetime)
.add("transportFactoryClassName", transportFactoryClassName)
.add("quotaProjectId", quotaProjectId)
.add("iamEndpointOverride", iamEndpointOverride)
.toString();
.add("sourceCredentials", sourceCredentials)
.add("targetPrincipal", targetPrincipal)
.add("delegates", delegates)
.add("scopes", scopes)
.add("lifetime", lifetime)
.add("transportFactoryClassName", transportFactoryClassName)
.add("quotaProjectId", quotaProjectId)
.add("iamEndpointOverride", iamEndpointOverride)
.toString();
}

@Override
Expand Down