Use @DefaultCredential for Cloud API access in GAE

This change completes the switch to @DefaultCredential for
all use cases in GAE.

Impacted modules:
- IcannReporting
- CreateCdnsTld command
- LoadSnapshot command.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=213511730
This commit is contained in:
weiminyu 2018-09-18 14:01:10 -07:00 committed by Ben McIlwain
parent 9bcd5579ef
commit 961e5cc7c7
20 changed files with 184 additions and 226 deletions

View file

@ -1,4 +1,4 @@
// Copyright 2017 The Nomulus Authors. All Rights Reserved.
// Copyright 2018 The Nomulus Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -17,15 +17,11 @@ package google.registry.bigquery;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.bigquery.BigqueryUtils.FieldType.STRING;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.services.bigquery.Bigquery;
import com.google.api.services.bigquery.model.Dataset;
import com.google.api.services.bigquery.model.Table;
@ -39,11 +35,10 @@ import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.ArgumentCaptor;
/** Unit tests for {@link BigqueryFactory}. */
/** Unit tests for {@link CheckedBigquery}. */
@RunWith(JUnit4.class)
public class BigqueryFactoryTest {
public class CheckedBigqueryTest {
private final BigqueryFactory.Subfactory subfactory = mock(BigqueryFactory.Subfactory.class);
private final Bigquery bigquery = mock(Bigquery.class);
private final Bigquery.Datasets bigqueryDatasets = mock(Bigquery.Datasets.class);
private final Bigquery.Datasets.Insert bigqueryDatasetsInsert =
@ -51,25 +46,19 @@ public class BigqueryFactoryTest {
private final Bigquery.Tables bigqueryTables = mock(Bigquery.Tables.class);
private final Bigquery.Tables.Insert bigqueryTablesInsert = mock(Bigquery.Tables.Insert.class);
private BigqueryFactory factory;
private CheckedBigquery checkedBigquery;
@Before
public void before() throws Exception {
when(subfactory.create(
anyString(),
any(HttpTransport.class),
any(JsonFactory.class),
any(HttpRequestInitializer.class)))
.thenReturn(bigquery);
when(bigquery.datasets()).thenReturn(bigqueryDatasets);
when(bigqueryDatasets.insert(eq("Project-Id"), any(Dataset.class)))
.thenReturn(bigqueryDatasetsInsert);
when(bigquery.tables()).thenReturn(bigqueryTables);
when(bigqueryTables.insert(eq("Project-Id"), any(String.class), any(Table.class)))
.thenReturn(bigqueryTablesInsert);
factory = new BigqueryFactory();
factory.subfactory = subfactory;
factory.bigquerySchemas =
checkedBigquery = new CheckedBigquery();
checkedBigquery.bigquery = bigquery;
checkedBigquery.bigquerySchemas =
new ImmutableMap.Builder<String, ImmutableList<TableFieldSchema>>()
.put(
"Table-Id",
@ -82,7 +71,7 @@ public class BigqueryFactoryTest {
@Test
public void testSuccess_datastoreCreation() throws Exception {
factory.create("Project-Id", "Dataset-Id");
checkedBigquery.ensureDataSetExists("Project-Id", "Dataset-Id");
ArgumentCaptor<Dataset> datasetArg = ArgumentCaptor.forClass(Dataset.class);
verify(bigqueryDatasets).insert(eq("Project-Id"), datasetArg.capture());
@ -95,7 +84,7 @@ public class BigqueryFactoryTest {
@Test
public void testSuccess_datastoreAndTableCreation() throws Exception {
factory.create("Project-Id", "Dataset2", "Table2");
checkedBigquery.ensureDataSetAndTableExist("Project-Id", "Dataset2", "Table2");
ArgumentCaptor<Dataset> datasetArg = ArgumentCaptor.forClass(Dataset.class);
verify(bigqueryDatasets).insert(eq("Project-Id"), datasetArg.capture());

View file

@ -12,6 +12,7 @@ java_library(
srcs = glob(["*.java"]),
deps = [
"//java/google/registry/config",
"@com_google_auto_value",
"@com_google_guava",
"@com_google_truth",
"@com_google_truth_extensions_truth_java8_extension",

View file

@ -0,0 +1,72 @@
// Copyright 2018 The Nomulus Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package google.registry.config;
import static com.google.common.truth.Truth.assertThat;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import google.registry.config.RegistryConfig.ConfigModule;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
/** Unit tests for environment-specific CloudDns configurations. */
@RunWith(Parameterized.class)
public class CloudDnsConfigTest {
@Parameters
public static final Collection<RegistryEnvironment> environments() {
return ImmutableList.copyOf(RegistryEnvironment.values());
}
private static final ImmutableList<Optional<String>> TEST_CONFIGS =
ImmutableList.of(
Optional.of("https://staging-www.sandbox.googleapis.com"),
Optional.of("dns/v2beta1_staging/projects/"));
private static final ImmutableList<Optional<String>> PROD_CONFIGS =
ImmutableList.of(Optional.empty(), Optional.empty());
private static final Map<RegistryEnvironment, ImmutableList<Optional<String>>> data =
ImmutableMap.of(
RegistryEnvironment.PRODUCTION, PROD_CONFIGS,
RegistryEnvironment.SANDBOX, PROD_CONFIGS);
@Parameter public RegistryEnvironment environment;
private RegistryConfigSettings registryConfigSettings;
@Before
public void setup() {
System.setProperty(RegistryEnvironment.PROPERTY, environment.name());
registryConfigSettings = YamlUtils.getConfigSettings();
}
@Test
public void test() {
ImmutableList<Optional<String>> expectedConfigs = data.getOrDefault(environment, TEST_CONFIGS);
assertThat(ConfigModule.getCloudDnsRootUrl(registryConfigSettings))
.isEqualTo(expectedConfigs.get(0));
assertThat(ConfigModule.getCloudDnsServicePath(registryConfigSettings))
.isEqualTo(expectedConfigs.get(1));
}
}

View file

@ -40,7 +40,7 @@ import com.google.api.services.bigquery.model.JobReference;
import com.google.appengine.api.taskqueue.QueueFactory;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import google.registry.bigquery.BigqueryFactory;
import google.registry.bigquery.CheckedBigquery;
import google.registry.export.BigqueryPollJobAction.BigqueryPollJobEnqueuer;
import google.registry.request.HttpException.BadRequestException;
import google.registry.request.HttpException.InternalServerErrorException;
@ -65,7 +65,7 @@ public class LoadSnapshotActionTest {
public final AppEngineRule appEngine = AppEngineRule.builder()
.withTaskQueue()
.build();
private final BigqueryFactory bigqueryFactory = mock(BigqueryFactory.class);
private final CheckedBigquery checkedBigquery = mock(CheckedBigquery.class);
private final Bigquery bigquery = mock(Bigquery.class);
private final Bigquery.Jobs bigqueryJobs = mock(Bigquery.Jobs.class);
private final Bigquery.Jobs.Insert bigqueryJobsInsert = mock(Bigquery.Jobs.Insert.class);
@ -79,14 +79,14 @@ public class LoadSnapshotActionTest {
@Before
public void before() throws Exception {
when(bigqueryFactory.create("Project-Id", "snapshots")).thenReturn(bigquery);
when(checkedBigquery.ensureDataSetExists("Project-Id", "snapshots")).thenReturn(bigquery);
when(bigquery.jobs()).thenReturn(bigqueryJobs);
when(bigqueryJobs.insert(eq("Project-Id"), any(Job.class))).thenReturn(bigqueryJobsInsert);
when(bigquery.datasets()).thenReturn(bigqueryDatasets);
when(bigqueryDatasets.insert(eq("Project-Id"), any(Dataset.class)))
.thenReturn(bigqueryDatasetsInsert);
action = new LoadSnapshotAction();
action.bigqueryFactory = bigqueryFactory;
action.checkedBigquery = checkedBigquery;
action.bigqueryPollEnqueuer = bigqueryPollEnqueuer;
action.clock = clock;
action.projectId = "Project-Id";
@ -113,9 +113,9 @@ public class LoadSnapshotActionTest {
public void testSuccess_doPost() throws Exception {
action.run();
// Verify that bigqueryFactory was called in a way that would create the dataset if it didn't
// Verify that checkedBigquery was called in a way that would create the dataset if it didn't
// already exist.
verify(bigqueryFactory).create("Project-Id", "snapshots");
verify(checkedBigquery).ensureDataSetExists("Project-Id", "snapshots");
// Capture the load jobs we inserted to do additional checking on them.
ArgumentCaptor<Job> jobArgument = ArgumentCaptor.forClass(Job.class);

View file

@ -34,7 +34,7 @@ import com.google.api.services.bigquery.Bigquery;
import com.google.api.services.bigquery.model.Dataset;
import com.google.api.services.bigquery.model.Table;
import com.google.common.collect.Iterables;
import google.registry.bigquery.BigqueryFactory;
import google.registry.bigquery.CheckedBigquery;
import google.registry.request.HttpException.InternalServerErrorException;
import google.registry.testing.AppEngineRule;
import google.registry.testing.TaskQueueHelper.TaskMatcher;
@ -55,7 +55,7 @@ public class UpdateSnapshotViewActionTest {
public final AppEngineRule appEngine = AppEngineRule.builder()
.withTaskQueue()
.build();
private final BigqueryFactory bigqueryFactory = mock(BigqueryFactory.class);
private final CheckedBigquery checkedBigquery = mock(CheckedBigquery.class);
private final Bigquery bigquery = mock(Bigquery.class);
private final Bigquery.Datasets bigqueryDatasets = mock(Bigquery.Datasets.class);
private final Bigquery.Datasets.Insert bigqueryDatasetsInsert =
@ -67,7 +67,7 @@ public class UpdateSnapshotViewActionTest {
@Before
public void before() throws Exception {
when(bigqueryFactory.create(anyString(), anyString())).thenReturn(bigquery);
when(checkedBigquery.ensureDataSetExists(anyString(), anyString())).thenReturn(bigquery);
when(bigquery.datasets()).thenReturn(bigqueryDatasets);
when(bigqueryDatasets.insert(anyString(), any(Dataset.class)))
.thenReturn(bigqueryDatasetsInsert);
@ -76,7 +76,7 @@ public class UpdateSnapshotViewActionTest {
.thenReturn(bigqueryTablesUpdate);
action = new UpdateSnapshotViewAction();
action.bigqueryFactory = bigqueryFactory;
action.checkedBigquery = checkedBigquery;
action.datasetId = "some_dataset";
action.kindName = "fookind";
action.projectId = "myproject";
@ -99,10 +99,12 @@ public class UpdateSnapshotViewActionTest {
public void testSuccess_doPost() throws Exception {
action.run();
InOrder factoryOrder = inOrder(bigqueryFactory);
InOrder factoryOrder = inOrder(checkedBigquery);
// Check that the BigQuery factory was called in such a way that the dataset would be created
// if it didn't already exist.
factoryOrder.verify(bigqueryFactory).create("myproject", "latest_datastore_export");
factoryOrder
.verify(checkedBigquery)
.ensureDataSetExists("myproject", "latest_datastore_export");
// Check that we updated both views
InOrder tableOrder = inOrder(bigqueryTables);

View file

@ -20,9 +20,6 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.services.bigquery.Bigquery;
import com.google.api.services.bigquery.Bigquery.Tabledata;
import com.google.api.services.bigquery.Bigquery.Tabledata.InsertAll;
@ -31,7 +28,7 @@ import com.google.api.services.bigquery.model.TableDataInsertAllResponse;
import com.google.api.services.bigquery.model.TableDataInsertAllResponse.InsertErrors;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableListMultimap;
import google.registry.bigquery.BigqueryFactory;
import google.registry.bigquery.CheckedBigquery;
import google.registry.testing.AppEngineRule;
import org.junit.Before;
import org.junit.Rule;
@ -48,7 +45,7 @@ public class MetricsExportActionTest {
public final AppEngineRule appEngine =
AppEngineRule.builder().withDatastore().withTaskQueue().build();
private final BigqueryFactory bigqueryFactory = mock(BigqueryFactory.class);
private final CheckedBigquery checkedBigquery = mock(CheckedBigquery.class);
private final Bigquery bigquery = mock(Bigquery.class);
private final Tabledata tabledata = mock(Tabledata.class);
private final InsertAll insertAll = mock(InsertAll.class);
@ -69,13 +66,8 @@ public class MetricsExportActionTest {
@Before
public void setup() throws Exception {
when(bigqueryFactory.create(anyString(), anyString(), anyString())).thenReturn(bigquery);
when(bigqueryFactory.create(
anyString(),
Matchers.any(HttpTransport.class),
Matchers.any(JsonFactory.class),
Matchers.any(HttpRequestInitializer.class)))
.thenReturn(bigquery);
when(checkedBigquery.ensureDataSetAndTableExist(anyString(), anyString(), anyString()))
.thenReturn(bigquery);
when(bigquery.tabledata()).thenReturn(tabledata);
when(tabledata.insertAll(
@ -84,7 +76,7 @@ public class MetricsExportActionTest {
anyString(),
Matchers.any(TableDataInsertAllRequest.class))).thenReturn(insertAll);
action = new MetricsExportAction();
action.bigqueryFactory = bigqueryFactory;
action.checkedBigquery = checkedBigquery;
action.insertId = "insert id";
action.parameters = parameters;
action.projectId = "project id";

View file

@ -41,16 +41,9 @@ public class CreateCdnsTldTest extends CommandTestCase<CreateCdnsTld> {
public void setUp() throws Exception {
when(dnsService.managedZones()).thenReturn(managedZones);
when(managedZones.create(projectId.capture(), requestBody.capture())).thenReturn(request);
command = new CreateCdnsTldForTest();
command = new CreateCdnsTld();
command.projectId = "test-project";
}
/** Fake the command class so we can override createDnsService() */
class CreateCdnsTldForTest extends CreateCdnsTld {
@Override
Dns createDnsService() {
return dnsService;
}
command.dnsService = dnsService;
}
private ManagedZone createZone(