mirror of
https://github.com/bitwarden/server.git
synced 2025-07-02 00:22:50 -05:00
Migrate deprecated Microsoft.Azure.Storage.Blob to Azure.Storage.Blobs (#1732)
* Migrate from deprecated Microsoft.Azure.Storage to Azure.Storage.Blobs * Remove and order usings * Do not fetch BlobProperties before uploading a new file. * Save an api call by calling GetPropertiesAsync and catching an error instead of calling Exists first * Formatted files * Verified ContentLength is the correct blob property for file-size * Use a generic Exception catch for file validation * Added a catch all to the GetBlobCertificateAsync in case something throws * Remove and sort using * Changes after running dotnet-format * Remove checks for CanGenerateSasUri
This commit is contained in:

committed by
GitHub

parent
bb34de74cb
commit
355bf2127b
@ -10,10 +10,11 @@ using System.Security.Cryptography.X509Certificates;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
using Azure.Storage.Queues;
|
||||
using Azure;
|
||||
using Azure.Storage.Blobs;
|
||||
using Azure.Storage.Blobs.Models;
|
||||
using Azure.Storage.Queues.Models;
|
||||
using Bit.Core.Context;
|
||||
using Bit.Core.Enums;
|
||||
@ -24,8 +25,6 @@ using Bit.Core.Settings;
|
||||
using Dapper;
|
||||
using IdentityModel;
|
||||
using Microsoft.AspNetCore.DataProtection;
|
||||
using Microsoft.Azure.Storage;
|
||||
using Microsoft.Azure.Storage.Blob;
|
||||
using MimeKit;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
@ -253,22 +252,27 @@ namespace Bit.Core.Utilities
|
||||
}
|
||||
}
|
||||
|
||||
public async static Task<X509Certificate2> GetBlobCertificateAsync(CloudStorageAccount cloudStorageAccount,
|
||||
string container, string file, string password)
|
||||
public async static Task<X509Certificate2> GetBlobCertificateAsync(string connectionString, string container, string file, string password)
|
||||
{
|
||||
var blobClient = cloudStorageAccount.CreateCloudBlobClient();
|
||||
var containerRef = blobClient.GetContainerReference(container);
|
||||
if (await containerRef.ExistsAsync().ConfigureAwait(false))
|
||||
try
|
||||
{
|
||||
var blobRef = containerRef.GetBlobReference(file);
|
||||
if (await blobRef.ExistsAsync().ConfigureAwait(false))
|
||||
{
|
||||
var blobBytes = new byte[blobRef.Properties.Length];
|
||||
await blobRef.DownloadToByteArrayAsync(blobBytes, 0).ConfigureAwait(false);
|
||||
return new X509Certificate2(blobBytes, password);
|
||||
}
|
||||
var blobServiceClient = new BlobServiceClient(connectionString);
|
||||
var containerRef2 = blobServiceClient.GetBlobContainerClient(container);
|
||||
var blobRef = containerRef2.GetBlobClient(file);
|
||||
|
||||
using var memStream = new MemoryStream();
|
||||
await blobRef.DownloadToAsync(memStream).ConfigureAwait(false);
|
||||
return new X509Certificate2(memStream.ToArray(), password);
|
||||
}
|
||||
catch (RequestFailedException ex)
|
||||
when (ex.ErrorCode == BlobErrorCode.ContainerNotFound || ex.ErrorCode == BlobErrorCode.BlobNotFound)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static long ToEpocMilliseconds(DateTime date)
|
||||
@ -756,8 +760,7 @@ namespace Bit.Core.Utilities
|
||||
SettingHasValue(globalSettings.Storage?.ConnectionString) &&
|
||||
SettingHasValue(globalSettings.IdentityServer.CertificatePassword))
|
||||
{
|
||||
var storageAccount = CloudStorageAccount.Parse(globalSettings.Storage.ConnectionString);
|
||||
return GetBlobCertificateAsync(storageAccount, "certificates",
|
||||
return GetBlobCertificateAsync(globalSettings.Storage.ConnectionString, "certificates",
|
||||
"identity.pfx", globalSettings.IdentityServer.CertificatePassword).GetAwaiter().GetResult();
|
||||
}
|
||||
return null;
|
||||
|
Reference in New Issue
Block a user