Skip to content

Commit 8d41112

Browse files
Adds license warnings to the BFF (#2130)
* licensing * format * Apply suggestions from code review Co-authored-by: Maarten Balliauw <maarten.balliauw@duendesoftware.com> * Update LicensingLogMessages.cs * Update LicensingLogMessages.cs added the word 'trial' --------- Co-authored-by: Maarten Balliauw <maarten.balliauw@duendesoftware.com>
1 parent 9586cb5 commit 8d41112

17 files changed

+330
-487
lines changed

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<PackageVersion Include="Duende.IdentityModel" Version="7.1.0" />
1515
<PackageVersion Include="Duende.IdentityModel.OidcClient" Version="6.0.1" />
1616
<PackageVersion Include="Duende.IdentityServer" Version="7.1.0" />
17+
<PackageVersion Include="Duende.Private.Licensing" Version="1.0.0" />
1718
<PackageVersion Include="IdentityModel.AspNetCore.OAuth2Introspection" Version="6.2.0" />
1819
<PackageVersion Include="Meziantou.Extensions.Logging.Xunit" Version="1.0.8" />
1920
<PackageVersion Include="Microsoft.AspNetCore.Authentication.Certificate" Condition="'$(TargetFramework)' == 'net8.0'" Version="8.0.16" />

bff/src/Bff.Blazor.Client/Bff.Blazor.Client.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" />
1313
<PackageReference Include="Microsoft.Extensions.Http" />
1414
<PackageReference Include="System.Text.Json" />
15+
<PackageReference Include="Duende.Private.Licensing" />
1516
</ItemGroup>
1617

1718
<ItemGroup>
1819
<InternalsVisibleTo Include="Duende.Bff.Tests" />
1920
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
21+
2022
</ItemGroup>
2123

2224
</Project>

bff/src/Bff.Blazor/BffServerAuthenticationStateProvider.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Security.Claims;
66
using Duende.Bff.Configuration;
77
using Duende.Bff.Internal;
8+
using Duende.Bff.Licensing;
89
using Duende.Bff.SessionManagement.SessionStore;
910
using Duende.IdentityModel;
1011
using Microsoft.AspNetCore.Components;
@@ -50,6 +51,7 @@ public BffServerAuthenticationStateProvider(
5051
BuildUserSessionPartitionKey buildUserSessionPartitionKey,
5152
IOptions<BffBlazorServerOptions> blazorOptions,
5253
IOptions<BffOptions> bffOptions,
54+
LicenseValidator licenseValidator,
5355
ILoggerFactory loggerFactory)
5456
: base(loggerFactory)
5557
{
@@ -65,20 +67,16 @@ public BffServerAuthenticationStateProvider(
6567
AuthenticationStateChanged += OnAuthenticationStateChanged;
6668
_subscription = _state.RegisterOnPersisting(OnPersistingAsync, RenderMode.InteractiveWebAssembly);
6769

68-
CheckLicense(loggerFactory, _bffOptions);
70+
CheckLicense(licenseValidator);
6971
}
7072

71-
internal static bool LicenseChecked;
7273

73-
internal static void CheckLicense(ILoggerFactory loggerFactory, BffOptions options)
74+
internal static void CheckLicense(LicenseValidator validator)
7475
{
75-
if (LicenseChecked == false)
76+
if (!validator.IsValid())
7677
{
77-
Licensing.LicenseValidator.Initalize(loggerFactory, options);
78-
Licensing.LicenseValidator.ValidateLicense();
78+
// todo: license enforcement
7979
}
80-
81-
LicenseChecked = true;
8280
}
8381

8482
private void OnAuthenticationStateChanged(Task<AuthenticationState> task) => _authenticationStateTask = task;

bff/src/Bff/Bff.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@
2222
<InternalsVisibleTo Include="Duende.Bff.Yarp" />
2323
<InternalsVisibleTo Include="Duende.Bff.Tests" />
2424
</ItemGroup>
25+
26+
<ItemGroup>
27+
<PackageReference Include="Duende.Private.Licensing" />
28+
</ItemGroup>
2529
</Project>

bff/src/Bff/BffBuilderExtensions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
using Duende.Bff.Endpoints;
1111
using Duende.Bff.Endpoints.Internal;
1212
using Duende.Bff.Internal;
13+
using Duende.Bff.Licensing;
1314
using Duende.Bff.Otel;
1415
using Duende.Bff.SessionManagement.Configuration;
1516
using Duende.Bff.SessionManagement.Revocation;
1617
using Duende.Bff.SessionManagement.SessionStore;
1718
using Duende.Bff.SessionManagement.TicketStore;
19+
using Duende.Private.Licensing;
1820
using Microsoft.AspNetCore.Authentication;
1921
using Microsoft.AspNetCore.Authentication.Cookies;
2022
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
@@ -44,6 +46,10 @@ public static T ConfigureCookies<T>(this T builder, Action<CookieAuthenticationO
4446

4547
internal static T AddBaseBffServices<T>(this T builder) where T : IBffServicesBuilder
4648
{
49+
builder.Services.AddSingleton<GetLicenseKey>(sp => () => sp.GetRequiredService<IOptions<BffOptions>>().Value.LicenseKey);
50+
builder.Services.AddSingleton<LicenseAccessor<BffLicense>>();
51+
builder.Services.AddSingleton<BffLicense>(sp => sp.GetRequiredService<LicenseAccessor<BffLicense>>().Current);
52+
builder.Services.TryAddSingleton<LicenseValidator>();
4753
builder.Services.AddDistributedMemoryCache();
4854
// IMPORTANT: The BffConfigureOpenIdConnectOptions MUST be called before calling
4955
// AddOpenIdConnectAccessTokenManagement because both configure the same options

bff/src/Bff/BffEndpointRouteBuilderExtensions.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
using Duende.Bff.Configuration;
55
using Duende.Bff.Endpoints;
66
using Duende.Bff.Endpoints.Internal;
7+
using Duende.Bff.Licensing;
78
using Duende.Bff.Otel;
89
using Microsoft.AspNetCore.Builder;
910
using Microsoft.AspNetCore.Http;
1011
using Microsoft.AspNetCore.Routing;
1112
using Microsoft.Extensions.DependencyInjection;
1213
using Microsoft.Extensions.Logging;
1314
using Microsoft.Extensions.Options;
14-
using LicenseValidator = Duende.Bff.Licensing.LicenseValidator;
1515

1616

1717
namespace Duende.Bff;
@@ -175,15 +175,13 @@ public static void MapBffDiagnosticsEndpoint(this IEndpointRouteBuilder endpoint
175175

176176
internal static void CheckLicense(this IServiceProvider serviceProvider)
177177
{
178-
if (LicenseChecked == false)
179-
{
180-
var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
181-
var options = serviceProvider.GetRequiredService<IOptions<BffOptions>>().Value;
178+
var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
179+
var options = serviceProvider.GetRequiredService<IOptions<BffOptions>>().Value;
182180

183-
LicenseValidator.Initalize(loggerFactory, options);
184-
LicenseValidator.ValidateLicense();
181+
var license = serviceProvider.GetRequiredService<LicenseValidator>();
182+
if (!license.IsValid())
183+
{
184+
// Todo, enforce license validation
185185
}
186-
187-
LicenseChecked = true;
188186
}
189187
}

bff/src/Bff/DynamicFrontends/Internal/FrontendCollection.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33

44
using System.Collections;
55
using Duende.Bff.Configuration;
6+
using Duende.Bff.Licensing;
67
using Microsoft.Extensions.Options;
78

89
namespace Duende.Bff.DynamicFrontends.Internal;
9-
1010
internal class FrontendCollection : IDisposable, IFrontendCollection
1111
{
12+
private readonly LicenseValidator _licenseValidator;
1213
private readonly IBffPluginLoader[] _plugins;
1314
private readonly object _syncRoot = new();
1415

@@ -24,11 +25,13 @@ internal class FrontendCollection : IDisposable, IFrontendCollection
2425
internal event Action<BffFrontend> OnFrontendAdded = (_) => { };
2526

2627
public FrontendCollection(
28+
LicenseValidator licenseValidator,
2729
IOptionsMonitor<BffConfiguration> bffConfiguration,
2830
IEnumerable<IBffPluginLoader> plugins,
2931
IEnumerable<BffFrontend>? frontendsConfiguredDuringStartup = null
3032
)
3133
{
34+
_licenseValidator = licenseValidator;
3235
_plugins = plugins.ToArray();
3336
_frontends = ReadFrontends(bffConfiguration.CurrentValue, frontendsConfiguredDuringStartup ?? []);
3437

@@ -57,9 +60,18 @@ public FrontendCollection(
5760
.Where(frontend => oldFrontends.All(x => x.Name != frontend.Name))
5861
.ToArray();
5962

63+
var totalFrontends = oldFrontends.Length - removedFrontends.Length;
64+
65+
foreach (var frontend in addedFrontends)
66+
{
67+
_licenseValidator.LogFrontendAdded(frontend.Name, ++totalFrontends);
68+
}
69+
6070
Interlocked.Exchange(ref _frontends, newFrontends);
71+
6172
}
6273

74+
6375
foreach (var added in addedFrontends)
6476
{
6577
OnFrontendAdded(added);
@@ -77,6 +89,9 @@ public FrontendCollection(
7789
});
7890
}
7991

92+
93+
94+
8095
private static bool IsUpdated(BffFrontend left, BffFrontend right)
8196
{
8297
if (!left.Equals(right))
@@ -171,6 +186,7 @@ public void AddOrUpdate(BffFrontend frontend)
171186
}
172187
else
173188
{
189+
_licenseValidator.LogFrontendAdded(frontend.Name, _frontends.Length);
174190
OnFrontendAdded(frontend);
175191
}
176192

0 commit comments

Comments
 (0)