Skip to content

Commit 5c3400e

Browse files
author
Manikanta Nallagatla
committed
Address comments
1 parent 9e46fdd commit 5c3400e

File tree

9 files changed

+49
-72
lines changed

9 files changed

+49
-72
lines changed

src/WebJobs.Script.WebHost/ContainerManagement/LinuxContainerInitializationHostedService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private async Task ApplyStartContextIfPresent(CancellationToken cancellationToke
4848
{
4949
_logger.LogDebug("Applying host context");
5050

51-
var encryptedAssignmentContext = JsonConvert.DeserializeObject<EncryptedHostAssignmentContext>(startContext);
51+
var encryptedAssignmentContext = JsonConvert.DeserializeObject<HostAssignmentRequest>(startContext);
5252
var assignmentContext = _startupContextProvider.SetContext(encryptedAssignmentContext);
5353
await SpecializeMSISideCar(assignmentContext);
5454

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

4-
using System.Threading.Tasks;
54
using Microsoft.AspNetCore.Authorization;
65
using Microsoft.AspNetCore.Http;
76
using Microsoft.AspNetCore.Mvc;
87
using Microsoft.Azure.WebJobs.Script.WebHost.Management;
98
using Microsoft.Azure.WebJobs.Script.WebHost.Models;
109
using Microsoft.Azure.WebJobs.Script.WebHost.Security.Authorization.Policies;
1110
using Microsoft.Extensions.Logging;
11+
using Microsoft.IdentityModel.Tokens;
12+
using System.Threading.Tasks;
1213

1314
namespace Microsoft.Azure.WebJobs.Script.WebHost.Controllers
1415
{
@@ -35,49 +36,19 @@ public InstanceController(IEnvironment environment, IInstanceManager instanceMan
3536
[HttpPost]
3637
[Route("admin/instance/assign")]
3738
[Authorize(Policy = PolicyNames.AdminAuthLevel)]
38-
public async Task<IActionResult> Assign([FromBody] EncryptedHostAssignmentContext encryptedAssignmentContext)
39+
public async Task<IActionResult> Assign([FromBody] HostAssignmentRequest hostAssignmentContext)
3940
{
40-
_logger.LogDebug($"Starting container assignment for host : {Request?.Host}. ContextLength is: {encryptedAssignmentContext.EncryptedContext?.Length}");
41-
42-
var assignmentContext = _startupContextProvider.SetContext(encryptedAssignmentContext);
43-
44-
return await AssignInternal(assignmentContext);
45-
}
46-
47-
[HttpPost]
48-
[Route("admin/instance/assign2")]
49-
[Authorize(Policy = PolicyNames.AdminAuthLevel)]
50-
public async Task<IActionResult> Assign2([FromBody] FunctionsWorkerContainerAssignmentContext workerAssignmentContext)
51-
{
52-
if (workerAssignmentContext?.AssignmentContext == null || workerAssignmentContext.AssignmentContext == null)
41+
if (string.IsNullOrEmpty(hostAssignmentContext.EncryptedContext) &&
42+
(hostAssignmentContext?.AssignmentContext == null || hostAssignmentContext.AssignmentContext == null))
5343
{
54-
return BadRequest("Assignment context is missing.");
44+
return BadRequest("Assignment context and EncryptedContext is missing.");
5545
}
5646

57-
_logger.LogDebug($"Starting container assignment for host : {Request?.Host}");
58-
var assignmentContext = _startupContextProvider.SetContext(workerAssignmentContext.AssignmentContext);
59-
60-
return await AssignInternal(assignmentContext);
61-
}
47+
var debugMessage = (hostAssignmentContext.EncryptedContext.Length > 0) ? "ContextLength is : " + hostAssignmentContext.EncryptedContext.Length : "Using unencrypted assignment context";
48+
_logger.LogDebug($"Starting container assignment for host : {Request?.Host}. {debugMessage}");
6249

63-
[HttpGet]
64-
[Route("admin/instance/info")]
65-
[Authorize(Policy = PolicyNames.AdminAuthLevel)]
66-
public IActionResult GetInstanceInfo()
67-
{
68-
return Ok(_instanceManager.GetInstanceInfo());
69-
}
50+
var assignmentContext = _startupContextProvider.SetContext(hostAssignmentContext);
7051

71-
[HttpGet]
72-
[Route("admin/instance/http-health")]
73-
public IActionResult GetHttpHealthStatus()
74-
{
75-
// Reaching here implies that http health of the container is ok.
76-
return Ok();
77-
}
78-
79-
private async Task<IActionResult> AssignInternal(HostAssignmentContext assignmentContext)
80-
{
8152
// before starting the assignment we want to perform as much
8253
// up front validation on the context as possible
8354
string error = await _instanceManager.ValidateContext(assignmentContext);
@@ -100,5 +71,21 @@ private async Task<IActionResult> AssignInternal(HostAssignmentContext assignmen
10071
? Accepted()
10172
: StatusCode(StatusCodes.Status409Conflict, "Instance already assigned");
10273
}
74+
75+
[HttpGet]
76+
[Route("admin/instance/info")]
77+
[Authorize(Policy = PolicyNames.AdminAuthLevel)]
78+
public IActionResult GetInstanceInfo()
79+
{
80+
return Ok(_instanceManager.GetInstanceInfo());
81+
}
82+
83+
[HttpGet]
84+
[Route("admin/instance/http-health")]
85+
public IActionResult GetHttpHealthStatus()
86+
{
87+
// Reaching here implies that http health of the container is ok.
88+
return Ok();
89+
}
10390
}
10491
}

src/WebJobs.Script.WebHost/Models/FunctionsWorkerContainerAssignmentContext.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/WebJobs.Script.WebHost/Models/EncryptedHostAssignmentContext.cs renamed to src/WebJobs.Script.WebHost/Models/HostAssignmentRequest.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55

66
namespace Microsoft.Azure.WebJobs.Script.WebHost.Models
77
{
8-
public class EncryptedHostAssignmentContext
8+
public class HostAssignmentRequest
99
{
1010
[JsonProperty("encryptedContext")]
1111
public string EncryptedContext { get; set; }
12+
13+
[JsonProperty("assignmentContext")]
14+
public HostAssignmentContext? AssignmentContext { get; set; }
1215
}
1316
}

src/WebJobs.Script.WebHost/StartupContextProvider.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,19 @@ private StartupContext GetStartupContextOrNull()
135135
/// Decrypt and deserialize the specified context, and apply values from it to the
136136
/// startup cache context.
137137
/// </summary>
138-
/// <param name="encryptedContext">The encrypted assignment context.</param>
138+
/// <param name="hostAssignmentRequest">The Host assignment request.</param>
139139
/// <returns>The decrypted assignment context</returns>
140-
public virtual HostAssignmentContext SetContext(EncryptedHostAssignmentContext encryptedContext)
140+
public virtual HostAssignmentContext SetContext(HostAssignmentRequest hostAssignmentRequest)
141141
{
142-
string decryptedContext = EncryptionHelper.Decrypt(encryptedContext.EncryptedContext, environment: _environment);
143-
var hostAssignmentContext = JsonConvert.DeserializeObject<HostAssignmentContext>(decryptedContext);
142+
var hostAssignmentContext = hostAssignmentRequest.AssignmentContext;
144143

145-
return SetContext(hostAssignmentContext);
146-
}
144+
if (hostAssignmentRequest == null)
145+
{
146+
147+
string decryptedContext = EncryptionHelper.Decrypt(hostAssignmentRequest.EncryptedContext, environment: _environment);
148+
hostAssignmentContext = JsonConvert.DeserializeObject<HostAssignmentContext>(decryptedContext);
149+
}
147150

148-
public virtual HostAssignmentContext SetContext(HostAssignmentContext hostAssignmentContext)
149-
{
150151
// Don't update StartupContext for warmup requests
151152
if (!hostAssignmentContext.IsWarmupRequest)
152153
{

test/WebJobs.Script.Tests.Integration/Host/StandbyManager/StandbyManagerE2ETests_Linux.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,13 +279,13 @@ private async Task Assign(string encryptionKey)
279279
Assert.Equal(HttpStatusCode.Accepted, response.StatusCode);
280280
}
281281

282-
private static EncryptedHostAssignmentContext CreateEncryptedContext(HostAssignmentContext context, string key)
282+
private static HostAssignmentRequest CreateEncryptedContext(HostAssignmentContext context, string key)
283283
{
284284
string json = JsonConvert.SerializeObject(context);
285285
var encryptionKey = Convert.FromBase64String(key);
286286
string encrypted = EncryptionHelper.Encrypt(json, encryptionKey);
287287

288-
return new EncryptedHostAssignmentContext { EncryptedContext = encrypted };
288+
return new HostAssignmentRequest { EncryptedContext = encrypted };
289289
}
290290
}
291291
}

test/WebJobs.Script.Tests.Integration/Management/InstanceControllerTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public async Task Assign_MSISpecializationFailure_ReturnsError()
8080

8181
var encryptedHostAssignmentValue = EncryptionHelper.Encrypt(JsonConvert.SerializeObject(hostAssignmentContext), TestHelpers.EncryptionKey.ToKeyBytes());
8282

83-
var encryptedHostAssignmentContext = new EncryptedHostAssignmentContext()
83+
var encryptedHostAssignmentContext = new HostAssignmentRequest()
8484
{
8585
EncryptedContext = encryptedHostAssignmentValue
8686
};
@@ -156,7 +156,7 @@ public async Task Assignment_Sets_Secrets_Context()
156156

157157
var encryptedHostAssignmentValue = EncryptionHelper.Encrypt(JsonConvert.SerializeObject(hostAssignmentContext), TestHelpers.EncryptionKey.ToKeyBytes());
158158

159-
var encryptedHostAssignmentContext = new EncryptedHostAssignmentContext()
159+
var encryptedHostAssignmentContext = new HostAssignmentRequest()
160160
{
161161
EncryptedContext = encryptedHostAssignmentValue
162162
};
@@ -209,7 +209,7 @@ public async Task Assignment_Does_Not_Set_Secrets_Context_For_Warmup_Request()
209209

210210
var encryptedHostAssignmentValue = EncryptionHelper.Encrypt(JsonConvert.SerializeObject(hostAssignmentContext), TestHelpers.EncryptionKey.ToKeyBytes());
211211

212-
var encryptedHostAssignmentContext = new EncryptedHostAssignmentContext()
212+
var encryptedHostAssignmentContext = new HostAssignmentRequest()
213213
{
214214
EncryptedContext = encryptedHostAssignmentValue
215215
};
@@ -250,7 +250,7 @@ public async Task Assignment_Invokes_InstanceManager_Methods_For_Warmup_Requests
250250
EncryptionHelper.Encrypt(JsonConvert.SerializeObject(hostAssignmentContext),
251251
TestHelpers.EncryptionKey.ToKeyBytes());
252252

253-
var encryptedHostAssignmentContext = new EncryptedHostAssignmentContext()
253+
var encryptedHostAssignmentContext = new HostAssignmentRequest()
254254
{
255255
EncryptedContext = encryptedHostAssignmentValue
256256
};

test/WebJobs.Script.Tests/ContainerManagment/LinuxContainerInitializationHostedServiceTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public async Task AssignInstanceAsyncIsAwaitedTest()
3737
_mockInstanceManager.Setup(m => m.AssignInstanceAsync(It.IsAny<HostAssignmentContext>())).Returns(tcs.Task);
3838

3939
var assignmentContext = new HostAssignmentContext();
40-
_mockStartupContextProvider.Setup(p => p.SetContext(It.IsAny<EncryptedHostAssignmentContext>())).Returns(assignmentContext);
40+
_mockStartupContextProvider.Setup(p => p.SetContext(It.IsAny<HostAssignmentRequest>())).Returns(assignmentContext);
4141

4242
var service = new TestLinuxContainerInitializationHostedService(GetTestEnvironment(), _mockInstanceManager.Object,
4343
_mockLogger.Object, _mockStartupContextProvider.Object);
@@ -65,7 +65,7 @@ public TestLinuxContainerInitializationHostedService(IEnvironment environment, I
6565

6666
protected override Task<(bool HasStartContext, string StartContext)> TryGetStartContextOrNullAsync(CancellationToken cancellationToken)
6767
{
68-
var encryptedAssignmentContext = new EncryptedHostAssignmentContext { EncryptedContext = "test" };
68+
var encryptedAssignmentContext = new HostAssignmentRequest { EncryptedContext = "test" };
6969

7070
return Task.FromResult((true, JsonConvert.SerializeObject(encryptedAssignmentContext)));
7171
}

test/WebJobs.Script.Tests/StartupContextProviderTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public void SetContext_AppliesHostAssignmentContext()
188188
};
189189
string json = JsonConvert.SerializeObject(context);
190190
string encrypted = EncryptionHelper.Encrypt(json, environment: _environment);
191-
var encryptedContext = new EncryptedHostAssignmentContext { EncryptedContext = encrypted };
191+
var encryptedContext = new HostAssignmentRequest { EncryptedContext = encrypted };
192192

193193
var result = _startupContextProvider.SetContext(encryptedContext);
194194
Assert.Equal(context.SiteName, result.SiteName);
@@ -212,7 +212,7 @@ public void Does_Not_SetContext_AppliesHostAssignmentContext_For_Warmup_Request(
212212
};
213213
string json = JsonConvert.SerializeObject(context);
214214
string encrypted = EncryptionHelper.Encrypt(json, environment: _environment);
215-
var encryptedContext = new EncryptedHostAssignmentContext { EncryptedContext = encrypted };
215+
var encryptedContext = new HostAssignmentRequest { EncryptedContext = encrypted };
216216

217217
var result = _startupContextProvider.SetContext(encryptedContext);
218218
Assert.Equal(context.SiteName, result.SiteName);

0 commit comments

Comments
 (0)