Skip to content

Commit 43cee87

Browse files
Merge pull request #33 from step-security/fix/update-validation
Fix validation during plan stage for various resources
2 parents 6e817d2 + 909d6ac commit 43cee87

File tree

2 files changed

+36
-29
lines changed

2 files changed

+36
-29
lines changed

internal/provider/resource_github_supression_rule.go

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -152,61 +152,66 @@ func (r *githubSupressionRuleResource) ValidateConfig(ctx context.Context, req r
152152

153153
switch rule.Type.ValueString() {
154154
case "source_code_overwritten":
155-
if rule.File.IsNull() || rule.File.IsUnknown() || rule.FilePath.IsNull() || rule.FilePath.IsUnknown() {
155+
if (!rule.File.IsUnknown() && rule.File.IsNull()) || (!rule.FilePath.IsUnknown() && rule.FilePath.IsNull()) {
156156
resp.Diagnostics.AddError(
157157
"File is required",
158158
"File is required when type is source_code_overwritten",
159159
)
160160
}
161-
if !rule.Process.IsNull() {
161+
if !rule.Process.IsUnknown() && !rule.Process.IsNull() {
162162
resp.Diagnostics.AddError(
163163
"Process is not allowed",
164164
"Process is not allowed when type is source_code_overwritten",
165165
)
166166
}
167-
if !rule.Destination.IsNull() {
167+
if !rule.Destination.IsUnknown() && !rule.Destination.IsNull() {
168168
resp.Diagnostics.AddError(
169169
"Destination is not allowed",
170170
"Destination is not allowed when type is source_code_overwritten",
171171
)
172172
}
173173
case "anomalous_outbound_network_call":
174-
if !rule.File.IsNull() || !rule.FilePath.IsNull() {
174+
if (!rule.File.IsUnknown() && !rule.File.IsNull()) || (!rule.FilePath.IsUnknown() && !rule.FilePath.IsNull()) {
175175
resp.Diagnostics.AddError(
176176
"File, File Path parameters are not allowed",
177177
"File, File Path parameters are not allowed when type is anomalous_outbound_network_call",
178178
)
179179
}
180-
if rule.Process.IsNull() || rule.Process.IsUnknown() {
180+
if !rule.Process.IsUnknown() && rule.Process.IsNull() {
181181
resp.Diagnostics.AddError(
182182
"Process is required",
183183
"Process is required when type is anomalous_outbound_network_call",
184184
)
185185
}
186-
if rule.Destination.IsNull() || rule.Destination.IsUnknown() {
186+
if !rule.Destination.IsUnknown() && rule.Destination.IsNull() {
187187
resp.Diagnostics.AddError(
188188
"Destination is required",
189189
"Destination is required when type is anomalous_outbound_network_call",
190190
)
191191
}
192-
var destination destinationModel
193-
diags := rule.Destination.As(ctx, &destination, basetypes.ObjectAsOptions{})
194-
resp.Diagnostics.Append(diags...)
195-
if resp.Diagnostics.HasError() {
196-
return
197-
}
198-
isIpEmpty := destination.IP.IsNull() || destination.IP.IsUnknown()
199-
isDomainEmpty := destination.Domain.IsNull() || destination.Domain.IsUnknown()
200-
if isIpEmpty && isDomainEmpty {
201-
resp.Diagnostics.AddError(
202-
"Destination is required",
203-
"Destination is required when type is anomalous_outbound_network_call. please provide either ip or domain.",
204-
)
205-
} else if !isIpEmpty && !isDomainEmpty {
206-
resp.Diagnostics.AddError(
207-
"Cannot provide both ip and domain in destination",
208-
"Destination can only have either ip or domain",
209-
)
192+
if !rule.Destination.IsUnknown() {
193+
var destination destinationModel
194+
diags := rule.Destination.As(ctx, &destination, basetypes.ObjectAsOptions{})
195+
resp.Diagnostics.Append(diags...)
196+
if resp.Diagnostics.HasError() {
197+
return
198+
}
199+
// Skip validation if either field is unknown (from variables during plan)
200+
if !destination.IP.IsUnknown() && !destination.Domain.IsUnknown() {
201+
isIpEmpty := destination.IP.IsNull()
202+
isDomainEmpty := destination.Domain.IsNull()
203+
if isIpEmpty && isDomainEmpty {
204+
resp.Diagnostics.AddError(
205+
"Destination is required",
206+
"Destination is required when type is anomalous_outbound_network_call. please provide either ip or domain.",
207+
)
208+
} else if !isIpEmpty && !isDomainEmpty {
209+
resp.Diagnostics.AddError(
210+
"Cannot provide both ip and domain in destination",
211+
"Destination can only have either ip or domain",
212+
)
213+
}
214+
}
210215
}
211216
}
212217
}

internal/provider/resource_policy_driven_pr.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ func (r *policyDrivenPRResource) ValidateConfig(ctx context.Context, req resourc
414414
return
415415
}
416416

417-
if config.SelectedRepos.IsNull() || len(config.SelectedRepos.Elements()) == 0 {
417+
if !config.SelectedRepos.IsUnknown() && (config.SelectedRepos.IsNull() || len(config.SelectedRepos.Elements()) == 0) {
418418
resp.Diagnostics.AddError(
419419
"Selected Repos is required",
420420
"At least one repo is required in selected_repos",
@@ -424,14 +424,16 @@ func (r *policyDrivenPRResource) ValidateConfig(ctx context.Context, req resourc
424424

425425
// Get selected repos
426426
var selectedRepos []string
427-
elements := config.SelectedRepos.Elements()
428-
for _, elem := range elements {
429-
selectedRepos = append(selectedRepos, elem.(types.String).ValueString())
427+
if !config.SelectedRepos.IsUnknown() {
428+
elements := config.SelectedRepos.Elements()
429+
for _, elem := range elements {
430+
selectedRepos = append(selectedRepos, elem.(types.String).ValueString())
431+
}
430432
}
431433

432434
// Validate excluded_repos only makes sense with wildcard
433435
hasWildcard := len(selectedRepos) == 1 && selectedRepos[0] == "*"
434-
if !config.ExcludedRepos.IsNull() && len(config.ExcludedRepos.Elements()) > 0 {
436+
if !config.ExcludedRepos.IsUnknown() && !config.ExcludedRepos.IsNull() && len(config.ExcludedRepos.Elements()) > 0 {
435437
if !hasWildcard {
436438
resp.Diagnostics.AddError(
437439
"Invalid Configuration",

0 commit comments

Comments
 (0)