SCP
Since Camel 2.10
Only producer is supported
The Camel Jsch component supports the SCP protocol using the Client API of the Jsch project. Jsch is already used in camel by the FTP component for the sftp: protocol.
Maven users will need to add the following dependency to their pom.xml for this component:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jsch</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency> URI format
scp://host[:port]/destination[?options]
The file name can be specified either in the <path> part of the URI or as a "CamelFileName" header on the message (Exchange.FILE_NAME if used in code).
Configuring Options
Camel components are configured on two separate levels:
-
component level
-
endpoint level
Configuring Component Options
At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.
For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.
Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.
You can configure components using:
-
the Component DSL.
-
in a configuration file (
application.properties,*.yamlfiles, etc). -
directly in the Java code.
Configuring Endpoint Options
You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.
Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.
A good practice when configuring options is to use Property Placeholders.
Property placeholders provide a few benefits:
-
They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.
-
They allow externalizing the configuration from the code.
-
They help the code to become more flexible and reusable.
The following two sections list all the options, firstly for the component followed by the endpoint.
Component Options
The SCP component supports the following options which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. | true | boolean | |
Whether to log the INFO messages of JSch at INFO level. JSch logs much at INFO level, so by default the component logs these messages at DEBUG level. The component passes the JSch log messages, with the [JSCH] prefix, to its own logger. | false | boolean | |
Used for enabling or disabling all consumer based health checks from this component. | true | boolean | |
Used for enabling or disabling all producer based health checks from this component. Notice: Camel has by default disabled all producer based health-checks. You can turn on producer checks globally by setting camel.health.producersEnabled=true. | true | boolean |
Endpoint Options
The SCP endpoint is configured using URI syntax:
scp:host:port/directoryName
With the following path and query parameters:
Query Parameters
| Name | Description | Default | Type |
|---|---|---|---|
Whether to disconnect from the remote server right after use: the consumer disconnects after each poll, and the producer after each message it writes. This only closes the current connection: to stop a consumer, stop the consumer or the route instead. | false | boolean | |
The algorithm of a checksum to calculate from the content of the written file. The checksum is set in the CamelFileChecksum header (and written to a checksum file, see checksumWriteFile). The name is passed to java.security.MessageDigest, which accepts names such as MD5, SHA-1, SHA-256 or SHA3-256. Enum values:
| String | ||
The permissions of the file that the producer writes, as three octal digits, for example 644. Any other value is replaced by 664. The producer sends it in the scp protocol line of the file, and the remote scp uses it when it creates the file. The directories that the producer creates for a file name with a path get 775. | 664 | String | |
The file name, as an expression such as the File Language. Consumer: only the file with exactly this name is consumed, as the name is not a pattern (use antInclude or include to consume the files that match a pattern); for example consume the file of today with mydata-$\{date:now:yyyyMMdd}.txt. Producer: the name of the file to write; when set, it is used instead of the CamelFileName header, unless that header holds an org.apache.camel.Expression (a String header is used as it is, not evaluated). A String value of this option is evaluated with the File Language, and an Expression object as it is. The producer also supports the CamelOverruleFileName header, which takes precedence over the CamelFileName header and is used only once, so CamelFileName does not have to be stored and restored around the producer. | String | ||
Used for jailing (restricting) writing files to the starting directory (and sub) only. This is enabled by default to not allow Camel to write files to outside directories (to be more secured out of the box). You can turn this off to allow writing files to directories outside the starting directory, such as parent or root folders. For consumers that use a localWorkDirectory, this also restricts the downloaded files to stay within the configured localWorkDirectory. | true | boolean | |
Whether to also write the checksum to a file, when checksumFileAlgorithm is set. The checksum file is written in the same directory as the file, with the algorithm as extension (for example hello.txt.MD5 with checksumFileAlgorithm=MD5). | true | boolean | |
Whether to strip the leading paths from the file name, so only the name of the file is left. The consumer then sets the CamelFileName header to the name only, and the producer strips the paths from the name it writes (from the fileName option or the CamelFileName header). For example, files consumed recursively from subdirectories are then all written in a single directory. | false | boolean | |
Whether a message with a null body can be written. If true, an empty file is written: an existing file is truncated with fileExist=Override, and left unchanged with fileExist=Append. If false, a null body fails with a GenericFileOperationFailedException. | false | boolean | |
Whether the producer disconnects from the remote server after the last message of a batch, which is a message whose exchange property CamelBatchComplete is true, such as the last file of a poll of a file consumer. This only closes the current connection. | false | boolean | |
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
A custom FileMoveExistingStrategy that moves the existing file when fileExist=Move is used. By default, the component uses its own implementation, which moves the file to the name computed by moveExisting. | FileMoveExistingStrategy | ||
Maximum number of messages to keep in memory available for browsing. Use 0 for unlimited. | 100 | int | |
The timeout in milliseconds for establishing the connection to the server. For sftp, mina-sftp and scp, it also applies to the SSH handshake and the login. 0 means no timeout. | 10000 | int | |
The socket timeout (SO_TIMEOUT) in milliseconds, so that a connection whose server no longer answers does not hang. For ftp and ftps, it is the read timeout of the control connection: a command whose reply does not come within this time fails. For sftp, it is the timeout of the JSch session: when the server sends nothing for this time, JSch sends a keep-alive message, and closes the connection after serverAliveCountMax such messages; it is not used when serverAliveInterval is set. The mina-sftp and scp components do not use it. 0 means no timeout. | 300000 | int | |
The data timeout in milliseconds. For ftp and ftps, it is the read timeout of the data connection (the data timeout of the Apache Commons Net FTPClient), and 0 means no timeout. For scp, it is the timeout of the JSch session. The sftp and mina-sftp components do not use it. | 30000 | int | |
A comma-separated list of the signature algorithms that are accepted for the CA signature of an OpenSSH host certificate, for example ssh-ed25519,rsa-sha2-512. The CA keys are the cert-authority entries of the known hosts file. If not set, the JSch default list is used, which does not accept the SHA-1 based ssh-rsa. | String | ||
The OpenSSH user certificate as a byte array, for certificate-based authentication. Only used together with privateKeyFile or privateKeyBytes, and when neither certFile nor certUri is set. | byte[] | ||
The OpenSSH user certificate file for certificate-based authentication, for example id_ed25519-cert.pub. A path on the file system, or with the classpath: prefix a resource on the classpath. Only used together with privateKeyFile or privateKeyBytes, the private key of the certificate. Takes precedence over certUri and certBytes. | String | ||
Deprecated The OpenSSH user certificate for certificate-based authentication, loaded the same way as certFile: a path on the file system, or with the classpath: prefix a resource on the classpath. Only used together with privateKeyFile or privateKeyBytes, and when certFile is not set. Takes precedence over certBytes. Deprecation note: Use certFile, which loads the certificate in the same way and takes precedence. | String | ||
The known hosts file that JSch checks the key of the remote host against when the producer connects, in the OpenSSH known_hosts format. A path on the file system, or with the classpath: prefix a resource on the classpath. An cert-authority entry lets the producer accept an OpenSSH host certificate signed by that CA. What happens with a host that is not in the file, or whose key changed, depends on strictHostKeyChecking. | String | ||
Password to use for login. | String | ||
A comma-separated list of the SSH authentication methods to try, in order of preference, for example publickey,password. JSch supports gssapi-with-mic, publickey, keyboard-interactive and password. If not set, the JSch default gssapi-with-mic,publickey,keyboard-interactive,password is used. | String | ||
The private key as a byte array, for public key authentication. Only used when privateKeyFile is not set. Use privateKeyFilePassphrase for an encrypted key. | byte[] | ||
The private key file for public key authentication. A path on the file system, or with the classpath: prefix a resource on the classpath. The key must be in a format that JSch reads, such as OpenSSH, PEM or PuTTY. Takes precedence over privateKeyBytes. Use privateKeyFilePassphrase for an encrypted key. | String | ||
The passphrase of an encrypted private key of privateKeyFile or privateKeyBytes. Not needed for a key that is not encrypted. | String | ||
Sets whether to use strict host key checking. Setting this to 'no' (the default) disables host key verification and makes the connection vulnerable to man-in-the-middle attacks. Use 'yes' in production environments. Enum values:
| no | String | |
The user name to log in with. For ftp and ftps, the login is anonymous when it is not set. | String | ||
Whether to use the known hosts file of the user, the .ssh/known_hosts file in the home directory (the user.home system property, or the HOME environment variable), when knownHostsFile is not set. If that file does not exist, or if false and knownHostsFile is not set, no host is known, and strictHostKeyChecking decides whether the producer connects. | true | boolean | |
A comma-separated list of the ciphers for the SSH connection, in order of preference, for example aes128-ctr,aes256-ctr. The cipher names are those of JSch. If not set, the JSch default list is used. | String |
Message Headers
The SCP component supports the following message header(s), which is/are listed below:
| Name | Description | Default | Type |
|---|---|---|---|
| Constant: | The name of the file to write, relative to the directory of the endpoint, as a String or an Expression. The fileName option is used instead when it is set, unless the header is an Expression. Without either, the producer uses a name generated from the message ID. | String | |
CamelOverruleFileName (producer) Constant: | A name that the producer uses instead of the CamelFileName header, as a String or an Expression, only once: the producer removes this header after writing the file. The fileName option still takes precedence over a String. | Object | |
CamelFileNameProduced (producer) Constant: | The path and name of the file that the producer wrote: the directory of the endpoint followed by the file name. | String | |
| Constant: | The checksum of the file that the producer wrote, as a hexadecimal String, computed with the algorithm of the checksumFileAlgorithm option. Only set when that option is set. | String |
Message Body
The message body at each side of the SCP component: Consumer is the body of the messages the consumer creates, Consumer reply the bodies a consumer that answers its caller can reply with, Producer input the bodies the producer can send or write, and Producer output the body after the producer.
| Side | Body | Description |
|---|---|---|
Producer input |
| The content of the file: text, bytes, a stream or a file, which is read into memory first to send its size. A Map or a List fails: marshal it first.
|
Producer output |
| Unchanged, with the name of the remote file in the CamelFileNameProduced header. |
The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers, the sides and the tools that use them are described in Message Body Types.
Usage
The SCP component copies the message body to a file on the remote host. In the following route, the file consumer sets the CamelFileName header, and each file of a local folder is copied to the /upload directory of the host. The host key is checked against a known hosts file:
- route:
from:
uri: file:outbox
steps:
- to:
uri: scp://myhost:22/upload
parameters:
username: "{{scp.username}}"
password: "{{scp.password}}"
knownHostsFile: /home/camel/.ssh/known_hosts
strictHostKeyChecking: "yes" The directory of the endpoint (/upload above) must exist on the remote host, as scp does not create it: when it does not exist, each file is written to a file named like the directory, and the last one wins. The sub directories in the CamelFileName header (such as mysub/hello.txt) are created in that directory.
scp sends the name of each sub directory and of the file on a protocol line that ends with a line feed, so a file name with a line feed or a NUL character is refused with a GenericFileOperationFailedException before anything is sent to the remote host. Any other character, such as a carriage return or a tab, is sent as is.
Without a known hosts file (useUserKnownHostsFile=false and no knownHostsFile, or the default useUserKnownHostsFile=true when .ssh/known_hosts does not exist in the home directory), no host is known, so the option strictHostKeyChecking decides: with yes the connection fails, with no (the default) the host key is not checked.
With the password option the producer can log in with the password and the keyboard-interactive authentication methods. With keyboard-interactive, it answers with the password when the server asks one question that is not echoed and contains password: (in any case), as a PAM setup does, and cancels any other question.
OpenSSH Certificate Authentication
Camel supports OpenSSH certificate-based authentication for SCP connections, where a user’s public key is signed by a Certificate Authority (CA). This is configured using the certFile or certBytes parameters (or the deprecated certUri) alongside the private key:
-
Java
-
YAML
to("scp://host/path?username=myuser"
+ "&privateKeyFile=/path/to/user_key"
+ "&certFile=/path/to/user_key-cert.pub"); - route:
from:
uri: file:outbox
steps:
- to:
uri: scp://host/path
parameters:
username: myuser
privateKeyFile: /path/to/user_key
certFile: /path/to/user_key-cert.pub See the SFTP component documentation for more details on certificate authentication options.
Limitations
Currently, camel-jsch only supports a Producer (i.e., copy files to another host).