Java Code Examples for io.grpc.auth.MoreCallCredentials#from()

The following examples show how to use io.grpc.auth.MoreCallCredentials#from() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GrpcClient.java    From cloud-spanner-r2dbc with Apache License 2.0 6 votes vote down vote up
/**
 * Initializes the Cloud Spanner gRPC async stub.
 *
 * @param credentials the Google Cloud Platform credentials used to authenticate with Spanner.
 */
public GrpcClient(GoogleCredentials credentials) {
  // Create blocking and async stubs using the channel
  CallCredentials callCredentials = MoreCallCredentials.from(credentials);

  // Create a channel
  this.channel = ManagedChannelBuilder
      .forTarget(GRPC_TARGET)
      .userAgent(USER_AGENT_LIBRARY_NAME + "/" + PACKAGE_VERSION)
      .build();

  // Async stub for general Spanner SQL queries
  this.spanner = SpannerGrpc.newStub(this.channel)
      .withCallCredentials(callCredentials);

  // Async stub for DDL queries
  this.databaseAdmin = DatabaseAdminGrpc.newStub(this.channel)
      .withCallCredentials(callCredentials);

  this.operations = OperationsGrpc.newStub(this.channel).withCallCredentials(callCredentials);
}
 
Example 2
Source File: GoogleAuthClient.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
/**
 * The app requires 2 arguments as described in
 * @see <a href="../../../../../../GOOGLE_AUTH_EXAMPLE.md">Google Auth Example README</a>
 *
 * arg0 = location of the JSON file for the service account you created in the GCP console
 * arg1 = project name in the form "projects/balmy-cirrus-225307" where "balmy-cirrus-225307" is
 *        the project ID for the project you created.
 *
 */
public static void main(String[] args) throws Exception {
  if (args.length < 2) {
    logger.severe("Usage: please pass 2 arguments:\n" +
                  "arg0 = location of the JSON file for the service account you created in the GCP console\n" +
                  "arg1 = project name in the form \"projects/xyz\" where \"xyz\" is the project ID of the project you created.\n");
    System.exit(1);
  }
  GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(args[0]));

  // We need to create appropriate scope as per https://cloud.google.com/storage/docs/authentication#oauth-scopes
  credentials = credentials.createScoped(Arrays.asList("https://www.googleapis.com/auth/cloud-platform"));

  // credentials must be refreshed before the access token is available
  credentials.refreshAccessToken();
  GoogleAuthClient client =
          new GoogleAuthClient("pubsub.googleapis.com", 443, MoreCallCredentials.from(credentials));

  try {
    client.getTopics(args[1]);
  } finally {
    client.shutdown();
  }
}
 
Example 3
Source File: ComputeEngineChannelBuilder.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
private ComputeEngineChannelBuilder(String target) {
  delegate = NettyChannelBuilder.forTarget(target);
  SslContext sslContext;
  try {
    sslContext = GrpcSslContexts.forClient().build();
  } catch (SSLException e) {
    throw new RuntimeException(e);
  }
  InternalNettyChannelBuilder.setProtocolNegotiatorFactory(
      delegate(),
      new GoogleDefaultProtocolNegotiatorFactory(
          /* targetServiceAccounts= */ ImmutableList.<String>of(),
          SharedResourcePool.forResource(HandshakerServiceChannel.SHARED_HANDSHAKER_CHANNEL),
          sslContext));
  CallCredentials credentials = MoreCallCredentials.from(ComputeEngineCredentials.create());
  Status status = Status.OK;
  if (!CheckGcpEnvironment.isOnGcp()) {
    status =
        Status.INTERNAL.withDescription(
            "Compute Engine Credentials can only be used on Google Cloud Platform");
  }
  delegate().intercept(new CallCredentialsInterceptor(credentials, status));
}
 
Example 4
Source File: GoogleDefaultChannelBuilder.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Override
public ManagedChannel build() {
  @Nullable CallCredentials credentials = null;
  Status status = Status.OK;
  try {
    credentials = MoreCallCredentials.from(GoogleCredentials.getApplicationDefault());
  } catch (IOException e) {
    status =
        Status.UNAUTHENTICATED
            .withDescription("Failed to get Google default credentials")
            .withCause(e);
  }
  return delegate().intercept(new GoogleDefaultInterceptor(credentials, status)).build();
}
 
Example 5
Source File: AssistantClient.java    From google-assistant-java-demo with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Get CallCredentials from OAuthCredentials
 *
 * @param oAuthCredentials the credentials from the AuthenticationHelper
 * @return the CallCredentials for the GRPC requests
 */
private CallCredentials getCallCredentials(OAuthCredentials oAuthCredentials) {

    AccessToken accessToken = new AccessToken(
            oAuthCredentials.getAccessToken(),
            new Date(oAuthCredentials.getExpirationTime())
    );

    OAuth2Credentials oAuth2Credentials = new OAuth2Credentials(accessToken);

    // Create an instance of {@link io.grpc.CallCredentials}
    return MoreCallCredentials.from(oAuth2Credentials);
}
 
Example 6
Source File: FirestoreIntegrationTestsConfiguration.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Bean
FirestoreGrpc.FirestoreStub firestoreStub()  throws IOException {
	GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
	CallCredentials callCredentials = MoreCallCredentials.from(credentials);

	// Create a channel
	ManagedChannel channel = ManagedChannelBuilder
			.forTarget("dns:///firestore.googleapis.com:443")
			.build();
	return FirestoreGrpc.newStub(channel).withCallCredentials(callCredentials);
}
 
Example 7
Source File: GoogleAuthUtils.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new {@link CallCredentials} object.
 *
 * @throws IOException in case the call credentials can't be constructed.
 */
public static CallCredentials newCallCredentials(AuthAndTLSOptions options) throws IOException {
  Credentials creds = newCredentials(options);
  if (creds != null) {
    return MoreCallCredentials.from(creds);
  }
  return null;
}
 
Example 8
Source File: GoogleAuthUtils.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
public static CallCredentials newCallCredentials(
    @Nullable InputStream credentialsFile, List<String> authScope) throws IOException {
  Credentials creds = newCredentials(credentialsFile, authScope);
  if (creds != null) {
    return MoreCallCredentials.from(creds);
  }
  return null;
}
 
Example 9
Source File: GoogleAuthUtils.java    From bazel with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new {@link CallCredentials} object.
 *
 * @throws IOException in case the call credentials can't be constructed.
 */
public static CallCredentials newCallCredentials(AuthAndTLSOptions options) throws IOException {
  Credentials creds = newCredentials(options);
  if (creds != null) {
    return MoreCallCredentials.from(creds);
  }
  return null;
}
 
Example 10
Source File: GoogleAuthUtils.java    From bazel with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
public static CallCredentials newCallCredentials(
    @Nullable InputStream credentialsFile, List<String> authScope) throws IOException {
  Credentials creds = newCredentials(credentialsFile, authScope);
  if (creds != null) {
    return MoreCallCredentials.from(creds);
  }
  return null;
}