From 997e0c04685f841e8575c24996fce5c50a07bf81 Mon Sep 17 00:00:00 2001 From: "Steve Lee (POWERSHELL HE/HIM) (from Dev Box)" Date: Wed, 28 May 2025 20:39:51 -0700 Subject: [PATCH 1/3] Fix registry resource to handle deleting non-existing reg value/key --- registry/src/registry_helper.rs | 14 +++++++++-- registry/tests/registry.config.set.tests.ps1 | 26 ++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/registry/src/registry_helper.rs b/registry/src/registry_helper.rs index 5263eab84..b26bf1ebf 100644 --- a/registry/src/registry_helper.rs +++ b/registry/src/registry_helper.rs @@ -200,7 +200,12 @@ impl RegistryHelper { Err(e) => return Err(e), }; if let Some(value_name) = &self.config.value_name { - reg_key.delete_value(value_name)?; + match reg_key.delete_value(value_name) { + Ok(_) | Err(value::Error::NotFound(_, _)) => { + // if the value doesn't exist, we don't need to do anything + }, + Err(e) => return Err(RegistryError::RegistryValue(e)), + } } else { // to delete the key, we need to open the parent key first let parent_path = get_parent_key_path(&self.config.key_path); @@ -215,7 +220,12 @@ impl RegistryHelper { return Err(RegistryError::Utf16Conversion("subkey_name".to_string())); }; - parent_reg_key.delete(subkey_name, true)?; + match parent_reg_key.delete(subkey_name, true) { + Ok(_) | Err(key::Error::NotFound(_, _)) => { + // if the subkey doesn't exist, we don't need to do anything + }, + Err(e) => return Err(RegistryError::RegistryKey(e)), + } } Ok(()) } diff --git a/registry/tests/registry.config.set.tests.ps1 b/registry/tests/registry.config.set.tests.ps1 index 2d6289a56..28a564ab9 100644 --- a/registry/tests/registry.config.set.tests.ps1 +++ b/registry/tests/registry.config.set.tests.ps1 @@ -99,4 +99,30 @@ Describe 'registry config set tests' { Remove-Item -Path 'HKCU:\1' -Recurse -ErrorAction Ignore } + + It 'Should succeed when _exist is false and value does not exist' -Skip:(!$IsWindows) { + $config = @{ + '$schema' = 'https://aka.ms/dsc/schemas/v3/bundled/config/document.json' + resources = @( + @{ + name = 'reg' + type = 'Microsoft.Windows/Registry' + properties = @{ + keyPath = 'HKCU' + valueName = 'Test' + valueData = @{ + String = 'Test' + } + _exist = $false + } + } + ) + } + + $out = dsc config set -i ($config | ConvertTo-Json -Depth 10) | ConvertFrom-Json + $LASTEXITCODE | Should -Be 0 + $out.results[0].result.afterState._exist | Should -Be $false + + Get-ItemProperty -Path 'HKCU:\1\2' -Name 'Test' -ErrorAction Ignore | Should -BeNullOrEmpty + } } From 2e0f1d79ad41a9aad2fc76fabc8e231bf3c9a694 Mon Sep 17 00:00:00 2001 From: "Steve Lee (POWERSHELL HE/HIM) (from Dev Box)" Date: Wed, 28 May 2025 20:43:01 -0700 Subject: [PATCH 2/3] fix clippy --- registry/src/registry_helper.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/registry/src/registry_helper.rs b/registry/src/registry_helper.rs index b26bf1ebf..deaf01f8d 100644 --- a/registry/src/registry_helper.rs +++ b/registry/src/registry_helper.rs @@ -201,7 +201,7 @@ impl RegistryHelper { }; if let Some(value_name) = &self.config.value_name { match reg_key.delete_value(value_name) { - Ok(_) | Err(value::Error::NotFound(_, _)) => { + Ok(()) | Err(value::Error::NotFound(_, _)) => { // if the value doesn't exist, we don't need to do anything }, Err(e) => return Err(RegistryError::RegistryValue(e)), @@ -221,7 +221,7 @@ impl RegistryHelper { }; match parent_reg_key.delete(subkey_name, true) { - Ok(_) | Err(key::Error::NotFound(_, _)) => { + Ok(()) | Err(key::Error::NotFound(_, _)) => { // if the subkey doesn't exist, we don't need to do anything }, Err(e) => return Err(RegistryError::RegistryKey(e)), From 2e55eb8a9e6d59326384b37d1854e5b12b9ec947 Mon Sep 17 00:00:00 2001 From: "Steve Lee (POWERSHELL HE/HIM) (from Dev Box)" Date: Wed, 28 May 2025 20:45:37 -0700 Subject: [PATCH 3/3] add test for reg key --- registry/tests/registry.config.set.tests.ps1 | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/registry/tests/registry.config.set.tests.ps1 b/registry/tests/registry.config.set.tests.ps1 index 28a564ab9..bb29e5dd7 100644 --- a/registry/tests/registry.config.set.tests.ps1 +++ b/registry/tests/registry.config.set.tests.ps1 @@ -125,4 +125,24 @@ Describe 'registry config set tests' { Get-ItemProperty -Path 'HKCU:\1\2' -Name 'Test' -ErrorAction Ignore | Should -BeNullOrEmpty } + + It 'Should succeed when _exist is false and key does not exist' -Skip:(!$IsWindows) { + $config = @{ + '$schema' = 'https://aka.ms/dsc/schemas/v3/bundled/config/document.json' + resources = @( + @{ + name = 'reg' + type = 'Microsoft.Windows/Registry' + properties = @{ + keyPath = 'HKCU\1' + _exist = $false + } + } + ) + } + + $out = dsc config set -i ($config | ConvertTo-Json -Depth 10) | ConvertFrom-Json + $LASTEXITCODE | Should -Be 0 + $out.results[0].result.afterState._exist | Should -Be $false + } }