Skip to content

Commit 7cd78f7

Browse files
author
Andrew
committed
Blocked non-list operations on adapters
1 parent 93554f6 commit 7cd78f7

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

dsc/src/resource_command.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::args::OutputFormat;
55
use crate::util::{EXIT_DSC_ERROR, EXIT_INVALID_ARGS, EXIT_JSON_ERROR, add_type_name_to_json, write_output};
66
use dsc_lib::configure::config_doc::{Configuration, ExecutionKind};
77
use dsc_lib::configure::add_resource_export_results_to_configuration;
8-
use dsc_lib::dscresources::invoke_result::{GetResult, ResourceGetResponse};
8+
use dsc_lib::dscresources::{resource_manifest::Kind, invoke_result::{GetResult, ResourceGetResponse}};
99
use dsc_lib::dscerror::DscError;
1010
use tracing::{error, debug};
1111

@@ -22,6 +22,11 @@ pub fn get(dsc: &DscManager, resource_type: &str, mut input: String, format: &Op
2222
};
2323

2424
debug!("resource.type_name - {} implemented_as - {:?}", resource.type_name, resource.implemented_as);
25+
if resource.kind == Kind::Adapter {
26+
error!("Can not perform this operation on the adapter {} itself", resource.type_name);
27+
exit(EXIT_DSC_ERROR);
28+
}
29+
2530
if let Some(requires) = &resource.require_adapter {
2631
input = add_type_name_to_json(input, resource.type_name.clone());
2732
if let Some(pr) = get_resource(dsc, requires) {
@@ -59,6 +64,11 @@ pub fn get_all(dsc: &DscManager, resource_type: &str, format: &Option<OutputForm
5964
};
6065

6166
debug!("resource.type_name - {} implemented_as - {:?}", resource.type_name, resource.implemented_as);
67+
if resource.kind == Kind::Adapter {
68+
error!("Can not perform this operation on the adapter {} itself", resource.type_name);
69+
exit(EXIT_DSC_ERROR);
70+
}
71+
6272
if let Some(requires) = &resource.require_adapter {
6373
input = add_type_name_to_json(input, resource.type_name.clone());
6474
if let Some(pr) = get_resource(dsc, requires) {
@@ -106,6 +116,10 @@ pub fn set(dsc: &DscManager, resource_type: &str, mut input: String, format: &Op
106116
};
107117

108118
debug!("resource.type_name - {} implemented_as - {:?}", resource.type_name, resource.implemented_as);
119+
if resource.kind == Kind::Adapter {
120+
error!("Can not perform this operation on the adapter {} itself", resource.type_name);
121+
exit(EXIT_DSC_ERROR);
122+
}
109123

110124
if let Some(requires) = &resource.require_adapter {
111125
input = add_type_name_to_json(input, resource.type_name.clone());
@@ -143,6 +157,10 @@ pub fn test(dsc: &DscManager, resource_type: &str, mut input: String, format: &O
143157
};
144158

145159
debug!("resource.type_name - {} implemented_as - {:?}", resource.type_name, resource.implemented_as);
160+
if resource.kind == Kind::Adapter {
161+
error!("Can not perform this operation on the adapter {} itself", resource.type_name);
162+
exit(EXIT_DSC_ERROR);
163+
}
146164

147165
if let Some(requires) = &resource.require_adapter {
148166
input = add_type_name_to_json(input, resource.type_name.clone());
@@ -180,6 +198,10 @@ pub fn delete(dsc: &DscManager, resource_type: &str, mut input: String) {
180198
};
181199

182200
debug!("resource.type_name - {} implemented_as - {:?}", resource.type_name, resource.implemented_as);
201+
if resource.kind == Kind::Adapter {
202+
error!("Can not perform this operation on the adapter {} itself", resource.type_name);
203+
exit(EXIT_DSC_ERROR);
204+
}
183205

184206
if let Some(requires) = &resource.require_adapter {
185207
input = add_type_name_to_json(input, resource.type_name.clone());
@@ -205,6 +227,11 @@ pub fn schema(dsc: &DscManager, resource_type: &str, format: &Option<OutputForma
205227
error!("{}", DscError::ResourceNotFound(resource_type.to_string()).to_string());
206228
return
207229
};
230+
if resource.kind == Kind::Adapter {
231+
error!("Can not perform this operation on the adapter {} itself", resource.type_name);
232+
exit(EXIT_DSC_ERROR);
233+
}
234+
208235
match resource.schema() {
209236
Ok(json) => {
210237
// verify is json
@@ -231,6 +258,11 @@ pub fn export(dsc: &mut DscManager, resource_type: &str, format: &Option<OutputF
231258
return
232259
};
233260

261+
if dsc_resource.kind == Kind::Adapter {
262+
error!("Can not perform this operation on the adapter {} itself", dsc_resource.type_name);
263+
exit(EXIT_DSC_ERROR);
264+
}
265+
234266
let mut adapter_resource: Option<&DscResource> = None;
235267
if let Some(requires) = &dsc_resource.require_adapter {
236268
input = add_type_name_to_json(input, dsc_resource.type_name.clone());

dsc/tests/dsc_args.tests.ps1

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,4 +272,40 @@ resources:
272272
$stderr = dsc config get -d $configFile 2>&1
273273
$stderr | Should -Match '.*?--path.*?'
274274
}
275+
276+
It 'Get operation on the adapter itself should fail' -Tag 'z1' {
277+
dsc resource get -r Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt
278+
$LASTEXITCODE | Should -Be 2
279+
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Can not perform this operation on the adapter'
280+
}
281+
282+
It 'Get-all operation on the adapter itself should fail' -Tag 'z1' {
283+
dsc resource get --all -r Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt
284+
$LASTEXITCODE | Should -Be 2
285+
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Can not perform this operation on the adapter'
286+
}
287+
288+
It 'Set operation on the adapter itself should fail' -Tag 'z1' {
289+
'abc' | dsc resource set -r Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt
290+
$LASTEXITCODE | Should -Be 2
291+
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Can not perform this operation on the adapter'
292+
}
293+
294+
It 'Test operation on the adapter itself should fail' -Tag 'z1' {
295+
dsc resource test -r Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt
296+
$LASTEXITCODE | Should -Be 2
297+
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Can not perform this operation on the adapter'
298+
}
299+
300+
It 'Export operation on the adapter itself should fail' -Tag 'z1' {
301+
dsc resource export -r Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt
302+
$LASTEXITCODE | Should -Be 2
303+
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Can not perform this operation on the adapter'
304+
}
305+
306+
It 'Delete operation on the adapter itself should fail' -Tag 'z1' {
307+
dsc resource delete -r Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt
308+
$LASTEXITCODE | Should -Be 2
309+
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Can not perform this operation on the adapter'
310+
}
275311
}

0 commit comments

Comments
 (0)