Search in sources :

Example 21 with ApiException

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

the class SDSSession method getRequiredKeyPairVersion.

private UserKeyPair.Version getRequiredKeyPairVersion() {
    final AlgorithmVersionInfoList algorithms;
    try {
        algorithms = new ConfigApi(client).requestAlgorithms(null);
        final List<AlgorithmVersionInfo> keyPairAlgorithms = algorithms.getKeyPairAlgorithms();
        for (AlgorithmVersionInfo kpa : keyPairAlgorithms) {
            if (kpa.getStatus() == AlgorithmVersionInfo.StatusEnum.REQUIRED) {
                return UserKeyPair.Version.getByValue(kpa.getVersion());
            }
        }
        log.error("No available key pair algorithm with status required found.");
    } catch (ApiException e) {
        log.warn(String.format("Ignore failure reading key pair version. %s", new SDSExceptionMappingService(nodeid).map(e)));
    } catch (UnknownVersionException e) {
        log.warn(String.format("Ignore failure reading required key pair algorithm. %s", new TripleCryptExceptionMappingService().map(e)));
    }
    return UserKeyPair.Version.RSA2048;
}
Also used : ConfigApi(ch.cyberduck.core.sds.io.swagger.client.api.ConfigApi) TripleCryptExceptionMappingService(ch.cyberduck.core.sds.triplecrypt.TripleCryptExceptionMappingService) AlgorithmVersionInfoList(ch.cyberduck.core.sds.io.swagger.client.model.AlgorithmVersionInfoList) AlgorithmVersionInfo(ch.cyberduck.core.sds.io.swagger.client.model.AlgorithmVersionInfo) UnknownVersionException(com.dracoon.sdk.crypto.error.UnknownVersionException) ApiException(ch.cyberduck.core.sds.io.swagger.client.ApiException)

Example 22 with ApiException

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

the class SDSUploadService method start.

/**
 * @param file   Remote path
 * @param status Length and modification date for file uploaded
 * @return Uplaod URI
 */
public CreateFileUploadResponse start(final Path file, final TransferStatus status) throws BackgroundException {
    try {
        final CreateFileUploadRequest body = new CreateFileUploadRequest().size(TransferStatus.UNKNOWN_LENGTH == status.getLength() ? null : status.getLength()).parentId(Long.parseLong(nodeid.getVersionId(file.getParent(), new DisabledListProgressListener()))).name(file.getName()).directS3Upload(null);
        if (status.getTimestamp() != null) {
            final SoftwareVersionData version = session.softwareVersion();
            final Matcher matcher = Pattern.compile(SDSSession.VERSION_REGEX).matcher(version.getRestApiVersion());
            if (matcher.matches()) {
                if (new Version(matcher.group(1)).compareTo(new Version("4.22")) >= 0) {
                    body.timestampModification(new DateTime(status.getTimestamp()));
                }
            }
        }
        return new NodesApi(session.getClient()).createFileUploadChannel(body, StringUtils.EMPTY);
    } catch (ApiException e) {
        throw new SDSExceptionMappingService(nodeid).map("Upload {0} failed", e, file);
    }
}
Also used : NodesApi(ch.cyberduck.core.sds.io.swagger.client.api.NodesApi) CreateFileUploadRequest(ch.cyberduck.core.sds.io.swagger.client.model.CreateFileUploadRequest) DisabledListProgressListener(ch.cyberduck.core.DisabledListProgressListener) Matcher(java.util.regex.Matcher) Version(ch.cyberduck.core.Version) SoftwareVersionData(ch.cyberduck.core.sds.io.swagger.client.model.SoftwareVersionData) DateTime(org.joda.time.DateTime) ApiException(ch.cyberduck.core.sds.io.swagger.client.ApiException)

Example 23 with ApiException

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

the class SDSUploadService method complete.

/**
 * Complete file upload
 *
 * @param file        Remote path
 * @param uploadToken Upload token
 * @param status      Transfer status
 * @return Node Id from server
 */
public Node complete(final Path file, final String uploadToken, final TransferStatus status) throws BackgroundException {
    try {
        final CompleteUploadRequest body = new CompleteUploadRequest().keepShareLinks(status.isExists() ? new HostPreferences(session.getHost()).getBoolean("sds.upload.sharelinks.keep") : false).resolutionStrategy(status.isExists() ? CompleteUploadRequest.ResolutionStrategyEnum.OVERWRITE : CompleteUploadRequest.ResolutionStrategyEnum.FAIL);
        if (status.getFilekey() != null) {
            final ObjectReader reader = session.getClient().getJSON().getContext(null).readerFor(FileKey.class);
            final FileKey fileKey = reader.readValue(status.getFilekey().array());
            final EncryptedFileKey encryptFileKey = Crypto.encryptFileKey(TripleCryptConverter.toCryptoPlainFileKey(fileKey), TripleCryptConverter.toCryptoUserPublicKey(session.keyPair().getPublicKeyContainer()));
            body.setFileKey(TripleCryptConverter.toSwaggerFileKey(encryptFileKey));
        }
        final Node upload = new UploadsApi(session.getClient()).completeFileUploadByToken(body, uploadToken, StringUtils.EMPTY);
        if (!upload.isIsEncrypted()) {
            final Checksum checksum = status.getChecksum();
            if (Checksum.NONE != checksum) {
                final Checksum server = Checksum.parse(upload.getHash());
                if (Checksum.NONE != server) {
                    if (checksum.algorithm.equals(server.algorithm)) {
                        if (!server.equals(checksum)) {
                            throw new ChecksumException(MessageFormat.format(LocaleFactory.localizedString("Upload {0} failed", "Error"), file.getName()), MessageFormat.format("Mismatch between MD5 hash {0} of uploaded data and ETag {1} returned by the server", checksum.hash, server.hash));
                        }
                    }
                }
            }
        }
        nodeid.cache(file, String.valueOf(upload.getId()));
        return upload;
    } catch (ApiException e) {
        throw new SDSExceptionMappingService(nodeid).map("Upload {0} failed", e, file);
    } catch (CryptoSystemException | InvalidFileKeyException | InvalidKeyPairException | UnknownVersionException e) {
        throw new TripleCryptExceptionMappingService().map("Upload {0} failed", e, file);
    } catch (IOException e) {
        throw new DefaultIOExceptionMappingService().map("Upload {0} failed", e, file);
    }
}
Also used : EncryptedFileKey(com.dracoon.sdk.crypto.model.EncryptedFileKey) FileKey(ch.cyberduck.core.sds.io.swagger.client.model.FileKey) InvalidFileKeyException(com.dracoon.sdk.crypto.error.InvalidFileKeyException) EncryptedFileKey(com.dracoon.sdk.crypto.model.EncryptedFileKey) ChecksumException(ch.cyberduck.core.exception.ChecksumException) Node(ch.cyberduck.core.sds.io.swagger.client.model.Node) UploadsApi(ch.cyberduck.core.sds.io.swagger.client.api.UploadsApi) InvalidKeyPairException(com.dracoon.sdk.crypto.error.InvalidKeyPairException) IOException(java.io.IOException) HostPreferences(ch.cyberduck.core.preferences.HostPreferences) Checksum(ch.cyberduck.core.io.Checksum) CompleteUploadRequest(ch.cyberduck.core.sds.io.swagger.client.model.CompleteUploadRequest) TripleCryptExceptionMappingService(ch.cyberduck.core.sds.triplecrypt.TripleCryptExceptionMappingService) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) DefaultIOExceptionMappingService(ch.cyberduck.core.DefaultIOExceptionMappingService) UnknownVersionException(com.dracoon.sdk.crypto.error.UnknownVersionException) ApiException(ch.cyberduck.core.sds.io.swagger.client.ApiException) CryptoSystemException(com.dracoon.sdk.crypto.error.CryptoSystemException)

Example 24 with ApiException

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

the class SDSSessionTest method testKeyPairMigration.

@Test
public void testKeyPairMigration() throws Exception {
    final UserApi userApi = new UserApi(session.getClient());
    try {
        userApi.removeUserKeyPair(UserKeyPair.Version.RSA2048.getValue(), null);
    } catch (ApiException e) {
        if (e.getCode() == HttpStatus.SC_NOT_FOUND) {
        // ignore
        } else {
            throw e;
        }
    }
    try {
        userApi.removeUserKeyPair(UserKeyPair.Version.RSA4096.getValue(), null);
    } catch (ApiException e) {
        if (e.getCode() == HttpStatus.SC_NOT_FOUND) {
        // ignore
        } else {
            throw e;
        }
    }
    // create legacy key pair
    final UserKeyPair userKeyPair = Crypto.generateUserKeyPair(UserKeyPair.Version.RSA2048, "eth[oh8uv4Eesij");
    userApi.setUserKeyPair(TripleCryptConverter.toSwaggerUserKeyPairContainer(userKeyPair), null);
    List<UserKeyPairContainer> keyPairs = userApi.requestUserKeyPairs(null, null);
    assertEquals(1, keyPairs.size());
    // Start migration
    session.unlockTripleCryptKeyPair(new DisabledLoginCallback() {

        @Override
        public Credentials prompt(final Host bookmark, final String title, final String reason, final LoginOptions options) throws LoginCanceledException {
            return new VaultCredentials("eth[oh8uv4Eesij");
        }
    }, session.userAccount(), UserKeyPair.Version.RSA4096);
    keyPairs = userApi.requestUserKeyPairs(null, null);
    assertEquals(2, keyPairs.size());
    assertEquals(UserKeyPair.Version.RSA4096.getValue(), session.keyPair().getPublicKeyContainer().getVersion());
    assertEquals(UserKeyPair.Version.RSA2048.getValue(), session.keyPairDeprecated().getPublicKeyContainer().getVersion());
}
Also used : UserKeyPair(com.dracoon.sdk.crypto.model.UserKeyPair) UserKeyPairContainer(ch.cyberduck.core.sds.io.swagger.client.model.UserKeyPairContainer) VaultCredentials(ch.cyberduck.core.vault.VaultCredentials) LoginCanceledException(ch.cyberduck.core.exception.LoginCanceledException) UserApi(ch.cyberduck.core.sds.io.swagger.client.api.UserApi) VaultCredentials(ch.cyberduck.core.vault.VaultCredentials) ApiException(ch.cyberduck.core.sds.io.swagger.client.ApiException) Test(org.junit.Test) IntegrationTest(ch.cyberduck.test.IntegrationTest)

Example 25 with ApiException

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

the class MultipartUploadTokenOutputStream method write.

@Override
public void write(final byte[] b, final int off, final int len) throws IOException {
    try {
        if (null != canceled.get()) {
            throw canceled.get();
        }
        final byte[] content = Arrays.copyOfRange(b, off, len);
        final HttpEntity entity = EntityBuilder.create().setBinary(content).build();
        new DefaultRetryCallable<>(session.getHost(), new BackgroundExceptionCallable<Void>() {

            @Override
            public Void call() throws BackgroundException {
                final SDSApiClient client = session.getClient();
                try {
                    final HttpPost request = new HttpPost(uploadUrl);
                    request.setEntity(entity);
                    request.setHeader(HttpHeaders.CONTENT_TYPE, MimeTypeService.DEFAULT_CONTENT_TYPE);
                    request.setHeader(SDSSession.SDS_AUTH_TOKEN_HEADER, StringUtils.EMPTY);
                    if (0L != overall.getLength() && 0 != content.length) {
                        final HttpRange range = HttpRange.byLength(offset, content.length);
                        final String header;
                        if (overall.getLength() == TransferStatus.UNKNOWN_LENGTH) {
                            header = String.format("%d-%d/*", range.getStart(), range.getEnd());
                        } else {
                            header = String.format("%d-%d/%d", range.getStart(), range.getEnd(), length);
                        }
                        request.addHeader(HttpHeaders.CONTENT_RANGE, String.format("bytes %s", header));
                    }
                    final HttpResponse response = client.getClient().execute(request);
                    try {
                        // Validate response
                        switch(response.getStatusLine().getStatusCode()) {
                            case HttpStatus.SC_CREATED:
                                // Upload complete
                                offset += content.length;
                                break;
                            default:
                                EntityUtils.updateEntity(response, new BufferedHttpEntity(response.getEntity()));
                                throw new SDSExceptionMappingService(nodeid).map(new ApiException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase(), Collections.emptyMap(), EntityUtils.toString(response.getEntity())));
                        }
                    } catch (BackgroundException e) {
                        canceled.set(e);
                        throw e;
                    } finally {
                        EntityUtils.consume(response.getEntity());
                    }
                } catch (HttpResponseException e) {
                    throw new DefaultHttpResponseExceptionMappingService().map(e);
                } catch (IOException e) {
                    throw new DefaultIOExceptionMappingService().map(e);
                }
                // Void
                return null;
            }
        }, overall).call();
    } catch (BackgroundException e) {
        throw new IOException(e.getMessage(), e);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) DefaultHttpResponseExceptionMappingService(ch.cyberduck.core.http.DefaultHttpResponseExceptionMappingService) HttpEntity(org.apache.http.HttpEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) HttpResponse(org.apache.http.HttpResponse) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException) BackgroundExceptionCallable(ch.cyberduck.core.threading.BackgroundExceptionCallable) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) DefaultIOExceptionMappingService(ch.cyberduck.core.DefaultIOExceptionMappingService) BackgroundException(ch.cyberduck.core.exception.BackgroundException) HttpRange(ch.cyberduck.core.http.HttpRange) 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