From c465502407392ca244a489bc6ef25f0b7f0ae199 Mon Sep 17 00:00:00 2001 From: Bin Du Date: Tue, 3 Nov 2026 20:41:09 -0800 Subject: [PATCH 1/2] Generate an empty JSON object for container registry configs Upload the config blob using its matching digest and advertise its two-byte size in the OCI manifest. Add focused config and upload regression tests. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 7d4b68cd-b7b7-476b-a441-4e443201413b --- src/code/ContainerRegistryServerAPICalls.cs | 51 +++----- .../ContainerRegistryConfig.Tests.ps1 | 119 ++++++++++++++++++ 2 files changed, 139 insertions(+), 31 deletions(-) create mode 100644 test/PublishPSResourceTests/ContainerRegistryConfig.Tests.ps1 diff --git a/src/code/ContainerRegistryServerAPICalls.cs b/src/code/ContainerRegistryServerAPICalls.cs index f2ac74b90..45acf76e7 100644 --- a/src/code/ContainerRegistryServerAPICalls.cs +++ b/src/code/ContainerRegistryServerAPICalls.cs @@ -52,6 +52,7 @@ internal class ContainerRegistryServerAPICalls : ServerApiCall const string catalogScope = "&scope=registry:catalog:*"; const string grantTypeTemplate = "grant_type=access_token&service={0}{1}"; // 0 - registry, 1 - scope const string authUrlTemplate = "{0}?service={1}{2}"; // 0 - realm, 1 - service, 2 - scope + const string emptyConfigContent = "{}"; const string containerRegistryRepositoryListTemplate = "https://{0}/v2/_catalog"; // 0 - registry @@ -1568,17 +1569,16 @@ internal bool PushNupkgContainerRegistry( return false; } - // Create and upload an empty file-- needed by ContainerRegistry server - CreateAndUploadEmptyFile(outputNupkgDir, packageNameLowercase, containerRegistryAccessToken, out errRecord); + // Create config.json file + var configFilePath = System.IO.Path.Combine(outputNupkgDir, "config.json"); + _cmdletPassedIn.WriteVerbose($"Create config.json file at path: {configFilePath}"); + string configDigest = CreateConfigFile(configFilePath, out errRecord); if (errRecord != null) { return false; } - // Create config.json file - var configFilePath = System.IO.Path.Combine(outputNupkgDir, "config.json"); - _cmdletPassedIn.WriteVerbose($"Create config.json file at path: {configFilePath}"); - string configDigest = CreateConfigFile(configFilePath, out errRecord); + UploadConfigFile(configFilePath, configDigest, packageNameLowercase, containerRegistryAccessToken, out errRecord); if (errRecord != null) { return false; @@ -1667,38 +1667,27 @@ private string UploadNupkgFile(string packageNameLowercase, string containerRegi } /// - /// Uploads an empty file at the start of publish as is needed. + /// Uploads the config file referenced by the package manifest. /// - private void CreateAndUploadEmptyFile(string outputNupkgDir, string pkgNameLower, string containerRegistryAccessToken, out ErrorRecord errRecord) + private void UploadConfigFile(string configFilePath, string configDigest, string pkgNameLower, string containerRegistryAccessToken, out ErrorRecord errRecord) { - _cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::CreateAndUploadEmptyFile()"); - _cmdletPassedIn.WriteVerbose("Create an empty file"); - string emptyFileName = "empty" + Guid.NewGuid().ToString() + ".txt"; - var emptyFilePath = System.IO.Path.Combine(outputNupkgDir, emptyFileName); + _cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::UploadConfigFile()"); + errRecord = null; try { - Utils.CreateFile(emptyFilePath); - - _cmdletPassedIn.WriteVerbose("Start uploading an empty file"); - string emptyLocation = GetStartUploadBlobLocation(pkgNameLower, containerRegistryAccessToken).Result; - - _cmdletPassedIn.WriteVerbose("Computing digest for empty file"); - string emptyFileDigest = CreateDigest(emptyFilePath, out errRecord); - if (errRecord != null) - { - return; - } + _cmdletPassedIn.WriteVerbose("Start uploading the config file"); + string configLocation = GetStartUploadBlobLocation(pkgNameLower, containerRegistryAccessToken).Result; - _cmdletPassedIn.WriteVerbose("Finish uploading empty file"); - var emptyResponse = EndUploadBlob(emptyLocation, emptyFilePath, emptyFileDigest, false, containerRegistryAccessToken).Result; - bool uploadSuccessful = emptyResponse.IsSuccessStatusCode; + _cmdletPassedIn.WriteVerbose("Finish uploading the config file"); + var configResponse = EndUploadBlob(configLocation, configFilePath, configDigest, false, containerRegistryAccessToken).Result; + bool uploadSuccessful = configResponse.IsSuccessStatusCode; if (!uploadSuccessful) { errRecord = new ErrorRecord( - new UploadBlobException($"Error occurred while uploading blob, response code was: {emptyResponse.StatusCode} with reason {emptyResponse.ReasonPhrase}"), - "UploadEmptyFileError", + new UploadBlobException($"Error occurred while uploading blob, response code was: {configResponse.StatusCode} with reason {configResponse.ReasonPhrase}"), + "UploadConfigFileError", ErrorCategory.InvalidResult, _cmdletPassedIn); @@ -1709,7 +1698,7 @@ private void CreateAndUploadEmptyFile(string outputNupkgDir, string pkgNameLower { errRecord = new ErrorRecord( e, - "UploadEmptyFileError", + "UploadConfigFileError", ErrorCategory.InvalidResult, _cmdletPassedIn); @@ -1732,7 +1721,7 @@ private string CreateConfigFile(string configFilePath, out ErrorRecord errRecord try { - Utils.CreateFile(configFilePath); + File.WriteAllText(configFilePath, emptyConfigContent); _cmdletPassedIn.WriteVerbose("Computing digest for config"); configFileDigest = CreateDigest(configFilePath, out errRecord); @@ -1837,7 +1826,7 @@ private string CreateManifestContent( jsonWriter.WritePropertyName("digest"); jsonWriter.WriteValue($"sha256:{configDigest}"); jsonWriter.WritePropertyName("size"); - jsonWriter.WriteValue(0); + jsonWriter.WriteValue(Encoding.UTF8.GetByteCount(emptyConfigContent)); jsonWriter.WriteEndObject(); jsonWriter.WritePropertyName("layers"); diff --git a/test/PublishPSResourceTests/ContainerRegistryConfig.Tests.ps1 b/test/PublishPSResourceTests/ContainerRegistryConfig.Tests.ps1 new file mode 100644 index 000000000..ee891316f --- /dev/null +++ b/test/PublishPSResourceTests/ContainerRegistryConfig.Tests.ps1 @@ -0,0 +1,119 @@ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. + +Import-Module "$PSScriptRoot\..\PSGetTestUtils.psm1" -Force + +Describe 'Container registry config blob' -Tags 'CI' { + BeforeAll { + Add-Type -AssemblyName System.Net.Http + $references = @( + [System.Net.Http.HttpClient].Assembly.Location + [System.Net.HttpStatusCode].Assembly.Location + ) + Add-Type -ReferencedAssemblies $references -TypeDefinition @' +using System; +using System.Net; +using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; + +public class ConfigBlobUploadHandler : HttpMessageHandler +{ + public byte[] UploadedContent { get; private set; } + public Uri UploadUri { get; private set; } + public HttpStatusCode UploadStatusCode = HttpStatusCode.Created; + + protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + if (request.Method == HttpMethod.Post) + { + var response = new HttpResponseMessage(HttpStatusCode.Accepted); + response.Headers.Location = new Uri("/v2/test/blobs/uploads/id?upload=1", UriKind.Relative); + return response; + } + + UploadedContent = await request.Content.ReadAsByteArrayAsync(); + UploadUri = request.RequestUri; + return new HttpResponseMessage(UploadStatusCode); + } +} +'@ + + function Invoke-ContainerRegistryMethod { + [CmdletBinding()] + param($MethodName, [object[]] $MethodArguments, $Handler) + + $type = [Microsoft.PowerShell.PSResourceGet.Cmdlets.PublishPSResource].Assembly.GetType('Microsoft.PowerShell.PSResourceGet.ContainerRegistryServerAPICalls') + $repository = [Microsoft.PowerShell.PSResourceGet.UtilClasses.PSRepositoryInfo]::new( + 'Test', [uri]'https://registry.example', 50, $true, $null, 'None', 'ContainerRegistry', $true) + $api = [Activator]::CreateInstance($type, @($repository, $PSCmdlet, $null, 'test')) + $flags = [Reflection.BindingFlags]'Instance,NonPublic' + $clientProperty = $type.GetProperty('_sessionClient', $flags) + try { + if ($Handler) { + $clientProperty.GetValue($api).Dispose() + $clientProperty.SetValue($api, [System.Net.Http.HttpClient]::new($Handler)) + } + + for ($i = 0; $i -lt $MethodArguments.Length; $i++) { + if ($null -ne $MethodArguments[$i]) { + $MethodArguments[$i] = $MethodArguments[$i].PSObject.BaseObject + } + } + + $type.GetMethod($MethodName, $flags).Invoke($api, $MethodArguments) + } + finally { + $clientProperty.GetValue($api).Dispose() + } + } + } + + BeforeEach { + $configPath = Join-Path $TestDrive "$([guid]::NewGuid()).json" + $configArguments = @($configPath, $null) + $configDigest = Invoke-ContainerRegistryMethod 'CreateConfigFile' $configArguments + } + + It 'Creates a config containing exactly an empty JSON object and hashes those bytes' { + $configArguments[1] | Should -BeNullOrEmpty + $content = [IO.File]::ReadAllBytes($configPath) + $content.Length | Should -Be 2 + [Text.Encoding]::UTF8.GetString($content) | Should -BeExactly '{}' + $configDigest | Should -BeExactly (Get-FileHash $configPath -Algorithm SHA256).Hash.ToLowerInvariant() + } + + It 'Describes the config bytes in the manifest without changing the package layer' { + $manifestArguments = @( + 'packageDigest', $configDigest, [long]123, 'test.nupkg', 'test', + [Microsoft.PowerShell.PSResourceGet.UtilClasses.ResourceType]::Module, '{}') + $manifest = Invoke-ContainerRegistryMethod 'CreateManifestContent' $manifestArguments | ConvertFrom-Json + + $manifest.config.mediaType | Should -BeExactly 'application/vnd.oci.image.config.v1+json' + $manifest.config.digest | Should -BeExactly "sha256:$configDigest" + $manifest.config.size | Should -Be (Get-Item $configPath).Length + $manifest.layers.Count | Should -Be 1 + $manifest.layers[0].digest | Should -BeExactly 'sha256:packageDigest' + $manifest.layers[0].size | Should -Be 123 + } + + It 'Uploads the config JSON with its matching digest' { + $handler = [ConfigBlobUploadHandler]::new() + $uploadArguments = @($configPath, $configDigest, 'test', 'test-token', $null) + Invoke-ContainerRegistryMethod 'UploadConfigFile' $uploadArguments $handler + + $uploadArguments[4] | Should -BeNullOrEmpty + $handler.UploadedContent.Length | Should -Be 2 + [Text.Encoding]::UTF8.GetString($handler.UploadedContent) | Should -BeExactly '{}' + $handler.UploadUri.Query | Should -Match "digest=sha256:$configDigest" + } + + It 'Reports a failed config upload' { + $handler = [ConfigBlobUploadHandler]::new() + $handler.UploadStatusCode = [System.Net.HttpStatusCode]::BadRequest + $uploadArguments = @($configPath, $configDigest, 'test', 'test-token', $null) + Invoke-ContainerRegistryMethod 'UploadConfigFile' $uploadArguments $handler + + $uploadArguments[4].FullyQualifiedErrorId | Should -BeExactly 'UploadConfigFileError' + } +} From bead5f08497c2ef6d3be3a4859cd80395fa29a9f Mon Sep 17 00:00:00 2001 From: Bin Du Date: Tue, 3 Nov 2026 21:13:19 -0800 Subject: [PATCH 2/2] Use a unique config path throughout container registry publishing Choose the config filename before creating, hashing, and uploading it, and remove the helper's unreported path substitution. Cover publishing with an existing module config.json without overwriting its content. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 7d4b68cd-b7b7-476b-a441-4e443201413b --- src/code/ContainerRegistryServerAPICalls.cs | 11 ++--- .../ContainerRegistryConfig.Tests.ps1 | 49 ++++++++++++++++++- 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/src/code/ContainerRegistryServerAPICalls.cs b/src/code/ContainerRegistryServerAPICalls.cs index 45acf76e7..31ceced4b 100644 --- a/src/code/ContainerRegistryServerAPICalls.cs +++ b/src/code/ContainerRegistryServerAPICalls.cs @@ -1569,9 +1569,9 @@ internal bool PushNupkgContainerRegistry( return false; } - // Create config.json file - var configFilePath = System.IO.Path.Combine(outputNupkgDir, "config.json"); - _cmdletPassedIn.WriteVerbose($"Create config.json file at path: {configFilePath}"); + // Create config file + var configFilePath = System.IO.Path.Combine(outputNupkgDir, Guid.NewGuid().ToString() + ".json"); + _cmdletPassedIn.WriteVerbose($"Create config file at path: {configFilePath}"); string configDigest = CreateConfigFile(configFilePath, out errRecord); if (errRecord != null) { @@ -1714,11 +1714,6 @@ private string CreateConfigFile(string configFilePath, out ErrorRecord errRecord _cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::CreateConfigFile()"); string configFileDigest = string.Empty; _cmdletPassedIn.WriteVerbose("Create the config file"); - while (File.Exists(configFilePath)) - { - configFilePath = Guid.NewGuid().ToString() + ".json"; - } - try { File.WriteAllText(configFilePath, emptyConfigContent); diff --git a/test/PublishPSResourceTests/ContainerRegistryConfig.Tests.ps1 b/test/PublishPSResourceTests/ContainerRegistryConfig.Tests.ps1 index ee891316f..0cc418d6f 100644 --- a/test/PublishPSResourceTests/ContainerRegistryConfig.Tests.ps1 +++ b/test/PublishPSResourceTests/ContainerRegistryConfig.Tests.ps1 @@ -20,6 +20,7 @@ using System.Threading.Tasks; public class ConfigBlobUploadHandler : HttpMessageHandler { public byte[] UploadedContent { get; private set; } + public byte[] UploadedManifest { get; private set; } public Uri UploadUri { get; private set; } public HttpStatusCode UploadStatusCode = HttpStatusCode.Created; @@ -32,8 +33,17 @@ public class ConfigBlobUploadHandler : HttpMessageHandler return response; } - UploadedContent = await request.Content.ReadAsByteArrayAsync(); - UploadUri = request.RequestUri; + var content = await request.Content.ReadAsByteArrayAsync(); + if (request.RequestUri.AbsolutePath.Contains("/manifests/")) + { + UploadedManifest = content; + } + else + { + UploadedContent = content; + UploadUri = request.RequestUri; + } + return new HttpResponseMessage(UploadStatusCode); } } @@ -53,6 +63,7 @@ public class ConfigBlobUploadHandler : HttpMessageHandler if ($Handler) { $clientProperty.GetValue($api).Dispose() $clientProperty.SetValue($api, [System.Net.Http.HttpClient]::new($Handler)) + $type.GetField('_cachedContainterRegistryToken', $flags).SetValue($api, 'test-token') } for ($i = 0; $i -lt $MethodArguments.Length; $i++) { @@ -116,4 +127,38 @@ public class ConfigBlobUploadHandler : HttpMessageHandler $uploadArguments[4].FullyQualifiedErrorId | Should -BeExactly 'UploadConfigFileError' } + + It 'Publishes matching config bytes and manifest when the module already contains config.json' { + $outputDirectory = Join-Path $TestDrive 'nupkg' + $null = New-Item $outputDirectory -ItemType Directory + $existingConfigPath = Join-Path $outputDirectory 'config.json' + $existingConfigContent = '{"module":true}' + [IO.File]::WriteAllText($existingConfigPath, $existingConfigContent) + [IO.File]::WriteAllText((Join-Path $outputDirectory 'test.1.0.0.nupkg'), 'package') + + $handler = [ConfigBlobUploadHandler]::new() + $type = [Microsoft.PowerShell.PSResourceGet.Cmdlets.PublishPSResource].Assembly.GetType('Microsoft.PowerShell.PSResourceGet.ContainerRegistryServerAPICalls') + $versionType = $type.GetMethod('PushNupkgContainerRegistry', [Reflection.BindingFlags]'Instance,NonPublic').GetParameters()[3].ParameterType + $version = [Activator]::CreateInstance($versionType, @('1.0.0')) + $publishArguments = @( + $outputDirectory, 'test', $null, $version, + [Microsoft.PowerShell.PSResourceGet.UtilClasses.ResourceType]::Module, + @{ ModuleVersion = '1.0.0' }, @{}, $false, $null, $null) + Push-Location $TestDrive + try { + $published = Invoke-ContainerRegistryMethod 'PushNupkgContainerRegistry' $publishArguments $handler + } + finally { + Pop-Location + } + + $published | Should -Be $true + $publishArguments[9] | Should -BeNullOrEmpty + [Text.Encoding]::UTF8.GetString($handler.UploadedContent) | Should -BeExactly '{}' + $handler.UploadUri.Query | Should -Match "digest=sha256:$configDigest" + $manifest = [Text.Encoding]::UTF8.GetString($handler.UploadedManifest) | ConvertFrom-Json + $manifest.config.digest | Should -BeExactly "sha256:$configDigest" + $manifest.config.size | Should -Be $handler.UploadedContent.Length + [IO.File]::ReadAllText($existingConfigPath) | Should -BeExactly $existingConfigContent + } }