2019-06-26 10:13:11 +02:00
|
|
|
|
/*
|
2024-03-05 18:37:42 +01:00
|
|
|
|
* Copyright (c) 2024 ETH Zürich, IT Services
|
2019-06-26 10:13:11 +02:00
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-06-27 08:32:37 +02:00
|
|
|
|
using System;
|
2019-07-05 12:28:42 +02:00
|
|
|
|
using System.Collections.Generic;
|
2019-06-27 08:32:37 +02:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2019-08-30 09:55:26 +02:00
|
|
|
|
using SafeExamBrowser.Lockdown.Contracts;
|
|
|
|
|
using SafeExamBrowser.Logging.Contracts;
|
2019-06-26 10:13:11 +02:00
|
|
|
|
|
|
|
|
|
namespace SafeExamBrowser.Lockdown
|
|
|
|
|
{
|
|
|
|
|
public class AutoRestoreMechanism : IAutoRestoreMechanism
|
|
|
|
|
{
|
2019-06-27 08:32:37 +02:00
|
|
|
|
private readonly object @lock = new object();
|
|
|
|
|
|
2019-06-26 10:13:11 +02:00
|
|
|
|
private IFeatureConfigurationBackup backup;
|
2019-06-27 08:32:37 +02:00
|
|
|
|
private ILogger logger;
|
2019-07-03 12:27:02 +02:00
|
|
|
|
private ISystemConfigurationUpdate systemConfigurationUpdate;
|
2019-06-27 08:32:37 +02:00
|
|
|
|
private bool running;
|
|
|
|
|
private int timeout_ms;
|
2019-06-26 10:13:11 +02:00
|
|
|
|
|
2019-07-03 12:27:02 +02:00
|
|
|
|
public AutoRestoreMechanism(
|
|
|
|
|
IFeatureConfigurationBackup backup,
|
|
|
|
|
ILogger logger,
|
|
|
|
|
ISystemConfigurationUpdate systemConfigurationUpdate,
|
|
|
|
|
int timeout_ms)
|
2019-06-26 10:13:11 +02:00
|
|
|
|
{
|
2019-06-27 08:32:37 +02:00
|
|
|
|
if (timeout_ms < 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Must be 0 or greater!", nameof(timeout_ms));
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-26 10:13:11 +02:00
|
|
|
|
this.backup = backup;
|
2019-06-27 08:32:37 +02:00
|
|
|
|
this.logger = logger;
|
2019-07-03 12:27:02 +02:00
|
|
|
|
this.systemConfigurationUpdate = systemConfigurationUpdate;
|
2019-06-27 08:32:37 +02:00
|
|
|
|
this.timeout_ms = timeout_ms;
|
2019-06-26 10:13:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Start()
|
|
|
|
|
{
|
2019-06-27 08:32:37 +02:00
|
|
|
|
lock (@lock)
|
|
|
|
|
{
|
|
|
|
|
if (!running)
|
|
|
|
|
{
|
|
|
|
|
running = true;
|
2019-07-05 12:28:42 +02:00
|
|
|
|
Task.Run(new Action(AutoRestore));
|
2019-06-27 08:32:37 +02:00
|
|
|
|
logger.Info("Started auto-restore mechanism.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Info("Auto-restore mechanism is already running.");
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-26 10:13:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Stop()
|
|
|
|
|
{
|
2019-06-27 08:32:37 +02:00
|
|
|
|
lock (@lock)
|
|
|
|
|
{
|
|
|
|
|
if (running)
|
|
|
|
|
{
|
|
|
|
|
running = false;
|
|
|
|
|
logger.Info("Stopped auto-restore mechanism.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Info("Auto-restore mechanism is not running.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-05 12:28:42 +02:00
|
|
|
|
private void AutoRestore()
|
2019-06-27 08:32:37 +02:00
|
|
|
|
{
|
2019-07-05 12:28:42 +02:00
|
|
|
|
if (IsStopped())
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-27 08:32:37 +02:00
|
|
|
|
var configurations = backup.GetAllConfigurations();
|
|
|
|
|
|
2019-07-03 12:27:02 +02:00
|
|
|
|
if (configurations.Any())
|
2019-06-27 08:32:37 +02:00
|
|
|
|
{
|
2019-07-05 12:28:42 +02:00
|
|
|
|
var success = TryRestoreAll(configurations);
|
2019-06-27 08:32:37 +02:00
|
|
|
|
|
2019-07-05 12:28:42 +02:00
|
|
|
|
if (success == true)
|
2019-07-03 12:27:02 +02:00
|
|
|
|
{
|
2019-07-05 12:28:42 +02:00
|
|
|
|
systemConfigurationUpdate.ExecuteAsync();
|
2019-06-27 08:32:37 +02:00
|
|
|
|
}
|
2019-07-05 12:28:42 +02:00
|
|
|
|
else if (success == false)
|
2019-06-27 08:32:37 +02:00
|
|
|
|
{
|
2019-07-05 12:28:42 +02:00
|
|
|
|
Task.Delay(timeout_ms).ContinueWith((_) => AutoRestore());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Info("Auto-restore mechanism was aborted.");
|
2019-06-27 08:32:37 +02:00
|
|
|
|
}
|
2019-07-03 12:27:02 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-06-27 08:32:37 +02:00
|
|
|
|
lock (@lock)
|
|
|
|
|
{
|
2019-07-03 12:27:02 +02:00
|
|
|
|
running = false;
|
|
|
|
|
logger.Info("Nothing to restore, stopped auto-restore mechanism.");
|
2019-06-27 08:32:37 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-26 10:13:11 +02:00
|
|
|
|
}
|
2019-07-05 12:28:42 +02:00
|
|
|
|
|
|
|
|
|
private bool IsStopped()
|
|
|
|
|
{
|
|
|
|
|
lock (@lock)
|
|
|
|
|
{
|
|
|
|
|
return !running;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool? TryRestoreAll(IList<IFeatureConfiguration> configurations)
|
|
|
|
|
{
|
|
|
|
|
var restored = 0;
|
|
|
|
|
|
|
|
|
|
logger.Info($"Attempting to restore {configurations.Count} items...");
|
|
|
|
|
|
|
|
|
|
foreach (var configuration in configurations)
|
|
|
|
|
{
|
|
|
|
|
var success = TryRestore(configuration);
|
|
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
|
{
|
|
|
|
|
restored++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (IsStopped())
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.Info($"Restored {restored} of {configurations.Count} items.");
|
|
|
|
|
|
|
|
|
|
return restored == configurations.Count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool TryRestore(IFeatureConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
var success = configuration.Restore();
|
|
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
|
{
|
|
|
|
|
backup.Delete(configuration);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Warn($"Failed to restore {configuration}!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return success;
|
|
|
|
|
}
|
2019-06-26 10:13:11 +02:00
|
|
|
|
}
|
|
|
|
|
}
|