2018-02-06 15:12:11 +01:00
|
|
|
|
/*
|
2024-03-05 18:37:42 +01:00
|
|
|
|
* Copyright (c) 2024 ETH Zürich, IT Services
|
2018-02-06 15:12:11 +01: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/.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
2018-11-08 09:39:52 +01:00
|
|
|
|
using System.Collections.Generic;
|
2018-02-06 15:12:11 +01:00
|
|
|
|
using System.IO;
|
2018-11-08 09:39:52 +01:00
|
|
|
|
using System.Linq;
|
2018-12-21 11:36:20 +01:00
|
|
|
|
using SafeExamBrowser.Configuration.ConfigurationData;
|
2019-08-30 09:55:26 +02:00
|
|
|
|
using SafeExamBrowser.Configuration.Contracts;
|
|
|
|
|
using SafeExamBrowser.Configuration.Contracts.Cryptography;
|
|
|
|
|
using SafeExamBrowser.Configuration.Contracts.DataFormats;
|
|
|
|
|
using SafeExamBrowser.Configuration.Contracts.DataResources;
|
|
|
|
|
using SafeExamBrowser.Logging.Contracts;
|
2020-01-10 08:54:10 +01:00
|
|
|
|
using SafeExamBrowser.Settings;
|
2018-02-06 15:12:11 +01:00
|
|
|
|
|
|
|
|
|
namespace SafeExamBrowser.Configuration
|
|
|
|
|
{
|
|
|
|
|
public class ConfigurationRepository : IConfigurationRepository
|
|
|
|
|
{
|
2022-04-19 18:21:29 +02:00
|
|
|
|
private readonly ICertificateStore certificateStore;
|
|
|
|
|
private readonly IList<IDataParser> dataParsers;
|
|
|
|
|
private readonly IList<IDataSerializer> dataSerializers;
|
|
|
|
|
private readonly DataMapper dataMapper;
|
|
|
|
|
private readonly DataProcessor dataProcessor;
|
|
|
|
|
private readonly DataValues dataValues;
|
|
|
|
|
private readonly ILogger logger;
|
|
|
|
|
private readonly IList<IResourceLoader> resourceLoaders;
|
|
|
|
|
private readonly IList<IResourceSaver> resourceSavers;
|
|
|
|
|
|
|
|
|
|
public ConfigurationRepository(ICertificateStore certificateStore, IModuleLogger logger)
|
2018-09-04 10:58:56 +02:00
|
|
|
|
{
|
2019-02-22 10:02:34 +01:00
|
|
|
|
this.certificateStore = certificateStore;
|
2019-01-09 16:01:56 +01:00
|
|
|
|
this.logger = logger;
|
|
|
|
|
|
2018-12-21 11:36:20 +01:00
|
|
|
|
dataParsers = new List<IDataParser>();
|
|
|
|
|
dataSerializers = new List<IDataSerializer>();
|
|
|
|
|
dataMapper = new DataMapper();
|
2020-01-10 08:54:10 +01:00
|
|
|
|
dataProcessor = new DataProcessor();
|
2020-02-10 12:19:25 +01:00
|
|
|
|
dataValues = new DataValues();
|
2018-12-21 11:36:20 +01:00
|
|
|
|
resourceLoaders = new List<IResourceLoader>();
|
|
|
|
|
resourceSavers = new List<IResourceSaver>();
|
2018-02-06 15:12:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-14 15:30:10 +01:00
|
|
|
|
public SaveStatus ConfigureClientWith(Uri resource, PasswordParameters password = null)
|
2018-12-14 09:50:10 +01:00
|
|
|
|
{
|
2018-12-21 11:36:20 +01:00
|
|
|
|
logger.Info($"Attempting to configure local client with '{resource}'...");
|
2018-12-14 09:50:10 +01:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-12-21 11:36:20 +01:00
|
|
|
|
TryLoadData(resource, out var data);
|
2018-12-14 09:50:10 +01:00
|
|
|
|
|
2018-12-21 11:36:20 +01:00
|
|
|
|
using (data)
|
2018-12-14 09:50:10 +01:00
|
|
|
|
{
|
2018-12-21 11:36:20 +01:00
|
|
|
|
TryParseData(data, out var encryption, out var format, out var rawData, password);
|
|
|
|
|
|
2019-02-22 10:02:34 +01:00
|
|
|
|
certificateStore.ExtractAndImportIdentities(rawData);
|
2018-12-21 11:36:20 +01:00
|
|
|
|
encryption = DetermineEncryptionForClientConfiguration(rawData, encryption);
|
2018-12-14 09:50:10 +01:00
|
|
|
|
|
2018-12-21 11:36:20 +01:00
|
|
|
|
var status = TrySerializeData(rawData, format, out var serialized, encryption);
|
2018-12-14 15:30:10 +01:00
|
|
|
|
|
2018-12-21 11:36:20 +01:00
|
|
|
|
using (serialized)
|
|
|
|
|
{
|
|
|
|
|
if (status == SaveStatus.Success)
|
|
|
|
|
{
|
|
|
|
|
status = TrySaveData(new Uri(dataValues.GetAppDataFilePath()), serialized);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-14 09:50:10 +01:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-12-21 11:36:20 +01:00
|
|
|
|
logger.Error($"Unexpected error while trying to configure local client with '{resource}'!", e);
|
2018-12-14 15:30:10 +01:00
|
|
|
|
|
|
|
|
|
return SaveStatus.UnexpectedError;
|
2018-12-14 09:50:10 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-12 11:16:59 +02:00
|
|
|
|
public AppConfig InitializeAppConfig()
|
2018-02-06 15:12:11 +01:00
|
|
|
|
{
|
2018-12-21 11:36:20 +01:00
|
|
|
|
return dataValues.InitializeAppConfig();
|
2018-10-12 11:16:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-11 09:53:33 +02:00
|
|
|
|
public SessionConfiguration InitializeSessionConfiguration()
|
2018-10-12 11:16:59 +02:00
|
|
|
|
{
|
2018-12-21 11:36:20 +01:00
|
|
|
|
return dataValues.InitializeSessionConfiguration();
|
2018-10-12 11:16:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-01 11:30:53 +02:00
|
|
|
|
public AppSettings LoadDefaultSettings()
|
2018-10-12 11:16:59 +02:00
|
|
|
|
{
|
2018-12-21 11:36:20 +01:00
|
|
|
|
return dataValues.LoadDefaultSettings();
|
|
|
|
|
}
|
2018-10-12 11:16:59 +02:00
|
|
|
|
|
2018-12-21 11:36:20 +01:00
|
|
|
|
public void Register(IDataParser parser)
|
|
|
|
|
{
|
|
|
|
|
dataParsers.Add(parser);
|
|
|
|
|
}
|
2018-10-12 11:16:59 +02:00
|
|
|
|
|
2018-12-21 11:36:20 +01:00
|
|
|
|
public void Register(IDataSerializer serializer)
|
|
|
|
|
{
|
|
|
|
|
dataSerializers.Add(serializer);
|
2018-10-12 11:16:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-21 11:36:20 +01:00
|
|
|
|
public void Register(IResourceLoader loader)
|
2018-11-08 09:39:52 +01:00
|
|
|
|
{
|
2018-12-21 11:36:20 +01:00
|
|
|
|
resourceLoaders.Add(loader);
|
2018-11-08 09:39:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-21 11:36:20 +01:00
|
|
|
|
public void Register(IResourceSaver saver)
|
2018-11-08 09:39:52 +01:00
|
|
|
|
{
|
2018-12-21 11:36:20 +01:00
|
|
|
|
resourceSavers.Add(saver);
|
2018-11-08 09:39:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-01 11:30:53 +02:00
|
|
|
|
public LoadStatus TryLoadSettings(Uri resource, out AppSettings settings, PasswordParameters password = null)
|
2018-10-12 11:16:59 +02:00
|
|
|
|
{
|
2024-01-17 11:08:03 +01:00
|
|
|
|
var status = default(LoadStatus);
|
|
|
|
|
|
2018-11-30 14:50:28 +01:00
|
|
|
|
settings = LoadDefaultSettings();
|
2024-08-19 12:19:32 +02:00
|
|
|
|
dataProcessor.ProcessDefault(settings);
|
|
|
|
|
|
2023-10-06 16:22:57 +02:00
|
|
|
|
logger.Info($"Initialized default settings, now attempting to load '{resource}'...");
|
|
|
|
|
|
2018-11-08 09:39:52 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
2024-01-17 11:08:03 +01:00
|
|
|
|
status = TryLoadData(resource, out var stream);
|
2018-11-08 09:39:52 +01:00
|
|
|
|
|
2018-12-14 09:50:10 +01:00
|
|
|
|
using (stream)
|
2018-11-08 09:39:52 +01:00
|
|
|
|
{
|
2018-12-14 09:50:10 +01:00
|
|
|
|
if (status == LoadStatus.Success)
|
2018-11-15 08:45:17 +01:00
|
|
|
|
{
|
2018-12-21 11:36:20 +01:00
|
|
|
|
status = TryParseData(stream, out _, out _, out var data, password);
|
|
|
|
|
|
|
|
|
|
if (status == LoadStatus.Success)
|
|
|
|
|
{
|
2024-08-19 12:19:32 +02:00
|
|
|
|
dataMapper.Map(data, settings);
|
2020-01-10 08:54:10 +01:00
|
|
|
|
dataProcessor.Process(data, settings);
|
2018-12-21 11:36:20 +01:00
|
|
|
|
}
|
2018-11-15 08:45:17 +01:00
|
|
|
|
}
|
2018-11-30 14:50:28 +01:00
|
|
|
|
}
|
2018-11-08 09:39:52 +01:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2024-01-17 11:08:03 +01:00
|
|
|
|
status = LoadStatus.UnexpectedError;
|
2018-11-08 09:39:52 +01:00
|
|
|
|
logger.Error($"Unexpected error while trying to load '{resource}'!", e);
|
|
|
|
|
}
|
2024-01-17 11:08:03 +01:00
|
|
|
|
|
|
|
|
|
return status;
|
2018-11-08 09:39:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-21 11:36:20 +01:00
|
|
|
|
private EncryptionParameters DetermineEncryptionForClientConfiguration(IDictionary<string, object> data, EncryptionParameters encryption)
|
2018-12-11 16:06:10 +01:00
|
|
|
|
{
|
2018-12-21 11:36:20 +01:00
|
|
|
|
var hasKey = data.TryGetValue(Keys.ConfigurationFile.KeepClientConfigEncryption, out var value);
|
|
|
|
|
var useDefaultEncryption = value is bool keepEncryption && !keepEncryption;
|
2018-12-14 09:50:10 +01:00
|
|
|
|
|
2018-12-21 11:36:20 +01:00
|
|
|
|
if (!hasKey || (hasKey && useDefaultEncryption))
|
2018-12-14 09:50:10 +01:00
|
|
|
|
{
|
2018-12-21 11:36:20 +01:00
|
|
|
|
encryption = new PasswordParameters { Password = string.Empty, IsHash = true };
|
2018-12-14 09:50:10 +01:00
|
|
|
|
}
|
2018-12-11 16:06:10 +01:00
|
|
|
|
|
2018-12-21 11:36:20 +01:00
|
|
|
|
return encryption;
|
2018-12-11 16:06:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-09 14:15:56 +01:00
|
|
|
|
private LoadStatus TryLoadData(Uri resource, out Stream data)
|
2018-11-08 09:39:52 +01:00
|
|
|
|
{
|
2018-11-09 14:15:56 +01:00
|
|
|
|
var status = LoadStatus.NotSupported;
|
2018-12-21 11:36:20 +01:00
|
|
|
|
var resourceLoader = resourceLoaders.FirstOrDefault(l => l.CanLoad(resource));
|
2018-11-09 14:15:56 +01:00
|
|
|
|
|
2022-04-19 18:21:29 +02:00
|
|
|
|
data = default;
|
2018-11-09 14:15:56 +01:00
|
|
|
|
|
|
|
|
|
if (resourceLoader != null)
|
|
|
|
|
{
|
|
|
|
|
status = resourceLoader.TryLoad(resource, out data);
|
|
|
|
|
logger.Info($"Tried to load data from '{resource}' using {resourceLoader.GetType().Name} -> Result: {status}.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Warn($"No resource loader found for '{resource}'!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2018-11-08 09:39:52 +01:00
|
|
|
|
|
2018-12-21 11:36:20 +01:00
|
|
|
|
private LoadStatus TryParseData(Stream data, out EncryptionParameters encryption, out FormatType format, out IDictionary<string, object> rawData, PasswordParameters password = null)
|
2018-11-09 14:15:56 +01:00
|
|
|
|
{
|
2018-12-21 11:36:20 +01:00
|
|
|
|
var parser = dataParsers.FirstOrDefault(p => p.CanParse(data));
|
2018-12-11 16:06:10 +01:00
|
|
|
|
var status = LoadStatus.NotSupported;
|
2018-11-09 14:15:56 +01:00
|
|
|
|
|
2022-04-19 18:21:29 +02:00
|
|
|
|
encryption = default;
|
|
|
|
|
format = default;
|
2018-12-14 09:50:10 +01:00
|
|
|
|
rawData = default(Dictionary<string, object>);
|
|
|
|
|
|
2018-12-21 11:36:20 +01:00
|
|
|
|
if (parser != null)
|
2018-11-09 14:15:56 +01:00
|
|
|
|
{
|
2018-12-21 11:36:20 +01:00
|
|
|
|
var result = parser.TryParse(data, password);
|
2018-12-11 16:06:10 +01:00
|
|
|
|
|
2018-12-14 09:50:10 +01:00
|
|
|
|
encryption = result.Encryption;
|
|
|
|
|
format = result.Format;
|
|
|
|
|
rawData = result.RawData;
|
2018-12-11 16:06:10 +01:00
|
|
|
|
status = result.Status;
|
2018-12-14 09:50:10 +01:00
|
|
|
|
|
2018-12-21 11:36:20 +01:00
|
|
|
|
logger.Info($"Tried to parse data from '{data}' using {parser.GetType().Name} -> Result: {status}.");
|
2018-11-09 14:15:56 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-12-21 11:36:20 +01:00
|
|
|
|
logger.Warn($"No data parser found which can parse '{data}'!");
|
2018-11-09 14:15:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-21 11:36:20 +01:00
|
|
|
|
private SaveStatus TrySaveData(Uri destination, Stream data)
|
2018-03-14 11:04:28 +01:00
|
|
|
|
{
|
2018-12-21 11:36:20 +01:00
|
|
|
|
var status = SaveStatus.NotSupported;
|
|
|
|
|
var resourceSaver = resourceSavers.FirstOrDefault(s => s.CanSave(destination));
|
|
|
|
|
|
|
|
|
|
if (resourceSaver != null)
|
|
|
|
|
{
|
|
|
|
|
status = resourceSaver.TrySave(destination, data);
|
|
|
|
|
logger.Info($"Tried to save data as '{destination}' using {resourceSaver.GetType().Name} -> Result: {status}.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Warn($"No resource saver found for '{destination}'!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private SaveStatus TrySerializeData(IDictionary<string, object> data, FormatType format, out Stream serialized, EncryptionParameters encryption = null)
|
|
|
|
|
{
|
|
|
|
|
var serializer = dataSerializers.FirstOrDefault(s => s.CanSerialize(format));
|
|
|
|
|
var status = SaveStatus.NotSupported;
|
|
|
|
|
|
2022-04-19 18:21:29 +02:00
|
|
|
|
serialized = default;
|
2018-12-21 11:36:20 +01:00
|
|
|
|
|
|
|
|
|
if (serializer != null)
|
|
|
|
|
{
|
|
|
|
|
var result = serializer.TrySerialize(data, encryption);
|
|
|
|
|
|
|
|
|
|
serialized = result.Data;
|
|
|
|
|
status = result.Status;
|
|
|
|
|
|
|
|
|
|
logger.Info($"Tried to serialize data as '{format}' using {serializer.GetType().Name} -> Result: {status}.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Error($"No data serializer found which can serialize '{format}'!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return status;
|
2018-03-14 11:04:28 +01:00
|
|
|
|
}
|
2018-02-06 15:12:11 +01:00
|
|
|
|
}
|
|
|
|
|
}
|