Install with database permissions check WORKS. Preparing to install live hosted server.

This commit is contained in:
Rick Falkvinge
2014-09-08 13:57:45 +00:00
parent 377905c6e6
commit ac461a8d03
7 changed files with 390 additions and 41 deletions
+5 -1
View File
@@ -114,7 +114,11 @@ namespace Swarmops.Basic.Enums
/// <summary>
/// Role for non officers helping with ballot distribution etc
/// </summary>
LocalElectionSupport = 25
LocalElectionSupport = 25,
/// <summary>
/// Temporary - Jörgen added this; the whole enum is deprecated anyway; added for compatibility with v4
/// </summary>
OrganizationMailSender
}
+12
View File
@@ -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 +
+200
View File
@@ -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;
}
}
}
}
+1
View File
@@ -124,6 +124,7 @@
<Compile Include="Database-SwarmopsLog.cs" />
<Compile Include="Database-TaxLevels.cs" />
<Compile Include="Database-TemporaryIdentities.cs" />
<Compile Include="Database-Tests.cs" />
<Compile Include="Database-UrlTranslations.cs" />
<Compile Include="Database-Volunteers.cs" />
<Compile Include="Database-Permissions.cs" />
+2 -2
View File
@@ -5,10 +5,10 @@
<asp:Content ID="Content2" ContentPlaceHolderID="PlaceHolderMain" Runat="Server">
<h2>Dashboard Placeholder</h2>
<p>This system, <em>Swarmops,</em> 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.</p>
<p>This system, <em>Swarmops,</em> 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.</p>
<p>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.</p>
<p>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.</p>
<p>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 <a href="http://scrum.pirateacademy.eu/">SCRUM planning</a> for Swarmops.</p>
<p>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 <a href="http://scrum.pirateacademy.eu/p/SWARMOPS#releasePlan">SCRUM planning</a> for Swarmops.</p>
</asp:Content>
+30 -12
View File
@@ -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 @@
<asp:TextBox CssClass="textinput" ID="TextCredentialsAdminUser" runat="server" />&nbsp;<br />
<asp:TextBox CssClass="textinput" ID="TextCredentialsAdminPassword" TextMode="Password" runat="server" />&nbsp;<br />
</div>
<div style="display:none"><asp:Button runat="server" ID="ButtonInitDatabase" Text="You should not see this button" OnClick="ButtonInitDatabase_Click"/></div>
<div style="display:none"><asp:Button runat="server" ID="ButtonTestCredentials" Text="You should not see this button" OnClick="ButtonTestCredentials_Click"/><<asp:Button runat="server" ID="ButtonInitDatabase" Text="You should not see this button" OnClick="ButtonInitDatabase_Click"/></div>
</div>
</div>
<div id="step-3">
@@ -624,6 +641,7 @@
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ButtonInitDatabase" EventName="Click"/>
<asp:AsyncPostBackTrigger ControlID="ButtonTestCredentials" EventName="Click"/>
</Triggers>
</asp:UpdatePanel>
+140 -26
View File
@@ -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()
{