mirror of
https://github.com/bitwarden/server
synced 2025-12-23 19:53:40 +00:00
[SM-394] Secrets Manager (#2164)
Long lived feature branch for Secrets Manager Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com> Co-authored-by: cd-bitwarden <106776772+cd-bitwarden@users.noreply.github.com> Co-authored-by: CarleyDiaz-Bitwarden <103955722+CarleyDiaz-Bitwarden@users.noreply.github.com> Co-authored-by: Thomas Avery <tavery@bitwarden.com> Co-authored-by: Colton Hurst <colton@coltonhurst.com>
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Utilities;
|
||||
|
||||
namespace Bit.Api.SecretManagerFeatures.Models.Request;
|
||||
|
||||
public class AccessTokenCreateRequestModel
|
||||
{
|
||||
[Required]
|
||||
[EncryptedString]
|
||||
[EncryptedStringLength(200)]
|
||||
public string Name { get; set; }
|
||||
|
||||
[Required]
|
||||
[EncryptedString]
|
||||
[EncryptedStringLength(4000)]
|
||||
public string EncryptedPayload { get; set; }
|
||||
|
||||
[Required]
|
||||
[EncryptedString]
|
||||
public string Key { get; set; }
|
||||
|
||||
public DateTime? ExpireAt { get; set; }
|
||||
|
||||
public ApiKey ToApiKey(Guid serviceAccountId)
|
||||
{
|
||||
return new ApiKey()
|
||||
{
|
||||
ServiceAccountId = serviceAccountId,
|
||||
Name = Name,
|
||||
Key = Key,
|
||||
ExpireAt = ExpireAt,
|
||||
Scope = "[\"api.secrets\"]",
|
||||
EncryptedPayload = EncryptedPayload,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Utilities;
|
||||
|
||||
namespace Bit.Api.SecretManagerFeatures.Models.Request;
|
||||
|
||||
public class ProjectCreateRequestModel
|
||||
{
|
||||
[Required]
|
||||
[EncryptedString]
|
||||
public string Name { get; set; }
|
||||
|
||||
public Project ToProject(Guid organizationId)
|
||||
{
|
||||
return new Project()
|
||||
{
|
||||
OrganizationId = organizationId,
|
||||
Name = Name,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Utilities;
|
||||
|
||||
namespace Bit.Api.SecretManagerFeatures.Models.Request;
|
||||
|
||||
public class ProjectUpdateRequestModel
|
||||
{
|
||||
[Required]
|
||||
[EncryptedString]
|
||||
public string Name { get; set; }
|
||||
|
||||
public Project ToProject(Guid id)
|
||||
{
|
||||
return new Project()
|
||||
{
|
||||
Id = id,
|
||||
Name = Name,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Utilities;
|
||||
|
||||
namespace Bit.Api.SecretManagerFeatures.Models.Request;
|
||||
|
||||
public class SecretCreateRequestModel
|
||||
{
|
||||
[Required]
|
||||
[EncryptedString]
|
||||
public string Key { get; set; }
|
||||
|
||||
[Required]
|
||||
[EncryptedString]
|
||||
public string Value { get; set; }
|
||||
|
||||
[Required]
|
||||
[EncryptedString]
|
||||
public string Note { get; set; }
|
||||
|
||||
public Guid[] ProjectIds { get; set; }
|
||||
|
||||
public Secret ToSecret(Guid organizationId)
|
||||
{
|
||||
return new Secret()
|
||||
{
|
||||
OrganizationId = organizationId,
|
||||
Key = Key,
|
||||
Value = Value,
|
||||
Note = Note,
|
||||
DeletedDate = null,
|
||||
Projects = ProjectIds != null && ProjectIds.Any() ? ProjectIds.Select(x => new Project() { Id = x }).ToList() : null,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Utilities;
|
||||
|
||||
namespace Bit.Api.SecretManagerFeatures.Models.Request;
|
||||
|
||||
public class SecretUpdateRequestModel
|
||||
{
|
||||
[Required]
|
||||
[EncryptedString]
|
||||
public string Key { get; set; }
|
||||
|
||||
[Required]
|
||||
[EncryptedString]
|
||||
public string Value { get; set; }
|
||||
|
||||
[Required]
|
||||
[EncryptedString]
|
||||
public string Note { get; set; }
|
||||
|
||||
public Guid[] ProjectIds { get; set; }
|
||||
|
||||
public Secret ToSecret(Guid id)
|
||||
{
|
||||
return new Secret()
|
||||
{
|
||||
Id = id,
|
||||
Key = Key,
|
||||
Value = Value,
|
||||
Note = Note,
|
||||
DeletedDate = null,
|
||||
Projects = ProjectIds != null && ProjectIds.Any() ? ProjectIds.Select(x => new Project() { Id = x }).ToList() : null,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Utilities;
|
||||
|
||||
namespace Bit.Api.SecretManagerFeatures.Models.Request;
|
||||
|
||||
public class ServiceAccountUpdateRequestModel
|
||||
{
|
||||
[Required]
|
||||
[EncryptedString]
|
||||
public string Name { get; set; }
|
||||
|
||||
public ServiceAccount ToServiceAccount(Guid id)
|
||||
{
|
||||
return new ServiceAccount()
|
||||
{
|
||||
Id = id,
|
||||
Name = Name,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Utilities;
|
||||
|
||||
namespace Bit.Api.SecretManagerFeatures.Models.Request;
|
||||
|
||||
public class ServiceAccountCreateRequestModel
|
||||
{
|
||||
[Required]
|
||||
[EncryptedString]
|
||||
public string Name { get; set; }
|
||||
|
||||
public ServiceAccount ToServiceAccount(Guid organizationId)
|
||||
{
|
||||
return new ServiceAccount()
|
||||
{
|
||||
OrganizationId = organizationId,
|
||||
Name = Name,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#nullable enable
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Models.Api;
|
||||
|
||||
namespace Bit.Api.SecretManagerFeatures.Models.Response;
|
||||
|
||||
public class AccessTokenCreationResponseModel : ResponseModel
|
||||
{
|
||||
public AccessTokenCreationResponseModel(ApiKey apiKey, string obj = "accessTokenCreation") : base(obj)
|
||||
{
|
||||
Id = apiKey.Id;
|
||||
Name = apiKey.Name;
|
||||
ClientSecret = apiKey.ClientSecret;
|
||||
ExpireAt = apiKey.ExpireAt;
|
||||
CreationDate = apiKey.CreationDate;
|
||||
RevisionDate = apiKey.RevisionDate;
|
||||
}
|
||||
|
||||
public Guid Id { get; }
|
||||
public string Name { get; }
|
||||
public string ClientSecret { get; }
|
||||
public DateTime? ExpireAt { get; }
|
||||
public DateTime CreationDate { get; }
|
||||
public DateTime RevisionDate { get; }
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#nullable enable
|
||||
using Bit.Core.Models.Api;
|
||||
|
||||
namespace Bit.Api.SecretManagerFeatures.Models.Response;
|
||||
|
||||
public class BulkDeleteResponseModel : ResponseModel
|
||||
{
|
||||
public BulkDeleteResponseModel(Guid id, string error, string obj = "BulkDeleteResponseModel") : base(obj)
|
||||
{
|
||||
Id = id;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(error))
|
||||
{
|
||||
Error = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
Error = error;
|
||||
}
|
||||
}
|
||||
|
||||
public Guid Id { get; set; }
|
||||
|
||||
public string? Error { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Models.Api;
|
||||
|
||||
namespace Bit.Api.SecretManagerFeatures.Models.Response;
|
||||
|
||||
public class ProjectResponseModel : ResponseModel
|
||||
{
|
||||
public ProjectResponseModel(Project project, string obj = "project")
|
||||
: base(obj)
|
||||
{
|
||||
if (project == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(project));
|
||||
}
|
||||
|
||||
Id = project.Id.ToString();
|
||||
OrganizationId = project.OrganizationId.ToString();
|
||||
Name = project.Name;
|
||||
CreationDate = project.CreationDate;
|
||||
RevisionDate = project.RevisionDate;
|
||||
}
|
||||
|
||||
public string Id { get; set; }
|
||||
|
||||
public string OrganizationId { get; set; }
|
||||
|
||||
public string Name { get; set; }
|
||||
|
||||
public DateTime CreationDate { get; set; }
|
||||
|
||||
public DateTime RevisionDate { get; set; }
|
||||
|
||||
public IEnumerable<Guid> Secrets { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Models.Api;
|
||||
|
||||
namespace Bit.Api.SecretManagerFeatures.Models.Response;
|
||||
|
||||
public class SecretResponseModel : ResponseModel
|
||||
{
|
||||
public SecretResponseModel(Secret secret, string obj = "secret")
|
||||
: base(obj)
|
||||
{
|
||||
if (secret == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(secret));
|
||||
}
|
||||
|
||||
Id = secret.Id.ToString();
|
||||
OrganizationId = secret.OrganizationId.ToString();
|
||||
Key = secret.Key;
|
||||
Value = secret.Value;
|
||||
Note = secret.Note;
|
||||
CreationDate = secret.CreationDate;
|
||||
RevisionDate = secret.RevisionDate;
|
||||
Projects = secret.Projects?.Select(p => new InnerProject(p));
|
||||
}
|
||||
|
||||
public string Id { get; set; }
|
||||
|
||||
public string OrganizationId { get; set; }
|
||||
|
||||
public string Key { get; set; }
|
||||
|
||||
public string Value { get; set; }
|
||||
|
||||
public string Note { get; set; }
|
||||
|
||||
public DateTime CreationDate { get; set; }
|
||||
|
||||
public DateTime RevisionDate { get; set; }
|
||||
|
||||
public IEnumerable<InnerProject> Projects { get; set; }
|
||||
|
||||
public class InnerProject
|
||||
{
|
||||
public InnerProject(Project project)
|
||||
{
|
||||
Id = project.Id;
|
||||
Name = project.Name;
|
||||
}
|
||||
|
||||
public Guid Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Models.Api;
|
||||
|
||||
namespace Bit.Api.SecretManagerFeatures.Models.Response;
|
||||
|
||||
public class SecretWithProjectsListResponseModel : ResponseModel
|
||||
{
|
||||
public SecretWithProjectsListResponseModel(IEnumerable<Secret> secrets, string obj = "SecretsWithProjectsList") : base(obj)
|
||||
{
|
||||
Secrets = secrets.Select(s => new InnerSecret(s));
|
||||
Projects = secrets.SelectMany(s => s.Projects).DistinctBy(p => p.Id).Select(p => new InnerProject(p));
|
||||
}
|
||||
|
||||
public IEnumerable<InnerSecret> Secrets { get; set; }
|
||||
public IEnumerable<InnerProject> Projects { get; set; }
|
||||
|
||||
public class InnerProject
|
||||
{
|
||||
public InnerProject(Project project)
|
||||
{
|
||||
Id = project.Id;
|
||||
Name = project.Name;
|
||||
}
|
||||
|
||||
public Guid Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
}
|
||||
|
||||
public class InnerSecret
|
||||
{
|
||||
public InnerSecret(Secret secret)
|
||||
{
|
||||
Id = secret.Id.ToString();
|
||||
OrganizationId = secret.OrganizationId.ToString();
|
||||
Key = secret.Key;
|
||||
CreationDate = secret.CreationDate;
|
||||
RevisionDate = secret.RevisionDate;
|
||||
Projects = secret.Projects?.Select(p => new InnerProject(p));
|
||||
}
|
||||
|
||||
public string Id { get; set; }
|
||||
|
||||
public string OrganizationId { get; set; }
|
||||
|
||||
public string Key { get; set; }
|
||||
|
||||
public DateTime CreationDate { get; set; }
|
||||
|
||||
public DateTime RevisionDate { get; set; }
|
||||
|
||||
public IEnumerable<InnerProject> Projects { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Models.Api;
|
||||
|
||||
namespace Bit.Api.SecretManagerFeatures.Models.Response;
|
||||
|
||||
public class ServiceAccountResponseModel : ResponseModel
|
||||
{
|
||||
public ServiceAccountResponseModel(ServiceAccount serviceAccount, string obj = "serviceAccount")
|
||||
: base(obj)
|
||||
{
|
||||
if (serviceAccount == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(serviceAccount));
|
||||
}
|
||||
|
||||
Id = serviceAccount.Id.ToString();
|
||||
OrganizationId = serviceAccount.OrganizationId.ToString();
|
||||
Name = serviceAccount.Name;
|
||||
CreationDate = serviceAccount.CreationDate;
|
||||
RevisionDate = serviceAccount.RevisionDate;
|
||||
}
|
||||
|
||||
public string Id { get; set; }
|
||||
|
||||
public string OrganizationId { get; set; }
|
||||
|
||||
public string Name { get; set; }
|
||||
|
||||
public DateTime CreationDate { get; set; }
|
||||
|
||||
public DateTime RevisionDate { get; set; }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user