Skip to content

Commit

Permalink
Attempt to refactor logging
Browse files Browse the repository at this point in the history
Also added DefaultUser writing to the logs directory
  • Loading branch information
SimonFlapse authored and grilledham committed Aug 22, 2020
1 parent c91e887 commit 1920cd2
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions FactorioWebInterface/Services/DefaultAdminAccountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.AspNetCore.Identity;
using Serilog;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

Expand Down Expand Up @@ -35,7 +36,7 @@ public async Task SetupDefaultUserAsync()
{
case AccountsNumbers.DefaultIsOnlyRootAccount:
case AccountsNumbers.OnlyDefaultAccount:
Log.Information(Constants.DefaultAdminAccount + " could not be created, another account already exists");
Log.Information("{UserId} could not be created, another account already exists", Constants.DefaultAdminAccount);
return;
case AccountsNumbers.MultipleAccounts:
await ValidateOrClearDefaultUserAsync(id, true);
Expand Down Expand Up @@ -79,7 +80,7 @@ private async Task ValidateOrClearDefaultUserAsync(string id, bool force = false
ApplicationUser userResult = await _userManager.FindByIdAsync(id);
if (await ValidateDefaultUserAsync(userResult) && !force)
{
Log.Information("Valid " + Constants.DefaultAdminAccount + " already exists");
Log.Information("Valid {UserId} already exists", Constants.DefaultAdminAccount);
return;
}

Expand All @@ -88,9 +89,9 @@ private async Task ValidateOrClearDefaultUserAsync(string id, bool force = false
var deleteResult = await _userManager.DeleteAsync(userResult);
if (!deleteResult.Succeeded)
{
Log.Error(Constants.DefaultAdminAccount + " couldn't be deleted! This may pose a security risk for your application. Will attempt to delete again at next reboot");
Log.Error("{UserId} couldn't be deleted! This may pose a security risk for your application. Will attempt to delete again at next reboot", Constants.DefaultAdminAccount);
}
Log.Information(Constants.DefaultAdminAccount + " deleted");
Log.Information("{UserId} deleted", Constants.DefaultAdminAccount);
}
}

Expand Down Expand Up @@ -125,32 +126,57 @@ private async Task CreateDefaultUserAsync(string id)
var result = await _userManager.CreateAsync(user);
if (!result.Succeeded)
{
Log.Error("Couldn't create " + Constants.DefaultAdminAccount);
Log.Error("Couldn't create {UserId}", Constants.DefaultAdminAccount);
return;
}

result = await _userManager.AddToRoleAsync(user, Constants.RootRole);
if (!result.Succeeded)
{
Log.Error("Couldn't add role to " + Constants.DefaultAdminAccount);
Log.Error("Couldn't add role to {UserId}", Constants.DefaultAdminAccount);
return;
}

result = await _userManager.AddToRoleAsync(user, Constants.AdminRole);
if (!result.Succeeded)
{
Log.Error("Couldn't add role to " + Constants.DefaultAdminAccount);
Log.Error("Couldn't add role to {UserId}", Constants.DefaultAdminAccount);
return;
}

string password = Guid.NewGuid().ToString();
result = await _userManager.AddPasswordAsync(user, password);
if (!result.Succeeded)
{
Log.Error("Couldn't add password to " + Constants.DefaultAdminAccount);
Log.Error("Couldn't add password to {UserId} ", Constants.DefaultAdminAccount);
return;
}
Log.Warning(Constants.DefaultAdminAccount + " named \'" + userName + "\' created with the password: " + password + "\nThis action potential exposes your interface, creating a new account and restarting this web interface will disable the default admin account");
Log.Warning("{UserId} created. This action potential exposes your interface, creating a new account and restarting this web interface will disable the default admin account", Constants.DefaultAdminAccount);

string warningTag = "! - Warning - !";
var path = GetDefaultAccountFilePath();
RemoveDefaultAccountFile();
File.WriteAllText(path, $"{warningTag}\nThis account is unsecure. Please setup a personal account.\n{warningTag}\nUsername: {userName}\nPassword: {password}");
}

private void RemoveDefaultAccountFile()
{
var path = GetDefaultAccountFilePath();
try
{
File.Delete(path);
} catch {
Log.Information("Couldn't delete DefaultAccount file");
return;
}
Log.Information("DefaultAccount file deleted");
}

private string GetDefaultAccountFilePath()
{
string path = AppDomain.CurrentDomain.BaseDirectory!;
path = Path.Combine(path, "logs/DefaultUser.txt");
return path;
}
}
}

0 comments on commit 1920cd2

Please sign in to comment.