Skip to content

feat: 2 parameter validation modes for create & import #381

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

Open
wants to merge 20 commits into
base: main
Choose a base branch
from

Conversation

Emyrk
Copy link
Member

@Emyrk Emyrk commented Apr 14, 2025

What this PR does

Parameter validation is now split into 2 modes: CODER_VALIDATION_MODE

  • Default used for creating a workspace
    • Primary goal is the validate the final parameter.value
  • template-import used for importing a template
    • Allows an undefined parameter.value. Will enforce validation on default and option.value.
default import-template
empty-value allowed_for_now allowed
invalid option allowed
invalid default allowed

Validation changes

See diff from main. Left side is the tests passing on main.
https://www.diffchecker.com/GaUlaTze/

  • Input values must be in the option set. Before this was allowed, but enforced in coder/coder
  • Number parameters require values to be numbers. Before this was allowed, but enforced in coder/coder
  • Valid options checked before validation block. So you will get "not a valid option" before you get "invalid regex"

Future work

  • Disallow empty values for the default mode:
    // TODO: When empty values want to be rejected, uncomment this.
    // coder/coder should update to use template import mode first,
    // before this is uncommented.
    //if value == nil && mode == ValidationModeDefault {
    // var empty string
    // value = &empty
    //}
  • coder/coder to set the correct validation mode
  • validate monotonic in the provider, passing in the old value via env var

@Emyrk
Copy link
Member Author

Emyrk commented Apr 14, 2025

@johnstcn @matifali should this pass a template import?

Before this PR, this would pass a terraform plan.

data "coder_parameter" "region" {
  name        = "region"
  description = "Which region would you like to deploy to?"
  type        = "string"
  order       = 1

  option {
    name  = "Europe"
    value = "eu"
  }
  option {
    name  = "United States"
    value = "us"
  }
}

EDIT: Test case here

| EmtyOpts | string,number | | | 1,2,3 | | | "" | false | |

This PR sets the error to:

Planning failed. Terraform encountered an error while generating this plan.

╷
│ Error: Value must be a valid option. The value is empty, did you forget to set it with a default or from user input?
│ 
│   with data.coder_parameter.region,
│   on main.tf line 21, in data "coder_parameter" "region":
│   21: data "coder_parameter" "region" {
│ 
│ the value "" must be defined as one of options

To fix this, either the value must be passed via an env var:

CODER_PARAMETER_c697d2981bf416569a16cfbcdec1542b5398f3cc77d2b905819aa99c46ecf6f6=us terraform plan

Or with a default:

data "coder_parameter" "region" {
  # ...
  default = "us"
  # ...
}

I feel like we should reject it? But that is a breaking change.

@matifali
Copy link
Member

matifali commented Apr 14, 2025

There are use cases where an admin may not want to set a default to force the user to choose an option. At least it was possible before this PR. I am not sure how many users were actually using it.

But I have seen requests to support coder_parameter without a default value better: #144

@johnstcn
Copy link
Member

johnstcn commented Apr 15, 2025

There are use cases where an admin may not want to set a default to force the user to choose an option.

This behaviour is problematic because you could end up with a plan that does not correspond to the apply, depending on how the value of the parameter is used.

Would marking the parameter as ephemeral be a possible workaround here?

EDIT: marking a parameter as ephemeral requires it to be mutable and to also have a default set. This may not suit certain use-cases.

@matifali
Copy link
Member

matifali commented Apr 15, 2025

Do we require a user input if the parameters are ephemeral? Do we not allow setting a default value for such parameters?
Do such ephemeral parameters support options too?

If the answers are yes, then we just need better docs on how to fulfill this use case.

So that people don't try to use the existing non ephemeral parameters.

@Emyrk
Copy link
Member Author

Emyrk commented Apr 29, 2025

@matifali @johnstcn

How I see it is the relaxed validation on template import is incorrect. If a parameter has 0 values, but will have some value at workspace create, then the terraform plan could be incorrect.

However, I understand the use case. The provider SDK we are using does not indicate anywhere if terraform plan vs terraform apply is being run. So there is no way to conditionally enforce validation that way.

My gut thought is that we need to effectively have 2 modes of validation. 1 for template import, 1 for workspace create, where the latter is strict. The strict validation is the most correct.

What if we pass through an env var a relaxed validation mode? coder/coder can pass CODER_VALIDATION=relaxed when doing a template import. Then the provider can accept null values.

@johnstcn
Copy link
Member

What if we pass through an env var a relaxed validation mode? coder/coder can pass CODER_VALIDATION=relaxed when doing a template import. Then the provider can accept null values.

I think this approach could work, but we'd want to perhaps tie its behaviour specifically to what "operation" we're doing in Coder. We might also want to be wary of folks just setting this env var on the provisioner to work around null values entirely.

@Emyrk
Copy link
Member Author

Emyrk commented Apr 30, 2025

I think this approach could work, but we'd want to perhaps tie its behaviour specifically to what "operation" we're doing in Coder. We might also want to be wary of folks just setting this env var on the provisioner to work around null values entirely.

Yes, template import would have to pass some env var to change the behavior.

As for folks setting this env var at home, I think that would just be on them. By default the validation should be handled correctly in coder (when to be relaxed, and when not)

@Emyrk Emyrk force-pushed the stevenmasley/value_validate branch from a61664c to 5e19e2a Compare April 30, 2025 20:46
Emyrk added 3 commits April 30, 2025 16:10
Some validation changes:
- Invalid options rejected (not in option set)
@Emyrk Emyrk changed the title feat: validate user input values the same way as "default" feat: stricter parameter input validation May 1, 2025
@Emyrk Emyrk marked this pull request as ready for review May 1, 2025 19:17
@Emyrk Emyrk changed the title feat: stricter parameter input validation feat: 2 parameter validation modes for create & import May 1, 2025
@Emyrk Emyrk requested review from johnstcn and spikecurtis May 1, 2025 21:12
@spikecurtis
Copy link
Contributor

As for folks setting this env var at home, I think that would just be on them. By default the validation should be handled correctly in coder (when to be relaxed, and when not)

If provisionerd always sets the environment variable before exec'ing terraform, then I don't think we need to worry about users setting it.

Copy link
Contributor

@spikecurtis spikecurtis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems very odd to me that in this PR, template-import mode, which is supposedly being created for back-compatibility is more strict than what you're calling default mode.

Shouldn't it be the other way around? That template-import mode skips some checks that would break backward compatibility?

},
}
}
}
}

if len(v.Validation) == 1 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there somewhere we enforce that there can only be a max of one Validation?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is here in the schema:

You get this error:

│ Error: Too many validation blocks
│ 
│   on main.tf line 38, in data "coder_parameter" "project":
│   38:   validation {
│ 
│ No more than 1 "validation" blocks are allowed

return "", diags
}

if mode == ValidationModeTemplateImport && v.Default != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a weird case where template import is more strict than default. What is the justification for that? I thought we were only making template import less strict so that we don't break people.

Copy link
Member Author

@Emyrk Emyrk May 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct. When we turn this on in coderd, new templates imported will be subject to a stricter validation. There is 2 reasons.

Strict options

You can currently add options to your parameters that are invalid. That is a bit confusing on the UI, when options exist, but if you select them, it would fail to build.

At create time, the options are ignored, since they are not used.

Strict Default

This was actually done before too iff no input value was given (which is usually the case for template-import). I made this more explicit, where the Default has to be valid if set. Even if you passed in an input value at import time.

I enforce this, because if you do not, then a user who leaves a default value for a form has an invalid value and cannot build.


Sidenote, but if you want to place a value in the UI without it being valid, we are adding styling:

data "coder_parameter" "numbers" {
  name        = "numebrs"
  type        = "number"
  order       = 1

  styling = jsonencode({
    placeholder = "Enter a number"
  })
}

//if value == nil && mode == ValidationModeDefault {
// var empty string
// value = &empty
//}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This business wouldn't be necessary if the provider assumes the "looser" validation mode if unset. That is, there are 2 modes: WorkspaceCreate and TemplateImport, which in future we will explicitly set via environment variable. If unset, we assume TemplateImport, which has no breaking changes from current behavior.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this and did the early return instead as you suggested.

TemplateImport actually has more breaking changes than create in the end. create is backwards compatible because imported templates already exist and must still work.

Newly imported templates (once coderd switches), is going to require every option to also be valid. By doing this, the workspace create form will only present valid options to the user.

Eventually I would like to disallow an empty input value at create entirely.

@Emyrk
Copy link
Member Author

Emyrk commented May 2, 2025

It seems very odd to me that in this PR, template-import mode, which is supposedly being created for back-compatibility is more strict than what you're calling default mode.

Shouldn't it be the other way around? That template-import mode skips some checks that would break backward compatibility?

Maybe I should have broken this into 2 PRs. My goal is to make existing imported templates backwards compatible. They will function as they used to.

Future imported templates I am closing some of the validation holes.

And the backwards feature I am keeping, is allowing empty an default value. So this is still valid:

data "coder_parameter" "region" {
  name        = "region"
  description = "Which region would you like to deploy to?"
  type        = "string"
  order       = 1

  option {
    name  = "Europe"
    value = "eu"
  }
}

The other changes I made are fixing validation bugs. Meaning validation used to be skipped on present values.


Validating input values is in the option set

This is in coder/coder. This change shifts some of the validation in coder/coder -> terraform-provider-coder.

This is now invalid except at template-import, since no input values are given. So at template-import, there is no change in behavior here.

# Using input value 3
# CODER_PARAMETER_f3c7807d475073ba009bf4801b2d934e9f0126cb96dd19a27dbffcae23a7f5a3=3 terraform plan

data "coder_parameter" "numbers" {
  name        = "numebrs"
  type        = "number"
  order       = 1

  option {
    name  = "Five"
    value = "5"
  }
}

Validating option values

This used to pass, and it will continue to pass create so that previous templates still build if they have been imported. I cannot break existing imported templates.

For future template-imports, I throw an error. I think this fix can be seen as a bug fix. Otherwise, dropdown options in the UI form will fail when you select them.

data "coder_parameter" "numbers" {
  name        = "numebrs"
  type        = "number"
  order       = 1
  default = 7

  validation {
    min = 6
    max = 10
    error = "The number must be between 6 and 10"
  }

  option {
    name  = "Seven"
    value = "7"
  }

  option {
    name  = "Four"
    value = "4"
  }
}
│ Error: Option "Four": Invalid parameter value according to 'validation' block
│ 
│   with data.coder_parameter.numbers,
│   on main.tf line 10, in data "coder_parameter" "numbers":
│   10: data "coder_parameter" "numbers" {
│ 
│ The number must be between 6 and 10

@Emyrk
Copy link
Member Author

Emyrk commented May 2, 2025

If provisionerd always sets the environment variable before exec'ing terraform, then I don't think we need to worry about users setting it.

I agree, I was asked this in a voice chat. It can be set by users, but I don't imagine they will.

@Emyrk Emyrk requested a review from spikecurtis May 2, 2025 15:05
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 this pull request may close these issues.

4 participants