Skip to content

Commit aa67d45

Browse files
committed
fix tests and clippy
1 parent 91c6c1c commit aa67d45

File tree

3 files changed

+88
-53
lines changed

3 files changed

+88
-53
lines changed

dsc/tests/dsc_parameters.tests.ps1

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -274,26 +274,61 @@ Describe 'Parameters tests' {
274274
$out.results[0].result.inDesiredState | Should -BeTrue
275275
}
276276

277-
It 'secure types can be passed as objects to resources but redacted in output' {
278-
$out = dsc -l trace config -f $PSScriptRoot/../examples/secure_parameters.parameters.yaml get -f $PSScriptRoot/../examples/secure_parameters.dsc.yaml 2> $TestDrive/error.log | ConvertFrom-Json
277+
It 'secure types can be passed as objects to resources but redacted in output: <operation> <property>' -TestCases @(
278+
@{ operation = 'get'; property = 'actualState' }
279+
@{ operation = 'set'; property = 'beforeState' }
280+
@{ operation = 'set'; property = 'afterState' }
281+
@{ operation = 'test'; property = 'desiredState' }
282+
@{ operation = 'test'; property = 'actualState' }
283+
@{ operation = 'export'; property = $null }
284+
) {
285+
param($operation, $property)
286+
287+
$out = dsc -l trace config -f $PSScriptRoot/../examples/secure_parameters.parameters.yaml $operation -f $PSScriptRoot/../examples/secure_parameters.dsc.yaml 2> $TestDrive/error.log | ConvertFrom-Json
279288
$LASTEXITCODE | Should -Be 0
280-
$out.results.Count | Should -Be 4
281-
$out.results[0].result.actualState.output | Should -BeExactly '<secureValue>'
282-
$out.results[1].result.actualState.output | Should -BeExactly '<secureValue>'
283-
$out.results[2].result.actualState.output[0] | Should -BeExactly '<secureValue>'
284-
$out.results[2].result.actualState.output[1] | Should -BeExactly '<secureValue>'
285-
$out.results[3].result.actualState.output | Should -BeExactly '<secureValue>'
289+
if ($operation -eq 'export') {
290+
$out.resources.Count | Should -Be 4
291+
$out.resources[0].properties.output | Should -BeExactly '<secureValue>'
292+
$out.resources[1].properties.output | Should -BeExactly '<secureValue>'
293+
$out.resources[2].properties.output[0] | Should -BeExactly '<secureValue>'
294+
$out.resources[2].properties.output[1] | Should -BeExactly '<secureValue>'
295+
$out.resources[3].properties.output | Should -BeExactly '<secureValue>'
296+
} else {
297+
$out.results.Count | Should -Be 4 -Because ($out | ConvertTo-Json -Dep 10 | Out-String)
298+
$out.results[0].result.$property.output | Should -BeExactly '<secureValue>' -Because ($out | ConvertTo-Json -Dep 10 | Out-String)
299+
$out.results[1].result.$property.output | Should -BeExactly '<secureValue>'
300+
$out.results[2].result.$property.output[0] | Should -BeExactly '<secureValue>'
301+
$out.results[2].result.$property.output[1] | Should -BeExactly '<secureValue>'
302+
$out.results[3].result.$property.output | Should -BeExactly '<secureValue>'
303+
}
286304
}
287305

288-
It 'secure types can be passed as objects to resources' {
289-
$out = dsc config -f $PSScriptRoot/../examples/secure_parameters_shown.parameters.yaml get -f $PSScriptRoot/../examples/secure_parameters.dsc.yaml | ConvertFrom-Json
306+
It 'secure types can be passed as objects to resources: <operation> <property>' -TestCases @(
307+
# `set` beforeState is redacted in output, `test` desiredState is redacted in output so those test cases are not included here
308+
@{ operation = 'get'; property = 'actualState' }
309+
@{ operation = 'set'; property = 'afterState' }
310+
@{ operation = 'test'; property = 'actualState' }
311+
@{ operation = 'export'; property = $null }
312+
) {
313+
param($operation, $property)
314+
315+
$out = dsc config -f $PSScriptRoot/../examples/secure_parameters_shown.parameters.yaml $operation -f $PSScriptRoot/../examples/secure_parameters.dsc.yaml | ConvertFrom-Json
290316
$LASTEXITCODE | Should -Be 0
291-
$out.results.Count | Should -Be 4
292-
$out.results[0].result.actualState.output.secureString | Should -BeExactly 'mySecret'
293-
$out.results[1].result.actualState.output.secureString | Should -BeExactly 'mySecretProperty'
294-
$out.results[2].result.actualState.output[0].secureString | Should -BeExactly 'item1'
295-
$out.results[2].result.actualState.output[1].secureString | Should -BeExactly 'item2'
296-
$out.results[3].result.actualState.output.secureObject.secureString | Should -BeExactly 'item2'
317+
if ($operation -eq 'export') {
318+
$out.resources.Count | Should -Be 4 -Because ($out | ConvertTo-Json -Dep 10 | Out-String)
319+
$out.resources[0].properties.output.secureString | Should -BeExactly 'mySecret'
320+
$out.resources[1].properties.output.secureString | Should -BeExactly 'mySecretProperty'
321+
$out.resources[2].properties.output[0].secureString | Should -BeExactly 'item1'
322+
$out.resources[2].properties.output[1].secureString | Should -BeExactly 'item2'
323+
$out.resources[3].properties.output.secureObject.secureString | Should -BeExactly 'item2'
324+
} else {
325+
$out.results.Count | Should -Be 4
326+
$out.results[0].result.$property.output.secureString | Should -BeExactly 'mySecret' -Because ($out | ConvertTo-Json -Dep 10 | Out-String)
327+
$out.results[1].result.$property.output.secureString | Should -BeExactly 'mySecretProperty'
328+
$out.results[2].result.$property.output[0].secureString | Should -BeExactly 'item1'
329+
$out.results[2].result.$property.output[1].secureString | Should -BeExactly 'item2'
330+
$out.results[3].result.$property.output.secureObject.secureString | Should -BeExactly 'item2'
331+
}
297332
}
298333

299334
It 'parameter types are validated for <type>' -TestCases @(

dsc_lib/src/dscresources/command_resource.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,9 @@ pub fn invoke_set(resource: &ResourceManifest, cwd: &str, desired: &str, skip_te
120120
};
121121

122122
if in_desired_state && execution_type == &ExecutionKind::Actual {
123+
let before_state = redact(&serde_json::from_str(desired)?);
123124
return Ok(SetResult::Resource(ResourceSetResponse{
124-
before_state: serde_json::from_str(desired)?,
125+
before_state,
125126
after_state: actual_state,
126127
changed_properties: None,
127128
}));
@@ -152,7 +153,7 @@ pub fn invoke_set(resource: &ResourceManifest, cwd: &str, desired: &str, skip_te
152153
else {
153154
return Err(DscError::Command(resource.resource_type.clone(), exit_code, stderr));
154155
};
155-
let pre_state = if pre_state_value.is_object() {
156+
let mut pre_state = if pre_state_value.is_object() {
156157
let mut pre_state_map: Map<String, Value> = serde_json::from_value(pre_state_value)?;
157158

158159
// if the resource is an adapter, then the `get` will return a `result`, but a full `set` expects the before state to be `resources`
@@ -200,6 +201,7 @@ pub fn invoke_set(resource: &ResourceManifest, cwd: &str, desired: &str, skip_te
200201

201202
// for changed_properties, we compare post state to pre state
202203
let diff_properties = get_diff( &actual_value, &pre_state);
204+
pre_state = redact(&pre_state);
203205
Ok(SetResult::Resource(ResourceSetResponse{
204206
before_state: pre_state,
205207
after_state: actual_value,
@@ -241,6 +243,7 @@ pub fn invoke_set(resource: &ResourceManifest, cwd: &str, desired: &str, skip_te
241243
}
242244
};
243245
let diff_properties = get_diff( &actual_state, &pre_state);
246+
pre_state = redact(&pre_state);
244247
Ok(SetResult::Resource(ResourceSetResponse {
245248
before_state: pre_state,
246249
after_state: actual_state,

dscecho/src/main.rs

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,45 +17,42 @@ const SECURE_VALUE_REDACTED: &str = "<secureValue>";
1717

1818
fn main() {
1919
let args = Args::parse();
20-
match args.input {
21-
Some(input) => {
22-
let mut echo = match serde_json::from_str::<Echo>(&input) {
23-
Ok(echo) => echo,
24-
Err(err) => {
25-
eprintln!("{}: {err}", t!("main.invalidJson"));
26-
std::process::exit(1);
27-
}
28-
};
29-
if echo.show_secrets != Some(true) {
30-
match echo.output {
31-
Output::SecureString(_) | Output::SecureObject(_) => {
32-
echo.output = Output::String(SECURE_VALUE_REDACTED.to_string());
33-
},
34-
Output::Array(ref mut arr) => {
35-
for item in arr.iter_mut() {
36-
if is_secure_value(item) {
37-
*item = Value::String(SECURE_VALUE_REDACTED.to_string());
38-
} else {
39-
*item = redact(item);
40-
}
20+
if let Some(input) = args.input {
21+
let mut echo = match serde_json::from_str::<Echo>(&input) {
22+
Ok(echo) => echo,
23+
Err(err) => {
24+
eprintln!("{}: {err}", t!("main.invalidJson"));
25+
std::process::exit(1);
26+
}
27+
};
28+
if echo.show_secrets != Some(true) {
29+
match echo.output {
30+
Output::SecureString(_) | Output::SecureObject(_) => {
31+
echo.output = Output::String(SECURE_VALUE_REDACTED.to_string());
32+
},
33+
Output::Array(ref mut arr) => {
34+
for item in arr.iter_mut() {
35+
if is_secure_value(item) {
36+
*item = Value::String(SECURE_VALUE_REDACTED.to_string());
37+
} else {
38+
*item = redact(item);
4139
}
42-
},
43-
Output::Object(ref mut obj) => {
44-
*obj = redact(obj);
45-
},
46-
_ => {}
47-
}
40+
}
41+
},
42+
Output::Object(ref mut obj) => {
43+
*obj = redact(obj);
44+
},
45+
_ => {}
4846
}
49-
let json = serde_json::to_string(&echo).unwrap();
50-
println!("{json}");
51-
return;
52-
},
53-
None => {
54-
let schema = schema_for!(Echo);
55-
let json = serde_json::to_string_pretty(&schema).unwrap();
56-
println!("{json}");
5747
}
48+
let json = serde_json::to_string(&echo).unwrap();
49+
println!("{json}");
50+
return;
5851
}
52+
53+
let schema = schema_for!(Echo);
54+
let json = serde_json::to_string_pretty(&schema).unwrap();
55+
println!("{json}");
5956
}
6057

6158
fn is_secure_value(value: &Value) -> bool {

0 commit comments

Comments
 (0)