Add unit test for GetAdminToken endpoint
MODIFY
src/WebJobs.Script.WebHost/Controllers/HostController.cs
src/WebJobs.Script.WebHost/Controllers/HostController.cs
using Microsoft.Azure.WebJobs.Script.WebHost.Filters;
using Microsoft.Azure.WebJobs.Script.WebHost.Management;
using Microsoft.Azure.WebJobs.Script.WebHost.Models;
+using Microsoft.Azure.WebJobs.Script.WebHost.Security;
using Microsoft.Azure.WebJobs.Script.WebHost.Security.Authorization;
using Microsoft.Azure.WebJobs.Script.WebHost.Security.Authorization.Policies;
using Microsoft.Extensions.Logging;
@@ -229,6 +230,28 @@ public async Task<IActionResult> SetState([FromBody] string state)
return Accepted();
}
+ /// <summary>
+ /// This endpoint generates a temporary x-ms-site-restricted-token for core tool
+ /// to access KuduLite zipdeploy endpoint in Linux Consumption
+ /// </summary>
+ /// <returns>
+ /// 200 on token generated
+ /// 400 on non-Linux container environment
+ /// </returns>
+ [HttpGet]
+ [Route("admin/host/token")]
+ [Authorize(Policy = PolicyNames.AdminAuthLevel)]
+ public IActionResult GetAdminToken()
+ {
+ if (!_environment.IsLinuxContainerEnvironment())
+ {
+ return BadRequest("Endpoint is only available when running in Linux Container");
+ }
+
+ string requestHeaderToken = SimpleWebTokenHelper.CreateToken(DateTime.UtcNow.AddHours(2));
+ return Ok(requestHeaderToken);
+ }
+
[AcceptVerbs("GET", "POST", "DELETE")]
[Authorize(AuthenticationSchemes = AuthLevelAuthenticationDefaults.AuthenticationScheme)]
[Route("runtime/webhooks/{name}/{*extra}")]

MODIFY
test/WebJobs.Script.Tests/Controllers/Admin/HostControllerTests.cs
test/WebJobs.Script.Tests/Controllers/Admin/HostControllerTests.cs
@@ -90,5 +90,43 @@ public async Task SetState_Succeeds(string desiredState, ScriptHostState current
Assert.False(fileExists);
}
}
+
+ [Fact]
+ public void GetAdminToken_Succeeds()
+ {
+ // Arrange
+ _mockEnvironment.Setup(p => p.GetEnvironmentVariable(It.Is<string>(k => k == EnvironmentSettingNames.ContainerName))).Returns<string>(v => v = "ContainerName");
+
+ var key = TestHelpers.GenerateKeyBytes();
+ var stringKey = TestHelpers.GenerateKeyHexString(key);
+ using (new TestScopedEnvironmentVariable(EnvironmentSettingNames.WebSiteAuthEncryptionKey, stringKey))
+ {
+ // Act
+ ObjectResult result = (ObjectResult)_hostController.GetAdminToken();
+ HttpStatusCode resultStatus = (HttpStatusCode)result.StatusCode;
+
+ // Assert
+ Assert.Equal(HttpStatusCode.OK, resultStatus);
+ }
+ }
+
+ [Fact]
+ public void GetAdminToken_Fails_NotLinuxContainer()
+ {
+ // Arrange
+ _mockEnvironment.Setup(p => p.GetEnvironmentVariable(It.Is<string>(k => k == EnvironmentSettingNames.ContainerName))).Returns<string>(v => v = null);
+
+ var key = TestHelpers.GenerateKeyBytes();
+ var stringKey = TestHelpers.GenerateKeyHexString(key);
+ using (new TestScopedEnvironmentVariable(EnvironmentSettingNames.WebSiteAuthEncryptionKey, stringKey))
+ {
+ // Act
+ ObjectResult result = (ObjectResult)_hostController.GetAdminToken();
+ HttpStatusCode resultStatus = (HttpStatusCode)result.StatusCode;
+
+ // Assert
+ Assert.Equal(HttpStatusCode.BadRequest, resultStatus);
+ }
+ }
}
}

Add unit test for GetAdminToken endpoint