Fix core classes not being able to read configuration

This commit is contained in:
Daniel_I_Am 2020-10-16 21:12:25 +02:00
parent 951692e5fb
commit 858edf6851
No known key found for this signature in database
GPG Key ID: 80C428FCC9743E84
8 changed files with 45 additions and 83 deletions

View File

@ -13,30 +13,30 @@ namespace ChaosBot.ConfigHelpers
private static readonly Dictionary<string, IConfigurationDetails> ConfigurationFlags = new Dictionary<string, IConfigurationDetails> private static readonly Dictionary<string, IConfigurationDetails> ConfigurationFlags = new Dictionary<string, IConfigurationDetails>
{ {
{"Bot:Name", new ConfigurationDetails<string>("Bot:Name", "ChaosBot", true)}, {"Bot:Name", new ConfigurationDetails<string>("Bot:Name", "ChaosBot", true, false)},
{"Bot:Version", new ConfigurationDetails<string>("Bot:Version", "1.0.0", true)}, {"Bot:Version", new ConfigurationDetails<string>("Bot:Version", "1.0.0", true, false)},
{"WebServer:Port", new ConfigurationDetails<int>("WebServer:Port", 8080, true)}, {"WebServer:Port", new ConfigurationDetails<int>("WebServer:Port", 8080, true, true)},
{"WebServer:Debug", new ConfigurationDetails<bool>("WebServer:Debug", false, true)}, {"WebServer:Debug", new ConfigurationDetails<bool>("WebServer:Debug", false, true, false)},
{"Discord:Prefix", new ConfigurationDetails<string>("Discord:Prefix", "!", true)}, {"Discord:Prefix", new ConfigurationDetails<string>("Discord:Prefix", "!", true, false)},
{"Discord:Token", new ConfigurationDetails<string>("Discord:Token", "SECRET_TOKEN", true)}, {"Discord:Token", new ConfigurationDetails<string>("Discord:Token", "SECRET_TOKEN", true, true)},
{"Discord:BaseUri", new ConfigurationDetails<string>("Discord:BaseUri", "http://localhost:8080/", true)}, {"Discord:BaseUri", new ConfigurationDetails<string>("Discord:BaseUri", "http://localhost:8080/", true, false)},
{"Discord:ClientId", new ConfigurationDetails<string>("Discord:ClientId", "1234567890", true)}, {"Discord:ClientId", new ConfigurationDetails<string>("Discord:ClientId", "1234567890", true, false)},
{"Discord:ClientSecret", new ConfigurationDetails<string>("Discord:ClientSecret", "1234567890_SECRET_TOKEN", true)}, {"Discord:ClientSecret", new ConfigurationDetails<string>("Discord:ClientSecret", "1234567890_SECRET_TOKEN", true, true)},
{"Lodestone:ChaosBotApi:ApiToken", new ConfigurationDetails<string>("Lodestone:ChaosBotApi:ApiToken", "SECRET_TOKEN", true)}, {"Lodestone:ChaosBotApi:ApiToken", new ConfigurationDetails<string>("Lodestone:ChaosBotApi:ApiToken", "SECRET_TOKEN", true, true)},
{"Lodestone:ChaosBotApi:Url", new ConfigurationDetails<string>("Lodestone:ChaosBotApi:Url", "http://locahost:8000", true)}, {"Lodestone:ChaosBotApi:Url", new ConfigurationDetails<string>("Lodestone:ChaosBotApi:Url", "http://locahost:8000", true, true)},
{"Database:Host", new ConfigurationDetails<string>("Database:Host", "localhost", true)}, {"Database:Host", new ConfigurationDetails<string>("Database:Host", "localhost", true, true)},
{"Database:Port", new ConfigurationDetails<int>("Database:Port", 3306, true)}, {"Database:Port", new ConfigurationDetails<int>("Database:Port", 3306, true, true)},
{"Database:User", new ConfigurationDetails<string>("Database:User", "root", true)}, {"Database:User", new ConfigurationDetails<string>("Database:User", "root", true, true)},
{"Database:Pass", new ConfigurationDetails<string>("Database:Pass", "password", true)}, {"Database:Pass", new ConfigurationDetails<string>("Database:Pass", "password", true, true)},
{"Database:Name", new ConfigurationDetails<string>("Database:Name", "chaosbot", true)}, {"Database:Name", new ConfigurationDetails<string>("Database:Name", "chaosbot", true, true)},
{"Module:Experience:Enabled", new ConfigurationDetails<bool>("Module:Experience:Enabled", true, false)}, {"Module:Experience:Enabled", new ConfigurationDetails<bool>("Module:Experience:Enabled", true, false, false)},
{"LevelUp:Channel", new ConfigurationDetails<string>("LevelUp:Channel", null, false)}, {"LevelUp:Channel", new ConfigurationDetails<string>("LevelUp:Channel", null, false, false)},
{"LevelUp:MentionUser", new ConfigurationDetails<bool>("LevelUp:MentionUser", true, false)}, {"LevelUp:MentionUser", new ConfigurationDetails<bool>("LevelUp:MentionUser", true, false, false)},
}; };
public Configuration() public Configuration()
@ -86,7 +86,8 @@ namespace ChaosBot.ConfigHelpers
public interface IConfigurationDetails public interface IConfigurationDetails
{ {
string Key { get; } string Key { get; }
bool Restricted { get; } bool WriteRestricted { get; }
bool ReadRestricted { get; }
Type Type { get; } Type Type { get; }
object DefaultValue { get; } object DefaultValue { get; }
string GetStringValue(ulong guildId); string GetStringValue(ulong guildId);
@ -98,24 +99,26 @@ namespace ChaosBot.ConfigHelpers
{ {
new T DefaultValue { get; } new T DefaultValue { get; }
T GetValue(ulong? guildId = null, bool readRestricted = false); T GetValue(ulong? guildId = null, bool readRestricted = false);
T GetValue(T defaultValue, ulong? guildId = null, bool readRestricted = false); T GetValue(T defaultValue, ulong? guildId = null, bool readValueEvenIfRestricted = false);
void SetValue(T value, ulong guildId); void SetValue(T value, ulong guildId);
} }
public class ConfigurationDetails<T> : IConfigurationDetails<T> public class ConfigurationDetails<T> : IConfigurationDetails<T>
{ {
public string Key { get; } public string Key { get; }
public bool Restricted { get; } public bool WriteRestricted { get; }
public bool ReadRestricted { get; }
public T DefaultValue { get; } public T DefaultValue { get; }
object IConfigurationDetails.DefaultValue => DefaultValue; object IConfigurationDetails.DefaultValue => DefaultValue;
public Type Type => typeof(T); public Type Type => typeof(T);
public ConfigurationDetails(string key, T defaultValue, bool restricted) public ConfigurationDetails(string key, T defaultValue, bool writeRestricted, bool readRestricted)
{ {
Key = key; Key = key;
DefaultValue = defaultValue; DefaultValue = defaultValue;
Restricted = restricted; WriteRestricted = writeRestricted;
ReadRestricted = readRestricted;
} }
public T GetValue(ulong? guildId = null, bool readRestricted = false) public T GetValue(ulong? guildId = null, bool readRestricted = false)
@ -123,9 +126,9 @@ namespace ChaosBot.ConfigHelpers
return GetValue(DefaultValue, guildId, readRestricted); return GetValue(DefaultValue, guildId, readRestricted);
} }
public T GetValue(T defaultValue, ulong? guildId = null, bool readRestricted = false) public T GetValue(T defaultValue, ulong? guildId = null, bool readValueEvenIfRestricted = false)
{ {
if (!readRestricted && Restricted) if (!readValueEvenIfRestricted && ReadRestricted)
throw new UnauthorizedAccessException($"Configuration key '{Key}' is restricted"); throw new UnauthorizedAccessException($"Configuration key '{Key}' is restricted");
if (guildId.HasValue) if (guildId.HasValue)
@ -143,7 +146,7 @@ namespace ChaosBot.ConfigHelpers
public void SetValue(T value, ulong guildId) public void SetValue(T value, ulong guildId)
{ {
if (Restricted) if (WriteRestricted)
throw new UnauthorizedAccessException($"Configuration key '{Key}' is restricted"); throw new UnauthorizedAccessException($"Configuration key '{Key}' is restricted");
ConfigurationRepository.SetValue(Key, value, guildId); ConfigurationRepository.SetValue(Key, value, guildId);
} }
@ -155,7 +158,7 @@ namespace ChaosBot.ConfigHelpers
public void DeleteValue(ulong guildId) public void DeleteValue(ulong guildId)
{ {
if (Restricted) if (WriteRestricted)
throw new UnauthorizedAccessException($"Configuration key '{Key}' is restricted"); throw new UnauthorizedAccessException($"Configuration key '{Key}' is restricted");
ConfigurationRepository.DeleteValue(Key, guildId); ConfigurationRepository.DeleteValue(Key, guildId);
} }

View File

@ -34,7 +34,7 @@ namespace ChaosBot.Discord
Configuration config = new Configuration(); Configuration config = new Configuration();
// this is where we get the Token value from the configuration file, and start the bot // this is where we get the Token value from the configuration file, and start the bot
await client.LoginAsync(TokenType.Bot, config.GetByKey<string>("Discord:Token").GetValue()); await client.LoginAsync(TokenType.Bot, config.GetByKey<string>("Discord:Token").GetValue(readRestricted: true));
await client.StartAsync(); await client.StartAsync();
// we get the CommandHandler class here and call the InitializeAsync method to start things up for the CommandHandler service // we get the CommandHandler class here and call the InitializeAsync method to start things up for the CommandHandler service

View File

@ -81,13 +81,10 @@ namespace ChaosBot.Discord.Modules.Admin
try try
{ {
if ((key != null) && (value != null) ) if ((key != null) && (value != null) )
{
if(RestrictedConfig.IsAllowed(key))
{ {
new Configuration().GetByKey(key).SetValueFromString(value, Context.Guild.Id); new Configuration().GetByKey(key).SetValueFromString(value, Context.Guild.Id);
await ConfigGet(key, true); await ConfigGet(key, true);
} }
}
else else
{ {
await ConfigHelp(); await ConfigHelp();
@ -104,7 +101,7 @@ namespace ChaosBot.Discord.Modules.Admin
{ {
try try
{ {
if ((key != null) && (RestrictedConfig.IsAllowed(key))) if ((key != null))
{ {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
EmbedBuilder embed = new EmbedBuilder(); EmbedBuilder embed = new EmbedBuilder();
@ -159,7 +156,7 @@ namespace ChaosBot.Discord.Modules.Admin
{ {
try try
{ {
if ((key != null) && (RestrictedConfig.IsAllowed(key))) if ((key != null))
{ {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
EmbedBuilder embed = new EmbedBuilder(); EmbedBuilder embed = new EmbedBuilder();

View File

@ -110,8 +110,8 @@ namespace ChaosBot.Discord.Modules.Admin
using HttpClient client = new HttpClient(); using HttpClient client = new HttpClient();
Configuration config = new Configuration(); Configuration config = new Configuration();
string endpoint =config.GetByKey<string>("Lodestone:ChaosBotApi:Url").GetValue(); string endpoint =config.GetByKey<string>("Lodestone:ChaosBotApi:Url").GetValue(readRestricted: true);
string apiToken = config.GetByKey<string>("Lodestone:ChaosBotApi:ApiToken").GetValue(); string apiToken = config.GetByKey<string>("Lodestone:ChaosBotApi:ApiToken").GetValue(readRestricted: true);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiToken); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiToken);
HttpResponseMessage result = HttpResponseMessage result =

View File

@ -38,11 +38,11 @@ namespace ChaosBot.Models
{ {
ConfigHelpers.Configuration config = new ConfigHelpers.Configuration(); ConfigHelpers.Configuration config = new ConfigHelpers.Configuration();
server = config.GetByKey<string>("Database:Host").GetValue(); server = config.GetByKey<string>("Database:Host").GetValue(readRestricted: true);
port = config.GetByKey<int>("Database:Port").GetValue(); port = config.GetByKey<int>("Database:Port").GetValue(readRestricted: true);
user = config.GetByKey<string>("Database:User").GetValue(); user = config.GetByKey<string>("Database:User").GetValue(readRestricted: true);
pass = config.GetByKey<string>("Database:Pass").GetValue(); pass = config.GetByKey<string>("Database:Pass").GetValue(readRestricted: true);
name = config.GetByKey<string>("Database:Name").GetValue(); name = config.GetByKey<string>("Database:Name").GetValue(readRestricted: true);
} }
optionsBuilder.UseMySql( optionsBuilder.UseMySql(

View File

@ -1,38 +0,0 @@
using System;
using System.Collections.Generic;
namespace ChaosBot.Services
{
public static class RestrictedConfig
{
public static bool IsAllowed(string key)
{
// TODO: List populated from DB
List<string> restrictedCfg = new List<string>
{
"Bot:Name",
"Bot:Version",
"WebServer:Port",
"WebServer:Debug",
"Discord:Prefix",
"Discord:Token",
"Discord:BaseUri",
"Discord:ClientId",
"Discord:ClientSecret",
"Lodestone:ChaosBotApi:ApiToken",
"Lodestone:ChaosBotApi:Url",
"Database:Host",
"Database:Port",
"Database:User",
"Database:Pass",
"Database:Name",
};
return !restrictedCfg.Contains(key);
}
}
}

View File

@ -40,7 +40,7 @@ namespace ChaosBot.WebServer.App
string redirectUri = $"{config.GetByKey<string>("Discord:BaseUri").GetValue()}/api/discord"; string redirectUri = $"{config.GetByKey<string>("Discord:BaseUri").GetValue()}/api/discord";
string clientId = config.GetByKey<string>("Discord:ClientId").GetValue(); string clientId = config.GetByKey<string>("Discord:ClientId").GetValue();
string clientSecret = config.GetByKey<string>("Discord:ClientSecret").GetValue(); string clientSecret = config.GetByKey<string>("Discord:ClientSecret").GetValue(readRestricted: true);
if (code == null) if (code == null)
return Redirect($"https://discord.com/api/oauth2/authorize?client_id={clientId}&redirect_uri={redirectUri}&response_type=code&scope=identify%20guilds"); return Redirect($"https://discord.com/api/oauth2/authorize?client_id={clientId}&redirect_uri={redirectUri}&response_type=code&scope=identify%20guilds");

View File

@ -29,7 +29,7 @@ namespace ChaosBot.WebServer
webBuilder.UseWebRoot(webRoot); webBuilder.UseWebRoot(webRoot);
webBuilder.ConfigureKestrel(serverOptions => webBuilder.ConfigureKestrel(serverOptions =>
{ {
serverOptions.Listen(IPAddress.Any, config.GetByKey<int>("WebServer:Port").GetValue(), serverOptions.Listen(IPAddress.Any, config.GetByKey<int>("WebServer:Port").GetValue(readRestricted: true),
listenOptions => listenOptions =>
{ {
listenOptions.UseConnectionLogging(); listenOptions.UseConnectionLogging();