From 9a2990f750dbc7ff974147f7c74678f3209cf6b2 Mon Sep 17 00:00:00 2001 From: Kim Date: Mon, 13 Nov 2023 09:40:34 +0100 Subject: [PATCH] New Buckets now have cors --- csharp/App/Backend/DataTypes/Methods/ExoCmd.cs | 12 ++++++++++-- csharp/App/Backend/DataTypes/Methods/Session.cs | 7 ++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/csharp/App/Backend/DataTypes/Methods/ExoCmd.cs b/csharp/App/Backend/DataTypes/Methods/ExoCmd.cs index 8f2836cd5..d1e8f3c57 100644 --- a/csharp/App/Backend/DataTypes/Methods/ExoCmd.cs +++ b/csharp/App/Backend/DataTypes/Methods/ExoCmd.cs @@ -6,7 +6,9 @@ using System.Net; using System.Net.Http.Headers; using System.Text; using System.Text.Json.Nodes; +using Amazon.S3.Model; using InnovEnergy.App.Backend.Database; +using InnovEnergy.Lib.Utils; namespace InnovEnergy.App.Backend.DataTypes.Methods; @@ -242,11 +244,17 @@ public static class ExoCmd return id; } - + public static async Task CreateBucket(this Installation installation) { + var cors = new CORSConfiguration(); + cors.Rules.Add(new CORSRule()); + cors.Rules[0].AllowedHeaders = new List { "*" }; + cors.Rules[0].AllowedOrigins = new List { "*" }; + cors.Rules[0].AllowedMethods = new List { "Get", "Head" }; var s3Region = new S3Region($"https://{installation.S3Region}.{installation.S3Provider}", S3Creds!); - return await s3Region.PutBucket(installation.BucketName()) != null; + var a = await s3Region.PutBucket(installation.BucketName()); + return a != null && await a.PutCors(cors); } public static async Task SendConfig(this Installation installation, String config) diff --git a/csharp/App/Backend/DataTypes/Methods/Session.cs b/csharp/App/Backend/DataTypes/Methods/Session.cs index 2db8e0ff4..9e162fd6f 100644 --- a/csharp/App/Backend/DataTypes/Methods/Session.cs +++ b/csharp/App/Backend/DataTypes/Methods/Session.cs @@ -99,10 +99,11 @@ public static class SessionMethods && user.HasWriteAccess && user.HasAccessToParentOf(installation) && Db.Create(installation) // TODO: these two in a transaction - && installation.SetOrderNumbers() + && installation.SetOrderNumbers() && Db.Create(new InstallationAccess { UserId = user.Id, InstallationId = installation.Id }) - && await installation.CreateBucket() - && await installation.RenewS3Credentials(); // generation of access _after_ generation of + && await installation.CreateBucket() + && await installation.RenewS3Credentials(); + // generation of access _after_ generation of // bucket to prevent "zombie" access-rights. // This might ** us over if the creation of access rights fails, // as bucket-names are unique and bound to the installation id... -K