Search in sources :

Example 1 with ApiException

use of ch.cyberduck.core.sds.io.swagger.client.ApiException in project cyberduck by iterate-ch.

the class SDSListService method list.

protected AttributedList<Path> list(final Path directory, final ListProgressListener listener, final int chunksize) throws BackgroundException {
    final AttributedList<Path> children = new AttributedList<Path>();
    try {
        int offset = 0;
        final SDSAttributesAdapter feature = new SDSAttributesAdapter(session);
        NodeList nodes;
        do {
            nodes = new NodesApi(session.getClient()).requestNodes(null, 0, Long.parseLong(nodeid.getVersionId(directory, new DisabledListProgressListener())), false, null, "name:asc", offset, chunksize, StringUtils.EMPTY);
            for (Node node : nodes.getItems()) {
                final PathAttributes attributes = feature.toAttributes(node);
                final EnumSet<Path.Type> type = feature.toType(node);
                final Path file = new Path(directory, node.getName(), type, attributes);
                if (references && node.getCntDeletedVersions() != null && node.getCntDeletedVersions() > 0) {
                    try {
                        final AttributedList<Path> versions = new SDSAttributesFinderFeature(session, nodeid).findDeleted(file, chunksize);
                        children.addAll(versions);
                        attributes.setVersions(versions);
                    } catch (AccessDeniedException e) {
                        log.warn(String.format("Ignore failure %s fetching versions for %s", e, file));
                    }
                }
                children.add(file);
                listener.chunk(directory, children);
            }
            offset += chunksize;
        } while (nodes.getItems().size() == chunksize);
    } catch (ApiException e) {
        throw new SDSExceptionMappingService(nodeid).map("Listing directory {0} failed", e, directory);
    }
    return children;
}
Also used : Path(ch.cyberduck.core.Path) AccessDeniedException(ch.cyberduck.core.exception.AccessDeniedException) DisabledListProgressListener(ch.cyberduck.core.DisabledListProgressListener) NodeList(ch.cyberduck.core.sds.io.swagger.client.model.NodeList) Node(ch.cyberduck.core.sds.io.swagger.client.model.Node) PathAttributes(ch.cyberduck.core.PathAttributes) NodesApi(ch.cyberduck.core.sds.io.swagger.client.api.NodesApi) AttributedList(ch.cyberduck.core.AttributedList) ApiException(ch.cyberduck.core.sds.io.swagger.client.ApiException)

Example 2 with ApiException

use of ch.cyberduck.core.sds.io.swagger.client.ApiException 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);
    }
}
Also used : MissingKeysResponse(ch.cyberduck.core.sds.io.swagger.client.model.MissingKeysResponse) DisabledListProgressListener(ch.cyberduck.core.DisabledListProgressListener) EncryptedFileKey(com.dracoon.sdk.crypto.model.EncryptedFileKey) ArrayList(java.util.ArrayList) TripleCryptKeyPair(ch.cyberduck.core.sds.triplecrypt.TripleCryptKeyPair) NodesApi(ch.cyberduck.core.sds.io.swagger.client.api.NodesApi) FileFileKeys(ch.cyberduck.core.sds.io.swagger.client.model.FileFileKeys) ArrayList(java.util.ArrayList) List(java.util.List) UserKeyPair(com.dracoon.sdk.crypto.model.UserKeyPair) UserKeyPairContainer(ch.cyberduck.core.sds.io.swagger.client.model.UserKeyPairContainer) VersionIdProvider(ch.cyberduck.core.features.VersionIdProvider) UserUserPublicKey(ch.cyberduck.core.sds.io.swagger.client.model.UserUserPublicKey) UserFileKeySetRequest(ch.cyberduck.core.sds.io.swagger.client.model.UserFileKeySetRequest) UserIdFileIdItem(ch.cyberduck.core.sds.io.swagger.client.model.UserIdFileIdItem) UserFileKeySetBatchRequest(ch.cyberduck.core.sds.io.swagger.client.model.UserFileKeySetBatchRequest) TripleCryptExceptionMappingService(ch.cyberduck.core.sds.triplecrypt.TripleCryptExceptionMappingService) CryptoException(com.dracoon.sdk.crypto.error.CryptoException) Credentials(ch.cyberduck.core.Credentials) ApiException(ch.cyberduck.core.sds.io.swagger.client.ApiException)

Example 3 with ApiException

use of ch.cyberduck.core.sds.io.swagger.client.ApiException in project cyberduck by iterate-ch.

the class SDSMoveFeature method move.

@Override
public Path move(final Path file, final Path renamed, final TransferStatus status, final Delete.Callback callback, final ConnectionCallback connectionCallback) throws BackgroundException {
    try {
        final long nodeId = Long.parseLong(nodeid.getVersionId(file, new DisabledListProgressListener()));
        if (containerService.isContainer(file)) {
            final Node node = new NodesApi(session.getClient()).updateRoom(new UpdateRoomRequest().name(renamed.getName()), nodeId, StringUtils.EMPTY, null);
            nodeid.cache(renamed, file.attributes().getVersionId());
            nodeid.cache(file, null);
            return renamed.withAttributes(new SDSAttributesAdapter(session).toAttributes(node));
        } else {
            if (status.isExists()) {
                // Handle case insensitive. Find feature will have reported target to exist if same name with different case
                if (!new CaseInsensitivePathPredicate(file).test(renamed)) {
                    log.warn(String.format("Delete existing file %s", renamed));
                    new SDSDeleteFeature(session, nodeid).delete(Collections.singletonMap(renamed, status), connectionCallback, callback);
                }
            }
            if (new SimplePathPredicate(file.getParent()).test(renamed.getParent())) {
                // Rename only
                if (file.isDirectory()) {
                    new NodesApi(session.getClient()).updateFolder(new UpdateFolderRequest().name(renamed.getName()), nodeId, StringUtils.EMPTY, null);
                } else {
                    new NodesApi(session.getClient()).updateFile(new UpdateFileRequest().name(renamed.getName()), nodeId, StringUtils.EMPTY, null);
                }
            } else {
                // Move to different parent
                new NodesApi(session.getClient()).moveNodes(new MoveNodesRequest().resolutionStrategy(MoveNodesRequest.ResolutionStrategyEnum.OVERWRITE).addItemsItem(new MoveNode().id(nodeId).name(renamed.getName())).keepShareLinks(new HostPreferences(session.getHost()).getBoolean("sds.upload.sharelinks.keep")), Long.parseLong(nodeid.getVersionId(renamed.getParent(), new DisabledListProgressListener())), StringUtils.EMPTY, null);
            }
            nodeid.cache(renamed, file.attributes().getVersionId());
            nodeid.cache(file, null);
            // Copy original file attributes
            return renamed.withAttributes(new PathAttributes(file.attributes()).withVersionId(String.valueOf(nodeId)));
        }
    } catch (ApiException e) {
        throw new SDSExceptionMappingService(nodeid).map("Cannot rename {0}", e, file);
    }
}
Also used : CaseInsensitivePathPredicate(ch.cyberduck.core.CaseInsensitivePathPredicate) DisabledListProgressListener(ch.cyberduck.core.DisabledListProgressListener) Node(ch.cyberduck.core.sds.io.swagger.client.model.Node) MoveNode(ch.cyberduck.core.sds.io.swagger.client.model.MoveNode) PathAttributes(ch.cyberduck.core.PathAttributes) MoveNode(ch.cyberduck.core.sds.io.swagger.client.model.MoveNode) HostPreferences(ch.cyberduck.core.preferences.HostPreferences) NodesApi(ch.cyberduck.core.sds.io.swagger.client.api.NodesApi) UpdateRoomRequest(ch.cyberduck.core.sds.io.swagger.client.model.UpdateRoomRequest) MoveNodesRequest(ch.cyberduck.core.sds.io.swagger.client.model.MoveNodesRequest) SimplePathPredicate(ch.cyberduck.core.SimplePathPredicate) UpdateFileRequest(ch.cyberduck.core.sds.io.swagger.client.model.UpdateFileRequest) UpdateFolderRequest(ch.cyberduck.core.sds.io.swagger.client.model.UpdateFolderRequest) ApiException(ch.cyberduck.core.sds.io.swagger.client.ApiException)

Example 4 with ApiException

use of ch.cyberduck.core.sds.io.swagger.client.ApiException in project cyberduck by iterate-ch.

the class SDSSharesUrlProvider method toDownloadUrl.

@Override
public DescriptiveUrl toDownloadUrl(final Path file, CreateDownloadShareRequest options, final PasswordCallback callback) throws BackgroundException {
    try {
        if (log.isDebugEnabled()) {
            log.debug(String.format("Create download share for %s", file));
        }
        if (null == options) {
            options = new CreateDownloadShareRequest();
            log.warn(String.format("Use default share options %s", options));
        }
        final Long fileid = Long.parseLong(nodeid.getVersionId(file, new DisabledListProgressListener()));
        final Host bookmark = session.getHost();
        if (SDSNodeIdProvider.isEncrypted(file)) {
            // get existing file key associated with the sharing user
            final FileKey key = new NodesApi(session.getClient()).requestUserFileKey(fileid, null, null);
            final EncryptedFileKey encFileKey = TripleCryptConverter.toCryptoEncryptedFileKey(key);
            final UserKeyPairContainer keyPairContainer = session.getKeyPairForFileKey(encFileKey.getVersion());
            final UserKeyPair userKeyPair = TripleCryptConverter.toCryptoUserKeyPair(keyPairContainer);
            final Credentials passphrase = new TripleCryptKeyPair().unlock(callback, bookmark, userKeyPair);
            final PlainFileKey plainFileKey = Crypto.decryptFileKey(encFileKey, userKeyPair.getUserPrivateKey(), passphrase.getPassword());
            // encrypt file key with a new key pair
            final UserKeyPair pair;
            if (null == options.getPassword()) {
                pair = Crypto.generateUserKeyPair(session.requiredKeyPairVersion(), callback.prompt(bookmark, LocaleFactory.localizedString("Passphrase", "Cryptomator"), LocaleFactory.localizedString("Provide additional login credentials", "Credentials"), new LoginOptions().icon(session.getHost().getProtocol().disk())).getPassword());
            } else {
                pair = Crypto.generateUserKeyPair(session.requiredKeyPairVersion(), options.getPassword());
            }
            final EncryptedFileKey encryptedFileKey = Crypto.encryptFileKey(plainFileKey, pair.getUserPublicKey());
            options.setPassword(null);
            options.setKeyPair(TripleCryptConverter.toSwaggerUserKeyPairContainer(pair));
            options.setFileKey(TripleCryptConverter.toSwaggerFileKey(encryptedFileKey));
        }
        final DownloadShare share = new SharesApi(session.getClient()).createDownloadShare(options.nodeId(fileid), StringUtils.EMPTY, null);
        final String help;
        if (null == share.getExpireAt()) {
            help = MessageFormat.format(LocaleFactory.localizedString("{0} URL"), LocaleFactory.localizedString("Pre-Signed", "S3"));
        } else {
            final long expiry = share.getExpireAt().getMillis();
            help = MessageFormat.format(LocaleFactory.localizedString("{0} URL"), LocaleFactory.localizedString("Pre-Signed", "S3")) + " (" + MessageFormat.format(LocaleFactory.localizedString("Expires {0}", "S3") + ")", UserDateFormatterFactory.get().getShortFormat(expiry * 1000));
        }
        return new DescriptiveUrl(URI.create(String.format("%s://%s/#/public/shares-downloads/%s", bookmark.getProtocol().getScheme(), bookmark.getHostname(), share.getAccessKey())), DescriptiveUrl.Type.signed, help);
    } catch (ApiException e) {
        throw new SDSExceptionMappingService(nodeid).map(e);
    } catch (CryptoException e) {
        throw new TripleCryptExceptionMappingService().map(e);
    }
}
Also used : UserKeyPair(com.dracoon.sdk.crypto.model.UserKeyPair) EncryptedFileKey(com.dracoon.sdk.crypto.model.EncryptedFileKey) PlainFileKey(com.dracoon.sdk.crypto.model.PlainFileKey) FileKey(ch.cyberduck.core.sds.io.swagger.client.model.FileKey) DownloadShare(ch.cyberduck.core.sds.io.swagger.client.model.DownloadShare) UserKeyPairContainer(ch.cyberduck.core.sds.io.swagger.client.model.UserKeyPairContainer) DisabledListProgressListener(ch.cyberduck.core.DisabledListProgressListener) EncryptedFileKey(com.dracoon.sdk.crypto.model.EncryptedFileKey) Host(ch.cyberduck.core.Host) CreateDownloadShareRequest(ch.cyberduck.core.sds.io.swagger.client.model.CreateDownloadShareRequest) TripleCryptKeyPair(ch.cyberduck.core.sds.triplecrypt.TripleCryptKeyPair) LoginOptions(ch.cyberduck.core.LoginOptions) NodesApi(ch.cyberduck.core.sds.io.swagger.client.api.NodesApi) DescriptiveUrl(ch.cyberduck.core.DescriptiveUrl) SharesApi(ch.cyberduck.core.sds.io.swagger.client.api.SharesApi) TripleCryptExceptionMappingService(ch.cyberduck.core.sds.triplecrypt.TripleCryptExceptionMappingService) CryptoException(com.dracoon.sdk.crypto.error.CryptoException) Credentials(ch.cyberduck.core.Credentials) PlainFileKey(com.dracoon.sdk.crypto.model.PlainFileKey) ApiException(ch.cyberduck.core.sds.io.swagger.client.ApiException)

Example 5 with ApiException

use of ch.cyberduck.core.sds.io.swagger.client.ApiException in project cyberduck by iterate-ch.

the class SDSSharesUrlProvider method toUploadUrl.

@Override
public DescriptiveUrl toUploadUrl(final Path file, CreateUploadShareRequest options, final PasswordCallback callback) throws BackgroundException {
    try {
        if (log.isDebugEnabled()) {
            log.debug(String.format("Create upload share for %s", file));
        }
        if (null == options) {
            options = new CreateUploadShareRequest();
            log.warn(String.format("Use default share options %s", options));
        }
        final Host bookmark = session.getHost();
        final UploadShare share = new SharesApi(session.getClient()).createUploadShare(options.targetId(Long.parseLong(nodeid.getVersionId(file, new DisabledListProgressListener()))), StringUtils.EMPTY, null);
        final String help;
        if (null == share.getExpireAt()) {
            help = MessageFormat.format(LocaleFactory.localizedString("{0} URL"), LocaleFactory.localizedString("Pre-Signed", "S3"));
        } else {
            final long expiry = share.getExpireAt().getMillis();
            help = MessageFormat.format(LocaleFactory.localizedString("{0} URL"), LocaleFactory.localizedString("Pre-Signed", "S3")) + " (" + MessageFormat.format(LocaleFactory.localizedString("Expires {0}", "S3") + ")", UserDateFormatterFactory.get().getShortFormat(expiry * 1000));
        }
        return new DescriptiveUrl(URI.create(String.format("%s://%s/#/public/shares-uploads/%s", bookmark.getProtocol().getScheme(), bookmark.getHostname(), share.getAccessKey())), DescriptiveUrl.Type.signed, help);
    } catch (ApiException e) {
        throw new SDSExceptionMappingService(nodeid).map(e);
    }
}
Also used : DescriptiveUrl(ch.cyberduck.core.DescriptiveUrl) DisabledListProgressListener(ch.cyberduck.core.DisabledListProgressListener) SharesApi(ch.cyberduck.core.sds.io.swagger.client.api.SharesApi) Host(ch.cyberduck.core.Host) CreateUploadShareRequest(ch.cyberduck.core.sds.io.swagger.client.model.CreateUploadShareRequest) UploadShare(ch.cyberduck.core.sds.io.swagger.client.model.UploadShare) ApiException(ch.cyberduck.core.sds.io.swagger.client.ApiException)

Aggregations

ApiException (ch.cyberduck.core.sds.io.swagger.client.ApiException)28 NodesApi (ch.cyberduck.core.sds.io.swagger.client.api.NodesApi)18 DisabledListProgressListener (ch.cyberduck.core.DisabledListProgressListener)14 TripleCryptExceptionMappingService (ch.cyberduck.core.sds.triplecrypt.TripleCryptExceptionMappingService)7 Node (ch.cyberduck.core.sds.io.swagger.client.model.Node)6 EncryptedFileKey (com.dracoon.sdk.crypto.model.EncryptedFileKey)6 IOException (java.io.IOException)6 DefaultIOExceptionMappingService (ch.cyberduck.core.DefaultIOExceptionMappingService)5 Path (ch.cyberduck.core.Path)5 PathAttributes (ch.cyberduck.core.PathAttributes)5 BackgroundException (ch.cyberduck.core.exception.BackgroundException)5 HostPreferences (ch.cyberduck.core.preferences.HostPreferences)5 FileKey (ch.cyberduck.core.sds.io.swagger.client.model.FileKey)5 UserKeyPair (com.dracoon.sdk.crypto.model.UserKeyPair)5 UserApi (ch.cyberduck.core.sds.io.swagger.client.api.UserApi)4 UserKeyPairContainer (ch.cyberduck.core.sds.io.swagger.client.model.UserKeyPairContainer)4 UnknownVersionException (com.dracoon.sdk.crypto.error.UnknownVersionException)4 InteroperabilityException (ch.cyberduck.core.exception.InteroperabilityException)3 DefaultHttpResponseExceptionMappingService (ch.cyberduck.core.http.DefaultHttpResponseExceptionMappingService)3 CreateFileUploadRequest (ch.cyberduck.core.sds.io.swagger.client.model.CreateFileUploadRequest)3