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;
}
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);
}
}
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);
}
}
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());
}
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);
}
}
Aggregations