-
Notifications
You must be signed in to change notification settings - Fork 106
Register MAR as default trusted repository #1955
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
Open
adityapatwardhan
wants to merge
5
commits into
master
Choose a base branch
from
MARAsDefault
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,8 +26,12 @@ internal static class RepositorySettings | |
| // The repository store file's location is currently only at '%LOCALAPPDATA%\PSResourceGet' for the user account. | ||
| private const string PSGalleryRepoName = "PSGallery"; | ||
| private const string PSGalleryRepoUri = "https://www.powershellgallery.com/api/v2"; | ||
| private const string MARRepoName = "MAR"; | ||
| private const string MARRepoUri = "https://mcr.microsoft.com"; | ||
| private const int DefaultPriority = 50; | ||
| private const int MARDefaultPriority = 40; | ||
| private const bool DefaultTrusted = false; | ||
| private const bool MARDefaultTrusted = true; | ||
| private const string RepositoryFileName = "PSResourceRepository.xml"; | ||
| private static readonly string RepositoryPath = Path.Combine(Environment.GetFolderPath( | ||
| Environment.SpecialFolder.LocalApplicationData), "PSResourceGet"); | ||
|
|
@@ -63,6 +67,10 @@ public static void CheckRepositoryStore() | |
| // Add PSGallery to the newly created store | ||
| Uri psGalleryUri = new Uri(PSGalleryRepoUri); | ||
| Add(PSGalleryRepoName, psGalleryUri, DefaultPriority, DefaultTrusted, repoCredentialInfo: null, repoCredentialProvider: CredentialProviderType.None, APIVersion.V2, force: false); | ||
|
|
||
| // Add MAR to the newly created store | ||
| Uri marUri = new Uri(MARRepoUri); | ||
| Add(MARRepoName, marUri, MARDefaultPriority, MARDefaultTrusted, repoCredentialInfo: null, repoCredentialProvider: CredentialProviderType.None, APIVersion.ContainerRegistry, force: false); | ||
| } | ||
|
|
||
| // Open file (which should exist now), if cannot/is corrupted then throw error | ||
|
|
@@ -85,6 +93,12 @@ public static PSRepositoryInfo AddRepository(string repoName, Uri repoUri, int r | |
| return null; | ||
| } | ||
|
|
||
| if (repoName.Equals("MAR", StringComparison.OrdinalIgnoreCase)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as above |
||
| { | ||
| errorMsg = "Cannot register MAR with -Name parameter. The MAR repository is automatically registered. Try: Reset-PSResourceRepository to restore default repositories."; | ||
| return null; | ||
| } | ||
|
|
||
| return AddToRepositoryStore(repoName, repoUri, repoPriority, repoTrusted, apiVersion, repoCredentialInfo, repoCredentialProvider, force, cmdletPassedIn, out errorMsg); | ||
| } | ||
|
|
||
|
|
@@ -187,6 +201,20 @@ public static PSRepositoryInfo UpdateRepositoryStore(string repoName, Uri repoUr | |
| return null; | ||
| } | ||
|
|
||
| // check MAR Uri is not trying to be set | ||
| if (repoName.Equals("MAR", StringComparison.OrdinalIgnoreCase) && repoUri != null) | ||
| { | ||
| errorMsg = "The MAR repository has a predefined Uri. Setting the -Uri parameter for this repository is not allowed. Please run 'Reset-PSResourceRepository' to restore default repositories."; | ||
| return null; | ||
| } | ||
|
|
||
| // check MAR CredentialInfo is not trying to be set | ||
| if (repoName.Equals("MAR", StringComparison.OrdinalIgnoreCase) && repoCredentialInfo != null) | ||
| { | ||
| errorMsg = "Setting the -CredentialInfo parameter for MAR is not allowed. Run 'Reset-PSResourceRepository' to restore default repositories."; | ||
| return null; | ||
| } | ||
|
|
||
| // determine trusted value to pass in (true/false if set, null otherwise, hence the nullable bool variable) | ||
| bool? _trustedNullable = isSet ? new bool?(repoTrusted) : new bool?(); | ||
|
|
||
|
|
@@ -908,6 +936,10 @@ public static PSRepositoryInfo Reset(out string errorMsg) | |
| Uri psGalleryUri = new Uri(PSGalleryRepoUri); | ||
| PSRepositoryInfo psGalleryRepo = Add(PSGalleryRepoName, psGalleryUri, DefaultPriority, DefaultTrusted, repoCredentialInfo: null, repoCredentialProvider: CredentialProviderType.None, APIVersion.V2, force: false); | ||
|
|
||
| // Add MAR to the newly created store | ||
| Uri marUri = new Uri(MARRepoUri); | ||
| Add(MARRepoName, marUri, MARDefaultPriority, MARDefaultTrusted, repoCredentialInfo: null, repoCredentialProvider: CredentialProviderType.None, APIVersion.ContainerRegistry, force: false); | ||
|
|
||
| // Clean up backup file on success | ||
| if (!string.IsNullOrEmpty(backupFilePath) && File.Exists(backupFilePath)) | ||
| { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|
|
||
| $modPath = "$psscriptroot/../PSGetTestUtils.psm1" | ||
| Write-Verbose -Verbose -Message "PSGetTestUtils path: $modPath" | ||
| Import-Module $modPath -Force -Verbose | ||
|
|
||
| Describe "Test MAR Repository Registration" -tags 'CI' { | ||
| BeforeEach { | ||
| $MARName = Get-MARName | ||
| $MARUri = Get-MARLocation | ||
| $PSGalleryName = Get-PSGalleryName | ||
| $PSGalleryUri = Get-PSGalleryLocation | ||
| Get-NewPSResourceRepositoryFile | ||
| } | ||
| AfterEach { | ||
| Get-RevertPSResourceRepositoryFile | ||
| } | ||
|
|
||
| Context "MAR is auto-registered with expected values" { | ||
| It "MAR repository should be present with expected default values" { | ||
| $res = Get-PSResourceRepository -Name $MARName | ||
| $res | Should -Not -BeNullOrEmpty | ||
| $res.Name | Should -Be $MARName | ||
| $res.Uri | Should -Be "$MARUri/" | ||
| $res.Trusted | Should -Be True | ||
| $res.Priority | Should -Be 40 | ||
| $res.ApiVersion | Should -Be 'ContainerRegistry' | ||
| } | ||
|
|
||
| It "MAR repository should have lower priority number (higher priority) than PSGallery" { | ||
| $mar = Get-PSResourceRepository -Name $MARName | ||
| $psGallery = Get-PSResourceRepository -Name $PSGalleryName | ||
| $mar.Priority | Should -BeLessThan $psGallery.Priority | ||
| } | ||
| } | ||
|
|
||
| Context "MAR name protection" { | ||
| It "should not allow registering MAR with -Name parameter" { | ||
| { Register-PSResourceRepository -Name "MAR" -Uri "https://mcr.microsoft.com" -ErrorAction Stop } | Should -Throw -ErrorId "ErrorInNameParameterSet,Microsoft.PowerShell.PSResourceGet.Cmdlets.RegisterPSResourceRepository" | ||
| } | ||
|
|
||
| It "should not allow registering MAR (case insensitive) with -Name parameter" { | ||
| { Register-PSResourceRepository -Name "mar" -Uri "https://mcr.microsoft.com" -ErrorAction Stop } | Should -Throw -ErrorId "ErrorInNameParameterSet,Microsoft.PowerShell.PSResourceGet.Cmdlets.RegisterPSResourceRepository" | ||
| } | ||
|
|
||
| It "should not allow registering MAR with -Name parameter in hashtable" { | ||
| Unregister-PSResourceRepository -Name $MARName | ||
| $hashtable = @{Name = "MAR"; Uri = "https://mcr.microsoft.com"} | ||
| Register-PSResourceRepository -Repository $hashtable -ErrorVariable err -ErrorAction SilentlyContinue | ||
| $err.Count | Should -BeGreaterThan 0 | ||
| $err[0].FullyQualifiedErrorId | Should -BeExactly "MARProvidedAsNameRepoPSet,Microsoft.PowerShell.PSResourceGet.Cmdlets.RegisterPSResourceRepository" | ||
| } | ||
|
|
||
| It "should not allow setting Uri for MAR repository" { | ||
| { Set-PSResourceRepository -Name $MARName -Uri "https://example.com" -ErrorAction Stop } | Should -Throw -ErrorId "ErrorInNameParameterSet,Microsoft.PowerShell.PSResourceGet.Cmdlets.SetPSResourceRepository" | ||
| } | ||
|
|
||
| It "should not allow setting CredentialInfo for MAR repository" { | ||
| $randomSecret = [System.IO.Path]::GetRandomFileName() | ||
| $credentialInfo = New-Object Microsoft.PowerShell.PSResourceGet.UtilClasses.PSCredentialInfo ("testvault", $randomSecret) | ||
| { Set-PSResourceRepository -Name $MARName -CredentialInfo $credentialInfo -ErrorAction Stop } | Should -Throw -ErrorId "ErrorInNameParameterSet,Microsoft.PowerShell.PSResourceGet.Cmdlets.SetPSResourceRepository" | ||
| } | ||
|
|
||
| It "should allow setting Trusted for MAR repository" { | ||
| Set-PSResourceRepository -Name $MARName -Trusted:$false | ||
| $res = Get-PSResourceRepository -Name $MARName | ||
| $res.Trusted | Should -Be False | ||
| } | ||
|
|
||
| It "should allow setting Priority for MAR repository" { | ||
| Set-PSResourceRepository -Name $MARName -Priority 10 | ||
| $res = Get-PSResourceRepository -Name $MARName | ||
| $res.Priority | Should -Be 10 | ||
| } | ||
| } | ||
|
|
||
| Context "Reset repository store includes MAR" { | ||
| It "Reset-PSResourceRepository should register MAR alongside PSGallery" { | ||
| Reset-PSResourceRepository -Confirm:$false | ||
| $res = Get-PSResourceRepository -Name $MARName | ||
| $res | Should -Not -BeNullOrEmpty | ||
| $res.Name | Should -Be $MARName | ||
| $res.Uri | Should -Be "$MARUri/" | ||
| $res.Trusted | Should -Be True | ||
| $res.Priority | Should -Be 40 | ||
| $res.ApiVersion | Should -Be 'ContainerRegistry' | ||
|
|
||
| $psGallery = Get-PSResourceRepository -Name $PSGalleryName | ||
| $psGallery | Should -Not -BeNullOrEmpty | ||
| } | ||
|
|
||
| It "Reset-PSResourceRepository should restore MAR after unregistration" { | ||
| Unregister-PSResourceRepository -Name $MARName | ||
| $res = Get-PSResourceRepository -Name $MARName -ErrorAction SilentlyContinue | ||
| $res | Should -BeNullOrEmpty | ||
|
|
||
| Reset-PSResourceRepository -Confirm:$false | ||
| $res = Get-PSResourceRepository -Name $MARName | ||
| $res | Should -Not -BeNullOrEmpty | ||
| $res.Name | Should -Be $MARName | ||
| $res.Uri | Should -Be "$MARUri/" | ||
| $res.Trusted | Should -Be True | ||
| $res.Priority | Should -Be 40 | ||
| } | ||
|
|
||
| It "Reset-PSResourceRepository should restore both PSGallery and MAR" { | ||
| Unregister-PSResourceRepository -Name $MARName | ||
| Unregister-PSResourceRepository -Name $PSGalleryName | ||
| Reset-PSResourceRepository -Confirm:$false | ||
|
|
||
| $mar = Get-PSResourceRepository -Name $MARName | ||
| $mar | Should -Not -BeNullOrEmpty | ||
| $mar.Priority | Should -Be 40 | ||
| $mar.Trusted | Should -Be True | ||
| $mar.ApiVersion | Should -Be 'ContainerRegistry' | ||
|
|
||
| $psGallery = Get-PSResourceRepository -Name $PSGalleryName | ||
| $psGallery | Should -Not -BeNullOrEmpty | ||
| $psGallery.Priority | Should -Be 50 | ||
| } | ||
| } | ||
|
|
||
| Context "MAR first due to higher priority" { | ||
| It "Find-PSResource Az.Accounts module from MAR" { | ||
| $res = Find-PSResource -Name "Az.Accounts" | ||
| $res | Should -Not -BeNullOrEmpty | ||
| $res.Count | Should -BeGreaterThan 0 | ||
| $firstRes = $res | Select-Object -First 1 | ||
| $firstRes.Name | Should -Be "Az.Accounts" | ||
| $firstRes.Repository | Should -Be $MARName | ||
| } | ||
|
|
||
| It 'Find-PSResource fallback to PSGallery if module not in MAR' { | ||
| $res = Find-PSResource -Name "Pscx" | ||
| $res | Should -Not -BeNullOrEmpty | ||
| $firstRes = $res | Select-Object -First 1 | ||
| $firstRes.Name | Should -Be "Pscx" | ||
| $firstRes.Repository | Should -Be $PSGalleryName | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should allow users to register MAR in case they delete MAR and would like to register it again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add SwitchParameter for "Microsoft Artifact Registery" with alias to "MAR"