Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send additional data to metadata end point #1678

Merged
merged 4 commits into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public static bool RemoveTelemetryData(string key)
return telemetryData.TryRemove(key, out _);
}

internal static void SetTelemetryData(HttpRequestMessage request)
internal static void SetTelemetryData(HttpRequestMessage request, IDictionary<string, string> additionalHeaders)
{
if (request == null)
return;
Expand All @@ -130,6 +130,12 @@ internal static void SetTelemetryData(HttpRequestMessage request)
request.Headers.Remove(parameter.Key);
request.Headers.Add(parameter.Key, parameter.Value);
}

if (additionalHeaders != null)
{
foreach (var header in additionalHeaders)
request.Headers.Add(header.Key, header.Value);
}
}

internal static bool UpdateDefaultTelemetryData(string key, string value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Threading;
Expand Down Expand Up @@ -59,6 +60,8 @@ public bool SendAdditionalHeaderData
set { _sendAdditionalHeaderData = value; }
}

internal IDictionary<string, string> AdditionalHeaderData { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="HttpDocumentRetriever"/> class.
/// </summary>
Expand Down Expand Up @@ -108,7 +111,7 @@ public async Task<string> GetDocumentAsync(string address, CancellationToken can
using (var message = new HttpRequestMessage(HttpMethod.Get, uri))
{
if (SendAdditionalHeaderData)
IdentityModelTelemetryUtil.SetTelemetryData(message);
IdentityModelTelemetryUtil.SetTelemetryData(message, AdditionalHeaderData);

response = await httpClient.SendAsync(message).ConfigureAwait(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[assembly: AssemblyInformationalVersion("0.0.1")]
Expand All @@ -35,3 +36,5 @@
[assembly: AssemblyVersion("0.0.1")]
[assembly: CLSCompliant(true)]
[assembly: ComVisible(false)]

[assembly: InternalsVisibleTo("Microsoft.IdentityModel.S2S.Tokens, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9")]
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void SetTelemetry(TelemetryTheoryData theoryData)

try
{
IdentityModelTelemetryUtil.SetTelemetryData(theoryData.HttpRequestMessage);
IdentityModelTelemetryUtil.SetTelemetryData(theoryData.HttpRequestMessage, theoryData.AdditionalHeaders);
// check if the resulting headers are as expected
if (!IdentityComparer.AreEqual(theoryData.ExpectedHeaders, theoryData.HttpRequestMessage?.Headers))
throw new ArgumentException("resulting headers do not match the expected headers.");
Expand Down Expand Up @@ -82,7 +82,7 @@ public static TheoryData<TelemetryTheoryData> SetTelemetryDataTheoryData
{
HttpRequestMessage = BuildHttpRequestMessage(new Dictionary<string, string> { {"header1", "value1"} }),
ExpectedHeaders = BuildHttpRequestHeaders(new Dictionary<string, string> { {"header1", "value1"} }),
TestId = "withAdditionalHeaders"
TestId = "withAdditionalIncomingHeaders"
},
new TelemetryTheoryData
{
Expand All @@ -95,7 +95,14 @@ public static TheoryData<TelemetryTheoryData> SetTelemetryDataTheoryData
HttpRequestMessage = BuildHttpRequestMessage(new Dictionary<string, string> { { "header1", "value1" }, { IdentityModelTelemetryUtil.skuTelemetry, "some-other-value"}, {"header2", "value2"} }),
ExpectedHeaders = BuildHttpRequestHeaders(new Dictionary<string, string> { {"header1", "value1"}, {"header2", "value2"} }),
TestId = "overwriteExistingButKeepOtherHeaders"
}
},
new TelemetryTheoryData
{
HttpRequestMessage = BuildHttpRequestMessage(),
ExpectedHeaders = BuildHttpRequestHeaders(new Dictionary<string, string> { {"header1", "value1"} }),
AdditionalHeaders = new Dictionary<string, string> { { "header1", "value1" } },
TestId = "withAdditionalInputHeader"
},
};
}
}
Expand Down Expand Up @@ -186,6 +193,8 @@ public TelemetryTheoryData(): base(false)
public HttpRequestMessage HttpRequestMessage { get; set; }

public HttpRequestHeaders ExpectedHeaders { get; set; }

public IDictionary<string, string> AdditionalHeaders { get; set; }
}
}
}
Expand Down