Skip to content

Commit

Permalink
fix: add validation for subscription tags (#336)
Browse files Browse the repository at this point in the history
* fix: add validation for subscription tags
Fixes #332

* fix: use alltrue to test length individually
  • Loading branch information
matt-FFFFFF authored Feb 16, 2024
1 parent 767ee55 commit 2523521
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
12 changes: 12 additions & 0 deletions modules/subscription/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,18 @@ subscription_tags = {
```
DESCRIPTION
default = {}
validation {
error_message = "Tag values must contain neither `<>%&\\?/` nor control characters, and must be between 0-256 characters."
condition = alltrue(
[for _, v in var.subscription_tags : can(regex("^[^<>%&\\?/[:cntrl:]]{0,256}$", v))]
)
}
validation {
error_message = "Tag name must contain neither `<>%&\\?/` nor control characters, and must be between 0-512 characters."
condition = alltrue(
[for k, _ in var.subscription_tags : can(regex("^[^<>%&\\?/[:cntrl:]]{0,512}$", k))]
)
}
}

variable "subscription_use_azapi" {
Expand Down
27 changes: 27 additions & 0 deletions tests/subscription/subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,33 @@ func TestSubscriptionAliasCreateInvalidManagementGroupIdLength(t *testing.T) {
assert.Contains(t, utils.SanitiseErrorMessage(err), "The management group ID must be between 1 and 90 characters in length and formed of the following characters: a-z, A-Z, 0-9, -, _, (, ), and a period (.).")
}

func TestSubscriptionInvalidTagValue(t *testing.T) {
t.Parallel()

v := getMockInputVariables()
v["subscription_tags"] = map[string]any{
"illegal-value": "illegal-<-value",
}
test, err := setuptest.Dirs(moduleDir, "").WithVars(v).InitPlanShowWithPrepFunc(t, utils.AzureRmAndRequiredProviders)
defer test.Cleanup()
assert.Contains(t, utils.SanitiseErrorMessage(err), "Tag values must contain neither `<>%&\\?/` nor control characters, and must be between 0-256 characters.")
}

func TestSubscriptionInvalidTagName(t *testing.T) {
t.Parallel()
var tagname string
for i := 0; i < 513; i++ {
tagname += "a"
}
v := getMockInputVariables()
v["subscription_tags"] = map[string]any{
tagname: "illegal-name",
}
test, err := setuptest.Dirs(moduleDir, "").WithVars(v).InitPlanShowWithPrepFunc(t, utils.AzureRmAndRequiredProviders)
defer test.Cleanup()
assert.Contains(t, utils.SanitiseErrorMessage(err), "Tag name must contain neither `<>%&\\?/` nor control characters, and must be between 0-512 characters.")
}

// getMockInputVariables returns a set of mock input variables that can be used and modified for testing scenarios.
func getMockInputVariables() map[string]any {
return map[string]any{
Expand Down

0 comments on commit 2523521

Please sign in to comment.