Exemple #1
0
        private static void MigrateDatabase(int attempt = 1)
        {
            try
            {
                Console.WriteLine("Migrating database.");

                var dbPass = Helpers.GetValueFronEnvFile("mssql", "SA_PASSWORD");
                var masterConnectionString = Helpers.MakeSqlConnectionString(
                    "mssql", "master", "sa", dbPass ?? string.Empty);
                var vaultConnectionString = Helpers.MakeSqlConnectionString(
                    "mssql", "vault", "sa", dbPass ?? string.Empty);

                using (var connection = new SqlConnection(masterConnectionString))
                {
                    var command = new SqlCommand(
                        "IF ((SELECT COUNT(1) FROM sys.databases WHERE [name] = 'vault') = 0) " +
                        "CREATE DATABASE [vault];", connection);
                    command.Connection.Open();
                    command.ExecuteNonQuery();
                    command.CommandText = "IF ((SELECT DATABASEPROPERTYEX([name], 'IsAutoClose') " +
                                          "FROM sys.databases WHERE [name] = 'vault') = 1) " +
                                          "ALTER DATABASE [vault] SET AUTO_CLOSE OFF;";
                    command.ExecuteNonQuery();
                }

                var upgrader = DeployChanges.To
                               .SqlDatabase(vaultConnectionString)
                               .JournalToSqlTable("dbo", "Migration")
                               .WithScriptsAndCodeEmbeddedInAssembly(Assembly.GetExecutingAssembly(),
                                                                     s => s.Contains($".DbScripts.") && !s.Contains(".Archive."))
                               .WithTransaction()
                               .WithExecutionTimeout(new TimeSpan(0, 5, 0))
                               .LogToConsole()
                               .Build();

                var result = upgrader.PerformUpgrade();
                if (result.Successful)
                {
                    Console.WriteLine("Migration successful.");
                }
                else
                {
                    Console.WriteLine("Migration failed.");
                }
            }
            catch (SqlException e)
            {
                if (e.Message.Contains("Server is in script upgrade mode") && attempt < 10)
                {
                    var nextAttempt = attempt + 1;
                    Console.WriteLine("Database is in script upgrade mode. " +
                                      "Trying again (attempt #{0})...", nextAttempt);
                    System.Threading.Thread.Sleep(20000);
                    MigrateDatabase(nextAttempt);
                    return;
                }

                throw e;
            }
        }
Exemple #2
0
        private static void MigrateDatabase()
        {
            Console.WriteLine("Migrating database.");

            var dbPass = Helpers.GetValueFronEnvFile("mssql", "SA_PASSWORD");
            var masterConnectionString = Helpers.MakeSqlConnectionString("mssql", "master", "sa", dbPass ?? string.Empty);
            var vaultConnectionString  = Helpers.MakeSqlConnectionString("mssql", "vault", "sa", dbPass ?? string.Empty);

            using (var connection = new SqlConnection(masterConnectionString))
            {
                var command = new SqlCommand(
                    "IF ((SELECT COUNT(1) FROM sys.databases WHERE [name] = 'vault') = 0) CREATE DATABASE [vault];",
                    connection);
                command.Connection.Open();
                command.ExecuteNonQuery();
            }

            // Namespace changed, so migrate DbUp script names
            using (var connection = new SqlConnection(vaultConnectionString))
            {
                var command = new SqlCommand(
                    "IF ((SELECT COUNT(1) FROM INFORMATION_SCHEMA.TABLES " +
                    "WHERE TABLE_SCHEMA = 'dbo' AND TABLE_NAME = 'Migration') > 0) " +
                    "UPDATE [dbo].[Migration] " +
                    "SET [ScriptName] = REPLACE([ScriptName], 'Setup.DbScripts.', 'Bit.Setup.DbScripts.') " +
                    "WHERE [ScriptName] LIKE 'Setup.DbScripts.%';",
                    connection);
                command.Connection.Open();
                command.ExecuteNonQuery();
            }

            var upgrader = DeployChanges.To
                           .SqlDatabase(vaultConnectionString)
                           .JournalToSqlTable("dbo", "Migration")
                           .WithScriptsAndCodeEmbeddedInAssembly(Assembly.GetExecutingAssembly(),
                                                                 s => s.Contains($".DbScripts.") && !s.Contains(".Archive."))
                           .WithTransaction()
                           .WithExecutionTimeout(new TimeSpan(0, 5, 0))
                           .LogToConsole()
                           .Build();

            var result = upgrader.PerformUpgrade();

            if (result.Successful)
            {
                Console.WriteLine("Migration successful.");
            }
            else
            {
                Console.WriteLine("Migration failed.");
            }
        }
        private void Init()
        {
            var dbPassword         = Helpers.SecureRandomString(32);
            var dbConnectionString = Helpers.MakeSqlConnectionString("mssql", "vault", "sa", dbPassword);

            _globalOverrideValues = new Dictionary <string, string>
            {
                ["globalSettings__baseServiceUri__vault"]               = _context.Config.Url,
                ["globalSettings__baseServiceUri__api"]                 = $"{_context.Config.Url}/api",
                ["globalSettings__baseServiceUri__identity"]            = $"{_context.Config.Url}/identity",
                ["globalSettings__baseServiceUri__admin"]               = $"{_context.Config.Url}/admin",
                ["globalSettings__baseServiceUri__notifications"]       = $"{_context.Config.Url}/notifications",
                ["globalSettings__sqlServer__connectionString"]         = $"\"{dbConnectionString}\"",
                ["globalSettings__identityServer__certificatePassword"] = _context.Install?.IdentityCertPassword,
                ["globalSettings__attachment__baseDirectory"]           = $"{_context.OutputDir}/core/attachments",
                ["globalSettings__attachment__baseUrl"]                 = $"{_context.Config.Url}/attachments",
                ["globalSettings__dataProtection__directory"]           = $"{_context.OutputDir}/core/aspnet-dataprotection",
                ["globalSettings__logDirectory"]         = $"{_context.OutputDir}/logs",
                ["globalSettings__licenseDirectory"]     = $"{_context.OutputDir}/core/licenses",
                ["globalSettings__internalIdentityKey"]  = Helpers.SecureRandomString(64, alpha: true, numeric: true),
                ["globalSettings__duo__aKey"]            = Helpers.SecureRandomString(64, alpha: true, numeric: true),
                ["globalSettings__installation__id"]     = _context.Install?.InstallationId.ToString(),
                ["globalSettings__installation__key"]    = _context.Install?.InstallationKey,
                ["globalSettings__yubico__clientId"]     = "REPLACE",
                ["globalSettings__yubico__key"]          = "REPLACE",
                ["globalSettings__mail__replyToEmail"]   = $"no-reply@{_context.Config.Domain}",
                ["globalSettings__mail__smtp__host"]     = "REPLACE",
                ["globalSettings__mail__smtp__username"] = "******",
                ["globalSettings__mail__smtp__password"] = "******",
                ["globalSettings__mail__smtp__ssl"]      = "true",
                ["globalSettings__mail__smtp__port"]     = "587",
                ["globalSettings__mail__smtp__useDefaultCredentials"] = "false",
                ["globalSettings__disableUserRegistration"]           = "false",
                ["adminSettings__admins"] = string.Empty,
            };

            if (!_context.Config.PushNotifications)
            {
                _globalOverrideValues.Add("globalSettings__pushRelayBaseUri", "REPLACE");
            }

            _mssqlOverrideValues = new Dictionary <string, string>
            {
                ["ACCEPT_EULA"] = "Y",
                ["MSSQL_PID"]   = "Express",
                ["SA_PASSWORD"] = dbPassword,
            };
        }
Exemple #4
0
        private static void BuildEnvironmentFiles()
        {
            Console.WriteLine("Building docker environment override files.");
            Directory.CreateDirectory("/bitwarden/env/");
            var dbPass             = Helpers.SecureRandomString(32);
            var dbConnectionString = Helpers.MakeSqlConnectionString("mssql", "vault", "sa", dbPass);

            using (var sw = File.CreateText("/bitwarden/env/global.override.env"))
            {
                sw.Write($@"globalSettings__baseServiceUri__vault={_url}
globalSettings__baseServiceUri__api={_url}/api
globalSettings__baseServiceUri__identity={_url}/identity
globalSettings__sqlServer__connectionString=""{dbConnectionString}""
globalSettings__identityServer__certificatePassword={_identityCertPassword}
globalSettings__attachment__baseDirectory={_outputDir}/core/attachments
globalSettings__attachment__baseUrl={_url}/attachments
globalSettings__dataProtection__directory={_outputDir}/core/aspnet-dataprotection
globalSettings__logDirectory={_outputDir}/core/logs
globalSettings__licenseDirectory={_outputDir}/core/licenses
globalSettings__duo__aKey={Helpers.SecureRandomString(64, alpha: true, numeric: true)}
globalSettings__installation__id={_installationId}
globalSettings__installation__key={_installationKey}
globalSettings__yubico__clientId=REPLACE
globalSettings__yubico__key=REPLACE
globalSettings__mail__replyToEmail=no-reply@{_domain}
globalSettings__mail__smtp__host=REPLACE
globalSettings__mail__smtp__username=REPLACE
globalSettings__mail__smtp__password=REPLACE
globalSettings__mail__smtp__ssl=true
globalSettings__mail__smtp__port=587
globalSettings__mail__smtp__useDefaultCredentials=false
globalSettings__disableUserRegistration=false");

                if (!_push)
                {
                    sw.Write(@"
globalSettings__pushRelayBaseUri=REPLACE");
                }
            }

            using (var sw = File.CreateText("/bitwarden/env/mssql.override.env"))
            {
                sw.Write($@"ACCEPT_EULA=Y
MSSQL_PID=Express
SA_PASSWORD={dbPass}");
            }
        }
        private void Init(bool forInstall)
        {
            var dbConnectionString = Helpers.MakeSqlConnectionString("mssql", "vault", "sa", DatabasePassword);

            _globalValues = new Dictionary <string, string>
            {
                ["globalSettings__baseServiceUri__vault"]               = Url,
                ["globalSettings__baseServiceUri__api"]                 = $"{Url}/api",
                ["globalSettings__baseServiceUri__identity"]            = $"{Url}/identity",
                ["globalSettings__sqlServer__connectionString"]         = $"\"{ dbConnectionString }\"",
                ["globalSettings__identityServer__certificatePassword"] = IdentityCertPassword,
                ["globalSettings__attachment__baseDirectory"]           = $"{OutputDirectory}/core/attachments",
                ["globalSettings__attachment__baseUrl"]                 = $"{Url}/attachments",
                ["globalSettings__dataProtection__directory"]           = $"{OutputDirectory}/core/aspnet-dataprotection",
                ["globalSettings__logDirectory"]         = $"{OutputDirectory}/core/logs",
                ["globalSettings__licenseDirectory"]     = $"{OutputDirectory}/core/licenses",
                ["globalSettings__duo__aKey"]            = $"{Helpers.SecureRandomString(64, alpha: true, numeric: true)}",
                ["globalSettings__installation__id"]     = InstallationId?.ToString(),
                ["globalSettings__installation__key"]    = InstallationKey,
                ["globalSettings__yubico__clientId"]     = "REPLACE",
                ["globalSettings__yubico__key"]          = "REPLACE",
                ["globalSettings__mail__replyToEmail"]   = $"no-reply@{Domain}",
                ["globalSettings__mail__smtp__host"]     = "REPLACE",
                ["globalSettings__mail__smtp__username"] = "******",
                ["globalSettings__mail__smtp__password"] = "******",
                ["globalSettings__mail__smtp__ssl"]      = "true",
                ["globalSettings__mail__smtp__port"]     = "587",
                ["globalSettings__mail__smtp__useDefaultCredentials"] = "false",
                ["globalSettings__disableUserRegistration"]           = "false",
            };

            if (forInstall && !Push)
            {
                _globalValues.Add("globalSettings__pushRelayBaseUri", "REPLACE");
            }

            _mssqlValues = new Dictionary <string, string>
            {
                ["ACCEPT_EULA"] = "Y",
                ["MSSQL_PID"]   = "Express",
                ["SA_PASSWORD"] = DatabasePassword,
            };
        }
Exemple #6
0
        private static void MigrateDatabase()
        {
            Console.WriteLine("Migrating database.");

            var dbPass = Helpers.GetValueFronEnvFile("mssql", "SA_PASSWORD");
            var masterConnectionString = Helpers.MakeSqlConnectionString(
                "mssql", "master", "sa", dbPass ?? string.Empty);
            var vaultConnectionString = Helpers.MakeSqlConnectionString(
                "mssql", "vault", "sa", dbPass ?? string.Empty);

            using (var connection = new SqlConnection(masterConnectionString))
            {
                var command = new SqlCommand(
                    "IF ((SELECT COUNT(1) FROM sys.databases WHERE [name] = 'vault') = 0) CREATE DATABASE [vault];",
                    connection);
                command.Connection.Open();
                command.ExecuteNonQuery();
            }

            var upgrader = DeployChanges.To
                           .SqlDatabase(vaultConnectionString)
                           .JournalToSqlTable("dbo", "Migration")
                           .WithScriptsAndCodeEmbeddedInAssembly(Assembly.GetExecutingAssembly(),
                                                                 s => s.Contains($".DbScripts.") && !s.Contains(".Archive."))
                           .WithTransaction()
                           .WithExecutionTimeout(new TimeSpan(0, 5, 0))
                           .LogToConsole()
                           .Build();

            var result = upgrader.PerformUpgrade();

            if (result.Successful)
            {
                Console.WriteLine("Migration successful.");
            }
            else
            {
                Console.WriteLine("Migration failed.");
            }
        }