Search in sources :

Example 1 with Encryption

use of ch.cyberduck.core.features.Encryption in project cyberduck by iterate-ch.

the class AbstractUploadFilter method prepare.

@Override
public TransferStatus prepare(final Path file, final Local local, final TransferStatus parent, final ProgressListener progress) throws BackgroundException {
    if (log.isDebugEnabled()) {
        log.debug(String.format("Prepare %s", file));
    }
    final TransferStatus status = new TransferStatus().hidden(!hidden.accept(file)).withLockId(parent.getLockId());
    // Read remote attributes first
    if (parent.isExists()) {
        if (find.find(file)) {
            status.setExists(true);
            // Read remote attributes
            final PathAttributes attributes = attribute.find(file);
            status.setRemote(attributes);
        } else {
            // Look if there is directory or file that clashes with this upload
            if (file.getType().contains(Path.Type.file)) {
                if (find.find(new Path(file.getAbsolute(), EnumSet.of(Path.Type.directory)))) {
                    throw new AccessDeniedException(String.format("Cannot replace folder %s with file %s", file.getAbsolute(), local.getName()));
                }
            }
            if (file.getType().contains(Path.Type.directory)) {
                if (find.find(new Path(file.getAbsolute(), EnumSet.of(Path.Type.file)))) {
                    throw new AccessDeniedException(String.format("Cannot replace file %s with folder %s", file.getAbsolute(), local.getName()));
                }
            }
        }
    }
    if (file.isFile()) {
        // Set content length from local file
        if (local.isSymbolicLink()) {
            if (!symlinkResolver.resolve(local)) {
                // Will resolve the symbolic link when the file is requested.
                final Local target = local.getSymlinkTarget();
                status.setLength(target.attributes().getSize());
            }
        // No file size increase for symbolic link to be created on the server
        } else {
            // Read file size from filesystem
            status.setLength(local.attributes().getSize());
        }
        if (options.temporary) {
            final Move feature = session.getFeature(Move.class);
            final Path renamed = new Path(file.getParent(), MessageFormat.format(preferences.getProperty("queue.upload.file.temporary.format"), file.getName(), new AlphanumericRandomStringService().random()), file.getType());
            if (feature.isSupported(file, renamed)) {
                if (log.isDebugEnabled()) {
                    log.debug(String.format("Set temporary filename %s", renamed));
                }
                status.temporary(renamed, file);
            }
        }
        status.withMime(new MappingMimeTypeService().getMime(file.getName()));
    }
    if (file.isDirectory()) {
        status.setLength(0L);
    }
    if (options.permissions) {
        final UnixPermission feature = session.getFeature(UnixPermission.class);
        if (feature != null) {
            if (status.isExists()) {
                // Already set when reading attributes of file
                status.setPermission(status.getRemote().getPermission());
            } else {
                status.setPermission(feature.getDefault(local));
            }
        } else {
            // Setting target UNIX permissions in transfer status
            status.setPermission(Permission.EMPTY);
        }
    }
    if (options.acl) {
        final AclPermission feature = session.getFeature(AclPermission.class);
        if (feature != null) {
            if (status.isExists()) {
                progress.message(MessageFormat.format(LocaleFactory.localizedString("Getting permission of {0}", "Status"), file.getName()));
                try {
                    status.setAcl(feature.getPermission(file));
                } catch (NotfoundException | AccessDeniedException | InteroperabilityException e) {
                    status.setAcl(feature.getDefault(file, local));
                }
            } else {
                status.setAcl(feature.getDefault(file, local));
            }
        } else {
            // Setting target ACL in transfer status
            status.setAcl(Acl.EMPTY);
        }
    }
    if (options.timestamp) {
        final Timestamp feature = session.getFeature(Timestamp.class);
        if (feature != null) {
            // Read timestamps from local file
            status.setTimestamp(feature.getDefault(local));
        } else {
            if (1L != local.attributes().getModificationDate()) {
                status.setTimestamp(local.attributes().getModificationDate());
            }
        }
    }
    if (options.metadata) {
        final Headers feature = session.getFeature(Headers.class);
        if (feature != null) {
            if (status.isExists()) {
                progress.message(MessageFormat.format(LocaleFactory.localizedString("Reading metadata of {0}", "Status"), file.getName()));
                try {
                    status.setMetadata(feature.getMetadata(file));
                } catch (NotfoundException | AccessDeniedException | InteroperabilityException e) {
                    status.setMetadata(feature.getDefault(local));
                }
            } else {
                status.setMetadata(feature.getDefault(local));
            }
        }
    }
    if (options.encryption) {
        final Encryption feature = session.getFeature(Encryption.class);
        if (feature != null) {
            if (status.isExists()) {
                progress.message(MessageFormat.format(LocaleFactory.localizedString("Reading metadata of {0}", "Status"), file.getName()));
                try {
                    status.setEncryption(feature.getEncryption(file));
                } catch (NotfoundException | AccessDeniedException | InteroperabilityException e) {
                    status.setEncryption(feature.getDefault(file));
                }
            } else {
                status.setEncryption(feature.getDefault(file));
            }
        }
    }
    if (options.redundancy) {
        if (file.isFile()) {
            final Redundancy feature = session.getFeature(Redundancy.class);
            if (feature != null) {
                if (status.isExists()) {
                    progress.message(MessageFormat.format(LocaleFactory.localizedString("Reading metadata of {0}", "Status"), file.getName()));
                    try {
                        status.setStorageClass(feature.getClass(file));
                    } catch (NotfoundException | AccessDeniedException | InteroperabilityException e) {
                        status.setStorageClass(feature.getDefault());
                    }
                } else {
                    status.setStorageClass(feature.getDefault());
                }
            }
        }
    }
    if (options.checksum) {
        if (file.isFile()) {
            final ChecksumCompute feature = session.getFeature(Write.class).checksum(file, status);
            if (feature != null) {
                progress.message(MessageFormat.format(LocaleFactory.localizedString("Calculate checksum for {0}", "Status"), file.getName()));
                try {
                    status.setChecksum(feature.compute(local.getInputStream(), status));
                } catch (LocalAccessDeniedException e) {
                    // Ignore failure reading file when in sandbox when we miss a security scoped access bookmark.
                    // Lock for files is obtained only later in Transfer#pre
                    log.warn(e.getMessage());
                }
            }
        }
    }
    return status;
}
Also used : Path(ch.cyberduck.core.Path) Write(ch.cyberduck.core.features.Write) AccessDeniedException(ch.cyberduck.core.exception.AccessDeniedException) LocalAccessDeniedException(ch.cyberduck.core.exception.LocalAccessDeniedException) LocalNotfoundException(ch.cyberduck.core.exception.LocalNotfoundException) NotfoundException(ch.cyberduck.core.exception.NotfoundException) InteroperabilityException(ch.cyberduck.core.exception.InteroperabilityException) Redundancy(ch.cyberduck.core.features.Redundancy) Headers(ch.cyberduck.core.features.Headers) PathAttributes(ch.cyberduck.core.PathAttributes) Local(ch.cyberduck.core.Local) Encryption(ch.cyberduck.core.features.Encryption) Timestamp(ch.cyberduck.core.features.Timestamp) UnixPermission(ch.cyberduck.core.features.UnixPermission) ChecksumCompute(ch.cyberduck.core.io.ChecksumCompute) AclPermission(ch.cyberduck.core.features.AclPermission) Move(ch.cyberduck.core.features.Move) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) AlphanumericRandomStringService(ch.cyberduck.core.AlphanumericRandomStringService) MappingMimeTypeService(ch.cyberduck.core.MappingMimeTypeService) LocalAccessDeniedException(ch.cyberduck.core.exception.LocalAccessDeniedException)

Example 2 with Encryption

use of ch.cyberduck.core.features.Encryption in project cyberduck by iterate-ch.

the class ListEncryptionKeysWorker method run.

@Override
public Set<Encryption.Algorithm> run(final Session<?> session) throws BackgroundException {
    final Encryption feature = session.getFeature(Encryption.class);
    if (log.isDebugEnabled()) {
        log.debug(String.format("Run with feature %s", feature));
    }
    final PathContainerService container = session.getFeature(PathContainerService.class);
    final Set<Encryption.Algorithm> keys = new HashSet<>();
    for (Path file : this.getContainers(container, files)) {
        if (this.isCanceled()) {
            throw new ConnectionCanceledException();
        }
        keys.addAll(feature.getKeys(file, prompt));
    }
    return keys;
}
Also used : Path(ch.cyberduck.core.Path) PathContainerService(ch.cyberduck.core.PathContainerService) ConnectionCanceledException(ch.cyberduck.core.exception.ConnectionCanceledException) Encryption(ch.cyberduck.core.features.Encryption) HashSet(java.util.HashSet)

Example 3 with Encryption

use of ch.cyberduck.core.features.Encryption in project cyberduck by iterate-ch.

the class InfoController method encryptionPopupClicked.

@Action
public void encryptionPopupClicked(final NSPopUpButton sender) {
    final String algorithm = sender.selectedItem().representedObject();
    if (null != algorithm && this.toggleS3Settings(false)) {
        final Encryption.Algorithm encryption = Encryption.Algorithm.fromString(algorithm);
        this.background(new WorkerBackgroundAction<>(controller, session, new WriteEncryptionWorker(files, encryption, new PromptRecursiveCallback<>(this), controller) {

            @Override
            public void cleanup(final Boolean v) {
                toggleS3Settings(true);
                initS3();
            }
        }));
    }
}
Also used : NSMutableAttributedString(ch.cyberduck.binding.foundation.NSMutableAttributedString) NSString(ch.cyberduck.binding.foundation.NSString) NSAttributedString(ch.cyberduck.binding.foundation.NSAttributedString) Encryption(ch.cyberduck.core.features.Encryption) Action(ch.cyberduck.binding.Action) RegistryBackgroundAction(ch.cyberduck.core.threading.RegistryBackgroundAction) WindowMainAction(ch.cyberduck.core.threading.WindowMainAction) WorkerBackgroundAction(ch.cyberduck.core.threading.WorkerBackgroundAction)

Example 4 with Encryption

use of ch.cyberduck.core.features.Encryption in project cyberduck by iterate-ch.

the class S3VersioningFeature method revert.

/**
 * Versioning support. Copy a previous version of the object into the same bucket.
 * The copied object becomes the latest version of that object and all object versions are preserved.
 */
@Override
public void revert(final Path file) throws BackgroundException {
    if (file.isFile()) {
        try {
            final S3Object destination = new S3Object(containerService.getKey(file));
            // Keep same storage class
            destination.setStorageClass(file.attributes().getStorageClass());
            final Encryption.Algorithm encryption = file.attributes().getEncryption();
            destination.setServerSideEncryptionAlgorithm(encryption.algorithm);
            // Set custom key id stored in KMS
            destination.setServerSideEncryptionKmsKeyId(encryption.key);
            try {
                // Apply non standard ACL
                destination.setAcl(accessControlListFeature.toAcl(accessControlListFeature.getPermission(file)));
            } catch (AccessDeniedException | InteroperabilityException e) {
                log.warn(String.format("Ignore failure %s", e));
            }
            session.getClient().copyVersionedObject(file.attributes().getVersionId(), containerService.getContainer(file).getName(), containerService.getKey(file), containerService.getContainer(file).getName(), destination, false);
            if (file.getParent().attributes().getCustom().containsKey(S3VersionedObjectListService.KEY_DELETE_MARKER)) {
                // revert placeholder
                session.getClient().deleteVersionedObject(file.getParent().attributes().getVersionId(), containerService.getContainer(file).getName(), containerService.getKey(file.getParent()));
            }
        } catch (ServiceException e) {
            throw new S3ExceptionMappingService().map("Cannot revert file", e, file);
        }
    }
}
Also used : AccessDeniedException(ch.cyberduck.core.exception.AccessDeniedException) InteroperabilityException(ch.cyberduck.core.exception.InteroperabilityException) ServiceException(org.jets3t.service.ServiceException) S3Object(org.jets3t.service.model.S3Object) Encryption(ch.cyberduck.core.features.Encryption)

Example 5 with Encryption

use of ch.cyberduck.core.features.Encryption in project cyberduck by iterate-ch.

the class S3WriteFeature method getDetails.

/**
 * Add default metadata
 */
protected S3Object getDetails(final Path file, final TransferStatus status) throws BackgroundException {
    final S3Object object = new S3Object(containerService.getKey(file));
    final String mime = status.getMime();
    if (StringUtils.isNotBlank(mime)) {
        object.setContentType(mime);
    }
    final Checksum checksum = status.getChecksum();
    if (Checksum.NONE != checksum) {
        switch(checksum.algorithm) {
            case md5:
                object.setMd5Hash(ServiceUtils.fromHex(checksum.hash));
                break;
            case sha256:
                object.addMetadata("x-amz-content-sha256", checksum.hash);
                break;
        }
    }
    if (StringUtils.isNotBlank(status.getStorageClass())) {
        object.setStorageClass(status.getStorageClass());
    }
    final Encryption.Algorithm encryption = status.getEncryption();
    object.setServerSideEncryptionAlgorithm(encryption.algorithm);
    // If the x-amz-server-side-encryption is present and has the value of aws:kms, this header specifies the ID of the
    // AWS Key Management Service (KMS) master encryption key that was used for the object.
    object.setServerSideEncryptionKmsKeyId(encryption.key);
    for (Map.Entry<String, String> m : status.getMetadata().entrySet()) {
        object.addMetadata(m.getKey(), m.getValue());
    }
    if (!Acl.EMPTY.equals(status.getAcl())) {
        if (status.getAcl().isCanned()) {
            if (log.isDebugEnabled()) {
                log.debug(String.format("Set canned ACL %s for %s", status.getAcl(), file));
            }
            object.setAcl(new S3AccessControlListFeature(session).toAcl(status.getAcl()));
            // Reset in status to skip setting ACL in upload filter already applied as canned ACL
            status.setAcl(Acl.EMPTY);
        }
    }
    if (status.getTimestamp() != null) {
        // Interoperable with rsync
        object.addMetadata(S3TimestampFeature.METADATA_MODIFICATION_DATE, String.valueOf(status.getTimestamp()));
    }
    if (status.getLength() != TransferStatus.UNKNOWN_LENGTH) {
        object.setContentLength(status.getLength());
    }
    return object;
}
Also used : Checksum(ch.cyberduck.core.io.Checksum) S3Object(org.jets3t.service.model.S3Object) Encryption(ch.cyberduck.core.features.Encryption) Map(java.util.Map)

Aggregations

Encryption (ch.cyberduck.core.features.Encryption)12 Path (ch.cyberduck.core.Path)5 InteroperabilityException (ch.cyberduck.core.exception.InteroperabilityException)5 Redundancy (ch.cyberduck.core.features.Redundancy)5 AccessDeniedException (ch.cyberduck.core.exception.AccessDeniedException)4 AclPermission (ch.cyberduck.core.features.AclPermission)4 TransferStatus (ch.cyberduck.core.transfer.TransferStatus)4 PathAttributes (ch.cyberduck.core.PathAttributes)3 NotfoundException (ch.cyberduck.core.exception.NotfoundException)3 UnixPermission (ch.cyberduck.core.features.UnixPermission)3 S3Object (org.jets3t.service.model.S3Object)3 NSAttributedString (ch.cyberduck.binding.foundation.NSAttributedString)2 NSMutableAttributedString (ch.cyberduck.binding.foundation.NSMutableAttributedString)2 NSString (ch.cyberduck.binding.foundation.NSString)2 MappingMimeTypeService (ch.cyberduck.core.MappingMimeTypeService)2 BackgroundException (ch.cyberduck.core.exception.BackgroundException)2 Headers (ch.cyberduck.core.features.Headers)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 StorageObject (org.jets3t.service.model.StorageObject)2