Skip to content

Commit

Permalink
Add Program Context to UserAgent Header (#517)
Browse files Browse the repository at this point in the history
Add Program context comment to user agent header to enable tracking of
usage across our credprovider wrappers (ex conda, artifacts-keyring)


example header with changes:
`(NuGet) CredentialProvider.Microsoft/1.2.1 (Windows; X64; Microsoft
Windows 10.0.22631) CLR/6.0.33 (.NETCoreApp,Version=v6.0; win10-x64;
.NET 6.0.33)`

before:
`CredentialProvider.Microsoft/1.2.1 (Windows; X64; Microsoft Windows
10.0.22631) CLR/6.0.33 (.NETCoreApp,Version=v6.0; win10-x64; .NET
6.0.33)`
  • Loading branch information
embetten committed Sep 19, 2024
1 parent d6adc53 commit 0ba6c4e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
7 changes: 5 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"name": "net 6 launch",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
Expand All @@ -13,7 +13,10 @@
],
"cwd": "${workspaceFolder}/CredentialProvider.Microsoft",
"console": "integratedTerminal",
"stopAtEntry": false
"stopAtEntry": false,
"env": {
"ARTIFACTS_CREDENTIALPROVIDER_PROGRAM_CONTEXT":"nuGET"
}
},
{
"name": ".NET Core Attach",
Expand Down
1 change: 1 addition & 0 deletions CredentialProvider.Microsoft/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public static async Task<int> Main(string[] args)
multiLogger.Add(new PluginConnectionLogger(plugin.Connection));
multiLogger.Verbose(Resources.RunningInPlugin);
multiLogger.Verbose(string.Format(Resources.CommandLineArgs, PlatformInformation.GetProgramVersion(), Environment.CommandLine));
EnvUtil.SetProgramContextInEnvironment(Context.NuGet);

await WaitForPluginExitAsync(plugin, multiLogger, TimeSpan.FromMinutes(2)).ConfigureAwait(continueOnCapturedContext: false);
}
Expand Down
20 changes: 20 additions & 0 deletions CredentialProvider.Microsoft/Util/EnvUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public static class EnvUtil
public const string EndpointCredentials = "ARTIFACTS_CREDENTIALPROVIDER_FEED_ENDPOINTS";
public const string BuildTaskExternalEndpoints = "VSS_NUGET_EXTERNAL_FEED_ENDPOINTS";

public const string ProgramContext = "ARTIFACTS_CREDENTIALPROVIDER_PROGRAM_CONTEXT";

public static bool GetLogPIIEnabled()
{
return GetEnabledFromEnvironment(LogPIIEnvVar, defaultValue: false);
Expand Down Expand Up @@ -182,6 +184,24 @@ public static int GetDeviceFlowTimeoutFromEnvironmentInSeconds(ILogger logger)
return null;
}

public static Context? GetProgramContextFromEnvironment()
{
var context = Environment.GetEnvironmentVariable(ProgramContext);

if (!string.IsNullOrWhiteSpace(context) && Enum.TryParse<Context>(context, ignoreCase: true, out Context result))
{
return result;
}

return null;
}

public static void SetProgramContextInEnvironment(Context context)
{
Environment.SetEnvironmentVariable(ProgramContext, context.ToString());
return;
}

private static bool GetEnabledFromEnvironment(string envVar, bool defaultValue = true)
{
if (bool.TryParse(Environment.GetEnvironmentVariable(envVar), out bool result))
Expand Down
26 changes: 26 additions & 0 deletions CredentialProvider.Microsoft/Util/HttpClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Licensed under the MIT license.

using System.Net.Http;
using System.Net.Http.Headers;
using Microsoft.Artifacts.Authentication;

namespace NuGetCredentialProvider.Util
Expand All @@ -26,7 +27,32 @@ static HttpClientFactory()
UseDefaultCredentials = true
});

// Add program context to headers if available
if (ProgramContext != null)
{
httpClient.DefaultRequestHeaders.UserAgent.Add(ProgramContext);
}

httpClientFactory = new(httpClient);
}

private static ProductInfoHeaderValue ProgramContext
{
get
{
var context = EnvUtil.GetProgramContextFromEnvironment();
return context != null
? new ProductInfoHeaderValue($"({context})")
: null;
}
}
}

public enum Context
{
Maven,
NuGet,
Pip,
Conda,
}
}

0 comments on commit 0ba6c4e

Please sign in to comment.