rewrote the bucket url generator, now creates new urls every 12 hours

This commit is contained in:
Kim 2023-03-16 10:31:24 +01:00
parent d6248ead09
commit a2836b1900
5 changed files with 39 additions and 164 deletions

View File

@ -14,6 +14,6 @@ public class Installation : TreeNode
public Double Long { get; set; } public Double Long { get; set; }
public String S3Bucket { get; set; } = ""; public String S3Bucket { get; set; } = "";
public String S3Key { get; set; } = ""; public String S3Url { get; set; } = "";
} }

View File

@ -1,3 +1,5 @@
using CliWrap;
using CliWrap.Buffered;
using InnovEnergy.App.Backend.Database; using InnovEnergy.App.Backend.Database;
namespace InnovEnergy.App.Backend.DataTypes.Methods; namespace InnovEnergy.App.Backend.DataTypes.Methods;
@ -5,6 +7,28 @@ namespace InnovEnergy.App.Backend.DataTypes.Methods;
public static class InstallationMethods 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<Folder> Ancestors(this Installation installation) public static IEnumerable<Folder> Ancestors(this Installation installation)
{ {
var parentFolder = Parent(installation); var parentFolder = Parent(installation);

View File

@ -1,9 +1,11 @@
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Net.Mail; using System.Net.Mail;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text;
using System.Text.Json.Nodes; using System.Text.Json.Nodes;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using CliWrap; using CliWrap;
using CliWrap.Buffered;
using InnovEnergy.App.Backend.Database; using InnovEnergy.App.Backend.Database;
using InnovEnergy.Lib.Utils; using InnovEnergy.Lib.Utils;
using Convert = System.Convert; using Convert = System.Convert;
@ -175,159 +177,9 @@ public static class UserMethods
return $"{user.Id}InnovEnergy"; 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 // TODO
private static Boolean IsValidEmail(String email) private static Boolean IsValidEmail(String email)

View File

@ -39,13 +39,12 @@ public static partial class Db
}); });
var installation = Installations.First();
UserMethods.CreateAndSaveInstallationS3BuckitUrl(installation);
Observable.Interval(TimeSpan.FromDays(0.5))
Observable.Interval(TimeSpan.FromDays(1)) .StartWith(0) // Do it right away (on startup)
.StartWith(0) // Do it right away (on startup) .SelectMany(Cleanup)
.Subscribe(Cleanup); // and then daily .Subscribe(); // and then daily
} }
@ -111,10 +110,11 @@ public static partial class Db
} }
private static void Cleanup(Int64 _) private static async Task<Boolean> Cleanup(Int64 _)
{ {
DeleteS3Keys(); await UpdateS3Urls();
DeleteStaleSessions(); DeleteStaleSessions();
return true;
} }
private static void DeleteStaleSessions() private static void DeleteStaleSessions()
@ -123,10 +123,9 @@ public static partial class Db
Sessions.Delete(s => s.LastSeen < deadline); Sessions.Delete(s => s.LastSeen < deadline);
} }
private static void DeleteS3Keys() private static Task UpdateS3Urls()
{ {
void DeleteKeys() => Installations.Do(i => i.S3Key = "").ForEach(Update); // TODO var renewTasks = Installations.Select(i => i.RenewS3BucketUrl()).ToArray();
return Task.WhenAll(renewTasks);
Connection.RunInTransaction(DeleteKeys);
} }
} }

Binary file not shown.