Skip to content

Commit fe270a1

Browse files
committed
Bug fixes and clean up
1 parent 6d52514 commit fe270a1

File tree

4 files changed

+86
-30
lines changed

4 files changed

+86
-30
lines changed

src/code/CredentialProvider.cs

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44
using System.Security;
55
using System.Management.Automation;
66
using System.Text.Json;
7+
using System.Net.Http;
8+
using System.Net;
79

810
namespace Microsoft.PowerShell.PSResourceGet.UtilClasses
911
{
1012
internal static class CredentialProvider
1113
{
14+
private static readonly string _credProviderExe = "CredentialProvider.Microsoft.exe";
15+
private static readonly string _credProviderDll = "CredentialProvider.Microsoft.dll";
16+
1217
private static string FindCredProviderFromPluginsPath()
1318
{
1419
// Get environment variable "NUGET_PLUGIN_PATHS"
@@ -33,16 +38,16 @@ private static string FindCredProviderFromDefaultLocation()
3338
{
3439
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
3540
{
36-
credProviderPath = Path.Combine(netCorePath, "CredentialProvider.Microsoft.exe");
41+
credProviderPath = Path.Combine(netCorePath, _credProviderExe);
3742
}
3843
else
3944
{
40-
credProviderPath = Path.Combine(netCorePath, "CredentialProvider.Microsoft.dll");
45+
credProviderPath = Path.Combine(netCorePath, _credProviderDll);
4146
}
4247
}
4348
else if (Directory.Exists(netFxPath) && Environment.OSVersion.Platform == PlatformID.Win32NT)
4449
{
45-
credProviderPath = Path.Combine(netFxPath, "CredentialProvider.Microsoft.exe");
50+
credProviderPath = Path.Combine(netFxPath, _credProviderExe);
4651
}
4752

4853
return credProviderPath;
@@ -62,11 +67,11 @@ private static string FindCredProviderFromVSLocation(out ErrorRecord error)
6267
{
6368
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
6469
{
65-
credProviderPath = VSCredentialProviderFile(visualStudioPath, "CredentialProvider.Microsoft.exe", out error);
70+
credProviderPath = VSCredentialProviderFile(visualStudioPath, _credProviderExe, out error);
6671
}
6772
else if (string.IsNullOrEmpty(credProviderPath))
6873
{
69-
credProviderPath = VSCredentialProviderFile(visualStudioPath, "CredentialProvider.Microsoft.dll", out error);
74+
credProviderPath = VSCredentialProviderFile(visualStudioPath, _credProviderDll, out error);
7075
}
7176
}
7277

@@ -108,8 +113,9 @@ private static string VSCredentialProviderFile(string visualStudioPath, string c
108113

109114
internal static PSCredential GetCredentialsFromProvider(Uri uri, PSCmdlet cmdletPassedIn)
110115
{
116+
cmdletPassedIn.WriteDebug("Enterting CredentialProvider::GetCredentialsFromProvider");
111117
string credProviderPath = string.Empty;
112-
118+
113119
// Find credential provider
114120
// Option 1. Use env var 'NUGET_PLUGIN_PATHS' to find credential provider.
115121
// See: https://docs.microsoft.com/en-us/nuget/reference/extensibility/nuget-cross-platform-plugins#plugin-installation-and-discovery
@@ -137,25 +143,47 @@ internal static PSCredential GetCredentialsFromProvider(Uri uri, PSCmdlet cmdlet
137143
}
138144
}
139145

146+
cmdletPassedIn.WriteDebug($"Credential provider path is '{credProviderPath}'");
140147
if (string.IsNullOrEmpty(credProviderPath))
141148
{
142149
cmdletPassedIn.WriteError(new ErrorRecord(
143150
new ArgumentNullException("Path to the Azure Artifacts Credential Provider is null or empty. See https://github.com/NuGet/Home/wiki/NuGet-cross-plat-authentication-plugin#plugin-installation-and-discovery to set up the Credential Provider."),
144151
"CredentialProviderPathIsNullOrEmpty",
145152
ErrorCategory.InvalidArgument,
146-
null));
153+
credProviderPath));
147154
return null;
148155
}
149156

150-
// Check case sensitivity here
151157
if (!File.Exists(credProviderPath))
152158
{
153-
cmdletPassedIn.WriteError(new ErrorRecord(
154-
new FileNotFoundException($"Path found '{credProviderPath}' is not a valid Azure Artifact Credential Provider executable. See https://github.com/NuGet/Home/wiki/NuGet-cross-plat-authentication-plugin#plugin-installation-and-discovery to set up the Credential Provider."),
155-
"CredentialProviderFileNotFound",
156-
ErrorCategory.ObjectNotFound,
157-
null));
158-
return null;
159+
// If the Credential Provider is not found on a Unix machine, try looking for a case insensitive file.
160+
if (Environment.OSVersion.Platform == PlatformID.Unix)
161+
{
162+
FileInfo fileInfo = new FileInfo(credProviderPath);
163+
string resolvedFilePath = Utils.GetCaseInsensitiveFilePath(fileInfo.Directory.FullName, _credProviderDll);
164+
if (resolvedFilePath != null)
165+
{
166+
credProviderPath = resolvedFilePath;
167+
}
168+
else
169+
{
170+
cmdletPassedIn.WriteError(new ErrorRecord(
171+
new FileNotFoundException($"Path found '{credProviderPath}' is not a valid Azure Artifact Credential Provider executable. See https://github.com/NuGet/Home/wiki/NuGet-cross-plat-authentication-plugin#plugin-installation-and-discovery to set up the Credential Provider."),
172+
"CredentialProviderFileNotFound",
173+
ErrorCategory.ObjectNotFound,
174+
credProviderPath));
175+
}
176+
}
177+
else
178+
{
179+
cmdletPassedIn.WriteError(new ErrorRecord(
180+
new FileNotFoundException($"Path found '{credProviderPath}' is not a valid Azure Artifact Credential Provider executable. See https://github.com/NuGet/Home/wiki/NuGet-cross-plat-authentication-plugin#plugin-installation-and-discovery to set up the Credential Provider."),
181+
"CredentialProviderFileNotFound",
182+
ErrorCategory.ObjectNotFound,
183+
credProviderPath));
184+
185+
return null;
186+
}
159187
}
160188

161189
cmdletPassedIn.WriteVerbose($"Credential Provider path found at: '{credProviderPath}'");
@@ -234,7 +262,7 @@ internal static PSCredential GetCredentialsFromProvider(Uri uri, PSCmdlet cmdlet
234262
new ArgumentNullException("Credential Provider username is null or empty. See https://github.com/NuGet/Home/wiki/NuGet-cross-plat-authentication-plugin#plugin-installation-and-discovery for more info."),
235263
"CredentialProviderUserNameIsNullOrEmpty",
236264
ErrorCategory.InvalidArgument,
237-
null));
265+
credProviderPath));
238266
return null;
239267
}
240268

@@ -247,7 +275,7 @@ internal static PSCredential GetCredentialsFromProvider(Uri uri, PSCmdlet cmdlet
247275
new ArgumentNullException("Credential Provider password is null or empty. See https://github.com/NuGet/Home/wiki/NuGet-cross-plat-authentication-plugin#plugin-installation-and-discovery for more info."),
248276
"CredentialProviderUserNameIsNullOrEmpty",
249277
ErrorCategory.InvalidArgument,
250-
null));
278+
credProviderPath));
251279
return null;
252280
}
253281

@@ -262,7 +290,7 @@ internal static PSCredential GetCredentialsFromProvider(Uri uri, PSCmdlet cmdlet
262290
new Exception("Error retrieving credentials from Credential Provider. See https://github.com/NuGet/Home/wiki/NuGet-cross-plat-authentication-plugin#plugin-installation-and-discovery for more info.", e),
263291
"InvalidCredentialProviderResponse",
264292
ErrorCategory.InvalidResult,
265-
null));
293+
credProviderPath));
266294
return null;
267295
}
268296

src/code/RegisterPSResourceRepository.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,13 @@ class RegisterPSResourceRepository : PSCmdlet, IDynamicParameters
117117

118118
public object GetDynamicParameters()
119119
{
120-
if (Uri.Contains("pkgs.dev.azure.com") || Uri.Contains("pkgs.visualstudio.com"))
120+
if(Uri.EndsWith(".azurecr.io") || Uri.EndsWith(".azurecr.io/") || Uri.Contains("mcr.microsoft.com"))
121121
{
122-
_credentialProvider = new CredentialProviderDynamicParameters();
123-
return _credentialProvider;
122+
return null;
124123
}
125124

126-
return null;
125+
_credentialProvider = new CredentialProviderDynamicParameters();
126+
return _credentialProvider;
127127
}
128128

129129
#endregion
@@ -144,7 +144,7 @@ protected override void ProcessRecord()
144144
repoApiVersion = ApiVersion;
145145
}
146146

147-
PSRepositoryInfo.CredentialProviderType credentialProvider = _credentialProvider.CredentialProvider;
147+
PSRepositoryInfo.CredentialProviderType? credentialProvider = _credentialProvider?.CredentialProvider;
148148

149149
switch (ParameterSetName)
150150
{
@@ -438,10 +438,22 @@ private PSRepositoryInfo RepoValidationHelper(Hashtable repo)
438438

439439
public class CredentialProviderDynamicParameters
440440
{
441+
PSRepositoryInfo.CredentialProviderType? _credProvider = null;
442+
441443
/// <summary>
442444
/// Specifies which credential provider to use.
443445
/// </summary>
444446
[Parameter]
445-
public PSRepositoryInfo.CredentialProviderType CredentialProvider { get; set; }
447+
public PSRepositoryInfo.CredentialProviderType? CredentialProvider {
448+
get
449+
{
450+
return _credProvider;
451+
}
452+
453+
set
454+
{
455+
_credProvider = value;
456+
}
457+
}
446458
}
447459
}

src/code/SetPSResourceRepository.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,14 @@ public SwitchParameter Trusted
109109
public object GetDynamicParameters()
110110
{
111111
PSRepositoryInfo repository = RepositorySettings.Read(new[] { Name }, out string[] _).FirstOrDefault();
112-
if (repository is not null &&
113-
(repository.Uri.AbsoluteUri.Contains("pkgs.dev.azure.com") || repository.Uri.AbsoluteUri.Contains("pkgs.visualstudio.com")))
112+
if (repository is not null &&
113+
(repository.Uri.AbsoluteUri.EndsWith(".azurecr.io") || repository.Uri.AbsoluteUri.EndsWith(".azurecr.io/") || repository.Uri.AbsoluteUri.Contains("mcr.microsoft.com")))
114114
{
115-
_credentialProvider = new CredentialProviderDynamicParameters();
116-
return _credentialProvider;
115+
return null;
117116
}
118117

119-
return null;
118+
_credentialProvider = new CredentialProviderDynamicParameters();
119+
return _credentialProvider;
120120
}
121121

122122
#endregion
@@ -161,7 +161,7 @@ protected override void ProcessRecord()
161161
repoApiVersion = ApiVersion;
162162
}
163163

164-
PSRepositoryInfo.CredentialProviderType credentialProvider = _credentialProvider.CredentialProvider;
164+
PSRepositoryInfo.CredentialProviderType? credentialProvider = _credentialProvider?.CredentialProvider;
165165

166166
List<PSRepositoryInfo> items = new List<PSRepositoryInfo>();
167167

@@ -306,7 +306,7 @@ private PSRepositoryInfo RepoValidationHelper(Hashtable repo)
306306
return null;
307307
}
308308

309-
PSRepositoryInfo.CredentialProviderType credentialProvider = _credentialProvider.CredentialProvider;
309+
PSRepositoryInfo.CredentialProviderType? credentialProvider = _credentialProvider?.CredentialProvider;
310310

311311
try
312312
{

src/code/Utils.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,6 +1238,22 @@ internal static void GetMetadataFilesFromPath(string dirPath, string packageName
12381238
}
12391239
}
12401240
}
1241+
1242+
internal static string GetCaseInsensitiveFilePath(string directory, string fileName)
1243+
{
1244+
var files = Directory.GetFiles(directory);
1245+
foreach (var file in files)
1246+
{
1247+
if (string.Equals(Path.GetFileName(file), fileName, StringComparison.OrdinalIgnoreCase))
1248+
{
1249+
return file;
1250+
}
1251+
}
1252+
1253+
// File not found
1254+
return null;
1255+
}
1256+
12411257
#endregion
12421258

12431259
#region PSDataFile parsing

0 commit comments

Comments
 (0)