Skip to content

Commit

Permalink
feat: Add subnet and virtual network name validation (#376)
Browse files Browse the repository at this point in the history
Resolves #268 issue
  • Loading branch information
szymonnogiec authored Jul 21, 2022
1 parent c18b0ec commit b6ca3a4
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
10 changes: 7 additions & 3 deletions resources/types/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/multycloud/multy/resources"
"github.com/multycloud/multy/validate"
"net"
"regexp"
)

/*
Expand Down Expand Up @@ -49,9 +50,12 @@ func NewSubnet(s *Subnet, resourceId string, subnet *resourcespb.SubnetArgs, oth
}

func (r *Subnet) Validate(ctx resources.MultyContext) (errs []validate.ValidationError) {
//if vn.Name contains not letters,numbers,_,- { return false }
//if vn.Name length? { return false }
//if vn.AvailbilityZone valid { return false }
nameRestrictionRegex := regexp.MustCompile(validate.WordWithDotHyphenUnder80Pattern)
if !nameRestrictionRegex.MatchString(r.Args.Name) {
errs = append(errs, r.NewValidationError(fmt.Errorf("%s can contain only alphanumerics, underscores, periods, and hyphens;"+
" must start with alphanumeric and end with alphanumeric or underscore and have 1-80 lenght", r.ResourceId), "name"))
}

if len(r.Args.CidrBlock) == 0 { // max len?
errs = append(errs, r.NewValidationError(fmt.Errorf("%s cidr_block length is invalid", r.ResourceId), "cidr_block"))
}
Expand Down
11 changes: 8 additions & 3 deletions resources/types/virtual_network.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package types

import (
"fmt"
"github.com/multycloud/multy/api/proto/resourcespb"
"github.com/multycloud/multy/resources"
"github.com/multycloud/multy/validate"
"net"
"regexp"
)

/*
Expand Down Expand Up @@ -50,9 +52,12 @@ func NewVirtualNetwork(r *VirtualNetwork, resourceId string, vn *resourcespb.Vir

func (r *VirtualNetwork) Validate(ctx resources.MultyContext) (errs []validate.ValidationError) {
errs = append(errs, r.ResourceWithId.Validate()...)
//if r.Name contains not letters,numbers,_,- { return false }
//if r.Name length? { return false }
//if r.CidrBlock valid CIDR { return false }
nameRestrictionRegex := regexp.MustCompile(validate.WordWithDotHyphenUnder80Pattern)
if !nameRestrictionRegex.MatchString(r.Args.Name) {
errs = append(errs, r.NewValidationError(fmt.Errorf("%s can contain only alphanumerics, underscores, periods, and hyphens;"+
" must start with alphanumeric and end with alphanumeric or underscore and have 1-80 lenght", r.ResourceId), "name"))
}

if len(r.Args.CidrBlock) == 0 { // max len?
errs = append(errs, validate.ValidationError{
ErrorMessage: "cidr_block length is invalid",
Expand Down
5 changes: 5 additions & 0 deletions validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import (
"io/ioutil"
)

// WordWithDotHyphenUnder80Pattern is a regexp pattern that matches string that contain alphanumerics, underscores, periods,
// and hyphens that start with alphanumeric and End alphanumeric or underscore. Limits size to 1-80.
// Based on https://docs.microsoft.com/en-us/azure/azure-resource-manager/management/resource-name-rules
const WordWithDotHyphenUnder80Pattern = string(`^[a-zA-Z\d]$|^[a-zA-Z\d][\w\-.]{0,78}\w$`)

type ResourceValidationInfo struct {
SourceRanges map[string]hcl.Range
BlockDefRange hcl.Range
Expand Down
41 changes: 41 additions & 0 deletions validate/validate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package validate_test

import (
"github.com/multycloud/multy/validate"
"regexp"
"testing"
)

// TestWordWithDotHyphenUnder80Pattern checks whether validate.WordWithDotHyphenUnder80Pattern matches
// expected expressions
func TestWordWithDotHyphenUnder80Pattern(t *testing.T) {
testRegexp, err := regexp.Compile(validate.WordWithDotHyphenUnder80Pattern)
if err != nil {
t.Fatalf("Could not compile regex: %s", validate.WordWithDotHyphenUnder80Pattern)
}

shouldMatch := []string{
"a",
"9",
"aZ9",
"ThisIs67dots..................................................................._",
}
shouldntMatch := []string{
"",
"_",
"<someName",
"ThisIs68dots...................................................................._",
"Maybe?inThe.Middle_",
}

for _, name := range shouldMatch {
if !testRegexp.MatchString(name) {
t.Errorf("%s should match %s, but didn't", validate.WordWithDotHyphenUnder80Pattern, name)
}
}
for _, name := range shouldntMatch {
if testRegexp.MatchString(name) {
t.Errorf("%s shouldn't match %s, but did", validate.WordWithDotHyphenUnder80Pattern, name)
}
}
}

0 comments on commit b6ca3a4

Please sign in to comment.