Encrypt: fix

This commit is contained in:
pavelbannov 2020-09-16 16:32:46 +03:00
parent 94a5079503
commit 54cf80dafa
7 changed files with 57 additions and 42 deletions

View File

@ -34,7 +34,6 @@ using System.Web;
using ASC.Common.Logging;
using ASC.Core;
using ASC.Data.Storage.Configuration;
using ASC.Data.Storage.Encryption;
using ASC.Security.Cryptography;
using Microsoft.AspNetCore.Http;

View File

@ -104,13 +104,18 @@ namespace ASC.Data.Storage.DiscStorage
}
public override Stream GetReadStream(string domain, string path)
{
return GetReadStream(domain, path, true);
}
public Stream GetReadStream(string domain, string path, bool withDecription)
{
if (path == null) throw new ArgumentNullException("path");
var target = GetTarget(domain, path);
if (File.Exists(target))
{
return File.OpenRead(target);
return withDecription ? Crypt.GetReadStream(target) : File.OpenRead(target);
}
throw new FileNotFoundException("File not found", Path.GetFullPath(target));
}
@ -123,8 +128,8 @@ namespace ASC.Data.Storage.DiscStorage
if (File.Exists(target))
{
var stream = File.OpenRead(target);
if (0 < offset) stream.Seek(offset, SeekOrigin.Begin);
var stream = Crypt.GetReadStream(target);
if (0 < offset && stream.CanSeek) stream.Seek(offset, SeekOrigin.Begin);
return stream;
}
throw new FileNotFoundException("File not found", Path.GetFullPath(target));
@ -184,6 +189,8 @@ namespace ASC.Data.Storage.DiscStorage
QuotaUsedAdd(domain, fslen);
Crypt.EncryptFile(target);
return GetUri(domain, path);
}
@ -222,11 +229,21 @@ namespace ASC.Data.Storage.DiscStorage
public override Uri FinalizeChunkedUpload(string domain, string path, string uploadId, Dictionary<int, string> eTags)
{
var target = GetTarget(domain, path);
if (QuotaController != null)
{
var size = GetFileSize(domain, path);
if (!File.Exists(target))
{
throw new FileNotFoundException("file not found " + target);
}
var size = Crypt.GetFileSize(target);
QuotaUsedAdd(domain, size);
}
Crypt.EncryptFile(target);
return GetUri(domain, path);
}
@ -253,7 +270,7 @@ namespace ASC.Data.Storage.DiscStorage
if (File.Exists(target))
{
var size = new FileInfo(target).Length;
var size = Crypt.GetFileSize(target);
File.Delete(target);
QuotaUsedDelete(domain, size);
@ -275,7 +292,7 @@ namespace ASC.Data.Storage.DiscStorage
if (!File.Exists(target))
continue;
var size = new FileInfo(target).Length;
var size = Crypt.GetFileSize(target);
File.Delete(target);
QuotaUsedDelete(domain, size);
@ -318,8 +335,9 @@ namespace ASC.Data.Storage.DiscStorage
var fileInfo = new FileInfo(entry);
if (fileInfo.LastWriteTime >= fromDate && fileInfo.LastWriteTime <= toDate)
{
var size = Crypt.GetFileSize(entry);
File.Delete(entry);
QuotaUsedDelete(domain, fileInfo.Length);
QuotaUsedDelete(domain, size);
}
}
}
@ -356,7 +374,7 @@ namespace ASC.Data.Storage.DiscStorage
Directory.CreateDirectory(Path.GetDirectoryName(newtarget));
}
var flength = new FileInfo(target).Length;
var flength = Crypt.GetFileSize(target);
//Delete file if exists
if (File.Exists(newtarget))
@ -405,7 +423,7 @@ namespace ASC.Data.Storage.DiscStorage
if (!Directory.Exists(targetDir)) return;
var entries = Directory.GetFiles(targetDir, "*.*", SearchOption.AllDirectories);
var size = entries.Select(entry => new FileInfo(entry)).Select(info => info.Length).Sum();
var size = entries.Select(entry => Crypt.GetFileSize(entry)).Sum();
var subDirs = Directory.GetDirectories(targetDir, "*", SearchOption.AllDirectories).ToList();
subDirs.Reverse();
@ -422,7 +440,7 @@ namespace ASC.Data.Storage.DiscStorage
if (File.Exists(target))
{
return new FileInfo(target).Length;
return Crypt.GetFileSize(target);
}
throw new FileNotFoundException("file not found " + target);
}
@ -434,8 +452,8 @@ namespace ASC.Data.Storage.DiscStorage
if (Directory.Exists(target))
{
return Directory.GetFiles(target, "*.*", SearchOption.AllDirectories)
.Select(entry => new FileInfo(entry))
.Sum(info => info.Length);
.Select(entry => Crypt.GetFileSize(entry))
.Sum();
}
throw new FileNotFoundException("directory not found " + target);
@ -470,9 +488,10 @@ namespace ASC.Data.Storage.DiscStorage
var finfo = new FileInfo(entry);
if ((DateTime.UtcNow - finfo.CreationTimeUtc) > oldThreshold)
{
var size = Crypt.GetFileSize(entry);
File.Delete(entry);
QuotaUsedDelete(domain, finfo.Length);
QuotaUsedDelete(domain, size);
}
}
}
@ -559,7 +578,7 @@ namespace ASC.Data.Storage.DiscStorage
if (Directory.Exists(target))
{
var entries = Directory.GetFiles(target, "*.*", SearchOption.AllDirectories);
size = entries.Select(entry => new FileInfo(entry)).Select(info => info.Length).Sum();
size = entries.Select(entry => Crypt.GetFileSize(entry)).Sum();
}
return size;
}
@ -580,7 +599,7 @@ namespace ASC.Data.Storage.DiscStorage
File.Copy(target, newtarget, true);
var flength = new FileInfo(target).Length;
var flength = Crypt.GetFileSize(target);
QuotaUsedAdd(newdomain, flength);
}
else
@ -612,8 +631,10 @@ namespace ASC.Data.Storage.DiscStorage
// Copy each file into it's new directory.
foreach (var fi in source.GetFiles())
{
fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
QuotaUsedAdd(newdomain, fi.Length);
var fp = Path.Combine(target.ToString(), fi.Name);
fi.CopyTo(fp, true);
var size = Crypt.GetFileSize(fp);
QuotaUsedAdd(newdomain, size);
}
// Copy each subdirectory using recursion.
@ -637,18 +658,15 @@ namespace ASC.Data.Storage.DiscStorage
public Stream GetWriteStream(string domain, string path)
{
if (path == null) throw new ArgumentNullException("path");
var target = GetTarget(domain, path);
CreateDirectory(target);
return File.Open(target, FileMode.Create);
return GetWriteStream(domain, path, FileMode.Create);
}
public Stream GetWriteStream(string domain, string path, FileMode mode)
public Stream GetWriteStream(string domain, string path, FileMode fileMode)
{
if (path == null) throw new ArgumentNullException("path");
var target = GetTarget(domain, path);
CreateDirectory(target);
return File.Open(target, mode);
return File.Open(target, fileMode);
}
private static void CreateDirectory(string target)

View File

@ -25,12 +25,9 @@
using System;
using System.Configuration;
using System.IO;
using System.Security.Cryptography;
using ASC.Common;
using Microsoft.Extensions.Configuration;
namespace ASC.Data.Storage.Encryption

View File

@ -28,7 +28,6 @@ using System;
using ASC.Common;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace ASC.Data.Storage.Encryption

View File

@ -30,6 +30,7 @@ using System.Security.Cryptography;
using ASC.Common;
using ASC.Common.Caching;
using ASC.Core;
using ASC.Security.Cryptography;
namespace ASC.Data.Storage.Encryption
{
@ -69,11 +70,13 @@ namespace ASC.Data.Storage.Encryption
private CoreConfiguration CoreConfiguration { get; }
private AscCacheNotify AscCacheNotify { get; }
public InstanceCrypto InstanceCrypto { get; }
public EncryptionSettingsHelper(CoreConfiguration coreConfiguration, AscCacheNotify ascCacheNotify)
public EncryptionSettingsHelper(CoreConfiguration coreConfiguration, AscCacheNotify ascCacheNotify, InstanceCrypto instanceCrypto)
{
CoreConfiguration = coreConfiguration;
AscCacheNotify = ascCacheNotify;
InstanceCrypto = instanceCrypto;
}
public void Save(EncryptionSettings encryptionSettings)
@ -94,7 +97,7 @@ namespace ASC.Data.Storage.Encryption
public string Serialize(EncryptionSettings encryptionSettings)
{
return string.Join("#",
string.IsNullOrEmpty(encryptionSettings.password) ? string.Empty : Crypto.GetV(encryptionSettings.password, 1, true),
string.IsNullOrEmpty(encryptionSettings.password) ? string.Empty : InstanceCrypto.Encrypt(encryptionSettings.password),
(int)encryptionSettings.Status,
encryptionSettings.NotifyUsers
);
@ -109,7 +112,7 @@ namespace ASC.Data.Storage.Encryption
var parts = value.Split(new[] { '#' }, StringSplitOptions.None);
var password = string.IsNullOrEmpty(parts[0]) ? string.Empty : Crypto.GetV(parts[0], 1, false);
var password = string.IsNullOrEmpty(parts[0]) ? string.Empty : InstanceCrypto.Decrypt(parts[0]);
var status = int.Parse(parts[1]);
var notifyUsers = bool.Parse(parts[2]);
@ -193,8 +196,8 @@ namespace ASC.Data.Storage.Encryption
{
public static DIHelper AddEncryptionSettingsHelperService(this DIHelper services)
{
services.TryAddScoped<EncryptionSettingsHelper>();
services.TryAddScoped<AscCacheNotify>();
services.TryAddSingleton<EncryptionSettingsHelper>();
services.TryAddSingleton<AscCacheNotify>();
return services
.AddCoreConfigurationService();
}

View File

@ -25,7 +25,6 @@
using System;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Security.Cryptography;

View File

@ -333,7 +333,7 @@ namespace ASC.Data.Backup.Tasks
{
if (services.TryAddScoped<RestorePortalTask>())
{
services.TryAddScoped<AscCacheNotify>();
services.TryAddSingleton<AscCacheNotify>();
return services
.AddCoreConfigurationService()
.AddStorageFactoryService()