Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for attribute value being null or unknown before using it for validation #1123

Merged
merged 4 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 12.4.1 (November 11, 2024). Tested on Artifactory 7.98.8 with Terraform 1.9.8 and OpenTofu 1.8.5

BUG FIXES:

* resource/artifactory_build_webhook, resource/artifactory_release_bundle_webhook, resource/artifactory_release_bundle_v2_webhook, resource/artifactory_artifact_webhook, resource/artifactory_artifact_property_webhook, resource/artifactory_docker_webhook, resource/artifactory_build_custom_webhook, resource/artifactory_release_bundle_custom_webhook, resource/artifactory_release_bundle_v2_custom_webhook, resource/artifactory_artifact_custom_webhook, resource/artifactory_artifact_property_custom_webhook, resource/artifactory_docker_custom_webhook, resource/artifactory_ldap_group_setting_v2, resource/artifactory_repository_layout, resource/artifactory_release_bundle_v2, resource/artifactory_vault_configuration, resource/artifactory_user, resource/artifactory_managed_user, resource/artifactory_unmanaged_user: Fix attribute validation not working for unknown value (e.g. when resource is used in a module). Issue: [#1120](https://github.com/jfrog/terraform-provider-artifactory/issues/1120) PR: [#1123](https://github.com/jfrog/terraform-provider-artifactory/pull/1123)

## 12.4.0 (November 4, 2024). Tested on Artifactory 7.98.7 with Terraform 1.9.8 and OpenTofu 1.8.5

IMPROVEMENTS:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,16 @@ func (r *ArtifactoryLdapGroupSettingResource) ValidateConfig(ctx context.Context
return
}

if data.Strategy.IsNull() || data.Strategy.IsUnknown() {
return
}

if data.SubTree.IsNull() || data.SubTree.IsUnknown() {
return
}

// Validate strategy and sub_tree
if !data.Strategy.IsNull() && strings.ToUpper(data.Strategy.ValueString()) == "HIERARCHICAL" && data.SubTree.ValueBool() {
if strings.ToUpper(data.Strategy.ValueString()) == "HIERARCHICAL" && data.SubTree.ValueBool() {
resp.Diagnostics.AddAttributeError(
path.Root("sub_tree"),
"Incorrect Attribute Configuration",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,15 @@ func (r RepositoryLayoutResource) ValidateConfig(ctx context.Context, req resour
return
}

if data.DistinctiveDescriptorPathPattern.ValueBool() && len(data.DescriptorPathPattern.ValueString()) == 0 {
if data.DistinctiveDescriptorPathPattern.IsNull() || data.DistinctiveDescriptorPathPattern.IsUnknown() {
return
}

if data.DescriptorPathPattern.IsUnknown() {
return
}

if data.DistinctiveDescriptorPathPattern.ValueBool() && (data.DescriptorPathPattern.IsNull() || len(data.DescriptorPathPattern.ValueString()) == 0) {
resp.Diagnostics.AddAttributeError(
path.Root("descriptor_path_pattern"),
"Invalid attribute configuration",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,14 @@ func (r ReleaseBundleV2Resource) ValidateConfig(ctx context.Context, req resourc
return
}

if data.SourceType.IsNull() || data.SourceType.IsUnknown() {
return
}

if data.Source.IsNull() || data.Source.IsUnknown() {
return
}

sourceType := data.SourceType.ValueString()
sourceAttrs := data.Source.Attributes()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,10 @@ func (r VaultConfigurationResource) ValidateConfig(ctx context.Context, req reso
return
}

if data.Config.IsNull() || data.Config.IsUnknown() {
return
}

configAttrs := data.Config.Attributes()
authAttrs := configAttrs["auth"].(types.Object).Attributes()
authType := authAttrs["type"].(types.String)
Expand Down
4 changes: 4 additions & 0 deletions pkg/artifactory/resource/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,10 @@ func (r *ArtifactoryBaseUserResource) validatePasswordByPolicy(plan ArtifactoryU
return nil
}

if plan.PasswordPolicy.IsUnknown() {
return nil
}

// Default password policy should match Access default configuration:
// https://jfrog.com/help/r/jfrog-installation-setup-documentation/supported-access-configurations
minLength := int64(8)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ func (m *BuildCustomWebhookResourceModel) fromAPIModel(ctx context.Context, apiM
criteriaAPIModel := apiModel.EventFilter.Criteria.(map[string]interface{})

baseCriteriaAttrs, d := m.CustomWebhookResourceModel.fromBaseCriteriaAPIModel(ctx, criteriaAPIModel)
if d.HasError() {
diags.Append(d...)
}

criteriaSet, d := fromBuildAPIModel(ctx, criteriaAPIModel, baseCriteriaAttrs)
if d.HasError() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ func (m *ReleaseBundleCustomWebhookResourceModel) fromAPIModel(ctx context.Conte
criteriaAPIModel := apiModel.EventFilter.Criteria.(map[string]interface{})

baseCriteriaAttrs, d := m.CustomWebhookResourceModel.fromBaseCriteriaAPIModel(ctx, criteriaAPIModel)
if d.HasError() {
diags.Append(d...)
}

criteriaSet, d := fromReleaseBundleCriteriaAPIModel(ctx, criteriaAPIModel, baseCriteriaAttrs)
if d.HasError() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ func (m *ReleaseBundleV2CustomWebhookResourceModel) fromAPIModel(ctx context.Con
criteriaAPIModel := apiModel.EventFilter.Criteria.(map[string]interface{})

baseCriteriaAttrs, d := m.CustomWebhookResourceModel.fromBaseCriteriaAPIModel(ctx, criteriaAPIModel)
if d.HasError() {
diags.Append(d...)
}

criteriaSet, d := fromReleaseBundleV2APIModel(ctx, criteriaAPIModel, baseCriteriaAttrs)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ func (m *ReleaseBundleV2PromotionCustomWebhookResourceModel) fromAPIModel(ctx co
criteriaAPIModel := apiModel.EventFilter.Criteria.(map[string]interface{})

baseCriteriaAttrs, d := m.CustomWebhookResourceModel.fromBaseCriteriaAPIModel(ctx, criteriaAPIModel)
if d.HasError() {
diags.Append(d...)
}

criteriaSet, d := fromReleaseBundleV2PromotionAPIModel(ctx, criteriaAPIModel, baseCriteriaAttrs)
if d.HasError() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,23 @@ func (r RepoCustomWebhookResource) ValidateConfig(ctx context.Context, req resou
return
}

if data.Criteria.IsNull() || data.Criteria.IsUnknown() {
return
}

criteriaObj := data.Criteria.Elements()[0].(types.Object)
criteriaAttrs := criteriaObj.Attributes()

anyLocal := criteriaAttrs["any_local"].(types.Bool).ValueBool()
anyRemote := criteriaAttrs["any_remote"].(types.Bool).ValueBool()
anyFederated := criteriaAttrs["any_federated"].(types.Bool).ValueBool()
anyLocal := criteriaAttrs["any_local"].(types.Bool)
anyRemote := criteriaAttrs["any_remote"].(types.Bool)
anyFederated := criteriaAttrs["any_federated"].(types.Bool)
repoKeys := criteriaAttrs["repo_keys"].(types.Set)

if anyLocal.IsUnknown() || anyRemote.IsUnknown() || anyFederated.IsUnknown() || repoKeys.IsUnknown() {
return
}

if (!anyLocal && !anyRemote && !anyFederated) && len(criteriaAttrs["repo_keys"].(types.Set).Elements()) == 0 {
if (!anyLocal.ValueBool() && !anyRemote.ValueBool() && !anyFederated.ValueBool()) && len(repoKeys.Elements()) == 0 {
resp.Diagnostics.AddAttributeError(
path.Root("criteria").AtSetValue(criteriaObj).AtName("repo_keys"),
"Invalid Attribute Configuration",
Expand Down Expand Up @@ -208,6 +217,9 @@ func (m RepoCustomWebhookResourceModel) toAPIModel(ctx context.Context, domain s
}

criteriaAPIModel, d := toRepoCriteriaAPIModel(ctx, baseCriteria, criteriaAttrs)
if d.HasError() {
diags.Append(d...)
}

d = m.CustomWebhookResourceModel.toAPIModel(ctx, domain, criteriaAPIModel, apiModel)
if d.HasError() {
Expand All @@ -223,6 +235,9 @@ func (m *RepoCustomWebhookResourceModel) fromAPIModel(ctx context.Context, apiMo
criteriaAPIModel := apiModel.EventFilter.Criteria.(map[string]interface{})

baseCriteriaAttrs, d := m.CustomWebhookResourceModel.fromBaseCriteriaAPIModel(ctx, criteriaAPIModel)
if d.HasError() {
diags.Append(d...)
}

criteriaSet, d := fromRepoCriteriaAPIMode(ctx, criteriaAPIModel, baseCriteriaAttrs)
if d.HasError() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,22 @@ func (r *BuildWebhookResource) Configure(ctx context.Context, req resource.Confi
}

func buildValidateConfig(criteria basetypes.SetValue, resp *resource.ValidateConfigResponse) {
if criteria.IsNull() || criteria.IsUnknown() {
return
}

criteriaObj := criteria.Elements()[0].(types.Object)
criteriaAttrs := criteriaObj.Attributes()

anyBuild := criteriaAttrs["any_build"].(types.Bool).ValueBool()
anyBuild := criteriaAttrs["any_build"].(types.Bool)
selectedBuilds := criteriaAttrs["selected_builds"].(types.Set)
includePatterns := criteriaAttrs["include_patterns"].(types.Set)

if anyBuild.IsUnknown() || selectedBuilds.IsUnknown() || includePatterns.IsUnknown() {
return
}

if !anyBuild && len(criteriaAttrs["selected_builds"].(types.Set).Elements()) == 0 && len(criteriaAttrs["include_patterns"].(types.Set).Elements()) == 0 {
if !anyBuild.ValueBool() && len(selectedBuilds.Elements()) == 0 && len(includePatterns.Elements()) == 0 {
resp.Diagnostics.AddAttributeError(
path.Root("criteria").AtSetValue(criteriaObj).AtName("any_build"),
"Invalid Attribute Configuration",
Expand Down Expand Up @@ -281,6 +291,9 @@ func fromBuildAPIModel(ctx context.Context, criteriaAPIModel map[string]interfac
buildCriteriaSetResourceModelElementTypes,
[]attr.Value{criteria},
)
if d.HasError() {
diags.Append(d...)
}

return criteriaSet, diags
}
Expand All @@ -291,6 +304,9 @@ func (m *BuildWebhookResourceModel) fromAPIModel(ctx context.Context, apiModel W
criteriaAPIModel := apiModel.EventFilter.Criteria.(map[string]interface{})

baseCriteriaAttrs, d := m.WebhookResourceModel.fromBaseCriteriaAPIModel(ctx, criteriaAPIModel)
if d.HasError() {
diags.Append(d...)
}

criteriaSet, d := fromBuildAPIModel(ctx, criteriaAPIModel, baseCriteriaAttrs)
if d.HasError() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,21 @@ func (r *ReleaseBundleWebhookResource) Configure(ctx context.Context, req resour
}

func releaseBundleValidateConfig(criteria basetypes.SetValue, resp *resource.ValidateConfigResponse) {
if criteria.IsNull() || criteria.IsUnknown() {
return
}

criteriaObj := criteria.Elements()[0].(types.Object)
criteriaAttrs := criteriaObj.Attributes()

anyReleaseBundle := criteriaAttrs["any_release_bundle"].(types.Bool).ValueBool()
anyReleaseBundle := criteriaAttrs["any_release_bundle"].(types.Bool)
registeredReleaseBundleNames := criteriaAttrs["registered_release_bundle_names"].(types.Set)

if !anyReleaseBundle && len(criteriaAttrs["registered_release_bundle_names"].(types.Set).Elements()) == 0 {
if anyReleaseBundle.IsUnknown() || registeredReleaseBundleNames.IsUnknown() {
return
}

if !anyReleaseBundle.ValueBool() && len(registeredReleaseBundleNames.Elements()) == 0 {
resp.Diagnostics.AddAttributeError(
path.Root("criteria").AtSetValue(criteriaObj).AtName("any_release_bundle"),
"Invalid Attribute Configuration",
Expand Down Expand Up @@ -329,6 +338,9 @@ func (m *ReleaseBundleWebhookResourceModel) fromAPIModel(ctx context.Context, ap
criteriaAPIModel := apiModel.EventFilter.Criteria.(map[string]interface{})

baseCriteriaAttrs, d := m.WebhookResourceModel.fromBaseCriteriaAPIModel(ctx, criteriaAPIModel)
if d.HasError() {
diags.Append(d...)
}

criteriaSet, d := fromReleaseBundleCriteriaAPIModel(ctx, criteriaAPIModel, baseCriteriaAttrs)
if d.HasError() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,21 @@ func (r *ReleaseBundleV2WebhookResource) Configure(ctx context.Context, req reso
}

func releaseBundleV2ValidatConfig(criteria basetypes.SetValue, resp *resource.ValidateConfigResponse) {
if criteria.IsNull() || criteria.IsUnknown() {
return
}

criteriaObj := criteria.Elements()[0].(types.Object)
criteriaAttrs := criteriaObj.Attributes()

anyReleaseBundle := criteriaAttrs["any_release_bundle"].(types.Bool).ValueBool()
anyReleaseBundle := criteriaAttrs["any_release_bundle"].(types.Bool)
selectedReleaseBundles := criteriaAttrs["selected_release_bundles"].(types.Set)

if anyReleaseBundle.IsUnknown() || selectedReleaseBundles.IsUnknown() {
return
}

if !anyReleaseBundle && len(criteriaAttrs["selected_release_bundles"].(types.Set).Elements()) == 0 {
if !anyReleaseBundle.ValueBool() && len(selectedReleaseBundles.Elements()) == 0 {
resp.Diagnostics.AddAttributeError(
path.Root("criteria").AtSetValue(criteriaObj).AtName("any_release_bundle"),
"Invalid Attribute Configuration",
Expand Down Expand Up @@ -279,6 +288,9 @@ func fromReleaseBundleV2APIModel(ctx context.Context, criteriaAPIModel map[strin
releaseBundleV2CriteriaSetResourceModelElementTypes,
[]attr.Value{criteria},
)
if d.HasError() {
diags.Append(d...)
}

return
}
Expand All @@ -289,6 +301,9 @@ func (m *ReleaseBundleV2WebhookResourceModel) fromAPIModel(ctx context.Context,
criteriaAPIModel := apiModel.EventFilter.Criteria.(map[string]interface{})

baseCriteriaAttrs, d := m.WebhookResourceModel.fromBaseCriteriaAPIModel(ctx, criteriaAPIModel)
if d.HasError() {
diags.Append(d...)
}

criteriaSet, d := fromReleaseBundleV2APIModel(ctx, criteriaAPIModel, baseCriteriaAttrs)
if d.HasError() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ func (m *ReleaseBundleV2PromotionWebhookResourceModel) fromAPIModel(ctx context.
criteriaAPIModel := apiModel.EventFilter.Criteria.(map[string]interface{})

baseCriteriaAttrs, d := m.WebhookResourceModel.fromBaseCriteriaAPIModel(ctx, criteriaAPIModel)
if d.HasError() {
diags.Append(d...)
}

criteriaSet, d := fromReleaseBundleV2PromotionAPIModel(ctx, criteriaAPIModel, baseCriteriaAttrs)
if d.HasError() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,16 @@ func (r RepoWebhookResource) ValidateConfig(ctx context.Context, req resource.Va
criteriaObj := data.Criteria.Elements()[0].(types.Object)
criteriaAttrs := criteriaObj.Attributes()

anyLocal := criteriaAttrs["any_local"].(types.Bool).ValueBool()
anyRemote := criteriaAttrs["any_remote"].(types.Bool).ValueBool()
anyFederated := criteriaAttrs["any_federated"].(types.Bool).ValueBool()
anyLocal := criteriaAttrs["any_local"].(types.Bool)
anyRemote := criteriaAttrs["any_remote"].(types.Bool)
anyFederated := criteriaAttrs["any_federated"].(types.Bool)
repoKeys := criteriaAttrs["repo_keys"].(types.Set)

if (!anyLocal && !anyRemote && !anyFederated) && len(criteriaAttrs["repo_keys"].(types.Set).Elements()) == 0 {
if anyLocal.IsUnknown() || anyRemote.IsUnknown() || anyFederated.IsUnknown() || repoKeys.IsUnknown() {
return
}

if (!anyLocal.ValueBool() && !anyRemote.ValueBool() && !anyFederated.ValueBool()) && len(repoKeys.Elements()) == 0 {
resp.Diagnostics.AddAttributeError(
path.Root("criteria").AtSetValue(criteriaObj).AtName("repo_keys"),
"Invalid Attribute Configuration",
Expand Down Expand Up @@ -257,6 +262,9 @@ func (m RepoWebhookResourceModel) toAPIModel(ctx context.Context, domain string,
}

criteriaAPIModel, d := toRepoCriteriaAPIModel(ctx, baseCriteria, criteriaAttrs)
if d.HasError() {
diags.Append(d...)
}

d = m.WebhookResourceModel.toAPIModel(ctx, domain, criteriaAPIModel, apiModel)
if d.HasError() {
Expand Down Expand Up @@ -323,6 +331,9 @@ func (m *RepoWebhookResourceModel) fromAPIModel(ctx context.Context, apiModel We
criteriaAPIModel := apiModel.EventFilter.Criteria.(map[string]interface{})

baseCriteriaAttrs, d := m.WebhookResourceModel.fromBaseCriteriaAPIModel(ctx, criteriaAPIModel)
if d.HasError() {
diags.Append(d...)
}

criteriaSet, d := fromRepoCriteriaAPIMode(ctx, criteriaAPIModel, baseCriteriaAttrs)
if d.HasError() {
Expand Down
Loading