diff --git a/src/CryptoAgent/Migrations/MySql/20210817205815_InitialCreate.Designer.cs b/src/CryptoAgent/Migrations/MySql/20210820155954_InitialCreate.Designer.cs similarity index 89% rename from src/CryptoAgent/Migrations/MySql/20210817205815_InitialCreate.Designer.cs rename to src/CryptoAgent/Migrations/MySql/20210820155954_InitialCreate.Designer.cs index 7f1b841..1ad903d 100644 --- a/src/CryptoAgent/Migrations/MySql/20210817205815_InitialCreate.Designer.cs +++ b/src/CryptoAgent/Migrations/MySql/20210820155954_InitialCreate.Designer.cs @@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace Bit.CryptoAgent.Migrations.MySql { [DbContext(typeof(MySqlDatabaseContext))] - [Migration("20210817205815_InitialCreate")] + [Migration("20210820155954_InitialCreate")] partial class InitialCreate { protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -21,9 +21,15 @@ namespace Bit.CryptoAgent.Migrations.MySql modelBuilder.Entity("Bit.CryptoAgent.Repositories.EntityFramework.ApplicationData", b => { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + b.Property("SymmetricKey") .HasColumnType("longtext"); + b.HasKey("Id"); + b.ToTable("ApplicationDatas"); }); diff --git a/src/CryptoAgent/Migrations/MySql/20210817205815_InitialCreate.cs b/src/CryptoAgent/Migrations/MySql/20210820155954_InitialCreate.cs similarity index 86% rename from src/CryptoAgent/Migrations/MySql/20210817205815_InitialCreate.cs rename to src/CryptoAgent/Migrations/MySql/20210820155954_InitialCreate.cs index 4ee99a6..3836464 100644 --- a/src/CryptoAgent/Migrations/MySql/20210817205815_InitialCreate.cs +++ b/src/CryptoAgent/Migrations/MySql/20210820155954_InitialCreate.cs @@ -1,4 +1,5 @@ using System; +using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Migrations; namespace Bit.CryptoAgent.Migrations.MySql @@ -14,11 +15,14 @@ namespace Bit.CryptoAgent.Migrations.MySql name: "ApplicationDatas", columns: table => new { + Id = table.Column(type: "int", nullable: false) + .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn), SymmetricKey = table.Column(type: "longtext", nullable: true) .Annotation("MySql:CharSet", "utf8mb4") }, constraints: table => { + table.PrimaryKey("PK_ApplicationDatas", x => x.Id); }) .Annotation("MySql:CharSet", "utf8mb4"); diff --git a/src/CryptoAgent/Migrations/MySql/MySqlDatabaseContextModelSnapshot.cs b/src/CryptoAgent/Migrations/MySql/MySqlDatabaseContextModelSnapshot.cs index 7e085af..6dbe314 100644 --- a/src/CryptoAgent/Migrations/MySql/MySqlDatabaseContextModelSnapshot.cs +++ b/src/CryptoAgent/Migrations/MySql/MySqlDatabaseContextModelSnapshot.cs @@ -19,9 +19,15 @@ namespace Bit.CryptoAgent.Migrations.MySql modelBuilder.Entity("Bit.CryptoAgent.Repositories.EntityFramework.ApplicationData", b => { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + b.Property("SymmetricKey") .HasColumnType("longtext"); + b.HasKey("Id"); + b.ToTable("ApplicationDatas"); }); diff --git a/src/CryptoAgent/Migrations/PostgreSql/20210817205827_InitialCreate.Designer.cs b/src/CryptoAgent/Migrations/PostgreSql/20210820160016_InitialCreate.Designer.cs similarity index 85% rename from src/CryptoAgent/Migrations/PostgreSql/20210817205827_InitialCreate.Designer.cs rename to src/CryptoAgent/Migrations/PostgreSql/20210820160016_InitialCreate.Designer.cs index 8951f0c..9ceb1d8 100644 --- a/src/CryptoAgent/Migrations/PostgreSql/20210817205827_InitialCreate.Designer.cs +++ b/src/CryptoAgent/Migrations/PostgreSql/20210820160016_InitialCreate.Designer.cs @@ -10,7 +10,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace Bit.CryptoAgent.Migrations.PostgreSql { [DbContext(typeof(PostgreSqlDatabaseContext))] - [Migration("20210817205827_InitialCreate")] + [Migration("20210820160016_InitialCreate")] partial class InitialCreate { protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -23,9 +23,16 @@ namespace Bit.CryptoAgent.Migrations.PostgreSql modelBuilder.Entity("Bit.CryptoAgent.Repositories.EntityFramework.ApplicationData", b => { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); + b.Property("SymmetricKey") .HasColumnType("text"); + b.HasKey("Id"); + b.ToTable("ApplicationDatas"); }); diff --git a/src/CryptoAgent/Migrations/PostgreSql/20210817205827_InitialCreate.cs b/src/CryptoAgent/Migrations/PostgreSql/20210820160016_InitialCreate.cs similarity index 83% rename from src/CryptoAgent/Migrations/PostgreSql/20210817205827_InitialCreate.cs rename to src/CryptoAgent/Migrations/PostgreSql/20210820160016_InitialCreate.cs index 75da7ee..e9463b9 100644 --- a/src/CryptoAgent/Migrations/PostgreSql/20210817205827_InitialCreate.cs +++ b/src/CryptoAgent/Migrations/PostgreSql/20210820160016_InitialCreate.cs @@ -1,5 +1,6 @@ using System; using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace Bit.CryptoAgent.Migrations.PostgreSql { @@ -11,10 +12,13 @@ namespace Bit.CryptoAgent.Migrations.PostgreSql name: "ApplicationDatas", columns: table => new { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), SymmetricKey = table.Column(type: "text", nullable: true) }, constraints: table => { + table.PrimaryKey("PK_ApplicationDatas", x => x.Id); }); migrationBuilder.CreateTable( diff --git a/src/CryptoAgent/Migrations/PostgreSql/PostgreSqlDatabaseContextModelSnapshot.cs b/src/CryptoAgent/Migrations/PostgreSql/PostgreSqlDatabaseContextModelSnapshot.cs index ca38e84..2acc427 100644 --- a/src/CryptoAgent/Migrations/PostgreSql/PostgreSqlDatabaseContextModelSnapshot.cs +++ b/src/CryptoAgent/Migrations/PostgreSql/PostgreSqlDatabaseContextModelSnapshot.cs @@ -21,9 +21,16 @@ namespace Bit.CryptoAgent.Migrations.PostgreSql modelBuilder.Entity("Bit.CryptoAgent.Repositories.EntityFramework.ApplicationData", b => { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); + b.Property("SymmetricKey") .HasColumnType("text"); + b.HasKey("Id"); + b.ToTable("ApplicationDatas"); }); diff --git a/src/CryptoAgent/Migrations/SqlServer/20210817205227_InitialCreate.Designer.cs b/src/CryptoAgent/Migrations/SqlServer/20210820154908_InitialCreate.Designer.cs similarity index 85% rename from src/CryptoAgent/Migrations/SqlServer/20210817205227_InitialCreate.Designer.cs rename to src/CryptoAgent/Migrations/SqlServer/20210820154908_InitialCreate.Designer.cs index 9bbdd9d..833c54a 100644 --- a/src/CryptoAgent/Migrations/SqlServer/20210817205227_InitialCreate.Designer.cs +++ b/src/CryptoAgent/Migrations/SqlServer/20210820154908_InitialCreate.Designer.cs @@ -10,7 +10,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace Bit.CryptoAgent.Migrations.SqlServer { [DbContext(typeof(SqlServerDatabaseContext))] - [Migration("20210817205227_InitialCreate")] + [Migration("20210820154908_InitialCreate")] partial class InitialCreate { protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -23,9 +23,16 @@ namespace Bit.CryptoAgent.Migrations.SqlServer modelBuilder.Entity("Bit.CryptoAgent.Repositories.EntityFramework.ApplicationData", b => { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + b.Property("SymmetricKey") .HasColumnType("nvarchar(max)"); + b.HasKey("Id"); + b.ToTable("ApplicationDatas"); }); diff --git a/src/CryptoAgent/Migrations/SqlServer/20210817205227_InitialCreate.cs b/src/CryptoAgent/Migrations/SqlServer/20210820154908_InitialCreate.cs similarity index 88% rename from src/CryptoAgent/Migrations/SqlServer/20210817205227_InitialCreate.cs rename to src/CryptoAgent/Migrations/SqlServer/20210820154908_InitialCreate.cs index 9190761..ad7ea28 100644 --- a/src/CryptoAgent/Migrations/SqlServer/20210817205227_InitialCreate.cs +++ b/src/CryptoAgent/Migrations/SqlServer/20210820154908_InitialCreate.cs @@ -11,10 +11,13 @@ namespace Bit.CryptoAgent.Migrations.SqlServer name: "ApplicationDatas", columns: table => new { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), SymmetricKey = table.Column(type: "nvarchar(max)", nullable: true) }, constraints: table => { + table.PrimaryKey("PK_ApplicationDatas", x => x.Id); }); migrationBuilder.CreateTable( diff --git a/src/CryptoAgent/Migrations/SqlServer/SqlServerDatabaseContextModelSnapshot.cs b/src/CryptoAgent/Migrations/SqlServer/SqlServerDatabaseContextModelSnapshot.cs index 47639f9..bcc92d2 100644 --- a/src/CryptoAgent/Migrations/SqlServer/SqlServerDatabaseContextModelSnapshot.cs +++ b/src/CryptoAgent/Migrations/SqlServer/SqlServerDatabaseContextModelSnapshot.cs @@ -21,9 +21,16 @@ namespace Bit.CryptoAgent.Migrations.SqlServer modelBuilder.Entity("Bit.CryptoAgent.Repositories.EntityFramework.ApplicationData", b => { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + b.Property("SymmetricKey") .HasColumnType("nvarchar(max)"); + b.HasKey("Id"); + b.ToTable("ApplicationDatas"); }); diff --git a/src/CryptoAgent/Migrations/Sqlite/20210817205807_InitialCreate.Designer.cs b/src/CryptoAgent/Migrations/Sqlite/20210820155938_InitialCreate.Designer.cs similarity index 88% rename from src/CryptoAgent/Migrations/Sqlite/20210817205807_InitialCreate.Designer.cs rename to src/CryptoAgent/Migrations/Sqlite/20210820155938_InitialCreate.Designer.cs index f2e645b..945ce68 100644 --- a/src/CryptoAgent/Migrations/Sqlite/20210817205807_InitialCreate.Designer.cs +++ b/src/CryptoAgent/Migrations/Sqlite/20210820155938_InitialCreate.Designer.cs @@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace Bit.CryptoAgent.Migrations.Sqlite { [DbContext(typeof(SqliteDatabaseContext))] - [Migration("20210817205807_InitialCreate")] + [Migration("20210820155938_InitialCreate")] partial class InitialCreate { protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -20,9 +20,15 @@ namespace Bit.CryptoAgent.Migrations.Sqlite modelBuilder.Entity("Bit.CryptoAgent.Repositories.EntityFramework.ApplicationData", b => { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + b.Property("SymmetricKey") .HasColumnType("TEXT"); + b.HasKey("Id"); + b.ToTable("ApplicationDatas"); }); diff --git a/src/CryptoAgent/Migrations/Sqlite/20210817205807_InitialCreate.cs b/src/CryptoAgent/Migrations/Sqlite/20210820155938_InitialCreate.cs similarity index 87% rename from src/CryptoAgent/Migrations/Sqlite/20210817205807_InitialCreate.cs rename to src/CryptoAgent/Migrations/Sqlite/20210820155938_InitialCreate.cs index 972a292..96d17f9 100644 --- a/src/CryptoAgent/Migrations/Sqlite/20210817205807_InitialCreate.cs +++ b/src/CryptoAgent/Migrations/Sqlite/20210820155938_InitialCreate.cs @@ -11,10 +11,13 @@ namespace Bit.CryptoAgent.Migrations.Sqlite name: "ApplicationDatas", columns: table => new { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), SymmetricKey = table.Column(type: "TEXT", nullable: true) }, constraints: table => { + table.PrimaryKey("PK_ApplicationDatas", x => x.Id); }); migrationBuilder.CreateTable( diff --git a/src/CryptoAgent/Migrations/Sqlite/SqliteDatabaseContextModelSnapshot.cs b/src/CryptoAgent/Migrations/Sqlite/SqliteDatabaseContextModelSnapshot.cs index bd604ea..993391c 100644 --- a/src/CryptoAgent/Migrations/Sqlite/SqliteDatabaseContextModelSnapshot.cs +++ b/src/CryptoAgent/Migrations/Sqlite/SqliteDatabaseContextModelSnapshot.cs @@ -18,9 +18,15 @@ namespace Bit.CryptoAgent.Migrations.Sqlite modelBuilder.Entity("Bit.CryptoAgent.Repositories.EntityFramework.ApplicationData", b => { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + b.Property("SymmetricKey") .HasColumnType("TEXT"); + b.HasKey("Id"); + b.ToTable("ApplicationDatas"); }); diff --git a/src/CryptoAgent/Repositories/EntityFramework/ApplicationDataRepository.cs b/src/CryptoAgent/Repositories/EntityFramework/ApplicationDataRepository.cs index ff8b6d7..2bc3b55 100644 --- a/src/CryptoAgent/Repositories/EntityFramework/ApplicationDataRepository.cs +++ b/src/CryptoAgent/Repositories/EntityFramework/ApplicationDataRepository.cs @@ -14,14 +14,15 @@ namespace Bit.CryptoAgent.Repositories.EntityFramework { using var scope = ServiceScopeFactory.CreateScope(); var dbContext = GetDatabaseContext(scope); - return Task.FromResult(dbContext.ApplicationDatas.FirstOrDefault().SymmetricKey); + return Task.FromResult(dbContext.ApplicationDatas.FirstOrDefault()?.SymmetricKey); } public async Task UpdateSymmetricKeyAsync(string key) { using var scope = ServiceScopeFactory.CreateScope(); var dbContext = GetDatabaseContext(scope); - if (dbContext.ApplicationDatas.FirstOrDefault() == null) + var data = dbContext.ApplicationDatas.FirstOrDefault(); + if (data == null) { await dbContext.AddAsync(new ApplicationData { @@ -30,7 +31,7 @@ namespace Bit.CryptoAgent.Repositories.EntityFramework } else { - dbContext.ApplicationDatas.FirstOrDefault().SymmetricKey = key; + data.SymmetricKey = key; } await dbContext.SaveChangesAsync(); } diff --git a/src/CryptoAgent/Repositories/EntityFramework/DatabaseContext.cs b/src/CryptoAgent/Repositories/EntityFramework/DatabaseContext.cs index f22f6c8..3e72c8a 100644 --- a/src/CryptoAgent/Repositories/EntityFramework/DatabaseContext.cs +++ b/src/CryptoAgent/Repositories/EntityFramework/DatabaseContext.cs @@ -63,9 +63,9 @@ namespace Bit.CryptoAgent.Repositories.EntityFramework ServerVersion.AutoDetect(_settings.Database.MySqlConnectionString)); } - [Keyless] public class ApplicationData { + public int Id { get; set; } public string SymmetricKey { get; set; } } diff --git a/src/CryptoAgent/Repositories/EntityFramework/UserKeyRepository.cs b/src/CryptoAgent/Repositories/EntityFramework/UserKeyRepository.cs index c1ac113..b2d5bf5 100644 --- a/src/CryptoAgent/Repositories/EntityFramework/UserKeyRepository.cs +++ b/src/CryptoAgent/Repositories/EntityFramework/UserKeyRepository.cs @@ -25,7 +25,7 @@ namespace Bit.CryptoAgent.Repositories.EntityFramework using var scope = ServiceScopeFactory.CreateScope(); var dbContext = GetDatabaseContext(scope); var entity = await dbContext.UserKeys.FindAsync(id); - return entity.ToUserKeyModel(); + return entity?.ToUserKeyModel(); } public virtual async Task UpdateAsync(UserKeyModel item) diff --git a/src/CryptoAgent/Repositories/Mongo/ApplicationDataRepository.cs b/src/CryptoAgent/Repositories/Mongo/ApplicationDataRepository.cs index 8afd742..9be2f13 100644 --- a/src/CryptoAgent/Repositories/Mongo/ApplicationDataRepository.cs +++ b/src/CryptoAgent/Repositories/Mongo/ApplicationDataRepository.cs @@ -14,12 +14,16 @@ namespace Bit.CryptoAgent.Repositories.Mongo public async Task ReadSymmetricKeyAsync() { var document = await Collection.Find(new BsonDocument()).FirstOrDefaultAsync(); - return document.SymmetricKey; + return document?.SymmetricKey; } public async Task UpdateSymmetricKeyAsync(string key) { var document = await Collection.Find(new BsonDocument()).FirstOrDefaultAsync(); + if (document == null) + { + document = new ApplicationData(); + } document.SymmetricKey = key; await Collection.ReplaceOneAsync(d => d.Id == document.Id, document, new ReplaceOptions { diff --git a/src/CryptoAgent/Services/AwsKmsRsaKeyService.cs b/src/CryptoAgent/Services/AwsKmsRsaKeyService.cs index 0009f17..1cd32ad 100644 --- a/src/CryptoAgent/Services/AwsKmsRsaKeyService.cs +++ b/src/CryptoAgent/Services/AwsKmsRsaKeyService.cs @@ -83,7 +83,7 @@ namespace Bit.CryptoAgent.Services var response = await _kmsClient.GetPublicKeyAsync(request); var rsa = RSA.Create(); rsa.ImportSubjectPublicKeyInfo(response.PublicKey.ToArray(), out _); - return rsa.ExportRSAPublicKey(); + return rsa.ExportSubjectPublicKeyInfo(); } } } diff --git a/src/CryptoAgent/Services/AzureKeyVaultRsaKeyService.cs b/src/CryptoAgent/Services/AzureKeyVaultRsaKeyService.cs index 6edc13a..291916b 100644 --- a/src/CryptoAgent/Services/AzureKeyVaultRsaKeyService.cs +++ b/src/CryptoAgent/Services/AzureKeyVaultRsaKeyService.cs @@ -53,7 +53,7 @@ namespace Bit.CryptoAgent.Services public async Task GetPublicKeyAsync() { var key = await GetKeyAsync(); - return key.Key.ToRSA().ExportRSAPublicKey(); + return key.Key.ToRSA().ExportSubjectPublicKeyInfo(); } private async Task GetCryptographyClientAsync() diff --git a/src/CryptoAgent/Services/GoogleCloudKmsRsaKeyService.cs b/src/CryptoAgent/Services/GoogleCloudKmsRsaKeyService.cs index 3d8d167..0a585cf 100644 --- a/src/CryptoAgent/Services/GoogleCloudKmsRsaKeyService.cs +++ b/src/CryptoAgent/Services/GoogleCloudKmsRsaKeyService.cs @@ -61,7 +61,7 @@ namespace Bit.CryptoAgent.Services public async Task GetPublicKeyAsync() { var rsa = await GetRsaPublicKeyAsync(); - return rsa.ExportRSAPublicKey(); + return rsa.ExportSubjectPublicKeyInfo(); } private async Task GetRsaPublicKeyAsync() diff --git a/src/CryptoAgent/Services/LocalCertificateRsaKeyService.cs b/src/CryptoAgent/Services/LocalCertificateRsaKeyService.cs index 8761295..ebd2acf 100644 --- a/src/CryptoAgent/Services/LocalCertificateRsaKeyService.cs +++ b/src/CryptoAgent/Services/LocalCertificateRsaKeyService.cs @@ -59,7 +59,7 @@ namespace Bit.CryptoAgent.Services public async Task GetPublicKeyAsync() { var certificate = await GetCertificateAsync(); - return certificate.GetRSAPublicKey().ExportRSAPublicKey(); + return certificate.GetRSAPublicKey().ExportSubjectPublicKeyInfo(); } private async Task GetCertificateAsync()