update google drive source handler

move google api credentials from file to bot.db
This commit is contained in:
13xforever 2022-11-09 11:14:35 +05:00
parent c89f93f561
commit 2a89d75cd5
No known key found for this signature in database
GPG Key ID: 2B2A36B482FE70C5
15 changed files with 90 additions and 16 deletions

View File

@ -10,6 +10,7 @@ using CompatApiClient.Utils;
using CompatBot.Database;
using CompatBot.Database.Providers;
using CompatBot.EventHandlers;
using CompatBot.EventHandlers.LogParsing.SourceHandlers;
using CompatBot.Utils;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
@ -71,14 +72,13 @@ internal sealed class BotStats: BaseCommandModuleCustom
private static string GetConfiguredApiStats()
{
return new StringBuilder()
.Append(File.Exists(Config.GoogleApiConfigPath) ? "✅" : "❌").AppendLine(" Google Drive")
.Append(GoogleDriveHandler.ValidateCredentials() ? "✅" : "❌").AppendLine(" Google Drive")
.Append(string.IsNullOrEmpty(Config.AzureDevOpsToken) ? "❌" : "✅").AppendLine(" Azure DevOps")
.Append(string.IsNullOrEmpty(Config.AzureComputerVisionKey) ? "❌" : "✅").AppendLine(" Computer Vision")
.Append(string.IsNullOrEmpty(Config.AzureAppInsightsConnectionString) ? "❌" : "✅").AppendLine(" AppInsights")
.Append(string.IsNullOrEmpty(Config.GithubToken) ? "❌" : "✅").AppendLine(" Github")
.ToString()
.Trim();
}
private static void AppendPiracyStats(DiscordEmbedBuilder embed)

View File

@ -84,12 +84,13 @@ internal static class Config
public static Guid AzureDevOpsProjectId => config.GetValue(nameof(AzureDevOpsProjectId), new Guid("3598951b-4d39-4fad-ad3b-ff2386a649de"));
public static string AzureAppInsightsConnectionString => config.GetValue(nameof(AzureAppInsightsConnectionString), "")!;
public static string GithubToken => config.GetValue(nameof(GithubToken), "")!;
public static string GoogleApiCredentials => config.GetValue(nameof(GoogleApiCredentials), "")!;
public static string PreferredFontFamily => config.GetValue(nameof(PreferredFontFamily), "")!;
public static string LogPath => config.GetValue(nameof(LogPath), "./logs/")!; // paths are relative to the working directory
public static string IrdCachePath => config.GetValue(nameof(IrdCachePath), "./ird/")!;
public static double GameTitleMatchThreshold => config.GetValue(nameof(GameTitleMatchThreshold), 0.57);
public static byte[] CryptoSalt => Convert.FromBase64String(config.GetValue(nameof(CryptoSalt), "")!);
public static string RenameNameSuffix => config.GetValue(nameof(RenameNameSuffix), " (Rule 7)")!;
public static string RenameNameSuffix => config.GetValue(nameof(RenameNameSuffix), " (Rule 7)")!;
internal static class AllowedMentions
{
@ -180,9 +181,12 @@ internal static class Config
public static readonly IReadOnlyCollection<ulong> LogParsingChannels = new HashSet<ulong>
{
277227681836302338, // #help
272081036316377088, // donors
272081036316377088, // #donors
319224795785068545, // #bot-spam
442667232489897997, // testers
442667232489897997, // #testers
// test server
988392381118308394,
};
public static readonly IReadOnlyCollection<string> RoleWhiteList = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase)

View File

@ -1,5 +1,8 @@
using System.Linq;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using CompatBot.EventHandlers.LogParsing.SourceHandlers;
using Microsoft.EntityFrameworkCore;
namespace CompatBot.Database.Providers;
@ -17,6 +20,30 @@ internal static class SqlConfiguration
foreach (var stateVar in setVars)
if (stateVar.Value is string value)
Config.InMemorySettings[stateVar.Key[ConfigVarPrefix.Length ..]] = value;
if (!Config.InMemorySettings.TryGetValue(nameof(Config.GoogleApiCredentials), out var googleCreds) || string.IsNullOrEmpty(googleCreds))
{
if (Path.Exists(Config.GoogleApiConfigPath))
{
Config.Log.Info("Migrating Google API credentials storage from file to db...");
try
{
googleCreds = await File.ReadAllTextAsync(Config.GoogleApiConfigPath).ConfigureAwait(false);
if (GoogleDriveHandler.ValidateCredentials(googleCreds))
{
Config.InMemorySettings[nameof(Config.GoogleApiCredentials)] = googleCreds;
Config.Log.Info("Successfully migrated Google API credentials");
}
else
{
Config.Log.Error("Failed to migrate Google API credentials");
}
}
catch (Exception e)
{
Config.Log.Error(e, "Failed to migrate Google API credentials");
}
}
}
Config.RebuildConfiguration();
}
}

View File

@ -71,5 +71,7 @@ internal sealed class DiscordAttachmentHandler : BaseSourceHandler
await using var stream = await client.GetStreamAsync(attachment.Url, cancellationToken).ConfigureAwait(false);
await handler.FillPipeAsync(stream, writer, cancellationToken).ConfigureAwait(false);
}
public void Dispose() { }
}
}

View File

@ -104,5 +104,7 @@ internal sealed class DropboxHandler : BaseSourceHandler
await using var stream = await client.GetStreamAsync(uri, cancellationToken).ConfigureAwait(false);
await handler.FillPipeAsync(stream, writer, cancellationToken).ConfigureAwait(false);
}
public void Dispose() { }
}
}

View File

@ -51,4 +51,6 @@ internal class FileSource : ISource
}
throw new InvalidOperationException("Unknown source type");
}
public void Dispose() { }
}

View File

@ -104,5 +104,7 @@ internal sealed class GenericLinkHandler : BaseSourceHandler
await using var stream = await client.GetStreamAsync(uri, cancellationToken).ConfigureAwait(false);
await handler.FillPipeAsync(stream, writer, cancellationToken).ConfigureAwait(false);
}
public void Dispose() { }
}
}

View File

@ -27,7 +27,7 @@ internal sealed class GoogleDriveHandler: BaseSourceHandler
if (string.IsNullOrEmpty(message.Content))
return (null, null);
if (!File.Exists(Config.GoogleApiConfigPath))
if (string.IsNullOrEmpty(Config.GoogleApiCredentials))
return (null, null);
var matches = ExternalLink.Matches(message.Content);
@ -64,7 +64,7 @@ internal sealed class GoogleDriveHandler: BaseSourceHandler
{
var (canHandle, reason) = handler.CanHandle(fileMeta.Name, (int)fileMeta.Size, buf.AsSpan(0, read));
if (canHandle)
return (new GoogleDriveSource(fileInfoRequest, fileMeta, handler), null);
return (new GoogleDriveSource(client, fileInfoRequest, fileMeta, handler), null);
else if (!string.IsNullOrEmpty(reason))
return(null, reason);
}
@ -84,10 +84,10 @@ internal sealed class GoogleDriveHandler: BaseSourceHandler
return (null, null);
}
private static DriveService GetClient()
private static DriveService GetClient(string? json = null)
{
var credential = GoogleCredential.FromFile(Config.GoogleApiConfigPath).CreateScoped(Scopes);
var service = new DriveService(new BaseClientService.Initializer()
var credential = GoogleCredential.FromJson(json ?? Config.GoogleApiCredentials).CreateScoped(Scopes);
var service = new DriveService(new()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
@ -95,6 +95,20 @@ internal sealed class GoogleDriveHandler: BaseSourceHandler
return service;
}
internal static bool ValidateCredentials(string? json = null)
{
try
{
using var _ = GetClient(json);
return true;
}
catch (Exception e)
{
Config.Log.Error(e);
return false;
}
}
private sealed class GoogleDriveSource : ISource
{
public string SourceType => "Google Drive";
@ -103,12 +117,14 @@ internal sealed class GoogleDriveHandler: BaseSourceHandler
public long SourceFilePosition => handler.SourcePosition;
public long LogFileSize => handler.LogSize;
private readonly DriveService driveService;
private readonly FilesResource.GetRequest fileInfoRequest;
private readonly FileMeta fileMeta;
private readonly IArchiveHandler handler;
public GoogleDriveSource(FilesResource.GetRequest fileInfoRequest, FileMeta fileMeta, IArchiveHandler handler)
public GoogleDriveSource(DriveService driveService, FilesResource.GetRequest fileInfoRequest, FileMeta fileMeta, IArchiveHandler handler)
{
this.driveService = driveService;
this.fileInfoRequest = fileInfoRequest;
this.fileMeta = fileMeta;
this.handler = handler;
@ -135,5 +151,7 @@ internal sealed class GoogleDriveHandler: BaseSourceHandler
Config.Log.Error(e, "Failed to download file from Google Drive");
}
}
public void Dispose() => driveService.Dispose();
}
}

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.IO.Pipelines;
using System.Threading;
using System.Threading.Tasks;
@ -12,7 +13,7 @@ public interface ISourceHandler
Task<(ISource? source, string? failReason)> FindHandlerAsync(DiscordMessage message, ICollection<IArchiveHandler> handlers);
}
public interface ISource
public interface ISource: IDisposable
{
string SourceType { get; }
string FileName { get; }

View File

@ -110,5 +110,7 @@ internal sealed class MediafireHandler : BaseSourceHandler
await using var stream = await client.GetStreamAsync(uri, cancellationToken).ConfigureAwait(false);
await handler.FillPipeAsync(stream, writer, cancellationToken).ConfigureAwait(false);
}
public void Dispose() { }
}
}

View File

@ -96,5 +96,7 @@ internal sealed class MegaHandler : BaseSourceHandler
await using var stream = await client.DownloadAsync(uri, Doodad, cancellationToken).ConfigureAwait(false);
await handler.FillPipeAsync(stream, writer, cancellationToken).ConfigureAwait(false);
}
public void Dispose() { }
}
}

View File

@ -103,5 +103,7 @@ internal sealed class OneDriveSourceHandler : BaseSourceHandler
await using var stream = await client.GetStreamAsync(uri, cancellationToken).ConfigureAwait(false);
await handler.FillPipeAsync(stream, writer, cancellationToken).ConfigureAwait(false);
}
public void Dispose() { }
}
}

View File

@ -88,5 +88,7 @@ internal sealed class PastebinHandler : BaseSourceHandler
await using var stream = await client.GetStreamAsync(uri, cancellationToken).ConfigureAwait(false);
await handler.FillPipeAsync(stream, writer, cancellationToken).ConfigureAwait(false);
}
public void Dispose() { }
}
}

View File

@ -101,6 +101,7 @@ internal sealed class YandexDiskHandler: BaseSourceHandler
await using var stream = await client.GetStreamAsync(uri, cancellationToken).ConfigureAwait(false);
await handler.FillPipeAsync(stream, writer, cancellationToken).ConfigureAwait(false);
}
}
public void Dispose() { }
}
}

View File

@ -93,8 +93,15 @@ public static class LogParsingHandler
try
{
var possibleHandlers = SourceHandlers.Select(h => h.FindHandlerAsync(message, ArchiveHandlers).ConfigureAwait(false).GetAwaiter().GetResult()).ToList();
var source = possibleHandlers.FirstOrDefault(h => h.source != null).source;
using var source = possibleHandlers.FirstOrDefault(h => h.source != null).source;
var fail = possibleHandlers.FirstOrDefault(h => !string.IsNullOrEmpty(h.failReason)).failReason;
foreach (var (s, _) in possibleHandlers)
{
if (ReferenceEquals(s, source))
continue;
s?.Dispose();
}
var isSpamChannel = LimitedToSpamChannel.IsSpamChannel(channel);
var isHelpChannel = LimitedToHelpChannel.IsHelpChannel(channel);