Skip to content

Rich Parameter validation is skipped if min or max is 0. #7582

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

Closed
spikecurtis opened this issue May 17, 2023 · 6 comments · Fixed by #7755
Closed

Rich Parameter validation is skipped if min or max is 0. #7582

spikecurtis opened this issue May 17, 2023 · 6 comments · Fixed by #7755
Assignees
Milestone

Comments

@spikecurtis
Copy link
Contributor

If you set validation.min or validation.max to 0 in a rich parameter, we skip validation.

This makes it impossible to correctly enforce a parameter range where 0 is the min or max, e.g. 0-9.

Spotted during a code read:

func validationEnabled(param TemplateVersionParameter) bool {

Unfortunately this needs to be resolved at the protobuf / data persistence layer, since we only store the min and max, and not whether either or both were explicitly set in the terraform.

cc @mtojek

@mtojek
Copy link
Member

mtojek commented May 17, 2023

Thanks for linking the source code. It is a valid concern. As a short-term mitigation, we can modify the condition:
(param.ValidationMin == 0 && param.ValidationMax != 0) || (param.ValidationMin != 0 && param.ValidationMax == 0)

It won't cover the edge case Min = Max = 0, but it seems to be rare. Do you think that it is worth applying it in your PR, @spikecurtis?

Nevertheless, I agree that the proper fix requires changes in the persistence layer (nullable column).

@spikecurtis
Copy link
Contributor Author

I agree that Min = Max = 0 isn't an important case, as the parameter then is just a fixed value.

However, (param.ValidationMin == 0 && param.ValidationMax != 0) || (param.ValidationMin != 0 && param.ValidationMax == 0) has the drawback that it's hard express a min without a max or vice versa, e.g. v >= 0.

In practice there is almost always a min or max, e.g. max int32 or whatever, but it's kind of obnoxious to force template authors to provide both.

I think we should just do the proper fix in a separate PR and not absorb it into the one I'm currently working on.

@mtojek
Copy link
Member

mtojek commented May 17, 2023

I think we should just do the proper fix in a separate PR and not absorb it into the one I'm currently working on.

👍

Fortunately, the fix should not have implications on the API compatibility (/rich-parameters) as ValidationMin and ValidationMax have omitempty annotation in:

	ValidationMin        int32                            `json:"validation_min,omitempty"`
	ValidationMax        int32                            `json:"validation_max,omitempty"`

@ammario ammario added this to the 🧹 Sprint 0 milestone May 22, 2023
@ammario ammario removed the sprint-0 label May 22, 2023
@mtojek mtojek self-assigned this May 26, 2023
@mtojek
Copy link
Member

mtojek commented May 26, 2023

FYI: I'm working on fixing this issue.

However, (param.ValidationMin == 0 && param.ValidationMax != 0) || (param.ValidationMin != 0 && param.ValidationMax == 0) has the drawback that it's hard express a min without a max or vice versa, e.g. v >= 0.

The problem has its roots in the terraform-provider-coder, which assumes that both properties (min and max) come together.

@mtojek
Copy link
Member

mtojek commented May 30, 2023

Resolved in coder/terraform-provider-coder#122

@mtojek mtojek closed this as completed May 30, 2023
@mtojek mtojek reopened this May 30, 2023
@mtojek
Copy link
Member

mtojek commented May 30, 2023

I'm afraid that I found another issue. When terraform is resolving the plan it ends up with zero-populated values, which is odd as parameterDataSource() fixes all zero -> nil values.

Fragment of the .tfplan file:

{
            "address": "data.coder_parameter.compute_instances_7",
            "mode": "data",
            "type": "coder_parameter",
            "name": "compute_instances_7",
            "provider_name": "registry.terraform.io/coder/coder",
            "schema_version": 0,
            "values": {
              "default": "3",
              "description": "Let's set the expected number of instances.",
              "display_name": null,
              "icon": "/icon/rubymine.svg",
              "id": "a7564661-442f-4ee6-a3d0-49fcdda08839",
              "legacy_variable": null,
              "legacy_variable_name": null,
              "mutable": true,
              "name": "Compute instances",
              "option": null,
              "optional": true,
              "type": "number",
              "validation": [
                {
                  "error": "",
                  "max": 8,
                  "min": 0,
                  "monotonic": "increasing",
                  "regex": ""
                }
              ],
              "value": "3"
            },
            "sensitive_values": {
              "validation": [
                {}
              ]
            }
          }

I'm investigating it. It doesn't make sense as display_name is null, legacy_variable_name too, but min not...

BTW Is that what you meant @mafredri?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants