1
0
mirror of https://github.com/bitwarden/server synced 2026-02-24 08:33:06 +00:00

feat: Add Aspire Apphost and ServiceDefault projects

This commit is contained in:
Stephon Brown
2025-12-10 17:01:07 -06:00
parent fafc61d7b9
commit f570887999
11 changed files with 526 additions and 0 deletions

59
AppHost/AppHost.cs Normal file
View File

@@ -0,0 +1,59 @@
using Bit.AppHost;
var builder = DistributedApplication.CreateBuilder(args);
var secretsSetup = builder.ConfigureSecrets();
var isSelfHosted = builder.Configuration["globalSettings:selfHosted"]?.ToLowerInvariant() == "true";
// Add Pricing Service - use port from pricingUri in secrets
var pricingService =
builder
.AddProject("pricing-service",
builder.Configuration["pricingServiceRelativePath"]
?? throw new ArgumentNullException("pricingServiceRelativePath", "Missing pricing service relative path"));
// Add Database and run migrations
var db = builder.AddSqlServerDatabaseResource(isSelfHosted);
builder.ConfigureMigrations(isSelfHosted)
.WaitFor(db)
.ExcludeFromManifest()
.WaitForCompletion(secretsSetup);
var azurite = builder.ConfigureAzurite();
// Add MailCatcher
var mail = builder
.AddContainer("mailcatcher", "sj26/mailcatcher:latest")
.WithLifetime(ContainerLifetime.Persistent)
.WithEndpoint(port: 10250, name: "smtp", targetPort: 1025) // SMTP port
.WithHttpEndpoint(port: 1080, name: "web", targetPort: 1080);
// Add Services
builder.AddBitwardenService<Projects.Admin>(db, secretsSetup, mail, "admin");
var api = builder.AddBitwardenService<Projects.Api>(db, secretsSetup, mail, "api")
.WithReference(pricingService)
.WaitFor(azurite);
var billing = builder.AddBitwardenService<Projects.Billing>(db, secretsSetup, mail, "billing");
builder.AddBitwardenService<Projects.Identity>(db, secretsSetup, mail, "identity");
builder.AddBitwardenService<Projects.Notifications>(db, secretsSetup, mail, "notifications")
.WaitFor(azurite);
// Add Client Apps
builder.AddBitwardenNpmApp("web-frontend", "web", api)
.WithHttpsEndpoint(8080, 8080, "angular-http", isProxied: false)
.WithUrl("https://bitwarden.test:8080")
.WithExternalHttpEndpoints();
builder.AddBitwardenNpmApp("desktop-frontend", "desktop", api, "start");
builder.AddBitwardenNpmApp("browser-frontend", "browser", api, "build:bit:watch:chrome");
// Add Ngrok
builder.ConfigureNgrok((billing, "billing-http"));
builder.Build().Run();

29
AppHost/AppHost.csproj Normal file
View File

@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">
<Sdk Name="Aspire.AppHost.Sdk" Version="9.3.1" />
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UserSecretsId>e0dba0c6-d131-43bd-9143-2260f11a14ad</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Aspire.Hosting.AppHost" Version="9.3.1" />
<PackageReference Include="Aspire.Hosting.Azure.Storage" Version="9.3.1" />
<PackageReference Include="Aspire.Hosting.NodeJs" Version="9.3.1" />
<PackageReference Include="Aspire.Hosting.SqlServer" Version="9.3.2" />
<PackageReference Include="CommunityToolkit.Aspire.Hosting.Ngrok" Version="9.3.0" />
<PackageReference Include="CommunityToolkit.Aspire.Hosting.NodeJS.Extensions" Version="9.3.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\src\Admin\Admin.csproj" />
<ProjectReference Include="..\src\Api\Api.csproj" />
<ProjectReference Include="..\src\Billing\Billing.csproj" />
<ProjectReference Include="..\src\Identity\Identity.csproj" />
<ProjectReference Include="..\src\Notifications\Notifications.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,156 @@
using Aspire.Hosting.Azure;
using Azure.Provisioning;
using Azure.Provisioning.Storage;
namespace Bit.AppHost;
public static class BuilderExtensions
{
public static IResourceBuilder<ExecutableResource> ConfigureSecrets(this IDistributedApplicationBuilder builder)
{
// Setup secrets before starting services
var secretsScript = builder.Configuration["scripts:secretsSetup"] ?? throw new ArgumentNullException("setupSecretsScriptPath", "Missing setup secrets script path");
var pricingSecretsPath = builder.Configuration["pricingServiceSecretsPath"] ?? throw new ArgumentNullException("pricingServiceSecretsPath", "Missing secrets path");
//Pricing Secrets
builder
.AddExecutable("pricing-setup-secrets", "pwsh", pricingSecretsPath, "-File", secretsScript, "-clear")
.ExcludeFromManifest();
return builder
.AddExecutable("setup-secrets", "pwsh", "../dev", "-File", secretsScript, "-clear")
.ExcludeFromManifest();
}
public static IResourceBuilder<SqlServerDatabaseResource> AddSqlServerDatabaseResource(this IDistributedApplicationBuilder builder, bool isSelfHosted = false)
{
var password = isSelfHosted
? builder.Configuration["dev:selfHostOverride:globalSettings:sqlServer:password"]
: builder.Configuration["globalSettings:sqlServer:password"];
// Add MSSQL - retrieve password from connection string in secrets
var dbpassword = builder.AddParameter("dbPassword", password!, secret: true);
return builder
.AddSqlServer("mssql", password: dbpassword, 1433)
.WithImage("mssql/server:2022-latest")
.WithLifetime(ContainerLifetime.Persistent)
.WithDataVolume()
.AddDatabase("vault", isSelfHosted ? "self_host_dev" : "vault_dev");
}
public static IResourceBuilder<AzureStorageResource> ConfigureAzurite(this IDistributedApplicationBuilder builder)
{
// https://github.com/dotnet/aspire/discussions/5552
var azurite = builder
.AddAzureStorage("azurite").ConfigureInfrastructure(c =>
{
var blobStorage = c.GetProvisionableResources().OfType<BlobService>().Single();
blobStorage.CorsRules.Add(new BicepValue<StorageCorsRule>(new StorageCorsRule
{
AllowedOrigins = [new BicepValue<string>("*")],
AllowedMethods = [CorsRuleAllowedMethod.Get, CorsRuleAllowedMethod.Put],
AllowedHeaders = [new BicepValue<string>("*")],
ExposedHeaders = [new BicepValue<string>("*")],
MaxAgeInSeconds = new BicepValue<int>("30")
}));
})
.RunAsEmulator(c =>
{
c.WithBlobPort(10000).
WithQueuePort(10001).
WithTablePort(10002);
});
var workingDirectory = builder.Configuration["workingDirectory"] ?? throw new ArgumentNullException("workingDirectory", "Missing working directory");
//Run Azurite setup
var azuriteSetupScript =
builder
.Configuration["scripts:azuriteSetup"]
?? throw new ArgumentNullException("azuriteSetupScriptPath", "Missing azurite setup script path");
builder
.AddExecutable("azurite-setup", "pwsh", workingDirectory, "-File", azuriteSetupScript)
.WaitFor(azurite)
.ExcludeFromManifest();
return azurite;
}
public static IResourceBuilder<NgrokResource> ConfigureNgrok(this IDistributedApplicationBuilder builder, (IResourceBuilder<ProjectResource>, string) tunnelResource)
{
var authToken = builder
.AddParameter("ngrok-auth-token",
builder.Configuration["ngrokAuthToken"]
?? throw new ArgumentNullException("ngrokAuthToken", "Missing ngrok auth token"),
secret: true);
return builder.AddNgrok("billing-webhook-ngrok-endpoint", endpointPort: 59600)
.WithAuthToken(authToken)
.WithTunnelEndpoint(tunnelResource.Item1, tunnelResource.Item2)
.WithExplicitStart();
}
public static IResourceBuilder<ExecutableResource> ConfigureMigrations(this IDistributedApplicationBuilder builder, bool isSelfHosted)
{
var workingDirectory = builder.Configuration["workingDirectory"] ??
throw new ArgumentNullException("workingDirectory", "Missing working directory");
var migrationArgs = new List<string>
{
"-File",
builder.Configuration["scripts:dbMigration"]
?? throw new ArgumentNullException("migrationScriptPath", "Missing migration script path")
};
if (isSelfHosted)
{
migrationArgs.Add("-self-hosted");
}
return builder
.AddExecutable("run-db-migrations", "pwsh", workingDirectory, migrationArgs.ToArray());
}
public static IResourceBuilder<ProjectResource> AddBitwardenService<TProject>(
this IDistributedApplicationBuilder builder, IResourceBuilder<SqlServerDatabaseResource> db,
IResourceBuilder<ExecutableResource> secretsSetup, IResourceBuilder<ContainerResource> mail, string name)
where TProject : IProjectMetadata, new()
{
var service = builder.AddProject<TProject>(name)
.WithHttpEndpoint(port: builder.GetBitwardenServicePort(name), name: $"{name}-http")
.WithReference(db)
.WaitFor(db)
.WaitForCompletion(secretsSetup);
if (name is "admin" or "identity" or "billing")
{
service.WithReference(mail.GetEndpoint("smtp"));
}
return service;
}
public static IResourceBuilder<NodeAppResource> AddBitwardenNpmApp(this IDistributedApplicationBuilder builder,
string name, string path, IResourceBuilder<ProjectResource> api, string scriptName = "build:bit:watch")
{
var clientsRelativePath = builder.Configuration["clientsRelativePath"] ??
throw new ArgumentNullException("clientsRelativePath", "Missing client relative path");
return builder
.AddNpmApp(name, $"{clientsRelativePath}/{path}", scriptName)
.WithReference(api)
.WaitFor(api)
.WithExplicitStart();
}
public static int GetBitwardenServicePort(this IDistributedApplicationBuilder builder, string serviceName)
{
var isSelfHosted = builder.Configuration["isSelfHosted"] == "true";
var configKey = isSelfHosted
? $"dev:selfHostOverride:globalSettings:baseServiceUri:{serviceName}"
: $"globalSettings:baseServiceUri:{serviceName}";
var uriString = builder.Configuration[configKey]
?? throw new InvalidOperationException($"Configuration value for '{configKey}' not found.");
return new Uri(uriString).Port;
}
}

View File

@@ -0,0 +1,29 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:17271;http://localhost:15055",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21022",
"ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22177"
}
},
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:15055",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19147",
"ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20252"
}
}
}
}

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

9
AppHost/appsettings.json Normal file
View File

@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Aspire.Hosting.Dcp": "Warning"
}
}
}