From ed7c65be2cd9a0bad95eeaabd2d53b5ec716aa35 Mon Sep 17 00:00:00 2001 From: ig Date: Sun, 19 Mar 2023 16:09:37 +0100 Subject: [PATCH] group S3 related functions into S3 namespace --- .../Backend/DataTypes/Methods/Installation.cs | 100 ++++++------------ .../App/Backend/DataTypes/Methods/Session.cs | 33 +++--- csharp/App/Backend/Database/Db.cs | 9 +- csharp/App/Backend/S3/S3Access.cs | 16 +++ csharp/App/Backend/S3/S3Cmd.cs | 62 +++++++++++ csharp/InnovEnergy.sln.DotSettings | 1 + 6 files changed, 129 insertions(+), 92 deletions(-) create mode 100644 csharp/App/Backend/S3/S3Access.cs create mode 100644 csharp/App/Backend/S3/S3Cmd.cs diff --git a/csharp/App/Backend/DataTypes/Methods/Installation.cs b/csharp/App/Backend/DataTypes/Methods/Installation.cs index 608827041..8409371a0 100644 --- a/csharp/App/Backend/DataTypes/Methods/Installation.cs +++ b/csharp/App/Backend/DataTypes/Methods/Installation.cs @@ -1,6 +1,5 @@ -using CliWrap; -using CliWrap.Buffered; using InnovEnergy.App.Backend.Database; +using InnovEnergy.App.Backend.S3; using InnovEnergy.Lib.Utils; namespace InnovEnergy.App.Backend.DataTypes.Methods; @@ -8,96 +7,59 @@ namespace InnovEnergy.App.Backend.DataTypes.Methods; public static class InstallationMethods { - public static async Task RenewS3BucketUrl(this Installation installation) + private const String BucketNameSalt = "3e5b3069-214a-43ee-8d85-57d72000c19d"; + + public static String BucketName(this Installation installation) { - await RenewS3BucketUrl(installation, TimeSpan.FromDays(1)); + return $"s3://{installation.Id}-{BucketNameSalt}"; } - public static async Task RenewS3BucketUrl(this Installation installation, TimeSpan validity) + public static async Task RenewS3BucketUrl(this Installation installation) { - const String secret = "55MAqyO_FqUmh7O64VIO0egq50ERn_WIAWuc2QC44QU"; - const String apiKey = "EXO44d2979c8e570eae81ead564"; - const String salt = "3e5b3069-214a-43ee-8d85-57d72000c19d"; - var cmd = Cli - .Wrap("python3") - .WithArguments(new[] - { - "Resources/s3cmd.py", "signurl", $"s3://{installation.Id}-{salt}", validity.TotalSeconds.ToString(), "--access_key", - apiKey, "--secret_key", secret - }); - var x = await cmd.ExecuteBufferedAsync(); - installation.S3Url = x.StandardOutput.Replace("\n", "").Replace(" ", ""); + return await RenewS3BucketUrl(installation, TimeSpan.FromDays(1)); + } - Db.Update(installation); + public static async Task RenewS3BucketUrl(this Installation installation, TimeSpan validity) + { + installation.S3Url = await S3Access.ReadOnly.SignUrl(installation.BucketName(), validity); + return Db.Update(installation); } - - public static async Task CreateBucket(this Installation installation) + public static Task CreateBucket(this Installation installation) { - const String secret = "-T9TAqy9a3-0-xj7HKsFFJOCcxfRpcnL6OW5oOrOcWU"; - - const String apiKey = "EXO87ca85e29dd412f1238f1cf0"; - const String salt = "3e5b3069-214a-43ee-8d85-57d72000c19d"; - - var cmd = Cli - .Wrap("python3") - .WithArguments(new[] - { - "Resources/s3cmd.py", "mb", $"s3://{installation.Id}-{salt}", "--access_key", - apiKey, "--secret_key", secret - }); - var x = await cmd.ExecuteBufferedAsync(); - - //Updating the url in the db as not wait until the next bi-daily update - var cmd2 = Cli - .Wrap("python3") - .WithArguments(new[] - { - "Resources/s3cmd.py", "signurl", $"s3://{installation.Id}-{salt}", - TimeSpan.FromDays(1).TotalSeconds.ToString(), "--access_key", - apiKey, "--secret_key", secret - }); - - var y = await cmd2.ExecuteBufferedAsync(); - installation.S3Url = y.StandardOutput.Replace("\n", "").Replace(" ", ""); - - Db.Update(installation); - - return x.ExitCode == 0; + return S3Access + .ReadWrite + .CreateBucket(installation.BucketName()); } - public static async Task DeleteBucket(this Installation installation) + public static Task DeleteBucket(this Installation installation) { - const String secret = "-T9TAqy9a3-0-xj7HKsFFJOCcxfRpcnL6OW5oOrOcWU"; - const String apiKey = "EXO87ca85e29dd412f1238f1cf0"; - const String salt = "3e5b3069-214a-43ee-8d85-57d72000c19d"; - var cmd = Cli - .Wrap("python3") - .WithArguments(new[] - { - "Resources/s3cmd.py", "rb", $"s3://{installation.Id}-{salt}", "--access_key", - apiKey, "--secret_key", secret - }); - var x = await cmd.ExecuteBufferedAsync(); - return x.ExitCode == 0; + return S3Access + .ReadWrite + .DeleteBucket(installation.BucketName()); } public static IEnumerable UsersWithAccess(this Installation installation) { - return UsersWithDirectAccess(installation).Concat(UsersWithInheritedAccess(installation)); + return installation.UsersWithDirectAccess() + .Concat(installation.UsersWithInheritedAccess()); } public static IEnumerable UsersWithDirectAccess(this Installation installation) { - return Db.InstallationAccess - .Where(access => access.InstallationId == installation.Id) - .Select(access => Db.GetUserById(access.UserId)) - .NotNull(); + return Db + .InstallationAccess + .Where(a => a.InstallationId == installation.Id) + .Select(a => Db.GetUserById(a.UserId)) + .NotNull(); } public static IEnumerable UsersWithInheritedAccess(this Installation installation) { - return installation.Ancestors().SelectMany(f => f.UsersWithDirectAccess()).NotNull(); + return installation + .Ancestors() + .SelectMany(FolderMethods.UsersWithDirectAccess) + .NotNull(); } public static IEnumerable Ancestors(this Installation installation) diff --git a/csharp/App/Backend/DataTypes/Methods/Session.cs b/csharp/App/Backend/DataTypes/Methods/Session.cs index 5b8dfae0a..7d76b1f0b 100644 --- a/csharp/App/Backend/DataTypes/Methods/Session.cs +++ b/csharp/App/Backend/DataTypes/Methods/Session.cs @@ -14,7 +14,7 @@ public static class SessionMethods && user.HasWriteAccess && user.HasAccessTo(folder.Parent()) && Db.Create(folder) - && Db.Create(new FolderAccess() { UserId = user.Id, FolderId = folder.Id }); + && Db.Create(new FolderAccess { UserId = user.Id, FolderId = folder.Id }); } public static Boolean Update(this Session? session, Folder? folder) @@ -52,7 +52,7 @@ public static class SessionMethods && user.HasWriteAccess && user.HasAccessTo(installation.Parent()) && Db.Create(installation) - && installation.CreateBucket().Result + && installation.CreateBucket().Result // TODO: await? && Db.Create(new InstallationAccess { UserId = user.Id, InstallationId = installation.Id }); } @@ -77,6 +77,7 @@ public static class SessionMethods && installation is not null && user.HasWriteAccess && user.HasAccessTo(installation) +// && installation.DeleteBucket().Result // TODO: await? && Db.Delete(installation); } @@ -154,13 +155,12 @@ public static class SessionMethods var sessionUser = session?.User; return sessionUser is not null - && user is not null - && installation is not null - && user.IsDescendantOf(sessionUser) - && sessionUser.HasAccessTo(installation) - && user.HasAccessTo(installation) - && Db.InstallationAccess.Delete(access => - access.UserId == user.Id && access.InstallationId == installation.Id) > 0; + && user is not null + && installation is not null + && user.IsDescendantOf(sessionUser) + && sessionUser.HasAccessTo(installation) + && user.HasAccessTo(installation) + && Db.InstallationAccess.Delete(a => a.UserId == user.Id && a.InstallationId == installation.Id) > 0; } public static Boolean RevokeAccessTo(this Session? session, User? user, Folder? folder) @@ -168,19 +168,18 @@ public static class SessionMethods var sessionUser = session?.User; return sessionUser is not null - && user is not null - && folder is not null - && user.IsDescendantOf(sessionUser) - && sessionUser.HasAccessTo(folder) - && user.HasAccessTo(folder) - && Db.FolderAccess.Delete(access => - access.UserId == user.Id && access.FolderId == folder.Id) > 0; + && user is not null + && folder is not null + && user.IsDescendantOf(sessionUser) + && sessionUser.HasAccessTo(folder) + && user.HasAccessTo(folder) + && Db.FolderAccess.Delete(a => a.UserId == user.Id && a.FolderId == folder.Id) > 0; } public static Boolean Logout(this Session? session) { return session is not null - && Db.Sessions.Delete(s => s.Token == session.Token) > 0; + && Db.Sessions.Delete(s => s.Token == session.Token) > 0; } } \ No newline at end of file diff --git a/csharp/App/Backend/Database/Db.cs b/csharp/App/Backend/Database/Db.cs index f7eaafd7f..80cd15454 100644 --- a/csharp/App/Backend/Database/Db.cs +++ b/csharp/App/Backend/Database/Db.cs @@ -6,7 +6,6 @@ using InnovEnergy.Lib.Utils; using SQLite; - namespace InnovEnergy.App.Backend.Database; @@ -38,9 +37,6 @@ public static partial class Db Connection.CreateTable(); }); - - - Observable.Interval(TimeSpan.FromDays(0.5)) .StartWith(0) // Do it right away (on startup) .SelectMany(Cleanup) @@ -85,7 +81,8 @@ public static partial class Db private static Task UpdateS3Urls() { - var renewTasks = Installations.Select(i => i.RenewS3BucketUrl()).ToArray(); - return Task.WhenAll(renewTasks); + return Installations + .Select(i => i.RenewS3BucketUrl()) + .WhenAll(); } } \ No newline at end of file diff --git a/csharp/App/Backend/S3/S3Access.cs b/csharp/App/Backend/S3/S3Access.cs new file mode 100644 index 000000000..7ef89ca8f --- /dev/null +++ b/csharp/App/Backend/S3/S3Access.cs @@ -0,0 +1,16 @@ +namespace InnovEnergy.App.Backend.S3; + +public static class S3Access +{ + public static S3Cmd ReadOnly { get; } = new S3Cmd + ( + key : "EXO44d2979c8e570eae81ead564", + secret: "55MAqyO_FqUmh7O64VIO0egq50ERn_WIAWuc2QC44QU" + ); + + public static S3Cmd ReadWrite { get; } = new S3Cmd + ( + key : "EXO87ca85e29dd412f1238f1cf0", + secret: "-T9TAqy9a3-0-xj7HKsFFJOCcxfRpcnL6OW5oOrOcWU" + ); +} \ No newline at end of file diff --git a/csharp/App/Backend/S3/S3Cmd.cs b/csharp/App/Backend/S3/S3Cmd.cs new file mode 100644 index 000000000..fd1442f74 --- /dev/null +++ b/csharp/App/Backend/S3/S3Cmd.cs @@ -0,0 +1,62 @@ +using CliWrap; +using CliWrap.Buffered; +using InnovEnergy.Lib.Utils; + +namespace InnovEnergy.App.Backend.S3; + +public class S3Cmd +{ + private static readonly Command Python = Cli.Wrap("python3"); + + private const String S3CmdPath = "Resources/s3cmd.py"; + private const String BucketPrefix = "s3://"; + + private String[] DefaultArgs { get; } + + // ReSharper disable StringLiteralTypo + // ReSharper enable StringLiteralTypo + + public S3Cmd(String key, String secret) + { + DefaultArgs = new[] + { + S3CmdPath, + "--access_key", key, + "--secret_key", secret, + }; + } + + public async Task SignUrl(String bucketName, TimeSpan validity) + { + var result = await Run(bucketName, "signurl", $"+{validity.TotalSeconds}"); + + return result + .StandardOutput + .Replace("\n", "") + .Replace(" ", ""); + } + + public async Task CreateBucket(String bucketName) + { + var result = await Run(bucketName, "mb"); + return result.ExitCode == 0; + } + + public async Task DeleteBucket(String bucketName) + { + var result = await Run(bucketName, "rb"); + return result.ExitCode == 0; + } + + private Task Run(String bucketName, String operation, params String[] optionalArgs) + { + var args = DefaultArgs + .Append(operation) + .Append(bucketName.EnsureStartsWith(BucketPrefix)) + .Concat(optionalArgs); + + return Python + .WithArguments(args) + .ExecuteBufferedAsync(); + } +} \ No newline at end of file diff --git a/csharp/InnovEnergy.sln.DotSettings b/csharp/InnovEnergy.sln.DotSettings index 0584ce254..116325457 100644 --- a/csharp/InnovEnergy.sln.DotSettings +++ b/csharp/InnovEnergy.sln.DotSettings @@ -33,6 +33,7 @@ True True True + True True True True