From 21d9c17533551bd9dd073d8a9f436be21127af4d Mon Sep 17 00:00:00 2001 From: Ivar Nesje Date: Wed, 9 Feb 2022 10:01:04 +0100 Subject: [PATCH] Add possibility to run localtest in docker with --profile (#7898) * Add possibility to run localtest in docker with --profile ``` cd src/development docker compose --profile localtest up --build ``` Also add .env.template file in `src/development` to make it easy to set environment variables to configure docker. Just rename to the gitignored .env file and make your changes. * Update src/development/LocalTest/appsettings.Docker.json * Use resourceService to get local text resources instead of fetching from storage This means IText is not used anywhere, but because every app inherits from AppBase, it can't be removed. * Make it really easy to change domain to use local.altinn.cloud altinn3local.no is used in lots of tests, and I can't really fix it everywhere, but I think that this change should be non-breaking for everyone without a TEST_DOMAIN environment variable that does not resolve to localhost --- .env.template | 16 ++++++++++++++++ .gitignore | 1 + docker-compose.yml | 28 ++++++++++++++++++++++++++-- loadbalancer/502LocalTest.html | 16 ++++++++++++---- loadbalancer/nginx.conf | 11 +++++++++-- src/Controllers/HomeController.cs | 8 ++++---- src/Dockerfile | 26 ++++++++++++++++++++++++++ src/Startup.cs | 6 +++--- src/appsettings.Docker.json | 8 ++++++++ 9 files changed, 105 insertions(+), 15 deletions(-) create mode 100644 .env.template create mode 100644 .gitignore create mode 100644 src/Dockerfile create mode 100644 src/appsettings.Docker.json diff --git a/.env.template b/.env.template new file mode 100644 index 00000000..478025dc --- /dev/null +++ b/.env.template @@ -0,0 +1,16 @@ +## This is a template for the .env file +## To use this, rename the file from .env.template to .env +## and uncomment some variables that you want different values for + +## What port should the locadbalancer use +## sometimes PCs have another service running on port 80, so you might need to +## change this. +#ALTINN3LOCAL_PORT=80 + +## If you want to see the storage files on disk (instead of readig them trough +## the browser), change this to a local path on your computer (ensure that it exists) +#ALTINN3LOCALSTORAGE_PATH:C:/AltinnPlatformLocal/ + +## If you want to use another domain than altinn3local.no for local testing +## add it here +#TEST_DOMAIN:local.altinn.cloud diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..4c49bd78 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env diff --git a/docker-compose.yml b/docker-compose.yml index 5dc5191e..7c26a46d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,11 +12,12 @@ services: networks: - altinntestlocal_network ports: - - "80:80" + - "${ALTINN3LOCAL_PORT:-80}:80" environment: - NGINX_HOST=localhost - NGINX_PORT=80 - SUB_FILTER=${SUB_FILTER:-} + - TEST_DOMAIN=${TEST_DOMAIN:-altinn3local.no} - NGINX_ENVSUBST_OUTPUT_DIR=/etc/nginx build: context: ./loadbalancer @@ -29,4 +30,27 @@ services: ports: - "5070:5070" build: - context: ./../Altinn.Platform/Altinn.Platform.PDF/ \ No newline at end of file + context: ./../Altinn.Platform/Altinn.Platform.PDF/ + altinn_localtest: + container_name: localtest + image: localtest:latest + restart: always + profiles: + - localtest + networks: + - altinntestlocal_network + ports: + - "5101:5101" + build: + dockerfile: ./LocalTest/Dockerfile + context: ./ + environment: + - DOTNET_ENVIRONMENT=Docker + - ASPNETCORE_URLS=http://*:5101/ + - GeneralSettings__BaseUrl=http://${TEST_DOMAIN:-altinn3local.no}:${ALTINN3LOCAL_PORT:-80} + - GeneralSettings__HostName=${TEST_DOMAIN:-altinn3local.no} + volumes: + - ${ALTINN3LOCALSTORAGE_PATH:-AltinnPlatformLocal}:/AltinnPlatformLocal + +volumes: + AltinnPlatformLocal: diff --git a/loadbalancer/502LocalTest.html b/loadbalancer/502LocalTest.html index b005f341..a1716a5c 100644 --- a/loadbalancer/502LocalTest.html +++ b/loadbalancer/502LocalTest.html @@ -1,11 +1,13 @@ + - - - + + + Localtest is not running +

502 Bad Gateway

Localtest is not running

@@ -14,5 +16,11 @@

Localtest is not running

> cd src/development/LocalTest > dotnet run +

Alternaltivly you can run localtest in docker with the localtest profile

+
+    > cd src/development
+    > docker compose --profile localtest up --build
+  
- \ No newline at end of file + + diff --git a/loadbalancer/nginx.conf b/loadbalancer/nginx.conf index 4c9e7c87..5b4a0a00 100644 --- a/loadbalancer/nginx.conf +++ b/loadbalancer/nginx.conf @@ -15,14 +15,21 @@ http { upstream app { server host.docker.internal:5005; } + # Redirect localhost (future altinn3local.no) to the configured test domain + server { + listen 80; + server_name localhost; + # server_name localhost altinn3local.no; + return 307 $scheme://${TEST_DOMAIN}$request_uri; + } upstream receiptcomp { server host.docker.internal:5060; } server { - listen 80; - server_name altinn3local.no localhost; + listen 80 default_server; + server_name ${TEST_DOMAIN}; proxy_redirect off; proxy_set_header Host $host; diff --git a/src/Controllers/HomeController.cs b/src/Controllers/HomeController.cs index ea0051b5..557ef2c3 100644 --- a/src/Controllers/HomeController.cs +++ b/src/Controllers/HomeController.cs @@ -107,7 +107,7 @@ public async Task LogInTestUser(StartAppModel startAppModel) UserProfile profile = await _userProfileService.GetUser(startAppModel.UserId); List claims = new List(); - string issuer = "altinn3local.no"; + string issuer = _generalSettings.Hostname; claims.Add(new Claim(ClaimTypes.NameIdentifier, profile.UserId.ToString(), ClaimValueTypes.String, issuer)); claims.Add(new Claim(AltinnCoreClaimTypes.UserId, profile.UserId.ToString(), ClaimValueTypes.String, issuer)); claims.Add(new Claim(AltinnCoreClaimTypes.UserName, profile.UserName, ClaimValueTypes.String, issuer)); @@ -127,7 +127,7 @@ public async Task LogInTestUser(StartAppModel startAppModel) } /// - /// + /// /// /// /// @@ -141,7 +141,7 @@ public async Task GetTestUserToken(int userId) } List claims = new List(); - string issuer = "altinn3local.no"; + string issuer = _generalSettings.Hostname; claims.Add(new Claim(AltinnCoreClaimTypes.UserId, profile.UserId.ToString(), ClaimValueTypes.String, issuer)); claims.Add(new Claim(AltinnCoreClaimTypes.UserName, profile.UserName, ClaimValueTypes.String, issuer)); claims.Add(new Claim(AltinnCoreClaimTypes.PartyID, profile.PartyId.ToString(), ClaimValueTypes.Integer32, issuer)); @@ -164,7 +164,7 @@ public async Task GetTestUserToken(int userId) public async Task GetTestOrgToken(string id, [FromQuery] string orgNumber = "") { List claims = new List(); - string issuer = "altinn3local.no"; + string issuer = _generalSettings.Hostname; claims.Add(new Claim(AltinnCoreClaimTypes.Org, id.ToLower(), ClaimValueTypes.String, issuer)); claims.Add(new Claim(AltinnCoreClaimTypes.AuthenticationLevel, "2", ClaimValueTypes.Integer32, issuer)); claims.Add(new Claim("urn:altinn:scope", "altinn:serviceowner/instances.read", ClaimValueTypes.String, issuer)); diff --git a/src/Dockerfile b/src/Dockerfile new file mode 100644 index 00000000..285a6f3a --- /dev/null +++ b/src/Dockerfile @@ -0,0 +1,26 @@ +FROM mcr.microsoft.com/dotnet/sdk:5.0-alpine AS build +WORKDIR /LocalTest + +COPY ./LocalTest/LocalTest.csproj /LocalTest +RUN dotnet restore LocalTest.csproj + +COPY ./LocalTest /LocalTest + + +RUN ls / +RUN dotnet build LocalTest.csproj -c Release -o /app_output +RUN dotnet publish LocalTest.csproj -c Release -o /app_output + +FROM mcr.microsoft.com/dotnet/aspnet:5.0-alpine AS final +EXPOSE 5101 +WORKDIR /app +COPY --from=build /app_output . + +# Copy various data +COPY ./TestData /TestData + +# setup the user and group (not important for LocalTest and this removes write access to /AltinnPlatformLocal) +# RUN addgroup -g 3000 dotnet && adduser -u 1000 -G dotnet -D -s /bin/false dotnet +# USER dotnet + +ENTRYPOINT ["dotnet", "LocalTest.dll"] diff --git a/src/Startup.cs b/src/Startup.cs index 7eea8e53..3fa0f219 100644 --- a/src/Startup.cs +++ b/src/Startup.cs @@ -136,7 +136,7 @@ public void ConfigureServices(IServiceCollection services) // Adding custom model binders options.ModelBinderProviders.Insert(0, new XacmlRequestApiModelBinderProvider()); }); - + services.AddDirectoryBrowser(); // Access local app details depending on LocalAppMode ("file" or "http") @@ -156,7 +156,7 @@ public void Configure( IWebHostEnvironment env, IOptions localPlatformSettings) { - if (env.IsDevelopment()) + if (env.IsDevelopment() || env.IsEnvironment("docker")) { app.UseDeveloperExceptionPage(); @@ -169,7 +169,7 @@ public void Configure( // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } - + app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider(localPlatformSettings.Value.LocalTestingStorageBasePath), diff --git a/src/appsettings.Docker.json b/src/appsettings.Docker.json new file mode 100644 index 00000000..6208392c --- /dev/null +++ b/src/appsettings.Docker.json @@ -0,0 +1,8 @@ +{ + "LocalPlatformSettings": { + "LocalAppMode": "http", + "LocalAppUrl": "http://host.docker.internal:5005", + "LocalTestingStorageBasePath": "/AltinnPlatformLocal/", + "LocalTestingStaticTestDataPath": "/TestData/" + } +}