2025-04-23 10:44:43 -04:00
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
2025-07-01 08:52:38 -04:00
|
|
|
|
using System.Text.Json;
|
2025-04-23 10:44:43 -04:00
|
|
|
|
using Bit.Core.AdminConsole.Entities;
|
2025-07-01 08:52:38 -04:00
|
|
|
|
using Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
2025-04-23 10:44:43 -04:00
|
|
|
|
using Bit.Core.Enums;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Bit.Api.AdminConsole.Models.Request.Organizations;
|
|
|
|
|
|
|
|
|
|
|
|
public class OrganizationIntegrationRequestModel : IValidatableObject
|
|
|
|
|
|
{
|
|
|
|
|
|
public string? Configuration { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
public IntegrationType Type { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
public OrganizationIntegration ToOrganizationIntegration(Guid organizationId)
|
|
|
|
|
|
{
|
|
|
|
|
|
return new OrganizationIntegration()
|
|
|
|
|
|
{
|
|
|
|
|
|
OrganizationId = organizationId,
|
|
|
|
|
|
Configuration = Configuration,
|
|
|
|
|
|
Type = Type,
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public OrganizationIntegration ToOrganizationIntegration(OrganizationIntegration currentIntegration)
|
|
|
|
|
|
{
|
|
|
|
|
|
currentIntegration.Configuration = Configuration;
|
|
|
|
|
|
return currentIntegration;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
|
|
|
|
|
|
{
|
|
|
|
|
|
switch (Type)
|
|
|
|
|
|
{
|
|
|
|
|
|
case IntegrationType.CloudBillingSync or IntegrationType.Scim:
|
|
|
|
|
|
yield return new ValidationResult($"{nameof(Type)} integrations are not yet supported.", new[] { nameof(Type) });
|
|
|
|
|
|
break;
|
|
|
|
|
|
case IntegrationType.Slack:
|
|
|
|
|
|
yield return new ValidationResult($"{nameof(Type)} integrations cannot be created directly.", new[] { nameof(Type) });
|
|
|
|
|
|
break;
|
|
|
|
|
|
case IntegrationType.Webhook:
|
2025-07-01 08:52:38 -04:00
|
|
|
|
if (string.IsNullOrWhiteSpace(Configuration))
|
|
|
|
|
|
{
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!IsIntegrationValid<WebhookIntegration>())
|
2025-04-23 10:44:43 -04:00
|
|
|
|
{
|
|
|
|
|
|
yield return new ValidationResult(
|
2025-07-01 08:52:38 -04:00
|
|
|
|
"Webhook integrations must include valid configuration.",
|
|
|
|
|
|
new[] { nameof(Configuration) });
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case IntegrationType.Hec:
|
|
|
|
|
|
if (!IsIntegrationValid<HecIntegration>())
|
|
|
|
|
|
{
|
|
|
|
|
|
yield return new ValidationResult(
|
|
|
|
|
|
"HEC integrations must include valid configuration.",
|
2025-04-23 10:44:43 -04:00
|
|
|
|
new[] { nameof(Configuration) });
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
2025-09-08 12:39:59 -04:00
|
|
|
|
case IntegrationType.Datadog:
|
|
|
|
|
|
if (!IsIntegrationValid<DatadogIntegration>())
|
|
|
|
|
|
{
|
|
|
|
|
|
yield return new ValidationResult(
|
|
|
|
|
|
"Datadog integrations must include valid configuration.",
|
|
|
|
|
|
new[] { nameof(Configuration) });
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
2025-04-23 10:44:43 -04:00
|
|
|
|
default:
|
|
|
|
|
|
yield return new ValidationResult(
|
|
|
|
|
|
$"Integration type '{Type}' is not recognized.",
|
|
|
|
|
|
new[] { nameof(Type) });
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-07-01 08:52:38 -04:00
|
|
|
|
|
|
|
|
|
|
private bool IsIntegrationValid<T>()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(Configuration))
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var config = JsonSerializer.Deserialize<T>(Configuration);
|
|
|
|
|
|
return config is not null;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-04-23 10:44:43 -04:00
|
|
|
|
}
|