Skip to content

Commit fe9017b

Browse files
committed
Allow Internal Auth for SyncTriggers
1 parent fc747db commit fe9017b

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

src/WebJobs.Script.WebHost/Controllers/HostController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public IActionResult LaunchDebugger()
151151

152152
[HttpPost]
153153
[Route("admin/host/synctriggers")]
154-
[Authorize(Policy = PolicyNames.AdminAuthLevel)]
154+
[Authorize(Policy = PolicyNames.AdminAuthLevelOrInternal)]
155155
public async Task<IActionResult> SyncTriggers()
156156
{
157157
var result = await _functionsSyncManager.TrySyncTriggersAsync();

test/WebJobs.Script.Tests.Integration/WebHostEndToEnd/EndToEndTestFixture.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
using Microsoft.WindowsAzure.Storage.Queue;
2222
using Microsoft.WindowsAzure.Storage.Table;
2323
using Xunit;
24+
using Microsoft.Azure.WebJobs.Script.WebHost.Management;
25+
using Moq;
2426

2527
namespace Microsoft.Azure.WebJobs.Script.Tests
2628
{
@@ -60,6 +62,8 @@ protected EndToEndTestFixture(string rootPath, string testId, string functionsWo
6062

6163
public TestMetricsLogger MetricsLogger { get; private set; } = new TestMetricsLogger();
6264

65+
public Mock<IFunctionsSyncManager> FunctionsSyncManagerMock { get; private set; }
66+
6367
protected virtual ExtensionPackageReference[] GetExtensionsToInstall()
6468
{
6569
return null;
@@ -89,9 +93,13 @@ public async Task InitializeAsync()
8993
Environment.SetEnvironmentVariable(LanguageWorkerConstants.FunctionWorkerRuntimeSettingName, _functionsWorkerRuntime);
9094
}
9195

96+
FunctionsSyncManagerMock = new Mock<IFunctionsSyncManager>(MockBehavior.Strict);
97+
FunctionsSyncManagerMock.Setup(p => p.TrySyncTriggersAsync(It.IsAny<bool>())).ReturnsAsync(new SyncTriggersResult { Success = true });
98+
9299
Host = new TestFunctionHost(_copiedRootPath, logPath, webJobsBuilder =>
93100
{
94101
webJobsBuilder.Services.AddSingleton<IMetricsLogger>(_ => MetricsLogger);
102+
webJobsBuilder.Services.AddSingleton<IFunctionsSyncManager>(_ => FunctionsSyncManagerMock.Object);
95103
ConfigureJobHost(webJobsBuilder);
96104
});
97105

test/WebJobs.Script.Tests.Integration/WebHostEndToEnd/SamplesEndToEndTests_CSharp.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,37 @@ public async Task HostPing_Succeeds(string method)
9898
Assert.Equal("no-store, no-cache", cacheHeader);
9999
}
100100

101+
[Fact]
102+
public async Task SyncTriggers_InternalAuth_Succeeds()
103+
{
104+
using (new TestScopedSettings(_settingsManager, EnvironmentSettingNames.AzureWebsiteInstanceId, "testinstance"))
105+
{
106+
string uri = "admin/host/synctriggers";
107+
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, uri);
108+
HttpResponseMessage response = await _fixture.Host.HttpClient.SendAsync(request);
109+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
110+
}
111+
}
112+
113+
[Fact]
114+
public async Task SyncTriggers_ExternalUnauthorized_ReturnsUnauthorized()
115+
{
116+
string uri = "admin/host/synctriggers";
117+
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, uri);
118+
HttpResponseMessage response = await _fixture.Host.HttpClient.SendAsync(request);
119+
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
120+
}
121+
122+
[Fact]
123+
public async Task SyncTriggers_AdminLevel_Succeeds()
124+
{
125+
string uri = "admin/host/synctriggers";
126+
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, uri);
127+
request.Headers.Add(AuthenticationLevelHandler.FunctionsKeyHeaderName, await _fixture.Host.GetMasterKeyAsync());
128+
HttpResponseMessage response = await _fixture.Host.HttpClient.SendAsync(request);
129+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
130+
}
131+
101132
[Fact]
102133
public async Task HostLog_Anonymous_Fails()
103134
{

test/WebJobs.Script.Tests/Controllers/Admin/AdminControllerTests.cs renamed to test/WebJobs.Script.Tests/Controllers/Admin/FunctionsControllerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
namespace Microsoft.Azure.WebJobs.Script.Tests
2929
{
30-
public class AdminControllerTests : IDisposable
30+
public class FunctionsControllerTests : IDisposable
3131
{
3232
private readonly TempDirectory _secretsDirectory = new TempDirectory();
3333

0 commit comments

Comments
 (0)