Skip to content
This repository has been archived by the owner on Mar 11, 2021. It is now read-only.

Validate space name with a regexp in the controller (OSIO#3580) #2100

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions controller/space.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"net/url"
"regexp"

"github.com/fabric8-services/fabric8-wit/app"
"github.com/fabric8-services/fabric8-wit/application"
Expand Down Expand Up @@ -546,6 +547,18 @@ func (c *SpaceController) Update(ctx *app.UpdateSpaceContext) error {
return ctx.OK(&response)
}

const (
// see https://github.com/kubernetes/community/blob/master/contributors/design-proposals/architecture/identifiers.md
spaceNameMaxLength int = 63
Copy link
Contributor

Choose a reason for hiding this comment

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

@xcoulon space_max_length is hard coded at link. See if you can have shared variable here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, but I remove the use of the nameValidationFunction at the design level to validate the space name, actually. I'm not sure why the same length rule applies to area, etc., though...

spaceNamePattern string = `^([A-Za-z0-9][-A-Za-z0-9]*)?[A-Za-z0-9]$`
)

var nameRegex *regexp.Regexp

func init() {
nameRegex = regexp.MustCompile(spaceNamePattern) // will panic if the pattern is invalid
}

func validateCreateSpace(ctx *app.CreateSpaceContext) error {
if ctx.Payload.Data == nil {
return errors.NewBadParameterError("data", nil).Expected("not nil")
Expand All @@ -556,6 +569,16 @@ func validateCreateSpace(ctx *app.CreateSpaceContext) error {
if ctx.Payload.Data.Attributes.Name == nil {
return errors.NewBadParameterError("data.attributes.name", nil).Expected("not nil")
}
name := *ctx.Payload.Data.Attributes.Name
// now, verify the length and pattern for the space name
if len(name) > spaceNameMaxLength {
return errors.NewBadParameterError("data.attributes.name", name).Expected(fmt.Sprintf("max length: %d (was %d)", spaceNameMaxLength, len(name)))
}
matched := nameRegex.MatchString(name)
if !matched {
return errors.NewBadParameterError("data.attributes.name", name).Expected(fmt.Sprintf("matching '%s' pattern", spaceNamePattern))
}

// // TODO(kwk): Comment back in once space template is official
// if ctx.Payload.Data.Relationships == nil {
// return errors.NewBadParameterError("data.relationships", nil).Expected("not nil")
Expand Down
Loading