Skip to content

Commit

Permalink
Add possibility to run localtest in docker with --profile (#7898)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ivarne authored Feb 9, 2022
1 parent e593645 commit 21d9c17
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 15 deletions.
16 changes: 16 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
28 changes: 26 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,4 +30,27 @@ services:
ports:
- "5070:5070"
build:
context: ./../Altinn.Platform/Altinn.Platform.PDF/
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:
16 changes: 12 additions & 4 deletions loadbalancer/502LocalTest.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Localtest is not running</title>
</head>

<body>
<h1>502 Bad Gateway</h1>
<h2>Localtest is not running</h2>
Expand All @@ -14,5 +16,11 @@ <h2>Localtest is not running</h2>
> cd src/development/LocalTest
> dotnet run
</pre>
<p>Alternaltivly you can run localtest in docker with the localtest profile</p>
<pre>
> cd src/development
> docker compose --profile localtest up --build
</pre>
</body>
</html>

</html>
11 changes: 9 additions & 2 deletions loadbalancer/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions src/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public async Task<ActionResult> LogInTestUser(StartAppModel startAppModel)
UserProfile profile = await _userProfileService.GetUser(startAppModel.UserId);

List<Claim> claims = new List<Claim>();
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));
Expand All @@ -127,7 +127,7 @@ public async Task<ActionResult> LogInTestUser(StartAppModel startAppModel)
}

/// <summary>
///
///
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
Expand All @@ -141,7 +141,7 @@ public async Task<ActionResult> GetTestUserToken(int userId)
}

List<Claim> claims = new List<Claim>();
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));
Expand All @@ -164,7 +164,7 @@ public async Task<ActionResult> GetTestUserToken(int userId)
public async Task<ActionResult> GetTestOrgToken(string id, [FromQuery] string orgNumber = "")
{
List<Claim> claims = new List<Claim>();
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));
Expand Down
26 changes: 26 additions & 0 deletions src/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
6 changes: 3 additions & 3 deletions src/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -156,7 +156,7 @@ public void Configure(
IWebHostEnvironment env,
IOptions<LocalPlatformSettings> localPlatformSettings)
{
if (env.IsDevelopment())
if (env.IsDevelopment() || env.IsEnvironment("docker"))
{
app.UseDeveloperExceptionPage();

Expand All @@ -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),
Expand Down
8 changes: 8 additions & 0 deletions src/appsettings.Docker.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"LocalPlatformSettings": {
"LocalAppMode": "http",
"LocalAppUrl": "http://host.docker.internal:5005",
"LocalTestingStorageBasePath": "/AltinnPlatformLocal/",
"LocalTestingStaticTestDataPath": "/TestData/"
}
}

0 comments on commit 21d9c17

Please sign in to comment.