Example #1
0
        protected RavenConfiguration CreateConfiguration(
            string tenantId,
            CounterStorageDocument document,
            string folderPropName,
            RavenConfiguration parentConfiguration)
        {
            var config = RavenConfiguration.CreateFrom(parentConfiguration);

            SetupTenantConfiguration(config);

            config.CustomizeValuesForCounterStorageTenant(tenantId);

            foreach (var setting in document.Settings)
            {
                config.SetSetting(setting.Key, setting.Value);
            }

            Unprotect(document);

            foreach (var securedSetting in document.SecuredSettings)
            {
                config.SetSetting(securedSetting.Key, securedSetting.Value);
            }

            config.SetSetting(folderPropName, config.GetSetting(folderPropName).ToFullPath(parentConfiguration.Counter.DataDirectory));
            config.SetSetting(RavenConfiguration.GetKey(x => x.Storage.JournalsStoragePath), config.GetSetting(RavenConfiguration.GetKey(x => x.Storage.JournalsStoragePath)).ToFullPath(parentConfiguration.Core.DataDirectory));

            config.CounterStorageName = tenantId;

            config.Initialize();
            config.CopyParentSettings(parentConfiguration);
            return(config);
        }
Example #2
0
        void Unprotect(CounterStorageDocument configDocument)
        {
            if (configDocument.SecuredSettings == null)
            {
                configDocument.SecuredSettings = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
                return;
            }

            foreach (var prop in configDocument.SecuredSettings.ToList())
            {
                if (prop.Value == null)
                {
                    continue;
                }
                var bytes    = Convert.FromBase64String(prop.Value);
                var entrophy = Encoding.UTF8.GetBytes(prop.Key);
                try
                {
                    var unprotectedValue = ProtectedData.Unprotect(bytes, entrophy, DataProtectionScope.CurrentUser);
                    configDocument.SecuredSettings[prop.Key] = Encoding.UTF8.GetString(unprotectedValue);
                }
                catch (Exception e)
                {
                    Logger.WarnException("Could not unprotect secured db data " + prop.Key + " setting the value to '<data could not be decrypted>'", e);
                    configDocument.SecuredSettings[prop.Key] = Constants.DataCouldNotBeDecrypted;
                }
            }
        }
Example #3
0
            /// <summary>
            /// Create new counter storage on the server.
            /// </summary>
            /// <param name="counterStorageDocument">Settings for the counter storage. If null, default settings will be used, and the name specified in the client ctor will be used</param>
            /// <param name="counterStorageName">Override counter storage name specified in client ctor. If null, the name already specified will be used</param>
            /// <param name="shouldUpateIfExists">If the storage already there, should we update it</param>
            /// <param name="credentials">Credentials used for this operation.</param>
            /// <param name="token">Cancellation token used for this operation.</param>
            public async Task <CounterStore> CreateCounterStorageAsync(CounterStorageDocument counterStorageDocument,
                                                                       string counterStorageName,
                                                                       bool shouldUpateIfExists         = false,
                                                                       OperationCredentials credentials = null,
                                                                       CancellationToken token          = default(CancellationToken))
            {
                if (counterStorageDocument == null)
                {
                    throw new ArgumentNullException(nameof(counterStorageDocument));
                }

                if (counterStorageName == null)
                {
                    throw new ArgumentNullException(nameof(counterStorageName));
                }

                parent.AssertInitialized();

                var urlTemplate = "{0}/admin/cs/{1}";

                if (shouldUpateIfExists)
                {
                    urlTemplate += "?update=true";
                }

                var requestUriString = String.Format(urlTemplate, parent.Url, counterStorageName);

                using (var request = parent.CreateHttpJsonRequest(requestUriString, HttpMethods.Put))
                {
                    try
                    {
                        await request.WriteAsync(RavenJObject.FromObject(counterStorageDocument)).WithCancellation(token).ConfigureAwait(false);
                    }
                    catch (ErrorResponseException e)
                    {
                        if (e.StatusCode == HttpStatusCode.Conflict)
                        {
                            throw new InvalidOperationException($"Cannot create counter storage with the name '{counterStorageName}' because it already exists. Use shouldUpateIfExists = true flag in case you want to update an existing counter storage", e);
                        }

                        throw;
                    }
                }

                return(new CounterStore
                {
                    Name = counterStorageName,
                    Url = parent.Url,
                    Credentials = credentials ?? parent.Credentials
                });
            }
Example #4
0
        public BackupOperation(CounterStorage storage, string backupSourceDirectory, string backupDestinationDirectory, StorageEnvironment env, bool incrementalBackup, CounterStorageDocument counterDocument)
        {
            this.storage = storage;
            this.backupDestinationDirectory = backupDestinationDirectory;
            this.env = env;
            this.incrementalBackup     = incrementalBackup;
            this.counterDocument       = counterDocument;
            this.backupSourceDirectory = backupSourceDirectory;
            backupFilename             = counterDocument.Id + ".Voron.Backup";

            if (incrementalBackup)
            {
                PrepareForIncrementalBackup();
            }
        }
Example #5
0
        public static bool IsClusterDatabase(this CounterStorageDocument document)
        {
            string value;

            if (document.Settings.TryGetValue(Constants.Cluster.NonClusterDatabaseMarker, out value) == false)
            {
                return(true);
            }

            bool result;

            if (bool.TryParse(value, out result) == false)
            {
                return(true);
            }

            return(!result);
        }
Example #6
0
        protected InMemoryRavenConfiguration CreateConfiguration(
            string tenantId,
            CounterStorageDocument document,
            string folderPropName,
            InMemoryRavenConfiguration parentConfiguration)
        {
            var config = new InMemoryRavenConfiguration
            {
                Settings = new NameValueCollection(parentConfiguration.Settings),
            };

            SetupTenantConfiguration(config);

            config.CustomizeValuesForCounterStorageTenant(tenantId);

            /*config.Settings[Constants.Counter.DataDirectory] = parentConfiguration.Counter.DataDirectory;
             * //config.Settings["Raven/StorageEngine"] = parentConfiguration.DefaultStorageTypeName;
             * //TODO: what counters storage dir path?
             * //config.Settings["Raven/Counters/Storage"] = parentConfiguration.FileSystem.DefaultStorageTypeName;*/

            foreach (var setting in document.Settings)
            {
                config.Settings[setting.Key] = setting.Value;
            }
            Unprotect(document);

            foreach (var securedSetting in document.SecuredSettings)
            {
                config.Settings[securedSetting.Key] = securedSetting.Value;
            }

            config.Settings[folderPropName] = config.Settings[folderPropName].ToFullPath(parentConfiguration.Counter.DataDirectory);
            //config.Settings["Raven/Esent/LogsPath"] = config.Settings["Raven/Esent/LogsPath"].ToFullPath(parentConfiguration.DataDirectory);
            config.Settings[Constants.RavenTxJournalPath] = config.Settings[Constants.RavenTxJournalPath].ToFullPath(parentConfiguration.DataDirectory);

            config.Settings["Raven/VirtualDir"] = config.Settings["Raven/VirtualDir"] + "/" + tenantId;

            config.CounterStorageName = tenantId;

            config.Initialize();
            config.CopyParentSettings(parentConfiguration);
            return(config);
        }
Example #7
0
        public void Protect(CounterStorageDocument configDocument)
        {
            if (configDocument.SecuredSettings == null)
            {
                configDocument.SecuredSettings = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
                return;
            }

            foreach (var prop in configDocument.SecuredSettings.ToList())
            {
                if (prop.Value == null)
                {
                    continue;
                }
                var bytes          = Encoding.UTF8.GetBytes(prop.Value);
                var entrophy       = Encoding.UTF8.GetBytes(prop.Key);
                var protectedValue = ProtectedData.Protect(bytes, entrophy, DataProtectionScope.CurrentUser);
                configDocument.SecuredSettings[prop.Key] = Convert.ToBase64String(protectedValue);
            }
        }
Example #8
0
        public async Task Should_be_able_to_create_multiple_counter_storages()
        {
            var expectedClientNames = new ReadOnlyCollection <string>(new List <string>()
            {
                CounterStorageName + "A", CounterStorageName + "B", CounterStorageName + "C"
            });

            using (var store = NewRemoteCountersStore(DefaultCounterStorageName, createDefaultCounter: false))
            {
                var defaultCountersDocument = new CounterStorageDocument();
                await store.Admin.CreateCounterStorageAsync(defaultCountersDocument, expectedClientNames[0]);

                await store.Admin.CreateCounterStorageAsync(defaultCountersDocument, expectedClientNames[1]);

                await store.Admin.CreateCounterStorageAsync(defaultCountersDocument, expectedClientNames[2]);

                var counterStorageNames = (await store.Admin.GetCounterStoragesNamesAsync());
                Assert.Equal(counterStorageNames, expectedClientNames);
            }
        }