mondoohq / terraform-provider-mondoo

Terraform Mondoo provider
https://registry.terraform.io/providers/mondoohq/mondoo
Other
5 stars 1 forks source link

Validate input against Org / Space Name Spec #92

Open ehaselwanter opened 4 months ago

ehaselwanter commented 4 months ago

The space Ressource needs some input validation to meet the platform requirements

To Reproduce

│ Error: Client Error
│
│   with mondoo_space.spaces["macos_clients"],
│   on mondoo.tf line 3, in resource "mondoo_space" "spaces":
│    3: resource "mondoo_space" "spaces" {
│
│ Unable to create space, got error: rpc error: code = InvalidArgument desc =
│ space name is not valid

with

# mondoo_space.spaces["macos_clients"] will be created
  + resource "mondoo_space" "spaces" {
  + id     = (known after apply)
  + mrn    = (known after apply)
  + name   = "Zaatars solutions MacOS clients"
  + org_id = "whatever-613383"
}

Expected behavior

validate against:

Org / Space Name: name: letters, numbers, single quotes, hyphens, spaces or exclamation points, more then 4 chars Regex: ^([a-zA-Z \-'_]|\d){2,30}$

chris-rock commented 4 months ago

The solution is described in https://developer.hashicorp.com/terraform/plugin/framework/validation, we need to implement an attribute validation:

// Typically within the schema.Schema returned by Schema() for a provider,
// resource, or data source.
schema.StringAttribute{
    // ... other Attribute configuration ...

    Validators: []validator.String{
        // These are example validators from terraform-plugin-framework-validators
        stringvalidator.LengthBetween(10, 256),
        stringvalidator.RegexMatches(
            regexp.MustCompile(`^[a-z0-9]+$`),
            "must contain only lowercase alphanumeric characters",
        ),
    },
}
chris-rock commented 3 months ago

We should use the following regex:

IDs: ^[a-z]([\d-_]|[a-z]){4,33}[a-z\d]$ Name: ^([a-zA-Z \-'_]|\d){2,30}$

mati007thm commented 2 months ago

@chris-rock According to the picture, the validation should be ^[a-z]([\d-_]|[a-z]){6,35}[a-z\d]$ or am I misunderstanding something?

image

Another question that we are unsure of, if there should be a validation of other fields (e.g. Name of an integration), since no validation applies in the UI?

image
Pauti commented 2 months ago

https://github.com/mondoohq/terraform-provider-mondoo/pull/114

Pauti commented 2 months ago

For instance, we tried to exceed 256 characters in the name string, which resulted in the error: Unable to create Domain integration, got error: client: validator failed for field "IntegrationsManager.name": value is greater than the required length at runtime (In the UI and with Terraform). However, there was no limitation on special characters.