An Aspire hosting package that enables easy integration of SFTP servers into your app host.
F23.Aspire.Hosting.Sftp provides extensions to Aspire for hosting and managing a SFTP servers in your development workflow.
It handles Docker image management, volume persistence, user credentials, and SSH host key management.
The Docker image used for the SFTP server is based on the excellent MIT-licensed project atmoz/sftp. It has been modified with the following changes:
- Supports volume persistence for SSH host keys where the keys are generated on first startup if not present
- Fixes permission issues with mounted volumes by ensuring correct ownership and permissions on startup
This project is an open-source project by feature[23].
- Easy SFTP Server Setup - Add SFTP servers to your Aspire app host with a single method call
- Automatic User Management - Create SFTP users with auto-generated secure passwords
- Volume Persistence - Configure persistent volumes for user data and SSH host keys
- Password Management - Seamlessly integrate with Aspire's parameter resources for secure credential handling
- Docker-based - Uses containerized SFTP servers for easy startup and isolation
Install the NuGet package:
dotnet add package F23.Aspire.Hosting.SftpAdd the following to your Aspire AppHost.cs:
var builder = DistributedApplication.CreateBuilder(args);
var sftp = builder.AddSftpServer("sftp-server")
// Persist SSH host keys across restarts:
.WithSshHostKeysVolume()
// Create user with auto-generated password:
.WithSftpUser(username: "foo", directories: ["data"])
// Create persistent volume for user data
.WithSftpVolume(username: "foo", path: "data");
builder.Build().Run();var backupService = builder.AddDockerfile("backup-service")
.WaitFor(sftp)
.WithReference(sftp.GetEndpoint("sftp"))
.WithEnvironment("SFTP_USER", "backup-user")
.WithEnvironment("SFTP_PASSWORD", sftp.GetSftpUserPassword("backup-user"));Adds an SFTP server resource to your application.
Parameters:
name(string) - The logical name of the SFTP resourceport(int?, optional) - The port to expose the SFTP server on. If not specified, a random port is assignedvolumeNamePrefix(string?, optional) - Prefix for generated Docker volume names
Returns: IResourceBuilder<SftpServerResource> for further configuration
Example:
var sftp = builder.AddSftpServer("sftp-server", port: 2222);Configures SSH host key persistence across container restarts. It is strongly recommended to use this, so that you do not get SSH host key warnings when restarting the app host.
Parameters:
volumeName(string?, optional) - Custom Docker volume name. If not specified, one is auto-generated
Example:
sftp.WithSshHostKeysVolume();Creates an SFTP user account.
Parameters:
username(string) - The username for the SFTP accountpassword(IResourceBuilder?, optional) - Custom password parameter. If not specified, a secure password is auto-generateddirectories(IReadOnlyList?, optional) - List of directories the user has access to in the user's home directory
Example:
sftp.WithSftpUser("foo", directories: ["data", "uploads"]);Configures a persistent volume for a user's directory.
Parameters:
username(string) - The SFTP usernamepath(string) - The path within the user's home directory (e.g., "data", "uploads")volumeName(string?, optional) - Custom Docker volume name. If not specified, one is auto-generatedisReadOnly(bool, default: false) - Whether the Docker volume should be mounted as read-only
Example:
sftp.WithSftpVolume("foo", "data");
sftp.WithSftpVolume("admin", "backups", isReadOnly: true);Retrieves the password parameter for a specific SFTP user. Useful for passing credentials to other services.
Parameters:
username(string) - The username to retrieve the password for
Returns: ParameterResource containing the password
Example:
var fooPassword = sftp.GetSftpUserPassword("foo");var customPassword = builder.AddParameter("my-sftp-password", secret: true);
var sftp = builder.AddSftpServer("sftp-server")
.WithSftpUser("admin", password: customPassword);MIT - See LICENSE file for details