コード例 #1
0
        /// <summary>
        /// Sets up the sql database for SonarQube service.
        /// </summary>
        void SetupSonarQubeDatabaseForExpressInstallation()
        {
            string databaseName = BootstrapperManager.Instance.Bootstrapper.Engine.StringVariables["DATABASENAME"];
            string userName     = BootstrapperManager.Instance.Bootstrapper.Engine.StringVariables["DATABASEUSERNAME"];
            string password     = BootstrapperManager.Instance.Bootstrapper.Engine.StringVariables["DATABASEPASSWORD"];
            string dataSource   = BootstrapperManager.Instance.Bootstrapper.Engine.StringVariables["INSTANCE"];
            string sqlAuthType  = BootstrapperManager.Instance.Bootstrapper.Engine.StringVariables["SQLAUTHTYPE"];
            string installSql   = BootstrapperManager.Instance.Bootstrapper.Engine.StringVariables["INSTALLSQL"];

            // Scenario 1 - Sql New Installation
            //              a) Windows Authentication
            //                 -- Only create db (local user gets permissions by default)
            //              b) Sql Authentication
            //                 -- Create login with given credentials -> create db -> create user with created login -> make user db_owner
            // Scenario 2 - Sql Old Installation
            //              a) Windows Authentication
            //                 -- Create db
            //              b) Sql Authentication
            //                 -- Create db using given login -> create user for given login -> make user db_owner

            if (AuthenticationType.Windows.Equals(sqlAuthType, StringComparison.InvariantCultureIgnoreCase))
            {
                using (SqlConnection connection = SqlServerHelper.CreateSqlConnectionWithWindowsLogin(dataSource))
                {
                    connection.Open();

                    connection.CreateSqlServerDatabase(databaseName);
                }
            }
            else
            {
                // If sql has been installed by our installer and user has chosen the option to have sql authentication,
                // we will have to use windows authentication to create that login.
                if (installSql.Equals("TRUE", StringComparison.InvariantCultureIgnoreCase))
                {
                    using (SqlConnection connection = SqlServerHelper.CreateSqlConnectionWithWindowsLogin(dataSource))
                    {
                        connection.Open();

                        connection.CreateNewSqlServerLogin(userName, password);
                        connection.CreateSqlServerDatabase(databaseName);
                    }

                    using (SqlConnection connection = SqlServerHelper.CreateSqlConnectionWithSqlLogin(dataSource, userName, password, databaseName))
                    {
                        connection.Open();

                        connection.CreateSqlUserFromSqlLogin(userName, userName);
                        connection.ChangeSqlUserDbRole("db_owner", userName);
                    }
                }
                else
                {
                    using (SqlConnection connection = SqlServerHelper.CreateSqlConnectionWithSqlLogin(dataSource, userName, password))
                    {
                        connection.Open();

                        connection.CreateSqlServerDatabase(databaseName);
                    }

                    using (SqlConnection connection = SqlServerHelper.CreateSqlConnectionWithSqlLogin(dataSource, userName, password, databaseName))
                    {
                        connection.Open();

                        connection.CreateSqlUserFromSqlLogin(userName, userName);
                        connection.ChangeSqlUserDbRole("db_owner", userName);
                    }
                }

                // For sql authentication, SonarQube service runs as SYSTEM,
                // We need to give permission to this account in db.
                using (SqlConnection connection = SqlServerHelper.CreateSqlConnectionWithSqlLogin(dataSource, userName, password, databaseName))
                {
                    connection.Open();

                    connection.CreateSqlUserFromSqlLogin("NTSYSTEM", @"NT AUTHORITY\SYSTEM");
                    connection.ChangeSqlUserDbRole("db_owner", "NTSYSTEM");
                }
            }
        }