@Test(expected = LoginCanceledException.class) public void testWrongPassword() throws Exception { final SDSNodeIdProvider nodeid = new SDSNodeIdProvider(session).withCache(cache); final SDSMissingFileKeysSchedulerFeature background = new SDSMissingFileKeysSchedulerFeature(); final AtomicBoolean prompt = new AtomicBoolean(); final List<UserFileKeySetRequest> processed = background.operate(session, new DisabledPasswordCallback() { @Override public Credentials prompt(final Host bookmark, final String title, final String reason, final LoginOptions options) throws LoginCanceledException { if(prompt.get()) { throw new LoginCanceledException(); } prompt.set(true); return new VaultCredentials("n"); } }, null); assertTrue(prompt.get()); }
@Override public List<UserFileKeySetRequest> operate(final Session<SDSApiClient> session, final PasswordCallback callback, final Path file) throws BackgroundException { try { final UserAccountWrapper account = ((SDSSession) session).userAccount(); if(!account.isEncryptionEnabled()) { log.warn(String.format("No key pair found in user account %s", account)); return Collections.emptyList(); } final List<UserFileKeySetRequest> processed = new ArrayList<>(); final UserPrivateKey privateKey = new UserPrivateKey(); final UserKeyPairContainer keyPairContainer = ((SDSSession) session).keyPair(); privateKey.setPrivateKey(keyPairContainer.getPrivateKeyContainer().getPrivateKey()); privateKey.setVersion(keyPairContainer.getPrivateKeyContainer().getVersion()); final UserKeyPair userKeyPair = new UserKeyPair(); userKeyPair.setUserPrivateKey(privateKey); final Credentials passphrase = new TripleCryptKeyPair().unlock(callback, session.getHost(), userKeyPair); final IdProvider node = session.getFeature(IdProvider.class); final Long fileId = file != null ? Long.parseLong(node.getFileid(file, new DisabledListProgressListener())) : null; UserFileKeySetBatchRequest request; do { if(log.isDebugEnabled()) { log.debug(String.format("Request a list of missing file keys for file %s", file)); } final MissingKeysResponse missingKeys = new NodesApi(session.getClient()).requestMissingFileKeys( null, null, null, fileId, null, null); final Map<Long, UserUserPublicKey> publicKeys = missingKeys.getUsers().stream().collect(Collectors.toMap(UserUserPublicKey::getId, Function.identity())); final Map<Long, FileFileKeys> files = missingKeys.getFiles().stream().collect(Collectors.toMap(FileFileKeys::getId, Function.identity())); request = new UserFileKeySetBatchRequest(); for(UserIdFileIdItem item : missingKeys.getItems()) { final UserUserPublicKey publicKey = publicKeys.get(item.getUserId()); final FileFileKeys fileKeys = files.get(item.getFileId()); final UserFileKeySetRequest keySetRequest = new UserFileKeySetRequest() .fileId(item.getFileId()) .userId(item.getUserId()); processed.add(keySetRequest); final PlainFileKey plainFileKey = Crypto.decryptFileKey( TripleCryptConverter.toCryptoEncryptedFileKey(fileKeys.getFileKeyContainer()), privateKey, passphrase.getPassword()); final EncryptedFileKey encryptFileKey = Crypto.encryptFileKey( plainFileKey, TripleCryptConverter.toCryptoUserPublicKey(publicKey.getPublicKeyContainer()) ); keySetRequest.setFileKey(TripleCryptConverter.toSwaggerFileKey(encryptFileKey)); if(log.isDebugEnabled()) { log.debug(String.format("Missing file key for file with id %d processed", item.getFileId())); } request.addItemsItem(keySetRequest); } if(!request.getItems().isEmpty()) { if(log.isDebugEnabled()) { log.debug(String.format("Set file keys with %s", request)); } new NodesApi(session.getClient()).setUserFileKeys(request, StringUtils.EMPTY); } } while(!request.getItems().isEmpty()); return processed; } catch(ApiException e) { throw new SDSExceptionMappingService().map(e); } catch(CryptoException e) { throw new TripleCryptExceptionMappingService().map(e); } }
SDSMissingFileKeysSchedulerFeature extends AbstractSchedulerFeature<List<UserFileKeySetRequest>, SDSApiClient> { @Override public List<UserFileKeySetRequest> operate(final Session<SDSApiClient> session, final PasswordCallback callback, final Path file) throws BackgroundException { try { final UserAccountWrapper account = ((SDSSession) session).userAccount(); if(!account.isEncryptionEnabled()) { log.warn(String.format("No key pair found in user account %s", account)); return Collections.emptyList(); } final List<UserFileKeySetRequest> processed = new ArrayList<>(); final UserPrivateKey privateKey = new UserPrivateKey(); final UserKeyPairContainer keyPairContainer = ((SDSSession) session).keyPair(); privateKey.setPrivateKey(keyPairContainer.getPrivateKeyContainer().getPrivateKey()); privateKey.setVersion(keyPairContainer.getPrivateKeyContainer().getVersion()); final UserKeyPair userKeyPair = new UserKeyPair(); userKeyPair.setUserPrivateKey(privateKey); final Credentials passphrase = new TripleCryptKeyPair().unlock(callback, session.getHost(), userKeyPair); final IdProvider node = session.getFeature(IdProvider.class); final Long fileId = file != null ? Long.parseLong(node.getFileid(file, new DisabledListProgressListener())) : null; UserFileKeySetBatchRequest request; do { if(log.isDebugEnabled()) { log.debug(String.format("Request a list of missing file keys for file %s", file)); } final MissingKeysResponse missingKeys = new NodesApi(session.getClient()).requestMissingFileKeys( null, null, null, fileId, null, null); final Map<Long, UserUserPublicKey> publicKeys = missingKeys.getUsers().stream().collect(Collectors.toMap(UserUserPublicKey::getId, Function.identity())); final Map<Long, FileFileKeys> files = missingKeys.getFiles().stream().collect(Collectors.toMap(FileFileKeys::getId, Function.identity())); request = new UserFileKeySetBatchRequest(); for(UserIdFileIdItem item : missingKeys.getItems()) { final UserUserPublicKey publicKey = publicKeys.get(item.getUserId()); final FileFileKeys fileKeys = files.get(item.getFileId()); final UserFileKeySetRequest keySetRequest = new UserFileKeySetRequest() .fileId(item.getFileId()) .userId(item.getUserId()); processed.add(keySetRequest); final PlainFileKey plainFileKey = Crypto.decryptFileKey( TripleCryptConverter.toCryptoEncryptedFileKey(fileKeys.getFileKeyContainer()), privateKey, passphrase.getPassword()); final EncryptedFileKey encryptFileKey = Crypto.encryptFileKey( plainFileKey, TripleCryptConverter.toCryptoUserPublicKey(publicKey.getPublicKeyContainer()) ); keySetRequest.setFileKey(TripleCryptConverter.toSwaggerFileKey(encryptFileKey)); if(log.isDebugEnabled()) { log.debug(String.format("Missing file key for file with id %d processed", item.getFileId())); } request.addItemsItem(keySetRequest); } if(!request.getItems().isEmpty()) { if(log.isDebugEnabled()) { log.debug(String.format("Set file keys with %s", request)); } new NodesApi(session.getClient()).setUserFileKeys(request, StringUtils.EMPTY); } } while(!request.getItems().isEmpty()); return processed; } catch(ApiException e) { throw new SDSExceptionMappingService().map(e); } catch(CryptoException e) { throw new TripleCryptExceptionMappingService().map(e); } } }
SDSMissingFileKeysSchedulerFeature extends AbstractSchedulerFeature<List<UserFileKeySetRequest>, SDSApiClient> { @Override public List<UserFileKeySetRequest> operate(final Session<SDSApiClient> session, final PasswordCallback callback, final Path file) throws BackgroundException { try { final UserAccountWrapper account = ((SDSSession) session).userAccount(); if(!account.isEncryptionEnabled()) { log.warn(String.format("No key pair found in user account %s", account)); return Collections.emptyList(); } final List<UserFileKeySetRequest> processed = new ArrayList<>(); final UserPrivateKey privateKey = new UserPrivateKey(); final UserKeyPairContainer keyPairContainer = ((SDSSession) session).keyPair(); privateKey.setPrivateKey(keyPairContainer.getPrivateKeyContainer().getPrivateKey()); privateKey.setVersion(keyPairContainer.getPrivateKeyContainer().getVersion()); final UserKeyPair userKeyPair = new UserKeyPair(); userKeyPair.setUserPrivateKey(privateKey); final Credentials passphrase = new TripleCryptKeyPair().unlock(callback, session.getHost(), userKeyPair); final IdProvider node = session.getFeature(IdProvider.class); final Long fileId = file != null ? Long.parseLong(node.getFileid(file, new DisabledListProgressListener())) : null; UserFileKeySetBatchRequest request; do { if(log.isDebugEnabled()) { log.debug(String.format("Request a list of missing file keys for file %s", file)); } final MissingKeysResponse missingKeys = new NodesApi(session.getClient()).requestMissingFileKeys( null, null, null, fileId, null, null); final Map<Long, UserUserPublicKey> publicKeys = missingKeys.getUsers().stream().collect(Collectors.toMap(UserUserPublicKey::getId, Function.identity())); final Map<Long, FileFileKeys> files = missingKeys.getFiles().stream().collect(Collectors.toMap(FileFileKeys::getId, Function.identity())); request = new UserFileKeySetBatchRequest(); for(UserIdFileIdItem item : missingKeys.getItems()) { final UserUserPublicKey publicKey = publicKeys.get(item.getUserId()); final FileFileKeys fileKeys = files.get(item.getFileId()); final UserFileKeySetRequest keySetRequest = new UserFileKeySetRequest() .fileId(item.getFileId()) .userId(item.getUserId()); processed.add(keySetRequest); final PlainFileKey plainFileKey = Crypto.decryptFileKey( TripleCryptConverter.toCryptoEncryptedFileKey(fileKeys.getFileKeyContainer()), privateKey, passphrase.getPassword()); final EncryptedFileKey encryptFileKey = Crypto.encryptFileKey( plainFileKey, TripleCryptConverter.toCryptoUserPublicKey(publicKey.getPublicKeyContainer()) ); keySetRequest.setFileKey(TripleCryptConverter.toSwaggerFileKey(encryptFileKey)); if(log.isDebugEnabled()) { log.debug(String.format("Missing file key for file with id %d processed", item.getFileId())); } request.addItemsItem(keySetRequest); } if(!request.getItems().isEmpty()) { if(log.isDebugEnabled()) { log.debug(String.format("Set file keys with %s", request)); } new NodesApi(session.getClient()).setUserFileKeys(request, StringUtils.EMPTY); } } while(!request.getItems().isEmpty()); return processed; } catch(ApiException e) { throw new SDSExceptionMappingService().map(e); } catch(CryptoException e) { throw new TripleCryptExceptionMappingService().map(e); } } SDSMissingFileKeysSchedulerFeature();  SDSMissingFileKeysSchedulerFeature(final long period); }
SDSMissingFileKeysSchedulerFeature extends AbstractSchedulerFeature<List<UserFileKeySetRequest>, SDSApiClient> { @Override public List<UserFileKeySetRequest> operate(final Session<SDSApiClient> session, final PasswordCallback callback, final Path file) throws BackgroundException { try { final UserAccountWrapper account = ((SDSSession) session).userAccount(); if(!account.isEncryptionEnabled()) { log.warn(String.format("No key pair found in user account %s", account)); return Collections.emptyList(); } final List<UserFileKeySetRequest> processed = new ArrayList<>(); final UserPrivateKey privateKey = new UserPrivateKey(); final UserKeyPairContainer keyPairContainer = ((SDSSession) session).keyPair(); privateKey.setPrivateKey(keyPairContainer.getPrivateKeyContainer().getPrivateKey()); privateKey.setVersion(keyPairContainer.getPrivateKeyContainer().getVersion()); final UserKeyPair userKeyPair = new UserKeyPair(); userKeyPair.setUserPrivateKey(privateKey); final Credentials passphrase = new TripleCryptKeyPair().unlock(callback, session.getHost(), userKeyPair); final IdProvider node = session.getFeature(IdProvider.class); final Long fileId = file != null ? Long.parseLong(node.getFileid(file, new DisabledListProgressListener())) : null; UserFileKeySetBatchRequest request; do { if(log.isDebugEnabled()) { log.debug(String.format("Request a list of missing file keys for file %s", file)); } final MissingKeysResponse missingKeys = new NodesApi(session.getClient()).requestMissingFileKeys( null, null, null, fileId, null, null); final Map<Long, UserUserPublicKey> publicKeys = missingKeys.getUsers().stream().collect(Collectors.toMap(UserUserPublicKey::getId, Function.identity())); final Map<Long, FileFileKeys> files = missingKeys.getFiles().stream().collect(Collectors.toMap(FileFileKeys::getId, Function.identity())); request = new UserFileKeySetBatchRequest(); for(UserIdFileIdItem item : missingKeys.getItems()) { final UserUserPublicKey publicKey = publicKeys.get(item.getUserId()); final FileFileKeys fileKeys = files.get(item.getFileId()); final UserFileKeySetRequest keySetRequest = new UserFileKeySetRequest() .fileId(item.getFileId()) .userId(item.getUserId()); processed.add(keySetRequest); final PlainFileKey plainFileKey = Crypto.decryptFileKey( TripleCryptConverter.toCryptoEncryptedFileKey(fileKeys.getFileKeyContainer()), privateKey, passphrase.getPassword()); final EncryptedFileKey encryptFileKey = Crypto.encryptFileKey( plainFileKey, TripleCryptConverter.toCryptoUserPublicKey(publicKey.getPublicKeyContainer()) ); keySetRequest.setFileKey(TripleCryptConverter.toSwaggerFileKey(encryptFileKey)); if(log.isDebugEnabled()) { log.debug(String.format("Missing file key for file with id %d processed", item.getFileId())); } request.addItemsItem(keySetRequest); } if(!request.getItems().isEmpty()) { if(log.isDebugEnabled()) { log.debug(String.format("Set file keys with %s", request)); } new NodesApi(session.getClient()).setUserFileKeys(request, StringUtils.EMPTY); } } while(!request.getItems().isEmpty()); return processed; } catch(ApiException e) { throw new SDSExceptionMappingService().map(e); } catch(CryptoException e) { throw new TripleCryptExceptionMappingService().map(e); } } SDSMissingFileKeysSchedulerFeature();  SDSMissingFileKeysSchedulerFeature(final long period); @Override List<UserFileKeySetRequest> operate(final Session<SDSApiClient> session, final PasswordCallback callback, final Path file); }
SDSMissingFileKeysSchedulerFeature extends AbstractSchedulerFeature<List<UserFileKeySetRequest>, SDSApiClient> { @Override public List<UserFileKeySetRequest> operate(final Session<SDSApiClient> session, final PasswordCallback callback, final Path file) throws BackgroundException { try { final UserAccountWrapper account = ((SDSSession) session).userAccount(); if(!account.isEncryptionEnabled()) { log.warn(String.format("No key pair found in user account %s", account)); return Collections.emptyList(); } final List<UserFileKeySetRequest> processed = new ArrayList<>(); final UserPrivateKey privateKey = new UserPrivateKey(); final UserKeyPairContainer keyPairContainer = ((SDSSession) session).keyPair(); privateKey.setPrivateKey(keyPairContainer.getPrivateKeyContainer().getPrivateKey()); privateKey.setVersion(keyPairContainer.getPrivateKeyContainer().getVersion()); final UserKeyPair userKeyPair = new UserKeyPair(); userKeyPair.setUserPrivateKey(privateKey); final Credentials passphrase = new TripleCryptKeyPair().unlock(callback, session.getHost(), userKeyPair); final IdProvider node = session.getFeature(IdProvider.class); final Long fileId = file != null ? Long.parseLong(node.getFileid(file, new DisabledListProgressListener())) : null; UserFileKeySetBatchRequest request; do { if(log.isDebugEnabled()) { log.debug(String.format("Request a list of missing file keys for file %s", file)); } final MissingKeysResponse missingKeys = new NodesApi(session.getClient()).requestMissingFileKeys( null, null, null, fileId, null, null); final Map<Long, UserUserPublicKey> publicKeys = missingKeys.getUsers().stream().collect(Collectors.toMap(UserUserPublicKey::getId, Function.identity())); final Map<Long, FileFileKeys> files = missingKeys.getFiles().stream().collect(Collectors.toMap(FileFileKeys::getId, Function.identity())); request = new UserFileKeySetBatchRequest(); for(UserIdFileIdItem item : missingKeys.getItems()) { final UserUserPublicKey publicKey = publicKeys.get(item.getUserId()); final FileFileKeys fileKeys = files.get(item.getFileId()); final UserFileKeySetRequest keySetRequest = new UserFileKeySetRequest() .fileId(item.getFileId()) .userId(item.getUserId()); processed.add(keySetRequest); final PlainFileKey plainFileKey = Crypto.decryptFileKey( TripleCryptConverter.toCryptoEncryptedFileKey(fileKeys.getFileKeyContainer()), privateKey, passphrase.getPassword()); final EncryptedFileKey encryptFileKey = Crypto.encryptFileKey( plainFileKey, TripleCryptConverter.toCryptoUserPublicKey(publicKey.getPublicKeyContainer()) ); keySetRequest.setFileKey(TripleCryptConverter.toSwaggerFileKey(encryptFileKey)); if(log.isDebugEnabled()) { log.debug(String.format("Missing file key for file with id %d processed", item.getFileId())); } request.addItemsItem(keySetRequest); } if(!request.getItems().isEmpty()) { if(log.isDebugEnabled()) { log.debug(String.format("Set file keys with %s", request)); } new NodesApi(session.getClient()).setUserFileKeys(request, StringUtils.EMPTY); } } while(!request.getItems().isEmpty()); return processed; } catch(ApiException e) { throw new SDSExceptionMappingService().map(e); } catch(CryptoException e) { throw new TripleCryptExceptionMappingService().map(e); } } SDSMissingFileKeysSchedulerFeature();  SDSMissingFileKeysSchedulerFeature(final long period); @Override List<UserFileKeySetRequest> operate(final Session<SDSApiClient> session, final PasswordCallback callback, final Path file);  }