group S3 related functions into S3 namespace

This commit is contained in:
ig 2023-03-19 16:09:37 +01:00
parent 96bdc7a494
commit ed7c65be2c
6 changed files with 129 additions and 92 deletions

View File

@ -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<Boolean> 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<Boolean> RenewS3BucketUrl(this Installation installation, TimeSpan validity)
{
installation.S3Url = await S3Access.ReadOnly.SignUrl(installation.BucketName(), validity);
return Db.Update(installation);
}
public static async Task<Boolean> CreateBucket(this Installation installation)
public static Task<Boolean> 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<Boolean> DeleteBucket(this Installation installation)
public static Task<Boolean> 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<User> UsersWithAccess(this Installation installation)
{
return UsersWithDirectAccess(installation).Concat(UsersWithInheritedAccess(installation));
return installation.UsersWithDirectAccess()
.Concat(installation.UsersWithInheritedAccess());
}
public static IEnumerable<User> 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<User> UsersWithInheritedAccess(this Installation installation)
{
return installation.Ancestors().SelectMany(f => f.UsersWithDirectAccess()).NotNull();
return installation
.Ancestors()
.SelectMany(FolderMethods.UsersWithDirectAccess)
.NotNull();
}
public static IEnumerable<Folder> Ancestors(this Installation installation)

View File

@ -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;
}
}

View File

@ -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<Session>();
});
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();
}
}

View File

@ -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"
);
}

View File

@ -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<String> SignUrl(String bucketName, TimeSpan validity)
{
var result = await Run(bucketName, "signurl", $"+{validity.TotalSeconds}");
return result
.StandardOutput
.Replace("\n", "")
.Replace(" ", "");
}
public async Task<Boolean> CreateBucket(String bucketName)
{
var result = await Run(bucketName, "mb");
return result.ExitCode == 0;
}
public async Task<Boolean> DeleteBucket(String bucketName)
{
var result = await Run(bucketName, "rb");
return result.ExitCode == 0;
}
private Task<BufferedCommandResult> Run(String bucketName, String operation, params String[] optionalArgs)
{
var args = DefaultArgs
.Append(operation)
.Append(bucketName.EnsureStartsWith(BucketPrefix))
.Concat(optionalArgs);
return Python
.WithArguments(args)
.ExecuteBufferedAsync();
}
}

View File

@ -33,6 +33,7 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=proxyport/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=resultset/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Salimax/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=signurl/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Trumpf/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=ttyusb/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=tupled/@EntryIndexedValue">True</s:Boolean>