From d4bb543f70026fa87a6975c5eb9ff9c5fa350355 Mon Sep 17 00:00:00 2001 From: karshinlin Date: Wed, 14 Apr 2021 10:47:43 -0400 Subject: [PATCH] Adding api to retrieve environment variables (#195) --- .../Diagnostics/ProcessEnvironmentInfo.cs | 24 +++++++++++++++++++ Kudu.Services.Web/Startup.cs | 3 +++ .../Diagnostics/LinuxProcessController.cs | 16 +++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 Kudu.Contracts/Diagnostics/ProcessEnvironmentInfo.cs diff --git a/Kudu.Contracts/Diagnostics/ProcessEnvironmentInfo.cs b/Kudu.Contracts/Diagnostics/ProcessEnvironmentInfo.cs new file mode 100644 index 00000000..bd40a6f2 --- /dev/null +++ b/Kudu.Contracts/Diagnostics/ProcessEnvironmentInfo.cs @@ -0,0 +1,24 @@ +using System.Collections.Generic; +using Kudu.Contracts.Infrastructure; +using Newtonsoft.Json; + +namespace Kudu.Contracts.Diagnostics +{ + public class ProcessEnvironmentInfo : Dictionary, INamedObject + { + private readonly string _name; + + public ProcessEnvironmentInfo() + { + } + + public ProcessEnvironmentInfo(string name, Dictionary values) + : base(values) + { + _name = name; + } + + [JsonProperty(PropertyName = "name")] + string INamedObject.Name { get { return _name; } } + } +} \ No newline at end of file diff --git a/Kudu.Services.Web/Startup.cs b/Kudu.Services.Web/Startup.cs index 1f8f7a24..41d9645c 100644 --- a/Kudu.Services.Web/Startup.cs +++ b/Kudu.Services.Web/Startup.cs @@ -678,6 +678,9 @@ public void Configure(IApplicationBuilder app, routes.MapHttpProcessesRoute("one-process-module", "/{id}/modules/{baseAddress}", new {controller = processControllerName, action = "GetModule"}, new {verb = new HttpMethodRouteConstraint("GET")}); + routes.MapHttpProcessesRoute("all-envs", "/{id}/environments/{filter}", + new { controller = processControllerName, action = "GetEnvironments" }, + new { verb = new HttpMethodRouteConstraint("GET") }); // Runtime routes.MapHttpRouteDual("runtime", "diagnostics/runtime", diff --git a/Kudu.Services/Diagnostics/LinuxProcessController.cs b/Kudu.Services/Diagnostics/LinuxProcessController.cs index 985057ff..01178277 100644 --- a/Kudu.Services/Diagnostics/LinuxProcessController.cs +++ b/Kudu.Services/Diagnostics/LinuxProcessController.cs @@ -1,5 +1,10 @@ +using System; +using System.Collections; using System.Diagnostics.CodeAnalysis; +using System.Linq; using System.Net; +using Kudu.Contracts.Diagnostics; +using Kudu.Services.Arm; using Microsoft.AspNetCore.Mvc; namespace Kudu.Services.Performance @@ -91,5 +96,16 @@ public IActionResult StopProfileAsync(int id) Response.StatusCode = (int)HttpStatusCode.BadRequest; return new JsonResult(ERRORMSG); } + + [HttpGet] + public IActionResult GetEnvironments(int id, string filter) + { + var envs = System.Environment.GetEnvironmentVariables() + .Cast().ToDictionary(kvp => kvp.Key.ToString(), kvp => kvp.Value.ToString()) + .Where(p => string.Equals("all", filter, StringComparison.OrdinalIgnoreCase) || p.Key.IndexOf(filter, StringComparison.OrdinalIgnoreCase) >= 0) + .ToDictionary(p => p.Key, p => p.Value); + + return Ok(ArmUtils.AddEnvelopeOnArmRequest(new ProcessEnvironmentInfo(filter, envs), Request)); + } } } \ No newline at end of file