From 858edf685151c6b1148b6152c6bad5bfa7a5c51a Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Fri, 16 Oct 2020 21:12:25 +0200 Subject: [PATCH] Fix core classes not being able to read configuration --- ChaosBot/ConfigHelpers/Configuration.cs | 59 +++++++++++---------- ChaosBot/Discord/DiscordConnect.cs | 2 +- ChaosBot/Discord/Modules/Admin/Config.cs | 11 ++-- ChaosBot/Discord/Modules/Admin/RankCheck.cs | 4 +- ChaosBot/Models/ChaosbotContext.cs | 10 ++-- ChaosBot/Services/RestrictedConfig.cs | 38 ------------- ChaosBot/WebServer/App/DiscordController.cs | 2 +- ChaosBot/WebServer/WebServer.cs | 2 +- 8 files changed, 45 insertions(+), 83 deletions(-) delete mode 100644 ChaosBot/Services/RestrictedConfig.cs diff --git a/ChaosBot/ConfigHelpers/Configuration.cs b/ChaosBot/ConfigHelpers/Configuration.cs index de6cd76..d65d0b8 100644 --- a/ChaosBot/ConfigHelpers/Configuration.cs +++ b/ChaosBot/ConfigHelpers/Configuration.cs @@ -13,30 +13,30 @@ namespace ChaosBot.ConfigHelpers private static readonly Dictionary ConfigurationFlags = new Dictionary { - {"Bot:Name", new ConfigurationDetails("Bot:Name", "ChaosBot", true)}, - {"Bot:Version", new ConfigurationDetails("Bot:Version", "1.0.0", true)}, + {"Bot:Name", new ConfigurationDetails("Bot:Name", "ChaosBot", true, false)}, + {"Bot:Version", new ConfigurationDetails("Bot:Version", "1.0.0", true, false)}, - {"WebServer:Port", new ConfigurationDetails("WebServer:Port", 8080, true)}, - {"WebServer:Debug", new ConfigurationDetails("WebServer:Debug", false, true)}, + {"WebServer:Port", new ConfigurationDetails("WebServer:Port", 8080, true, true)}, + {"WebServer:Debug", new ConfigurationDetails("WebServer:Debug", false, true, false)}, - {"Discord:Prefix", new ConfigurationDetails("Discord:Prefix", "!", true)}, - {"Discord:Token", new ConfigurationDetails("Discord:Token", "SECRET_TOKEN", true)}, - {"Discord:BaseUri", new ConfigurationDetails("Discord:BaseUri", "http://localhost:8080/", true)}, - {"Discord:ClientId", new ConfigurationDetails("Discord:ClientId", "1234567890", true)}, - {"Discord:ClientSecret", new ConfigurationDetails("Discord:ClientSecret", "1234567890_SECRET_TOKEN", true)}, + {"Discord:Prefix", new ConfigurationDetails("Discord:Prefix", "!", true, false)}, + {"Discord:Token", new ConfigurationDetails("Discord:Token", "SECRET_TOKEN", true, true)}, + {"Discord:BaseUri", new ConfigurationDetails("Discord:BaseUri", "http://localhost:8080/", true, false)}, + {"Discord:ClientId", new ConfigurationDetails("Discord:ClientId", "1234567890", true, false)}, + {"Discord:ClientSecret", new ConfigurationDetails("Discord:ClientSecret", "1234567890_SECRET_TOKEN", true, true)}, - {"Lodestone:ChaosBotApi:ApiToken", new ConfigurationDetails("Lodestone:ChaosBotApi:ApiToken", "SECRET_TOKEN", true)}, - {"Lodestone:ChaosBotApi:Url", new ConfigurationDetails("Lodestone:ChaosBotApi:Url", "http://locahost:8000", true)}, + {"Lodestone:ChaosBotApi:ApiToken", new ConfigurationDetails("Lodestone:ChaosBotApi:ApiToken", "SECRET_TOKEN", true, true)}, + {"Lodestone:ChaosBotApi:Url", new ConfigurationDetails("Lodestone:ChaosBotApi:Url", "http://locahost:8000", true, true)}, - {"Database:Host", new ConfigurationDetails("Database:Host", "localhost", true)}, - {"Database:Port", new ConfigurationDetails("Database:Port", 3306, true)}, - {"Database:User", new ConfigurationDetails("Database:User", "root", true)}, - {"Database:Pass", new ConfigurationDetails("Database:Pass", "password", true)}, - {"Database:Name", new ConfigurationDetails("Database:Name", "chaosbot", true)}, + {"Database:Host", new ConfigurationDetails("Database:Host", "localhost", true, true)}, + {"Database:Port", new ConfigurationDetails("Database:Port", 3306, true, true)}, + {"Database:User", new ConfigurationDetails("Database:User", "root", true, true)}, + {"Database:Pass", new ConfigurationDetails("Database:Pass", "password", true, true)}, + {"Database:Name", new ConfigurationDetails("Database:Name", "chaosbot", true, true)}, - {"Module:Experience:Enabled", new ConfigurationDetails("Module:Experience:Enabled", true, false)}, - {"LevelUp:Channel", new ConfigurationDetails("LevelUp:Channel", null, false)}, - {"LevelUp:MentionUser", new ConfigurationDetails("LevelUp:MentionUser", true, false)}, + {"Module:Experience:Enabled", new ConfigurationDetails("Module:Experience:Enabled", true, false, false)}, + {"LevelUp:Channel", new ConfigurationDetails("LevelUp:Channel", null, false, false)}, + {"LevelUp:MentionUser", new ConfigurationDetails("LevelUp:MentionUser", true, false, false)}, }; public Configuration() @@ -86,7 +86,8 @@ namespace ChaosBot.ConfigHelpers public interface IConfigurationDetails { string Key { get; } - bool Restricted { get; } + bool WriteRestricted { get; } + bool ReadRestricted { get; } Type Type { get; } object DefaultValue { get; } string GetStringValue(ulong guildId); @@ -98,24 +99,26 @@ namespace ChaosBot.ConfigHelpers { new T DefaultValue { get; } 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); } public class ConfigurationDetails : IConfigurationDetails { public string Key { get; } - public bool Restricted { get; } + public bool WriteRestricted { get; } + public bool ReadRestricted { get; } public T DefaultValue { get; } object IConfigurationDetails.DefaultValue => DefaultValue; 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; DefaultValue = defaultValue; - Restricted = restricted; + WriteRestricted = writeRestricted; + ReadRestricted = readRestricted; } public T GetValue(ulong? guildId = null, bool readRestricted = false) @@ -123,9 +126,9 @@ namespace ChaosBot.ConfigHelpers 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"); if (guildId.HasValue) @@ -143,7 +146,7 @@ namespace ChaosBot.ConfigHelpers public void SetValue(T value, ulong guildId) { - if (Restricted) + if (WriteRestricted) throw new UnauthorizedAccessException($"Configuration key '{Key}' is restricted"); ConfigurationRepository.SetValue(Key, value, guildId); } @@ -155,7 +158,7 @@ namespace ChaosBot.ConfigHelpers public void DeleteValue(ulong guildId) { - if (Restricted) + if (WriteRestricted) throw new UnauthorizedAccessException($"Configuration key '{Key}' is restricted"); ConfigurationRepository.DeleteValue(Key, guildId); } diff --git a/ChaosBot/Discord/DiscordConnect.cs b/ChaosBot/Discord/DiscordConnect.cs index 86914d5..878db02 100644 --- a/ChaosBot/Discord/DiscordConnect.cs +++ b/ChaosBot/Discord/DiscordConnect.cs @@ -34,7 +34,7 @@ namespace ChaosBot.Discord Configuration config = new Configuration(); // this is where we get the Token value from the configuration file, and start the bot - await client.LoginAsync(TokenType.Bot, config.GetByKey("Discord:Token").GetValue()); + await client.LoginAsync(TokenType.Bot, config.GetByKey("Discord:Token").GetValue(readRestricted: true)); await client.StartAsync(); // we get the CommandHandler class here and call the InitializeAsync method to start things up for the CommandHandler service diff --git a/ChaosBot/Discord/Modules/Admin/Config.cs b/ChaosBot/Discord/Modules/Admin/Config.cs index e5d2089..29935f6 100644 --- a/ChaosBot/Discord/Modules/Admin/Config.cs +++ b/ChaosBot/Discord/Modules/Admin/Config.cs @@ -82,11 +82,8 @@ namespace ChaosBot.Discord.Modules.Admin { if ((key != null) && (value != null) ) { - if(RestrictedConfig.IsAllowed(key)) - { - new Configuration().GetByKey(key).SetValueFromString(value, Context.Guild.Id); - await ConfigGet(key, true); - } + new Configuration().GetByKey(key).SetValueFromString(value, Context.Guild.Id); + await ConfigGet(key, true); } else { @@ -104,7 +101,7 @@ namespace ChaosBot.Discord.Modules.Admin { try { - if ((key != null) && (RestrictedConfig.IsAllowed(key))) + if ((key != null)) { StringBuilder sb = new StringBuilder(); EmbedBuilder embed = new EmbedBuilder(); @@ -159,7 +156,7 @@ namespace ChaosBot.Discord.Modules.Admin { try { - if ((key != null) && (RestrictedConfig.IsAllowed(key))) + if ((key != null)) { StringBuilder sb = new StringBuilder(); EmbedBuilder embed = new EmbedBuilder(); diff --git a/ChaosBot/Discord/Modules/Admin/RankCheck.cs b/ChaosBot/Discord/Modules/Admin/RankCheck.cs index 8c9a402..ca4a380 100644 --- a/ChaosBot/Discord/Modules/Admin/RankCheck.cs +++ b/ChaosBot/Discord/Modules/Admin/RankCheck.cs @@ -110,8 +110,8 @@ namespace ChaosBot.Discord.Modules.Admin using HttpClient client = new HttpClient(); Configuration config = new Configuration(); - string endpoint =config.GetByKey("Lodestone:ChaosBotApi:Url").GetValue(); - string apiToken = config.GetByKey("Lodestone:ChaosBotApi:ApiToken").GetValue(); + string endpoint =config.GetByKey("Lodestone:ChaosBotApi:Url").GetValue(readRestricted: true); + string apiToken = config.GetByKey("Lodestone:ChaosBotApi:ApiToken").GetValue(readRestricted: true); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiToken); HttpResponseMessage result = diff --git a/ChaosBot/Models/ChaosbotContext.cs b/ChaosBot/Models/ChaosbotContext.cs index 64ee36b..4eb3543 100644 --- a/ChaosBot/Models/ChaosbotContext.cs +++ b/ChaosBot/Models/ChaosbotContext.cs @@ -38,11 +38,11 @@ namespace ChaosBot.Models { ConfigHelpers.Configuration config = new ConfigHelpers.Configuration(); - server = config.GetByKey("Database:Host").GetValue(); - port = config.GetByKey("Database:Port").GetValue(); - user = config.GetByKey("Database:User").GetValue(); - pass = config.GetByKey("Database:Pass").GetValue(); - name = config.GetByKey("Database:Name").GetValue(); + server = config.GetByKey("Database:Host").GetValue(readRestricted: true); + port = config.GetByKey("Database:Port").GetValue(readRestricted: true); + user = config.GetByKey("Database:User").GetValue(readRestricted: true); + pass = config.GetByKey("Database:Pass").GetValue(readRestricted: true); + name = config.GetByKey("Database:Name").GetValue(readRestricted: true); } optionsBuilder.UseMySql( diff --git a/ChaosBot/Services/RestrictedConfig.cs b/ChaosBot/Services/RestrictedConfig.cs deleted file mode 100644 index e21e90f..0000000 --- a/ChaosBot/Services/RestrictedConfig.cs +++ /dev/null @@ -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 restrictedCfg = new List - { - "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); - } - } -} diff --git a/ChaosBot/WebServer/App/DiscordController.cs b/ChaosBot/WebServer/App/DiscordController.cs index 97fa6cf..7991957 100644 --- a/ChaosBot/WebServer/App/DiscordController.cs +++ b/ChaosBot/WebServer/App/DiscordController.cs @@ -40,7 +40,7 @@ namespace ChaosBot.WebServer.App string redirectUri = $"{config.GetByKey("Discord:BaseUri").GetValue()}/api/discord"; string clientId = config.GetByKey("Discord:ClientId").GetValue(); - string clientSecret = config.GetByKey("Discord:ClientSecret").GetValue(); + string clientSecret = config.GetByKey("Discord:ClientSecret").GetValue(readRestricted: true); if (code == null) return Redirect($"https://discord.com/api/oauth2/authorize?client_id={clientId}&redirect_uri={redirectUri}&response_type=code&scope=identify%20guilds"); diff --git a/ChaosBot/WebServer/WebServer.cs b/ChaosBot/WebServer/WebServer.cs index 503e97c..21c6c1c 100644 --- a/ChaosBot/WebServer/WebServer.cs +++ b/ChaosBot/WebServer/WebServer.cs @@ -29,7 +29,7 @@ namespace ChaosBot.WebServer webBuilder.UseWebRoot(webRoot); webBuilder.ConfigureKestrel(serverOptions => { - serverOptions.Listen(IPAddress.Any, config.GetByKey("WebServer:Port").GetValue(), + serverOptions.Listen(IPAddress.Any, config.GetByKey("WebServer:Port").GetValue(readRestricted: true), listenOptions => { listenOptions.UseConnectionLogging();