diff --git a/csharp/App/Backend/DataTypes/Installation.cs b/csharp/App/Backend/DataTypes/Installation.cs index c3f60e2e2..ebc9244c7 100644 --- a/csharp/App/Backend/DataTypes/Installation.cs +++ b/csharp/App/Backend/DataTypes/Installation.cs @@ -14,6 +14,6 @@ public class Installation : TreeNode public Double Long { get; set; } public String S3Bucket { get; set; } = ""; - public String S3Key { get; set; } = ""; + public String S3Url { get; set; } = ""; } \ No newline at end of file diff --git a/csharp/App/Backend/DataTypes/Methods/Installation.cs b/csharp/App/Backend/DataTypes/Methods/Installation.cs index 795e8a1dc..15fda5ff1 100644 --- a/csharp/App/Backend/DataTypes/Methods/Installation.cs +++ b/csharp/App/Backend/DataTypes/Methods/Installation.cs @@ -1,3 +1,5 @@ +using CliWrap; +using CliWrap.Buffered; using InnovEnergy.App.Backend.Database; namespace InnovEnergy.App.Backend.DataTypes.Methods; @@ -5,6 +7,28 @@ namespace InnovEnergy.App.Backend.DataTypes.Methods; public static class InstallationMethods { + public static async Task RenewS3BucketUrl(this Installation installation) + { + await RenewS3BucketUrl(installation, TimeSpan.FromDays(1)); + } + + public static async Task RenewS3BucketUrl(this Installation installation, TimeSpan validity) + { + //secret 55MAqyO_FqUmh7O64VIO0egq50ERn_WIAWuc2QC44QU + const String apiKey = "EXO44d2979c8e570eae81ead564"; + + var cmd = Cli + .Wrap("s3cmd") + .WithArguments(new[] { "signurl",$"s3://{installation.Name.Replace(" ", "-")}", validity.TotalSeconds.ToString(), "--access_key", apiKey}); + + var x = await cmd.ExecuteBufferedAsync(); + installation.S3Url = x.StandardOutput.Replace("\n", "").Replace(" ", ""); + + Console.WriteLine(installation.S3Url); + + Db.Update(installation); + } + public static IEnumerable Ancestors(this Installation installation) { var parentFolder = Parent(installation); diff --git a/csharp/App/Backend/DataTypes/Methods/User.cs b/csharp/App/Backend/DataTypes/Methods/User.cs index 16de6065e..b517bbf8c 100644 --- a/csharp/App/Backend/DataTypes/Methods/User.cs +++ b/csharp/App/Backend/DataTypes/Methods/User.cs @@ -1,9 +1,11 @@ using System.Net.Http.Headers; using System.Net.Mail; using System.Security.Cryptography; +using System.Text; using System.Text.Json.Nodes; using System.Text.RegularExpressions; using CliWrap; +using CliWrap.Buffered; using InnovEnergy.App.Backend.Database; using InnovEnergy.Lib.Utils; using Convert = System.Convert; @@ -175,159 +177,9 @@ public static class UserMethods return $"{user.Id}InnovEnergy"; } - - // private static Byte[] HmacSha256Digest(String message, String secret) - // { - // // var encoding = new UTF8Encoding(); - // // var keyBytes = encoding.GetBytes(secret); - // // var messageBytes = encoding.GetBytes(message); - // // var cryptographer = new HMACSHA256(keyBytes); - // // return cryptographer.ComputeHash(messageBytes); - // - // var keyBytes = UTF8.GetBytes(secret); - // var messageBytes = UTF8.GetBytes(message); - // - // return HMACSHA256.HashData(keyBytes, messageBytes); - // } - // - // private static String BuildSignature(String method, String path, String data, Int64 time, String secret) - // { - // var messageToSign = ""; - // messageToSign += method + " /v2/" + path + "\n"; - // messageToSign += data + "\n"; - // - // // query strings - // messageToSign += "\n"; - // // headers - // messageToSign += "\n"; - // - // messageToSign += time; - // - // Console.WriteLine("Message to sign:\n" + messageToSign); - // - // var hmac = HmacSha256Digest(messageToSign, secret); - // return Convert.ToBase64String(hmac); - // } + - // public Object CreateAndSaveUserS3ApiKey(User user) - // { - // //EXOSCALE API URL - // const String url = "https://api-ch-dk-2.exoscale.com/v2/"; - // const String path = "access-key"; - // - // //TODO HIDE ME - // const String secret = "S2K1okphiCSNK4mzqr4swguFzngWAMb1OoSlZsJa9F0"; - // const String apiKey = "EXOb98ec9008e3ec16e19d7b593"; - // - // var installationList = User2Installation - // .Where(i => i.UserId == user.Id) - // .SelectMany(i => Installations.Where(f => i.InstallationId == f.Id)) - // .ToList(); - // - // - // var instList = new JsonArray(); - // - // foreach (var installation in installationList) - // { - // instList.Add(new JsonObject {["domain"] = "sos",["resource-name"] = installation.Name,["resource-type"] = "bucket"}); - // } - // - // var jsonPayload = new JsonObject { ["name"] = user.Email, ["operations"] = new JsonArray{ "list-sos-bucket", "get-sos-object" }, ["content"] = instList}; - // var stringPayload = jsonPayload.ToJsonString(); - // - // var unixExpiration = DateTimeOffset.UtcNow.ToUnixTimeSeconds()+60; - // var signature = BuildSignature("POST", path, stringPayload, unixExpiration , secret); - // - // var authHeader = "credential="+apiKey+",expires="+unixExpiration+",signature="+signature; - // - // var client = new HttpClient(); - // client.DefaultRequestHeaders.Authorization = - // new AuthenticationHeaderValue("EXO2-HMAC-SHA256", authHeader); - // - // var content = new StringContent(stringPayload, Encoding.UTF8, "application/json"); - // - // - // var response = client.PostAsync(url+path, content).Result; - // - // if (response.StatusCode.ToString() != "OK") - // { - // return response; - // } - // - // var responseString = response.Content.ReadAsStringAsync().Result; - // return Enumerable.Last(Regex.Match(responseString, "key\\\":\\\"([A-Z])\\w+").ToString().Split('"')); - // // return SetUserS3ApiKey(user, newKey); - // - // } - - public static Object CreateAndSaveInstallationS3BuckitUrl(Installation installation) - { - // //EXOSCALE API URL - // const String url = "https://api-ch-dk-2.exoscale.com/v2/"; - // const String path = "access-key"; - // - // //TODO HIDE ME - // const String secret = "S2K1okphiCSNK4mzqr4swguFzngWAMb1OoSlZsJa9F0"; - // const String apiKey = "EXOb98ec9008e3ec16e19d7b593"; - // - // - // var jsonPayload = new JsonObject - // { - // ["name"] = installation.Id, - // ["operations"] = new JsonArray - // { - // "list-sos-bucket", - // "get-sos-object" - // }, - // ["content"] = new JsonArray - // { - // new JsonObject - // { - // ["domain"] = "sos", - // ["resource-name"] = installation.Name, - // ["resource-type"] = "bucket" - // } - // } - // }; - // - // var stringPayload = jsonPayload.ToJsonString(); - // - // var unixExpiration = DateTimeOffset.UtcNow.ToUnixTimeSeconds() + 60; - // - // var signature = BuildSignature("POST", path, stringPayload, unixExpiration, secret); - // - // var authHeader = "credential=" + apiKey + ",expires=" + unixExpiration + ",signature=" + signature; - // - // var client = new HttpClient(); - // - // client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("EXO2-HMAC-SHA256", authHeader); - // - // var content = new StringContent(stringPayload, UTF8, "application/json"); - // - // var response = client.PostAsync(url + path, content).Result; - // - // if (response.StatusCode.ToString() != "OK") - // { - // return response; - // } - // - // var responseString = response.Content.ReadAsStringAsync().Result; - // var newKey = Regex - // .Match(responseString, "key\\\":\\\"([A-Z])\\w+") - // .ToString() - // .Split('"') - // .Last(); - // Cli.Wrap(); - // - // - // installation.S3Key = newKey; - // Db.Update(installation); - // return newKey; - return 0; - } - - // TODO private static Boolean IsValidEmail(String email) diff --git a/csharp/App/Backend/Database/Db.cs b/csharp/App/Backend/Database/Db.cs index 2fc111844..05b3c6a7c 100644 --- a/csharp/App/Backend/Database/Db.cs +++ b/csharp/App/Backend/Database/Db.cs @@ -39,13 +39,12 @@ public static partial class Db }); - var installation = Installations.First(); - UserMethods.CreateAndSaveInstallationS3BuckitUrl(installation); + - - Observable.Interval(TimeSpan.FromDays(1)) - .StartWith(0) // Do it right away (on startup) - .Subscribe(Cleanup); // and then daily + Observable.Interval(TimeSpan.FromDays(0.5)) + .StartWith(0) // Do it right away (on startup) + .SelectMany(Cleanup) + .Subscribe(); // and then daily } @@ -111,10 +110,11 @@ public static partial class Db } - private static void Cleanup(Int64 _) + private static async Task Cleanup(Int64 _) { - DeleteS3Keys(); + await UpdateS3Urls(); DeleteStaleSessions(); + return true; } private static void DeleteStaleSessions() @@ -123,10 +123,9 @@ public static partial class Db Sessions.Delete(s => s.LastSeen < deadline); } - private static void DeleteS3Keys() + private static Task UpdateS3Urls() { - void DeleteKeys() => Installations.Do(i => i.S3Key = "").ForEach(Update); // TODO - - Connection.RunInTransaction(DeleteKeys); + var renewTasks = Installations.Select(i => i.RenewS3BucketUrl()).ToArray(); + return Task.WhenAll(renewTasks); } } \ No newline at end of file diff --git a/csharp/App/Backend/db.sqlite b/csharp/App/Backend/db.sqlite index c4bf0e94d..f0db31089 100644 Binary files a/csharp/App/Backend/db.sqlite and b/csharp/App/Backend/db.sqlite differ