DocSpace-buildtools/web/ASC.Web.Api/Api/Settings/StorageController.cs

481 lines
15 KiB
C#
Raw Normal View History

2022-03-15 18:00:53 +00:00
// (c) Copyright Ascensio System SIA 2010-2022
//
// This program is a free software product.
// You can redistribute it and/or modify it under the terms
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
// Foundation. In accordance with Section 7(a) of the GNU AGPL its Section 15 shall be amended
// to the effect that Ascensio System SIA expressly excludes the warranty of non-infringement of
// any third-party rights.
//
// This program is distributed WITHOUT ANY WARRANTY, without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, see
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
//
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
//
// The interactive user interfaces in modified source and object code versions of the Program must
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
//
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
// trademark law for use of our trademarks.
//
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
using ASC.Data.Storage.Encryption.IntegrationEvents.Events;
using ASC.EventBus.Abstractions;
2022-03-15 18:00:53 +00:00
namespace ASC.Web.Api.Controllers.Settings;
2022-03-01 10:58:02 +00:00
2022-03-02 08:08:59 +00:00
public class StorageController : BaseSettingsController
2022-03-01 10:58:02 +00:00
{
2022-03-25 16:26:06 +00:00
private Tenant Tenant { get { return ApiContext.Tenant; } }
2022-03-01 10:58:02 +00:00
private readonly MessageService _messageService;
private readonly StudioNotifyService _studioNotifyService;
private readonly IWebHostEnvironment _webHostEnvironment;
private readonly ConsumerFactory _consumerFactory;
private readonly TenantManager _tenantManager;
private readonly TenantExtra _tenantExtra;
private readonly PermissionContext _permissionContext;
private readonly SettingsManager _settingsManager;
private readonly CoreBaseSettings _coreBaseSettings;
private readonly CommonLinkUtility _commonLinkUtility;
private readonly StorageSettingsHelper _storageSettingsHelper;
private readonly ServiceClient _serviceClient;
private readonly EncryptionSettingsHelper _encryptionSettingsHelper;
private readonly BackupAjaxHandler _backupAjaxHandler;
private readonly ICacheNotify<DeleteSchedule> _cacheDeleteSchedule;
private readonly EncryptionWorker _encryptionWorker;
private readonly ILogger _log;
private readonly IEventBus _eventBus;
private readonly ASC.Core.SecurityContext _securityContext;
2022-03-01 10:58:02 +00:00
public StorageController(
ILoggerProvider option,
2022-03-02 08:08:59 +00:00
ServiceClient serviceClient,
2022-03-01 10:58:02 +00:00
MessageService messageService,
ASC.Core.SecurityContext securityContext,
2022-03-01 10:58:02 +00:00
StudioNotifyService studioNotifyService,
ApiContext apiContext,
TenantManager tenantManager,
TenantExtra tenantExtra,
PermissionContext permissionContext,
SettingsManager settingsManager,
WebItemManager webItemManager,
CoreBaseSettings coreBaseSettings,
CommonLinkUtility commonLinkUtility,
StorageSettingsHelper storageSettingsHelper,
IWebHostEnvironment webHostEnvironment,
ConsumerFactory consumerFactory,
IMemoryCache memoryCache,
IEventBus eventBus,
2022-03-01 10:58:02 +00:00
EncryptionSettingsHelper encryptionSettingsHelper,
BackupAjaxHandler backupAjaxHandler,
ICacheNotify<DeleteSchedule> cacheDeleteSchedule,
2022-04-15 10:27:48 +00:00
EncryptionWorker encryptionWorker,
IHttpContextAccessor httpContextAccessor) : base(apiContext, memoryCache, webItemManager, httpContextAccessor)
2022-03-01 10:58:02 +00:00
{
_log = option.CreateLogger("ASC.Api");
_eventBus = eventBus;
2022-03-02 08:08:59 +00:00
_serviceClient = serviceClient;
2022-03-01 10:58:02 +00:00
_webHostEnvironment = webHostEnvironment;
_consumerFactory = consumerFactory;
_messageService = messageService;
_studioNotifyService = studioNotifyService;
_tenantManager = tenantManager;
_tenantExtra = tenantExtra;
_permissionContext = permissionContext;
_settingsManager = settingsManager;
_coreBaseSettings = coreBaseSettings;
_commonLinkUtility = commonLinkUtility;
_storageSettingsHelper = storageSettingsHelper;
_encryptionSettingsHelper = encryptionSettingsHelper;
_backupAjaxHandler = backupAjaxHandler;
_cacheDeleteSchedule = cacheDeleteSchedule;
_encryptionWorker = encryptionWorker;
_securityContext = securityContext;
2022-03-01 10:58:02 +00:00
}
[HttpGet("storage")]
2022-03-15 10:00:41 +00:00
public List<StorageDto> GetAllStorages()
2022-03-01 10:58:02 +00:00
{
_permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
_tenantExtra.DemandControlPanelPermission();
var current = _settingsManager.Load<StorageSettings>();
var consumers = _consumerFactory.GetAll<DataStoreConsumer>();
2022-03-15 10:00:41 +00:00
return consumers.Select(consumer => new StorageDto(consumer, current)).ToList();
2022-03-01 10:58:02 +00:00
}
[AllowNotPayment]
[HttpGet("storage/progress")]
2022-03-01 10:58:02 +00:00
public double GetStorageProgress()
{
_permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
2022-03-17 15:01:39 +00:00
if (!_coreBaseSettings.Standalone)
{
return -1;
}
2022-03-01 10:58:02 +00:00
return _serviceClient.GetProgress(Tenant.Id);
}
public readonly object Locker = new object();
[HttpPost("encryption/start")]
public bool StartStorageEncryption(StorageEncryptionRequestsDto inDto)
2022-03-01 10:58:02 +00:00
{
if (_coreBaseSettings.CustomMode)
{
return false;
}
lock (Locker)
{
var activeTenants = _tenantManager.GetTenants();
if (activeTenants.Count > 0)
{
2022-03-15 10:56:22 +00:00
StartEncryption(inDto.NotifyUsers);
2022-03-01 10:58:02 +00:00
}
}
return true;
}
private void StartEncryption(bool notifyUsers)
{
if (!SetupInfo.IsVisibleSettings<EncryptionSettings>())
{
throw new NotSupportedException();
}
if (!_coreBaseSettings.Standalone)
{
throw new NotSupportedException();
}
_permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
_tenantExtra.DemandControlPanelPermission();
var storages = GetAllStorages();
if (storages.Any(s => s.Current))
{
throw new NotSupportedException();
}
var cdnStorages = GetAllCdnStorages();
if (cdnStorages.Any(s => s.Current))
{
throw new NotSupportedException();
}
var tenants = _tenantManager.GetTenants();
foreach (var tenant in tenants)
{
var progress = _backupAjaxHandler.GetBackupProgress(tenant.Id);
if (progress != null && !progress.IsCompleted)
{
throw new Exception();
}
}
foreach (var tenant in tenants)
{
2022-04-15 09:08:06 +00:00
_cacheDeleteSchedule.Publish(new DeleteSchedule() { TenantId = tenant.Id }, CacheNotifyAction.Insert);
2022-03-01 10:58:02 +00:00
}
var settings = _encryptionSettingsHelper.Load();
settings.NotifyUsers = notifyUsers;
if (settings.Status == EncryprtionStatus.Decrypted)
{
settings.Status = EncryprtionStatus.EncryptionStarted;
settings.Password = _encryptionSettingsHelper.GeneratePassword(32, 16);
}
else if (settings.Status == EncryprtionStatus.Encrypted)
{
settings.Status = EncryprtionStatus.DecryptionStarted;
}
_messageService.Send(settings.Status == EncryprtionStatus.EncryptionStarted ? MessageAction.StartStorageEncryption : MessageAction.StartStorageDecryption);
var serverRootPath = _commonLinkUtility.GetFullAbsolutePath("~").TrimEnd('/');
foreach (var tenant in tenants)
{
_tenantManager.SetCurrentTenant(tenant);
if (notifyUsers)
{
if (settings.Status == EncryprtionStatus.EncryptionStarted)
{
_studioNotifyService.SendStorageEncryptionStart(serverRootPath);
}
else
{
_studioNotifyService.SendStorageDecryptionStart(serverRootPath);
}
}
tenant.SetStatus(TenantStatus.Encryption);
_tenantManager.SaveTenant(tenant);
}
_encryptionSettingsHelper.Save(settings);
_eventBus.Publish(new EncryptionDataStorageRequestedIntegration
(
encryptionSettings: new EncryptionSettings
2022-06-07 18:00:08 +00:00
{
NotifyUsers = settings.NotifyUsers,
Password = settings.Password,
Status = settings.Status
},
serverRootPath: serverRootPath,
createBy: _securityContext.CurrentAccount.ID,
tenantId: _tenantManager.GetCurrentTenant().Id
));
2022-03-01 10:58:02 +00:00
}
/// <summary>
/// Get storage encryption settings
/// </summary>
/// <returns>EncryptionSettings</returns>
/// <visible>false</visible>
[HttpGet("encryption/settings")]
2022-03-01 10:58:02 +00:00
public EncryptionSettings GetStorageEncryptionSettings()
{
try
{
if (_coreBaseSettings.CustomMode)
{
return null;
}
if (!SetupInfo.IsVisibleSettings<EncryptionSettings>())
{
throw new NotSupportedException();
}
if (!_coreBaseSettings.Standalone)
{
throw new NotSupportedException();
}
_permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
_tenantExtra.DemandControlPanelPermission();
var settings = _encryptionSettingsHelper.Load();
settings.Password = string.Empty; // Don't show password
return settings;
}
catch (Exception e)
{
2022-05-13 13:09:13 +00:00
_log.ErrorGetStorageEncryptionSettings(e);
2022-03-01 10:58:02 +00:00
return null;
}
}
[HttpGet("encryption/progress")]
2022-03-01 10:58:02 +00:00
public double? GetStorageEncryptionProgress()
{
if (_coreBaseSettings.CustomMode)
{
return -1;
}
if (!SetupInfo.IsVisibleSettings<EncryptionSettings>())
{
throw new NotSupportedException();
}
if (!_coreBaseSettings.Standalone)
{
throw new NotSupportedException();
}
return _encryptionWorker.GetEncryptionProgress();
}
[HttpPut("storage")]
public StorageSettings UpdateStorage(StorageRequestsDto inDto)
2022-03-01 10:58:02 +00:00
{
try
{
_permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
2022-03-17 15:01:39 +00:00
if (!_coreBaseSettings.Standalone)
{
return null;
}
2022-03-01 10:58:02 +00:00
_tenantExtra.DemandControlPanelPermission();
2022-03-15 10:56:22 +00:00
var consumer = _consumerFactory.GetByKey(inDto.Module);
2022-03-17 15:01:39 +00:00
if (!consumer.IsSet)
{
throw new ArgumentException("module");
}
2022-03-01 10:58:02 +00:00
var settings = _settingsManager.Load<StorageSettings>();
2022-03-17 15:01:39 +00:00
if (settings.Module == inDto.Module)
{
return settings;
}
2022-03-15 10:56:22 +00:00
settings.Module = inDto.Module;
settings.Props = inDto.Props.ToDictionary(r => r.Key, b => b.Value);
2022-03-01 10:58:02 +00:00
StartMigrate(settings);
return settings;
}
catch (Exception e)
{
2022-05-13 13:09:13 +00:00
_log.ErrorUpdateStorage(e);
2022-03-01 10:58:02 +00:00
throw;
}
}
[HttpDelete("storage")]
2022-03-01 10:58:02 +00:00
public void ResetStorageToDefault()
{
try
{
_permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
2022-03-17 15:01:39 +00:00
if (!_coreBaseSettings.Standalone)
{
return;
}
2022-03-01 10:58:02 +00:00
_tenantExtra.DemandControlPanelPermission();
var settings = _settingsManager.Load<StorageSettings>();
settings.Module = null;
settings.Props = null;
StartMigrate(settings);
}
catch (Exception e)
{
2022-05-13 13:09:13 +00:00
_log.ErrorResetStorageToDefault(e);
2022-03-01 10:58:02 +00:00
throw;
}
}
[HttpGet("storage/cdn")]
2022-03-15 10:00:41 +00:00
public List<StorageDto> GetAllCdnStorages()
2022-03-01 10:58:02 +00:00
{
_permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
2022-03-17 15:01:39 +00:00
if (!_coreBaseSettings.Standalone)
{
return null;
}
2022-03-01 10:58:02 +00:00
_tenantExtra.DemandControlPanelPermission();
var current = _settingsManager.Load<CdnStorageSettings>();
var consumers = _consumerFactory.GetAll<DataStoreConsumer>().Where(r => r.Cdn != null);
2022-03-15 10:00:41 +00:00
return consumers.Select(consumer => new StorageDto(consumer, current)).ToList();
2022-03-01 10:58:02 +00:00
}
[HttpPut("storage/cdn")]
public CdnStorageSettings UpdateCdn(StorageRequestsDto inDto)
2022-03-01 10:58:02 +00:00
{
_permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
2022-03-17 15:01:39 +00:00
if (!_coreBaseSettings.Standalone)
{
return null;
}
2022-03-01 10:58:02 +00:00
_tenantExtra.DemandControlPanelPermission();
2022-03-15 10:56:22 +00:00
var consumer = _consumerFactory.GetByKey(inDto.Module);
2022-03-17 15:01:39 +00:00
if (!consumer.IsSet)
{
throw new ArgumentException("module");
}
2022-03-01 10:58:02 +00:00
var settings = _settingsManager.Load<CdnStorageSettings>();
2022-03-17 15:01:39 +00:00
if (settings.Module == inDto.Module)
{
return settings;
}
2022-03-15 10:56:22 +00:00
settings.Module = inDto.Module;
settings.Props = inDto.Props.ToDictionary(r => r.Key, b => b.Value);
2022-03-01 10:58:02 +00:00
try
{
_serviceClient.UploadCdn(Tenant.Id, "/", _webHostEnvironment.ContentRootPath, settings);
}
catch (Exception e)
{
2022-05-13 13:09:13 +00:00
_log.ErrorUpdateCdn(e);
2022-03-01 10:58:02 +00:00
throw;
}
return settings;
}
[HttpDelete("storage/cdn")]
2022-03-01 10:58:02 +00:00
public void ResetCdnToDefault()
{
_permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
2022-03-17 15:01:39 +00:00
if (!_coreBaseSettings.Standalone)
{
return;
}
2022-03-01 10:58:02 +00:00
_tenantExtra.DemandControlPanelPermission();
_storageSettingsHelper.Clear(_settingsManager.Load<CdnStorageSettings>());
}
[HttpGet("storage/backup")]
2022-03-15 10:00:41 +00:00
public List<StorageDto> GetAllBackupStorages()
2022-03-01 10:58:02 +00:00
{
_permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
if (_coreBaseSettings.Standalone)
{
_tenantExtra.DemandControlPanelPermission();
}
var schedule = _backupAjaxHandler.GetSchedule();
var current = new StorageSettings();
if (schedule != null && schedule.StorageType == BackupStorageType.ThirdPartyConsumer)
{
current = new StorageSettings
{
Module = schedule.StorageParams["module"],
Props = schedule.StorageParams.Where(r => r.Key != "module").ToDictionary(r => r.Key, r => r.Value)
};
}
var consumers = _consumerFactory.GetAll<DataStoreConsumer>();
2022-03-15 10:00:41 +00:00
return consumers.Select(consumer => new StorageDto(consumer, current)).ToList();
2022-03-01 10:58:02 +00:00
}
private void StartMigrate(StorageSettings settings)
{
_serviceClient.Migrate(Tenant.Id, settings);
Tenant.SetStatus(TenantStatus.Migrating);
_tenantManager.SaveTenant(Tenant);
}
}