Skip to content

Aspirification of API service (incl admin + identity dbs) #5902

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
424 changes: 424 additions & 0 deletions bitwarden-server.sln

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "8.0.100",
"version": "9.0.100",
Copy link
Member

@audreyality audreyality May 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๐Ÿ“ @justindbaur, @withinfocus - Wanted to draw your attention to this. I'm not sure who owns the .Net SDK dependency, but it looks like it needs to update before we merge this PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, and we are currently planning on just jumping to 10. We'll see.

"rollForward": "latestFeature"
},
"msbuild-sdks": {
Expand Down
3 changes: 3 additions & 0 deletions src/.aspire/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"appHostPath": "../Apphost/Apphost.csproj"
}
1 change: 1 addition & 0 deletions src/Admin/Admin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<ItemGroup>
<ProjectReference Include="..\..\util\MySqlMigrations\MySqlMigrations.csproj" />
<ProjectReference Include="..\..\util\PostgresMigrations\PostgresMigrations.csproj" />
<ProjectReference Include="..\ServiceDefaults\ServiceDefaults.csproj" />
<ProjectReference Include="..\SharedWeb\SharedWeb.csproj" />
<ProjectReference Include="..\..\util\Migrator\Migrator.csproj" />
<ProjectReference Include="..\Core\Core.csproj" />
Expand Down
69 changes: 43 additions & 26 deletions src/Admin/Program.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,52 @@
๏ปฟusing Bit.Core.Utilities;
๏ปฟusing Bit.Core.Settings;

namespace Bit.Admin;

public class Program
{
public static void Main(string[] args)
{
Host
.CreateDefaultBuilder(args)
.ConfigureCustomAppConfiguration(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureKestrel(o =>
{
o.Limits.MaxRequestLineSize = 20_000;
});
webBuilder.UseStartup<Startup>();
webBuilder.ConfigureLogging((hostingContext, logging) =>
logging.AddSerilog(hostingContext, (e, globalSettings) =>
{
var context = e.Properties["SourceContext"].ToString();
if (e.Properties.ContainsKey("RequestPath") &&
!string.IsNullOrWhiteSpace(e.Properties["RequestPath"]?.ToString()) &&
(context.Contains(".Server.Kestrel") || context.Contains(".Core.IISHttpServer")))
{
return false;
}
return e.Level >= globalSettings.MinLogLevel.AdminSettings.Default;
}));
})
.Build()
.Run();
var builder = WebApplication.CreateBuilder(args);

builder.AddServiceDefaults();

var startup = new Startup(builder.Environment, builder.Configuration);

startup.ConfigureServices(builder.Services);

var app = builder.Build();

app.MapDefaultEndpoints();

var settings = app.Services.GetRequiredService<GlobalSettings>();

startup.Configure(app, app.Environment, app.Lifetime, settings);

app.Run();
// Host
// .CreateDefaultBuilder(args)
// .ConfigureCustomAppConfiguration(args)
// .ConfigureWebHostDefaults(webBuilder =>
// {
// webBuilder.ConfigureKestrel(o =>
// {
// o.Limits.MaxRequestLineSize = 20_000;
// });
// webBuilder.UseStartup<Startup>();
// webBuilder.ConfigureLogging((hostingContext, logging) =>
// logging.AddSerilog(hostingContext, (e, globalSettings) =>
// {
// var context = e.Properties["SourceContext"].ToString();
// if (e.Properties.ContainsKey("RequestPath") &&
// !string.IsNullOrWhiteSpace(e.Properties["RequestPath"]?.ToString()) &&
// (context.Contains(".Server.Kestrel") || context.Contains(".Core.IISHttpServer")))
// {
// return false;
// }
// return e.Level >= globalSettings.MinLogLevel.AdminSettings.Default;
// }));
// })
// .Build()
// .Run();
}
}
2 changes: 1 addition & 1 deletion src/Admin/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"Admin": {
"commandName": "Project",
"applicationUrl": "http://localhost:62911/",
"applicationUrl": "http://localhost:6291/",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
Expand Down
1 change: 1 addition & 0 deletions src/Api/Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<PropertyGroup Condition=" '$(RunConfiguration)' == 'Api' " />
<PropertyGroup Condition=" '$(RunConfiguration)' == 'Api-SelfHost' " />
<ItemGroup>
<ProjectReference Include="..\ServiceDefaults\ServiceDefaults.csproj" />
<ProjectReference Include="..\SharedWeb\SharedWeb.csproj" />
<ProjectReference Include="..\Core\Core.csproj" />
</ItemGroup>
Expand Down
103 changes: 67 additions & 36 deletions src/Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,46 +1,77 @@
๏ปฟusing AspNetCoreRateLimit;
using Bit.Core.Utilities;
using Microsoft.IdentityModel.Tokens;
๏ปฟusing Bit.Core.Settings;
using Bit.SharedWeb.Health;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;

namespace Bit.Api;

public class Program
{
public static void Main(string[] args)
{
Host
.CreateDefaultBuilder(args)
.ConfigureCustomAppConfiguration(args)
.ConfigureWebHostDefaults(webBuilder =>
var builder = WebApplication.CreateBuilder(args);

builder.AddServiceDefaults();

var startup = new Startup(builder.Environment, builder.Configuration);

startup.ConfigureServices(builder.Services);

var app = builder.Build();

app.MapDefaultEndpoints();

var globalSettings = app.Services.GetRequiredService<GlobalSettings>();
var logger = app.Services.GetRequiredService<ILogger<Startup>>();

startup.Configure(app, app.Environment, app.Lifetime, globalSettings, logger);

app.MapDefaultControllerRoute();

if (!globalSettings.SelfHosted)
{
app.MapHealthChecks("/healthz");

app.MapHealthChecks("/healthz/extended", new HealthCheckOptions
{
webBuilder.UseStartup<Startup>();
webBuilder.ConfigureLogging((hostingContext, logging) =>
logging.AddSerilog(hostingContext, (e, globalSettings) =>
{
var context = e.Properties["SourceContext"].ToString();
if (e.Exception != null &&
(e.Exception.GetType() == typeof(SecurityTokenValidationException) ||
e.Exception.Message == "Bad security stamp."))
{
return false;
}

if (
context.Contains(typeof(IpRateLimitMiddleware).FullName))
{
return e.Level >= globalSettings.MinLogLevel.ApiSettings.IpRateLimit;
}

if (context.Contains("Duende.IdentityServer.Validation.TokenValidator") ||
context.Contains("Duende.IdentityServer.Validation.TokenRequestValidator"))
{
return e.Level >= globalSettings.MinLogLevel.ApiSettings.IdentityToken;
}

return e.Level >= globalSettings.MinLogLevel.ApiSettings.Default;
}));
})
.Build()
.Run();
ResponseWriter = HealthCheckServiceExtensions.WriteResponse
});
}

app.Run();

// Host
// .CreateDefaultBuilder(args)
// .ConfigureCustomAppConfiguration(args)
// .ConfigureWebHostDefaults(webBuilder =>
// {
// webBuilder.UseStartup<Startup>();
// webBuilder.ConfigureLogging((hostingContext, logging) =>
// logging.AddSerilog(hostingContext, (e, globalSettings) =>
// {
// var context = e.Properties["SourceContext"].ToString();
// if (e.Exception != null &&
// (e.Exception.GetType() == typeof(SecurityTokenValidationException) ||
// e.Exception.Message == "Bad security stamp."))
// {
// return false;
// }

// if (
// context.Contains(typeof(IpRateLimitMiddleware).FullName))
// {
// return e.Level >= globalSettings.MinLogLevel.ApiSettings.IpRateLimit;
// }

// if (context.Contains("Duende.IdentityServer.Validation.TokenValidator") ||
// context.Contains("Duende.IdentityServer.Validation.TokenRequestValidator"))
// {
// return e.Level >= globalSettings.MinLogLevel.ApiSettings.IdentityToken;
// }

// return e.Level >= globalSettings.MinLogLevel.ApiSettings.Default;
// }));
// })
// .Build()
// .Run();
}
}
7 changes: 7 additions & 0 deletions src/Api/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"applicationUrl": "https://localhost:4001",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Api": {
"commandName": "Project",
"applicationUrl": "http://localhost:4000",
Expand Down
16 changes: 0 additions & 16 deletions src/Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,22 +272,6 @@ public void Configure(
// Add current context
app.UseMiddleware<CurrentContextMiddleware>();

// Add endpoints to the request pipeline.
app.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute();

if (!globalSettings.SelfHosted)
{
endpoints.MapHealthChecks("/healthz");

endpoints.MapHealthChecks("/healthz/extended", new HealthCheckOptions
{
ResponseWriter = HealthCheckServiceExtensions.WriteResponse
});
}
});

// Add Swagger
if (Environment.IsDevelopment() || globalSettings.SelfHosted)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Api/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"baseServiceUri": {
"vault": "https://localhost:8080",
"api": "http://localhost:4000",
"identity": "http://localhost:33656",
"identity": "http://identity",
"admin": "http://localhost:62911",
"notifications": "http://localhost:61840",
"sso": "http://localhost:51822",
Expand Down
55 changes: 55 additions & 0 deletions src/Apphost/AppHost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
var builder = DistributedApplication.CreateBuilder(args);

var bitwardenid = builder.AddParameter("bitwardenid", secret: true);
var bitwardenkey = builder.AddParameter("bitwardenkey", secret: true);

// var mail = builder.AddMailPit("mail")
// .WithLifetime(ContainerLifetime.Persistent)
// .WithDataVolume("mail-data")
// .WithHttpHealthCheck("/health");

builder.AddGlobalSettings(c =>
{
// var smtp = mail.Resource.GetEndpoint("smtp");

// Set selfhosted to true in all projects
// and pass bitwarden id and keys
c.WithEnvironment(context =>
{
var prefix = "globalSettings__";
context.EnvironmentVariables[$"{prefix}selfHosted"] = "true";
context.EnvironmentVariables[$"{prefix}installation__id"] = bitwardenid;
context.EnvironmentVariables[$"{prefix}installation__key"] = bitwardenkey;

// context.EnvironmentVariables[$"{prefix}mail__smtp__host"] = smtp.Property(EndpointProperty.Host);
// context.EnvironmentVariables[$"{prefix}mail__smtp__port"] = smtp.Property(EndpointProperty.Port);
});
});

var iconServce = builder.AddProject<Projects.Icons>("icons", launchProfileName: "Icons");

var sql = builder.AddSqlServer("sql")
.WithLifetime(ContainerLifetime.Persistent)
.WithDataVolume("sql-data");
var theDb = sql.AddDatabase("the-db");

var identityapi = builder.AddProject<Projects.Identity>("identity", launchProfileName: "Identity")
.WithHttpHealthCheck("/health")
.WithDb(theDb);

builder.AddProject<Projects.Api>("api", launchProfileName: "Api")
.WithHttpHealthCheck("/health")
.WithReference(identityapi)
.WithDb(theDb);

builder.AddProject<Projects.Billing>("billing", launchProfileName: "Billing")
.WithUrlForEndpoint("http", url => url.Url = "/swagger")
.WithHttpHealthCheck("/alive");

builder.AddProject<Projects.Admin>("admin", launchProfileName: "Admin")
.WithHttpHealthCheck("/alive")
.WithReference(identityapi)
.WithDb(theDb)
.InstallAssets();

builder.Build().Run();
27 changes: 27 additions & 0 deletions src/Apphost/Apphost.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<Sdk Name="Aspire.AppHost.Sdk" Version="9.3.0" />

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UserSecretsId>79ddae47-9251-44d8-9897-e44dd94074c5</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Aspire.Hosting.AppHost" Version="9.3.0" />
<PackageReference Include="Aspire.Hosting.SqlServer" Version="9.3.0" />
<PackageReference Include="CommunityToolkit.Aspire.Hosting.MailPit" Version="9.5.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Admin\Admin.csproj" />
<ProjectReference Include="..\Api\Api.csproj" />
<ProjectReference Include="..\Billing\Billing.csproj" />
<ProjectReference Include="..\Icons\Icons.csproj" />
<ProjectReference Include="..\Identity\Identity.csproj" />
</ItemGroup>

</Project>
Loading
Loading