Search in sources :

Example 1 with Encrypter

use of org.dataportabilityproject.security.Encrypter in project data-transfer-project by google.

the class StartJobAction method encryptAndUpdateJobWithCredentials.

/**
 * Encrypt the export and import credentials with a new {@link SecretKey} and {@link PublicKey}
 * assigned to this job then update the data store to {@code State.CREDS_ENCRYPTED} state.
 */
private void encryptAndUpdateJobWithCredentials(UUID jobId, PortabilityJob job, String encryptedExportAuthCredential, String encryptedImportAuthCredential) {
    // Step 1 - Generate authSecretKey, a new SecretKey which must not be persisted as is.
    SecretKey authSecretKey = symmetricKeyGenerator.generate();
    // Step 2 - Encrypt the auth data with authSecretKey
    Encrypter secretKeyEncrypter = EncrypterFactory.create(authSecretKey);
    String doublyEncryptedExportAuthData = secretKeyEncrypter.encrypt(encryptedExportAuthCredential);
    String doublyEncryptedImportAuthData = secretKeyEncrypter.encrypt(encryptedImportAuthCredential);
    // Step 3 - Encrypt the authSecretKey itself with the authPublickey
    PublicKey authPublicKey = asymmetricKeyGenerator.parse(BaseEncoding.base64Url().decode(job.jobAuthorization().authPublicKey()));
    Encrypter asymmetricEncrypter = EncrypterFactory.create(authPublicKey);
    String encryptedAuthSecretKey = asymmetricEncrypter.encrypt(BaseEncoding.base64Url().encode(authSecretKey.getEncoded()));
    // Populate job with encrypted auth data
    JobAuthorization updatedJobAuthorization = job.jobAuthorization().toBuilder().setEncryptedExportAuthData(doublyEncryptedExportAuthData).setEncryptedImportAuthData(doublyEncryptedImportAuthData).setAuthSecretKey(encryptedAuthSecretKey).setState(JobAuthorization.State.CREDS_ENCRYPTED).build();
    job = job.toBuilder().setAndValidateJobAuthorization(updatedJobAuthorization).build();
    logger.debug("Updating job {} from CREDS_ENCRYPTION_KEY_GENERATED to CREDS_ENCRYPTED", jobId);
    try {
        store.updateJob(jobId, job);
        logger.debug("Updated job {} to CREDS_ENCRYPTED", jobId);
    } catch (IOException e) {
        throw new RuntimeException("Unable to update job", e);
    }
}
Also used : Encrypter(org.dataportabilityproject.security.Encrypter) JobAuthorization(org.dataportabilityproject.spi.cloud.types.JobAuthorization) SecretKey(javax.crypto.SecretKey) PublicKey(java.security.PublicKey) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 PublicKey (java.security.PublicKey)1 SecretKey (javax.crypto.SecretKey)1 Encrypter (org.dataportabilityproject.security.Encrypter)1 JobAuthorization (org.dataportabilityproject.spi.cloud.types.JobAuthorization)1