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

Commit

Permalink
Validate space name with a regexp in the controller (OSIO#3580)
Browse files Browse the repository at this point in the history
This will prevent creating spaces with a name that cannot be used
as a value in pod labels.
The regexp used in the controller replaces the validation function
that was previously used at the design level, which allows for returning
proper JSON-API errors to the clients.

Fixes openshiftio/openshift.io#3580

Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon committed May 24, 2018
1 parent 60b9d64 commit 553c482
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 193 deletions.
18 changes: 18 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,16 @@ func (c *SpaceController) Update(ctx *app.UpdateSpaceContext) error {
return ctx.OK(&response)
}

const (
spaceNamePattern string = `^([A-Za-z0-9][-A-Za-z0-9_.]{0-38})?[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 +567,13 @@ 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
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

0 comments on commit 553c482

Please sign in to comment.