Skip to content

Commit c5845b3

Browse files
Show allowed by policy on PSResourceRepositoryInfo
1 parent 6af98c8 commit c5845b3

File tree

4 files changed

+41
-9
lines changed

4 files changed

+41
-9
lines changed

src/PSGet.Format.ps1xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
<TableColumnHeader><Label>Uri</Label></TableColumnHeader>
9595
<TableColumnHeader><Label>Trusted</Label></TableColumnHeader>
9696
<TableColumnHeader><Label>Priority</Label></TableColumnHeader>
97+
<TableColumnHeader><Label>IsAllowedByPolicy</Label></TableColumnHeader>
9798
</TableHeaders>
9899
<TableRowEntries>
99100
<TableRowEntry>
@@ -102,6 +103,7 @@
102103
<TableColumnItem><PropertyName>Uri</PropertyName></TableColumnItem>
103104
<TableColumnItem><PropertyName>Trusted</PropertyName></TableColumnItem>
104105
<TableColumnItem><PropertyName>Priority</PropertyName></TableColumnItem>
106+
<TableColumnItem><PropertyName>IsAllowedByPolicy</PropertyName></TableColumnItem>
105107
</TableColumnItems>
106108
</TableRowEntry>
107109
</TableRowEntries>

src/code/GroupPolicyRepositoryEnforcement.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static bool IsGroupPolicyEnabled()
3737

3838
var values = ReadGPFromRegistry();
3939

40-
if (values is not null)
40+
if (values is not null && values.Count > 0)
4141
{
4242
return true;
4343
}
@@ -79,7 +79,7 @@ public static bool IsGroupPolicyEnabled()
7979
}
8080
}
8181

82-
internal static bool IsRepositoryAllowed(string repositoryUri)
82+
internal static bool IsRepositoryAllowed(Uri repositoryUri)
8383
{
8484
bool isAllowed = false;
8585

@@ -89,7 +89,7 @@ internal static bool IsRepositoryAllowed(string repositoryUri)
8989

9090
if (allowedList != null && allowedList.Length > 0)
9191
{
92-
isAllowed = allowedList.Any(uri => uri.Equals(new Uri(repositoryUri)));
92+
isAllowed = allowedList.Any(uri => uri.Equals(repositoryUri));
9393
}
9494
}
9595
else

src/code/PSRepositoryInfo.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@ public enum APIVersion
2727

2828
#region Constructor
2929

30-
public PSRepositoryInfo(string name, Uri uri, int priority, bool trusted, PSCredentialInfo credentialInfo, APIVersion apiVersion)
30+
public PSRepositoryInfo(string name, Uri uri, int priority, bool trusted, PSCredentialInfo credentialInfo, APIVersion apiVersion, bool allowed)
3131
{
3232
Name = name;
3333
Uri = uri;
3434
Priority = priority;
3535
Trusted = trusted;
3636
CredentialInfo = credentialInfo;
3737
ApiVersion = apiVersion;
38+
IsAllowedByPolicy = allowed;
3839
}
3940

4041
#endregion
@@ -88,6 +89,11 @@ public enum RepositoryProviderType
8889
/// </summary>
8990
public APIVersion ApiVersion { get; }
9091

92+
// <summary>
93+
/// is it allowed by policy
94+
/// </summary>
95+
public bool IsAllowedByPolicy { get; set; }
96+
9197
#endregion
9298
}
9399
}

src/code/RepositorySettings.cs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Management.Automation;
1010
using System.Xml;
1111
using System.Xml.Linq;
12+
using Microsoft.PowerShell.PSResourceGet.Cmdlets;
1213
using static Microsoft.PowerShell.PSResourceGet.UtilClasses.PSRepositoryInfo;
1314

1415
namespace Microsoft.PowerShell.PSResourceGet.UtilClasses
@@ -279,7 +280,9 @@ public static PSRepositoryInfo Add(string repoName, Uri repoUri, int repoPriorit
279280
throw new PSInvalidOperationException(String.Format("Adding to repository store failed: {0}", e.Message));
280281
}
281282

282-
return new PSRepositoryInfo(repoName, repoUri, repoPriority, repoTrusted, repoCredentialInfo, apiVersion);
283+
bool isAllowed = GroupPolicyRepositoryEnforcement.IsGroupPolicyEnabled() ? GroupPolicyRepositoryEnforcement.IsRepositoryAllowed(repoUri) : true;
284+
285+
return new PSRepositoryInfo(repoName, repoUri, repoPriority, repoTrusted, repoCredentialInfo, apiVersion, isAllowed);
283286
}
284287

285288
/// <summary>
@@ -436,13 +439,22 @@ public static PSRepositoryInfo Update(string repoName, Uri repoUri, int repoPrio
436439
node.Attribute(PSCredentialInfo.SecretNameAttribute).Value);
437440
}
438441

442+
if (GroupPolicyRepositoryEnforcement.IsGroupPolicyEnabled())
443+
{
444+
var allowedList = GroupPolicyRepositoryEnforcement.GetAllowedRepositoryURIs();
445+
446+
}
447+
448+
bool isAllowed = GroupPolicyRepositoryEnforcement.IsGroupPolicyEnabled() ? GroupPolicyRepositoryEnforcement.IsRepositoryAllowed(thisUrl) : true;
449+
439450
RepositoryProviderType repositoryProvider= GetRepositoryProviderType(thisUrl);
440451
updatedRepo = new PSRepositoryInfo(repoName,
441452
thisUrl,
442453
Int32.Parse(node.Attribute("Priority").Value),
443454
Boolean.Parse(node.Attribute("Trusted").Value),
444455
thisCredentialInfo,
445-
resolvedAPIVersion);
456+
resolvedAPIVersion,
457+
isAllowed);
446458

447459
// Close the file
448460
root.Save(FullRepositoryPath);
@@ -522,14 +534,18 @@ public static List<PSRepositoryInfo> Remove(string[] repoNames, out string[] err
522534

523535
string attributeUrlUriName = urlAttributeExists ? "Url" : "Uri";
524536
Uri repoUri = new Uri(node.Attribute(attributeUrlUriName).Value);
537+
538+
bool isAllowed = GroupPolicyRepositoryEnforcement.IsGroupPolicyEnabled() ? GroupPolicyRepositoryEnforcement.IsRepositoryAllowed(repoUri) : true;
539+
525540
RepositoryProviderType repositoryProvider= GetRepositoryProviderType(repoUri);
526541
removedRepos.Add(
527542
new PSRepositoryInfo(repo,
528543
new Uri(node.Attribute(attributeUrlUriName).Value),
529544
Int32.Parse(node.Attribute("Priority").Value),
530545
Boolean.Parse(node.Attribute("Trusted").Value),
531546
repoCredentialInfo,
532-
(PSRepositoryInfo.APIVersion)Enum.Parse(typeof(PSRepositoryInfo.APIVersion), node.Attribute("APIVersion").Value, ignoreCase: true)));
547+
(PSRepositoryInfo.APIVersion)Enum.Parse(typeof(PSRepositoryInfo.APIVersion), node.Attribute("APIVersion").Value, ignoreCase: true),
548+
isAllowed));
533549

534550
// Remove item from file
535551
node.Remove();
@@ -654,12 +670,16 @@ public static List<PSRepositoryInfo> Read(string[] repoNames, out string[] error
654670
}
655671

656672
RepositoryProviderType repositoryProvider= GetRepositoryProviderType(thisUrl);
673+
674+
bool isAllowed = GroupPolicyRepositoryEnforcement.IsGroupPolicyEnabled() ? GroupPolicyRepositoryEnforcement.IsRepositoryAllowed(thisUrl) : true;
675+
657676
PSRepositoryInfo currentRepoItem = new PSRepositoryInfo(repo.Attribute("Name").Value,
658677
thisUrl,
659678
Int32.Parse(repo.Attribute("Priority").Value),
660679
Boolean.Parse(repo.Attribute("Trusted").Value),
661680
thisCredentialInfo,
662-
(PSRepositoryInfo.APIVersion)Enum.Parse(typeof(PSRepositoryInfo.APIVersion), repo.Attribute("APIVersion").Value, ignoreCase: true));
681+
(PSRepositoryInfo.APIVersion)Enum.Parse(typeof(PSRepositoryInfo.APIVersion), repo.Attribute("APIVersion").Value, ignoreCase: true),
682+
isAllowed);
663683

664684
foundRepos.Add(currentRepoItem);
665685
}
@@ -758,12 +778,16 @@ public static List<PSRepositoryInfo> Read(string[] repoNames, out string[] error
758778
}
759779

760780
RepositoryProviderType repositoryProvider= GetRepositoryProviderType(thisUrl);
781+
782+
bool isAllowed = GroupPolicyRepositoryEnforcement.IsGroupPolicyEnabled() ? GroupPolicyRepositoryEnforcement.IsRepositoryAllowed(thisUrl) : true;
783+
761784
PSRepositoryInfo currentRepoItem = new PSRepositoryInfo(node.Attribute("Name").Value,
762785
thisUrl,
763786
Int32.Parse(node.Attribute("Priority").Value),
764787
Boolean.Parse(node.Attribute("Trusted").Value),
765788
thisCredentialInfo,
766-
(PSRepositoryInfo.APIVersion)Enum.Parse(typeof(PSRepositoryInfo.APIVersion), node.Attribute("APIVersion").Value, ignoreCase: true));
789+
(PSRepositoryInfo.APIVersion)Enum.Parse(typeof(PSRepositoryInfo.APIVersion), node.Attribute("APIVersion").Value, ignoreCase: true),
790+
isAllowed);
767791

768792
foundRepos.Add(currentRepoItem);
769793
}

0 commit comments

Comments
 (0)