Path: redb-route/redb.Route.Sftp/SftpEndpointOptions.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Sftp/SftpEndpointOptions.cs
----
using redb.Route.Core;
using redb.Route.GenericFile;
namespace redb.Route.Sftp;
///
/// Endpoint options for SFTP transport. All properties are auto-bound from URI query parameters.
///
/// URI format: sftp:///remote/path?host=server&port=22&username=admin&password=secret&...
///
///
/// Inherits shared polling, post-processing, idempotency, and producer options from
/// . Adds SFTP-specific: SSH authentication,
/// proxy tunneling, chmod, flatten, jail, and more.
///
///
public class SftpEndpointOptions : RemoteFileEndpointOptions
{
/// Creates SFTP endpoint options with protocol-specific defaults.
public SftpEndpointOptions()
{
Delay = 60_000;
InitialDelay = 1000;
StartingDirectoryMustExist = true;
}
// ═══════════════════════════════════════════════════════════════════
// CONNECTION: SFTP-specific
// ═══════════════════════════════════════════════════════════════════
/// SFTP server port. (default: 22)
public override int Port { get; set; } = 22;
///
/// Path to a PEM/OpenSSH private key file for public-key authentication.
/// Supports RSA, DSA, ECDSA, ED25519 keys.
///
public string PrivateKeyPath { get; set; } = "";
/// Passphrase for the encrypted private key (if applicable).
[Sensitive]
public string PrivateKeyPassphrase { get; set; } = "";
///
/// Enable keyboard-interactive authentication in addition to password/key.
/// Useful for 2FA or custom PAM prompts. The password is sent as the response. (default: false)
///
public bool UseKeyboardInteractive { get; set; }
///
/// Preferred authentication order. Comma-separated: "publickey,password,keyboard-interactive".
/// Empty = SSH.NET default (try all configured methods).
///
public string PreferredAuthentications { get; set; } = "";
///
/// Expected server host key fingerprint (MD5 hex, colon-separated, e.g. "AA:BB:CC:...").
/// If set and StrictHostKeyChecking is true, the connection is rejected on mismatch.
///
public string ServerFingerprint { get; set; } = "";
///
/// If true, validate the server's host key against .
/// If false (default), all host keys are accepted. (default: false)
///
public bool StrictHostKeyChecking { get; set; }
///
/// Path to a known_hosts file (OpenSSH format) for host key verification.
/// Used only when is true and ServerFingerprint is empty.
///
public string KnownHostsFile { get; set; } = "";
///
/// SSH keep-alive interval in milliseconds. 0 = disabled.
/// Sends SSH_MSG_IGNORE packets to prevent firewall/NAT timeouts. (default: 0)
///
public int KeepAliveInterval { get; set; }
/// SSH.NET internal buffer size in bytes. (default: 32768 = 32 KB)
public int BufferSize { get; set; } = 32_768;
/// Enable zlib compression on the SSH channel. (default: false)
public bool Compression { get; set; }
// ── Proxy ────────────────────────────────────────────────────────
/// Proxy type for tunneling the SSH connection. (default: None)
public SftpProxyType ProxyType { get; set; } = SftpProxyType.None;
/// Proxy hostname or IP.
public string ProxyHost { get; set; } = "";
/// Proxy port. (default: 1080)
public int ProxyPort { get; set; } = 1080;
/// Proxy username (for SOCKS5 or HTTP proxy auth).
public string ProxyUsername { get; set; } = "";
/// Proxy password.
[Sensitive]
public string ProxyPassword { get; set; } = "";
// ═══════════════════════════════════════════════════════════════════
// TRANSFER OPTIONS (SFTP-specific)
// ═══════════════════════════════════════════════════════════════════
// Binary and StepWise are gone (the options sweep): both were declared and read by
// nothing, and neither CAN mean anything here. SFTP has no text mode - the protocol moves raw
// bytes, full stop. And it has no change-directory operation either (SSH_FXP_* requests carry
// paths; a client-side "cd" is bookkeeping), so the FTP notion of step-wise navigation does
// not map - directory creation already walks segment by segment unconditionally.
///
/// Input normalization for remote paths: Auto (default) converts backslashes in configured
/// paths and file names to "/", Unix takes them byte-for-byte. The wire separator is always
/// "/" — that is the SFTP protocol, there is no Windows mode.
///
public SftpSeparator Separator { get; set; } = SftpSeparator.Auto;
///
/// If true, silently ignore FileNotFoundException or permission errors during listing.
/// Useful when the directory may not exist yet. (default: false)
///
public bool IgnoreFileNotFoundOrPermissionError { get; set; }
///
/// If true, each subdirectory must exist when using recursive mode.
/// If false, silently skip missing subdirectories. (default: false)
///
public bool DirectoryMustExist { get; set; }
// ═══════════════════════════════════════════════════════════════════
// PRODUCER: SFTP-specific
// ═══════════════════════════════════════════════════════════════════
///
/// Strategy for moving existing files when FileExist=Move. (default: Timestamp)
///
public SftpMoveExistingStrategy MoveExistingFileStrategy { get; set; } = SftpMoveExistingStrategy.Timestamp;
///
/// Set POSIX file permissions after upload (octal string, e.g. "0644").
/// Empty = do not change permissions. Requires server support.
///
public string Chmod { get; set; } = "";
///
/// Set POSIX permissions on auto-created directories (octal string, e.g. "0755").
/// Empty = server default.
///
public string ChmodDirectory { get; set; } = "";
///
/// If true, preserve the CamelFileLastModified header as the remote file's modification time.
/// Requires SFTP v3+ set-stat support. (default: false)
///
public bool KeepLastModified { get; set; }
///
/// If true, flatten nested directory structures — all files are uploaded to the base directory.
/// File names from subdirectories lose their directory prefix. (default: false)
///
public bool Flatten { get; set; }
///
/// If true, prevent the producer from writing outside the base directory.
/// FileName expressions that navigate with ".." are rejected. (default: true)
///
public bool JailStartingDirectory { get; set; } = true;
// ═══════════════════════════════════════════════════════════════════
// VALIDATION
// ═══════════════════════════════════════════════════════════════════
///
public override void Validate()
{
ValidateRemote();
if (string.IsNullOrWhiteSpace(Username))
throw new ArgumentException("SFTP Username must be specified.");
if (string.IsNullOrEmpty(Password) && string.IsNullOrEmpty(PrivateKeyPath))
throw new ArgumentException("At least one authentication method required: provide Password or PrivateKeyPath.");
if (BufferSize < 1024)
throw new ArgumentOutOfRangeException(nameof(BufferSize), BufferSize,
"BufferSize must be at least 1024 bytes.");
if (ProxyType != SftpProxyType.None && string.IsNullOrWhiteSpace(ProxyHost))
throw new ArgumentException("ProxyHost must be specified when ProxyType is not None.");
if (ProxyPort is < 1 or > 65535)
throw new ArgumentOutOfRangeException(nameof(ProxyPort), ProxyPort, "ProxyPort must be 1–65535.");
if (!string.IsNullOrEmpty(Chmod) && !IsValidOctalPermissions(Chmod))
throw new ArgumentException($"Chmod must be a valid octal permission string (e.g. '0644'), got: '{Chmod}'");
if (!string.IsNullOrEmpty(ChmodDirectory) && !IsValidOctalPermissions(ChmodDirectory))
throw new ArgumentException($"ChmodDirectory must be a valid octal permission string (e.g. '0755'), got: '{ChmodDirectory}'");
}
///
/// Parses an octal permissions string (e.g. "0644") to a short value for SFTP chmod.
///
internal static short ParseOctalPermissions(string octal)
{
return Convert.ToInt16(octal, 8);
}
private static bool IsValidOctalPermissions(string value)
{
if (string.IsNullOrEmpty(value)) return false;
try
{
var v = Convert.ToInt32(value, 8);
return v is >= 0 and <= 0x1FF; // 0000 to 0777
}
catch
{
return false;
}
}
}