From ac461a8d03333914884f0d51ff6b28128fe8faab Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Mon, 8 Sep 2014 13:57:45 +0000 Subject: [PATCH] Install with database permissions check WORKS. Preparing to install live hosted server. --- Basic/Enums/RoleTypeClass.cs | 6 +- Database/Database-Constructors.cs | 12 ++ Database/Database-Tests.cs | 200 ++++++++++++++++++++++++++++ Database/Database.csproj | 1 + Site5/Default.aspx | 4 +- Site5/Pages/v5/Init/Default.aspx | 42 ++++-- Site5/Pages/v5/Init/Default.aspx.cs | 166 +++++++++++++++++++---- 7 files changed, 390 insertions(+), 41 deletions(-) create mode 100644 Database/Database-Tests.cs diff --git a/Basic/Enums/RoleTypeClass.cs b/Basic/Enums/RoleTypeClass.cs index 757a9150..913737ca 100644 --- a/Basic/Enums/RoleTypeClass.cs +++ b/Basic/Enums/RoleTypeClass.cs @@ -114,7 +114,11 @@ namespace Swarmops.Basic.Enums /// /// Role for non officers helping with ballot distribution etc /// - LocalElectionSupport = 25 + LocalElectionSupport = 25, + /// + /// Temporary - Jörgen added this; the whole enum is deprecated anyway; added for compatibility with v4 + /// + OrganizationMailSender } diff --git a/Database/Database-Constructors.cs b/Database/Database-Constructors.cs index 17e07727..a449c619 100644 --- a/Database/Database-Constructors.cs +++ b/Database/Database-Constructors.cs @@ -30,6 +30,18 @@ namespace Swarmops.Database return new SwarmDb(ConstructConnectString(new Configuration().Get().Admin)); } + public static SwarmDb GetTestDatabase(Credentials credentials) + { + // For security reasons, this function is only available BEFORE the database has been initialized. + + if (Configuration.IsConfigured()) + { + throw new UnauthorizedAccessException("Cannot probe arbitrary database credentials once database initialized"); + } + + return new SwarmDb(ConstructConnectString(credentials)); + } + private static string ConstructConnectString (Credentials credentials) { return "server=" + credentials.ServerSet.ServerPriorities[0].Split(';')[0] + ";database=" + credentials.Database + diff --git a/Database/Database-Tests.cs b/Database/Database-Tests.cs new file mode 100644 index 00000000..7b502894 --- /dev/null +++ b/Database/Database-Tests.cs @@ -0,0 +1,200 @@ +using System; +using System.Data; +using System.Data.Common; +using System.Diagnostics; +using MySql.Data; +using MySql.Data.MySqlClient; + +namespace Swarmops.Database +{ + public partial class SwarmDb + { + public bool TestLogin() + { + try + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + return true; + } + } + catch (Exception) + { + return false; + } + } + + public bool TestCreateTable() + { + try + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand( + "CREATE TABLE `CredentialsTests` (" + + " `TestId` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT," + + " `Description` VARCHAR(256) NOT NULL," + + " `DateTimeCreated` DATETIME NOT NULL," + + " PRIMARY KEY (`TestId`)" + + ") ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='This table is created to test database credentials'", connection); + + command.ExecuteNonQuery(); + return true; + } + } + catch (Exception debug) + { + Debug.WriteLine(debug.ToString()); + return false; + } + } + + public bool TestAlterTable() + { + try + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand( + "ALTER TABLE `CredentialsTests` ADD COLUMN `AppendedColumn` INT AFTER `DateTimeCreated`", connection); + + command.ExecuteNonQuery(); + return true; + } + } + catch (Exception debug) + { + Debug.WriteLine(debug.ToString()); + return false; + } + } + + + public bool TestDropTable() + { + try + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand( + "DROP TABLE `CredentialsTests`", connection); + + command.ExecuteNonQuery(); + return true; + } + } + catch (Exception debug) + { + Debug.WriteLine(debug.ToString()); + return false; + } + } + + public bool TestCreateProcedure() + { + try + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand( + "CREATE PROCEDURE `CreateCredentialsTest`(" + + " description VARCHAR(128)" + + ")" + + "BEGIN" + + "" + + " INSERT INTO CredentialsTests (Description,DateTimeCreated)" + + " VALUES(description, NOW());" + + " " + + "END", connection); + + command.ExecuteNonQuery(); + return true; + } + } + catch (Exception debug) + { + Debug.WriteLine(debug.ToString()); + return false; + } + } + + public bool TestDropProcedure() + { + try + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand( + "DROP PROCEDURE `CreateCredentialsTest`", connection); + + command.ExecuteNonQuery(); + return true; + } + } + catch (Exception debug) + { + Debug.WriteLine(debug.ToString()); + return false; + } + } + + public bool TestExecute(string testDescription) + { + try + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + DbCommand command = GetDbCommand("CreateCredentialsTest", connection); + command.CommandType = CommandType.StoredProcedure; + + AddParameterWithName(command, "description", testDescription); + + command.ExecuteNonQuery(); + return true; + } + } + catch (Exception debug) + { + Debug.WriteLine(debug.ToString()); + return false; + } + } + + public bool TestSelect() + { + try + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = + GetDbCommand( + "SELECT * FROM CredentialsTests", connection); + DbDataReader reader = command.ExecuteReader(); + + return true; + } + } + catch (Exception debug) + { + Debug.WriteLine(debug.ToString()); + return false; + } + + } + + } +} \ No newline at end of file diff --git a/Database/Database.csproj b/Database/Database.csproj index fa5be156..1b49fdf0 100644 --- a/Database/Database.csproj +++ b/Database/Database.csproj @@ -124,6 +124,7 @@ + diff --git a/Site5/Default.aspx b/Site5/Default.aspx index 2425b28e..a50f54b9 100644 --- a/Site5/Default.aspx +++ b/Site5/Default.aspx @@ -5,10 +5,10 @@

Dashboard Placeholder

-

This system, Swarmops, is under heavy (re)construction. It has been the admin mainstay of the Swedish Pirate Party for six years, and is being generalized to encompass any organization.

+

This system, Swarmops, is under heavy (re)construction. It has been the admin mainstay of the Swedish Pirate Party since 2006, and is being generalized to encompass any organization.

What you see here is the new interface being rewritten on top of the already-running engine. It runs on Debian/Ubuntu servers for frontend and backend.

This notice will be replaced with a proper Dashboard when all the basic functions are in place. You can see functions appear as they are implemented in the menu.

-

A rough roadmap says that basic financials are implemented first, then member registry functions, then communications, then responsibilities, then governance. For more on the specific roadmap, go to the SCRUM planning for Swarmops.

+

A rough roadmap says that basic financials are implemented first, then member registry functions, then communications, then responsibilities, then governance. For more on the specific roadmap, go to the SCRUM planning for Swarmops.

diff --git a/Site5/Pages/v5/Init/Default.aspx b/Site5/Pages/v5/Init/Default.aspx index a821a9e0..0a573e7b 100644 --- a/Site5/Pages/v5/Init/Default.aspx +++ b/Site5/Pages/v5/Init/Default.aspx @@ -174,7 +174,10 @@ // Verify database credentials are good and permissions correct if (isValid) { - disableNext = true; // block Next during synchro call - it may take a while + DisableNext(); // block Next during synchro call - it may take a while + // also, won't get re-enabled no matter the outcome + $('#<%=this.ButtonTestCredentials.ClientID %>').click(); + $.ajax({ type: "POST", url: "Default.aspx/TestDatabasePermissions", @@ -189,11 +192,9 @@ updatePermissionsAnalysisDisplay(msg.d); $('#DivDatabaseWritable').fadeOut(400, 'swing', function() { $('#DivDatabasePermissionProblem').fadeIn(200); }); setTimeout('recheckDatabasePermissionsAnalysis();', 5000); // 5s until first re-check - DisableNext(); isValid = false; } else { - // re-enable Next - disableNext = false; + // re-enable Next only on exit of step 3 } } }); @@ -201,12 +202,12 @@ if (isValid) { - $('#DivProgressDatabase').progressbar({ value: 0, max: 100 }); setTimeout('updateInitProgressBar();', 1000); setTimeout('updateInitProgressMessage();', 1500); $('#<%=this.ButtonInitDatabase.ClientID %>').click(); beginInitDatabase(); + DisableNext(); } } @@ -285,10 +286,26 @@ disableNext = false; } + function ClickNext() { + $('a.buttonNext').click(); + } + + function ReturnToCredentials() { stopRechecking = true; - $('#DivDatabasePermissionProblem').fadeOut(200, 'swing', function () { $('#DivDatabaseWritable').fadeIn(200); EnableNext(); }); - } + + $.ajax({ + type: "POST", + url: "Default.aspx/ResetTestCredentials", + data: "{}", + contentType: "application/json; charset=utf-8", + dataType: "json", + success: function (msg) { + $('#DivDatabasePermissionProblem').fadeOut(200, 'swing', function () { $('#DivDatabaseWritable').fadeIn(200); EnableNext(); }); + } + }); + } + function recheckConfigurationWritability() { @@ -330,7 +347,8 @@ width: "100%" }, { queue: false }); databaseInitComplete = true; - $(".buttonNext").click(); + EnableNext(); // re-enable - it has been disabled throughout Init + ClickNext(); } else { // We're not done yet. Keep the progress bar on-screen and keep re-checking. @@ -388,13 +406,12 @@ contentType: "application/json; charset=utf-8", dataType: "json", success: function (msg) { + updatePermissionsAnalysisDisplay(msg.d); if (!msg.d.AllPermissionsOk) { - updatePermissionsAnalysisDisplay(msg.d); setTimeout('recheckDatabasePermissionsAnalysis();', 2000); } else { - alert('AllPermissionsOk = true'); EnableNext(); - $('#wizard').smartWizard('goForward'); // triggers a re-check, but doesn't matter, more maintainable this way + ClickNext(); } } }); @@ -587,7 +604,7 @@  
 
-
+
<
@@ -624,6 +641,7 @@ + diff --git a/Site5/Pages/v5/Init/Default.aspx.cs b/Site5/Pages/v5/Init/Default.aspx.cs index d3653861..e78ce7ee 100644 --- a/Site5/Pages/v5/Init/Default.aspx.cs +++ b/Site5/Pages/v5/Init/Default.aspx.cs @@ -196,25 +196,159 @@ public partial class Pages_v5_Init_Default : System.Web.UI.Page } } + if (_testReadCredentials == null) + { + throw new InvalidOperationException("Can't init database before it has been tested - invalid state"); + } + // Store database credentials SwarmDb.Configuration.Set( new SwarmDb.Configuration( - new SwarmDb.Credentials( + _testReadCredentials, + _testWriteCredentials, + _testAdminCredentials)); + } + + protected void ButtonTestCredentials_Click(object sender, EventArgs args) + { + // Check the host names and addresses again as a security measure - after all, we can be called from outside our intended script + + if (!(VerifyHostName(this.TextServerName.Text) && VerifyHostAddress(this.TextServerAddress.Text))) + { + if (!System.Diagnostics.Debugger.IsAttached) + { + return; // Probable hack attempt - fail silently + } + } + + _testReadCredentials = new SwarmDb.Credentials( this.TextCredentialsReadDatabase.Text, new SwarmDb.ServerSet(this.TextCredentialsReadServer.Text), this.TextCredentialsReadUser.Text, - this.TextCredentialsReadPassword.Text), - new SwarmDb.Credentials(this.TextCredentialsWriteDatabase.Text, + this.TextCredentialsReadPassword.Text); + _testWriteCredentials = new SwarmDb.Credentials(this.TextCredentialsWriteDatabase.Text, new SwarmDb.ServerSet(this.TextCredentialsWriteServer.Text), this.TextCredentialsWriteUser.Text, - this.TextCredentialsWritePassword.Text), - new SwarmDb.Credentials(this.TextCredentialsAdminDatabase.Text, + this.TextCredentialsWritePassword.Text); + _testAdminCredentials = new SwarmDb.Credentials(this.TextCredentialsAdminDatabase.Text, new SwarmDb.ServerSet(this.TextCredentialsAdminServer.Text), this.TextCredentialsAdminUser.Text, - this.TextCredentialsAdminPassword.Text))); + this.TextCredentialsAdminPassword.Text); } + private static SwarmDb.Credentials _testReadCredentials; + private static SwarmDb.Credentials _testWriteCredentials; + private static SwarmDb.Credentials _testAdminCredentials; + + + [WebMethod(true)] + public static void ResetTestCredentials() + { + _testReadCredentials = + _testWriteCredentials = + _testAdminCredentials = null; + } + + + [WebMethod(true)] + public static PermissionsAnalysis TestDatabasePermissions() + { + while (_testReadCredentials == null || _testWriteCredentials == null || _testAdminCredentials == null) + { + Thread.Sleep(100); // A couple of async race conditions happen as this is called, we need to wait for credentials + } + + PermissionsAnalysis result = new PermissionsAnalysis(); + + // First, test ADMIN + + SwarmDb adminDb = SwarmDb.GetTestDatabase(_testAdminCredentials); + + // Drop table, procedure first just in case there's garbage left behind. Ignore result. + adminDb.TestDropTable(); + adminDb.TestDropProcedure(); + + // All these should pass. + result.AdminCredentialsCanLogin = adminDb.TestLogin(); + result.AdminCredentialsCanAdmin = adminDb.TestCreateTable(); + result.AdminCredentialsCanAdmin &= adminDb.TestDropTable(); + result.AdminCredentialsCanAdmin &= adminDb.TestCreateTable(); + result.AdminCredentialsCanAdmin &= adminDb.TestAlterTable(); + result.AdminCredentialsCanAdmin &= adminDb.TestCreateProcedure(); // AND -- all must succeed + result.AdminCredentialsCanAdmin &= adminDb.TestDropProcedure(); // Test DROP before we mess up the state of the table, procedure + result.AdminCredentialsCanAdmin &= adminDb.TestCreateProcedure(); // therefore, recreate it after the drop + + if (result.AdminCredentialsCanAdmin) // if we have a created table and procedure, otherwise default fail + { + result.AdminCredentialsCanExecute = adminDb.TestExecute("Admin Execute"); + result.AdminCredentialsCanSelect = adminDb.TestSelect(); + } + + // Within the created table, test WRITE and READ accounts before testing them on excessive rights. + + SwarmDb writeDb = SwarmDb.GetTestDatabase(_testWriteCredentials); + + result.WriteCredentialsCanLogin = writeDb.TestLogin(); + + if (result.WriteCredentialsCanLogin && result.AdminCredentialsCanAdmin) + { + result.WriteCredentialsCanExecute = writeDb.TestExecute("Write Execute"); + result.WriteCredentialsCanSelect = writeDb.TestSelect(); + } + + SwarmDb readDb = SwarmDb.GetTestDatabase(_testReadCredentials); + + result.ReadCredentialsCanLogin = readDb.TestLogin(); + + if (result.ReadCredentialsCanLogin && result.AdminCredentialsCanAdmin) + { + result.ReadCredentialsCanExecute = readDb.TestExecute("Read Execute"); + result.ReadCredentialsCanSelect = readDb.TestSelect(); + } + + // Finally, test the write and read accounts for admin rights. Note the "OR" here rather than "AND" - + // any one of these rights present should return a true, because it's a fail. + + if (result.ReadCredentialsCanLogin) + { + result.ReadCredentialsCanAdmin = readDb.TestDropProcedure(); + result.ReadCredentialsCanAdmin |= readDb.TestDropTable(); + result.ReadCredentialsCanAdmin |= readDb.TestCreateTable(); + result.ReadCredentialsCanAdmin |= readDb.TestCreateProcedure(); + } + + if (result.WriteCredentialsCanLogin) + { + result.WriteCredentialsCanAdmin = writeDb.TestDropProcedure(); + result.WriteCredentialsCanAdmin |= writeDb.TestDropTable(); + result.WriteCredentialsCanAdmin |= writeDb.TestCreateTable(); + result.WriteCredentialsCanAdmin |= writeDb.TestCreateProcedure(); + } + + // Clean up + + adminDb.TestDropTable(); // ignore result + adminDb.TestDropProcedure(); + + result.AllPermissionsOk = + result.AdminCredentialsCanLogin && + result.AdminCredentialsCanSelect && + result.AdminCredentialsCanExecute && + result.AdminCredentialsCanAdmin && + result.WriteCredentialsCanLogin && + result.WriteCredentialsCanSelect && + result.WriteCredentialsCanExecute && + !result.WriteCredentialsCanAdmin && // not this + result.ReadCredentialsCanLogin && + result.ReadCredentialsCanSelect && + !result.ReadCredentialsCanExecute && // not this + !result.ReadCredentialsCanAdmin; // not this + + return result; + } + + [WebMethod(true)] public static void InitDatabase() { @@ -614,26 +748,6 @@ public partial class Pages_v5_Init_Default : System.Web.UI.Page return SwarmDb.Configuration.TestConfigurationWritable(); } - [WebMethod(true)] - public static PermissionsAnalysis TestDatabasePermissions() - { - PermissionsAnalysis result = new PermissionsAnalysis(); - result.AllPermissionsOk = false; - result.ReadCredentialsCanLogin = RandomBool(); - result.ReadCredentialsCanSelect = RandomBool(); - result.ReadCredentialsCanExecute = RandomBool(); - result.ReadCredentialsCanAdmin = RandomBool(); - result.WriteCredentialsCanLogin = RandomBool(); - result.WriteCredentialsCanSelect = RandomBool(); - result.WriteCredentialsCanExecute = RandomBool(); - result.WriteCredentialsCanAdmin = RandomBool(); - result.AdminCredentialsCanLogin = RandomBool(); - result.AdminCredentialsCanSelect = RandomBool(); - result.AdminCredentialsCanExecute = RandomBool(); - result.AdminCredentialsCanAdmin = RandomBool(); - - return result; - } private static bool RandomBool() {