-
Notifications
You must be signed in to change notification settings - Fork 169
SFTP Integration #1087
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
SFTP Integration #1087
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b600437
Added integration, example and test SFTP projects
fabio-marini 8c208ba
Fixed issues raised by copilot PR review (hosting)
fabio-marini 53cf3c2
Replaced custom health check with one in package AspNetCore.HealthChe…
fabio-marini fb7922a
Refactored to use strongly-typed connection infos
fabio-marini 2cfa4eb
Merge branch 'CommunityToolkit:main' into sftp
fabio-marini 9c0f808
Various API and test changes following Aaron's review
fabio-marini aa45b17
Updated test list in github actions workflow
fabio-marini 001e5f5
Merge branch 'main' into sftp
aaronpowell 4a7f802
stuffed up the merge
aaronpowell 8f20b94
Increased retry count and interval for all hosting tests
fabio-marini File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...kit.Aspire.Hosting.Sftp.ApiService/CommunityToolkit.Aspire.Hosting.Sftp.ApiService.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
|
||
| <PropertyGroup> | ||
| <Nullable>enable</Nullable> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <None Include="..\CommunityToolkit.Aspire.Hosting.Sftp.AppHost\home\foo\.ssh\keys\id_ed25519" Link="id_ed25519"> | ||
| <CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
| </None> | ||
| <None Include="..\CommunityToolkit.Aspire.Hosting.Sftp.AppHost\home\foo\.ssh\keys\id_ed25519.pub" Link="id_ed25519.pub" /> | ||
| <None Include="..\CommunityToolkit.Aspire.Hosting.Sftp.AppHost\home\foo\.ssh\keys\id_rsa" Link="id_rsa"> | ||
| <CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
| </None> | ||
| <None Include="..\CommunityToolkit.Aspire.Hosting.Sftp.AppHost\home\foo\.ssh\keys\id_rsa.pub" Link="id_rsa.pub" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.AspNetCore.OpenApi" /> | ||
| <PackageReference Include="Swashbuckle.AspNetCore" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\..\src\CommunityToolkit.Aspire.Sftp\CommunityToolkit.Aspire.Sftp.csproj" /> | ||
| <ProjectReference Include="..\CommunityToolkit.Aspire.Hosting.Sftp.ServiceDefaults\CommunityToolkit.Aspire.Hosting.Sftp.ServiceDefaults.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
82 changes: 82 additions & 0 deletions
82
examples/sftp/CommunityToolkit.Aspire.Hosting.Sftp.ApiService/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| using Renci.SshNet; | ||
| using System.Text; | ||
|
|
||
| var builder = WebApplication.CreateBuilder(args); | ||
|
|
||
| builder.AddServiceDefaults(); | ||
|
|
||
| builder.AddSftpClient("sftp", cfg => | ||
| { | ||
| cfg.Username = "foo"; | ||
| cfg.Password = "pass"; | ||
| }); | ||
|
|
||
| // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle | ||
| builder.Services.AddEndpointsApiExplorer(); | ||
| builder.Services.AddSwaggerGen(); | ||
|
|
||
| var app = builder.Build(); | ||
|
|
||
| app.UseHttpsRedirection(); | ||
|
|
||
| app.MapDefaultEndpoints(); | ||
|
|
||
| if (app.Environment.IsDevelopment()) | ||
| { | ||
| app.UseSwagger(); | ||
| app.UseSwaggerUI(); | ||
| } | ||
|
|
||
| const string fileName = "uploads/hello.txt"; | ||
|
|
||
| app.MapPost("/upload", async (SftpClient client, CancellationToken cancellationToken) => | ||
| { | ||
| try | ||
| { | ||
| using var tokenSource = new CancellationTokenSource(TimeSpan.FromMinutes(2)); | ||
|
|
||
| await client.ConnectAsync(tokenSource.Token); | ||
|
|
||
| var fileContent = Encoding.UTF8.GetBytes("Hello world!"); | ||
|
|
||
| using var inputStream = new MemoryStream(fileContent); | ||
|
|
||
| await client.UploadFileAsync(inputStream, fileName); | ||
|
|
||
| return Results.File(inputStream.ToArray()); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| return Results.InternalServerError(ex.ToString()); | ||
| } | ||
| finally | ||
| { | ||
| client.Disconnect(); | ||
| } | ||
| }); | ||
|
|
||
| app.MapGet("/download", async (SftpClient client, CancellationToken cancellationToken) => | ||
| { | ||
| try | ||
| { | ||
| using var tokenSource = new CancellationTokenSource(TimeSpan.FromMinutes(2)); | ||
|
|
||
| await client.ConnectAsync(tokenSource.Token); | ||
|
|
||
| using var outputStream = new MemoryStream(); | ||
|
|
||
| await client.DownloadFileAsync(fileName, outputStream); | ||
|
|
||
| return Results.File(outputStream.ToArray()); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| return Results.InternalServerError(ex.ToString()); | ||
| } | ||
| finally | ||
| { | ||
| client.Disconnect(); | ||
| } | ||
| }); | ||
|
|
||
| app.Run(); |
41 changes: 41 additions & 0 deletions
41
examples/sftp/CommunityToolkit.Aspire.Hosting.Sftp.ApiService/Properties/launchSettings.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| { | ||
| "$schema": "http://json.schemastore.org/launchsettings.json", | ||
| "iisSettings": { | ||
| "windowsAuthentication": false, | ||
| "anonymousAuthentication": true, | ||
| "iisExpress": { | ||
| "applicationUrl": "http://localhost:38959", | ||
| "sslPort": 44303 | ||
| } | ||
| }, | ||
| "profiles": { | ||
| "http": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "launchUrl": "swagger", | ||
| "applicationUrl": "http://localhost:5279", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| }, | ||
| "https": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "launchUrl": "swagger", | ||
| "applicationUrl": "https://localhost:7015;http://localhost:5279", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| }, | ||
| "IIS Express": { | ||
| "commandName": "IISExpress", | ||
| "launchBrowser": true, | ||
| "launchUrl": "swagger", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| } | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
examples/sftp/CommunityToolkit.Aspire.Hosting.Sftp.ApiService/appsettings.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| }, | ||
| "AllowedHosts": "*" | ||
| } |
42 changes: 42 additions & 0 deletions
42
...tyToolkit.Aspire.Hosting.Sftp.AppHost/CommunityToolkit.Aspire.Hosting.Sftp.AppHost.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| <Project Sdk="Aspire.AppHost.Sdk/13.0.0"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| <IsAspireHost>true</IsAspireHost> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <None Include="home\foo\.ssh\keys\id_ed25519"> | ||
| <CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
| </None> | ||
| <None Include="home\foo\.ssh\keys\id_ed25519.pub"> | ||
| <CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
| </None> | ||
| <None Include="home\foo\.ssh\keys\id_rsa"> | ||
| <CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
| </None> | ||
| <None Include="home\foo\.ssh\keys\id_rsa.pub"> | ||
| <CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
| </None> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\..\src\CommunityToolkit.Aspire.Hosting.Sftp\CommunityToolkit.Aspire.Hosting.Sftp.csproj" IsAspireProjectResource="false" /> | ||
| <ProjectReference Include="..\CommunityToolkit.Aspire.Hosting.Sftp.ApiService\CommunityToolkit.Aspire.Hosting.Sftp.ApiService.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <None Update="etc\sftp\users.conf"> | ||
| <CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
| </None> | ||
| <None Update="etc\ssh\ssh_host_ed25519_key"> | ||
| <CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
| </None> | ||
| <None Update="etc\ssh\ssh_host_rsa_key"> | ||
| <CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
| </None> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
12 changes: 12 additions & 0 deletions
12
examples/sftp/CommunityToolkit.Aspire.Hosting.Sftp.AppHost/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| using Aspire.Hosting; | ||
| using Projects; | ||
|
|
||
| var builder = DistributedApplication.CreateBuilder(args); | ||
|
|
||
| var sftp = builder.AddSftp("sftp").WithEnvironment("SFTP_USERS", "foo:$5$t9qxNlrcFqVBNnad$U27ZrjbKNjv4JkRWvi6MjX4x6KXNQGr8NTIySOcDgi4:e:::uploads"); | ||
|
|
||
| builder.AddProject<Projects.CommunityToolkit_Aspire_Hosting_Sftp_ApiService>("api") | ||
| .WithReference(sftp) | ||
| .WaitForStart(sftp); | ||
|
|
||
| builder.Build().Run(); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the
SFTP_USERSalways going to be needed when using the hosting integration? If so, is there a way we could wrap it in an API that helps craft the environment variable in the appropriate format to avoid user error?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it's not. There are two other ways to supply users to the server: arguments (
WithArgs()) and config file (WithUsersFile()). But at least one user is required for the server to start.All of the above methods use the same format for specifying users and support both clear-text (
pass) and encrypted ($5$t9qxNlrcFqVBNnad$U27ZrjbKNjv4JkRWvi6MjX4x6KXNQGr8NTIySOcDgi4:e) passwords.There's a quick note in the
READMEabout generating encrypted passwords usingmkpasswd.I've only added a public API for files so they get mounted in the right place, but it's easy enough to add APIs for
WithArgs()andWithEnvironment(), just let me know?