Skip to content

Commit 3dbd0cd

Browse files
ahmelsayedmathewc
authored andcommitted
Code review feedback changes
1 parent 7dbc8aa commit 3dbd0cd

File tree

12 files changed

+24
-111
lines changed

12 files changed

+24
-111
lines changed

src/WebJobs.Script.WebHost/App_Start/AutofacBootstrap.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
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.Web.Http;
54
using Autofac;
6-
using Autofac.Integration.WebApi;
75
using Microsoft.Azure.WebJobs.Host;
86
using Microsoft.Azure.WebJobs.Script.Config;
97
using Microsoft.Azure.WebJobs.Script.WebHost.WebHooks;
@@ -12,7 +10,7 @@ namespace Microsoft.Azure.WebJobs.Script.WebHost
1210
{
1311
public static class AutofacBootstrap
1412
{
15-
internal static void Initialize(ScriptSettingsManager settingsManager, ContainerBuilder builder, WebHostSettings settings, HttpConfiguration config)
13+
internal static void Initialize(ScriptSettingsManager settingsManager, ContainerBuilder builder, WebHostSettings settings)
1614
{
1715
builder.RegisterInstance(settingsManager);
1816

@@ -26,7 +24,6 @@ internal static void Initialize(ScriptSettingsManager settingsManager, Container
2624
builder.Register<WebScriptHostManager>(ct => ct.Resolve<WebHostResolver>().GetWebScriptHostManager(settings)).ExternallyOwned();
2725
builder.Register<WebHookReceiverManager>(ct => ct.Resolve<WebHostResolver>().GetWebHookReceiverManager(settings)).ExternallyOwned();
2826
builder.RegisterInstance(settings);
29-
builder.RegisterHttpRequestMessage(config);
3027
}
3128
}
3229
}

src/WebJobs.Script.WebHost/App_Start/WebApiConfig.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static void Register(HttpConfiguration config, ScriptSettingsManager sett
4040

4141
var builder = new ContainerBuilder();
4242
builder.RegisterApiControllers(typeof(FunctionsController).Assembly);
43-
AutofacBootstrap.Initialize(settingsManager, builder, settings, config);
43+
AutofacBootstrap.Initialize(settingsManager, builder, settings);
4444

4545
// Invoke registration callback
4646
dependencyCallback?.Invoke(builder, settings);
@@ -93,7 +93,6 @@ private static WebHostSettings GetDefaultSettings(ScriptSettingsManager settings
9393
settings.ScriptPath = settingsManager.GetSetting(EnvironmentSettingNames.AzureWebJobsScriptRoot);
9494
settings.LogPath = Path.Combine(Path.GetTempPath(), @"Functions");
9595
settings.SecretsPath = HttpContext.Current.Server.MapPath("~/App_Data/Secrets");
96-
settings.IsSelfHost = true;
9796
}
9897
else
9998
{

src/WebJobs.Script.WebHost/App_Start/WebHostResolver.cs

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,8 @@ private static ScriptHostConfiguration CreateScriptHostConfiguration(WebHostSett
151151
};
152152

153153
// If running on Azure Web App, derive the host ID from the default subdomain
154-
// Otherwise, derive it from machine name and folder name
155154
string hostId = _settingsManager.AzureWebsiteDefaultSubdomain;
156155

157-
if (string.IsNullOrEmpty(hostId))
158-
{
159-
hostId = MakeValidHostId($"{Environment.MachineName}-{Path.GetFileName(Environment.CurrentDirectory)}");
160-
}
161-
162156
if (!String.IsNullOrEmpty(hostId))
163157
{
164158
// Truncate to the max host name length if needed
@@ -177,51 +171,6 @@ private static ScriptHostConfiguration CreateScriptHostConfiguration(WebHostSett
177171
return scriptHostConfig;
178172
}
179173

180-
private static string MakeValidHostId(string id)
181-
{
182-
var sb = new StringBuilder();
183-
184-
//filter for valid characters
185-
foreach (var c in id)
186-
{
187-
if (c == '-')
188-
{
189-
//dashes are valid
190-
//but it cannot start with one
191-
//nor can it have consecutive dashes
192-
if (sb.Length != 0 && sb[sb.Length - 1] != '-')
193-
{
194-
sb.Append(c);
195-
}
196-
}
197-
else if (char.IsDigit(c))
198-
{
199-
//digits are valid
200-
sb.Append(c);
201-
}
202-
else if (char.IsLetter(c))
203-
{
204-
//letters are valid but must be lowercase
205-
sb.Append(char.ToLowerInvariant(c));
206-
}
207-
}
208-
209-
//it cannot end with a dash
210-
if (sb.Length > 0 && sb[sb.Length - 1] == '-')
211-
{
212-
sb.Length -= 1;
213-
}
214-
215-
//length cannot exceed 32
216-
const int MaximumHostIdLength = 32;
217-
if (sb.Length > MaximumHostIdLength)
218-
{
219-
sb.Length = MaximumHostIdLength;
220-
}
221-
222-
return sb.ToString();
223-
}
224-
225174
private static void InitializeFileSystem(string scriptPath)
226175
{
227176
if (ScriptSettingsManager.Instance.IsAzureEnvironment)

src/WebJobs.Script.WebHost/App_Start/WebHostSettings.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,22 @@
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using Microsoft.Azure.WebJobs.Host;
5-
using Newtonsoft.Json;
65

76
namespace Microsoft.Azure.WebJobs.Script.WebHost
87
{
98
public class WebHostSettings
109
{
11-
[JsonProperty("isSelfHost")]
1210
public bool IsSelfHost { get; set; }
1311

14-
[JsonProperty("scriptPath")]
1512
public string ScriptPath { get; set; }
1613

17-
[JsonProperty("logPath")]
1814
public string LogPath { get; set; }
1915

20-
[JsonProperty("secretsPath")]
2116
public string SecretsPath { get; set; }
2217

23-
[JsonProperty("nodeDebugPort")]
24-
public int NodeDebugPort { get; set; }
18+
// Used by Cli to disable auth when running locally.
19+
public bool IsAuthDisabled { get; set; } = false;
2520

26-
[JsonIgnore]
2721
public TraceWriter TraceWriter { get; set; }
2822
}
2923
}

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,8 @@ public HttpResponseMessage Invoke(string name, [FromBody] FunctionInvocation inv
6262
[Route("admin/functions/{name}/status")]
6363
public FunctionStatus GetFunctionStatus(string name)
6464
{
65+
FunctionStatus status = new FunctionStatus();
6566
Collection<string> functionErrors = null;
66-
FunctionDescriptor function = _scriptHostManager.Instance.Functions.FirstOrDefault(p => p.Name.ToLowerInvariant() == name.ToLowerInvariant());
67-
FunctionStatus status = new FunctionStatus
68-
{
69-
Metadata = function?.Metadata
70-
};
7167

7268
// first see if the function has any errors
7369
if (_scriptHostManager.Instance.FunctionErrors.TryGetValue(name, out functionErrors))
@@ -78,6 +74,7 @@ public FunctionStatus GetFunctionStatus(string name)
7874
{
7975
// if we don't have any errors registered, make sure the function exists
8076
// before returning empty errors
77+
FunctionDescriptor function = _scriptHostManager.Instance.Functions.FirstOrDefault(p => p.Name.ToLowerInvariant() == name.ToLowerInvariant());
8178
if (function == null)
8279
{
8380
throw new HttpResponseException(HttpStatusCode.NotFound);
@@ -93,10 +90,7 @@ public HostStatus GetHostStatus()
9390
{
9491
HostStatus status = new HostStatus
9592
{
96-
Id = _scriptHostManager.Instance?.ScriptConfig.HostConfig.HostId,
97-
WebHostSettings = _webHostSettings,
98-
ProcessId = Process.GetCurrentProcess().Id,
99-
IsDebuggerAttached = Debugger.IsAttached
93+
Id = _scriptHostManager.Instance?.ScriptConfig.HostConfig.HostId
10094
};
10195

10296
var lastError = _scriptHostManager.LastError;
@@ -111,13 +105,21 @@ public HostStatus GetHostStatus()
111105

112106
[HttpPost]
113107
[Route("admin/host/debug")]
114-
public bool LaunchDebugger()
108+
public HttpResponseMessage LaunchDebugger()
115109
{
116110
if (_webHostSettings.IsSelfHost)
117111
{
118-
return Debugger.Launch();
112+
// If debugger is already running, this will be a no-op returning true.
113+
if (Debugger.Launch())
114+
{
115+
return new HttpResponseMessage(HttpStatusCode.OK);
116+
}
117+
else
118+
{
119+
return new HttpResponseMessage(HttpStatusCode.Conflict);
120+
}
119121
}
120-
return false;
122+
return new HttpResponseMessage(HttpStatusCode.NotImplemented);
121123
}
122124

123125
public override Task<HttpResponseMessage> ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public override async Task<HttpResponseMessage> ExecuteAsync(HttpControllerConte
4444
// Determine the authorization level of the request
4545
ISecretManager secretManager = controllerContext.Configuration.DependencyResolver.GetService<ISecretManager>();
4646
var settings = controllerContext.Configuration.DependencyResolver.GetService<WebHostSettings>();
47-
var authorizationLevel = settings.IsSelfHost
47+
var authorizationLevel = settings.IsAuthDisabled
4848
? AuthorizationLevel.Admin
4949
: await AuthorizationLevelAttribute.GetAuthorizationLevelAsync(request, secretManager, functionName: function.Name);
5050

src/WebJobs.Script.WebHost/Filters/AuthorizationLevelAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public async override Task OnAuthorizationAsync(HttpActionContext actionContext,
3434
ISecretManager secretManager = actionContext.ControllerContext.Configuration.DependencyResolver.GetService<ISecretManager>();
3535
var settings = actionContext.ControllerContext.Configuration.DependencyResolver.GetService<WebHostSettings>();
3636

37-
if (!settings.IsSelfHost && !await IsAuthorizedAsync(actionContext.Request, Level, secretManager))
37+
if (!settings.IsAuthDisabled && !await IsAuthorizedAsync(actionContext.Request, Level, secretManager))
3838
{
3939
actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized);
4040
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,5 @@ public class FunctionStatus
1414
/// </summary>
1515
[JsonProperty(PropertyName = "errors", DefaultValueHandling = DefaultValueHandling.Ignore)]
1616
public Collection<string> Errors { get; set; }
17-
18-
[JsonProperty(PropertyName = "metadata", DefaultValueHandling = DefaultValueHandling.Ignore)]
19-
public FunctionMetadata Metadata { get; set; }
2017
}
2118
}

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

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,5 @@ public HostStatus()
3131
/// </summary>
3232
[JsonProperty(PropertyName = "errors", DefaultValueHandling = DefaultValueHandling.Ignore)]
3333
public Collection<string> Errors { get; set; }
34-
35-
/// <summary>
36-
/// Gets or sets the web host settings.
37-
/// </summary>
38-
[JsonProperty(PropertyName = "webHostSettings", DefaultValueHandling = DefaultValueHandling.Ignore)]
39-
public WebHostSettings WebHostSettings { get; set; }
40-
41-
/// <summary>
42-
/// Gets or sets the processId for the host.
43-
/// </summary>
44-
[JsonProperty(PropertyName = "processId", DefaultValueHandling = DefaultValueHandling.Ignore)]
45-
public int ProcessId { get; set; }
46-
47-
/// <summary>
48-
/// Gets or sets whether the process has a debugger attached or not.
49-
/// </summary>
50-
[JsonProperty(PropertyName = "isDebuggerAttached", DefaultValueHandling = DefaultValueHandling.Ignore)]
51-
public bool IsDebuggerAttached { get; set; }
52-
53-
internal static string GetAssemblyFileVersion(Assembly assembly)
54-
{
55-
AssemblyFileVersionAttribute fileVersionAttr = assembly.GetCustomAttribute<AssemblyFileVersionAttribute>();
56-
return fileVersionAttr?.Version ?? "Unknown";
57-
}
5834
}
5935
}

src/WebJobs.Script/Host/ScriptHost.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,9 @@ private void RestartHost()
406406
#endif
407407
}
408408

409-
/// <summary>
410-
/// Whenever the debug marker file changes we update our debug timeout
411-
/// </summary>
409+
/// <summary>
410+
/// Whenever the debug marker file changes we update our debug timeout
411+
/// </summary>
412412
private void OnDebugModeFileChanged(object sender, FileSystemEventArgs e)
413413
{
414414
LastDebugNotify = DateTime.UtcNow;

0 commit comments

Comments
 (0)