Skip to content

Commit

Permalink
Allow user to set HTTP request headers for HTTP tasks (#46)
Browse files Browse the repository at this point in the history
* allows user to add X-User-Id and X-Auth-Token to http requests

* uses HttpHeaderReplacements value in appsettings to replace user-provided variables when building http request headers
  • Loading branch information
sei-awelle authored Nov 14, 2022
1 parent 750b259 commit 24f406c
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ public static HttpClient GetHttpClient(IHttpClientFactory httpClientFactory, str
{
var client = httpClientFactory.CreateClient();
client.BaseAddress = new Uri(apiUrl);
client.DefaultRequestHeaders.Add("authorization", $"{tokenResponse.TokenType} {tokenResponse.AccessToken}");
// Only add the header if the token was passed
if (tokenResponse != null)
{
client.DefaultRequestHeaders.Add("authorization", $"{tokenResponse.TokenType} {tokenResponse.AccessToken}");
}
return client;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ public class VmTaskProcessingOptions
public int TaskProcessMaxWaitSeconds { get; set; }
public int ExpirationCheckSeconds { get; set; }
public Dictionary<string, string> ApiParameters { get; set; }
public Dictionary<string, string> HttpHeaderReplacements { get; set; }
}
}
36 changes: 33 additions & 3 deletions Steamfitter.Api/Services/TaskExecutionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using Steamfitter.Api.Infrastructure.HealthChecks;
using System.Data;
using System.Text;
using System.Text.RegularExpressions;

namespace Steamfitter.Api.Services
{
Expand Down Expand Up @@ -596,13 +597,42 @@ private async STT.Task<string> HttpTaskTask(TaskEntity taskToExecute)
HttpResponseMessage response;
using (var scope = _scopeFactory.CreateScope())
{
// TODO: re-use tokens
var tokenResponse = await ApiClientsExtensions.GetToken(scope);
var actionParameters = JsonSerializer.Deserialize<HttpInputString>(taskToExecute.InputString);
// TODO: re-use tokens
TokenResponse tokenResponse = null;
// If the user specified headers, assume we do not need crucible auth token
if (String.IsNullOrEmpty(actionParameters.Headers))
{
tokenResponse = await ApiClientsExtensions.GetToken(scope);
}
var url = actionParameters.Url;
var client = ApiClientsExtensions.GetHttpClient(_httpClientFactory, url, tokenResponse);
if (!String.IsNullOrEmpty(actionParameters.Headers))
{
var replacementValues = _vmTaskProcessingOptions.CurrentValue.HttpHeaderReplacements;
if (replacementValues != null)
{
var headers = JsonSerializer.Deserialize<Dictionary<string, String>>(actionParameters.Headers);

// look up any replacement value and replace if found
foreach (var header in headers)
{
Match output = Regex.Match(header.Value, @"\{(\w+)\}");
if (output.Success && replacementValues.ContainsKey(output.Groups[1].Value))
{
var value = header.Value.Replace(header.Value, replacementValues[output.Groups[1].Value]);
client.DefaultRequestHeaders.Add(header.Key, value);
}
else
{
client.DefaultRequestHeaders.Add(header.Key, header.Value);
}
}
}
}
switch (taskToExecute.Action)
{

case TaskAction.http_get:
{
response = await client.GetAsync(url);
Expand Down Expand Up @@ -768,6 +798,6 @@ class HttpInputString
{
public string Url { get; set; }
public string Body { get; set; }
public string Headers { get; set; }
}

}
1 change: 1 addition & 0 deletions Steamfitter.Api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"TaskProcessIntervalMilliseconds": 5000,
"TaskProcessMaxWaitSeconds": 120,
"ExpirationCheckSeconds": 30,
"HttpHeaderReplacements": {},
"ApiParameters": {}
},
"ApplicationInsights": {
Expand Down
6 changes: 6 additions & 0 deletions Steamfitter.Api/availableCommands.json
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,12 @@
"display": "Body",
"hint": "The body to be passed to the POST request.",
"inputType": "textArea"
},
{
"key": "Headers",
"display": "Headers",
"hint": "Request headers",
"inputType": "textArea"
}
]
},
Expand Down

0 comments on commit 24f406c

Please sign in to comment.