Skip to content
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
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,
}
}