Skip to content

Commit

Permalink
Changes required for LocalTest to talk to app over http (instead of a…
Browse files Browse the repository at this point in the history
…ccessing files locally) (#7657)

* Changes required to run LocalTest in http mode

Originaly proposed as part of #7525

* Fixes on error page when Localtest LocalPlatformSettings.LocalAppMode = "http"

* Add notes about how to configure `LocalAppMode` == "http"

* fix typo

* Update src/development/LocalTest/Startup.cs

Co-authored-by: Ivar Nesje <[email protected]>

* Add default config and order settings in LOCALAPP.md as in settings

Co-authored-by: Stephanie Buadu <[email protected]>
  • Loading branch information
ivarne and acn-sbuad authored Dec 16, 2021
1 parent 180745d commit 22b7c96
Show file tree
Hide file tree
Showing 15 changed files with 336 additions and 125 deletions.
3 changes: 3 additions & 0 deletions loadbalancer/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ http {
location /localtestresources/ {
proxy_pass http://localtest/localtestresources/;
}
location /LocalPlatformStorage/ {
proxy_pass http://localtest/LocalPlatformStorage/;
}
location /502LocalTest.html {
root /www;
}
Expand Down
10 changes: 10 additions & 0 deletions src/Configuration/LocalPlatformSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ public class LocalPlatformSettings
/// </summary>
public string LocalTestingStaticTestDataPath { get; set; }

/// <summary>
/// Url for the local app when LocalAppMode == http
/// <summary>
public string LocalAppUrl { get; set; }

/// <summary>
/// which access mode to use ("file", "http")
/// <summary>
public string LocalAppMode { get; set; }

public string DocumentDbFolder { get; set; } = "documentdb/";

public string InstanceCollectionFolder { get; set; } = "instances/";
Expand Down
71 changes: 21 additions & 50 deletions src/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Security.Claims;

Expand All @@ -21,66 +22,66 @@
using LocalTest.Models;
using LocalTest.Services.Authentication.Interface;
using LocalTest.Services.Profile.Interface;
using LocalTest.Services.Localtest.Interface;
using LocalTest.Services.LocalApp.Interface;

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Authentication.Cookies;
using System.Text;
using Newtonsoft.Json;
using LocalTest.Services.Localtest.Interface;

namespace LocalTest.Controllers
{
public class HomeController : Controller
{
private readonly GeneralSettings _generalSettings;
private readonly LocalPlatformSettings _localPlatformSettings;
private readonly IApplicationRepository _applicationRepository;
private readonly IUserProfiles _userProfileService;
private readonly IAuthentication _authenticationService;
private readonly ILocalTestAppSelection _appSelectionService;
private readonly ILocalApp _localApp;

public HomeController(
IOptions<GeneralSettings> generalSettings,
IOptions<LocalPlatformSettings> localPlatformSettings,
IApplicationRepository applicationRepository,
IUserProfiles userProfileService,
IAuthentication authenticationService,
ILocalTestAppSelection appSelectionService)
ILocalApp localApp)
{
_generalSettings = generalSettings.Value;
_localPlatformSettings = localPlatformSettings.Value;
_applicationRepository = applicationRepository;
_userProfileService = userProfileService;
_authenticationService = authenticationService;
_appSelectionService = appSelectionService;
_localApp = localApp;
}

[AllowAnonymous]
public async Task<IActionResult> Index()
{
StartAppModel model = new StartAppModel();
model.TestApps = await GetAppsList();
Application app = await _applicationRepository.FindOne("", "");
try
{
model.TestApps = await GetAppsList();
}
catch(HttpRequestException e)
{
model.HttpException = e;
}

model.TestUsers = await GetTestUsersForList();
model.AppPath = _localPlatformSettings.AppRepositoryBasePath;
model.StaticTestDataPath = _localPlatformSettings.LocalTestingStaticTestDataPath;
model.LocalAppUrl = _localPlatformSettings.LocalAppUrl;

if (!model.TestApps.Any())
if (!model.TestApps?.Any() ?? true)
{
model.InvalidAppPath = true;
}

if (!model.TestUsers.Any())
if (!model.TestUsers?.Any() ?? true)
{
model.InvalidTestDataPath = true;
}

if (app != null)
{
model.Org = app.Org;
model.App = app.Id.Split("/")[1];
}

return View(model);
}

Expand Down Expand Up @@ -120,7 +121,7 @@ public async Task<ActionResult> LogInTestUser(StartAppModel startAppModel)
string token = _authenticationService.GenerateToken(principal, int.Parse(_generalSettings.GetJwtCookieValidityTime));
CreateJwtCookieAndAppendToResponse(token);

Application app = GetAppItem(startAppModel.AppPathSelection + "/config");
Application app = await _localApp.GetApplicationMetadata(startAppModel.AppPathSelection);

return Redirect($"{_generalSettings.GetBaseUrl}/{app.Id}/");
}
Expand Down Expand Up @@ -229,38 +230,8 @@ private async Task<IEnumerable<SelectListItem>> GetTestUsersForList()

private async Task<IEnumerable<SelectListItem>> GetAppsList()
{
List<SelectListItem> apps = new List<SelectListItem>();

string path = this._localPlatformSettings.AppRepositoryBasePath;

if (!Directory.Exists(path))
{
return apps;
}

string configPath = path + "config";
if (Directory.Exists(configPath))
{
Application app = GetAppItem(configPath);
if (app != null)
{
apps.Add(GetSelectItem(app, path));
}
}

string[] directories = Directory.GetDirectories(path);

foreach(string directory in directories)
{

Application app = GetAppItem(directory + "/App/config");
if (app != null)
{
apps.Add(GetSelectItem(app, directory + "/App/"));
}
}

return apps;
var applications = await _localApp.GetApplications();
return applications.Select((kv)=> GetSelectItem(kv.Value, kv.Key)).ToList();
}

private SelectListItem GetSelectItem(Application app, string path)
Expand Down
14 changes: 13 additions & 1 deletion src/LocalTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,19 @@
<PackageReference Include="Altinn.Platform.Storage.Interface" Version="3.6.2" />
<PackageReference Include="JWTCookieAuthentication" Version="2.4.2" />
<PackageReference Include="Microsoft.Azure.DocumentDB.Core" Version="2.16.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<None Remove="jwtselfsignedcert.pfx" />
<None Remove="JWTValidationCert.cer" />
<None Remove="MaskinportenJwtValidationCert.cer" />
<Content Include="jwtselfsignedcert.pfx">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="JWTValidationCert.cer">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
28 changes: 17 additions & 11 deletions src/Models/StartAppModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;

namespace LocalTest.Models
Expand All @@ -24,47 +25,52 @@ public class StartAppModel
public bool InvalidAppPath { get; set; }

/// <summary>
///
/// Title of the app
/// </summary>
public string AppTitle { get; set; }

/// <summary>
///
/// _localPlatformSettings.AppRepositoryBasePath
/// </summary>
public string AppPath { get; set; }

/// <summary>
///
/// Path to TestData form localPlatformSettings
/// </summary>
public string StaticTestDataPath { get; set; }

/// <summary>
///
/// Signals that no TestUsers could be found in TestData
/// </summary>
public bool InvalidTestDataPath { get; set; }

/// <summary>
///
/// LocalAppUrl from localPlatformSettings
/// </summary>
public int UserId { get; set; }
public string LocalAppUrl { get; set; }

/// <summary>
///
/// HttpRequestException that might have resultet from _localApp.GetApplications()
/// </summary>
public string SelectedApp { get; set; }
public HttpRequestException HttpException { get; set; }

/// <summary>
/// Selected userId
/// </summary>
public int UserId { get; set; }

/// <summary>
///
/// Path for the selected app
/// </summary>
public string AppPathSelection { get; set; }

/// <summary>
///
/// List of TestUsers for dropdown
/// </summary>
public IEnumerable<SelectListItem> TestUsers { get; set; }

/// <summary>
///
/// List of selectable Apps for dropdown
/// </summary>
public IEnumerable<SelectListItem> TestApps { get; set; }
}
Expand Down
31 changes: 15 additions & 16 deletions src/Services/Authorization/Implementation/PolicyRetrievalPoint.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Xml;
using Altinn.Authorization.ABAC.Utils;
using Altinn.Authorization.ABAC.Xacml;
using Altinn.Platform.Authorization.Constants;
using LocalTest.Configuration;
using LocalTest.Services.Authorization.Interface;
using LocalTest.Services.LocalApp.Interface;
using LocalTest.Services.Localtest.Interface;
using Microsoft.Extensions.Options;

Expand All @@ -18,43 +21,39 @@ namespace Altinn.Platform.Authorization.Services.Implementation
public class PolicyRetrievalPoint : IPolicyRetrievalPoint
{
private readonly LocalPlatformSettings _localPlatformSettings;
private readonly ILocalTestAppSelection _localTestAppSelectionService;
private readonly ILocalApp _localApp;

/// <summary>
/// Initializes a new instance of the <see cref="PolicyRetrievalPoint"/> class.
/// </summary>
/// <param name="policyRepository">The policy Repository..</param>
public PolicyRetrievalPoint(IOptions<LocalPlatformSettings> localPlatformSettings, ILocalTestAppSelection localTestAppSelectionService)
public PolicyRetrievalPoint(
IOptions<LocalPlatformSettings> localPlatformSettings,
ILocalApp localApp)
{
_localPlatformSettings = localPlatformSettings.Value;
_localTestAppSelectionService = localTestAppSelectionService;

_localApp = localApp;
}

/// <inheritdoc/>
public Task<XacmlPolicy> GetPolicyAsync(XacmlContextRequest request)
public async Task<XacmlPolicy> GetPolicyAsync(XacmlContextRequest request)
{
string policyPath = GetPolicyPath(request);
return Task.FromResult(ParsePolicy(policyPath));
var app = request.GetResourceAttributes().Attributes.Where(a => a.AttributeId.ToString() == XacmlRequestAttribute.AppAttribute).Select(a => a.AttributeValues.FirstOrDefault()).FirstOrDefault().Value;
var org = request.GetResourceAttributes().Attributes.Where(a => a.AttributeId.ToString() == XacmlRequestAttribute.OrgAttribute).Select(a => a.AttributeValues.FirstOrDefault()).FirstOrDefault().Value;
string policyString = await _localApp.GetXACMLPolicy($"{org}/{app}");
return ParsePolicyContent(policyString);
}

/// <inheritdoc/>
public Task<XacmlPolicy> GetPolicyAsync(string org, string app)
{
throw new NotImplementedException();
}

private string GetPolicyPath(XacmlContextRequest request)
{
return _localTestAppSelectionService.GetAppPath(request) + $"config/authorization/policy.xml";
}

public static XacmlPolicy ParsePolicy(string policyPath)
public static XacmlPolicy ParsePolicyContent(string policyContent)
{
XmlDocument policyDocument = new XmlDocument();
policyDocument.Load(policyPath);
XacmlPolicy policy;
using (XmlReader reader = XmlReader.Create(new StringReader(policyDocument.OuterXml)))
using (XmlReader reader = XmlReader.Create(new StringReader(policyContent)))
{
policy = XacmlParser.ParseXacmlPolicy(reader);
}
Expand Down
Loading

0 comments on commit 22b7c96

Please sign in to comment.