/** {@inheritDoc} */ @Override public String toString() { return MoreObjects.toStringHelper(this) .omitNullValues() .add("dataHost", dataHost) .add("adminHost", adminHost) .add("port", port) .add("projectId", projectId) .add("instanceId", instanceId) .add("appProfileId", appProfileId) .add("userAgent", userAgent) .add("credentialType", credentialOptions.getCredentialType()) .add("dataChannelCount", dataChannelCount) .add("retryOptions", retryOptions) .add("bulkOptions", bulkOptions) .add("callOptionsConfig", callOptionsConfig) .add("usePlaintextNegotiation", usePlaintextNegotiation) .add("useCachedDataPool", useCachedDataPool) .add("useBatch", useBatch) .toString(); }
/** * <p> * jsonCredentials. * </p> * @param jsonString a {@link String} object. * @return a {@link com.google.cloud.bigtable.config.CredentialOptions} object. */ public static CredentialOptions jsonCredentials(String jsonString) { return jsonCredentials(new ByteArrayInputStream(jsonString.getBytes(StandardCharsets.UTF_8))); }
/** * <p> * Use the Application Default Credentials which are credentials that identify and authorize the * whole application. This is the built-in service account if running on Google Compute Engine. * Alternatively, the credentials file from the path in the environment variable * GOOGLE_APPLICATION_CREDENTIAL. If GOOGLE_APPLICATION_CREDENTIAL is not set, look at the * gcloud/application_default_credentials.json file in the (User)/APPDATA/ directory on Windows or * ~/.config/ directory on other OSs . * </p> * Initializes OAuth2 credential using preconfigured ServiceAccount settings on the local Google * Compute Engine VM. See: * <a href="https://cloud.google.com/compute/docs/access/create-enable-service-accounts-for-instances" * >Creating and Enabling Service Accounts for Instances</a>. * * @return a {@link com.google.cloud.bigtable.config.CredentialOptions} object. */ public static CredentialOptions defaultCredentials() { return new CredentialOptions(CredentialType.DefaultCredentials); }
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) { String keyFileLocation = LOG.debug("Using json keyfile: %s", keyFileLocation); builder.setCredentialOptions( CredentialOptions.jsonCredentials( new FileInputStream(keyFileLocation))); } else if (configuration.get(BIGTABLE_SERVICE_ACCOUNT_EMAIL_KEY) != null) { 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 {
/** * 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()); }
/** 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()); }
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; }
@Deprecated public Builder() { options = new BigtableOptions(); options.appProfileId = BIGTABLE_APP_PROFILE_DEFAULT; // Optional configuration for hosts - useful for the Bigtable team, more than anything else. options.dataHost = BIGTABLE_DATA_HOST_DEFAULT; options.adminHost = BIGTABLE_ADMIN_HOST_DEFAULT; options.port = BIGTABLE_PORT_DEFAULT; options.dataChannelCount = BIGTABLE_DATA_CHANNEL_COUNT_DEFAULT; options.usePlaintextNegotiation = false; options.useCachedDataPool = false; options.retryOptions = new RetryOptions.Builder().build(); options.callOptionsConfig = new CallOptionsConfig.Builder().build(); // CredentialOptions.defaultCredentials() gets credentials from well known locations, such as // the Google Compute Engine metadata service or gcloud configuration in other environments. A // user can also override the default behavior with P12 or JSON configuration. options.credentialOptions = CredentialOptions.defaultCredentials(); options.useBatch = false; }
@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(); }
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) { String keyFileLocation = LOG.debug("Using json keyfile: %s", keyFileLocation); builder.setCredentialOptions( CredentialOptions.jsonCredentials( new FileInputStream(keyFileLocation))); } else if (configuration.get(BIGTABLE_SERVICE_ACCOUNT_EMAIL_KEY) != null) { 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 {
credentialOptions.getCredentialType() == CredentialType.DefaultCredentials;
/** * No credentials - used for unit testing. * * @return a {@link com.google.cloud.bigtable.config.CredentialOptions} object. */ public static CredentialOptions nullCredential() { LOG.info("Enabling the use of null credentials. This should not be used in production."); return new CredentialOptions(CredentialType.None); }
switch (options.getCredentialType()) { case DefaultCredentials: return getApplicationDefaultCredential(); default: throw new IllegalStateException("Cannot process Credential type: " + options.getCredentialType());