use of ch.cyberduck.core.sds.io.swagger.client.model.UserIdFileIdItem in project cyberduck by iterate-ch.
the class SDSMissingFileKeysSchedulerFeature method operate.
@Override
public List<UserFileKeySetRequest> operate(final Session<?> client, final PasswordCallback callback, final Path file) throws BackgroundException {
final SDSSession session = (SDSSession) client;
final SDSNodeIdProvider nodeid = (SDSNodeIdProvider) session._getFeature(VersionIdProvider.class);
try {
final UserAccountWrapper account = 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 UserKeyPairContainer userKeyPairContainer = session.keyPair();
final UserKeyPair keyPair = TripleCryptConverter.toCryptoUserKeyPair(userKeyPairContainer);
final TripleCryptKeyPair triplecrypt = new TripleCryptKeyPair();
final Credentials passphrase = triplecrypt.unlock(callback, session.getHost(), keyPair);
final UserKeyPairContainer userKeyPairContainerDeprecated = session.keyPairDeprecated();
Credentials passphraseDeprecated = passphrase;
if (userKeyPairContainerDeprecated != null) {
passphraseDeprecated = triplecrypt.unlock(callback, session.getHost(), TripleCryptConverter.toCryptoUserKeyPair(userKeyPairContainerDeprecated));
}
// Null when operating from scheduler. File reference is set for post upload.
final Long fileId = file != null ? Long.parseLong(nodeid.getVersionId(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));
}
request = new UserFileKeySetBatchRequest();
final MissingKeysResponse missingKeys = new NodesApi(session.getClient()).requestMissingFileKeys(null, null, null, fileId, null, null, null);
final Map<Long, List<UserUserPublicKey>> userPublicKeys = missingKeys.getUsers().stream().collect(groupingBy(UserUserPublicKey::getId));
final Map<Long, List<FileFileKeys>> files = missingKeys.getFiles().stream().collect(groupingBy(FileFileKeys::getId));
for (UserIdFileIdItem item : missingKeys.getItems()) {
for (FileFileKeys fileKey : files.get(item.getFileId())) {
final EncryptedFileKey encryptedFileKey = TripleCryptConverter.toCryptoEncryptedFileKey(fileKey.getFileKeyContainer());
final UserKeyPairContainer keyPairForDecryption = session.getKeyPairForFileKey(encryptedFileKey.getVersion());
for (UserUserPublicKey userPublicKey : userPublicKeys.get(item.getUserId())) {
final EncryptedFileKey fk = this.encryptFileKey(TripleCryptConverter.toCryptoUserPrivateKey(keyPairForDecryption.getPrivateKeyContainer()), encryptedFileKey.getVersion() == EncryptedFileKey.Version.RSA2048_AES256GCM ? passphraseDeprecated : passphrase, userPublicKey, fileKey);
final UserFileKeySetRequest keySetRequest = new UserFileKeySetRequest().fileId(item.getFileId()).userId(item.getUserId()).fileKey(TripleCryptConverter.toSwaggerFileKey(fk));
if (log.isDebugEnabled()) {
log.debug(String.format("Missing file key processed for file %d and user %d", item.getFileId(), item.getUserId()));
}
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);
processed.addAll(request.getItems());
}
} while (!request.getItems().isEmpty());
this.deleteDeprecatedKeyPair(session);
return processed;
} catch (ApiException e) {
throw new SDSExceptionMappingService(nodeid).map(e);
} catch (CryptoException e) {
throw new TripleCryptExceptionMappingService().map(e);
}
}
Aggregations