mirror of
https://github.com/bitwarden/server.git
synced 2024-11-21 12:05:42 +01:00
Use all organization memberships for LaunchDarkly user context, better separate context kinds (#2807)
* Use all organization memberships for LaunchDarkly context * Use simpler null check Co-authored-by: Oscar Hinton <Hinton@users.noreply.github.com> * Remove unnecessary interpolation Co-authored-by: Oscar Hinton <Hinton@users.noreply.github.com> * Remove unnecessary interpolation Co-authored-by: Oscar Hinton <Hinton@users.noreply.github.com> * Fully spell out organizations * Use client type for context separation decisions --------- Co-authored-by: Oscar Hinton <Hinton@users.noreply.github.com>
This commit is contained in:
parent
e667908a06
commit
3d0ca908ff
@ -104,18 +104,42 @@ public class LaunchDarklyFeatureService : IFeatureService, IDisposable
|
||||
{
|
||||
var builder = LaunchDarkly.Sdk.Context.MultiBuilder();
|
||||
|
||||
if (currentContext.UserId.HasValue)
|
||||
switch (currentContext.ClientType)
|
||||
{
|
||||
var user = LaunchDarkly.Sdk.Context.Builder(currentContext.UserId.Value.ToString());
|
||||
user.Kind(LaunchDarkly.Sdk.ContextKind.Default);
|
||||
builder.Add(user.Build());
|
||||
case Identity.ClientType.User:
|
||||
{
|
||||
var ldUser = LaunchDarkly.Sdk.Context.Builder(currentContext.UserId.Value.ToString());
|
||||
ldUser.Kind(LaunchDarkly.Sdk.ContextKind.Default);
|
||||
|
||||
if (currentContext.Organizations?.Any() ?? false)
|
||||
{
|
||||
var ldOrgs = currentContext.Organizations.Select(o => LaunchDarkly.Sdk.LdValue.Of(o.Id.ToString()));
|
||||
ldUser.Set("organizations", LaunchDarkly.Sdk.LdValue.ArrayFrom(ldOrgs));
|
||||
}
|
||||
|
||||
if (currentContext.OrganizationId.HasValue)
|
||||
builder.Add(ldUser.Build());
|
||||
}
|
||||
break;
|
||||
|
||||
case Identity.ClientType.Organization:
|
||||
{
|
||||
var org = LaunchDarkly.Sdk.Context.Builder(currentContext.OrganizationId.Value.ToString());
|
||||
org.Kind("org");
|
||||
builder.Add(org.Build());
|
||||
var ldOrg = LaunchDarkly.Sdk.Context.Builder(currentContext.OrganizationId.Value.ToString());
|
||||
ldOrg.Kind("organization");
|
||||
builder.Add(ldOrg.Build());
|
||||
}
|
||||
break;
|
||||
|
||||
case Identity.ClientType.ServiceAccount:
|
||||
{
|
||||
var ldServiceAccount = LaunchDarkly.Sdk.Context.Builder(currentContext.UserId.Value.ToString());
|
||||
ldServiceAccount.Kind("service-account");
|
||||
builder.Add(ldServiceAccount.Build());
|
||||
|
||||
var ldOrg = LaunchDarkly.Sdk.Context.Builder(currentContext.OrganizationId.Value.ToString());
|
||||
ldOrg.Kind("organization");
|
||||
builder.Add(ldOrg.Build());
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return builder.Build();
|
||||
|
@ -1,30 +1,79 @@
|
||||
using System.Net.Http.Headers;
|
||||
using Bit.Api.IntegrationTest.Factories;
|
||||
using Bit.Api.IntegrationTest.Helpers;
|
||||
using Bit.Api.Models.Response;
|
||||
using Bit.Core.Entities;
|
||||
using Xunit;
|
||||
|
||||
namespace Bit.Api.IntegrationTest.Controllers;
|
||||
|
||||
public class ConfigControllerTests : IClassFixture<ApiApplicationFactory>
|
||||
public class ConfigControllerTests : IClassFixture<ApiApplicationFactory>, IAsyncLifetime
|
||||
{
|
||||
private readonly HttpClient _client;
|
||||
private readonly ApiApplicationFactory _factory;
|
||||
|
||||
public ConfigControllerTests(ApiApplicationFactory factory) => _factory = factory;
|
||||
private string _email = null!;
|
||||
|
||||
public ConfigControllerTests(ApiApplicationFactory factory)
|
||||
{
|
||||
_factory = factory;
|
||||
_client = _factory.CreateClient();
|
||||
}
|
||||
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
_email = $"integration-test{Guid.NewGuid()}@bitwarden.com";
|
||||
|
||||
var tokens = await _factory.LoginWithNewAccount(_email);
|
||||
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokens.Token);
|
||||
}
|
||||
|
||||
public Task DisposeAsync()
|
||||
{
|
||||
_client.Dispose();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private async Task LoginAsync()
|
||||
{
|
||||
var tokens = await _factory.LoginAsync(_email);
|
||||
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokens.Token);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetConfigs()
|
||||
{
|
||||
var tokens = await _factory.LoginWithNewAccount();
|
||||
var client = _factory.CreateClient();
|
||||
|
||||
using var message = new HttpRequestMessage(HttpMethod.Get, "/config");
|
||||
message.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.Token);
|
||||
var response = await client.SendAsync(message);
|
||||
|
||||
var response = await _client.GetAsync("/config");
|
||||
response.EnsureSuccessStatusCode();
|
||||
var result = await response.Content.ReadFromJsonAsync<ConfigResponseModel>();
|
||||
|
||||
var content = await response.Content.ReadFromJsonAsync<ConfigResponseModel>();
|
||||
Assert.NotNull(result);
|
||||
Assert.NotEmpty(result!.Version);
|
||||
}
|
||||
|
||||
Assert.NotEmpty(content!.Version);
|
||||
[Theory]
|
||||
[InlineData(1)]
|
||||
[InlineData(3)]
|
||||
public async Task GetConfigs_WithOrganizations(int orgCount)
|
||||
{
|
||||
for (var i = 0; i < orgCount; i++)
|
||||
{
|
||||
var ownerEmail = $"integration-test{Guid.NewGuid()}@bitwarden.com";
|
||||
await _factory.LoginWithNewAccount(ownerEmail);
|
||||
|
||||
Organization org;
|
||||
(org, _) = await OrganizationTestHelpers.SignUpAsync(_factory, plan: Core.Enums.PlanType.Free, ownerEmail: ownerEmail,
|
||||
name: i.ToString(), billingEmail: ownerEmail, ownerKey: i.ToString());
|
||||
await OrganizationTestHelpers.CreateUserAsync(_factory, org.Id, _email, Core.Enums.OrganizationUserType.User);
|
||||
}
|
||||
|
||||
await LoginAsync();
|
||||
|
||||
var response = await _client.GetAsync("/config");
|
||||
response.EnsureSuccessStatusCode();
|
||||
var result = await response.Content.ReadFromJsonAsync<ConfigResponseModel>();
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.NotEmpty(result!.Version);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user