mirror of
https://github.com/bitwarden/server.git
synced 2025-12-10 17:45:21 -06:00
Clean up OrgnizationIntegrationRequestModel validations and nullable declarations (#6301)
* Clean up OrgnizationIntegrationRequestModel validations; remove unnecessary nullable enables * Fix weird line break
This commit is contained in:
parent
226f274a72
commit
d0778a8a7b
@ -8,9 +8,9 @@ namespace Bit.Api.AdminConsole.Models.Request.Organizations;
|
||||
|
||||
public class OrganizationIntegrationRequestModel : IValidatableObject
|
||||
{
|
||||
public string? Configuration { get; set; }
|
||||
public string? Configuration { get; init; }
|
||||
|
||||
public IntegrationType Type { get; set; }
|
||||
public IntegrationType Type { get; init; }
|
||||
|
||||
public OrganizationIntegration ToOrganizationIntegration(Guid organizationId)
|
||||
{
|
||||
@ -33,62 +33,55 @@ public class OrganizationIntegrationRequestModel : IValidatableObject
|
||||
switch (Type)
|
||||
{
|
||||
case IntegrationType.CloudBillingSync or IntegrationType.Scim:
|
||||
yield return new ValidationResult($"{nameof(Type)} integrations are not yet supported.", new[] { nameof(Type) });
|
||||
yield return new ValidationResult($"{nameof(Type)} integrations are not yet supported.", [nameof(Type)]);
|
||||
break;
|
||||
case IntegrationType.Slack:
|
||||
yield return new ValidationResult($"{nameof(Type)} integrations cannot be created directly.", new[] { nameof(Type) });
|
||||
yield return new ValidationResult($"{nameof(Type)} integrations cannot be created directly.", [nameof(Type)]);
|
||||
break;
|
||||
case IntegrationType.Webhook:
|
||||
if (string.IsNullOrWhiteSpace(Configuration))
|
||||
{
|
||||
break;
|
||||
}
|
||||
if (!IsIntegrationValid<WebhookIntegration>())
|
||||
{
|
||||
yield return new ValidationResult(
|
||||
"Webhook integrations must include valid configuration.",
|
||||
new[] { nameof(Configuration) });
|
||||
}
|
||||
foreach (var r in ValidateConfiguration<WebhookIntegration>(allowNullOrEmpty: true))
|
||||
yield return r;
|
||||
break;
|
||||
case IntegrationType.Hec:
|
||||
if (!IsIntegrationValid<HecIntegration>())
|
||||
{
|
||||
yield return new ValidationResult(
|
||||
"HEC integrations must include valid configuration.",
|
||||
new[] { nameof(Configuration) });
|
||||
}
|
||||
foreach (var r in ValidateConfiguration<HecIntegration>(allowNullOrEmpty: false))
|
||||
yield return r;
|
||||
break;
|
||||
case IntegrationType.Datadog:
|
||||
if (!IsIntegrationValid<DatadogIntegration>())
|
||||
{
|
||||
yield return new ValidationResult(
|
||||
"Datadog integrations must include valid configuration.",
|
||||
new[] { nameof(Configuration) });
|
||||
}
|
||||
foreach (var r in ValidateConfiguration<DatadogIntegration>(allowNullOrEmpty: false))
|
||||
yield return r;
|
||||
break;
|
||||
default:
|
||||
yield return new ValidationResult(
|
||||
$"Integration type '{Type}' is not recognized.",
|
||||
new[] { nameof(Type) });
|
||||
[nameof(Type)]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsIntegrationValid<T>()
|
||||
private List<ValidationResult> ValidateConfiguration<T>(bool allowNullOrEmpty)
|
||||
{
|
||||
var results = new List<ValidationResult>();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(Configuration))
|
||||
{
|
||||
return false;
|
||||
if (!allowNullOrEmpty)
|
||||
results.Add(InvalidConfig<T>());
|
||||
return results;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var config = JsonSerializer.Deserialize<T>(Configuration);
|
||||
return config is not null;
|
||||
if (JsonSerializer.Deserialize<T>(Configuration) is null)
|
||||
results.Add(InvalidConfig<T>());
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
results.Add(InvalidConfig<T>());
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
private static ValidationResult InvalidConfig<T>() =>
|
||||
new(errorMessage: $"Must include valid {typeof(T).Name} configuration.", memberNames: [nameof(Configuration)]);
|
||||
}
|
||||
|
||||
@ -1,5 +1,3 @@
|
||||
#nullable enable
|
||||
|
||||
namespace Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||
namespace Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||
|
||||
public record HecIntegration(Uri Uri, string Scheme, string Token, string? Service = null);
|
||||
|
||||
@ -1,6 +1,4 @@
|
||||
#nullable enable
|
||||
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Enums;
|
||||
|
||||
namespace Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||
|
||||
|
||||
@ -1,6 +1,4 @@
|
||||
#nullable enable
|
||||
|
||||
namespace Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||
namespace Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||
|
||||
public class IntegrationFilterGroup
|
||||
{
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
#nullable enable
|
||||
namespace Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||
namespace Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||
|
||||
public enum IntegrationFilterOperation
|
||||
{
|
||||
|
||||
@ -1,6 +1,4 @@
|
||||
#nullable enable
|
||||
|
||||
namespace Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||
namespace Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||
|
||||
public class IntegrationFilterRule
|
||||
{
|
||||
|
||||
@ -1,6 +1,4 @@
|
||||
#nullable enable
|
||||
|
||||
namespace Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||
namespace Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||
|
||||
public class IntegrationHandlerResult
|
||||
{
|
||||
|
||||
@ -1,6 +1,4 @@
|
||||
#nullable enable
|
||||
|
||||
using System.Text.Json;
|
||||
using System.Text.Json;
|
||||
using Bit.Core.Enums;
|
||||
|
||||
namespace Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||
|
||||
@ -1,6 +1,4 @@
|
||||
#nullable enable
|
||||
|
||||
using Bit.Core.AdminConsole.Entities;
|
||||
using Bit.Core.AdminConsole.Entities;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Models.Data;
|
||||
|
||||
@ -1,5 +1,3 @@
|
||||
#nullable enable
|
||||
|
||||
namespace Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||
namespace Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||
|
||||
public record SlackIntegration(string Token);
|
||||
|
||||
@ -1,5 +1,3 @@
|
||||
#nullable enable
|
||||
|
||||
namespace Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||
namespace Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||
|
||||
public record SlackIntegrationConfiguration(string ChannelId);
|
||||
|
||||
@ -1,5 +1,3 @@
|
||||
#nullable enable
|
||||
|
||||
namespace Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||
namespace Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||
|
||||
public record SlackIntegrationConfigurationDetails(string ChannelId, string Token);
|
||||
|
||||
@ -1,5 +1,3 @@
|
||||
#nullable enable
|
||||
|
||||
namespace Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||
namespace Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||
|
||||
public record WebhookIntegration(Uri Uri, string? Scheme = null, string? Token = null);
|
||||
|
||||
@ -1,5 +1,3 @@
|
||||
#nullable enable
|
||||
|
||||
namespace Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||
namespace Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||
|
||||
public record WebhookIntegrationConfiguration(Uri Uri, string? Scheme = null, string? Token = null);
|
||||
|
||||
@ -1,5 +1,3 @@
|
||||
#nullable enable
|
||||
|
||||
namespace Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||
namespace Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||
|
||||
public record WebhookIntegrationConfigurationDetails(Uri Uri, string? Scheme = null, string? Token = null);
|
||||
|
||||
@ -1,6 +1,4 @@
|
||||
#nullable enable
|
||||
|
||||
using System.Text;
|
||||
using System.Text;
|
||||
using Azure.Messaging.ServiceBus;
|
||||
using Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
@ -1,6 +1,4 @@
|
||||
#nullable enable
|
||||
|
||||
using Azure.Messaging.ServiceBus;
|
||||
using Azure.Messaging.ServiceBus;
|
||||
using Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
@ -1,6 +1,4 @@
|
||||
#nullable enable
|
||||
|
||||
using System.Text.Json;
|
||||
using System.Text.Json;
|
||||
using Bit.Core.Models.Data;
|
||||
|
||||
namespace Bit.Core.Services;
|
||||
|
||||
@ -1,6 +1,4 @@
|
||||
#nullable enable
|
||||
|
||||
using System.Text.Json;
|
||||
using System.Text.Json;
|
||||
using Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||
using Bit.Core.AdminConsole.Utilities;
|
||||
using Bit.Core.Enums;
|
||||
|
||||
@ -1,6 +1,4 @@
|
||||
#nullable enable
|
||||
|
||||
using Bit.Core.Models.Data;
|
||||
using Bit.Core.Models.Data;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Bit.Core.Services;
|
||||
|
||||
@ -1,6 +1,4 @@
|
||||
#nullable enable
|
||||
|
||||
using Bit.Core.Models.Data;
|
||||
using Bit.Core.Models.Data;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Bit.Core.Services;
|
||||
|
||||
@ -1,6 +1,4 @@
|
||||
#nullable enable
|
||||
|
||||
using System.Linq.Expressions;
|
||||
using System.Linq.Expressions;
|
||||
using Bit.Core.Models.Data;
|
||||
|
||||
namespace Bit.Core.Services;
|
||||
|
||||
@ -1,6 +1,4 @@
|
||||
#nullable enable
|
||||
|
||||
using System.Text.Json;
|
||||
using System.Text.Json;
|
||||
using Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||
using Bit.Core.Models.Data;
|
||||
|
||||
|
||||
@ -1,6 +1,4 @@
|
||||
#nullable enable
|
||||
|
||||
using System.Text;
|
||||
using System.Text;
|
||||
using Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using RabbitMQ.Client;
|
||||
|
||||
@ -1,6 +1,4 @@
|
||||
#nullable enable
|
||||
|
||||
using System.Text;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
@ -1,6 +1,4 @@
|
||||
#nullable enable
|
||||
|
||||
using System.Text;
|
||||
using System.Text;
|
||||
using Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Settings;
|
||||
|
||||
@ -1,6 +1,4 @@
|
||||
#nullable enable
|
||||
|
||||
using Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||
using Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||
|
||||
namespace Bit.Core.Services;
|
||||
|
||||
|
||||
@ -1,6 +1,4 @@
|
||||
#nullable enable
|
||||
|
||||
using System.Net.Http.Headers;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Net.Http.Json;
|
||||
using System.Web;
|
||||
using Bit.Core.Models.Slack;
|
||||
|
||||
@ -1,6 +1,4 @@
|
||||
#nullable enable
|
||||
|
||||
using System.Net.Http.Headers;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||
|
||||
|
||||
@ -84,7 +84,7 @@ public class OrganizationIntegrationRequestModelTests
|
||||
|
||||
Assert.Single(results);
|
||||
Assert.Contains(nameof(model.Configuration), results[0].MemberNames);
|
||||
Assert.Contains("must include valid configuration", results[0].ErrorMessage);
|
||||
Assert.Contains("Must include valid", results[0].ErrorMessage);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@ -114,7 +114,7 @@ public class OrganizationIntegrationRequestModelTests
|
||||
|
||||
Assert.Single(results);
|
||||
Assert.Contains(nameof(model.Configuration), results[0].MemberNames);
|
||||
Assert.Contains("must include valid configuration", results[0].ErrorMessage);
|
||||
Assert.Contains("Must include valid", results[0].ErrorMessage);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@ -130,7 +130,7 @@ public class OrganizationIntegrationRequestModelTests
|
||||
|
||||
Assert.Single(results);
|
||||
Assert.Contains(nameof(model.Configuration), results[0].MemberNames);
|
||||
Assert.Contains("must include valid configuration", results[0].ErrorMessage);
|
||||
Assert.Contains("Must include valid", results[0].ErrorMessage);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@ -160,7 +160,7 @@ public class OrganizationIntegrationRequestModelTests
|
||||
|
||||
Assert.Single(results);
|
||||
Assert.Contains(nameof(model.Configuration), results[0].MemberNames);
|
||||
Assert.Contains("must include valid configuration", results[0].ErrorMessage);
|
||||
Assert.Contains("Must include valid", results[0].ErrorMessage);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@ -176,7 +176,7 @@ public class OrganizationIntegrationRequestModelTests
|
||||
|
||||
Assert.Single(results);
|
||||
Assert.Contains(nameof(model.Configuration), results[0].MemberNames);
|
||||
Assert.Contains("must include valid configuration", results[0].ErrorMessage);
|
||||
Assert.Contains("Must include valid", results[0].ErrorMessage);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user