mirror of
https://github.com/bitwarden/server
synced 2025-12-10 13:23:27 +00: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:
@@ -8,9 +8,9 @@ namespace Bit.Api.AdminConsole.Models.Request.Organizations;
|
|||||||
|
|
||||||
public class OrganizationIntegrationRequestModel : IValidatableObject
|
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)
|
public OrganizationIntegration ToOrganizationIntegration(Guid organizationId)
|
||||||
{
|
{
|
||||||
@@ -33,62 +33,55 @@ public class OrganizationIntegrationRequestModel : IValidatableObject
|
|||||||
switch (Type)
|
switch (Type)
|
||||||
{
|
{
|
||||||
case IntegrationType.CloudBillingSync or IntegrationType.Scim:
|
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;
|
break;
|
||||||
case IntegrationType.Slack:
|
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;
|
break;
|
||||||
case IntegrationType.Webhook:
|
case IntegrationType.Webhook:
|
||||||
if (string.IsNullOrWhiteSpace(Configuration))
|
foreach (var r in ValidateConfiguration<WebhookIntegration>(allowNullOrEmpty: true))
|
||||||
{
|
yield return r;
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (!IsIntegrationValid<WebhookIntegration>())
|
|
||||||
{
|
|
||||||
yield return new ValidationResult(
|
|
||||||
"Webhook integrations must include valid configuration.",
|
|
||||||
new[] { nameof(Configuration) });
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case IntegrationType.Hec:
|
case IntegrationType.Hec:
|
||||||
if (!IsIntegrationValid<HecIntegration>())
|
foreach (var r in ValidateConfiguration<HecIntegration>(allowNullOrEmpty: false))
|
||||||
{
|
yield return r;
|
||||||
yield return new ValidationResult(
|
|
||||||
"HEC integrations must include valid configuration.",
|
|
||||||
new[] { nameof(Configuration) });
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case IntegrationType.Datadog:
|
case IntegrationType.Datadog:
|
||||||
if (!IsIntegrationValid<DatadogIntegration>())
|
foreach (var r in ValidateConfiguration<DatadogIntegration>(allowNullOrEmpty: false))
|
||||||
{
|
yield return r;
|
||||||
yield return new ValidationResult(
|
|
||||||
"Datadog integrations must include valid configuration.",
|
|
||||||
new[] { nameof(Configuration) });
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
yield return new ValidationResult(
|
yield return new ValidationResult(
|
||||||
$"Integration type '{Type}' is not recognized.",
|
$"Integration type '{Type}' is not recognized.",
|
||||||
new[] { nameof(Type) });
|
[nameof(Type)]);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool IsIntegrationValid<T>()
|
private List<ValidationResult> ValidateConfiguration<T>(bool allowNullOrEmpty)
|
||||||
{
|
{
|
||||||
|
var results = new List<ValidationResult>();
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(Configuration))
|
if (string.IsNullOrWhiteSpace(Configuration))
|
||||||
{
|
{
|
||||||
return false;
|
if (!allowNullOrEmpty)
|
||||||
|
results.Add(InvalidConfig<T>());
|
||||||
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var config = JsonSerializer.Deserialize<T>(Configuration);
|
if (JsonSerializer.Deserialize<T>(Configuration) is null)
|
||||||
return config is not null;
|
results.Add(InvalidConfig<T>());
|
||||||
}
|
}
|
||||||
catch
|
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);
|
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;
|
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
|
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
|
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
|
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
|
public class IntegrationHandlerResult
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
#nullable enable
|
using System.Text.Json;
|
||||||
|
|
||||||
using System.Text.Json;
|
|
||||||
using Bit.Core.Enums;
|
using Bit.Core.Enums;
|
||||||
|
|
||||||
namespace Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
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.Entities;
|
||||||
using Bit.Core.Enums;
|
using Bit.Core.Enums;
|
||||||
using Bit.Core.Models.Data;
|
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);
|
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);
|
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);
|
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);
|
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);
|
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);
|
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 Azure.Messaging.ServiceBus;
|
||||||
using Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
using Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||||
using Microsoft.Extensions.Logging;
|
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 Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
#nullable enable
|
using System.Text.Json;
|
||||||
|
|
||||||
using System.Text.Json;
|
|
||||||
using Bit.Core.Models.Data;
|
using Bit.Core.Models.Data;
|
||||||
|
|
||||||
namespace Bit.Core.Services;
|
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.Models.Data.EventIntegrations;
|
||||||
using Bit.Core.AdminConsole.Utilities;
|
using Bit.Core.AdminConsole.Utilities;
|
||||||
using Bit.Core.Enums;
|
using Bit.Core.Enums;
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
#nullable enable
|
using Bit.Core.Models.Data;
|
||||||
|
|
||||||
using Bit.Core.Models.Data;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
namespace Bit.Core.Services;
|
namespace Bit.Core.Services;
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
#nullable enable
|
using Bit.Core.Models.Data;
|
||||||
|
|
||||||
using Bit.Core.Models.Data;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
namespace Bit.Core.Services;
|
namespace Bit.Core.Services;
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
#nullable enable
|
using System.Linq.Expressions;
|
||||||
|
|
||||||
using System.Linq.Expressions;
|
|
||||||
using Bit.Core.Models.Data;
|
using Bit.Core.Models.Data;
|
||||||
|
|
||||||
namespace Bit.Core.Services;
|
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.Models.Data.EventIntegrations;
|
||||||
using Bit.Core.Models.Data;
|
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 Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using RabbitMQ.Client;
|
using RabbitMQ.Client;
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
#nullable enable
|
using System.Text;
|
||||||
|
|
||||||
using System.Text;
|
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
using Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||||
using Microsoft.Extensions.Hosting;
|
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.AdminConsole.Models.Data.EventIntegrations;
|
||||||
using Bit.Core.Enums;
|
using Bit.Core.Enums;
|
||||||
using Bit.Core.Settings;
|
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;
|
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.Net.Http.Json;
|
||||||
using System.Web;
|
using System.Web;
|
||||||
using Bit.Core.Models.Slack;
|
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 System.Text;
|
||||||
using Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
using Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||||
|
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ public class OrganizationIntegrationRequestModelTests
|
|||||||
|
|
||||||
Assert.Single(results);
|
Assert.Single(results);
|
||||||
Assert.Contains(nameof(model.Configuration), results[0].MemberNames);
|
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]
|
[Fact]
|
||||||
@@ -114,7 +114,7 @@ public class OrganizationIntegrationRequestModelTests
|
|||||||
|
|
||||||
Assert.Single(results);
|
Assert.Single(results);
|
||||||
Assert.Contains(nameof(model.Configuration), results[0].MemberNames);
|
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]
|
[Fact]
|
||||||
@@ -130,7 +130,7 @@ public class OrganizationIntegrationRequestModelTests
|
|||||||
|
|
||||||
Assert.Single(results);
|
Assert.Single(results);
|
||||||
Assert.Contains(nameof(model.Configuration), results[0].MemberNames);
|
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]
|
[Fact]
|
||||||
@@ -160,7 +160,7 @@ public class OrganizationIntegrationRequestModelTests
|
|||||||
|
|
||||||
Assert.Single(results);
|
Assert.Single(results);
|
||||||
Assert.Contains(nameof(model.Configuration), results[0].MemberNames);
|
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]
|
[Fact]
|
||||||
@@ -176,7 +176,7 @@ public class OrganizationIntegrationRequestModelTests
|
|||||||
|
|
||||||
Assert.Single(results);
|
Assert.Single(results);
|
||||||
Assert.Contains(nameof(model.Configuration), results[0].MemberNames);
|
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]
|
[Fact]
|
||||||
|
|||||||
Reference in New Issue
Block a user