Skip to content

Commit ec33ed2

Browse files
authored
Merge pull request #1229 from Gijsreyn/gh-1228/main/fix-defaultvalue-constraint
Add parameter validation checks for length, allowed values, and number limits on defaultValue
2 parents 5d8d877 + b5defea commit ec33ed2

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

dsc/tests/dsc_parameters.tests.ps1

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,4 +577,128 @@ Describe 'Parameters tests' {
577577
$LASTEXITCODE | Should -Be 4
578578
$testError | Should -Match 'Circular dependency or unresolvable parameter references detected in parameters'
579579
}
580+
581+
It 'Default value violates <constraint> constraint' -TestCases @(
582+
@{ constraint = 'minLength'; type = 'string'; value = 'ab'; min = 3; max = 20; errorMatch = 'minimum length' }
583+
@{ constraint = 'maxLength'; type = 'string'; value = 'verylongusernamethatexceedslimit'; min = 3; max = 20; errorMatch = 'maximum length' }
584+
@{ constraint = 'minValue'; type = 'int'; value = 0; min = 1; max = 65535; errorMatch = 'minimum value' }
585+
@{ constraint = 'maxValue'; type = 'int'; value = 99999; min = 1; max = 65535; errorMatch = 'maximum value' }
586+
) {
587+
param($constraint, $type, $value, $min, $max, $errorMatch)
588+
589+
if ($type -eq 'string') {
590+
$value = "'$value'"
591+
}
592+
593+
$minConstraint = if ($type -eq 'string') { "minLength: $min" } else { "minValue: $min" }
594+
$maxConstraint = if ($type -eq 'string') { "maxLength: $max" } else { "maxValue: $max" }
595+
596+
$config_yaml = @"
597+
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
598+
parameters:
599+
param1:
600+
type: $type
601+
$minConstraint
602+
$maxConstraint
603+
defaultValue: $value
604+
resources:
605+
- name: Echo
606+
type: Microsoft.DSC.Debug/Echo
607+
properties:
608+
output: '[parameters(''param1'')]'
609+
"@
610+
611+
$testError = & {$config_yaml | dsc config get -f - 2>&1}
612+
$LASTEXITCODE | Should -Be 4
613+
$testError | Should -Match $errorMatch
614+
}
615+
616+
It 'Default value violates allowedValues constraint for <type>' -TestCases @(
617+
@{ type = 'string'; value = 'staging'; allowed = @('dev', 'test', 'prod') }
618+
@{ type = 'int'; value = 7; allowed = @(1, 5, 10) }
619+
) {
620+
param($type, $value, $allowed)
621+
622+
if ($type -eq 'string') {
623+
$value = "'$value'"
624+
}
625+
626+
$config_yaml = @"
627+
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
628+
parameters:
629+
param1:
630+
type: $type
631+
allowedValues: $($allowed | ConvertTo-Json -Compress)
632+
defaultValue: $value
633+
resources:
634+
- name: Echo
635+
type: Microsoft.DSC.Debug/Echo
636+
properties:
637+
output: '[parameters(''param1'')]'
638+
"@
639+
640+
$testError = & {$config_yaml | dsc config get -f - 2>&1}
641+
$LASTEXITCODE | Should -Be 4
642+
$testError | Should -Match 'allowed values'
643+
}
644+
645+
It 'Default values pass constraint validation for <type>' -TestCases @(
646+
@{ type = 'string'; value = 'admin'; min = 3; max = 20 }
647+
@{ type = 'string'; value = 'abc'; min = 3; max = 20 }
648+
@{ type = 'string'; value = 'abcdefghijklmnopqrst'; min = 3; max = 20 }
649+
@{ type = 'int'; value = 8080; min = 1; max = 65535 }
650+
@{ type = 'int'; value = 1; min = 1; max = 65535 }
651+
@{ type = 'int'; value = 65535; min = 1; max = 65535 }
652+
) {
653+
param($type, $value, $min, $max)
654+
655+
$minConstraint = if ($type -eq 'string') { "minLength: $min" } else { "minValue: $min" }
656+
$maxConstraint = if ($type -eq 'string') { "maxLength: $max" } else { "maxValue: $max" }
657+
658+
$config_yaml = @"
659+
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
660+
parameters:
661+
param1:
662+
type: $type
663+
$minConstraint
664+
$maxConstraint
665+
defaultValue: $value
666+
resources:
667+
- name: Echo
668+
type: Microsoft.DSC.Debug/Echo
669+
properties:
670+
output: '[parameters(''param1'')]'
671+
"@
672+
673+
$out = $config_yaml | dsc config get -f - | ConvertFrom-Json
674+
$LASTEXITCODE | Should -Be 0
675+
$out.results[0].result.actualState.output | Should -BeExactly $value
676+
}
677+
678+
It 'Default values with allowedValues pass validation for <type>' -TestCases @(
679+
@{ type = 'string'; value = 'dev'; allowed = @('dev', 'test', 'prod') }
680+
@{ type = 'string'; value = 'prod'; allowed = @('dev', 'test', 'prod') }
681+
@{ type = 'int'; value = 5; allowed = @(1, 5, 10) }
682+
@{ type = 'int'; value = 10; allowed = @(1, 5, 10) }
683+
) {
684+
param($type, $value, $allowed)
685+
686+
$config_yaml = @"
687+
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
688+
parameters:
689+
param1:
690+
type: $type
691+
allowedValues: $($allowed | ConvertTo-Json -Compress)
692+
defaultValue: $value
693+
resources:
694+
- name: Echo
695+
type: Microsoft.DSC.Debug/Echo
696+
properties:
697+
output: '[parameters(''param1'')]'
698+
"@
699+
700+
$out = $config_yaml | dsc config get -f - | ConvertFrom-Json
701+
$LASTEXITCODE | Should -Be 0
702+
$out.results[0].result.actualState.output | Should -BeExactly $value
703+
}
580704
}

lib/dsc-lib/src/configure/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,9 @@ impl Configurator {
895895
};
896896

897897
if let Ok(value) = value_result {
898+
check_length(name, &value, parameter)?;
899+
check_allowed_values(name, &value, parameter)?;
900+
check_number_limits(name, &value, parameter)?;
898901
validate_parameter_type(name, &value, &parameter.parameter_type)?;
899902
self.context.parameters.insert(name.to_string(), (value, parameter.parameter_type.clone()));
900903
resolved_in_this_pass.push(name.clone());

0 commit comments

Comments
 (0)