-
Notifications
You must be signed in to change notification settings - Fork 524
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add configurable cors support (#340)
- Loading branch information
1 parent
9bbd389
commit 9541e9e
Showing
13 changed files
with
309 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"FhirServer": { | ||
"Cors": { | ||
"Origins": ["https://localhost:6001"], | ||
"Methods": ["*"], | ||
"Headers": ["*"], | ||
"MaxAge": 1440 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
src/Microsoft.Health.Fhir.Api.UnitTests/Features/Cors/CorsModuleTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System; | ||
using Microsoft.AspNetCore.Cors.Infrastructure; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Health.Fhir.Api.Configs; | ||
using Microsoft.Health.Fhir.Api.Modules; | ||
using Microsoft.Health.Fhir.Core.Configs; | ||
using NSubstitute; | ||
using Xunit; | ||
|
||
namespace Microsoft.Health.Fhir.Api.UnitTests.Features.Cors | ||
{ | ||
public class CorsModuleTests | ||
{ | ||
private readonly CorsModule _corsModule; | ||
private readonly CorsConfiguration _corsConfiguration = Substitute.For<CorsConfiguration>(); | ||
private readonly IServiceCollection _servicesCollection = Substitute.For<IServiceCollection>(); | ||
|
||
public CorsModuleTests() | ||
{ | ||
var fhirServerConfiguration = Substitute.For<FhirServerConfiguration>(); | ||
|
||
fhirServerConfiguration.Cors.Returns(_corsConfiguration); | ||
|
||
_corsModule = new CorsModule(fhirServerConfiguration); | ||
} | ||
|
||
[Fact] | ||
public void GivenACorsConfiguration_WhenNoValuesSet_PolicyHasOnlyDefaults() | ||
{ | ||
_corsModule.Load(_servicesCollection); | ||
|
||
CorsPolicy corsPolicy = _corsModule.DefaultCorsPolicy; | ||
Assert.Empty(corsPolicy.Origins); | ||
Assert.Empty(corsPolicy.Headers); | ||
Assert.Empty(corsPolicy.Methods); | ||
Assert.False(corsPolicy.SupportsCredentials); | ||
Assert.Null(corsPolicy.PreflightMaxAge); | ||
} | ||
|
||
[Fact] | ||
public void GivenACorsConfiguration_WhenAllOriginsSet_PolicyHasAllowAnyOrigin() | ||
{ | ||
_corsConfiguration.Origins.Add("*"); | ||
_corsModule.Load(_servicesCollection); | ||
|
||
Assert.True(_corsModule.DefaultCorsPolicy.AllowAnyOrigin); | ||
} | ||
|
||
[Fact] | ||
public void GivenACorsConfiguration_WhenAllMethodsSet_PolicyHasAllowAnyMethod() | ||
{ | ||
_corsConfiguration.Methods.Add("*"); | ||
_corsModule.Load(_servicesCollection); | ||
|
||
Assert.True(_corsModule.DefaultCorsPolicy.AllowAnyMethod); | ||
} | ||
|
||
[Fact] | ||
public void GivenACorsConfiguration_WhenAllHeadersSet_PolicyHasAllowAnyHeader() | ||
{ | ||
_corsConfiguration.Headers.Add("*"); | ||
_corsModule.Load(_servicesCollection); | ||
|
||
Assert.True(_corsModule.DefaultCorsPolicy.AllowAnyHeader); | ||
} | ||
|
||
[Fact] | ||
public void GivenACorsConfiguration_WhenAllowCredentials_PolicyHasSupportsCredentials() | ||
{ | ||
_corsConfiguration.AllowCredentials = true; | ||
_corsModule.Load(_servicesCollection); | ||
|
||
Assert.True(_corsModule.DefaultCorsPolicy.SupportsCredentials); | ||
} | ||
|
||
[Fact] | ||
public void GivenACorsConfiguration_WhenMaxAgeSet_PolicyHasMaxAge() | ||
{ | ||
_corsConfiguration.MaxAge = 100; | ||
_corsModule.Load(_servicesCollection); | ||
|
||
Assert.Equal(TimeSpan.FromSeconds(100), _corsModule.DefaultCorsPolicy.PreflightMaxAge); | ||
} | ||
|
||
[Fact] | ||
public void GivenACorsConfiguration_WhenMultipleValuesSet_PolicyHasSpecifiedValues() | ||
{ | ||
_corsConfiguration.Origins.Add("https://example.com"); | ||
_corsConfiguration.Origins.Add("https://contoso"); | ||
|
||
_corsConfiguration.Methods.Add("PATCH"); | ||
_corsConfiguration.Methods.Add("DELETE"); | ||
|
||
_corsConfiguration.Headers.Add("authorization"); | ||
_corsConfiguration.Headers.Add("content-type"); | ||
|
||
_corsModule.Load(_servicesCollection); | ||
|
||
Assert.Equal(2, _corsModule.DefaultCorsPolicy.Origins.Count); | ||
Assert.Equal(2, _corsModule.DefaultCorsPolicy.Methods.Count); | ||
Assert.Equal(2, _corsModule.DefaultCorsPolicy.Headers.Count); | ||
|
||
Assert.Contains("https://example.com", _corsModule.DefaultCorsPolicy.Origins); | ||
Assert.Contains("https://contoso", _corsModule.DefaultCorsPolicy.Origins); | ||
|
||
Assert.Contains("PATCH", _corsModule.DefaultCorsPolicy.Methods); | ||
Assert.Contains("DELETE", _corsModule.DefaultCorsPolicy.Methods); | ||
|
||
Assert.Contains("authorization", _corsModule.DefaultCorsPolicy.Headers); | ||
Assert.Contains("content-type", _corsModule.DefaultCorsPolicy.Headers); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Linq; | ||
using EnsureThat; | ||
using Microsoft.AspNetCore.Cors.Infrastructure; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Health.Extensions.DependencyInjection; | ||
using Microsoft.Health.Fhir.Api.Configs; | ||
using Microsoft.Health.Fhir.Core.Configs; | ||
using Microsoft.Health.Fhir.Core.Features.Cors; | ||
|
||
namespace Microsoft.Health.Fhir.Api.Modules | ||
{ | ||
public class CorsModule : IStartupModule | ||
{ | ||
private readonly CorsConfiguration _corsConfiguration; | ||
|
||
public CorsModule(FhirServerConfiguration fhirServerConfiguration) | ||
{ | ||
EnsureArg.IsNotNull(fhirServerConfiguration, nameof(fhirServerConfiguration)); | ||
_corsConfiguration = fhirServerConfiguration.Cors; | ||
} | ||
|
||
internal CorsPolicy DefaultCorsPolicy { get; private set; } | ||
|
||
public void Load(IServiceCollection services) | ||
{ | ||
EnsureArg.IsNotNull(services, nameof(services)); | ||
|
||
var corsPolicyBuilder = new CorsPolicyBuilder() | ||
.WithOrigins(_corsConfiguration.Origins.ToArray()) | ||
.WithHeaders(_corsConfiguration.Headers.ToArray()) | ||
.WithMethods(_corsConfiguration.Methods.ToArray()); | ||
|
||
if (_corsConfiguration.MaxAge != null) | ||
{ | ||
corsPolicyBuilder.SetPreflightMaxAge(TimeSpan.FromSeconds(_corsConfiguration.MaxAge.Value)); | ||
} | ||
|
||
if (_corsConfiguration.AllowCredentials) | ||
{ | ||
corsPolicyBuilder.AllowCredentials(); | ||
} | ||
|
||
DefaultCorsPolicy = corsPolicyBuilder.Build(); | ||
|
||
services.AddCors(options => | ||
{ | ||
options.AddPolicy( | ||
Constants.DefaultCorsPolicy, | ||
DefaultCorsPolicy); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/Microsoft.Health.Fhir.Core/Configs/CorsConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System.Collections.Generic; | ||
|
||
namespace Microsoft.Health.Fhir.Core.Configs | ||
{ | ||
public class CorsConfiguration | ||
{ | ||
public IList<string> Origins { get; } = new List<string>(); | ||
|
||
public IList<string> Headers { get; } = new List<string>(); | ||
|
||
public IList<string> Methods { get; } = new List<string>(); | ||
|
||
public int? MaxAge { get; set; } | ||
|
||
public bool AllowCredentials { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
namespace Microsoft.Health.Fhir.Core.Features.Cors | ||
{ | ||
public static class Constants | ||
{ | ||
public const string DefaultCorsPolicy = "DefaultCorsPolicy"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Microsoft.Health.Fhir.Tests.Common; | ||
using Microsoft.Health.Fhir.Tests.E2E.Common; | ||
using Microsoft.Health.Fhir.Web; | ||
using Microsoft.Net.Http.Headers; | ||
using Xunit; | ||
using HttpMethod = System.Net.Http.HttpMethod; | ||
|
||
namespace Microsoft.Health.Fhir.Tests.E2E.Rest | ||
{ | ||
public class CorsTests : IClassFixture<HttpIntegrationTestFixture<Startup>> | ||
{ | ||
private readonly FhirClient _client; | ||
|
||
public CorsTests(HttpIntegrationTestFixture<Startup> fixture) | ||
{ | ||
_client = fixture.FhirClient; | ||
} | ||
|
||
[Fact] | ||
[Trait(Traits.Priority, Priority.One)] | ||
public async Task WhenGettingOptions_GivenAppropriateHeaders_TheServerShouldReturnTheAppropriateCorsHeaders() | ||
{ | ||
var message = new HttpRequestMessage | ||
{ | ||
Method = HttpMethod.Options, | ||
}; | ||
|
||
message.Headers.Add(HeaderNames.Origin, "https://localhost:6001"); | ||
message.Headers.Add(HeaderNames.AccessControlRequestMethod, "PUT"); | ||
message.Headers.Add(HeaderNames.AccessControlRequestHeaders, "authorization"); | ||
message.Headers.Add(HeaderNames.AccessControlRequestHeaders, "content-type"); | ||
message.RequestUri = new Uri(_client.HttpClient.BaseAddress, "/patient"); | ||
|
||
HttpResponseMessage response = await _client.HttpClient.SendAsync(message); | ||
|
||
Assert.Equal(HttpStatusCode.NoContent, response.StatusCode); | ||
Assert.Contains("https://localhost:6001", response.Headers.GetValues(HeaderNames.AccessControlAllowOrigin)); | ||
|
||
Assert.Contains("PUT", response.Headers.GetValues(HeaderNames.AccessControlAllowMethods)); | ||
|
||
Assert.Contains("authorization,content-type", response.Headers.GetValues(HeaderNames.AccessControlAllowHeaders)); | ||
|
||
Assert.Equal("1440", response.Headers.GetValues(HeaderNames.AccessControlMaxAge).First()); | ||
} | ||
} | ||
} |
Oops, something went wrong.