From 584db6a607362d2103fa696edb9b3852a037f59f Mon Sep 17 00:00:00 2001 From: Mikayla Dobson Date: Fri, 1 Dec 2023 13:09:55 -0600 Subject: [PATCH] updated docker config; corrected env variable workflow --- .gitignore | 4 + Unbinder/Docker/DOTNET.Dockerfile | 16 ++-- ...31201185549_update-table-names.Designer.cs | 79 +++++++++++++++++++ .../20231201185549_update-table-names.cs | 22 ++++++ Unbinder/Models/Ingredient.cs | 5 +- Unbinder/Models/Recipe.cs | 5 +- Unbinder/Program.cs | 24 +++--- Unbinder/Services/EnvironmentLoader.cs | 13 +++ Unbinder/Services/S3Service.cs | 4 +- Unbinder/Unbinder.csproj | 1 + Unbinder/appsettings.Development.json | 3 + Unbinder/appsettings.json | 2 +- Unbinder/docker-compose.yaml | 43 +++++++--- 13 files changed, 183 insertions(+), 38 deletions(-) create mode 100644 Unbinder/Migrations/20231201185549_update-table-names.Designer.cs create mode 100644 Unbinder/Migrations/20231201185549_update-table-names.cs create mode 100644 Unbinder/Services/EnvironmentLoader.cs diff --git a/.gitignore b/.gitignore index 9df718e..72610cf 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,10 @@ __notes__ *.env +# Files for/generated by Docker Compose +secrets +db_data + # User-specific files *.rsuser *.suo diff --git a/Unbinder/Docker/DOTNET.Dockerfile b/Unbinder/Docker/DOTNET.Dockerfile index 6818813..23705df 100644 --- a/Unbinder/Docker/DOTNET.Dockerfile +++ b/Unbinder/Docker/DOTNET.Dockerfile @@ -1,18 +1,18 @@ # get .net 8 FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build - -COPY . ./src -WORKDIR /src - -RUN dotnet build -o /app -RUN dotnet publish -o /publish +WORKDIR /app ENV AWS_S3_URL=$AWS_S3_URL ENV AWS_ACCESS_KEY=$AWS_ACCESS_KEY ENV AWS_SECRET_KEY=$AWS_SECRET_KEY ENV AWS_BUCKET_NAME=$AWS_BUCKET_NAME -WORKDIR /publish +COPY . . +RUN dotnet restore +RUN dotnet publish -o out -EXPOSE 80 +# build runtime image +FROM mcr.microsoft.com/dotnet/aspnet:8.0 +WORKDIR /app +COPY --from=build /app/out . ENTRYPOINT ["./Unbinder"] \ No newline at end of file diff --git a/Unbinder/Migrations/20231201185549_update-table-names.Designer.cs b/Unbinder/Migrations/20231201185549_update-table-names.Designer.cs new file mode 100644 index 0000000..63f7b3d --- /dev/null +++ b/Unbinder/Migrations/20231201185549_update-table-names.Designer.cs @@ -0,0 +1,79 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Unbinder.DB; + +#nullable disable + +namespace Unbinder.Migrations +{ + [DbContext(typeof(UnbinderDbContext))] + [Migration("20231201185549_update-table-names")] + partial class updatetablenames + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.0") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("Unbinder.Models.Ingredient", b => + { + b.Property("IngredientId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("IngredientId")); + + b.Property("Description") + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("IngredientId"); + + b.ToTable("Ingredients"); + }); + + modelBuilder.Entity("Unbinder.Models.Recipe", b => + { + b.Property("RecipeId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("RecipeId")); + + b.Property("Author") + .HasColumnType("nvarchar(max)"); + + b.Property("ImageUrl") + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("RecipeText") + .HasColumnType("nvarchar(max)"); + + b.Property("ShortDescription") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("RecipeId"); + + b.ToTable("Recipes"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Unbinder/Migrations/20231201185549_update-table-names.cs b/Unbinder/Migrations/20231201185549_update-table-names.cs new file mode 100644 index 0000000..94fc211 --- /dev/null +++ b/Unbinder/Migrations/20231201185549_update-table-names.cs @@ -0,0 +1,22 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Unbinder.Migrations +{ + /// + public partial class updatetablenames : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + + } + } +} diff --git a/Unbinder/Models/Ingredient.cs b/Unbinder/Models/Ingredient.cs index f9dbc68..6a6f78b 100644 --- a/Unbinder/Models/Ingredient.cs +++ b/Unbinder/Models/Ingredient.cs @@ -1,5 +1,8 @@ -namespace Unbinder.Models +using System.ComponentModel.DataAnnotations.Schema; + +namespace Unbinder.Models { + [Table("Ingredients")] public record Ingredient { public int IngredientId { get; init; } diff --git a/Unbinder/Models/Recipe.cs b/Unbinder/Models/Recipe.cs index 81503a8..42a5226 100644 --- a/Unbinder/Models/Recipe.cs +++ b/Unbinder/Models/Recipe.cs @@ -1,5 +1,8 @@ -namespace Unbinder.Models +using System.ComponentModel.DataAnnotations.Schema; + +namespace Unbinder.Models { + [Table("Recipes")] public record Recipe { public int RecipeId { get; init; } diff --git a/Unbinder/Program.cs b/Unbinder/Program.cs index 36816d5..568a34f 100644 --- a/Unbinder/Program.cs +++ b/Unbinder/Program.cs @@ -1,19 +1,13 @@ -using Microsoft.AspNetCore.Authentication; -using Microsoft.AspNetCore.Authentication.OpenIdConnect; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Mvc.Authorization; -using Microsoft.Identity.Web; +using Microsoft.Data.SqlClient; using Microsoft.EntityFrameworkCore; -using Microsoft.Identity.Web.UI; using Unbinder.DB; -using Unbinder.Models; using Unbinder.Repositories; using Unbinder.Services; -using Amazon.S3; -using Microsoft.Extensions.Azure; var builder = WebApplication.CreateBuilder(args); -var connectionString = builder.Configuration.GetConnectionString("DefaultConnection"); + +// pull in environment variables from secrets +EnvironmentLoader.Load(builder); // Add services to the container. //builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme) @@ -23,8 +17,16 @@ var connectionString = builder.Configuration.GetConnectionString("DefaultConnect // // By default, all incoming requests will be authorized according to the default policy. // options.FallbackPolicy = options.DefaultPolicy); +SqlConnectionStringBuilder connBuilder = new() +{ + ConnectionString = builder.Configuration.GetConnectionString("DefaultConnection"), + Password = builder.Configuration["SA_PASSWORD"], +}; + builder.Services.AddDbContext(options => - options.UseSqlServer(connectionString)); +{ + options.UseSqlServer(connBuilder.ConnectionString); +}); // configure MVC builder.Services.AddControllersWithViews(); diff --git a/Unbinder/Services/EnvironmentLoader.cs b/Unbinder/Services/EnvironmentLoader.cs new file mode 100644 index 0000000..f7dc346 --- /dev/null +++ b/Unbinder/Services/EnvironmentLoader.cs @@ -0,0 +1,13 @@ +namespace Unbinder.Services +{ + public static class EnvironmentLoader + { + public static void Load(WebApplicationBuilder builder) + { + Environment.SetEnvironmentVariable("AWS_S3_URL", builder.Configuration["AWS_S3_URL"]); + Environment.SetEnvironmentVariable("AWS_ACCESS_KEY", builder.Configuration["AWS_ACCESS_KEY"]); + Environment.SetEnvironmentVariable("AWS_SECRET_KEY", builder.Configuration["AWS_SECRET_KEY"]); + Environment.SetEnvironmentVariable("AWS_BUCKET_NAME", builder.Configuration["AWS_BUCKET_NAME"]); + } + } +} diff --git a/Unbinder/Services/S3Service.cs b/Unbinder/Services/S3Service.cs index b7666a3..f03072e 100644 --- a/Unbinder/Services/S3Service.cs +++ b/Unbinder/Services/S3Service.cs @@ -16,8 +16,6 @@ namespace Unbinder.Services string? accessKey = Environment.GetEnvironmentVariable("AWS_ACCESS_KEY"); string? secret = Environment.GetEnvironmentVariable("AWS_SECRET_KEY"); - Console.WriteLine(accessKey ?? "(none)", secret ?? "(none)"); - if (accessKey == null || secret == null) { throw new Exception("AWS credentials not found"); @@ -32,7 +30,7 @@ namespace Unbinder.Services RegionEndpoint = RegionEndpoint.USEast2 }; - private static AmazonS3Client Client => new(Credentials); + private static AmazonS3Client Client => new(Credentials, Config); private static readonly AmazonS3Client client = Client; private static readonly string? BucketName = Environment.GetEnvironmentVariable("AWS_BUCKET_NAME"); diff --git a/Unbinder/Unbinder.csproj b/Unbinder/Unbinder.csproj index 876938d..ea4b29c 100644 --- a/Unbinder/Unbinder.csproj +++ b/Unbinder/Unbinder.csproj @@ -25,6 +25,7 @@ + diff --git a/Unbinder/appsettings.Development.json b/Unbinder/appsettings.Development.json index 770d3e9..9712dc5 100644 --- a/Unbinder/appsettings.Development.json +++ b/Unbinder/appsettings.Development.json @@ -5,5 +5,8 @@ "Default": "Information", "Microsoft.AspNetCore": "Warning" } + }, + "ConnectionStrings": { + "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=Unbinder111623" } } diff --git a/Unbinder/appsettings.json b/Unbinder/appsettings.json index bbd6504..40beec2 100644 --- a/Unbinder/appsettings.json +++ b/Unbinder/appsettings.json @@ -7,7 +7,7 @@ "CallbackPath": "/signin-oidc" }, "ConnectionStrings": { - "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=Unbinder11623" + "DefaultConnection": "Server=db;Database=master;User=sa;Encrypt=False;TrustServerCertificate=true" }, "Logging": { "LogLevel": { diff --git a/Unbinder/docker-compose.yaml b/Unbinder/docker-compose.yaml index 1045d4d..ceec6fc 100644 --- a/Unbinder/docker-compose.yaml +++ b/Unbinder/docker-compose.yaml @@ -2,23 +2,40 @@ version: '3.8' services: client: + depends_on: + - db build: context: . dockerfile: ./Docker/DOTNET.Dockerfile - args: - AWS_S3_URL: ${AWS_S3_URL} - AWS_ACCESS_KEY: ${AWS_ACCESS_KEY} - AWS_SECRET_KEY: ${AWS_SECRET_KEY} - AWS_BUCKET_NAME: ${AWS_BUCKET_NAME} ports: - 80:80 - depends_on: - - db + environment: + SA_PASSWORD: /run/secrets/SA_PASSWORD + AWS_S3_URL: /run/secrets/AWS_S3_URL + AWS_ACCESS_KEY: /run/secrets/AWS_ACCESS_KEY + AWS_SECRET_KEY: /run/secrets/AWS_SECRET_KEY + AWS_BUCKET_NAME: /run/secrets/AWS_BUCKET_NAME db: - build: - context: . - dockerfile: ./Docker/MSSQL.Dockerfile - args: - DB_PASSWORD: ${DB_PASSWORD} + image: mcr.microsoft.com/mssql/server:2022-latest + volumes: + - ./db_data:/var/opt/mssql/data ports: - - 1433:1433 \ No newline at end of file + - 1433:1433 + environment: + SA_PASSWORD: /run/secrets/SA_PASSWORD + ACCEPT_EULA: "Y" + +secrets: + AWS_S3_URL: + file: ./secrets/AWS_S3_URL.txt + AWS_ACCESS_KEY: + file: ./secrets/AWS_ACCESS_KEY.txt + AWS_SECRET_KEY: + file: ./secrets/AWS_SECRET_KEY.txt + AWS_BUCKET_NAME: + file: ./secrets/AWS_BUCKET_NAME.txt + SA_PASSWORD: + file: ./secrets/SA_PASSWORD.txt + +volumes: + db_data: \ No newline at end of file