Java Code Examples for com.google.auth.oauth2.ServiceAccountCredentials#fromStream()

The following examples show how to use com.google.auth.oauth2.ServiceAccountCredentials#fromStream() . 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: GoogleCloudCredentialsConfig.java    From datacollector with Apache License 2.0 6 votes vote down vote up
/**
 * Reads a JSON credentials file for a service account from and returns any errors.
 *
 * @param issues list to append any discovered issues.
 * @return a generic credentials object
 */
private Credentials getCredentials(Stage.Context context, List<Stage.ConfigIssue> issues) {
  Credentials credentials = null;

  try (InputStream in = getCredentialsInputStream(context, issues)) {
    if (in != null) {
      credentials = ServiceAccountCredentials.fromStream(in);
    }
  } catch (IOException | IllegalArgumentException e) {
    LOG.error(Errors.GOOGLE_02.getMessage(), e);
    issues.add(context.createConfigIssue("CREDENTIALS",
        CONF_CREDENTIALS_CREDENTIALS_PROVIDER,
        Errors.GOOGLE_02
    ));
  }

  return credentials;
}
 
Example 2
Source File: BeastFactory.java    From beast with Apache License 2.0 5 votes vote down vote up
private GoogleCredentials getGoogleCredentials() {
    GoogleCredentials credentials = null;
    File credentialsPath = new File(bqConfig.getGoogleCredentials());
    try (FileInputStream serviceAccountStream = new FileInputStream(credentialsPath)) {
        credentials = ServiceAccountCredentials.fromStream(serviceAccountStream);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return credentials;
}
 
Example 3
Source File: BaseBQTest.java    From beast with Apache License 2.0 5 votes vote down vote up
private GoogleCredentials getGoogleCredentials() {
    GoogleCredentials credentials = null;
    File credentialsPath = new File(System.getenv("GOOGLE_CREDENTIALS"));
    try (FileInputStream serviceAccountStream = new FileInputStream(credentialsPath)) {
        credentials = ServiceAccountCredentials.fromStream(serviceAccountStream);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return credentials;
}
 
Example 4
Source File: GoogleCloudDatastoreFactory.java    From nexus-blobstore-google-cloud with Eclipse Public License 1.0 5 votes vote down vote up
Datastore create(final BlobStoreConfiguration configuration) throws Exception {
  DatastoreOptions.Builder builder = DatastoreOptions.newBuilder().setTransportOptions(transportOptions());

  String credentialFile = configuration.attributes(CONFIG_KEY).get(CREDENTIAL_FILE_KEY, String.class);
  if (StringUtils.hasText(credentialFile)) {
    ServiceAccountCredentials credentials = ServiceAccountCredentials.fromStream(new FileInputStream(credentialFile));
    logger.debug("loaded {} from {} for Google Datastore client", credentials, credentialFile);
    builder.setCredentials(credentials);
    builder.setProjectId(getProjectId(credentialFile));
  }

  return builder.build().getService();
}
 
Example 5
Source File: GoogleCloudStorageFactory.java    From nexus-blobstore-google-cloud with Eclipse Public License 1.0 5 votes vote down vote up
Storage create(final BlobStoreConfiguration configuration) throws Exception {
  StorageOptions.Builder builder = StorageOptions.newBuilder().setTransportOptions(transportOptions());

  String credentialFile = configuration.attributes(CONFIG_KEY).get(CREDENTIAL_FILE_KEY, String.class);
  if (StringUtils.hasText(credentialFile)) {
    ServiceAccountCredentials credentials = ServiceAccountCredentials.fromStream(new FileInputStream(credentialFile));
    logger.debug("loaded {} from {} for Google storage client", credentials, credentialFile);
    builder.setCredentials(credentials);
    builder.setProjectId(getProjectId(credentialFile));
  }

  return builder.build().getService();
}
 
Example 6
Source File: CryptoSignersTest.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testServiceAccountCryptoSigner() throws IOException {
  ServiceAccountCredentials credentials = ServiceAccountCredentials.fromStream(
      ServiceAccount.EDITOR.asStream());
  byte[] expected = credentials.sign("foo".getBytes());
  CryptoSigner signer = new CryptoSigners.ServiceAccountCryptoSigner(credentials);
  byte[] data = signer.sign("foo".getBytes());
  assertArrayEquals(expected, data);
}
 
Example 7
Source File: GcpConfiguration.java    From spydra with Apache License 2.0 5 votes vote down vote up
@Override
public Credentials getCredentials() {
  try {
    return ServiceAccountCredentials.fromStream(
        new ByteArrayInputStream(credentialJsonFromEnv().getBytes("UTF-8")));
  } catch (IOException e) {
    throw new RuntimeException("Failed to load service account credentials from file", e);
  }
}
 
Example 8
Source File: EntityManagerFactory.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
/**
 * Creates and returns the credentials from the given connection parameters.
 * 
 * @param parameters
 *          the connection parameters
 * @return the credentials for authenticating with the Datastore service.
 * @throws IOException
 *           if any error occurs such as not able to read the credentials file.
 */
private static Credentials getCredentials(ConnectionParameters parameters) throws IOException {
  if (parameters.isEmulator()) {
    return NoCredentials.getInstance();
  }
  InputStream jsonCredentialsStream = parameters.getJsonCredentialsStream();
  if (jsonCredentialsStream != null) {
    return ServiceAccountCredentials.fromStream(jsonCredentialsStream);
  }
  File jsonCredentialsFile = parameters.getJsonCredentialsFile();
  if (jsonCredentialsFile != null) {
    return ServiceAccountCredentials.fromStream(new FileInputStream(jsonCredentialsFile));
  }
  return ServiceAccountCredentials.getApplicationDefault();
}
 
Example 9
Source File: GoogleJwtClient.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Generates a signed JSON Web Token using a Google API Service Account
 * utilizes com.auth0.jwt.
 */
public static String generateJwt(final String saKeyfile, final String saEmail,
    final String audience, final int expiryLength)
    throws FileNotFoundException, IOException {

  Date now = new Date();
  Date expTime = new Date(System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(expiryLength));

  // Build the JWT payload
  JWTCreator.Builder token = JWT.create()
      .withIssuedAt(now)
      // Expires after 'expiraryLength' seconds
      .withExpiresAt(expTime)
      // Must match 'issuer' in the security configuration in your
      // swagger spec (e.g. service account email)
      .withIssuer(saEmail)
      // Must be either your Endpoints service name, or match the value
      // specified as the 'x-google-audience' in the OpenAPI document
      .withAudience(audience)
      // Subject and email should match the service account's email
      .withSubject(saEmail)
      .withClaim("email", saEmail);

  // Sign the JWT with a service account
  FileInputStream stream = new FileInputStream(saKeyfile);
  ServiceAccountCredentials cred = ServiceAccountCredentials.fromStream(stream);
  RSAPrivateKey key = (RSAPrivateKey) cred.getPrivateKey();
  Algorithm algorithm = Algorithm.RSA256(null, key);
  return token.sign(algorithm);
}