public Builder enableEmulator(String host, int port) { Preconditions.checkArgument(host != null && !host.isEmpty(), "Host cannot be null or empty"); Preconditions.checkArgument(port > 0, "Port must be positive"); setUsePlaintextNegotiation(true); setCredentialOptions(CredentialOptions.nullCredential()); setDataHost(host); setAdminHost(host); setPort(port); LOG.info("Connecting to the Bigtable emulator at " + host + ":" + port); return this; }
builder.setCredentialOptions(CredentialOptions.credential(credentials)); } else if (configuration.get(BIGTABLE_SERVICE_ACCOUNT_JSON_VALUE_KEY) != null) { String jsonValue = configuration.get(BIGTABLE_SERVICE_ACCOUNT_JSON_VALUE_KEY); LOG.debug("Using json value"); builder.setCredentialOptions( CredentialOptions.jsonCredentials(jsonValue)); } else if (configuration.get(BIGTABLE_SERVICE_ACCOUNT_JSON_KEYFILE_LOCATION_KEY) != null) { configuration.get(BIGTABLE_SERVICE_ACCOUNT_JSON_KEYFILE_LOCATION_KEY); LOG.debug("Using json keyfile: %s", keyFileLocation); builder.setCredentialOptions( CredentialOptions.jsonCredentials( new FileInputStream(keyFileLocation))); "Key file location must be specified when setting service account email"); LOG.debug("Using p12 keyfile: %s", keyFileLocation); builder.setCredentialOptions( CredentialOptions.p12Credential(serviceAccount, keyFileLocation)); } else { LOG.debug("Using default credentials."); builder.setCredentialOptions( CredentialOptions.defaultCredentials()); builder.setCredentialOptions(CredentialOptions.nullCredential()); LOG.info("Enabling the use of null credentials. This should not be used in production."); } else {
builder.setCredentialOptions(CredentialOptions.credential(credentials)); } else if (configuration.get(BIGTABLE_SERVICE_ACCOUNT_JSON_VALUE_KEY) != null) { String jsonValue = configuration.get(BIGTABLE_SERVICE_ACCOUNT_JSON_VALUE_KEY); LOG.debug("Using json value"); builder.setCredentialOptions( CredentialOptions.jsonCredentials(jsonValue)); } else if (configuration.get(BIGTABLE_SERVICE_ACCOUNT_JSON_KEYFILE_LOCATION_KEY) != null) { configuration.get(BIGTABLE_SERVICE_ACCOUNT_JSON_KEYFILE_LOCATION_KEY); LOG.debug("Using json keyfile: %s", keyFileLocation); builder.setCredentialOptions( CredentialOptions.jsonCredentials( new FileInputStream(keyFileLocation))); "Key file location must be specified when setting service account email"); LOG.debug("Using p12 keyfile: %s", keyFileLocation); builder.setCredentialOptions( CredentialOptions.p12Credential(serviceAccount, keyFileLocation)); } else { LOG.debug("Using default credentials."); builder.setCredentialOptions( CredentialOptions.defaultCredentials()); builder.setCredentialOptions(CredentialOptions.nullCredential()); LOG.info("Enabling the use of null credentials. This should not be used in production."); } else {
/** Tests that credentials are used from PipelineOptions if not supplied by BigtableOptions. */ @Test public void testUsePipelineOptionsCredentialsIfNotSpecifiedInBigtableOptions() throws Exception { BigtableOptions options = BIGTABLE_OPTIONS .toBuilder() .setCredentialOptions(CredentialOptions.defaultCredentials()) .build(); GcpOptions pipelineOptions = PipelineOptionsFactory.as(GcpOptions.class); pipelineOptions.setGcpCredential(new TestCredential()); BigtableService readService = BigtableIO.read() .withBigtableOptions(options) .withTableId("TEST-TABLE") .getBigtableConfig() .getBigtableService(pipelineOptions); BigtableService writeService = BigtableIO.write() .withBigtableOptions(options) .withTableId("TEST-TABLE") .getBigtableConfig() .getBigtableService(pipelineOptions); assertEquals( CredentialType.SuppliedCredentials, readService.getBigtableOptions().getCredentialOptions().getCredentialType()); assertEquals( CredentialType.SuppliedCredentials, writeService.getBigtableOptions().getCredentialOptions().getCredentialType()); }
/** Tests that credentials are not used from PipelineOptions if supplied by BigtableOptions. */ @Test public void testDontUsePipelineOptionsCredentialsIfSpecifiedInBigtableOptions() throws Exception { BigtableOptions options = BIGTABLE_OPTIONS .toBuilder() .setCredentialOptions(CredentialOptions.nullCredential()) .build(); GcpOptions pipelineOptions = PipelineOptionsFactory.as(GcpOptions.class); pipelineOptions.setGcpCredential(new TestCredential()); BigtableService readService = BigtableIO.read() .withBigtableOptions(options) .withTableId("TEST-TABLE") .getBigtableConfig() .getBigtableService(pipelineOptions); BigtableService writeService = BigtableIO.write() .withBigtableOptions(options) .withTableId("TEST-TABLE") .getBigtableConfig() .getBigtableService(pipelineOptions); assertEquals( CredentialType.None, readService.getBigtableOptions().getCredentialOptions().getCredentialType()); assertEquals( CredentialType.None, writeService.getBigtableOptions().getCredentialOptions().getCredentialType()); }
/** * Helper function that either returns the mock Bigtable service supplied by {@link * #withBigtableService} or creates and returns an implementation that talks to {@code Cloud * Bigtable}. * * <p>Also populate the credentials option from {@link GcpOptions#getGcpCredential()} if the * default credentials are being used on {@link BigtableOptions}. */ @VisibleForTesting BigtableService getBigtableService(PipelineOptions pipelineOptions) { if (getBigtableService() != null) { return getBigtableService(); } BigtableOptions.Builder bigtableOptions = effectiveUserProvidedBigtableOptions(); bigtableOptions.setUserAgent(pipelineOptions.getUserAgent()); if (bigtableOptions.build().getCredentialOptions().getCredentialType() == CredentialOptions.CredentialType.DefaultCredentials) { bigtableOptions.setCredentialOptions( CredentialOptions.credential(pipelineOptions.as(GcpOptions.class).getGcpCredential())); } // Default option that should be forced bigtableOptions.setUseCachedDataPool(true); return new BigtableServiceImpl(bigtableOptions.build()); }
@Before public void setup() throws Exception { PipelineOptionsFactory.register(BigtableTestOptions.class); options = TestPipeline.testingPipelineOptions().as(BigtableTestOptions.class); project = options.as(GcpOptions.class).getProject(); bigtableOptions = new Builder() .setProjectId(project) .setInstanceId(options.getInstanceId()) .setUserAgent("apache-beam-test") .build(); session = new BigtableSession( bigtableOptions .toBuilder() .setCredentialOptions( CredentialOptions.credential(options.as(GcpOptions.class).getGcpCredential())) .build()); tableAdminClient = session.getTableAdminClient(); }