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);
}
}
Aggregations