Skip to content

Commit 2f38e3b

Browse files
buminksspan786
authored andcommitted
Add support for Sensitive data in registry_value
- Updated REFERENCE.md to include Sensitive[String] in the data type options. - Enhanced registry_value provider to unwrap Sensitive values before processing. - Modified registry_value type to handle Sensitive data during value munging. - Updated value.pp documentation to demonstrate usage of Sensitive types. - Added unit tests to verify support for Sensitive data across different types.
1 parent 58bb4f7 commit 2f38e3b

6 files changed

Lines changed: 93 additions & 12 deletions

File tree

REFERENCE.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,13 @@ Data type:
164164
Optional[Variant[
165165
String,
166166
Numeric,
167-
Array[String]
167+
Array[String],
168+
Sensitive[String]
168169
]]
169170
```
170171

171-
The data to place inside the registry value.
172+
The data to place inside the registry value. Can be a String, Numeric, Array[String],
173+
or Sensitive[String] for sensitive data like passwords.
172174

173175
Default value: `undef`
174176

examples/sensitive_example.pp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Example demonstrating Sensitive[String] support in registry::value
2+
# This example shows how to use Sensitive types for sensitive data like passwords
3+
4+
# Create a sensitive password value
5+
$sensitive_password = Sensitive('mysecretpassword123')
6+
7+
# Use the sensitive password in a registry value
8+
registry::value { 'DefaultPassword':
9+
key => 'HKLM\Software\MyApp',
10+
data => $sensitive_password,
11+
type => 'string',
12+
}
13+
14+
# You can also use it directly inline
15+
registry::value { 'ApiKey':
16+
key => 'HKLM\Software\MyApp',
17+
data => Sensitive('sk-1234567890abcdef'),
18+
type => 'string',
19+
}
20+
21+
# For array types, you can mix sensitive and non-sensitive values
22+
registry::value { 'MixedArray':
23+
key => 'HKLM\Software\MyApp',
24+
data => ['public_value', Sensitive('secret_value'), 'another_public'],
25+
type => 'array',
26+
}

lib/puppet/provider/registry_value/registry.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,27 @@ def to_native(ptype, pdata)
117117
# array to something usable by the Win API.
118118
raise Puppet::Error, 'Data should be an Array (ErrorID 37D9BBAB-52E8-4A7C-9F2E-D7BF16A59050)' unless pdata.is_a?(Array)
119119

120+
# Unwrap Sensitive values if present
121+
unwrapped_data = pdata.map do |item|
122+
if item.is_a?(Puppet::Pops::Types::PSensitiveType::Sensitive)
123+
item.unwrap
124+
else
125+
item
126+
end
127+
end
128+
120129
ndata =
121130
case ptype
122131
when :binary
123-
pdata.first.scan(%r{[a-f\d]{2}}i).map { |byte| [byte].pack('H2') }.join
132+
unwrapped_data.first.scan(%r{[a-f\d]{2}}i).map { |byte| [byte].pack('H2') }.join
124133
when :array
125134
# We already have an array, and the native API write method takes an
126135
# array, so send it thru.
127-
pdata
136+
unwrapped_data
128137
else
129138
# Since we have an array, take the first element and send it to the
130139
# native API which is expecting a scalar.
131-
pdata.first
140+
unwrapped_data.first
132141
end
133142

134143
[PuppetX::Puppetlabs::Registry.name2type(ptype), ndata]

lib/puppet/type/registry_value.rb

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,25 @@ def self.title_patterns
115115
end
116116

117117
munge do |value|
118+
# Unwrap Sensitive values if present
119+
unwrapped_value = if value.is_a?(Puppet::Pops::Types::PSensitiveType::Sensitive)
120+
value.unwrap
121+
else
122+
value
123+
end
124+
118125
case resource[:type]
119126
when :dword, :qword
120127
begin
121-
Integer(value)
128+
Integer(unwrapped_value)
122129
rescue StandardError
123130
nil
124131
end
125132
when :binary
126-
munged = if (value.respond_to?(:length) && value.length == 1) || (value.is_a?(Integer) && value <= 9)
127-
"0#{value}"
133+
munged = if (unwrapped_value.respond_to?(:length) && unwrapped_value.length == 1) || (unwrapped_value.is_a?(Integer) && unwrapped_value <= 9)
134+
"0#{unwrapped_value}"
128135
else
129-
value
136+
unwrapped_value
130137
end
131138

132139
# First, strip out all spaces from the string in the manfest. Next,
@@ -138,7 +145,7 @@ def self.title_patterns
138145
.rstrip
139146
.downcase
140147
else # :string, :expand, :array
141-
value
148+
unwrapped_value
142149
end
143150
end
144151

manifests/value.pp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
# `puppet describe registry_value` for a list of supported types in the
1818
# "type" parameter.
1919
# @param data
20-
# The data to place inside the registry value.
20+
# The data to place inside the registry value. Can be a String, Numeric, Array[String],
21+
# or Sensitive[String] for sensitive data like passwords.
2122
#
2223
# Actions:
2324
# - Manage the parent key if not already managed.
@@ -36,14 +37,24 @@
3637
# }
3738
# }
3839
#
40+
# @example This example shows how to use Sensitive types for sensitive data like passwords.
41+
# class myapp {
42+
# registry::value { 'DefaultPassword':
43+
# key => 'HKLM\Software\MyApp',
44+
# data => Sensitive('mysecretpassword123'),
45+
# type => 'string',
46+
# }
47+
# }
48+
#
3949
define registry::value (
4050
Pattern[/^\w+/] $key,
4151
Optional[String] $value = undef,
4252
Pattern[/^\w+/] $type = 'string',
4353
Optional[Variant[
4454
String,
4555
Numeric,
46-
Array[String]
56+
Array[String],
57+
Sensitive[String]
4758
]] $data = undef,
4859
) {
4960
# ensure windows os

spec/unit/puppet/type/registry_value_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,5 +237,31 @@
237237
end
238238
end
239239
end
240+
241+
context 'when sensitive data' do
242+
it 'supports Sensitive[String] for string type' do
243+
value[:type] = :string
244+
sensitive_data = Puppet::Pops::Types::PSensitiveType::Sensitive.new('secret_password')
245+
expect(value[:data] = sensitive_data)
246+
end
247+
248+
it 'supports Sensitive[String] for expand type' do
249+
value[:type] = :expand
250+
sensitive_data = Puppet::Pops::Types::PSensitiveType::Sensitive.new('secret_path')
251+
expect(value[:data] = sensitive_data)
252+
end
253+
254+
it 'supports Sensitive[String] in array type' do
255+
value[:type] = :array
256+
sensitive_data = ['public_value', Puppet::Pops::Types::PSensitiveType::Sensitive.new('secret_value'), 'another_public']
257+
expect(value[:data] = sensitive_data)
258+
end
259+
260+
it 'supports Sensitive[String] for binary type' do
261+
value[:type] = :binary
262+
sensitive_data = Puppet::Pops::Types::PSensitiveType::Sensitive.new('CAFEBEEF')
263+
expect(value[:data] = sensitive_data)
264+
end
265+
end
240266
end
241267
end

0 commit comments

Comments
 (0)