Search in sources :

Example 1 with MasterPasswordChangeResult

use of io.hops.hopsworks.common.security.MasterPasswordChangeResult in project hopsworks by logicalclocks.

the class SecretsPasswordHandler method perform.

@Override
public MasterPasswordChangeResult perform(String oldPassword, String newPassword) {
    Map<SecretId, byte[]> secrets2Rollback = new HashMap<>();
    StringBuilder successLog = new StringBuilder();
    successLog.append("Performing change of master password for Secrets\n");
    SecretId secretId;
    Secret newSecret;
    SymmetricEncryptionDescriptor inDescriptor;
    SymmetricEncryptionDescriptor outDescriptor;
    try {
        LOGGER.log(Level.INFO, "Updating Secrets with new Hopsworks master encryption password");
        List<Secret> cipheredSecrets = secretsController.getAllCiphered();
        for (Secret cipheredSecret : cipheredSecrets) {
            secretId = cipheredSecret.getId();
            secrets2Rollback.put(secretId, cipheredSecret.getSecret());
            // First decrypt with the old password
            byte[][] cryptoPrimitives = symmetricEncryptionService.splitPayloadFromCryptoPrimitives(cipheredSecret.getSecret());
            inDescriptor = new SymmetricEncryptionDescriptor.Builder().setPassword(oldPassword).setSalt(cryptoPrimitives[0]).setIV(cryptoPrimitives[1]).setInput(cryptoPrimitives[2]).build();
            outDescriptor = symmetricEncryptionService.decrypt(inDescriptor);
            inDescriptor.clearPassword();
            // Then encrypt plaintext secret with the new password
            inDescriptor = new SymmetricEncryptionDescriptor.Builder().setInput(outDescriptor.getOutput()).setPassword(newPassword).build();
            outDescriptor = symmetricEncryptionService.encrypt(inDescriptor);
            inDescriptor.clearPassword();
            byte[] newCipheredSecret = symmetricEncryptionService.mergePayloadWithCryptoPrimitives(outDescriptor.getSalt(), outDescriptor.getIv(), outDescriptor.getOutput());
            // Store new API key
            newSecret = new Secret(secretId, newCipheredSecret, cipheredSecret.getAddedOn());
            newSecret.setVisibilityType(cipheredSecret.getVisibilityType());
            if (cipheredSecret.getProjectIdScope() != null) {
                newSecret.setProjectIdScope(cipheredSecret.getProjectIdScope());
            }
            secretsFacade.update(newSecret);
            successLog.append("Updated Secret <").append(newSecret.getId().getUid()).append(",").append(newSecret.getId().getName()).append(">\n");
        }
        return new MasterPasswordChangeResult<>(successLog, secrets2Rollback, null);
    } catch (Exception ex) {
        String errorMsg = "Error while updating master encryption password for Secrets";
        LOGGER.log(Level.SEVERE, errorMsg, ex);
        return new MasterPasswordChangeResult<>(secrets2Rollback, new EncryptionMasterPasswordException(errorMsg, ex));
    }
}
Also used : SymmetricEncryptionDescriptor(io.hops.hopsworks.common.security.SymmetricEncryptionDescriptor) HashMap(java.util.HashMap) EncryptionMasterPasswordException(io.hops.hopsworks.exceptions.EncryptionMasterPasswordException) Secret(io.hops.hopsworks.persistence.entity.user.security.secrets.Secret) MasterPasswordChangeResult(io.hops.hopsworks.common.security.MasterPasswordChangeResult) SecretId(io.hops.hopsworks.persistence.entity.user.security.secrets.SecretId) EncryptionMasterPasswordException(io.hops.hopsworks.exceptions.EncryptionMasterPasswordException)

Aggregations

MasterPasswordChangeResult (io.hops.hopsworks.common.security.MasterPasswordChangeResult)1 SymmetricEncryptionDescriptor (io.hops.hopsworks.common.security.SymmetricEncryptionDescriptor)1 EncryptionMasterPasswordException (io.hops.hopsworks.exceptions.EncryptionMasterPasswordException)1 Secret (io.hops.hopsworks.persistence.entity.user.security.secrets.Secret)1 SecretId (io.hops.hopsworks.persistence.entity.user.security.secrets.SecretId)1 HashMap (java.util.HashMap)1