mirror of
https://github.com/google/nomulus.git
synced 2025-07-10 05:03:24 +02:00
Replace deprecated GoogleCredential with new auth lib (#129)
Replace deprecated GoogleCredential with new lib This PR also introduced a CredentialsBundle class to carry HttpTransport and JsonFactory object which are needed by most of the GCP library to instantiate client.
This commit is contained in:
parent
7ce37147c0
commit
6daf72a54e
29 changed files with 822 additions and 684 deletions
|
@ -17,17 +17,17 @@ package google.registry.export.datastore;
|
|||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static com.google.common.truth.Truth8.assertThat;
|
||||
|
||||
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
|
||||
import com.google.api.client.http.GenericUrl;
|
||||
import com.google.api.client.http.HttpRequest;
|
||||
import com.google.api.client.http.HttpTransport;
|
||||
import com.google.api.client.http.javanet.NetHttpTransport;
|
||||
import com.google.api.client.json.jackson2.JacksonFactory;
|
||||
import com.google.auth.oauth2.AccessToken;
|
||||
import com.google.auth.oauth2.GoogleCredentials;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import google.registry.testing.TestDataHelper;
|
||||
import google.registry.util.GoogleCredentialsBundle;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Date;
|
||||
import java.util.Optional;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
|
@ -48,27 +48,44 @@ public class DatastoreAdminTest {
|
|||
|
||||
@Rule public final MockitoRule mocks = MockitoJUnit.rule();
|
||||
|
||||
private HttpTransport httpTransport;
|
||||
private GoogleCredential googleCredential;
|
||||
private DatastoreAdmin datastoreAdmin;
|
||||
|
||||
private static HttpRequest simulateSendRequest(HttpRequest httpRequest) {
|
||||
try {
|
||||
httpRequest.setUrl(new GenericUrl("https://localhost:65537")).execute();
|
||||
} catch (Exception expected) {
|
||||
}
|
||||
return httpRequest;
|
||||
}
|
||||
|
||||
private static Optional<String> getAccessToken(HttpRequest httpRequest) {
|
||||
return httpRequest.getHeaders().getAuthorizationAsList().stream()
|
||||
.filter(header -> header.startsWith(AUTH_HEADER_PREFIX))
|
||||
.map(header -> header.substring(AUTH_HEADER_PREFIX.length()))
|
||||
.findAny();
|
||||
}
|
||||
|
||||
private static Optional<String> getRequestContent(HttpRequest httpRequest) throws IOException {
|
||||
if (httpRequest.getContent() == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
httpRequest.getContent().writeTo(outputStream);
|
||||
outputStream.close();
|
||||
return Optional.of(outputStream.toString(StandardCharsets.UTF_8.name()));
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
httpTransport = new NetHttpTransport();
|
||||
googleCredential =
|
||||
new GoogleCredential.Builder()
|
||||
.setTransport(httpTransport)
|
||||
.setJsonFactory(JacksonFactory.getDefaultInstance())
|
||||
.setClock(() -> 0)
|
||||
.build();
|
||||
googleCredential.setAccessToken(ACCESS_TOKEN);
|
||||
googleCredential.setExpiresInSeconds(1_000L);
|
||||
|
||||
Date oneHourLater = new Date(System.currentTimeMillis() + 3_600_000);
|
||||
GoogleCredentials googleCredentials = GoogleCredentials
|
||||
.create(new AccessToken(ACCESS_TOKEN, oneHourLater));
|
||||
GoogleCredentialsBundle credentialsBundle = GoogleCredentialsBundle.create(googleCredentials);
|
||||
datastoreAdmin =
|
||||
new DatastoreAdmin.Builder(
|
||||
googleCredential.getTransport(),
|
||||
googleCredential.getJsonFactory(),
|
||||
googleCredential)
|
||||
credentialsBundle.getHttpTransport(),
|
||||
credentialsBundle.getJsonFactory(),
|
||||
credentialsBundle.getHttpRequestInitializer())
|
||||
.setApplicationName("MyApplication")
|
||||
.setProjectId("MyCloudProject")
|
||||
.build();
|
||||
|
@ -151,29 +168,4 @@ public class DatastoreAdminTest {
|
|||
simulateSendRequest(httpRequest);
|
||||
assertThat(getAccessToken(httpRequest)).hasValue(ACCESS_TOKEN);
|
||||
}
|
||||
|
||||
private static HttpRequest simulateSendRequest(HttpRequest httpRequest) {
|
||||
try {
|
||||
httpRequest.setUrl(new GenericUrl("https://localhost:65537")).execute();
|
||||
} catch (Exception expected) {
|
||||
}
|
||||
return httpRequest;
|
||||
}
|
||||
|
||||
private static Optional<String> getAccessToken(HttpRequest httpRequest) {
|
||||
return httpRequest.getHeaders().getAuthorizationAsList().stream()
|
||||
.filter(header -> header.startsWith(AUTH_HEADER_PREFIX))
|
||||
.map(header -> header.substring(AUTH_HEADER_PREFIX.length()))
|
||||
.findAny();
|
||||
}
|
||||
|
||||
private static Optional<String> getRequestContent(HttpRequest httpRequest) throws IOException {
|
||||
if (httpRequest.getContent() == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
httpRequest.getContent().writeTo(outputStream);
|
||||
outputStream.close();
|
||||
return Optional.of(outputStream.toString(StandardCharsets.UTF_8.name()));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,34 +16,40 @@ package google.registry.tools;
|
|||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static google.registry.tools.RequestFactoryModule.REQUEST_TIMEOUT_MS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
|
||||
import com.google.api.client.http.GenericUrl;
|
||||
import com.google.api.client.http.HttpRequest;
|
||||
import com.google.api.client.http.HttpRequestFactory;
|
||||
import com.google.api.client.http.HttpRequestInitializer;
|
||||
import google.registry.config.RegistryConfig;
|
||||
import google.registry.testing.SystemPropertyRule;
|
||||
import google.registry.util.GoogleCredentialsBundle;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnit;
|
||||
import org.mockito.junit.MockitoRule;
|
||||
|
||||
@RunWith(JUnit4.class)
|
||||
public class RequestFactoryModuleTest {
|
||||
|
||||
private final GoogleCredential googleCredential = mock(GoogleCredential.class);
|
||||
|
||||
@Rule public final MockitoRule mockitoRule = MockitoJUnit.rule();
|
||||
@Rule public final SystemPropertyRule systemPropertyRule = new SystemPropertyRule();
|
||||
|
||||
@Mock public GoogleCredentialsBundle credentialsBundle;
|
||||
@Mock public HttpRequestInitializer httpRequestInitializer;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
RegistryToolEnvironment.UNITTEST.setup(systemPropertyRule);
|
||||
when(credentialsBundle.getHttpRequestInitializer()).thenReturn(httpRequestInitializer);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -52,12 +58,13 @@ public class RequestFactoryModuleTest {
|
|||
boolean origIsLocal = RegistryConfig.CONFIG_SETTINGS.get().appEngine.isLocal;
|
||||
RegistryConfig.CONFIG_SETTINGS.get().appEngine.isLocal = true;
|
||||
try {
|
||||
HttpRequestFactory factory = RequestFactoryModule.provideHttpRequestFactory(googleCredential);
|
||||
HttpRequestFactory factory =
|
||||
RequestFactoryModule.provideHttpRequestFactory(credentialsBundle);
|
||||
HttpRequestInitializer initializer = factory.getInitializer();
|
||||
assertThat(initializer).isNotNull();
|
||||
HttpRequest request = factory.buildGetRequest(new GenericUrl("http://localhost"));
|
||||
initializer.initialize(request);
|
||||
verifyZeroInteractions(googleCredential);
|
||||
verifyZeroInteractions(httpRequestInitializer);
|
||||
} finally {
|
||||
RegistryConfig.CONFIG_SETTINGS.get().appEngine.isLocal = origIsLocal;
|
||||
}
|
||||
|
@ -69,15 +76,16 @@ public class RequestFactoryModuleTest {
|
|||
boolean origIsLocal = RegistryConfig.CONFIG_SETTINGS.get().appEngine.isLocal;
|
||||
RegistryConfig.CONFIG_SETTINGS.get().appEngine.isLocal = false;
|
||||
try {
|
||||
HttpRequestFactory factory = RequestFactoryModule.provideHttpRequestFactory(googleCredential);
|
||||
HttpRequestFactory factory =
|
||||
RequestFactoryModule.provideHttpRequestFactory(credentialsBundle);
|
||||
HttpRequestInitializer initializer = factory.getInitializer();
|
||||
assertThat(initializer).isNotNull();
|
||||
// HttpRequestFactory#buildGetRequest() calls initialize() once.
|
||||
HttpRequest request = factory.buildGetRequest(new GenericUrl("http://localhost"));
|
||||
verify(googleCredential).initialize(request);
|
||||
verify(httpRequestInitializer).initialize(request);
|
||||
assertThat(request.getConnectTimeout()).isEqualTo(REQUEST_TIMEOUT_MS);
|
||||
assertThat(request.getReadTimeout()).isEqualTo(REQUEST_TIMEOUT_MS);
|
||||
verifyNoMoreInteractions(googleCredential);
|
||||
verifyNoMoreInteractions(httpRequestInitializer);
|
||||
} finally {
|
||||
RegistryConfig.CONFIG_SETTINGS.get().appEngine.isLocal = origIsLocal;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue