Skip to content

Commit

Permalink
Added validation for project name
Browse files Browse the repository at this point in the history
Signed-off-by: hiteshwani29 <hiteshwani29@gmail.com>
  • Loading branch information
hiteshwani29 committed Mar 21, 2023
1 parent 74f1928 commit 7f9a6be
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
All notable changes to this project will be documented in this file.

## Unreleased
## Fixed
- Fix: add project name validation [hiteshwani29](https://github.com/hiteshwani29)

## [0.2.1] - 2023-02-24
### Added
Expand Down
10 changes: 9 additions & 1 deletion pkg/common/constants.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package common

import "time"
import (
"regexp"
"time"
)

// environment variables for configuration
const (
Expand Down Expand Up @@ -49,3 +52,8 @@ const (

var SessionDataKey contextKey
var SessionInternalKey contextKey

// Regex
var (
PrjNameRX = regexp.MustCompile(`^[a-zA-Z][-a-zA-Z0-9]*[a-zA-Z0-9]$`)
)
7 changes: 7 additions & 0 deletions pkg/service/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package service
import (
"context"
"database/sql"
"errors"
"fmt"
"time"

"github.com/google/uuid"
cdao "github.com/paralus/paralus/internal/cluster/dao"
"github.com/paralus/paralus/internal/dao"
"github.com/paralus/paralus/internal/models"
"github.com/paralus/paralus/pkg/common"
authzv1 "github.com/paralus/paralus/proto/types/authz"
commonv3 "github.com/paralus/paralus/proto/types/commonpb/v3"
v3 "github.com/paralus/paralus/proto/types/commonpb/v3"
Expand Down Expand Up @@ -60,6 +62,11 @@ func (s *projectService) Create(ctx context.Context, project *systemv3.Project)
return nil, fmt.Errorf("missing organization in metadata")
}

matched := common.PrjNameRX.MatchString(project.Metadata.GetName())
if !matched {
return nil, errors.New("project name contains invalid characters. Valid characters are alphanumeric and hyphen, except at the beginning or the end")
}

var org models.Organization
_, err := dao.GetByName(ctx, s.db, project.Metadata.Organization, &org)
if err != nil {
Expand Down

0 comments on commit 7f9a6be

Please sign in to comment.