Skip to content

Commit

Permalink
#326: review redirect validation regexes (#327)
Browse files Browse the repository at this point in the history
* Review redirect validation regexes

* Add unit test check

* Re-enabling import verification
  • Loading branch information
fformica authored Aug 23, 2024
1 parent 4a6a048 commit ff77aa1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 2.4.1 (August 23, 2024)
* Fixed validation regex for redirects

## 2.4.0 (July 22, 2024)
* Remove ns1_subnet provider, used for dhcp/ipam resources

Expand Down
2 changes: 1 addition & 1 deletion ns1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

var (
clientVersion = "2.4.0"
clientVersion = "2.4.1"
providerUserAgent = "tf-ns1" + "/" + clientVersion
defaultRetryMax = 3
)
Expand Down
6 changes: 3 additions & 3 deletions ns1/resource_redirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ func RedirectCertUpdate(d *schema.ResourceData, meta interface{}) error {
func validateDomain(val interface{}, key string) (warns []string, errs []error) {
v := val.(string)

match, err := regexp.MatchString("^(\\*\\.)?([\\w-]+\\.)*[\\w-]+$", v)
match, err := regexp.MatchString("^(\\*\\.)?([a-zA-Z0-9\\-]+\\.)+[a-zA-Z0-9]+$", v)
if err != nil {
errs = append(errs, fmt.Errorf("%s is invalid, got: %s, error: %e", key, v, err))
}
Expand All @@ -354,7 +354,7 @@ func validateDomain(val interface{}, key string) (warns []string, errs []error)
func validatePath(val interface{}, key string) (warns []string, errs []error) {
v := val.(string)

match, err := regexp.MatchString("^[*]?[a-zA-Z0-9\\.\\-/$!+(_)' ]+[*]?$", v)
match, err := regexp.MatchString("^[*]?[a-zA-Z0-9\\.\\-/_~%%]+[*]?$", v)
if err != nil {
errs = append(errs, fmt.Errorf("%s is invalid, got: %s, error: %e", key, v, err))
}
Expand All @@ -370,7 +370,7 @@ func validatePath(val interface{}, key string) (warns []string, errs []error) {
func validateURL(val interface{}, key string) (warns []string, errs []error) {
v := val.(string)

match, err := regexp.MatchString("^(http://|https://)?[a-zA-Z0-9\\.\\-/$!+(_)' ]+$", v)
match, err := regexp.MatchString("^(http://|https://)?[a-zA-Z0-9\\-\\.]+(:\\d+)?(/[a-zA-Z0-9\\.\\-/_~%%:]*)?(\\?[a-zA-Z0-9\\.\\-/_~%%=+&#]+)?(#[a-zA-Z0-9\\.\\-/_~%%]+)?$", v)
if err != nil {
errs = append(errs, fmt.Errorf("%s is invalid, got: %s, error: %e", key, v, err))
}
Expand Down
16 changes: 15 additions & 1 deletion ns1/resource_redirect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func TestAccRedirectConfig_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckRedirectConfigExists("ns1_redirect.it", &redirect),
testAccCheckRedirectConfigDomain(&redirect, "test."+domainName),
testAccCheckRedirectConfigTarget(&redirect, "https://url.com/target/path"),
testAccCheckRedirectConfigFwType(&redirect, "masking"),
testAccCheckRedirectConfigTags(&redirect, []string{"test", "it"}),
testAccCheckRedirectConfigHTTPS(&redirect, true),
Expand All @@ -40,6 +41,7 @@ func TestAccRedirectConfig_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckRedirectConfigExists("ns1_redirect.it", &redirect),
testAccCheckRedirectConfigDomain(&redirect, "test."+domainName),
testAccCheckRedirectConfigTarget(&redirect, "https://url.com/target/path?q=param#frag"),
testAccCheckRedirectConfigFwType(&redirect, "permanent"),
testAccCheckRedirectConfigTags(&redirect, []string{}),
testAccCheckRedirectConfigHTTPS(&redirect, true),
Expand Down Expand Up @@ -75,6 +77,7 @@ func TestAccRedirectConfig_http_to_https(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckRedirectConfigExists("ns1_redirect.it", &redirect),
testAccCheckRedirectConfigDomain(&redirect, "test."+domainName),
testAccCheckRedirectConfigTarget(&redirect, "https://url.com/target/path"),
testAccCheckRedirectConfigFwType(&redirect, "permanent"),
testAccCheckRedirectConfigTags(&redirect, []string{}),
testAccCheckRedirectConfigHTTPS(&redirect, false),
Expand All @@ -86,6 +89,7 @@ func TestAccRedirectConfig_http_to_https(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckRedirectConfigExists("ns1_redirect.it", &redirect),
testAccCheckRedirectConfigDomain(&redirect, "test."+domainName),
testAccCheckRedirectConfigTarget(&redirect, "https://url.com/target/path?q=param#frag"),
testAccCheckRedirectConfigFwType(&redirect, "permanent"),
testAccCheckRedirectConfigTags(&redirect, []string{}),
testAccCheckRedirectConfigHTTPS(&redirect, true),
Expand All @@ -97,6 +101,7 @@ func TestAccRedirectConfig_http_to_https(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckRedirectConfigExists("ns1_redirect.it", &redirect),
testAccCheckRedirectConfigDomain(&redirect, "test."+domainName),
testAccCheckRedirectConfigTarget(&redirect, "https://url.com/target/path"),
testAccCheckRedirectConfigFwType(&redirect, "permanent"),
testAccCheckRedirectConfigTags(&redirect, []string{}),
testAccCheckRedirectConfigHTTPS(&redirect, true),
Expand Down Expand Up @@ -210,7 +215,7 @@ resource "ns1_redirect" "it" {
certificate_id = "${ns1_redirect_certificate.example.id}"
domain = "test.${ns1_zone.test.zone}"
path = "/from/path/*"
target = "https://url.com/target/path"
target = "https://url.com/target/path?q=param#frag"
forwarding_mode = "capture"
forwarding_type = "permanent"
https_forced = true
Expand Down Expand Up @@ -343,6 +348,15 @@ func testAccCheckRedirectConfigDomain(cfg *redirect.Configuration, expected stri
}
}

func testAccCheckRedirectConfigTarget(cfg *redirect.Configuration, expected string) resource.TestCheckFunc {
return func(s *terraform.State) error {
if cfg.Target != expected {
return fmt.Errorf("Target: got: %s want: %s", cfg.Domain, expected)
}
return nil
}
}

func testAccCheckRedirectConfigFwType(cfg *redirect.Configuration, expected string) resource.TestCheckFunc {
return func(s *terraform.State) error {
if cfg.ForwardingType.String() != expected {
Expand Down

0 comments on commit ff77aa1

Please sign in to comment.