Beispiel #1
0
        /// <summary>
        /// A constructor that accepts a database and logger.
        /// </summary>
        /// <param name="database">The client database connection.</param>
        /// <param name="logger">A logging instance.</param>
        /// <param name="providers">A collection of cloud backup providers.</param>
        /// <param name="instanceID">An engine instance ID.</param>
        public FileSender(IClientDatabase database, ILogger logger, StorageProviderConnectionsCollection providers, int instanceID)
        {
            if (database == null)
            {
                throw new ArgumentNullException(nameof(database));
            }
            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }
            if (providers == null)
            {
                throw new ArgumentNullException(nameof(providers));
            }
            if (providers.Count == 0)
            {
                throw new ArgumentException(nameof(providers) + " must be provided.");
            }

            Database   = database;
            Logger     = logger;
            Providers  = providers;
            Hasher     = new Hasher(Logger);
            InstanceID = instanceID;
        }
Beispiel #2
0
        /// <summary>
        /// Checks to see if storage providers are configured.
        /// </summary>
        /// <returns></returns>
        private async Task <bool> StorageProvidersAreConfiguredAsync()
        {
            try
            {
                if (Providers != null)
                {
                    // providers are already configured.
                    return(true);
                }
                else
                {
                    // otherwise check the database to see if we have any providers.

                    var storageProviders = await Database.GetProvidersAsync(ProviderTypes.Storage).ConfigureAwait(false);

                    if (storageProviders.Count > 0)
                    {
                        // attemp to configure the providers.
                        var connections = new ProviderConnections(Database);
                        var storageProviderConnections = await connections.ConfigureStorageProviderConnectionsAsync(Logger).ConfigureAwait(false);

                        if (storageProviderConnections != null)
                        {
                            Providers = storageProviderConnections;
                            return(true);
                        }
                        else
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        // no providers setup yet.
                        Logger.WriteTraceWarning("No storage providers have been configured yet. The cleanup engine(s) won't work until these have been configured.");
                        return(false);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.WriteTraceError("Failed to lookup or configure storage providers.", ex, Logger.GenerateFullContextStackTrace());
                return(false);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Configures the cloud storage provider connections.
        /// </summary>
        /// <returns>True if successful, otherwise false.</returns>
        public async Task <StorageProviderConnectionsCollection> ConfigureStorageProviderConnectionsAsync(ILogger Log)
        {
            Log.WriteSystemEvent("Configuring cloud storage provider connections.", EventLogEntryType.Information, Constants.EventIDs.ConfiguringCloudProviderConnections, true);

            var storageConnections = new StorageProviderConnectionsCollection();

            try
            {
                // establish the protected store.

                var settingName = Constants.RuntimeSettingNames.ProtectionIV;
                var protectionIvEncodedString = await Database.GetApplicationOptionAsync(settingName).ConfigureAwait(false);

                var ivBytes = Convert.FromBase64String(protectionIvEncodedString);

                ProtectedDataStore protectedStore = new ProtectedDataStore(Database, DataProtectionScope.LocalMachine, ivBytes);

                // configure the provider implementation instances.
                // add each to the collection of providers.

                var providersList = await Database.GetProvidersAsync(ProviderTypes.Storage).ConfigureAwait(false);

                foreach (var provider in providersList)
                {
                    Log.WriteTraceMessage(string.Format("A storage provider was found in the configuration database: Name: {0}", provider.Name));

                    switch (provider.Name)
                    {
                    case nameof(StorageProviderTypes.Azure):
                    {
                        Log.WriteTraceMessage("Checking for Azure cloud storage provider connection settings.");
                        string storageAccountName = await protectedStore.GetApplicationSecretAsync(Constants.RuntimeSettingNames.AzureStorageAccountName).ConfigureAwait(false);

                        string storageAccountToken = await protectedStore.GetApplicationSecretAsync(Constants.RuntimeSettingNames.AzureStorageAccountToken).ConfigureAwait(false);

                        Log.WriteTraceMessage("Initializing Azure cloud storage provider.");
                        var azureConnection = new AzureStorageProviderFileOperations(Log, storageAccountName, storageAccountToken);
                        storageConnections.Add(StorageProviderTypes.Azure, azureConnection);
                        Log.WriteTraceMessage("Successfully initialized the cloud storage provider.");

                        break;
                    }

                    default:
                    {
                        throw new NotImplementedException("Unexpected provider type specified: " + provider.Type.ToString());
                    }
                    }
                }

                if (storageConnections.Count == 0)
                {
                    return(null);
                }
                else
                {
                    Log.WriteSystemEvent("Successfully configured cloud storage provider connections.", EventLogEntryType.Information, Constants.EventIDs.ConfiguredCloudProviderConnections, true);
                    return(storageConnections);
                }
            }
            catch (ApplicationCoreSettingMissingException ex)
            {
                Log.WriteSystemEvent("A core application setting has not been configured yet: " + ex.Message,
                                     EventLogEntryType.Error, Constants.EventIDs.CoreSettingMissing, true);

                return(null);
            }
            catch (ApplicationCoreSettingInvalidValueException ex)
            {
                Log.WriteSystemEvent("A core application setting has an invalid value specified: " + ex.Message,
                                     EventLogEntryType.Error, Constants.EventIDs.CoreSettingInvalid, true);

                return(null);
            }
            catch (ApplicationSecretMissingException)
            {
                Log.WriteSystemEvent("Failed to configure cloud storage provider connections: A cloud storage provider is missing required connection settings.",
                                     EventLogEntryType.Error, Constants.EventIDs.FailedToConfigureProvidersMissingSettings, true);

                return(null);
            }
            catch (Exception ex)
            {
                var message = "Failed to configure cloud storage provider connections.";
                var context = Log.GenerateFullContextStackTrace();
                Log.WriteSystemEvent(message, ex, context, Constants.EventIDs.FailedToConfigureStorageProviderConnections, true);
                return(null);
            }
        }